This repository was archived by the owner on Dec 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathproject_config.cpp
More file actions
468 lines (404 loc) · 16.2 KB
/
project_config.cpp
File metadata and controls
468 lines (404 loc) · 16.2 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "project_config.hpp"
ProjectConfig::ProjectConfig(const std::string& name,
const std::string& mdnsName)
: _name(std::move(name)),
_mdnsName(std::move(mdnsName)),
_already_loaded(false) {}
ProjectConfig::~ProjectConfig() {}
/**
*@brief Initializes the structures with blank data to prevent empty memory
*sectors and nullptr errors.
*@brief This is to be called in setup() before loading the config.
*/
void ProjectConfig::initConfig() {
if (_name.empty()) {
log_e("Config name is null\n");
_name = "openiris";
}
bool success = begin(_name.c_str());
log_i("[Project Config]: Config name: %s", _name.c_str());
log_i("[Project Config]: Config loaded: %s", success ? "true" : "false");
/*
* If the config is not loaded,
* we need to initialize the config with default data
! Do not initialize the WiFiConfig_t struct here,
! as it will create a blank network which breaks the WiFiManager
*/
this->config.device = {OTA_LOGIN, OTA_PASSWORD, 3232};
if (_mdnsName.empty()) {
log_e("MDNS name is null\n Autoassigning name to 'openiristracker'");
_mdnsName = "openiristracker";
}
this->config.mdns = {
_mdnsName,
"openiristracker",
};
log_i("[Project Config]: MDNS name: %s", _mdnsName.c_str());
this->config.ap_network = {
"",
"",
1,
false,
};
this->config.camera = {
.vflip = 0,
.href = 0,
.framesize = 4,
.quality = 7,
.brightness = BRIGHTNESS,
.simple_auto_exposure_on = SIMPLE_AUTO_EXPOSURE_ON,
.fancy_auto_exposure_on = FANCY_AUTO_EXPOSURE_ON,
.ae_level = AE_LEVEL,
.aec_value = AEC_VALUE,
.auto_gain_on = AUTO_GAIN_ON,
};
}
void ProjectConfig::save() {
log_d("Saving project config");
deviceConfigSave();
mdnsConfigSave();
cameraConfigSave();
wifiConfigSave();
wifiTxPowerConfigSave();
end(); // we call end() here to close the connection to the NVS partition, we
// only do this because we call ESP.restart() next.
OpenIrisTasks::ScheduleRestart(2000);
}
void ProjectConfig::wifiConfigSave() {
log_d("Saving wifi config");
/* WiFi Config */
putInt("networkCount", this->config.networks.size());
std::string name = "name";
std::string ssid = "ssid";
std::string password = "pass";
std::string channel = "channel";
std::string power = "power";
for (int i = 0; i < this->config.networks.size(); i++) {
char buffer[2];
std::string iter_str = Helpers::itoa(i, buffer, 10);
name.append(iter_str);
ssid.append(iter_str);
password.append(iter_str);
channel.append(iter_str);
power.append(iter_str);
putString(name.c_str(), this->config.networks[i].name.c_str());
putString(ssid.c_str(), this->config.networks[i].ssid.c_str());
putString(password.c_str(), this->config.networks[i].password.c_str());
putUInt(channel.c_str(), this->config.networks[i].channel);
putUInt(power.c_str(), this->config.networks[i].power);
}
/* AP Config */
putString("apSSID", this->config.ap_network.ssid.c_str());
putString("apPass", this->config.ap_network.password.c_str());
putUInt("apChannel", this->config.ap_network.channel);
log_i("[Project Config]: Wifi configs saved");
}
void ProjectConfig::deviceConfigSave() {
/* Device Config */
putString("OTAPassword", this->config.device.OTAPassword.c_str());
putString("OTALogin", this->config.device.OTALogin.c_str());
putInt("OTAPort", this->config.device.OTAPort);
}
void ProjectConfig::mdnsConfigSave() {
/* Device Config */
putString("hostname", this->config.mdns.hostname.c_str());
putString("service", this->config.mdns.service.c_str());
}
void ProjectConfig::wifiTxPowerConfigSave() {
/* Device Config */
putInt("power", this->config.txpower.power);
}
void ProjectConfig::cameraConfigSave() {
/* Camera Config */
putInt("vflip", this->config.camera.vflip);
putInt("href", this->config.camera.href);
putInt("framesize", this->config.camera.framesize);
putInt("quality", this->config.camera.quality);
putInt("brightness", this->config.camera.brightness);
putInt("brightness", this->config.camera.brightness);
putInt("simple_auto_exposure_on", this->config.camera.fancy_auto_exposure_on);
putInt("fancy_auto_exposure_on", this->config.camera.fancy_auto_exposure_on);
putInt("ae_level", this->config.camera.ae_level);
putInt("aec_value", this->config.camera.aec_value);
putInt("auto_gain_on ", this->config.camera.auto_gain_on);
}
bool ProjectConfig::reset() {
log_w("Resetting project config");
return clear();
}
void ProjectConfig::load() {
log_d("Loading project config");
if (this->_already_loaded) {
log_w("Project config already loaded");
return;
}
/* Device Config */
this->config.device.OTALogin = getString("OTALogin", "openiris").c_str();
this->config.device.OTAPassword =
getString("OTAPassword", "12345678").c_str();
this->config.device.OTAPort = getInt("OTAPort", 3232);
/* MDNS Config */
this->config.mdns.hostname = getString("hostname", _mdnsName.c_str()).c_str();
this->config.mdns.service = getString("service").c_str();
/* Wifi TX Power Config */
this->config.txpower.power = getUInt("power", 52);
/* WiFi Config */
int networkCount = getInt("networkCount", 0);
std::string name = "name";
std::string ssid = "ssid";
std::string password = "pass";
std::string channel = "channel";
std::string power = "power";
for (int i = 0; i < networkCount; i++) {
char buffer[2];
std::string iter_str = Helpers::itoa(i, buffer, 10);
name.append(iter_str);
ssid.append(iter_str);
password.append(iter_str);
channel.append(iter_str);
power.append(iter_str);
const std::string& temp_1 = getString(name.c_str()).c_str();
const std::string& temp_2 = getString(ssid.c_str()).c_str();
const std::string& temp_3 = getString(password.c_str()).c_str();
uint8_t temp_4 = getUInt(channel.c_str());
uint8_t temp_5 = getUInt(power.c_str());
//! push_back creates a copy of the object, so we need to use emplace_back
this->config.networks.emplace_back(
temp_1, temp_2, temp_3, temp_4, temp_5,
false); // false because the networks we store in the config are the
// ones we want the esp to connect to, rather than host as AP
}
/* AP Config */
this->config.ap_network.ssid = getString("apSSID").c_str();
this->config.ap_network.password = getString("apPass").c_str();
this->config.ap_network.channel = getUInt("apChannel");
/* Camera Config */
this->config.camera.vflip = getInt("vflip", 0);
this->config.camera.href = getInt("href", 0);
this->config.camera.framesize = getInt("framesize", 4);
this->config.camera.quality = getInt("quality", 7);
this->config.camera.brightness = getInt("brightness", BRIGHTNESS);
this->config.camera.fancy_auto_exposure_on = getInt("simple_auto_exposure_on", SIMPLE_AUTO_EXPOSURE_ON);
this->config.camera.fancy_auto_exposure_on = getInt("fancy_auto_exposure_on", FANCY_AUTO_EXPOSURE_ON);
this->config.camera.ae_level = getInt("ae_level", AE_LEVEL);
this->config.camera.aec_value = getInt("aec_value", AEC_VALUE);
this->config.camera.auto_gain_on = getInt("auto_gain_on ", AUTO_GAIN_ON);
this->_already_loaded = true;
this->notifyAll(ConfigState_e::configLoaded);
}
//**********************************************************************************************************************
//*
//! DeviceConfig
//*
//**********************************************************************************************************************
void ProjectConfig::setDeviceConfig(const std::string& OTALogin,
const std::string& OTAPassword,
int OTAPort,
bool shouldNotify) {
log_d("Updating device config");
this->config.device.OTALogin.assign(OTALogin);
this->config.device.OTAPassword.assign(OTAPassword);
this->config.device.OTAPort = OTAPort;
if (shouldNotify)
this->notifyAll(ConfigState_e::deviceConfigUpdated);
}
void ProjectConfig::setMDNSConfig(const std::string& hostname,
const std::string& service,
bool shouldNotify) {
log_d("Updating MDNS config");
this->config.mdns.hostname.assign(hostname);
this->config.mdns.service.assign(service);
if (shouldNotify)
this->notifyAll(ConfigState_e::mdnsConfigUpdated);
}
void ProjectConfig::setCameraConfig(uint8_t vflip,
uint8_t framesize,
uint8_t href,
uint8_t quality,
uint8_t brightness,
bool simple_auto_exposure_on,
bool fancy_auto_exposure_on,
int ae_level,
unsigned int aec_value,
bool auto_gain_on,
bool shouldNotify) {
log_d("Updating camera config");
this->config.camera.vflip = vflip;
this->config.camera.href = href;
this->config.camera.framesize = framesize;
this->config.camera.quality = quality;
this->config.camera.brightness = brightness;
this->config.camera.simple_auto_exposure_on = simple_auto_exposure_on;
this->config.camera.fancy_auto_exposure_on = fancy_auto_exposure_on;
this->config.camera.ae_level = ae_level;
this->config.camera.aec_value = aec_value;
this->config.camera.auto_gain_on = auto_gain_on;
log_d("Updating Camera config");
if (shouldNotify)
this->notifyAll(ConfigState_e::cameraConfigUpdated);
}
void ProjectConfig::setWifiConfig(const std::string& networkName,
const std::string& ssid,
const std::string& password,
uint8_t channel,
uint8_t power,
bool adhoc,
bool shouldNotify) {
// we store the ADHOC flag as false because the networks we store in the
// config are the ones we want the esp to connect to, rather than host as AP,
// and here we're just updating them
size_t size = this->config.networks.size();
for (auto it = this->config.networks.begin();
it != this->config.networks.end();) {
if (it->name == networkName) {
log_i("[Project Config]: Found network %s, updating it ...",
it->name.c_str());
it->name = networkName;
it->ssid = ssid;
it->password = password;
it->channel = channel;
it->power = power;
it->adhoc = false;
if (shouldNotify) {
wifiStateManager.setState(WiFiState_e::WiFiState_Disconnected);
WiFi.disconnect();
this->wifiConfigSave();
this->notifyAll(ConfigState_e::networksConfigUpdated);
}
return;
} else {
++it;
}
}
if (size < 3 && size > 0) {
Serial.println("We're adding a new network");
// we don't have that network yet, we can add it as we still have some
// space we're using emplace_back as push_back will create a copy of it,
// we want to avoid that
this->config.networks.emplace_back(networkName, ssid, password, channel,
power, false);
}
// we're allowing to store up to three additional networks
if (size == 0) {
Serial.println("No networks, We're adding a new network");
this->config.networks.emplace_back(networkName, ssid, password, channel,
power, false);
}
if (shouldNotify) {
wifiStateManager.setState(WiFiState_e::WiFiState_None);
WiFi.disconnect();
this->wifiConfigSave();
this->notifyAll(ConfigState_e::networksConfigUpdated);
}
}
void ProjectConfig::deleteWifiConfig(const std::string& networkName,
bool shouldNotify) {
size_t size = this->config.networks.size();
if (size == 0) {
Serial.println("No networks, nothing to delete");
}
for (auto it = this->config.networks.begin();
it != this->config.networks.end();) {
if (it->name == networkName) {
log_i("[Project Config]: Found network %s", it->name.c_str());
it = this->config.networks.erase(it);
log_i("[Project Config]: Deleted network %s", networkName.c_str());
} else {
++it;
}
}
if (shouldNotify) {
this->wifiConfigSave();
this->notifyAll(ConfigState_e::networksConfigUpdated);
}
}
void ProjectConfig::setWiFiTxPower(uint8_t power, bool shouldNotify) {
this->config.txpower.power = power;
log_d("Updating wifi tx power");
if (shouldNotify)
this->notifyAll(ConfigState_e::wifiTxPowerUpdated);
}
void ProjectConfig::setAPWifiConfig(const std::string& ssid,
const std::string& password,
uint8_t channel,
bool adhoc,
bool shouldNotify) {
this->config.ap_network.ssid.assign(ssid);
this->config.ap_network.password.assign(password);
this->config.ap_network.channel = channel;
this->config.ap_network.adhoc = adhoc;
log_d("Updating access point config");
if (shouldNotify) {
wifiStateManager.setState(WiFiState_e::WiFiState_None);
WiFi.disconnect();
this->wifiConfigSave();
this->notifyAll(ConfigState_e::networksConfigUpdated);
}
}
std::string ProjectConfig::DeviceConfig_t::toRepresentation() {
std::string json = Helpers::format_string(
"\"device_config\": {\"OTALogin\": \"%s\", \"OTAPassword\": \"%s\", "
"\"OTAPort\": %u}",
this->OTALogin.c_str(), this->OTAPassword.c_str(), this->OTAPort);
return json;
}
std::string ProjectConfig::MDNSConfig_t::toRepresentation() {
std::string json = Helpers::format_string(
"\"mdns_config\": {\"hostname\": \"%s\", \"service\": \"%s\"}",
this->hostname.c_str(), this->service.c_str());
return json;
}
std::string ProjectConfig::CameraConfig_t::toRepresentation() {
std::string json = Helpers::format_string(
"\"camera_config\": {\"vflip\": %d,\"framesize\": %d,\"href\": "
"%d,\"quality\": %d,\"brightness\": %d, \"simple_auto_exposure_on\": %d, \"fancy_auto_exposure_on\": %d,\"ae_level\": %d, \"aec_value\": %d, \"auto_gain_on\": %d }",
this->vflip, this->framesize, this->href, this->quality,
this->brightness, this->simple_auto_exposure_on, this->fancy_auto_exposure_on,
this->aec_value, this->ae_level, this->auto_gain_on);
return json;
}
std::string ProjectConfig::WiFiConfig_t::toRepresentation() {
std::string json = Helpers::format_string(
"{\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", "
"\"channel\": "
"%u, \"power\": %u,\"adhoc\": %s}",
this->name.c_str(), this->ssid.c_str(), this->password.c_str(),
this->channel, this->power, this->adhoc ? "true" : "false");
return json;
}
std::string ProjectConfig::AP_WiFiConfig_t::toRepresentation() {
std::string json = Helpers::format_string(
"\"ap_wifi_config\": {\"ssid\": \"%s\", \"password\": \"%s\", "
"\"channel\": %u, \"adhoc\": %s}",
this->ssid.c_str(), this->password.c_str(), this->channel,
this->adhoc ? "true" : "false");
return json;
}
std::string ProjectConfig::WiFiTxPower_t::toRepresentation() {
std::string json =
Helpers::format_string("\"wifi_tx_power\": {\"power\": %u}", this->power);
return json;
}
//**********************************************************************************************************************
//*
//! Get Methods
//*
//**********************************************************************************************************************
ProjectConfig::DeviceConfig_t& ProjectConfig::getDeviceConfig() {
return this->config.device;
}
ProjectConfig::CameraConfig_t& ProjectConfig::getCameraConfig() {
return this->config.camera;
}
std::vector<ProjectConfig::WiFiConfig_t>& ProjectConfig::getWifiConfigs() {
return this->config.networks;
}
ProjectConfig::AP_WiFiConfig_t& ProjectConfig::getAPWifiConfig() {
return this->config.ap_network;
}
ProjectConfig::MDNSConfig_t& ProjectConfig::getMDNSConfig() {
return this->config.mdns;
}
ProjectConfig::WiFiTxPower_t& ProjectConfig::getWiFiTxPowerConfig() {
return this->config.txpower;
}