cmd_nvs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* Console example — NVS commands
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. //#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include "nvs_flash.h"
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <inttypes.h>
  17. #include "esp_log.h"
  18. #include "esp_console.h"
  19. #include "argtable3/argtable3.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/event_groups.h"
  22. #include "esp_err.h"
  23. #include "cmd_nvs.h"
  24. #include "nvs.h"
  25. #include "nvs_utilities.h"
  26. static const char *ARG_TYPE_STR = "type can be: i8, u8, i16, u16 i32, u32 i64, u64, str, blob";
  27. static const char * TAG = "cmd_nvs";
  28. static struct {
  29. struct arg_str *key;
  30. struct arg_str *type;
  31. struct arg_str *value;
  32. struct arg_end *end;
  33. } set_args;
  34. static struct {
  35. struct arg_str *key;
  36. struct arg_str *type;
  37. struct arg_end *end;
  38. } get_args;
  39. static struct {
  40. struct arg_str *key;
  41. struct arg_end *end;
  42. } erase_args;
  43. static struct {
  44. struct arg_str *namespace;
  45. struct arg_end *end;
  46. } erase_all_args;
  47. static struct {
  48. struct arg_str *partition;
  49. struct arg_str *namespace;
  50. struct arg_str *type;
  51. struct arg_end *end;
  52. } list_args;
  53. static esp_err_t store_blob(nvs_handle nvs, const char *key, const char *str_values)
  54. {
  55. uint8_t value;
  56. size_t str_len = strlen(str_values);
  57. size_t blob_len = str_len / 2;
  58. if (str_len % 2) {
  59. ESP_LOGE(TAG, "Blob data must contain even number of characters");
  60. return ESP_ERR_NVS_TYPE_MISMATCH;
  61. }
  62. char *blob = (char *)malloc(blob_len);
  63. if (blob == NULL) {
  64. return ESP_ERR_NO_MEM;
  65. }
  66. for (int i = 0, j = 0; i < str_len; i++) {
  67. char ch = str_values[i];
  68. if (ch >= '0' && ch <= '9') {
  69. value = ch - '0';
  70. } else if (ch >= 'A' && ch <= 'F') {
  71. value = ch - 'A' + 10;
  72. } else if (ch >= 'a' && ch <= 'f') {
  73. value = ch - 'a' + 10;
  74. } else {
  75. ESP_LOGE(TAG, "Blob data contain invalid character");
  76. free(blob);
  77. return ESP_ERR_NVS_TYPE_MISMATCH;
  78. }
  79. if (i & 1) {
  80. blob[j++] += value;
  81. } else {
  82. blob[j] = value << 4;
  83. }
  84. }
  85. esp_err_t err = nvs_set_blob(nvs, key, blob, blob_len);
  86. free(blob);
  87. if (err == ESP_OK) {
  88. err = nvs_commit(nvs);
  89. }
  90. return err;
  91. }
  92. static esp_err_t set_value_in_nvs(const char *key, const char *str_type, const char *str_value)
  93. {
  94. esp_err_t err;
  95. nvs_handle nvs;
  96. bool range_error = false;
  97. nvs_type_t type = str_to_type(str_type);
  98. if (type == NVS_TYPE_ANY) {
  99. return ESP_ERR_NVS_TYPE_MISMATCH;
  100. }
  101. err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  102. if (err != ESP_OK) {
  103. return err;
  104. }
  105. if (type == NVS_TYPE_I8) {
  106. int32_t value = strtol(str_value, NULL, 0);
  107. if (value < INT8_MIN || value > INT8_MAX || errno == ERANGE) {
  108. range_error = true;
  109. } else {
  110. err = nvs_set_i8(nvs, key, (int8_t)value);
  111. }
  112. } else if (type == NVS_TYPE_U8) {
  113. uint32_t value = strtoul(str_value, NULL, 0);
  114. if (value > UINT8_MAX || errno == ERANGE) {
  115. range_error = true;
  116. } else {
  117. err = nvs_set_u8(nvs, key, (uint8_t)value);
  118. }
  119. } else if (type == NVS_TYPE_I16) {
  120. int32_t value = strtol(str_value, NULL, 0);
  121. if (value < INT16_MIN || value > INT16_MAX || errno == ERANGE) {
  122. range_error = true;
  123. } else {
  124. err = nvs_set_i16(nvs, key, (int16_t)value);
  125. }
  126. } else if (type == NVS_TYPE_U16) {
  127. uint32_t value = strtoul(str_value, NULL, 0);
  128. if (value > UINT16_MAX || errno == ERANGE) {
  129. range_error = true;
  130. } else {
  131. err = nvs_set_u16(nvs, key, (uint16_t)value);
  132. }
  133. } else if (type == NVS_TYPE_I32) {
  134. int32_t value = strtol(str_value, NULL, 0);
  135. if (errno != ERANGE) {
  136. err = nvs_set_i32(nvs, key, value);
  137. }
  138. } else if (type == NVS_TYPE_U32) {
  139. uint32_t value = strtoul(str_value, NULL, 0);
  140. if (errno != ERANGE) {
  141. err = nvs_set_u32(nvs, key, value);
  142. }
  143. } else if (type == NVS_TYPE_I64) {
  144. int64_t value = strtoll(str_value, NULL, 0);
  145. if (errno != ERANGE) {
  146. err = nvs_set_i64(nvs, key, value);
  147. }
  148. } else if (type == NVS_TYPE_U64) {
  149. uint64_t value = strtoull(str_value, NULL, 0);
  150. if (errno != ERANGE) {
  151. err = nvs_set_u64(nvs, key, value);
  152. }
  153. } else if (type == NVS_TYPE_STR) {
  154. err = nvs_set_str(nvs, key, str_value);
  155. } else if (type == NVS_TYPE_BLOB) {
  156. err = store_blob(nvs, key, str_value);
  157. }
  158. if (range_error || errno == ERANGE) {
  159. nvs_close(nvs);
  160. return ESP_ERR_NVS_VALUE_TOO_LONG;
  161. }
  162. if (err == ESP_OK) {
  163. ESP_LOGI(TAG, "Set value ok. Committing '%s'", key);
  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. static esp_err_t get_value_from_nvs(const char *key, const char *str_type)
  173. {
  174. nvs_handle nvs;
  175. esp_err_t err;
  176. nvs_type_t type = str_to_type(str_type);
  177. if (type == NVS_TYPE_ANY) {
  178. return ESP_ERR_NVS_TYPE_MISMATCH;
  179. }
  180. err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  181. if (err != ESP_OK) {
  182. return err;
  183. }
  184. if (type == NVS_TYPE_I8) {
  185. int8_t value;
  186. err = nvs_get_i8(nvs, key, &value);
  187. if (err == ESP_OK) {
  188. printf("Value associated with key '%s' is %d \n", key, value);
  189. }
  190. } else if (type == NVS_TYPE_U8) {
  191. uint8_t value;
  192. err = nvs_get_u8(nvs, key, &value);
  193. if (err == ESP_OK) {
  194. printf("Value associated with key '%s' is %u \n", key, value);
  195. }
  196. } else if (type == NVS_TYPE_I16) {
  197. int16_t value;
  198. err = nvs_get_i16(nvs, key, &value);
  199. if (err == ESP_OK) {
  200. printf("Value associated with key '%s' is %d \n", key, value);
  201. }
  202. } else if (type == NVS_TYPE_U16) {
  203. uint16_t value;
  204. if ((err = nvs_get_u16(nvs, key, &value)) == ESP_OK) {
  205. printf("Value associated with key '%s' is %u", key, value);
  206. }
  207. } else if (type == NVS_TYPE_I32) {
  208. int32_t value;
  209. if ((err = nvs_get_i32(nvs, key, &value)) == ESP_OK) {
  210. printf("Value associated with key '%s' is %d \n", key, value);
  211. }
  212. } else if (type == NVS_TYPE_U32) {
  213. uint32_t value;
  214. if ((err = nvs_get_u32(nvs, key, &value)) == ESP_OK) {
  215. printf("Value associated with key '%s' is %u \n", key, value);
  216. }
  217. } else if (type == NVS_TYPE_I64) {
  218. int64_t value;
  219. if ((err = nvs_get_i64(nvs, key, &value)) == ESP_OK) {
  220. printf("Value associated with key '%s' is %lld \n", key, value);
  221. }
  222. } else if (type == NVS_TYPE_U64) {
  223. uint64_t value;
  224. if ( (err = nvs_get_u64(nvs, key, &value)) == ESP_OK) {
  225. printf("Value associated with key '%s' is %llu \n", key, value);
  226. }
  227. } else if (type == NVS_TYPE_STR) {
  228. size_t len=0;
  229. if ( (err = nvs_get_str(nvs, key, NULL, &len)) == ESP_OK) {
  230. char *str = (char *)malloc(len);
  231. if ( (err = nvs_get_str(nvs, key, str, &len)) == ESP_OK) {
  232. printf("String associated with key '%s' is %s \n", key, str);
  233. }
  234. free(str);
  235. }
  236. } else if (type == NVS_TYPE_BLOB) {
  237. size_t len;
  238. if ( (err = nvs_get_blob(nvs, key, NULL, &len)) == ESP_OK) {
  239. char *blob = (char *)malloc(len);
  240. if ( (err = nvs_get_blob(nvs, key, blob, &len)) == ESP_OK) {
  241. printf("Blob associated with key '%s' is %d bytes long: \n", key, len);
  242. print_blob(blob, len);
  243. }
  244. free(blob);
  245. }
  246. }
  247. nvs_close(nvs);
  248. return err;
  249. }
  250. static esp_err_t erase(const char *key)
  251. {
  252. nvs_handle nvs;
  253. esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  254. if (err == ESP_OK) {
  255. err = nvs_erase_key(nvs, key);
  256. if (err == ESP_OK) {
  257. err = nvs_commit(nvs);
  258. if (err == ESP_OK) {
  259. ESP_LOGI(TAG, "Value with key '%s' erased", key);
  260. }
  261. }
  262. nvs_close(nvs);
  263. }
  264. return err;
  265. }
  266. static esp_err_t erase_all(const char *name)
  267. {
  268. nvs_handle nvs;
  269. esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  270. if (err == ESP_OK) {
  271. err = nvs_erase_all(nvs);
  272. if (err == ESP_OK) {
  273. err = nvs_commit(nvs);
  274. }
  275. }
  276. ESP_LOGI(TAG, "Namespace '%s' was %s erased", name, (err == ESP_OK) ? "" : "not");
  277. nvs_close(nvs);
  278. return ESP_OK;
  279. }
  280. static int set_value(int argc, char **argv)
  281. {
  282. ESP_LOGD(TAG, "%s %u - Parsing keys ",__func__,__LINE__);
  283. int nerrors = arg_parse(argc, argv, (void **) &set_args);
  284. if (nerrors != 0) {
  285. ESP_LOGE(TAG, "%s %u - Error Parsing keys ",__func__,__LINE__);
  286. arg_print_errors(stderr, set_args.end, argv[0]);
  287. return 1;
  288. }
  289. const char *key = set_args.key->sval[0];
  290. const char *type = set_args.type->sval[0];
  291. const char *values = set_args.value->sval[0];
  292. ESP_LOGI(TAG, "Setting '%s' (type %s)", key,type);
  293. esp_err_t err = set_value_in_nvs(key, type, values);
  294. if (err != ESP_OK) {
  295. ESP_LOGE(TAG, "%s", esp_err_to_name(err));
  296. return 1;
  297. }
  298. return 0;
  299. }
  300. static int get_value(int argc, char **argv)
  301. {
  302. int nerrors = arg_parse(argc, argv, (void **) &get_args);
  303. if (nerrors != 0) {
  304. arg_print_errors(stderr, get_args.end, argv[0]);
  305. return 1;
  306. }
  307. const char *key = get_args.key->sval[0];
  308. const char *type = get_args.type->sval[0];
  309. esp_err_t err = get_value_from_nvs(key, type);
  310. if (err != ESP_OK) {
  311. ESP_LOGE(TAG, "%s", esp_err_to_name(err));
  312. return 1;
  313. }
  314. return 0;
  315. }
  316. static int erase_value(int argc, char **argv)
  317. {
  318. int nerrors = arg_parse(argc, argv, (void **) &erase_args);
  319. if (nerrors != 0) {
  320. arg_print_errors(stderr, erase_args.end, argv[0]);
  321. return 1;
  322. }
  323. const char *key = erase_args.key->sval[0];
  324. esp_err_t err = erase(key);
  325. if (err != ESP_OK) {
  326. ESP_LOGE(TAG, "%s", esp_err_to_name(err));
  327. return 1;
  328. }
  329. return 0;
  330. }
  331. static int erase_namespace(int argc, char **argv)
  332. {
  333. int nerrors = arg_parse(argc, argv, (void **) &erase_all_args);
  334. if (nerrors != 0) {
  335. arg_print_errors(stderr, erase_all_args.end, argv[0]);
  336. return 1;
  337. }
  338. const char *name = erase_all_args.namespace->sval[0];
  339. esp_err_t err = erase_all(name);
  340. if (err != ESP_OK) {
  341. ESP_LOGE(TAG, "%s", esp_err_to_name(err));
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. static int erase_wifi_manager(int argc, char **argv)
  347. {
  348. nvs_handle nvs;
  349. esp_err_t err = nvs_open("config", NVS_READWRITE, &nvs);
  350. if (err == ESP_OK) {
  351. err = nvs_erase_all(nvs);
  352. if (err == ESP_OK) {
  353. err = nvs_commit(nvs);
  354. }
  355. }
  356. nvs_close(nvs);
  357. if (err != ESP_OK) {
  358. ESP_LOGE(TAG, "wifi manager configuration was not erase. %s", esp_err_to_name(err));
  359. return 1;
  360. }
  361. else {
  362. ESP_LOGW(TAG, "Wifi manager configuration was erased");
  363. }
  364. return 0;
  365. }
  366. static int list(const char *part, const char *name, const char *str_type)
  367. {
  368. nvs_type_t type = str_to_type(str_type);
  369. nvs_iterator_t it = nvs_entry_find(part, NULL, type);
  370. if (it == NULL) {
  371. ESP_LOGE(TAG, "No such enty was found");
  372. return 1;
  373. }
  374. do {
  375. nvs_entry_info_t info;
  376. nvs_entry_info(it, &info);
  377. it = nvs_entry_next(it);
  378. printf("namespace '%s', key '%s', type '%s' \n",
  379. info.namespace_name, info.key, type_to_str(info.type));
  380. } while (it != NULL);
  381. return 0;
  382. }
  383. static int list_entries(int argc, char **argv)
  384. {
  385. list_args.partition->sval[0] = "";
  386. list_args.namespace->sval[0] = "";
  387. list_args.type->sval[0] = "";
  388. int nerrors = arg_parse(argc, argv, (void **) &list_args);
  389. if (nerrors != 0) {
  390. arg_print_errors(stderr, list_args.end, argv[0]);
  391. return 1;
  392. }
  393. const char *part = list_args.partition->sval[0];
  394. const char *name = list_args.namespace->sval[0];
  395. const char *type = list_args.type->sval[0];
  396. return list(part, name, type);
  397. }
  398. void register_nvs()
  399. {
  400. set_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be set");
  401. set_args.type = arg_str1(NULL, NULL, "<type>", ARG_TYPE_STR);
  402. set_args.value = arg_str1("v", "value", "<value>", "value to be stored");
  403. set_args.end = arg_end(2);
  404. get_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be read");
  405. get_args.type = arg_str1(NULL, NULL, "<type>", ARG_TYPE_STR);
  406. get_args.end = arg_end(2);
  407. erase_args.key = arg_str1(NULL, NULL, "<key>", "key of the value to be erased");
  408. erase_args.end = arg_end(2);
  409. erase_all_args.namespace = arg_str1(NULL, NULL, "<namespace>", "namespace to be erased");
  410. erase_all_args.end = arg_end(2);
  411. list_args.partition = arg_str1(NULL, NULL, "<partition>", "partition name");
  412. list_args.namespace = arg_str0("n", "namespace", "<namespace>", "namespace name");
  413. list_args.type = arg_str0("t", "type", "<type>", ARG_TYPE_STR);
  414. list_args.end = arg_end(2);
  415. const esp_console_cmd_t set_cmd = {
  416. .command = "nvs_set",
  417. .help = "Set variable in selected namespace. Blob type must be comma separated list of hex values. \n"
  418. "Examples:\n"
  419. " nvs_set VarName i32 -v 123 \n"
  420. " nvs_set VarName srt -v YourString \n"
  421. " nvs_set VarName blob -v 0123456789abcdef \n",
  422. .hint = NULL,
  423. .func = &set_value,
  424. .argtable = &set_args
  425. };
  426. const esp_console_cmd_t get_cmd = {
  427. .command = "nvs_get",
  428. .help = "Get variable from selected namespace. \n"
  429. "Example: nvs_get VarName i32",
  430. .hint = NULL,
  431. .func = &get_value,
  432. .argtable = &get_args
  433. };
  434. const esp_console_cmd_t erase_cmd = {
  435. .command = "nvs_erase",
  436. .help = "Erase variable from current namespace",
  437. .hint = NULL,
  438. .func = &erase_value,
  439. .argtable = &erase_args
  440. };
  441. const esp_console_cmd_t erase_namespace_cmd = {
  442. .command = "nvs_erase_namespace",
  443. .help = "Erases specified namespace",
  444. .hint = NULL,
  445. .func = &erase_namespace,
  446. .argtable = &erase_all_args
  447. };
  448. const esp_console_cmd_t erase_wifimanager_cmd = {
  449. .command = "nvs_erase_wifi_manager",
  450. .help = "Erases wifi_manager's config",
  451. .hint = NULL,
  452. .func = &erase_wifi_manager,
  453. .argtable = NULL
  454. };
  455. const esp_console_cmd_t list_entries_cmd = {
  456. .command = "nvs_list",
  457. .help = "List stored key-value pairs stored in NVS."
  458. "Namespace and type can be specified to print only those key-value pairs.\n"
  459. "Following command list variables stored inside 'nvs' partition, under namespace 'storage' with type uint32_t"
  460. "Example: nvs_list nvs -n storage -t u32 \n",
  461. .hint = NULL,
  462. .func = &list_entries,
  463. .argtable = &list_args
  464. };
  465. ESP_ERROR_CHECK(esp_console_cmd_register(&list_entries_cmd));
  466. ESP_ERROR_CHECK(esp_console_cmd_register(&set_cmd));
  467. ESP_ERROR_CHECK(esp_console_cmd_register(&get_cmd));
  468. ESP_ERROR_CHECK(esp_console_cmd_register(&erase_cmd));
  469. ESP_ERROR_CHECK(esp_console_cmd_register(&erase_namespace_cmd));
  470. ESP_ERROR_CHECK(esp_console_cmd_register(&erase_wifimanager_cmd));
  471. }
  472. #ifdef __cplusplus
  473. extern }
  474. #endif