platform_config.c 25 KB

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