initial refactoring

This commit is contained in:
Sebastien L
2023-12-04 23:25:57 -05:00
parent d03678ea81
commit c0ddf0a997
331 changed files with 29663 additions and 16553 deletions

View File

@@ -1,7 +1,5 @@
idf_component_register( SRC_DIRS .
idf_component_register(SRC_DIRS .
INCLUDE_DIRS .
PRIV_REQUIRES tools newlib console esp_common freertos tools
REQUIRES nvs_flash json
PRIV_REQUIRES tools newlib console esp_common freertos tools services
REQUIRES spiffs
)

View File

@@ -0,0 +1,480 @@
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include "Configurator.h"
#include "esp_log.h"
#include "esp_system.h"
#include "pb_common.h" // Nanopb header for encoding (serialization)
#include "pb_decode.h" // Nanopb header for decoding (deserialization)
#include "pb_encode.h" // Nanopb header for encoding (serialization)
// #include "sys_options.h"
#include "tools.h"
#include <string.h>
#include <algorithm>
static const char* TAG = "Configurator";
static const char* targets_folder = "targets";
static const char* config_file_name = "settings.bin";
static const char* state_file_name = "state.bin";
__attribute__((section(".ext_ram.bss"))) PlatformConfig::Configurator configurator;
sys_Config* platform = NULL;
sys_State* sys_state = NULL;
bool set_pb_string_from_mac(pb_ostream_t* stream, const pb_field_t* field, void* const* arg) {
if (!stream) {
// This is a size calculation pass, return true to indicate field presence
return true;
}
// Generate the string based on MAC and prefix
const char* prefix = reinterpret_cast<const char*>(*arg);
char* value = alloc_get_string_with_mac(prefix && strlen(prefix) > 0 ? prefix : "squeezelite-");
// Write the string to the stream
if (!pb_encode_string(stream, (uint8_t*)value, strlen(value))) {
free(value); // Free memory if encoding fails
return false;
}
free(value); // Free memory after encoding
return true;
}
namespace PlatformConfig {
EXT_RAM_ATTR static const int NO_COMMIT_PENDING = BIT0;
EXT_RAM_ATTR static const int LOAD_BIT = BIT1;
EXT_RAM_ATTR static const int NO_STATE_COMMIT_PENDING = BIT2;
const int Configurator::MaxDelay = 1000;
const int Configurator::LockMaxWait = 20 * Configurator::MaxDelay;
EXT_RAM_ATTR TimerHandle_t Configurator::_timer;
EXT_RAM_ATTR SemaphoreHandle_t Configurator::_mutex;
EXT_RAM_ATTR SemaphoreHandle_t Configurator::_state_mutex;
EXT_RAM_ATTR EventGroupHandle_t Configurator::_group;
static void ConfiguratorCallback(TimerHandle_t xTimer) {
static int cnt = 0, scnt = 0;
if (configurator.HasChanges()) {
ESP_LOGI(TAG, "Configuration has some uncommitted entries");
configurator.CommitChanges();
} else {
if (++cnt >= 15) {
ESP_LOGV(TAG, "commit timer: commit flag not set");
cnt = 0;
}
}
if (configurator.HasStateChanges()) {
ESP_LOGI(TAG, "State has some uncommitted changes");
configurator.CommitState();
} else {
if (++scnt >= 15) {
ESP_LOGV(TAG, "commit timer: commit flag not set");
cnt = 0;
}
}
xTimerReset(xTimer, 10);
}
void Configurator::RaiseStateModified() { SetGroupBit(NO_STATE_COMMIT_PENDING, false); }
void Configurator::RaiseModified() { SetGroupBit(NO_COMMIT_PENDING, false); }
void Configurator::ResetModified() {
ESP_LOGV(TAG, "Resetting the global commit flag.");
SetGroupBit(NO_COMMIT_PENDING, false);
}
void Configurator::ResetStateModified() {
ESP_LOGV(TAG, "Resetting the state commit flag.");
SetGroupBit(NO_STATE_COMMIT_PENDING, false);
}
bool Configurator::SetGroupBit(int bit_num, bool flag) {
bool result = true;
int curFlags = xEventGroupGetBits(_group);
if ((curFlags & LOAD_BIT) && bit_num == NO_COMMIT_PENDING) {
ESP_LOGD(TAG, "Loading config, ignoring changes");
result = false;
}
if (result) {
bool curBit = (xEventGroupGetBits(_group) & bit_num);
if (curBit == flag) {
ESP_LOGV(TAG, "Flag %d already %s", bit_num, flag ? "Set" : "Cleared");
result = false;
}
}
if (result) {
ESP_LOGV(TAG, "%s Flag %d ", flag ? "Setting" : "Clearing", bit_num);
if (!flag) {
xEventGroupClearBits(_group, bit_num);
} else {
xEventGroupSetBits(_group, bit_num);
}
}
return result;
}
bool Configurator::Lock() {
ESP_LOGV(TAG, "Locking Configurator");
if (xSemaphoreTake(_mutex, LockMaxWait) == pdTRUE) {
ESP_LOGV(TAG, "Configurator locked!");
return true;
} else {
ESP_LOGE(TAG, "Semaphore take failed. Unable to lock Configurator");
return false;
}
}
bool Configurator::LockState() {
ESP_LOGV(TAG, "Locking State");
if (xSemaphoreTake(_state_mutex, LockMaxWait) == pdTRUE) {
ESP_LOGV(TAG, "State locked!");
return true;
} else {
ESP_LOGE(TAG, "Semaphore take failed. Unable to lock State");
return false;
}
}
void* Configurator::AllocGetConfigBuffer(size_t* sz, sys_Config* config) {
size_t datasz;
pb_byte_t* data = NULL;
if (!pb_get_encoded_size(&datasz, sys_Config_fields, (const void*)platform) || datasz <= 0) {
return data;
}
data = (pb_byte_t*)malloc_init_external(datasz * sizeof(pb_byte_t));
pb_ostream_t stream = pb_ostream_from_buffer(data, datasz);
pb_encode(&stream, sys_Config_fields, (const void*)platform);
if (sz) {
*sz = datasz * sizeof(pb_byte_t);
}
return data;
}
void* Configurator::AllocGetConfigBuffer(size_t* sz) {
return AllocGetConfigBuffer(sz, &this->_root);
}
bool Configurator::WaitForCommit() {
bool commit_pending = (xEventGroupGetBits(_group) & NO_COMMIT_PENDING) == 0;
while (commit_pending) {
ESP_LOGW(TAG, "Waiting for config commit ...");
commit_pending = (xEventGroupWaitBits(_group, NO_COMMIT_PENDING | NO_STATE_COMMIT_PENDING, pdFALSE, pdTRUE,
(MaxDelay * 2) / portTICK_PERIOD_MS) &
( NO_COMMIT_PENDING | NO_STATE_COMMIT_PENDING)) == 0;
if (commit_pending) {
ESP_LOGW(TAG, "Timeout waiting for config commit.");
} else {
ESP_LOGI(TAG, "Config committed!");
}
}
return !commit_pending;
}
void Configurator::CommitChanges() {
esp_err_t err = ESP_OK;
ESP_LOGI(TAG, "Committing configuration to flash. Locking config object.");
if (!Lock()) {
ESP_LOGE(TAG, "Unable to lock config for commit ");
return;
}
ESP_LOGV(TAG, "Config Locked. Committing");
Commit(&_root);
ResetModified();
Unlock();
ESP_LOGI(TAG, "Done Committing configuration to flash.");
}
bool Configurator::CommitState() {
esp_err_t err = ESP_OK;
ESP_LOGI(TAG, "Committing configuration to flash. Locking config object.");
if (!LockState()) {
ESP_LOGE(TAG, "Unable to lock config for commit ");
return false;
}
ESP_LOGV(TAG, "Config Locked. Committing");
CommitState(&_sys_state);
ResetStateModified();
Unlock();
ESP_LOGI(TAG, "Done Committing configuration to flash.");
return true;
}
bool Configurator::HasChanges() { return (xEventGroupGetBits(_group) & NO_COMMIT_PENDING); }
bool Configurator::HasStateChanges() { return (xEventGroupGetBits(_group) & NO_STATE_COMMIT_PENDING); }
void Configurator::Unlock() {
ESP_LOGV(TAG, "Unlocking Configurator!");
xSemaphoreGive(_mutex);
}
void Configurator::UnlockState() {
ESP_LOGV(TAG, "Unlocking State!");
xSemaphoreGive(_state_mutex);
}
void Configurator::ResetStructure(sys_Config* config) {
if (!config) {
return;
}
sys_Config blankconfig = sys_Config_init_default;
memcpy(config, &blankconfig, sizeof(blankconfig));
}
bool Configurator::LoadDecodeBuffer(void* buffer, size_t buffer_size) {
size_t msgsize = 0;
size_t newsize = 0;
sys_Config config = sys_Config_init_default;
bool result = Configurator::LoadDecode(buffer, buffer_size, &config);
if (result) {
Configurator::ApplyTargetSettings(&config);
}
if (result) {
void* currentbuffer = AllocGetConfigBuffer(&msgsize);
void* newbuffer = AllocGetConfigBuffer(&newsize, &config);
if (msgsize != newsize || !memcmp(currentbuffer, newbuffer, msgsize)) {
ESP_LOGI(TAG, "Config change detected.");
// todo: here we are assuming that all strings and repeated elements have fixed size
// and therefore size should always be the same.
result = Configurator::LoadDecode(buffer, buffer_size, &this->_root);
RaiseModified();
}
free(currentbuffer);
free(newbuffer);
}
return result;
}
bool Configurator::LoadDecodeState() {
bool result = true;
sys_State blank_state = sys_State_init_default;
FILE* file = open_file("rb", state_file_name);
if (file == nullptr) {
ESP_LOGD(TAG,"No state file found. Initializing ");
pb_release(&sys_State_msg,(void *)&_sys_state);
memcpy(&_sys_state, &blank_state, sizeof(sys_State));
ESP_LOGD(TAG,"Done Initializing state");
return true;
}
ESP_LOGD(TAG, "Creating binding");
pb_istream_t filestream = {&in_file_binding,NULL,0};
ESP_LOGD(TAG, "Starting encode");
if (!pb_decode(&filestream, &sys_State_msg, (void*)&_sys_state)) {
ESP_LOGE(TAG, "Decoding failed: %s\n", PB_GET_ERROR(&filestream));
result = false;
}
fclose(file);
configurator_raise_state_changed();
ESP_LOGD(TAG, "State loaded");
return true;
}
bool Configurator::LoadDecode(
void* buffer, size_t buffer_size, sys_Config* conf_root, bool noinit) {
if (!conf_root || !buffer) {
ESP_LOGE(TAG, "Invalid arguments passed to Load");
}
bool result = true;
// Prepare to read the data into the 'config' structure
pb_istream_t stream = pb_istream_from_buffer((uint8_t*)buffer, buffer_size);
// Decode the Protocol Buffers message
if (noinit) {
ESP_LOGD(TAG, "Decoding WITHOUT initialization");
result = pb_decode_noinit(&stream, &sys_Config_msg, conf_root);
} else {
ESP_LOGD(TAG, "Decoding WITH initialization");
result = pb_decode(&stream, &sys_Config_msg, conf_root);
}
if (!result) {
ESP_LOGE(TAG, "Failed to decode settings: %s", PB_GET_ERROR(&stream));
return false;
}
ESP_LOGD(TAG, "Settings decoded");
return true;
}
bool Configurator::Commit(sys_Config* config) {
if (!config) {
ESP_LOGE(TAG, "Invalid configuration structure!");
return false;
}
FILE* file = open_file("wb", config_file_name);
bool result = true;
if (file == nullptr) {
return false;
}
ESP_LOGD(TAG, "Creating binding");
pb_ostream_t filestream = {&out_file_binding, file, SIZE_MAX, 0};
ESP_LOGD(TAG, "Starting encode");
if (!pb_encode(&filestream, sys_Config_fields, (void*)config)) {
ESP_LOGE(TAG, "Encoding failed: %s\n", PB_GET_ERROR(&filestream));
result = false;
}
ESP_LOGD(TAG, "Encoded size: %d", filestream.bytes_written);
if (filestream.bytes_written == 0) {
ESP_LOGE(TAG, "Empty configuration!");
ESP_LOGD(TAG, "Device name: %s", config->names.device);
}
fclose(file);
return result;
}
bool Configurator::CommitState(sys_State* state) {
if (!state) {
ESP_LOGE(TAG, "Invalid state structure!");
return false;
}
FILE* file = open_file("wb", state_file_name);
bool result = true;
if (file == nullptr) {
return false;
}
ESP_LOGD(TAG, "Creating binding for state commit");
pb_ostream_t filestream = {&out_file_binding, file, SIZE_MAX, 0};
ESP_LOGD(TAG, "Starting state encode");
if (!pb_encode(&filestream, sys_Config_fields, (void*)state)) {
ESP_LOGE(TAG, "Encoding failed: %s\n", PB_GET_ERROR(&filestream));
result = false;
}
ESP_LOGD(TAG, "Encoded size: %d", filestream.bytes_written);
if (filestream.bytes_written == 0) {
ESP_LOGE(TAG, "Empty state!");
}
fclose(file);
return result;
}
void Configurator::InitLoadConfig(const char* filename) {
return Configurator::InitLoadConfig(filename, &this->_root);
}
void Configurator::InitLoadConfig(const char* filename, sys_Config* conf_root, bool noinit) {
esp_err_t err = ESP_OK;
size_t data_length = 0;
bool result = false;
ESP_LOGI(TAG, "Loading settings from %s", filename);
void* data = load_file(&data_length, filename);
if (!data) {
ESP_LOGW(TAG, "Config file %s was empty. ", filename);
return;
} else {
result = LoadDecode(data, data_length, conf_root, noinit);
free(data);
}
if (ApplyTargetSettings(conf_root)) {
result = true;
}
if (result) {
_timer = xTimerCreate(
"configTimer", MaxDelay / portTICK_RATE_MS, pdFALSE, NULL, ConfiguratorCallback);
if (xTimerStart(_timer, MaxDelay / portTICK_RATE_MS) != pdPASS) {
ESP_LOGE(TAG, "config commitment timer failed to start.");
}
}
return;
}
bool Configurator::ApplyTargetSettings() { return ApplyTargetSettings(&this->_root); }
bool Configurator::ApplyTargetSettings(sys_Config* conf_root) {
size_t data_length = 0;
bool result = false;
std::string target_name = conf_root->target;
std::string target_file;
#ifdef CONFIG_FW_PLATFORM_NAME
if( target_name.empty()){
target_name = CONFIG_FW_PLATFORM_NAME;
}
#endif
target_file = target_name+ std::string(".bin");
std::transform(target_file.begin(), target_file.end(), target_file.begin(),
[](unsigned char c){ return std::tolower(c); });
if (target_file.empty() || !get_file_info(NULL, targets_folder, target_file.c_str())) {
ESP_LOGD(TAG, "Platform settings file not found: %s", target_file.c_str());
return result;
}
ESP_LOGI(TAG, "Applying target %s settings", target_name.c_str());
void* data = load_file(&data_length, targets_folder, target_file.c_str());
if (!data) {
ESP_LOGE(TAG, "File read fail");
return false;
} else {
result = LoadDecode(data, data_length, conf_root, true);
if (result) {
ESP_LOGI(TAG, "Target %s settings loaded", target_name.c_str());
}
free(data);
}
return result;
}
}; // namespace PlatformConfig
void configurator_reset_configuration() {
ESP_LOGI(TAG, "Creating default configuration file. ");
sys_Config config = sys_Config_init_default;
ESP_LOGD(TAG, "Device name before target settings: %s", config.names.device);
PlatformConfig::Configurator::ApplyTargetSettings(&config);
ESP_LOGD(TAG, "Device name after target settings: %s", config.names.device);
ESP_LOGD(TAG, "Committing new structure");
PlatformConfig::Configurator::Commit(&config);
}
void configurator_load() {
struct stat fileInformation;
ESP_LOGI(TAG, "Loading system settings file");
ESP_LOGD(TAG, "Checking if file %s exists", config_file_name);
bool found = get_file_info(&fileInformation, config_file_name);
if (!found || fileInformation.st_size == 0) {
ESP_LOGI(TAG, "Configuration file not found or is empty. ");
configurator_reset_configuration();
}
configurator.InitLoadConfig(config_file_name);
ESP_LOGD(TAG, "Assigning global config pointer");
platform = configurator.Root();
configurator.LoadDecodeState();
sys_state = configurator.RootState();
}
bool configurator_lock() { return configurator.Lock(); }
void configurator_unlock() { configurator.Unlock(); }
void configurator_raise_changed() { configurator.RaiseModified(); }
void configurator_raise_state_changed() { configurator.RaiseStateModified(); }
bool configurator_has_changes() { return configurator.HasChanges(); }
bool configurator_waitcommit() { return configurator.WaitForCommit(); }
void* configurator_alloc_get_config(size_t* sz) { return configurator.AllocGetConfigBuffer(sz); }
bool configurator_parse_config(void* buffer, size_t buffer_size) {
// Load and decode buffer. The method also applies any overlay if needed.
return configurator.LoadDecodeBuffer(buffer, buffer_size);
}
pb_type_t configurator_get_field_type(const pb_msgdesc_t* desc, uint32_t tag) {
pb_field_iter_t iter;
if (pb_field_iter_begin(&iter, desc, NULL) && pb_field_iter_find(&iter, tag)) {
/* Found our field. */
return iter.type;
}
return 0;
}
bool configurator_set_string(
const pb_msgdesc_t* desc, uint32_t field_tag, void* message, const char* value) {
pb_field_iter_t iter;
const char * newval = STR_OR_BLANK(value);
ESP_LOGD(TAG, "Setting value [%s] in message field tag %d",newval , field_tag);
if (pb_field_iter_begin(&iter, desc, message) && pb_field_iter_find(&iter, field_tag)) {
if (iter.pData && !strcmp((char*)iter.pData, newval)) {
ESP_LOGW(TAG, "No change, from and to values are the same: [%s]", STR_OR_BLANK(newval));
return false;
}
if (PB_ATYPE(iter.type) == PB_ATYPE_POINTER) {
ESP_LOGD(TAG, "Field is a pointer. Freeing previous value if any");
FREE_AND_NULL(iter.pData);
ESP_LOGD(TAG, "Field is a pointer. Setting new value ");
if(newval && strlen(newval)>0){
iter.pData = strdup_psram(newval);
}
} else if (PB_ATYPE(iter.type) == PB_ATYPE_STATIC) {
ESP_LOGD(TAG, "Static string. Setting new value");
memset(iter.pData,0x00,iter.data_size);
if(newval && strlen(newval)>0){
strncpy((char*)iter.pData, newval, iter.data_size);
}
}
ESP_LOGD(TAG, "Done setting value ");
}
return true;
}

View File

@@ -0,0 +1,107 @@
#pragma once
#include "State.pb.h"
#include "configuration.pb.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"
#include "status.pb.h"
#include <strings.h>
#include "accessors.h"
#ifdef __cplusplus
#include <cstdlib>
#include <string>
#include <vector>
extern "C" {
#endif
#define PLATFORM_GET_PTR(base, sname) \
{ \
(base && (base)->##has_##(sname) ? &(base)->sname : NULL)
#define PLATFORM_DEVICES PLATFORM_GET_PTR(platform)
void configurator_load();
bool configurator_waitcommit();
bool configurator_has_changes();
bool configurator_lock();
void configurator_unlock();
void configurator_raise_changed();
void configurator_raise_state_changed();
bool configurator_has_changes();
bool configurator_waitcommit();
void* configurator_alloc_get_config(size_t* sz);
bool configurator_parse_config(void* buffer, size_t buffer_size);
void configurator_reset_configuration();
pb_type_t configurator_get_field_type(const pb_msgdesc_t* desc, uint32_t tag);
bool configurator_set_string(
const pb_msgdesc_t* desc, uint32_t field_tag, void* message, const char* value);
extern sys_Config* platform;
extern sys_State* sys_state;
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
namespace PlatformConfig {
class Configurator {
private:
static const int MaxDelay;
static const int LockMaxWait;
EXT_RAM_ATTR static TimerHandle_t _timer;
EXT_RAM_ATTR static SemaphoreHandle_t _mutex;
EXT_RAM_ATTR static SemaphoreHandle_t _state_mutex;
EXT_RAM_ATTR static EventGroupHandle_t _group;
bool SetGroupBit(int bit_num, bool flag);
void ResetModified();
void ResetStateModified();
sys_Config _root;
sys_State _sys_state;
public:
sys_Config* Root() { return &_root; }
sys_State* RootState() { return &_sys_state; }
bool WaitForCommit();
bool Lock();
bool LockState();
void Unlock();
void UnlockState();
void* AllocGetConfigBuffer(size_t* sz);
static void* AllocGetConfigBuffer(size_t* sz, sys_Config* config);
static void ResetStructure(sys_Config* config);
bool LoadDecodeState();
void CommitChanges();
bool Commit();
static bool Commit(sys_Config* config);
bool CommitState();
static bool CommitState(sys_State* state);
sys_Config* AllocDefaultStruct();
static bool ApplyTargetSettings(sys_Config* conf_root);
bool ApplyTargetSettings();
bool HasStateChanges();
bool LoadDecodeBuffer(void* buffer, size_t buffer_size);
void InitLoadConfig(const char* filename);
void InitLoadConfig(const char* filename, sys_Config* conf_root, bool noinit = false);
static bool LoadDecode(
void* buffer, size_t buffer_size, sys_Config* conf_root, bool noinit = false);
Configurator() {
_mutex = xSemaphoreCreateMutex();
_state_mutex = xSemaphoreCreateMutex();
_group = xEventGroupCreate();
}
void RaiseStateModified();
void RaiseModified();
bool HasChanges();
~Configurator() {}
};
} // namespace PlatformConfig
extern PlatformConfig::Configurator configurator;
#endif

View File

@@ -1,11 +0,0 @@
#
# Component Makefile
#
# This Makefile should, at the very least, just include $(SDK_PATH)/Makefile. By default,
# this will take the sources in the src/ directory, compile them and link them into
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
# please read the SDK documents if you need to do this.
#
COMPONENT_ADD_INCLUDEDIRS := .

View File

@@ -1,391 +0,0 @@
#include "nvs_utilities.h"
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "nvs_utilities.h"
#include "platform_config.h"
#include "tools.h"
const char current_namespace[] = "config";
const char settings_partition[] = "settings";
static const char * TAG = "nvs_utilities";
typedef struct {
nvs_type_t type;
const char *str;
} type_str_pair_t;
static const type_str_pair_t type_str_pair[] = {
{ NVS_TYPE_I8, "i8" },
{ NVS_TYPE_U8, "u8" },
{ NVS_TYPE_U16, "u16" },
{ NVS_TYPE_I16, "i16" },
{ NVS_TYPE_U32, "u32" },
{ NVS_TYPE_I32, "i32" },
{ NVS_TYPE_U64, "u64" },
{ NVS_TYPE_I64, "i64" },
{ NVS_TYPE_STR, "str" },
{ NVS_TYPE_BLOB, "blob" },
{ NVS_TYPE_ANY, "any" },
};
static const size_t TYPE_STR_PAIR_SIZE = sizeof(type_str_pair) / sizeof(type_str_pair[0]);
void print_blob(const char *blob, size_t len)
{
for (int i = 0; i < len; i++) {
printf("%02x", blob[i]);
}
printf("\n");
}
nvs_type_t str_to_type(const char *type)
{
for (int i = 0; i < TYPE_STR_PAIR_SIZE; i++) {
const type_str_pair_t *p = &type_str_pair[i];
if (strcmp(type, p->str) == 0) {
return p->type;
}
}
return NVS_TYPE_ANY;
}
const char *type_to_str(nvs_type_t type)
{
for (int i = 0; i < TYPE_STR_PAIR_SIZE; i++) {
const type_str_pair_t *p = &type_str_pair[i];
if (p->type == type) {
return p->str;
}
}
return "Unknown";
}
void erase_settings_partition(){
ESP_LOGW(TAG, "Erasing nvs on partition %s",settings_partition);
ESP_ERROR_CHECK(nvs_flash_erase_partition(settings_partition));
nvs_flash_init_partition(settings_partition);
}
void initialize_nvs() {
ESP_LOGI(TAG, "Initializing flash nvs ");
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGW(TAG, "%s. Erasing nvs flash", esp_err_to_name(err));
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
if(err != ESP_OK){
ESP_LOGE(TAG, "nvs_flash_init failed. %s.", esp_err_to_name(err));
}
ESP_ERROR_CHECK(err);
ESP_LOGI(TAG, "Initializing nvs partition %s",settings_partition);
err = nvs_flash_init_partition(settings_partition);
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGW(TAG, "%s. Erasing nvs on partition %s",esp_err_to_name(err),settings_partition);
ESP_ERROR_CHECK(nvs_flash_erase_partition(settings_partition));
err = nvs_flash_init_partition(settings_partition);
}
if(err!=ESP_OK){
ESP_LOGE(TAG, "nvs_flash_init_partition failed. %s",esp_err_to_name(err));
}
ESP_ERROR_CHECK(err);
ESP_LOGD(TAG, "nvs init completed");
}
esp_err_t nvs_load_config() {
nvs_entry_info_t info;
esp_err_t err = ESP_OK;
size_t malloc_int = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
size_t malloc_spiram = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
nvs_iterator_t it = nvs_entry_find(settings_partition, NULL, NVS_TYPE_ANY);
if (it == NULL) {
ESP_LOGW(TAG, "empty nvs partition %s, namespace %s", settings_partition, current_namespace);
}
while (it != NULL) {
nvs_entry_info(it, &info);
if (strstr(info.namespace_name, current_namespace)) {
if (strlen(info.key) == 0) {
ESP_LOGW(TAG, "empty key name in namespace %s. Removing it.", current_namespace);
nvs_handle_t nvs_handle;
err = nvs_open(settings_partition, NVS_READWRITE, &nvs_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "nvs_open failed. %s", esp_err_to_name(err));
} else {
if ((err = nvs_erase_key(nvs_handle, info.key)) != ESP_OK) {
ESP_LOGE(TAG, "nvs_erase_key failed. %s", esp_err_to_name(err));
} else {
nvs_commit(nvs_handle);
}
nvs_close(nvs_handle);
if (err == ESP_OK) {
ESP_LOGW(TAG, "nvs_erase_key completed on empty key. Restarting system to apply changes.");
esp_restart();
}
}
if (err != ESP_OK) {
ESP_LOGW(TAG, "nvs_erase_key failed on empty key. Configuration partition should be erased. %s", esp_err_to_name(err));
err = ESP_OK;
}
}
else {
void* value = get_nvs_value_alloc(info.type, info.key);
if (value == NULL) {
ESP_LOGE(TAG, "nvs read failed.");
return ESP_FAIL;
}
config_set_value(info.type, info.key, value);
free(value);
}
}
it = nvs_entry_next(it);
}
char* json_string = config_alloc_get_json(false);
if (json_string != NULL) {
ESP_LOGD(TAG, "config json : %s\n", json_string);
free(json_string);
}
ESP_LOGW(TAG, "Configuration memory usage. Heap internal:%zu (min:%zu) (used:%zu) external:%zu (min:%zu) (used:%zd)",
heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
malloc_int - heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
malloc_spiram - heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
return err;
}
esp_err_t store_nvs_value(nvs_type_t type, const char *key, void * data) {
if (type == NVS_TYPE_BLOB)
return ESP_ERR_NVS_TYPE_MISMATCH;
return store_nvs_value_len(type, key, data,0);
}
esp_err_t store_nvs_value_len_for_partition(const char * partition,const char * namespace,nvs_type_t type, const char *key, const void * data,size_t data_len) {
esp_err_t err;
nvs_handle nvs;
if(!key || key[0]=='\0'){
ESP_LOGE(TAG, "Cannot store value to nvs: key is empty");
return ESP_ERR_INVALID_ARG;
}
if (type == NVS_TYPE_ANY) {
return ESP_ERR_NVS_TYPE_MISMATCH;
}
err = nvs_open_from_partition(partition, namespace, NVS_READWRITE, &nvs);
if (err != ESP_OK) {
return err;
}
if (type == NVS_TYPE_I8) {
err = nvs_set_i8(nvs, key, *(int8_t *) data);
} else if (type == NVS_TYPE_U8) {
err = nvs_set_u8(nvs, key, *(uint8_t *) data);
} else if (type == NVS_TYPE_I16) {
err = nvs_set_i16(nvs, key, *(int16_t *) data);
} else if (type == NVS_TYPE_U16) {
err = nvs_set_u16(nvs, key, *(uint16_t *) data);
} else if (type == NVS_TYPE_I32) {
err = nvs_set_i32(nvs, key, *(int32_t *) data);
} else if (type == NVS_TYPE_U32) {
err = nvs_set_u32(nvs, key, *(uint32_t *) data);
} else if (type == NVS_TYPE_I64) {
err = nvs_set_i64(nvs, key, *(int64_t *) data);
} else if (type == NVS_TYPE_U64) {
err = nvs_set_u64(nvs, key, *(uint64_t *) data);
} else if (type == NVS_TYPE_STR) {
err = nvs_set_str(nvs, key, data);
} else if (type == NVS_TYPE_BLOB) {
err = nvs_set_blob(nvs, key, (void *) data, data_len);
}
if (err == ESP_OK) {
err = nvs_commit(nvs);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Value stored under key '%s'", key);
}
}
nvs_close(nvs);
return err;
}
esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data,
size_t data_len) {
return store_nvs_value_len_for_partition(settings_partition,current_namespace,type,key,data,data_len);
}
void * get_nvs_value_alloc_for_partition(const char * partition,const char * namespace,nvs_type_t type, const char *key, size_t * size){
nvs_handle nvs;
esp_err_t err;
void * value=NULL;
if(size){
*size=0;
}
err = nvs_open_from_partition(partition, namespace, NVS_READONLY, &nvs);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Could not open the nvs storage.");
return NULL;
}
if (type == NVS_TYPE_I8) {
value=malloc_init_external(sizeof(int8_t));
err = nvs_get_i8(nvs, key, (int8_t *) value);
} else if (type == NVS_TYPE_U8) {
value=malloc_init_external(sizeof(uint8_t));
err = nvs_get_u8(nvs, key, (uint8_t *) value);
} else if (type == NVS_TYPE_I16) {
value=malloc_init_external(sizeof(int16_t));
err = nvs_get_i16(nvs, key, (int16_t *) value);
} else if (type == NVS_TYPE_U16) {
value=malloc_init_external(sizeof(uint16_t));
err = nvs_get_u16(nvs, key, (uint16_t *) value);
} else if (type == NVS_TYPE_I32) {
value=malloc_init_external(sizeof(int32_t));
err = nvs_get_i32(nvs, key, (int32_t *) value);
} else if (type == NVS_TYPE_U32) {
value=malloc_init_external(sizeof(uint32_t));
err = nvs_get_u32(nvs, key, (uint32_t *) value);
} else if (type == NVS_TYPE_I64) {
value=malloc_init_external(sizeof(int64_t));
err = nvs_get_i64(nvs, key, (int64_t *) value);
} else if (type == NVS_TYPE_U64) {
value=malloc_init_external(sizeof(uint64_t));
err = nvs_get_u64(nvs, key, (uint64_t *) value);
} else if (type == NVS_TYPE_STR) {
size_t len=0;
err = nvs_get_str(nvs, key, NULL, &len);
if (err == ESP_OK) {
value=malloc_init_external(len+1);
err = nvs_get_str(nvs, key, value, &len);
if(size){
*size=len;
}
}
} else if (type == NVS_TYPE_BLOB) {
size_t len;
err = nvs_get_blob(nvs, key, NULL, &len);
if (err == ESP_OK) {
value=malloc_init_external(len+1);
if(size){
*size=len;
}
err = nvs_get_blob(nvs, key, value, &len);
}
}
if(err!=ESP_OK){
ESP_LOGD(TAG, "Value not found for key %s",key);
if(value!=NULL)
free(value);
value=NULL;
}
nvs_close(nvs);
return value;
}
void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
return get_nvs_value_alloc_for_partition(settings_partition, current_namespace,type,key,NULL);
}
esp_err_t get_nvs_value(nvs_type_t type, const char *key, void*value, const uint8_t buf_size) {
nvs_handle nvs;
esp_err_t err;
err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READONLY, &nvs);
if (err != ESP_OK) {
return err;
}
if (type == NVS_TYPE_I8) {
err = nvs_get_i8(nvs, key, (int8_t *) value);
} else if (type == NVS_TYPE_U8) {
err = nvs_get_u8(nvs, key, (uint8_t *) value);
} else if (type == NVS_TYPE_I16) {
err = nvs_get_i16(nvs, key, (int16_t *) value);
} else if (type == NVS_TYPE_U16) {
err = nvs_get_u16(nvs, key, (uint16_t *) value);
} else if (type == NVS_TYPE_I32) {
err = nvs_get_i32(nvs, key, (int32_t *) value);
} else if (type == NVS_TYPE_U32) {
err = nvs_get_u32(nvs, key, (uint32_t *) value);
} else if (type == NVS_TYPE_I64) {
err = nvs_get_i64(nvs, key, (int64_t *) value);
} else if (type == NVS_TYPE_U64) {
err = nvs_get_u64(nvs, key, (uint64_t *) value);
} else if (type == NVS_TYPE_STR) {
size_t len;
if ((err = nvs_get_str(nvs, key, NULL, &len)) == ESP_OK) {
if (len > buf_size) {
//ESP_LOGE("Error reading value for %s. Buffer size: %d, Value Length: %d", key, buf_size, len);
err = ESP_FAIL;
} else {
err = nvs_get_str(nvs, key, value, &len);
}
}
} else if (type == NVS_TYPE_BLOB) {
size_t len;
if ((err = nvs_get_blob(nvs, key, NULL, &len)) == ESP_OK) {
if (len > buf_size) {
//ESP_LOGE("Error reading value for %s. Buffer size: %d, Value Length: %d",
// key, buf_size, len);
err = ESP_FAIL;
} else {
err = nvs_get_blob(nvs, key, value, &len);
}
}
}
nvs_close(nvs);
return err;
}
esp_err_t erase_nvs_for_partition(const char * partition, const char * namespace,const char *key)
{
nvs_handle nvs;
esp_err_t err = nvs_open_from_partition(partition,namespace, NVS_READWRITE, &nvs);
if (err == ESP_OK) {
err = nvs_erase_key(nvs, key);
if (err == ESP_OK) {
err = nvs_commit(nvs);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Value with key '%s' erased", key);
}
}
nvs_close(nvs);
}
else {
ESP_LOGE(TAG,"Could not erase key %s from partition %s namespace %s : %s", key,partition,namespace, esp_err_to_name(err));
}
return err;
}
esp_err_t erase_nvs(const char *key)
{
return erase_nvs_for_partition(NVS_DEFAULT_PART_NAME, current_namespace,key);
}
esp_err_t erase_nvs_partition(const char * partition, const char * namespace){
nvs_handle nvs;
const char * step = "Opening";
ESP_LOGD(TAG,"%s partition %s, namespace %s ",step,partition,namespace);
esp_err_t err = nvs_open_from_partition(partition,namespace, NVS_READWRITE, &nvs);
if (err == ESP_OK) {
step = "Erasing";
ESP_LOGD(TAG,"%s namespace %s ",step,partition);
err = nvs_erase_all(nvs);
if (err == ESP_OK) {
step = "Committing";
ESP_LOGD(TAG,"%s",step);
err = nvs_commit(nvs);
}
}
if(err !=ESP_OK){
ESP_LOGE(TAG,"%s partition %s, name space %s : %s",step,partition,namespace,esp_err_to_name(err));
}
ESP_LOGD(TAG,"Closing %s ",namespace);
nvs_close(nvs);
return err;
}

