From 016d0e2416b1519e8cb21f2ba96b459b33e4d13c Mon Sep 17 00:00:00 2001 From: Luke Cyca Date: Mon, 19 Oct 2020 20:26:19 -0700 Subject: [PATCH] Remove bespoke nvs --- src/dns_server.c | 1 - src/nvs_sync.c | 79 ------------------ src/nvs_sync.h | 76 ----------------- src/wifi_manager.c | 203 +++------------------------------------------ src/wifi_manager.h | 11 --- 5 files changed, 13 insertions(+), 357 deletions(-) delete mode 100644 src/nvs_sync.c delete mode 100644 src/nvs_sync.h diff --git a/src/dns_server.c b/src/dns_server.c index 6693913a..ceaea233 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -42,7 +42,6 @@ Contains the freeRTOS task for the DNS server that processes the requests. #include #include #include -#include #include #include #include diff --git a/src/nvs_sync.c b/src/nvs_sync.c deleted file mode 100644 index 4ceed8bd..00000000 --- a/src/nvs_sync.c +++ /dev/null @@ -1,79 +0,0 @@ -/** -Copyright (c) 2020 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 nvs_sync.c -@author Tony Pottier -@brief Exposes a simple API to synchronize NVS memory read and writes - -@see https://idyl.io -@see https://github.com/tonyp7/esp32-wifi-manager -*/ - -#include -#include -#include -#include -#include "nvs_sync.h" - - -static SemaphoreHandle_t nvs_sync_mutex = NULL; - -esp_err_t nvs_sync_create(){ - if(nvs_sync_mutex == NULL){ - - nvs_sync_mutex = xSemaphoreCreateMutex(); - - if(nvs_sync_mutex){ - return ESP_OK; - } - else{ - return ESP_FAIL; - } - } - else{ - return ESP_OK; - } -} - -void nvs_sync_free(){ - if(nvs_sync_mutex != NULL){ - vSemaphoreDelete( nvs_sync_mutex ); - nvs_sync_mutex = NULL; - } -} - -bool nvs_sync_lock(TickType_t xTicksToWait){ - if(nvs_sync_mutex){ - if( xSemaphoreTake( nvs_sync_mutex, xTicksToWait ) == pdTRUE ) { - return true; - } - else{ - return false; - } - } - else{ - return false; - } -} - -void nvs_sync_unlock(){ - xSemaphoreGive( nvs_sync_mutex ); -} \ No newline at end of file diff --git a/src/nvs_sync.h b/src/nvs_sync.h deleted file mode 100644 index e03396e5..00000000 --- a/src/nvs_sync.h +++ /dev/null @@ -1,76 +0,0 @@ -/** -Copyright (c) 2020 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 nvs_sync.h -@author Tony Pottier -@brief Exposes a simple API to synchronize NVS memory read and writes - -@see https://idyl.io -@see https://github.com/tonyp7/esp32-wifi-manager -*/ - - - -#ifndef WIFI_MANAGER_NVS_SYNC_H_INCLUDED -#define WIFI_MANAGER_NVS_SYNC_H_INCLUDED - -#include /* for type bool */ -#include /* for TickType_t */ -#include /* for esp_err_t */ - -#ifdef __cplusplus -extern "C" { -#endif - - -/** - * @brief Attempts to get hold of the NVS semaphore for a set amount of ticks. - * @note If you are uncertain about the number of ticks to wait use portMAX_DELAY. - * @return true on a succesful lock, false otherwise - */ -bool nvs_sync_lock(TickType_t xTicksToWait); - - -/** - * @brief Releases the NVS semaphore - */ -void nvs_sync_unlock(); - - -/** - * @brief Create the NVS semaphore - * @return ESP_OK: success or if the semaphore already exists - * ESP_FAIL: failure - */ -esp_err_t nvs_sync_create(); - -/** - * @brief Frees memory associated with the NVS semaphore - * @warning Do not delete a semaphore that has tasks blocked on it (tasks that are in the Blocked state waiting for the semaphore to become available). - */ -void nvs_sync_free(); - - -#ifdef __cplusplus -} -#endif - -#endif /* WIFI_MANAGER_NVS_SYNC_H_INCLUDED */ \ No newline at end of file diff --git a/src/wifi_manager.c b/src/wifi_manager.c index 3d11a940..bcd395a9 100644 --- a/src/wifi_manager.c +++ b/src/wifi_manager.c @@ -57,7 +57,6 @@ Contains the freeRTOS task and all necessary support #include "json.h" #include "dns_server.h" -#include "nvs_sync.h" #include "wifi_manager.h" @@ -111,8 +110,6 @@ struct wifi_settings_t wifi_settings = { .sta_static_ip = 0, }; -const char wifi_manager_nvs_namespace[] = "espwifimgr"; - static EventGroupHandle_t wifi_manager_event_group; /* @brief indicate that the ESP32 is currently connected. */ @@ -180,8 +177,6 @@ void wifi_manager_start(){ /* initialize flash memory */ nvs_flash_init(); - ESP_ERROR_CHECK(nvs_sync_create()); /* semaphore for thread synchronization on NVS memory */ - /* memory allocation */ wifi_manager_queue = xQueueCreate( 3, sizeof( queue_message) ); wifi_manager_json_mutex = xSemaphoreCreateMutex(); @@ -212,184 +207,6 @@ void wifi_manager_start(){ 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; - size_t sz; - - /* variables used to check if write is really needed */ - wifi_config_t tmp_conf; - struct wifi_settings_t tmp_settings; - memset(&tmp_conf, 0x00, sizeof(tmp_conf)); - memset(&tmp_settings, 0x00, sizeof(tmp_settings)); - bool change = false; - - ESP_LOGI(TAG, "About to save config to flash!!"); - - if(wifi_manager_config_sta && nvs_sync_lock( portMAX_DELAY )){ - - esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle); - if (esp_err != ESP_OK){ - nvs_sync_unlock(); - return esp_err; - } - - sz = sizeof(tmp_conf.sta.ssid); - esp_err = nvs_get_blob(handle, "ssid", tmp_conf.sta.ssid, &sz); - if( (esp_err == ESP_OK || esp_err == ESP_ERR_NVS_NOT_FOUND) && strcmp( (char*)tmp_conf.sta.ssid, (char*)wifi_manager_config_sta->sta.ssid) != 0){ - /* different ssid or ssid does not exist in flash: save new ssid */ - esp_err = nvs_set_blob(handle, "ssid", wifi_manager_config_sta->sta.ssid, 32); - if (esp_err != ESP_OK){ - nvs_sync_unlock(); - return esp_err; - } - change = true; - ESP_LOGI(TAG, "wifi_manager_wrote wifi_sta_config: ssid:%s",wifi_manager_config_sta->sta.ssid); - - } - - sz = sizeof(tmp_conf.sta.password); - esp_err = nvs_get_blob(handle, "password", tmp_conf.sta.password, &sz); - if( (esp_err == ESP_OK || esp_err == ESP_ERR_NVS_NOT_FOUND) && strcmp( (char*)tmp_conf.sta.password, (char*)wifi_manager_config_sta->sta.password) != 0){ - /* different password or password does not exist in flash: save new password */ - esp_err = nvs_set_blob(handle, "password", wifi_manager_config_sta->sta.password, 64); - if (esp_err != ESP_OK){ - nvs_sync_unlock(); - return esp_err; - } - change = true; - ESP_LOGI(TAG, "wifi_manager_wrote wifi_sta_config: password:%s",wifi_manager_config_sta->sta.password); - } - - sz = sizeof(tmp_settings); - esp_err = nvs_get_blob(handle, "settings", &tmp_settings, &sz); - if( (esp_err == ESP_OK || esp_err == ESP_ERR_NVS_NOT_FOUND) && - ( - strcmp( (char*)tmp_settings.ap_ssid, (char*)wifi_settings.ap_ssid) != 0 || - strcmp( (char*)tmp_settings.ap_pwd, (char*)wifi_settings.ap_pwd) != 0 || - tmp_settings.ap_ssid_hidden != wifi_settings.ap_ssid_hidden || - tmp_settings.ap_bandwidth != wifi_settings.ap_bandwidth || - tmp_settings.sta_only != wifi_settings.sta_only || - tmp_settings.sta_power_save != wifi_settings.sta_power_save || - tmp_settings.ap_channel != wifi_settings.ap_channel - ) - ){ - esp_err = nvs_set_blob(handle, "settings", &wifi_settings, sizeof(wifi_settings)); - if (esp_err != ESP_OK){ - nvs_sync_unlock(); - return esp_err; - } - change = true; - - ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: SoftAP_ssid: %s",wifi_settings.ap_ssid); - ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: SoftAP_pwd: %s",wifi_settings.ap_pwd); - ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: SoftAP_channel: %i",wifi_settings.ap_channel); - ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: SoftAP_hidden (1 = yes): %i",wifi_settings.ap_ssid_hidden); - ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: SoftAP_bandwidth (1 = 20MHz, 2 = 40MHz): %i",wifi_settings.ap_bandwidth); - ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: sta_only (0 = APSTA, 1 = STA when connected): %i",wifi_settings.sta_only); - ESP_LOGD(TAG, "wifi_manager_wrote wifi_settings: sta_power_save (1 = yes): %i",wifi_settings.sta_power_save); - } - - if(change){ - esp_err = nvs_commit(handle); - } - else{ - ESP_LOGI(TAG, "Wifi config was not saved to flash because no change has been detected."); - } - - if (esp_err != ESP_OK) return esp_err; - - nvs_close(handle); - nvs_sync_unlock(); - - } - else{ - ESP_LOGE(TAG, "wifi_manager_save_sta_config failed to acquire nvs_sync mutex"); - } - - return ESP_OK; -} - -bool wifi_manager_fetch_wifi_sta_config(){ - - nvs_handle handle; - esp_err_t esp_err; - if(nvs_sync_lock( portMAX_DELAY )){ - - esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle); - - if(esp_err != ESP_OK){ - nvs_sync_unlock(); - return false; - } - - if(wifi_manager_config_sta == NULL){ - wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t)); - } - memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t)); - - /* allocate buffer */ - size_t sz = sizeof(wifi_settings); - uint8_t *buff = (uint8_t*)malloc(sizeof(uint8_t) * sz); - memset(buff, 0x00, sizeof(sz)); - - /* ssid */ - sz = sizeof(wifi_manager_config_sta->sta.ssid); - esp_err = nvs_get_blob(handle, "ssid", buff, &sz); - if(esp_err != ESP_OK){ - free(buff); - nvs_sync_unlock(); - return false; - } - memcpy(wifi_manager_config_sta->sta.ssid, buff, sz); - - /* password */ - sz = sizeof(wifi_manager_config_sta->sta.password); - esp_err = nvs_get_blob(handle, "password", buff, &sz); - if(esp_err != ESP_OK){ - free(buff); - nvs_sync_unlock(); - return false; - } - memcpy(wifi_manager_config_sta->sta.password, buff, sz); - - /* settings */ - sz = sizeof(wifi_settings); - esp_err = nvs_get_blob(handle, "settings", buff, &sz); - if(esp_err != ESP_OK){ - free(buff); - nvs_sync_unlock(); - return false; - } - memcpy(&wifi_settings, buff, sz); - - free(buff); - nvs_close(handle); - nvs_sync_unlock(); - - - ESP_LOGI(TAG, "wifi_manager_fetch_wifi_sta_config: ssid:%s password:%s",wifi_manager_config_sta->sta.ssid,wifi_manager_config_sta->sta.password); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: SoftAP_ssid:%s",wifi_settings.ap_ssid); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: SoftAP_pwd:%s",wifi_settings.ap_pwd); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: SoftAP_channel:%i",wifi_settings.ap_channel); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: SoftAP_hidden (1 = yes):%i",wifi_settings.ap_ssid_hidden); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: SoftAP_bandwidth (1 = 20MHz, 2 = 40MHz)%i",wifi_settings.ap_bandwidth); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: sta_only (0 = APSTA, 1 = STA when connected):%i",wifi_settings.sta_only); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: sta_power_save (1 = yes):%i",wifi_settings.sta_power_save); - ESP_LOGD(TAG, "wifi_manager_fetch_wifi_settings: sta_static_ip (0 = dhcp client, 1 = static ip):%i",wifi_settings.sta_static_ip); - - return wifi_manager_config_sta->sta.ssid[0] != '\0'; - - - } - else{ - return false; - } - -} - - void wifi_manager_clear_ip_info_json(){ strcpy(ip_info_json, "{}\n"); } @@ -764,6 +581,10 @@ void wifi_manager_connect_async(){ wifi_manager_clear_ip_info_json(); wifi_manager_unlock_json_buffer(); } + + /* update config to latest and attempt connection */ + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_manager_get_wifi_sta_config())); + wifi_manager_send_message(WM_ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_USER); } @@ -910,7 +731,6 @@ void wifi_manager( void * pvParameters ){ /* default wifi config */ wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); /* event handler for the connection */ esp_event_handler_instance_t instance_wifi_event; @@ -1027,7 +847,11 @@ void wifi_manager( void * pvParameters ){ case WM_ORDER_LOAD_AND_RESTORE_STA: ESP_LOGI(TAG, "MESSAGE: ORDER_LOAD_AND_RESTORE_STA"); - if(wifi_manager_fetch_wifi_sta_config()){ + /* Check if a wifi SSID is already set */ + wifi_config_t wifi_cfg; + ESP_ERROR_CHECK(esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK); + + if (strlen((const char *) wifi_cfg.sta.ssid)) { ESP_LOGI(TAG, "Saved wifi found on startup. Will attempt to connect."); wifi_manager_send_message(WM_ORDER_CONNECT_STA, (void*)CONNECTION_REQUEST_RESTORE_CONNECTION); } @@ -1058,9 +882,6 @@ void wifi_manager( void * pvParameters ){ uxBits = xEventGroupGetBits(wifi_manager_event_group); if( ! (uxBits & WIFI_MANAGER_WIFI_CONNECTED_BIT) ){ - /* update config to latest and attempt connection */ - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_manager_get_wifi_sta_config())); - /* if there is a wifi scan in progress abort it first Calling esp_wifi_scan_stop will trigger a SCAN_DONE event which will reset this bit */ if(uxBits & WIFI_MANAGER_SCAN_BIT){ @@ -1166,7 +987,8 @@ void wifi_manager( void * pvParameters ){ } /* save NVS memory */ - wifi_manager_save_sta_config(); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_manager_config_sta) ); + /* start SoftAP */ wifi_manager_send_message(WM_ORDER_START_AP, NULL); @@ -1268,7 +1090,8 @@ void wifi_manager( void * pvParameters ){ xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_RESTORE_STA_BIT); } else{ - wifi_manager_save_sta_config(); + ESP_LOGI(TAG, "WM_EVENT_STA_GOT_IP Stored"); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_manager_config_sta) ); } /* reset number of retries */ diff --git a/src/wifi_manager.h b/src/wifi_manager.h index cba3b989..e2f0eb83 100644 --- a/src/wifi_manager.h +++ b/src/wifi_manager.h @@ -306,17 +306,6 @@ char* wifi_manager_get_ip_info_json(); void wifi_manager_scan_async(); -/** - * @brief saves the current STA wifi config to flash ram storage. - */ -esp_err_t wifi_manager_save_sta_config(); - -/** - * @brief fetch a previously STA wifi config in the flash ram storage. - * @return true if a previously saved config was found, false otherwise. - */ -bool wifi_manager_fetch_wifi_sta_config(); - wifi_config_t* wifi_manager_get_wifi_sta_config();