Skip to content

Commit 235b63e

Browse files
authored
Merge pull request #106 from tonyp7/bug-fixes
fix #105
2 parents 75351ee + 16b07c7 commit 235b63e

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

src/http_app.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static esp_err_t http_server_get_handler(httpd_req_t *req){
215215
size_t buf_len;
216216
esp_err_t ret = ESP_OK;
217217

218-
ESP_LOGI(TAG, "GET %s", req->uri);
218+
ESP_LOGD(TAG, "GET %s", req->uri);
219219

220220
/* Get header value string length and allocate memory for length + 1,
221221
* extra byte for null termination */
@@ -245,8 +245,6 @@ static esp_err_t http_server_get_handler(httpd_req_t *req){
245245
}
246246
else{
247247

248-
ESP_LOGD(TAG, "GET %s", req->uri);
249-
250248
/* GET / */
251249
if(strcmp(req->uri, http_root_url) == 0){
252250
httpd_resp_set_status(req, http_200_hdr);

src/nvs_sync.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,27 @@ SOFTWARE.
3030
#include <stdbool.h>
3131
#include <freertos/FreeRTOS.h>
3232
#include <freertos/semphr.h>
33+
#include <esp_err.h>
3334
#include "nvs_sync.h"
3435

3536

3637
static SemaphoreHandle_t nvs_sync_mutex = NULL;
3738

38-
bool nvs_sync_create(){
39+
esp_err_t nvs_sync_create(){
3940
if(nvs_sync_mutex == NULL){
41+
4042
nvs_sync_mutex = xSemaphoreCreateMutex();
41-
}
4243

43-
return nvs_sync_mutex != NULL;
44+
if(nvs_sync_mutex){
45+
return ESP_OK;
46+
}
47+
else{
48+
return ESP_FAIL;
49+
}
50+
}
51+
else{
52+
return ESP_OK;
53+
}
4454
}
4555

4656
void nvs_sync_free(){

src/nvs_sync.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ SOFTWARE.
3434

3535
#include <stdbool.h> /* for type bool */
3636
#include <freertos/FreeRTOS.h> /* for TickType_t */
37+
#include <esp_err.h> /* for esp_err_t */
3738

3839
#ifdef __cplusplus
3940
extern "C" {
@@ -56,9 +57,10 @@ void nvs_sync_unlock();
5657

5758
/**
5859
* @brief Create the NVS semaphore
59-
* @return true on success or if the semaphore already exists, false otherwise
60+
* @return ESP_OK: success or if the semaphore already exists
61+
* ESP_FAIL: failure
6062
*/
61-
bool nvs_sync_create();
63+
esp_err_t nvs_sync_create();
6264

6365
/**
6466
* @brief Frees memory associated with the NVS semaphore

src/wifi_manager.c

+29-7
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void wifi_manager_start(){
180180

181181
/* initialize flash memory */
182182
nvs_flash_init();
183-
nvs_sync_create(); /* semaphore for thread synchronization on NVS memory */
183+
ESP_ERROR_CHECK(nvs_sync_create()); /* semaphore for thread synchronization on NVS memory */
184184

185185
/* memory allocation */
186186
wifi_manager_queue = xQueueCreate( 3, sizeof( queue_message) );
@@ -225,19 +225,25 @@ esp_err_t wifi_manager_save_sta_config(){
225225
memset(&tmp_settings, 0x00, sizeof(tmp_settings));
226226
bool change = false;
227227

228-
ESP_LOGI(TAG, "About to save config to flash");
228+
ESP_LOGI(TAG, "About to save config to flash!!");
229229

230230
if(wifi_manager_config_sta && nvs_sync_lock( portMAX_DELAY )){
231231

232232
esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READWRITE, &handle);
233-
if (esp_err != ESP_OK) return esp_err;
233+
if (esp_err != ESP_OK){
234+
nvs_sync_unlock();
235+
return esp_err;
236+
}
234237

235238
sz = sizeof(tmp_conf.sta.ssid);
236239
esp_err = nvs_get_blob(handle, "ssid", tmp_conf.sta.ssid, &sz);
237240
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){
238241
/* different ssid or ssid does not exist in flash: save new ssid */
239242
esp_err = nvs_set_blob(handle, "ssid", wifi_manager_config_sta->sta.ssid, 32);
240-
if (esp_err != ESP_OK) return esp_err;
243+
if (esp_err != ESP_OK){
244+
nvs_sync_unlock();
245+
return esp_err;
246+
}
241247
change = true;
242248
ESP_LOGI(TAG, "wifi_manager_wrote wifi_sta_config: ssid:%s",wifi_manager_config_sta->sta.ssid);
243249

@@ -248,7 +254,10 @@ esp_err_t wifi_manager_save_sta_config(){
248254
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){
249255
/* different password or password does not exist in flash: save new password */
250256
esp_err = nvs_set_blob(handle, "password", wifi_manager_config_sta->sta.password, 64);
251-
if (esp_err != ESP_OK) return esp_err;
257+
if (esp_err != ESP_OK){
258+
nvs_sync_unlock();
259+
return esp_err;
260+
}
252261
change = true;
253262
ESP_LOGI(TAG, "wifi_manager_wrote wifi_sta_config: password:%s",wifi_manager_config_sta->sta.password);
254263
}
@@ -267,7 +276,10 @@ esp_err_t wifi_manager_save_sta_config(){
267276
)
268277
){
269278
esp_err = nvs_set_blob(handle, "settings", &wifi_settings, sizeof(wifi_settings));
270-
if (esp_err != ESP_OK) return esp_err;
279+
if (esp_err != ESP_OK){
280+
nvs_sync_unlock();
281+
return esp_err;
282+
}
271283
change = true;
272284

273285
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(){
292304
nvs_sync_unlock();
293305

294306
}
307+
else{
308+
ESP_LOGE(TAG, "wifi_manager_save_sta_config failed to acquire nvs_sync mutex");
309+
}
295310

296311
return ESP_OK;
297312
}
@@ -300,7 +315,14 @@ bool wifi_manager_fetch_wifi_sta_config(){
300315

301316
nvs_handle handle;
302317
esp_err_t esp_err;
303-
if(nvs_sync_lock( portMAX_DELAY ) && nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle) == ESP_OK){
318+
if(nvs_sync_lock( portMAX_DELAY )){
319+
320+
esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle);
321+
322+
if(esp_err != ESP_OK){
323+
nvs_sync_unlock();
324+
return false;
325+
}
304326

305327
if(wifi_manager_config_sta == NULL){
306328
wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));

0 commit comments

Comments
 (0)