forked from liebman/esp32-gps-ntp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
249 lines (223 loc) · 6.32 KB
/
Config.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
/*
* MIT License
*
* Copyright (c) 2020 Christopher B. Liebman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Config.h"
#include "esp_log.h"
#include "string.h"
static const char* TAG = "Config";
static const char* KEY_WIFI_SSID = "wifi_ssid";
static const char* KEY_WIFI_PASS = "wifi_pass";
static const char* KEY_BIAS = "bias";
static const char* KEY_TARGET = "target";
Config::Config()
{
setWiFiSSID("");
setWiFiPassword("");
}
Config::~Config()
{
}
bool Config::begin(const char* partition_name)
{
ESP_LOGI(TAG, "::begin()");
esp_err_t err = nvs_flash_init_partition(partition_name);
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_LOGI(TAG, "call nvs_flash_erase_partition '%s'", partition_name);
err = nvs_flash_erase_partition(partition_name);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "failed nvs_flash_erase_partition '%s' nvs: %d (%s)", partition_name, err, esp_err_to_name(err));
return false;
}
ESP_LOGI(TAG, "call nvs_flash_init_partition '%s'", partition_name);
err = nvs_flash_init_partition(partition_name);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "failed nvs_flash_init_partition '%s' nvs: %d (%s)", partition_name, err, esp_err_to_name(err));
return false;
}
}
ESP_LOGI(TAG, "opening NVS partition '%s'", partition_name);
err = nvs_open(partition_name, NVS_READWRITE, &_nvs);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "failed to open '%s' nvs: %d (%s)", partition_name, err, esp_err_to_name(err));
return false;
}
return true;
}
void Config::getString(const char* key, const char** valuep, const char* def_value)
{
if (*valuep)
{
delete[] *valuep;
}
*valuep = def_value;
size_t len;
esp_err_t err = nvs_get_str(_nvs, key, nullptr, &len);
if (err != ESP_OK)
{
return;
}
char* value = new char[len+1];
nvs_get_str(_nvs, key, value, &len);
*valuep = value;
}
char* Config::getString(const char* key, const char* def_value)
{
size_t len;
esp_err_t err = nvs_get_str(_nvs, key, nullptr, &len);
if (err != ESP_OK)
{
return copyString(def_value);
}
char* value = new char[len+1];
nvs_get_str(_nvs, key, value, &len);
return value;
}
char* Config::copyString(const char* str)
{
if (str == nullptr)
{
return nullptr;
}
size_t len = strlen(str);
char* value = new char[len+1];
strcpy(value, str);
return value;
}
float Config::getFloat(const char* key, float def_value)
{
size_t len;
esp_err_t err = nvs_get_blob(_nvs, key, nullptr, &len);
if (err != ESP_OK)
{
return def_value;
}
if (len != sizeof(float))
{
ESP_LOGE(TAG, "wrong size for value of '%s' %d != %d", key, len, sizeof(float));
return def_value;
}
float value;
nvs_get_blob(_nvs, key, &value, &len);
return value;
}
bool Config::load()
{
ESP_LOGI(TAG, "::load()");
if (_wifi_ssid != nullptr)
{
delete[] _wifi_ssid;
}
_wifi_ssid = getString(KEY_WIFI_SSID);
if (_wifi_pass != nullptr)
{
delete[] _wifi_pass;
}
_wifi_pass = getString(KEY_WIFI_PASS);
_bias = getFloat(KEY_BIAS);
_target = getFloat(KEY_TARGET);
ESP_LOGI(TAG, "::load: ssid=%s pass=%s bias=%0f target=%0f", _wifi_ssid, _wifi_pass, _bias, _target);
return true;
}
bool Config::save()
{
ESP_LOGI(TAG, "::save()");
bool ret = true;
esp_err_t err = nvs_set_str(_nvs, KEY_WIFI_SSID, _wifi_ssid);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "::save failed to set '%s=%s': %d (%s)", KEY_WIFI_SSID, _wifi_ssid, err, esp_err_to_name(err));
ret = false;
}
err = nvs_set_str(_nvs, KEY_WIFI_PASS, _wifi_pass);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "::save failed to set '%s=%s': %d (%s)", KEY_WIFI_PASS, _wifi_pass, err, esp_err_to_name(err));
ret = false;
}
err = nvs_set_blob(_nvs, KEY_BIAS, &_bias, sizeof(_bias));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "::save failed to set '%s=%f': %d (%s)", KEY_BIAS, _bias, err, esp_err_to_name(err));
ret = false;
}
err = nvs_set_blob(_nvs, KEY_TARGET, &_target, sizeof(_target));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "::save failed to set '%s=%f': %d (%s)", KEY_TARGET, _target, err, esp_err_to_name(err));
ret = false;
}
return ret;
}
void Config::setWiFiSSID(const char* ssid)
{
if (_wifi_ssid != nullptr)
{
delete[] _wifi_ssid;
}
_wifi_ssid = copyString(ssid);
}
const char* Config::getWiFiSSID()
{
if (_wifi_ssid == nullptr)
{
return "";
}
return _wifi_ssid;
}
void Config::setWiFiPassword(const char* password)
{
if (_wifi_pass != nullptr)
{
delete[] _wifi_pass;
}
_wifi_pass = copyString(password);
}
const char* Config::getWiFiPassword()
{
if (_wifi_pass == nullptr)
{
return "";
}
return _wifi_pass;
}
void Config::setBias(float bias)
{
_bias = bias;
}
float Config::getBias()
{
return _bias;
}
void Config::setTarget(float target)
{
_target = target;
}
float Config::getTarget()
{
return _target;
}