29#include <system_error>
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>;
95 [[nodiscard]]
const char*
name() const noexcept override final;
98 [[nodiscard]] std::
string message(
int ec) const override final;
113#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
123 [[nodiscard]]
explicit nvs(std::string_view namespace_name,
bool read_only =
false);
136#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
214 requires sized_integral<T>
228 requires sized_integral<T>
230 return unwrap(try_get_value<T>(key));
295 requires sized_integral<T>
305 requires sized_integral<T>
344 constexpr static size_t key_size = 32;
350 std::array<uint8_t, key_size>
eky;
351 std::array<uint8_t, key_size>
tky;
366#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
422 static void init(std::string_view partition_label) {
unwrap(try_init(partition_label)); }
434 unwrap(try_init(partition_label, cfg));
445 static void init(
insecure_t, std::string_view partition_label) {
unwrap(try_init(insecure, partition_label)); }
550#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
566 static void deinit(std::string_view partition_label) {
unwrap(try_deinit(partition_label)); }
586#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
602 static void erase(std::string_view partition_label) {
unwrap(try_erase(partition_label)); }
// end of idfxx_nvs
Error category for NVS errors.
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.
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.
static void init(std::string_view partition_label, const secure_config &cfg)
Initialize NVS flash storage for the specified partition.
static void init(const secure_config &cfg)
Initialize the default NVS partition with encryption.
static void deinit()
Deinitialize NVS storage for the default NVS partition.
static void erase(std::string_view partition_label)
Erase specified NVS 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 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.
static void init()
Initializes the default NVS partition.
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.
static void init(insecure_t, std::string_view partition_label)
Initialize NVS flash storage for the specified partition without encryption.
Non-Volatile Storage handle.
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.
void commit()
Commits pending changes to flash.
result< void > try_erase(std::string_view key)
Erases a key.
std::string get_string(std::string_view key)
Retrieves a string.
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.
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
void set_blob(std::string_view key, const void *data, size_t length)
Stores binary data.
result< void > try_commit()
Commits pending changes to flash.
void set_string(std::string_view key, std::string_view value)
Stores a string.
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.
bool is_writeable() const
Returns true if the handle allows writes.
result< void > try_set_value(std::string_view key, T value)
Stores an integer value.
void erase(std::string_view key)
Erases a key.
errc
Error codes for NVS operations.
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.
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.
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.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
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.
Key for encryption and decryption.
std::array< uint8_t, key_size > eky
std::array< uint8_t, key_size > tky