-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaracal_Firmware.ino
949 lines (838 loc) · 23.7 KB
/
Caracal_Firmware.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
/*
ESP8266 WiFi Shield for xatLabs IBIS converter
(C) 2017-2018 Julian Metzler
*/
/*
IMPORTANT
For the HTTPS OTA update to work, the verify() method of TLSTraits
in ESP8266HTTPClient.cpp needs to be modified to always return true.
*/
/*
UPLOAD SETTINGS
Board: Generic ESP8266 Module
Flash Mode: DIO
Flash Size: 4M
SPIFFS Size: 1M
Debug port: Disabled
Debug Level: None
Reset Method: ck
Flash Freq: 40 MHz
CPU Freq: 80 MHz
Upload Speed: 115200
*/
#define ARDUINO_OTA_ENABLEDXXX
#define INIT_EEPROMXXX
#define SERIAL_DEBUGXXX
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <EEPROM.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <ESP8266mDNS.h>
#ifdef ARDUINO_OTA_ENABLED
#include <ArduinoOTA.h>
#endif
#include "ibis.h"
/*
TYPEDEFS
*/
enum UpdateStatus {
US_NO_UPDATE,
US_AVAILABLE,
US_FAILED
};
/*
CONSTANTS
*/
#define HW_GROUP 2 // Changes with hardware changes that require software changes
#if HW_GROUP == 1
#define PIN_STATUS 10
#define PIN_ACTIVITY 9 // Unusable
#define PIN_CONFIG 0
#endif
#if HW_GROUP == 2
#define PIN_STATUS 5
#define PIN_ACTIVITY 4
#define PIN_CONFIG 0
#endif
#define UPDATE_START_DELAY 3000
#define EEPROM_SIZE 129
#define DEFAULT_HOSTNAME "xatLabs-WiFi-Module"
#define DEFAULT_STA_TIMEOUT 10
/*
GLOBAL VARIABLES
*/
unsigned long FW_VERSION = 2011110001; // Changes with each release; must always increase
unsigned long SP_VERSION = 0; // Loaded from SPIFFS; changed with each SPIFFS build; must always increase (uses timestamp as version)
// FW & SPIFFS update settings
const char* UPDATE_HOST = "static.mezgrman.de";
const int UPDATE_PORT_HTTPS = 443;
const int UPDATE_PORT_HTTP = 80;
String UPDATE_PATH_BASE_HTTPS = "/firmware/wifi_shield/";
String UPDATE_URL_BASE_HTTPS = "https://static.mezgrman.de/firmware/wifi_shield/";
String UPDATE_URL_BASE_HTTP = "http://static.mezgrman.de/firmware/wifi_shield/";
String UPDATE_FINGERPRINT_HTTPS = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"; // Not important, will be ignored anyway
// Update flags
bool updateFWSecure_flag = false;
bool updateSPSecure_flag = false;
bool updateFWInsecure_flag = false;
bool updateSPInsecure_flag = false;
unsigned long updateFlagSetTimestamp = 0;
// Start time of the last WiFi connection attempt
unsigned long wifiTimer = 0;
bool wifiTimedOut = false;
// Variables for Station WiFi
String STA_SSID;
String STA_PASS;
String STA_HOSTNAME;
char STA_HOSTNAME_CHAR[33];
bool STA_SETUP = false;
unsigned char STA_TIMEOUT = DEFAULT_STA_TIMEOUT;
// Variables for Access Point WiFi
String AP_SSID = "xatLabs WiFi Module";
String AP_PASS = "xatlabs_wifi";
bool AP_ACTIVE = false;
// Variable for keeping track of the IP address
String IP_ADDRESS;
// Variables for keeping track of the config button
volatile bool btnState = 0; // Current button state
bool oldBtnState = 0; // Previous button state
volatile bool btnPressed = 0; // Flag to check if the last button press has already been processed
volatile unsigned long btnTimer = 0; // Start time of last button press (only while pressed)
volatile unsigned long btnDur = 0; // Duration of the last button press (only while released)
ESP8266WebServer server(80);
WiFiServer IBISServer(5001);
WiFiClient client;
/*
CONFIG FILE HANDLING
*/
void reset_EEPROM() {
for (byte i = 0; i < EEPROM_SIZE; i++) {
EEPROM.write(i, 0x00);
}
EEPROM.commit();
}
void reset_EEPROM(uint16_t startAddress, uint16_t endAddress) {
for (uint16_t i = startAddress; (i < EEPROM_SIZE && i < endAddress); i++) {
EEPROM.write(i, 0x00);
}
EEPROM.commit();
}
bool loadConfig() {
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
return false;
}
size_t size = configFile.size();
if (size > 1024) {
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success()) {
return false;
}
SP_VERSION = json["SPVersion"];
char curChar;
bool ssidSetup = false;
bool passSetup = false;
bool hostnameSetup = false;
STA_SSID = "";
for (byte i = 0; i < 32; i++) {
curChar = EEPROM.read(i);
if (curChar == 0x00) {
break;
}
STA_SSID += curChar;
ssidSetup = true;
}
STA_PASS = "";
for (byte i = 0; i < 64; i++) {
curChar = EEPROM.read(i + 32);
if (curChar == 0x00) {
break;
}
STA_PASS += curChar;
passSetup = true;
}
STA_HOSTNAME = "";
for (byte i = 0; i < 32; i++) {
curChar = EEPROM.read(i + 96);
if (curChar == 0x00) {
break;
}
STA_HOSTNAME += curChar;
hostnameSetup = true;
}
if (STA_HOSTNAME == "") STA_HOSTNAME = DEFAULT_HOSTNAME;
STA_HOSTNAME.toCharArray(STA_HOSTNAME_CHAR, 32);
STA_TIMEOUT = EEPROM.read(128);
STA_SETUP = (ssidSetup && passSetup);
return true;
}
bool saveConfig() {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["SPVersion"] = SP_VERSION;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
return false;
}
json.printTo(configFile);
reset_EEPROM();
for (byte i = 0; i < STA_SSID.length(); i++) {
EEPROM.write(i, STA_SSID.charAt(i));
}
for (byte i = 0; i < STA_PASS.length(); i++) {
EEPROM.write(i + 32, STA_PASS.charAt(i));
}
for (byte i = 0; i < STA_HOSTNAME.length(); i++) {
EEPROM.write(i + 96, STA_HOSTNAME.charAt(i));
}
EEPROM.write(128, STA_TIMEOUT);
EEPROM.commit();
return true;
}
/*
HELPER FUNCTIONS
*/
void setLEDStatus(bool state) {
digitalWrite(PIN_STATUS, state);
}
void blinkLEDStatusSingle(unsigned int duration) {
setLEDStatus(1);
delay(duration);
setLEDStatus(0);
}
void blinkLEDStatusLoop(unsigned int duration) {
blinkLEDStatusSingle(duration);
delay(duration);
}
void setLEDActivity(bool state) {
#if HW_GROUP == 1
digitalWrite(PIN_STATUS, state);
#else
digitalWrite(PIN_ACTIVITY, state);
#endif
}
void blinkLEDActivitySingle(unsigned int duration) {
setLEDActivity(1);
delay(duration);
setLEDActivity(0);
}
void blinkLEDActivityLoop(unsigned int duration) {
blinkLEDActivitySingle(duration);
delay(duration);
}
void handleConfigButton() {
// Read the config button state to determine if it has been pressed or released
btnState = !digitalRead(PIN_CONFIG);
// Calculate the last press duration
if (btnState) {
btnTimer = millis();
btnDur = 0;
} else {
btnDur = millis() - btnTimer;
btnTimer = 0;
// Discard presses <= 50ms
if (btnDur > 50) {
btnPressed = 1;
} else {
btnDur = 0;
}
}
}
/*
WEB SERVER
*/
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
String formatPageBaseWithExtraHead(String content, String extraHead) {
String page;
page += "<html>";
page += "<head>";
page += "<link rel='shortcut icon' href='/favicon.ico'>";
page += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
page += "<meta charset='UTF-8'>";
page += "<link rel='stylesheet' href='/main.css'>";
page += "<title>WiFi Module</title>";
page += extraHead;
page += "</head>";
page += "<body>";
page += "<img id='logo' src='/xatlabs_logo.png'>";
page += content;
page += "</body>";
page += "</html>";
return page;
}
String formatPageBase(String content) {
return formatPageBaseWithExtraHead(content, "");
}
void handleRoot() {
String c;
c += "<h1>WiFi Module</h1>";
c += "<h2>WiFi Setup</h2>";
c += "<div>Current network: <b>";
if (AP_ACTIVE) {
c += AP_SSID;
} else if (STA_SETUP) {
c += STA_SSID;
} else {
c += "None";
}
c += "</b></div>";
c += "<form action='/wifi-setup' method='post'>";
c += "<table>";
c += "<tr><td>Network Name</td><td><input type='text' name='ssid' maxlength='32' /></td></tr>";
c += "<tr><td>Password</td><td><input type='password' name='password' maxlength='64' /></td></tr>";
c += "<tr><td>Timeout</td><td><input type='text' name='timeout' maxlength='3' value='";
c += String(STA_TIMEOUT);
c += "' /> sec (5 to 254)</td></tr>";
c += "<tr><td><input type='submit' value='Save and Reboot' /></td></tr>";
c += "</table>";
c += "</form>";
c += "<h2>Hostname Setup</h2>";
c += "<div>Current Hostname: <b>";
c += STA_HOSTNAME;
c += "</b></div>";
c += "<div>Attention! Valid characters: A-Z, a-z, 0-9 and -</div>";
c += "<div>You can use this to access the WiFi Module at <b><a href='http://";
c += STA_HOSTNAME;
c += ".local/'>http://";
c += STA_HOSTNAME;
c += ".local/</a></b></div>";
c += "<div>Alternatively, you can use the IP address: <b><a href='http://";
c += IP_ADDRESS;
c += "/'>";
c += IP_ADDRESS;
c += "</a></b></div>";
c += "<form action='/hostname-setup' method='post'>";
c += "<table>";
c += "<tr><td>Hostname</td><td><input type='text' name='hostname' maxlength='32' /></td></tr>";
c += "<tr><td><input type='submit' value='Save and Reboot' /></td></tr>";
c += "</table>";
c += "</form>";
c += "<a href='/check-update'>Check for firmware update</a>";
server.send(200, "text/html", formatPageBase(c));
}
void handle_wifi_setup() {
STA_SSID = server.arg("ssid");
STA_PASS = server.arg("password");
String timeoutStr = server.arg("timeout");
unsigned int timeout = atoi(timeoutStr.c_str());
STA_TIMEOUT = (timeout >= 5 && timeout <= 254) ? timeout : STA_TIMEOUT;
STA_SETUP = true;
saveConfig();
server.sendHeader("Location", "/", true);
server.send(303, "text/plain", "");
ESP.restart();
}
void handle_hostname_setup() {
STA_HOSTNAME = server.arg("hostname");
STA_HOSTNAME.toCharArray(STA_HOSTNAME_CHAR, 32);
saveConfig();
server.sendHeader("Location", "/", true);
server.send(303, "text/plain", "");
ESP.restart();
}
void handle_check_update() {
String c;
UpdateStatus fwStatusSecure = checkForUpdateSecure(false);
UpdateStatus spStatusSecure = checkForUpdateSecure(true);
UpdateStatus fwStatusInsecure = checkForUpdateInsecure(false);
UpdateStatus spStatusInsecure = checkForUpdateInsecure(true);
c += "<h1>Update Status</h1>";
c += "<table><tr><td>Firmware version:</td>";
c += "<td>";
c += "WS-";
c += HW_GROUP;
c += "-";
c += FW_VERSION;
c += "</td>";
/*if (fwStatusSecure == US_AVAILABLE) {
c += "<td>Update available (HTTPS)</td>";
c += "<td><form action='/update-fw-https' method='post'><input type='submit' value='Update (HTTPS)' /></form></td>";
} else if (fwStatusSecure == US_FAILED) {
c += "<td>Update check failed (HTTPS)</td>";
} else if (fwStatusSecure == US_NO_UPDATE) {
c += "<td>No update available (HTTPS)</td>";
}*/
if (fwStatusInsecure == US_AVAILABLE) {
c += "<td>Update available (HTTP)</td>";
c += "<td><form action='/update-fw-http' method='post'><input type='submit' value='Update (HTTP)' /></form></td>";
} else if (fwStatusInsecure == US_FAILED) {
c += "<td>Update check failed (HTTP)</td>";
} else if (fwStatusInsecure == US_NO_UPDATE) {
c += "<td>No update available (HTTP)</td>";
}
c += "</tr>";
c += "<tr><td>Filesystem version:</td>";
c += "<td>";
c += "WSF-";
c += HW_GROUP;
c += "-";
c += SP_VERSION;
c += "</td>";
/*if (spStatusSecure == US_AVAILABLE) {
c += "<td>Update available (HTTPS)</td>";
c += "<td><form action='/update-sp-https' method='post'><input type='submit' value='Update (HTTPS)' /></form></td>";
} else if (spStatusSecure == US_FAILED) {
c += "<td>Update check failed (HTTPS)</td>";
} else if (spStatusSecure == US_NO_UPDATE) {
c += "<td>No update available (HTTPS)</td>";
}*/
if (spStatusInsecure == US_AVAILABLE) {
c += "<td>Update available (HTTP)</td>";
c += "<td><form action='/update-sp-http' method='post'><input type='submit' value='Update (HTTP)' /></form></td>";
} else if (spStatusInsecure == US_FAILED) {
c += "<td>Update check failed (HTTP)</td>";
} else if (spStatusInsecure == US_NO_UPDATE) {
c += "<td>No update available (HTTP)</td>";
}
c += "</tr></table>";
server.send(200, "text/html", formatPageBase(c));
}
void handle_update_fw_https() {
updateFWSecure_flag = true;
updateFlagSetTimestamp = millis();
server.sendHeader("Location", "/update-running", true);
server.send(303, "text/plain", "");
}
void handle_update_sp_https() {
updateSPSecure_flag = true;
updateFlagSetTimestamp = millis();
server.sendHeader("Location", "/update-running", true);
server.send(303, "text/plain", "");
}
void handle_update_fw_http() {
updateFWInsecure_flag = true;
updateFlagSetTimestamp = millis();
server.sendHeader("Location", "/update-running", true);
server.send(303, "text/plain", "");
}
void handle_update_sp_http() {
updateSPInsecure_flag = true;
updateFlagSetTimestamp = millis();
server.sendHeader("Location", "/update-running", true);
server.send(303, "text/plain", "");
}
void handle_update_running() {
String c;
c += "<h1>Update in progress...</h1>";
c += "<p>Please wait while the update is being downloaded and installed.</p>";
server.send(200, "text/html", formatPageBaseWithExtraHead(c, "<meta http-equiv='refresh' content='5'>"));
}
/*
INTERRUPT ROUTINES
*/
void ISR_config() {
handleConfigButton();
}
/*
PROGRAM ROUTINES
*/
void doWiFiConfigViaWPS() {
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.beginWPSConfig();
while (WiFi.status() != WL_CONNECTED) {
blinkLEDStatusLoop(250);
}
STA_SSID = WiFi.SSID();
STA_PASS = WiFi.psk();
STA_SETUP = true;
saveConfig();
for (int i = 0; i < 3; i++) {
blinkLEDStatusLoop(125);
}
}
void doWiFiConfigViaAP() {
IPAddress apIP(192, 168, 4, 1);
IPAddress apSubnet(255, 255, 255, 0);
IP_ADDRESS = apIP.toString();
WiFi.disconnect();
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, apSubnet);
WiFi.softAP(AP_SSID.c_str(), AP_PASS.c_str());
AP_ACTIVE = true;
setLEDStatus(1);
}
void printIPAddress() {
// TODO: Stops working after the first time
IBIS_DS009(IP_ADDRESS);
IBIS_DS003c(IP_ADDRESS);
IBIS_GSP(1, "WLAN-Modul", IP_ADDRESS);
}
void resetWiFiSettings() {
STA_SSID = "";
STA_PASS = "";
STA_HOSTNAME = DEFAULT_HOSTNAME;
STA_TIMEOUT = DEFAULT_STA_TIMEOUT;
STA_SETUP = false;
saveConfig();
ESP.restart();
}
/*
FIRMWARE & SPIFFS UPDATE
*/
UpdateStatus checkForUpdateSecure(bool spiffs) {
String url = UPDATE_PATH_BASE_HTTPS + HW_GROUP;
if (spiffs) {
url += "/spiffs.version";
} else {
url += "/firmware.version";
}
WiFiClientSecure httpsClient;
if (!httpsClient.connect(UPDATE_HOST, UPDATE_PORT_HTTPS)) return US_FAILED;
httpsClient.println("GET " + url + " HTTP/1.0");
httpsClient.print("Host: ");
httpsClient.println(UPDATE_HOST);
httpsClient.println("Connection: close");
httpsClient.println();
while (httpsClient.connected()) {
String line = httpsClient.readStringUntil('\n');
if (line == "\r") {
// Headers received
break;
}
}
String newVersionStr = httpsClient.readStringUntil('\n');
unsigned long newVersion = newVersionStr.toInt();
if (spiffs) {
if (newVersion > SP_VERSION ) {
return US_AVAILABLE;
}
} else {
if (newVersion > FW_VERSION ) {
return US_AVAILABLE;
}
}
return US_NO_UPDATE;
}
UpdateStatus checkForUpdateInsecure(bool spiffs) {
String url = UPDATE_URL_BASE_HTTP + HW_GROUP;
if (spiffs) {
url += "/spiffs.version";
} else {
url += "/firmware.version";
}
HTTPClient httpClient;
httpClient.begin(url);
int httpCode = httpClient.GET();
if (httpCode == 200) {
String newVersionStr = httpClient.getString();
unsigned long newVersion = newVersionStr.toInt();
if (spiffs) {
if (newVersion > SP_VERSION ) {
return US_AVAILABLE;
}
} else {
if (newVersion > FW_VERSION ) {
return US_AVAILABLE;
}
}
} else {
return US_FAILED;
}
return US_NO_UPDATE;
}
UpdateStatus checkForUpdate(bool spiffs) {
// First check securely, fallback to insecure
UpdateStatus statusSecure = US_FAILED;//checkForUpdateSecure(spiffs);
if (statusSecure == US_FAILED) {
return checkForUpdateInsecure(spiffs);
} else {
return statusSecure;
}
}
UpdateStatus checkForFWUpdate() {
return checkForUpdate(false);
}
UpdateStatus checkForSPUpdate() {
return checkForUpdate(true);
}
t_httpUpdate_return doUpdateSecure(bool spiffs) {
// Set both LEDs on during update
setLEDStatus(1);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
t_httpUpdate_return ret;
String url = UPDATE_URL_BASE_HTTPS + HW_GROUP;
if (spiffs) {
url += "/spiffs.bin";
ret = ESPhttpUpdate.updateSpiffs(url, "", UPDATE_FINGERPRINT_HTTPS);
} else {
url += "/firmware.bin";
ret = ESPhttpUpdate.update(url, "", UPDATE_FINGERPRINT_HTTPS);
}
if (ret == HTTP_UPDATE_OK) {
ESP.restart();
}
return ret;
}
t_httpUpdate_return doUpdateInsecure(bool spiffs) {
// Set both LEDs on during update
setLEDStatus(1);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
t_httpUpdate_return ret;
String url = UPDATE_URL_BASE_HTTP + HW_GROUP;
if (spiffs) {
url += "/spiffs.bin";
ret = ESPhttpUpdate.updateSpiffs(url, "");
} else {
url += "/firmware.bin";
ret = ESPhttpUpdate.update(url, "");
}
if (ret == HTTP_UPDATE_OK) {
ESP.restart();
}
return ret;
}
void doUpdate(bool spiffs) {
// Set both LEDs on during update
setLEDStatus(1);
setLEDActivity(1);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
t_httpUpdate_return ret;
// Try secure update first
ret = HTTP_UPDATE_FAILED;//doUpdateSecure(spiffs);
if (ret == HTTP_UPDATE_FAILED) {
// Failover to insecure update
ret = doUpdateInsecure(spiffs);
if (ret == HTTP_UPDATE_FAILED) {
setLEDStatus(0);
digitalWrite(2, LOW);
pinMode(2, INPUT);
for (int i = 0; i < 3; i++) {
blinkLEDStatusLoop(750);
}
}
}
}
void doFWUpdate() {
doUpdate(false);
}
void doSPUpdate() {
doUpdate(true);
}
/*
MAIN PROGRAM
*/
void setup() {
WiFi.mode(WIFI_AP);
pinMode(PIN_STATUS, OUTPUT);
#if HW_GROUP != 1
pinMode(PIN_ACTIVITY, OUTPUT);
#endif
pinMode(PIN_CONFIG, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(PIN_CONFIG), ISR_config, CHANGE);
EEPROM.begin(EEPROM_SIZE); // 32 for SSID, 64 for PSK, 32 for hostname, 1 for timeout
// Reset hostname area of EEPROM if required
if (EEPROM.read(96) == 0xff) {
reset_EEPROM(96, 128);
}
// Reset timeout byte of EEPROM if required
if (EEPROM.read(128) == 0xff) {
EEPROM.write(128, STA_TIMEOUT);
EEPROM.commit();
}
#ifdef INIT_EEPROM
reset_EEPROM();
#endif
SPIFFS.begin();
loadConfig();
#ifdef ARDUINO_OTA_ENABLED
ArduinoOTA.setHostname(STA_HOSTNAME);
ArduinoOTA.begin();
#endif
if (STA_SETUP) {
// WiFi connection has been set up
WiFi.mode(WIFI_STA);
WiFi.begin(STA_SSID.c_str(), STA_PASS.c_str());
wifiTimer = millis();
while (WiFi.status() != WL_CONNECTED) {
if ((millis() - wifiTimer) > STA_TIMEOUT * 1000) {
wifiTimedOut = true;
break;
}
blinkLEDStatusLoop(250);
}
if (wifiTimedOut) {
// Connection timed out, fall back to AP mode
doWiFiConfigViaAP();
} else {
// Connected
WiFi.hostname(STA_HOSTNAME);
IP_ADDRESS = WiFi.localIP().toString();
}
} else {
// No WiFi connection has yet been set up, enter AP mode
doWiFiConfigViaAP();
}
// Set up time
configTime(1 * 3600, 0, "pool.ntp.org");
// Set up mDNS responder
MDNS.begin(STA_HOSTNAME_CHAR);
IBISServer.begin();
server.onNotFound(handleNotFound);
server.on("/", handleRoot);
server.on("/wifi-setup", handle_wifi_setup);
server.on("/hostname-setup", handle_hostname_setup);
server.on("/check-update", handle_check_update);
server.on("/update-fw-https", handle_update_fw_https);
server.on("/update-sp-https", handle_update_sp_https);
server.on("/update-fw-http", handle_update_fw_http);
server.on("/update-sp-http", handle_update_sp_http);
server.on("/update-running", handle_update_running);
server.serveStatic("/main.css", SPIFFS, "/main.css");
server.serveStatic("/favicon.ico", SPIFFS, "/favicon.ico");
server.serveStatic("/xatlabs_logo.png", SPIFFS, "/xatlabs_logo.png");
server.begin();
#ifdef SERIAL_DEBUG
Serial.begin(115200);
Serial.setDebugOutput(1);
#else
IBIS_init();
#endif
for (int i = 0; i < 3; i++) {
blinkLEDStatusLoop(60);
blinkLEDActivityLoop(60);
}
if (AP_ACTIVE) {
setLEDStatus(1);
} else {
if (checkForFWUpdate() == US_AVAILABLE) {
doFWUpdate();
}
if (checkForSPUpdate() == US_AVAILABLE) {
doSPUpdate();
}
}
}
void loop() {
#ifdef ARDUINO_OTA_ENABLED
ArduinoOTA.handle();
#endif
server.handleClient();
btnState = !digitalRead(PIN_CONFIG);
if (btnState != oldBtnState) {
oldBtnState = btnState;
handleConfigButton();
}
WiFiClient newClient = IBISServer.available();
if (newClient) client = newClient;
if (client) {
while (client.available()) {
setLEDActivity(1);
Serial.write(client.read());
setLEDActivity(0);
}
while (Serial.available()) {
setLEDActivity(1);
client.write(Serial.read());
setLEDActivity(0);
}
}
// Handle LED blinking while button is being pressed (visual timing help)
if (btnState) {
unsigned long dur = (millis() - btnTimer) % 1000;
setLEDStatus(dur > 500);
}
// Update a certain time after flag is set
// (to give the web server time to send the redirect after initiating the update)
if (updateFWSecure_flag) {
if (millis() - updateFlagSetTimestamp >= UPDATE_START_DELAY) {
updateFWSecure_flag = false;
updateFlagSetTimestamp = 0;
if (checkForUpdateSecure(false) == US_AVAILABLE) {
doUpdateSecure(false);
}
}
}
if (updateSPSecure_flag) {
if (millis() - updateFlagSetTimestamp >= UPDATE_START_DELAY) {
updateSPSecure_flag = false;
updateFlagSetTimestamp = 0;
if (checkForUpdateSecure(true) == US_AVAILABLE) {
doUpdateSecure(true);
}
}
}
if (updateFWInsecure_flag) {
if (millis() - updateFlagSetTimestamp >= UPDATE_START_DELAY) {
updateFWInsecure_flag = false;
updateFlagSetTimestamp = 0;
if (checkForUpdateInsecure(false) == US_AVAILABLE) {
doUpdateInsecure(false);
}
}
}
if (updateSPInsecure_flag) {
if (millis() - updateFlagSetTimestamp >= UPDATE_START_DELAY) {
updateSPInsecure_flag = false;
updateFlagSetTimestamp = 0;
if (checkForUpdateInsecure(true) == US_AVAILABLE) {
doUpdateInsecure(true);
}
}
}
// Check if the button has been pressed and for how long
if (btnPressed) {
btnPressed = 0;
setLEDStatus(0);
int selectedOption = btnDur / 1000;
switch (selectedOption) {
case 0: {
// Short press
break;
}
case 1: {
// WPS mode
doWiFiConfigViaWPS();
break;
}
case 2: {
// AP mode
doWiFiConfigViaAP();
break;
}
case 3: {
// AP mode
printIPAddress();
break;
}
case 10: {
// Reset WiFi Credentials
resetWiFiSettings();
break;
}
default: {
break;
}
}
}
}