nvs_handle.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef NVS_HANDLE_HPP_
  2. #define NVS_HANDLE_HPP_
  3. #include <string>
  4. #include <memory>
  5. #include <type_traits>
  6. #include "nvs.h"
  7. namespace nvs {
  8. /**
  9. * The possible blob types. This is a helper definition for template functions.
  10. */
  11. enum class ItemType : uint8_t {
  12. U8 = NVS_TYPE_U8,
  13. I8 = NVS_TYPE_I8,
  14. U16 = NVS_TYPE_U16,
  15. I16 = NVS_TYPE_I16,
  16. U32 = NVS_TYPE_U32,
  17. I32 = NVS_TYPE_I32,
  18. U64 = NVS_TYPE_U64,
  19. I64 = NVS_TYPE_I64,
  20. SZ = NVS_TYPE_STR,
  21. BLOB = 0x41,
  22. BLOB_DATA = NVS_TYPE_BLOB,
  23. BLOB_IDX = 0x48,
  24. ANY = NVS_TYPE_ANY
  25. };
  26. /**
  27. * @brief A handle allowing nvs-entry related operations on the NVS.
  28. *
  29. * @note The scope of this handle may vary depending on the implementation, but normally would be the namespace of
  30. * a particular partition. Outside that scope, nvs entries can't be accessed/altered.
  31. */
  32. class NVSHandle {
  33. public:
  34. virtual ~NVSHandle() { }
  35. /**
  36. * @brief set value for given key
  37. *
  38. * Sets value for key. Note that physical storage will not be updated until nvs_commit function is called.
  39. *
  40. * @param[in] key Key name. Maximal length is determined by the underlying
  41. * implementation, but is guaranteed to be at least
  42. * 15 characters. Shouldn't be empty.
  43. * @param[in] value The value to set. Allowed types are the ones declared in ItemType.
  44. * For strings, the maximum length (including null character) is
  45. * 4000 bytes.
  46. *
  47. * @return
  48. * - ESP_OK if value was set successfully
  49. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  50. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  51. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  52. * underlying storage to save the value
  53. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  54. * write operation has failed. The value was written however, and
  55. * update will be finished after re-initialization of nvs, provided that
  56. * flash operation doesn't fail again.
  57. * - ESP_ERR_NVS_VALUE_TOO_LONG if the string value is too long
  58. */
  59. template<typename T>
  60. esp_err_t set_item(const char *key, T value);
  61. virtual
  62. esp_err_t set_string(const char *key, const char* value) = 0;
  63. /**
  64. * @brief get value for given key
  65. *
  66. * These functions retrieve value for the key, given its name. If key does not
  67. * exist, or the requested variable type doesn't match the type which was used
  68. * when setting a value, an error is returned.
  69. *
  70. * In case of any error, out_value is not modified.
  71. *
  72. * @param[in] key Key name. Maximal length is determined by the underlying
  73. * implementation, but is guaranteed to be at least
  74. * 15 characters. Shouldn't be empty.
  75. * @param value The output value.
  76. *
  77. * @return
  78. * - ESP_OK if the value was retrieved successfully
  79. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  80. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  81. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  82. */
  83. template<typename T>
  84. esp_err_t get_item(const char *key, T &value);
  85. /**
  86. * @brief set variable length binary value for given key
  87. *
  88. * This family of functions set value for the key, given its name. Note that
  89. * actual storage will not be updated until nvs_commit function is called.
  90. *
  91. * @param[in] key Key name. Maximal length is 15 characters. Shouldn't be empty.
  92. * @param[in] blob The blob value to set.
  93. * @param[in] len length of binary value to set, in bytes; Maximum length is
  94. * 508000 bytes or (97.6% of the partition size - 4000) bytes
  95. * whichever is lower.
  96. *
  97. * @return
  98. * - ESP_OK if value was set successfully
  99. * - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
  100. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  101. * - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
  102. * underlying storage to save the value
  103. * - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
  104. * write operation has failed. The value was written however, and
  105. * update will be finished after re-initialization of nvs, provided that
  106. * flash operation doesn't fail again.
  107. * - ESP_ERR_NVS_VALUE_TOO_LONG if the value is too long
  108. *
  109. * @note compare to \ref nvs_set_blob in nvs.h
  110. */
  111. virtual esp_err_t set_blob(const char *key, const void* blob, size_t len) = 0;
  112. /**
  113. * @brief get value for given key
  114. *
  115. * These functions retrieve the data of an entry, given its key. If key does not
  116. * exist, or the requested variable type doesn't match the type which was used
  117. * when setting a value, an error is returned.
  118. *
  119. * In case of any error, out_value is not modified.
  120. *
  121. * Both functions expect out_value to be a pointer to an already allocated variable
  122. * of the given type.
  123. *
  124. * It is suggested that nvs_get/set_str is used for zero-terminated C strings, and
  125. * nvs_get/set_blob used for arbitrary data structures.
  126. *
  127. * @param[in] key Key name. Maximal length is determined by the underlying
  128. * implementation, but is guaranteed to be at least
  129. * 15 characters. Shouldn't be empty.
  130. * @param out_str/ Pointer to the output value.
  131. * out_blob
  132. * @param[inout] length A non-zero pointer to the variable holding the length of out_value.
  133. * It will be set to the actual length of the value
  134. * written. For nvs_get_str this includes the zero terminator.
  135. *
  136. * @return
  137. * - ESP_OK if the value was retrieved successfully
  138. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  139. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  140. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  141. */
  142. virtual esp_err_t get_string(const char *key, char* out_str, size_t len) = 0;
  143. virtual esp_err_t get_blob(const char *key, void* out_blob, size_t len) = 0;
  144. /**
  145. * @brief Looks up the size of an entry's data.
  146. *
  147. * For strings, this size includes the zero terminator.
  148. */
  149. virtual esp_err_t get_item_size(ItemType datatype, const char *key, size_t &size) = 0;
  150. /**
  151. * @brief Erases an entry.
  152. */
  153. virtual esp_err_t erase_item(const char* key) = 0;
  154. /**
  155. * Erases all entries in the scope of this handle. The scope may vary, depending on the implementation.
  156. *
  157. * @not If you want to erase the whole nvs flash (partition), refer to \ref
  158. */
  159. virtual esp_err_t erase_all() = 0;
  160. /**
  161. * Commits all changes done through this handle so far.
  162. */
  163. virtual esp_err_t commit() = 0;
  164. /**
  165. * @brief Calculate all entries in the scope of the handle.
  166. *
  167. * @param[out] used_entries Returns amount of used entries from a namespace on success.
  168. *
  169. *
  170. * @return
  171. * - ESP_OK if the changes have been written successfully.
  172. * Return param used_entries will be filled valid value.
  173. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
  174. * Return param used_entries will be filled 0.
  175. * - ESP_ERR_INVALID_ARG if nvs_stats equal to NULL.
  176. * - Other error codes from the underlying storage driver.
  177. * Return param used_entries will be filled 0.
  178. */
  179. virtual esp_err_t get_used_entry_count(size_t& usedEntries) = 0;
  180. protected:
  181. virtual esp_err_t set_typed_item(ItemType datatype, const char *key, const void* data, size_t dataSize) = 0;
  182. virtual esp_err_t get_typed_item(ItemType datatype, const char *key, void* data, size_t dataSize) = 0;
  183. };
  184. /**
  185. * @brief Opens non-volatile storage and returns a handle object.
  186. *
  187. * The handle is automatically closed on desctruction. The scope of the handle is the namespace ns_name
  188. * in a particular partition partition_name.
  189. * The parameters partition_name, ns_name and open_mode have the same meaning and restrictions as the parameters
  190. * part_name, name and open_mode in \ref nvs_open_from_partition, respectively.
  191. *
  192. * @param err an optional pointer to an esp_err_t result of the open operation, having the same meaning as the return
  193. * value in \ref nvs_open_from_partition:
  194. * - ESP_OK if storage handle was opened successfully
  195. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  196. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "nvs" is not found
  197. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  198. * mode is NVS_READONLY
  199. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  200. * - other error codes from the underlying storage driver
  201. *
  202. * @return shared pointer of an nvs handle on success, an empty shared pointer otherwise
  203. */
  204. std::unique_ptr<NVSHandle> open_nvs_handle_from_partition(const char *partition_name,
  205. const char *ns_name,
  206. nvs_open_mode_t open_mode,
  207. esp_err_t *err = nullptr);
  208. /**
  209. * @brief This function does the same as \ref open_nvs_handle_from_partition but uses the default nvs partition
  210. * instead of a partition_name parameter.
  211. */
  212. std::unique_ptr<NVSHandle> open_nvs_handle(const char *ns_name,
  213. nvs_open_mode_t open_mode,
  214. esp_err_t *err = nullptr);
  215. // Helper functions for template usage
  216. template<typename T, typename std::enable_if<std::is_integral<T>::value, void*>::type = nullptr>
  217. constexpr ItemType itemTypeOf()
  218. {
  219. return static_cast<ItemType>(((std::is_signed<T>::value)?0x10:0x00) | sizeof(T));
  220. }
  221. template<typename T>
  222. constexpr ItemType itemTypeOf(const T&)
  223. {
  224. return itemTypeOf<T>();
  225. }
  226. // Template Implementations
  227. template<typename T>
  228. esp_err_t NVSHandle::set_item(const char *key, T value) {
  229. return set_typed_item(itemTypeOf(value), key, &value, sizeof(value));
  230. }
  231. template<typename T>
  232. esp_err_t NVSHandle::get_item(const char *key, T &value) {
  233. return get_typed_item(itemTypeOf(value), key, &value, sizeof(value));
  234. }
  235. } // nvs
  236. #endif // NVS_HANDLE_HPP_