View File

@@ -1,27 +0,0 @@
#pragma once
#include "esp_err.h"
#include "nvs.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const char current_namespace[];
extern const char settings_partition[];
#define NUM_BUFFER_LEN 101
void initialize_nvs();
esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data, size_t data_len);
esp_err_t store_nvs_value(nvs_type_t type, const char *key, void * data);
esp_err_t get_nvs_value(nvs_type_t type, const char *key, void*value, const uint8_t buf_size);
void * get_nvs_value_alloc(nvs_type_t type, const char *key);
void * get_nvs_value_alloc_for_partition(const char * partition,const char * name_space,nvs_type_t type, const char *key, size_t * size);
esp_err_t erase_nvs_for_partition(const char * partition, const char * name_space,const char *key);
esp_err_t store_nvs_value_len_for_partition(const char * partition,const char * name_space,nvs_type_t type, const char *key, const void * data,size_t data_len);
esp_err_t erase_nvs(const char *key);
void print_blob(const char *blob, size_t len);
const char *type_to_str(nvs_type_t type);
nvs_type_t str_to_type(const char *type);
esp_err_t erase_nvs_partition(const char * partition, const char * name_space);
void erase_settings_partition();
#ifdef __cplusplus
}
#endif

View File

@@ -1,807 +0,0 @@
/*
* Squeezelite for esp32
*
* (c) Sebastien 2019
* Philippe G. 2019, philippe_44@outlook.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "platform_config.h"
#include "nvs_utilities.h"
#include "platform_esp32.h"
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "nvs_utilities.h"
#include "cJSON.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include "tools.h"
#include "trace.h"
#define CONFIG_COMMIT_DELAY 1000
#define LOCK_MAX_WAIT 20*CONFIG_COMMIT_DELAY
static const char * TAG = "config";
EXT_RAM_ATTR static cJSON * nvs_json=NULL;
EXT_RAM_ATTR static TimerHandle_t timer;
EXT_RAM_ATTR static SemaphoreHandle_t config_mutex = NULL;
EXT_RAM_ATTR static EventGroupHandle_t config_group;
/* @brief indicate that the ESP32 is currently connected. */
EXT_RAM_ATTR static const int CONFIG_NO_COMMIT_PENDING = BIT0;
EXT_RAM_ATTR static const int CONFIG_LOAD_BIT = BIT1;
bool config_lock(TickType_t xTicksToWait);
void config_unlock();
extern esp_err_t nvs_load_config();
void config_raise_change(bool flag);
cJSON_bool config_is_entry_changed(cJSON * entry);
bool config_set_group_bit(int bit_num,bool flag);
cJSON * config_set_value_safe(nvs_type_t nvs_type, const char *key,const void * value);
static void vCallbackFunction( TimerHandle_t xTimer );
void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag);
#define IMPLEMENT_SET_DEFAULT(t,nt) void config_set_default_## t (const char *key, t value){\
void * pval = malloc_init_external(sizeof(value));\
*((t *) pval) = value;\
config_set_default(nt, key,pval,0);\
free(pval); }
#define IMPLEMENT_GET_NUM(t,nt) esp_err_t config_get_## t (const char *key, t * value){\
void * pval = config_alloc_get(nt, key);\
if(pval!=NULL){ *value = *(t * )pval; free(pval); return ESP_OK; }\
return ESP_FAIL;}
static void * malloc_fn(size_t sz){
void * ptr = is_recovery_running?malloc(sz):heap_caps_malloc(sz, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if(ptr==NULL){
ESP_LOGE(TAG,"malloc_fn: unable to allocate memory!");
}
return ptr;
}
void init_cJSON(){
static cJSON_Hooks hooks;
hooks.malloc_fn=&malloc_fn;
cJSON_InitHooks(&hooks);
}
void config_init(){
ESP_LOGD(TAG, "Creating mutex for Config");
MEMTRACE_PRINT_DELTA();
config_mutex = xSemaphoreCreateMutex();
MEMTRACE_PRINT_DELTA();
ESP_LOGD(TAG, "Creating event group");
MEMTRACE_PRINT_DELTA();
config_group = xEventGroupCreate();
MEMTRACE_PRINT_DELTA();
ESP_LOGD(TAG, "Loading config from nvs");
init_cJSON();
MEMTRACE_PRINT_DELTA();
if(nvs_json !=NULL){
cJSON_Delete(nvs_json);
}
nvs_json = cJSON_CreateObject();
config_set_group_bit(CONFIG_LOAD_BIT,true);
MEMTRACE_PRINT_DELTA();
nvs_load_config();
MEMTRACE_PRINT_DELTA();
config_set_group_bit(CONFIG_LOAD_BIT,false);
MEMTRACE_PRINT_DELTA();
config_start_timer();
}
void config_start_timer(){
ESP_LOGD(TAG, "Starting config timer");
timer = xTimerCreate("configTimer", CONFIG_COMMIT_DELAY / portTICK_RATE_MS, pdFALSE, NULL, vCallbackFunction);
if( xTimerStart( timer , CONFIG_COMMIT_DELAY/ portTICK_RATE_MS ) != pdPASS ) {
ESP_LOGE(TAG, "config commitment timer failed to start.");
}
}
nvs_type_t config_get_item_type(cJSON * entry){
if(entry==NULL){
ESP_LOGE(TAG,"null pointer received!");
return 0;
}
cJSON * item_type = cJSON_GetObjectItemCaseSensitive(entry, "type");
if(item_type ==NULL ) {
ESP_LOGE(TAG, "Item type not found! ");
return 0;
}
ESP_LOGD(TAG,"Found item type %f",item_type->valuedouble);
return item_type->valuedouble;
}
cJSON * config_set_value_safe(nvs_type_t nvs_type, const char *key, const void * value){
cJSON * entry = cJSON_CreateObject();
double numvalue = 0;
if(entry == NULL) {
ESP_LOGE(TAG, "Unable to allocate memory for entry %s",key);
return NULL;
}
cJSON * existing = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
if(existing !=NULL && nvs_type == NVS_TYPE_STR && config_get_item_type(existing) != NVS_TYPE_STR ) {
ESP_LOGW(TAG, "Storing numeric value from string");
numvalue = atof((char *)value);
cJSON_AddNumberToObject(entry,"value", numvalue );
nvs_type_t exist_type = config_get_item_type(existing);
ESP_LOGW(TAG, "Stored value %f from string %s as type %d",numvalue, (char *)value,exist_type);
cJSON_AddNumberToObject(entry,"type", exist_type);
}
else {
cJSON_AddNumberToObject(entry,"type", nvs_type );
switch (nvs_type) {
case NVS_TYPE_I8:
cJSON_AddNumberToObject(entry,"value", *(int8_t*)value );
break;
case NVS_TYPE_I16:
cJSON_AddNumberToObject(entry,"value", *(int16_t*)value );
break;
case NVS_TYPE_I32:
cJSON_AddNumberToObject(entry,"value", *(int32_t*)value );
break;
case NVS_TYPE_U8:
cJSON_AddNumberToObject(entry,"value", *(uint8_t*)value );
break;
case NVS_TYPE_U16:
cJSON_AddNumberToObject(entry,"value", *(uint16_t*)value );
break;
case NVS_TYPE_U32:
cJSON_AddNumberToObject(entry,"value", *(uint32_t*)value );
break;
case NVS_TYPE_STR:
cJSON_AddStringToObject(entry, "value", (char *)value);
break;
case NVS_TYPE_I64:
case NVS_TYPE_U64:
default:
ESP_LOGE(TAG, "nvs type %u not supported", nvs_type);
break;
}
}
if(existing!=NULL ) {
ESP_LOGV(TAG, "Changing existing entry [%s].", key);
char * exist_str = cJSON_PrintUnformatted(existing);
if(exist_str!=NULL){
ESP_LOGV(TAG,"Existing entry: %s", exist_str);
free(exist_str);
}
else {
ESP_LOGV(TAG,"Failed to print existing entry");
}
// set commit flag as equal so we can compare
cJSON_AddBoolToObject(entry,"chg",config_is_entry_changed(existing));
if(!cJSON_Compare(entry,existing,false)){
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGD(TAG,"New config object: \n%s", entry_str );
free(entry_str);
}
else {
ESP_LOGD(TAG,"Failed to print entry");
}
ESP_LOGI(TAG, "Setting changed flag config [%s]", key);
config_set_entry_changed_flag(entry,true);
ESP_LOGI(TAG, "Updating config [%s]", key);
cJSON_ReplaceItemInObject(nvs_json,key, entry);
entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGD(TAG,"New config: %s", entry_str );
free(entry_str);
}
else {
ESP_LOGD(TAG,"Failed to print entry");
}
}
else {
ESP_LOGD(TAG, "Config not changed. ");
cJSON_Delete(entry);
entry = existing;
}
}
else {
// This is a new entry.
config_set_entry_changed_flag(entry,true);
cJSON_AddItemToObject(nvs_json, key, entry);
}
return entry;
}
nvs_type_t config_get_entry_type(cJSON * entry){
if(entry==NULL){
ESP_LOGE(TAG,"null pointer received!");
return 0;
}
cJSON * entry_type = cJSON_GetObjectItemCaseSensitive(entry, "type");
if(entry_type ==NULL ) {
ESP_LOGE(TAG, "Entry type not found in nvs cache for existing setting.");
return 0;
}
ESP_LOGV(TAG,"Found type %s",type_to_str(entry_type->valuedouble));
return entry_type->valuedouble;
}
void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag){
ESP_LOGV(TAG, "config_set_entry_changed_flag: begin");
if(entry==NULL){
ESP_LOGE(TAG,"null pointer received!");
return;
}
bool bIsConfigLoading=((xEventGroupGetBits(config_group) & CONFIG_LOAD_BIT)!=0);
bool changedFlag=bIsConfigLoading?false:flag;
ESP_LOGV(TAG, "config_set_entry_changed_flag: retrieving chg flag from entry");
cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
if(changed ==NULL ) {
ESP_LOGV(TAG, "config_set_entry_changed_flag: chg flag not found. Adding. ");
cJSON_AddBoolToObject(entry,"chg",changedFlag);
}
else {
ESP_LOGV(TAG, "config_set_entry_changed_flag: Existing change flag found. ");
if(cJSON_IsTrue(changed) && changedFlag){
ESP_LOGW(TAG, "Commit flag not changed!");
}
else{
ESP_LOGV(TAG, "config_set_entry_changed_flag: Updating change flag to %s",changedFlag?"TRUE":"FALSE");
changed->type = changedFlag?cJSON_True:cJSON_False ;
}
}
if(changedFlag) {
ESP_LOGV(TAG, "config_set_entry_changed_flag: Calling config_raise_change. ");
config_raise_change(true);
}
ESP_LOGV(TAG, "config_set_entry_changed_flag: done. ");
}
cJSON_bool config_is_entry_changed(cJSON * entry){
if(entry==NULL){
ESP_LOGE(TAG,"null pointer received!");
return true;
}
cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
if(changed ==NULL ) {
ESP_LOGE(TAG, "Change flag not found! ");
return true;
}
return cJSON_IsTrue(changed);
}
void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry){
void * value=NULL;
if(entry==NULL){
ESP_LOGE(TAG,"null pointer received!");
}
ESP_LOGV(TAG, "getting config value type %s", type_to_str(nvs_type));
cJSON * entry_value = cJSON_GetObjectItemCaseSensitive(entry, "value");
if(entry_value==NULL ) {
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGE(TAG, "Missing config value!. Object: \n%s", entry_str);
free(entry_str);
}
else{
ESP_LOGE(TAG, "Missing config value");
}
return NULL;
}
nvs_type_t type = config_get_entry_type(entry);
if(nvs_type != type){
// requested value type different than the stored type
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGE(TAG, "Requested value type %s, found value type %s instead, Object: \n%s", type_to_str(nvs_type), type_to_str(type),entry_str);
free(entry_str);
}
else{
ESP_LOGE(TAG, "Requested value type %s, found value type %s instead", type_to_str(nvs_type), type_to_str(type));
}
return NULL;
}
if (nvs_type == NVS_TYPE_I8) {
value=malloc_init_external(sizeof(int8_t));
*(int8_t *)value = (int8_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_U8) {
value=malloc_init_external(sizeof(uint8_t));
*(uint8_t *)value = (uint8_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_I16) {
value=malloc_init_external(sizeof(int16_t));
*(int16_t *)value = (int16_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_U16) {
value=malloc_init_external(sizeof(uint16_t));
*(uint16_t *)value = (uint16_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_I32) {
value=malloc_init_external(sizeof(int32_t));
*(int32_t *)value = (int32_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_U32) {
value=malloc_init_external(sizeof(uint32_t));
*(uint32_t *)value = (uint32_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_I64) {
value=malloc_init_external(sizeof(int64_t));
*(int64_t *)value = (int64_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_U64) {
value=malloc_init_external(sizeof(uint64_t));
*(uint64_t *)value = (uint64_t)entry_value->valuedouble;
} else if (nvs_type == NVS_TYPE_STR) {
if(!cJSON_IsString(entry_value)){
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d, Object: \n%s",
str_or_null(entry_value->string),
str_or_null(entry_value->valuestring),
entry_value->type,
str_or_null(entry_str));
free(entry_str);
}
else {
ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d",
str_or_null(entry_value->string),
str_or_null(entry_value->valuestring),
entry_value->type);
}
}
else {
size_t len=strlen(cJSON_GetStringValue(entry_value));
value=(void *)malloc_init_external(len+1);
memcpy(value,cJSON_GetStringValue(entry_value),len);
if(value==NULL){
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGE(TAG, "strdup failed on value for object \n%s",entry_str);
free(entry_str);
}
else {
ESP_LOGE(TAG, "strdup failed on value");
}
}
}
} else if (nvs_type == NVS_TYPE_BLOB) {
ESP_LOGE(TAG, "Unsupported type NVS_TYPE_BLOB");
}
return value;
}
void config_commit_to_nvs(){
ESP_LOGI(TAG,"Committing configuration to nvs. Locking config object.");
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
ESP_LOGE(TAG, "config_commit_to_nvs: Unable to lock config for commit ");
return ;
}
if(nvs_json==NULL){
ESP_LOGE(TAG, ": cJSON nvs cache object not set.");
return;
}
ESP_LOGV(TAG,"config_commit_to_nvs. Config Locked!");
cJSON * entry=nvs_json->child;
while(entry!= NULL){
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGV(TAG,"config_commit_to_nvs processing item %s",entry_str);
free(entry_str);
}
if(config_is_entry_changed(entry)){
ESP_LOGD(TAG, "Committing entry %s value to nvs.",(entry->string==NULL)?"UNKNOWN":entry->string);
nvs_type_t type = config_get_entry_type(entry);
void * value = config_safe_alloc_get_entry_value(type, entry);
if(value!=NULL){
size_t len=strlen(entry->string);
char * key=(void *)malloc_init_external(len+1);
memcpy(key,entry->string,len);
esp_err_t err = store_nvs_value(type,key,value);
FREE_AND_NULL(key);
FREE_AND_NULL(value);
if(err!=ESP_OK){
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGE(TAG, "Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
free(entry_str);
}
else {
ESP_LOGE(TAG, "Error comitting value to nvs for key %s",entry->string);
}
}
else {
config_set_entry_changed_flag(entry, false);
}
}
else {
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
free(entry_str);
}
else {
ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s",entry->string);
}
}
}
else {
ESP_LOGV(TAG,"config_commit_to_nvs. Item already committed. Ignoring.");
}
taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
entry = entry->next;
}
ESP_LOGV(TAG,"config_commit_to_nvs. Resetting the global commit flag.");
config_raise_change(false);
ESP_LOGV(TAG,"config_commit_to_nvs. Releasing the lock object.");
config_unlock();
ESP_LOGI(TAG,"Done Committing configuration to nvs.");
}
bool config_has_changes(){
return (xEventGroupGetBits(config_group) & CONFIG_NO_COMMIT_PENDING)==0;
}
bool wait_for_commit(){
bool commit_pending=(xEventGroupGetBits(config_group) & CONFIG_NO_COMMIT_PENDING)==0;
while (commit_pending){
ESP_LOGW(TAG,"Waiting for config commit ...");
commit_pending = (xEventGroupWaitBits(config_group, CONFIG_NO_COMMIT_PENDING,pdFALSE, pdTRUE, (CONFIG_COMMIT_DELAY*2) / portTICK_PERIOD_MS) & CONFIG_NO_COMMIT_PENDING)==0;
if(commit_pending){
ESP_LOGW(TAG,"Timeout waiting for config commit.");
}
else {
ESP_LOGI(TAG,"Config committed!");
}
}
return !commit_pending;
}
bool config_lock(TickType_t xTicksToWait) {
ESP_LOGV(TAG, "Locking config json object");
if( xSemaphoreTake( config_mutex, xTicksToWait ) == pdTRUE ) {
ESP_LOGV(TAG, "config Json object locked!");
return true;
}
else {
ESP_LOGE(TAG, "Semaphore take failed. Unable to lock config Json object mutex");
return false;
}
}
void config_unlock() {
ESP_LOGV(TAG, "Unlocking json buffer!");
xSemaphoreGive( config_mutex );
}
static void vCallbackFunction( TimerHandle_t xTimer ) {
static int cnt=0;
if(config_has_changes()){
ESP_LOGI(TAG, "configuration has some uncommitted entries");
config_commit_to_nvs();
}
else{
if(++cnt>=15){
ESP_LOGV(TAG,"commit timer: commit flag not set");
cnt=0;
}
}
xTimerReset( xTimer, 10 );
}
void config_raise_change(bool change_found){
if(config_set_group_bit(CONFIG_NO_COMMIT_PENDING,!change_found))
{
ESP_LOGD(TAG,"Config commit set to %s",change_found?"Pending Commit":"Committed");
}
}
bool config_set_group_bit(int bit_num,bool flag){
bool result = true;
int curFlags=xEventGroupGetBits(config_group);
if((curFlags & CONFIG_LOAD_BIT) && bit_num == CONFIG_NO_COMMIT_PENDING ){
ESP_LOGD(TAG,"Loading config, ignoring changes");
result = false;
}
if(result){
bool curBit=(xEventGroupGetBits(config_group) & bit_num);
if(curBit == flag){
ESP_LOGV(TAG,"Flag %d already %s", bit_num, flag?"Set":"Cleared");
result = false;
}
}
if(result){
ESP_LOGV(TAG,"%s Flag %d ", flag?"Setting":"Clearing",bit_num);
if(!flag){
xEventGroupClearBits(config_group, bit_num);
}
else {
xEventGroupSetBits(config_group, bit_num);
}
}
return result;
}
void config_set_default(nvs_type_t type, const char *key, const void * default_value, size_t blob_size) {
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
ESP_LOGE(TAG, "Unable to lock config");
return;
}
ESP_LOGV(TAG, "Checking if key %s exists in nvs cache for type %s.", key,type_to_str(type));
cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
if(entry !=NULL){
ESP_LOGV(TAG, "Entry found.");
}
else {
// Value was not found
ESP_LOGW(TAG, "Adding default value for [%s].", key);
entry=config_set_value_safe(type, key, default_value);
if(entry == NULL){
ESP_LOGE(TAG, "Failed to add value to cache!");
}
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGD(TAG, "Value added to default for object: \n%s",entry_str);
free(entry_str);
}
}
config_unlock();
}
void config_delete_key(const char *key){
nvs_handle nvs;
ESP_LOGD(TAG, "Deleting nvs entry for [%s]", key);
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
ESP_LOGE(TAG, "Unable to lock config for delete");
return ;
}
esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
if (err == ESP_OK) {
err = nvs_erase_key(nvs, key);
if (err == ESP_OK) {
ESP_LOGD(TAG, "key [%s] erased from nvs.",key);
err = nvs_commit(nvs);
if (err == ESP_OK) {
ESP_LOGD(TAG, "nvs erase committed.");
}
else {
ESP_LOGE(TAG, "Unable to commit nvs erase operation for key [%s]. %s.",key,esp_err_to_name(err));
}
}
else {
ESP_LOGE(TAG, "Unable to delete nvs key [%s]. %s. ",key, esp_err_to_name(err));
}
nvs_close(nvs);
}
else {
ESP_LOGE(TAG, "Error opening nvs: %s. Unable to delete nvs key [%s].",esp_err_to_name(err),key);
}
char * struc_str = cJSON_PrintUnformatted(nvs_json);
if(struc_str!=NULL){
ESP_LOGV(TAG, "Structure before delete \n%s", struc_str);
free(struc_str);
}
cJSON * entry = cJSON_DetachItemFromObjectCaseSensitive(nvs_json, key);
if(entry !=NULL){
ESP_LOGI(TAG, "Removing config key [%s]", entry->string);
cJSON_Delete(entry);
struc_str = cJSON_PrintUnformatted(nvs_json);
if(struc_str!=NULL){
ESP_LOGV(TAG, "Structure after delete \n%s", struc_str);
free(struc_str);
}
}
else {
ESP_LOGW(TAG, "Unable to remove config key [%s]: not found.", key);
}
config_unlock();
}
void * config_alloc_get(nvs_type_t nvs_type, const char *key) {
return config_alloc_get_default(nvs_type, key, NULL, 0);
}
cJSON * config_alloc_get_cjson(const char *key){
char * conf_str = config_alloc_get_default(NVS_TYPE_STR, key, NULL, 0);
if(conf_str==NULL){
ESP_LOGE(TAG, "Unable to get config value for key [%s]", key);
return NULL;
}
cJSON * conf_json = cJSON_Parse(conf_str);
free(conf_str);
if(conf_json==NULL){
ESP_LOGE(TAG, "Unable to parse config value for key [%s]", key);
return NULL;
}
return conf_json;
}
esp_err_t config_set_cjson(const char *key, cJSON *value, bool free_cjson){
char * value_str = cJSON_PrintUnformatted(value);
if(value_str==NULL){
ESP_LOGE(TAG, "Unable to print cJSON for key [%s]", key);
return ESP_ERR_INVALID_ARG;
}
esp_err_t err = config_set_value(NVS_TYPE_STR,key, value_str);
free(value_str);
if(free_cjson){
cJSON_Delete(value);
}
return err;
}
esp_err_t config_set_cjson_str_and_free(const char *key, cJSON *value){
return config_set_cjson(key, value, true);
}
void config_get_uint16t_from_str(const char *key, uint16_t *value, uint16_t default_value){
char * str_value = config_alloc_get(NVS_TYPE_STR, key);
if(str_value == NULL){
*value = default_value;
return ;
}
*value = atoi(str_value);
free(str_value);
}
void * config_alloc_get_str(const char *key, char *lead, char *fallback) {
if (lead && *lead) return strdup_psram(lead);
char *value = config_alloc_get_default(NVS_TYPE_STR, key, NULL, 0);
if ((!value || !*value) && fallback) {
if (value) free(value);
value = strdup_psram(fallback);
}
return value;
}
void * config_alloc_get_default(nvs_type_t nvs_type, const char *key, void * default_value, size_t blob_size) {
void * value = NULL;
ESP_LOGV(TAG, "Retrieving key %s from nvs cache for type %s.", key,type_to_str(nvs_type));
if(nvs_json==NULL){
ESP_LOGE(TAG,"configuration not loaded!");
return value;
}
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
ESP_LOGE(TAG, "Unable to lock config");
return value;
}
ESP_LOGD(TAG,"Getting config entry for key %s",key);
cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
if(entry !=NULL){
ESP_LOGV(TAG, "Entry found, getting value.");
value = config_safe_alloc_get_entry_value(nvs_type, entry);
}
else if(default_value!=NULL){
// Value was not found
ESP_LOGW(TAG, "Adding new config value for key [%s]",key);
entry=config_set_value_safe(nvs_type, key, default_value);
if(entry == NULL){
ESP_LOGE(TAG, "Failed to add value to cache");
}
else {
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGV(TAG, "Value added configuration object for key [%s]: \n%s", entry->string,entry_str);
free(entry_str);
}
else {
ESP_LOGV(TAG, "Value added configuration object for key [%s]", entry->string);
}
value = config_safe_alloc_get_entry_value(nvs_type, entry);
}
}
else{
ESP_LOGW(TAG,"Value not found for key %s",key);
}
config_unlock();
return value;
}
char * config_alloc_get_json(bool bFormatted){
char * json_buffer = NULL;
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
return strdup_psram("{\"error\":\"Unable to lock configuration object.\"}");
}
if(bFormatted){
json_buffer= cJSON_Print(nvs_json);
}
else {
json_buffer= cJSON_PrintUnformatted(nvs_json);
}
config_unlock();
return json_buffer;
}
esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, const void * value){
esp_err_t result = ESP_OK;
if(!key ||!key[0]){
ESP_LOGW(TAG,"Empty key passed. Ignoring entry!");
return ESP_ERR_INVALID_ARG;
}
if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
result = ESP_FAIL;
}
cJSON * entry = config_set_value_safe(nvs_type, key, value);
if(entry == NULL){
result = ESP_FAIL;
}
else{
char * entry_str = cJSON_PrintUnformatted(entry);
if(entry_str!=NULL){
ESP_LOGV(TAG,"config_set_value result: \n%s",entry_str);
free(entry_str);
}
else {
ESP_LOGV(TAG,"config_set_value completed");
}
}
config_unlock();
return result;
}
cJSON* cjson_update_string(cJSON** root, const char* key, const char* value) {
if (*root == NULL) {
*root = cJSON_CreateObject();
if (*root == NULL) {
ESP_LOGE(TAG, "Error creating cJSON object!");
}
}
if (!key || !value || strlen(key) == 0) {
ESP_LOGE(TAG, "cjson_update_string. Invalid key or value passed! key: %s, value: %s", STR_OR_ALT(key, ""), STR_OR_ALT(value, ""));
return *root;
}
cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
if (cjsonvalue && strcasecmp(cJSON_GetStringValue(cjsonvalue), value) != 0) {
ESP_LOGD(TAG, "Value %s changed from %s to %s", key, cJSON_GetStringValue(cjsonvalue), value);
cJSON_SetValuestring(cjsonvalue, value);
} else if(!cjsonvalue){
ESP_LOGD(TAG, "Adding new value %s: %s", key, value);
cJSON_AddItemToObject(*root, key, cJSON_CreateString(value));
}
return *root;
}
cJSON* cjson_update_number(cJSON** root, const char* key, int value) {
if (*root == NULL) {
*root = cJSON_CreateObject();
}
if (key && strlen(key) != 0) {
cJSON* cjsonvalue = cJSON_GetObjectItemCaseSensitive(*root, key);
if (cjsonvalue) {
cJSON_SetNumberValue(cjsonvalue, value);
} else {
cJSON_AddNumberToObject(*root, key, value);
}
}
return *root;
}
IMPLEMENT_SET_DEFAULT(uint8_t,NVS_TYPE_U8);
IMPLEMENT_SET_DEFAULT(int8_t,NVS_TYPE_I8);
IMPLEMENT_SET_DEFAULT(uint16_t,NVS_TYPE_U16);
IMPLEMENT_SET_DEFAULT(int16_t,NVS_TYPE_I16);
IMPLEMENT_SET_DEFAULT(uint32_t,NVS_TYPE_U32);
IMPLEMENT_SET_DEFAULT(int32_t,NVS_TYPE_I32);
IMPLEMENT_GET_NUM(uint8_t,NVS_TYPE_U8);
IMPLEMENT_GET_NUM(int8_t,NVS_TYPE_I8);
IMPLEMENT_GET_NUM(uint16_t,NVS_TYPE_U16);
IMPLEMENT_GET_NUM(int16_t,NVS_TYPE_I16);
IMPLEMENT_GET_NUM(uint32_t,NVS_TYPE_U32);
IMPLEMENT_GET_NUM(int32_t,NVS_TYPE_I32);

