nvs.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef ESP_NVS_H
  14. #define ESP_NVS_H
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include <stdbool.h>
  18. #include "esp_attr.h"
  19. #include "esp_err.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * Opaque pointer type representing non-volatile storage handle
  25. */
  26. typedef uint32_t nvs_handle_t;
  27. /*
  28. * Pre-IDF V4.0 uses nvs_handle, so leaving the original typedef here for compatibility.
  29. */
  30. typedef nvs_handle_t nvs_handle IDF_DEPRECATED("Replace with nvs_handle_t");
  31. #define ESP_ERR_NVS_BASE 0x1100 /*!< Starting number of error codes */
  32. #define ESP_ERR_NVS_NOT_INITIALIZED (ESP_ERR_NVS_BASE + 0x01) /*!< The storage driver is not initialized */
  33. #define ESP_ERR_NVS_NOT_FOUND (ESP_ERR_NVS_BASE + 0x02) /*!< Id namespace doesn’t exist yet and mode is NVS_READONLY */
  34. #define ESP_ERR_NVS_TYPE_MISMATCH (ESP_ERR_NVS_BASE + 0x03) /*!< The type of set or get operation doesn't match the type of value stored in NVS */
  35. #define ESP_ERR_NVS_READ_ONLY (ESP_ERR_NVS_BASE + 0x04) /*!< Storage handle was opened as read only */
  36. #define ESP_ERR_NVS_NOT_ENOUGH_SPACE (ESP_ERR_NVS_BASE + 0x05) /*!< There is not enough space in the underlying storage to save the value */
  37. #define ESP_ERR_NVS_INVALID_NAME (ESP_ERR_NVS_BASE + 0x06) /*!< Namespace name doesn’t satisfy constraints */
  38. #define ESP_ERR_NVS_INVALID_HANDLE (ESP_ERR_NVS_BASE + 0x07) /*!< Handle has been closed or is NULL */
  39. #define ESP_ERR_NVS_REMOVE_FAILED (ESP_ERR_NVS_BASE + 0x08) /*!< The value wasn’t updated because flash write operation has failed. The value was written however, and update will be finished after re-initialization of nvs, provided that flash operation doesn’t fail again. */
  40. #define ESP_ERR_NVS_KEY_TOO_LONG (ESP_ERR_NVS_BASE + 0x09) /*!< Key name is too long */
  41. #define ESP_ERR_NVS_PAGE_FULL (ESP_ERR_NVS_BASE + 0x0a) /*!< Internal error; never returned by nvs API functions */
  42. #define ESP_ERR_NVS_INVALID_STATE (ESP_ERR_NVS_BASE + 0x0b) /*!< NVS is in an inconsistent state due to a previous error. Call nvs_flash_init and nvs_open again, then retry. */
  43. #define ESP_ERR_NVS_INVALID_LENGTH (ESP_ERR_NVS_BASE + 0x0c) /*!< String or blob length is not sufficient to store data */
  44. #define ESP_ERR_NVS_NO_FREE_PAGES (ESP_ERR_NVS_BASE + 0x0d) /*!< NVS partition doesn't contain any empty pages. This may happen if NVS partition was truncated. Erase the whole partition and call nvs_flash_init again. */
  45. #define ESP_ERR_NVS_VALUE_TOO_LONG (ESP_ERR_NVS_BASE + 0x0e) /*!< String or blob length is longer than supported by the implementation */
  46. #define ESP_ERR_NVS_PART_NOT_FOUND (ESP_ERR_NVS_BASE + 0x0f) /*!< Partition with specified name is not found in the partition table */
  47. #define ESP_ERR_NVS_NEW_VERSION_FOUND (ESP_ERR_NVS_BASE + 0x10) /*!< NVS partition contains data in new format and cannot be recognized by this version of code */
  48. #define ESP_ERR_NVS_XTS_ENCR_FAILED (ESP_ERR_NVS_BASE + 0x11) /*!< XTS encryption failed while writing NVS entry */
  49. #define ESP_ERR_NVS_XTS_DECR_FAILED (ESP_ERR_NVS_BASE + 0x12) /*!< XTS decryption failed while reading NVS entry */
  50. #define ESP_ERR_NVS_XTS_CFG_FAILED (ESP_ERR_NVS_BASE + 0x13) /*!< XTS configuration setting failed */
  51. #define ESP_ERR_NVS_XTS_CFG_NOT_FOUND (ESP_ERR_NVS_BASE + 0x14) /*!< XTS configuration not found */
  52. #define ESP_ERR_NVS_ENCR_NOT_SUPPORTED (ESP_ERR_NVS_BASE + 0x15) /*!< NVS encryption is not supported in this version */
  53. #define ESP_ERR_NVS_KEYS_NOT_INITIALIZED (ESP_ERR_NVS_BASE + 0x16) /*!< NVS key partition is uninitialized */
  54. #define ESP_ERR_NVS_CORRUPT_KEY_PART (ESP_ERR_NVS_BASE + 0x17) /*!< NVS key partition is corrupt */
  55. #define ESP_ERR_NVS_CONTENT_DIFFERS (ESP_ERR_NVS_BASE + 0x18) /*!< Internal error; never returned by nvs API functions. NVS key is different in comparison */
  56. #define NVS_DEFAULT_PART_NAME "nvs" /*!< Default partition name of the NVS partition in the partition table */
  57. #define NVS_PART_NAME_MAX_SIZE 16 /*!< maximum length of partition name (excluding null terminator) */
  58. /**
  59. * @brief Mode of opening the non-volatile storage
  60. */
  61. typedef enum {
  62. NVS_READONLY, /*!< Read only */
  63. NVS_READWRITE /*!< Read and write */
  64. } nvs_open_mode_t;
  65. /*
  66. * Pre-IDF V4.0 uses nvs_open_mode, so leaving the original typedef here for compatibility.
  67. */
  68. typedef nvs_open_mode_t nvs_open_mode IDF_DEPRECATED("Replace with nvs_open_mode_t");
  69. /**
  70. * @brief Types of variables
  71. *
  72. */
  73. typedef enum {
  74. NVS_TYPE_U8 = 0x01, /*!< Type uint8_t */
  75. NVS_TYPE_I8 = 0x11, /*!< Type int8_t */
  76. NVS_TYPE_U16 = 0x02, /*!< Type uint16_t */
  77. NVS_TYPE_I16 = 0x12, /*!< Type int16_t */
  78. NVS_TYPE_U32 = 0x04, /*!< Type uint32_t */
  79. NVS_TYPE_I32 = 0x14, /*!< Type int32_t */
  80. NVS_TYPE_U64 = 0x08, /*!< Type uint64_t */
  81. NVS_TYPE_I64 = 0x18, /*!< Type int64_t */
  82. NVS_TYPE_STR = 0x21, /*!< Type string */
  83. NVS_TYPE_BLOB = 0x42, /*!< Type blob */
  84. NVS_TYPE_ANY = 0xff /*!< Must be last */
  85. } nvs_type_t;
  86. /**
  87. * @brief information about entry obtained from nvs_entry_info function
  88. */
  89. typedef struct {
  90. char namespace_name[16]; /*!< Namespace to which key-value belong */
  91. char key[16]; /*!< Key of stored key-value pair */
  92. nvs_type_t type; /*!< Type of stored key-value pair */
  93. } nvs_entry_info_t;
  94. /**
  95. * Opaque pointer type representing iterator to nvs entries
  96. */
  97. typedef struct nvs_opaque_iterator_t *nvs_iterator_t;
  98. /**
  99. * @brief Open non-volatile storage with a given namespace from the default NVS partition
  100. *
  101. * Multiple internal ESP-IDF and third party application modules can store
  102. * their key-value pairs in the NVS module. In order to reduce possible
  103. * conflicts on key names, each module can use its own namespace.
  104. * The default NVS partition is the one that is labelled "nvs" in the partition
  105. * table.
  106. *
  107. * @param[in] name Namespace name. Maximal length is determined by the
  108. * underlying implementation, but is guaranteed to be
  109. * at least 15 characters. Shouldn't be empty.
  110. * @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
  111. * open a handle for reading only. All write requests will
  112. * be rejected for this handle.
  113. * @param[out] out_handle If successful (return code is zero), handle will be
  114. * returned in this argument.
  115. *
  116. * @return
  117. * - ESP_OK if storage handle was opened successfully
  118. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  119. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "nvs" is not found
  120. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  121. * mode is NVS_READONLY
  122. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  123. * - other error codes from the underlying storage driver
  124. */
  125. esp_err_t nvs_open(const char* name, nvs_open_mode_t open_mode, nvs_handle_t *out_handle);
  126. /**
  127. * @brief Open non-volatile storage with a given namespace from specified partition
  128. *
  129. * The behaviour is same as nvs_open() API. However this API can operate on a specified NVS
  130. * partition instead of default NVS partition. Note that the specified partition must be registered
  131. * with NVS using nvs_flash_init_partition() API.
  132. *
  133. * @param[in] part_name Label (name) of the partition of interest for object read/write/erase
  134. * @param[in] name Namespace name. Maximal length is determined by the
  135. * underlying implementation, but is guaranteed to be
  136. * at least 15 characters. Shouldn't be empty.
  137. * @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
  138. * open a handle for reading only. All write requests will
  139. * be rejected for this handle.
  140. * @param[out] out_handle If successful (return code is zero), handle will be
  141. * returned in this argument.
  142. *
  143. * @return
  144. * - ESP_OK if storage handle was opened successfully
  145. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  146. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with specified name is not found
  147. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  148. * mode is NVS_READONLY
  149. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  150. * - other error codes from the underlying storage driver
  151. */
  152. esp_err_t nvs_open_from_partition(const char *part_name, const char* name, nvs_open_mode_t open_mode, nvs_handle_t *out_handle);
  153. /**@{*/
  154. /**
  155. * @brief set int8_t value for given key
  156. *
  157. * Set value for the key, given its name. Note that the actual storage will not be updated
  158. * until \c nvs_commit is called.
  159. *
  160. * @param[in] handle Handle obtained from nvs_open function.
  161. * Handles that were opened read only cannot be used.
  162. * @param[in] key Key name. Maximal length is determined by the underlying
  163. * implementation, but is guaranteed to be at least
  164. * 15 characters. Shouldn't be empty.
  165. * @param[in] value The value to set.
  166. *
  167. * @return
  168. * - ESP_OK if value was set successfully
  169. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  170. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  171. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  172. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  173. * underlying storage to save the value
  174. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  175. * write operation has failed. The value was written however, and
  176. * update will be finished after re-initialization of nvs, provided that
  177. * flash operation doesn't fail again.
  178. */
  179. esp_err_t nvs_set_i8 (nvs_handle_t handle, const char* key, int8_t value);
  180. /**
  181. * @brief set uint8_t value for given key
  182. *
  183. * This function is the same as \c nvs_set_i8 except for the data type.
  184. */
  185. esp_err_t nvs_set_u8 (nvs_handle_t handle, const char* key, uint8_t value);
  186. /**
  187. * @brief set int16_t value for given key
  188. *
  189. * This function is the same as \c nvs_set_i8 except for the data type.
  190. */
  191. esp_err_t nvs_set_i16 (nvs_handle_t handle, const char* key, int16_t value);
  192. /**
  193. * @brief set uint16_t value for given key
  194. *
  195. * This function is the same as \c nvs_set_i8 except for the data type.
  196. */
  197. esp_err_t nvs_set_u16 (nvs_handle_t handle, const char* key, uint16_t value);
  198. /**
  199. * @brief set int32_t value for given key
  200. *
  201. * This function is the same as \c nvs_set_i8 except for the data type.
  202. */
  203. esp_err_t nvs_set_i32 (nvs_handle_t handle, const char* key, int32_t value);
  204. /**
  205. * @brief set uint32_t value for given key
  206. *
  207. * This function is the same as \c nvs_set_i8 except for the data type.
  208. */
  209. esp_err_t nvs_set_u32 (nvs_handle_t handle, const char* key, uint32_t value);
  210. /**
  211. * @brief set int64_t value for given key
  212. *
  213. * This function is the same as \c nvs_set_i8 except for the data type.
  214. */
  215. esp_err_t nvs_set_i64 (nvs_handle_t handle, const char* key, int64_t value);
  216. /**
  217. * @brief set uint64_t value for given key
  218. *
  219. * This function is the same as \c nvs_set_i8 except for the data type.
  220. */
  221. esp_err_t nvs_set_u64 (nvs_handle_t handle, const char* key, uint64_t value);
  222. /**
  223. * @brief set string for given key
  224. *
  225. * Set value for the key, given its name. Note that the actual storage will not be updated
  226. * until \c nvs_commit is called.
  227. *
  228. * @param[in] handle Handle obtained from nvs_open function.
  229. * Handles that were opened read only cannot be used.
  230. * @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
  231. * @param[in] value The value to set.
  232. * For strings, the maximum length (including null character) is
  233. * 4000 bytes, if there is one complete page free for writing.
  234. * This decreases, however, if the free space is fragmented.
  235. *
  236. * @return
  237. * - ESP_OK if value was set successfully
  238. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  239. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  240. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  241. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  242. * underlying storage to save the value
  243. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  244. * write operation has failed. The value was written however, and
  245. * update will be finished after re-initialization of nvs, provided that
  246. * flash operation doesn't fail again.
  247. * - ESP_ERR_NVS_VALUE_TOO_LONG if the string value is too long
  248. */
  249. esp_err_t nvs_set_str (nvs_handle_t handle, const char* key, const char* value);
  250. /**@}*/
  251. /**
  252. * @brief set variable length binary value for given key
  253. *
  254. * This family of functions set value for the key, given its name. Note that
  255. * actual storage will not be updated until nvs_commit function is called.
  256. *
  257. * @param[in] handle Handle obtained from nvs_open function.
  258. * Handles that were opened read only cannot be used.
  259. * @param[in] key Key name. Maximal length is 15 characters. Shouldn't be empty.
  260. * @param[in] value The value to set.
  261. * @param[in] length length of binary value to set, in bytes; Maximum length is
  262. * 508000 bytes or (97.6% of the partition size - 4000) bytes
  263. * whichever is lower.
  264. *
  265. * @return
  266. * - ESP_OK if value was set successfully
  267. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  268. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  269. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  270. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  271. * underlying storage to save the value
  272. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  273. * write operation has failed. The value was written however, and
  274. * update will be finished after re-initialization of nvs, provided that
  275. * flash operation doesn't fail again.
  276. * - ESP_ERR_NVS_VALUE_TOO_LONG if the value is too long
  277. */
  278. esp_err_t nvs_set_blob(nvs_handle_t handle, const char* key, const void* value, size_t length);
  279. /**@{*/
  280. /**
  281. * @brief get int8_t value for given key
  282. *
  283. * These functions retrieve value for the key, given its name. If \c key does not
  284. * exist, or the requested variable type doesn't match the type which was used
  285. * when setting a value, an error is returned.
  286. *
  287. * In case of any error, out_value is not modified.
  288. *
  289. * \c out_value has to be a pointer to an already allocated variable of the given type.
  290. *
  291. * \code{c}
  292. * // Example of using nvs_get_i32:
  293. * int32_t max_buffer_size = 4096; // default value
  294. * esp_err_t err = nvs_get_i32(my_handle, "max_buffer_size", &max_buffer_size);
  295. * assert(err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND);
  296. * // if ESP_ERR_NVS_NOT_FOUND was returned, max_buffer_size will still
  297. * // have its default value.
  298. *
  299. * \endcode
  300. *
  301. * @param[in] handle Handle obtained from nvs_open function.
  302. * @param[in] key Key name. Maximal length is determined by the underlying
  303. * implementation, but is guaranteed to be at least
  304. * 15 characters. Shouldn't be empty.
  305. * @param out_value Pointer to the output value.
  306. * May be NULL for nvs_get_str and nvs_get_blob, in this
  307. * case required length will be returned in length argument.
  308. *
  309. * @return
  310. * - ESP_OK if the value was retrieved successfully
  311. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  312. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  313. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  314. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  315. */
  316. esp_err_t nvs_get_i8 (nvs_handle_t handle, const char* key, int8_t* out_value);
  317. /**
  318. * @brief get uint8_t value for given key
  319. *
  320. * This function is the same as \c nvs_get_i8 except for the data type.
  321. */
  322. esp_err_t nvs_get_u8 (nvs_handle_t handle, const char* key, uint8_t* out_value);
  323. /**
  324. * @brief get int16_t value for given key
  325. *
  326. * This function is the same as \c nvs_get_i8 except for the data type.
  327. */
  328. esp_err_t nvs_get_i16 (nvs_handle_t handle, const char* key, int16_t* out_value);
  329. /**
  330. * @brief get uint16_t value for given key
  331. *
  332. * This function is the same as \c nvs_get_i8 except for the data type.
  333. */
  334. esp_err_t nvs_get_u16 (nvs_handle_t handle, const char* key, uint16_t* out_value);
  335. /**
  336. * @brief get int32_t value for given key
  337. *
  338. * This function is the same as \c nvs_get_i8 except for the data type.
  339. */
  340. esp_err_t nvs_get_i32 (nvs_handle_t handle, const char* key, int32_t* out_value);
  341. /**
  342. * @brief get uint32_t value for given key
  343. *
  344. * This function is the same as \c nvs_get_i8 except for the data type.
  345. */
  346. esp_err_t nvs_get_u32 (nvs_handle_t handle, const char* key, uint32_t* out_value);
  347. /**
  348. * @brief get int64_t value for given key
  349. *
  350. * This function is the same as \c nvs_get_i8 except for the data type.
  351. */
  352. esp_err_t nvs_get_i64 (nvs_handle_t handle, const char* key, int64_t* out_value);
  353. /**
  354. * @brief get uint64_t value for given key
  355. *
  356. * This function is the same as \c nvs_get_i8 except for the data type.
  357. */
  358. esp_err_t nvs_get_u64 (nvs_handle_t handle, const char* key, uint64_t* out_value);
  359. /**@}*/
  360. /**@{*/
  361. /**
  362. * @brief get string value for given key
  363. *
  364. * These functions retrieve the data of an entry, given its key. If key does not
  365. * exist, or the requested variable type doesn't match the type which was used
  366. * when setting a value, an error is returned.
  367. *
  368. * In case of any error, out_value is not modified.
  369. *
  370. * All functions expect out_value to be a pointer to an already allocated variable
  371. * of the given type.
  372. *
  373. * nvs_get_str and nvs_get_blob functions support WinAPI-style length queries.
  374. * To get the size necessary to store the value, call nvs_get_str or nvs_get_blob
  375. * with zero out_value and non-zero pointer to length. Variable pointed to
  376. * by length argument will be set to the required length. For nvs_get_str,
  377. * this length includes the zero terminator. When calling nvs_get_str and
  378. * nvs_get_blob with non-zero out_value, length has to be non-zero and has to
  379. * point to the length available in out_value.
  380. * It is suggested that nvs_get/set_str is used for zero-terminated C strings, and
  381. * nvs_get/set_blob used for arbitrary data structures.
  382. *
  383. * \code{c}
  384. * // Example (without error checking) of using nvs_get_str to get a string into dynamic array:
  385. * size_t required_size;
  386. * nvs_get_str(my_handle, "server_name", NULL, &required_size);
  387. * char* server_name = malloc(required_size);
  388. * nvs_get_str(my_handle, "server_name", server_name, &required_size);
  389. *
  390. * // Example (without error checking) of using nvs_get_blob to get a binary data
  391. * into a static array:
  392. * uint8_t mac_addr[6];
  393. * size_t size = sizeof(mac_addr);
  394. * nvs_get_blob(my_handle, "dst_mac_addr", mac_addr, &size);
  395. * \endcode
  396. *
  397. * @param[in] handle Handle obtained from nvs_open function.
  398. * @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
  399. * @param[out] out_value Pointer to the output value.
  400. * May be NULL for nvs_get_str and nvs_get_blob, in this
  401. * case required length will be returned in length argument.
  402. * @param[inout] length A non-zero pointer to the variable holding the length of out_value.
  403. * In case out_value a zero, will be set to the length
  404. * required to hold the value. In case out_value is not
  405. * zero, will be set to the actual length of the value
  406. * written. For nvs_get_str this includes zero terminator.
  407. *
  408. * @return
  409. * - ESP_OK if the value was retrieved successfully
  410. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  411. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  412. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  413. * - ESP_ERR_NVS_INVALID_LENGTH if \c length is not sufficient to store data
  414. */
  415. esp_err_t nvs_get_str (nvs_handle_t handle, const char* key, char* out_value, size_t* length);
  416. /**
  417. * @brief get blob value for given key
  418. *
  419. * This function behaves the same as \c nvs_get_str, except for the data type.
  420. */
  421. esp_err_t nvs_get_blob(nvs_handle_t handle, const char* key, void* out_value, size_t* length);
  422. /**@}*/
  423. /**
  424. * @brief Erase key-value pair with given key name.
  425. *
  426. * Note that actual storage may not be updated until nvs_commit function is called.
  427. *
  428. * @param[in] handle Storage handle obtained with nvs_open.
  429. * Handles that were opened read only cannot be used.
  430. *
  431. * @param[in] key Key name. Maximal length is determined by the underlying
  432. * implementation, but is guaranteed to be at least
  433. * 15 characters. Shouldn't be empty.
  434. *
  435. * @return
  436. * - ESP_OK if erase operation was successful
  437. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  438. * - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
  439. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  440. * - other error codes from the underlying storage driver
  441. */
  442. esp_err_t nvs_erase_key(nvs_handle_t handle, const char* key);
  443. /**
  444. * @brief Erase all key-value pairs in a namespace
  445. *
  446. * Note that actual storage may not be updated until nvs_commit function is called.
  447. *
  448. * @param[in] handle Storage handle obtained with nvs_open.
  449. * Handles that were opened read only cannot be used.
  450. *
  451. * @return
  452. * - ESP_OK if erase operation was successful
  453. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  454. * - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
  455. * - other error codes from the underlying storage driver
  456. */
  457. esp_err_t nvs_erase_all(nvs_handle_t handle);
  458. /**
  459. * @brief Write any pending changes to non-volatile storage
  460. *
  461. * After setting any values, nvs_commit() must be called to ensure changes are written
  462. * to non-volatile storage. Individual implementations may write to storage at other times,
  463. * but this is not guaranteed.
  464. *
  465. * @param[in] handle Storage handle obtained with nvs_open.
  466. * Handles that were opened read only cannot be used.
  467. *
  468. * @return
  469. * - ESP_OK if the changes have been written successfully
  470. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  471. * - other error codes from the underlying storage driver
  472. */
  473. esp_err_t nvs_commit(nvs_handle_t handle);
  474. /**
  475. * @brief Close the storage handle and free any allocated resources
  476. *
  477. * This function should be called for each handle opened with nvs_open once
  478. * the handle is not in use any more. Closing the handle may not automatically
  479. * write the changes to nonvolatile storage. This has to be done explicitly using
  480. * nvs_commit function.
  481. * Once this function is called on a handle, the handle should no longer be used.
  482. *
  483. * @param[in] handle Storage handle to close
  484. */
  485. void nvs_close(nvs_handle_t handle);
  486. /**
  487. * @note Info about storage space NVS.
  488. */
  489. typedef struct {
  490. size_t used_entries; /**< Amount of used entries. */
  491. size_t free_entries; /**< Amount of free entries. */
  492. size_t total_entries; /**< Amount all available entries. */
  493. size_t namespace_count; /**< Amount name space. */
  494. } nvs_stats_t;
  495. /**
  496. * @brief Fill structure nvs_stats_t. It provides info about used memory the partition.
  497. *
  498. * This function calculates to runtime the number of used entries, free entries, total entries,
  499. * and amount namespace in partition.
  500. *
  501. * \code{c}
  502. * // Example of nvs_get_stats() to get the number of used entries and free entries:
  503. * nvs_stats_t nvs_stats;
  504. * nvs_get_stats(NULL, &nvs_stats);
  505. * printf("Count: UsedEntries = (%d), FreeEntries = (%d), AllEntries = (%d)\n",
  506. nvs_stats.used_entries, nvs_stats.free_entries, nvs_stats.total_entries);
  507. * \endcode
  508. *
  509. * @param[in] part_name Partition name NVS in the partition table.
  510. * If pass a NULL than will use NVS_DEFAULT_PART_NAME ("nvs").
  511. *
  512. * @param[out] nvs_stats Returns filled structure nvs_states_t.
  513. * It provides info about used memory the partition.
  514. *
  515. *
  516. * @return
  517. * - ESP_OK if the changes have been written successfully.
  518. * Return param nvs_stats will be filled.
  519. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "name" is not found.
  520. * Return param nvs_stats will be filled 0.
  521. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
  522. * Return param nvs_stats will be filled 0.
  523. * - ESP_ERR_INVALID_ARG if nvs_stats equal to NULL.
  524. * - ESP_ERR_INVALID_STATE if there is page with the status of INVALID.
  525. * Return param nvs_stats will be filled not with correct values because
  526. * not all pages will be counted. Counting will be interrupted at the first INVALID page.
  527. */
  528. esp_err_t nvs_get_stats(const char *part_name, nvs_stats_t *nvs_stats);
  529. /**
  530. * @brief Calculate all entries in a namespace.
  531. *
  532. * An entry represents the smallest storage unit in NVS.
  533. * Strings and blobs may occupy more than one entry.
  534. * Note that to find out the total number of entries occupied by the namespace,
  535. * add one to the returned value used_entries (if err is equal to ESP_OK).
  536. * Because the name space entry takes one entry.
  537. *
  538. * \code{c}
  539. * // Example of nvs_get_used_entry_count() to get amount of all key-value pairs in one namespace:
  540. * nvs_handle_t handle;
  541. * nvs_open("namespace1", NVS_READWRITE, &handle);
  542. * ...
  543. * size_t used_entries;
  544. * size_t total_entries_namespace;
  545. * if(nvs_get_used_entry_count(handle, &used_entries) == ESP_OK){
  546. * // the total number of entries occupied by the namespace
  547. * total_entries_namespace = used_entries + 1;
  548. * }
  549. * \endcode
  550. *
  551. * @param[in] handle Handle obtained from nvs_open function.
  552. *
  553. * @param[out] used_entries Returns amount of used entries from a namespace.
  554. *
  555. *
  556. * @return
  557. * - ESP_OK if the changes have been written successfully.
  558. * Return param used_entries will be filled valid value.
  559. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
  560. * Return param used_entries will be filled 0.
  561. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL.
  562. * Return param used_entries will be filled 0.
  563. * - ESP_ERR_INVALID_ARG if used_entries equal to NULL.
  564. * - Other error codes from the underlying storage driver.
  565. * Return param used_entries will be filled 0.
  566. */
  567. esp_err_t nvs_get_used_entry_count(nvs_handle_t handle, size_t* used_entries);
  568. /**
  569. * @brief Create an iterator to enumerate NVS entries based on one or more parameters
  570. *
  571. * \code{c}
  572. * // Example of listing all the key-value pairs of any type under specified partition and namespace
  573. * nvs_iterator_t it = nvs_entry_find(partition, namespace, NVS_TYPE_ANY);
  574. * while (it != NULL) {
  575. * nvs_entry_info_t info;
  576. * nvs_entry_info(it, &info);
  577. * it = nvs_entry_next(it);
  578. * printf("key '%s', type '%d' \n", info.key, info.type);
  579. * };
  580. * // Note: no need to release iterator obtained from nvs_entry_find function when
  581. * // nvs_entry_find or nvs_entry_next function return NULL, indicating no other
  582. * // element for specified criteria was found.
  583. * }
  584. * \endcode
  585. *
  586. * @param[in] part_name Partition name
  587. *
  588. * @param[in] namespace_name Set this value if looking for entries with
  589. * a specific namespace. Pass NULL otherwise.
  590. *
  591. * @param[in] type One of nvs_type_t values.
  592. *
  593. * @return
  594. * Iterator used to enumerate all the entries found,
  595. * or NULL if no entry satisfying criteria was found.
  596. * Iterator obtained through this function has to be released
  597. * using nvs_release_iterator when not used any more.
  598. */
  599. nvs_iterator_t nvs_entry_find(const char *part_name, const char *namespace_name, nvs_type_t type);
  600. /**
  601. * @brief Returns next item matching the iterator criteria, NULL if no such item exists.
  602. *
  603. * Note that any copies of the iterator will be invalid after this call.
  604. *
  605. * @param[in] iterator Iterator obtained from nvs_entry_find function. Must be non-NULL.
  606. *
  607. * @return
  608. * NULL if no entry was found, valid nvs_iterator_t otherwise.
  609. */
  610. nvs_iterator_t nvs_entry_next(nvs_iterator_t iterator);
  611. /**
  612. * @brief Fills nvs_entry_info_t structure with information about entry pointed to by the iterator.
  613. *
  614. * @param[in] iterator Iterator obtained from nvs_entry_find or nvs_entry_next function. Must be non-NULL.
  615. *
  616. * @param[out] out_info Structure to which entry information is copied.
  617. */
  618. void nvs_entry_info(nvs_iterator_t iterator, nvs_entry_info_t *out_info);
  619. /**
  620. * @brief Release iterator
  621. *
  622. * @param[in] iterator Release iterator obtained from nvs_entry_find function. NULL argument is allowed.
  623. *
  624. */
  625. void nvs_release_iterator(nvs_iterator_t iterator);
  626. #ifdef __cplusplus
  627. } // extern "C"
  628. #endif
  629. #endif //ESP_NVS_H