config.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. #if RECOVERY_APPLICATION==0
  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. heap_caps_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. #if RECOVERY_APPLICATION==0
  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. cJSON_Delete(entry);
  218. entry = existing;
  219. }
  220. }
  221. else {
  222. // This is a new entry.
  223. config_set_entry_changed_flag(entry,true);
  224. cJSON_AddItemToObject(nvs_json, key, entry);
  225. }
  226. return entry;
  227. }
  228. nvs_type_t config_get_entry_type(cJSON * entry){
  229. if(entry==NULL){
  230. ESP_LOGE(TAG,"null pointer received!");
  231. return 0;
  232. }
  233. cJSON * entry_type = cJSON_GetObjectItemCaseSensitive(entry, "type");
  234. if(entry_type ==NULL ) {
  235. ESP_LOGE(TAG, "Entry type not found in nvs cache for existing setting.");
  236. return 0;
  237. }
  238. ESP_LOGV(TAG,"Found type %s",type_to_str(entry_type->valuedouble));
  239. return entry_type->valuedouble;
  240. }
  241. void config_set_entry_changed_flag(cJSON * entry, cJSON_bool flag){
  242. ESP_LOGV(TAG, "config_set_entry_changed_flag: begin");
  243. if(entry==NULL){
  244. ESP_LOGE(TAG,"null pointer received!");
  245. return;
  246. }
  247. bool bIsConfigLoading=((xEventGroupGetBits(config_group) & CONFIG_LOAD_BIT)!=0);
  248. bool changedFlag=bIsConfigLoading?false:flag;
  249. ESP_LOGV(TAG, "config_set_entry_changed_flag: retrieving chg flag from entry");
  250. cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
  251. if(changed ==NULL ) {
  252. ESP_LOGV(TAG, "config_set_entry_changed_flag: chg flag not found. Adding. ");
  253. cJSON_AddBoolToObject(entry,"chg",changedFlag);
  254. }
  255. else {
  256. ESP_LOGV(TAG, "config_set_entry_changed_flag: Existing change flag found. ");
  257. if(cJSON_IsTrue(changed) && changedFlag){
  258. ESP_LOGW(TAG, "Commit flag not changed!");
  259. }
  260. else{
  261. ESP_LOGV(TAG, "config_set_entry_changed_flag: Updating change flag to %s",changedFlag?"TRUE":"FALSE");
  262. changed->type = changedFlag?cJSON_True:cJSON_False ;
  263. }
  264. }
  265. if(changedFlag) {
  266. ESP_LOGV(TAG, "config_set_entry_changed_flag: Calling config_raise_change. ");
  267. config_raise_change(true);
  268. }
  269. ESP_LOGV(TAG, "config_set_entry_changed_flag: done. ");
  270. }
  271. cJSON_bool config_is_entry_changed(cJSON * entry){
  272. if(entry==NULL){
  273. ESP_LOGE(TAG,"null pointer received!");
  274. return true;
  275. }
  276. cJSON * changed = cJSON_GetObjectItemCaseSensitive(entry, "chg");
  277. if(changed ==NULL ) {
  278. ESP_LOGE(TAG, "Change flag not found! ");
  279. return true;
  280. }
  281. return cJSON_IsTrue(changed);
  282. }
  283. void * config_safe_alloc_get_entry_value(nvs_type_t nvs_type, cJSON * entry){
  284. void * value=NULL;
  285. if(entry==NULL){
  286. ESP_LOGE(TAG,"null pointer received!");
  287. }
  288. ESP_LOGV(TAG, "getting config value type %s", type_to_str(nvs_type));
  289. cJSON * entry_value = cJSON_GetObjectItemCaseSensitive(entry, "value");
  290. if(entry_value==NULL ) {
  291. char * entry_str = cJSON_PrintUnformatted(entry);
  292. if(entry_str!=NULL){
  293. ESP_LOGE(TAG, "Missing config value!. Object: \n%s", entry_str);
  294. free(entry_str);
  295. }
  296. else{
  297. ESP_LOGE(TAG, "Missing config value");
  298. }
  299. return NULL;
  300. }
  301. nvs_type_t type = config_get_entry_type(entry);
  302. if(nvs_type != type){
  303. // requested value type different than the stored type
  304. char * entry_str = cJSON_PrintUnformatted(entry);
  305. if(entry_str!=NULL){
  306. 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);
  307. free(entry_str);
  308. }
  309. else{
  310. ESP_LOGE(TAG, "Requested value type %s, found value type %s instead", type_to_str(nvs_type), type_to_str(type));
  311. }
  312. return NULL;
  313. }
  314. if (nvs_type == NVS_TYPE_I8) {
  315. value=malloc(sizeof(int8_t));
  316. *(int8_t *)value = (int8_t)entry_value->valuedouble;
  317. } else if (nvs_type == NVS_TYPE_U8) {
  318. value=malloc(sizeof(uint8_t));
  319. *(uint8_t *)value = (uint8_t)entry_value->valuedouble;
  320. } else if (nvs_type == NVS_TYPE_I16) {
  321. value=malloc(sizeof(int16_t));
  322. *(int16_t *)value = (int16_t)entry_value->valuedouble;
  323. } else if (nvs_type == NVS_TYPE_U16) {
  324. value=malloc(sizeof(uint16_t));
  325. *(uint16_t *)value = (uint16_t)entry_value->valuedouble;
  326. } else if (nvs_type == NVS_TYPE_I32) {
  327. value=malloc(sizeof(int32_t));
  328. *(int32_t *)value = (int32_t)entry_value->valuedouble;
  329. } else if (nvs_type == NVS_TYPE_U32) {
  330. value=malloc(sizeof(uint32_t));
  331. *(uint32_t *)value = (uint32_t)entry_value->valuedouble;
  332. } else if (nvs_type == NVS_TYPE_I64) {
  333. value=malloc(sizeof(int64_t));
  334. *(int64_t *)value = (int64_t)entry_value->valuedouble;
  335. } else if (nvs_type == NVS_TYPE_U64) {
  336. value=malloc(sizeof(uint64_t));
  337. *(uint64_t *)value = (uint64_t)entry_value->valuedouble;
  338. } else if (nvs_type == NVS_TYPE_STR) {
  339. if(!cJSON_IsString(entry_value)){
  340. char * entry_str = cJSON_PrintUnformatted(entry);
  341. if(entry_str!=NULL){
  342. ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d, Object: \n%s",
  343. entry_value->string,
  344. entry_value->valuestring,
  345. entry_value->type,
  346. entry_str);
  347. free(entry_str);
  348. }
  349. else {
  350. ESP_LOGE(TAG, "requested value type string, config type is different. key: %s, value: %s, type %d",
  351. entry_value->string,
  352. entry_value->valuestring,
  353. entry_value->type);
  354. }
  355. }
  356. else {
  357. value=(void *)strdup(cJSON_GetStringValue(entry_value));
  358. if(value==NULL){
  359. char * entry_str = cJSON_PrintUnformatted(entry);
  360. if(entry_str!=NULL){
  361. ESP_LOGE(TAG, "strdup failed on value for object \n%s",entry_str);
  362. free(entry_str);
  363. }
  364. else {
  365. ESP_LOGE(TAG, "strdup failed on value");
  366. }
  367. }
  368. }
  369. } else if (nvs_type == NVS_TYPE_BLOB) {
  370. ESP_LOGE(TAG, "Unsupported type NVS_TYPE_BLOB");
  371. }
  372. return value;
  373. }
  374. void config_commit_to_nvs(){
  375. ESP_LOGI(TAG,"Committing configuration to nvs. Locking config object.");
  376. ESP_LOGV(TAG,"config_commit_to_nvs. Locking config object.");
  377. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  378. ESP_LOGE(TAG, "config_commit_to_nvs: Unable to lock config for commit ");
  379. return ;
  380. }
  381. if(nvs_json==NULL){
  382. ESP_LOGE(TAG, ": cJSON nvs cache object not set.");
  383. return;
  384. }
  385. ESP_LOGV(TAG,"config_commit_to_nvs. Config Locked!");
  386. cJSON * entry=nvs_json->child;
  387. while(entry!= NULL){
  388. char * entry_str = cJSON_PrintUnformatted(entry);
  389. if(entry_str!=NULL){
  390. ESP_LOGV(TAG,"config_commit_to_nvs processing item %s",entry_str);
  391. free(entry_str);
  392. }
  393. if(config_is_entry_changed(entry)){
  394. ESP_LOGD(TAG, "Committing entry %s value to nvs.",(entry->string==NULL)?"UNKNOWN":entry->string);
  395. nvs_type_t type = config_get_entry_type(entry);
  396. void * value = config_safe_alloc_get_entry_value(type, entry);
  397. if(value!=NULL){
  398. esp_err_t err = store_nvs_value(type,entry->string,value);
  399. free(value);
  400. if(err!=ESP_OK){
  401. char * entry_str = cJSON_PrintUnformatted(entry);
  402. if(entry_str!=NULL){
  403. ESP_LOGE(TAG, "Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
  404. free(entry_str);
  405. }
  406. else {
  407. ESP_LOGE(TAG, "Error comitting value to nvs for key %s",entry->string);
  408. }
  409. }
  410. else {
  411. config_set_entry_changed_flag(entry, false);
  412. }
  413. }
  414. else {
  415. char * entry_str = cJSON_PrintUnformatted(entry);
  416. if(entry_str!=NULL){
  417. ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s, Object: \n%s",entry->string,entry_str);
  418. free(entry_str);
  419. }
  420. else {
  421. ESP_LOGE(TAG, "Unable to retrieve value. Error comitting value to nvs for key %s",entry->string);
  422. }
  423. }
  424. }
  425. else {
  426. ESP_LOGV(TAG,"config_commit_to_nvs. Item already committed. Ignoring.");
  427. }
  428. taskYIELD(); /* allows the freeRTOS scheduler to take over if needed. */
  429. entry = entry->next;
  430. }
  431. ESP_LOGV(TAG,"config_commit_to_nvs. Resetting the global commit flag.");
  432. config_raise_change(false);
  433. ESP_LOGV(TAG,"config_commit_to_nvs. Releasing the lock object.");
  434. config_unlock();
  435. }
  436. bool config_has_changes(){
  437. return (xEventGroupGetBits(config_group) & CONFIG_NO_COMMIT_PENDING)==0;
  438. }
  439. bool wait_for_commit(){
  440. bool commit_pending=(xEventGroupGetBits(config_group) & CONFIG_NO_COMMIT_PENDING)==0;
  441. while (commit_pending){
  442. ESP_LOGW(TAG,"Waiting for config commit ...");
  443. commit_pending = (xEventGroupWaitBits(config_group, CONFIG_NO_COMMIT_PENDING,pdFALSE, pdTRUE, (CONFIG_COMMIT_DELAY*2) / portTICK_PERIOD_MS) & CONFIG_NO_COMMIT_PENDING)==0;
  444. if(commit_pending){
  445. ESP_LOGW(TAG,"Timeout waiting for config commit.");
  446. }
  447. else {
  448. ESP_LOGI(TAG,"Config committed!");
  449. }
  450. }
  451. return !commit_pending;
  452. }
  453. bool config_lock(TickType_t xTicksToWait) {
  454. ESP_LOGV(TAG, "Locking config json object");
  455. if( xSemaphoreTake( config_mutex, xTicksToWait ) == pdTRUE ) {
  456. ESP_LOGV(TAG, "config Json object locked!");
  457. return true;
  458. }
  459. else {
  460. ESP_LOGE(TAG, "Semaphore take failed. Unable to lock config Json object mutex");
  461. return false;
  462. }
  463. }
  464. void config_unlock() {
  465. ESP_LOGV(TAG, "Unlocking json buffer!");
  466. xSemaphoreGive( config_mutex );
  467. }
  468. static void vCallbackFunction( TimerHandle_t xTimer ) {
  469. static int cnt=0;
  470. if(config_has_changes()){
  471. ESP_LOGI(TAG, "configuration has some uncommitted entries");
  472. config_commit_to_nvs();
  473. }
  474. else{
  475. if(++cnt>=15){
  476. ESP_LOGV(TAG,"commit timer: commit flag not set");
  477. cnt=0;
  478. }
  479. }
  480. xTimerReset( xTimer, 10 );
  481. }
  482. void config_raise_change(bool change_found){
  483. if(config_set_group_bit(CONFIG_NO_COMMIT_PENDING,!change_found))
  484. {
  485. ESP_LOGD(TAG,"Config commit set to %s",change_found?"Pending Commit":"Committed");
  486. }
  487. }
  488. bool config_set_group_bit(int bit_num,bool flag){
  489. bool result = true;
  490. int curFlags=xEventGroupGetBits(config_group);
  491. if((curFlags & CONFIG_LOAD_BIT) && bit_num == CONFIG_NO_COMMIT_PENDING ){
  492. ESP_LOGD(TAG,"Loading config, ignoring changes");
  493. result = false;
  494. }
  495. if(result){
  496. bool curBit=(xEventGroupGetBits(config_group) & bit_num);
  497. if(curBit == flag){
  498. ESP_LOGV(TAG,"Flag %d already %s", bit_num, flag?"Set":"Cleared");
  499. result = false;
  500. }
  501. }
  502. if(result){
  503. ESP_LOGV(TAG,"%s Flag %d ", flag?"Setting":"Clearing",bit_num);
  504. if(!flag){
  505. xEventGroupClearBits(config_group, bit_num);
  506. }
  507. else {
  508. xEventGroupSetBits(config_group, bit_num);
  509. }
  510. }
  511. return result;
  512. }
  513. void config_set_default(nvs_type_t type, const char *key, void * default_value, size_t blob_size) {
  514. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  515. ESP_LOGE(TAG, "Unable to lock config");
  516. return;
  517. }
  518. ESP_LOGV(TAG, "Checking if key %s exists in nvs cache for type %s.", key,type_to_str(type));
  519. cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  520. if(entry !=NULL){
  521. ESP_LOGV(TAG, "Entry found.");
  522. }
  523. else {
  524. // Value was not found
  525. ESP_LOGW(TAG, "Adding default value for [%s].", key);
  526. entry=config_set_value_safe(type, key, default_value);
  527. if(entry == NULL){
  528. ESP_LOGE(TAG, "Failed to add value to cache!");
  529. }
  530. char * entry_str = cJSON_PrintUnformatted(entry);
  531. if(entry_str!=NULL){
  532. ESP_LOGD(TAG, "Value added to default for object: \n%s",entry_str);
  533. free(entry_str);
  534. }
  535. }
  536. config_unlock();
  537. }
  538. void config_delete_key(const char *key){
  539. nvs_handle nvs;
  540. ESP_LOGD(TAG, "Deleting nvs entry for [%s]", key);
  541. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  542. ESP_LOGE(TAG, "Unable to lock config for delete");
  543. return false;
  544. }
  545. esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  546. if (err == ESP_OK) {
  547. err = nvs_erase_key(nvs, key);
  548. if (err == ESP_OK) {
  549. ESP_LOGD(TAG, "key [%s] erased from nvs.",key);
  550. err = nvs_commit(nvs);
  551. if (err == ESP_OK) {
  552. ESP_LOGD(TAG, "nvs erase committed.");
  553. }
  554. else {
  555. ESP_LOGE(TAG, "Unable to commit nvs erase operation for key [%s]. %s.",key,esp_err_to_name(err));
  556. }
  557. }
  558. else {
  559. ESP_LOGE(TAG, "Unable to delete nvs key [%s]. %s. ",key, esp_err_to_name(err));
  560. }
  561. nvs_close(nvs);
  562. }
  563. else {
  564. ESP_LOGE(TAG, "Error opening nvs: %s. Unable to delete nvs key [%s].",esp_err_to_name(err),key);
  565. }
  566. char * struc_str = cJSON_PrintUnformatted(nvs_json);
  567. if(struc_str!=NULL){
  568. ESP_LOGV(TAG, "Structure before delete \n%s", struc_str);
  569. free(struc_str);
  570. }
  571. cJSON * entry = cJSON_DetachItemFromObjectCaseSensitive(nvs_json, key);
  572. if(entry !=NULL){
  573. ESP_LOGI(TAG, "Removing config key [%s]", entry->string);
  574. cJSON_Delete(entry);
  575. struc_str = cJSON_PrintUnformatted(nvs_json);
  576. if(struc_str!=NULL){
  577. ESP_LOGV(TAG, "Structure after delete \n%s", struc_str);
  578. free(struc_str);
  579. }
  580. }
  581. else {
  582. ESP_LOGW(TAG, "Unable to remove config key [%s]: not found.", key);
  583. }
  584. config_unlock();
  585. }
  586. void * config_alloc_get(nvs_type_t nvs_type, const char *key) {
  587. return config_alloc_get_default(nvs_type, key, NULL, 0);
  588. }
  589. void * config_alloc_get_default(nvs_type_t nvs_type, const char *key, void * default_value, size_t blob_size) {
  590. void * value = NULL;
  591. ESP_LOGV(TAG, "Retrieving key %s from nvs cache for type %s.", key,type_to_str(nvs_type));
  592. if(nvs_json==NULL){
  593. ESP_LOGE(TAG,"configuration not loaded!");
  594. return value;
  595. }
  596. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  597. ESP_LOGE(TAG, "Unable to lock config");
  598. return value;
  599. }
  600. ESP_LOGD(TAG,"Getting config entry for key %s",key);
  601. cJSON * entry = cJSON_GetObjectItemCaseSensitive(nvs_json, key);
  602. if(entry !=NULL){
  603. ESP_LOGV(TAG, "Entry found, getting value.");
  604. value = config_safe_alloc_get_entry_value(nvs_type, entry);
  605. }
  606. else if(default_value!=NULL){
  607. // Value was not found
  608. ESP_LOGW(TAG, "Adding new config value for key [%s]",key);
  609. entry=config_set_value_safe(nvs_type, key, default_value);
  610. if(entry == NULL){
  611. ESP_LOGE(TAG, "Failed to add value to cache");
  612. }
  613. else {
  614. char * entry_str = cJSON_PrintUnformatted(entry);
  615. if(entry_str!=NULL){
  616. ESP_LOGV(TAG, "Value added configuration object for key [%s]: \n%s", entry->string,entry_str);
  617. free(entry_str);
  618. }
  619. else {
  620. ESP_LOGV(TAG, "Value added configuration object for key [%s]", entry->string);
  621. }
  622. value = config_safe_alloc_get_entry_value(nvs_type, entry);
  623. }
  624. }
  625. else{
  626. ESP_LOGW(TAG,"Value not found for key %s",key);
  627. }
  628. config_unlock();
  629. return value;
  630. }
  631. char * config_alloc_get_json(bool bFormatted){
  632. char * json_buffer = NULL;
  633. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  634. ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
  635. return strdup("{\"error\":\"Unable to lock configuration object.\"}");
  636. }
  637. if(bFormatted){
  638. json_buffer= cJSON_Print(nvs_json);
  639. }
  640. else {
  641. json_buffer= cJSON_PrintUnformatted(nvs_json);
  642. }
  643. config_unlock();
  644. return json_buffer;
  645. }
  646. esp_err_t config_set_value(nvs_type_t nvs_type, const char *key, void * value){
  647. esp_err_t result = ESP_OK;
  648. if(!config_lock(LOCK_MAX_WAIT/portTICK_PERIOD_MS)){
  649. ESP_LOGE(TAG, "Unable to lock config after %d ms",LOCK_MAX_WAIT);
  650. result = ESP_FAIL;
  651. }
  652. cJSON * entry = config_set_value_safe(nvs_type, key, value);
  653. if(entry == NULL){
  654. result = ESP_FAIL;
  655. }
  656. else{
  657. char * entry_str = cJSON_PrintUnformatted(entry);
  658. if(entry_str!=NULL){
  659. ESP_LOGV(TAG,"config_set_value result: \n%s",entry_str);
  660. free(entry_str);
  661. }
  662. else {
  663. ESP_LOGV(TAG,"config_set_value completed");
  664. }
  665. }
  666. config_unlock();
  667. return result;
  668. }
  669. IMPLEMENT_SET_DEFAULT(uint8_t,NVS_TYPE_U8);
  670. IMPLEMENT_SET_DEFAULT(int8_t,NVS_TYPE_I8);
  671. IMPLEMENT_SET_DEFAULT(uint16_t,NVS_TYPE_U16);
  672. IMPLEMENT_SET_DEFAULT(int16_t,NVS_TYPE_I16);
  673. IMPLEMENT_SET_DEFAULT(uint32_t,NVS_TYPE_U32);
  674. IMPLEMENT_SET_DEFAULT(int32_t,NVS_TYPE_I32);
  675. IMPLEMENT_GET_NUM(uint8_t,NVS_TYPE_U8);
  676. IMPLEMENT_GET_NUM(int8_t,NVS_TYPE_I8);
  677. IMPLEMENT_GET_NUM(uint16_t,NVS_TYPE_U16);
  678. IMPLEMENT_GET_NUM(int16_t,NVS_TYPE_I16);
  679. IMPLEMENT_GET_NUM(uint32_t,NVS_TYPE_U32);
  680. IMPLEMENT_GET_NUM(int32_t,NVS_TYPE_I32);