code optimization
- lifecycle functions for widgets - persistence functions Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
persistence.c
|
||||
espressif/persistence.c
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
nvs_flash
|
||||
|
116
components/ruth/espressif/persistence.c
Normal file
116
components/ruth/espressif/persistence.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "persistence.h"
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
static const char *TAG = "persistence";
|
||||
|
||||
static nvs_handle_t persistence_handle;
|
||||
static SemaphoreHandle_t persistence_mutex;
|
||||
|
||||
void *persistence_init(const char *namespace_name)
|
||||
{
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
ESP_ERROR_CHECK(nvs_open(namespace_name, NVS_READWRITE, &persistence_handle));
|
||||
|
||||
persistence_mutex = xSemaphoreCreateMutex();
|
||||
if (persistence_mutex == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to create mutex");
|
||||
}
|
||||
return &persistence_handle;
|
||||
}
|
||||
|
||||
void persistence_save(persistence_value_t value_type, const char *key, const void *value)
|
||||
{
|
||||
if (persistence_mutex != NULL)
|
||||
{
|
||||
if (xSemaphoreTake(persistence_mutex, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
esp_err_t err = ESP_ERR_INVALID_ARG;
|
||||
|
||||
switch (value_type)
|
||||
{
|
||||
case VALUE_TYPE_STRING:
|
||||
err = nvs_set_str(persistence_handle, key, (char *)value);
|
||||
break;
|
||||
|
||||
case VALUE_TYPE_INT32:
|
||||
err = nvs_set_i32(persistence_handle, key, *(int32_t *)value);
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "Unsupported value type");
|
||||
break;
|
||||
}
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_commit(persistence_handle));
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Error saving key %s: %s", key, esp_err_to_name(err));
|
||||
}
|
||||
|
||||
xSemaphoreGive(persistence_mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void *persistence_load(persistence_value_t value_type, const char *key, void *out)
|
||||
{
|
||||
if (persistence_mutex != NULL)
|
||||
{
|
||||
if (xSemaphoreTake(persistence_mutex, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
esp_err_t err = ESP_ERR_INVALID_ARG;
|
||||
|
||||
switch (value_type)
|
||||
{
|
||||
case VALUE_TYPE_STRING:
|
||||
err = nvs_get_str(persistence_handle, key, (char *)out, NULL);
|
||||
break;
|
||||
|
||||
case VALUE_TYPE_INT32:
|
||||
err = nvs_get_i32(persistence_handle, key, (int32_t *)out);
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "Unsupported value type");
|
||||
break;
|
||||
}
|
||||
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Error loading key %s: %s", key, esp_err_to_name(err));
|
||||
}
|
||||
|
||||
xSemaphoreGive(persistence_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void persistence_deinit()
|
||||
{
|
||||
if (persistence_mutex != NULL)
|
||||
{
|
||||
vSemaphoreDelete(persistence_mutex);
|
||||
persistence_mutex = NULL;
|
||||
}
|
||||
|
||||
nvs_close(persistence_handle);
|
||||
}
|
@@ -9,10 +9,17 @@ typedef enum
|
||||
typedef struct
|
||||
{
|
||||
void *handle;
|
||||
void (*save)(const char *key, const char *value);
|
||||
void (*save)(persistence_value_t value_type, const char *key, const void *value);
|
||||
} persistence_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
void *persistence_init(const char *namespace_name);
|
||||
void persistence_save(persistence_value_t value_type, const char *key, const void *value);
|
||||
void *persistence_load(persistence_value_t value_type, const char *key, void *out);
|
||||
void persistence_deinit();
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -1,13 +1,25 @@
|
||||
#include "persistence.h"
|
||||
#include "stddef.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void *persistence_init(const char *namespace_name)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void persistence_save(persistence_value_t value_type, const char *key, const void *value)
|
||||
void persistence_save(const persistence_value_t value_type, const char *key, const void *value)
|
||||
{
|
||||
printf("Key: %s - ", key);
|
||||
switch (value_type)
|
||||
{
|
||||
case VALUE_TYPE_STRING:
|
||||
printf("Value (s): %s\n", (char *)value);
|
||||
break;
|
||||
|
||||
case VALUE_TYPE_INT32:
|
||||
printf("Value (i): %d\n", *(int32_t *)value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void *persistence_load(persistence_value_t value_type, const char *key, void *out)
|
||||
@@ -17,121 +29,4 @@ void *persistence_load(persistence_value_t value_type, const char *key, void *ou
|
||||
|
||||
void persistence_deinit()
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
static const char *TAG = "persistence";
|
||||
|
||||
static nvs_handle_t persistence_handle;
|
||||
static SemaphoreHandle_t persistence_mutex;
|
||||
|
||||
void *persistence_init(const char *namespace_name)
|
||||
{
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
ESP_ERROR_CHECK(nvs_open(namespace_name, NVS_READWRITE, &persistence_handle));
|
||||
|
||||
persistence_mutex = xSemaphoreCreateMutex();
|
||||
if (persistence_mutex == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to create mutex");
|
||||
}
|
||||
return &persistence_handle;
|
||||
}
|
||||
|
||||
void persistence_save(persistence_value_t value_type, const char *key, const void *value)
|
||||
{
|
||||
if (persistence_mutex != NULL)
|
||||
{
|
||||
if (xSemaphoreTake(persistence_mutex, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
esp_err_t err = ESP_ERR_INVALID_ARG;
|
||||
|
||||
switch (value_type)
|
||||
{
|
||||
case VALUE_TYPE_STRING:
|
||||
err = nvs_set_str(persistence_handle, key, (char *)value);
|
||||
break;
|
||||
|
||||
case VALUE_TYPE_INT32:
|
||||
err = nvs_set_i32(persistence_handle, key, *(int32_t *)value);
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "Unsupported value type");
|
||||
break;
|
||||
}
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_commit(persistence_handle));
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Error saving key %s: %s", key, esp_err_to_name(err));
|
||||
}
|
||||
|
||||
xSemaphoreGive(persistence_mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void *persistence_load(persistence_value_t value_type, const char *key, void *out)
|
||||
{
|
||||
if (persistence_mutex != NULL)
|
||||
{
|
||||
if (xSemaphoreTake(persistence_mutex, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
esp_err_t err = ESP_ERR_INVALID_ARG;
|
||||
|
||||
switch (value_type)
|
||||
{
|
||||
case VALUE_TYPE_STRING:
|
||||
err = nvs_get_str(persistence_handle, key, (char *)out, NULL);
|
||||
break;
|
||||
|
||||
case VALUE_TYPE_INT32:
|
||||
err = nvs_get_i32(persistence_handle, key, (int32_t *)out);
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "Unsupported value type");
|
||||
break;
|
||||
}
|
||||
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Error loading key %s: %s", key, esp_err_to_name(err));
|
||||
}
|
||||
|
||||
xSemaphoreGive(persistence_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void persistence_deinit()
|
||||
{
|
||||
if (persistence_mutex != NULL)
|
||||
{
|
||||
vSemaphoreDelete(persistence_mutex);
|
||||
persistence_mutex = NULL;
|
||||
}
|
||||
|
||||
nvs_close(persistence_handle);
|
||||
}
|
||||
*/
|
||||
}
|
Reference in New Issue
Block a user