config.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * (c) Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. //#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
  12. #include "config.h"
  13. #include "nvs_utilities.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "esp_system.h"
  17. #include "esp_log.h"
  18. #include "esp_console.h"
  19. #include "esp_vfs_dev.h"
  20. #include "driver/uart.h"
  21. #include "linenoise/linenoise.h"
  22. #include "argtable3/argtable3.h"
  23. #include "esp_vfs_fat.h"
  24. #include "nvs.h"
  25. #include "nvs_flash.h"
  26. #include "nvs_utilities.h"
  27. #include "cJSON.h"
  28. #include "freertos/timers.h"
  29. #include "freertos/event_groups.h"
  30. #define CONFIG_COMMIT_DELAY 1000
  31. #define LOCK_MAX_WAIT 20*CONFIG_COMMIT_DELAY
  32. static const char * TAG = "config";
  33. static cJSON * nvs_json=NULL;
  34. static TimerHandle_t timer;
  35. static SemaphoreHandle_t config_mutex = NULL;
  36. static EventGroupHandle_t config_group;
  37. /* @brief indicate that the ESP32 is currently connected. */
  38. static const int CONFIG_NO_COMMIT_PENDING = BIT0;
  39. static const int CONFIG_LOAD_BIT = BIT1;
  40. bool config_lock(TickType_t xTicksToWait);
  41. void config_unlock();
  42. extern esp_err_t nvs_load_config();
  43. void config_raise_change(bool flag);
  44. cJSON_bool config_is_entry_changed(cJSON * entry);
  45. bool config_set_group_bit(int bit_num,bool flag);
  46. cJSON * config_set_value_safe(nvs_type_t nvs_type, const char *key, void * value);
  47. static void vCallbackFunction( TimerHandle_t xTimer );
  48. void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag);
  49. #define IMPLEMENT_SET_DEFAULT(t,nt) void config_set_default_## t (const char *key, t value){\
  50. void * pval = malloc(sizeof(value));\
  51. *((t *) pval) = value;\
  52. config_set_default(nt, key,pval,0);\
  53. free(pval); }
  54. #define IMPLEMENT_GET_NUM(t,nt) esp_err_t config_get_## t (const char *key, t * value){\
  55. void * pval = config_alloc_get(nt, key);\
  56. if(pval!=NULL){ *value = *(t * )pval; free(pval); return ESP_OK; }\
  57. return ESP_FAIL;}
  58. #if RECOVERY_APPLICATION==0
  59. static void * malloc_fn(size_t sz){
  60. void * ptr = heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
  61. if(ptr==NULL){
  62. ESP_LOGE(TAG,"malloc_fn: unable to allocate memory!");
  63. }
  64. return ptr;
  65. }
  66. /*
  67. static void * free_fn(void * ptr){
  68. if(ptr!=NULL){
  69. heap_caps_free(ptr);
  70. }
  71. else {
  72. ESP_LOGW(TAG,"free_fn: Cannot free null pointer!");
  73. }
  74. return NULL;
  75. }
  76. */
  77. #endif
  78. void init_cJSON(){
  79. static cJSON_Hooks hooks;
  80. // initialize cJSON hooks it uses SPIRAM memory
  81. // as opposed to IRAM
  82. #if RECOVERY_APPLICATION==0
  83. // In squeezelite mode, allocate memory from PSRAM. Otherwise allocate from internal RAM
  84. // as recovery will lock flash access when erasing FLASH or writing to OTA partition.
  85. hooks.malloc_fn=&malloc_fn;
  86. //hooks.free_fn=&free_fn;
  87. cJSON_InitHooks(&hooks);
  88. #endif
  89. }
  90. void config_init(){
  91. ESP_LOGD(TAG, "Creating mutex for Config");
  92. config_mutex = xSemaphoreCreateMutex();
  93. ESP_LOGD(TAG, "Creating event group");
  94. config_group = xEventGroupCreate();
  95. ESP_LOGD(TAG, "Loading config from nvs");
  96. init_cJSON();
  97. if(nvs_json !=NULL){
  98. cJSON_Delete(nvs_json);
  99. }
  100. nvs_json = cJSON_CreateObject();
  101. config_set_group_bit(CONFIG_LOAD_BIT,true);
  102. nvs_load_config();
  103. config_set_group_bit(CONFIG_LOAD_BIT,false);
  104. config_start_timer();
  105. }
  106. void config_start_timer(){
  107. ESP_LOGD(TAG, "Starting config timer");
  108. timer = xTimerCreate("configTimer", CONFIG_COMMIT_DELAY / portTICK_RATE_MS, pdFALSE, NULL, vCallbackFunction);
  109. if( xTimerStart( timer , CONFIG_COMMIT_DELAY/ portTICK_RATE_MS ) != pdPASS ) {
  110. ESP_LOGE(TAG, "config commitment timer failed to start.");
  111. }
  112. }
  113. nvs_type_t config_get_item_type(cJSON * entry){
  114. if(entry==NULL){
  115. ESP_LOGE(TAG,"null pointer received!");
  116. return true;
  117. }
  118. cJSON * item_type = cJSON_GetObjectItemCaseSensitive(entry, "type");
  119. if(item_type ==NULL ) {
  120. ESP_LOGE(TAG, "Item type not found! ");
  121. return 0;
  122. }
  123. ESP_LOGD(TAG,"Found item type %f",item_type->valuedouble);
  124. return item_type->valuedouble;
  125. }
  126. cJSON * config_set_value_safe(nvs_type_t nvs_type, const char *key, void * value){
  127. cJSON * entry = cJSON_CreateObject();
  128. double numvalue = 0;
  129. if(entry == NULL) {
  130. ESP_LOGE(TAG, "Unable to allocate memory for entry %s",key);
  131. return NULL;
  132. }
  133. cJSON * existing = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  134. if(existing !=NULL && nvs_type == NVS_TYPE_STR && config_get_item_type(existing) != NVS_TYPE_STR ) {
  135. ESP_LOGW(TAG, "Storing numeric value from string");
  136. numvalue = atof((char *)value);
  137. cJSON_AddNumberToObject(entry,"value", numvalue );
  138. nvs_type_t exist_type = config_get_item_type(existing);
  139. ESP_LOGW(TAG, "Stored value %f from string %s as type %d",numvalue, (char *)value,exist_type);
  140. cJSON_AddNumberToObject(entry,"type", exist_type);
  141. }
  142. else {
  143. cJSON_AddNumberToObject(entry,"type", nvs_type );
  144. switch (nvs_type) {
  145. case NVS_TYPE_I8:
  146. cJSON_AddNumberToObject(entry,"value", *(int8_t*)value );
  147. break;
  148. case NVS_TYPE_I16:
  149. cJSON_AddNumberToObject(entry,"value", *(int16_t*)value );
  150. break;
  151. case NVS_TYPE_I32:
  152. cJSON_AddNumberToObject(entry,"value", *(int32_t*)value );
  153. break;
  154. case NVS_TYPE_U8:
  155. cJSON_AddNumberToObject(entry,"value", *(uint8_t*)value );
  156. break;
  157. case NVS_TYPE_U16:
  158. cJSON_AddNumberToObject(entry,"value", *(uint16_t*)value );
  159. break;
  160. case NVS_TYPE_U32:
  161. cJSON_AddNumberToObject(entry,"value", *(uint32_t*)value );
  162. break;
  163. case NVS_TYPE_STR:
  164. cJSON_AddStringToObject(entry, "value", (char *)value);
  165. break;
  166. case NVS_TYPE_I64:
  167. case NVS_TYPE_U64:
  168. default:
  169. ESP_LOGE(TAG, "nvs type %u not supported", nvs_type);
  170. break;
  171. }
  172. }
  173. if(existing!=NULL ) {
  174. ESP_LOGV(TAG, "Changing existing entry [%s].", key);
  175. char * exist_str = cJSON_PrintUnformatted(existing);
  176. if(exist_str!=NULL){
  177. ESP_LOGV(TAG,"Existing entry: %s", exist_str);
  178. free(exist_str);
  179. }
  180. else {
  181. ESP_LOGV(TAG,"Failed to print existing entry");
  182. }
  183. // set commit flag as equal so we can compare
  184. cJSON_AddBoolToObject(entry,"chg",config_is_entry_changed(existing));
  185. if(!cJSON_Compare(entry,existing,false)){
  186. char * entry_str = cJSON_PrintUnformatted(entry);
  187. if(entry_str!=NULL){
  188. ESP_LOGD(TAG,"New config object: \n%s", entry_str );
  189. free(entry_str);
  190. }
  191. else {
  192. ESP_LOGD(TAG,"Failed to print entry");
  193. }
  194. ESP_LOGI(TAG, "Setting changed flag config [%s]", key);
  195. config_set_entry_changed_flag(entry,true);
  196. ESP_LOGI(TAG, "Updating config [%s]", key);
  197. cJSON_ReplaceItemInObject(nvs_json,key, entry);
  198. entry_str = cJSON_PrintUnformatted(entry);
  199. if(entry_str!=NULL){
  200. ESP_LOGD(TAG,"New config: %s", entry_str );
  201. free(entry_str);
  202. }
  203. else {
  204. ESP_LOGD(TAG,"Failed to print entry");
  205. }
  206. }
  207. else {
  208. ESP_LOGD(TAG, "Config not changed. ");
  209. cJSON_Delete(entry);
  210. entry = existing;
  211. }
  212. }
  213. else {
  214. // This is a new entry.
  215. config_set_entry_changed_flag(entry,true);
  216. cJSON_AddItemToObject(nvs_json, key, entry);
  217. }
  218. return entry;
  219. }
  220. nvs_type_t config_get_entry_type(cJSON * entry){
  221. if(entry==NULL){
  222. ESP_LOGE(TAG,"null pointer received!");
  223. return 0;
  224. }
  225. cJSON * entry_type = cJSON_GetObjectItemCaseSensitive(entry, "type");
  226. if(entry_type ==NULL ) {
  227. ESP_LOGE(TAG, "Entry type not found in nvs cache for existing setting.");
  228. return 0;
  229. }
  230. ESP_LOGV(TAG,"Found type %s",type_to_str(entry_type->valuedouble));
  231. return entry_type->valuedouble;
  232. }
  233. void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag){
  234. ESP_LOGV(TAG, "config_set_entry_changed_flag: begin");
  235. if(entry==NULL){
  236. ESP_LOGE(TAG,"null pointer received!");
  237. return;
  238. }
  239. bool bIsConfigLoading=((xEventGroupGetBits(config_group) & CONFIG_LOAD_BIT)!=0);
  240. bool changedFlag=bIsConfigLoading?false:flag;
  241. ESP_LOGV(TAG, "config_set_entry_changed_flag: retrieving chg flag from entry");
  242. cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
  243. if(changed ==NULL ) {
  244. ESP_LOGV(TAG, "config_set_entry_changed_flag: chg flag not found. Adding. ");
  245. cJSON_AddBoolToObject(entry,"chg",changedFlag);
  246. }
  247. else {
  248. ESP_LOGV(TAG, "config_set_entry_changed_flag: Existing change flag found. ");
  249. if(cJSON_IsTrue(changed) && changedFlag){
  250. ESP_LOGW(TAG, "Commit flag not changed!");
  251. }
  252. else{
  253. ESP_LOGV(TAG, "config_set_entry_changed_flag: Updating change flag to %s",changedFlag?"TRUE":"FALSE");
  254. changed->type = changedFlag?cJSON_True:cJSON_False ;
  255. }
  256. }
  257. if(changedFlag) {
  258. ESP_LOGV(TAG, "config_set_entry_changed_flag: Calling config_raise_change. ");
  259. config_raise_change(true);
  260. }
  261. ESP_LOGV(TAG, "config_set_entry_changed_flag: done. ");
  262. }
  263. cJSON_bool config_is_entry_changed(cJSON * entry){
  264. if(entry==NULL){
  265. ESP_LOGE(TAG,"null pointer received!");
  266. return true;
  267. }
  268. cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
  269. if(changed ==NULL ) {
  270. ESP_LOGE(TAG, "Change flag not found! ");
  271. return true;
  272. }
  273. return cJSON_IsTrue(changed);
  274. }
  275. void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry){
  276. void * value=NULL;
  277. if(entry==NULL){
  278. ESP_LOGE(TAG,"null pointer received!");
  279. }
  280. ESP_LOGV(TAG, "getting config value type %s", type_to_str(nvs_type));
  281. cJSON * entry_value = cJSON_GetObjectItemCaseSensitive(entry, "value");
  282. if(entry_value==NULL ) {
  283. char * entry_str = cJSON_PrintUnformatted(entry);
  284. if(entry_str!=NULL){
  285. ESP_LOGE(TAG, "Missing config value!. Object: \n%s", entry_str);
  286. free(entry_str);
  287. }
  288. else{
  289. ESP_LOGE(TAG, "Missing config value");
  290. }
  291. return NULL;
  292. }
  293. nvs_type_t type = config_get_entry_type(entry);
  294. if(nvs_type != type){
  295. // requested value type different than the stored type
  296. char * entry_str = cJSON_PrintUnformatted(entry);
  297. if(entry_str!=NULL){
  298. 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);
  299. free(entry_str);
  300. }
  301. else{
  302. ESP_LOGE(TAG, "Requested value type %s, found value type %s instead", type_to_str(nvs_type), type_to_str(type));
  303. }
  304. return NULL;
  305. }
  306. if (nvs_type == NVS_TYPE_I8) {
  307. value=malloc(sizeof(int8_t));
  308. *(int8_t *)value = (int8_t)entry_value->valuedouble;
  309. } else if (nvs_type == NVS_TYPE_U8) {
  310. value=malloc(sizeof(uint8_t));
  311. *(uint8_t *)value = (uint8_t)entry_value->valuedouble;
  312. } else if (nvs_type == NVS_TYPE_I16) {
  313. value=malloc(sizeof(int16_t));
  314. *(int16_t *)value = (int16_t)entry_value->valuedouble;
  315. } else if (nvs_type == NVS_TYPE_U16) {
  316. value=malloc(sizeof(uint16_t));
  317. *(uint16_t *)value = (uint16_t)entry_value->valuedouble;
  318. } else if (nvs_type == NVS_TYPE_I32) {
  319. value=malloc(sizeof(int32_t));
  320. *(int32_t *)value = (int32_t)entry_value->valuedouble;
  321. } else if (nvs_type == NVS_TYPE_U32) {
  322. value=malloc(sizeof(uint32_t));
  323. *(uint32_t *)value = (uint32_t)entry_value->valuedouble;
  324. } else if (nvs_type == NVS_TYPE_I64) {
  325. value=malloc(sizeof(int64_t));
  326. *(int64_t *)value = (int64_t)entry_value->valuedouble;
  327. } else if (nvs_type == NVS_TYPE_U64) {
  328. value=malloc(sizeof(uint64_t));
  329. *(uint64_t *)value = (uint64_t)entry_value->valuedouble;
  330. } else if (nvs_type == NVS_TYPE_STR) {
  331. if(!cJSON_IsString(entry_value)){
  332. char * entry_str = cJSON_PrintUnformatted(entry);
  333. if(entry_str!=NULL){
  334. ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d, Object: \n%s",
  335. entry_value->string,
  336. entry_value->valuestring,
  337. entry_value->type,
  338. entry_str);
  339. free(entry_str);
  340. }
  341. else {
  342. ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d",
  343. entry_value->string,
  344. entry_value->valuestring,
  345. entry_value->type);
  346. }
  347. }
  348. else {
  349. value=(void *)strdup(cJSON_GetStringValue(entry_value));
  350. if(value==NULL){
  351. char * entry_str = cJSON_PrintUnformatted(entry);
  352. if(entry_str!=NULL){
  353. ESP_LOGE(TAG, "strdup failed on value for object \n%s",entry_str);
  354. free(entry_str);
  355. }
  356. else {
  357. ESP_LOGE(TAG, "strdup failed on value");
  358. }
  359. }
  360. }
  361. } else if (nvs_type == NVS_TYPE_BLOB) {
  362. ESP_LOGE(TAG, "Unsupported type NVS_TYPE_BLOB");
  363. }
  364. return value;
  365. }
  366. void config_commit_to_nvs(){
  367. ESP_LOGI(TAG,"Committing configuration to nvs. Locking config object.");
  368. ESP_LOGV(TAG,"config_commit_to_nvs. Locking config object.");
  369. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  370. ESP_LOGE(TAG, "config_commit_to_nvs: Unable to lock config for commit ");
  371. return ;
  372. }
  373. if(nvs_json==NULL){
  374. ESP_LOGE(TAG, ": cJSON nvs cache object not set.");
  375. return;
  376. }
  377. ESP_LOGV(TAG,"config_commit_to_nvs. Config Locked!");
  378. cJSON * entry=nvs_json->child;
  379. while(entry!= NULL){
  380. char * entry_str = cJSON_PrintUnformatted(entry);
  381. if(entry_str!=NULL){
  382. ESP_LOGV(TAG,"config_commit_to_nvs processing item %s",entry_str);
  383. free(entry_str);
  384. }
  385. if(config_is_entry_changed(entry)){
  386. ESP_LOGD(TAG, "Committing entry %s value to nvs.",(entry->string==NULL)?"UNKNOWN":entry->string);
  387. nvs_type_t type = config_get_entry_type(entry);
  388. void * value = config_safe_alloc_get_entry_value(type, entry);
  389. if(value!=NULL){
  390. esp_err_t err = store_nvs_value(type,entry->string,value);
  391. free(value);
  392. if(err!=ESP_OK){
  393. char * entry_str = cJSON_PrintUnformatted(entry);
  394. if(entry_str!=NULL){
  395. ESP_LOGE(TAG, "Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
  396. free(entry_str);
  397. }
  398. else {
  399. ESP_LOGE(TAG, "Error comitting value to nvs for key %s",entry->string);
  400. }
  401. }
  402. else {
  403. config_set_entry_changed_flag(entry, false);
  404. }
  405. }
  406. else {
  407. char * entry_str = cJSON_PrintUnformatted(entry);
  408. if(entry_str!=NULL){
  409. ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
  410. free(entry_str);
  411. }
  412. else {
  413. ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s",entry->string);
  414. }
  415. }
  416. }
  417. else {
  418. ESP_LOGV(TAG,"config_commit_to_nvs. Item already committed. Ignoring.");
  419. }
  420. taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
  421. entry = entry->next;
  422. }
  423. ESP_LOGV(TAG,"config_commit_to_nvs. Resetting the global commit flag.");
  424. config_raise_change(false);
  425. ESP_LOGV(TAG,"config_commit_to_nvs. Releasing the lock object.");
  426. config_unlock();
  427. }
  428. bool config_has_changes(){
  429. return (xEventGroupGetBits(config_group) & CONFIG_NO_COMMIT_PENDING)==0;
  430. }
  431. bool wait_for_commit(){
  432. bool commit_pending=(xEventGroupGetBits(config_group) & CONFIG_NO_COMMIT_PENDING)==0;
  433. while (commit_pending){
  434. ESP_LOGW(TAG,"Waiting for config commit ...");
  435. commit_pending = (xEventGroupWaitBits(config_group, CONFIG_NO_COMMIT_PENDING,pdFALSE, pdTRUE, (CONFIG_COMMIT_DELAY*2) / portTICK_PERIOD_MS) & CONFIG_NO_COMMIT_PENDING)==0;
  436. if(commit_pending){
  437. ESP_LOGW(TAG,"Timeout waiting for config commit.");
  438. }
  439. else {
  440. ESP_LOGI(TAG,"Config committed!");
  441. }
  442. }
  443. return !commit_pending;
  444. }
  445. bool config_lock(TickType_t xTicksToWait) {
  446. ESP_LOGV(TAG, "Locking config json object");
  447. if( xSemaphoreTake( config_mutex, xTicksToWait ) == pdTRUE ) {
  448. ESP_LOGV(TAG, "config Json object locked!");
  449. return true;
  450. }
  451. else {
  452. ESP_LOGE(TAG, "Semaphore take failed. Unable to lock config Json object mutex");
  453. return false;
  454. }
  455. }
  456. void config_unlock() {
  457. ESP_LOGV(TAG, "Unlocking json buffer!");
  458. xSemaphoreGive( config_mutex );
  459. }
  460. static void vCallbackFunction( TimerHandle_t xTimer ) {
  461. static int cnt=0;
  462. if(config_has_changes()){
  463. ESP_LOGI(TAG, "configuration has some uncommitted entries");
  464. config_commit_to_nvs();
  465. }
  466. else{
  467. if(++cnt>=15){
  468. ESP_LOGV(TAG,"commit timer: commit flag not set");
  469. cnt=0;
  470. }
  471. }
  472. xTimerReset( xTimer, 10 );
  473. }
  474. void config_raise_change(bool change_found){
  475. if(config_set_group_bit(CONFIG_NO_COMMIT_PENDING,!change_found))
  476. {
  477. ESP_LOGD(TAG,"Config commit set to %s",change_found?"Pending Commit":"Committed");
  478. }
  479. }
  480. bool config_set_group_bit(int bit_num,bool flag){
  481. bool result = true;
  482. int curFlags=xEventGroupGetBits(config_group);
  483. if((curFlags & CONFIG_LOAD_BIT) && bit_num == CONFIG_NO_COMMIT_PENDING ){
  484. ESP_LOGD(TAG,"Loading config, ignoring changes");
  485. result = false;
  486. }
  487. if(result){
  488. bool curBit=(xEventGroupGetBits(config_group) & bit_num);
  489. if(curBit == flag){
  490. ESP_LOGV(TAG,"Flag %d already %s", bit_num, flag?"Set":"Cleared");
  491. result = false;
  492. }
  493. }
  494. if(result){
  495. ESP_LOGV(TAG,"%s Flag %d ", flag?"Setting":"Clearing",bit_num);
  496. if(!flag){
  497. xEventGroupClearBits(config_group, bit_num);
  498. }
  499. else {
  500. xEventGroupSetBits(config_group, bit_num);
  501. }
  502. }
  503. return result;
  504. }
  505. void config_set_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size) {
  506. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  507. ESP_LOGE(TAG, "Unable to lock config");
  508. return;
  509. }
  510. ESP_LOGV(TAG, "Checking if key %s exists in nvs cache for type %s.", key,type_to_str(type));
  511. cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  512. if(entry !=NULL){
  513. ESP_LOGV(TAG, "Entry found.");
  514. }
  515. else {
  516. // Value was not found
  517. ESP_LOGW(TAG, "Adding default value for [%s].", key);
  518. entry=config_set_value_safe(type, key, default_value);
  519. if(entry == NULL){
  520. ESP_LOGE(TAG, "Failed to add value to cache!");
  521. }
  522. char * entry_str = cJSON_PrintUnformatted(entry);
  523. if(entry_str!=NULL){
  524. ESP_LOGD(TAG, "Value added to default for object: \n%s",entry_str);
  525. free(entry_str);
  526. }
  527. }
  528. config_unlock();
  529. }
  530. void config_delete_key(const char *key){
  531. nvs_handle nvs;
  532. ESP_LOGD(TAG, "Deleting nvs entry for [%s]", key);
  533. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  534. ESP_LOGE(TAG, "Unable to lock config for delete");
  535. return false;
  536. }
  537. esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  538. if (err == ESP_OK) {
  539. err = nvs_erase_key(nvs, key);
  540. if (err == ESP_OK) {
  541. ESP_LOGD(TAG, "key [%s] erased from nvs.",key);
  542. err = nvs_commit(nvs);
  543. if (err == ESP_OK) {
  544. ESP_LOGD(TAG, "nvs erase committed.");
  545. }
  546. else {
  547. ESP_LOGE(TAG, "Unable to commit nvs erase operation for key [%s]. %s.",key,esp_err_to_name(err));
  548. }
  549. }
  550. else {
  551. ESP_LOGE(TAG, "Unable to delete nvs key [%s]. %s. ",key, esp_err_to_name(err));
  552. }
  553. nvs_close(nvs);
  554. }
  555. else {
  556. ESP_LOGE(TAG, "Error opening nvs: %s. Unable to delete nvs key [%s].",esp_err_to_name(err),key);
  557. }
  558. char * struc_str = cJSON_PrintUnformatted(nvs_json);
  559. if(struc_str!=NULL){
  560. ESP_LOGV(TAG, "Structure before delete \n%s", struc_str);
  561. free(struc_str);
  562. }
  563. cJSON * entry = cJSON_DetachItemFromObjectCaseSensitive(nvs_json, key);
  564. if(entry !=NULL){
  565. ESP_LOGI(TAG, "Removing config key [%s]", entry->string);
  566. cJSON_Delete(entry);
  567. struc_str = cJSON_PrintUnformatted(nvs_json);
  568. if(struc_str!=NULL){
  569. ESP_LOGV(TAG, "Structure after delete \n%s", struc_str);
  570. free(struc_str);
  571. }
  572. }
  573. else {
  574. ESP_LOGW(TAG, "Unable to remove config key [%s]: not found.", key);
  575. }
  576. config_unlock();
  577. }
  578. void * config_alloc_get(nvs_type_t nvs_type, const char *key) {
  579. return config_alloc_get_default(nvs_type, key, NULL, 0);
  580. }
  581. void * config_alloc_get_str(const char *key, char *lead, char *fallback) {
  582. if (lead && *lead) return strdup(lead);
  583. char *value = config_alloc_get_default(NVS_TYPE_STR, key, NULL, 0);
  584. if ((!value || !*value) && fallback) {
  585. if (value) free(value);
  586. value = strdup(fallback);
  587. }
  588. return value;
  589. }
  590. void * config_alloc_get_default(nvs_type_t nvs_type, const char *key, void * default_value, size_t blob_size) {
  591. void * value = NULL;
  592. ESP_LOGV(TAG, "Retrieving key %s from nvs cache for type %s.", key,type_to_str(nvs_type));
  593. if(nvs_json==NULL){
  594. ESP_LOGE(TAG,"configuration not loaded!");
  595. return value;
  596. }
  597. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  598. ESP_LOGE(TAG, "Unable to lock config");
  599. return value;
  600. }
  601. ESP_LOGD(TAG,"Getting config entry for key %s",key);
  602. cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  603. if(entry !=NULL){
  604. ESP_LOGV(TAG, "Entry found, getting value.");
  605. value = config_safe_alloc_get_entry_value(nvs_type, entry);
  606. }
  607. else if(default_value!=NULL){
  608. // Value was not found
  609. ESP_LOGW(TAG, "Adding new config value for key [%s]",key);
  610. entry=config_set_value_safe(nvs_type, key, default_value);
  611. if(entry == NULL){
  612. ESP_LOGE(TAG, "Failed to add value to cache");
  613. }
  614. else {
  615. char * entry_str = cJSON_PrintUnformatted(entry);
  616. if(entry_str!=NULL){
  617. ESP_LOGV(TAG, "Value added configuration object for key [%s]: \n%s", entry->string,entry_str);
  618. free(entry_str);
  619. }
  620. else {
  621. ESP_LOGV(TAG, "Value added configuration object for key [%s]", entry->string);
  622. }
  623. value = config_safe_alloc_get_entry_value(nvs_type, entry);
  624. }
  625. }
  626. else{
  627. ESP_LOGW(TAG,"Value not found for key %s",key);
  628. }
  629. config_unlock();
  630. return value;
  631. }
  632. char * config_alloc_get_json(bool bFormatted){
  633. char * json_buffer = NULL;
  634. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  635. ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
  636. return strdup("{\"error\":\"Unable to lock configuration object.\"}");
  637. }
  638. if(bFormatted){
  639. json_buffer= cJSON_Print(nvs_json);
  640. }
  641. else {
  642. json_buffer= cJSON_PrintUnformatted(nvs_json);
  643. }
  644. config_unlock();
  645. return json_buffer;
  646. }
  647. esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
  648. esp_err_t result = ESP_OK;
  649. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  650. ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
  651. result = ESP_FAIL;
  652. }
  653. cJSON * entry = config_set_value_safe(nvs_type, key, value);
  654. if(entry == NULL){
  655. result = ESP_FAIL;
  656. }
  657. else{
  658. char * entry_str = cJSON_PrintUnformatted(entry);
  659. if(entry_str!=NULL){
  660. ESP_LOGV(TAG,"config_set_value result: \n%s",entry_str);
  661. free(entry_str);
  662. }
  663. else {
  664. ESP_LOGV(TAG,"config_set_value completed");
  665. }
  666. }
  667. config_unlock();
  668. return result;
  669. }
  670. IMPLEMENT_SET_DEFAULT(uint8_t,NVS_TYPE_U8);
  671. IMPLEMENT_SET_DEFAULT(int8_t,NVS_TYPE_I8);
  672. IMPLEMENT_SET_DEFAULT(uint16_t,NVS_TYPE_U16);
  673. IMPLEMENT_SET_DEFAULT(int16_t,NVS_TYPE_I16);
  674. IMPLEMENT_SET_DEFAULT(uint32_t,NVS_TYPE_U32);
  675. IMPLEMENT_SET_DEFAULT(int32_t,NVS_TYPE_I32);
  676. IMPLEMENT_GET_NUM(uint8_t,NVS_TYPE_U8);
  677. IMPLEMENT_GET_NUM(int8_t,NVS_TYPE_I8);
  678. IMPLEMENT_GET_NUM(uint16_t,NVS_TYPE_U16);
  679. IMPLEMENT_GET_NUM(int16_t,NVS_TYPE_I16);
  680. IMPLEMENT_GET_NUM(uint32_t,NVS_TYPE_U32);
  681. IMPLEMENT_GET_NUM(int32_t,NVS_TYPE_I32);