wifi_manager.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. Copyright (c) 2017-2019 Tony Pottier
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. @file wifi_manager.c
  19. @author Tony Pottier
  20. @brief Defines all functions necessary for esp32 to connect to a wifi/scan wifis
  21. Contains the freeRTOS task and all necessary support
  22. @see https://idyl.io
  23. @see https://github.com/tonyp7/esp32-wifi-manager
  24. */
  25. #include "wifi_manager.h"
  26. #include "platform_esp32.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdbool.h>
  31. #include "dns_server.h"
  32. #include "http_server.h"
  33. #include "esp_system.h"
  34. #include "freertos/FreeRTOS.h"
  35. #include "freertos/task.h"
  36. #include "freertos/event_groups.h"
  37. #include "esp_event_loop.h"
  38. #include "tcpip_adapter.h"
  39. #include "esp_event.h"
  40. #include "esp_wifi.h"
  41. #include "esp_wifi_types.h"
  42. #include "esp_log.h"
  43. #include "nvs.h"
  44. #include "nvs_flash.h"
  45. #include "mdns.h"
  46. #include "lwip/api.h"
  47. #include "lwip/err.h"
  48. #include "lwip/netdb.h"
  49. #include "lwip/ip4_addr.h"
  50. #include "esp_ota_ops.h"
  51. #include "esp_app_format.h"
  52. #include "driver/gpio.h"
  53. #include "driver/adc.h"
  54. #include "cJSON.h"
  55. #include "nvs_utilities.h"
  56. #ifndef RECOVERY_APPLICATION
  57. #define RECOVERY_APPLICATION 0
  58. #endif
  59. #ifndef SQUEEZELITE_ESP32_RELEASE_URL
  60. #define SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
  61. #endif
  62. #ifdef TAS575x
  63. #define JACK_GPIO 34
  64. #define JACK_LEVEL !gpio_get_level(JACK_GPIO)?"1":"0";
  65. #else
  66. #define JACK_LEVEL "N/A"
  67. #endif
  68. #define STR_OR_BLANK(p) p==NULL?"":p
  69. #define FREE_AND_NULL(p) if(p!=NULL){ free(p); p=NULL;}
  70. /* objects used to manipulate the main queue of events */
  71. QueueHandle_t wifi_manager_queue;
  72. SemaphoreHandle_t wifi_manager_json_mutex = NULL;
  73. SemaphoreHandle_t wifi_manager_sta_ip_mutex = NULL;
  74. char *wifi_manager_sta_ip = NULL;
  75. uint16_t ap_num = MAX_AP_NUM;
  76. wifi_ap_record_t *accessp_records=NULL;
  77. cJSON * accessp_cjson=NULL;
  78. char *ip_info_json = NULL;
  79. char *host_name = NULL;
  80. char * release_url=NULL;
  81. cJSON * ip_info_cjson=NULL;
  82. wifi_config_t* wifi_manager_config_sta = NULL;
  83. static update_reason_code_t last_update_reason_code=0;
  84. static int32_t total_connected_time=0;
  85. static int64_t last_connected=0;
  86. static uint16_t num_disconnect=0;
  87. void (**cb_ptr_arr)(void*) = NULL;
  88. /* @brief tag used for ESP serial console messages */
  89. static const char TAG[] = "wifi_manager";
  90. /* @brief task handle for the main wifi_manager task */
  91. static TaskHandle_t task_wifi_manager = NULL;
  92. /**
  93. * The actual WiFi settings in use
  94. */
  95. struct wifi_settings_t wifi_settings = {
  96. .sta_only = DEFAULT_STA_ONLY,
  97. .sta_power_save = DEFAULT_STA_POWER_SAVE,
  98. .sta_static_ip = 0
  99. };
  100. /* wifi scanner config */
  101. wifi_scan_config_t scan_config = {
  102. .ssid = 0,
  103. .bssid = 0,
  104. .channel = 0,
  105. .show_hidden = true
  106. };
  107. const char wifi_manager_nvs_namespace[] = "config";
  108. EventGroupHandle_t wifi_manager_event_group;
  109. /* @brief indicate that the ESP32 is currently connected. */
  110. const int WIFI_MANAGER_WIFI_CONNECTED_BIT = BIT0;
  111. const int WIFI_MANAGER_AP_STA_CONNECTED_BIT = BIT1;
  112. /* @brief Set automatically once the SoftAP is started */
  113. const int WIFI_MANAGER_AP_STARTED_BIT = BIT2;
  114. /* @brief When set, means a client requested to connect to an access point.*/
  115. const int WIFI_MANAGER_REQUEST_STA_CONNECT_BIT = BIT3;
  116. /* @brief This bit is set automatically as soon as a connection was lost */
  117. const int WIFI_MANAGER_STA_DISCONNECT_BIT = BIT4;
  118. /* @brief When set, means the wifi manager attempts to restore a previously saved connection at startup. */
  119. const int WIFI_MANAGER_REQUEST_RESTORE_STA_BIT = BIT5;
  120. /* @brief When set, means a client requested to disconnect from currently connected AP. */
  121. const int WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT = BIT6;
  122. /* @brief When set, means a scan is in progress */
  123. const int WIFI_MANAGER_SCAN_BIT = BIT7;
  124. /* @brief When set, means user requested for a disconnect */
  125. const int WIFI_MANAGER_REQUEST_DISCONNECT_BIT = BIT8;
  126. bool isGroupBitSet(uint8_t bit){
  127. EventBits_t uxBits= xEventGroupGetBits(wifi_manager_event_group);
  128. return (uxBits & bit);
  129. }
  130. void wifi_manager_refresh_ota_json(){
  131. wifi_manager_send_message(EVENT_REFRESH_OTA, NULL);
  132. }
  133. void wifi_manager_scan_async(){
  134. wifi_manager_send_message(ORDER_START_WIFI_SCAN, NULL);
  135. }
  136. void wifi_manager_disconnect_async(){
  137. wifi_manager_send_message(ORDER_DISCONNECT_STA, NULL);
  138. //xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT); TODO: delete
  139. }
  140. void wifi_manager_init_wifi(){
  141. /* event handler and event group for the wifi driver */
  142. wifi_manager_event_group = xEventGroupCreate();
  143. // Now Initialize the Wifi Stack
  144. tcpip_adapter_init();
  145. wifi_manager_event_group = xEventGroupCreate();
  146. ESP_ERROR_CHECK(esp_event_loop_create_default());
  147. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  148. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  149. wifi_manager_register_handlers();
  150. ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  151. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
  152. ESP_ERROR_CHECK( esp_wifi_start() );
  153. }
  154. void wifi_manager_start(){
  155. /* memory allocation */
  156. wifi_manager_queue = xQueueCreate( 3, sizeof( queue_message) );
  157. wifi_manager_json_mutex = xSemaphoreCreateMutex();
  158. wifi_manager_sta_ip_mutex = xSemaphoreCreateMutex();
  159. accessp_cjson = wifi_manager_clear_ap_list_json(&accessp_cjson);
  160. ip_info_json = NULL;
  161. ip_info_cjson = wifi_manager_clear_ip_info_json(&ip_info_cjson);
  162. wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));
  163. memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  164. memset(&wifi_settings, 0x00, sizeof(wifi_settings));
  165. cb_ptr_arr = malloc( sizeof( sizeof( void (*)( void* ) )) * MESSAGE_CODE_COUNT);
  166. for(int i=0; i<MESSAGE_CODE_COUNT; i++){
  167. cb_ptr_arr[i] = NULL;
  168. }
  169. wifi_manager_sta_ip = (char*)malloc(sizeof(char) * IP4ADDR_STRLEN_MAX);
  170. wifi_manager_safe_update_sta_ip_string(NULL);
  171. host_name = (char * )get_nvs_value_alloc_default(NVS_TYPE_STR, "host_name", "squeezelite-esp32", 0);
  172. char * release_url = (char * )get_nvs_value_alloc_default(NVS_TYPE_STR, "release_url", QUOTE(SQUEEZELITE_ESP32_RELEASE_URL), 0);
  173. if(release_url == NULL){
  174. ESP_LOGE(TAG,"Unable to retrieve the release url from nvs");
  175. }
  176. wifi_manager_init_wifi();
  177. /* Fetch configuration from nvs */
  178. wifi_manager_fetch_wifi_sta_config();
  179. /* start wifi manager task */
  180. xTaskCreate(&wifi_manager, "wifi_manager", 4096, NULL, WIFI_MANAGER_TASK_PRIORITY, &task_wifi_manager);
  181. }
  182. esp_err_t wifi_manager_save_sta_config(){
  183. nvs_handle handle;
  184. esp_err_t esp_err;
  185. ESP_LOGI(TAG, "About to save config to flash");
  186. if(wifi_manager_config_sta){
  187. esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
  188. if (esp_err != ESP_OK) {
  189. ESP_LOGE(TAG,"Unable to open name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
  190. return esp_err;
  191. }
  192. esp_err = nvs_set_blob(handle, "ssid", wifi_manager_config_sta->sta.ssid, sizeof(wifi_manager_config_sta->sta.ssid));
  193. if (esp_err != ESP_OK) {
  194. ESP_LOGE(TAG,"Unable to save ssid in name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
  195. return esp_err;
  196. }
  197. esp_err = nvs_set_blob(handle, "password", wifi_manager_config_sta->sta.password, sizeof(wifi_manager_config_sta->sta.password));
  198. if (esp_err != ESP_OK) {
  199. ESP_LOGE(TAG,"Unable to save password in name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
  200. return esp_err;
  201. }
  202. esp_err = nvs_set_blob(handle, "settings", &wifi_settings, sizeof(wifi_settings));
  203. if (esp_err != ESP_OK) {
  204. ESP_LOGE(TAG,"Unable to save wifi_settings in name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
  205. return esp_err;
  206. }
  207. esp_err = nvs_commit(handle);
  208. if (esp_err != ESP_OK) {
  209. ESP_LOGE(TAG,"Unable to commit changes. Error %s", esp_err_to_name(esp_err));
  210. return esp_err;
  211. }
  212. nvs_close(handle);
  213. ESP_LOGD(TAG, "wifi_manager_wrote wifi_sta_config: ssid:%s password:%s",wifi_manager_config_sta->sta.ssid,wifi_manager_config_sta->sta.password);
  214. ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: sta_static_ip (0 = dhcp client, 1 = static ip): %i",wifi_settings.sta_static_ip);
  215. ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: sta_ip_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip));
  216. ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: sta_gw_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw));
  217. ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: sta_netmask: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
  218. }
  219. return ESP_OK;
  220. }
  221. #define RETRIEVE_CONFIG_STRING_VALUE(name,def,var) value = get_nvs_value_alloc_default(NVS_TYPE_STR, name, def, 0); if(value!=NULL){ strlcpy(var, value, sizeof(var)); free(value); value=NULL;}
  222. bool wifi_manager_fetch_wifi_sta_config(){
  223. nvs_handle handle;
  224. esp_err_t esp_err;
  225. ESP_LOGD(TAG,"Fetching wifi sta config.");
  226. if(nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle) == ESP_OK){
  227. if(wifi_manager_config_sta == NULL){
  228. wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));
  229. }
  230. memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  231. /* ssid */
  232. size_t sz = sizeof(wifi_manager_config_sta->sta.ssid);
  233. uint8_t *buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
  234. esp_err = nvs_get_blob(handle, "ssid", buff, &sz);
  235. if(esp_err != ESP_OK){
  236. ESP_LOGI(TAG,"No ssid found in nvs.");
  237. free(buff);
  238. nvs_close(handle);
  239. return false;
  240. }
  241. memcpy(wifi_manager_config_sta->sta.ssid, buff, sz);
  242. FREE_AND_NULL(buff);
  243. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_sta_config: ssid:%s ",wifi_manager_config_sta->sta.ssid);
  244. /* password */
  245. sz = sizeof(wifi_manager_config_sta->sta.password);
  246. buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
  247. esp_err = nvs_get_blob(handle, "password", buff, &sz);
  248. if(esp_err != ESP_OK){
  249. // Don't take this as an error. This could be an opened access point?
  250. ESP_LOGW(TAG,"No wifi password found in nvs");
  251. }
  252. else {
  253. memcpy(wifi_manager_config_sta->sta.password, buff, sz);
  254. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_sta_config: password:%s",wifi_manager_config_sta->sta.password);
  255. }
  256. FREE_AND_NULL(buff);
  257. /* settings */
  258. sz = sizeof(wifi_settings);
  259. buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
  260. esp_err = nvs_get_blob(handle, "settings", buff, &sz);
  261. if(esp_err != ESP_OK){
  262. // SSID was found, we should have some settings as well. Log this as an error
  263. ESP_LOGW(TAG,"No wifi settings found in nvs. Freeing nvs buffer");
  264. FREE_AND_NULL(buff);
  265. ESP_LOGD(TAG,"Closing nvs Handle");
  266. nvs_close(handle);
  267. ESP_LOGD(TAG,"load sta config done");
  268. return wifi_manager_config_sta->sta.ssid[0] != '\0';
  269. }
  270. if(sz!=sizeof(wifi_settings)){
  271. ESP_LOGW(TAG,"Unable to retrieve settings buffer from nvs. Size did not match");
  272. }
  273. else {
  274. ESP_LOGD(TAG,"Copying configuration restored from nvs");
  275. memcpy(&wifi_settings, buff, sz);
  276. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_only (0 = APSTA, 1 = STA when connected):%i",wifi_settings.sta_only);
  277. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_power_save (1 = yes):%i",wifi_settings.sta_power_save);
  278. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_static_ip (0 = dhcp client, 1 = static ip):%i",wifi_settings.sta_static_ip);
  279. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_static_ip_config: IP: %s , GW: %s , Mask: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip), ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw), ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
  280. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_ip_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip));
  281. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_gw_addr: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw));
  282. ESP_LOGI(TAG, "wifi_manager_fetch_wifi_settings: sta_netmask: %s", ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
  283. }
  284. FREE_AND_NULL(buff);
  285. nvs_close(handle);
  286. return wifi_manager_config_sta->sta.ssid[0] != '\0';
  287. }
  288. else{
  289. ESP_LOGE(TAG,"Failed to open nvs namespace %s.",wifi_manager_nvs_namespace);
  290. return false;
  291. }
  292. }
  293. cJSON * wifi_manager_get_new_json(cJSON **old){
  294. ESP_LOGD(TAG,"wifi_manager_get_new_json called");
  295. cJSON * root=*old;
  296. if(root!=NULL){
  297. cJSON_Delete(root);
  298. *old=NULL;
  299. }
  300. ESP_LOGD(TAG,"wifi_manager_get_new_json done");
  301. return cJSON_CreateObject();
  302. }
  303. cJSON * wifi_manager_get_new_array_json(cJSON **old){
  304. ESP_LOGD(TAG,"wifi_manager_get_new_array_json called");
  305. cJSON * root=*old;
  306. if(root!=NULL){
  307. cJSON_Delete(root);
  308. *old=NULL;
  309. }
  310. ESP_LOGD(TAG,"wifi_manager_get_new_array_json done");
  311. return cJSON_CreateArray();
  312. }
  313. cJSON * wifi_manager_get_basic_info(cJSON **old){
  314. const esp_app_desc_t* desc = esp_ota_get_app_description();
  315. ESP_LOGD(TAG,"wifi_manager_get_basic_info called");
  316. cJSON *root = wifi_manager_get_new_json(old);
  317. cJSON_AddItemToObject(root, "project_name", cJSON_CreateString(desc->project_name));
  318. cJSON_AddItemToObject(root, "version", cJSON_CreateString(desc->version));
  319. if(release_url !=NULL) cJSON_AddItemToObject(root, "release_url", cJSON_CreateString(release_url));
  320. cJSON_AddNumberToObject(root,"recovery", RECOVERY_APPLICATION );
  321. cJSON_AddItemToObject(root, "ota_dsc", cJSON_CreateString(ota_get_status()));
  322. cJSON_AddNumberToObject(root,"ota_pct", ota_get_pct_complete() );
  323. cJSON_AddItemToObject(root, "Jack", cJSON_CreateString(JACK_LEVEL));
  324. cJSON_AddNumberToObject(root,"Voltage", adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1);
  325. cJSON_AddNumberToObject(root,"disconnect_count", num_disconnect );
  326. cJSON_AddNumberToObject(root,"avg_conn_time", num_disconnect>0?(total_connected_time/num_disconnect):0 );
  327. ESP_LOGV(TAG,"wifi_manager_get_basic_info done");
  328. return root;
  329. }
  330. cJSON * wifi_manager_clear_ip_info_json(cJSON **old){
  331. ESP_LOGV(TAG,"wifi_manager_clear_ip_info_json called");
  332. cJSON *root = wifi_manager_get_basic_info(old);
  333. ESP_LOGV(TAG,"wifi_manager_clear_ip_info_json done");
  334. return root;
  335. }
  336. cJSON * wifi_manager_clear_ap_list_json(cJSON **old){
  337. ESP_LOGV(TAG,"wifi_manager_clear_ap_list_json called");
  338. cJSON *root = wifi_manager_get_new_array_json(old);
  339. ESP_LOGV(TAG,"wifi_manager_clear_ap_list_json done");
  340. return root;
  341. }
  342. void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code){
  343. ESP_LOGD(TAG,"wifi_manager_generate_ip_info_json called");
  344. wifi_config_t *config = wifi_manager_get_wifi_sta_config();
  345. ip_info_cjson = wifi_manager_get_basic_info(&ip_info_cjson);
  346. if(update_reason_code == UPDATE_OTA) {
  347. update_reason_code = last_update_reason_code;
  348. }
  349. else {
  350. last_update_reason_code = update_reason_code;
  351. }
  352. cJSON_AddNumberToObject(ip_info_cjson, "urc", update_reason_code);
  353. if(config){
  354. cJSON_AddItemToObject(ip_info_cjson, "ssid", cJSON_CreateString((char *)config->sta.ssid));
  355. if(update_reason_code == UPDATE_CONNECTION_OK){
  356. /* rest of the information is copied after the ssid */
  357. tcpip_adapter_ip_info_t ip_info;
  358. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  359. cJSON_AddItemToObject(ip_info_cjson, "ip", cJSON_CreateString(ip4addr_ntoa(&ip_info.ip)));
  360. cJSON_AddItemToObject(ip_info_cjson, "netmask", cJSON_CreateString(ip4addr_ntoa(&ip_info.netmask)));
  361. cJSON_AddItemToObject(ip_info_cjson, "gw", cJSON_CreateString(ip4addr_ntoa(&ip_info.gw)));
  362. }
  363. }
  364. ESP_LOGV(TAG,"wifi_manager_generate_ip_info_json done");
  365. }
  366. #define LOCAL_MAC_SIZE 20
  367. char * get_mac_string(uint8_t mac[6]){
  368. char * macStr=malloc(LOCAL_MAC_SIZE);
  369. memset(macStr, 0x00, LOCAL_MAC_SIZE);
  370. snprintf(macStr, LOCAL_MAC_SIZE-1,MACSTR, MAC2STR(mac));
  371. return macStr;
  372. }
  373. void wifi_manager_generate_access_points_json(cJSON ** ap_list){
  374. *ap_list = wifi_manager_get_new_array_json(ap_list);
  375. if(*ap_list==NULL) return;
  376. for(int i=0; i<ap_num;i++){
  377. cJSON * ap = cJSON_CreateObject();
  378. if(ap == NULL) {
  379. ESP_LOGE(TAG,"Unable to allocate memory for access point entry #%d",i);
  380. return;
  381. }
  382. cJSON * radio = cJSON_CreateObject();
  383. if(radio == NULL) {
  384. ESP_LOGE(TAG,"Unable to allocate memory for access point entry #%d",i);
  385. cJSON_Delete(ap);
  386. return;
  387. }
  388. wifi_ap_record_t ap_rec = accessp_records[i];
  389. cJSON_AddNumberToObject(ap, "chan", ap_rec.primary);
  390. cJSON_AddNumberToObject(ap, "rssi", ap_rec.rssi);
  391. cJSON_AddNumberToObject(ap, "auth", ap_rec.authmode);
  392. cJSON_AddItemToObject(ap, "ssid", cJSON_CreateString((char *)ap_rec.ssid));
  393. char * bssid = get_mac_string(ap_rec.bssid);
  394. cJSON_AddItemToObject(ap, "bssid", cJSON_CreateString(STR_OR_BLANK(bssid)));
  395. FREE_AND_NULL(bssid);
  396. cJSON_AddNumberToObject(radio, "b", ap_rec.phy_11b?1:0);
  397. cJSON_AddNumberToObject(radio, "g", ap_rec.phy_11g?1:0);
  398. cJSON_AddNumberToObject(radio, "n", ap_rec.phy_11n?1:0);
  399. cJSON_AddNumberToObject(radio, "low_rate", ap_rec.phy_lr?1:0);
  400. cJSON_AddItemToObject(ap,"radio", radio);
  401. cJSON_AddItemToArray(*ap_list, ap);
  402. ESP_LOGD(TAG,"New access point found: %s", cJSON_Print(ap));
  403. }
  404. ESP_LOGD(TAG,"Full access point list: %s", cJSON_Print(*ap_list));
  405. }
  406. bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait){
  407. if(wifi_manager_sta_ip_mutex){
  408. if( xSemaphoreTake( wifi_manager_sta_ip_mutex, xTicksToWait ) == pdTRUE ) {
  409. return true;
  410. }
  411. else{
  412. return false;
  413. }
  414. }
  415. else{
  416. return false;
  417. }
  418. }
  419. void wifi_manager_unlock_sta_ip_string(){
  420. xSemaphoreGive( wifi_manager_sta_ip_mutex );
  421. }
  422. void wifi_manager_safe_update_sta_ip_string(struct ip4_addr * ip4){
  423. if(wifi_manager_lock_sta_ip_string(portMAX_DELAY)){
  424. strcpy(wifi_manager_sta_ip, ip4!=NULL?ip4addr_ntoa(ip4):"0.0.0.0");
  425. ESP_LOGI(TAG, "Set STA IP String to: %s", wifi_manager_sta_ip);
  426. wifi_manager_unlock_sta_ip_string();
  427. }
  428. }
  429. char* wifi_manager_get_sta_ip_string(){
  430. return wifi_manager_sta_ip;
  431. }
  432. bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait){
  433. ESP_LOGV(TAG,"Locking json buffer");
  434. if(wifi_manager_json_mutex){
  435. if( xSemaphoreTake( wifi_manager_json_mutex, xTicksToWait ) == pdTRUE ) {
  436. ESP_LOGV(TAG,"Json buffer locked!");
  437. return true;
  438. }
  439. else{
  440. ESP_LOGE(TAG,"Semaphore take failed. Unable to lock json buffer mutex");
  441. return false;
  442. }
  443. }
  444. else{
  445. ESP_LOGV(TAG,"Unable to lock json buffer mutex");
  446. return false;
  447. }
  448. }
  449. void wifi_manager_unlock_json_buffer(){
  450. ESP_LOGV(TAG,"Unlocking json buffer!");
  451. xSemaphoreGive( wifi_manager_json_mutex );
  452. }
  453. char* wifi_manager_get_ap_list_json(){
  454. return cJSON_Print(accessp_cjson);
  455. }
  456. static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
  457. // if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  458. // led_blink_pushed(LED_GREEN, 250, 250);
  459. // esp_wifi_connect();
  460. // xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  461. // } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  462. // led_unpush(LED_GREEN);
  463. // xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  464. // }
  465. if(event_base== WIFI_EVENT){
  466. switch(event_id) {
  467. case WIFI_EVENT_WIFI_READY:
  468. ESP_LOGI(TAG, "WIFI_EVENT_WIFI_READY");
  469. break;
  470. case WIFI_EVENT_SCAN_DONE:
  471. ESP_LOGD(TAG, "WIFI_EVENT_SCAN_DONE");
  472. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_SCAN_BIT);
  473. wifi_manager_send_message(EVENT_SCAN_DONE, NULL);
  474. break;
  475. case WIFI_EVENT_STA_AUTHMODE_CHANGE:
  476. ESP_LOGI(TAG, "WIFI_EVENT_STA_AUTHMODE_CHANGE");
  477. // structwifi_event_sta_authmode_change_t
  478. // Argument structure for WIFI_EVENT_STA_AUTHMODE_CHANGE event
  479. //
  480. // Public Members
  481. //
  482. // wifi_auth_mode_told_mode
  483. // the old auth mode of AP
  484. //
  485. // wifi_auth_mode_tnew_mode
  486. // the new auth mode of AP
  487. break;
  488. case WIFI_EVENT_AP_START:
  489. ESP_LOGI(TAG, "WIFI_EVENT_AP_START");
  490. xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_AP_STARTED_BIT);
  491. break;
  492. case WIFI_EVENT_AP_STOP:
  493. ESP_LOGD(TAG,"WIFI_EVENT_AP_STOP");
  494. break;
  495. case WIFI_EVENT_AP_PROBEREQRECVED:{
  496. // wifi_event_ap_probe_req_rx_t
  497. // Argument structure for WIFI_EVENT_AP_PROBEREQRECVED event
  498. //
  499. // Public Members
  500. //
  501. // int rssi
  502. // Received probe request signal strength
  503. //
  504. // uint8_t mac[6]
  505. // MAC address of the station which send probe request
  506. wifi_event_ap_probe_req_rx_t * s =(wifi_event_ap_probe_req_rx_t*)event_data;
  507. char * mac = get_mac_string(s->mac);
  508. ESP_LOGD(TAG,"WIFI_EVENT_AP_PROBEREQRECVED. RSSI: %d, MAC: %s",s->rssi, STR_OR_BLANK(mac));
  509. FREE_AND_NULL(mac);
  510. }
  511. break;
  512. case WIFI_EVENT_STA_WPS_ER_SUCCESS:
  513. ESP_LOGD(TAG,"WIFI_EVENT_STA_WPS_ER_SUCCESS");
  514. break;
  515. case WIFI_EVENT_STA_WPS_ER_FAILED:
  516. ESP_LOGD(TAG,"WIFI_EVENT_STA_WPS_ER_FAILED");
  517. break;
  518. case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
  519. ESP_LOGD(TAG,"WIFI_EVENT_STA_WPS_ER_TIMEOUT");
  520. break;
  521. case WIFI_EVENT_STA_WPS_ER_PIN:
  522. ESP_LOGD(TAG,"WIFI_EVENT_STA_WPS_ER_PIN");
  523. break;
  524. case WIFI_EVENT_AP_STACONNECTED: /* a user disconnected from the SoftAP */
  525. ESP_LOGI(TAG, "WIFI_EVENT_AP_STACONNECTED");
  526. xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT);
  527. break;
  528. case WIFI_EVENT_AP_STADISCONNECTED:
  529. ESP_LOGI(TAG, "WIFI_EVENT_AP_STADISCONNECTED");
  530. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT);
  531. break;
  532. case WIFI_EVENT_STA_START:
  533. ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
  534. break;
  535. case WIFI_EVENT_STA_STOP:
  536. ESP_LOGI(TAG, "WIFI_EVENT_STA_STOP");
  537. break;
  538. case WIFI_EVENT_STA_CONNECTED:{
  539. // structwifi_event_sta_connected_t
  540. // Argument structure for WIFI_EVENT_STA_CONNECTED event
  541. //
  542. // Public Members
  543. //
  544. // uint8_t ssid[32]
  545. // SSID of connected AP
  546. //
  547. // uint8_t ssid_len
  548. // SSID length of connected AP
  549. //
  550. // uint8_t bssid[6]
  551. // BSSID of connected AP
  552. //
  553. // uint8_t channel
  554. // channel of connected AP
  555. //
  556. // wifi_auth_mode_tauthmode
  557. // authentication mode used by AP
  558. //, get_mac_string(EVENT_HANDLER_ARG_FIELD(wifi_event_ap_probe_req_rx_t, mac)));
  559. ESP_LOGD(TAG, "WIFI_EVENT_STA_CONNECTED. ");
  560. wifi_event_sta_connected_t * s =(wifi_event_sta_connected_t*)event_data;
  561. char * bssid = get_mac_string(s->bssid);
  562. char * ssid = strdup((char*)s->ssid);
  563. ESP_LOGI(TAG, "WIFI_EVENT_STA_CONNECTED. Channel: %d, Access point: %s, BSSID: %s ", s->channel, STR_OR_BLANK(ssid), (bssid));
  564. FREE_AND_NULL(bssid);
  565. FREE_AND_NULL(ssid);
  566. }
  567. break;
  568. case WIFI_EVENT_STA_DISCONNECTED:{
  569. // structwifi_event_sta_disconnected_t
  570. // Argument structure for WIFI_EVENT_STA_DISCONNECTED event
  571. //
  572. // Public Members
  573. //
  574. // uint8_t ssid[32]
  575. // SSID of disconnected AP
  576. //
  577. // uint8_t ssid_len
  578. // SSID length of disconnected AP
  579. //
  580. // uint8_t bssid[6]
  581. // BSSID of disconnected AP
  582. //
  583. // uint8_t reason
  584. // reason of disconnection
  585. wifi_event_sta_disconnected_t * s =(wifi_event_sta_disconnected_t*)event_data;
  586. char * bssid = get_mac_string(s->bssid);
  587. ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED. From BSSID: %s, reason code: %d", STR_OR_BLANK(bssid),s->reason);
  588. FREE_AND_NULL(bssid);
  589. if(last_connected>0) total_connected_time+=((esp_timer_get_time()-last_connected)/(1000*1000));
  590. last_connected = 0;
  591. num_disconnect++;
  592. ESP_LOGW(TAG,"Wifi disconnected. Number of disconnects: %d, Average time connected: %d", num_disconnect, num_disconnect>0?(total_connected_time/num_disconnect):0);
  593. /* 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 */
  594. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_WIFI_CONNECTED_BIT | WIFI_MANAGER_SCAN_BIT);
  595. // We want to process this message asynchronously, so make sure we copy the event buffer
  596. ESP_LOGD(TAG,"Preparing to trigger event EVENT_STA_DISCONNECTED ");
  597. void * parm=malloc(sizeof(wifi_event_sta_disconnected_t));
  598. memcpy(parm,event_data,sizeof(wifi_event_sta_disconnected_t));
  599. ESP_LOGD(TAG,"Triggering EVENT_STA_DISCONNECTED ");
  600. /* post disconnect event with reason code */
  601. wifi_manager_send_message(EVENT_STA_DISCONNECTED, parm );
  602. }
  603. break;
  604. default:
  605. break;
  606. }
  607. }
  608. else if(event_base== IP_EVENT){
  609. switch (event_id) {
  610. case IP_EVENT_STA_GOT_IP:{
  611. // structip_event_got_ip_t
  612. // tcpip_adapter_if_t if_index; /*!< Interface for which the event is received */
  613. // tcpip_adapter_ip6_info_t ip6_info; /*!< IPv6 address of the interface */
  614. // // Event structure for IP_EVENT_STA_GOT_IP, IP_EVENT_ETH_GOT_IP events
  615. //
  616. // Public Members
  617. //
  618. // tcpip_adapter_if_tif_index
  619. // Interface for which the event is received
  620. //
  621. // tcpip_adapter_ip_info_t ip_info
  622. // IP address, netmask, gatway IP address
  623. //
  624. // bool ip_changed
  625. // Whether the assigned IP has changed or not
  626. ip_event_got_ip_t * s =(ip_event_got_ip_t*)event_data;
  627. tcpip_adapter_if_t index = s->if_index;
  628. char * ip=strdup(ip4addr_ntoa(&(s->ip_info.ip)));
  629. char * gw=strdup(ip4addr_ntoa(&(s->ip_info.gw)));
  630. char * nm=strdup(ip4addr_ntoa(&(s->ip_info.netmask)));
  631. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP. IP=%s, Gateway=%s, NetMask=%s, Interface: %s %s",
  632. ip,
  633. gw,
  634. nm,
  635. index==TCPIP_ADAPTER_IF_STA?"TCPIP_ADAPTER_IF_STA":index==TCPIP_ADAPTER_IF_AP?"TCPIP_ADAPTER_IF_AP":index==TCPIP_ADAPTER_IF_ETH?"TCPIP_ADAPTER_IF_ETH":"Unknown",
  636. s->ip_changed?"Address was changed":"Address unchanged");
  637. FREE_AND_NULL(ip);
  638. FREE_AND_NULL(gw);
  639. FREE_AND_NULL(nm);
  640. xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_WIFI_CONNECTED_BIT);
  641. last_connected = esp_timer_get_time();
  642. void * parm=malloc(sizeof(ip_event_got_ip_t));
  643. memcpy(parm,event_data,sizeof(ip_event_got_ip_t));
  644. wifi_manager_send_message(EVENT_STA_GOT_IP, parm );
  645. }
  646. break;
  647. case IP_EVENT_STA_LOST_IP:
  648. ESP_LOGI(TAG, "IP_EVENT_STA_LOST_IP");
  649. break;
  650. case IP_EVENT_AP_STAIPASSIGNED:
  651. ESP_LOGI(TAG, "IP_EVENT_AP_STAIPASSIGNED");
  652. break;
  653. case IP_EVENT_GOT_IP6:
  654. ESP_LOGI(TAG, "IP_EVENT_GOT_IP6");
  655. break;
  656. case IP_EVENT_ETH_GOT_IP:
  657. ESP_LOGI(TAG, "IP_EVENT_ETH_GOT_IP");
  658. break;
  659. default:
  660. break;
  661. }
  662. }
  663. }
  664. wifi_config_t* wifi_manager_get_wifi_sta_config(){
  665. return wifi_manager_config_sta;
  666. }
  667. void wifi_manager_connect_async(){
  668. /* in order to avoid a false positive on the front end app we need to quickly flush the ip json
  669. * There'se a risk the front end sees an IP or a password error when in fact
  670. * it's a remnant from a previous connection
  671. */
  672. if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
  673. ip_info_cjson= wifi_manager_clear_ip_info_json(&ip_info_cjson);
  674. wifi_manager_unlock_json_buffer();
  675. }
  676. wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_USER);
  677. }
  678. void set_status_message(message_severity_t severity, const char * message){
  679. if(ip_info_cjson==NULL){
  680. ip_info_cjson = wifi_manager_get_new_json(&ip_info_cjson);
  681. }
  682. if(ip_info_cjson==NULL){
  683. ESP_LOGE(TAG,"Error setting status message. Unable to allocate cJSON.");
  684. return;
  685. }
  686. cJSON * item=cJSON_GetObjectItem(ip_info_cjson, "message");
  687. item = wifi_manager_get_new_json(&item);
  688. cJSON_AddItemToObject(item, "severity", cJSON_CreateString(severity==INFO?"INFO":severity==WARNING?"WARNING":severity==ERROR?"ERROR":"" ));
  689. cJSON_AddItemToObject(item, "text", cJSON_CreateString(message));
  690. }
  691. char* wifi_manager_get_ip_info_json(){
  692. return cJSON_Print(ip_info_cjson);
  693. }
  694. void wifi_manager_destroy(){
  695. vTaskDelete(task_wifi_manager);
  696. task_wifi_manager = NULL;
  697. free(host_name);
  698. /* heap buffers */
  699. free(ip_info_json);
  700. free(release_url);
  701. cJSON_Delete(ip_info_cjson);
  702. cJSON_Delete(accessp_cjson);
  703. ip_info_cjson=NULL;
  704. accessp_cjson=NULL;
  705. free(wifi_manager_sta_ip);
  706. wifi_manager_sta_ip = NULL;
  707. if(wifi_manager_config_sta){
  708. free(wifi_manager_config_sta);
  709. wifi_manager_config_sta = NULL;
  710. }
  711. /* RTOS objects */
  712. vSemaphoreDelete(wifi_manager_json_mutex);
  713. wifi_manager_json_mutex = NULL;
  714. vSemaphoreDelete(wifi_manager_sta_ip_mutex);
  715. wifi_manager_sta_ip_mutex = NULL;
  716. vEventGroupDelete(wifi_manager_event_group);
  717. wifi_manager_event_group = NULL;
  718. vQueueDelete(wifi_manager_queue);
  719. wifi_manager_queue = NULL;
  720. }
  721. void wifi_manager_filter_unique( wifi_ap_record_t * aplist, uint16_t * aps) {
  722. int total_unique;
  723. wifi_ap_record_t * first_free;
  724. total_unique=*aps;
  725. first_free=NULL;
  726. for(int i=0; i<*aps-1;i++) {
  727. wifi_ap_record_t * ap = &aplist[i];
  728. /* skip the previously removed APs */
  729. if (ap->ssid[0] == 0) continue;
  730. /* remove the identical SSID+authmodes */
  731. for(int j=i+1; j<*aps;j++) {
  732. wifi_ap_record_t * ap1 = &aplist[j];
  733. if ( (strcmp((const char *)ap->ssid, (const char *)ap1->ssid)==0) &&
  734. (ap->authmode == ap1->authmode) ) { /* same SSID, different auth mode is skipped */
  735. /* save the rssi for the display */
  736. if ((ap1->rssi) > (ap->rssi)) ap->rssi=ap1->rssi;
  737. /* clearing the record */
  738. memset(ap1,0, sizeof(wifi_ap_record_t));
  739. }
  740. }
  741. }
  742. /* reorder the list so APs follow each other in the list */
  743. for(int i=0; i<*aps;i++) {
  744. wifi_ap_record_t * ap = &aplist[i];
  745. /* skipping all that has no name */
  746. if (ap->ssid[0] == 0) {
  747. /* mark the first free slot */
  748. if (first_free==NULL) first_free=ap;
  749. total_unique--;
  750. continue;
  751. }
  752. if (first_free!=NULL) {
  753. memcpy(first_free, ap, sizeof(wifi_ap_record_t));
  754. memset(ap,0, sizeof(wifi_ap_record_t));
  755. /* find the next free slot */
  756. for(int j=0; j<*aps;j++) {
  757. if (aplist[j].ssid[0]==0) {
  758. first_free=&aplist[j];
  759. break;
  760. }
  761. }
  762. }
  763. }
  764. /* update the length of the list */
  765. *aps = total_unique;
  766. }
  767. BaseType_t wifi_manager_send_message_to_front(message_code_t code, void *param){
  768. queue_message msg;
  769. msg.code = code;
  770. msg.param = param;
  771. return xQueueSendToFront( wifi_manager_queue, &msg, portMAX_DELAY);
  772. }
  773. BaseType_t wifi_manager_send_message(message_code_t code, void *param){
  774. queue_message msg;
  775. msg.code = code;
  776. msg.param = param;
  777. return xQueueSend( wifi_manager_queue, &msg, portMAX_DELAY);
  778. }
  779. void wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) ){
  780. if(cb_ptr_arr && message_code < MESSAGE_CODE_COUNT){
  781. cb_ptr_arr[message_code] = func_ptr;
  782. }
  783. }
  784. void wifi_manager_register_handlers(){
  785. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_WIFI_READY, &event_handler, NULL));
  786. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &event_handler, NULL));
  787. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_AUTHMODE_CHANGE, &event_handler, NULL));
  788. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, &event_handler, NULL));
  789. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STOP, &event_handler, NULL));
  790. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_PROBEREQRECVED, &event_handler, NULL));
  791. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_SUCCESS, &event_handler, NULL));
  792. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, &event_handler, NULL));
  793. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_TIMEOUT, &event_handler, NULL));
  794. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_PIN, &event_handler, NULL));
  795. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &event_handler, NULL ));
  796. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &event_handler, NULL));
  797. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, &event_handler, NULL));
  798. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_STOP, &event_handler, NULL));
  799. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &event_handler, NULL));
  800. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL));
  801. // ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT, &event_handler, NULL));
  802. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  803. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_LOST_IP, &event_handler, NULL));
  804. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &event_handler, NULL));
  805. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &event_handler, NULL));
  806. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL));
  807. }
  808. void wifi_manager_config_ap(){
  809. /* SoftAP - Wifi Access Point configuration setup */
  810. tcpip_adapter_ip_info_t info;
  811. memset(&info, 0x00, sizeof(info));
  812. char * value = NULL;
  813. wifi_config_t ap_config = {
  814. .ap = {
  815. .ssid_len = 0,
  816. },
  817. };
  818. ESP_LOGD(TAG,"Configuring Access Point.");
  819. ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); /* stop AP DHCP server */
  820. value = get_nvs_value_alloc_default(NVS_TYPE_STR, "ap_ssid", CONFIG_DEFAULT_AP_SSID, 0);
  821. if(value!=NULL){
  822. strlcpy((char *)ap_config.ap.ssid, value,sizeof(ap_config.ap.ssid) );
  823. ESP_LOGD(TAG,"AP SSID: %s", (char *)ap_config.ap.ssid);
  824. }
  825. FREE_AND_NULL(value);
  826. value = get_nvs_value_alloc_default(NVS_TYPE_STR, "ap_pwd", DEFAULT_AP_PASSWORD, 0);
  827. if(value!=NULL){
  828. strlcpy((char *)ap_config.ap.password, value,sizeof(ap_config.ap.password) );
  829. ESP_LOGD(TAG,"AP Password: %s", (char *)ap_config.ap.password);
  830. }
  831. FREE_AND_NULL(value);
  832. value = get_nvs_value_alloc_default(NVS_TYPE_STR, "ap_ip_address", DEFAULT_AP_IP, 0);
  833. if(value!=NULL){
  834. ESP_LOGD(TAG,"IP Address: %s", value);
  835. inet_pton(AF_INET,value, &info.ip); /* access point is on a static IP */
  836. }
  837. FREE_AND_NULL(value);
  838. value = get_nvs_value_alloc_default(NVS_TYPE_STR, "ap_ip_gateway", CONFIG_DEFAULT_AP_GATEWAY, 0);
  839. if(value!=NULL){
  840. ESP_LOGD(TAG,"Gateway: %s", value);
  841. inet_pton(AF_INET,value, &info.gw); /* access point is on a static IP */
  842. }
  843. FREE_AND_NULL(value);
  844. value = get_nvs_value_alloc_default(NVS_TYPE_STR, "ap_ip_netmask", CONFIG_DEFAULT_AP_NETMASK, 0);
  845. if(value!=NULL){
  846. ESP_LOGD(TAG,"Netmask: %s", value);
  847. inet_pton(AF_INET,value, &info.netmask); /* access point is on a static IP */
  848. }
  849. FREE_AND_NULL(value);
  850. value = get_nvs_value_alloc_default(NVS_TYPE_STR, "ap_channel", STR(CONFIG_DEFAULT_AP_CHANNEL), 0);
  851. if(value!=NULL){
  852. ESP_LOGD(TAG,"Channel: %s", value);
  853. ap_config.ap.channel=atoi(value);
  854. }
  855. FREE_AND_NULL(value);
  856. ap_config.ap.authmode = AP_AUTHMODE;
  857. ap_config.ap.ssid_hidden = DEFAULT_AP_SSID_HIDDEN;
  858. ap_config.ap.max_connection = DEFAULT_AP_MAX_CONNECTIONS;
  859. ap_config.ap.beacon_interval = DEFAULT_AP_BEACON_INTERVAL;
  860. ESP_LOGD(TAG,"Auth Mode: %d", ap_config.ap.authmode);
  861. ESP_LOGD(TAG,"SSID Hidden: %d", ap_config.ap.ssid_hidden);
  862. ESP_LOGD(TAG,"Max Connections: %d", ap_config.ap.max_connection);
  863. ESP_LOGD(TAG,"Beacon interval: %d", ap_config.ap.beacon_interval);
  864. ESP_LOGD(TAG,"Setting tcp_ip info for interface TCPIP_ADAPTER_IF_AP");
  865. ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
  866. ESP_LOGD(TAG,"Starting dhcps on interface TCPIP_ADAPTER_IF_AP");
  867. ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); /* start AP DHCP server */
  868. ESP_LOGD(TAG,"Setting wifi mode as WIFI_MODE_APSTA");
  869. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
  870. ESP_LOGD(TAG,"Setting wifi AP configuration for WIFI_IF_AP");
  871. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
  872. ESP_LOGD(TAG,"Setting wifi bandwidth (%d) for WIFI_IF_AP",DEFAULT_AP_BANDWIDTH);
  873. ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, DEFAULT_AP_BANDWIDTH));
  874. ESP_LOGD(TAG,"Setting wifi power save (%d) for WIFI_IF_AP",DEFAULT_STA_POWER_SAVE);
  875. ESP_ERROR_CHECK(esp_wifi_set_ps(DEFAULT_STA_POWER_SAVE));
  876. ESP_LOGD(TAG,"Done configuring Soft Access Point");
  877. dns_server_start();
  878. }
  879. void wifi_manager( void * pvParameters ){
  880. queue_message msg;
  881. BaseType_t xStatus;
  882. EventBits_t uxBits;
  883. uint8_t retries = 0;
  884. esp_err_t err=ESP_OK;
  885. /* start http server */
  886. http_server_start();
  887. /* enqueue first event: load previous config and start AP or STA mode */
  888. wifi_manager_send_message(ORDER_LOAD_AND_RESTORE_STA, NULL);
  889. /* main processing loop */
  890. for(;;){
  891. xStatus = xQueueReceive( wifi_manager_queue, &msg, portMAX_DELAY );
  892. if( xStatus == pdPASS ){
  893. switch(msg.code){
  894. case EVENT_SCAN_DONE:
  895. /* As input param, it stores max AP number ap_records can hold. As output param, it receives the actual AP number this API returns.
  896. * As a consequence, ap_num MUST be reset to MAX_AP_NUM at every scan */
  897. ESP_LOGD(TAG,"Getting AP list records");
  898. ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_num));
  899. if(ap_num>0){
  900. accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * ap_num);
  901. ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records));
  902. /* make sure the http server isn't trying to access the list while it gets refreshed */
  903. ESP_LOGD(TAG,"Preparing to build ap JSON list");
  904. if(wifi_manager_lock_json_buffer( pdMS_TO_TICKS(1000) )){
  905. /* Will remove the duplicate SSIDs from the list and update ap_num */
  906. wifi_manager_filter_unique(accessp_records, &ap_num);
  907. wifi_manager_generate_access_points_json(&accessp_cjson);
  908. wifi_manager_unlock_json_buffer();
  909. ESP_LOGD(TAG,"Done building ap JSON list");
  910. }
  911. else{
  912. ESP_LOGE(TAG, "could not get access to json mutex in wifi_scan");
  913. }
  914. free(accessp_records);
  915. }
  916. else{
  917. //
  918. ESP_LOGD(TAG,"No AP Found. Emptying the list.");
  919. accessp_cjson = wifi_manager_get_new_array_json(&accessp_cjson);
  920. }
  921. /* callback */
  922. if(cb_ptr_arr[msg.code]) {
  923. ESP_LOGD(TAG,"Invoking SCAN DONE callback");
  924. (*cb_ptr_arr[msg.code])(NULL);
  925. ESP_LOGD(TAG,"Done Invoking SCAN DONE callback");
  926. }
  927. break;
  928. case EVENT_REFRESH_OTA:
  929. if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
  930. wifi_manager_generate_ip_info_json( UPDATE_OTA );
  931. wifi_manager_unlock_json_buffer();
  932. }
  933. break;
  934. case ORDER_START_WIFI_SCAN:
  935. ESP_LOGD(TAG, "MESSAGE: ORDER_START_WIFI_SCAN");
  936. /* if a scan is already in progress this message is simply ignored thanks to the WIFI_MANAGER_SCAN_BIT uxBit */
  937. if(! isGroupBitSet(WIFI_MANAGER_SCAN_BIT) ){
  938. if(esp_wifi_scan_start(&scan_config, false)!=ESP_OK){
  939. ESP_LOGW(TAG,"Unable to start scan; wifi is trying to connect");
  940. // set_status_message(WARNING, "Wifi Connecting. Cannot start scan.");
  941. }
  942. else {
  943. xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_SCAN_BIT);
  944. }
  945. }
  946. else {
  947. ESP_LOGW(TAG,"Scan already in progress!");
  948. }
  949. /* callback */
  950. if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
  951. break;
  952. case ORDER_LOAD_AND_RESTORE_STA:
  953. ESP_LOGI(TAG, "MESSAGE: ORDER_LOAD_AND_RESTORE_STA");
  954. if(wifi_manager_fetch_wifi_sta_config()){
  955. ESP_LOGI(TAG, "Saved wifi found on startup. Will attempt to connect.");
  956. wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_RESTORE_CONNECTION);
  957. }
  958. else{
  959. /* no wifi saved: start soft AP! This is what should happen during a first run */
  960. ESP_LOGI(TAG, "No saved wifi found on startup. Starting access point.");
  961. wifi_manager_send_message(ORDER_START_AP, NULL);
  962. }
  963. /* callback */
  964. if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
  965. break;
  966. case ORDER_CONNECT_STA:
  967. ESP_LOGI(TAG, "MESSAGE: ORDER_CONNECT_STA - Begin");
  968. /* very important: precise that this connection attempt is specifically requested.
  969. * Param in that case is a boolean indicating if the request was made automatically
  970. * by the wifi_manager.
  971. * */
  972. if((BaseType_t)msg.param == CONNECTION_REQUEST_USER) {
  973. ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - Connection request with no nvs connection saved yet");
  974. xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
  975. }
  976. else if((BaseType_t)msg.param == CONNECTION_REQUEST_RESTORE_CONNECTION) {
  977. ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - Connection request after restoring the AP configuration");
  978. xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
  979. /* STA - Wifi Station configuration setup */
  980. if(wifi_settings.sta_static_ip) {
  981. // There's a static ip address configured, so
  982. ESP_LOGI(TAG, "Assigning static ip to STA interface. IP: %s , GW: %s , Mask: %s",
  983. ip4addr_ntoa(&wifi_settings.sta_static_ip_config.ip),
  984. ip4addr_ntoa(&wifi_settings.sta_static_ip_config.gw),
  985. ip4addr_ntoa(&wifi_settings.sta_static_ip_config.netmask));
  986. /* stop DHCP client*/
  987. ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
  988. /* assign a static IP to the STA network interface */
  989. ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &wifi_settings.sta_static_ip_config));
  990. }
  991. else {
  992. /* start DHCP client if not started*/
  993. tcpip_adapter_dhcp_status_t status;
  994. ESP_LOGD(TAG, "wifi_manager: Checking if DHCP client for STA interface is running");
  995. ESP_ERROR_CHECK(tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &status));
  996. if (status!=TCPIP_ADAPTER_DHCP_STARTED) {
  997. ESP_LOGI(TAG, "wifi_manager: Start DHCP client for STA interface");
  998. ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
  999. }
  1000. }
  1001. }
  1002. uxBits = xEventGroupGetBits(wifi_manager_event_group);
  1003. if( uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT ){
  1004. ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - Wifi connected bit set, ordering disconnect (WIFI_MANAGER_WIFI_CONNECTED_BIT)");
  1005. wifi_manager_send_message(ORDER_DISCONNECT_STA, NULL);
  1006. /* todo: reconnect */
  1007. }
  1008. else{
  1009. wifi_mode_t mode;
  1010. /* update config to latest and attempt connection */
  1011. esp_wifi_get_mode(&mode);
  1012. if( WIFI_MODE_APSTA != mode && WIFI_MODE_STA !=mode ){
  1013. // the soft ap is not started, so let's set the WiFi mode to STA
  1014. ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - setting mode WIFI_MODE_STA");
  1015. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  1016. }
  1017. ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - setting config for WIFI_IF_STA");
  1018. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, wifi_manager_get_wifi_sta_config()));
  1019. ESP_LOGD(TAG,"Setting host name to : %s",host_name);
  1020. if((err=tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, host_name)) !=ESP_OK){
  1021. ESP_LOGE(TAG,"Unable to set host name. Error: %s",esp_err_to_name(err));
  1022. }
  1023. ESP_LOGI(TAG,"Wifi Connecting...");
  1024. ESP_ERROR_CHECK(esp_wifi_connect());
  1025. }
  1026. /* callback */
  1027. if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
  1028. break;
  1029. case EVENT_STA_DISCONNECTED:{
  1030. wifi_event_sta_disconnected_t disc_event;
  1031. ESP_LOGI(TAG, "MESSAGE: EVENT_STA_DISCONNECTED");
  1032. if(msg.param == NULL){
  1033. ESP_LOGE(TAG,"MESSAGE: EVENT_STA_DISCONNECTED - expected parameter not found!");
  1034. }
  1035. else{
  1036. memcpy(&disc_event,(wifi_event_sta_disconnected_t*)msg.param,sizeof(disc_event));
  1037. free(msg.param);
  1038. ESP_LOGI(TAG, "MESSAGE: EVENT_STA_DISCONNECTED with Reason code: %d", disc_event.reason);
  1039. }
  1040. /* this even can be posted in numerous different conditions
  1041. *
  1042. * 1. SSID password is wrong
  1043. * 2. Manual disconnection ordered
  1044. * 3. Connection lost
  1045. *
  1046. * Having clear understand as to WHY the event was posted is key to having an efficient wifi manager
  1047. *
  1048. * With wifi_manager, we determine:
  1049. * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT is set, We consider it's a client that requested the connection.
  1050. * When SYSTEM_EVENT_STA_DISCONNECTED is posted, it's probably a password/something went wrong with the handshake.
  1051. *
  1052. * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT is set, it's a disconnection that was ASKED by the client (clicking disconnect in the app)
  1053. * When SYSTEM_EVENT_STA_DISCONNECTED is posted, saved wifi is erased from the NVS memory.
  1054. *
  1055. * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT and WIFI_MANAGER_REQUEST_STA_CONNECT_BIT are NOT set, it's a lost connection
  1056. *
  1057. * In this version of the software, reason codes are not used. They are indicated here for potential future usage.
  1058. *
  1059. * REASON CODE:
  1060. * 1 UNSPECIFIED
  1061. * 2 AUTH_EXPIRE auth no longer valid, this smells like someone changed a password on the AP
  1062. * 3 AUTH_LEAVE
  1063. * 4 ASSOC_EXPIRE
  1064. * 5 ASSOC_TOOMANY too many devices already connected to the AP => AP fails to respond
  1065. * 6 NOT_AUTHED
  1066. * 7 NOT_ASSOCED
  1067. * 8 ASSOC_LEAVE
  1068. * 9 ASSOC_NOT_AUTHED
  1069. * 10 DISASSOC_PWRCAP_BAD
  1070. * 11 DISASSOC_SUPCHAN_BAD
  1071. * 12 <n/a>
  1072. * 13 IE_INVALID
  1073. * 14 MIC_FAILURE
  1074. * 15 4WAY_HANDSHAKE_TIMEOUT wrong password! This was personnaly tested on my home wifi with a wrong password.
  1075. * 16 GROUP_KEY_UPDATE_TIMEOUT
  1076. * 17 IE_IN_4WAY_DIFFERS
  1077. * 18 GROUP_CIPHER_INVALID
  1078. * 19 PAIRWISE_CIPHER_INVALID
  1079. * 20 AKMP_INVALID
  1080. * 21 UNSUPP_RSN_IE_VERSION
  1081. * 22 INVALID_RSN_IE_CAP
  1082. * 23 802_1X_AUTH_FAILED wrong password?
  1083. * 24 CIPHER_SUITE_REJECTED
  1084. * 200 BEACON_TIMEOUT
  1085. * 201 NO_AP_FOUND
  1086. * 202 AUTH_FAIL
  1087. * 203 ASSOC_FAIL
  1088. * 204 HANDSHAKE_TIMEOUT
  1089. *
  1090. * */
  1091. /* reset saved sta IP */
  1092. wifi_manager_safe_update_sta_ip_string((struct ip4_addr * )0);
  1093. uxBits = xEventGroupGetBits(wifi_manager_event_group);
  1094. if( uxBits & WIFI_MANAGER_REQUEST_STA_CONNECT_BIT ){
  1095. ESP_LOGW(TAG, "WiFi Disconnected while processing user connect request. Wrong password?");
  1096. /* there are no retries when it's a user requested connection by design. This avoids a user hanging too much
  1097. * in case they typed a wrong password for instance. Here we simply clear the request bit and move on */
  1098. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
  1099. if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
  1100. wifi_manager_generate_ip_info_json( UPDATE_FAILED_ATTEMPT );
  1101. wifi_manager_unlock_json_buffer();
  1102. }
  1103. }
  1104. else if (uxBits & WIFI_MANAGER_REQUEST_DISCONNECT_BIT){
  1105. ESP_LOGI(TAG, "WiFi disconnected by user");
  1106. /* user manually requested a disconnect so the lost connection is a normal event. Clear the flag and restart the AP */
  1107. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
  1108. if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
  1109. wifi_manager_generate_ip_info_json( UPDATE_USER_DISCONNECT );
  1110. wifi_manager_unlock_json_buffer();
  1111. }
  1112. /* erase configuration */
  1113. if(wifi_manager_config_sta){
  1114. ESP_LOGI(TAG, "Erasing WiFi Configuration.");
  1115. memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  1116. /* save NVS memory */
  1117. wifi_manager_save_sta_config();
  1118. }
  1119. /* start SoftAP */
  1120. ESP_LOGD(TAG, "Disconnect processing complete. Ordering an AP start.");
  1121. wifi_manager_send_message(ORDER_START_AP, NULL);
  1122. }
  1123. else{
  1124. /* lost connection ? */
  1125. ESP_LOGE(TAG, "WiFi Connection lost.");
  1126. if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
  1127. wifi_manager_generate_ip_info_json( UPDATE_LOST_CONNECTION );
  1128. wifi_manager_unlock_json_buffer();
  1129. }
  1130. if(retries < WIFI_MANAGER_MAX_RETRY){
  1131. ESP_LOGD(TAG, "Issuing ORDER_CONNECT_STA to retry connection.");
  1132. retries++;
  1133. wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_AUTO_RECONNECT);
  1134. }
  1135. else{
  1136. /* In this scenario the connection was lost beyond repair: kick start the AP! */
  1137. retries = 0;
  1138. ESP_LOGW(TAG, "All connect retry attempts failed.");
  1139. /* if it was a restore attempt connection, we clear the bit */
  1140. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
  1141. /* erase configuration that could not be used to connect */
  1142. // if(wifi_manager_config_sta){
  1143. // ESP_LOGW(TAG, "Erasing wifi manager config.");
  1144. // memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
  1145. // /* save empty connection info in NVS memory */
  1146. // wifi_manager_save_sta_config();
  1147. // }
  1148. ESP_LOGD(TAG, "Issuing ORDER_START_AP to trigger AP start.");
  1149. /* start SoftAP */
  1150. wifi_manager_send_message(ORDER_START_AP, NULL);
  1151. }
  1152. }
  1153. /* callback */
  1154. if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
  1155. }
  1156. break;
  1157. case ORDER_START_AP:
  1158. ESP_LOGI(TAG, "MESSAGE: ORDER_START_AP");
  1159. wifi_manager_config_ap();
  1160. ESP_LOGD(TAG,"AP Starting, requesting wifi scan.");
  1161. wifi_manager_scan_async();
  1162. /* callback */
  1163. if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
  1164. break;
  1165. case EVENT_STA_GOT_IP:
  1166. ESP_LOGI(TAG, "MESSAGE: EVENT_STA_GOT_IP");
  1167. uxBits = xEventGroupGetBits(wifi_manager_event_group);
  1168. /* reset connection requests bits -- doesn't matter if it was set or not */
  1169. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
  1170. /* save IP as a string for the HTTP server host */
  1171. //s->ip_info.ip.addr
  1172. ip_event_got_ip_t * event =(ip_event_got_ip_t*)msg.param;
  1173. wifi_manager_safe_update_sta_ip_string(&(event->ip_info.ip));
  1174. free(msg.param);
  1175. /* save wifi config in NVS if it wasn't a restored of a connection */
  1176. if(uxBits & WIFI_MANAGER_REQUEST_RESTORE_STA_BIT){
  1177. ESP_LOGD(TAG,"Configuration came from nvs, no need to save.");
  1178. xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
  1179. }
  1180. else{
  1181. ESP_LOGD(TAG,"Connection was initiated by user, storing config to nvs.");
  1182. wifi_manager_save_sta_config();
  1183. }
  1184. /* refresh JSON with the new IP */
  1185. if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
  1186. /* generate the connection info with success */
  1187. wifi_manager_generate_ip_info_json( UPDATE_CONNECTION_OK );
  1188. wifi_manager_unlock_json_buffer();
  1189. }
  1190. else {
  1191. ESP_LOGW(TAG,"Unable to lock status json buffer. ");
  1192. }
  1193. /* bring down DNS hijack */
  1194. ESP_LOGD(TAG,"Stopping dns server.");
  1195. dns_server_stop();
  1196. /* callback */
  1197. if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
  1198. break;
  1199. case UPDATE_CONNECTION_OK:
  1200. /* refresh JSON with the new ota data */
  1201. if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
  1202. /* generate the connection info with success */
  1203. wifi_manager_generate_ip_info_json( UPDATE_CONNECTION_OK );
  1204. wifi_manager_unlock_json_buffer();
  1205. }
  1206. break;
  1207. case ORDER_DISCONNECT_STA:
  1208. ESP_LOGI(TAG, "MESSAGE: ORDER_DISCONNECT_STA. Calling esp_wifi_disconnect()");
  1209. /* precise this is coming from a user request */
  1210. xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
  1211. /* order wifi discconect */
  1212. ESP_ERROR_CHECK(esp_wifi_disconnect());
  1213. /* callback */
  1214. if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
  1215. break;
  1216. default:
  1217. break;
  1218. } /* end of switch/case */
  1219. } /* end of if status=pdPASS */
  1220. } /* end of for loop */
  1221. vTaskDelete( NULL );
  1222. }