HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
BaseNvs.h
Go to the documentation of this file.
1
19#pragma once
20
21#include "HardwareTypes.h"
22#include <cstdint>
23#include <string_view>
24
30//--------------------------------------
31// HardFOC NVS Error Codes (Table)
32//--------------------------------------
41#define HF_NVS_ERR_LIST(X) \
42 /* Success codes */ \
43 X(NVS_SUCCESS, 0, "Success") \
44 /* General errors */ \
45 X(NVS_ERR_FAILURE, 1, "General failure") \
46 X(NVS_ERR_NOT_INITIALIZED, 2, "Not initialized") \
47 X(NVS_ERR_ALREADY_INITIALIZED, 3, "Already initialized") \
48 X(NVS_ERR_INVALID_PARAMETER, 4, "Invalid parameter") \
49 X(NVS_ERR_NULL_POINTER, 5, "Null pointer") \
50 X(NVS_ERR_OUT_OF_MEMORY, 6, "Out of memory") \
51 /* Storage specific errors */ \
52 X(NVS_ERR_KEY_NOT_FOUND, 7, "Key not found") \
53 X(NVS_ERR_KEY_TOO_LONG, 8, "Key too long") \
54 X(NVS_ERR_VALUE_TOO_LARGE, 9, "Value too large") \
55 X(NVS_ERR_NAMESPACE_NOT_FOUND, 10, "Namespace not found") \
56 X(NVS_ERR_STORAGE_FULL, 11, "Storage full") \
57 X(NVS_ERR_INVALID_DATA, 12, "Invalid data") \
58 X(NVS_ERR_READ_ONLY, 13, "Read only mode") \
59 X(NVS_ERR_CORRUPTED, 14, "Data corrupted") \
60 /* ESP32-C6 encryption and advanced feature errors */ \
61 X(NVS_ERR_ENCRYPTION_FAILED, 15, "Encryption operation failed") \
62 X(NVS_ERR_DECRYPTION_FAILED, 16, "Decryption operation failed") \
63 X(NVS_ERR_ENCRYPTION_NOT_CONFIGURED, 17, "Encryption not configured") \
64 X(NVS_ERR_ENCRYPTION_NOT_SUPPORTED, 18, "Encryption not supported") \
65 X(NVS_ERR_KEY_PARTITION_CORRUPTED, 19, "Key partition corrupted") \
66 X(NVS_ERR_WRONG_ENCRYPTION_SCHEME, 20, "Wrong encryption scheme") \
67 X(NVS_ERR_VERSION_MISMATCH, 21, "NVS version mismatch") \
68 X(NVS_ERR_NO_FREE_PAGES, 22, "No free pages available") \
69 X(NVS_ERR_PARTITION_NOT_FOUND, 23, "NVS partition not found") \
70 X(NVS_ERR_ITERATOR_INVALID, 24, "Iterator invalid or expired") \
71 X(NVS_ERR_SECURITY_VIOLATION, 25, "Security policy violation") \
72 X(NVS_ERR_UNSUPPORTED_OPERATION, 26, "Unsupported operation") \
73 X(NVS_ERR_UNKNOWN, 27, "Unknown error")
74
75// Generate enum class from X-macro
76enum class hf_nvs_err_t : hf_i32_t {
77#define X(name, value, desc) name = value,
79#undef X
80};
81
87constexpr std::string_view HfNvsErrToString(hf_nvs_err_t err) noexcept {
88 switch (err) {
89#define X(NAME, VALUE, DESC) \
90 case hf_nvs_err_t::NAME: \
91 return DESC;
93#undef X
94 default:
96 }
97}
98
116
126
147class BaseNvs {
148public:
152 virtual ~BaseNvs() noexcept = default;
153
154 // Disable copy constructor and assignment operator for safety
155 BaseNvs(const BaseNvs&) = delete;
156 BaseNvs& operator=(const BaseNvs&) = delete;
157
158 //==============================================//
159 // LAZY-INITIALIZATION PATTERN //
160 //==============================================//
161
168 if (!initialized_) {
170 }
171 return initialized_;
172 }
173
179 if (initialized_) {
181 }
182 return !initialized_;
183 }
184
189 bool IsInitialized() const noexcept {
190 return initialized_;
191 }
192
193 //==============================================//
194 // PURE VIRTUAL FUNCTIONS (MUST BE IMPLEMENTED) //
195 //==============================================//
196
201 virtual hf_nvs_err_t Initialize() noexcept = 0;
202
207 virtual hf_nvs_err_t Deinitialize() noexcept = 0;
208
215 virtual hf_nvs_err_t SetU32(const char* key, hf_u32_t value) noexcept = 0;
216
223 virtual hf_nvs_err_t GetU32(const char* key, hf_u32_t& value) noexcept = 0;
224
231 virtual hf_nvs_err_t SetString(const char* key, const char* value) noexcept = 0;
232
241 virtual hf_nvs_err_t GetString(const char* key, char* buffer, size_t buffer_size,
242 size_t* actual_size = nullptr) noexcept = 0;
243
251 virtual hf_nvs_err_t SetBlob(const char* key, const void* data, size_t data_size) noexcept = 0;
252
261 virtual hf_nvs_err_t GetBlob(const char* key, void* buffer, size_t buffer_size,
262 size_t* actual_size = nullptr) noexcept = 0;
263
269 virtual hf_nvs_err_t EraseKey(const char* key) noexcept = 0;
270
275 virtual hf_nvs_err_t Commit() noexcept = 0;
276
282 virtual bool KeyExists(const char* key) noexcept = 0;
283
290 virtual hf_nvs_err_t GetSize(const char* key, size_t& size) noexcept = 0;
291
292 //==============================================//
293 // PUBLIC INTERFACE (IMPLEMENTED) //
294 //==============================================//
295
300 const char* GetNamespace() const noexcept {
301 return namespace_name_;
302 }
303
308 virtual const char* GetDescription() const noexcept = 0;
309
314 virtual size_t GetMaxKeyLength() const noexcept = 0;
315
320 virtual size_t GetMaxValueSize() const noexcept = 0;
321
322 //==============================================//
323 // STATISTICS AND DIAGNOSTICS
324 //==============================================//
325
331 virtual hf_nvs_err_t ResetStatistics() noexcept {
332 statistics_ = hf_nvs_statistics_t{}; // Reset statistics to default values
334 }
335
341 virtual hf_nvs_err_t ResetDiagnostics() noexcept {
342 diagnostics_ = hf_nvs_diagnostics_t{}; // Reset diagnostics to default values
344 }
345
351 virtual hf_nvs_err_t GetStatistics(hf_nvs_statistics_t& statistics) const noexcept {
352 statistics = statistics_; // Return statistics by default
354 }
355
361 virtual hf_nvs_err_t GetDiagnostics(hf_nvs_diagnostics_t& diagnostics) const noexcept {
362 diagnostics = diagnostics_; // Return diagnostics by default
364 }
365
366protected:
371 explicit BaseNvs(const char* namespace_name) noexcept
372 : namespace_name_(namespace_name), initialized_(false), statistics_{}, diagnostics_{} {}
373
378 void SetInitialized(bool initialized) noexcept {
379 initialized_ = initialized;
380 }
381
382 const char* namespace_name_;
386
387private:
388};
hf_nvs_err_t
Definition BaseNvs.h:76
@ NVS_ERR_UNSUPPORTED_OPERATION
constexpr std::string_view HfNvsErrToString(hf_nvs_err_t err) noexcept
Convert NVS error code to string view.
Definition BaseNvs.h:87
#define X(name, value, desc)
Definition BaseNvs.h:77
Platform-agnostic hardware type definitions for the HardFOC system.
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
int32_t hf_i32_t
Platform-agnostic 32-bit signed integer type.
Definition HardwareTypes.h:76
Abstract base class for non-volatile storage operations.
Definition BaseNvs.h:147
virtual hf_nvs_err_t ResetDiagnostics() noexcept
Reset NVS diagnostic information.
Definition BaseNvs.h:341
virtual size_t GetMaxValueSize() const noexcept=0
Get maximum value size supported.
virtual hf_nvs_err_t Commit() noexcept=0
Commit any pending writes to non-volatile storage.
hf_nvs_statistics_t statistics_
NVS operation statistics.
Definition BaseNvs.h:384
void SetInitialized(bool initialized) noexcept
Set the initialized state.
Definition BaseNvs.h:378
virtual hf_nvs_err_t SetBlob(const char *key, const void *data, size_t data_size) noexcept=0
Store binary data (blob).
virtual hf_nvs_err_t GetStatistics(hf_nvs_statistics_t &statistics) const noexcept
Get NVS operation statistics.
Definition BaseNvs.h:351
virtual ~BaseNvs() noexcept=default
Virtual destructor to ensure proper cleanup.
virtual hf_nvs_err_t Initialize() noexcept=0
Initialize the storage system and open the namespace.
bool EnsureDeinitialized()
Ensures that the NVS storage is deinitialized.
Definition BaseNvs.h:178
bool EnsureInitialized()
Ensures that the NVS storage is initialized (lazy initialization).
Definition BaseNvs.h:167
virtual hf_nvs_err_t GetDiagnostics(hf_nvs_diagnostics_t &diagnostics) const noexcept
Get NVS diagnostic information.
Definition BaseNvs.h:361
virtual size_t GetMaxKeyLength() const noexcept=0
Get maximum key length supported.
virtual hf_nvs_err_t Deinitialize() noexcept=0
Deinitialize the storage system and close the namespace.
virtual const char * GetDescription() const noexcept=0
Get description of this storage implementation.
const char * namespace_name_
Namespace name.
Definition BaseNvs.h:382
const char * GetNamespace() const noexcept
Get the namespace name.
Definition BaseNvs.h:300
hf_nvs_diagnostics_t diagnostics_
NVS diagnostic information.
Definition BaseNvs.h:385
virtual hf_nvs_err_t ResetStatistics() noexcept
Reset NVS operation statistics.
Definition BaseNvs.h:331
virtual hf_nvs_err_t GetSize(const char *key, size_t &size) noexcept=0
Get the size of a stored value.
bool initialized_
Initialization status.
Definition BaseNvs.h:383
virtual hf_nvs_err_t GetString(const char *key, char *buffer, size_t buffer_size, size_t *actual_size=nullptr) noexcept=0
Retrieve a string value.
virtual hf_nvs_err_t SetU32(const char *key, hf_u32_t value) noexcept=0
Store a 32-bit unsigned integer value.
virtual bool KeyExists(const char *key) noexcept=0
Check if a key exists in storage.
virtual hf_nvs_err_t GetU32(const char *key, hf_u32_t &value) noexcept=0
Retrieve a 32-bit unsigned integer value.
virtual hf_nvs_err_t GetBlob(const char *key, void *buffer, size_t buffer_size, size_t *actual_size=nullptr) noexcept=0
Retrieve binary data (blob).
BaseNvs(const char *namespace_name) noexcept
Protected constructor with namespace specification.
Definition BaseNvs.h:371
virtual hf_nvs_err_t EraseKey(const char *key) noexcept=0
Remove a key from storage.
bool IsInitialized() const noexcept
Check if storage is initialized.
Definition BaseNvs.h:189
virtual hf_nvs_err_t SetString(const char *key, const char *value) noexcept=0
Store a string value.
#define HF_NVS_ERR_LIST(X)
HardFOC NVS error codes.
Definition BaseNvs.h:41
NVS diagnostic information.
Definition BaseNvs.h:120
hf_u32_t consecutive_errors
Definition BaseNvs.h:122
hf_u32_t system_uptime_ms
Definition BaseNvs.h:124
hf_nvs_err_t last_error
Definition BaseNvs.h:121
bool storage_healthy
Definition BaseNvs.h:123
NVS operation statistics.
Definition BaseNvs.h:102
hf_u32_t failed_ops
Definition BaseNvs.h:112
hf_u32_t total_erases
Definition BaseNvs.h:108
hf_u32_t total_commits
Definition BaseNvs.h:107
hf_u32_t bytes_written
Definition BaseNvs.h:113
hf_nvs_err_t last_error
Definition BaseNvs.h:109
hf_u32_t last_operation_time_us
Definition BaseNvs.h:110
hf_u32_t bytes_read
Definition BaseNvs.h:114
hf_u32_t total_reads
Definition BaseNvs.h:105
hf_u32_t total_operations
Definition BaseNvs.h:103
hf_u32_t total_errors
Definition BaseNvs.h:104
hf_u32_t total_writes
Definition BaseNvs.h:106
hf_u32_t successful_ops
Definition BaseNvs.h:111