config.c 20 KB

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