-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from tonyp7/new-features
New features
- Loading branch information
Showing
4 changed files
with
184 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
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 <stdbool.h> | ||
#include <freertos/FreeRTOS.h> | ||
#include <freertos/semphr.h> | ||
#include "nvs_sync.h" | ||
|
||
|
||
static SemaphoreHandle_t nvs_sync_mutex = NULL; | ||
|
||
bool nvs_sync_create(){ | ||
if(nvs_sync_mutex == NULL){ | ||
nvs_sync_mutex = xSemaphoreCreateMutex(); | ||
} | ||
|
||
return nvs_sync_mutex != NULL; | ||
} | ||
|
||
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
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 <stdbool.h> /* for type bool */ | ||
#include <freertos/FreeRTOS.h> /* for TickType_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 true on success or if the semaphore already exists, false otherwise | ||
*/ | ||
bool 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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters