This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncESP8266_Ethernet_Manager_Impl.h
1194 lines (857 loc) · 30.8 KB
/
AsyncESP8266_Ethernet_Manager_Impl.h
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
/****************************************************************************************************************************
AsyncESP8266_Ethernet_Manager_Impl.h
For Ethernet shields using ESP8266 Ethernet (ESP8266 + LwIP W5500 / W5100(S) / ENC28J60)
WebServer_ESP8266_W5500 is a library for the ESP8266 with Ethernet LwIP W5500 / W5100(S) / ENC28J60
Modified from
1. Tzapu (https://github.com/tzapu/WiFiManager)
2. Ken Taylor (https://github.com/kentaylor)
3. Alan Steremberg (https://github.com/alanswx/ESPAsyncWiFiManager)
4. Khoi Hoang (https://github.com/khoih-prog/ESPAsync_WiFiManager)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncESP8266_Ethernet_Manager
Licensed under MIT license
Version: 1.0.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 13/12/2022 Initial coding for (ESP8266 + LwIP W5500 / W5100(S) / ENC28J60)
*****************************************************************************************************************************/
#pragma once
#ifndef AsyncESP8266_Ethernet_Manager_Impl_h
#define AsyncESP8266_Ethernet_Manager_Impl_h
//////////////////////////////////////////
ESPAsync_EMParameter::ESPAsync_EMParameter(const char *custom)
{
_WMParam_data._id = NULL;
_WMParam_data._placeholder = NULL;
_WMParam_data._length = 0;
_WMParam_data._value = NULL;
_WMParam_data._labelPlacement = WFM_LABEL_BEFORE;
_customHTML = custom;
}
//////////////////////////////////////////
ESPAsync_EMParameter::ESPAsync_EMParameter(const char *id, const char *placeholder, const char *defaultValue,
const int& length, const char *custom, const int& labelPlacement)
{
init(id, placeholder, defaultValue, length, custom, labelPlacement);
}
//////////////////////////////////////////
// KH, using struct
ESPAsync_EMParameter::ESPAsync_EMParameter(const WMParam_Data& WMParam_data)
{
init(WMParam_data._id, WMParam_data._placeholder, WMParam_data._value,
WMParam_data._length, "", WMParam_data._labelPlacement);
}
//////////////////////////////////////////
void ESPAsync_EMParameter::init(const char *id, const char *placeholder, const char *defaultValue,
const int& length, const char *custom, const int& labelPlacement)
{
_WMParam_data._id = id;
_WMParam_data._placeholder = placeholder;
_WMParam_data._length = length;
_WMParam_data._labelPlacement = labelPlacement;
_WMParam_data._value = new char[_WMParam_data._length + 1];
if (_WMParam_data._value != NULL)
{
memset(_WMParam_data._value, 0, _WMParam_data._length + 1);
if (defaultValue != NULL)
{
strncpy(_WMParam_data._value, defaultValue, _WMParam_data._length);
}
}
_customHTML = custom;
}
//////////////////////////////////////////
ESPAsync_EMParameter::~ESPAsync_EMParameter()
{
if (_WMParam_data._value != NULL)
{
delete[] _WMParam_data._value;
}
}
//////////////////////////////////////////
// Using Struct to get/set whole data at once
void ESPAsync_EMParameter::setWMParam_Data(const WMParam_Data& WMParam_data)
{
LOGINFO(F("setWMParam_Data"));
memcpy(&_WMParam_data, &WMParam_data, sizeof(_WMParam_data));
}
//////////////////////////////////////////
void ESPAsync_EMParameter::getWMParam_Data(WMParam_Data& WMParam_data)
{
LOGINFO(F("getWMParam_Data"));
memcpy(&WMParam_data, &_WMParam_data, sizeof(WMParam_data));
}
//////////////////////////////////////////
const char* ESPAsync_EMParameter::getValue()
{
return _WMParam_data._value;
}
//////////////////////////////////////////
const char* ESPAsync_EMParameter::getID()
{
return _WMParam_data._id;
}
//////////////////////////////////////////
const char* ESPAsync_EMParameter::getPlaceholder()
{
return _WMParam_data._placeholder;
}
//////////////////////////////////////////
int ESPAsync_EMParameter::getValueLength()
{
return _WMParam_data._length;
}
//////////////////////////////////////////
int ESPAsync_EMParameter::getLabelPlacement()
{
return _WMParam_data._labelPlacement;
}
//////////////////////////////////////////
const char* ESPAsync_EMParameter::getCustomHTML()
{
return _customHTML;
}
//////////////////////////////////////////
/**
[getParameters description]
@access public
*/
ESPAsync_EMParameter** AsyncESP8266_Ethernet_Manager::getParameters()
{
return _params;
}
//////////////////////////////////////////
//////////////////////////////////////////
/**
[getParametersCount description]
@access public
*/
int AsyncESP8266_Ethernet_Manager::getParametersCount()
{
return _paramsCount;
}
//////////////////////////////////////////
char* AsyncESP8266_Ethernet_Manager::getRFC952_hostname(const char* iHostname)
{
memset(RFC952_hostname, 0, sizeof(RFC952_hostname));
size_t len = (RFC952_HOSTNAME_MAXLEN < strlen(iHostname)) ? RFC952_HOSTNAME_MAXLEN : strlen(iHostname);
size_t j = 0;
for (size_t i = 0; i < len - 1; i++)
{
if (isalnum(iHostname[i]) || iHostname[i] == '-')
{
RFC952_hostname[j] = iHostname[i];
j++;
}
}
// no '-' as last char
if (isalnum(iHostname[len - 1]) || (iHostname[len - 1] != '-'))
RFC952_hostname[j] = iHostname[len - 1];
return RFC952_hostname;
}
//////////////////////////////////////////
AsyncESP8266_Ethernet_Manager::AsyncESP8266_Ethernet_Manager(AsyncWebServer * webserver, AsyncDNSServer *dnsserver,
const char *iHostname)
{
server = webserver;
dnsServer = dnsserver;
#if USE_DYNAMIC_PARAMS
_max_params = ETH_MANAGER_MAX_PARAMS;
_params = (ESPAsync_EMParameter**) malloc(_max_params * sizeof(ESPAsync_EMParameter*));
#endif
if (iHostname[0] == 0)
{
String _hostname = "ESP8266-" + String(ESP.getChipId(), HEX);
_hostname.toUpperCase();
getRFC952_hostname(_hostname.c_str());
}
else
{
// Prepare and store the hostname only not NULL
getRFC952_hostname(iHostname);
}
LOGWARN1(F("RFC925 Hostname ="), RFC952_hostname);
setHostname();
}
//////////////////////////////////////////
AsyncESP8266_Ethernet_Manager::~AsyncESP8266_Ethernet_Manager()
{
#if USE_DYNAMIC_PARAMS
if (_params != NULL)
{
LOGINFO(F("freeing allocated params!"));
free(_params);
}
#endif
}
//////////////////////////////////////////
#if USE_DYNAMIC_PARAMS
bool AsyncESP8266_Ethernet_Manager::addParameter(ESPAsync_EMParameter *p)
#else
void AsyncESP8266_Ethernet_Manager::addParameter(ESPAsync_EMParameter *p)
#endif
{
#if USE_DYNAMIC_PARAMS
if (_paramsCount == _max_params)
{
// rezise the params array
_max_params += ETH_MANAGER_MAX_PARAMS;
LOGINFO1(F("Increasing _max_params to:"), _max_params);
ESPAsync_EMParameter** new_params = (ESPAsync_EMParameter**)realloc(_params,
_max_params * sizeof(ESPAsync_EMParameter*));
if (new_params != NULL)
{
_params = new_params;
}
else
{
LOGINFO(F("ERROR: failed to realloc params, size not increased!"));
return false;
}
}
_params[_paramsCount] = p;
_paramsCount++;
LOGINFO1(F("Adding parameter"), p->getID());
return true;
#else
// Danger here. Better to use Tzapu way here
if (_paramsCount < (ETH_MANAGER_MAX_PARAMS))
{
_params[_paramsCount] = p;
_paramsCount++;
LOGINFO1(F("Adding parameter"), p->getID());
}
else
{
LOGINFO("Can't add parameter. Full");
}
#endif
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::setupConfigPortal()
{
stopConfigPortal = false; //Signal not to close config portal
server->reset();
if (!dnsServer)
dnsServer = new AsyncDNSServer;
/* Setup the DNS server redirecting all the domains to the apIP */
if (dnsServer)
{
dnsServer->setErrorReplyCode(AsyncDNSReplyCode::NoError);
// AsyncDNSServer started with "*" domain name, all DNS requests will be passsed to eth.localIP()
if (! dnsServer->start(DNS_PORT, "*", eth.localIP()))
{
// No socket available
LOGERROR(F("Can't start DNS Server. No available socket"));
}
}
_configPortalStart = millis();
LOGDEBUG1(F("_configPortalStart millis() ="), millis());
LOGWARN1(F("Config Portal IP address ="), eth.localIP());
/* Setup web pages: root, eth config pages, SO captive portal detectors and not found. */
server->on("/", std::bind(&AsyncESP8266_Ethernet_Manager::handleRoot, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
server->on("/eth", std::bind(&AsyncESP8266_Ethernet_Manager::handleETH, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
server->on("/ethsave", std::bind(&AsyncESP8266_Ethernet_Manager::handleETHSave, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
server->on("/close", std::bind(&AsyncESP8266_Ethernet_Manager::handleServerClose, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
server->on("/i", std::bind(&AsyncESP8266_Ethernet_Manager::handleInfo, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
server->on("/r", std::bind(&AsyncESP8266_Ethernet_Manager::handleReset, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
server->on("/state", std::bind(&AsyncESP8266_Ethernet_Manager::handleState, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
//Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server->on("/fwlink", std::bind(&AsyncESP8266_Ethernet_Manager::handleRoot, this,
std::placeholders::_1)).setFilter(ON_AP_FILTER);
server->onNotFound (std::bind(&AsyncESP8266_Ethernet_Manager::handleNotFound, this, std::placeholders::_1));
server->begin(); // Web server start
LOGWARN(F("HTTP server started"));
}
//////////////////////////////////////////
bool AsyncESP8266_Ethernet_Manager::startConfigPortal()
{
connect = false;
setupConfigPortal();
bool TimedOut = true;
LOGINFO("startConfigPortal : Enter loop");
while (true)
{
if (connect)
{
TimedOut = false;
if (_shouldBreakAfterConfig)
{
//flag set to exit after config after trying to connect
//notify that configuration has changed and any optional parameters should be saved
if (_savecallback != NULL)
{
//todo: check if any custom parameters actually exist, and check if they really changed maybe
_savecallback();
}
LOGDEBUG("Stop ConfigPortal: _shouldBreakAfterConfig");
break;
}
}
if (stopConfigPortal)
{
TimedOut = false;
LOGERROR("stopConfigPortal");
stopConfigPortal = false;
break;
}
if (_configPortalTimeout > 0 && ( millis() > _configPortalStart + _configPortalTimeout) )
{
//LOGDEBUG3("startConfigPortal: timeout, _configPortalTimeout =", _configPortalTimeout, "millis() =", millis());
stopConfigPortal = false;
break;
}
#define TIME_BETWEEN_CONFIG_PORTAL_LOOP 50
delay(TIME_BETWEEN_CONFIG_PORTAL_LOOP);
}
//LOGDEBUG3("startConfigPortal: exit, _configPortalTimeout =", _configPortalTimeout, "millis() =", millis());
server->reset();
dnsServer->stop();
return (eth.connected());
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::setTimeout(const unsigned long& seconds)
{
setConfigPortalTimeout(seconds);
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::setConfigPortalTimeout(const unsigned long& seconds)
{
_configPortalTimeout = seconds * 1000;
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::setConnectTimeout(const unsigned long& seconds)
{
_connectTimeout = seconds * 1000;
}
void AsyncESP8266_Ethernet_Manager::setDebugOutput(bool debug)
{
_debug = debug;
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::setBreakAfterConfig(bool shouldBreak)
{
_shouldBreakAfterConfig = shouldBreak;
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::reportStatus(String& page)
{
page += FPSTR(EM_HTTP_SCRIPT_NTP_MSG);
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::setSTAStaticIPConfig(const IPAddress& ip, const IPAddress& gw, const IPAddress& sn)
{
LOGINFO(F("setSTAStaticIPConfig"));
_ETH_STA_IPconfig._sta_static_ip = ip;
_ETH_STA_IPconfig._sta_static_gw = gw;
_ETH_STA_IPconfig._sta_static_sn = sn;
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::setSTAStaticIPConfig(const ETH_STA_IPConfig& EM_STA_IPconfig)
{
LOGINFO(F("setSTAStaticIPConfig"));
memcpy((void *) &_ETH_STA_IPconfig, &EM_STA_IPconfig, sizeof(_ETH_STA_IPconfig));
}
//////////////////////////////////////////
void AsyncESP8266_Ethernet_Manager::getSTAStaticIPConfig(ETH_STA_IPConfig& EM_STA_IPconfig)
{
LOGINFO(F("getSTAStaticIPConfig"));
memcpy((void *) &EM_STA_IPconfig, &_ETH_STA_IPconfig, sizeof(EM_STA_IPconfig));
}
//////////////////////////////////////////
#if USE_CONFIGURABLE_DNS
void AsyncESP8266_Ethernet_Manager::setSTAStaticIPConfig(const IPAddress& ip, const IPAddress& gw, const IPAddress& sn,
const IPAddress& dns_address_1, const IPAddress& dns_address_2)
{
LOGINFO(F("setSTAStaticIPConfig for USE_CONFIGURABLE_DNS"));
_ETH_STA_IPconfig._sta_static_ip = ip;
_ETH_STA_IPconfig._sta_static_gw = gw;
_ETH_STA_IPconfig._sta_static_sn = sn;
_ETH_STA_IPconfig._sta_static_dns1 = dns_address_1; //***** Added argument *****
_ETH_STA_IPconfig._sta_static_dns2 = dns_address_2; //***** Added argument *****
}
#endif
//////////////////////////////////////////
// Handle root or redirect to captive portal
void AsyncESP8266_Ethernet_Manager::handleRoot(AsyncWebServerRequest *request)
{
LOGDEBUG(F("handleRoot"));
// Disable _configPortalTimeout when someone accessing Portal to give some time to config
_configPortalTimeout = 0;
if (captivePortal(request))
{
LOGDEBUG(F("handleRoot: captive portal exit"));
// If captive portal redirect instead of displaying the error page.
return;
}
String page = FPSTR(EM_HTTP_HEAD_START);
page.replace("{v}", "Options");
page += FPSTR(EM_HTTP_SCRIPT);
page += FPSTR(EM_HTTP_SCRIPT_NTP);
page += FPSTR(EM_HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(EM_HTTP_HEAD_END);
page += FPSTR(EM_FLDSET_START);
page += FPSTR(EM_HTTP_PORTAL_OPTIONS);
page += F("<div class=\"msg\">");
reportStatus(page);
page += F("</div>");
page += FPSTR(EM_FLDSET_END);
page += FPSTR(EM_HTTP_END);
AsyncWebServerResponse *response = request->beginResponse(200, EM_HTTP_HEAD_CT, page);
response->addHeader(FPSTR(EM_HTTP_CACHE_CONTROL), FPSTR(EM_HTTP_NO_STORE));
#if USING_CORS_FEATURE
// For configuring CORS Header, default to EM_HTTP_CORS_ALLOW_ALL = "*"
response->addHeader(FPSTR(EM_HTTP_CORS), _CORS_Header);
#endif
response->addHeader(FPSTR(EM_HTTP_PRAGMA), FPSTR(EM_HTTP_NO_CACHE));
response->addHeader(FPSTR(EM_HTTP_EXPIRES), "-1");
request->send(response);
}
//////////////////////////////////////////
// ETH config page handler
void AsyncESP8266_Ethernet_Manager::handleETH(AsyncWebServerRequest *request)
{
LOGDEBUG(F("Handle ETH"));
// Disable _configPortalTimeout when someone accessing Portal to give some time to config
_configPortalTimeout = 0;
String page = FPSTR(EM_HTTP_HEAD_START);
page.replace("{v}", "Config ESP");
page += FPSTR(EM_HTTP_SCRIPT);
page += FPSTR(EM_HTTP_SCRIPT_NTP);
page += FPSTR(EM_HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(EM_HTTP_HEAD_END);
page += F("<h2>Configuration</h2>");
page += FPSTR(EM_HTTP_FORM_START);
char parLength[2];
page += FPSTR(EM_FLDSET_START);
// add the extra parameters to the form
for (int i = 0; i < _paramsCount; i++)
{
if (_params[i] == NULL)
{
break;
}
String pitem;
switch (_params[i]->getLabelPlacement())
{
case WFM_LABEL_BEFORE:
pitem = FPSTR(EM_HTTP_FORM_LABEL_BEFORE);
break;
case WFM_LABEL_AFTER:
pitem = FPSTR(EM_HTTP_FORM_LABEL_AFTER);
break;
default:
// WFM_NO_LABEL
pitem = FPSTR(EM_HTTP_FORM_PARAM);
break;
}
if (_params[i]->getID() != NULL)
{
pitem.replace("{i}", _params[i]->getID());
pitem.replace("{n}", _params[i]->getID());
pitem.replace("{p}", _params[i]->getPlaceholder());
snprintf(parLength, 2, "%d", _params[i]->getValueLength());
pitem.replace("{l}", parLength);
pitem.replace("{v}", _params[i]->getValue());
pitem.replace("{c}", _params[i]->getCustomHTML());
}
else
{
pitem = _params[i]->getCustomHTML();
}
page += pitem;
}
if (_paramsCount > 0)
{
page += FPSTR(EM_FLDSET_END);
}
if (_params[0] != NULL)
{
page += "<br/>";
}
LOGDEBUG1(F("Static IP ="), _ETH_STA_IPconfig._sta_static_ip.toString());
// KH, Comment out to permit changing from DHCP to static IP, or vice versa
// and add staticIP label in CP
// To permit disable/enable StaticIP configuration in Config Portal from sketch. Valid only if DHCP is used.
// You'll loose the feature of dynamically changing from DHCP to static IP, or vice versa
// You have to explicitly specify false to disable the feature.
#if !USE_STATIC_IP_CONFIG_IN_CP
if (_ETH_STA_IPconfig._sta_static_ip)
#endif
{
page += FPSTR(EM_FLDSET_START);
String item = FPSTR(EM_HTTP_FORM_LABEL);
item += FPSTR(EM_HTTP_FORM_PARAM);
item.replace("{i}", "ip");
item.replace("{n}", "ip");
item.replace("{p}", "Static IP");
item.replace("{l}", "15");
item.replace("{v}", _ETH_STA_IPconfig._sta_static_ip.toString());
page += item;
item = FPSTR(EM_HTTP_FORM_LABEL);
item += FPSTR(EM_HTTP_FORM_PARAM);
item.replace("{i}", "gw");
item.replace("{n}", "gw");
item.replace("{p}", "Gateway IP");
item.replace("{l}", "15");
item.replace("{v}", _ETH_STA_IPconfig._sta_static_gw.toString());
page += item;
item = FPSTR(EM_HTTP_FORM_LABEL);
item += FPSTR(EM_HTTP_FORM_PARAM);
item.replace("{i}", "sn");
item.replace("{n}", "sn");
item.replace("{p}", "Subnet");
item.replace("{l}", "15");
item.replace("{v}", _ETH_STA_IPconfig._sta_static_sn.toString());
#if USE_CONFIGURABLE_DNS
//***** Added for DNS address options *****
page += item;
item = FPSTR(EM_HTTP_FORM_LABEL);
item += FPSTR(EM_HTTP_FORM_PARAM);
item.replace("{i}", "dns1");
item.replace("{n}", "dns1");
item.replace("{p}", "DNS1 IP");
item.replace("{l}", "15");
item.replace("{v}", _ETH_STA_IPconfig._sta_static_dns1.toString());
page += item;
item = FPSTR(EM_HTTP_FORM_LABEL);
item += FPSTR(EM_HTTP_FORM_PARAM);
item.replace("{i}", "dns2");
item.replace("{n}", "dns2");
item.replace("{p}", "DNS2 IP");
item.replace("{l}", "15");
item.replace("{v}", _ETH_STA_IPconfig._sta_static_dns2.toString());
//***** End added for DNS address options *****
#endif
page += item;
page += FPSTR(EM_FLDSET_END);
page += "<br/>";
}
page += FPSTR(EM_HTTP_SCRIPT_NTP_HIDDEN);
page += FPSTR(EM_HTTP_FORM_END);
page += FPSTR(EM_HTTP_END);
AsyncWebServerResponse *response = request->beginResponse(200, EM_HTTP_HEAD_CT, page);
response->addHeader(FPSTR(EM_HTTP_CACHE_CONTROL), FPSTR(EM_HTTP_NO_STORE));
#if USING_CORS_FEATURE
response->addHeader(FPSTR(EM_HTTP_CORS), _CORS_Header);
#endif
response->addHeader(FPSTR(EM_HTTP_PRAGMA), FPSTR(EM_HTTP_NO_CACHE));
response->addHeader(FPSTR(EM_HTTP_EXPIRES), "-1");
request->send(response);
LOGDEBUG(F("Sent config page"));
}
//////////////////////////////////////////
// Handle the WLAN save form and redirect to WLAN config page again
void AsyncESP8266_Ethernet_Manager::handleETHSave(AsyncWebServerRequest *request)
{
LOGDEBUG(F("ETH save"));
#if USE_ESP_ETH_MANAGER_NTP
if (request->hasArg("timezone"))
{
_timezoneName = request->arg("timezone"); //.c_str();
LOGDEBUG1(F("TZ ="), _timezoneName);
}
else
{
LOGDEBUG(F("No TZ arg"));
}
#endif
///////////////////////
//parameters
for (int i = 0; i < _paramsCount; i++)
{
if (_params[i] == NULL)
{
break;
}
//read parameter
String value = request->arg(_params[i]->getID()).c_str();
//store it in array
value.toCharArray(_params[i]->_WMParam_data._value, _params[i]->_WMParam_data._length);
LOGDEBUG2(F("Parameter and value :"), _params[i]->getID(), value);
}
if (request->hasArg("ip"))
{
String ip = request->arg("ip");
optionalIPFromString(&_ETH_STA_IPconfig._sta_static_ip, ip.c_str());
LOGDEBUG1(F("New Static IP ="), _ETH_STA_IPconfig._sta_static_ip.toString());
}
if (request->hasArg("gw"))
{
String gw = request->arg("gw");
optionalIPFromString(&_ETH_STA_IPconfig._sta_static_gw, gw.c_str());
LOGDEBUG1(F("New Static Gateway ="), _ETH_STA_IPconfig._sta_static_gw.toString());
}
if (request->hasArg("sn"))
{
String sn = request->arg("sn");
optionalIPFromString(&_ETH_STA_IPconfig._sta_static_sn, sn.c_str());
LOGDEBUG1(F("New Static Netmask ="), _ETH_STA_IPconfig._sta_static_sn.toString());
}
#if USE_CONFIGURABLE_DNS
//***** Added for DNS Options *****
if (request->hasArg("dns1"))
{
String dns1 = request->arg("dns1");
optionalIPFromString(&_ETH_STA_IPconfig._sta_static_dns1, dns1.c_str());
LOGDEBUG1(F("New Static DNS1 ="), _ETH_STA_IPconfig._sta_static_dns1.toString());
}
if (request->hasArg("dns2"))
{
String dns2 = request->arg("dns2");
optionalIPFromString(&_ETH_STA_IPconfig._sta_static_dns2, dns2.c_str());
LOGDEBUG1(F("New Static DNS2 ="), _ETH_STA_IPconfig._sta_static_dns2.toString());
}
//***** End added for DNS Options *****
#endif
String page = FPSTR(EM_HTTP_HEAD_START);
page.replace("{v}", "Credentials Saved");
page += FPSTR(EM_HTTP_SCRIPT);
page += FPSTR(EM_HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(EM_HTTP_HEAD_END);
page += FPSTR(EM_HTTP_SAVED);
page += FPSTR(EM_HTTP_END);
AsyncWebServerResponse *response = request->beginResponse(200, EM_HTTP_HEAD_CT, page);
response->addHeader(FPSTR(EM_HTTP_CACHE_CONTROL), FPSTR(EM_HTTP_NO_STORE));
#if USING_CORS_FEATURE
response->addHeader(FPSTR(EM_HTTP_CORS), _CORS_Header);
#endif
response->addHeader(FPSTR(EM_HTTP_PRAGMA), FPSTR(EM_HTTP_NO_CACHE));
response->addHeader(FPSTR(EM_HTTP_EXPIRES), "-1");
request->send(response);
LOGDEBUG(F("Sent eth save page"));
connect = true; //signal ready to connect/reset
stopConfigPortal = true; //signal ready to shutdown config portal
}
//////////////////////////////////////////
// Handle shut down the server page
void AsyncESP8266_Ethernet_Manager::handleServerClose(AsyncWebServerRequest *request)
{
LOGDEBUG(F("Server Close"));
String page = FPSTR(EM_HTTP_HEAD_START);
page.replace("{v}", "Close Server");
page += FPSTR(EM_HTTP_SCRIPT);
page += FPSTR(EM_HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(EM_HTTP_HEAD_END);
page += F("<div class=\"msg\">");
page += F("</b><br>");
page += F("IP address is <b>");
page += eth.localIP().toString();
page += F("</b><br><br>");
page += F("Portal closed...<br><br>");
//page += F("Push button on device to restart configuration server!");
page += FPSTR(EM_HTTP_END);
AsyncWebServerResponse *response = request->beginResponse(200, EM_HTTP_HEAD_CT, page);
response->addHeader(FPSTR(EM_HTTP_CACHE_CONTROL), FPSTR(EM_HTTP_NO_STORE));
#if USING_CORS_FEATURE
response->addHeader(FPSTR(EM_HTTP_CORS), _CORS_Header);
#endif
response->addHeader(FPSTR(EM_HTTP_PRAGMA), FPSTR(EM_HTTP_NO_CACHE));
response->addHeader(FPSTR(EM_HTTP_EXPIRES), "-1");
request->send(response);
stopConfigPortal = true; //signal ready to shutdown config portal
LOGDEBUG(F("Sent server close page"));
}
//////////////////////////////////////////
// Handle the info page
void AsyncESP8266_Ethernet_Manager::handleInfo(AsyncWebServerRequest *request)
{
LOGDEBUG(F("Info"));
// Disable _configPortalTimeout when someone accessing Portal to give some time to config
_configPortalTimeout = 0;
String page = FPSTR(EM_HTTP_HEAD_START);
page.replace("{v}", "Info");
page += FPSTR(EM_HTTP_SCRIPT);
page += FPSTR(EM_HTTP_SCRIPT_NTP);
page += FPSTR(EM_HTTP_STYLE);
page += _customHeadElement;
if (connect)
page += F("<meta http-equiv=\"refresh\" content=\"5; url=/i\">");
page += FPSTR(EM_HTTP_HEAD_END);
page += F("<dl>");
if (connect)
{
page += F("<dt>Trying to connect</dt><dd>");
page += ethStatus;
page += F("</dd>");
}
page += pager;
page += F("<h2>Information</h2>");
reportStatus(page);
page += FPSTR(EM_FLDSET_START);
page += F("<h3>Device Data</h3>");
page += F("<table class=\"table\">");
page += F("<thead><tr><th>Name</th><th>Value</th></tr></thead><tbody><tr><td>Chip ID</td><td>");
page += String(ESP.getChipId(), HEX);
page += F("</td></tr>");
page += F("<tr><td>Flash Chip ID</td><td>");
page += String(ESP.getFlashChipId(), HEX);
page += F("</td></tr>");
page += F("<tr><td>IDE Flash Size</td><td>");
page += ESP.getFlashChipSize();
page += F(" bytes</td></tr>");
page += F("<tr><td>Real Flash Size</td><td>");
page += ESP.getFlashChipRealSize();
page += F(" bytes</td></tr>");
page += F("<tr><td>Station IP</td><td>");
page += eth.localIP().toString();
page += F("</td></tr>");
page += F("</tbody></table>");
page += FPSTR(EM_FLDSET_END);
#if USE_AVAILABLE_PAGES
page += FPSTR(EM_FLDSET_START);
page += FPSTR(EM_HTTP_AVAILABLE_PAGES);
page += FPSTR(EM_FLDSET_END);
#endif
page += F("<p/>More information about AsyncESP8266_Ethernet_Manager at");
page += F("<p/><a href=\"https://github.com/khoih-prog/AsyncESP8266_Ethernet_Manager\">https://github.com/khoih-prog/AsyncESP8266_Ethernet_Manager</a>");
page += FPSTR(EM_HTTP_END);
AsyncWebServerResponse *response = request->beginResponse(200, EM_HTTP_HEAD_CT, page);