1
6
7
8
9
18
19
20
21
22
23
24
25
26
30
31
36
37
38
39
42
43
44
45
55
56
57
58
59
60
61
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
88
89
90
91
94
95
99
100
101
102
106
107
111
112
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
145
146
147
148
149
150
151
152
153
154
155
156
157
158
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* ... */
#ifndef nvs_storage_hpp
#define nvs_storage_hpp
#include <memory>
#include <cstdlib>
#include <unordered_map>
#include "nvs.hpp"
#include "nvs_types.hpp"
#include "nvs_page.hpp"
#include "nvs_pagemanager.hpp"
#include "nvs_memory_management.hpp"
#include "partition.hpp"9 includes
namespace nvs
{
class Storage : public intrusive_list_node<Storage>, public ExceptionlessAllocatable
{
enum class StorageState : uint32_t {
INVALID,
ACTIVE,
}{...};
struct NamespaceEntry : public intrusive_list_node<NamespaceEntry>, public ExceptionlessAllocatable {
public:
char mName[Item::MAX_KEY_LENGTH + 1];
uint8_t mIndex;...
}{...};
typedef intrusive_list<NamespaceEntry> TNamespaces;
struct UsedPageNode: public intrusive_list_node<UsedPageNode>, public ExceptionlessAllocatable {
public: Page* mPage;
}{...};
typedef intrusive_list<UsedPageNode> TUsedPageList;
struct BlobIndexNode: public intrusive_list_node<BlobIndexNode>, public ExceptionlessAllocatable {
public:
char key[Item::MAX_KEY_LENGTH + 1];
uint8_t nsIndex;
uint8_t chunkCount;
VerOffset chunkStart;
size_t dataSize;
size_t observedDataSize;
size_t observedChunkCount;...
}{...};
typedef intrusive_list<BlobIndexNode> TBlobIndexList;
public:
~Storage();
Storage(Partition *partition) : mPartition(partition) {
if (partition == nullptr) {
abort();
}{...}
}{ ... };
esp_err_t init(uint32_t baseSector, uint32_t sectorCount);
bool isValid() const;
esp_err_t createOrOpenNamespace(const char* nsName, bool canCreate, uint8_t& nsIndex);
esp_err_t writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize);
esp_err_t readItem(uint8_t nsIndex, ItemType datatype, const char* key, void* data, size_t dataSize);
esp_err_t findKey(const uint8_t nsIndex, const char* key, ItemType* datatype);
esp_err_t getItemDataSize(uint8_t nsIndex, ItemType datatype, const char* key, size_t& dataSize);
esp_err_t eraseItem(uint8_t nsIndex, ItemType datatype, const char* key);
template<typename T>
esp_err_t writeItem(uint8_t nsIndex, const char* key, const T& value)
{
return writeItem(nsIndex, itemTypeOf(value), key, &value, sizeof(value));
}{...}
template<typename T>
esp_err_t readItem(uint8_t nsIndex, const char* key, T& value)
{
return readItem(nsIndex, itemTypeOf(value), key, &value, sizeof(value));
}{...}
esp_err_t eraseItem(uint8_t nsIndex, const char* key)
{
return eraseItem(nsIndex, ItemType::ANY, key);
}{ ... }
esp_err_t eraseNamespace(uint8_t nsIndex);
const Partition *getPart() const
{
return mPartition;
}{...}
const char *getPartName() const
{
return mPartition->get_partition_name();
}{...}
uint32_t getBaseSector()
{
return mPageManager.getBaseSector();
}{ ... }
esp_err_t writeMultiPageBlob(uint8_t nsIndex, const char* key, const void* data, size_t dataSize, VerOffset chunkStart);
esp_err_t readMultiPageBlob(uint8_t nsIndex, const char* key, void* data, size_t dataSize);
esp_err_t cmpMultiPageBlob(uint8_t nsIndex, const char* key, const void* data, size_t dataSize);
esp_err_t eraseMultiPageBlob(uint8_t nsIndex, const char* key, VerOffset chunkStart = VerOffset::VER_ANY);
void debugDump();
void debugCheck();
esp_err_t fillStats(nvs_stats_t& nvsStats);
esp_err_t calcEntriesInNamespace(uint8_t nsIndex, size_t& usedEntries);
bool findEntry(nvs_opaque_iterator_t* it, const char* name);
bool findEntryNs(nvs_opaque_iterator_t* it, uint8_t nsIndex);
bool nextEntry(nvs_opaque_iterator_t* it);
...
protected:
Page& getCurrentPage()
{
return mPageManager.back();
}{ ... }
void clearNamespaces();
esp_err_t populateBlobIndices(TBlobIndexList&);
void eraseMismatchedBlobIndexes(TBlobIndexList&);
void eraseOrphanDataBlobs(TBlobIndexList&);
void fillEntryInfo(Item &item, nvs_entry_info_t &info);
esp_err_t findItem(uint8_t nsIndex, ItemType datatype, const char* key, Page* &page, Item& item, uint8_t chunkIdx = Page::CHUNK_ANY, VerOffset chunkStart = VerOffset::VER_ANY);
...
protected:
Partition *mPartition;
size_t mPageCount;
PageManager mPageManager;
TNamespaces mNamespaces;
CompressedEnumTable<bool, 1, 256> mNamespaceUsage;
StorageState mState = StorageState::INVALID;...
}{...};
}{...}
struct nvs_opaque_iterator_t
{
nvs_type_t type;
uint8_t nsIndex;
size_t entryIndex;
nvs::Storage *storage;
intrusive_list<nvs::Page>::iterator page;
nvs_entry_info_t entry_info;
}{ ... };
/* ... */
#endif