nvs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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
  27. #define ESP_ERR_NVS_NOT_INITIALIZED (ESP_ERR_NVS_BASE + 0x01)
  28. #define ESP_ERR_NVS_NOT_FOUND (ESP_ERR_NVS_BASE + 0x02)
  29. #define ESP_ERR_NVS_TYPE_MISMATCH (ESP_ERR_NVS_BASE + 0x03)
  30. #define ESP_ERR_NVS_READ_ONLY (ESP_ERR_NVS_BASE + 0x04)
  31. #define ESP_ERR_NVS_NOT_ENOUGH_SPACE (ESP_ERR_NVS_BASE + 0x05)
  32. #define ESP_ERR_NVS_INVALID_NAME (ESP_ERR_NVS_BASE + 0x06)
  33. #define ESP_ERR_NVS_INVALID_HANDLE (ESP_ERR_NVS_BASE + 0x07)
  34. #define ESP_ERR_NVS_REMOVE_FAILED (ESP_ERR_NVS_BASE + 0x08)
  35. #define ESP_ERR_NVS_KEY_TOO_LONG (ESP_ERR_NVS_BASE + 0x09)
  36. #define ESP_ERR_NVS_PAGE_FULL (ESP_ERR_NVS_BASE + 0x0a)
  37. #define ESP_ERR_NVS_INVALID_STATE (ESP_ERR_NVS_BASE + 0x0b)
  38. #define ESP_ERR_NVS_INVALID_LENGTH (ESP_ERR_NVS_BASE + 0x0c)
  39. typedef enum {
  40. NVS_READONLY,
  41. NVS_READWRITE
  42. } nvs_open_mode;
  43. /**
  44. * @brief Open non-volatile storage with a given namespace
  45. *
  46. * Multiple internal ESP-IDF and third party application modules can store
  47. * their key-value pairs in the NVS module. In order to reduce possible
  48. * conflicts on key names, each module can use its own namespace.
  49. *
  50. * @param[in] name Namespace name. Maximal length is determined by the
  51. * underlying implementation, but is guaranteed to be
  52. * at least 16 characters. Shouldn't be empty.
  53. * @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
  54. * open a handle for reading only. All write requests will
  55. * be rejected for this handle.
  56. * @param[out] out_handle If successful (return code is zero), handle will be
  57. * returned in this argument.
  58. *
  59. * @return - ESP_OK if storage handle was opened successfully
  60. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  61. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  62. * mode is NVS_READONLY
  63. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  64. * - other error codes from the underlying storage driver
  65. */
  66. esp_err_t nvs_open(const char* name, nvs_open_mode open_mode, nvs_handle *out_handle);
  67. /**
  68. * @brief nvs_set_X - set value for given key
  69. *
  70. * This family of functions set value for the key, given its name. Note that
  71. * actual storage will not be updated until nvs_commit function is called.
  72. *
  73. * @param[in] handle Handle obtained from nvs_open function. If the handle was
  74. * opened with read_only set to true, nvs_set_X functions will
  75. * fail with ESP_ERR_NVS_READONLY.
  76. * @param[in] key Key name. Maximal length is determined by the underlying
  77. * implementation, but is guaranteed to be at least
  78. * 16 characters. Shouldn't be empty.
  79. * @param[in] value The value to set.
  80. * @param[in] length For nvs_set_blob: length of binary value to set, in bytes.
  81. *
  82. * @return - ESP_OK if value was set successfully
  83. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  84. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  85. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  86. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  87. * underlying storage to save the value
  88. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  89. * write operation has failed. The value was written however, and
  90. * update will be finished after re-initialization of nvs, provided that
  91. * flash operation doesn't fail again.
  92. */
  93. esp_err_t nvs_set_i8 (nvs_handle handle, const char* key, int8_t value);
  94. esp_err_t nvs_set_u8 (nvs_handle handle, const char* key, uint8_t value);
  95. esp_err_t nvs_set_i16 (nvs_handle handle, const char* key, int16_t value);
  96. esp_err_t nvs_set_u16 (nvs_handle handle, const char* key, uint16_t value);
  97. esp_err_t nvs_set_i32 (nvs_handle handle, const char* key, int32_t value);
  98. esp_err_t nvs_set_u32 (nvs_handle handle, const char* key, uint32_t value);
  99. esp_err_t nvs_set_i64 (nvs_handle handle, const char* key, int64_t value);
  100. esp_err_t nvs_set_u64 (nvs_handle handle, const char* key, uint64_t value);
  101. esp_err_t nvs_set_str (nvs_handle handle, const char* key, const char* value);
  102. esp_err_t nvs_set_blob(nvs_handle handle, const char* key, const void* value, size_t length);
  103. /**
  104. * @brief nvs_get_X - get value for given key
  105. *
  106. * These functions retrieve value for the key, given its name. If key does not
  107. * exist, or the requested variable type doesn't match the type which was used
  108. * when setting a value, an error is returned.
  109. *
  110. * In case of any error, out_value is not modified.
  111. *
  112. * All functions expect out_value to be a pointer to an already allocated variable
  113. * of the given type.
  114. * Additionally, nvs_get_str and nvs_get_blob support WinAPI-style length queries.
  115. * To get the size necessary to store the value, call nvs_get_str or nvs_get_blob
  116. * with zero out_value and non-zero pointer to length. Variable pointed to
  117. * by length argument will be set to the required length. For nvs_get_str,
  118. * this length includes the zero terminator. When calling nvs_get_str and
  119. * nvs_get_blob with non-zero out_value, length has to be non-zero and has to
  120. * point to the length available in out_value.
  121. * It is suggested that nvs_get/set_str is used for zero-terminated C strings, and
  122. * nvs_get/set_blob used for arbitrary data structures.
  123. *
  124. * Example of using nvs_get_i32:
  125. * int32_t max_buffer_size = 4096; // default value
  126. * esp_err_t err = nvs_get_i32(my_handle, "max_buffer_size", &max_buffer_size);
  127. * assert(err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND);
  128. * // if ESP_ERR_NVS_NOT_FOUND was returned, max_buffer_size will still
  129. * // have its default value.
  130. *
  131. * Example (without error checking) of using nvs_get_str to get a string into dynamic array:
  132. * size_t required_size;
  133. * nvs_get_str(my_handle, "server_name", NULL, &required_size);
  134. * char* server_name = malloc(required_size);
  135. * nvs_get_str(my_handle, "server_name", server_name, &required_size);
  136. *
  137. * Example (without error checking) of using nvs_get_blob to get a binary data
  138. * into a static array:
  139. * uint8_t mac_addr[6];
  140. * size_t size = sizeof(mac_addr);
  141. * nvs_get_blob(my_handle, "dst_mac_addr", mac_addr, &size);
  142. *
  143. * @param[in] handle Handle obtained from nvs_open function.
  144. * @param[in] key Key name. Maximal length is determined by the underlying
  145. * implementation, but is guaranteed to be at least
  146. * 16 characters. Shouldn't be empty.
  147. * @param out_value Pointer to the output value.
  148. * May be NULL for nvs_get_str and nvs_get_blob, in this
  149. * case required length will be returned in length argument.
  150. * @param[inout] length For nvs_get_str and nvs_get_blob, non-zero pointer
  151. * to the variable holding the length of out_value.
  152. * In case out_value a zero, will be set to the length
  153. * required to hold the value. In case out_value is not
  154. * zero, will be set to the actual length of the value
  155. * written. For nvs_get_str this includes zero terminator.
  156. *
  157. * @return - ESP_OK if the value was retrieved successfully
  158. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  159. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  160. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  161. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  162. */
  163. esp_err_t nvs_get_i8 (nvs_handle handle, const char* key, int8_t* out_value);
  164. esp_err_t nvs_get_u8 (nvs_handle handle, const char* key, uint8_t* out_value);
  165. esp_err_t nvs_get_i16 (nvs_handle handle, const char* key, int16_t* out_value);
  166. esp_err_t nvs_get_u16 (nvs_handle handle, const char* key, uint16_t* out_value);
  167. esp_err_t nvs_get_i32 (nvs_handle handle, const char* key, int32_t* out_value);
  168. esp_err_t nvs_get_u32 (nvs_handle handle, const char* key, uint32_t* out_value);
  169. esp_err_t nvs_get_i64 (nvs_handle handle, const char* key, int64_t* out_value);
  170. esp_err_t nvs_get_u64 (nvs_handle handle, const char* key, uint64_t* out_value);
  171. esp_err_t nvs_get_str (nvs_handle handle, const char* key, char* out_value, size_t* length);
  172. esp_err_t nvs_get_blob(nvs_handle handle, const char* key, void* out_value, size_t* length);
  173. /**
  174. * @brief Write any pending changes to non-volatile storage
  175. *
  176. * After setting any values, nvs_commit() must be called to ensure changes are written
  177. * to non-volatile storage. Individual implementations may write to storage at other times,
  178. * but this is not guaranteed.
  179. *
  180. * @param[in] handle Storage handle obtained with nvs_open. If handle has to be
  181. * opened as not read only for this call to succeed.
  182. *
  183. * @return - ESP_OK if the changes have been written successfully
  184. * - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
  185. * - other error codes from the underlying storage driver
  186. */
  187. esp_err_t nvs_commit(nvs_handle handle);
  188. /**
  189. * @brief Close the storage handle and free any allocated resources
  190. *
  191. * This function should be called for each handle opened with nvs_open once
  192. * the handle is not in use any more. Closing the handle may not automatically
  193. * write the changes to nonvolatile storage. This has to be done explicitly using
  194. * nvs_commit function.
  195. * Once this function is called on a handle, the handle should no longer be used.
  196. *
  197. * @param[in] handle Storage handle to close
  198. */
  199. void nvs_close(nvs_handle handle);
  200. #ifdef __cplusplus
  201. } // extern "C"
  202. #endif
  203. #endif //ESP_NVS_H