nvs.h 37 KB

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