-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvs_memory.cpp
203 lines (162 loc) · 4.86 KB
/
nvs_memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "nvs_memory.h"
#include <cstring>
#include <esp_log.h>
using namespace libesp;
const char *NVS::LOGTAG="NVS";
NVS::NVS(const char *partLabel, const char *ns, bool readOnly)
: PartitionLabel(), Namespace(), MyHandle(0), ReadOnly(readOnly) {
strncpy(&PartitionLabel[0],partLabel,sizeof(PartitionLabel));
strncpy(&Namespace[0],ns,sizeof(Namespace));
}
ErrorType NVS::initStorage() {
esp_err_t ret = nvs_flash_init_partition(&PartitionLabel[0]);
ESP_LOGI(LOGTAG,"%d %s",ret, esp_err_to_name(ret));
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase_partition(&PartitionLabel[0]));
ret = nvs_flash_init_partition(&PartitionLabel[0]);
}
return ret;
}
ErrorType NVS::init() {
return nvs_open_from_partition(&PartitionLabel[0],&Namespace[0],(ReadOnly?NVS_READONLY:NVS_READWRITE),&MyHandle);
}
ErrorType NVS::wipe() {
return nvs_erase_all(MyHandle);
}
ErrorType NVS::eraseKey(const char *name) {
return nvs_erase_key(MyHandle,name);
}
ErrorType NVS::setValue(const char *name, int8_t value) {
return nvs_set_i8(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, uint8_t value) {
return nvs_set_u8(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, int16_t value) {
return nvs_set_i16(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, uint16_t value) {
return nvs_set_u16(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, int32_t value) {
return nvs_set_i32(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, uint32_t value) {
return nvs_set_u32(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, int64_t value) {
return nvs_set_i64(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, uint64_t value) {
return nvs_set_u64(MyHandle,name,value);
}
ErrorType NVS::setValue(const char *name, const char *value) {
return nvs_set_str(MyHandle,name,value);
}
ErrorType NVS::setBlob(const char *name, const void *data, uint32_t length) {
return nvs_set_blob(MyHandle,name,data,length);
}
ErrorType NVS::getValue(const char *name, int8_t &value) {
return nvs_get_i8(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, uint8_t &value) {
return nvs_get_u8(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, int16_t &value) {
return nvs_get_i16(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, uint16_t &value) {
return nvs_get_u16(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, int32_t &value) {
return nvs_get_i32(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, uint32_t &value) {
return nvs_get_u32(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, int64_t &value) {
return nvs_get_i64(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, uint64_t &value) {
return nvs_get_u64(MyHandle,name,&value);
}
ErrorType NVS::getValue(const char *name, char *value, uint32_t &length) {
return nvs_get_str(MyHandle,name,value, &length);
}
ErrorType NVS::getBlob(const char *name, void *data, uint32_t &length) {
return nvs_get_blob(MyHandle,name,data, &length);
}
ErrorType NVS::commit() {
return nvs_commit(MyHandle);
}
void NVS::close() {
nvs_close(MyHandle);
}
void NVS::logInfo() {
nvs_stats_t stats;
if(nvs_get_stats(&PartitionLabel[0],&stats)==ESP_OK) {
ESP_LOGI(LOGTAG, "Namespaces: %u, Total Entries: %u, Used: %u, Free: %u",
stats.namespace_count, stats.total_entries, stats.used_entries, stats.free_entries);
} else {
ESP_LOGI(LOGTAG, "Error getting info");
}
}
/*
bool NVS::doesKeyExist(const char *name) {
nvs_iterator it = nvs_entry_find(&PartitionLabel[0], &Namespace[0], NVS_TYPE_ANY);
while (it!=0) {
nvs_entry_info info;
nvs_entry_info(it, &info);
if(strcmp(name,info.key)==0) {
nvs_release_iterator(it);
return true;
}
it = nvs_entry_next(it);
}
return false;
}
uint16_t NVS::countInNamespace() {
uint16_t c = 0;
nvs_iterator_t it = nvs_entry_find(&PartitionLabel[0], &Namespace[0], NVS_TYPE_ANY);
while(it!=0) {
c++;
it = nvs_entry_next(it);
}
return return c;
}
*/
uint32_t NVS::getNumberOfNamespaces() {
nvs_stats_t stats;
if(nvs_get_stats(&PartitionLabel[0],&stats)==ESP_OK) {
return stats.namespace_count;
}
return NVS_ERROR;
}
uint32_t NVS::getUsedEntryCount() {
nvs_stats_t stats;
if(nvs_get_stats(&PartitionLabel[0],&stats)==ESP_OK) {
return stats.used_entries;
}
return NVS_ERROR;
}
uint32_t NVS::getNumberFreeEntries() {
nvs_stats_t stats;
if(nvs_get_stats(&PartitionLabel[0],&stats)==ESP_OK) {
return stats.free_entries;
}
return NVS_ERROR;
}
uint32_t NVS::getTotalEnties() {
nvs_stats_t stats;
if(nvs_get_stats(&PartitionLabel[0],&stats)==ESP_OK) {
return stats.total_entries;
}
return NVS_ERROR;
}
NVS::~NVS() {
close();
}
NVSStackCommit::NVSStackCommit(NVS *n) : nvs(n) {}
NVSStackCommit::~NVSStackCommit() {
nvs->commit();
}