mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-09 04:57:06 +03:00
Network manager implemented and relatively stable
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||
#include "nvs_utilities.h"
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -15,6 +14,7 @@
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs_utilities.h"
|
||||
#include "platform_config.h"
|
||||
#include "globdefs.h"
|
||||
|
||||
const char current_namespace[] = "config";
|
||||
const char settings_partition[] = "settings";
|
||||
@@ -69,6 +69,11 @@ const char *type_to_str(nvs_type_t type)
|
||||
|
||||
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();
|
||||
@@ -95,62 +100,89 @@ void initialize_nvs() {
|
||||
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);
|
||||
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);
|
||||
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)) {
|
||||
void * value = get_nvs_value_alloc(info.type,info.key);
|
||||
if(value==NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "nvs read failed.");
|
||||
return ESP_FAIL;
|
||||
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);
|
||||
}
|
||||
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_LOGD(TAG,"Config 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;
|
||||
}
|
||||
}
|
||||
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(nvs_type_t type, const char *key, void * data,
|
||||
size_t data_len) {
|
||||
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(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
err = nvs_open_from_partition(partition, namespace, NVS_READWRITE, &nvs);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
@@ -185,53 +217,65 @@ esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data,
|
||||
nvs_close(nvs);
|
||||
return err;
|
||||
}
|
||||
void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
|
||||
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;
|
||||
|
||||
err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READONLY, &nvs);
|
||||
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(sizeof(int8_t));
|
||||
value=malloc_init_external(sizeof(int8_t));
|
||||
err = nvs_get_i8(nvs, key, (int8_t *) value);
|
||||
} else if (type == NVS_TYPE_U8) {
|
||||
value=malloc(sizeof(uint8_t));
|
||||
value=malloc_init_external(sizeof(uint8_t));
|
||||
err = nvs_get_u8(nvs, key, (uint8_t *) value);
|
||||
} else if (type == NVS_TYPE_I16) {
|
||||
value=malloc(sizeof(int16_t));
|
||||
value=malloc_init_external(sizeof(int16_t));
|
||||
err = nvs_get_i16(nvs, key, (int16_t *) value);
|
||||
} else if (type == NVS_TYPE_U16) {
|
||||
value=malloc(sizeof(uint16_t));
|
||||
value=malloc_init_external(sizeof(uint16_t));
|
||||
err = nvs_get_u16(nvs, key, (uint16_t *) value);
|
||||
} else if (type == NVS_TYPE_I32) {
|
||||
value=malloc(sizeof(int32_t));
|
||||
value=malloc_init_external(sizeof(int32_t));
|
||||
err = nvs_get_i32(nvs, key, (int32_t *) value);
|
||||
} else if (type == NVS_TYPE_U32) {
|
||||
value=malloc(sizeof(uint32_t));
|
||||
value=malloc_init_external(sizeof(uint32_t));
|
||||
err = nvs_get_u32(nvs, key, (uint32_t *) value);
|
||||
} else if (type == NVS_TYPE_I64) {
|
||||
value=malloc(sizeof(int64_t));
|
||||
value=malloc_init_external(sizeof(int64_t));
|
||||
err = nvs_get_i64(nvs, key, (int64_t *) value);
|
||||
} else if (type == NVS_TYPE_U64) {
|
||||
value=malloc(sizeof(uint64_t));
|
||||
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(len);
|
||||
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(len+1);
|
||||
value=malloc_init_external(len+1);
|
||||
if(size){
|
||||
*size=len;
|
||||
}
|
||||
err = nvs_get_blob(nvs, key, value, &len);
|
||||
}
|
||||
}
|
||||
@@ -244,6 +288,9 @@ void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
|
||||
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;
|
||||
@@ -296,11 +343,10 @@ esp_err_t get_nvs_value(nvs_type_t type, const char *key, void*value, const uint
|
||||
nvs_close(nvs);
|
||||
return err;
|
||||
}
|
||||
esp_err_t erase_nvs(const char *key)
|
||||
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(current_namespace, NVS_READWRITE, &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) {
|
||||
@@ -311,7 +357,35 @@ esp_err_t erase_nvs(const char *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;
|
||||
}
|
||||
Reference in New Issue
Block a user