-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathconfig.cpp
More file actions
919 lines (792 loc) · 25.4 KB
/
Copy pathconfig.cpp
File metadata and controls
919 lines (792 loc) · 25.4 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
#include "config.h"
#include "mifare_keys_manager.h"
#include "sd_functions.h"
#include <algorithm>
JsonDocument BruceConfig::toJson() const {
JsonDocument jsonDoc;
JsonObject setting = jsonDoc.to<JsonObject>();
setting["priColor"] = String(priColor, HEX);
setting["secColor"] = String(secColor, HEX);
setting["bgColor"] = String(bgColor, HEX);
setting["themeFile"] = themePath;
setting["themeOnSd"] = theme.fs;
setting["dimmerSet"] = dimmerSet;
setting["bright"] = bright;
setting["automaticTimeUpdateViaNTP"] = automaticTimeUpdateViaNTP;
setting["tmz"] = tmz;
setting["dst"] = dst;
setting["clock24hr"] = clock24hr;
setting["soundEnabled"] = soundEnabled;
setting["soundVolume"] = soundVolume;
setting["wifiAtStartup"] = wifiAtStartup;
setting["instantBoot"] = instantBoot;
setting["keyboardLang"] = keyboardLang;
#ifdef HAS_RGB_LED
setting["ledBright"] = ledBright;
setting["ledColor"] = String(ledColor, HEX);
setting["ledBlinkEnabled"] = ledBlinkEnabled;
setting["ledEffect"] = ledEffect;
setting["ledEffectSpeed"] = ledEffectSpeed;
setting["ledEffectDirection"] = ledEffectDirection;
#endif
JsonObject _webUI = setting["webUI"].to<JsonObject>();
_webUI["user"] = webUI.user;
_webUI["pwd"] = webUI.pwd;
JsonObject _webUISessions = setting["webUISessions"].to<JsonObject>();
for (size_t i = 0; i < webUISessions.size(); i++) { _webUISessions[String(i + 1)] = webUISessions[i]; }
JsonObject _wifiAp = setting["wifiAp"].to<JsonObject>();
_wifiAp["ssid"] = wifiAp.ssid;
_wifiAp["pwd"] = wifiAp.pwd;
setting["wifiMAC"] = wifiMAC; //@IncursioHack
setting["TerminalLog"] = TerminalLog;
JsonArray _evilWifiNames = setting["evilWifiNames"].to<JsonArray>();
for (auto key : evilWifiNames) _evilWifiNames.add(key);
JsonObject _evilWifiEndpoints = setting["evilWifiEndpoints"].to<JsonObject>();
_evilWifiEndpoints["getCredsEndpoint"] = evilPortalEndpoints.getCredsEndpoint;
_evilWifiEndpoints["setSsidEndpoint"] = evilPortalEndpoints.setSsidEndpoint;
_evilWifiEndpoints["showEndpoints"] = evilPortalEndpoints.showEndpoints;
_evilWifiEndpoints["allowSetSsid"] = evilPortalEndpoints.allowSetSsid;
_evilWifiEndpoints["allowGetCreds"] = evilPortalEndpoints.allowGetCreds;
_evilWifiEndpoints["gatewayIp"] = evilPortalGatewayIp;
setting["evilWifiPasswordMode"] = evilPortalPasswordMode;
JsonObject _wifi = setting["wifi"].to<JsonObject>();
for (const auto &pair : wifi) { _wifi[pair.first] = pair.second; }
setting["startupApp"] = startupApp;
setting["startupAppJSInterpreterFile"] = startupAppJSInterpreterFile;
setting["wigleBasicToken"] = wigleBasicToken;
setting["wdgwarsApiKey"] = wdgwarsApiKey;
setting["devMode"] = devMode;
setting["colorInverted"] = colorInverted;
setting["mainMenuStyle"] = mainMenuStyle;
setting["badUSBBLEKeyboardLayout"] = badUSBBLEKeyboardLayout;
setting["badUSBBLEKeyDelay"] = badUSBBLEKeyDelay;
setting["badUSBBLEShowOutput"] = badUSBBLEShowOutput;
JsonArray dm = setting["disabledMenus"].to<JsonArray>();
for (int i = 0; i < disabledMenus.size(); i++) { dm.add(disabledMenus[i]); }
JsonArray qrArray = setting["qrCodes"].to<JsonArray>();
for (const auto &entry : qrCodes) {
JsonObject qrEntry = qrArray.add<JsonObject>();
qrEntry["menuName"] = entry.menuName;
qrEntry["content"] = entry.content;
}
return jsonDoc;
}
void BruceConfig::fromFile(bool checkFS) {
FS *fs;
if (checkFS) {
if (!getFsStorage(fs)) {
log_i("Fail getting filesystem for config");
return;
}
} else {
if (checkLittleFsSize()) fs = &LittleFS;
else return;
}
if (!fs->exists(filepath)) {
log_i("Config file not found. Creating default config");
return saveFile();
}
File file;
file = fs->open(filepath, FILE_READ);
if (!file) {
log_i("Config file not found. Using default values");
return;
}
// Deserialize the JSON document
JsonDocument jsonDoc;
if (deserializeJson(jsonDoc, file)) {
Serial.println("Failed to read config file, using default configuration");
return;
}
file.close();
JsonObject setting = jsonDoc.as<JsonObject>();
int count = 0;
if (!setting["priColor"].isNull()) {
priColor = strtoul(setting["priColor"], nullptr, 16);
} else {
count++;
log_e("Fail");
}
if (!setting["secColor"].isNull()) {
secColor = strtoul(setting["secColor"], nullptr, 16);
} else {
count++;
log_e("Fail");
}
if (!setting["bgColor"].isNull()) {
bgColor = strtoul(setting["bgColor"], nullptr, 16);
} else {
count++;
log_e("Fail");
}
if (!setting["themeFile"].isNull()) {
themePath = setting["themeFile"].as<String>();
} else {
count++;
log_e("Fail");
}
if (!setting["themeOnSd"].isNull()) {
theme.fs = setting["themeOnSd"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["dimmerSet"].isNull()) {
dimmerSet = setting["dimmerSet"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["bright"].isNull()) {
bright = setting["bright"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["automaticTimeUpdateViaNTP"].isNull()) {
automaticTimeUpdateViaNTP = setting["automaticTimeUpdateViaNTP"].as<bool>();
} else {
count++;
log_e("Fail");
}
if (!setting["tmz"].isNull()) {
tmz = setting["tmz"].as<float>();
} else {
count++;
log_e("Fail");
}
if (!setting["dst"].isNull()) {
dst = setting["dst"].as<bool>();
} else {
count++;
log_e("Fail");
}
if (!setting["clock24hr"].isNull()) {
clock24hr = setting["clock24hr"].as<bool>();
} else {
count++;
log_e("Fail");
}
if (!setting["soundEnabled"].isNull()) {
soundEnabled = setting["soundEnabled"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["soundVolume"].isNull()) {
soundVolume = setting["soundVolume"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["wifiAtStartup"].isNull()) {
wifiAtStartup = setting["wifiAtStartup"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["instantBoot"].isNull()) {
instantBoot = setting["instantBoot"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["keyboardLang"].isNull()) {
keyboardLang = setting["keyboardLang"].as<String>();
} else {
keyboardLang = "QWERTY";
}
#ifdef HAS_RGB_LED
if (!setting["ledBright"].isNull()) {
ledBright = setting["ledBright"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["ledColor"].isNull()) {
ledColor = strtoul(setting["ledColor"], nullptr, 16);
} else {
count++;
log_e("Fail");
}
if (!setting["ledBlinkEnabled"].isNull()) {
ledBlinkEnabled = setting["ledBlinkEnabled"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["ledEffect"].isNull()) {
ledEffect = setting["ledEffect"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["ledEffectSpeed"].isNull()) {
ledEffectSpeed = setting["ledEffectSpeed"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["ledEffectDirection"].isNull()) {
ledEffectDirection = setting["ledEffectDirection"].as<int>();
} else {
count++;
log_e("Fail");
}
#endif
if (!setting["webUI"].isNull()) {
JsonObject webUIObj = setting["webUI"].as<JsonObject>();
webUI.user = webUIObj["user"].as<String>();
webUI.pwd = webUIObj["pwd"].as<String>();
} else {
count++;
log_e("Fail");
}
if (!setting["webUISessions"].isNull()) {
webUISessions.clear();
JsonObject webUISessionsObj = setting["webUISessions"].as<JsonObject>();
for (JsonPair kv : webUISessionsObj) { webUISessions.push_back(kv.value().as<String>()); }
} else {
count++;
log_e("Fail");
}
if (!setting["wifiAp"].isNull()) {
JsonObject wifiApObj = setting["wifiAp"].as<JsonObject>();
wifiAp.ssid = wifiApObj["ssid"].as<String>();
wifiAp.pwd = wifiApObj["pwd"].as<String>();
} else {
count++;
log_e("Fail");
}
//@IncursioHack
if (!setting["wifiMAC"].isNull()) {
wifiMAC = setting["wifiMAC"].as<String>();
} else {
wifiMAC = "";
count++;
log_e("wifiMAC not found, using default");
}
if (!setting["TerminalLog"].isNull()) {
TerminalLog = setting["TerminalLog"].as<bool>();
} else {
count++;
log_e("TerminalLog not found, using default");
}
// Wifi List
if (!setting["wifi"].isNull()) {
wifi.clear();
JsonObject wifiObj = setting["wifi"].as<JsonObject>();
for (JsonPair kv : wifiObj) wifi[kv.key().c_str()] = kv.value().as<String>();
} else {
count++;
log_e("Fail");
}
if (!setting["evilWifiNames"].isNull()) {
evilWifiNames.clear();
JsonArray _evilWifiNames = setting["evilWifiNames"].as<JsonArray>();
for (JsonVariant key : _evilWifiNames) evilWifiNames.insert(key.as<String>());
} else {
count++;
log_e("Fail");
}
if (!setting["evilWifiEndpoints"].isNull()) {
JsonObject evilPortalEndpointsObj = setting["evilWifiEndpoints"].as<JsonObject>();
evilPortalEndpoints.getCredsEndpoint = evilPortalEndpointsObj["getCredsEndpoint"].as<String>();
evilPortalEndpoints.setSsidEndpoint = evilPortalEndpointsObj["setSsidEndpoint"].as<String>();
evilPortalEndpoints.showEndpoints = evilPortalEndpointsObj["showEndpoints"].as<bool>();
evilPortalEndpoints.allowSetSsid = evilPortalEndpointsObj["allowSetSsid"].as<bool>();
evilPortalEndpoints.allowGetCreds = evilPortalEndpointsObj["allowGetCreds"].as<bool>();
if (!evilPortalEndpointsObj["gatewayIp"].isNull()) {
evilPortalGatewayIp = evilPortalEndpointsObj["gatewayIp"].as<String>();
} else {
evilPortalGatewayIp = "172.0.0.1";
}
} else {
count++;
log_e("Fail");
}
if (!setting["evilWifiPasswordMode"].isNull()) {
int mode = setting["evilWifiPasswordMode"].as<int>();
if (mode >= 0 && mode <= 2) {
evilPortalPasswordMode = static_cast<EvilPortalPasswordMode>(mode);
} else {
evilPortalPasswordMode = FULL_PASSWORD;
log_w("Invalid evilWifiPasswordMode, using FULL_PASSWORD");
}
} else {
count++;
log_e("Fail");
}
if (!setting["startupApp"].isNull()) {
startupApp = setting["startupApp"].as<String>();
} else {
count++;
log_e("Fail");
}
if (!setting["startupAppJSInterpreterFile"].isNull()) {
startupAppJSInterpreterFile = setting["startupAppJSInterpreterFile"].as<String>();
} else {
count++;
log_e("Fail");
}
if (!setting["wigleBasicToken"].isNull()) {
wigleBasicToken = setting["wigleBasicToken"].as<String>();
} else {
count++;
log_e("Fail");
}
if (!setting["wdgwarsApiKey"].isNull()) {
wdgwarsApiKey = setting["wdgwarsApiKey"].as<String>();
} else {
count++;
log_e("Fail");
}
if (!setting["devMode"].isNull()) {
devMode = setting["devMode"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["colorInverted"].isNull()) {
colorInverted = setting["colorInverted"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["mainMenuStyle"].isNull()) {
mainMenuStyle = setting["mainMenuStyle"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["badUSBBLEKeyboardLayout"].isNull()) {
badUSBBLEKeyboardLayout = setting["badUSBBLEKeyboardLayout"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["badUSBBLEKeyDelay"].isNull()) {
badUSBBLEKeyDelay = setting["badUSBBLEKeyDelay"].as<int>();
} else {
count++;
log_e("Fail");
}
if (!setting["badUSBBLEShowOutput"].isNull()) {
badUSBBLEShowOutput = setting["badUSBBLEShowOutput"].as<bool>();
} else {
count++;
log_e("Fail");
}
if (!setting["disabledMenus"].isNull()) {
disabledMenus.clear();
JsonArray dm = setting["disabledMenus"].as<JsonArray>();
for (JsonVariant e : dm) { disabledMenus.push_back(e.as<String>()); }
} else {
count++;
log_e("Fail");
}
if (!setting["qrCodes"].isNull()) {
qrCodes.clear();
JsonArray qrArray = setting["qrCodes"].as<JsonArray>();
for (JsonObject qrEntry : qrArray) {
String menuName = qrEntry["menuName"].as<String>();
String content = qrEntry["content"].as<String>();
qrCodes.push_back({menuName, content});
}
} else {
count++;
log_e("Fail to load qrCodes");
}
validateConfig();
if (count > 0) saveFile();
log_i("Using config from file");
}
void BruceConfig::saveFile() {
FS *fs = &LittleFS;
JsonDocument jsonDoc = toJson();
// Open file for writing
File file = fs->open(filepath, FILE_WRITE);
if (!file) {
log_e("Failed to open config file");
file.close();
return;
};
// Serialize JSON to file
serializeJsonPretty(jsonDoc, Serial);
if (serializeJsonPretty(jsonDoc, file) < 5) log_e("Failed to write config file");
else log_i("config file written successfully");
file.close();
if (setupSdCard()) copyToFs(LittleFS, SD, filepath, false);
}
void BruceConfig::factoryReset() {
FS *fs = &LittleFS;
fs->rename(String(filepath), "/bak." + String(filepath).substring(1));
if (setupSdCard()) SD.rename(String(filepath), "/bak." + String(filepath).substring(1));
ESP.restart();
}
void BruceConfig::validateConfig() {
validateDimmerValue();
validateBrightValue();
validateTmzValue();
validateSoundEnabledValue();
validateSoundVolumeValue();
validateWifiAtStartupValue();
#ifdef HAS_RGB_LED
validateLedBrightValue();
validateLedColorValue();
validateLedBlinkEnabledValue();
validateLedEffectValue();
validateLedEffectSpeedValue();
validateLedEffectDirectionValue();
#endif
validateMifareKeysItems();
validateDevModeValue();
validateColorInverted();
validateMainMenuStyle();
validateBadUSBBLEKeyboardLayout();
validateBadUSBBLEKeyDelay();
validateEvilEndpointCreds();
validateEvilEndpointSsid();
validateEvilPasswordMode();
validateEvilGatewayIp();
}
void BruceConfig::setUiColor(uint16_t primary, uint16_t *secondary, uint16_t *background) {
BruceTheme::_setUiColor(primary, secondary, background);
saveFile();
}
void BruceConfig::setDimmer(int value) {
dimmerSet = value;
validateDimmerValue();
saveFile();
}
void BruceConfig::validateDimmerValue() {
if (dimmerSet < 0) dimmerSet = 10;
if (dimmerSet > 60) dimmerSet = 0;
}
void BruceConfig::setBright(uint8_t value) {
bright = value;
validateBrightValue();
saveFile();
}
void BruceConfig::validateBrightValue() {
if (bright > 100) bright = 100;
}
void BruceConfig::setAutomaticTimeUpdateViaNTP(bool value) {
automaticTimeUpdateViaNTP = value;
saveFile();
}
void BruceConfig::setTmz(float value) {
tmz = value;
validateTmzValue();
saveFile();
}
void BruceConfig::validateTmzValue() {
if (tmz < -12 || tmz > 14) tmz = 0;
}
void BruceConfig::setDST(bool value) {
dst = value;
saveFile();
}
void BruceConfig::setClock24Hr(bool value) {
clock24hr = value;
saveFile();
}
void BruceConfig::setSoundEnabled(int value) {
soundEnabled = value;
validateSoundEnabledValue();
saveFile();
}
void BruceConfig::setSoundVolume(int value) {
soundVolume = value;
validateSoundVolumeValue();
saveFile();
}
void BruceConfig::validateSoundEnabledValue() {
if (soundEnabled > 1) soundEnabled = 1;
}
void BruceConfig::validateSoundVolumeValue() {
if (soundVolume > 100) soundVolume = 100;
}
void BruceConfig::setWifiAtStartup(int value) {
wifiAtStartup = value;
validateWifiAtStartupValue();
saveFile();
}
void BruceConfig::validateWifiAtStartupValue() {
if (wifiAtStartup > 1) wifiAtStartup = 1;
}
#ifdef HAS_RGB_LED
void BruceConfig::setLedBright(int value) {
ledBright = value;
validateLedBrightValue();
saveFile();
}
void BruceConfig::validateLedBrightValue() { ledBright = max(0, min(100, ledBright)); }
void BruceConfig::setLedColor(uint32_t value) {
ledColor = value;
validateLedColorValue();
saveFile();
}
void BruceConfig::validateLedColorValue() {
ledColor = max<uint32_t>(0, min<uint32_t>(0xFFFFFFFF, ledColor));
}
void BruceConfig::setLedBlinkEnabled(int value) {
ledBlinkEnabled = value;
validateLedBlinkEnabledValue();
saveFile();
}
void BruceConfig::validateLedBlinkEnabledValue() {
if (ledBlinkEnabled > 1) ledBlinkEnabled = 1;
}
void BruceConfig::setLedEffect(int value) {
ledEffect = value;
validateLedEffectValue();
saveFile();
}
void BruceConfig::validateLedEffectValue() {
if (ledEffect < 0 || ledEffect > 9) ledEffect = 0;
}
void BruceConfig::setLedEffectSpeed(int value) {
ledEffectSpeed = value;
validateLedEffectSpeedValue();
saveFile();
}
void BruceConfig::validateLedEffectSpeedValue() {
#ifdef HAS_ENCODER_LED
if (ledEffectSpeed > 11) ledEffectSpeed = 11;
#else
if (ledEffectSpeed > 10) ledEffectSpeed = 10;
#endif
if (ledEffectSpeed < 0) ledEffectSpeed = 1;
}
void BruceConfig::setLedEffectDirection(int value) {
ledEffectDirection = value;
validateLedEffectDirectionValue();
saveFile();
}
void BruceConfig::validateLedEffectDirectionValue() {
if (ledEffectDirection > 1 || ledEffectDirection == 0) ledEffectDirection = 1;
if (ledEffectDirection < -1) ledEffectDirection = -1;
}
#endif
void BruceConfig::setWebUICreds(const String &usr, const String &pwd) {
webUI.user = usr;
webUI.pwd = pwd;
saveFile();
}
void BruceConfig::setWifiApCreds(const String &ssid, const String &pwd) {
wifiAp.ssid = ssid;
wifiAp.pwd = pwd;
saveFile();
}
void BruceConfig::setTerminalLog(bool value) {
TerminalLog = value;
saveFile();
}
void BruceConfig::addWifiCredential(const String &ssid, const String &pwd) {
wifi[ssid] = pwd;
saveFile();
}
String BruceConfig::getWifiPassword(const String &ssid) const {
auto it = wifi.find(ssid);
if (it != wifi.end()) return it->second;
return "";
}
void BruceConfig::addEvilWifiName(String value) {
evilWifiNames.insert(value);
saveFile();
}
void BruceConfig::removeEvilWifiName(String value) {
evilWifiNames.erase(value);
saveFile();
}
void BruceConfig::setEvilEndpointCreds(String value) {
evilPortalEndpoints.getCredsEndpoint = value;
validateEvilEndpointCreds();
saveFile();
}
void BruceConfig::validateEvilEndpointCreds() {
if (evilPortalEndpoints.getCredsEndpoint == evilPortalEndpoints.setSsidEndpoint) {
// on collision reset to defaults
evilPortalEndpoints.getCredsEndpoint = "/creds";
}
if (evilPortalEndpoints.getCredsEndpoint[0] != '/') {
evilPortalEndpoints.getCredsEndpoint = '/' + evilPortalEndpoints.getCredsEndpoint;
}
}
void BruceConfig::setEvilEndpointSsid(String value) {
evilPortalEndpoints.setSsidEndpoint = value;
validateEvilEndpointCreds();
saveFile();
}
void BruceConfig::validateEvilEndpointSsid() {
if (evilPortalEndpoints.getCredsEndpoint == evilPortalEndpoints.setSsidEndpoint) {
// on collision reset to defaults
evilPortalEndpoints.setSsidEndpoint = "/ssid";
}
if (evilPortalEndpoints.setSsidEndpoint[0] != '/') {
evilPortalEndpoints.setSsidEndpoint = '/' + evilPortalEndpoints.setSsidEndpoint;
}
}
void BruceConfig::setEvilAllowEndpointDisplay(bool value) {
evilPortalEndpoints.showEndpoints = value;
saveFile();
}
void BruceConfig::setEvilAllowGetCreds(bool value) {
evilPortalEndpoints.allowGetCreds = value;
saveFile();
}
void BruceConfig::setEvilAllowSetSsid(bool value) {
evilPortalEndpoints.allowSetSsid = value;
saveFile();
}
void BruceConfig::setEvilPasswordMode(EvilPortalPasswordMode value) {
evilPortalPasswordMode = value;
saveFile();
}
void BruceConfig::validateEvilPasswordMode() {
if (evilPortalPasswordMode < 0 || evilPortalPasswordMode > 2) evilPortalPasswordMode = FULL_PASSWORD;
}
void BruceConfig::setEvilGatewayIp(String value) {
evilPortalGatewayIp = value;
validateEvilGatewayIp();
saveFile();
}
void BruceConfig::validateEvilGatewayIp() {
IPAddress gatewayIp;
if (!gatewayIp.fromString(evilPortalGatewayIp)) evilPortalGatewayIp = "172.0.0.1";
}
void BruceConfig::setStartupApp(String value) {
startupApp = value;
saveFile();
}
void BruceConfig::setStartupAppJSInterpreterFile(String value) {
startupAppJSInterpreterFile = value;
saveFile();
}
void BruceConfig::setWigleBasicToken(String value) {
wigleBasicToken = value;
saveFile();
}
void BruceConfig::setWdgwarsApiKey(String value) {
wdgwarsApiKey = value;
saveFile();
}
void BruceConfig::setDevMode(int value) {
devMode = value;
validateDevModeValue();
saveFile();
}
void BruceConfig::validateDevModeValue() {
if (devMode > 1) devMode = 1;
}
void BruceConfig::setColorInverted(int value) {
colorInverted = value;
validateColorInverted();
saveFile();
}
void BruceConfig::validateColorInverted() {
if (colorInverted > 1) colorInverted = 1;
}
void BruceConfig::setMainMenuStyle(int value) {
mainMenuStyle = value;
validateMainMenuStyle();
saveFile();
}
void BruceConfig::validateMainMenuStyle() {
if (mainMenuStyle < MAIN_MENU_CAROUSEL || mainMenuStyle > MAIN_MENU_GRID)
mainMenuStyle = MAIN_MENU_CAROUSEL;
}
void BruceConfig::setBadUSBBLEKeyboardLayout(int value) {
badUSBBLEKeyboardLayout = value;
validateBadUSBBLEKeyboardLayout();
saveFile();
}
void BruceConfig::validateBadUSBBLEKeyboardLayout() {
if (badUSBBLEKeyboardLayout < 0 || badUSBBLEKeyboardLayout > 13) badUSBBLEKeyboardLayout = 0;
}
void BruceConfig::setBadUSBBLEKeyDelay(uint16_t value) {
badUSBBLEKeyDelay = value;
validateBadUSBBLEKeyDelay();
saveFile();
}
void BruceConfig::validateBadUSBBLEKeyDelay() {
if (badUSBBLEKeyDelay < 0) badUSBBLEKeyDelay = 0;
if (badUSBBLEKeyDelay > 500) badUSBBLEKeyDelay = 500;
}
void BruceConfig::setBadUSBBLEShowOutput(bool value) {
badUSBBLEShowOutput = value;
saveFile();
}
void BruceConfig::ensureMifareKeysLoaded() {
if (!_mifareKeysLoaded) {
MifareKeysManager::ensureLoaded(mifareKeys);
_mifareKeysLoaded = true;
}
}
void BruceConfig::addMifareKey(String value) {
ensureMifareKeysLoaded();
MifareKeysManager::addKey(mifareKeys, value);
}
void BruceConfig::validateMifareKeysItems() {
if (_mifareKeysLoaded) MifareKeysManager::validateKeys(mifareKeys);
}
void BruceConfig::addDisabledMenu(String value) {
if (std::find(disabledMenus.begin(), disabledMenus.end(), value) != disabledMenus.end()) return;
disabledMenus.push_back(value);
saveFile();
}
void BruceConfig::removeDisabledMenu(String value) {
auto it = std::find(disabledMenus.begin(), disabledMenus.end(), value);
if (it == disabledMenus.end()) return;
disabledMenus.erase(it);
saveFile();
}
void BruceConfig::addQrCodeEntry(const String &menuName, const String &content) {
qrCodes.push_back({menuName, content});
saveFile();
}
void BruceConfig::removeQrCodeEntry(const String &menuName) {
size_t writeIndex = 0;
for (size_t readIndex = 0; readIndex < qrCodes.size(); ++readIndex) {
const QrCodeEntry &entry = qrCodes[readIndex];
if (entry.menuName != menuName) {
if (writeIndex != readIndex) { qrCodes[writeIndex] = std::move(qrCodes[readIndex]); }
++writeIndex;
}
}
if (writeIndex < qrCodes.size()) { qrCodes.erase(qrCodes.begin() + writeIndex, qrCodes.end()); }
saveFile();
}
void BruceConfig::addWebUISession(const String &token) {
webUISessions.push_back(token);
// Limit to maximum 5 sessions - remove oldest (first element) if exceeded
if (webUISessions.size() > 5) { webUISessions.erase(webUISessions.begin()); }
saveFile();
}
void BruceConfig::removeWebUISession(const String &token) {
for (auto it = webUISessions.begin(); it != webUISessions.end(); ++it) {
if (*it == token) {
webUISessions.erase(it);
break;
}
}
saveFile();
}
bool BruceConfig::isValidWebUISession(const String &token) {
auto it = std::find(webUISessions.begin(), webUISessions.end(), token);
if (it == webUISessions.end()) {
return false; // Token not found
}
// Check if token is already at the end (most recent position)
if (it == webUISessions.end() - 1) {
return true; // Already most recent, no changes needed
}
// Move token to end and save
webUISessions.erase(it);
webUISessions.push_back(token);
// Limit to maximum 10 sessions
if (webUISessions.size() > 10) { webUISessions.erase(webUISessions.begin()); }
saveFile();
return true;
}