diff --git a/src/http_app.c b/src/http_app.c index 2a5ee163..8dbc40ef 100644 --- a/src/http_app.c +++ b/src/http_app.c @@ -215,7 +215,7 @@ static esp_err_t http_server_get_handler(httpd_req_t *req){ size_t buf_len; esp_err_t ret = ESP_OK; - ESP_LOGI(TAG, "GET %s", req->uri); + ESP_LOGD(TAG, "GET %s", req->uri); /* Get header value string length and allocate memory for length + 1, * extra byte for null termination */ @@ -245,8 +245,6 @@ static esp_err_t http_server_get_handler(httpd_req_t *req){ } else{ - ESP_LOGD(TAG, "GET %s", req->uri); - /* GET / */ if(strcmp(req->uri, http_root_url) == 0){ httpd_resp_set_status(req, http_200_hdr); diff --git a/src/nvs_sync.c b/src/nvs_sync.c index cd3bbc40..4ceed8bd 100644 --- a/src/nvs_sync.c +++ b/src/nvs_sync.c @@ -30,17 +30,27 @@ SOFTWARE. #include #include #include +#include #include "nvs_sync.h" static SemaphoreHandle_t nvs_sync_mutex = NULL; -bool nvs_sync_create(){ +esp_err_t nvs_sync_create(){ if(nvs_sync_mutex == NULL){ + nvs_sync_mutex = xSemaphoreCreateMutex(); - } - return nvs_sync_mutex != NULL; + if(nvs_sync_mutex){ + return ESP_OK; + } + else{ + return ESP_FAIL; + } + } + else{ + return ESP_OK; + } } void nvs_sync_free(){ diff --git a/src/nvs_sync.h b/src/nvs_sync.h index 0b2e33d1..e03396e5 100644 --- a/src/nvs_sync.h +++ b/src/nvs_sync.h @@ -34,6 +34,7 @@ SOFTWARE. #include /* for type bool */ #include /* for TickType_t */ +#include /* for esp_err_t */ #ifdef __cplusplus extern "C" { @@ -56,9 +57,10 @@ void nvs_sync_unlock(); /** * @brief Create the NVS semaphore - * @return true on success or if the semaphore already exists, false otherwise + * @return ESP_OK: success or if the semaphore already exists + * ESP_FAIL: failure */ -bool nvs_sync_create(); +esp_err_t nvs_sync_create(); /** * @brief Frees memory associated with the NVS semaphore diff --git a/src/wifi_manager.c b/src/wifi_manager.c index fb6c149e..84e3230c 100644 --- a/src/wifi_manager.c +++ b/src/wifi_manager.c @@ -180,7 +180,7 @@ void wifi_manager_start(){ /* initialize flash memory */ nvs_flash_init(); - nvs_sync_create(); /* semaphore for thread synchronization on NVS memory */ + ESP_ERROR_CHECK(nvs_sync_create()); /* semaphore for thread synchronization on NVS memory */ /* memory allocation */ wifi_manager_queue = xQueueCreate( 3, sizeof( queue_message) ); @@ -225,19 +225,25 @@ esp_err_t wifi_manager_save_sta_config(){ memset(&tmp_settings, 0x00, sizeof(tmp_settings)); bool change = false; - ESP_LOGI(TAG, "About to save config to flash"); + 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) return esp_err; + 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) return esp_err; + 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); @@ -248,7 +254,10 @@ esp_err_t wifi_manager_save_sta_config(){ 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) return esp_err; + 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); } @@ -267,7 +276,10 @@ esp_err_t wifi_manager_save_sta_config(){ ) ){ esp_err = nvs_set_blob(handle, "settings", &wifi_settings, sizeof(wifi_settings)); - if (esp_err != ESP_OK) return esp_err; + 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); @@ -292,6 +304,9 @@ esp_err_t wifi_manager_save_sta_config(){ nvs_sync_unlock(); } + else{ + ESP_LOGE(TAG, "wifi_manager_save_sta_config failed to acquire nvs_sync mutex"); + } return ESP_OK; } @@ -300,7 +315,14 @@ bool wifi_manager_fetch_wifi_sta_config(){ nvs_handle handle; esp_err_t esp_err; - if(nvs_sync_lock( portMAX_DELAY ) && nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle) == ESP_OK){ + 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));