2
0

config.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. cJSON * nvs_json=NULL;
  45. TimerHandle_t timer;
  46. SemaphoreHandle_t config_mutex = NULL;
  47. EventGroupHandle_t config_group;
  48. /* @brief indicate that the ESP32 is currently connected. */
  49. const int CONFIG_PENDING_CHANGE_BIT = BIT0;
  50. 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. void config_init(){
  61. ESP_LOGD(TAG, "Creating mutex for Config");
  62. config_mutex = xSemaphoreCreateMutex();
  63. ESP_LOGD(TAG, "Creating event group");
  64. config_group = xEventGroupCreate();
  65. ESP_LOGD(TAG, "Loading config from nvs");
  66. if(nvs_json !=NULL){
  67. cJSON_Delete(nvs_json);
  68. }
  69. nvs_json = cJSON_CreateObject();
  70. config_set_group_bit(CONFIG_LOAD_BIT,true);
  71. nvs_load_config();
  72. config_set_group_bit(CONFIG_LOAD_BIT,false);
  73. config_start_timer();
  74. }
  75. void config_start_timer(){
  76. ESP_LOGD(TAG, "Starting config timer");
  77. timer = xTimerCreate("configTimer", CONFIG_COMMIT_DELAY / portTICK_RATE_MS, pdFALSE, NULL, vCallbackFunction);
  78. if( xTimerStart( timer , CONFIG_COMMIT_DELAY/ portTICK_RATE_MS ) != pdPASS ) {
  79. ESP_LOGE(TAG, "config commitment timer failed to start.");
  80. }
  81. }
  82. cJSON * config_set_value_safe(nvs_type_t nvs_type, const char *key, void * value){
  83. char * num_buffer = NULL;
  84. num_buffer = malloc(NUM_BUFFER_LEN);
  85. memset(num_buffer,0x00,NUM_BUFFER_LEN);
  86. cJSON * entry = cJSON_CreateObject();
  87. if(entry == NULL) {
  88. ESP_LOGE(TAG, "Unable to allocate memory for entry %s",key);
  89. return NULL;
  90. }
  91. cJSON_AddNumberToObject(entry,"type", nvs_type );
  92. switch (nvs_type) {
  93. case NVS_TYPE_I8:
  94. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int8_t*)value);
  95. cJSON_AddNumberToObject(entry,"value", *(int8_t*)value );
  96. break;
  97. case NVS_TYPE_I16:
  98. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int16_t*)value);
  99. cJSON_AddNumberToObject(entry,"value", *(int16_t*)value );
  100. break;
  101. case NVS_TYPE_I32:
  102. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%i", *(int32_t*)value);
  103. cJSON_AddNumberToObject(entry,"value", *(int32_t*)value );
  104. break;
  105. case NVS_TYPE_U8:
  106. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint8_t*)value);
  107. cJSON_AddNumberToObject(entry,"value", *(uint8_t*)value );
  108. break;
  109. case NVS_TYPE_U16:
  110. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint16_t*)value);
  111. cJSON_AddNumberToObject(entry,"value", *(uint16_t*)value );
  112. break;
  113. case NVS_TYPE_U32:
  114. snprintf(num_buffer, NUM_BUFFER_LEN-1, "%u", *(uint32_t*)value);
  115. cJSON_AddNumberToObject(entry,"value", *(uint32_t*)value );
  116. break;
  117. case NVS_TYPE_STR:
  118. cJSON_AddStringToObject(entry, "value", (char *)value);
  119. break;
  120. case NVS_TYPE_I64:
  121. case NVS_TYPE_U64:
  122. default:
  123. ESP_LOGE(TAG, "nvs type %u not supported", nvs_type);
  124. break;
  125. }
  126. cJSON * existing = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  127. if(existing!=NULL ) {
  128. ESP_LOGV(TAG, "Changing existing entry [%s].", key);
  129. char * exist_str = cJSON_PrintUnformatted(existing);
  130. if(exist_str!=NULL){
  131. ESP_LOGV(TAG,"Existing entry: %s", exist_str);
  132. free(exist_str);
  133. }
  134. else {
  135. ESP_LOGV(TAG,"Failed to print existing entry");
  136. }
  137. // set commit flag as equal so we can compare
  138. cJSON * chg_flag =cJSON_AddBoolToObject(entry,"chg",config_is_entry_changed(existing));
  139. if(!cJSON_Compare(entry,existing,false)){
  140. char * entry_str = cJSON_PrintUnformatted(entry);
  141. if(entry_str!=NULL){
  142. ESP_LOGD(TAG,"New config object: \n%s", entry_str );
  143. free(entry_str);
  144. }
  145. else {
  146. ESP_LOGD(TAG,"Failed to print entry");
  147. }
  148. cJSON_Delete(chg_flag);
  149. ESP_LOGI(TAG, "Setting changed flag config [%s]", key);
  150. config_set_entry_changed_flag(entry,true);
  151. ESP_LOGI(TAG, "Updating config [%s]", key);
  152. cJSON_ReplaceItemInObject(nvs_json,key, entry);
  153. entry_str = cJSON_PrintUnformatted(entry);
  154. if(entry_str!=NULL){
  155. ESP_LOGD(TAG,"New config: %s", entry_str );
  156. free(entry_str);
  157. }
  158. else {
  159. ESP_LOGD(TAG,"Failed to print entry");
  160. }
  161. }
  162. else {
  163. ESP_LOGD(TAG, "Config not changed. ");
  164. }
  165. }
  166. else {
  167. // This is a new entry.
  168. config_set_entry_changed_flag(entry,true);
  169. cJSON_AddItemToObject(nvs_json, key, entry);
  170. }
  171. free(num_buffer);
  172. return entry;
  173. }
  174. nvs_type_t config_get_entry_type(cJSON * entry){
  175. if(entry==NULL){
  176. ESP_LOGE(TAG,"null pointer received!");
  177. return 0;
  178. }
  179. cJSON * entry_type = cJSON_GetObjectItemCaseSensitive(entry, "type");
  180. if(entry_type ==NULL ) {
  181. ESP_LOGE(TAG, "Entry type not found in nvs cache for existing setting.");
  182. return 0;
  183. }
  184. return entry_type->valuedouble;
  185. }
  186. void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag){
  187. if(entry==NULL){
  188. ESP_LOGE(TAG,"null pointer received!");
  189. return;
  190. }
  191. bool bIsConfigLoading=((xEventGroupGetBits(config_group) & CONFIG_LOAD_BIT)!=0);
  192. bool changedFlag=bIsConfigLoading?false:flag;
  193. cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
  194. if(changed ==NULL ) {
  195. ESP_LOGV(TAG, "Adding change flag. ");
  196. cJSON_AddBoolToObject(entry,"chg",changedFlag);
  197. }
  198. else {
  199. if(cJSON_IsTrue(changed) && changedFlag){
  200. ESP_LOGW(TAG, "Commit flag not changed!");
  201. }
  202. else{
  203. ESP_LOGV(TAG, "Updating change flag to %s",changedFlag?"TRUE":"FALSE");
  204. cJSON_Delete(changed);
  205. cJSON_AddBoolToObject(entry,"chg",changedFlag);
  206. }
  207. }
  208. if(changedFlag) config_raise_change(true);
  209. }
  210. cJSON_bool config_is_entry_changed(cJSON * entry){
  211. if(entry==NULL){
  212. ESP_LOGE(TAG,"null pointer received!");
  213. return true;
  214. }
  215. cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
  216. if(changed ==NULL ) {
  217. ESP_LOGE(TAG, "Change flag not found! ");
  218. return true;
  219. }
  220. return cJSON_IsTrue(changed);
  221. }
  222. void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry){
  223. void * value=NULL;
  224. if(entry==NULL){
  225. ESP_LOGE(TAG,"null pointer received!");
  226. }
  227. ESP_LOGV(TAG, "getting config value type %s", type_to_str(nvs_type));
  228. cJSON * entry_value = cJSON_GetObjectItemCaseSensitive(entry, "value");
  229. if(entry_value==NULL ) {
  230. char * entry_str = cJSON_PrintUnformatted(entry);
  231. if(entry_str!=NULL){
  232. ESP_LOGE(TAG, "Missing config value!. Object: \n%s", entry_str);
  233. free(entry_str);
  234. }
  235. else{
  236. ESP_LOGE(TAG, "Missing config value");
  237. }
  238. return NULL;
  239. }
  240. nvs_type_t type = config_get_entry_type(entry);
  241. if(nvs_type != type){
  242. // requested value type different than the stored type
  243. char * entry_str = cJSON_PrintUnformatted(entry);
  244. if(entry_str!=NULL){
  245. 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);
  246. free(entry_str);
  247. }
  248. else{
  249. ESP_LOGE(TAG, "Requested value type %s, found value type %s instead", type_to_str(nvs_type), type_to_str(type));
  250. }
  251. return NULL;
  252. }
  253. if (nvs_type == NVS_TYPE_I8) {
  254. value=malloc(sizeof(int8_t));
  255. *(int8_t *)value = (int8_t)entry_value->valuedouble;
  256. } else if (nvs_type == NVS_TYPE_U8) {
  257. value=malloc(sizeof(uint8_t));
  258. *(uint8_t *)value = (uint8_t)entry_value->valuedouble;
  259. } else if (nvs_type == NVS_TYPE_I16) {
  260. value=malloc(sizeof(int16_t));
  261. *(int16_t *)value = (int16_t)entry_value->valuedouble;
  262. } else if (nvs_type == NVS_TYPE_U16) {
  263. value=malloc(sizeof(uint16_t));
  264. *(uint16_t *)value = (uint16_t)entry_value->valuedouble;
  265. } else if (nvs_type == NVS_TYPE_I32) {
  266. value=malloc(sizeof(int32_t));
  267. *(int32_t *)value = (int32_t)entry_value->valuedouble;
  268. } else if (nvs_type == NVS_TYPE_U32) {
  269. value=malloc(sizeof(uint32_t));
  270. *(uint32_t *)value = (uint32_t)entry_value->valuedouble;
  271. } else if (nvs_type == NVS_TYPE_I64) {
  272. value=malloc(sizeof(int64_t));
  273. *(int64_t *)value = (int64_t)entry_value->valuedouble;
  274. } else if (nvs_type == NVS_TYPE_U64) {
  275. value=malloc(sizeof(uint64_t));
  276. *(uint64_t *)value = (uint64_t)entry_value->valuedouble;
  277. } else if (nvs_type == NVS_TYPE_STR) {
  278. if(!cJSON_IsString(entry_value)){
  279. char * entry_str = cJSON_PrintUnformatted(entry);
  280. if(entry_str!=NULL){
  281. ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d, Object: \n%s",
  282. entry_value->string,
  283. entry_value->valuestring,
  284. entry_value->type,
  285. entry_str);
  286. free(entry_str);
  287. }
  288. else {
  289. ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d",
  290. entry_value->string,
  291. entry_value->valuestring,
  292. entry_value->type);
  293. }
  294. }
  295. else {
  296. value=(void *)strdup(cJSON_GetStringValue(entry_value));
  297. if(value==NULL){
  298. char * entry_str = cJSON_PrintUnformatted(entry);
  299. if(entry_str!=NULL){
  300. ESP_LOGE(TAG, "strdup failed on value for object \n%s",entry_str);
  301. free(entry_str);
  302. }
  303. else {
  304. ESP_LOGE(TAG, "strdup failed on value");
  305. }
  306. }
  307. }
  308. } else if (nvs_type == NVS_TYPE_BLOB) {
  309. ESP_LOGE(TAG, "Unsupported type NVS_TYPE_BLOB");
  310. }
  311. return value;
  312. }
  313. void config_commit_to_nvs(){
  314. ESP_LOGI(TAG,"Committing configuration to nvs. Locking config object.");
  315. ESP_LOGV(TAG,"config_commit_to_nvs. Locking config object.");
  316. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  317. ESP_LOGE(TAG, "config_commit_to_nvs: Unable to lock config for commit ");
  318. return ;
  319. }
  320. if(nvs_json==NULL){
  321. ESP_LOGE(TAG, ": cJSON nvs cache object not set.");
  322. return;
  323. }
  324. ESP_LOGV(TAG,"config_commit_to_nvs. Config Locked!");
  325. cJSON * entry=nvs_json->child;
  326. while(entry!= NULL){
  327. char * entry_str = cJSON_PrintUnformatted(entry);
  328. if(entry_str!=NULL){
  329. ESP_LOGV(TAG,"config_commit_to_nvs processing item %s",entry_str);
  330. free(entry_str);
  331. }
  332. if(config_is_entry_changed(entry)){
  333. ESP_LOGD(TAG, "Committing entry %s value to nvs.",(entry->string==NULL)?"UNKNOWN":entry->string);
  334. nvs_type_t type = config_get_entry_type(entry);
  335. void * value = config_safe_alloc_get_entry_value(type, entry);
  336. if(value!=NULL){
  337. esp_err_t err = store_nvs_value(type,entry->string,value);
  338. free(value);
  339. if(err!=ESP_OK){
  340. char * entry_str = cJSON_PrintUnformatted(entry);
  341. if(entry_str!=NULL){
  342. ESP_LOGE(TAG, "Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
  343. free(entry_str);
  344. }
  345. else {
  346. ESP_LOGE(TAG, "Error comitting value to nvs for key %s",entry->string);
  347. }
  348. }
  349. else {
  350. config_set_entry_changed_flag(entry, false);
  351. }
  352. }
  353. else {
  354. char * entry_str = cJSON_PrintUnformatted(entry);
  355. if(entry_str!=NULL){
  356. ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
  357. free(entry_str);
  358. }
  359. else {
  360. ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s",entry->string);
  361. }
  362. }
  363. }
  364. else {
  365. ESP_LOGV(TAG,"config_commit_to_nvs. Item already committed. Ignoring.");
  366. }
  367. taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
  368. entry = entry->next;
  369. }
  370. ESP_LOGV(TAG,"config_commit_to_nvs. Resetting the global commit flag.");
  371. config_raise_change(false);
  372. ESP_LOGV(TAG,"config_commit_to_nvs. Releasing the lock object.");
  373. config_unlock();
  374. }
  375. bool config_has_changes(){
  376. return (xEventGroupGetBits(config_group) & CONFIG_PENDING_CHANGE_BIT)!=0;
  377. }
  378. bool wait_for_commit(){
  379. bool needs_commit=(xEventGroupGetBits(config_group) & CONFIG_PENDING_CHANGE_BIT)!=0;
  380. if(needs_commit){
  381. ESP_LOGD(TAG,"Waiting for config commit ...");
  382. needs_commit = (xEventGroupWaitBits(config_group, CONFIG_PENDING_CHANGE_BIT,pdFALSE, pdTRUE, (CONFIG_COMMIT_DELAY*5) / portTICK_PERIOD_MS) & CONFIG_PENDING_CHANGE_BIT)!=0;
  383. if(needs_commit){
  384. ESP_LOGE(TAG,"Timeout waiting for config commit.");
  385. }
  386. else
  387. {
  388. ESP_LOGI(TAG,"Config committed!");
  389. }
  390. }
  391. return needs_commit;
  392. }
  393. bool config_lock(TickType_t xTicksToWait) {
  394. ESP_LOGV(TAG, "Locking config json object");
  395. if( xSemaphoreTake( config_mutex, xTicksToWait ) == pdTRUE ) {
  396. ESP_LOGV(TAG, "config Json object locked!");
  397. return true;
  398. }
  399. else {
  400. ESP_LOGE(TAG, "Semaphore take failed. Unable to lock config Json object mutex");
  401. return false;
  402. }
  403. }
  404. void config_unlock() {
  405. ESP_LOGV(TAG, "Unlocking json buffer!");
  406. xSemaphoreGive( config_mutex );
  407. }
  408. static void vCallbackFunction( TimerHandle_t xTimer ) {
  409. if(config_has_changes()){
  410. ESP_LOGI(TAG, "configuration has some uncommitted entries");
  411. config_commit_to_nvs();
  412. }
  413. else{
  414. ESP_LOGV(TAG,"commit timer: commit flag not set");
  415. }
  416. xTimerReset( xTimer, 10 );
  417. }
  418. void config_raise_change(bool flag){
  419. if(config_set_group_bit(CONFIG_PENDING_CHANGE_BIT,flag))
  420. {
  421. ESP_LOGD(TAG,"Config change indicator was %s",flag?"Set":"Cleared");
  422. }
  423. }
  424. bool config_set_group_bit(int bit_num,bool flag){
  425. bool result = true;
  426. int curFlags=xEventGroupGetBits(config_group);
  427. if((curFlags & CONFIG_LOAD_BIT) && bit_num == CONFIG_PENDING_CHANGE_BIT ){
  428. ESP_LOGD(TAG,"Loading config, ignoring changes");
  429. result = false;
  430. }
  431. if(result){
  432. bool curBit=(xEventGroupGetBits(config_group) & bit_num);
  433. if(curBit == flag){
  434. ESP_LOGV(TAG,"Flag %d already %s", bit_num, flag?"Set":"Cleared");
  435. result = false;
  436. }
  437. }
  438. if(result){
  439. ESP_LOGV(TAG,"%s Flag %d ", flag?"Setting":"Clearing",bit_num);
  440. if(!flag){
  441. xEventGroupClearBits(config_group, bit_num);
  442. }
  443. else {
  444. xEventGroupSetBits(config_group, bit_num);
  445. }
  446. }
  447. return result;
  448. }
  449. void config_set_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size) {
  450. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  451. ESP_LOGE(TAG, "Unable to lock config");
  452. return;
  453. }
  454. ESP_LOGV(TAG, "Checking if key %s exists in nvs cache for type %s.", key,type_to_str(type));
  455. cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  456. if(entry !=NULL){
  457. ESP_LOGV(TAG, "Entry found.");
  458. }
  459. else {
  460. // Value was not found
  461. ESP_LOGW(TAG, "Adding default value for [%s].", key);
  462. entry=config_set_value_safe(type, key, default_value);
  463. if(entry == NULL){
  464. ESP_LOGE(TAG, "Failed to add value to cache!");
  465. }
  466. char * entry_str = cJSON_PrintUnformatted(entry);
  467. if(entry_str!=NULL){
  468. ESP_LOGD(TAG, "Value added to default for object: \n%s",entry_str);
  469. free(entry_str);
  470. }
  471. }
  472. config_unlock();
  473. }
  474. void config_delete_key(const char *key){
  475. nvs_handle nvs;
  476. ESP_LOGD(TAG, "Deleting nvs entry for [%s]", key);
  477. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  478. ESP_LOGE(TAG, "Unable to lock config for delete");
  479. return false;
  480. }
  481. esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  482. if (err == ESP_OK) {
  483. err = nvs_erase_key(nvs, key);
  484. if (err == ESP_OK) {
  485. ESP_LOGD(TAG, "key [%s] erased from nvs.",key);
  486. err = nvs_commit(nvs);
  487. if (err == ESP_OK) {
  488. ESP_LOGD(TAG, "nvs erase committed.");
  489. }
  490. else {
  491. ESP_LOGE(TAG, "Unable to commit nvs erase operation for key [%s]. %s.",key,esp_err_to_name(err));
  492. }
  493. }
  494. else {
  495. ESP_LOGE(TAG, "Unable to delete nvs key [%s]. %s. ",key, esp_err_to_name(err));
  496. }
  497. nvs_close(nvs);
  498. }
  499. else {
  500. ESP_LOGE(TAG, "Error opening nvs: %s. Unable to delete nvs key [%s].",esp_err_to_name(err),key);
  501. }
  502. cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  503. if(entry !=NULL){
  504. ESP_LOGI(TAG, "Removing config key [%s]", entry->string);
  505. cJSON_Delete(entry);
  506. }
  507. else {
  508. ESP_LOGW(TAG, "Unable to remove config key [%s]: not found.", key);
  509. }
  510. config_unlock();
  511. }
  512. void * config_alloc_get(nvs_type_t nvs_type, const char *key) {
  513. return config_alloc_get_default(nvs_type, key, NULL, 0);
  514. }
  515. void * config_alloc_get_default(nvs_type_t nvs_type, const char *key, void * default_value, size_t blob_size) {
  516. void * value = NULL;
  517. ESP_LOGV(TAG, "Retrieving key %s from nvs cache for type %s.", key,type_to_str(nvs_type));
  518. if(nvs_json==NULL){
  519. ESP_LOGE(TAG,"configuration not loaded!");
  520. return value;
  521. }
  522. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  523. ESP_LOGE(TAG, "Unable to lock config");
  524. return value;
  525. }
  526. ESP_LOGD(TAG,"Getting config entry for key %s",key);
  527. cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  528. if(entry !=NULL){
  529. ESP_LOGV(TAG, "Entry found, getting value.");
  530. value = config_safe_alloc_get_entry_value(nvs_type, entry);
  531. }
  532. else if(default_value!=NULL){
  533. // Value was not found
  534. ESP_LOGW(TAG, "Adding new config value for key [%s]",key);
  535. entry=config_set_value_safe(nvs_type, key, default_value);
  536. if(entry == NULL){
  537. ESP_LOGE(TAG, "Failed to add value to cache");
  538. }
  539. else {
  540. char * entry_str = cJSON_PrintUnformatted(entry);
  541. if(entry_str!=NULL){
  542. ESP_LOGV(TAG, "Value added configuration object for key [%s]: \n%s", entry->string,entry_str);
  543. free(entry_str);
  544. }
  545. else {
  546. ESP_LOGV(TAG, "Value added configuration object for key [%s]", entry->string);
  547. }
  548. value = config_safe_alloc_get_entry_value(nvs_type, entry);
  549. }
  550. }
  551. else{
  552. ESP_LOGW(TAG,"Value not found for key %s",key);
  553. }
  554. config_unlock();
  555. return value;
  556. }
  557. char * config_alloc_get_json(bool bFormatted){
  558. char * json_buffer = NULL;
  559. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  560. ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
  561. return strdup("{\"error\":\"Unable to lock configuration object.\"}");
  562. }
  563. if(bFormatted){
  564. json_buffer= cJSON_Print(nvs_json);
  565. }
  566. else {
  567. json_buffer= cJSON_PrintUnformatted(nvs_json);
  568. }
  569. config_unlock();
  570. return json_buffer;
  571. }
  572. esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
  573. esp_err_t result = ESP_OK;
  574. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  575. ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
  576. result = ESP_FAIL;
  577. }
  578. cJSON * entry = config_set_value_safe(nvs_type, key, value);
  579. if(entry == NULL){
  580. result = ESP_FAIL;
  581. }
  582. else{
  583. char * entry_str = cJSON_PrintUnformatted(entry);
  584. if(entry_str!=NULL){
  585. ESP_LOGV(TAG,"config_set_value result: \n%s",entry_str);
  586. free(entry_str);
  587. }
  588. else {
  589. ESP_LOGV(TAG,"config_set_value completed");
  590. }
  591. }
  592. config_unlock();
  593. return result;
  594. }