-
Notifications
You must be signed in to change notification settings - Fork 216
/
utils.cpp
1123 lines (1013 loc) · 39.4 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
// General utilities not specific to this app to support:
// - wifi
// - NTP
// - remote logging
// - base64 encoding
// - device sleep
//
// s60sc 2021, 2023
// some functions based on code contributed by gemi254
#include "appGlobals.h"
bool dbgVerbose = false;
bool timeSynchronized = false;
bool monitorOpen = true;
bool dataFilesChecked = false;
// allow any startup failures to be reported via browser for remote devices
char startupFailure[SF_LEN] = {0};
size_t alertBufferSize = 0;
byte* alertBuffer = NULL; // buffer for telegram / smtp alert image
RTC_NOINIT_ATTR uint32_t crashLoop;
RTC_NOINIT_ATTR char brownoutStatus;
static void initBrownout(void);
int wakePin; // if wakeUse is true
bool wakeUse = false; // true to allow app to sleep and wake
/************************** Wifi **************************/
#include <esp_task_wdt.h>
/** Do not hard code anything below here unless you know what you are doing **/
/** Use the web interface to configure wifi settings **/
char hostName[MAX_HOST_LEN] = ""; // Default Host name
char ST_SSID[MAX_HOST_LEN] = ""; //Default router ssid
char ST_Pass[MAX_PWD_LEN] = ""; //Default router passd
// leave following blank for dhcp
char ST_ip[MAX_IP_LEN] = ""; // Static IP
char ST_sn[MAX_IP_LEN] = ""; // subnet normally 255.255.255.0
char ST_gw[MAX_IP_LEN] = ""; // gateway to internet, normally router IP
char ST_ns1[MAX_IP_LEN] = ""; // DNS Server, can be router IP (needed for SNTP)
char ST_ns2[MAX_IP_LEN] = ""; // alternative DNS Server, can be blank
// Access point Config Portal SSID and Pass
char AP_SSID[MAX_HOST_LEN] = "";
char AP_Pass[MAX_PWD_LEN] = "";
char AP_ip[MAX_IP_LEN] = ""; // Leave blank to use 192.168.4.1
char AP_sn[MAX_IP_LEN] = "";
char AP_gw[MAX_IP_LEN] = "";
// basic HTTP Authentication access to web page
char Auth_Name[MAX_HOST_LEN] = "";
char Auth_Pass[MAX_PWD_LEN] = "";
int responseTimeoutSecs = 10; // time to wait for FTP or SMTP response
bool allowAP = true; // set to true to allow AP to startup if cannot connect to STA (router)
uint32_t wifiTimeoutSecs = 30; // how often to check wifi status
static bool APstarted = false;
esp_ping_handle_t pingHandle = NULL;
bool usePing = true;
static void startPing();
static void setupMdnsHost() {
// set up MDNS service
char mdnsName[MAX_IP_LEN]; // max mdns host name length
snprintf(mdnsName, MAX_IP_LEN, "%.*s", MAX_IP_LEN - 1, hostName);
if (MDNS.begin(mdnsName)) {
// Add service to MDNS
MDNS.addService("http", "tcp", HTTP_PORT);
MDNS.addService("https", "tcp", HTTPS_PORT);
//MDNS.addService("ws", "udp", 83);
//MDNS.addService("ftp", "tcp", 21);
LOG_INF("mDNS service: http://%s.local", mdnsName);
} else LOG_WRN("mDNS host: %s Failed", mdnsName);
debugMemory("setupMdnsHost");
}
static const char* wifiStatusStr(wl_status_t wlStat) {
switch (wlStat) {
case WL_NO_SHIELD: return "wifi not initialised";
case WL_IDLE_STATUS: return "WL_IDLE_STATUS";
case WL_NO_SSID_AVAIL: return "not available, use AP";
case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";
case WL_CONNECTED: return "WL_CONNECTED";
case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";
case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST";
case WL_DISCONNECTED: return "unable to connect";
case WL_STOPPED: return "wifi stopped";
default: return "Invalid WiFi.status";
}
}
const char* getEncType(int ssidIndex) {
switch (WiFi.encryptionType(ssidIndex)) {
case (WIFI_AUTH_OPEN): return "Open";
case (WIFI_AUTH_WEP): return "WEP";
case (WIFI_AUTH_WPA_PSK): return "WPA_PSK";
case (WIFI_AUTH_WPA2_PSK): return "WPA2_PSK";
case (WIFI_AUTH_WPA_WPA2_PSK): return "WPA_WPA2_PSK";
case (WIFI_AUTH_WPA2_ENTERPRISE): return "WPA2_ENTERPRISE";
case (WIFI_AUTH_MAX): return "AUTH_MAX";
default: return "Not listed";
}
}
static void onWiFiEvent(WiFiEvent_t event) {
// callback to report on wifi events
switch (event) {
case ARDUINO_EVENT_WIFI_READY: break;
case ARDUINO_EVENT_WIFI_SCAN_DONE: break;
case ARDUINO_EVENT_WIFI_STA_START: LOG_INF("Wifi Station started, connecting to: %s", ST_SSID); break;
case ARDUINO_EVENT_WIFI_STA_STOP: LOG_INF("Wifi Station stopped %s", ST_SSID); break;
case ARDUINO_EVENT_WIFI_AP_START: {
if (strlen(AP_SSID) && !strcmp(WiFi.softAPSSID().c_str(), AP_SSID)) {
LOG_INF("Wifi AP SSID: %s started, use '%s://%s' to connect", WiFi.softAPSSID().c_str(), useHttps ? "https" : "http", WiFi.softAPIP().toString().c_str());
APstarted = true;
}
break;
}
case ARDUINO_EVENT_WIFI_AP_STOP: {
if (!strcmp(WiFi.softAPSSID().c_str(), AP_SSID)) {
LOG_INF("Wifi AP stopped: %s", AP_SSID);
APstarted = false;
}
break;
}
case ARDUINO_EVENT_WIFI_STA_GOT_IP: LOG_INF("Wifi Station IP, use '%s://%s' to connect", useHttps ? "https" : "http", WiFi.localIP().toString().c_str()); break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP: LOG_INF("Wifi Station lost IP"); break;
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED: LOG_INF("WiFi Station connection to %s, using hostname: %s", ST_SSID, hostName); break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: LOG_INF("WiFi Station disconnected"); break;
case ARDUINO_EVENT_WIFI_AP_STACONNECTED: LOG_INF("WiFi AP client connection"); break;
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: LOG_INF("WiFi AP client disconnection"); break;
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: break;
case ARDUINO_EVENT_WIFI_AP_GOT_IP6: LOG_INF("AP interface V6 IP addr is preferred"); break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP6: LOG_INF("Station interface V6 IP addr is preferred"); break;
default: LOG_WRN("WiFi Unhandled event %d", event); break;
}
}
static void setWifiAP() {
if (!APstarted) {
// Set access point with static ip if provided
if (strlen(AP_ip) > 1) {
LOG_INF("Set AP static IP :%s, %s, %s", AP_ip, AP_gw, AP_sn);
IPAddress _ip, _gw, _sn, _ns1 ,_ns2;
_ip.fromString(AP_ip);
_gw.fromString(AP_gw);
_sn.fromString(AP_sn);
// set static ip
WiFi.softAPConfig(_ip, _gw, _sn);
}
WiFi.softAP(AP_SSID, AP_Pass);
debugMemory("setWifiAP");
}
}
static void setWifiSTA() {
// set station with static ip if provided
if (strlen(ST_ip) > 1) {
IPAddress _ip, _gw, _sn, _ns1, _ns2;
if (!_ip.fromString(ST_ip)) LOG_WRN("Failed to parse IP: %s", ST_ip);
else {
_ip.fromString(ST_ip);
_gw.fromString(ST_gw);
_sn.fromString(ST_sn);
_ns1.fromString(ST_ns1);
_ns2.fromString(ST_ns2);
// set static ip
WiFi.config(_ip, _gw, _sn, _ns1); // need DNS for SNTP
LOG_INF("Wifi Station set static IP");
}
} else LOG_INF("Wifi Station IP from DHCP");
WiFi.enableIPv6(USE_IP6);
WiFi.begin(ST_SSID, ST_Pass);
debugMemory("setWifiSTA");
}
bool startWifi(bool firstcall) {
// start wifi station (and wifi AP if allowed or station not defined)
if (firstcall) {
WiFi.mode(WIFI_AP_STA);
WiFi.persistent(false); // prevent the flash storage WiFi credentials
WiFi.setAutoReconnect(false); // Set whether module will attempt to reconnect to an access point in case it is disconnected
WiFi.softAPdisconnect(true); // kill rogue AP on startup
WiFi.setHostname(hostName);
delay(100);
WiFi.onEvent(onWiFiEvent);
}
setWifiSTA();
// connect to Wifi station
uint32_t startAttemptTime = millis();
// Stop trying on failure timeout, will try to reconnect later by ping
wl_status_t wlStat = WL_NO_SSID_AVAIL;
if (strlen(ST_SSID)) {
while (wlStat = WiFi.status(), wlStat != WL_CONNECTED && millis() - startAttemptTime < 5000) {
logPrint(".");
delay(500);
}
}
if (wlStat == WL_NO_SSID_AVAIL || allowAP) setWifiAP(); // AP allowed if no Station SSID eg on first time use
if (wlStat != WL_CONNECTED) LOG_WRN("SSID %s not connected %s", ST_SSID, wifiStatusStr(wlStat));
#if CONFIG_IDF_TARGET_ESP32S3
setupMdnsHost(); // not on ESP32 as uses 6k of heap
#endif
// show stats of requested SSID
int numNetworks = WiFi.scanNetworks();
for (int i=0; i < numNetworks; i++) {
if (!strcmp(WiFi.SSID(i).c_str(), ST_SSID))
LOG_INF("Wifi stats for %s - signal strength: %d dBm; Encryption: %s; channel: %u", ST_SSID, WiFi.RSSI(i), getEncType(i), WiFi.channel(i));
}
if (pingHandle == NULL) startPing();
return wlStat == WL_CONNECTED ? true : false;
}
void resetWatchDog() {
// use ping task as watchdog in case of freeze
static bool watchDogStarted = false;
if (watchDogStarted) esp_task_wdt_reset();
else {
esp_task_wdt_deinit();
esp_task_wdt_config_t twdt_config = {
.timeout_ms = (wifiTimeoutSecs * 1000 * 2),
.idle_core_mask = (1 << portNUM_PROCESSORS) - 1,
.trigger_panic = true, // panic abort on watchdog alert (contains wdt_isr)
};
esp_task_wdt_init(&twdt_config);
esp_task_wdt_add(NULL);
if (esp_task_wdt_status(NULL) == ESP_OK) {
watchDogStarted = true;
esp_task_wdt_reset();
LOG_INF("WatchDog started using task: %s", pcTaskGetName(NULL));
} else LOG_ERR("WatchDog failed to start");
}
}
static void statusCheck() {
// regular status checks
doAppPing();
if (!timeSynchronized) getLocalNTP();
if (!dataFilesChecked) dataFilesChecked = checkDataFiles();
#if INCLUDE_MQTT
if (mqtt_active) startMqttClient();
#endif
}
void resetCrashLoop() {
crashLoop = 0;
}
static void pingSuccess(esp_ping_handle_t hdl, void *args) {
//uint32_t elapsed_time;
//esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
if (DEBUG_MEM) {
static uint32_t minStack = UINT32_MAX;
uint32_t freeStack = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
if (freeStack < minStack) {
minStack = freeStack;
if (freeStack < MIN_STACK_FREE) LOG_WRN("Task ping stack space only: %u", freeStack);
else LOG_INF("Task ping stack space reduced to: %u", freeStack);
}
}
resetWatchDog();
if (dataFilesChecked) resetCrashLoop();
statusCheck();
}
static void pingTimeout(esp_ping_handle_t hdl, void *args) {
// a ping check is used because esp may maintain a connection to gateway which may be unuseable, which is detected by ping failure
// but some routers may not respond to ping - https://github.com/s60sc/ESP32-CAM_MJPEG2SD/issues/221
// so setting usePing to false ignores ping failure if connection still present
resetWatchDog();
if (strlen(ST_SSID)) {
wl_status_t wStat = WiFi.status();
if (wStat != WL_NO_SSID_AVAIL && wStat != WL_NO_SHIELD) {
if (usePing) {
LOG_WRN("Failed to ping gateway, restart wifi ...");
startWifi(false);
} else {
if (wStat == WL_CONNECTED) statusCheck(); // treat as ok
else {
LOG_WRN("Disconnected, restart wifi ...");
startWifi(false);
}
}
}
}
}
static void startPing() {
IPAddress ipAddr = WiFi.gatewayIP();
ip_addr_t pingDest;
IP_ADDR4(&pingDest, ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
esp_ping_config_t pingConfig = ESP_PING_DEFAULT_CONFIG();
pingConfig.target_addr = pingDest;
pingConfig.count = ESP_PING_COUNT_INFINITE;
pingConfig.interval_ms = wifiTimeoutSecs * 1000;
pingConfig.timeout_ms = 5000;
pingConfig.task_stack_size = PING_STACK_SIZE;
pingConfig.task_prio = 1;
// set ping task callback functions
esp_ping_callbacks_t cbs;
cbs.on_ping_success = pingSuccess;
cbs.on_ping_timeout = pingTimeout;
cbs.on_ping_end = NULL;
cbs.cb_args = NULL;
esp_ping_new_session(&pingConfig, &cbs, &pingHandle);
esp_ping_start(pingHandle);
LOG_INF("Started ping monitoring - %s", usePing ? "On" : "Off");
debugMemory("startPing");
}
void stopPing() {
if (pingHandle != NULL) {
esp_ping_stop(pingHandle);
esp_ping_delete_session(pingHandle);
pingHandle = NULL;
}
}
#define EXT_IP_HOST "api.ipify.org"
char extIP[MAX_IP_LEN] = "Not assigned"; // router external IP
bool doGetExtIP = true;
void getExtIP() {
// Get external IP address
if (doGetExtIP) {
NetworkClientSecure hclient;
if (remoteServerConnect(hclient, EXT_IP_HOST, HTTPS_PORT, "", GETEXTIP)) {
HTTPClient https;
int httpCode = HTTP_CODE_NOT_FOUND;
if (https.begin(hclient, EXT_IP_HOST, HTTPS_PORT, "/", true)) {
char newExtIp[MAX_IP_LEN] = "";
httpCode = https.GET();
if (httpCode == HTTP_CODE_OK) {
strncpy(newExtIp, https.getString().c_str(), sizeof(newExtIp) - 1);
if (strcmp(newExtIp, extIP)) {
// external IP changed
strncpy(extIP, newExtIp, sizeof(extIP) - 1);
updateStatus("extIP", extIP);
updateStatus("save", "0");
externalAlert("External IP changed", extIP);
} else LOG_INF("External IP: %s", extIP);
} else LOG_WRN("External IP request failed, error: %s", https.errorToString(httpCode).c_str());
if (httpCode != HTTP_CODE_OK) doGetExtIP = false;
https.end();
}
remoteServerClose(hclient);
}
}
}
/************** generic NetworkClientSecure functions ******************/
static uint8_t failCounts[REMFAILCNT] = {0};
void remoteServerClose(NetworkClientSecure& sclient) {
if (sclient.available()) sclient.clear();
if (sclient.connected()) sclient.stop();
}
bool remoteServerConnect(NetworkClientSecure& sclient, const char* serverName, uint16_t serverPort, const char* serverCert, uint8_t connIdx) {
// Connect to server if not already connected or previously disconnected
if (sclient.connected()) return true;
else {
if (failCounts[connIdx] >= MAX_FAIL) {
if (failCounts[connIdx] == MAX_FAIL) {
LOG_ERR("Abandon %s connection attempt until next rollover", serverName);
failCounts[connIdx] = MAX_FAIL + 1;
}
} else {
if (ESP.getFreeHeap() > TLS_HEAP) {
// not connected, so try for a period of time
if (useSecure && strlen(serverCert)) sclient.setCACert(serverCert);
else sclient.setInsecure(); // no cert check
uint32_t startTime = millis();
while (!sclient.connected()) {
if (sclient.connect(serverName, serverPort)) break;
if (millis() - startTime > responseTimeoutSecs * 1000) break;
delay(2000);
}
if (sclient.connected()) {
failCounts[connIdx] = 0;
return true;
}
else {
// failed to connect in allocated time
// 'Memory allocation failed' indicates lack of heap space
// 'Generic error' can indicate DNS failure
char errBuf[100] = "Unknown server error";
int errNum = sclient.lastError(errBuf, sizeof(errBuf));
LOG_WRN("Timed out connecting to server: %s, Err: %d, %s", serverName, errNum, errBuf);
}
} else LOG_WRN("Insufficient heap %s for %s TLS session", fmtSize(ESP.getFreeHeap()), serverName);
failCounts[connIdx]++;
}
}
return false;
}
void remoteServerReset() {
// reset fail counts
for (uint8_t i = 0; i < REMFAILCNT; i++) failCounts[i] = 0;
}
/************************** NTP **************************/
// Needs to be a time zone string from: https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv
char timezone[FILE_NAME_LEN] = "GMT0";
char ntpServer[MAX_HOST_LEN] = "pool.ntp.org";
uint8_t alarmHour = 1;
time_t getEpoch() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec;
}
void dateFormat(char* inBuff, size_t inBuffLen, bool isFolder) {
// construct filename from date/time
time_t currEpoch = getEpoch();
if (isFolder) strftime(inBuff, inBuffLen, "/%Y%m%d", localtime(&currEpoch));
else strftime(inBuff, inBuffLen, "/%Y%m%d/%Y%m%d_%H%M%S", localtime(&currEpoch));
}
static void showLocalTime(const char* timeSrc) {
time_t currEpoch = getEpoch();
char timeFormat[20];
strftime(timeFormat, sizeof(timeFormat), "%d/%m/%Y %H:%M:%S", localtime(&currEpoch));
LOG_INF("Got current time from %s: %s with tz: %s", timeSrc, timeFormat, timezone);
timeSynchronized = true;
}
bool getLocalNTP() {
// get current time from NTP server and apply to ESP32
LOG_INF("Using NTP server: %s", ntpServer);
configTzTime(timezone, ntpServer);
if (getEpoch() > 10000) {
showLocalTime("NTP");
return true;
}
else {
LOG_WRN("Not yet synced with NTP");
return false;
}
}
void syncToBrowser(uint32_t browserUTC) {
// Synchronize to browser clock if out of sync
if (!timeSynchronized) {
struct timeval tv;
tv.tv_sec = browserUTC;
settimeofday(&tv, NULL);
setenv("TZ", timezone, 1);
tzset();
showLocalTime("browser");
}
}
void formatElapsedTime(char* timeStr, uint32_t timeVal, bool noDays) {
// elapsed time that app has been running
uint32_t secs = timeVal / 1000; //convert milliseconds to seconds
uint32_t mins = secs / 60; //convert seconds to minutes
uint32_t hours = mins / 60; //convert minutes to hours
uint32_t days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the converted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the converted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the converted hours to days in order to display 23 hours max
if (noDays) sprintf(timeStr, "%02lu:%02lu:%02lu", hours, mins, secs);
else sprintf(timeStr, "%lu-%02lu:%02lu:%02lu", days, hours, mins, secs);
}
static time_t setAlarm(uint8_t alarmHour) {
// calculate future alarm datetime based on current datetime
// ensure relevant timezone identified (default GMT0)
time_t currEpoch = getEpoch();
struct tm* timeinfo = localtime(&currEpoch);
// set alarm date & time for next given hour
int nextDay = 0; // try same day then next day
do {
timeinfo->tm_mday += nextDay;
timeinfo->tm_hour = alarmHour;
timeinfo->tm_min = 0;
timeinfo->tm_sec = 0;
nextDay = 1;
} while (mktime(timeinfo) < getEpoch());
char inBuff[30];
strftime(inBuff, sizeof(inBuff), "%d/%m/%Y %H:%M:%S", timeinfo);
LOG_INF("Alarm scheduled at %s", inBuff);
// return future alarm time as epoch seconds
return mktime(timeinfo);
}
bool checkAlarm() {
// call from appPing() to check if daily alarm time at given hour has occurred
static time_t rolloverEpoch = 0;
if (timeSynchronized && getEpoch() >= rolloverEpoch) {
// alarm time reached
rolloverEpoch = setAlarm(alarmHour); // set next alarm time
return true;
}
return false;
}
/********************** misc functions ************************/
bool changeExtension(char* fileName, const char* newExt) {
// replace original file extension with supplied extension (buffer must be large enough)
size_t inNamePtr = strlen(fileName);
// find '.' before extension text
while (inNamePtr > 0 && fileName[inNamePtr] != '.') inNamePtr--;
inNamePtr++;
size_t extLen = strlen(newExt);
memcpy(fileName + inNamePtr, newExt, extLen);
fileName[inNamePtr + extLen] = 0;
return (inNamePtr > 1) ? true : false;
}
void showProgress(const char* marker) {
// show progess as dots
static uint8_t dotCnt = 0;
logPrint(marker); // progress marker
if (++dotCnt >= DOT_MAX) {
dotCnt = 0;
logLine();
}
}
bool calcProgress(int progressVal, int totalVal, int percentReport, uint8_t &pcProgress) {
// calculate percentage progress, only report back on percentReport boundary
uint8_t percentage = (progressVal * 100) / totalVal;
if (percentage >= pcProgress + percentReport) {
pcProgress = percentage;
return true;
} else return false;
}
bool urlEncode(const char* inVal, char* encoded, size_t maxSize) {
int encodedLen = 0;
char hexTable[] = "0123456789ABCDEF";
while (*inVal) {
if (isalnum(*inVal) || strchr("$-_.+!*'(),:@~#", *inVal)) *encoded++ = *inVal;
else {
encodedLen += 3;
if (encodedLen >= maxSize) return false; // Buffer overflow
*encoded++ = '%';
*encoded++ = hexTable[(*inVal) >> 4];
*encoded++ = hexTable[*inVal & 0xf];
}
inVal++;
}
*encoded = 0;
return true;
}
void urlDecode(char* inVal) {
// replace url encoded characters
std::string decodeVal(inVal);
std::string replaceVal = decodeVal;
std::smatch match;
while (regex_search(decodeVal, match, std::regex("(%)([0-9A-Fa-f]{2})"))) {
std::string s(1, static_cast<char>(std::strtoul(match.str(2).c_str(),nullptr,16))); // hex to ascii
replaceVal = std::regex_replace(replaceVal, std::regex(match.str(0)), s);
decodeVal = match.suffix().str();
}
strcpy(inVal, replaceVal.c_str());
}
void listBuff (const uint8_t* b, size_t len) {
// output buffer content as hex, 16 bytes per line
if (!len || !b) LOG_WRN("Nothing to print");
else {
for (size_t i = 0; i < len; i += 16) {
int linelen = (len - i) < 16 ? (len - i) : 16;
for (size_t k = 0; k < linelen; k++) logPrint(" %02x", b[i+k]);
puts(" ");
}
}
}
size_t isSubArray(uint8_t* haystack, uint8_t* needle, size_t hSize, size_t nSize) {
// find a subarray (needle) in another array (haystack)
size_t h = 0, n = 0; // Two pointers to traverse the arrays
// Traverse both arrays simultaneously
while (h < hSize && n < nSize) {
// If element matches, increment both pointers
if (haystack[h] == needle[n]) {
h++;
n++;
// If needle is completely traversed
if (n == nSize) return h; // position of end of needle
} else {
// if not, increment h and reset n
h = h - n + 1;
n = 0;
}
}
return 0; // not found
}
void removeChar(char* s, char c) {
// remove specified character from string
int writer = 0, reader = 0;
while (s[reader]) {
if (s[reader] != c) s[writer++] = s[reader];
reader++;
}
s[writer] = 0;
}
void replaceChar(char* s, char c, char r) {
// replace specified character in string
int reader = 0;
while (s[reader]) {
if (s[reader] == c) s[reader] = r;
reader++;
}
}
char* fmtSize (uint64_t sizeVal) {
// format size according to magnitude
// only one call per format string
static char returnStr[20];
if (sizeVal < 50 * 1024) sprintf(returnStr, "%llu bytes", sizeVal);
else if (sizeVal < ONEMEG) sprintf(returnStr, "%lluKB", sizeVal / 1024);
else if (sizeVal < ONEMEG * 1024) sprintf(returnStr, "%0.1fMB", (double)(sizeVal) / ONEMEG);
else sprintf(returnStr, "%0.1fGB", (double)(sizeVal) / (ONEMEG * 1024));
return returnStr;
}
void checkMemory(const char* source ) {
LOG_INF("%s Free: heap %u, block: %u, min: %u, pSRAM %u", source, ESP.getFreeHeap(), ESP.getMaxAllocHeap(), ESP.getMinFreeHeap(), ESP.getFreePsram());
if (ESP.getFreeHeap() < WARN_HEAP) LOG_WRN("Free heap only %u, min %u", ESP.getFreeHeap(), ESP.getMinFreeHeap());
if (ESP.getMaxAllocHeap() < WARN_ALLOC) LOG_WRN("Max allocatable heap block is only %u", ESP.getMaxAllocHeap());
}
uint32_t checkStackUse(TaskHandle_t thisTask, int taskIdx) {
// get minimum free stack size for task since started
static uint32_t minStack[20];
uint32_t freeStack = 0;
if (thisTask != NULL) {
freeStack = (uint32_t)uxTaskGetStackHighWaterMark(thisTask);
if (!minStack[taskIdx]) {
minStack[taskIdx] = freeStack; // initialise
LOG_INF("Task %s on core %d, initial stack space %u", pcTaskGetTaskName(thisTask), xPortGetCoreID(), freeStack);
}
if (freeStack < minStack[taskIdx]) {
minStack[taskIdx] = freeStack;
if (freeStack < MIN_STACK_FREE) LOG_WRN("Task %s on core %d, stack space only: %u", pcTaskGetTaskName(thisTask), xPortGetCoreID(), freeStack);
else LOG_INF("Task %s on core %d, stack space reduced to %u", pcTaskGetTaskName(thisTask), xPortGetCoreID(), freeStack);
}
}
return freeStack;
}
void debugMemory(const char* caller) {
if (DEBUG_MEM) {
logPrint("%s > Free: heap %u, block: %u, min: %u, pSRAM %u\n", caller, ESP.getFreeHeap(), ESP.getMaxAllocHeap(), ESP.getMinFreeHeap(), ESP.getFreePsram());
delay(FLUSH_DELAY);
}
}
void doRestart(const char* restartStr) {
LOG_ALT("Controlled restart: %s", restartStr);
#ifdef ISCAM
appShutdown();
#endif
#if INCLUDE_MQTT
if (mqtt_active) stopMqttClient();
#endif
resetCrashLoop();
flush_log(true);
delay(2000);
ESP.restart();
}
uint16_t smoothAnalog(int analogPin, int samples) {
// get averaged analog pin value
uint32_t level = 0;
if (analogPin > 0) {
for (int j = 0; j < samples; j++) level += analogRead(analogPin);
level /= samples;
}
return level;
}
void setupADC() {
analogSetAttenuation(ADC_ATTEN);
analogReadResolution(ADC_BITS);
}
float smoothSensor(float latestVal, float smoothedVal, float alpha) {
// simple Exponential Moving Average filter
// where alpha between 0.0 (max smooth) and 1.0 (no smooth)
return (latestVal * alpha) + smoothedVal * (1.0 - alpha);
}
// onboard chip temperature sensor
#if CONFIG_IDF_TARGET_ESP32
extern "C" {
// Use internal on chip temperature sensor (if present)
uint8_t temprature_sens_read(); // sic
}
#elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
#include "driver/temperature_sensor.h"
static temperature_sensor_handle_t temp_sensor = NULL;
#endif
static void prepInternalTemp() {
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
// setup internal sensor
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(20, 100);
temperature_sensor_install(&temp_sensor_config, &temp_sensor);
temperature_sensor_enable(temp_sensor);
#endif
}
float readInternalTemp() {
float intTemp = NULL_TEMP;
#if CONFIG_IDF_TARGET_ESP32
// convert on chip raw temperature in F to Celsius degrees
intTemp = (temprature_sens_read() - 32) / 1.8; // value of 55 means not present
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
temperature_sensor_get_celsius(temp_sensor, &intTemp);
#endif
return intTemp;
}
/*********************** Remote loggging ***********************/
/*
* Log mode selection in user interface:
* false : log to serial / web monitor only
* true : also saves log on SD card. To download the log generated, either:
* - To view the log, press Show Log button on the browser
* - To clear the log file contents, on log web page press Clear Log link
*/
#define MAX_OUT 200
static va_list arglist;
static char fmtBuf[MAX_OUT];
static char outBuf[MAX_OUT];
char alertMsg[MAX_OUT];
TaskHandle_t logHandle = NULL;
static SemaphoreHandle_t logSemaphore = NULL;
static SemaphoreHandle_t logMutex = NULL;
static int logWait = 100; // ms
bool useLogColors = false; // true to colorise log messages (eg if using idf.py, but not arduino)
bool wsLog = false;
#define WRITE_CACHE_CYCLE 5
bool sdLog = false; // log to SD
int logType = 0; // which log contents to display (0 : ram, 1 : sd, 2 : ws)
static FILE* log_remote_fp = NULL;
static uint32_t counter_write = 0;
// RAM memory based logging in RTC slow memory (cannot init)
RTC_NOINIT_ATTR char messageLog[RAM_LOG_LEN];
RTC_NOINIT_ATTR uint16_t mlogEnd;
static void ramLogClear() {
mlogEnd = 0;
memset(messageLog, 0, RAM_LOG_LEN);
}
static void ramLogStore(size_t msgLen) {
// save log entry in ram buffer
if (mlogEnd + msgLen >= RAM_LOG_LEN) {
// log needs to roll around cyclic buffer
uint16_t firstPart = RAM_LOG_LEN - mlogEnd;
memcpy(messageLog + mlogEnd, outBuf, firstPart);
msgLen -= firstPart;
memcpy(messageLog, outBuf + firstPart, msgLen);
mlogEnd = 0;
} else memcpy(messageLog + mlogEnd, outBuf, msgLen);
mlogEnd += msgLen;
}
void flush_log(bool andClose) {
if (log_remote_fp != NULL) {
fsync(fileno(log_remote_fp));
fflush(log_remote_fp);
if (andClose) {
LOG_INF("Closed SD file for logging");
fclose(log_remote_fp);
log_remote_fp = NULL;
} else delay(1000);
}
}
static void remote_log_init_SD() {
#if !CONFIG_IDF_TARGET_ESP32C3
STORAGE.mkdir(DATA_DIR);
// Open remote file
log_remote_fp = NULL;
log_remote_fp = fopen("/sdcard" LOG_FILE_PATH, "a");
if (log_remote_fp == NULL) {LOG_WRN("Failed to open SD log file %s", LOG_FILE_PATH);}
else {
logPrint(" \n");
LOG_INF("Opened SD file for logging");
}
#endif
}
void reset_log() {
if (logType == 0) ramLogClear();
if (logType == 2) {
if (log_remote_fp != NULL) flush_log(true); // Close log file
STORAGE.remove(LOG_FILE_PATH);
remote_log_init_SD();
}
if (logType != 1) LOG_INF("Cleared %s log file", logType == 0 ? "RAM" : "SD");
}
void remote_log_init() {
// setup required log mode
if (sdLog) {
flush_log(false);
remote_log_init_SD(); // store log on sd card
} else flush_log(true);
}
static void logTask(void *arg) {
// separate task to reduce stack size in other tasks
while(true) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
vsnprintf(outBuf, MAX_OUT, fmtBuf, arglist);
va_end(arglist);
xSemaphoreGive(logSemaphore);
}
}
void logPrint(const char *format, ...) {
// feeds logTask to format message, then outputs as required
if (xSemaphoreTake(logMutex, pdMS_TO_TICKS(logWait)) == pdTRUE) {
strncpy(fmtBuf, format, MAX_OUT);
va_start(arglist, format);
vTaskPrioritySet(logHandle, uxTaskPriorityGet(NULL) + 1);
xTaskNotifyGive(logHandle);
outBuf[MAX_OUT - 2] = '\n';
outBuf[MAX_OUT - 1] = 0; // ensure always have ending newline
xSemaphoreTake(logSemaphore, portMAX_DELAY); // wait for logTask to complete
// output to monitor console if attached
size_t msgLen = strlen(outBuf);
if (outBuf[msgLen - 2] == '~') {
// set up alert message for browser
outBuf[msgLen - 2] = ' ';
strncpy(alertMsg, outBuf, MAX_OUT - 1);
alertMsg[msgLen - 2] = 0;
}
ramLogStore(msgLen); // store in rtc ram
if (monitorOpen) Serial.print(outBuf);
else delay(10); // allow time for other tasks
if (sdLog) {
if (log_remote_fp != NULL) {
// output to SD if file opened
fwrite(outBuf, sizeof(char), msgLen, log_remote_fp); // log.txt
// periodic sync to SD
if (counter_write++ % WRITE_CACHE_CYCLE == 0) fsync(fileno(log_remote_fp));
}
}
// output to web socket if open
if (msgLen > 1) {
outBuf[msgLen - 1] = 0; // lose final '/n'
if (wsLog) wsAsyncSendText(outBuf);
}
xSemaphoreGive(logMutex);
}
}
void logLine() {
logPrint(" \n");
}
void logSetup() {
// prep logging environment
Serial.begin(115200);
Serial.setDebugOutput(DBG_ON);
printf("\n\n");
if (DEBUG_MEM) printf("init > Free: heap %lu\n", ESP.getFreeHeap());
if (!DBG_ON) esp_log_level_set("*", ESP_LOG_NONE); // suppress ESP_LOG_ERROR messages
if (crashLoop == MAGIC_NUM) snprintf(startupFailure, SF_LEN, STARTUP_FAIL "Crash loop detected, check log %s", (brownoutStatus == 'B' || brownoutStatus == 'R') ? "(brownout)" : " ");
crashLoop = MAGIC_NUM;
logSemaphore = xSemaphoreCreateBinary(); // flag that log message formatted
logMutex = xSemaphoreCreateMutex(); // control access to log formatter
xSemaphoreGive(logSemaphore);
xSemaphoreGive(logMutex);
xTaskCreate(logTask, "logTask", LOG_STACK_SIZE, NULL, LOG_PRI, &logHandle);
if (mlogEnd >= RAM_LOG_LEN) ramLogClear(); // init
LOG_INF("Setup RAM based log, size %u, starting from %u\n\n", RAM_LOG_LEN, mlogEnd);
LOG_INF("=============== %s %s ===============", APP_NAME, APP_VER);
initBrownout();
prepInternalTemp();
LOG_INF("Compiled with arduino-esp32 v%s", ESP_ARDUINO_VERSION_STR);
wakeupResetReason();
if (alertBuffer == NULL) alertBuffer = (byte*)ps_malloc(MAX_ALERT);
if (jsonBuff == NULL) jsonBuff = psramFound() ? (char*)ps_malloc(JSON_BUFF_LEN) : (char*)malloc(JSON_BUFF_LEN);
debugMemory("logSetup");
}
void formatHex(const char* inData, size_t inLen) {
// format data as hex bytes for output
char formatted[(inLen * 3) + 1];
for (int i=0; i<inLen; i++) sprintf(formatted + (i*3), "%02x ", inData[i]);
formatted[(inLen * 3)] = 0; // terminator
LOG_INF("Hex: %s", formatted);
}
const char* espErrMsg(esp_err_t errCode) {
// convert esp error code to text
static char errText[100];
esp_err_to_name_r(errCode, errText, 100);
return errText;
}
void forceCrash() {
// force crash for testing purposes
delay(5000);
#pragma GCC diagnostic ignored "-Wdiv-by-zero"
printf("%u\n", 1/0);
#pragma GCC diagnostic warning "-Wdiv-by-zero"
}
/****************** base 64 ******************/
#define BASE64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
const uint8_t* encode64chunk(const uint8_t* inp, int rem) {
// receive 3 byte input buffer and return 4 byte base64 buffer
rem = 3 - rem; // last chunk may be less than 3 bytes
uint32_t buff = 0; // hold 3 bytes as shifted 24 bits
static uint8_t b64[4];
// shift input into buffer
for (int i = 0; i < 3 - rem; i++) buff |= inp[i] << (8*(2-i));
// shift 6 bit output from buffer and encode
for (int i = 0; i < 4 - rem; i++) b64[i] = BASE64[buff >> (6*(3-i)) & 0x3F];
// filler for last chunk if less than 3 bytes
for (int i = 0; i < rem; i++) b64[3-i] = '=';
return b64;
}
const char* encode64(const char* inp) {
// helper to base64 encode strings up to 90 chars long
static char encoded[121]; // space for 4/3 expansion + terminator
encoded[0] = 0;
int len = strlen(inp);
if (len > 90) {
LOG_WRN("Input string too long: %u chars", len);
len = 90;
}
for (int i = 0; i < len; i += 3)
strncat(encoded, (char*)encode64chunk((uint8_t*)inp + i, min(len - i, 3)), 4);
return encoded;
}
/************** qualitive core idle time monitoring *************/
// not working properly
#include "esp_freertos_hooks.h"
#define INTERVAL_TIME 100 // reporting interval in ms
#define TICKS_PER_INTERVAL (pdMS_TO_TICKS(INTERVAL_TIME))
static uint32_t idleCalls[portNUM_PROCESSORS] = {0};
static uint32_t idleCnt[portNUM_PROCESSORS];
static bool hookCallback() {
idleCalls[xPortGetCoreID()]++;
return true;
}
uint32_t* reportIdle() {
static uint32_t idlePercent[portNUM_PROCESSORS];
for (int i = 0; i < portNUM_PROCESSORS; i++)
idlePercent[i] = (100 * idleCnt[i]) / TICKS_PER_INTERVAL;
return idlePercent;
}
static void idleMonTask(void* p) {
while (true) {
for (int i = 0; i < portNUM_PROCESSORS; i++) {
idleCnt[i] = idleCalls[i];
idleCalls[i] = 0;
}
vTaskDelay(TICKS_PER_INTERVAL);
}
vTaskDelete(NULL);
}
void startIdleMon() {
// report on each core idle time per interval
// Core 0: wifi, Core 1: Arduino
LOG_INF("Start core idle time monitoring @ interval %ums", INTERVAL_TIME);
for (int i = 0; i < portNUM_PROCESSORS; i++)
esp_register_freertos_idle_hook_for_cpu(hookCallback, i);
xTaskCreatePinnedToCore(idleMonTask, "idlemon", 1024, NULL, IDLEMON_PRI, NULL, 0);