platform_config.c 22 KB

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