Internal Interface Wrap
API reference
Loading...
Searching...
No Matches
StmNvs.h
Go to the documentation of this file.
1
30#pragma once
31
32#include "BaseNvs.h"
33#include "StmTypes.h"
34#include <cstring>
35
50class StmNvs : public BaseNvs {
51public:
57 explicit StmNvs(const char* namespace_name = "default",
58 const hf_stm32_nvs_config_t& flash_config = hf_stm32_nvs_config_t()) noexcept;
59
60 ~StmNvs() noexcept override;
61
62 // ── BaseNvs overrides ────────────────────────────────────────────────
63
64 hf_nvs_err_t Initialize() noexcept override;
65 hf_nvs_err_t Deinitialize() noexcept override;
66
67 hf_nvs_err_t SetU32(const char* key, hf_u32_t value) noexcept override;
68 hf_nvs_err_t GetU32(const char* key, hf_u32_t& value) noexcept override;
69
70 hf_nvs_err_t SetString(const char* key, const char* value) noexcept override;
71 hf_nvs_err_t GetString(const char* key, char* buffer, size_t buffer_size,
72 size_t* actual_size = nullptr) noexcept override;
73
74 hf_nvs_err_t SetBlob(const char* key, const void* data, size_t data_size) noexcept override;
75 hf_nvs_err_t GetBlob(const char* key, void* buffer, size_t buffer_size,
76 size_t* actual_size = nullptr) noexcept override;
77
78 hf_nvs_err_t EraseKey(const char* key) noexcept override;
79 hf_nvs_err_t Commit() noexcept override;
80 bool KeyExists(const char* key) noexcept override;
81 hf_nvs_err_t GetSize(const char* key, size_t& size) noexcept override;
82
83 const char* GetDescription() const noexcept override;
84 size_t GetMaxKeyLength() const noexcept override;
85 size_t GetMaxValueSize() const noexcept override;
86
87 // ── STM32-specific ───────────────────────────────────────────────────
88
90 const hf_stm32_nvs_config_t& GetFlashConfig() const noexcept { return flash_config_; }
91
93 size_t GetEntryCount() const noexcept { return entry_count_; }
94
96 size_t GetFreeSpace() const noexcept;
97
98private:
100 enum class EntryType : hf_u8_t {
101 U32 = 0x01,
102 STRING = 0x02,
103 BLOB = 0x03,
104 ERASED = 0xFF
105 };
106
108 struct __attribute__((packed)) EntryHeader {
109 hf_u8_t key_length;
110 EntryType type;
111 hf_u16_t data_length;
112 hf_u32_t crc32;
113 };
114
124
126 int FindCacheEntry(const char* key) const noexcept;
127
129 int AddOrUpdateCache(const char* key, EntryType type,
130 const void* data, size_t data_size) noexcept;
131
133 bool IsValidKey(const char* key) const noexcept;
134
136 hf_nvs_err_t FlushToFlash() noexcept;
137
139 hf_nvs_err_t LoadFromFlash() noexcept;
140
142 static hf_u32_t ComputeCrc32(const void* data, size_t length) noexcept;
143
145 static constexpr size_t kMaxEntries = 8;
149};
Abstract base class for Non-Volatile Storage implementations in the HardFOC system.
hf_nvs_err_t
Definition BaseNvs.h:76
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
uint16_t hf_u16_t
Platform-agnostic 16-bit unsigned integer type.
Definition HardwareTypes.h:46
STM32 platform-specific type definitions for hardware abstraction.
Abstract base class for non-volatile storage operations.
Definition BaseNvs.h:147
STM32 NVS — flash-backed key-value store with wear leveling.
Definition StmNvs.h:50
hf_nvs_err_t SetString(const char *key, const char *value) noexcept override
Store a string value.
Definition StmNvs.cpp:144
EntryType
NVS entry types.
Definition StmNvs.h:100
size_t entry_count_
Current entry count.
Definition StmNvs.h:147
hf_nvs_err_t GetU32(const char *key, hf_u32_t &value) noexcept override
Retrieve a 32-bit unsigned integer value.
Definition StmNvs.cpp:130
hf_stm32_nvs_config_t flash_config_
Flash region config.
Definition StmNvs.h:144
size_t GetEntryCount() const noexcept
Get number of stored entries.
Definition StmNvs.h:93
int FindCacheEntry(const char *key) const noexcept
Find entry in RAM cache by key.
Definition StmNvs.cpp:263
hf_nvs_err_t Commit() noexcept override
Commit any pending writes to non-volatile storage.
Definition StmNvs.cpp:221
hf_nvs_err_t SetBlob(const char *key, const void *data, size_t data_size) noexcept override
Store binary data (blob).
Definition StmNvs.cpp:178
struct __attribute__((packed)) EntryHeader
Entry header stored in flash (packed)
Definition StmNvs.h:108
size_t GetMaxKeyLength() const noexcept override
Get maximum key length supported.
Definition StmNvs.cpp:247
hf_nvs_err_t EraseKey(const char *key) noexcept override
Remove a key from storage.
Definition StmNvs.cpp:209
hf_nvs_err_t GetBlob(const char *key, void *buffer, size_t buffer_size, size_t *actual_size=nullptr) noexcept override
Retrieve binary data (blob).
Definition StmNvs.cpp:190
bool KeyExists(const char *key) noexcept override
Check if a key exists in storage.
Definition StmNvs.cpp:226
const char * GetDescription() const noexcept override
Get description of this storage implementation.
Definition StmNvs.cpp:243
size_t GetMaxValueSize() const noexcept override
Get maximum value size supported.
Definition StmNvs.cpp:251
hf_nvs_err_t SetU32(const char *key, hf_u32_t value) noexcept override
Store a 32-bit unsigned integer value.
Definition StmNvs.cpp:119
hf_nvs_err_t Deinitialize() noexcept override
Deinitialize the storage system and close the namespace.
Definition StmNvs.cpp:105
const hf_stm32_nvs_config_t & GetFlashConfig() const noexcept
Get the flash configuration.
Definition StmNvs.h:90
hf_nvs_err_t Initialize() noexcept override
Initialize the storage system and open the namespace.
Definition StmNvs.cpp:82
size_t GetFreeSpace() const noexcept
Get available free space in current sector (bytes)
Definition StmNvs.cpp:255
hf_nvs_err_t GetSize(const char *key, size_t &size) noexcept override
Get the size of a stored value.
Definition StmNvs.cpp:232
static hf_u32_t ComputeCrc32(const void *data, size_t length) noexcept
Simple CRC32 for entry validation.
Definition StmNvs.cpp:517
hf_nvs_err_t GetString(const char *key, char *buffer, size_t buffer_size, size_t *actual_size=nullptr) noexcept override
Retrieve a string value.
Definition StmNvs.cpp:158
static constexpr size_t kMaxEntries
Max cached entries (cal-sized)
Definition StmNvs.h:145
bool IsValidKey(const char *key) const noexcept
Validate a key string.
Definition StmNvs.cpp:293
hf_nvs_err_t LoadFromFlash() noexcept
Read entries from flash into RAM cache.
Definition StmNvs.cpp:446
hf_u32_t write_offset_
Current write position in flash.
Definition StmNvs.h:148
~StmNvs() noexcept override
Definition StmNvs.cpp:74
hf_nvs_err_t FlushToFlash() noexcept
Write all dirty entries to flash.
Definition StmNvs.cpp:299
int AddOrUpdateCache(const char *key, EntryType type, const void *data, size_t data_size) noexcept
Add or update entry in RAM cache.
Definition StmNvs.cpp:272
CacheEntry cache_[kMaxEntries]
RAM cache.
Definition StmNvs.h:146
RAM cache entry.
Definition StmNvs.h:116
bool dirty
Needs flash write.
Definition StmNvs.h:121
hf_u8_t data[576]
Value data (blob capacity for large cal records)
Definition StmNvs.h:119
hf_u16_t data_length
Actual data length.
Definition StmNvs.h:120
char key[64]
Key string.
Definition StmNvs.h:117
EntryType type
Value type.
Definition StmNvs.h:118
bool erased
Marked for deletion.
Definition StmNvs.h:122
Flash-based NVS configuration for STM32.
Definition StmTypes.h:537