config.c 22 KB

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