cmd_nvs.c 16 KB

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