-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
WiFiGeneric.cpp
1630 lines (1490 loc) · 61 KB
/
WiFiGeneric.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
/*
ESP8266WiFiGeneric.cpp - WiFi library for esp8266
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Reworked on 28 Dec 2015 by Markus Sattler
*/
#include "WiFi.h"
#include "WiFiGeneric.h"
extern "C" {
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <esp_err.h>
#include <esp_wifi.h>
#include <esp_event.h>
#include "lwip/ip_addr.h"
#include "lwip/opt.h"
#include "lwip/err.h"
#include "lwip/dns.h"
#include "dhcpserver/dhcpserver_options.h"
} //extern "C"
#include "esp32-hal.h"
#include <vector>
#include "sdkconfig.h"
#define _byte_swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
ESP_EVENT_DEFINE_BASE(ARDUINO_EVENTS);
/*
* Private (exposable) methods
* */
static esp_netif_t* esp_netifs[ESP_IF_MAX] = {NULL, NULL, NULL};
esp_interface_t get_esp_netif_interface(esp_netif_t* esp_netif){
for(int i=0; i<ESP_IF_MAX; i++){
if(esp_netifs[i] != NULL && esp_netifs[i] == esp_netif){
return (esp_interface_t)i;
}
}
return ESP_IF_MAX;
}
void add_esp_interface_netif(esp_interface_t interface, esp_netif_t* esp_netif){
if(interface < ESP_IF_MAX){
esp_netifs[interface] = esp_netif;
}
}
esp_netif_t* get_esp_interface_netif(esp_interface_t interface){
if(interface < ESP_IF_MAX){
return esp_netifs[interface];
}
return NULL;
}
esp_err_t set_esp_interface_hostname(esp_interface_t interface, const char * hostname){
if(interface < ESP_IF_MAX){
return esp_netif_set_hostname(esp_netifs[interface], hostname);
}
return ESP_FAIL;
}
esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPAddress(), IPAddress gateway=IPAddress(), IPAddress subnet=IPAddress(), IPAddress dhcp_lease_start=INADDR_NONE){
esp_netif_t *esp_netif = esp_netifs[interface];
esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
esp_netif_ip_info_t info;
info.ip.addr = static_cast<uint32_t>(local_ip);
info.gw.addr = static_cast<uint32_t>(gateway);
info.netmask.addr = static_cast<uint32_t>(subnet);
log_v("Configuring %s static IP: " IPSTR ", MASK: " IPSTR ", GW: " IPSTR,
interface == ESP_IF_WIFI_STA ? "Station" :
interface == ESP_IF_WIFI_AP ? "SoftAP" : "Ethernet",
IP2STR(&info.ip), IP2STR(&info.netmask), IP2STR(&info.gw));
esp_err_t err = ESP_OK;
if(interface != ESP_IF_WIFI_AP){
err = esp_netif_dhcpc_get_status(esp_netif, &status);
if(err){
log_e("DHCPC Get Status Failed! 0x%04x", err);
return err;
}
err = esp_netif_dhcpc_stop(esp_netif);
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
log_e("DHCPC Stop Failed! 0x%04x", err);
return err;
}
err = esp_netif_set_ip_info(esp_netif, &info);
if(err){
log_e("Netif Set IP Failed! 0x%04x", err);
return err;
}
if(info.ip.addr == 0){
err = esp_netif_dhcpc_start(esp_netif);
if(err){
log_e("DHCPC Start Failed! 0x%04x", err);
return err;
}
}
} else {
err = esp_netif_dhcps_get_status(esp_netif, &status);
if(err){
log_e("DHCPS Get Status Failed! 0x%04x", err);
return err;
}
err = esp_netif_dhcps_stop(esp_netif);
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
log_e("DHCPS Stop Failed! 0x%04x", err);
return err;
}
err = esp_netif_set_ip_info(esp_netif, &info);
if(err){
log_e("Netif Set IP Failed! 0x%04x", err);
return err;
}
dhcps_lease_t lease;
lease.enable = true;
uint8_t CIDR = WiFiGenericClass::calculateSubnetCIDR(subnet);
log_v("SoftAP: %s | Gateway: %s | DHCP Start: %s | Netmask: %s", local_ip.toString().c_str(), gateway.toString().c_str(), dhcp_lease_start.toString().c_str(), subnet.toString().c_str());
// netmask must have room for at least 12 IP addresses (AP + GW + 10 DHCP Leasing addresses)
// netmask also must be limited to the last 8 bits of IPv4, otherwise this function won't work
// IDF NETIF checks netmask for the 3rd byte: https://github.com/espressif/esp-idf/blob/master/components/esp_netif/lwip/esp_netif_lwip.c#L1857-L1862
if (CIDR > 28 || CIDR < 24) {
log_e("Bad netmask. It must be from /24 to /28 (255.255.255. 0<->240)");
return ESP_FAIL; // ESP_FAIL if initializing failed
}
// The code below is ready for any netmask, not limited to 255.255.255.0
uint32_t netmask = _byte_swap32(info.netmask.addr);
uint32_t ap_ipaddr = _byte_swap32(info.ip.addr);
uint32_t dhcp_ipaddr = _byte_swap32(static_cast<uint32_t>(dhcp_lease_start));
dhcp_ipaddr = dhcp_ipaddr == 0 ? ap_ipaddr + 1 : dhcp_ipaddr;
uint32_t leaseStartMax = ~netmask - 10;
// there will be 10 addresses for DHCP to lease
lease.start_ip.addr = dhcp_ipaddr;
lease.end_ip.addr = lease.start_ip.addr + 10;
// Check if local_ip is in the same subnet as the dhcp leasing range initial address
if ((ap_ipaddr & netmask) != (dhcp_ipaddr & netmask)) {
log_e("The AP IP address (%s) and the DHCP start address (%s) must be in the same subnet",
local_ip.toString().c_str(), IPAddress(_byte_swap32(dhcp_ipaddr)).toString().c_str());
return ESP_FAIL; // ESP_FAIL if initializing failed
}
// prevents DHCP lease range to overflow subnet range
if ((dhcp_ipaddr & ~netmask) >= leaseStartMax) {
// make first DHCP lease addr stay in the begining of the netmask range
lease.start_ip.addr = (dhcp_ipaddr & netmask) + 1;
lease.end_ip.addr = lease.start_ip.addr + 10;
log_w("DHCP Lease out of range - Changing DHCP leasing start to %s", IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str());
}
// Check if local_ip is within DHCP range
if (ap_ipaddr >= lease.start_ip.addr && ap_ipaddr <= lease.end_ip.addr) {
log_e("The AP IP address (%s) can't be within the DHCP range (%s -- %s)",
local_ip.toString().c_str(), IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str(), IPAddress(_byte_swap32(lease.end_ip.addr)).toString().c_str());
return ESP_FAIL; // ESP_FAIL if initializing failed
}
// Check if gateway is within DHCP range
uint32_t gw_ipaddr = _byte_swap32(info.gw.addr);
bool gw_in_same_subnet = (gw_ipaddr & netmask) == (ap_ipaddr & netmask);
if (gw_in_same_subnet && gw_ipaddr >= lease.start_ip.addr && gw_ipaddr <= lease.end_ip.addr) {
log_e("The GatewayP address (%s) can't be within the DHCP range (%s -- %s)",
gateway.toString().c_str(), IPAddress(_byte_swap32(lease.start_ip.addr)).toString().c_str(), IPAddress(_byte_swap32(lease.end_ip.addr)).toString().c_str());
return ESP_FAIL; // ESP_FAIL if initializing failed
}
// all done, just revert back byte order of DHCP lease range
lease.start_ip.addr = _byte_swap32(lease.start_ip.addr);
lease.end_ip.addr = _byte_swap32(lease.end_ip.addr);
log_v("DHCP Server Range: %s to %s", IPAddress(lease.start_ip.addr).toString().c_str(), IPAddress(lease.end_ip.addr).toString().c_str());
// Following block is commented because it breaks AP DHCPS on recent ESP-IDF
// err = esp_netif_dhcps_option(
// esp_netif,
// ESP_NETIF_OP_SET,
// ESP_NETIF_SUBNET_MASK,
// (void*)&info.netmask.addr, sizeof(info.netmask.addr)
// );
// if(err){
// log_e("DHCPS Set Netmask Failed! 0x%04x", err);
// return err;
// }
err = esp_netif_dhcps_option(
esp_netif,
ESP_NETIF_OP_SET,
ESP_NETIF_REQUESTED_IP_ADDRESS,
(void*)&lease, sizeof(dhcps_lease_t)
);
if(err){
log_e("DHCPS Set Lease Failed! 0x%04x", err);
return err;
}
err = esp_netif_dhcps_start(esp_netif);
if(err){
log_e("DHCPS Start Failed! 0x%04x", err);
return err;
}
}
return err;
}
esp_err_t set_esp_interface_dns(esp_interface_t interface, IPAddress main_dns=IPAddress(), IPAddress backup_dns=IPAddress(), IPAddress fallback_dns=IPAddress()){
esp_netif_t *esp_netif = esp_netifs[interface];
esp_netif_dns_info_t dns;
dns.ip.type = ESP_IPADDR_TYPE_V4;
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(main_dns);
if(dns.ip.u_addr.ip4.addr && esp_netif_set_dns_info(esp_netif, ESP_NETIF_DNS_MAIN, &dns) != ESP_OK){
log_e("Set Main DNS Failed!");
return ESP_FAIL;
}
if(interface != ESP_IF_WIFI_AP){
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(backup_dns);
if(dns.ip.u_addr.ip4.addr && esp_netif_set_dns_info(esp_netif, ESP_NETIF_DNS_BACKUP, &dns) != ESP_OK){
log_e("Set Backup DNS Failed!");
return ESP_FAIL;
}
dns.ip.u_addr.ip4.addr = static_cast<uint32_t>(fallback_dns);
if(dns.ip.u_addr.ip4.addr && esp_netif_set_dns_info(esp_netif, ESP_NETIF_DNS_FALLBACK, &dns) != ESP_OK){
log_e("Set Fallback DNS Failed!");
return ESP_FAIL;
}
}
return ESP_OK;
}
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
static const char * auth_mode_str(int authmode)
{
switch (authmode) {
case WIFI_AUTH_OPEN:
return ("OPEN");
break;
case WIFI_AUTH_WEP:
return ("WEP");
break;
case WIFI_AUTH_WPA_PSK:
return ("WPA_PSK");
break;
case WIFI_AUTH_WPA2_PSK:
return ("WPA2_PSK");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
return ("WPA_WPA2_PSK");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
return ("WPA2_ENTERPRISE");
break;
case WIFI_AUTH_WPA3_PSK:
return ("WPA3_PSK");
break;
case WIFI_AUTH_WPA2_WPA3_PSK:
return ("WPA2_WPA3_PSK");
break;
case WIFI_AUTH_WAPI_PSK:
return ("WPAPI_PSK");
break;
default:
break;
}
return ("UNKNOWN");
}
#endif
static char default_hostname[32] = {0,};
static const char * get_esp_netif_hostname(){
if(default_hostname[0] == 0){
uint8_t eth_mac[6];
esp_wifi_get_mac((wifi_interface_t)WIFI_IF_STA, eth_mac);
snprintf(default_hostname, 32, "%s%02X%02X%02X", CONFIG_IDF_TARGET "-", eth_mac[3], eth_mac[4], eth_mac[5]);
}
return (const char *)default_hostname;
}
static void set_esp_netif_hostname(const char * name){
if(name){
snprintf(default_hostname, 32, "%s", name);
}
}
static xQueueHandle _arduino_event_queue;
static TaskHandle_t _arduino_event_task_handle = NULL;
static EventGroupHandle_t _arduino_event_group = NULL;
static void _arduino_event_task(void * arg){
arduino_event_t *data = NULL;
for (;;) {
if(xQueueReceive(_arduino_event_queue, &data, portMAX_DELAY) == pdTRUE){
WiFiGenericClass::_eventCallback(data);
free(data);
data = NULL;
}
}
vTaskDelete(NULL);
_arduino_event_task_handle = NULL;
}
esp_err_t postArduinoEvent(arduino_event_t *data)
{
if(data == NULL){
return ESP_FAIL;
}
arduino_event_t * event = (arduino_event_t*)malloc(sizeof(arduino_event_t));
if(event == NULL){
log_e("Arduino Event Malloc Failed!");
return ESP_FAIL;
}
memcpy(event, data, sizeof(arduino_event_t));
if (xQueueSend(_arduino_event_queue, &event, portMAX_DELAY) != pdPASS) {
log_e("Arduino Event Send Failed!");
return ESP_FAIL;
}
return ESP_OK;
}
static void _arduino_event_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
arduino_event_t arduino_event;
arduino_event.event_id = ARDUINO_EVENT_MAX;
/*
* STA
* */
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
log_v("STA Started");
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_START;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_STOP) {
log_v("STA Stopped");
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_STOP;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_event_sta_authmode_change_t * event = (wifi_event_sta_authmode_change_t*)event_data;
log_v("STA Auth Mode Changed: From: %s, To: %s", auth_mode_str(event->old_mode), auth_mode_str(event->new_mode));
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE;
memcpy(&arduino_event.event_info.wifi_sta_authmode_change, event_data, sizeof(wifi_event_sta_authmode_change_t));
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_event_sta_connected_t * event = (wifi_event_sta_connected_t*)event_data;
log_v("STA Connected: SSID: %s, BSSID: " MACSTR ", Channel: %u, Auth: %s", event->ssid, MAC2STR(event->bssid), event->channel, auth_mode_str(event->authmode));
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_CONNECTED;
memcpy(&arduino_event.event_info.wifi_sta_connected, event_data, sizeof(wifi_event_sta_connected_t));
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_event_sta_disconnected_t * event = (wifi_event_sta_disconnected_t*)event_data;
log_v("STA Disconnected: SSID: %s, BSSID: " MACSTR ", Reason: %u", event->ssid, MAC2STR(event->bssid), event->reason);
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_DISCONNECTED;
memcpy(&arduino_event.event_info.wifi_sta_disconnected, event_data, sizeof(wifi_event_sta_disconnected_t));
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
log_v("STA Got %sIP:" IPSTR, event->ip_changed?"New ":"Same ", IP2STR(&event->ip_info.ip));
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_GOT_IP;
memcpy(&arduino_event.event_info.got_ip, event_data, sizeof(ip_event_got_ip_t));
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) {
log_v("STA IP Lost");
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_LOST_IP;
/*
* SCAN
* */
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_event_sta_scan_done_t * event = (wifi_event_sta_scan_done_t*)event_data;
log_v("SCAN Done: ID: %u, Status: %u, Results: %u", event->scan_id, event->status, event->number);
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_SCAN_DONE;
memcpy(&arduino_event.event_info.wifi_scan_done, event_data, sizeof(wifi_event_sta_scan_done_t));
/*
* AP
* */
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) {
log_v("AP Started");
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_START;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STOP) {
log_v("AP Stopped");
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_STOP;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_event_ap_probe_req_rx_t * event = (wifi_event_ap_probe_req_rx_t*)event_data;
log_v("AP Probe Request: RSSI: %d, MAC: " MACSTR, event->rssi, MAC2STR(event->mac));
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED;
memcpy(&arduino_event.event_info.wifi_ap_probereqrecved, event_data, sizeof(wifi_event_ap_probe_req_rx_t));
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
log_v("AP Station Connected: MAC: " MACSTR ", AID: %d", MAC2STR(event->mac), event->aid);
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_STACONNECTED;
memcpy(&arduino_event.event_info.wifi_ap_staconnected, event_data, sizeof(wifi_event_ap_staconnected_t));
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
log_v("AP Station Disconnected: MAC: " MACSTR ", AID: %d", MAC2STR(event->mac), event->aid);
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_STADISCONNECTED;
memcpy(&arduino_event.event_info.wifi_ap_stadisconnected, event_data, sizeof(wifi_event_ap_stadisconnected_t));
} else if (event_base == IP_EVENT && event_id == IP_EVENT_AP_STAIPASSIGNED) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
ip_event_ap_staipassigned_t * event = (ip_event_ap_staipassigned_t*)event_data;
log_v("AP Station IP Assigned:" IPSTR, IP2STR(&event->ip));
#endif
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED;
memcpy(&arduino_event.event_info.wifi_ap_staipassigned, event_data, sizeof(ip_event_ap_staipassigned_t));
/*
* ETH
* */
#ifdef CONFIG_ETH_ENABLED
} else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_CONNECTED) {
log_v("Ethernet Link Up");
arduino_event.event_id = ARDUINO_EVENT_ETH_CONNECTED;
memcpy(&arduino_event.event_info.eth_connected, event_data, sizeof(esp_eth_handle_t));
} else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_DISCONNECTED) {
log_v("Ethernet Link Down");
arduino_event.event_id = ARDUINO_EVENT_ETH_DISCONNECTED;
} else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) {
log_v("Ethernet Started");
arduino_event.event_id = ARDUINO_EVENT_ETH_START;
} else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_STOP) {
log_v("Ethernet Stopped");
arduino_event.event_id = ARDUINO_EVENT_ETH_STOP;
} else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
log_v("Ethernet got %sip:" IPSTR, event->ip_changed?"new":"", IP2STR(&event->ip_info.ip));
#endif
arduino_event.event_id = ARDUINO_EVENT_ETH_GOT_IP;
memcpy(&arduino_event.event_info.got_ip, event_data, sizeof(ip_event_got_ip_t));
#endif // CONFIG_ETH_ENABLED
/*
* IPv6
* */
} else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
ip_event_got_ip6_t * event = (ip_event_got_ip6_t*)event_data;
esp_interface_t iface = get_esp_netif_interface(event->esp_netif);
log_v("IF[%d] Got IPv6: IP Index: %d, Zone: %d, " IPV6STR, iface, event->ip_index, event->ip6_info.ip.zone, IPV62STR(event->ip6_info.ip));
memcpy(&arduino_event.event_info.got_ip6, event_data, sizeof(ip_event_got_ip6_t));
if(iface == ESP_IF_WIFI_STA){
arduino_event.event_id = ARDUINO_EVENT_WIFI_STA_GOT_IP6;
} else if(iface == ESP_IF_WIFI_AP){
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_GOT_IP6;
} else if(iface == ESP_IF_ETH){
arduino_event.event_id = ARDUINO_EVENT_ETH_GOT_IP6;
}
/*
* WPS
* */
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_WPS_ER_SUCCESS) {
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_SUCCESS;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_WPS_ER_FAILED) {
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_FAILED;
memcpy(&arduino_event.event_info.wps_fail_reason, event_data, sizeof(wifi_event_sta_wps_fail_reason_t));
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_WPS_ER_TIMEOUT) {
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_TIMEOUT;
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_WPS_ER_PIN) {
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_PIN;
memcpy(&arduino_event.event_info.wps_er_pin, event_data, sizeof(wifi_event_sta_wps_er_pin_t));
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP) {
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_PBC_OVERLAP;
/*
* FTM
* */
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_FTM_REPORT) {
arduino_event.event_id = ARDUINO_EVENT_WIFI_FTM_REPORT;
memcpy(&arduino_event.event_info.wifi_ftm_report, event_data, sizeof(wifi_event_ftm_report_t));
/*
* SMART CONFIG
* */
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) {
log_v("SC Scan Done");
arduino_event.event_id = ARDUINO_EVENT_SC_SCAN_DONE;
} else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) {
log_v("SC Found Channel");
arduino_event.event_id = ARDUINO_EVENT_SC_FOUND_CHANNEL;
} else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
smartconfig_event_got_ssid_pswd_t *event = (smartconfig_event_got_ssid_pswd_t *)event_data;
log_v("SC: SSID: %s, Password: %s", (const char *)event->ssid, (const char *)event->password);
#endif
arduino_event.event_id = ARDUINO_EVENT_SC_GOT_SSID_PSWD;
memcpy(&arduino_event.event_info.sc_got_ssid_pswd, event_data, sizeof(smartconfig_event_got_ssid_pswd_t));
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
log_v("SC Send Ack Done");
arduino_event.event_id = ARDUINO_EVENT_SC_SEND_ACK_DONE;
/*
* Provisioning
* */
} else if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_INIT) {
log_v("Provisioning Initialized!");
arduino_event.event_id = ARDUINO_EVENT_PROV_INIT;
} else if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_DEINIT) {
log_v("Provisioning Uninitialized!");
arduino_event.event_id = ARDUINO_EVENT_PROV_DEINIT;
} else if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_START) {
log_v("Provisioning Start!");
arduino_event.event_id = ARDUINO_EVENT_PROV_START;
} else if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_END) {
log_v("Provisioning End!");
wifi_prov_mgr_deinit();
arduino_event.event_id = ARDUINO_EVENT_PROV_END;
} else if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_CRED_RECV) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
wifi_sta_config_t *event = (wifi_sta_config_t *)event_data;
log_v("Provisioned Credentials: SSID: %s, Password: %s", (const char *) event->ssid, (const char *) event->password);
#endif
arduino_event.event_id = ARDUINO_EVENT_PROV_CRED_RECV;
memcpy(&arduino_event.event_info.prov_cred_recv, event_data, sizeof(wifi_sta_config_t));
} else if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_CRED_FAIL) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
log_e("Provisioning Failed: Reason : %s", (*reason == WIFI_PROV_STA_AUTH_ERROR)?"Authentication Failed":"AP Not Found");
#endif
arduino_event.event_id = ARDUINO_EVENT_PROV_CRED_FAIL;
memcpy(&arduino_event.event_info.prov_fail_reason, event_data, sizeof(wifi_prov_sta_fail_reason_t));
} else if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_CRED_SUCCESS) {
log_v("Provisioning Success!");
arduino_event.event_id = ARDUINO_EVENT_PROV_CRED_SUCCESS;
}
if(arduino_event.event_id < ARDUINO_EVENT_MAX){
postArduinoEvent(&arduino_event);
}
}
static bool _start_network_event_task(){
if(!_arduino_event_group){
_arduino_event_group = xEventGroupCreate();
if(!_arduino_event_group){
log_e("Network Event Group Create Failed!");
return false;
}
xEventGroupSetBits(_arduino_event_group, WIFI_DNS_IDLE_BIT);
}
if(!_arduino_event_queue){
_arduino_event_queue = xQueueCreate(32, sizeof(arduino_event_t*));
if(!_arduino_event_queue){
log_e("Network Event Queue Create Failed!");
return false;
}
}
esp_err_t err = esp_event_loop_create_default();
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
log_e("esp_event_loop_create_default failed!");
return err;
}
if(!_arduino_event_task_handle){
xTaskCreateUniversal(_arduino_event_task, "arduino_events", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_arduino_event_task_handle, ARDUINO_EVENT_RUNNING_CORE);
if(!_arduino_event_task_handle){
log_e("Network Event Task Start Failed!");
return false;
}
}
if(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb, NULL, NULL)){
log_e("event_handler_instance_register for WIFI_EVENT Failed!");
return false;
}
if(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb, NULL, NULL)){
log_e("event_handler_instance_register for IP_EVENT Failed!");
return false;
}
if(esp_event_handler_instance_register(SC_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb, NULL, NULL)){
log_e("event_handler_instance_register for SC_EVENT Failed!");
return false;
}
#ifdef CONFIG_ETH_ENABLED
if(esp_event_handler_instance_register(ETH_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb, NULL, NULL)){
log_e("event_handler_instance_register for ETH_EVENT Failed!");
return false;
}
#endif // CONFIG_ETH_ENABLED
if(esp_event_handler_instance_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &_arduino_event_cb, NULL, NULL)){
log_e("event_handler_instance_register for WIFI_PROV_EVENT Failed!");
return false;
}
return true;
}
bool tcpipInit(){
static bool initialized = false;
if(!initialized){
initialized = true;
#if CONFIG_IDF_TARGET_ESP32
uint8_t mac[8];
if(esp_efuse_mac_get_default(mac) == ESP_OK){
esp_base_mac_addr_set(mac);
}
#endif
initialized = esp_netif_init() == ESP_OK;
if(initialized){
initialized = _start_network_event_task();
} else {
log_e("esp_netif_init failed!");
}
}
return initialized;
}
/*
* WiFi INIT
* */
static bool lowLevelInitDone = false;
bool WiFiGenericClass::_wifiUseStaticBuffers = false;
bool WiFiGenericClass::useStaticBuffers(){
return _wifiUseStaticBuffers;
}
void WiFiGenericClass::useStaticBuffers(bool bufferMode){
if (lowLevelInitDone) {
log_w("WiFi already started. Call WiFi.mode(WIFI_MODE_NULL) before setting Static Buffer Mode.");
}
_wifiUseStaticBuffers = bufferMode;
}
// Temporary fix to ensure that CDC+JTAG stay on on ESP32-C3
#if CONFIG_IDF_TARGET_ESP32C3
extern "C" void phy_bbpll_en_usb(bool en);
#endif
bool wifiLowLevelInit(bool persistent){
if(!lowLevelInitDone){
lowLevelInitDone = true;
if(!tcpipInit()){
lowLevelInitDone = false;
return lowLevelInitDone;
}
if(esp_netifs[ESP_IF_WIFI_AP] == NULL){
esp_netifs[ESP_IF_WIFI_AP] = esp_netif_create_default_wifi_ap();
}
if(esp_netifs[ESP_IF_WIFI_STA] == NULL){
esp_netifs[ESP_IF_WIFI_STA] = esp_netif_create_default_wifi_sta();
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
if(!WiFiGenericClass::useStaticBuffers()) {
cfg.static_tx_buf_num = 0;
cfg.dynamic_tx_buf_num = 32;
cfg.tx_buf_type = 1;
cfg.cache_tx_buf_num = 4; // can't be zero!
cfg.static_rx_buf_num = 4;
cfg.dynamic_rx_buf_num = 32;
}
esp_err_t err = esp_wifi_init(&cfg);
if(err){
log_e("esp_wifi_init %d", err);
lowLevelInitDone = false;
return lowLevelInitDone;
}
// Temporary fix to ensure that CDC+JTAG stay on on ESP32-C3
#if CONFIG_IDF_TARGET_ESP32C3
phy_bbpll_en_usb(true);
#endif
if(!persistent){
lowLevelInitDone = esp_wifi_set_storage(WIFI_STORAGE_RAM) == ESP_OK;
}
if(lowLevelInitDone){
arduino_event_t arduino_event;
arduino_event.event_id = ARDUINO_EVENT_WIFI_READY;
postArduinoEvent(&arduino_event);
}
}
return lowLevelInitDone;
}
static bool wifiLowLevelDeinit(){
if(lowLevelInitDone){
lowLevelInitDone = !(esp_wifi_deinit() == ESP_OK);
}
return !lowLevelInitDone;
}
static bool _esp_wifi_started = false;
static bool espWiFiStart(){
if(_esp_wifi_started){
return true;
}
_esp_wifi_started = true;
esp_err_t err = esp_wifi_start();
if (err != ESP_OK) {
_esp_wifi_started = false;
log_e("esp_wifi_start %d", err);
return _esp_wifi_started;
}
return _esp_wifi_started;
}
static bool espWiFiStop(){
esp_err_t err;
if(!_esp_wifi_started){
return true;
}
_esp_wifi_started = false;
err = esp_wifi_stop();
if(err){
log_e("Could not stop WiFi! %d", err);
_esp_wifi_started = true;
return false;
}
return wifiLowLevelDeinit();
}
// -----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
typedef struct WiFiEventCbList {
static wifi_event_id_t current_id;
wifi_event_id_t id;
WiFiEventCb cb;
WiFiEventFuncCb fcb;
WiFiEventSysCb scb;
arduino_event_id_t event;
WiFiEventCbList() : id(current_id++), cb(NULL), fcb(NULL), scb(NULL), event(ARDUINO_EVENT_WIFI_READY) {}
} WiFiEventCbList_t;
wifi_event_id_t WiFiEventCbList::current_id = 1;
// arduino dont like std::vectors move static here
static std::vector<WiFiEventCbList_t> cbEventList;
bool WiFiGenericClass::_persistent = true;
bool WiFiGenericClass::_long_range = false;
wifi_mode_t WiFiGenericClass::_forceSleepLastMode = WIFI_MODE_NULL;
#if CONFIG_IDF_TARGET_ESP32S2
wifi_ps_type_t WiFiGenericClass::_sleepEnabled = WIFI_PS_NONE;
#else
wifi_ps_type_t WiFiGenericClass::_sleepEnabled = WIFI_PS_MIN_MODEM;
#endif
WiFiGenericClass::WiFiGenericClass()
{
}
/**
* @brief Convert wifi_err_reason_t to a string.
* @param [in] reason The reason to be converted.
* @return A string representation of the error code.
* @note: wifi_err_reason_t values as of Mar 2023 (arduino-esp32 r2.0.7) are: (1-39, 46-51, 67-68, 200-208) and are defined in /tools/sdk/esp32/include/esp_wifi/include/esp_wifi_types.h.
*/
const char * WiFiGenericClass::disconnectReasonName(wifi_err_reason_t reason) {
switch(reason) {
//ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2,0,7)
case WIFI_REASON_UNSPECIFIED: return "UNSPECIFIED";
case WIFI_REASON_AUTH_EXPIRE: return "AUTH_EXPIRE";
case WIFI_REASON_AUTH_LEAVE: return "AUTH_LEAVE";
case WIFI_REASON_ASSOC_EXPIRE: return "ASSOC_EXPIRE";
case WIFI_REASON_ASSOC_TOOMANY: return "ASSOC_TOOMANY";
case WIFI_REASON_NOT_AUTHED: return "NOT_AUTHED";
case WIFI_REASON_NOT_ASSOCED: return "NOT_ASSOCED";
case WIFI_REASON_ASSOC_LEAVE: return "ASSOC_LEAVE";
case WIFI_REASON_ASSOC_NOT_AUTHED: return "ASSOC_NOT_AUTHED";
case WIFI_REASON_DISASSOC_PWRCAP_BAD: return "DISASSOC_PWRCAP_BAD";
case WIFI_REASON_DISASSOC_SUPCHAN_BAD: return "DISASSOC_SUPCHAN_BAD";
case WIFI_REASON_BSS_TRANSITION_DISASSOC: return "BSS_TRANSITION_DISASSOC";
case WIFI_REASON_IE_INVALID: return "IE_INVALID";
case WIFI_REASON_MIC_FAILURE: return "MIC_FAILURE";
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: return "4WAY_HANDSHAKE_TIMEOUT";
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT: return "GROUP_KEY_UPDATE_TIMEOUT";
case WIFI_REASON_IE_IN_4WAY_DIFFERS: return "IE_IN_4WAY_DIFFERS";
case WIFI_REASON_GROUP_CIPHER_INVALID: return "GROUP_CIPHER_INVALID";
case WIFI_REASON_PAIRWISE_CIPHER_INVALID: return "PAIRWISE_CIPHER_INVALID";
case WIFI_REASON_AKMP_INVALID: return "AKMP_INVALID";
case WIFI_REASON_UNSUPP_RSN_IE_VERSION: return "UNSUPP_RSN_IE_VERSION";
case WIFI_REASON_INVALID_RSN_IE_CAP: return "INVALID_RSN_IE_CAP";
case WIFI_REASON_802_1X_AUTH_FAILED: return "802_1X_AUTH_FAILED";
case WIFI_REASON_CIPHER_SUITE_REJECTED: return "CIPHER_SUITE_REJECTED";
case WIFI_REASON_TDLS_PEER_UNREACHABLE: return "TDLS_PEER_UNREACHABLE";
case WIFI_REASON_TDLS_UNSPECIFIED: return "TDLS_UNSPECIFIED";
case WIFI_REASON_SSP_REQUESTED_DISASSOC: return "SSP_REQUESTED_DISASSOC";
case WIFI_REASON_NO_SSP_ROAMING_AGREEMENT: return "NO_SSP_ROAMING_AGREEMENT";
case WIFI_REASON_BAD_CIPHER_OR_AKM: return "BAD_CIPHER_OR_AKM";
case WIFI_REASON_NOT_AUTHORIZED_THIS_LOCATION: return "NOT_AUTHORIZED_THIS_LOCATION";
case WIFI_REASON_SERVICE_CHANGE_PERCLUDES_TS: return "SERVICE_CHANGE_PERCLUDES_TS";
case WIFI_REASON_UNSPECIFIED_QOS: return "UNSPECIFIED_QOS";
case WIFI_REASON_NOT_ENOUGH_BANDWIDTH: return "NOT_ENOUGH_BANDWIDTH";
case WIFI_REASON_MISSING_ACKS: return "MISSING_ACKS";
case WIFI_REASON_EXCEEDED_TXOP: return "EXCEEDED_TXOP";
case WIFI_REASON_STA_LEAVING: return "STA_LEAVING";
case WIFI_REASON_END_BA: return "END_BA";
case WIFI_REASON_UNKNOWN_BA: return "UNKNOWN_BA";
case WIFI_REASON_TIMEOUT: return "TIMEOUT";
case WIFI_REASON_PEER_INITIATED: return "PEER_INITIATED";
case WIFI_REASON_AP_INITIATED: return "AP_INITIATED";
case WIFI_REASON_INVALID_FT_ACTION_FRAME_COUNT: return "INVALID_FT_ACTION_FRAME_COUNT";
case WIFI_REASON_INVALID_PMKID: return "INVALID_PMKID";
case WIFI_REASON_INVALID_MDE: return "INVALID_MDE";
case WIFI_REASON_INVALID_FTE: return "INVALID_FTE";
case WIFI_REASON_TRANSMISSION_LINK_ESTABLISH_FAILED: return "TRANSMISSION_LINK_ESTABLISH_FAILED";
case WIFI_REASON_ALTERATIVE_CHANNEL_OCCUPIED: return "ALTERATIVE_CHANNEL_OCCUPIED";
case WIFI_REASON_BEACON_TIMEOUT: return "BEACON_TIMEOUT";
case WIFI_REASON_NO_AP_FOUND: return "NO_AP_FOUND";
case WIFI_REASON_AUTH_FAIL: return "AUTH_FAIL";
case WIFI_REASON_ASSOC_FAIL: return "ASSOC_FAIL";
case WIFI_REASON_HANDSHAKE_TIMEOUT: return "HANDSHAKE_TIMEOUT";
case WIFI_REASON_CONNECTION_FAIL: return "CONNECTION_FAIL";
case WIFI_REASON_AP_TSF_RESET: return "AP_TSF_RESET";
case WIFI_REASON_ROAMING: return "ROAMING";
default: return "";
}
}
/**
* @brief Convert arduino_event_id_t to a C string.
* @param [in] id The event id to be converted.
* @return A string representation of the event id.
* @note: arduino_event_id_t values as of Mar 2023 (arduino-esp32 r2.0.7) are: 0-39 (ARDUINO_EVENT_MAX=40) and are defined in WiFiGeneric.h.
*/
const char * WiFiGenericClass::eventName(arduino_event_id_t id) {
switch(id) {
case ARDUINO_EVENT_WIFI_READY: return "WIFI_READY";
case ARDUINO_EVENT_WIFI_SCAN_DONE: return "SCAN_DONE";
case ARDUINO_EVENT_WIFI_STA_START: return "STA_START";
case ARDUINO_EVENT_WIFI_STA_STOP: return "STA_STOP";
case ARDUINO_EVENT_WIFI_STA_CONNECTED: return "STA_CONNECTED";
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: return "STA_DISCONNECTED";
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: return "STA_AUTHMODE_CHANGE";
case ARDUINO_EVENT_WIFI_STA_GOT_IP: return "STA_GOT_IP";
case ARDUINO_EVENT_WIFI_STA_GOT_IP6: return "STA_GOT_IP6";
case ARDUINO_EVENT_WIFI_STA_LOST_IP: return "STA_LOST_IP";
case ARDUINO_EVENT_WIFI_AP_START: return "AP_START";
case ARDUINO_EVENT_WIFI_AP_STOP: return "AP_STOP";
case ARDUINO_EVENT_WIFI_AP_STACONNECTED: return "AP_STACONNECTED";
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: return "AP_STADISCONNECTED";
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: return "AP_STAIPASSIGNED";
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: return "AP_PROBEREQRECVED";
case ARDUINO_EVENT_WIFI_AP_GOT_IP6: return "AP_GOT_IP6";
case ARDUINO_EVENT_WIFI_FTM_REPORT: return "FTM_REPORT";
case ARDUINO_EVENT_ETH_START: return "ETH_START";
case ARDUINO_EVENT_ETH_STOP: return "ETH_STOP";
case ARDUINO_EVENT_ETH_CONNECTED: return "ETH_CONNECTED";
case ARDUINO_EVENT_ETH_DISCONNECTED: return "ETH_DISCONNECTED";
case ARDUINO_EVENT_ETH_GOT_IP: return "ETH_GOT_IP";
case ARDUINO_EVENT_ETH_GOT_IP6: return "ETH_GOT_IP6";
case ARDUINO_EVENT_WPS_ER_SUCCESS: return "WPS_ER_SUCCESS";
case ARDUINO_EVENT_WPS_ER_FAILED: return "WPS_ER_FAILED";
case ARDUINO_EVENT_WPS_ER_TIMEOUT: return "WPS_ER_TIMEOUT";
case ARDUINO_EVENT_WPS_ER_PIN: return "WPS_ER_PIN";
case ARDUINO_EVENT_WPS_ER_PBC_OVERLAP: return "WPS_ER_PBC_OVERLAP";
case ARDUINO_EVENT_SC_SCAN_DONE: return "SC_SCAN_DONE";
case ARDUINO_EVENT_SC_FOUND_CHANNEL: return "SC_FOUND_CHANNEL";
case ARDUINO_EVENT_SC_GOT_SSID_PSWD: return "SC_GOT_SSID_PSWD";
case ARDUINO_EVENT_SC_SEND_ACK_DONE: return "SC_SEND_ACK_DONE";
case ARDUINO_EVENT_PROV_INIT: return "PROV_INIT";
case ARDUINO_EVENT_PROV_DEINIT: return "PROV_DEINIT";
case ARDUINO_EVENT_PROV_START: return "PROV_START";
case ARDUINO_EVENT_PROV_END: return "PROV_END";
case ARDUINO_EVENT_PROV_CRED_RECV: return "PROV_CRED_RECV";
case ARDUINO_EVENT_PROV_CRED_FAIL: return "PROV_CRED_FAIL";
case ARDUINO_EVENT_PROV_CRED_SUCCESS: return "PROV_CRED_SUCCESS";
default: return "";
}
}
const char * WiFiGenericClass::getHostname()
{
return get_esp_netif_hostname();
}
bool WiFiGenericClass::setHostname(const char * hostname)
{
set_esp_netif_hostname(hostname);
return true;
}
int WiFiGenericClass::setStatusBits(int bits){
if(!_arduino_event_group){
return 0;
}
return xEventGroupSetBits(_arduino_event_group, bits);
}
int WiFiGenericClass::clearStatusBits(int bits){
if(!_arduino_event_group){
return 0;
}
return xEventGroupClearBits(_arduino_event_group, bits);
}
int WiFiGenericClass::getStatusBits(){
if(!_arduino_event_group){
return 0;
}
return xEventGroupGetBits(_arduino_event_group);
}
int WiFiGenericClass::waitStatusBits(int bits, uint32_t timeout_ms){
if(!_arduino_event_group){
return 0;
}
return xEventGroupWaitBits(
_arduino_event_group, // The event group being tested.
bits, // The bits within the event group to wait for.
pdFALSE, // BIT_0 and BIT_4 should be cleared before returning.
pdTRUE, // Don't wait for both bits, either bit will do.
timeout_ms / portTICK_PERIOD_MS ) & bits; // Wait a maximum of 100ms for either bit to be set.
}
/**
* set callback function
* @param cbEvent WiFiEventCb
* @param event optional filter (WIFI_EVENT_MAX is all events)
*/
wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventCb cbEvent, arduino_event_id_t event)
{
if(!cbEvent) {
return 0;
}
WiFiEventCbList_t newEventHandler;
newEventHandler.cb = cbEvent;
newEventHandler.fcb = NULL;
newEventHandler.scb = NULL;
newEventHandler.event = event;
cbEventList.push_back(newEventHandler);
return newEventHandler.id;
}
wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventFuncCb cbEvent, arduino_event_id_t event)
{
if(!cbEvent) {
return 0;
}
WiFiEventCbList_t newEventHandler;
newEventHandler.cb = NULL;
newEventHandler.fcb = cbEvent;
newEventHandler.scb = NULL;
newEventHandler.event = event;
cbEventList.push_back(newEventHandler);
return newEventHandler.id;
}
wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, arduino_event_id_t event)
{
if(!cbEvent) {
return 0;
}
WiFiEventCbList_t newEventHandler;
newEventHandler.cb = NULL;
newEventHandler.fcb = NULL;
newEventHandler.scb = cbEvent;
newEventHandler.event = event;
cbEventList.push_back(newEventHandler);
return newEventHandler.id;
}
/**
* removes a callback form event handler
* @param cbEvent WiFiEventCb
* @param event optional filter (WIFI_EVENT_MAX is all events)
*/
void WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, arduino_event_id_t event)
{
if(!cbEvent) {
return;
}
for(uint32_t i = 0; i < cbEventList.size(); i++) {
WiFiEventCbList_t entry = cbEventList[i];
if(entry.cb == cbEvent && entry.event == event) {