config.c 21 KB

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