config.c 23 KB

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