nvs_utilities.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
  2. #include "nvs_utilities.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "esp_system.h"
  6. #include "esp_log.h"
  7. #include "esp_console.h"
  8. #include "esp_vfs_dev.h"
  9. #include "driver/uart.h"
  10. #include "linenoise/linenoise.h"
  11. #include "argtable3/argtable3.h"
  12. #include "nvs.h"
  13. #include "nvs_flash.h"
  14. #include "nvs_utilities.h"
  15. #include "platform_config.h"
  16. const char current_namespace[] = "config";
  17. const char settings_partition[] = "settings";
  18. static const char * TAG = "nvs_utilities";
  19. typedef struct {
  20. nvs_type_t type;
  21. const char *str;
  22. } type_str_pair_t;
  23. static const type_str_pair_t type_str_pair[] = {
  24. { NVS_TYPE_I8, "i8" },
  25. { NVS_TYPE_U8, "u8" },
  26. { NVS_TYPE_U16, "u16" },
  27. { NVS_TYPE_I16, "i16" },
  28. { NVS_TYPE_U32, "u32" },
  29. { NVS_TYPE_I32, "i32" },
  30. { NVS_TYPE_U64, "u64" },
  31. { NVS_TYPE_I64, "i64" },
  32. { NVS_TYPE_STR, "str" },
  33. { NVS_TYPE_BLOB, "blob" },
  34. { NVS_TYPE_ANY, "any" },
  35. };
  36. static const size_t TYPE_STR_PAIR_SIZE = sizeof(type_str_pair) / sizeof(type_str_pair[0]);
  37. void print_blob(const char *blob, size_t len)
  38. {
  39. for (int i = 0; i < len; i++) {
  40. printf("%02x", blob[i]);
  41. }
  42. printf("\n");
  43. }
  44. nvs_type_t str_to_type(const char *type)
  45. {
  46. for (int i = 0; i < TYPE_STR_PAIR_SIZE; i++) {
  47. const type_str_pair_t *p = &type_str_pair[i];
  48. if (strcmp(type, p->str) == 0) {
  49. return p->type;
  50. }
  51. }
  52. return NVS_TYPE_ANY;
  53. }
  54. const char *type_to_str(nvs_type_t type)
  55. {
  56. for (int i = 0; i < TYPE_STR_PAIR_SIZE; i++) {
  57. const type_str_pair_t *p = &type_str_pair[i];
  58. if (p->type == type) {
  59. return p->str;
  60. }
  61. }
  62. return "Unknown";
  63. }
  64. void initialize_nvs() {
  65. ESP_LOGI(TAG, "Initializing flash nvs ");
  66. esp_err_t err = nvs_flash_init();
  67. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  68. ESP_LOGW(TAG, "%s. Erasing nvs flash", esp_err_to_name(err));
  69. ESP_ERROR_CHECK(nvs_flash_erase());
  70. err = nvs_flash_init();
  71. }
  72. if(err != ESP_OK){
  73. ESP_LOGE(TAG, "nvs_flash_init failed. %s.", esp_err_to_name(err));
  74. }
  75. ESP_ERROR_CHECK(err);
  76. ESP_LOGI(TAG, "Initializing nvs partition %s",settings_partition);
  77. err = nvs_flash_init_partition(settings_partition);
  78. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  79. ESP_LOGW(TAG, "%s. Erasing nvs on partition %s",esp_err_to_name(err),settings_partition);
  80. ESP_ERROR_CHECK(nvs_flash_erase_partition(settings_partition));
  81. err = nvs_flash_init_partition(settings_partition);
  82. }
  83. if(err!=ESP_OK){
  84. ESP_LOGE(TAG, "nvs_flash_init_partition failed. %s",esp_err_to_name(err));
  85. }
  86. ESP_ERROR_CHECK(err);
  87. ESP_LOGD(TAG, "nvs init completed");
  88. }
  89. esp_err_t nvs_load_config(){
  90. nvs_entry_info_t info;
  91. esp_err_t err = ESP_OK;
  92. size_t malloc_int = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  93. size_t malloc_spiram = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  94. nvs_iterator_t it = nvs_entry_find(settings_partition, NULL, NVS_TYPE_ANY);
  95. if(it == NULL) {
  96. ESP_LOGW(TAG, "empty nvs partition %s, namespace %s",settings_partition,current_namespace );
  97. }
  98. while (it != NULL) {
  99. nvs_entry_info(it, &info);
  100. if(strstr(info.namespace_name, current_namespace)) {
  101. void * value = get_nvs_value_alloc(info.type,info.key);
  102. if(value==NULL)
  103. {
  104. ESP_LOGE(TAG, "nvs read failed.");
  105. return ESP_FAIL;
  106. }
  107. config_set_value(info.type, info.key, value);
  108. free(value );
  109. }
  110. it = nvs_entry_next(it);
  111. }
  112. char * json_string= config_alloc_get_json(false);
  113. if(json_string!=NULL) {
  114. ESP_LOGD(TAG, "config json : %s\n", json_string);
  115. free(json_string);
  116. }
  117. ESP_LOGD(TAG,"Config memory usage. Heap internal:%zu (min:%zu) (used:%zu) external:%zu (min:%zu) (used:%zd)",
  118. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  119. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  120. malloc_int-heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  121. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  122. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  123. malloc_spiram -heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
  124. return err;
  125. }
  126. esp_err_t store_nvs_value(nvs_type_t type, const char *key, void * data) {
  127. if (type == NVS_TYPE_BLOB)
  128. return ESP_ERR_NVS_TYPE_MISMATCH;
  129. return store_nvs_value_len(type, key, data,0);
  130. }
  131. esp_err_t store_nvs_value_len(nvs_type_t type, const char *key, void * data,
  132. size_t data_len) {
  133. esp_err_t err;
  134. nvs_handle nvs;
  135. if (type == NVS_TYPE_ANY) {
  136. return ESP_ERR_NVS_TYPE_MISMATCH;
  137. }
  138. err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  139. if (err != ESP_OK) {
  140. return err;
  141. }
  142. if (type == NVS_TYPE_I8) {
  143. err = nvs_set_i8(nvs, key, *(int8_t *) data);
  144. } else if (type == NVS_TYPE_U8) {
  145. err = nvs_set_u8(nvs, key, *(uint8_t *) data);
  146. } else if (type == NVS_TYPE_I16) {
  147. err = nvs_set_i16(nvs, key, *(int16_t *) data);
  148. } else if (type == NVS_TYPE_U16) {
  149. err = nvs_set_u16(nvs, key, *(uint16_t *) data);
  150. } else if (type == NVS_TYPE_I32) {
  151. err = nvs_set_i32(nvs, key, *(int32_t *) data);
  152. } else if (type == NVS_TYPE_U32) {
  153. err = nvs_set_u32(nvs, key, *(uint32_t *) data);
  154. } else if (type == NVS_TYPE_I64) {
  155. err = nvs_set_i64(nvs, key, *(int64_t *) data);
  156. } else if (type == NVS_TYPE_U64) {
  157. err = nvs_set_u64(nvs, key, *(uint64_t *) data);
  158. } else if (type == NVS_TYPE_STR) {
  159. err = nvs_set_str(nvs, key, data);
  160. } else if (type == NVS_TYPE_BLOB) {
  161. err = nvs_set_blob(nvs, key, (void *) data, data_len);
  162. }
  163. if (err == ESP_OK) {
  164. err = nvs_commit(nvs);
  165. if (err == ESP_OK) {
  166. ESP_LOGI(TAG, "Value stored under key '%s'", key);
  167. }
  168. }
  169. nvs_close(nvs);
  170. return err;
  171. }
  172. void * get_nvs_value_alloc(nvs_type_t type, const char *key) {
  173. nvs_handle nvs;
  174. esp_err_t err;
  175. void * value=NULL;
  176. err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READONLY, &nvs);
  177. if (err != ESP_OK) {
  178. ESP_LOGE(TAG, "Could not open the nvs storage.");
  179. return NULL;
  180. }
  181. if (type == NVS_TYPE_I8) {
  182. value=malloc(sizeof(int8_t));
  183. err = nvs_get_i8(nvs, key, (int8_t *) value);
  184. } else if (type == NVS_TYPE_U8) {
  185. value=malloc(sizeof(uint8_t));
  186. err = nvs_get_u8(nvs, key, (uint8_t *) value);
  187. } else if (type == NVS_TYPE_I16) {
  188. value=malloc(sizeof(int16_t));
  189. err = nvs_get_i16(nvs, key, (int16_t *) value);
  190. } else if (type == NVS_TYPE_U16) {
  191. value=malloc(sizeof(uint16_t));
  192. err = nvs_get_u16(nvs, key, (uint16_t *) value);
  193. } else if (type == NVS_TYPE_I32) {
  194. value=malloc(sizeof(int32_t));
  195. err = nvs_get_i32(nvs, key, (int32_t *) value);
  196. } else if (type == NVS_TYPE_U32) {
  197. value=malloc(sizeof(uint32_t));
  198. err = nvs_get_u32(nvs, key, (uint32_t *) value);
  199. } else if (type == NVS_TYPE_I64) {
  200. value=malloc(sizeof(int64_t));
  201. err = nvs_get_i64(nvs, key, (int64_t *) value);
  202. } else if (type == NVS_TYPE_U64) {
  203. value=malloc(sizeof(uint64_t));
  204. err = nvs_get_u64(nvs, key, (uint64_t *) value);
  205. } else if (type == NVS_TYPE_STR) {
  206. size_t len=0;
  207. err = nvs_get_str(nvs, key, NULL, &len);
  208. if (err == ESP_OK) {
  209. value=malloc(len);
  210. err = nvs_get_str(nvs, key, value, &len);
  211. }
  212. } else if (type == NVS_TYPE_BLOB) {
  213. size_t len;
  214. err = nvs_get_blob(nvs, key, NULL, &len);
  215. if (err == ESP_OK) {
  216. value=malloc(len+1);
  217. err = nvs_get_blob(nvs, key, value, &len);
  218. }
  219. }
  220. if(err!=ESP_OK){
  221. ESP_LOGD(TAG, "Value not found for key %s",key);
  222. if(value!=NULL)
  223. free(value);
  224. value=NULL;
  225. }
  226. nvs_close(nvs);
  227. return value;
  228. }
  229. esp_err_t get_nvs_value(nvs_type_t type, const char *key, void*value, const uint8_t buf_size) {
  230. nvs_handle nvs;
  231. esp_err_t err;
  232. err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READONLY, &nvs);
  233. if (err != ESP_OK) {
  234. return err;
  235. }
  236. if (type == NVS_TYPE_I8) {
  237. err = nvs_get_i8(nvs, key, (int8_t *) value);
  238. } else if (type == NVS_TYPE_U8) {
  239. err = nvs_get_u8(nvs, key, (uint8_t *) value);
  240. } else if (type == NVS_TYPE_I16) {
  241. err = nvs_get_i16(nvs, key, (int16_t *) value);
  242. } else if (type == NVS_TYPE_U16) {
  243. err = nvs_get_u16(nvs, key, (uint16_t *) value);
  244. } else if (type == NVS_TYPE_I32) {
  245. err = nvs_get_i32(nvs, key, (int32_t *) value);
  246. } else if (type == NVS_TYPE_U32) {
  247. err = nvs_get_u32(nvs, key, (uint32_t *) value);
  248. } else if (type == NVS_TYPE_I64) {
  249. err = nvs_get_i64(nvs, key, (int64_t *) value);
  250. } else if (type == NVS_TYPE_U64) {
  251. err = nvs_get_u64(nvs, key, (uint64_t *) value);
  252. } else if (type == NVS_TYPE_STR) {
  253. size_t len;
  254. if ((err = nvs_get_str(nvs, key, NULL, &len)) == ESP_OK) {
  255. if (len > buf_size) {
  256. //ESP_LOGE("Error reading value for %s. Buffer size: %d, Value Length: %d", key, buf_size, len);
  257. err = ESP_FAIL;
  258. } else {
  259. err = nvs_get_str(nvs, key, value, &len);
  260. }
  261. }
  262. } else if (type == NVS_TYPE_BLOB) {
  263. size_t len;
  264. if ((err = nvs_get_blob(nvs, key, NULL, &len)) == ESP_OK) {
  265. if (len > buf_size) {
  266. //ESP_LOGE("Error reading value for %s. Buffer size: %d, Value Length: %d",
  267. // key, buf_size, len);
  268. err = ESP_FAIL;
  269. } else {
  270. err = nvs_get_blob(nvs, key, value, &len);
  271. }
  272. }
  273. }
  274. nvs_close(nvs);
  275. return err;
  276. }
  277. esp_err_t erase_nvs(const char *key)
  278. {
  279. nvs_handle nvs;
  280. esp_err_t err = nvs_open(current_namespace, NVS_READWRITE, &nvs);
  281. if (err == ESP_OK) {
  282. err = nvs_erase_key(nvs, key);
  283. if (err == ESP_OK) {
  284. err = nvs_commit(nvs);
  285. if (err == ESP_OK) {
  286. ESP_LOGI(TAG, "Value with key '%s' erased", key);
  287. }
  288. }
  289. nvs_close(nvs);
  290. }
  291. return err;
  292. }