nvs_handle.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
  41. * @param[in] value The value to set. Allowed types are the ones declared in ItemType as well as enums.
  42. * For strings, the maximum length (including null character) is
  43. * 4000 bytes.
  44. * Note that enums loose their type information when stored in NVS. Ensure that the correct
  45. * enum type is used during retrieval with \ref get_item!
  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 (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
  73. * @param value The output value. All integral types which are declared in ItemType as well as enums
  74. * are allowed. Note however that enums lost their type information when stored in NVS.
  75. * Ensure that the correct enum type is used during retrieval with \ref get_item!
  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 (NVS_KEY_NAME_MAX_SIZE-1) 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 (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
  128. * @param out_str/ Pointer to the output value.
  129. * out_blob
  130. * @param[inout] length A non-zero pointer to the variable holding the length of out_value.
  131. * It will be set to the actual length of the value
  132. * written. For nvs_get_str this includes the zero terminator.
  133. *
  134. * @return
  135. * - ESP_OK if the value was retrieved successfully
  136. * - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
  137. * - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
  138. * - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
  139. */
  140. virtual esp_err_t get_string(const char *key, char* out_str, size_t len) = 0;
  141. virtual esp_err_t get_blob(const char *key, void* out_blob, size_t len) = 0;
  142. /**
  143. * @brief Looks up the size of an entry's data.
  144. *
  145. * For strings, this size includes the zero terminator.
  146. */
  147. virtual esp_err_t get_item_size(ItemType datatype, const char *key, size_t &size) = 0;
  148. /**
  149. * @brief Erases an entry.
  150. */
  151. virtual esp_err_t erase_item(const char* key) = 0;
  152. /**
  153. * Erases all entries in the scope of this handle. The scope may vary, depending on the implementation.
  154. *
  155. * @not If you want to erase the whole nvs flash (partition), refer to \ref
  156. */
  157. virtual esp_err_t erase_all() = 0;
  158. /**
  159. * Commits all changes done through this handle so far.
  160. */
  161. virtual esp_err_t commit() = 0;
  162. /**
  163. * @brief Calculate all entries in the scope of the handle.
  164. *
  165. * @param[out] used_entries Returns amount of used entries from a namespace on success.
  166. *
  167. *
  168. * @return
  169. * - ESP_OK if the changes have been written successfully.
  170. * Return param used_entries will be filled valid value.
  171. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
  172. * Return param used_entries will be filled 0.
  173. * - ESP_ERR_INVALID_ARG if nvs_stats equal to NULL.
  174. * - Other error codes from the underlying storage driver.
  175. * Return param used_entries will be filled 0.
  176. */
  177. virtual esp_err_t get_used_entry_count(size_t& usedEntries) = 0;
  178. protected:
  179. virtual esp_err_t set_typed_item(ItemType datatype, const char *key, const void* data, size_t dataSize) = 0;
  180. virtual esp_err_t get_typed_item(ItemType datatype, const char *key, void* data, size_t dataSize) = 0;
  181. };
  182. /**
  183. * @brief Opens non-volatile storage and returns a handle object.
  184. *
  185. * The handle is automatically closed on desctruction. The scope of the handle is the namespace ns_name
  186. * in a particular partition partition_name.
  187. * The parameters partition_name, ns_name and open_mode have the same meaning and restrictions as the parameters
  188. * part_name, name and open_mode in \ref nvs_open_from_partition, respectively.
  189. *
  190. * @param err an optional pointer to an esp_err_t result of the open operation, having the same meaning as the return
  191. * value in \ref nvs_open_from_partition:
  192. * - ESP_OK if storage handle was opened successfully
  193. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
  194. * - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "nvs" is not found
  195. * - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
  196. * mode is NVS_READONLY
  197. * - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
  198. * - other error codes from the underlying storage driver
  199. *
  200. * @return shared pointer of an nvs handle on success, an empty shared pointer otherwise
  201. */
  202. std::unique_ptr<NVSHandle> open_nvs_handle_from_partition(const char *partition_name,
  203. const char *ns_name,
  204. nvs_open_mode_t open_mode,
  205. esp_err_t *err = nullptr);
  206. /**
  207. * @brief This function does the same as \ref open_nvs_handle_from_partition but uses the default nvs partition
  208. * instead of a partition_name parameter.
  209. */
  210. std::unique_ptr<NVSHandle> open_nvs_handle(const char *ns_name,
  211. nvs_open_mode_t open_mode,
  212. esp_err_t *err = nullptr);
  213. // Helper functions for template usage
  214. /**
  215. * Help to translate all integral types into ItemType.
  216. */
  217. template<typename T, typename std::enable_if<std::is_integral<T>::value, void*>::type = nullptr>
  218. constexpr ItemType itemTypeOf()
  219. {
  220. return static_cast<ItemType>(((std::is_signed<T>::value)?0x10:0x00) | sizeof(T));
  221. }
  222. /**
  223. * Help to translate all enum types into integral ItemType.
  224. */
  225. template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
  226. constexpr ItemType itemTypeOf()
  227. {
  228. return static_cast<ItemType>(((std::is_signed<T>::value)?0x10:0x00) | sizeof(T));
  229. }
  230. template<typename T>
  231. constexpr ItemType itemTypeOf(const T&)
  232. {
  233. return itemTypeOf<T>();
  234. }
  235. // Template Implementations
  236. template<typename T>
  237. esp_err_t NVSHandle::set_item(const char *key, T value) {
  238. return set_typed_item(itemTypeOf(value), key, &value, sizeof(value));
  239. }
  240. template<typename T>
  241. esp_err_t NVSHandle::get_item(const char *key, T &value) {
  242. return get_typed_item(itemTypeOf(value), key, &value, sizeof(value));
  243. }
  244. } // nvs
  245. #endif // NVS_HANDLE_HPP_