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#include <idfxx/partition>
24
25#include <span>
26#include <string_view>
27#include <vector>
28
29typedef uint32_t nvs_handle_t;
30typedef int esp_err_t;
31
32namespace idfxx {
33
39template<typename T>
40concept sized_integral =
41 std::same_as<T, uint8_t> || std::same_as<T, int8_t> || std::same_as<T, uint16_t> || std::same_as<T, int16_t> ||
42 std::same_as<T, uint32_t> || std::same_as<T, int32_t> || std::same_as<T, uint64_t> || std::same_as<T, int64_t>;
58class nvs {
59public:
63 enum class errc : esp_err_t {
64 // clang-format off
65 not_initialized = 0x1101,
66 not_found = 0x1102,
67 type_mismatch = 0x1103,
68 read_only = 0x1104,
69 not_enough_space = 0x1105,
70 invalid_name = 0x1106,
71 invalid_handle = 0x1107,
72 remove_failed = 0x1108,
73 key_too_long = 0x1109,
74 invalid_state = 0x110b,
75 invalid_length = 0x110c,
76 no_free_pages = 0x110d,
77 value_too_long = 0x110e,
78 part_not_found = 0x110f,
79 new_version_found = 0x1110,
80 xts_encr_failed = 0x1111,
81 xts_decr_failed = 0x1112,
82 xts_cfg_failed = 0x1113,
83 xts_cfg_not_found = 0x1114,
84 encr_not_supported = 0x1115,
85 keys_not_initialized = 0x1116,
86 corrupt_key_part = 0x1117,
87 wrong_encryption = 0x1119,
88 // clang-format on
89 };
90
94 class error_category : public std::error_category {
95 public:
98
100 [[nodiscard]] std::string message(int ec) const override final;
101 };
102
103 class flash;
104
105#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
115 [[nodiscard]] explicit nvs(std::string_view namespace_name, bool read_only = false);
116#endif
117
126 [[nodiscard]] static result<nvs> make(std::string_view namespace_name, bool read_only = false);
127
128#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
139 [[nodiscard]] explicit nvs(const partition& part, std::string_view namespace_name, bool read_only = false);
140#endif
141
151 [[nodiscard]] static result<nvs>
152 make(const partition& part, std::string_view namespace_name, bool read_only = false);
153
155
156 nvs(const nvs&) = delete;
157 nvs& operator=(const nvs&) = delete;
158
160 nvs(nvs&& other) noexcept;
161
163 nvs& operator=(nvs&& other) noexcept;
164
166 [[nodiscard]] bool is_writeable() const { return !_read_only; }
167
168#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
174 void commit() { unwrap(try_commit()); }
175
182 void erase(std::string_view key) { unwrap(try_erase(key)); }
183
190
198 void set_string(std::string_view key, std::string_view value) { unwrap(try_set_string(key, value)); }
199
207 [[nodiscard]] std::string get_string(std::string_view key) { return unwrap(try_get_string(key)); }
208
217 void set_blob(std::string_view key, const void* data, size_t length) { unwrap(try_set_blob(key, data, length)); }
218
226 void set_blob(std::string_view key, std::span<const uint8_t> data) { unwrap(try_set_blob(key, data)); }
227
235 [[nodiscard]] std::vector<uint8_t> get_blob(std::string_view key) { return unwrap(try_get_blob(key)); }
236
245 template<typename T>
246 requires sized_integral<T>
247 void set_value(std::string_view key, T value) {
248 unwrap(try_set_value(key, value));
249 }
250
259 template<typename T>
260 requires sized_integral<T>
261 [[nodiscard]] T get_value(std::string_view key) {
262 return unwrap(try_get_value<T>(key));
263 }
264#endif
265
271
277 [[nodiscard]] result<void> try_erase(std::string_view key);
278
284
291 [[nodiscard]] result<void> try_set_string(std::string_view key, std::string_view value);
292
299
307 [[nodiscard]] result<void> try_set_blob(std::string_view key, const void* data, size_t length);
308
315 [[nodiscard]] result<void> try_set_blob(std::string_view key, std::span<const uint8_t> data);
316
323
331 template<typename T>
332 requires sized_integral<T>
333 [[nodiscard]] result<void> try_set_value(std::string_view key, T value);
334
341 template<typename T>
342 requires sized_integral<T>
343 [[nodiscard]] result<T> try_get_value(std::string_view key);
344
345private:
346 nvs(nvs_handle_t handle, bool read_only);
347
348 nvs_handle_t _handle = 0;
349 bool _read_only = false;
350};
351
358
364 return {std::to_underlying(e), nvs_category()};
365}
366
377[[nodiscard]] std::unexpected<std::error_code> nvs_error(esp_err_t e);
378
380public:
381 constexpr static size_t key_size = 32;
387 std::array<uint8_t, key_size> eky;
388 std::array<uint8_t, key_size> tky;
389 };
390
391public:
392 // Non-instantiable
393 flash() = delete;
394
396 struct insecure_t {
397 explicit insecure_t() = default;
398 };
399
401 constexpr static insecure_t insecure{};
402
403#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
425 static void init() { unwrap(try_init()); }
426
438 static void init(const secure_config& cfg) { unwrap(try_init(cfg)); }
439
449 static void init(insecure_t) { unwrap(try_init(insecure)); }
450
459 static void init(std::string_view partition_label) { unwrap(try_init(partition_label)); }
460
470 static void init(std::string_view partition_label, const secure_config& cfg) {
471 unwrap(try_init(partition_label, cfg));
472 }
473
482 static void init(insecure_t, std::string_view partition_label) { unwrap(try_init(insecure, partition_label)); }
483
492 static void init(const partition& part) { unwrap(try_init(part)); }
493
503 static void init(const partition& part, const secure_config& cfg) { unwrap(try_init(part, cfg)); }
504
513 static void init(insecure_t, const partition& part) { unwrap(try_init(insecure, part)); }
514#endif
515
542
559
574
587 [[nodiscard]] static result<void> try_init(std::string_view partition_label);
588
602 [[nodiscard]] static result<void> try_init(std::string_view partition_label, const secure_config& cfg);
603
617
629
642
654
655#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
661 static void deinit() { unwrap(try_deinit()); }
662
671 static void deinit(std::string_view partition_label) { unwrap(try_deinit(partition_label)); }
672#endif
673
680
689 static result<void> try_deinit(std::string_view partition_label);
690
691#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
697 static void erase() { unwrap(try_erase()); }
698
707 static void erase(std::string_view partition_label) { unwrap(try_erase(partition_label)); }
708
717 static void erase(const partition& part) { unwrap(try_erase(part)); }
718
730 return unwrap(try_generate_keys(key_part));
731 }
732
744 return unwrap(try_read_security_cfg(key_part));
745 }
746#endif
747
754
763 [[nodiscard]] static result<void> try_erase(std::string_view partition_label);
764
773
783
794};
795
796} // namespace idfxx
797
799namespace std {
800template<>
802} // namespace std
// end of idfxx_nvs
Error category for NVS errors.
Definition nvs.hpp:94
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< secure_config > try_generate_keys(const partition &key_part)
Generate NVS encryption keys and store them on a key partition.
static secure_config generate_keys(const partition &key_part)
Generate NVS encryption keys and store them on a key partition.
Definition nvs.hpp:729
static secure_config read_security_cfg(const partition &key_part)
Read NVS encryption keys from a key partition.
Definition nvs.hpp:743
static result< void > try_init(const secure_config &cfg)
Initialize the default NVS partition with encryption.
static result< void > try_erase(const partition &part)
Erases the specified NVS partition.
static void init(insecure_t)
Initialize the default NVS partition without encryption.
Definition nvs.hpp:449
static result< void > try_deinit()
Deinitialize NVS storage for the default NVS partition.
static result< secure_config > try_read_security_cfg(const partition &key_part)
Read NVS encryption keys from a key partition.
static void init(std::string_view partition_label)
Initialize NVS flash storage for the specified partition.
Definition nvs.hpp:459
static void init(std::string_view partition_label, const secure_config &cfg)
Initialize NVS flash storage for the specified partition.
Definition nvs.hpp:470
static void init(const secure_config &cfg)
Initialize the default NVS partition with encryption.
Definition nvs.hpp:438
static void deinit()
Deinitialize NVS storage for the default NVS partition.
Definition nvs.hpp:661
static void erase(std::string_view partition_label)
Erase specified NVS partition.
Definition nvs.hpp:707
static result< void > try_init(const partition &part)
Initialize NVS flash storage for the specified partition.
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 void erase(const partition &part)
Erase the specified NVS partition.
Definition nvs.hpp:717
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 init(insecure_t, const partition &part)
Initialize NVS flash storage for the specified partition without encryption.
Definition nvs.hpp:513
static void deinit(std::string_view partition_label)
Deinitialize NVS storage for the specified partition.
Definition nvs.hpp:671
static result< void > try_init(const partition &part, const secure_config &cfg)
Initialize NVS flash storage for the specified partition with encryption.
static void init()
Initializes the default NVS partition.
Definition nvs.hpp:425
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:697
static void init(const partition &part)
Initialize NVS flash storage for the specified partition.
Definition nvs.hpp:492
static void init(insecure_t, std::string_view partition_label)
Initialize NVS flash storage for the specified partition without encryption.
Definition nvs.hpp:482
static void init(const partition &part, const secure_config &cfg)
Initialize NVS flash storage for the specified partition with encryption.
Definition nvs.hpp:503
static result< void > try_init(insecure_t, const partition &part)
Initialize NVS flash storage for the specified partition without encryption.
Non-Volatile Storage handle.
Definition nvs.hpp:58
nvs(nvs &&other) noexcept
Move constructor.
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:261
void commit()
Commits pending changes to flash.
Definition nvs.hpp:174
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:207
result< std::string > try_get_string(std::string_view key)
Retrieves a string.
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:247
nvs & operator=(const nvs &)=delete
nvs(const nvs &)=delete
nvs & operator=(nvs &&other) noexcept
Move assignment.
void set_blob(std::string_view key, const void *data, size_t length)
Stores binary data.
Definition nvs.hpp:217
static result< nvs > make(const partition &part, std::string_view namespace_name, bool read_only=false)
Opens a NVS namespace on a specific partition.
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:198
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:235
bool is_writeable() const
Returns true if the handle allows writes.
Definition nvs.hpp:166
nvs(const partition &part, std::string_view namespace_name, bool read_only=false)
Opens a NVS namespace on a specific partition.
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:182
errc
Error codes for NVS operations.
Definition nvs.hpp:63
void set_blob(std::string_view key, std::span< const uint8_t > data)
Stores binary data.
Definition nvs.hpp:226
nvs(std::string_view namespace_name, bool read_only=false)
Opens a NVS namespace.
result< void > try_set_blob(std::string_view key, std::span< const uint8_t > data)
Stores binary data.
result< void > try_set_string(std::string_view key, std::string_view value)
Stores a string.
static result< nvs > make(std::string_view namespace_name, bool read_only=false)
Opens a NVS namespace.
void erase_all()
Erases all keys in the namespace.
Definition nvs.hpp:189
A flash partition.
Definition partition.hpp:51
int esp_err_t
Definition error.hpp:35
uint32_t nvs_handle_t
Definition nvs.hpp:29
int esp_err_t
Definition nvs.hpp:30
const nvs::error_category & nvs_category() noexcept
Returns a reference to the NVS error category singleton.
std::unexpected< std::error_code > nvs_error(esp_err_t e)
Creates an unexpected error from an ESP-IDF error code, mapping to NVS error codes where possible.
std::error_code make_error_code(errc e) noexcept
Creates a std::error_code from an idfxx::errc value.
Definition error.hpp:101
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:307
errc
IDFXX error codes.
Definition error.hpp:46
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Tag type to indicate insecure (non-encrypted) initialization.
Definition nvs.hpp:396
Key for encryption and decryption.
Definition nvs.hpp:386
std::array< uint8_t, key_size > eky
Definition nvs.hpp:387
std::array< uint8_t, key_size > tky
Definition nvs.hpp:388