network_wifi.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. #include "network_wifi.h"
  2. #include <string.h>
  3. #include "cJSON.h"
  4. #include "dns_server.h"
  5. #include "esp_event.h"
  6. #include "esp_log.h"
  7. #include "esp_system.h"
  8. #include "esp_wifi.h"
  9. #include "esp_wifi_types.h"
  10. #include "globdefs.h"
  11. #include "lwip/sockets.h"
  12. #include "messaging.h"
  13. #include "network_status.h"
  14. #include "nvs.h"
  15. #include "nvs_flash.h"
  16. #include "platform_config.h"
  17. #include "platform_esp32.h"
  18. #include "tools.h"
  19. #include "trace.h"
  20. static void network_wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
  21. static void network_manager_wifi_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) ;
  22. static char* get_disconnect_code_desc(uint8_t reason);
  23. cJSON* accessp_cjson = NULL;
  24. wifi_config_t* wifi_manager_config_sta = NULL;
  25. static const char TAG[] = "network_wifi";
  26. const char wifi_manager_nvs_namespace[] = "config";
  27. uint16_t ap_num = MAX_AP_NUM;
  28. esp_netif_t *wifi_netif;
  29. wifi_ap_record_t* accessp_records = NULL;
  30. /* wifi scanner config */
  31. wifi_scan_config_t scan_config = {
  32. .ssid = 0,
  33. .bssid = 0,
  34. .channel = 0,
  35. .show_hidden = true};
  36. #ifndef STR_OR_BLANK
  37. #define STR_OR_BLANK(p) p == NULL ? "" : p
  38. #endif
  39. esp_netif_t *network_wifi_get_interface(){
  40. return wifi_netif;
  41. }
  42. void init_network_wifi() {
  43. ESP_LOGD(TAG, "WIFI Starting.");
  44. accessp_cjson = NULL;
  45. accessp_cjson = wifi_manager_clear_ap_list_json(&accessp_cjson);
  46. wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));
  47. memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  48. ESP_LOGD(TAG, "Init. ");
  49. wifi_netif = esp_netif_create_default_wifi_sta();
  50. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  51. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  52. ESP_LOGD(TAG, "Handlers");
  53. //wifi_manager_register_handlers();
  54. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
  55. ESP_EVENT_ANY_ID,
  56. &network_wifi_event_handler,
  57. NULL,
  58. NULL));
  59. esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &network_manager_wifi_ip_event_handler, NULL);
  60. ESP_LOGD(TAG, "Storage");
  61. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  62. ESP_LOGD(TAG, "Set Mode");
  63. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
  64. ESP_LOGD(TAG, "Start");
  65. ESP_ERROR_CHECK(esp_wifi_start());
  66. }
  67. void destroy_network_wifi() {
  68. cJSON_Delete(accessp_cjson);
  69. accessp_cjson = NULL;
  70. FREE_AND_NULL(wifi_manager_config_sta);
  71. }
  72. bool network_wifi_sta_config_changed() {
  73. bool changed = true;
  74. wifi_config_t wifi_manager_config_sta_existing;
  75. if(wifi_manager_config_sta && wifi_manager_load_wifi_sta_config(&wifi_manager_config_sta_existing )){
  76. changed = strcmp( (char *)wifi_manager_config_sta_existing.sta.ssid,(char *)wifi_manager_config_sta->sta.ssid ) !=0 ||
  77. strcmp((char *) wifi_manager_config_sta_existing.sta.password,(char *)wifi_manager_config_sta->sta.password ) !=0;
  78. }
  79. return changed;
  80. }
  81. esp_err_t network_wifi_save_sta_config() {
  82. nvs_handle handle;
  83. esp_err_t esp_err;
  84. ESP_LOGD(TAG, "Config Save");
  85. if (wifi_manager_config_sta) {
  86. esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
  87. if (esp_err != ESP_OK) {
  88. ESP_LOGE(TAG, "%s failure. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
  89. return esp_err;
  90. }
  91. esp_err = nvs_set_blob(handle, "ssid", wifi_manager_config_sta->sta.ssid, sizeof(wifi_manager_config_sta->sta.ssid));
  92. if (esp_err != ESP_OK) {
  93. ESP_LOGE(TAG, "ssid (%s). Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
  94. return esp_err;
  95. }
  96. esp_err = nvs_set_blob(handle, "password", wifi_manager_config_sta->sta.password, sizeof(wifi_manager_config_sta->sta.password));
  97. if (esp_err != ESP_OK) {
  98. ESP_LOGE(TAG, "pass (%s). Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
  99. return esp_err;
  100. }
  101. esp_err = nvs_commit(handle);
  102. if (esp_err != ESP_OK) {
  103. ESP_LOGE(TAG, "Commit error: %s", esp_err_to_name(esp_err));
  104. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Unable to save wifi credentials. %s", esp_err_to_name(esp_err));
  105. return esp_err;
  106. }
  107. nvs_close(handle);
  108. ESP_LOGD(TAG, "saved: ssid:%s password:%s", wifi_manager_config_sta->sta.ssid, wifi_manager_config_sta->sta.password);
  109. }
  110. return ESP_OK;
  111. }
  112. bool wifi_manager_load_wifi_sta_config(wifi_config_t* config ){
  113. nvs_handle handle;
  114. esp_err_t esp_err;
  115. ESP_LOGD(TAG, "Fetching wifi sta config.");
  116. esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle);
  117. if (esp_err == ESP_OK) {
  118. /* ssid */
  119. ESP_LOGD(TAG, "Fetching value for ssid.");
  120. size_t sz = sizeof(config->sta.ssid);
  121. uint8_t* buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
  122. memset(buff, 0x00, sizeof(uint8_t) * sz);
  123. esp_err = nvs_get_blob(handle, "ssid", buff, &sz);
  124. if (esp_err != ESP_OK) {
  125. ESP_LOGD(TAG, "No ssid found in nvs.");
  126. FREE_AND_NULL(buff);
  127. nvs_close(handle);
  128. return false;
  129. }
  130. memcpy(config->sta.ssid, buff, sizeof(config->sta.ssid));
  131. FREE_AND_NULL(buff);
  132. ESP_LOGD(TAG, "wifi_manager_fetch_wifi_sta_config: ssid:%s ", config->sta.ssid);
  133. /* password */
  134. sz = sizeof(config->sta.password);
  135. buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
  136. memset(buff, 0x00, sizeof(uint8_t) * sz);
  137. esp_err = nvs_get_blob(handle, "password", buff, &sz);
  138. if (esp_err != ESP_OK) {
  139. // Don't take this as an error. This could be an opened access point?
  140. ESP_LOGW(TAG, "No wifi password found in nvs");
  141. } else {
  142. memcpy(config->sta.password, buff, sizeof(config->sta.password));
  143. ESP_LOGD(TAG, "wifi_manager_fetch_wifi_sta_config: password:%s", config->sta.password);
  144. }
  145. FREE_AND_NULL(buff);
  146. nvs_close(handle);
  147. config->sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
  148. return config->sta.ssid[0] != '\0';
  149. } else {
  150. ESP_LOGW(TAG, "wifi manager has no previous configuration. %s", esp_err_to_name(esp_err));
  151. return false;
  152. }
  153. }
  154. bool wifi_manager_fetch_wifi_sta_config() {
  155. if (wifi_manager_config_sta == NULL) {
  156. ESP_LOGD(TAG, "Allocating memory for structure.");
  157. wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));
  158. }
  159. memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  160. return wifi_manager_load_wifi_sta_config(wifi_manager_config_sta);
  161. }
  162. wifi_config_t* wifi_manager_get_wifi_sta_config() {
  163. return wifi_manager_config_sta;
  164. }
  165. // void wifi_manager_register_handlers() {
  166. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_WIFI_READY, &network_wifi_event_handler, NULL));
  167. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &network_wifi_event_handler, NULL));
  168. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_AUTHMODE_CHANGE, &network_wifi_event_handler, NULL));
  169. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, &network_wifi_event_handler, NULL));
  170. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STOP, &network_wifi_event_handler, NULL));
  171. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_PROBEREQRECVED, &network_wifi_event_handler, NULL));
  172. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_SUCCESS, &network_wifi_event_handler, NULL));
  173. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, &network_wifi_event_handler, NULL));
  174. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_TIMEOUT, &network_wifi_event_handler, NULL));
  175. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_PIN, &network_wifi_event_handler, NULL));
  176. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &network_wifi_event_handler, NULL));
  177. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &network_wifi_event_handler, NULL));
  178. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, &network_wifi_event_handler, NULL));
  179. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_STOP, &network_wifi_event_handler, NULL));
  180. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &network_wifi_event_handler, NULL));
  181. // ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &network_wifi_event_handler, NULL));
  182. // }
  183. #define LOCAL_MAC_SIZE 20
  184. char* get_mac_string(uint8_t mac[6]) {
  185. char* macStr = malloc(LOCAL_MAC_SIZE);
  186. memset(macStr, 0x00, LOCAL_MAC_SIZE);
  187. snprintf(macStr, LOCAL_MAC_SIZE, MACSTR, MAC2STR(mac));
  188. return macStr;
  189. }
  190. static void network_wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  191. if (event_base != WIFI_EVENT)
  192. return;
  193. switch (event_id) {
  194. case WIFI_EVENT_WIFI_READY:
  195. ESP_LOGD(TAG, "WIFI_EVENT_WIFI_READY");
  196. break;
  197. case WIFI_EVENT_SCAN_DONE:
  198. ESP_LOGD(TAG, "WIFI_EVENT_SCAN_DONE");
  199. network_manager_async_scan_done();
  200. break;
  201. case WIFI_EVENT_STA_AUTHMODE_CHANGE:
  202. ESP_LOGD(TAG, "WIFI_EVENT_STA_AUTHMODE_CHANGE");
  203. break;
  204. case WIFI_EVENT_AP_START:
  205. ESP_LOGD(TAG, "WIFI_EVENT_AP_START");
  206. break;
  207. case WIFI_EVENT_AP_STOP:
  208. ESP_LOGD(TAG, "WIFI_EVENT_AP_STOP");
  209. break;
  210. case WIFI_EVENT_AP_PROBEREQRECVED: {
  211. // wifi_event_ap_probe_req_rx_t
  212. // Argument structure for WIFI_EVENT_AP_PROBEREQRECVED event
  213. //
  214. // Public Members
  215. //
  216. // int rssi
  217. // Received probe request signal strength
  218. //
  219. // uint8_t mac[6]
  220. // MAC address of the station which send probe request
  221. wifi_event_ap_probe_req_rx_t* s = (wifi_event_ap_probe_req_rx_t*)event_data;
  222. char* mac = get_mac_string(s->mac);
  223. ESP_LOGD(TAG, "WIFI_EVENT_AP_PROBEREQRECVED. RSSI: %d, MAC: %s", s->rssi, STR_OR_BLANK(mac));
  224. FREE_AND_NULL(mac);
  225. } break;
  226. case WIFI_EVENT_STA_WPS_ER_SUCCESS:
  227. ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS");
  228. break;
  229. case WIFI_EVENT_STA_WPS_ER_FAILED:
  230. ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_FAILED");
  231. break;
  232. case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
  233. ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_TIMEOUT");
  234. break;
  235. case WIFI_EVENT_STA_WPS_ER_PIN:
  236. ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_PIN");
  237. break;
  238. case WIFI_EVENT_AP_STACONNECTED: {
  239. wifi_event_ap_staconnected_t* stac = (wifi_event_ap_staconnected_t*)event_data;
  240. char* mac = get_mac_string(stac->mac);
  241. ESP_LOGD(TAG, "WIFI_EVENT_AP_STACONNECTED. aid: %d, mac: %s", stac->aid, STR_OR_BLANK(mac));
  242. FREE_AND_NULL(mac);
  243. } break;
  244. case WIFI_EVENT_AP_STADISCONNECTED:
  245. ESP_LOGD(TAG, "WIFI_EVENT_AP_STADISCONNECTED");
  246. break;
  247. case WIFI_EVENT_STA_START:
  248. ESP_LOGD(TAG, "WIFI_EVENT_STA_START");
  249. break;
  250. case WIFI_EVENT_STA_STOP:
  251. ESP_LOGD(TAG, "WIFI_EVENT_STA_STOP");
  252. break;
  253. case WIFI_EVENT_STA_CONNECTED: {
  254. // structwifi_event_sta_connected_t
  255. // Argument structure for WIFI_EVENT_STA_CONNECTED event
  256. //
  257. // Public Members
  258. //
  259. // uint8_t ssid[32]
  260. // SSID of connected AP
  261. //
  262. // uint8_t ssid_len
  263. // SSID length of connected AP
  264. //
  265. // uint8_t bssid[6]
  266. // BSSID of connected AP
  267. //
  268. // uint8_t channel
  269. // channel of connected AP
  270. //
  271. // wifi_auth_mode_tauthmode
  272. // authentication mode used by AP
  273. //, get_mac_string(EVENT_HANDLER_ARG_FIELD(wifi_event_ap_probe_req_rx_t, mac)));
  274. ESP_LOGD(TAG, "WIFI_EVENT_STA_CONNECTED. ");
  275. wifi_event_sta_connected_t* s = (wifi_event_sta_connected_t*)event_data;
  276. char* bssid = get_mac_string(s->bssid);
  277. char* ssid = strdup((char*)s->ssid);
  278. ESP_LOGD(TAG, "WIFI_EVENT_STA_CONNECTED. Channel: %d, Access point: %s, BSSID: %s ", s->channel, STR_OR_BLANK(ssid), (bssid));
  279. FREE_AND_NULL(bssid);
  280. FREE_AND_NULL(ssid);
  281. network_manager_async_success();
  282. } break;
  283. case WIFI_EVENT_STA_DISCONNECTED: {
  284. // structwifi_event_sta_disconnected_t
  285. // Argument structure for WIFI_EVENT_STA_DISCONNECTED event
  286. //
  287. // Public Members
  288. //
  289. // uint8_t ssid[32]
  290. // SSID of disconnected AP
  291. //
  292. // uint8_t ssid_len
  293. // SSID length of disconnected AP
  294. //
  295. // uint8_t bssid[6]
  296. // BSSID of disconnected AP
  297. //
  298. // uint8_t reason
  299. // reason of disconnection
  300. wifi_event_sta_disconnected_t* s = (wifi_event_sta_disconnected_t*)event_data;
  301. char* bssid = get_mac_string(s->bssid);
  302. ESP_LOGD(TAG, "WIFI_EVENT_STA_DISCONNECTED. From BSSID: %s, reason code: %d (%s)", STR_OR_BLANK(bssid), s->reason, get_disconnect_code_desc(s->reason));
  303. FREE_AND_NULL(bssid);
  304. /* if a DISCONNECT message is posted while a scan is in progress this scan will NEVER end, causing scan to never work again. For this reason SCAN_BIT is cleared too */
  305. // todo: check for implementation of this: network_manager_clear_flag(WIFI_MANAGER_WIFI_CONNECTED_BIT | WIFI_MANAGER_SCAN_BIT);
  306. wifi_event_sta_disconnected_t * disconnected_event = malloc(sizeof(wifi_event_sta_disconnected_t));
  307. memcpy(disconnected_event, event_data, sizeof(wifi_event_sta_disconnected_t));
  308. network_manager_async_lost_connection(disconnected_event);
  309. } break;
  310. default:
  311. break;
  312. }
  313. }
  314. cJSON* wifi_manager_get_new_array_json(cJSON** old) {
  315. ESP_LOGV(TAG, "wifi_manager_get_new_array_json called");
  316. cJSON* root = *old;
  317. if (root != NULL) {
  318. cJSON_Delete(root);
  319. *old = NULL;
  320. }
  321. ESP_LOGV(TAG, "wifi_manager_get_new_array_json done");
  322. return cJSON_CreateArray();
  323. }
  324. void wifi_manager_generate_access_points_json(cJSON** ap_list) {
  325. *ap_list = wifi_manager_get_new_array_json(ap_list);
  326. if (*ap_list == NULL)
  327. return;
  328. for (int i = 0; i < ap_num; i++) {
  329. cJSON* ap = cJSON_CreateObject();
  330. if (ap == NULL) {
  331. ESP_LOGE(TAG, "Unable to allocate memory for access point entry #%d", i);
  332. return;
  333. }
  334. cJSON* radio = cJSON_CreateObject();
  335. if (radio == NULL) {
  336. ESP_LOGE(TAG, "Unable to allocate memory for access point entry #%d", i);
  337. cJSON_Delete(ap);
  338. return;
  339. }
  340. wifi_ap_record_t ap_rec = accessp_records[i];
  341. cJSON_AddNumberToObject(ap, "chan", ap_rec.primary);
  342. cJSON_AddNumberToObject(ap, "rssi", ap_rec.rssi);
  343. cJSON_AddNumberToObject(ap, "auth", ap_rec.authmode);
  344. cJSON_AddItemToObject(ap, "ssid", cJSON_CreateString((char*)ap_rec.ssid));
  345. char* bssid = get_mac_string(ap_rec.bssid);
  346. cJSON_AddItemToObject(ap, "bssid", cJSON_CreateString(STR_OR_BLANK(bssid)));
  347. FREE_AND_NULL(bssid);
  348. cJSON_AddNumberToObject(radio, "b", ap_rec.phy_11b ? 1 : 0);
  349. cJSON_AddNumberToObject(radio, "g", ap_rec.phy_11g ? 1 : 0);
  350. cJSON_AddNumberToObject(radio, "n", ap_rec.phy_11n ? 1 : 0);
  351. cJSON_AddNumberToObject(radio, "low_rate", ap_rec.phy_lr ? 1 : 0);
  352. cJSON_AddItemToObject(ap, "radio", radio);
  353. cJSON_AddItemToArray(*ap_list, ap);
  354. char* ap_json = cJSON_PrintUnformatted(ap);
  355. if (ap_json != NULL) {
  356. ESP_LOGD(TAG, "New access point found: %s", ap_json);
  357. free(ap_json);
  358. }
  359. }
  360. char* ap_list_json = cJSON_PrintUnformatted(*ap_list);
  361. if (ap_list_json != NULL) {
  362. ESP_LOGV(TAG, "Full access point list: %s", ap_list_json);
  363. free(ap_list_json);
  364. }
  365. }
  366. void wifi_manager_set_ipv4val(const char* key, char* default_value, ip4_addr_t * target) {
  367. char* value = config_alloc_get_default(NVS_TYPE_STR, key, default_value, 0);
  368. if (value != NULL) {
  369. ESP_LOGD(TAG, "%s: %s", key, value);
  370. inet_pton(AF_INET, value, target); /* access point is on a static IP */
  371. }
  372. FREE_AND_NULL(value);
  373. }
  374. void wifi_manager_config_ap() {
  375. tcpip_adapter_ip_info_t info;
  376. esp_err_t err = ESP_OK;
  377. char* value = NULL;
  378. wifi_config_t ap_config = {
  379. .ap = {
  380. .ssid_len = 0,
  381. },
  382. };
  383. ESP_LOGI(TAG, "Configuring Access Point.");
  384. wifi_netif = esp_netif_create_default_wifi_ap();
  385. /* In order to change the IP info structure, we have to first stop
  386. * the DHCP server on the new interface
  387. */
  388. esp_netif_dhcps_stop(wifi_netif);
  389. // tcpip_adapter_dhcps_get_status(TCPIP_ADAPTER_IF_AP, &dhcp_status);
  390. // if (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED) {
  391. // ESP_LOGD(TAG, "Stopping DHCP on interface so we can ");
  392. // if ((err = tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)) != ESP_OK) /* stop AP DHCP server */
  393. // {
  394. // ESP_LOGW(TAG, "Stopping DHCP failed. Error %s", esp_err_to_name(err));
  395. // }
  396. // }
  397. /*
  398. * Set access point mode IP adapter configuration
  399. */
  400. wifi_manager_set_ipv4val("ap_ip_address", DEFAULT_AP_IP, &info.ip);
  401. wifi_manager_set_ipv4val("ap_ip_gateway", CONFIG_DEFAULT_AP_GATEWAY, &info.gw);
  402. wifi_manager_set_ipv4val("ap_ip_netmask", CONFIG_DEFAULT_AP_NETMASK, &info.netmask);
  403. ESP_LOGD(TAG, "Setting tcp_ip info for interface TCPIP_ADAPTER_IF_AP");
  404. if ((err = esp_netif_set_ip_info(wifi_netif, &info)) != ESP_OK) {
  405. ESP_LOGE(TAG, "Setting tcp_ip info for interface TCPIP_ADAPTER_IF_AP. Error %s", esp_err_to_name(err));
  406. return;
  407. }
  408. /*
  409. * Set Access Point configuration
  410. */
  411. value = config_alloc_get_default(NVS_TYPE_STR, "ap_ssid", CONFIG_DEFAULT_AP_SSID, 0);
  412. if (value != NULL) {
  413. strlcpy((char*)ap_config.ap.ssid, value, sizeof(ap_config.ap.ssid));
  414. ESP_LOGI(TAG, "AP SSID: %s", (char*)ap_config.ap.ssid);
  415. }
  416. FREE_AND_NULL(value);
  417. value = config_alloc_get_default(NVS_TYPE_STR, "ap_pwd", DEFAULT_AP_PASSWORD, 0);
  418. if (value != NULL) {
  419. strlcpy((char*)ap_config.ap.password, value, sizeof(ap_config.ap.password));
  420. ESP_LOGI(TAG, "AP Password: %s", (char*)ap_config.ap.password);
  421. }
  422. FREE_AND_NULL(value);
  423. value = config_alloc_get_default(NVS_TYPE_STR, "ap_channel", STR(CONFIG_DEFAULT_AP_CHANNEL), 0);
  424. if (value != NULL) {
  425. ESP_LOGD(TAG, "Channel: %s", value);
  426. ap_config.ap.channel = atoi(value);
  427. }
  428. FREE_AND_NULL(value);
  429. ap_config.ap.authmode = AP_AUTHMODE;
  430. ap_config.ap.ssid_hidden = DEFAULT_AP_SSID_HIDDEN;
  431. ap_config.ap.max_connection = DEFAULT_AP_MAX_CONNECTIONS;
  432. ap_config.ap.beacon_interval = DEFAULT_AP_BEACON_INTERVAL;
  433. ESP_LOGD(TAG, "Auth Mode: %d", ap_config.ap.authmode);
  434. ESP_LOGD(TAG, "SSID Hidden: %d", ap_config.ap.ssid_hidden);
  435. ESP_LOGD(TAG, "Max Connections: %d", ap_config.ap.max_connection);
  436. ESP_LOGD(TAG, "Beacon interval: %d", ap_config.ap.beacon_interval);
  437. const char* msg = "Setting wifi mode as WIFI_MODE_APSTA";
  438. ESP_LOGD(TAG, "%s",msg);
  439. if ((err = esp_wifi_set_mode(WIFI_MODE_APSTA)) != ESP_OK) {
  440. ESP_LOGE(TAG, "%s. Error %s",msg, esp_err_to_name(err));
  441. return;
  442. }
  443. msg = "Setting wifi AP configuration for WIFI_IF_AP";
  444. ESP_LOGD(TAG, "%s", msg);
  445. if ((err = esp_wifi_set_config(WIFI_IF_AP, &ap_config)) != ESP_OK) /* stop AP DHCP server */
  446. {
  447. ESP_LOGE(TAG, "%s . Error %s", msg, esp_err_to_name(err));
  448. return;
  449. }
  450. msg = "Setting wifi bandwidth";
  451. ESP_LOGD(TAG, "%s (%d)", msg, DEFAULT_AP_BANDWIDTH);
  452. if ((err = esp_wifi_set_bandwidth(WIFI_IF_AP, DEFAULT_AP_BANDWIDTH)) != ESP_OK) /* stop AP DHCP server */
  453. {
  454. ESP_LOGE(TAG, "%s failed. Error %s", msg, esp_err_to_name(err));
  455. return;
  456. }
  457. msg = "Setting wifi power save";
  458. ESP_LOGD(TAG, "%s (%d)", msg, DEFAULT_STA_POWER_SAVE);
  459. if ((err = esp_wifi_set_ps(DEFAULT_STA_POWER_SAVE)) != ESP_OK) /* stop AP DHCP server */
  460. {
  461. ESP_LOGE(TAG, "%s failed. Error %s", msg, esp_err_to_name(err));
  462. return;
  463. }
  464. esp_netif_dhcps_start(wifi_netif);
  465. ESP_LOGD(TAG, "Done configuring Soft Access Point");
  466. dns_server_start();
  467. }
  468. void wifi_manager_filter_unique(wifi_ap_record_t* aplist, uint16_t* aps) {
  469. int total_unique;
  470. wifi_ap_record_t* first_free;
  471. total_unique = *aps;
  472. first_free = NULL;
  473. for (int i = 0; i < *aps - 1; i++) {
  474. wifi_ap_record_t* ap = &aplist[i];
  475. /* skip the previously removed APs */
  476. if (ap->ssid[0] == 0)
  477. continue;
  478. /* remove the identical SSID+authmodes */
  479. for (int j = i + 1; j < *aps; j++) {
  480. wifi_ap_record_t* ap1 = &aplist[j];
  481. if ((strcmp((const char*)ap->ssid, (const char*)ap1->ssid) == 0) &&
  482. (ap->authmode == ap1->authmode)) { /* same SSID, different auth mode is skipped */
  483. /* save the rssi for the display */
  484. if ((ap1->rssi) > (ap->rssi))
  485. ap->rssi = ap1->rssi;
  486. /* clearing the record */
  487. memset(ap1, 0, sizeof(wifi_ap_record_t));
  488. }
  489. }
  490. }
  491. /* reorder the list so APs follow each other in the list */
  492. for (int i = 0; i < *aps; i++) {
  493. wifi_ap_record_t* ap = &aplist[i];
  494. /* skipping all that has no name */
  495. if (ap->ssid[0] == 0) {
  496. /* mark the first free slot */
  497. if (first_free == NULL)
  498. first_free = ap;
  499. total_unique--;
  500. continue;
  501. }
  502. if (first_free != NULL) {
  503. memcpy(first_free, ap, sizeof(wifi_ap_record_t));
  504. memset(ap, 0, sizeof(wifi_ap_record_t));
  505. /* find the next free slot */
  506. for (int j = 0; j < *aps; j++) {
  507. if (aplist[j].ssid[0] == 0) {
  508. first_free = &aplist[j];
  509. break;
  510. }
  511. }
  512. }
  513. }
  514. /* update the length of the list */
  515. *aps = total_unique;
  516. }
  517. char* wifi_manager_alloc_get_ap_list_json() {
  518. return cJSON_PrintUnformatted(accessp_cjson);
  519. }
  520. cJSON* wifi_manager_clear_ap_list_json(cJSON** old) {
  521. ESP_LOGV(TAG, "wifi_manager_clear_ap_list_json called");
  522. cJSON* root = wifi_manager_get_new_array_json(old);
  523. ESP_LOGV(TAG, "wifi_manager_clear_ap_list_json done");
  524. return root;
  525. }
  526. esp_err_t wifi_scan_done(queue_message* msg) {
  527. esp_err_t err = ESP_OK;
  528. /* As input param, it stores max AP number ap_records can hold. As output param, it receives the actual AP number this API returns.
  529. * As a consequence, ap_num MUST be reset to MAX_AP_NUM at every scan */
  530. ESP_LOGD(TAG, "Getting AP list records");
  531. if ((err = esp_wifi_scan_get_ap_num(&ap_num)) != ESP_OK) {
  532. ESP_LOGE(TAG, "Failed to retrieve scan results count. Error %s", esp_err_to_name(err));
  533. return err;
  534. }
  535. if (ap_num > 0) {
  536. accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * ap_num);
  537. if ((err = esp_wifi_scan_get_ap_records(&ap_num, accessp_records)) != ESP_OK) {
  538. ESP_LOGE(TAG, "Failed to retrieve scan results list. Error %s", esp_err_to_name(err));
  539. return err;
  540. }
  541. /* make sure the http server isn't trying to access the list while it gets refreshed */
  542. ESP_LOGD(TAG, "Preparing to build ap JSON list");
  543. if (wifi_manager_lock_json_buffer(pdMS_TO_TICKS(1000))) {
  544. /* Will remove the duplicate SSIDs from the list and update ap_num */
  545. wifi_manager_filter_unique(accessp_records, &ap_num);
  546. wifi_manager_generate_access_points_json(&accessp_cjson);
  547. wifi_manager_unlock_json_buffer();
  548. ESP_LOGD(TAG, "Done building ap JSON list");
  549. } else {
  550. ESP_LOGE(TAG, "could not get access to json mutex in wifi_scan");
  551. err = ESP_FAIL;
  552. }
  553. free(accessp_records);
  554. } else {
  555. //
  556. ESP_LOGD(TAG, "No AP Found. Emptying the list.");
  557. accessp_cjson = wifi_manager_get_new_array_json(&accessp_cjson);
  558. }
  559. return err;
  560. }
  561. bool is_wifi_up(){
  562. return wifi_netif!=NULL;
  563. }
  564. esp_err_t network_wifi_start_scan(queue_message* msg) {
  565. esp_err_t err = ESP_OK;
  566. ESP_LOGD(TAG, "MESSAGE: ORDER_START_WIFI_SCAN");
  567. if(!is_wifi_up()) {
  568. messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Wifi not started. Cannot scan");
  569. return ESP_FAIL;
  570. }
  571. /* if a scan is already in progress this message is simply ignored thanks to the WIFI_MANAGER_SCAN_BIT uxBit */
  572. if (!network_manager_is_flag_set(WIFI_MANAGER_SCAN_BIT)) {
  573. if ((err = esp_wifi_scan_start(&scan_config, false)) != ESP_OK) {
  574. ESP_LOGW(TAG, "Unable to start scan; %s ", esp_err_to_name(err));
  575. // set_status_message(WARNING, "Wifi Connecting. Cannot start scan.");
  576. messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Scanning failed: %s", esp_err_to_name(err));
  577. } else {
  578. network_manager_set_flag(WIFI_MANAGER_SCAN_BIT);
  579. }
  580. } else {
  581. ESP_LOGW(TAG, "Scan already in progress!");
  582. }
  583. return err;
  584. }
  585. static void polling_STA(void* timer_id) {
  586. network_manager_async_connect(wifi_manager_get_wifi_sta_config());
  587. }
  588. void set_host_name() {
  589. esp_err_t err;
  590. ESP_LOGD(TAG, "Retrieving host name from nvs");
  591. char* host_name = (char*)config_alloc_get(NVS_TYPE_STR, "host_name");
  592. if (host_name == NULL) {
  593. ESP_LOGE(TAG, "Could not retrieve host name from nvs");
  594. } else {
  595. ESP_LOGD(TAG, "Setting host name to : %s", host_name);
  596. if ((err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, host_name)) != ESP_OK) {
  597. ESP_LOGE(TAG, "Unable to set host name. Error: %s", esp_err_to_name(err));
  598. }
  599. // if((err=tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, host_name)) !=ESP_OK){
  600. // ESP_LOGE(TAG, "Unable to set host name. Error: %s",esp_err_to_name(err));
  601. // }
  602. free(host_name);
  603. }
  604. }
  605. esp_err_t network_wifi_connect(wifi_config_t * cfg){
  606. esp_err_t err = ESP_OK;
  607. ESP_LOGD(TAG, "network_wifi_connect");
  608. if(!is_wifi_up()) {
  609. messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Wifi not started. Cannot connect");
  610. return ESP_FAIL;
  611. }
  612. tcpip_adapter_dhcp_status_t status;
  613. ESP_LOGD(TAG, "wifi_manager: Checking if DHCP client for STA interface is running");
  614. ESP_ERROR_CHECK_WITHOUT_ABORT(tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &status));
  615. if (status != TCPIP_ADAPTER_DHCP_STARTED) {
  616. ESP_LOGD(TAG, "wifi_manager: Start DHCP client for STA interface");
  617. ESP_ERROR_CHECK_WITHOUT_ABORT(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
  618. }
  619. wifi_mode_t mode;
  620. /* update config to latest and attempt connection */
  621. esp_wifi_get_mode(&mode);
  622. if (WIFI_MODE_APSTA != mode && WIFI_MODE_STA != mode) {
  623. // the soft ap is not started, so let's set the WiFi mode to STA
  624. ESP_LOGD(TAG, "MESSAGE: network_wifi_connect_existing - setting mode WIFI_MODE_STA");
  625. if ((err = esp_wifi_set_mode(WIFI_MODE_STA)) != ESP_OK) {
  626. ESP_LOGE(TAG, "Failed to set wifi mode to STA. Error %s", esp_err_to_name(err));
  627. return err;
  628. }
  629. }
  630. if ((err = esp_wifi_set_config(WIFI_IF_STA, cfg)) != ESP_OK) {
  631. ESP_LOGE(TAG, "Failed to set STA configuration. Error %s", esp_err_to_name(err));
  632. return err;
  633. }
  634. set_host_name();
  635. ESP_LOGI(TAG, "Wifi Connecting...");
  636. if ((err = esp_wifi_connect()) != ESP_OK) {
  637. ESP_LOGE(TAG, "Failed to initiate wifi connection. Error %s", esp_err_to_name(err));
  638. return err;
  639. }
  640. return err;
  641. }
  642. void network_wifi_clear_config(){
  643. /* erase configuration */
  644. if (wifi_manager_config_sta) {
  645. ESP_LOGI(TAG, "Erasing WiFi Configuration.");
  646. memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  647. /* save NVS memory */
  648. network_wifi_save_sta_config();
  649. }
  650. }
  651. // esp_err_t network_wifi_disconnected(queue_message* msg) {
  652. // esp_err_t err = ESP_OK;
  653. // wifi_event_sta_disconnected_t disc_event;
  654. // // ESP_LOGD(TAG, "MESSAGE: EVENT_STA_DISCONNECTED");
  655. // // if (msg->param == NULL) {
  656. // // ESP_LOGE(TAG, "MESSAGE: EVENT_STA_DISCONNECTED - expected parameter not found!");
  657. // // } else {
  658. // // memcpy(&disc_event, (wifi_event_sta_disconnected_t*)msg->param, sizeof(disc_event));
  659. // // free(msg->param);
  660. // // ESP_LOGD(TAG, "MESSAGE: EVENT_STA_DISCONNECTED with Reason code: %d (%s)", disc_event.reason, get_disconnect_code_desc(disc_event.reason));
  661. // // }
  662. // /* this even can be posted in numerous different conditions
  663. // *
  664. // * 1. SSID password is wrong
  665. // * 2. Manual disconnection ordered
  666. // * 3. Connection lost
  667. // *
  668. // * Having clear understand as to WHY the event was posted is key to having an efficient wifi manager
  669. // *
  670. // * With wifi_manager, we determine:
  671. // * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT is set, We consider it's a client that requested the connection.
  672. // * When SYSTEM_EVENT_STA_DISCONNECTED is posted, it's probably a password/something went wrong with the handshake.
  673. // *
  674. // * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT is set, it's a disconnection that was ASKED by the client (clicking disconnect in the app)
  675. // * When SYSTEM_EVENT_STA_DISCONNECTED is posted, saved wifi is erased from the NVS memory.
  676. // *
  677. // * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT and WIFI_MANAGER_REQUEST_STA_CONNECT_BIT are NOT set, it's a lost connection
  678. // *
  679. // * In this version of the software, reason codes are not used. They are indicated here for potential future usage.
  680. // *
  681. // * REASON CODE:
  682. // * 1 UNSPECIFIED
  683. // * 2 AUTH_EXPIRE auth no longer valid, this smells like someone changed a password on the AP
  684. // * 3 AUTH_LEAVE
  685. // * 4 ASSOC_EXPIRE
  686. // * 5 ASSOC_TOOMANY too many devices already connected to the AP => AP fails to respond
  687. // * 6 NOT_AUTHED
  688. // * 7 NOT_ASSOCED
  689. // * 8 ASSOC_LEAVE
  690. // * 9 ASSOC_NOT_AUTHED
  691. // * 10 DISASSOC_PWRCAP_BAD
  692. // * 11 DISASSOC_SUPCHAN_BAD
  693. // * 12 <n/a>
  694. // * 13 IE_INVALID
  695. // * 14 MIC_FAILURE
  696. // * 15 4WAY_HANDSHAKE_TIMEOUT wrong password! This was personnaly tested on my home wifi with a wrong password.
  697. // * 16 GROUP_KEY_UPDATE_TIMEOUT
  698. // * 17 IE_IN_4WAY_DIFFERS
  699. // * 18 GROUP_CIPHER_INVALID
  700. // * 19 PAIRWISE_CIPHER_INVALID
  701. // * 20 AKMP_INVALID
  702. // * 21 UNSUPP_RSN_IE_VERSION
  703. // * 22 INVALID_RSN_IE_CAP
  704. // * 23 802_1X_AUTH_FAILED wrong password?
  705. // * 24 CIPHER_SUITE_REJECTED
  706. // * 200 BEACON_TIMEOUT
  707. // * 201 NO_AP_FOUND
  708. // * 202 AUTH_FAIL
  709. // * 203 ASSOC_FAIL
  710. // * 204 HANDSHAKE_TIMEOUT
  711. // *
  712. // * */
  713. // /* reset saved sta IP */
  714. // wifi_manager_safe_reset_sta_ip_string();
  715. // if (network_manager_is_flag_set(WIFI_MANAGER_REQUEST_STA_CONNECT_BIT)) {
  716. // network_manager_clear_flag(WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
  717. // ESP_LOGW(TAG, "WiFi Disconnected while processing user connect request. Wrong password?");
  718. // /* there are no retries when it's a user requested connection by design. This avoids a user hanging too much
  719. // * in case they typed a wrong password for instance. Here we simply clear the request bit and move on */
  720. // wifi_mode_t mode;
  721. // esp_wifi_get_mode(&mode);
  722. // if (WIFI_MODE_STA == mode) {
  723. // network_manager_set_flag(WIFI_MANAGER_REQUEST_STA_CONNECT_FAILED_BIT);
  724. // // if wifi was STA, attempt to reload the previous network connection
  725. // ESP_LOGW(TAG, "Attempting to restore previous network");
  726. // wifi_manager_send_message(ORDER_LOAD_AND_RESTORE_STA, NULL);
  727. // }
  728. // } else if (network_manager_is_flag_set(WIFI_MANAGER_REQUEST_DISCONNECT_BIT)) {
  729. // // ESP_LOGD(TAG, "WiFi disconnected by user");
  730. // // /* user manually requested a disconnect so the lost connection is a normal event. Clear the flag and restart the AP */
  731. // // network_manager_clear_flag(WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
  732. // // wifi_manager_generate_ip_info_json(UPDATE_USER_DISCONNECT, wifi_netif,false);
  733. // // /* erase configuration */
  734. // // if (wifi_manager_config_sta) {
  735. // // ESP_LOGI(TAG, "Erasing WiFi Configuration.");
  736. // // memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  737. // // /* save NVS memory */
  738. // // network_wifi_save_sta_config();
  739. // // }
  740. // // /* start SoftAP */
  741. // // ESP_LOGD(TAG, "Disconnect processing complete. Ordering an AP start.");
  742. // // wifi_manager_send_message(ORDER_START_AP, NULL);
  743. // } else {
  744. // /* lost connection ? */
  745. // // ESP_LOGE(TAG, "WiFi Connection lost.");
  746. // // messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "WiFi Connection lost");
  747. // // wifi_manager_generate_ip_info_json(UPDATE_LOST_CONNECTION, wifi_netif,false);
  748. // // if (retries < WIFI_MANAGER_MAX_RETRY) {
  749. // // ESP_LOGD(TAG, "Issuing ORDER_CONNECT_STA to retry connection.");
  750. // // retries++;
  751. // // wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_AUTO_RECONNECT);
  752. // // } else {
  753. // // /* In this scenario the connection was lost beyond repair: kick start the AP! */
  754. // // retries = 0;
  755. // // wifi_mode_t mode;
  756. // // ESP_LOGW(TAG, "All connect retry attempts failed.");
  757. // // /* put us in softAP mode first */
  758. // // esp_wifi_get_mode(&mode);
  759. // // /* if it was a restore attempt connection, we clear the bit */
  760. // // network_manager_clear_flag(WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
  761. // // if (WIFI_MODE_APSTA != mode) {
  762. // // STA_duration = STA_POLLING_MIN;
  763. // // wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_MAX_FAILED);
  764. // // } else if (STA_duration < STA_POLLING_MAX) {
  765. // // STA_duration *= 1.25;
  766. // // }
  767. // // xTimerChangePeriod(STA_timer, pdMS_TO_TICKS(STA_duration), portMAX_DELAY);
  768. // // xTimerStart(STA_timer, portMAX_DELAY);
  769. // // ESP_LOGD(TAG, "STA search slow polling of %d", STA_duration);
  770. // // }
  771. // }
  772. // return err;
  773. // }
  774. char* get_disconnect_code_desc(uint8_t reason) {
  775. switch (reason) {
  776. case 1:
  777. return "UNSPECIFIED";
  778. break;
  779. case 2:
  780. return "AUTH_EXPIRE";
  781. break;
  782. case 3:
  783. return "AUTH_LEAVE";
  784. break;
  785. case 4:
  786. return "ASSOC_EXPIRE";
  787. break;
  788. case 5:
  789. return "ASSOC_TOOMANY";
  790. break;
  791. case 6:
  792. return "NOT_AUTHED";
  793. break;
  794. case 7:
  795. return "NOT_ASSOCED";
  796. break;
  797. case 8:
  798. return "ASSOC_LEAVE";
  799. break;
  800. case 9:
  801. return "ASSOC_NOT_AUTHED";
  802. break;
  803. case 10:
  804. return "DISASSOC_PWRCAP_BAD";
  805. break;
  806. case 11:
  807. return "DISASSOC_SUPCHAN_BAD";
  808. break;
  809. case 12:
  810. return "<n/a>";
  811. break;
  812. case 13:
  813. return "IE_INVALID";
  814. break;
  815. case 14:
  816. return "MIC_FAILURE";
  817. break;
  818. case 15:
  819. return "4WAY_HANDSHAKE_TIMEOUT";
  820. break;
  821. case 16:
  822. return "GROUP_KEY_UPDATE_TIMEOUT";
  823. break;
  824. case 17:
  825. return "IE_IN_4WAY_DIFFERS";
  826. break;
  827. case 18:
  828. return "GROUP_CIPHER_INVALID";
  829. break;
  830. case 19:
  831. return "PAIRWISE_CIPHER_INVALID";
  832. break;
  833. case 20:
  834. return "AKMP_INVALID";
  835. break;
  836. case 21:
  837. return "UNSUPP_RSN_IE_VERSION";
  838. break;
  839. case 22:
  840. return "INVALID_RSN_IE_CAP";
  841. break;
  842. case 23:
  843. return "802_1X_AUTH_FAILED";
  844. break;
  845. case 24:
  846. return "CIPHER_SUITE_REJECTED";
  847. break;
  848. case 200:
  849. return "BEACON_TIMEOUT";
  850. break;
  851. case 201:
  852. return "NO_AP_FOUND";
  853. break;
  854. case 202:
  855. return "AUTH_FAIL";
  856. break;
  857. case 203:
  858. return "ASSOC_FAIL";
  859. break;
  860. case 204:
  861. return "HANDSHAKE_TIMEOUT";
  862. break;
  863. default:
  864. return "UNKNOWN";
  865. break;
  866. }
  867. return "";
  868. }
  869. static void network_manager_wifi_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  870. ip_event_got_ip_t* s = NULL;
  871. tcpip_adapter_if_t index;
  872. esp_netif_ip_info_t* ip_info = NULL;
  873. if (event_base != IP_EVENT)
  874. return;
  875. switch (event_id) {
  876. case IP_EVENT_ETH_GOT_IP:
  877. case IP_EVENT_STA_GOT_IP:
  878. s = (ip_event_got_ip_t*)event_data;
  879. //tcpip_adapter_if_t index = s->if_index;
  880. network_manager_async_got_ip();
  881. ip_info = &s->ip_info;
  882. index = s->if_index;
  883. ESP_LOGI(TAG, "Got an IP address from interface #%i. IP=" IPSTR ", Gateway=" IPSTR ", NetMask=" IPSTR ", %s",
  884. index,
  885. IP2STR(&ip_info->ip),
  886. IP2STR(&ip_info->gw),
  887. IP2STR(&ip_info->netmask),
  888. s->ip_changed ? "Address was changed" : "Address unchanged");
  889. break;
  890. case IP_EVENT_STA_LOST_IP:
  891. ESP_LOGD(TAG, "IP_EVENT_STA_LOST_IP");
  892. break;
  893. case IP_EVENT_AP_STAIPASSIGNED:
  894. ESP_LOGD(TAG, "IP_EVENT_AP_STAIPASSIGNED");
  895. break;
  896. case IP_EVENT_GOT_IP6:
  897. ESP_LOGD(TAG, "IP_EVENT_GOT_IP6");
  898. break;
  899. default:
  900. break;
  901. }
  902. }