-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvs32.cpp
273 lines (238 loc) · 5.59 KB
/
nvs32.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "nvs32.h"
/**
* @brief Init specifi NVS partition with specific namespace.
*
* @attention Custom NVS partition need to be create in partitions.csv.
*
* @param [*partition]: Partition name.
* @param [*name]: Namespace inside partition.
* @param [debug]: Enable debug-log when writing new values to keys.
*
* @return 0: Fail.
* @return 1: Sucess
*/
int8_t NVS::init(const char *partition, const char *name, int8_t debug=1)
{
esp_err_t err;
_debug = debug;
err = nvs_flash_init_partition(partition);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to init partition [0x%x]", err);
return 0;
}
err = nvs_open_from_partition(partition, name, NVS_READWRITE, &_handler);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to open namespace [0x%x]", err);
return 0;
}
return 1;
}
/**
* @brief Erase all keys.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::erase_all()
{
esp_err_t err = nvs_erase_all(_handler);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to erase all keys [0x%x]", err);
return 0;
}
nvs_commit(_handler);
return 1;
}
/**
* @brief Erase specific key.
*
* @param [*key]: Key.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::erase_key(const char *key)
{
esp_err_t err = nvs_erase_key(_handler, key);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to erase key ['%s'] [0x%x]", key, err);
return 0;
}
nvs_commit(_handler);
return 1;
}
/**
* @brief Create new key with [*value] ONLY if this key not exist in memory.
*
* @attention .create() functions only create the key if the key not exist.
* If it already exists with any value, the value will NOT be overwritten, the last value will remain.
*
* @param [*key]: Key.
* @param [*value]: String text.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::create(const char *key, const char *value)
{
esp_err_t err;
size_t sz;
err = nvs_get_str(_handler, key, NULL, &sz);
if (err == ESP_ERR_NVS_NOT_FOUND)
{
err = nvs_set_str(_handler, key, value);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to create key ['%s'] [0x%x]", key, err);
return 0;
}
nvs_commit(_handler);
if (_debug)
{
ESP_LOGI(tag, "Key ['%s'] created", key);
}
return 1;
}
return 0;
}
/**
* @brief Create new key with [value] ONLY if this key not exist in memory.
*
* @attention .create() functions only create the key if the key not exist.
* If it already exists with any value, the value will NOT be overwritten, the last value will remain.
*
* @param [*key]: Key.
* @param [value]: Value.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::create(const char *key, int32_t value)
{
esp_err_t err;
int32_t bff;
err = nvs_get_i32(_handler, key, &bff);
if (err == ESP_ERR_NVS_NOT_FOUND)
{
err = nvs_set_i32(_handler, key, value);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to create key ['%s'] [0x%x]", key, err);
return 0;
}
nvs_commit(_handler);
if (_debug)
{
ESP_LOGI(tag, "Key ['%s'] created", key);
}
return 1;
}
return 0;
}
/**
* @brief Write new value to key.
*
* @param [*key]: Key.
* @param [*value]: String text.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::write(const char *key, const char *value)
{
esp_err_t err = nvs_set_str(_handler, key, value);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to write key ['%s'] [0x%x]", key, err);
return 0;
}
nvs_commit(_handler);
if (_debug)
{
ESP_LOGI(tag, "['%s']: ['%s']", key, value);
}
return 1;
}
/**
* @brief Write new value to key.
*
* @param [*key]: Key.
* @param [value]: Value.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::write(const char *key, int32_t value)
{
esp_err_t err = nvs_set_i32(_handler, key, value);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to write key ['%s'] [0x%x]", key, err);
return 0;
}
nvs_commit(_handler);
if (_debug)
{
ESP_LOGI(tag, "['%s']: [%d]", key, value);
}
return 1;
}
/**
* @brief Read value of specific key.
*
* @attention Destiny char buffer [dst] need to be >= of string stored in key.
*
* @param [*key]: Key.
* @param [*dst]: Char buffer that will receive the stored string.
* @param [size]: Max size of [dst] buffer.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::read(const char *key, char *dst, uint16_t size)
{
size_t sz; //Size of string allocated in nvs key.
esp_err_t err;
err = nvs_get_str(_handler, key, NULL, &sz);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to read key ['%s'] [0x%x]", key, err);
return 0;
}
if (sz > size)
{
ESP_LOGE(tag, "Your buffer [%d] is smaller than necessary [%d]", size, sz);
return 0;
}
err = nvs_get_str(_handler, key, dst, &sz);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail2 to read key ['%s'] [0x%x]", key, err);
return 0;
}
return 1;
}
/**
* @brief Read value of specific key.
*
*
* @param [*key]: Key.
* @param [*dst]: Buffer that will receive the stored value.
*
* @return 0: Fail.
* @return 1: Sucess.
*/
int8_t NVS::read(const char *key, int32_t *dst)
{
esp_err_t err = nvs_get_i32(_handler, key, dst);
if (err != ESP_OK)
{
ESP_LOGE(tag, "Fail to read key ['%s'] [0x%x]", key, err);
return 0;
}
return 1;
}