Skip to content

Commit

Permalink
Merge pull request #106 from tonyp7/bug-fixes
Browse files Browse the repository at this point in the history
fix #105
  • Loading branch information
tonyp7 authored Aug 20, 2020
2 parents 75351ee + 16b07c7 commit 235b63e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/http_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 13 additions & 3 deletions src/nvs_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,27 @@ SOFTWARE.
#include <stdbool.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <esp_err.h>
#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(){
Expand Down
6 changes: 4 additions & 2 deletions src/nvs_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOFTWARE.

#include <stdbool.h> /* for type bool */
#include <freertos/FreeRTOS.h> /* for TickType_t */
#include <esp_err.h> /* for esp_err_t */

#ifdef __cplusplus
extern "C" {
Expand All @@ -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
Expand Down
36 changes: 29 additions & 7 deletions src/wifi_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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));
Expand Down

0 comments on commit 235b63e

Please sign in to comment.