View File

@@ -1,72 +0,0 @@
#pragma once
#include <stdio.h>
#include <string.h>
#include "nvs.h"
#include "assert.h"
#include "cJSON.h"
#ifdef __cplusplus
extern "C" {
#endif
#define PARSE_PARAM(S,P,C,V) do { \
char *__p; \
if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atoi(__p+1); \
} while (0)
#define PARSE_PARAM_FLOAT(S,P,C,V) do { \
char *__p; \
if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atof(__p+1); \
} while (0)
#define PARSE_PARAM_STR(S,P,C,V,I) do { \
char *__p; \
if ((__p = strstr(S, P)) && (__p = strchr(__p, C))) { \
while (*++__p == ' '); \
sscanf(__p,"%" #I "[^,]", V); \
} \
} while (0)
#define DECLARE_SET_DEFAULT(t) void config_set_default_## t (const char *key, t value);
#define DECLARE_GET_NUM(t) esp_err_t config_get_## t (const char *key, t * value);
#ifndef FREE_RESET
#define FREE_RESET(p) if(p!=NULL) { free(p); p=NULL; }
#endif
DECLARE_SET_DEFAULT(uint8_t);
DECLARE_SET_DEFAULT(uint16_t);
DECLARE_SET_DEFAULT(uint32_t);
DECLARE_SET_DEFAULT(int8_t);
DECLARE_SET_DEFAULT(int16_t);
DECLARE_SET_DEFAULT(int32_t);
DECLARE_GET_NUM(uint8_t);
DECLARE_GET_NUM(uint16_t);
DECLARE_GET_NUM(uint32_t);
DECLARE_GET_NUM(int8_t);
DECLARE_GET_NUM(int16_t);
DECLARE_GET_NUM(int32_t);
bool config_has_changes();
void config_commit_to_nvs();
void config_start_timer();
void config_init();
void * config_alloc_get_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size);
void * config_alloc_get_str(const char *key, char *lead, char *fallback);
cJSON * config_alloc_get_cjson(const char *key);
esp_err_t config_set_cjson_str_and_free(const char *key, cJSON *value);
esp_err_t config_set_cjson(const char *key, cJSON *value, bool free_cjson);
void config_get_uint16t_from_str(const char *key, uint16_t *value, uint16_t default_value);
void config_delete_key(const char *key);
void config_set_default(nvs_type_t type, const char *key, const void * default_value, size_t blob_size);
void * config_alloc_get(nvs_type_t nvs_type, const char *key) ;
bool wait_for_commit();
char * config_alloc_get_json(bool bFormatted);
esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, const void * value);
nvs_type_t config_get_item_type(cJSON * entry);
void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry);
cJSON* cjson_update_number(cJSON** root, const char* key, int value);
cJSON* cjson_update_string(cJSON** root, const char* key, const char* value);
#ifdef __cplusplus
}
#endif