nvs.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 NVS_DEFAULT_PART_NAME "nvs" /*!< Default partition name of the NVS partition in the partition table */
  43. /**
  44. * @brief Mode of opening the non-volatile storage
  45. *
  46. */
  47. typedef enum {
  48. NVS_READONLY, /*!< Read only */
  49. NVS_READWRITE /*!< Read and write */
  50. } nvs_open_mode;
  51. /**
  52. * @brief Open non-volatile storage with a given namespace from the default NVS partition
  53. *
  54. * Multiple internal ESP-IDF and third party application modules can store
  55. * their key-value pairs in the NVS module. In order to reduce possible
  56. * conflicts on key names, each module can use its own namespace.
  57. * The default NVS partition is the one that is labelled "nvs" in the partition
  58. * table.
  59. *
  60. * @param[in] name Namespace name. Maximal length is determined by the
  61. * underlying implementation, but is guaranteed to be
  62. * at least 15 characters. Shouldn't be empty.
  63. * @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
  64. * open a handle for reading only. All write requests will
  65. * be rejected for this handle.
  66. * @param[out] out_handle If successful (return code is zero), handle will be
  67. * returned in this argument.
  68. *
  69. * @return
  70. * - ESP_OK if storage handle was opened successfully
  71. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  72. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "nvs" is not found
  73. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  74. * mode is NVS_READONLY
  75. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  76. * - other error codes from the underlying storage driver
  77. */
  78. esp_err_t nvs_open(const char* name, nvs_open_mode open_mode, nvs_handle *out_handle);
  79. /**
  80. * @brief Open non-volatile storage with a given namespace from specified partition
  81. *
  82. * The behaviour is same as nvs_open() API. However this API can operate on a specified NVS
  83. * partition instead of default NVS partition. Note that the specified partition must be registered
  84. * with NVS using nvs_flash_init_partition() API.
  85. *
  86. * @param[in] part_name Label (name) of the partition of interest for object read/write/erase
  87. * @param[in] name Namespace name. Maximal length is determined by the
  88. * underlying implementation, but is guaranteed to be
  89. * at least 15 characters. Shouldn't be empty.
  90. * @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
  91. * open a handle for reading only. All write requests will
  92. * be rejected for this handle.
  93. * @param[out] out_handle If successful (return code is zero), handle will be
  94. * returned in this argument.
  95. *
  96. * @return
  97. * - ESP_OK if storage handle was opened successfully
  98. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  99. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with specified name is not found
  100. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  101. * mode is NVS_READONLY
  102. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  103. * - other error codes from the underlying storage driver
  104. */
  105. esp_err_t nvs_open_from_partition(const char *part_name, const char* name, nvs_open_mode open_mode, nvs_handle *out_handle);
  106. /**@{*/
  107. /**
  108. * @brief set value for given key
  109. *
  110. * This family of functions set value for the key, given its name. Note that
  111. * actual storage will not be updated until nvs_commit function is called.
  112. *
  113. * @param[in] handle Handle obtained from nvs_open function.
  114. * Handles that were opened read only cannot be used.
  115. * @param[in] key Key name. Maximal length is determined by the underlying
  116. * implementation, but is guaranteed to be at least
  117. * 15 characters. Shouldn't be empty.
  118. * @param[in] value The value to set.
  119. * For strings, the maximum length (including null character) is
  120. * 1984 bytes.
  121. *
  122. * @return
  123. * - ESP_OK if value was set successfully
  124. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  125. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  126. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  127. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  128. * underlying storage to save the value
  129. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  130. * write operation has failed. The value was written however, and
  131. * update will be finished after re-initialization of nvs, provided that
  132. * flash operation doesn't fail again.
  133. * - ESP_ERR_NVS_VALUE_TOO_LONG if the string value is too long
  134. */
  135. esp_err_t nvs_set_i8 (nvs_handle handle, const char* key, int8_t value);
  136. esp_err_t nvs_set_u8 (nvs_handle handle, const char* key, uint8_t value);
  137. esp_err_t nvs_set_i16 (nvs_handle handle, const char* key, int16_t value);
  138. esp_err_t nvs_set_u16 (nvs_handle handle, const char* key, uint16_t value);
  139. esp_err_t nvs_set_i32 (nvs_handle handle, const char* key, int32_t value);
  140. esp_err_t nvs_set_u32 (nvs_handle handle, const char* key, uint32_t value);
  141. esp_err_t nvs_set_i64 (nvs_handle handle, const char* key, int64_t value);
  142. esp_err_t nvs_set_u64 (nvs_handle handle, const char* key, uint64_t value);
  143. esp_err_t nvs_set_str (nvs_handle handle, const char* key, const char* value);
  144. /**@}*/
  145. /**
  146. * @brief set variable length binary value for given key
  147. *
  148. * This family of functions set value for the key, given its name. Note that
  149. * actual storage will not be updated until nvs_commit function is called.
  150. *
  151. * @param[in] handle Handle obtained from nvs_open function.
  152. * Handles that were opened read only cannot be used.
  153. * @param[in] key Key name. Maximal length is 15 characters. Shouldn't be empty.
  154. * @param[in] value The value to set.
  155. * @param[in] length length of binary value to set, in bytes; Maximum length is
  156. * 1984 bytes.
  157. *
  158. * @return
  159. * - ESP_OK if value was set successfully
  160. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  161. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  162. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  163. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  164. * underlying storage to save the value
  165. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  166. * write operation has failed. The value was written however, and
  167. * update will be finished after re-initialization of nvs, provided that
  168. * flash operation doesn't fail again.
  169. * - ESP_ERR_NVS_VALUE_TOO_LONG if the value is too long
  170. */
  171. esp_err_t nvs_set_blob(nvs_handle handle, const char* key, const void* value, size_t length);
  172. /**@{*/
  173. /**
  174. * @brief get value for given key
  175. *
  176. * These functions retrieve value for the key, given its name. If key does not
  177. * exist, or the requested variable type doesn't match the type which was used
  178. * when setting a value, an error is returned.
  179. *
  180. * In case of any error, out_value is not modified.
  181. *
  182. * All functions expect out_value to be a pointer to an already allocated variable
  183. * of the given type.
  184. *
  185. * \code{c}
  186. * // Example of using nvs_get_i32:
  187. * int32_t max_buffer_size = 4096; // default value
  188. * esp_err_t err = nvs_get_i32(my_handle, "max_buffer_size", &max_buffer_size);
  189. * assert(err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND);
  190. * // if ESP_ERR_NVS_NOT_FOUND was returned, max_buffer_size will still
  191. * // have its default value.
  192. *
  193. * \endcode
  194. *
  195. * @param[in] handle Handle obtained from nvs_open function.
  196. * @param[in] key Key name. Maximal length is determined by the underlying
  197. * implementation, but is guaranteed to be at least
  198. * 15 characters. Shouldn't be empty.
  199. * @param out_value Pointer to the output value.
  200. * May be NULL for nvs_get_str and nvs_get_blob, in this
  201. * case required length will be returned in length argument.
  202. *
  203. * @return
  204. * - ESP_OK if the value was retrieved successfully
  205. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  206. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  207. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  208. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  209. */
  210. esp_err_t nvs_get_i8 (nvs_handle handle, const char* key, int8_t* out_value);
  211. esp_err_t nvs_get_u8 (nvs_handle handle, const char* key, uint8_t* out_value);
  212. esp_err_t nvs_get_i16 (nvs_handle handle, const char* key, int16_t* out_value);
  213. esp_err_t nvs_get_u16 (nvs_handle handle, const char* key, uint16_t* out_value);
  214. esp_err_t nvs_get_i32 (nvs_handle handle, const char* key, int32_t* out_value);
  215. esp_err_t nvs_get_u32 (nvs_handle handle, const char* key, uint32_t* out_value);
  216. esp_err_t nvs_get_i64 (nvs_handle handle, const char* key, int64_t* out_value);
  217. esp_err_t nvs_get_u64 (nvs_handle handle, const char* key, uint64_t* out_value);
  218. /**@}*/
  219. /**
  220. * @brief get value for given key
  221. *
  222. * These functions retrieve value for the key, given its name. If key does not
  223. * exist, or the requested variable type doesn't match the type which was used
  224. * when setting a value, an error is returned.
  225. *
  226. * In case of any error, out_value is not modified.
  227. *
  228. * All functions expect out_value to be a pointer to an already allocated variable
  229. * of the given type.
  230. *
  231. * nvs_get_str and nvs_get_blob functions support WinAPI-style length queries.
  232. * To get the size necessary to store the value, call nvs_get_str or nvs_get_blob
  233. * with zero out_value and non-zero pointer to length. Variable pointed to
  234. * by length argument will be set to the required length. For nvs_get_str,
  235. * this length includes the zero terminator. When calling nvs_get_str and
  236. * nvs_get_blob with non-zero out_value, length has to be non-zero and has to
  237. * point to the length available in out_value.
  238. * It is suggested that nvs_get/set_str is used for zero-terminated C strings, and
  239. * nvs_get/set_blob used for arbitrary data structures.
  240. *
  241. * \code{c}
  242. * // Example (without error checking) of using nvs_get_str to get a string into dynamic array:
  243. * size_t required_size;
  244. * nvs_get_str(my_handle, "server_name", NULL, &required_size);
  245. * char* server_name = malloc(required_size);
  246. * nvs_get_str(my_handle, "server_name", server_name, &required_size);
  247. *
  248. * // Example (without error checking) of using nvs_get_blob to get a binary data
  249. * into a static array:
  250. * uint8_t mac_addr[6];
  251. * size_t size = sizeof(mac_addr);
  252. * nvs_get_blob(my_handle, "dst_mac_addr", mac_addr, &size);
  253. * \endcode
  254. *
  255. * @param[in] handle Handle obtained from nvs_open function.
  256. * @param[in] key Key name. Maximal length is determined by the underlying
  257. * implementation, but is guaranteed to be at least
  258. * 15 characters. Shouldn't be empty.
  259. * @param out_value Pointer to the output value.
  260. * May be NULL for nvs_get_str and nvs_get_blob, in this
  261. * case required length will be returned in length argument.
  262. * @param[inout] length A non-zero pointer to the variable holding the length of out_value.
  263. * In case out_value a zero, will be set to the length
  264. * required to hold the value. In case out_value is not
  265. * zero, will be set to the actual length of the value
  266. * written. For nvs_get_str this includes zero terminator.
  267. *
  268. * @return
  269. * - ESP_OK if the value was retrieved successfully
  270. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  271. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  272. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  273. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  274. */
  275. /**@{*/
  276. esp_err_t nvs_get_str (nvs_handle handle, const char* key, char* out_value, size_t* length);
  277. esp_err_t nvs_get_blob(nvs_handle handle, const char* key, void* out_value, size_t* length);
  278. /**@}*/
  279. /**
  280. * @brief Erase key-value pair with given key name.
  281. *
  282. * Note that actual storage may not be updated until nvs_commit function is called.
  283. *
  284. * @param[in] handle Storage handle obtained with nvs_open.
  285. * Handles that were opened read only cannot be used.
  286. *
  287. * @param[in] key Key name. Maximal length is determined by the underlying
  288. * implementation, but is guaranteed to be at least
  289. * 15 characters. Shouldn't be empty.
  290. *
  291. * @return
  292. * - ESP_OK if erase operation was successful
  293. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  294. * - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
  295. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  296. * - other error codes from the underlying storage driver
  297. */
  298. esp_err_t nvs_erase_key(nvs_handle handle, const char* key);
  299. /**
  300. * @brief Erase all key-value pairs in a namespace
  301. *
  302. * Note that actual storage may not be updated until nvs_commit function is called.
  303. *
  304. * @param[in] handle Storage handle obtained with nvs_open.
  305. * Handles that were opened read only cannot be used.
  306. *
  307. * @return
  308. * - ESP_OK if erase operation was successful
  309. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  310. * - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
  311. * - other error codes from the underlying storage driver
  312. */
  313. esp_err_t nvs_erase_all(nvs_handle handle);
  314. /**
  315. * @brief Write any pending changes to non-volatile storage
  316. *
  317. * After setting any values, nvs_commit() must be called to ensure changes are written
  318. * to non-volatile storage. Individual implementations may write to storage at other times,
  319. * but this is not guaranteed.
  320. *
  321. * @param[in] handle Storage handle obtained with nvs_open.
  322. * Handles that were opened read only cannot be used.
  323. *
  324. * @return
  325. * - ESP_OK if the changes have been written successfully
  326. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  327. * - other error codes from the underlying storage driver
  328. */
  329. esp_err_t nvs_commit(nvs_handle handle);
  330. /**
  331. * @brief Close the storage handle and free any allocated resources
  332. *
  333. * This function should be called for each handle opened with nvs_open once
  334. * the handle is not in use any more. Closing the handle may not automatically
  335. * write the changes to nonvolatile storage. This has to be done explicitly using
  336. * nvs_commit function.
  337. * Once this function is called on a handle, the handle should no longer be used.
  338. *
  339. * @param[in] handle Storage handle to close
  340. */
  341. void nvs_close(nvs_handle handle);
  342. #ifdef __cplusplus
  343. } // extern "C"
  344. #endif
  345. #endif //ESP_NVS_H