blufi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /****************************************************************************
  7. * This is a demo for bluetooth config wifi connection to ap. You can config ESP32 to connect a softap
  8. * or config ESP32 as a softap to be connected by other device. APP can be downloaded from github
  9. * android source code: https://github.com/EspressifApp/EspBlufi
  10. * iOS source code: https://github.com/EspressifApp/EspBlufiForiOS
  11. ****************************************************************************/
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "freertos/event_groups.h"
  18. #include "esp_system.h"
  19. #include "esp_mac.h"
  20. #include "esp_wifi.h"
  21. #include "esp_event.h"
  22. #include "esp_log.h"
  23. #include "nvs_flash.h"
  24. #if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
  25. #include "esp_bt.h"
  26. #endif
  27. #include "esp_blufi_api.h"
  28. #include "ssid_manager_c.h"
  29. #include "blufi.h"
  30. #include "esp_blufi.h"
  31. #define CUSTOM_BLE_NAME_LEN (sizeof(CUSTOM_BLE_NAME)-1)
  32. #define EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY 10
  33. #define EXAMPLE_INVALID_REASON 255
  34. #define EXAMPLE_INVALID_RSSI -128
  35. #define CONFIG_ESP_WIFI_AUTH_WPA2_PSK 1
  36. #if CONFIG_ESP_WIFI_AUTH_OPEN
  37. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
  38. #elif CONFIG_ESP_WIFI_AUTH_WEP
  39. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
  40. #elif CONFIG_ESP_WIFI_AUTH_WPA_PSK
  41. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
  42. #elif CONFIG_ESP_WIFI_AUTH_WPA2_PSK
  43. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
  44. #elif CONFIG_ESP_WIFI_AUTH_WPA_WPA2_PSK
  45. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
  46. #elif CONFIG_ESP_WIFI_AUTH_WPA3_PSK
  47. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
  48. #elif CONFIG_ESP_WIFI_AUTH_WPA2_WPA3_PSK
  49. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
  50. #elif CONFIG_ESP_WIFI_AUTH_WAPI_PSK
  51. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
  52. #endif
  53. static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);
  54. #define WIFI_LIST_NUM 2
  55. #define NVS_STA_SSID_KEY "sta_ssid"
  56. #define NVS_STA_PASS_KEY "sta_pass"
  57. static wifi_config_t sta_config;
  58. static wifi_config_t ap_config;
  59. /* FreeRTOS event group to signal when we are connected & ready to make a request */
  60. static EventGroupHandle_t wifi_event_group;
  61. /* The event group allows multiple bits for each event,
  62. but we only care about one event - are we connected
  63. to the AP with an IP? */
  64. const int CONNECTED_BIT = BIT0;
  65. static uint8_t example_wifi_retry = 0;
  66. /* store the station info for send back to phone */
  67. static bool gl_sta_connected = false;
  68. static bool gl_sta_got_ip = false;
  69. static bool ble_is_connected = false;
  70. static uint8_t gl_sta_bssid[6];
  71. static uint8_t gl_sta_ssid[32];
  72. static int gl_sta_ssid_len;
  73. static wifi_sta_list_t gl_sta_list;
  74. static bool gl_sta_is_connecting = false;
  75. static esp_blufi_extra_info_t gl_sta_conn_info;
  76. static void example_record_wifi_conn_info(int rssi, uint8_t reason)
  77. {
  78. memset(&gl_sta_conn_info, 0, sizeof(esp_blufi_extra_info_t));
  79. if (gl_sta_is_connecting) {
  80. gl_sta_conn_info.sta_max_conn_retry_set = true;
  81. gl_sta_conn_info.sta_max_conn_retry = EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY;
  82. } else {
  83. gl_sta_conn_info.sta_conn_rssi_set = true;
  84. gl_sta_conn_info.sta_conn_rssi = rssi;
  85. gl_sta_conn_info.sta_conn_end_reason_set = true;
  86. gl_sta_conn_info.sta_conn_end_reason = reason;
  87. }
  88. }
  89. static void example_wifi_connect(void)
  90. {
  91. example_wifi_retry = 0;
  92. gl_sta_is_connecting = (esp_wifi_connect() == ESP_OK);
  93. example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON);
  94. }
  95. static bool example_wifi_reconnect(void)
  96. {
  97. bool ret;
  98. if (gl_sta_is_connecting && example_wifi_retry++ < EXAMPLE_WIFI_CONNECTION_MAXIMUM_RETRY) {
  99. BLUFI_INFO("BLUFI WiFi starts reconnection\n");
  100. gl_sta_is_connecting = (esp_wifi_connect() == ESP_OK);
  101. example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON);
  102. ret = true;
  103. } else {
  104. ret = false;
  105. }
  106. return ret;
  107. }
  108. static int softap_get_current_connection_number(void)
  109. {
  110. esp_err_t ret;
  111. ret = esp_wifi_ap_get_sta_list(&gl_sta_list);
  112. if (ret == ESP_OK)
  113. {
  114. return gl_sta_list.num;
  115. }
  116. return 0;
  117. }
  118. static void ip_event_handler(void* arg, esp_event_base_t event_base,
  119. int32_t event_id, void* event_data)
  120. {
  121. wifi_mode_t mode;
  122. switch (event_id) {
  123. case IP_EVENT_STA_GOT_IP: {
  124. esp_blufi_extra_info_t info;
  125. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  126. esp_wifi_get_mode(&mode);
  127. memset(&info, 0, sizeof(esp_blufi_extra_info_t));
  128. memcpy(info.sta_bssid, gl_sta_bssid, 6);
  129. info.sta_bssid_set = true;
  130. info.sta_ssid = gl_sta_ssid;
  131. info.sta_ssid_len = gl_sta_ssid_len;
  132. gl_sta_got_ip = true;
  133. if (ble_is_connected == true) {
  134. // 保存SSID和密码到NVS
  135. SsidManager* manager = ssid_manager_create();
  136. if (manager != NULL) {
  137. // 添加当前连接的SSID到管理器(顶部位置)
  138. ssid_manager_add_ssid(manager,(const char*)sta_config.sta.ssid , (const char*)sta_config.sta.password);
  139. // 保存到NVS
  140. ssid_manager_save_to_nvs(manager);
  141. // 释放管理器资源
  142. ssid_manager_destroy(manager);
  143. }
  144. // 发送连接报告
  145. esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS,
  146. softap_get_current_connection_number(), &info);
  147. // 添加延迟确保数据写入完成
  148. vTaskDelay(pdMS_TO_TICKS(100));
  149. esp_blufi_host_deinit();
  150. esp_blufi_controller_deinit();
  151. // blufi_entry_func_deinit();
  152. // 重启设备
  153. esp_restart();
  154. } else {
  155. BLUFI_INFO("BLUFI BLE is not connected yet\n");
  156. }
  157. break;
  158. }
  159. default:
  160. break;
  161. }
  162. return;
  163. }
  164. static void wifi_event_handler(void* arg, esp_event_base_t event_base,
  165. int32_t event_id, void* event_data)
  166. {
  167. wifi_event_sta_connected_t *event;
  168. wifi_event_sta_disconnected_t *disconnected_event;
  169. wifi_mode_t mode;
  170. switch (event_id) {
  171. case WIFI_EVENT_STA_START:
  172. example_wifi_connect();
  173. break;
  174. case WIFI_EVENT_STA_CONNECTED:
  175. gl_sta_connected = true;
  176. gl_sta_is_connecting = false;
  177. event = (wifi_event_sta_connected_t*) event_data;
  178. memcpy(gl_sta_bssid, event->bssid, 6);
  179. memcpy(gl_sta_ssid, event->ssid, event->ssid_len);
  180. gl_sta_ssid_len = event->ssid_len;
  181. break;
  182. case WIFI_EVENT_STA_DISCONNECTED:
  183. /* Only handle reconnection during connecting */
  184. if (gl_sta_connected == false && example_wifi_reconnect() == false) {
  185. gl_sta_is_connecting = false;
  186. disconnected_event = (wifi_event_sta_disconnected_t*) event_data;
  187. example_record_wifi_conn_info(disconnected_event->rssi, disconnected_event->reason);
  188. }
  189. /* This is a workaround as ESP32 WiFi libs don't currently
  190. auto-reassociate. */
  191. gl_sta_connected = false;
  192. gl_sta_got_ip = false;
  193. memset(gl_sta_ssid, 0, 32);
  194. memset(gl_sta_bssid, 0, 6);
  195. gl_sta_ssid_len = 0;
  196. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  197. break;
  198. case WIFI_EVENT_AP_START:
  199. esp_wifi_get_mode(&mode);
  200. /* TODO: get config or information of softap, then set to report extra_info */
  201. if (ble_is_connected == true) {
  202. if (gl_sta_connected) {
  203. esp_blufi_extra_info_t info;
  204. memset(&info, 0, sizeof(esp_blufi_extra_info_t));
  205. memcpy(info.sta_bssid, gl_sta_bssid, 6);
  206. info.sta_bssid_set = true;
  207. info.sta_ssid = gl_sta_ssid;
  208. info.sta_ssid_len = gl_sta_ssid_len;
  209. esp_blufi_send_wifi_conn_report(mode, gl_sta_got_ip ? ESP_BLUFI_STA_CONN_SUCCESS : ESP_BLUFI_STA_NO_IP, softap_get_current_connection_number(), &info);
  210. } else if (gl_sta_is_connecting) {
  211. esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONNECTING, softap_get_current_connection_number(), &gl_sta_conn_info);
  212. } else {
  213. esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), &gl_sta_conn_info);
  214. }
  215. } else {
  216. BLUFI_INFO("BLUFI BLE is not connected yet\n");
  217. }
  218. break;
  219. case WIFI_EVENT_SCAN_DONE: {
  220. uint16_t apCount = 0;
  221. esp_wifi_scan_get_ap_num(&apCount);
  222. if (apCount == 0) {
  223. BLUFI_INFO("Nothing AP found");
  224. break;
  225. }
  226. wifi_ap_record_t *ap_list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount);
  227. if (!ap_list) {
  228. BLUFI_ERROR("malloc error, ap_list is NULL");
  229. esp_wifi_clear_ap_list();
  230. break;
  231. }
  232. ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, ap_list));
  233. esp_blufi_ap_record_t * blufi_ap_list = (esp_blufi_ap_record_t *)malloc(apCount * sizeof(esp_blufi_ap_record_t));
  234. if (!blufi_ap_list) {
  235. if (ap_list) {
  236. free(ap_list);
  237. }
  238. BLUFI_ERROR("malloc error, blufi_ap_list is NULL");
  239. break;
  240. }
  241. for (int i = 0; i < apCount; ++i)
  242. {
  243. blufi_ap_list[i].rssi = ap_list[i].rssi;
  244. memcpy(blufi_ap_list[i].ssid, ap_list[i].ssid, sizeof(ap_list[i].ssid));
  245. }
  246. if (ble_is_connected == true) {
  247. esp_blufi_send_wifi_list(apCount, blufi_ap_list);
  248. } else {
  249. BLUFI_INFO("BLUFI BLE is not connected yet\n");
  250. }
  251. esp_wifi_scan_stop();
  252. free(ap_list);
  253. free(blufi_ap_list);
  254. break;
  255. }
  256. case WIFI_EVENT_AP_STACONNECTED: {
  257. wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
  258. BLUFI_INFO("station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
  259. break;
  260. }
  261. case WIFI_EVENT_AP_STADISCONNECTED: {
  262. wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
  263. BLUFI_INFO("station "MACSTR" leave, AID=%d, reason=%d", MAC2STR(event->mac), event->aid, event->reason);
  264. break;
  265. }
  266. default:
  267. break;
  268. }
  269. return;
  270. }
  271. static void initialise_wifi(void)
  272. {
  273. ESP_ERROR_CHECK(esp_netif_init());
  274. wifi_event_group = xEventGroupCreate();
  275. //ESP_ERROR_CHECK(esp_event_loop_create_default());
  276. esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
  277. assert(sta_netif);
  278. esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
  279. assert(ap_netif);
  280. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
  281. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL));
  282. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  283. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  284. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  285. example_record_wifi_conn_info(EXAMPLE_INVALID_RSSI, EXAMPLE_INVALID_REASON);
  286. ESP_ERROR_CHECK( esp_wifi_start() );
  287. }
  288. static esp_blufi_callbacks_t example_callbacks = {
  289. .event_cb = example_event_callback,
  290. .negotiate_data_handler = blufi_dh_negotiate_data_handler,
  291. .encrypt_func = blufi_aes_encrypt,
  292. .decrypt_func = blufi_aes_decrypt,
  293. .checksum_func = blufi_crc_checksum,
  294. };
  295. static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
  296. {
  297. /* actually, should post to blufi_task handle the procedure,
  298. * now, as a example, we do it more simply */
  299. switch (event) {
  300. case ESP_BLUFI_EVENT_INIT_FINISH:
  301. BLUFI_INFO("BLUFI init finish\n");
  302. esp_blufi_adv_start();
  303. break;
  304. case ESP_BLUFI_EVENT_DEINIT_FINISH:
  305. BLUFI_INFO("BLUFI deinit finish\n");
  306. break;
  307. case ESP_BLUFI_EVENT_BLE_CONNECT:
  308. BLUFI_INFO("BLUFI ble connect\n");
  309. ble_is_connected = true;
  310. esp_blufi_adv_stop();
  311. blufi_security_init();
  312. break;
  313. case ESP_BLUFI_EVENT_BLE_DISCONNECT:
  314. BLUFI_INFO("BLUFI ble disconnect\n");
  315. ble_is_connected = false;
  316. blufi_security_deinit();
  317. esp_blufi_adv_start();
  318. break;
  319. case ESP_BLUFI_EVENT_SET_WIFI_OPMODE:
  320. BLUFI_INFO("BLUFI Set WIFI opmode %d\n", param->wifi_mode.op_mode);
  321. ESP_ERROR_CHECK( esp_wifi_set_mode(param->wifi_mode.op_mode) );
  322. break;
  323. case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP:
  324. BLUFI_INFO("BLUFI request wifi connect to AP\n");
  325. /* there is no wifi callback when the device has already connected to this wifi
  326. so disconnect wifi before connection.
  327. */
  328. esp_wifi_disconnect();
  329. example_wifi_connect();
  330. break;
  331. case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP:
  332. BLUFI_INFO("BLUFI request wifi disconnect from AP\n");
  333. esp_wifi_disconnect();
  334. break;
  335. case ESP_BLUFI_EVENT_REPORT_ERROR:
  336. BLUFI_ERROR("BLUFI report error, error code %d\n", param->report_error.state);
  337. esp_blufi_send_error_info(param->report_error.state);
  338. break;
  339. case ESP_BLUFI_EVENT_GET_WIFI_STATUS: {
  340. wifi_mode_t mode;
  341. esp_blufi_extra_info_t info;
  342. esp_wifi_get_mode(&mode);
  343. if (gl_sta_connected) {
  344. memset(&info, 0, sizeof(esp_blufi_extra_info_t));
  345. memcpy(info.sta_bssid, gl_sta_bssid, 6);
  346. info.sta_bssid_set = true;
  347. info.sta_ssid = gl_sta_ssid;
  348. info.sta_ssid_len = gl_sta_ssid_len;
  349. esp_blufi_send_wifi_conn_report(mode, gl_sta_got_ip ? ESP_BLUFI_STA_CONN_SUCCESS : ESP_BLUFI_STA_NO_IP, softap_get_current_connection_number(), &info);
  350. } else if (gl_sta_is_connecting) {
  351. esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONNECTING, softap_get_current_connection_number(), &gl_sta_conn_info);
  352. } else {
  353. esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, softap_get_current_connection_number(), &gl_sta_conn_info);
  354. }
  355. BLUFI_INFO("BLUFI get wifi status from AP\n");
  356. break;
  357. }
  358. case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE:
  359. BLUFI_INFO("blufi close a gatt connection");
  360. esp_blufi_disconnect();
  361. break;
  362. case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA:
  363. /* TODO */
  364. break;
  365. case ESP_BLUFI_EVENT_RECV_STA_BSSID:
  366. memcpy(sta_config.sta.bssid, param->sta_bssid.bssid, 6);
  367. sta_config.sta.bssid_set = 1;
  368. esp_wifi_set_config(WIFI_IF_STA, &sta_config);
  369. BLUFI_INFO("Recv STA BSSID %s\n", sta_config.sta.ssid);
  370. break;
  371. case ESP_BLUFI_EVENT_RECV_STA_SSID:
  372. if (param->sta_ssid.ssid_len >= sizeof(sta_config.sta.ssid)/sizeof(sta_config.sta.ssid[0])) {
  373. esp_blufi_send_error_info(ESP_BLUFI_DATA_FORMAT_ERROR);
  374. BLUFI_INFO("Invalid STA SSID\n");
  375. break;
  376. }
  377. strncpy((char *)sta_config.sta.ssid, (char *)param->sta_ssid.ssid, param->sta_ssid.ssid_len);
  378. sta_config.sta.ssid[param->sta_ssid.ssid_len] = '\0';
  379. esp_wifi_set_config(WIFI_IF_STA, &sta_config);
  380. BLUFI_INFO("Recv STA SSID %s\n", sta_config.sta.ssid);
  381. break;
  382. case ESP_BLUFI_EVENT_RECV_STA_PASSWD:
  383. if (param->sta_passwd.passwd_len >= sizeof(sta_config.sta.password)/sizeof(sta_config.sta.password[0])) {
  384. esp_blufi_send_error_info(ESP_BLUFI_DATA_FORMAT_ERROR);
  385. BLUFI_INFO("Invalid STA PASSWORD\n");
  386. break;
  387. }
  388. strncpy((char *)sta_config.sta.password, (char *)param->sta_passwd.passwd, param->sta_passwd.passwd_len);
  389. sta_config.sta.password[param->sta_passwd.passwd_len] = '\0';
  390. sta_config.sta.threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD;
  391. esp_wifi_set_config(WIFI_IF_STA, &sta_config);
  392. BLUFI_INFO("Recv STA PASSWORD %s\n", sta_config.sta.password);
  393. break;
  394. case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID:
  395. if (param->softap_ssid.ssid_len >= sizeof(ap_config.ap.ssid)/sizeof(ap_config.ap.ssid[0])) {
  396. esp_blufi_send_error_info(ESP_BLUFI_DATA_FORMAT_ERROR);
  397. BLUFI_INFO("Invalid SOFTAP SSID\n");
  398. break;
  399. }
  400. strncpy((char *)ap_config.ap.ssid, (char *)param->softap_ssid.ssid, param->softap_ssid.ssid_len);
  401. ap_config.ap.ssid[param->softap_ssid.ssid_len] = '\0';
  402. ap_config.ap.ssid_len = param->softap_ssid.ssid_len;
  403. esp_wifi_set_config(WIFI_IF_AP, &ap_config);
  404. BLUFI_INFO("Recv SOFTAP SSID %s, ssid len %d\n", ap_config.ap.ssid, ap_config.ap.ssid_len);
  405. break;
  406. case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD:
  407. if (param->softap_passwd.passwd_len >= sizeof(ap_config.ap.password)/sizeof(ap_config.ap.password[0])) {
  408. esp_blufi_send_error_info(ESP_BLUFI_DATA_FORMAT_ERROR);
  409. BLUFI_INFO("Invalid SOFTAP PASSWD\n");
  410. break;
  411. }
  412. strncpy((char *)ap_config.ap.password, (char *)param->softap_passwd.passwd, param->softap_passwd.passwd_len);
  413. ap_config.ap.password[param->softap_passwd.passwd_len] = '\0';
  414. esp_wifi_set_config(WIFI_IF_AP, &ap_config);
  415. BLUFI_INFO("Recv SOFTAP PASSWORD %s len = %d\n", ap_config.ap.password, param->softap_passwd.passwd_len);
  416. break;
  417. case ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM:
  418. if (param->softap_max_conn_num.max_conn_num > 4) {
  419. return;
  420. }
  421. ap_config.ap.max_connection = param->softap_max_conn_num.max_conn_num;
  422. esp_wifi_set_config(WIFI_IF_AP, &ap_config);
  423. BLUFI_INFO("Recv SOFTAP MAX CONN NUM %d\n", ap_config.ap.max_connection);
  424. break;
  425. case ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE:
  426. if (param->softap_auth_mode.auth_mode >= WIFI_AUTH_MAX) {
  427. return;
  428. }
  429. ap_config.ap.authmode = param->softap_auth_mode.auth_mode;
  430. esp_wifi_set_config(WIFI_IF_AP, &ap_config);
  431. BLUFI_INFO("Recv SOFTAP AUTH MODE %d\n", ap_config.ap.authmode);
  432. break;
  433. case ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL:
  434. if (param->softap_channel.channel > 13) {
  435. return;
  436. }
  437. ap_config.ap.channel = param->softap_channel.channel;
  438. esp_wifi_set_config(WIFI_IF_AP, &ap_config);
  439. BLUFI_INFO("Recv SOFTAP CHANNEL %d\n", ap_config.ap.channel);
  440. break;
  441. case ESP_BLUFI_EVENT_GET_WIFI_LIST:{
  442. wifi_scan_config_t scanConf = {
  443. .ssid = NULL,
  444. .bssid = NULL,
  445. .channel = 0,
  446. .show_hidden = false
  447. };
  448. esp_err_t ret = esp_wifi_scan_start(&scanConf, true);
  449. if (ret != ESP_OK) {
  450. esp_blufi_send_error_info(ESP_BLUFI_WIFI_SCAN_FAIL);
  451. }
  452. break;
  453. }
  454. case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA:
  455. BLUFI_INFO("Recv Custom Data %" PRIu32 "\n", param->custom_data.data_len);
  456. ESP_LOG_BUFFER_HEX("Custom Data", param->custom_data.data, param->custom_data.data_len);
  457. break;
  458. case ESP_BLUFI_EVENT_RECV_USERNAME:
  459. /* Not handle currently */
  460. break;
  461. case ESP_BLUFI_EVENT_RECV_CA_CERT:
  462. /* Not handle currently */
  463. break;
  464. case ESP_BLUFI_EVENT_RECV_CLIENT_CERT:
  465. /* Not handle currently */
  466. break;
  467. case ESP_BLUFI_EVENT_RECV_SERVER_CERT:
  468. /* Not handle currently */
  469. break;
  470. case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY:
  471. /* Not handle currently */
  472. break;;
  473. case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY:
  474. /* Not handle currently */
  475. break;
  476. default:
  477. break;
  478. }
  479. }
  480. void blufi_entry_func(void)
  481. {
  482. esp_err_t ret;
  483. initialise_wifi();
  484. #if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
  485. ret = esp_blufi_controller_init();
  486. if (ret) {
  487. BLUFI_ERROR("%s BLUFI controller init failed: %s\n", __func__, esp_err_to_name(ret));
  488. return;
  489. }
  490. #endif
  491. ret = esp_blufi_host_and_cb_init(&example_callbacks);
  492. if (ret) {
  493. BLUFI_ERROR("%s initialise failed: %s\n", __func__, esp_err_to_name(ret));
  494. return;
  495. }
  496. BLUFI_INFO("BLUFI VERSION %04x\n", esp_blufi_get_version());
  497. }
  498. void blufi_entry_func_deinit(void)
  499. {
  500. esp_err_t ret;
  501. BLUFI_INFO("BLUFI deinit start");
  502. ret = esp_blufi_host_deinit();
  503. if (ret) {
  504. BLUFI_ERROR( "%s deinit host failed: %s\n", __func__, esp_err_to_name(ret));
  505. }
  506. #if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
  507. ret = esp_blufi_controller_deinit();
  508. if (ret) {
  509. BLUFI_ERROR( "%s deinit controller failed: %s\n", __func__, esp_err_to_name(ret));
  510. }
  511. #endif
  512. BLUFI_INFO("BLUFI deinit finished");
  513. }