nvs.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_err.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * Opaque pointer type representing non-volatile storage handle
  24. */
  25. typedef uint32_t nvs_handle;
  26. #define ESP_ERR_NVS_BASE 0x1100 /*!< Starting number of error codes */
  27. #define ESP_ERR_NVS_NOT_INITIALIZED (ESP_ERR_NVS_BASE + 0x01) /*!< The storage driver is not initialized */
  28. #define ESP_ERR_NVS_NOT_FOUND (ESP_ERR_NVS_BASE + 0x02) /*!< Id namespace doesn’t exist yet and mode is NVS_READONLY */
  29. #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 */
  30. #define ESP_ERR_NVS_READ_ONLY (ESP_ERR_NVS_BASE + 0x04) /*!< Storage handle was opened as read only */
  31. #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 */
  32. #define ESP_ERR_NVS_INVALID_NAME (ESP_ERR_NVS_BASE + 0x06) /*!< Namespace name doesn’t satisfy constraints */
  33. #define ESP_ERR_NVS_INVALID_HANDLE (ESP_ERR_NVS_BASE + 0x07) /*!< Handle has been closed or is NULL */
  34. #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. */
  35. #define ESP_ERR_NVS_KEY_TOO_LONG (ESP_ERR_NVS_BASE + 0x09) /*!< Key name is too long */
  36. #define ESP_ERR_NVS_PAGE_FULL (ESP_ERR_NVS_BASE + 0x0a) /*!< Internal error; never returned by nvs API functions */
  37. #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. */
  38. #define ESP_ERR_NVS_INVALID_LENGTH (ESP_ERR_NVS_BASE + 0x0c) /*!< String or blob length is not sufficient to store data */
  39. #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. */
  40. #define ESP_ERR_NVS_VALUE_TOO_LONG (ESP_ERR_NVS_BASE + 0x0e) /*!< String or blob length is longer than supported by the implementation */
  41. #define ESP_ERR_NVS_PART_NOT_FOUND (ESP_ERR_NVS_BASE + 0x0f) /*!< Partition with specified name is not found in the partition table */
  42. #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 */
  43. #define ESP_ERR_NVS_XTS_ENCR_FAILED (ESP_ERR_NVS_BASE + 0x11) /*!< XTS encryption failed while writing NVS entry */
  44. #define ESP_ERR_NVS_XTS_DECR_FAILED (ESP_ERR_NVS_BASE + 0x12) /*!< XTS decryption failed while reading NVS entry */
  45. #define ESP_ERR_NVS_XTS_CFG_FAILED (ESP_ERR_NVS_BASE + 0x13) /*!< XTS configuration setting failed */
  46. #define ESP_ERR_NVS_XTS_CFG_NOT_FOUND (ESP_ERR_NVS_BASE + 0x14) /*!< XTS configuration not found */
  47. #define ESP_ERR_NVS_ENCR_NOT_SUPPORTED (ESP_ERR_NVS_BASE + 0x15) /*!< NVS encryption is not supported in this version */
  48. #define ESP_ERR_NVS_KEYS_NOT_INITIALIZED (ESP_ERR_NVS_BASE + 0x16) /*!< NVS key partition is uninitialized */
  49. #define ESP_ERR_NVS_CORRUPT_KEY_PART (ESP_ERR_NVS_BASE + 0x17) /*!< NVS key partition is corrupt */
  50. #define NVS_DEFAULT_PART_NAME "nvs" /*!< Default partition name of the NVS partition in the partition table */
  51. /**
  52. * @brief Mode of opening the non-volatile storage
  53. *
  54. */
  55. typedef enum {
  56. NVS_READONLY, /*!< Read only */
  57. NVS_READWRITE /*!< Read and write */
  58. } nvs_open_mode;
  59. typedef enum {
  60. NVS_TYPE_U8 = 0x01,
  61. NVS_TYPE_I8 = 0x11,
  62. NVS_TYPE_U16 = 0x02,
  63. NVS_TYPE_I16 = 0x12,
  64. NVS_TYPE_U32 = 0x04,
  65. NVS_TYPE_I32 = 0x14,
  66. NVS_TYPE_U64 = 0x08,
  67. NVS_TYPE_I64 = 0x18,
  68. NVS_TYPE_STR = 0x21,
  69. NVS_TYPE_BLOB = 0x42,
  70. NVS_TYPE_ANY = 0xff // Must be last
  71. } nvs_type_t;
  72. /**
  73. * @brief Open non-volatile storage with a given namespace from the default NVS partition
  74. *
  75. * Multiple internal ESP-IDF and third party application modules can store
  76. * their key-value pairs in the NVS module. In order to reduce possible
  77. * conflicts on key names, each module can use its own namespace.
  78. * The default NVS partition is the one that is labelled "nvs" in the partition
  79. * table.
  80. *
  81. * @param[in] name Namespace name. Maximal length is determined by the
  82. * underlying implementation, but is guaranteed to be
  83. * at least 15 characters. Shouldn't be empty.
  84. * @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
  85. * open a handle for reading only. All write requests will
  86. * be rejected for this handle.
  87. * @param[out] out_handle If successful (return code is zero), handle will be
  88. * returned in this argument.
  89. *
  90. * @return
  91. * - ESP_OK if storage handle was opened successfully
  92. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  93. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "nvs" is not found
  94. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  95. * mode is NVS_READONLY
  96. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  97. * - other error codes from the underlying storage driver
  98. */
  99. esp_err_t nvs_open(const char* name, nvs_open_mode open_mode, nvs_handle *out_handle);
  100. /**
  101. * @brief Open non-volatile storage with a given namespace from specified partition
  102. *
  103. * The behaviour is same as nvs_open() API. However this API can operate on a specified NVS
  104. * partition instead of default NVS partition. Note that the specified partition must be registered
  105. * with NVS using nvs_flash_init_partition() API.
  106. *
  107. * @param[in] part_name Label (name) of the partition of interest for object read/write/erase
  108. * @param[in] name Namespace name. Maximal length is determined by the
  109. * underlying implementation, but is guaranteed to be
  110. * at least 15 characters. Shouldn't be empty.
  111. * @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
  112. * open a handle for reading only. All write requests will
  113. * be rejected for this handle.
  114. * @param[out] out_handle If successful (return code is zero), handle will be
  115. * returned in this argument.
  116. *
  117. * @return
  118. * - ESP_OK if storage handle was opened successfully
  119. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  120. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with specified name is not found
  121. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  122. * mode is NVS_READONLY
  123. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  124. * - other error codes from the underlying storage driver
  125. */
  126. esp_err_t nvs_open_from_partition(const char *part_name, const char* name, nvs_open_mode open_mode, nvs_handle *out_handle);
  127. /**@{*/
  128. /**
  129. * @brief set value for given key
  130. *
  131. * This family of functions set value for the key, given its name. Note that
  132. * actual storage will not be updated until nvs_commit function is called.
  133. *
  134. * @param[in] handle Handle obtained from nvs_open function.
  135. * Handles that were opened read only cannot be used.
  136. * @param[in] key Key name. Maximal length is determined by the underlying
  137. * implementation, but is guaranteed to be at least
  138. * 15 characters. Shouldn't be empty.
  139. * @param[in] value The value to set.
  140. * For strings, the maximum length (including null character) is
  141. * 4000 bytes.
  142. *
  143. * @return
  144. * - ESP_OK if value was set successfully
  145. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  146. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  147. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  148. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  149. * underlying storage to save the value
  150. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  151. * write operation has failed. The value was written however, and
  152. * update will be finished after re-initialization of nvs, provided that
  153. * flash operation doesn't fail again.
  154. * - ESP_ERR_NVS_VALUE_TOO_LONG if the string value is too long
  155. */
  156. esp_err_t nvs_set_i8 (nvs_handle handle, const char* key, int8_t value);
  157. esp_err_t nvs_set_u8 (nvs_handle handle, const char* key, uint8_t value);
  158. esp_err_t nvs_set_i16 (nvs_handle handle, const char* key, int16_t value);
  159. esp_err_t nvs_set_u16 (nvs_handle handle, const char* key, uint16_t value);
  160. esp_err_t nvs_set_i32 (nvs_handle handle, const char* key, int32_t value);
  161. esp_err_t nvs_set_u32 (nvs_handle handle, const char* key, uint32_t value);
  162. esp_err_t nvs_set_i64 (nvs_handle handle, const char* key, int64_t value);
  163. esp_err_t nvs_set_u64 (nvs_handle handle, const char* key, uint64_t value);
  164. esp_err_t nvs_set_str (nvs_handle handle, const char* key, const char* value);
  165. /**@}*/
  166. /**
  167. * @brief set variable length binary value for given key
  168. *
  169. * This family of functions set value for the key, given its name. Note that
  170. * actual storage will not be updated until nvs_commit function is called.
  171. *
  172. * @param[in] handle Handle obtained from nvs_open function.
  173. * Handles that were opened read only cannot be used.
  174. * @param[in] key Key name. Maximal length is 15 characters. Shouldn't be empty.
  175. * @param[in] value The value to set.
  176. * @param[in] length length of binary value to set, in bytes; Maximum length is
  177. * 508000 bytes or (97.6% of the partition size - 4000) bytes
  178. * whichever is lower.
  179. *
  180. * @return
  181. * - ESP_OK if value was set successfully
  182. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  183. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  184. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  185. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  186. * underlying storage to save the value
  187. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  188. * write operation has failed. The value was written however, and
  189. * update will be finished after re-initialization of nvs, provided that
  190. * flash operation doesn't fail again.
  191. * - ESP_ERR_NVS_VALUE_TOO_LONG if the value is too long
  192. */
  193. esp_err_t nvs_set_blob(nvs_handle handle, const char* key, const void* value, size_t length);
  194. /**@{*/
  195. /**
  196. * @brief get value for given key
  197. *
  198. * These functions retrieve value for the key, given its name. If key does not
  199. * exist, or the requested variable type doesn't match the type which was used
  200. * when setting a value, an error is returned.
  201. *
  202. * In case of any error, out_value is not modified.
  203. *
  204. * All functions expect out_value to be a pointer to an already allocated variable
  205. * of the given type.
  206. *
  207. * \code{c}
  208. * // Example of using nvs_get_i32:
  209. * int32_t max_buffer_size = 4096; // default value
  210. * esp_err_t err = nvs_get_i32(my_handle, "max_buffer_size", &max_buffer_size);
  211. * assert(err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND);
  212. * // if ESP_ERR_NVS_NOT_FOUND was returned, max_buffer_size will still
  213. * // have its default value.
  214. *
  215. * \endcode
  216. *
  217. * @param[in] handle Handle obtained from nvs_open function.
  218. * @param[in] key Key name. Maximal length is determined by the underlying
  219. * implementation, but is guaranteed to be at least
  220. * 15 characters. Shouldn't be empty.
  221. * @param out_value Pointer to the output value.
  222. * May be NULL for nvs_get_str and nvs_get_blob, in this
  223. * case required length will be returned in length argument.
  224. *
  225. * @return
  226. * - ESP_OK if the value was retrieved successfully
  227. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  228. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  229. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  230. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  231. */
  232. esp_err_t nvs_get_i8 (nvs_handle handle, const char* key, int8_t* out_value);
  233. esp_err_t nvs_get_u8 (nvs_handle handle, const char* key, uint8_t* out_value);
  234. esp_err_t nvs_get_i16 (nvs_handle handle, const char* key, int16_t* out_value);
  235. esp_err_t nvs_get_u16 (nvs_handle handle, const char* key, uint16_t* out_value);
  236. esp_err_t nvs_get_i32 (nvs_handle handle, const char* key, int32_t* out_value);
  237. esp_err_t nvs_get_u32 (nvs_handle handle, const char* key, uint32_t* out_value);
  238. esp_err_t nvs_get_i64 (nvs_handle handle, const char* key, int64_t* out_value);
  239. esp_err_t nvs_get_u64 (nvs_handle handle, const char* key, uint64_t* out_value);
  240. /**@}*/
  241. /**
  242. * @brief get value for given key
  243. *
  244. * These functions retrieve value for the key, given its name. If key does not
  245. * exist, or the requested variable type doesn't match the type which was used
  246. * when setting a value, an error is returned.
  247. *
  248. * In case of any error, out_value is not modified.
  249. *
  250. * All functions expect out_value to be a pointer to an already allocated variable
  251. * of the given type.
  252. *
  253. * nvs_get_str and nvs_get_blob functions support WinAPI-style length queries.
  254. * To get the size necessary to store the value, call nvs_get_str or nvs_get_blob
  255. * with zero out_value and non-zero pointer to length. Variable pointed to
  256. * by length argument will be set to the required length. For nvs_get_str,
  257. * this length includes the zero terminator. When calling nvs_get_str and
  258. * nvs_get_blob with non-zero out_value, length has to be non-zero and has to
  259. * point to the length available in out_value.
  260. * It is suggested that nvs_get/set_str is used for zero-terminated C strings, and
  261. * nvs_get/set_blob used for arbitrary data structures.
  262. *
  263. * \code{c}
  264. * // Example (without error checking) of using nvs_get_str to get a string into dynamic array:
  265. * size_t required_size;
  266. * nvs_get_str(my_handle, "server_name", NULL, &required_size);
  267. * char* server_name = malloc(required_size);
  268. * nvs_get_str(my_handle, "server_name", server_name, &required_size);
  269. *
  270. * // Example (without error checking) of using nvs_get_blob to get a binary data
  271. * into a static array:
  272. * uint8_t mac_addr[6];
  273. * size_t size = sizeof(mac_addr);
  274. * nvs_get_blob(my_handle, "dst_mac_addr", mac_addr, &size);
  275. * \endcode
  276. *
  277. * @param[in] handle Handle obtained from nvs_open function.
  278. * @param[in] key Key name. Maximal length is determined by the underlying
  279. * implementation, but is guaranteed to be at least
  280. * 15 characters. Shouldn't be empty.
  281. * @param out_value Pointer to the output value.
  282. * May be NULL for nvs_get_str and nvs_get_blob, in this
  283. * case required length will be returned in length argument.
  284. * @param[inout] length A non-zero pointer to the variable holding the length of out_value.
  285. * In case out_value a zero, will be set to the length
  286. * required to hold the value. In case out_value is not
  287. * zero, will be set to the actual length of the value
  288. * written. For nvs_get_str this includes zero terminator.
  289. *
  290. * @return
  291. * - ESP_OK if the value was retrieved successfully
  292. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  293. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  294. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  295. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  296. */
  297. /**@{*/
  298. esp_err_t nvs_get_str (nvs_handle handle, const char* key, char* out_value, size_t* length);
  299. esp_err_t nvs_get_blob(nvs_handle handle, const char* key, void* out_value, size_t* length);
  300. /**@}*/
  301. /**
  302. * @brief Erase key-value pair with given key name.
  303. *
  304. * Note that actual storage may not be updated until nvs_commit function is called.
  305. *
  306. * @param[in] handle Storage handle obtained with nvs_open.
  307. * Handles that were opened read only cannot be used.
  308. *
  309. * @param[in] key Key name. Maximal length is determined by the underlying
  310. * implementation, but is guaranteed to be at least
  311. * 15 characters. Shouldn't be empty.
  312. *
  313. * @return
  314. * - ESP_OK if erase operation was successful
  315. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  316. * - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
  317. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  318. * - other error codes from the underlying storage driver
  319. */
  320. esp_err_t nvs_erase_key(nvs_handle handle, const char* key);
  321. /**
  322. * @brief Erase all key-value pairs in a namespace
  323. *
  324. * Note that actual storage may not be updated until nvs_commit function is called.
  325. *
  326. * @param[in] handle Storage handle obtained with nvs_open.
  327. * Handles that were opened read only cannot be used.
  328. *
  329. * @return
  330. * - ESP_OK if erase operation was successful
  331. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  332. * - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
  333. * - other error codes from the underlying storage driver
  334. */
  335. esp_err_t nvs_erase_all(nvs_handle handle);
  336. /**
  337. * @brief Write any pending changes to non-volatile storage
  338. *
  339. * After setting any values, nvs_commit() must be called to ensure changes are written
  340. * to non-volatile storage. Individual implementations may write to storage at other times,
  341. * but this is not guaranteed.
  342. *
  343. * @param[in] handle Storage handle obtained with nvs_open.
  344. * Handles that were opened read only cannot be used.
  345. *
  346. * @return
  347. * - ESP_OK if the changes have been written successfully
  348. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  349. * - other error codes from the underlying storage driver
  350. */
  351. esp_err_t nvs_commit(nvs_handle handle);
  352. /**
  353. * @brief Close the storage handle and free any allocated resources
  354. *
  355. * This function should be called for each handle opened with nvs_open once
  356. * the handle is not in use any more. Closing the handle may not automatically
  357. * write the changes to nonvolatile storage. This has to be done explicitly using
  358. * nvs_commit function.
  359. * Once this function is called on a handle, the handle should no longer be used.
  360. *
  361. * @param[in] handle Storage handle to close
  362. */
  363. void nvs_close(nvs_handle handle);
  364. /**
  365. * @note Info about storage space NVS.
  366. */
  367. typedef struct {
  368. size_t used_entries; /**< Amount of used entries. */
  369. size_t free_entries; /**< Amount of free entries. */
  370. size_t total_entries; /**< Amount all available entries. */
  371. size_t namespace_count; /**< Amount name space. */
  372. } nvs_stats_t;
  373. /**
  374. * @brief Fill structure nvs_stats_t. It provides info about used memory the partition.
  375. *
  376. * This function calculates to runtime the number of used entries, free entries, total entries,
  377. * and amount namespace in partition.
  378. *
  379. * \code{c}
  380. * // Example of nvs_get_stats() to get the number of used entries and free entries:
  381. * nvs_stats_t nvs_stats;
  382. * nvs_get_stats(NULL, &nvs_stats);
  383. * printf("Count: UsedEntries = (%d), FreeEntries = (%d), AllEntries = (%d)\n",
  384. nvs_stats.used_entries, nvs_stats.free_entries, nvs_stats.total_entries);
  385. * \endcode
  386. *
  387. * @param[in] part_name Partition name NVS in the partition table.
  388. * If pass a NULL than will use NVS_DEFAULT_PART_NAME ("nvs").
  389. *
  390. * @param[out] nvs_stats Returns filled structure nvs_states_t.
  391. * It provides info about used memory the partition.
  392. *
  393. *
  394. * @return
  395. * - ESP_OK if the changes have been written successfully.
  396. * Return param nvs_stats will be filled.
  397. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "name" is not found.
  398. * Return param nvs_stats will be filled 0.
  399. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
  400. * Return param nvs_stats will be filled 0.
  401. * - ESP_ERR_INVALID_ARG if nvs_stats equal to NULL.
  402. * - ESP_ERR_INVALID_STATE if there is page with the status of INVALID.
  403. * Return param nvs_stats will be filled not with correct values because
  404. * not all pages will be counted. Counting will be interrupted at the first INVALID page.
  405. */
  406. esp_err_t nvs_get_stats(const char* part_name, nvs_stats_t* nvs_stats);
  407. /**
  408. * @brief Calculate all entries in a namespace.
  409. *
  410. * Note that to find out the total number of records occupied by the namespace,
  411. * add one to the returned value used_entries (if err is equal to ESP_OK).
  412. * Because the name space entry takes one entry.
  413. *
  414. * \code{c}
  415. * // Example of nvs_get_used_entry_count() to get amount of all key-value pairs in one namespace:
  416. * nvs_handle handle;
  417. * nvs_open("namespace1", NVS_READWRITE, &handle);
  418. * ...
  419. * size_t used_entries;
  420. * size_t total_entries_namespace;
  421. * if(nvs_get_used_entry_count(handle, &used_entries) == ESP_OK){
  422. * // the total number of records occupied by the namespace
  423. * total_entries_namespace = used_entries + 1;
  424. * }
  425. * \endcode
  426. *
  427. * @param[in] handle Handle obtained from nvs_open function.
  428. *
  429. * @param[out] used_entries Returns amount of used entries from a namespace.
  430. *
  431. *
  432. * @return
  433. * - ESP_OK if the changes have been written successfully.
  434. * Return param used_entries will be filled valid value.
  435. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
  436. * Return param used_entries will be filled 0.
  437. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL.
  438. * Return param used_entries will be filled 0.
  439. * - ESP_ERR_INVALID_ARG if nvs_stats equal to NULL.
  440. * - Other error codes from the underlying storage driver.
  441. * Return param used_entries will be filled 0.
  442. */
  443. esp_err_t nvs_get_used_entry_count(nvs_handle handle, size_t* used_entries);
  444. #ifdef __cplusplus
  445. } // extern "C"
  446. #endif
  447. #endif //ESP_NVS_H