idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
nvs.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
22#include <idfxx/error>
23
24#include <expected>
25#include <functional>
26#include <memory>
27#include <optional>
28#include <string_view>
29#include <system_error>
30#include <vector>
31
32typedef uint32_t nvs_handle_t;
33typedef int esp_err_t;
34
35namespace idfxx {
36
42template<typename T>
43concept sized_integral =
44 std::same_as<T, uint8_t> || std::same_as<T, int8_t> || std::same_as<T, uint16_t> || std::same_as<T, int16_t> ||
45 std::same_as<T, uint32_t> || std::same_as<T, int32_t> || std::same_as<T, uint64_t> || std::same_as<T, int64_t>;
57class nvs {
58public:
62 enum class errc : esp_err_t {
63 // clang-format off
64 not_found = 0x1102,
65 type_mismatch = 0x1103,
66 read_only = 0x1104,
67 not_enough_space = 0x1105,
68 invalid_name = 0x1106,
69 invalid_handle = 0x1107,
70 remove_failed = 0x1108,
71 key_too_long = 0x1109,
72 invalid_state = 0x110b,
73 invalid_length = 0x110c,
74 no_free_pages = 0x110d,
75 value_too_long = 0x110e,
76 part_not_found = 0x110f,
77 new_version_found = 0x1110,
78 xts_encr_failed = 0x1111,
79 xts_decr_failed = 0x1112,
80 xts_cfg_failed = 0x1113,
81 xts_cfg_not_found = 0x1114,
82 encr_not_supported = 0x1115,
83 keys_not_initialized = 0x1116,
84 corrupt_key_part = 0x1117,
85 wrong_encryption = 0x1119,
86 // clang-format on
87 };
88
92 class error_category : public std::error_category {
93 public:
95 [[nodiscard]] const char* name() const noexcept override final;
96
98 [[nodiscard]] std::string message(int ec) const override final;
99 };
100
101 class flash;
102
111 [[nodiscard]] static result<std::unique_ptr<nvs>> make(std::string_view namespace_name, bool read_only = false);
112
113#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
123 [[nodiscard]] explicit nvs(std::string_view namespace_name, bool read_only = false);
124#endif
125
127
128 nvs(const nvs&) = delete;
129 nvs& operator=(const nvs&) = delete;
130 nvs(nvs&&) = delete;
131 nvs& operator=(nvs&&) = delete;
132
134 [[nodiscard]] bool is_writeable() const { return !_read_only; }
135
136#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
142 void commit() { unwrap(try_commit()); }
143
150 void erase(std::string_view key) { unwrap(try_erase(key)); }
151
158
166 void set_string(std::string_view key, std::string_view value) { unwrap(try_set_string(key, value)); }
167
175 [[nodiscard]] std::string get_string(std::string_view key) { return unwrap(try_get_string(key)); }
176
185 void set_blob(std::string_view key, const void* data, size_t length) { unwrap(try_set_blob(key, data, length)); }
186
194 void set_blob(std::string_view key, const std::vector<uint8_t>& data) { unwrap(try_set_blob(key, data)); }
195
203 [[nodiscard]] std::vector<uint8_t> get_blob(std::string_view key) { return unwrap(try_get_blob(key)); }
204
213 template<typename T>
214 requires sized_integral<T>
215 void set_value(std::string_view key, T value) {
216 unwrap(try_set_value(key, value));
217 }
218
227 template<typename T>
228 requires sized_integral<T>
229 [[nodiscard]] T get_value(std::string_view key) {
230 return unwrap(try_get_value<T>(key));
231 }
232#endif
233
238 [[nodiscard]] result<void> try_commit();
239
245 [[nodiscard]] result<void> try_erase(std::string_view key);
246
252
259 [[nodiscard]] result<void> try_set_string(std::string_view key, std::string_view value);
260
266 [[nodiscard]] result<std::string> try_get_string(std::string_view key);
267
275 [[nodiscard]] result<void> try_set_blob(std::string_view key, const void* data, size_t length);
276
278 [[nodiscard]] result<void> try_set_blob(std::string_view key, const std::vector<uint8_t>& data);
279
285 [[nodiscard]] result<std::vector<uint8_t>> try_get_blob(std::string_view key);
286
294 template<typename T>
295 requires sized_integral<T>
296 [[nodiscard]] result<void> try_set_value(std::string_view key, T value);
297
304 template<typename T>
305 requires sized_integral<T>
306 [[nodiscard]] result<T> try_get_value(std::string_view key);
307
308private:
309 nvs(nvs_handle_t handle, bool read_only);
310
311 nvs_handle_t _handle;
312 bool _read_only;
313};
314
320[[nodiscard]] const nvs::error_category& nvs_category() noexcept;
321
326[[nodiscard]] inline std::error_code make_error_code(nvs::errc e) noexcept {
327 return {std::to_underlying(e), nvs_category()};
328}
329
340[[nodiscard]] std::unexpected<std::error_code> nvs_error(esp_err_t e) noexcept;
341
343public:
344 constexpr static size_t key_size = 32;
350 std::array<uint8_t, key_size> eky;
351 std::array<uint8_t, key_size> tky;
352 };
353
354public:
355 // Non-instantiable
356 flash() = delete;
357
359 struct insecure_t {
360 explicit insecure_t() = default;
361 };
362
364 constexpr static insecure_t insecure{};
365
366#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
388 static void init() { unwrap(try_init()); }
389
401 static void init(const secure_config& cfg) { unwrap(try_init(cfg)); }
402
412 static void init(insecure_t) { unwrap(try_init(insecure)); }
413
422 static void init(std::string_view partition_label) { unwrap(try_init(partition_label)); }
423
433 static void init(std::string_view partition_label, const secure_config& cfg) {
434 unwrap(try_init(partition_label, cfg));
435 }
436
445 static void init(insecure_t, std::string_view partition_label) { unwrap(try_init(insecure, partition_label)); }
446#endif
447
473 [[nodiscard]] static result<void> try_init();
474
490 [[nodiscard]] static result<void> try_init(const secure_config& cfg);
491
505 [[nodiscard]] static result<void> try_init(insecure_t);
506
519 [[nodiscard]] static result<void> try_init(std::string_view partition_label);
520
534 [[nodiscard]] static result<void> try_init(std::string_view partition_label, const secure_config& cfg);
535
548 [[nodiscard]] static result<void> try_init(insecure_t, std::string_view partition_label);
549
550#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
556 static void deinit() { unwrap(try_deinit()); }
557
566 static void deinit(std::string_view partition_label) { unwrap(try_deinit(partition_label)); }
567#endif
568
574 [[nodiscard]] static result<void> try_deinit();
575
584 [[nodiscard]] static result<void> try_deinit(std::string_view partition_label);
585
586#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
592 static void erase() { unwrap(try_erase()); }
593
602 static void erase(std::string_view partition_label) { unwrap(try_erase(partition_label)); }
603#endif
604
610 [[nodiscard]] static result<void> try_erase();
611
620 [[nodiscard]] static result<void> try_erase(std::string_view partition_label);
621};
622
623} // namespace idfxx
624
626namespace std {
627template<>
628struct is_error_code_enum<idfxx::nvs::errc> : true_type {};
629} // namespace std
// end of idfxx_nvs
Error category for NVS errors.
Definition nvs.hpp:92
std::string message(int ec) const override final
Returns a human-readable message for the given error code.
const char * name() const noexcept override final
Returns the name of the error category.
static result< void > try_init(std::string_view partition_label, const secure_config &cfg)
Initialize NVS flash storage for the specified partition.
static result< void > try_init(const secure_config &cfg)
Initialize the default NVS partition with encryption.
static void init(insecure_t)
Initialize the default NVS partition without encryption.
Definition nvs.hpp:412
static result< void > try_deinit()
Deinitialize NVS storage for the default NVS partition.
static void init(std::string_view partition_label)
Initialize NVS flash storage for the specified partition.
Definition nvs.hpp:422
static void init(std::string_view partition_label, const secure_config &cfg)
Initialize NVS flash storage for the specified partition.
Definition nvs.hpp:433
static void init(const secure_config &cfg)
Initialize the default NVS partition with encryption.
Definition nvs.hpp:401
static void deinit()
Deinitialize NVS storage for the default NVS partition.
Definition nvs.hpp:556
static void erase(std::string_view partition_label)
Erase specified NVS partition.
Definition nvs.hpp:602
static result< void > try_init(insecure_t, std::string_view partition_label)
Initialize NVS flash storage for the specified partition without encryption.
static result< void > try_init()
Initializes the default NVS partition.
static result< void > try_erase(std::string_view partition_label)
Erases the specified NVS partition.
static result< void > try_erase()
Erases the default NVS partition.
static result< void > try_init(insecure_t)
Initialize the default NVS partition without encryption.
static void deinit(std::string_view partition_label)
Deinitialize NVS storage for the specified partition.
Definition nvs.hpp:566
static void init()
Initializes the default NVS partition.
Definition nvs.hpp:388
static result< void > try_deinit(std::string_view partition_label)
Deinitialize NVS storage for the specified partition.
static result< void > try_init(std::string_view partition_label)
Initialize NVS flash storage for the specified partition.
static void erase()
Erases the default NVS partition.
Definition nvs.hpp:592
static void init(insecure_t, std::string_view partition_label)
Initialize NVS flash storage for the specified partition without encryption.
Definition nvs.hpp:445
Non-Volatile Storage handle.
Definition nvs.hpp:57
result< T > try_get_value(std::string_view key)
Retrieves an integer value.
result< void > try_erase_all()
Erases all keys in the namespace.
T get_value(std::string_view key)
Retrieves an integer value.
Definition nvs.hpp:229
void commit()
Commits pending changes to flash.
Definition nvs.hpp:142
result< void > try_erase(std::string_view key)
Erases a key.
std::string get_string(std::string_view key)
Retrieves a string.
Definition nvs.hpp:175
result< std::string > try_get_string(std::string_view key)
Retrieves a string.
nvs(nvs &&)=delete
result< std::vector< uint8_t > > try_get_blob(std::string_view key)
Retrieves binary data.
void set_value(std::string_view key, T value)
Stores an integer value.
Definition nvs.hpp:215
static result< std::unique_ptr< nvs > > make(std::string_view namespace_name, bool read_only=false)
Opens a NVS namespace.
nvs & operator=(const nvs &)=delete
nvs(const nvs &)=delete
void set_blob(std::string_view key, const void *data, size_t length)
Stores binary data.
Definition nvs.hpp:185
result< void > try_commit()
Commits pending changes to flash.
void set_string(std::string_view key, std::string_view value)
Stores a string.
Definition nvs.hpp:166
result< void > try_set_blob(std::string_view key, const void *data, size_t length)
Stores binary data.
std::vector< uint8_t > get_blob(std::string_view key)
Retrieves binary data.
Definition nvs.hpp:203
bool is_writeable() const
Returns true if the handle allows writes.
Definition nvs.hpp:134
result< void > try_set_value(std::string_view key, T value)
Stores an integer value.
void erase(std::string_view key)
Erases a key.
Definition nvs.hpp:150
errc
Error codes for NVS operations.
Definition nvs.hpp:62
nvs(std::string_view namespace_name, bool read_only=false)
Opens a NVS namespace.
nvs & operator=(nvs &&)=delete
result< void > try_set_string(std::string_view key, std::string_view value)
Stores a string.
void set_blob(std::string_view key, const std::vector< uint8_t > &data)
Stores binary data.
Definition nvs.hpp:194
result< void > try_set_blob(std::string_view key, const std::vector< uint8_t > &data)
Stores binary data.
void erase_all()
Erases all keys in the namespace.
Definition nvs.hpp:157
int esp_err_t
Definition error.hpp:32
uint32_t nvs_handle_t
Definition nvs.hpp:32
int esp_err_t
Definition nvs.hpp:33
const nvs::error_category & nvs_category() noexcept
Returns a reference to the NVS error category singleton.
std::error_code make_error_code(errc e) noexcept
Creates a std::error_code from an idfxx::errc value.
Definition error.hpp:99
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:237
errc
IDFXX error codes.
Definition error.hpp:43
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:118
std::unexpected< std::error_code > nvs_error(esp_err_t e) noexcept
Creates an unexpected error from an ESP-IDF error code, mapping to NVS error codes where possible.
Tag type to indicate insecure (non-encrypted) initialization.
Definition nvs.hpp:359
Key for encryption and decryption.
Definition nvs.hpp:349
std::array< uint8_t, key_size > eky
Definition nvs.hpp:350
std::array< uint8_t, key_size > tky
Definition nvs.hpp:351