Skip to content

Commit eb5c9ba

Browse files
Merge pull request #30 from chence1219/main
update blufi src file
2 parents cbdb62d + 8c28455 commit eb5c9ba

File tree

12 files changed

+1439
-0
lines changed

12 files changed

+1439
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(blufi_app)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
idf_component_register(
2+
# 列出所有文件,顺序无所谓
3+
SRCS "blufi_app.cc"
4+
"blufi_wifi.c"
5+
"blufi_init.c"
6+
"blufi_security.c"
7+
"ssid_manager.cc"
8+
9+
INCLUDE_DIRS "."
10+
11+
# 告诉构建系统需要 C++ 标准库
12+
REQUIRES bt esp_wifi mbedtls nvs_flash app_update
13+
)
14+
15+
# # 如果 .cc 里用了 C++17/20 特性,可以指定标准
16+
# set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <stdio.h>
2+
#include "freertos/FreeRTOS.h"
3+
#include "freertos/task.h"
4+
#include "esp_log.h"
5+
#include "nvs_flash.h"
6+
#include <esp_mac.h>
7+
#include <esp_system.h>
8+
#include <esp_ota_ops.h>
9+
#include <esp_partition.h>
10+
#include <nvs_flash.h>
11+
#include "blufi_wifi.h"
12+
#include "ssid_manager.h"
13+
static const char *TAG = "BulfiApp";
14+
15+
extern "C" void app_main(void)
16+
{
17+
ESP_LOGI(TAG, "Starting BLUFI WiFi configuration...");
18+
/* 1. 初始化 NVS */
19+
esp_err_t ret = nvs_flash_init();
20+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
21+
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
22+
ESP_ERROR_CHECK(nvs_flash_erase());
23+
ESP_ERROR_CHECK(nvs_flash_init());
24+
}
25+
26+
std::string broad_name;
27+
// read nvs setting broad name
28+
nvs_handle_t nvs_handle = 0;
29+
nvs_open("board", NVS_READONLY, &nvs_handle);
30+
if (nvs_handle != 0) {
31+
size_t length = 0;
32+
if (nvs_get_str(nvs_handle, "name", nullptr, &length) == ESP_OK) {
33+
broad_name.resize(length);
34+
if (nvs_get_str(nvs_handle, "name", broad_name.data(), &length) == ESP_OK) {
35+
while (!broad_name.empty() && broad_name.back() == '\0') {
36+
broad_name.pop_back();
37+
}
38+
ESP_LOGI(TAG, "Read board name from nvs: %s", broad_name.c_str());
39+
} else {
40+
broad_name.clear();
41+
}
42+
}
43+
}
44+
45+
if (broad_name.empty()) {
46+
uint8_t mac[6];
47+
#if CONFIG_IDF_TARGET_ESP32P4
48+
esp_wifi_get_mac(WIFI_IF_AP, mac);
49+
#else
50+
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP));
51+
#endif
52+
char ssid[32];
53+
snprintf(ssid, sizeof(ssid), "%s-%02X%02X", "yunxin", mac[4], mac[5]);
54+
broad_name = std::string(ssid);
55+
}
56+
ESP_LOGI(TAG, "Broad name: %s", broad_name.c_str());
57+
58+
/* 2. 启动 Wi-Fi + Blufi */
59+
wifi_credential_t wifi_cred = initialise_wifi_and_blufi(broad_name.c_str());
60+
61+
if (wifi_cred.succ == 1) {
62+
ESP_LOGI(TAG, "BLUFI WiFi connected! SSID: %s", wifi_cred.ssid);
63+
SsidManager::GetInstance().AddSsid(wifi_cred.ssid, wifi_cred.password); //存储账号密码
64+
65+
/* 设置下一次启动分区为 ota_0(主工程的应用在ota_0) */
66+
const esp_partition_t *ota0 = esp_partition_find_first(
67+
ESP_PARTITION_TYPE_APP,
68+
ESP_PARTITION_SUBTYPE_APP_OTA_0,
69+
NULL);
70+
ESP_ERROR_CHECK( esp_ota_set_boot_partition(ota0) );
71+
} else {
72+
ESP_LOGE(TAG, "BLUFI WiFi connection failed");
73+
}
74+
75+
ESP_LOGI(TAG, "BLUFI WiFi configuration completed, restarting...");
76+
vTaskDelay(pdMS_TO_TICKS(500));
77+
// 执行重启
78+
esp_restart();
79+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
7+
8+
#pragma once
9+
10+
#include "freertos/FreeRTOS.h"
11+
#include "freertos/task.h"
12+
#include "freertos/event_groups.h"
13+
#include "esp_system.h"
14+
#include "esp_mac.h"
15+
#include "esp_wifi.h"
16+
#include "esp_event.h"
17+
#include "esp_log.h"
18+
#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
19+
#include "esp_bt.h"
20+
#endif
21+
#include "esp_blufi_api.h"
22+
#include "esp_blufi.h"
23+
#include "blufi_wifi.h"
24+
25+
#define BLUFI_CONFIG_TAG "BLUFI_CONFIG_WIFI"
26+
#define BLUFI_INFO(fmt, ...) ESP_LOGI(BLUFI_CONFIG_TAG, fmt, ##__VA_ARGS__)
27+
#define BLUFI_ERROR(fmt, ...) ESP_LOGE(BLUFI_CONFIG_TAG, fmt, ##__VA_ARGS__)
28+
29+
typedef struct {
30+
bool sta_connected;
31+
bool sta_got_ip;
32+
bool ble_connected;
33+
bool sta_is_connecting;
34+
uint8_t sta_bssid[6];
35+
uint8_t sta_ssid[32];
36+
int sta_ssid_len;
37+
wifi_sta_list_t sta_list;
38+
esp_blufi_extra_info_t sta_conn_info;
39+
char prefix[26];
40+
char device_name[32];
41+
wifi_config_t sta_config;
42+
wifi_config_t ap_config;
43+
uint8_t wifi_retry;
44+
EventGroupHandle_t wifi_result_group;
45+
EventGroupHandle_t wifi_event_group;
46+
} blufi_context_t;
47+
48+
void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_data, int *output_len, bool *need_free);
49+
int blufi_aes_encrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
50+
int blufi_aes_decrypt(uint8_t iv8, uint8_t *crypt_data, int crypt_len);
51+
uint16_t blufi_crc_checksum(uint8_t iv8, uint8_t *data, int len);
52+
53+
int blufi_security_init(void);
54+
void blufi_security_deinit(void);
55+
int esp_blufi_gap_register_callback(void);
56+
esp_err_t esp_blufi_host_init(void);
57+
esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *callbacks);
58+
esp_err_t esp_blufi_host_deinit(void);
59+
esp_err_t esp_blufi_controller_init(void);
60+
esp_err_t esp_blufi_controller_deinit(void);

0 commit comments

Comments
 (0)