config.c 22 KB

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