mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-22 23:48:31 +03:00
enhancement: new "config" module to access configuration in RAM
This is to allow moving some tasks/stack memory to SPIRAM, saving on precious IRAM. HTTP server should also be more responsive.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
set(COMPONENT_ADD_INCLUDEDIRS . )
|
||||
|
||||
set(COMPONENT_SRCS "esp_app_main.c" "platform_esp32.c" "cmd_wifi.c" "console.c" "nvs_utilities.c" "cmd_squeezelite.c")
|
||||
set(COMPONENT_SRCS "esp_app_main.c" "platform_esp32.c" "cmd_wifi.c" "console.c" "nvs_utilities.c" "cmd_squeezelite.c" "config.c")
|
||||
set(REQUIRES esp_common)
|
||||
set(REQUIRES_COMPONENTS freertos squeezelite nvs_flash esp32 spi_flash newlib log console ota tools )
|
||||
|
||||
|
||||
@@ -48,13 +48,13 @@ static void * squeezelite_thread(){
|
||||
// Let's not wait on WiFi to allow squeezelite to run in bluetooth mode
|
||||
// ESP_LOGI(TAG,"Waiting for WiFi.");
|
||||
// while(!wait_for_wifi()){usleep(100000);};
|
||||
ESP_LOGD(TAG ,"Number of args received: %u",thread_parms.argc );
|
||||
ESP_LOGD(TAG ,"Values:");
|
||||
ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
|
||||
ESP_LOGV(TAG ,"Values:");
|
||||
for(int i = 0;i<thread_parms.argc; i++){
|
||||
ESP_LOGD(TAG ," %s",thread_parms.argv[i]);
|
||||
ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG,"Starting Squeezelite runner Thread");
|
||||
ESP_LOGV(TAG,"Starting Squeezelite runner Thread");
|
||||
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
|
||||
cfg.thread_name= "squeezelite-run";
|
||||
cfg.inherit_cfg = true;
|
||||
@@ -80,9 +80,9 @@ static int launchsqueezelite(int argc, char **argv)
|
||||
{
|
||||
ESP_LOGV(TAG ,"Begin");
|
||||
|
||||
ESP_LOGD(TAG, "Parameters:");
|
||||
ESP_LOGV(TAG, "Parameters:");
|
||||
for(int i = 0;i<argc; i++){
|
||||
ESP_LOGD(TAG, " %s",argv[i]);
|
||||
ESP_LOGV(TAG, " %s",argv[i]);
|
||||
}
|
||||
ESP_LOGV(TAG,"Saving args in thread structure");
|
||||
|
||||
@@ -102,7 +102,7 @@ static int launchsqueezelite(int argc, char **argv)
|
||||
thread_parms.argv[thread_parms.argc++]=strdup("-?");
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG,"Starting Squeezelite Thread");
|
||||
ESP_LOGD(TAG,"Starting Squeezelite Thread");
|
||||
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
|
||||
cfg.thread_name= "squeezelite";
|
||||
cfg.inherit_cfg = true;
|
||||
|
||||
623
main/config.c
Normal file
623
main/config.c
Normal file
@@ -0,0 +1,623 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||
#include "config.h"
|
||||
#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 "cmd_decl.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs_utilities.h"
|
||||
#include "cJSON.h"
|
||||
#include "freertos/timers.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
|
||||
#define CONFIG_COMMIT_DELAY 1000
|
||||
#define LOCK_MAX_WAIT 20*CONFIG_COMMIT_DELAY
|
||||
static const char * TAG = "config";
|
||||
cJSON * nvs_json=NULL;
|
||||
TimerHandle_t timer;
|
||||
SemaphoreHandle_t config_mutex = NULL;
|
||||
EventGroupHandle_t config_group;
|
||||
/* @brief indicate that the ESP32 is currently connected. */
|
||||
const int CONFIG_PENDING_CHANGE_BIT = BIT0;
|
||||
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, void * value);
|
||||
static void vCallbackFunction( TimerHandle_t xTimer );
|
||||
void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag);
|
||||
|
||||
void config_init(){
|
||||
ESP_LOGD(TAG, "Creating mutex for Config");
|
||||
config_mutex = xSemaphoreCreateMutex();
|
||||
ESP_LOGD(TAG, "Creating event group");
|
||||
config_group = xEventGroupCreate();
|
||||
ESP_LOGD(TAG, "Loading config from nvs");
|
||||
if(nvs_json !=NULL){
|
||||
cJSON_Delete(nvs_json);
|
||||
}
|
||||
nvs_json = cJSON_CreateObject();
|
||||
|
||||
config_set_group_bit(CONFIG_LOAD_BIT,true);
|
||||
nvs_load_config();
|
||||
config_set_group_bit(CONFIG_LOAD_BIT,false);
|
||||
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.");
|
||||
}
|
||||
|
||||
}
|
||||
cJSON * config_set_value_safe(nvs_type_t nvs_type, const char *key, void * value){
|
||||
char * num_buffer = NULL;
|
||||
num_buffer = malloc(NUM_BUFFER_LEN);
|
||||
memset(num_buffer,0x00,NUM_BUFFER_LEN);
|
||||
cJSON * entry = cJSON_CreateObject();
|
||||
|
||||
if(entry == NULL) {
|
||||
ESP_LOGE(TAG, "Unable to allocate memory for entry %s",key);
|
||||
return NULL;
|
||||
}
|
||||
cJSON_AddNumberToObject(entry,"type", nvs_type );
|
||||
switch (nvs_type) {
|
||||
case NVS_TYPE_I8:
|
||||
snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int8_t*)value);
|
||||
cJSON_AddNumberToObject(entry,"value", *(int8_t*)value );
|
||||
break;
|
||||
case NVS_TYPE_I16:
|
||||
snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int16_t*)value);
|
||||
cJSON_AddNumberToObject(entry,"value", *(int16_t*)value );
|
||||
break;
|
||||
case NVS_TYPE_I32:
|
||||
snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int32_t*)value);
|
||||
cJSON_AddNumberToObject(entry,"value", *(int32_t*)value );
|
||||
break;
|
||||
case NVS_TYPE_U8:
|
||||
snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint8_t*)value);
|
||||
cJSON_AddNumberToObject(entry,"value", *(uint8_t*)value );
|
||||
break;
|
||||
case NVS_TYPE_U16:
|
||||
snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint16_t*)value);
|
||||
cJSON_AddNumberToObject(entry,"value", *(uint16_t*)value );
|
||||
break;
|
||||
case NVS_TYPE_U32:
|
||||
snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint32_t*)value);
|
||||
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;
|
||||
}
|
||||
cJSON * existing = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
|
||||
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 * chg_flag =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");
|
||||
}
|
||||
cJSON_Delete(chg_flag);
|
||||
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. ");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// This is a new entry.
|
||||
config_set_entry_changed_flag(entry,true);
|
||||
cJSON_AddItemToObject(nvs_json, key, entry);
|
||||
}
|
||||
free(num_buffer);
|
||||
|
||||
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;
|
||||
}
|
||||
return entry_type->valuedouble;
|
||||
}
|
||||
void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag){
|
||||
if(entry==NULL){
|
||||
ESP_LOGE(TAG,"null pointer received!");
|
||||
return;
|
||||
}
|
||||
bool bIsConfigLoading=((xEventGroupGetBits(config_group) & CONFIG_LOAD_BIT)!=0);
|
||||
bool changedFlag=bIsConfigLoading?false:flag;
|
||||
cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
|
||||
if(changed ==NULL ) {
|
||||
ESP_LOGV(TAG, "Adding change flag. ");
|
||||
cJSON_AddBoolToObject(entry,"chg",changedFlag);
|
||||
}
|
||||
else {
|
||||
if(cJSON_IsTrue(changed) && changedFlag){
|
||||
ESP_LOGW(TAG, "Commit flag not changed!");
|
||||
}
|
||||
else{
|
||||
ESP_LOGV(TAG, "Updating change flag to %s",changedFlag?"TRUE":"FALSE");
|
||||
cJSON_Delete(changed);
|
||||
cJSON_AddBoolToObject(entry,"chg",changedFlag);
|
||||
}
|
||||
}
|
||||
if(changedFlag) config_raise_change(true);
|
||||
}
|
||||
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(sizeof(int8_t));
|
||||
*(int8_t *)value = (int8_t)entry_value->valuedouble;
|
||||
} else if (nvs_type == NVS_TYPE_U8) {
|
||||
value=malloc(sizeof(uint8_t));
|
||||
*(uint8_t *)value = (uint8_t)entry_value->valuedouble;
|
||||
} else if (nvs_type == NVS_TYPE_I16) {
|
||||
value=malloc(sizeof(int16_t));
|
||||
*(int16_t *)value = (int16_t)entry_value->valuedouble;
|
||||
} else if (nvs_type == NVS_TYPE_U16) {
|
||||
value=malloc(sizeof(uint16_t));
|
||||
*(uint16_t *)value = (uint16_t)entry_value->valuedouble;
|
||||
} else if (nvs_type == NVS_TYPE_I32) {
|
||||
value=malloc(sizeof(int32_t));
|
||||
*(int32_t *)value = (int32_t)entry_value->valuedouble;
|
||||
} else if (nvs_type == NVS_TYPE_U32) {
|
||||
value=malloc(sizeof(uint32_t));
|
||||
*(uint32_t *)value = (uint32_t)entry_value->valuedouble;
|
||||
} else if (nvs_type == NVS_TYPE_I64) {
|
||||
value=malloc(sizeof(int64_t));
|
||||
*(int64_t *)value = (int64_t)entry_value->valuedouble;
|
||||
} else if (nvs_type == NVS_TYPE_U64) {
|
||||
value=malloc(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",
|
||||
entry_value->string,
|
||||
entry_value->valuestring,
|
||||
entry_value->type,
|
||||
entry_str);
|
||||
free(entry_str);
|
||||
}
|
||||
else {
|
||||
ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d",
|
||||
entry_value->string,
|
||||
entry_value->valuestring,
|
||||
entry_value->type);
|
||||
}
|
||||
}
|
||||
else {
|
||||
value=(void *)strdup(cJSON_GetStringValue(entry_value));
|
||||
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.");
|
||||
ESP_LOGV(TAG,"config_commit_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){
|
||||
esp_err_t err = store_nvs_value(type,entry->string,value);
|
||||
free(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();
|
||||
}
|
||||
bool config_has_changes(){
|
||||
return (xEventGroupGetBits(config_group) & CONFIG_PENDING_CHANGE_BIT)!=0;
|
||||
}
|
||||
|
||||
|
||||
bool wait_for_commit(){
|
||||
bool needs_commit=(xEventGroupGetBits(config_group) & CONFIG_PENDING_CHANGE_BIT)!=0;
|
||||
if(needs_commit){
|
||||
ESP_LOGD(TAG,"Waiting for config commit ...");
|
||||
needs_commit = (xEventGroupWaitBits(config_group, CONFIG_PENDING_CHANGE_BIT,pdFALSE, pdTRUE, (CONFIG_COMMIT_DELAY*5) / portTICK_PERIOD_MS) & CONFIG_PENDING_CHANGE_BIT)!=0;
|
||||
if(needs_commit){
|
||||
ESP_LOGE(TAG,"Timeout waiting for config commit.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG,"Config committed!");
|
||||
}
|
||||
}
|
||||
return needs_commit;
|
||||
}
|
||||
|
||||
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 ) {
|
||||
if(config_has_changes()){
|
||||
ESP_LOGI(TAG, "configuration has some uncommitted entries");
|
||||
config_commit_to_nvs();
|
||||
}
|
||||
else{
|
||||
ESP_LOGV(TAG,"commit timer: commit flag not set");
|
||||
}
|
||||
xTimerReset( xTimer, 10 );
|
||||
}
|
||||
void config_raise_change(bool flag){
|
||||
if(config_set_group_bit(CONFIG_PENDING_CHANGE_BIT,flag))
|
||||
{
|
||||
ESP_LOGD(TAG,"Config change indicator was %s",flag?"Set":"Cleared");
|
||||
}
|
||||
}
|
||||
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_PENDING_CHANGE_BIT ){
|
||||
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, 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 false;
|
||||
}
|
||||
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);
|
||||
}
|
||||
cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
|
||||
if(entry !=NULL){
|
||||
ESP_LOGI(TAG, "Removing config key [%s]", entry->string);
|
||||
cJSON_Delete(entry);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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("{\"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, void * value){
|
||||
esp_err_t result = ESP_OK;
|
||||
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;
|
||||
}
|
||||
|
||||
24
main/config.h
Normal file
24
main/config.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "esp_system.h"
|
||||
#include "nvs_utilities.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
bool config_has_uncommitted();
|
||||
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_delete_key(const char *key);
|
||||
void config_set_default(nvs_type_t type, const char *key, 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, void * value);
|
||||
|
||||
@@ -55,7 +55,7 @@ void process_autoexec(){
|
||||
char * autoexec_value=NULL;
|
||||
uint8_t autoexec_flag=0;
|
||||
|
||||
char * str_flag = get_nvs_value_alloc(NVS_TYPE_STR, "autoexec");
|
||||
char * str_flag = config_alloc_get(NVS_TYPE_STR, "autoexec");
|
||||
if(!bypass_wifi_manager){
|
||||
ESP_LOGW(TAG, "Procesing autoexec commands while wifi_manager active. Wifi related commands will be ignored.");
|
||||
}
|
||||
@@ -65,12 +65,12 @@ void process_autoexec(){
|
||||
|
||||
if(str_flag !=NULL ){
|
||||
autoexec_flag=atoi(str_flag);
|
||||
ESP_LOGI(TAG,"autoexec flag value found with value %u, from string value: %s", autoexec_flag, str_flag);
|
||||
ESP_LOGI(TAG,"autoexec is set to %s auto-process", autoexec_flag>0?"perform":"skip");
|
||||
if(autoexec_flag == 1) {
|
||||
do {
|
||||
snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);
|
||||
ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
|
||||
autoexec_value= get_nvs_value_alloc(NVS_TYPE_STR, autoexec_name);
|
||||
autoexec_value= config_alloc_get(NVS_TYPE_STR, autoexec_name);
|
||||
if(autoexec_value!=NULL ){
|
||||
if(!bypass_wifi_manager && strstr(autoexec_value, "join ")!=NULL ){
|
||||
ESP_LOGW(TAG,"Ignoring wifi join command.");
|
||||
@@ -97,11 +97,7 @@ void process_autoexec(){
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(TAG,"No matching command found for name autoexec. Adding default entries");
|
||||
char autoexec_dft[]="0";
|
||||
char autoexec1_dft[256]="squeezelite -o I2S -b 500:2000 -d all=info -M esp32";
|
||||
store_nvs_value(NVS_TYPE_STR,"autoexec",autoexec_dft);
|
||||
store_nvs_value(NVS_TYPE_STR,"autoexec1",autoexec1_dft);
|
||||
ESP_LOGD(TAG,"No matching command found for name autoexec.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,6 +258,8 @@ static void * console_thread() {
|
||||
run_command(line);
|
||||
/* linenoise allocates line buffer on the heap, so need to free it */
|
||||
linenoiseFree(line);
|
||||
config_commit_to_nvs();
|
||||
taskYIELD();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,8 @@
|
||||
#include "wifi_manager.h"
|
||||
#include "squeezelite-ota.h"
|
||||
#include <math.h>
|
||||
#include "config.h"
|
||||
|
||||
|
||||
EventGroupHandle_t wifi_event_group;
|
||||
bool enable_bt_sink=false;
|
||||
@@ -52,16 +54,17 @@ bool jack_mutes_amp=false;
|
||||
bool bypass_wifi_manager=false;
|
||||
const int CONNECTED_BIT = BIT0;
|
||||
#define JOIN_TIMEOUT_MS (10000)
|
||||
|
||||
#define LOCAL_MAC_SIZE 20
|
||||
static const char TAG[] = "esp_app_main";
|
||||
#define DEFAULT_HOST_NAME "squeezelite"
|
||||
char * fwurl = NULL;
|
||||
|
||||
#ifdef CONFIG_SQUEEZEAMP
|
||||
#define LED_GREEN_GPIO 12
|
||||
#define LED_RED_GPIO 13
|
||||
#else
|
||||
#define LED_GREEN_GPIO 0
|
||||
#define LED_RED_GPIO 0
|
||||
#define LED_GREEN_GPIO -1
|
||||
#define LED_RED_GPIO -1
|
||||
#endif
|
||||
static bool bWifiConnected=false;
|
||||
|
||||
@@ -96,61 +99,14 @@ bool wait_for_wifi(){
|
||||
}
|
||||
return connected;
|
||||
}
|
||||
static void initialize_nvs() {
|
||||
ESP_LOGI(TAG,"Initializing nvs from flash");
|
||||
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,"Error %s. Erasing nvs flash", esp_err_to_name(err));
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(err);
|
||||
ESP_LOGI(TAG,"Initializing nvs from 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,"Error %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 init completed with error %s",esp_err_to_name(err));
|
||||
}
|
||||
ESP_ERROR_CHECK(err);
|
||||
ESP_LOGD(TAG,"nvs init completed");
|
||||
|
||||
}
|
||||
char * process_ota_url(){
|
||||
nvs_handle nvs;
|
||||
ESP_LOGI(TAG,"Checking for update url");
|
||||
char * fwurl=get_nvs_value_alloc(NVS_TYPE_STR, "fwurl");
|
||||
char * fwurl=config_alloc_get(NVS_TYPE_STR, "fwurl");
|
||||
if(fwurl!=NULL)
|
||||
{
|
||||
ESP_LOGD(TAG,"Deleting nvs entry for Firmware URL %s", fwurl);
|
||||
esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_erase_key(nvs, "fwurl");
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGD(TAG,"Firmware url erased from nvs.");
|
||||
err = nvs_commit(nvs);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Value with key '%s' erased", "fwurl");
|
||||
ESP_LOGD(TAG,"nvs erase committed.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG,"Unable to commit nvs erase operation. Error : %s.",esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG,"Error : %s. Unable to delete firmware url key.",esp_err_to_name(err));
|
||||
}
|
||||
nvs_close(nvs);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG,"Error opening nvs: %s. Unable to delete firmware url key.",esp_err_to_name(err));
|
||||
}
|
||||
config_delete_key("fwurl");
|
||||
}
|
||||
return fwurl;
|
||||
}
|
||||
@@ -164,7 +120,7 @@ char * process_ota_url(){
|
||||
//CONFIG_WIFI_MANAGER_MAX_RETRY=2
|
||||
u16_t get_adjusted_volume(u16_t volume){
|
||||
|
||||
char * str_factor = get_nvs_value_alloc_default(NVS_TYPE_STR, "volumefactor", "3", 0);
|
||||
char * str_factor = config_alloc_get_default(NVS_TYPE_STR, "volumefactor", "3", 0);
|
||||
if(str_factor != NULL ){
|
||||
|
||||
float factor = atof(str_factor);
|
||||
@@ -175,54 +131,103 @@ u16_t get_adjusted_volume(u16_t volume){
|
||||
ESP_LOGW(TAG,"Error retrieving volume factor. Returning unmodified volume level. ");
|
||||
return volume;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void register_default_nvs(){
|
||||
uint8_t mac[6];
|
||||
char macStr[LOCAL_MAC_SIZE+1];
|
||||
char default_ap_name[strlen(CONFIG_DEFAULT_AP_SSID)+sizeof(macStr)];
|
||||
char default_host_name[strlen(DEFAULT_HOST_NAME)+sizeof(macStr)];
|
||||
char default_command_line[strlen(CONFIG_DEFAULT_COMMAND_LINE)+sizeof(macStr)];
|
||||
|
||||
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bt_sink_name", CONFIG_BT_NAME);
|
||||
nvs_value_set_default(NVS_TYPE_STR, "bt_sink_name", CONFIG_BT_NAME, 0);
|
||||
esp_read_mac((uint8_t *)&mac, ESP_MAC_WIFI_STA);
|
||||
snprintf(macStr, LOCAL_MAC_SIZE-1,"-%x%x%x", mac[3], mac[4], mac[5]);
|
||||
|
||||
strcpy(default_ap_name,CONFIG_DEFAULT_AP_SSID);
|
||||
strcat(default_ap_name,macStr);
|
||||
|
||||
strcpy(default_host_name,DEFAULT_HOST_NAME);
|
||||
strcat(default_host_name,macStr);
|
||||
|
||||
if(!strstr(CONFIG_DEFAULT_COMMAND_LINE, "-n %s")){
|
||||
snprintf(default_command_line, sizeof(default_command_line)-1,CONFIG_DEFAULT_COMMAND_LINE,default_host_name);
|
||||
}
|
||||
else{
|
||||
strncpy(default_command_line, CONFIG_DEFAULT_COMMAND_LINE,sizeof(default_command_line)-1);
|
||||
strncat(default_command_line, "-n ",sizeof(default_command_line)-1);
|
||||
strncat(default_command_line, default_host_name,sizeof(default_command_line)-1);
|
||||
}
|
||||
|
||||
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "autoexec", "1");
|
||||
config_set_default(NVS_TYPE_STR,"autoexec","1", 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "autoexec1",default_command_line);
|
||||
config_set_default(NVS_TYPE_STR,"autoexec1",default_command_line,0);
|
||||
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "volumefactor", "3");
|
||||
config_set_default(NVS_TYPE_STR, "volumefactor", "3", 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bt_name", CONFIG_BT_NAME);
|
||||
config_set_default(NVS_TYPE_STR, "bt_name", CONFIG_BT_NAME, 0);
|
||||
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bt_sink_pin", STR(CONFIG_BT_SINK_PIN));
|
||||
nvs_value_set_default(NVS_TYPE_STR, "bt_sink_pin", STR(CONFIG_BT_SINK_PIN), 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "host_name", "squeezelite-esp32");
|
||||
nvs_value_set_default(NVS_TYPE_STR, "host_name", "squeezelite-esp32", 0);
|
||||
config_set_default(NVS_TYPE_STR, "bt_sink_pin", STR(CONFIG_BT_SINK_PIN), 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "host_name", default_host_name);
|
||||
config_set_default(NVS_TYPE_STR, "host_name", default_host_name, 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "release_url", SQUEEZELITE_ESP32_RELEASE_URL);
|
||||
nvs_value_set_default(NVS_TYPE_STR, "release_url", SQUEEZELITE_ESP32_RELEASE_URL, 0);
|
||||
config_set_default(NVS_TYPE_STR, "release_url", SQUEEZELITE_ESP32_RELEASE_URL, 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s","ap_ip_address",CONFIG_DEFAULT_AP_IP );
|
||||
nvs_value_set_default(NVS_TYPE_STR, "ap_ip_address",CONFIG_DEFAULT_AP_IP , 0);
|
||||
config_set_default(NVS_TYPE_STR, "ap_ip_address",CONFIG_DEFAULT_AP_IP , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY );
|
||||
nvs_value_set_default(NVS_TYPE_STR, "ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY , 0);
|
||||
config_set_default(NVS_TYPE_STR, "ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s","ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK );
|
||||
nvs_value_set_default(NVS_TYPE_STR, "ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK , 0);
|
||||
config_set_default(NVS_TYPE_STR, "ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL));
|
||||
nvs_value_set_default(NVS_TYPE_STR, "ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL) , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_ssid",CONFIG_DEFAULT_AP_SSID );
|
||||
nvs_value_set_default(NVS_TYPE_STR, "ap_ssid",CONFIG_DEFAULT_AP_SSID , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_password", CONFIG_DEFAULT_AP_PASSWORD);
|
||||
nvs_value_set_default(NVS_TYPE_STR, "ap_password", CONFIG_DEFAULT_AP_PASSWORD, 0);
|
||||
config_set_default(NVS_TYPE_STR, "ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL) , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_ssid", default_ap_name);
|
||||
config_set_default(NVS_TYPE_STR, "ap_ssid",default_ap_name , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_pwd", CONFIG_DEFAULT_AP_PASSWORD);
|
||||
config_set_default(NVS_TYPE_STR, "ap_pwd", CONFIG_DEFAULT_AP_PASSWORD, 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "airplay_name",CONFIG_AIRPLAY_NAME);
|
||||
nvs_value_set_default(NVS_TYPE_STR, "airplay_name",CONFIG_AIRPLAY_NAME , 0);
|
||||
config_set_default(NVS_TYPE_STR, "airplay_name",CONFIG_AIRPLAY_NAME , 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "airplay_port", CONFIG_AIRPLAY_PORT);
|
||||
nvs_value_set_default(NVS_TYPE_STR, "airplay_port", CONFIG_AIRPLAY_PORT, 0);
|
||||
config_set_default(NVS_TYPE_STR, "airplay_port", CONFIG_AIRPLAY_PORT, 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "a2dp_sink_name", CONFIG_A2DP_SINK_NAME);
|
||||
nvs_value_set_default(NVS_TYPE_STR, "a2dp_sink_name", CONFIG_A2DP_SINK_NAME, 0);
|
||||
config_set_default(NVS_TYPE_STR, "a2dp_sink_name", CONFIG_A2DP_SINK_NAME, 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "a2dp_dev_name", CONFIG_A2DP_DEV_NAME);
|
||||
nvs_value_set_default(NVS_TYPE_STR, "a2dp_dev_name", CONFIG_A2DP_DEV_NAME, 0);
|
||||
config_set_default(NVS_TYPE_STR, "a2dp_dev_name", CONFIG_A2DP_DEV_NAME, 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bypass_wm", "0");
|
||||
nvs_value_set_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
|
||||
config_set_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "enable_bt_sink", STR(CONFIG_BT_SINK));
|
||||
char * flag = get_nvs_value_alloc_default(NVS_TYPE_STR, "enable_bt_sink", STR(CONFIG_BT_SINK), 0);
|
||||
enable_bt_sink= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
|
||||
free(flag);
|
||||
char * flag = config_alloc_get_default(NVS_TYPE_STR, "enable_bt_sink", STR(CONFIG_BT_SINK), 0);
|
||||
if(flag !=NULL){
|
||||
enable_bt_sink= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
|
||||
free(flag);
|
||||
}
|
||||
else {
|
||||
ESP_LOGE(TAG,"Unable to get flag 'enable_bt_sink'");
|
||||
}
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "enable_airplay", STR(CONFIG_AIRPLAY_SINK));
|
||||
flag = get_nvs_value_alloc_default(NVS_TYPE_STR, "enable_airplay", STR(CONFIG_AIRPLAY_SINK), 0);
|
||||
enable_airplay= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
|
||||
free(flag);
|
||||
flag = config_alloc_get_default(NVS_TYPE_STR, "enable_airplay", STR(CONFIG_AIRPLAY_SINK), 0);
|
||||
if(flag !=NULL){
|
||||
enable_airplay= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
|
||||
free(flag);
|
||||
}
|
||||
else {
|
||||
ESP_LOGE(TAG,"Unable to get flag 'enable_airplay'");
|
||||
}
|
||||
|
||||
|
||||
ESP_LOGD(TAG,"Registering default value for key %s, value %s", "jack_mutes_amp", "n");
|
||||
flag = get_nvs_value_alloc_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
|
||||
jack_mutes_amp= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
|
||||
free(flag);
|
||||
flag = config_alloc_get_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
|
||||
|
||||
if(flag !=NULL){
|
||||
jack_mutes_amp= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
|
||||
free(flag);
|
||||
}
|
||||
else {
|
||||
ESP_LOGE(TAG,"Unable to get flag 'jack_mutes_amp'");
|
||||
}
|
||||
ESP_LOGD(TAG,"Done setting default values in nvs.");
|
||||
}
|
||||
|
||||
@@ -238,6 +243,9 @@ void app_main()
|
||||
|
||||
ESP_LOGI(TAG,"Starting app_main");
|
||||
initialize_nvs();
|
||||
ESP_LOGI(TAG,"Setting up config subsystem.");
|
||||
config_init();
|
||||
|
||||
ESP_LOGD(TAG,"Registering default values");
|
||||
register_default_nvs();
|
||||
|
||||
@@ -246,7 +254,7 @@ void app_main()
|
||||
|
||||
|
||||
ESP_LOGD(TAG,"Getting value for WM bypass, nvs 'bypass_wm'");
|
||||
char * bypass_wm = get_nvs_value_alloc_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
|
||||
char * bypass_wm = config_alloc_get_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
|
||||
if(bypass_wm==NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Unable to retrieve the Wifi Manager bypass flag");
|
||||
@@ -257,6 +265,7 @@ void app_main()
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG,"Configuring Green led");
|
||||
|
||||
led_config(LED_GREEN, LED_GREEN_GPIO, 0);
|
||||
ESP_LOGD(TAG,"Configuring Red led");
|
||||
led_config(LED_RED, LED_RED_GPIO, 0);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
||||
#include "nvs_utilities.h"
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -13,10 +14,78 @@
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "nvs_utilities.h"
|
||||
|
||||
const char current_namespace[] = "config";
|
||||
const char settings_partition[] = "settings";
|
||||
static const char * TAG = "platform_esp32";
|
||||
static const char * TAG = "nvs_utilities";
|
||||
|
||||
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)) {
|
||||
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_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;
|
||||
}
|
||||
|
||||
|
||||
esp_err_t store_nvs_value(nvs_type_t type, const char *key, void * data) {
|
||||
if (type == NVS_TYPE_BLOB)
|
||||
@@ -61,39 +130,12 @@ esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data,
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Value stored under key '%s'", key);
|
||||
ESP_LOGI(TAG, "Value stored under key '%s'", key);
|
||||
}
|
||||
}
|
||||
nvs_close(nvs);
|
||||
return err;
|
||||
}
|
||||
void nvs_value_set_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size) {
|
||||
free(get_nvs_value_alloc_default(type, key, default_value, blob_size));
|
||||
}
|
||||
void * get_nvs_value_alloc_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size) {
|
||||
void * current_value = get_nvs_value_alloc(type, key);
|
||||
if(current_value == NULL && default_value != NULL){
|
||||
if(type == NVS_TYPE_BLOB && blob_size == 0){
|
||||
ESP_LOGE(TAG,"Unable to store default value for BLOB object' blob size was not specified");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
esp_err_t err = store_nvs_value_len(type, key, default_value, blob_size);
|
||||
if(err!=ESP_OK){
|
||||
ESP_LOGE(TAG,"Unable to store default nvs value for key %s. Error: %s", key,esp_err_to_name(err));
|
||||
return NULL;
|
||||
}
|
||||
else{
|
||||
ESP_LOGI(TAG,"Stored new default value for key %s", key);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(current_value == NULL){
|
||||
current_value = get_nvs_value_alloc(type, key);
|
||||
}
|
||||
return current_value;
|
||||
}
|
||||
|
||||
void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
|
||||
nvs_handle nvs;
|
||||
esp_err_t err;
|
||||
@@ -101,7 +143,7 @@ void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
|
||||
|
||||
err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READONLY, &nvs);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG,"Could not open the nvs storage.");
|
||||
ESP_LOGE(TAG, "Could not open the nvs storage.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -145,7 +187,7 @@ void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
|
||||
}
|
||||
}
|
||||
if(err!=ESP_OK){
|
||||
ESP_LOGD(TAG,"Value not found for key %s",key);
|
||||
ESP_LOGD(TAG, "Value not found for key %s",key);
|
||||
if(value!=NULL)
|
||||
free(value);
|
||||
value=NULL;
|
||||
@@ -215,7 +257,7 @@ esp_err_t erase_nvs(const char *key)
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_commit(nvs);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Value with key '%s' erased", key);
|
||||
ESP_LOGI(TAG, "Value with key '%s' erased", key);
|
||||
}
|
||||
}
|
||||
nvs_close(nvs);
|
||||
|
||||
@@ -2,18 +2,20 @@
|
||||
#include "esp_err.h"
|
||||
#include "nvs.h"
|
||||
#include "trace.h"
|
||||
#include "config.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);
|
||||
esp_err_t erase_nvs(const char *key);
|
||||
void * get_nvs_value_alloc_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size);
|
||||
void nvs_value_set_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user