1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492 |
- /*
- Copyright (c) 2017-2019 Tony Pottier
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- @file wifi_manager.c
- @author Tony Pottier
- @brief Defines all functions necessary for esp32 to connect to a wifi/scan wifis
- Contains the freeRTOS task and all necessary support
- @see https://idyl.io
- @see https://github.com/tonyp7/esp32-wifi-manager
- */
- #include "wifi_manager.h"
- #include "platform_esp32.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include "dns_server.h"
- #include "http_server.h"
- #include "esp_system.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/event_groups.h"
- #include "esp_event_loop.h"
- #include "tcpip_adapter.h"
- #include "esp_event.h"
- #include "esp_wifi.h"
- #include "esp_wifi_types.h"
- #include "esp_log.h"
- #include "nvs.h"
- #include "nvs_flash.h"
- #include "mdns.h"
- #include "lwip/api.h"
- #include "lwip/err.h"
- #include "lwip/netdb.h"
- #include "lwip/ip4_addr.h"
- #include "esp_ota_ops.h"
- #include "esp_app_format.h"
- #include "cJSON.h"
- #include "config.h"
- #include "trace.h"
- #include "cmd_system.h"
- #include "monitor.h"
- #include "globdefs.h"
- #ifndef RECOVERY_APPLICATION
- #define RECOVERY_APPLICATION 0
- #endif
- #ifndef SQUEEZELITE_ESP32_RELEASE_URL
- #define SQUEEZELITE_ESP32_RELEASE_URL "https://github.com/sle118/squeezelite-esp32/releases"
- #endif
- #define STR_OR_BLANK(p) p==NULL?"":p
- #define FREE_AND_NULL(p) if(p!=NULL){ free(p); p=NULL;}
- /* objects used to manipulate the main queue of events */
- QueueHandle_t wifi_manager_queue;
- SemaphoreHandle_t wifi_manager_json_mutex = NULL;
- SemaphoreHandle_t wifi_manager_sta_ip_mutex = NULL;
- char *wifi_manager_sta_ip = NULL;
- bool bHasConnected=false;
- uint16_t ap_num = MAX_AP_NUM;
- wifi_ap_record_t *accessp_records=NULL;
- cJSON * accessp_cjson=NULL;
- char *ip_info_json = NULL;
- char * release_url=NULL;
- cJSON * ip_info_cjson=NULL;
- wifi_config_t* wifi_manager_config_sta = NULL;
- static update_reason_code_t last_update_reason_code=0;
- static int32_t total_connected_time=0;
- static int64_t last_connected=0;
- static uint16_t num_disconnect=0;
- void (**cb_ptr_arr)(void*) = NULL;
- /* @brief tag used for ESP serial console messages */
- static const char TAG[] = "wifi_manager";
- /* @brief task handle for the main wifi_manager task */
- static TaskHandle_t task_wifi_manager = NULL;
- /**
- * The actual WiFi settings in use
- */
- //struct wifi_settings_t wifi_settings = {
- // .sta_only = DEFAULT_STA_ONLY,
- // .sta_power_save = DEFAULT_STA_POWER_SAVE,
- // .sta_static_ip = 0
- //};
- /* wifi scanner config */
- wifi_scan_config_t scan_config = {
- .ssid = 0,
- .bssid = 0,
- .channel = 0,
- .show_hidden = true
- };
- const char wifi_manager_nvs_namespace[] = "config";
- EventGroupHandle_t wifi_manager_event_group;
- /* @brief indicate that the ESP32 is currently connected. */
- const int WIFI_MANAGER_WIFI_CONNECTED_BIT = BIT0;
- const int WIFI_MANAGER_AP_STA_CONNECTED_BIT = BIT1;
- /* @brief Set automatically once the SoftAP is started */
- const int WIFI_MANAGER_AP_STARTED_BIT = BIT2;
- /* @brief When set, means a client requested to connect to an access point.*/
- const int WIFI_MANAGER_REQUEST_STA_CONNECT_BIT = BIT3;
- /* @brief This bit is set automatically as soon as a connection was lost */
- const int WIFI_MANAGER_STA_DISCONNECT_BIT = BIT4;
- /* @brief When set, means the wifi manager attempts to restore a previously saved connection at startup. */
- const int WIFI_MANAGER_REQUEST_RESTORE_STA_BIT = BIT5;
- /* @brief When set, means a client requested to disconnect from currently connected AP. */
- const int WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT = BIT6;
- /* @brief When set, means a scan is in progress */
- const int WIFI_MANAGER_SCAN_BIT = BIT7;
- /* @brief When set, means user requested for a disconnect */
- const int WIFI_MANAGER_REQUEST_DISCONNECT_BIT = BIT8;
- char * get_disconnect_code_desc(uint8_t reason){
- switch (reason) {
- case 1 : return "UNSPECIFIED"; break;
- case 2 : return "AUTH_EXPIRE"; break;
- case 3 : return "AUTH_LEAVE"; break;
- case 4 : return "ASSOC_EXPIRE"; break;
- case 5 : return "ASSOC_TOOMANY"; break;
- case 6 : return "NOT_AUTHED"; break;
- case 7 : return "NOT_ASSOCED"; break;
- case 8 : return "ASSOC_LEAVE"; break;
- case 9 : return "ASSOC_NOT_AUTHED"; break;
- case 10 : return "DISASSOC_PWRCAP_BAD"; break;
- case 11 : return "DISASSOC_SUPCHAN_BAD"; break;
- case 12 : return "<n/a>"; break;
- case 13 : return "IE_INVALID"; break;
- case 14 : return "MIC_FAILURE"; break;
- case 15 : return "4WAY_HANDSHAKE_TIMEOUT"; break;
- case 16 : return "GROUP_KEY_UPDATE_TIMEOUT"; break;
- case 17 : return "IE_IN_4WAY_DIFFERS"; break;
- case 18 : return "GROUP_CIPHER_INVALID"; break;
- case 19 : return "PAIRWISE_CIPHER_INVALID"; break;
- case 20 : return "AKMP_INVALID"; break;
- case 21 : return "UNSUPP_RSN_IE_VERSION"; break;
- case 22 : return "INVALID_RSN_IE_CAP"; break;
- case 23 : return "802_1X_AUTH_FAILED"; break;
- case 24 : return "CIPHER_SUITE_REJECTED"; break;
- case 200 : return "BEACON_TIMEOUT"; break;
- case 201 : return "NO_AP_FOUND"; break;
- case 202 : return "AUTH_FAIL"; break;
- case 203 : return "ASSOC_FAIL"; break;
- case 204 : return "HANDSHAKE_TIMEOUT"; break;
- default: return "UNKNOWN"; break;
- }
- return "";
- }
- void set_host_name(){
- esp_err_t err;
- ESP_LOGD(TAG, "Retrieving host name from nvs");
- char * host_name = (char * )config_alloc_get(NVS_TYPE_STR, "host_name");
- if(host_name ==NULL){
- ESP_LOGE(TAG, "Could not retrieve host name from nvs");
- }
- else {
- ESP_LOGD(TAG, "Setting host name to : %s",host_name);
- if((err=tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, host_name)) !=ESP_OK){
- ESP_LOGE(TAG, "Unable to set host name. Error: %s",esp_err_to_name(err));
- }
- free(host_name);
- }
- }
- bool isGroupBitSet(uint8_t bit){
- EventBits_t uxBits= xEventGroupGetBits(wifi_manager_event_group);
- return (uxBits & bit);
- }
- void wifi_manager_refresh_ota_json(){
- wifi_manager_send_message(EVENT_REFRESH_OTA, NULL);
- }
- void wifi_manager_scan_async(){
- wifi_manager_send_message(ORDER_START_WIFI_SCAN, NULL);
- }
- void wifi_manager_disconnect_async(){
- wifi_manager_send_message(ORDER_DISCONNECT_STA, NULL);
- //xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT); TODO: delete
- }
- void wifi_manager_reboot_ota(char * url){
- if(url == NULL){
- wifi_manager_send_message(ORDER_RESTART_OTA, NULL);
- }
- else {
- wifi_manager_send_message(ORDER_RESTART_OTA_URL,strdup(url) );
- }
- }
- void wifi_manager_reboot(reboot_type_t rtype){
- switch (rtype) {
- case OTA:
- wifi_manager_send_message(ORDER_RESTART_OTA, NULL);
- break;
- case RECOVERY:
- wifi_manager_send_message(ORDER_RESTART_RECOVERY, NULL);
- break;
- case RESTART:
- wifi_manager_send_message(ORDER_RESTART, NULL);
- break;
- default:
- ESP_LOGE(TAG,"Unknown reboot type %d", rtype);
- break;
- }
- wifi_manager_send_message(ORDER_DISCONNECT_STA, NULL);
- //xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT); TODO: delete
- }
- void wifi_manager_init_wifi(){
- /* event handler and event group for the wifi driver */
- ESP_LOGD(TAG, "Initializing wifi. Creating event group");
- wifi_manager_event_group = xEventGroupCreate();
- bHasConnected=false;
- // Now Initialize the Wifi Stack
- ESP_LOGD(TAG, "Initializing wifi. Initializing tcp_ip adapter");
- tcpip_adapter_init();
- ESP_LOGD(TAG, "Initializing wifi. Creating the default event loop");
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- ESP_LOGD(TAG, "Initializing wifi. Getting default wifi configuration");
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_LOGD(TAG, "Initializing wifi. Initializing wifi. ");
- ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
- ESP_LOGD(TAG, "Initializing wifi. Calling register handlers");
- wifi_manager_register_handlers();
- ESP_LOGD(TAG, "Initializing wifi. Setting WiFi storage as RAM");
- ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
- ESP_LOGD(TAG, "Initializing wifi. Setting WiFi mode to WIFI_MODE_NULL");
- ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
- ESP_LOGD(TAG, "Initializing wifi. Starting wifi");
- if (gpio36_39_used) {
- ESP_LOGW(TAG, "GPIO 36 or 39 are in use, need to disable WiFi PowerSave!");
- esp_wifi_set_ps(WIFI_PS_NONE);
- }
- ESP_ERROR_CHECK( esp_wifi_start() );
- taskYIELD();
- ESP_LOGD(TAG, "Initializing wifi. done");
- }
- void wifi_manager_start(){
- /* memory allocation */
- ESP_LOGD(TAG, "wifi_manager_start. Creating message queue");
- wifi_manager_queue = xQueueCreate( 3, sizeof( queue_message) );
- ESP_LOGD(TAG, "wifi_manager_start. Creating mutexes");
- wifi_manager_json_mutex = xSemaphoreCreateMutex();
- wifi_manager_sta_ip_mutex = xSemaphoreCreateMutex();
- ESP_LOGD(TAG, "wifi_manager_start. Creating access point json structure");
- accessp_cjson = NULL;
- accessp_cjson = wifi_manager_clear_ap_list_json(&accessp_cjson);
- ip_info_json = NULL;
- ESP_LOGD(TAG, "wifi_manager_start. Creating status jcon structure");
- ip_info_cjson = wifi_manager_clear_ip_info_json(&ip_info_cjson);
- ESP_LOGD(TAG, "wifi_manager_start. Allocating memory for wifi configuration structure");
- wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));
- memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
- // memset(&wifi_settings, 0x00, sizeof(wifi_settings));
- ESP_LOGD(TAG, "wifi_manager_start. Allocating memory for callback functions registration");
- cb_ptr_arr = malloc( sizeof( sizeof( void (*)( void* ) )) * MESSAGE_CODE_COUNT);
- for(int i=0; i<MESSAGE_CODE_COUNT; i++){
- cb_ptr_arr[i] = NULL;
- }
- ESP_LOGD(TAG, "About to set the STA IP String to 0.0.0.0");
- wifi_manager_sta_ip = (char*)malloc(sizeof(char) * IP4ADDR_STRLEN_MAX);
- wifi_manager_safe_update_sta_ip_string(NULL);
- ESP_LOGD(TAG, "Getting release url ");
- char * release_url = (char * )config_alloc_get_default(NVS_TYPE_STR, "release_url", QUOTE(SQUEEZELITE_ESP32_RELEASE_URL), 0);
- if(release_url == NULL){
- ESP_LOGE(TAG, "Unable to retrieve the release url from nvs");
- }
- else {
- ESP_LOGD(TAG, "Found release url %s", release_url);
- }
- ESP_LOGD(TAG, "About to call init wifi");
- wifi_manager_init_wifi();
- /* start wifi manager task */
- ESP_LOGD(TAG, "Creating wifi manager task");
- xTaskCreate(&wifi_manager, "wifi_manager", 4096, NULL, WIFI_MANAGER_TASK_PRIORITY, &task_wifi_manager);
- }
- esp_err_t wifi_manager_save_sta_config(){
- nvs_handle handle;
- esp_err_t esp_err;
- ESP_LOGI(TAG, "About to save config to flash");
- if(wifi_manager_config_sta){
- esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
- if (esp_err != ESP_OK) {
- ESP_LOGE(TAG, "Unable to open name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
- return esp_err;
- }
- esp_err = nvs_set_blob(handle, "ssid", wifi_manager_config_sta->sta.ssid, sizeof(wifi_manager_config_sta->sta.ssid));
- if (esp_err != ESP_OK) {
- ESP_LOGE(TAG, "Unable to save ssid in name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
- return esp_err;
- }
- esp_err = nvs_set_blob(handle, "password", wifi_manager_config_sta->sta.password, sizeof(wifi_manager_config_sta->sta.password));
- if (esp_err != ESP_OK) {
- ESP_LOGE(TAG, "Unable to save password in name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
- return esp_err;
- }
- // esp_err = nvs_set_blob(handle, "settings", &wifi_settings, sizeof(wifi_settings));
- // if (esp_err != ESP_OK) {
- // ESP_LOGE(TAG, "Unable to save wifi_settings in name namespace %s. Error %s", wifi_manager_nvs_namespace, esp_err_to_name(esp_err));
- // return esp_err;
- // }
- esp_err = nvs_commit(handle);
- if (esp_err != ESP_OK) {
- ESP_LOGE(TAG, "Unable to commit changes. Error %s", esp_err_to_name(esp_err));
- return esp_err;
- }
- nvs_close(handle);
- 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);
- }
- return ESP_OK;
- }
- bool wifi_manager_fetch_wifi_sta_config(){
- nvs_handle handle;
- esp_err_t esp_err;
- ESP_LOGD(TAG, "Fetching wifi sta config.");
- esp_err=nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle);
- if(esp_err == ESP_OK){
- if(wifi_manager_config_sta == NULL){
- ESP_LOGD(TAG, "Allocating memory for structure.");
- wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));
- }
- memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
- /* ssid */
- ESP_LOGD(TAG, "Fetching value for ssid.");
- size_t sz = sizeof(wifi_manager_config_sta->sta.ssid);
- uint8_t *buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
- memset(buff,0x00,sizeof(uint8_t) * sz);
- esp_err = nvs_get_blob(handle, "ssid", buff, &sz);
- if(esp_err != ESP_OK){
- ESP_LOGI(TAG, "No ssid found in nvs.");
- FREE_AND_NULL(buff);
- nvs_close(handle);
- return false;
- }
- memcpy(wifi_manager_config_sta->sta.ssid, buff, sizeof(wifi_manager_config_sta->sta.ssid));
- FREE_AND_NULL(buff);
- ESP_LOGI(TAG, "wifi_manager_fetch_wifi_sta_config: ssid:%s ",wifi_manager_config_sta->sta.ssid);
- /* password */
- sz = sizeof(wifi_manager_config_sta->sta.password);
- buff = (uint8_t*)malloc(sizeof(uint8_t) * sz);
- memset(buff,0x00,sizeof(uint8_t) * sz);
- esp_err = nvs_get_blob(handle, "password", buff, &sz);
- if(esp_err != ESP_OK){
- // Don't take this as an error. This could be an opened access point?
- ESP_LOGW(TAG, "No wifi password found in nvs");
- }
- else {
- memcpy(wifi_manager_config_sta->sta.password, buff, sizeof(wifi_manager_config_sta->sta.password));
- ESP_LOGI(TAG, "wifi_manager_fetch_wifi_sta_config: password:%s",wifi_manager_config_sta->sta.password);
- }
- FREE_AND_NULL(buff);
- nvs_close(handle);
- return wifi_manager_config_sta->sta.ssid[0] != '\0';
- }
- else{
- ESP_LOGW(TAG, "wifi manager has no previous configuration. %s",esp_err_to_name(esp_err));
- return false;
- }
- }
- cJSON * wifi_manager_get_new_json(cJSON **old){
- ESP_LOGV(TAG, "wifi_manager_get_new_json called");
- cJSON * root=*old;
- if(root!=NULL){
- cJSON_Delete(root);
- *old=NULL;
- }
- ESP_LOGV(TAG, "wifi_manager_get_new_json done");
- return cJSON_CreateObject();
- }
- cJSON * wifi_manager_get_new_array_json(cJSON **old){
- ESP_LOGV(TAG, "wifi_manager_get_new_array_json called");
- cJSON * root=*old;
- if(root!=NULL){
- cJSON_Delete(root);
- *old=NULL;
- }
- ESP_LOGV(TAG, "wifi_manager_get_new_array_json done");
- return cJSON_CreateArray();
- }
- cJSON * wifi_manager_get_basic_info(cJSON **old){
- const esp_app_desc_t* desc = esp_ota_get_app_description();
- ESP_LOGV(TAG, "wifi_manager_get_basic_info called");
- cJSON *root = wifi_manager_get_new_json(old);
- cJSON_AddItemToObject(root, "project_name", cJSON_CreateString(desc->project_name));
- cJSON_AddItemToObject(root, "version", cJSON_CreateString(desc->version));
- if(release_url !=NULL) cJSON_AddItemToObject(root, "release_url", cJSON_CreateString(release_url));
- cJSON_AddNumberToObject(root,"recovery", RECOVERY_APPLICATION );
- cJSON_AddItemToObject(root, "ota_dsc", cJSON_CreateString(ota_get_status()));
- cJSON_AddNumberToObject(root,"ota_pct", ota_get_pct_complete() );
- cJSON_AddItemToObject(root, "Jack", cJSON_CreateString(jack_inserted_svc() ? "1" : "0"));
- cJSON_AddNumberToObject(root,"Voltage", battery_value_svc());
- cJSON_AddNumberToObject(root,"disconnect_count", num_disconnect );
- cJSON_AddNumberToObject(root,"avg_conn_time", num_disconnect>0?(total_connected_time/num_disconnect):0 );
- ESP_LOGV(TAG, "wifi_manager_get_basic_info done");
- return root;
- }
- cJSON * wifi_manager_clear_ip_info_json(cJSON **old){
- ESP_LOGV(TAG, "wifi_manager_clear_ip_info_json called");
- cJSON *root = wifi_manager_get_basic_info(old);
- ESP_LOGV(TAG, "wifi_manager_clear_ip_info_json done");
- return root;
- }
- cJSON * wifi_manager_clear_ap_list_json(cJSON **old){
- ESP_LOGV(TAG, "wifi_manager_clear_ap_list_json called");
- cJSON *root = wifi_manager_get_new_array_json(old);
- ESP_LOGV(TAG, "wifi_manager_clear_ap_list_json done");
- return root;
- }
- void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code){
- ESP_LOGD(TAG, "wifi_manager_generate_ip_info_json called");
- wifi_config_t *config = wifi_manager_get_wifi_sta_config();
- ip_info_cjson = wifi_manager_get_basic_info(&ip_info_cjson);
- if(update_reason_code == UPDATE_OTA) {
- update_reason_code = last_update_reason_code;
- }
- else {
- last_update_reason_code = update_reason_code;
- }
- cJSON_AddNumberToObject(ip_info_cjson, "urc", update_reason_code);
- if(config){
- cJSON_AddItemToObject(ip_info_cjson, "ssid", cJSON_CreateString((char *)config->sta.ssid));
- if(update_reason_code == UPDATE_CONNECTION_OK){
- /* rest of the information is copied after the ssid */
- tcpip_adapter_ip_info_t ip_info;
- ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
- cJSON_AddItemToObject(ip_info_cjson, "ip", cJSON_CreateString(ip4addr_ntoa(&ip_info.ip)));
- cJSON_AddItemToObject(ip_info_cjson, "netmask", cJSON_CreateString(ip4addr_ntoa(&ip_info.netmask)));
- cJSON_AddItemToObject(ip_info_cjson, "gw", cJSON_CreateString(ip4addr_ntoa(&ip_info.gw)));
- }
- }
- ESP_LOGV(TAG, "wifi_manager_generate_ip_info_json done");
- }
- #define LOCAL_MAC_SIZE 20
- char * get_mac_string(uint8_t mac[6]){
- char * macStr=malloc(LOCAL_MAC_SIZE);
- memset(macStr, 0x00, LOCAL_MAC_SIZE);
- snprintf(macStr, LOCAL_MAC_SIZE-1,MACSTR, MAC2STR(mac));
- return macStr;
- }
- void wifi_manager_generate_access_points_json(cJSON ** ap_list){
- *ap_list = wifi_manager_get_new_array_json(ap_list);
- if(*ap_list==NULL) return;
- for(int i=0; i<ap_num;i++){
- cJSON * ap = cJSON_CreateObject();
- if(ap == NULL) {
- ESP_LOGE(TAG, "Unable to allocate memory for access point entry #%d",i);
- return;
- }
- cJSON * radio = cJSON_CreateObject();
- if(radio == NULL) {
- ESP_LOGE(TAG, "Unable to allocate memory for access point entry #%d",i);
- cJSON_Delete(ap);
- return;
- }
- wifi_ap_record_t ap_rec = accessp_records[i];
- cJSON_AddNumberToObject(ap, "chan", ap_rec.primary);
- cJSON_AddNumberToObject(ap, "rssi", ap_rec.rssi);
- cJSON_AddNumberToObject(ap, "auth", ap_rec.authmode);
- cJSON_AddItemToObject(ap, "ssid", cJSON_CreateString((char *)ap_rec.ssid));
- char * bssid = get_mac_string(ap_rec.bssid);
- cJSON_AddItemToObject(ap, "bssid", cJSON_CreateString(STR_OR_BLANK(bssid)));
- FREE_AND_NULL(bssid);
- cJSON_AddNumberToObject(radio, "b", ap_rec.phy_11b?1:0);
- cJSON_AddNumberToObject(radio, "g", ap_rec.phy_11g?1:0);
- cJSON_AddNumberToObject(radio, "n", ap_rec.phy_11n?1:0);
- cJSON_AddNumberToObject(radio, "low_rate", ap_rec.phy_lr?1:0);
- cJSON_AddItemToObject(ap,"radio", radio);
- cJSON_AddItemToArray(*ap_list, ap);
- char * ap_json = cJSON_PrintUnformatted(ap);
- if(ap_json!=NULL){
- ESP_LOGD(TAG, "New access point found: %s", ap_json);
- free(ap_json);
- }
- }
- char * ap_list_json = cJSON_PrintUnformatted(*ap_list);
- if(ap_list_json!=NULL){
- ESP_LOGV(TAG, "Full access point list: %s", ap_list_json);
- free(ap_list_json);
- }
- }
- bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait){
- if(wifi_manager_sta_ip_mutex){
- if( xSemaphoreTake( wifi_manager_sta_ip_mutex, xTicksToWait ) == pdTRUE ) {
- return true;
- }
- else{
- return false;
- }
- }
- else{
- return false;
- }
- }
- void wifi_manager_unlock_sta_ip_string(){
- xSemaphoreGive( wifi_manager_sta_ip_mutex );
- }
- void wifi_manager_safe_update_sta_ip_string(struct ip4_addr * ip4){
- if(wifi_manager_lock_sta_ip_string(portMAX_DELAY)){
- strcpy(wifi_manager_sta_ip, ip4!=NULL?ip4addr_ntoa(ip4):"0.0.0.0");
- ESP_LOGI(TAG, "Set STA IP String to: %s", wifi_manager_sta_ip);
- wifi_manager_unlock_sta_ip_string();
- }
- }
- char* wifi_manager_get_sta_ip_string(){
- return wifi_manager_sta_ip;
- }
- bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait){
- ESP_LOGV(TAG, "Locking json buffer");
- if(wifi_manager_json_mutex){
- if( xSemaphoreTake( wifi_manager_json_mutex, xTicksToWait ) == pdTRUE ) {
- ESP_LOGV(TAG, "Json buffer locked!");
- return true;
- }
- else{
- ESP_LOGE(TAG, "Semaphore take failed. Unable to lock json buffer mutex");
- return false;
- }
- }
- else{
- ESP_LOGV(TAG, "Unable to lock json buffer mutex");
- return false;
- }
- }
- void wifi_manager_unlock_json_buffer(){
- ESP_LOGV(TAG, "Unlocking json buffer!");
- xSemaphoreGive( wifi_manager_json_mutex );
- }
- char* wifi_manager_alloc_get_ap_list_json(){
- return cJSON_PrintUnformatted(accessp_cjson);
- }
- static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
- if(event_base== WIFI_EVENT){
- switch(event_id) {
- case WIFI_EVENT_WIFI_READY:
- ESP_LOGI(TAG, "WIFI_EVENT_WIFI_READY");
- break;
- case WIFI_EVENT_SCAN_DONE:
- ESP_LOGD(TAG, "WIFI_EVENT_SCAN_DONE");
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_SCAN_BIT);
- wifi_manager_send_message(EVENT_SCAN_DONE, NULL);
- break;
- case WIFI_EVENT_STA_AUTHMODE_CHANGE:
- ESP_LOGI(TAG, "WIFI_EVENT_STA_AUTHMODE_CHANGE");
- // structwifi_event_sta_authmode_change_t
- // Argument structure for WIFI_EVENT_STA_AUTHMODE_CHANGE event
- //
- // Public Members
- //
- // wifi_auth_mode_told_mode
- // the old auth mode of AP
- //
- // wifi_auth_mode_tnew_mode
- // the new auth mode of AP
- break;
- case WIFI_EVENT_AP_START:
- ESP_LOGI(TAG, "WIFI_EVENT_AP_START");
- xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_AP_STARTED_BIT);
- break;
- case WIFI_EVENT_AP_STOP:
- ESP_LOGD(TAG, "WIFI_EVENT_AP_STOP");
- break;
- case WIFI_EVENT_AP_PROBEREQRECVED:{
- // wifi_event_ap_probe_req_rx_t
- // Argument structure for WIFI_EVENT_AP_PROBEREQRECVED event
- //
- // Public Members
- //
- // int rssi
- // Received probe request signal strength
- //
- // uint8_t mac[6]
- // MAC address of the station which send probe request
- wifi_event_ap_probe_req_rx_t * s =(wifi_event_ap_probe_req_rx_t*)event_data;
- char * mac = get_mac_string(s->mac);
- ESP_LOGD(TAG, "WIFI_EVENT_AP_PROBEREQRECVED. RSSI: %d, MAC: %s",s->rssi, STR_OR_BLANK(mac));
- FREE_AND_NULL(mac);
- }
- break;
- case WIFI_EVENT_STA_WPS_ER_SUCCESS:
- ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS");
- break;
- case WIFI_EVENT_STA_WPS_ER_FAILED:
- ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_FAILED");
- break;
- case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
- ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_TIMEOUT");
- break;
- case WIFI_EVENT_STA_WPS_ER_PIN:
- ESP_LOGD(TAG, "WIFI_EVENT_STA_WPS_ER_PIN");
- break;
- case WIFI_EVENT_AP_STACONNECTED: /* a user disconnected from the SoftAP */
- ESP_LOGI(TAG, "WIFI_EVENT_AP_STACONNECTED");
- xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT);
- break;
- case WIFI_EVENT_AP_STADISCONNECTED:
- ESP_LOGI(TAG, "WIFI_EVENT_AP_STADISCONNECTED");
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT);
- break;
- case WIFI_EVENT_STA_START:
- ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
- break;
- case WIFI_EVENT_STA_STOP:
- ESP_LOGI(TAG, "WIFI_EVENT_STA_STOP");
- break;
- case WIFI_EVENT_STA_CONNECTED:{
- // structwifi_event_sta_connected_t
- // Argument structure for WIFI_EVENT_STA_CONNECTED event
- //
- // Public Members
- //
- // uint8_t ssid[32]
- // SSID of connected AP
- //
- // uint8_t ssid_len
- // SSID length of connected AP
- //
- // uint8_t bssid[6]
- // BSSID of connected AP
- //
- // uint8_t channel
- // channel of connected AP
- //
- // wifi_auth_mode_tauthmode
- // authentication mode used by AP
- //, get_mac_string(EVENT_HANDLER_ARG_FIELD(wifi_event_ap_probe_req_rx_t, mac)));
- ESP_LOGD(TAG, "WIFI_EVENT_STA_CONNECTED. ");
- wifi_event_sta_connected_t * s =(wifi_event_sta_connected_t*)event_data;
- char * bssid = get_mac_string(s->bssid);
- char * ssid = strdup((char*)s->ssid);
- ESP_LOGI(TAG, "WIFI_EVENT_STA_CONNECTED. Channel: %d, Access point: %s, BSSID: %s ", s->channel, STR_OR_BLANK(ssid), (bssid));
- FREE_AND_NULL(bssid);
- FREE_AND_NULL(ssid);
- }
- break;
- case WIFI_EVENT_STA_DISCONNECTED:{
- // structwifi_event_sta_disconnected_t
- // Argument structure for WIFI_EVENT_STA_DISCONNECTED event
- //
- // Public Members
- //
- // uint8_t ssid[32]
- // SSID of disconnected AP
- //
- // uint8_t ssid_len
- // SSID length of disconnected AP
- //
- // uint8_t bssid[6]
- // BSSID of disconnected AP
- //
- // uint8_t reason
- // reason of disconnection
- wifi_event_sta_disconnected_t * s =(wifi_event_sta_disconnected_t*)event_data;
- char * bssid = get_mac_string(s->bssid);
- ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED. From BSSID: %s, reason code: %d (%s)", STR_OR_BLANK(bssid),s->reason, get_disconnect_code_desc(s->reason));
- FREE_AND_NULL(bssid);
- if(last_connected>0) total_connected_time+=((esp_timer_get_time()-last_connected)/(1000*1000));
- last_connected = 0;
- num_disconnect++;
- ESP_LOGW(TAG, "Wifi disconnected. Number of disconnects: %d, Average time connected: %d", num_disconnect, num_disconnect>0?(total_connected_time/num_disconnect):0);
- /* 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 */
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_WIFI_CONNECTED_BIT | WIFI_MANAGER_SCAN_BIT);
- // We want to process this message asynchronously, so make sure we copy the event buffer
- ESP_LOGD(TAG, "Preparing to trigger event EVENT_STA_DISCONNECTED ");
- void * parm=malloc(sizeof(wifi_event_sta_disconnected_t));
- memcpy(parm,event_data,sizeof(wifi_event_sta_disconnected_t));
- ESP_LOGD(TAG, "Triggering EVENT_STA_DISCONNECTED ");
- /* post disconnect event with reason code */
- wifi_manager_send_message(EVENT_STA_DISCONNECTED, parm );
- }
- break;
- default:
- break;
- }
- }
- else if(event_base== IP_EVENT){
- switch (event_id) {
- case IP_EVENT_STA_GOT_IP:{
- // structip_event_got_ip_t
- // tcpip_adapter_if_t if_index; /*!< Interface for which the event is received */
- // tcpip_adapter_ip6_info_t ip6_info; /*!< IPv6 address of the interface */
- // // Event structure for IP_EVENT_STA_GOT_IP, IP_EVENT_ETH_GOT_IP events
- //
- // Public Members
- //
- // tcpip_adapter_if_tif_index
- // Interface for which the event is received
- //
- // tcpip_adapter_ip_info_t ip_info
- // IP address, netmask, gatway IP address
- //
- // bool ip_changed
- // Whether the assigned IP has changed or not
- ip_event_got_ip_t * s =(ip_event_got_ip_t*)event_data;
- tcpip_adapter_if_t index = s->if_index;
- char * ip=strdup(ip4addr_ntoa(&(s->ip_info.ip)));
- char * gw=strdup(ip4addr_ntoa(&(s->ip_info.gw)));
- char * nm=strdup(ip4addr_ntoa(&(s->ip_info.netmask)));
- ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP. IP=%s, Gateway=%s, NetMask=%s, Interface: %s %s",
- ip,
- gw,
- nm,
- 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",
- s->ip_changed?"Address was changed":"Address unchanged");
- FREE_AND_NULL(ip);
- FREE_AND_NULL(gw);
- FREE_AND_NULL(nm);
- xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_WIFI_CONNECTED_BIT);
- last_connected = esp_timer_get_time();
- void * parm=malloc(sizeof(ip_event_got_ip_t));
- memcpy(parm,event_data,sizeof(ip_event_got_ip_t));
- wifi_manager_send_message(EVENT_STA_GOT_IP, parm );
- }
- break;
- case IP_EVENT_STA_LOST_IP:
- ESP_LOGI(TAG, "IP_EVENT_STA_LOST_IP");
- break;
- case IP_EVENT_AP_STAIPASSIGNED:
- ESP_LOGI(TAG, "IP_EVENT_AP_STAIPASSIGNED");
- break;
- case IP_EVENT_GOT_IP6:
- ESP_LOGI(TAG, "IP_EVENT_GOT_IP6");
- break;
- case IP_EVENT_ETH_GOT_IP:
- ESP_LOGI(TAG, "IP_EVENT_ETH_GOT_IP");
- break;
- default:
- break;
- }
- }
- }
- wifi_config_t* wifi_manager_get_wifi_sta_config(){
- return wifi_manager_config_sta;
- }
- void wifi_manager_connect_async(){
- /* in order to avoid a false positive on the front end app we need to quickly flush the ip json
- * There'se a risk the front end sees an IP or a password error when in fact
- * it's a remnant from a previous connection
- */
- if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
- ip_info_cjson= wifi_manager_clear_ip_info_json(&ip_info_cjson);
- wifi_manager_unlock_json_buffer();
- }
- wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_USER);
- }
- void set_status_message(message_severity_t severity, const char * message){
- if(ip_info_cjson==NULL){
- ip_info_cjson = wifi_manager_get_new_json(&ip_info_cjson);
- }
- if(ip_info_cjson==NULL){
- ESP_LOGE(TAG, "Error setting status message. Unable to allocate cJSON.");
- return;
- }
- cJSON * item=cJSON_GetObjectItem(ip_info_cjson, "message");
- item = wifi_manager_get_new_json(&item);
- cJSON_AddItemToObject(item, "severity", cJSON_CreateString(severity==INFO?"INFO":severity==WARNING?"WARNING":severity==ERROR?"ERROR":"" ));
- cJSON_AddItemToObject(item, "text", cJSON_CreateString(message));
- }
- char* wifi_manager_alloc_get_ip_info_json(){
- return cJSON_PrintUnformatted(ip_info_cjson);
- }
- void wifi_manager_destroy(){
- vTaskDelete(task_wifi_manager);
- task_wifi_manager = NULL;
- /* heap buffers */
- free(ip_info_json);
- free(release_url);
- cJSON_Delete(ip_info_cjson);
- cJSON_Delete(accessp_cjson);
- ip_info_cjson=NULL;
- accessp_cjson=NULL;
- free(wifi_manager_sta_ip);
- wifi_manager_sta_ip = NULL;
- if(wifi_manager_config_sta){
- free(wifi_manager_config_sta);
- wifi_manager_config_sta = NULL;
- }
- /* RTOS objects */
- vSemaphoreDelete(wifi_manager_json_mutex);
- wifi_manager_json_mutex = NULL;
- vSemaphoreDelete(wifi_manager_sta_ip_mutex);
- wifi_manager_sta_ip_mutex = NULL;
- vEventGroupDelete(wifi_manager_event_group);
- wifi_manager_event_group = NULL;
- vQueueDelete(wifi_manager_queue);
- wifi_manager_queue = NULL;
- }
- void wifi_manager_filter_unique( wifi_ap_record_t * aplist, uint16_t * aps) {
- int total_unique;
- wifi_ap_record_t * first_free;
- total_unique=*aps;
- first_free=NULL;
- for(int i=0; i<*aps-1;i++) {
- wifi_ap_record_t * ap = &aplist[i];
- /* skip the previously removed APs */
- if (ap->ssid[0] == 0) continue;
- /* remove the identical SSID+authmodes */
- for(int j=i+1; j<*aps;j++) {
- wifi_ap_record_t * ap1 = &aplist[j];
- if ( (strcmp((const char *)ap->ssid, (const char *)ap1->ssid)==0) &&
- (ap->authmode == ap1->authmode) ) { /* same SSID, different auth mode is skipped */
- /* save the rssi for the display */
- if ((ap1->rssi) > (ap->rssi)) ap->rssi=ap1->rssi;
- /* clearing the record */
- memset(ap1,0, sizeof(wifi_ap_record_t));
- }
- }
- }
- /* reorder the list so APs follow each other in the list */
- for(int i=0; i<*aps;i++) {
- wifi_ap_record_t * ap = &aplist[i];
- /* skipping all that has no name */
- if (ap->ssid[0] == 0) {
- /* mark the first free slot */
- if (first_free==NULL) first_free=ap;
- total_unique--;
- continue;
- }
- if (first_free!=NULL) {
- memcpy(first_free, ap, sizeof(wifi_ap_record_t));
- memset(ap,0, sizeof(wifi_ap_record_t));
- /* find the next free slot */
- for(int j=0; j<*aps;j++) {
- if (aplist[j].ssid[0]==0) {
- first_free=&aplist[j];
- break;
- }
- }
- }
- }
- /* update the length of the list */
- *aps = total_unique;
- }
- BaseType_t wifi_manager_send_message_to_front(message_code_t code, void *param){
- queue_message msg;
- msg.code = code;
- msg.param = param;
- return xQueueSendToFront( wifi_manager_queue, &msg, portMAX_DELAY);
- }
- BaseType_t wifi_manager_send_message(message_code_t code, void *param){
- queue_message msg;
- msg.code = code;
- msg.param = param;
- return xQueueSend( wifi_manager_queue, &msg, portMAX_DELAY);
- }
- void wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) ){
- if(cb_ptr_arr && message_code < MESSAGE_CODE_COUNT){
- cb_ptr_arr[message_code] = func_ptr;
- }
- }
- void wifi_manager_register_handlers(){
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_WIFI_READY, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_AUTHMODE_CHANGE, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_START, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STOP, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_PROBEREQRECVED, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_SUCCESS, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_FAILED, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_TIMEOUT, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_WPS_ER_PIN, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &event_handler, NULL ));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_START, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_STOP, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_LOST_IP, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &event_handler, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL));
- }
- void wifi_manager_config_ap(){
- /* SoftAP - Wifi Access Point configuration setup */
- tcpip_adapter_ip_info_t info;
- memset(&info, 0x00, sizeof(info));
- char * value = NULL;
- wifi_config_t ap_config = {
- .ap = {
- .ssid_len = 0,
- },
- };
- ESP_LOGI(TAG, "Configuring Access Point.");
- ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); /* stop AP DHCP server */
- /*
- * Set access point mode IP adapter configuration
- */
- value = config_alloc_get_default(NVS_TYPE_STR, "ap_ip_address", DEFAULT_AP_IP, 0);
- if(value!=NULL){
- ESP_LOGI(TAG, "IP Address: %s", value);
- inet_pton(AF_INET,value, &info.ip); /* access point is on a static IP */
- }
- FREE_AND_NULL(value);
- value = config_alloc_get_default(NVS_TYPE_STR, "ap_ip_gateway", CONFIG_DEFAULT_AP_GATEWAY, 0);
- if(value!=NULL){
- ESP_LOGI(TAG, "Gateway: %s", value);
- inet_pton(AF_INET,value, &info.gw); /* access point is on a static IP */
- }
- FREE_AND_NULL(value);
- value = config_alloc_get_default(NVS_TYPE_STR, "ap_ip_netmask", CONFIG_DEFAULT_AP_NETMASK, 0);
- if(value!=NULL){
- ESP_LOGI(TAG, "Netmask: %s", value);
- inet_pton(AF_INET,value, &info.netmask); /* access point is on a static IP */
- }
- FREE_AND_NULL(value);
- ESP_LOGD(TAG, "Setting tcp_ip info for interface TCPIP_ADAPTER_IF_AP");
- ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
- /*
- * Set Access Point configuration
- */
- value = config_alloc_get_default(NVS_TYPE_STR, "ap_ssid", CONFIG_DEFAULT_AP_SSID, 0);
- if(value!=NULL){
- strlcpy((char *)ap_config.ap.ssid, value,sizeof(ap_config.ap.ssid) );
- ESP_LOGI(TAG, "AP SSID: %s", (char *)ap_config.ap.ssid);
- }
- FREE_AND_NULL(value);
- value = config_alloc_get_default(NVS_TYPE_STR, "ap_pwd", DEFAULT_AP_PASSWORD, 0);
- if(value!=NULL){
- strlcpy((char *)ap_config.ap.password, value,sizeof(ap_config.ap.password) );
- ESP_LOGI(TAG, "AP Password: %s", (char *)ap_config.ap.password);
- }
- FREE_AND_NULL(value);
- value = config_alloc_get_default(NVS_TYPE_STR, "ap_channel", STR(CONFIG_DEFAULT_AP_CHANNEL), 0);
- if(value!=NULL){
- ESP_LOGI(TAG, "Channel: %s", value);
- ap_config.ap.channel=atoi(value);
- }
- FREE_AND_NULL(value);
- ap_config.ap.authmode = AP_AUTHMODE;
- ap_config.ap.ssid_hidden = DEFAULT_AP_SSID_HIDDEN;
- ap_config.ap.max_connection = DEFAULT_AP_MAX_CONNECTIONS;
- ap_config.ap.beacon_interval = DEFAULT_AP_BEACON_INTERVAL;
- ESP_LOGI(TAG, "Auth Mode: %d", ap_config.ap.authmode);
- ESP_LOGI(TAG, "SSID Hidden: %d", ap_config.ap.ssid_hidden);
- ESP_LOGI(TAG, "Max Connections: %d", ap_config.ap.max_connection);
- ESP_LOGI(TAG, "Beacon interval: %d", ap_config.ap.beacon_interval);
- ESP_LOGD(TAG, "Starting dhcps on interface TCPIP_ADAPTER_IF_AP");
- ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); /* start AP DHCP server */
- ESP_LOGD(TAG, "Setting wifi mode as WIFI_MODE_APSTA");
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
- ESP_LOGD(TAG, "Setting wifi AP configuration for WIFI_IF_AP");
- ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
- ESP_LOGD(TAG, "Setting wifi bandwidth (%d) for WIFI_IF_AP",DEFAULT_AP_BANDWIDTH);
- ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, DEFAULT_AP_BANDWIDTH));
- ESP_LOGD(TAG, "Setting wifi power save (%d) for WIFI_IF_AP",DEFAULT_STA_POWER_SAVE);
- ESP_ERROR_CHECK(esp_wifi_set_ps(DEFAULT_STA_POWER_SAVE));
- ESP_LOGD(TAG, "Done configuring Soft Access Point");
- dns_server_start();
- }
- void wifi_manager( void * pvParameters ){
- queue_message msg;
- BaseType_t xStatus;
- EventBits_t uxBits;
- uint8_t retries = 0;
- /* start http server */
- http_server_start();
- /* enqueue first event: load previous config and start AP or STA mode */
- wifi_manager_send_message(ORDER_LOAD_AND_RESTORE_STA, NULL);
- /* main processing loop */
- for(;;){
- xStatus = xQueueReceive( wifi_manager_queue, &msg, portMAX_DELAY );
- if( xStatus == pdPASS ){
- switch(msg.code){
- case EVENT_SCAN_DONE:
- /* As input param, it stores max AP number ap_records can hold. As output param, it receives the actual AP number this API returns.
- * As a consequence, ap_num MUST be reset to MAX_AP_NUM at every scan */
- ESP_LOGD(TAG, "Getting AP list records");
- ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_num));
- if(ap_num>0){
- accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * ap_num);
- ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_num, accessp_records));
- /* make sure the http server isn't trying to access the list while it gets refreshed */
- ESP_LOGD(TAG, "Preparing to build ap JSON list");
- if(wifi_manager_lock_json_buffer( pdMS_TO_TICKS(1000) )){
- /* Will remove the duplicate SSIDs from the list and update ap_num */
- wifi_manager_filter_unique(accessp_records, &ap_num);
- wifi_manager_generate_access_points_json(&accessp_cjson);
- wifi_manager_unlock_json_buffer();
- ESP_LOGD(TAG, "Done building ap JSON list");
- }
- else{
- ESP_LOGE(TAG, "could not get access to json mutex in wifi_scan");
- }
- free(accessp_records);
- }
- else{
- //
- ESP_LOGD(TAG, "No AP Found. Emptying the list.");
- accessp_cjson = wifi_manager_get_new_array_json(&accessp_cjson);
- }
- /* callback */
- if(cb_ptr_arr[msg.code]) {
- ESP_LOGD(TAG, "Invoking SCAN DONE callback");
- (*cb_ptr_arr[msg.code])(NULL);
- ESP_LOGD(TAG, "Done Invoking SCAN DONE callback");
- }
- break;
- case EVENT_REFRESH_OTA:
- if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
- wifi_manager_generate_ip_info_json( UPDATE_OTA );
- wifi_manager_unlock_json_buffer();
- }
- break;
- case ORDER_START_WIFI_SCAN:
- ESP_LOGD(TAG, "MESSAGE: ORDER_START_WIFI_SCAN");
- /* if a scan is already in progress this message is simply ignored thanks to the WIFI_MANAGER_SCAN_BIT uxBit */
- if(! isGroupBitSet(WIFI_MANAGER_SCAN_BIT) ){
- if(esp_wifi_scan_start(&scan_config, false)!=ESP_OK){
- ESP_LOGW(TAG, "Unable to start scan; wifi is trying to connect");
- // set_status_message(WARNING, "Wifi Connecting. Cannot start scan.");
- }
- else {
- xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_SCAN_BIT);
- }
- }
- else {
- ESP_LOGW(TAG, "Scan already in progress!");
- }
- /* callback */
- if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
- break;
- case ORDER_LOAD_AND_RESTORE_STA:
- ESP_LOGI(TAG, "MESSAGE: ORDER_LOAD_AND_RESTORE_STA. About to fetch wifi STA configuration");
- if(wifi_manager_fetch_wifi_sta_config()){
- ESP_LOGI(TAG, "Saved wifi found on startup. Will attempt to connect.");
- wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_RESTORE_CONNECTION);
- }
- else{
- /* no wifi saved: start soft AP! This is what should happen during a first run */
- ESP_LOGI(TAG, "No saved wifi found on startup. Starting access point.");
- wifi_manager_send_message(ORDER_START_AP, NULL);
- }
- /* callback */
- if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
- break;
- case ORDER_CONNECT_STA:
- ESP_LOGI(TAG, "MESSAGE: ORDER_CONNECT_STA - Begin");
- /* very important: precise that this connection attempt is specifically requested.
- * Param in that case is a boolean indicating if the request was made automatically
- * by the wifi_manager.
- * */
- if((BaseType_t)msg.param == CONNECTION_REQUEST_USER) {
- ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - Connection request with no nvs connection saved yet");
- xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
- }
- else if((BaseType_t)msg.param == CONNECTION_REQUEST_RESTORE_CONNECTION) {
- ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - Connection request after restoring the AP configuration");
- xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
- /* STA - Wifi Station configuration setup */
- //todo: support static ip address
- // if(wifi_settings.sta_static_ip) {
- // // There's a static ip address configured, so
- // ESP_LOGI(TAG, "Assigning static ip to STA interface. 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));
- //
- // /* stop DHCP client*/
- // ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
- // /* assign a static IP to the STA network interface */
- // ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &wifi_settings.sta_static_ip_config));
- // }
- // else {
- /* start DHCP client if not started*/
- tcpip_adapter_dhcp_status_t status;
- ESP_LOGD(TAG, "wifi_manager: Checking if DHCP client for STA interface is running");
- ESP_ERROR_CHECK(tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &status));
- if (status!=TCPIP_ADAPTER_DHCP_STARTED) {
- ESP_LOGI(TAG, "wifi_manager: Start DHCP client for STA interface");
- ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
- // }
- }
- }
- uxBits = xEventGroupGetBits(wifi_manager_event_group);
- if( uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT ){
- ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - Wifi connected bit set, ordering disconnect (WIFI_MANAGER_WIFI_CONNECTED_BIT)");
- wifi_manager_send_message(ORDER_DISCONNECT_STA, NULL);
- /* todo: reconnect */
- }
- else{
- wifi_mode_t mode;
- /* update config to latest and attempt connection */
- esp_wifi_get_mode(&mode);
- if( WIFI_MODE_APSTA != mode && WIFI_MODE_STA !=mode ){
- // the soft ap is not started, so let's set the WiFi mode to STA
- ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - setting mode WIFI_MODE_STA");
- ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
- }
- ESP_LOGD(TAG, "MESSAGE: ORDER_CONNECT_STA - setting config for WIFI_IF_STA");
- ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, wifi_manager_get_wifi_sta_config()));
- set_host_name();
- ESP_LOGI(TAG, "Wifi Connecting...");
- ESP_ERROR_CHECK(esp_wifi_connect());
- }
- /* callback */
- if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
- break;
- case EVENT_STA_DISCONNECTED:{
- wifi_event_sta_disconnected_t disc_event;
- ESP_LOGI(TAG, "MESSAGE: EVENT_STA_DISCONNECTED");
- if(msg.param == NULL){
- ESP_LOGE(TAG, "MESSAGE: EVENT_STA_DISCONNECTED - expected parameter not found!");
- }
- else{
- memcpy(&disc_event,(wifi_event_sta_disconnected_t*)msg.param,sizeof(disc_event));
- free(msg.param);
- ESP_LOGI(TAG, "MESSAGE: EVENT_STA_DISCONNECTED with Reason code: %d (%s)", disc_event.reason, get_disconnect_code_desc(disc_event.reason));
- }
- /* this even can be posted in numerous different conditions
- *
- * 1. SSID password is wrong
- * 2. Manual disconnection ordered
- * 3. Connection lost
- *
- * Having clear understand as to WHY the event was posted is key to having an efficient wifi manager
- *
- * With wifi_manager, we determine:
- * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT is set, We consider it's a client that requested the connection.
- * When SYSTEM_EVENT_STA_DISCONNECTED is posted, it's probably a password/something went wrong with the handshake.
- *
- * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT is set, it's a disconnection that was ASKED by the client (clicking disconnect in the app)
- * When SYSTEM_EVENT_STA_DISCONNECTED is posted, saved wifi is erased from the NVS memory.
- *
- * If WIFI_MANAGER_REQUEST_STA_CONNECT_BIT and WIFI_MANAGER_REQUEST_STA_CONNECT_BIT are NOT set, it's a lost connection
- *
- * In this version of the software, reason codes are not used. They are indicated here for potential future usage.
- *
- * REASON CODE:
- * 1 UNSPECIFIED
- * 2 AUTH_EXPIRE auth no longer valid, this smells like someone changed a password on the AP
- * 3 AUTH_LEAVE
- * 4 ASSOC_EXPIRE
- * 5 ASSOC_TOOMANY too many devices already connected to the AP => AP fails to respond
- * 6 NOT_AUTHED
- * 7 NOT_ASSOCED
- * 8 ASSOC_LEAVE
- * 9 ASSOC_NOT_AUTHED
- * 10 DISASSOC_PWRCAP_BAD
- * 11 DISASSOC_SUPCHAN_BAD
- * 12 <n/a>
- * 13 IE_INVALID
- * 14 MIC_FAILURE
- * 15 4WAY_HANDSHAKE_TIMEOUT wrong password! This was personnaly tested on my home wifi with a wrong password.
- * 16 GROUP_KEY_UPDATE_TIMEOUT
- * 17 IE_IN_4WAY_DIFFERS
- * 18 GROUP_CIPHER_INVALID
- * 19 PAIRWISE_CIPHER_INVALID
- * 20 AKMP_INVALID
- * 21 UNSUPP_RSN_IE_VERSION
- * 22 INVALID_RSN_IE_CAP
- * 23 802_1X_AUTH_FAILED wrong password?
- * 24 CIPHER_SUITE_REJECTED
- * 200 BEACON_TIMEOUT
- * 201 NO_AP_FOUND
- * 202 AUTH_FAIL
- * 203 ASSOC_FAIL
- * 204 HANDSHAKE_TIMEOUT
- *
- * */
- /* reset saved sta IP */
- wifi_manager_safe_update_sta_ip_string((struct ip4_addr * )0);
- uxBits = xEventGroupGetBits(wifi_manager_event_group);
- if( uxBits & WIFI_MANAGER_REQUEST_STA_CONNECT_BIT ){
- ESP_LOGW(TAG, "WiFi Disconnected while processing user connect request. Wrong password?");
- /* there are no retries when it's a user requested connection by design. This avoids a user hanging too much
- * in case they typed a wrong password for instance. Here we simply clear the request bit and move on */
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
- if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
- wifi_manager_generate_ip_info_json( UPDATE_FAILED_ATTEMPT );
- wifi_manager_unlock_json_buffer();
- }
- }
- else if (uxBits & WIFI_MANAGER_REQUEST_DISCONNECT_BIT){
- ESP_LOGI(TAG, "WiFi disconnected by user");
- /* user manually requested a disconnect so the lost connection is a normal event. Clear the flag and restart the AP */
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
- if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
- wifi_manager_generate_ip_info_json( UPDATE_USER_DISCONNECT );
- wifi_manager_unlock_json_buffer();
- }
- /* erase configuration */
- if(wifi_manager_config_sta){
- ESP_LOGI(TAG, "Erasing WiFi Configuration.");
- memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
- /* save NVS memory */
- wifi_manager_save_sta_config();
- }
- /* start SoftAP */
- ESP_LOGD(TAG, "Disconnect processing complete. Ordering an AP start.");
- wifi_manager_send_message(ORDER_START_AP, NULL);
- }
- else{
- /* lost connection ? */
- ESP_LOGE(TAG, "WiFi Connection lost.");
- if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
- wifi_manager_generate_ip_info_json( UPDATE_LOST_CONNECTION );
- wifi_manager_unlock_json_buffer();
- }
- if(retries < WIFI_MANAGER_MAX_RETRY){
- ESP_LOGD(TAG, "Issuing ORDER_CONNECT_STA to retry connection.");
- if(!bHasConnected) retries++;
- wifi_manager_send_message(ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_AUTO_RECONNECT);
- }
- else{
- /* In this scenario the connection was lost beyond repair: kick start the AP! */
- retries = 0;
- ESP_LOGW(TAG, "All connect retry attempts failed.");
- /* if it was a restore attempt connection, we clear the bit */
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
- ESP_LOGD(TAG, "Issuing ORDER_START_AP to trigger AP start.");
- /* start SoftAP */
- wifi_manager_send_message(ORDER_START_AP, NULL);
- }
- }
- /* callback */
- if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
- }
- break;
- case ORDER_START_AP:
- ESP_LOGI(TAG, "MESSAGE: ORDER_START_AP");
- wifi_manager_config_ap();
- ESP_LOGD(TAG, "AP Starting, requesting wifi scan.");
- wifi_manager_scan_async();
- /* callback */
- if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
- break;
- case EVENT_STA_GOT_IP:
- ESP_LOGI(TAG, "MESSAGE: EVENT_STA_GOT_IP");
- uxBits = xEventGroupGetBits(wifi_manager_event_group);
- /* reset connection requests bits -- doesn't matter if it was set or not */
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_STA_CONNECT_BIT);
- /* save IP as a string for the HTTP server host */
- //s->ip_info.ip.addr
- ip_event_got_ip_t * event =(ip_event_got_ip_t*)msg.param;
- wifi_manager_safe_update_sta_ip_string(&(event->ip_info.ip));
- free(msg.param);
- /* save wifi config in NVS if it wasn't a restored of a connection */
- if(uxBits & WIFI_MANAGER_REQUEST_RESTORE_STA_BIT){
- ESP_LOGD(TAG, "Configuration came from nvs, no need to save.");
- xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT);
- }
- else{
- ESP_LOGD(TAG, "Connection was initiated by user, storing config to nvs.");
- wifi_manager_save_sta_config();
- }
- /* refresh JSON with the new IP */
- if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
- /* generate the connection info with success */
- wifi_manager_generate_ip_info_json( UPDATE_CONNECTION_OK );
- wifi_manager_unlock_json_buffer();
- }
- else {
- ESP_LOGW(TAG, "Unable to lock status json buffer. ");
- }
- /* bring down DNS hijack */
- ESP_LOGD(TAG, "Stopping dns server.");
- dns_server_stop();
- bHasConnected=true;
- /* callback */
- if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
- break;
- case UPDATE_CONNECTION_OK:
- /* refresh JSON with the new ota data */
- if(wifi_manager_lock_json_buffer( portMAX_DELAY )){
- /* generate the connection info with success */
- wifi_manager_generate_ip_info_json( UPDATE_CONNECTION_OK );
- wifi_manager_unlock_json_buffer();
- }
- break;
- case ORDER_DISCONNECT_STA:
- ESP_LOGI(TAG, "MESSAGE: ORDER_DISCONNECT_STA. Calling esp_wifi_disconnect()");
- /* precise this is coming from a user request */
- xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_DISCONNECT_BIT);
- /* order wifi discconect */
- ESP_ERROR_CHECK(esp_wifi_disconnect());
- /* callback */
- if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
- break;
- case ORDER_RESTART_OTA:
- ESP_LOGD(TAG, "Calling guided_restart_ota.");
- guided_restart_ota();
- break;
- case ORDER_RESTART_OTA_URL:
- ESP_LOGD(TAG, "Calling start_ota.");
- start_ota(msg.param);
- free(msg.param);
- break;
- case ORDER_RESTART_RECOVERY:
- ESP_LOGD(TAG, "Calling guided_factory.");
- guided_factory();
- break;
- case ORDER_RESTART:
- ESP_LOGD(TAG, "Calling simple_restart.");
- simple_restart();
- break;
- default:
- break;
- } /* end of switch/case */
- } /* end of if status=pdPASS */
- } /* end of for loop */
- vTaskDelete( NULL );
- }
|