-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbolty.ino
1040 lines (953 loc) · 34 KB
/
bolty.ino
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/*!
@file bolty.ino
@author Thilo
https://github.com/bitcoin-ring/Bolty
This sketch will start a wifi in AP or STA mode (WIFIMODE_*). If you want to
use STA mode you have to enter the WIFI credentials of your existing network.
A webserver allows to import keydata in form of the raw json of the lnbits
"Card key credentials" link. The keydata is saved on the eepom and should
therefore be availabe after reboot/powerdown. The saved keydata can be used
to provision/burn or wipe/delete ntag424 tags i.E. BoltCard, BoltRing.
Button1:
- śhort press: Load next boltconfiguration/keyset
- long press: toggle wifi on/off
Button2:
- śhort press: Toggle between burn and wipe
- long press: sleepmode
*/
/**************************************************************************/
#include <Arduino.h>
#include "bolt.h"
#include "gui.h"
#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>
#include <esp_wifi.h>
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
#include "ArduinoJson.h"
#include "AsyncJson.h"
#include "ESPAsyncWebSrv.h"
//#include <WiFiClient.h>
#include "FS.h"
#include "SPIFFS.h"
#include <WiFiAP.h>
#define DEST_FS_USES_SPIFFS
#include <ESP32-targz.h>
#include "tarstream.h"
#include "tardata.h"
#define WIFIMODE_AP 0
#define WIFIMODE_STA 1
#define CONFIGVERSION 1
#define WIFI_AP_PASSWORD_LENGTH 8
// uncommment the next line for static wifi password
//#define WIFI_AP_PASSWORD_STATIC "wango123"
#define HTTP_CREDENTIAL_SIZE 16
//if you change this do not exceed HTTP_CREDENTIAL_SIZE-1 chars!
const char* http_default_username = "bolty";
const char* http_default_password = "bolty";
#define MAX_BOLT_DEVICES 5
char charpool[] = {
"qweertzunopaasdfghjkyxcvbnm-?234567890QWEERTYUOPAASDFGHJKLZXCVBNM"};
// Set these to your desired credentials for WiFi AP-Mode.
char *ap_ssid = "Bolty";
#ifdef WIFI_AP_PASSWORD_STATIC
char ap_password[] = WIFI_AP_PASSWORD_STATIC;
#else
char ap_password[WIFI_AP_PASSWORD_LENGTH];
#endif
AsyncWebServer server(80);
const char *PARAM_CONFIG = "config";
// NFC Pins
#define PN532_SCK (17)
#define PN532_MOSI (13)
#define PN532_SS (15)
#define PN532_MISO (12)
#define PN532_RSTPD_N (2)
// For RSTPD_N to work i had to desolder a 10k resistor between RSTPD_N and VCC
#define APPS (3)
#define APP_KEYSETUP (0)
#define APP_BOLTBURN (1)
#define APP_BOLTWIPE (2)
#define APP_STATUS_START (0)
#define APP_STATUS_LOOP (1)
#define APP_STATUS_END (2)
BoltDevice bolt(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
struct sSettings {
char essid[33];
char password[65];
uint8_t wifimode;
bool wifi_enabled;
char http_username[HTTP_CREDENTIAL_SIZE];
char http_password[HTTP_CREDENTIAL_SIZE];
};
sSettings mSettings;
uint8_t app_active;
int8_t app_next;
uint8_t app_status;
String SIpAddress = "Waiting for ip..";
IPAddress myIP;
uint8_t active_bolt_config;
sBoltConfig mBoltConfig;
bool signal_update_screen = false;
int signal_restart_delayed = 0;
bool bolty_hw_ready = false;
typedef void (*tAppHandler)();
typedef void (*tEvtHandler)(uint8_t btn, uint8_t evt);
struct sAppHandler {
String app_title;
String app_desc;
tAppHandler app_start; // function pointer start lifecycle
tAppHandler app_end; // function pointer end lifecycle
tAppHandler app_loop; // function pointer mainloop
uint16_t app_fgcolor;
uint16_t app_bgcolor;
};
sAppHandler mAppHandler[APPS];
void saveSettings() {
char path[20];
sprintf(path, "/settings.dat");
fs::File myFile = SPIFFS.open(path, FILE_WRITE);
myFile.write((byte *)&mSettings, sizeof(sSettings));
myFile.close();
}
//Extract files needed by the webserver from the data in tardata.h
void extractfiles(){
Serial.println("Extracting files");
Stream *HTMLTarStream = new TarStream(data_tar, (size_t) data_tar_len);
//TarUnpacker *TARUnpacker = new TarUnpacker();
//TARUnpacker->haltOnError(true); // stop on fail (manual restart/reset required)
//TARUnpacker->setTarVerify(true); // true = enables health checks but slows down the overall process
//TARUnpacker->setupFSCallbacks(targzTotalBytesFn, targzFreeBytesFn); // prevent the partition from exploding, recommended
//TARGZUnpacker->setGzProgressCallback(BaseUnpacker::defaultProgressCallback ); // targzNullProgressCallback or defaultProgressCallback
//TARUnpacker->setLoggerCallback(BaseUnpacker::targzPrintLoggerCallback); // gz log verbosity
//TARUnpacker->setTarProgressCallback(BaseUnpacker::defaultProgressCallback); // prints the untarring progress for each individual file
//TARUnpacker->setTarStatusProgressCallback(BaseUnpacker::defaultTarStatusProgressCallback); // print the filenames as they're expanded
//TARUnpacker->setTarMessageCallback(BaseUnpacker::targzPrintLoggerCallback); // tar log verbosity
//if (!TARUnpacker->tarStreamExpander(HTMLTarStream, data_tar_len, tarGzFS, "/")){
TarGzUnpacker *TARGZUnpacker = new TarGzUnpacker();
TARGZUnpacker->haltOnError( true ); // stop on fail (manual restart/reset required)
TARGZUnpacker->setTarVerify( true ); // true = enables health checks but slows down the overall process
TARGZUnpacker->setupFSCallbacks( targzTotalBytesFn, targzFreeBytesFn ); // prevent the partition from exploding, recommended
TARGZUnpacker->setGzProgressCallback( BaseUnpacker::defaultProgressCallback ); // targzNullProgressCallback or defaultProgressCallback
TARGZUnpacker->setLoggerCallback( BaseUnpacker::targzPrintLoggerCallback ); // gz log verbosity
TARGZUnpacker->setTarProgressCallback( BaseUnpacker::defaultProgressCallback ); // prints the untarring progress for each individual file
TARGZUnpacker->setTarStatusProgressCallback( BaseUnpacker::defaultTarStatusProgressCallback ); // print the filenames as they're expanded
TARGZUnpacker->setTarMessageCallback( BaseUnpacker::targzPrintLoggerCallback ); // tar log verbosity
if( !TARGZUnpacker->tarGzStreamExpander( HTMLTarStream, tarGzFS ) ) {
Serial.println("Error while unpacking the webserver files");
return;
}
//write version info
fs::File myFile = SPIFFS.open("/fsversion.dat", FILE_WRITE);
myFile.write((byte *) &fsversion, sizeof(fsversion));
myFile.close();
Serial.println("done!");
}
int checkFsVersion(){
int myversion = -1;
fs::File myFile = SPIFFS.open("/fsversion.dat", FILE_READ);
myFile.read((byte *) &myversion, sizeof(myversion));
myFile.close();
Serial.println(myversion);
return myversion;
}
void loadSettings() {
char path[20];
sprintf(path, "/settings.dat");
Serial.print(path);
if (SPIFFS.exists(path) == 1) {
Serial.println(" found");
fs::File myFile = SPIFFS.open(path, FILE_READ);
myFile.read((byte *)&mSettings, sizeof(sSettings));
myFile.close();
} else {
Serial.println(" not found");
mSettings.essid[0] = 0;
mSettings.password[0] = 0;
mSettings.wifimode = WIFIMODE_AP;
mSettings.wifi_enabled = 1;
memcpy(mSettings.http_username, http_default_username, HTTP_CREDENTIAL_SIZE);
memcpy(mSettings.http_password, http_default_password, HTTP_CREDENTIAL_SIZE);
}
dumpsettings();
}
void saveBoltConfig(uint8_t slot) {
char path[20];
sprintf(path, "/config%02x.dat", slot);
fs::File myFile = SPIFFS.open(path, FILE_WRITE);
myFile.write((byte *)&mBoltConfig, sizeof(sBoltConfig));
myFile.close();
}
/*
uint8_t boltconfig_key[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; uint8_t newlen =
bolt.nfc->ntag424_addpadding(strlen(mBoltConfig.url), 16, (uint8_t *)
&mBoltConfig.url); Serial.println(bolt.nfc->ntag424_encrypt(boltconfig_key,
newlen, (uint8_t *) &mBoltConfig.url, testurl_enc));
Serial.println(bolt.nfc->ntag424_decrypt(boltconfig_key, newlen, testurl_enc,
testurl)); bolt.nfc->PrintHexChar(testurl, 56);
*/
void loadBoltConfig(uint8_t slot) {
char path[20];
sprintf(path, "/config%02x.dat", slot);
Serial.print(path);
Serial.println(sizeof(((sBoltConfig *)0)->url));
memset(&mBoltConfig, 0, sizeof(sBoltConfig));
uint8_t testurl_enc[sizeof(((sBoltConfig *)0)->url)];
uint8_t testurl[sizeof(((sBoltConfig *)0)->url)];
if (SPIFFS.exists(path) == 1) {
Serial.println(" found");
fs::File myFile = SPIFFS.open(path, FILE_READ);
myFile.read((byte *)&mBoltConfig, sizeof(sBoltConfig));
myFile.close();
} else {
Serial.println(" not found");
strcpy(mBoltConfig.card_name, "*new*");
mBoltConfig.url[0] = 0;
mBoltConfig.uid[0] = 0;
mBoltConfig.k0[0] = 0;
mBoltConfig.k1[0] = 0;
mBoltConfig.k2[0] = 0;
mBoltConfig.k3[0] = 0;
mBoltConfig.k4[0] = 0;
mBoltConfig.wallet_name[0] = 0;
mBoltConfig.wallet_url[0] = 0;
mBoltConfig.wallet_host[0] = 0;
}
dumpconfig();
}
String exportBoltConfig() {
String path = "/backup.dat";
SPIFFS.remove(path);
fs::File myFile = SPIFFS.open(path, FILE_APPEND);
for (uint8_t i = 0; i < MAX_BOLT_DEVICES; i++) {
loadBoltConfig(i);
myFile.write((byte *)&mBoltConfig, sizeof(sBoltConfig));
}
myFile.close();
loadBoltConfig(active_bolt_config);
return path;
}
void importBoltConfig() {
String path = "/backup.dat";
fs::File myFile = SPIFFS.open(path, FILE_READ);
for (uint8_t i = 0; i < MAX_BOLT_DEVICES; i++) {
myFile.seek(i * sizeof(sBoltConfig));
myFile.readBytes((char *)&mBoltConfig, sizeof(sBoltConfig));
// loadBoltConfig(i);
saveBoltConfig(i);
dumpconfig();
}
myFile.close();
loadBoltConfig(active_bolt_config);
}
// Keysetup
void app_keysetup_start() { Serial.println("app_KEYSETUP_start"); }
long lasttime = 0;
String default_app_message = "* Buttons are locked! *";
String app_message = default_app_message;
void app_keysetup_loop() {
if ((millis() - lasttime) > 200) {
lasttime = millis();
tft.setFreeFont(&FreeSans9pt7b);
tft.setTextColor(APPRED);
if (bolty_hw_ready){
bool success = bolt.scanUID();
app_message = bolt.getScannedUid();
}
if (app_message == "") {
app_message = default_app_message;
}
tft.fillRect(0, -3 + (3 * 23), tft.width(), 21, APPWHITE);
displayTextCentered(-3 + (4 * 21), app_message);
}
if (!mSettings.wifi_enabled) {
app_next = APP_BOLTBURN;
}
delay(50);
}
String web_keysetup_processor(const String &var) {
// Serial.println("web_keysetup_loop");
if (var == "wallet_name")
return mBoltConfig.wallet_name;
if (var == "wallet_host")
return mBoltConfig.wallet_host;
if (var == "wallet_url")
return mBoltConfig.wallet_url;
if (var == "wallet_link")
return mBoltConfig.wallet_url;
if (var == "uid")
return mBoltConfig.uid;
return processor_default(var);
}
void app_keysetup_end() { Serial.println("app_KEYSETUP_end"); }
// Ringsetup
void APP_BOLTBURN_start() { Serial.println("APP_BOLTBURN_start"); }
uint32_t previousMillis;
uint32_t Interval = 100;
void APP_BOLTBURN_loop() {
// Serial.println("APP_BOLTBURN_loop");
// set the keys
if (millis() - previousMillis < Interval) {
return;
}
previousMillis = millis();
Interval = 100;
if (bolty_hw_ready){
bolt.setDefautKeysCur();
bolt.setNewKey(mBoltConfig.k0, 0);
bolt.setNewKey(mBoltConfig.k1, 1);
bolt.setNewKey(mBoltConfig.k2, 2);
bolt.setNewKey(mBoltConfig.k3, 3);
bolt.setNewKey(mBoltConfig.k4, 4);
String lnurl = String(mBoltConfig.url);
uint8_t burn_result = bolt.burn(lnurl);
if (burn_result != JOBSTATUS_WAITING) {
previousMillis = millis();
Interval = 3000;
dumpconfig();
}
}
else{
tft.setFreeFont(&FreeSans9pt7b);
tft.setTextColor(APPRED);
tft.fillRect(0, -3 + (3 * 23), tft.width(), 21, APPWHITE);
displayTextCentered(-3 + (4 * 21), "nfc hw not ready");
}
}
String shortenkeys(const String &var){
return var.substring(0,3) + "*************";
}
String processor_default(const String &var){
if (var == "cnn")
return String(active_bolt_config + 1).c_str();
if (var == "cn")
return mBoltConfig.card_name;
if (var == "url")
return mBoltConfig.url;
if (var == "ks0")
return shortenkeys(mBoltConfig.k0);
if (var == "ks1")
return shortenkeys(mBoltConfig.k1);
if (var == "ks2")
return shortenkeys(mBoltConfig.k2);
if (var == "ks3")
return shortenkeys(mBoltConfig.k3);
if (var == "ks4")
return shortenkeys(mBoltConfig.k4);
if (var == "k0")
return mBoltConfig.k0;
if (var == "k1")
return mBoltConfig.k1;
if (var == "k2")
return mBoltConfig.k2;
if (var == "k3")
return mBoltConfig.k3;
if (var == "k4")
return mBoltConfig.k4;
if (var == "wifista")
return String(mSettings.wifimode);
if (var == "essid")
if (mSettings.wifimode == WIFIMODE_STA)
return String(mSettings.essid);
/*if (var == "essid")
//if (mSettings.wifimode == WIFIMODE_STA)
return String(mSettings.essid);
*/
return String();
}
String web_burn_processor(const String &var) {
Serial.println("web_ringsetup_loop");
if (var == "job")
return "Burn";
return processor_default(var);
}
void APP_BOLTBURN_end() { Serial.println("APP_BOLTBURN_end"); }
// Ringsetup
void APP_BOLTWIPE_start() { Serial.println("APP_BOLTWIPE_start"); }
void APP_BOLTWIPE_loop() {
// Serial.println("APP_BOLTWIPE_loop");
if (millis() - previousMillis < Interval) {
return;
}
previousMillis = millis();
Interval = 100;
if (bolty_hw_ready){
bolt.setDefautKeysNew();
bolt.setCurKey(mBoltConfig.k0, 0);
bolt.setCurKey(mBoltConfig.k1, 1);
bolt.setCurKey(mBoltConfig.k2, 2);
bolt.setCurKey(mBoltConfig.k3, 3);
bolt.setCurKey(mBoltConfig.k4, 4);
uint8_t wipe_result = bolt.wipe();
if (wipe_result != JOBSTATUS_WAITING) {
previousMillis = millis();
Interval = 3000;
dumpconfig();
}
}
else{
tft.setFreeFont(&FreeSans9pt7b);
tft.setTextColor(APPRED);
tft.fillRect(0, -3 + (3 * 23), tft.width(), 21, APPWHITE);
displayTextCentered(-3 + (4 * 21), "nfc hw not ready");
}
}
String web_ringwipe_processor(const String &var) {
Serial.println("web_ringwipe_loop");
if (var == "job")
return "Wipe";
return processor_default(var);
}
void APP_BOLTWIPE_end() { Serial.println("APP_BOLTWIPE_end"); }
uint8_t lineh = 21;
void update_screen() {
tft.fillScreen(fromrgb(0xed, 0xef, 0xf2));
int8_t ofs = -3;
tft.fillRect(0, 0, tft.width(), 23, mAppHandler[app_active].app_bgcolor);
draw_battery(true);
draw_wifi(mSettings.wifi_enabled);
tft.setFreeFont(&FreeSans9pt7b);
tft.setTextColor(APPWHITE);
displayTextCentered(ofs + (1 * lineh), mAppHandler[app_active].app_title);
tft.setTextColor(mAppHandler[app_active].app_fgcolor);
displayTextCentered(ofs + (2 * lineh), String(active_bolt_config + 1) + ". " +
mBoltConfig.card_name);
displayTextCentered(ofs + (3 * lineh), mAppHandler[app_active].app_desc);
if (mSettings.wifi_enabled) {
// displayTextLeft(ofs + (4 * lineh), "WiFi");
if (mSettings.wifimode == WIFIMODE_AP) {
displayTextLeft(ofs + (5 * lineh),
"WiFi " + String(ap_ssid) + ":" + String(ap_password));
}
SIpAddress = getIpAddress();
displayTextLeft(ofs + (6 * lineh), SIpAddress);
}
signal_update_screen = false;
}
void handle_events() {
// Button 0 short clicky = next keyset
if ((sharedvars.appbuttons[0] == 1) && (app_active != APP_KEYSETUP)) {
// we dont want to interrupt anything done with keyysetup
active_bolt_config += 1;
if (active_bolt_config >= MAX_BOLT_DEVICES) {
active_bolt_config = 0;
}
loadBoltConfig(active_bolt_config);
signal_update_screen = true;
}
// Button 1 short clicky = next app
if ((sharedvars.appbuttons[1] == 1) && (app_active != APP_KEYSETUP)) {
// we dont want to interrupt anything done with keyysetup
app_next = app_active + 1;
if (app_next > APPS - 1) {
app_next = 1;
}
}
// Button 0 long clicky =
if (sharedvars.appbuttons[0] == 2) {
wifi_toogle();
signal_update_screen = true;
}
// Button 1 long clicky =
if (sharedvars.appbuttons[1] == 2) {
nfc_stop();
esp_sleep_enable_ext0_wakeup(GPIO_NUM_35, 0); // 1 = High, 0 = Low
esp_deep_sleep_start();
}
// Button 0 double clicky =
if (sharedvars.appbuttons[0] == 3) {
Serial.println("double click btn0");
String wurl =
String(mBoltConfig.wallet_host) + "?" + String(mBoltConfig.wallet_url);
Serial.println(wurl);
// displayQR(wurl);
}
// Button 1 duoble clicky =
if (sharedvars.appbuttons[1] == 3) {
Serial.println("double click btn1");
}
// reset button events
sharedvars.appbuttons[0] = 0;
sharedvars.appbuttons[1] = 0;
}
void app_stateengine() {
handle_events();
if (signal_update_screen){
update_screen();
}
if (signal_restart_delayed > 0){
delay(3000);
ESP.restart();
}
if (app_next >= APPS)
app_next = 0;
// do not switch to keysetup using buttons
if (app_next < 0)
app_next = APPS - 1;
if (app_active != app_next) {
Serial.print("start: current app:");
Serial.println(app_active);
Serial.print("switching to app:");
Serial.println(app_next);
app_status = APP_STATUS_END;
}
if (app_status == APP_STATUS_START) {
(*mAppHandler[app_active].app_start)();
app_status = APP_STATUS_LOOP;
update_screen();
}
if (app_status == APP_STATUS_LOOP) {
draw_battery();
draw_wifi(mSettings.wifi_enabled);
(*mAppHandler[app_active].app_loop)();
}
if (app_status == APP_STATUS_END) {
Serial.print("end: ending app:");
Serial.println(app_active);
Serial.print("end: activating app:");
Serial.println(app_next);
(*mAppHandler[app_active].app_end)();
app_active = app_next;
app_status = APP_STATUS_START;
}
}
void dumpconfig() {
Serial.println(mBoltConfig.wallet_name);
Serial.println(mBoltConfig.wallet_host);
Serial.println(mBoltConfig.wallet_url);
Serial.println(mBoltConfig.uid);
Serial.println(mBoltConfig.card_name);
Serial.println(mBoltConfig.url);
Serial.println(mBoltConfig.k0);
Serial.println(mBoltConfig.k1);
Serial.println(mBoltConfig.k2);
Serial.println(mBoltConfig.k3);
Serial.println(mBoltConfig.k4);
}
void dumpsettings() {
Serial.println(mSettings.wifimode);
Serial.println(mSettings.essid);
Serial.println(mSettings.password);
Serial.println(mSettings.wifi_enabled);
Serial.println(mSettings.http_username);
Serial.println(mSettings.http_password);
}
void checkparams(AsyncWebServerRequest *request) {
if (request->hasParam("d")) {
AsyncWebParameter *p = request->getParam("d");
Serial.println("got d param");
Serial.println(p->value().c_str());
if (p->value() == "p") {
active_bolt_config =
constrain((active_bolt_config - 1), 0, MAX_BOLT_DEVICES - 1);
}
if (p->value() == "n") {
active_bolt_config =
constrain((active_bolt_config + 1), 0, MAX_BOLT_DEVICES - 1);
}
loadBoltConfig(active_bolt_config);
signal_update_screen = true;
}
Serial.println(active_bolt_config);
}
void empty() {
//
}
void randomchar(char *outbuf, uint8_t count) {
for (uint8_t i = 0; i < count; i++) {
Serial.print(i);
Serial.print(":");
outbuf[i] = charpool[random(0, 63)];
Serial.println(outbuf[i]);
}
outbuf[count] = 0;
}
String getIpAddress() {
if (mSettings.wifimode == WIFIMODE_AP)
myIP = WiFi.softAPIP();
if (mSettings.wifimode == WIFIMODE_STA)
myIP = WiFi.localIP();
return myIP.toString();
}
void wifi_start() {
// if we have credentials try connecting. WIFIMODE_STA will fallback to
// mSettings.wifimode == WIFIMODE_AP
if ((mSettings.essid != "") && (mSettings.password != "")) {
mSettings.wifimode = WIFIMODE_STA;
}
if (mSettings.wifimode == WIFIMODE_AP) {
#ifndef WIFI_AP_PASSWORD_STATIC
randomchar(ap_password, sizeof(ap_password));
#endif
WiFi.softAP(ap_ssid, ap_password);
myIP = WiFi.softAPIP();
}
uint8_t connect_count = 0;
if (mSettings.wifimode == WIFIMODE_STA) {
WiFi.begin(mSettings.essid, mSettings.password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
connect_count++;
Serial.println("Connecting to WiFi..");
// fall back to AP mode if we cannot connect for 10 seconds
if (connect_count > 10) {
Serial.println("Cannot connect to Network. Fallback to AP-Mode.");
wifi_stop();
delay(50);
mSettings.wifimode = WIFIMODE_AP;
#ifndef WIFI_AP_PASSWORD_STATIC
randomchar(ap_password, sizeof(ap_password));
#endif
WiFi.softAP(ap_ssid, ap_password);
myIP = WiFi.softAPIP();
break;
}
}
// still STA mode? then we should be connected by now!
if (mSettings.wifimode == WIFIMODE_STA) {
myIP = WiFi.localIP();
}
}
if (!mSettings.wifi_enabled) {
mSettings.wifi_enabled = true;
saveSettings();
//set wifimode after savesettings. we dont want to persist it if we fallback to apmode.
if ((mSettings.wifimode == WIFIMODE_STA) && (connect_count <= 10)) {
mSettings.wifimode = WIFIMODE_AP;
}
}
server.begin();
}
void wifi_stop() {
server.end();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
esp_wifi_stop();
Serial.println("WIFI: Disconnected");
delay(100);
WiFi.mode(WIFI_OFF);
delay(100);
if (mSettings.wifi_enabled) {
mSettings.wifi_enabled = false;
saveSettings();
}
}
void wifi_toogle() {
if (mSettings.wifi_enabled) {
wifi_stop();
} else {
wifi_start();
}
}
void nfc_start() {
Serial.println("switching nfc on");
digitalWrite(PN532_RSTPD_N, HIGH);
}
void nfc_stop() {
Serial.println("switching nfc off");
digitalWrite(PN532_RSTPD_N, LOW);
}
// handles uploads
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
uint8_t *data, size_t len, bool final) {
String logmessage = "Client:" + request->client()->remoteIP().toString() +
" " + request->url();
Serial.println(logmessage);
if (!index) {
logmessage = "Upload Start: " + String(filename);
// open the file on first call and store the file handle in the request
// object
request->_tempFile = SPIFFS.open("/backup.dat", "w");
Serial.println(logmessage);
}
if (len) {
// stream the incoming chunk to the opened file
request->_tempFile.write(data, len);
logmessage = "Writing file: " + String(filename) +
" index=" + String(index) + " len=" + String(len);
Serial.println(logmessage);
}
if (final) {
logmessage = "Upload Complete: " + String(filename) +
",size: " + String(index + len);
// close the file handle as the upload is now done
request->_tempFile.close();
Serial.println(logmessage);
importBoltConfig();
request->redirect("/");
}
}
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // for Leonardo/Micro/Zero
// setup tft display
setup_display();
// Initialize SPIFFS
if (!tarGzFS.begin()) {
Serial.println("An Error has occurred while mounting SPIFFS");
#if defined DEST_FS_USES_SPIFFS || defined DEST_FS_USES_LITTLEFS
Serial.println("Initializing flash!");
displayMessage("init flash...", 0);
tarGzFS.format();
#endif
if (!tarGzFS.begin()){
Serial.println("Failed to mount filesystem!");
while (true)
delay(1000);
}
displayMessage("extract files...", 0);
extractfiles();
}
if ((!SPIFFS.exists("/wipe.html")) || (!SPIFFS.exists("/fsversion.dat"))) {
Serial.println("Could not find files!");
displayMessage("extract files", 0);
extractfiles();
}
else if (checkFsVersion() < fsversion) {
Serial.println("updating");
displayMessage("updating...", 0);
extractfiles();
}
displayMessage("setup nfc", 0);
pinMode(PN532_RSTPD_N, OUTPUT);
nfc_start();
bolty_hw_ready = bolt.begin();
Serial.println("Setup done!");
app_active = APP_KEYSETUP;
app_next = APP_BOLTBURN;
app_status = APP_STATUS_START;
mAppHandler[APP_KEYSETUP].app_title = "Key-Setup";
mAppHandler[APP_KEYSETUP].app_desc = "Use a webbrowser";
mAppHandler[APP_KEYSETUP].app_start = app_keysetup_start;
mAppHandler[APP_KEYSETUP].app_end = app_keysetup_end;
mAppHandler[APP_KEYSETUP].app_loop = app_keysetup_loop;
mAppHandler[APP_KEYSETUP].app_fgcolor = APPBLACK;
mAppHandler[APP_KEYSETUP].app_bgcolor = fromrgb(0x3e, 0xaf, 0x7c);
mAppHandler[APP_BOLTBURN].app_title = "Burn";
mAppHandler[APP_BOLTBURN].app_desc = "Burn a Bolt Card";
mAppHandler[APP_BOLTBURN].app_start = APP_BOLTBURN_start;
mAppHandler[APP_BOLTBURN].app_end = APP_BOLTBURN_end;
mAppHandler[APP_BOLTBURN].app_loop = APP_BOLTBURN_loop;
mAppHandler[APP_BOLTBURN].app_fgcolor = APPBLACK;
mAppHandler[APP_BOLTBURN].app_bgcolor = fromrgb(0xff, 0xad, 0x33);
mAppHandler[APP_BOLTWIPE].app_title = "Wipe";
mAppHandler[APP_BOLTWIPE].app_desc = "Wipe a Bolt Card";
mAppHandler[APP_BOLTWIPE].app_start = APP_BOLTWIPE_start;
mAppHandler[APP_BOLTWIPE].app_end = APP_BOLTWIPE_end;
mAppHandler[APP_BOLTWIPE].app_loop = APP_BOLTWIPE_loop;
mAppHandler[APP_BOLTWIPE].app_fgcolor = APPBLACK;
mAppHandler[APP_BOLTWIPE].app_bgcolor = fromrgb(0xee, 0xa0, 0xa0);
// initialize the bolt configurations
active_bolt_config = 0;
loadBoltConfig(active_bolt_config);
Serial.println("Loading settings...");
displayMessage("load settings", 0);
loadSettings();
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
app_next = APP_BOLTBURN;
checkparams(request);
request->send(SPIFFS, "/burn.html", String(), false, web_burn_processor);
});
// Route for root / web page
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/favicon.ico", "image/png");
});
// Export Config
server.on("/export", HTTP_GET, [](AsyncWebServerRequest *request) {
String exportfile = exportBoltConfig();
request->send(SPIFFS, exportfile, "application/octet-stream");
});
// Route for keys / web page
server.on("/setup", HTTP_GET, [](AsyncWebServerRequest *request) {
if(!request->authenticate(mSettings.http_username, mSettings.http_password))
return request->requestAuthentication();
app_next = APP_KEYSETUP;
checkparams(request);
AsyncWebServerResponse *response = request->beginResponse(
SPIFFS, "/setup.html", String(), false,
web_keysetup_processor); // Sends File with cross-origin-header
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
// request->send(SPIFFS, "/setup.html", String(), false,
// web_keysetup_processor);
});
// Route for root / web page
server.on("/wipe", HTTP_GET, [](AsyncWebServerRequest *request) {
// Serial.println("request wipe");
app_next = APP_BOLTWIPE;
checkparams(request);
request->send(SPIFFS, "/wipe.html", String(), false,
web_ringwipe_processor);
});
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse(
SPIFFS, "/style.css", String());
response->addHeader("last-modified", "Mon, 13 Jun 2022 11:05:21 GMT");
response->addHeader("expires", "Sun, 13 Jun 2032 11:05:21 GMT");
response->addHeader("cache-control", "max-age=86400");
request->send(response);
});
server.on("/setup.js", HTTP_GET, [](AsyncWebServerRequest *request) {
if(!request->authenticate(mSettings.http_username, mSettings.http_password))
return request->requestAuthentication();
AsyncWebServerResponse *response = request->beginResponse(
SPIFFS, "/setup.js", String());
response->addHeader("last-modified", "Mon, 13 Jun 2022 11:05:21 GMT");
response->addHeader("expires", "Sun, 13 Jun 2032 11:05:21 GMT");
response->addHeader("cache-control", "max-age=86400");
request->send(response);
});
server.on("/bolty.js", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse(
SPIFFS, "/bolty.js", String());
response->addHeader("last-modified", "Mon, 13 Jun 2022 11:05:21 GMT");
response->addHeader("expires", "Sun, 13 Jun 2032 11:05:21 GMT");
response->addHeader("cache-control", "max-age=86400");
request->send(response);
});
server.on("/qr", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncResponseStream *response =
request->beginResponseStream("text/html; charset=utf-8");
response->print(
"<html><head><link rel='stylesheet' media='screen' "
"href='https://fontlibrary.org/face/dejavu-sans-mono' "
"type='text/css'></head><body><pre style='font-family: "
"DejaVuSansMonoBold,monospace;font-size: 0.2em;'>"); // font-size: 1vw;
String walurl = web_keysetup_processor("wallet_link");
SendQR(walurl, response);
response->print("</pre></body></html>");
request->send(response);
});
AsyncCallbackJsonWebHandler *handler = new AsyncCallbackJsonWebHandler(
"/status", [](AsyncWebServerRequest *request, JsonVariant &json) {
// Serial.println(bolt.get_job_status());
request->send(200, "application/json",
"{\"status\":\"" + bolt.get_job_status() +
"\",\"app\":\"" + app_active + "\",\"cnn\":\"" +
(active_bolt_config + 1) + "\"}");
});
server.addHandler(handler);
AsyncCallbackJsonWebHandler *handler_uid = new AsyncCallbackJsonWebHandler(
"/uid", [](AsyncWebServerRequest *request, JsonVariant &json) {
// Serial.println(bolt.get_job_status());
request->send(200, "application/json",
"{\"uid\":\"" + bolt.getScannedUid() + "\"}");
});
server.addHandler(handler_uid);
AsyncCallbackJsonWebHandler *handler2 = new AsyncCallbackJsonWebHandler(
"/keys", [](AsyncWebServerRequest *request, JsonVariant &json) {
if(!request->authenticate(mSettings.http_username, mSettings.http_password))
return request->requestAuthentication();
Serial.println("received keys");
StaticJsonDocument<200> data;
if (json.is<JsonArray>()) {
data = json.as<JsonArray>();
} else if (json.is<JsonObject>()) {
data = json.as<JsonObject>();
}
if (data.containsKey("card_name"))
strcpy(mBoltConfig.card_name, data["card_name"]);
if (data.containsKey("lnurlw_base"))
strcpy(mBoltConfig.url, data["lnurlw_base"]);
if (data.containsKey("wallet_name"))
strcpy(mBoltConfig.wallet_name, data["wallet_name"]);
if (data.containsKey("wallet_url"))
strcpy(mBoltConfig.wallet_url, data["wallet_url"]);
if (data.containsKey("wallet_host"))
strcpy(mBoltConfig.wallet_host, data["wallet_host"]);
if (data.containsKey("uid"))
strcpy(mBoltConfig.uid, data["uid"]);
if (data.containsKey("k0"))
strcpy(mBoltConfig.k0, data["k0"]);
if (data.containsKey("k1"))
strcpy(mBoltConfig.k1, data["k1"]);
if (data.containsKey("k2"))
strcpy(mBoltConfig.k2, data["k2"]);
if (data.containsKey("k3"))
strcpy(mBoltConfig.k3, data["k3"]);
if (data.containsKey("k4"))
strcpy(mBoltConfig.k4, data["k4"]);
saveBoltConfig(active_bolt_config);
Serial.println(mBoltConfig.card_name);
request->send(200, "application/json",
"{\"status\":\"received_keys\"}");
});
server.addHandler(handler2);
AsyncCallbackJsonWebHandler *handlerwifi = new AsyncCallbackJsonWebHandler(
"/wifi", [](AsyncWebServerRequest *request, JsonVariant &json) {
if(!request->authenticate(mSettings.http_username, mSettings.http_password))
return request->requestAuthentication();
Serial.println("received wifi-settings");
StaticJsonDocument<200> data;
if (json.is<JsonArray>()) {
data = json.as<JsonArray>();
} else if (json.is<JsonObject>()) {
data = json.as<JsonObject>();
}
if (data["essid"] != "") {
strcpy(mSettings.essid, data["essid"]);
}
if ((data["password"] != "*123--keep-my-current-password--321*")) {
strcpy(mSettings.password, data["password"]);
}
if (data["wifimode"] == "sta") {
mSettings.wifimode = WIFIMODE_STA;
} else {
mSettings.wifimode = WIFIMODE_AP;
}
saveSettings();
request->send(200, "application/json",
"{\"status\":\"received_keys\"}");
signal_restart_delayed = 2000;
});
server.addHandler(handlerwifi);
AsyncCallbackJsonWebHandler *handlerac = new AsyncCallbackJsonWebHandler(
"/ac", [](AsyncWebServerRequest *request, JsonVariant &json) {
if(!request->authenticate(mSettings.http_username, mSettings.http_password))
return request->requestAuthentication();
Serial.println("received credential-settings");
StaticJsonDocument<200> data;
if (json.is<JsonArray>()) {
data = json.as<JsonArray>();
} else if (json.is<JsonObject>()) {
data = json.as<JsonObject>();
}