-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathWiFiSTA.cpp
718 lines (628 loc) · 18.5 KB
/
WiFiSTA.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
/*
WiFiSTA.cpp - WiFi library for esp32
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"
#include "WiFiSTA.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_loop.h>
#include <esp32-hal.h>
#include <lwip/ip_addr.h>
#include "lwip/err.h"
#include "lwip/dns.h"
#include <esp_smartconfig.h>
#include <tcpip_adapter.h>
}
// -----------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------- Private functions ------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
static bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs);
/**
* compare two STA configurations
* @param lhs station_config
* @param rhs station_config
* @return equal
*/
static bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs)
{
if(memcmp(&lhs, &rhs, sizeof(wifi_config_t)) != 0) {
return false;
}
return true;
}
// -----------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------- STA function -----------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
bool WiFiSTAClass::_autoReconnect = true;
bool WiFiSTAClass::_useStaticIp = false;
static wl_status_t _sta_status = WL_NO_SHIELD;
static EventGroupHandle_t _sta_status_group = NULL;
void WiFiSTAClass::_setStatus(wl_status_t status)
{
if(!_sta_status_group){
_sta_status_group = xEventGroupCreate();
if(!_sta_status_group){
log_e("STA Status Group Create Failed!");
_sta_status = status;
return;
}
}
xEventGroupClearBits(_sta_status_group, 0x00FFFFFF);
xEventGroupSetBits(_sta_status_group, status);
}
/**
* Return Connection status.
* @return one of the value defined in wl_status_t
*
*/
wl_status_t WiFiSTAClass::status()
{
if(!_sta_status_group){
return _sta_status;
}
return (wl_status_t)xEventGroupClearBits(_sta_status_group, 0);
}
/**
* Start Wifi connection
* if passphrase is set the most secure supported mode will be automatically selected
* @param ssid const char* Pointer to the SSID string.
* @param passphrase const char * Optional. Passphrase. Valid characters in a passphrase must be between ASCII 32-126 (decimal).
* @param bssid uint8_t[6] Optional. BSSID / MAC of AP
* @param channel Optional. Channel of AP
* @param connect Optional. call connect
* @return
*/
wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid, bool connect)
{
if(!WiFi.enableSTA(true)) {
log_e("STA enable failed!");
return WL_CONNECT_FAILED;
}
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
log_e("SSID too long or missing!");
return WL_CONNECT_FAILED;
}
if(passphrase && strlen(passphrase) > 64) {
log_e("passphrase too long!");
return WL_CONNECT_FAILED;
}
wifi_config_t conf;
memset(&conf, 0, sizeof(wifi_config_t));
strcpy(reinterpret_cast<char*>(conf.sta.ssid), ssid);
if(passphrase) {
if (strlen(passphrase) == 64){ // it's not a passphrase, is the PSK
memcpy(reinterpret_cast<char*>(conf.sta.password), passphrase, 64);
} else {
strcpy(reinterpret_cast<char*>(conf.sta.password), passphrase);
}
}
if(bssid) {
conf.sta.bssid_set = 1;
memcpy((void *) &conf.sta.bssid[0], (void *) bssid, 6);
}
if(channel > 0 && channel <= 13) {
conf.sta.channel = channel;
}
wifi_config_t current_conf;
esp_wifi_get_config(WIFI_IF_STA, ¤t_conf);
if(!sta_config_equal(current_conf, conf)) {
if(esp_wifi_disconnect()){
log_e("disconnect failed!");
return WL_CONNECT_FAILED;
}
esp_wifi_set_config(WIFI_IF_STA, &conf);
} else if(status() == WL_CONNECTED){
return WL_CONNECTED;
} else {
esp_wifi_set_config(WIFI_IF_STA, &conf);
}
if(!_useStaticIp) {
if(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA) == ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED){
log_e("dhcp client start failed!");
return WL_CONNECT_FAILED;
}
} else {
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
}
if(connect && esp_wifi_connect()) {
log_e("connect failed!");
return WL_CONNECT_FAILED;
}
return status();
}
wl_status_t WiFiSTAClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid, bool connect)
{
return begin((const char*) ssid, (const char*) passphrase, channel, bssid, connect);
}
/**
* Use to connect to SDK config.
* @return wl_status_t
*/
wl_status_t WiFiSTAClass::begin()
{
if(!WiFi.enableSTA(true)) {
log_e("STA enable failed!");
return WL_CONNECT_FAILED;
}
wifi_config_t current_conf;
if(esp_wifi_get_config(WIFI_IF_STA, ¤t_conf) != ESP_OK || esp_wifi_set_config(WIFI_IF_STA, ¤t_conf) != ESP_OK) {
log_e("config failed");
return WL_CONNECT_FAILED;
}
if(!_useStaticIp) {
if(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA) == ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED){
log_e("dhcp client start failed!");
return WL_CONNECT_FAILED;
}
} else {
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
}
if(status() != WL_CONNECTED && esp_wifi_connect()){
log_e("connect failed!");
return WL_CONNECT_FAILED;
}
return status();
}
/**
* will force a disconnect an then start reconnecting to AP
* @return ok
*/
bool WiFiSTAClass::reconnect()
{
if(WiFi.getMode() & WIFI_MODE_STA) {
if(esp_wifi_disconnect() == ESP_OK) {
return esp_wifi_connect() == ESP_OK;
}
}
return false;
}
/**
* Disconnect from the network
* @param wifioff
* @return one value of wl_status_t enum
*/
bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap)
{
wifi_config_t conf;
if(WiFi.getMode() & WIFI_MODE_STA){
if(eraseap){
memset(&conf, 0, sizeof(wifi_config_t));
if(esp_wifi_set_config(WIFI_IF_STA, &conf)){
log_e("clear config failed!");
}
}
if(esp_wifi_disconnect()){
log_e("disconnect failed!");
return false;
}
if(wifioff) {
return WiFi.enableSTA(false);
}
return true;
}
return false;
}
/**
* Change IP configuration settings disabling the dhcp client
* @param local_ip Static ip configuration
* @param gateway Static gateway configuration
* @param subnet Static Subnet mask
* @param dns1 Static DNS server 1
* @param dns2 Static DNS server 2
*/
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
{
esp_err_t err = ESP_OK;
if(!WiFi.enableSTA(true)) {
return false;
}
tcpip_adapter_ip_info_t info;
if(local_ip != (uint32_t)0x00000000){
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);
} else {
info.ip.addr = 0;
info.gw.addr = 0;
info.netmask.addr = 0;
}
err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){
log_e("DHCP could not be stopped! Error: %d", err);
return false;
}
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &info);
if(err != ERR_OK){
log_e("STA IP could not be configured! Error: %d", err);
return false;
}
if(info.ip.addr){
_useStaticIp = true;
} else {
err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
if(err == ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED){
log_e("dhcp client start failed!");
return false;
}
_useStaticIp = false;
}
ip_addr_t d;
d.type = IPADDR_TYPE_V4;
if(dns1 != (uint32_t)0x00000000) {
// Set DNS1-Server
d.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
dns_setserver(0, &d);
}
if(dns2 != (uint32_t)0x00000000) {
// Set DNS2-Server
d.u_addr.ip4.addr = static_cast<uint32_t>(dns2);
dns_setserver(1, &d);
}
return true;
}
/**
* is STA interface connected?
* @return true if STA is connected to an AD
*/
bool WiFiSTAClass::isConnected()
{
return (status() == WL_CONNECTED);
}
/**
* Setting the ESP32 station to connect to the AP (which is recorded)
* automatically or not when powered on. Enable auto-connect by default.
* @param autoConnect bool
* @return if saved
*/
bool WiFiSTAClass::setAutoConnect(bool autoConnect)
{
/*bool ret;
ret = esp_wifi_set_auto_connect(autoConnect);
return ret;*/
return false;//now deprecated
}
/**
* Checks if ESP32 station mode will connect to AP
* automatically or not when it is powered on.
* @return auto connect
*/
bool WiFiSTAClass::getAutoConnect()
{
/*bool autoConnect;
esp_wifi_get_auto_connect(&autoConnect);
return autoConnect;*/
return false;//now deprecated
}
bool WiFiSTAClass::setAutoReconnect(bool autoReconnect)
{
_autoReconnect = autoReconnect;
return true;
}
bool WiFiSTAClass::getAutoReconnect()
{
return _autoReconnect;
}
/**
* Wait for WiFi connection to reach a result
* returns the status reached or disconnect if STA is off
* @return wl_status_t
*/
uint8_t WiFiSTAClass::waitForConnectResult()
{
//1 and 3 have STA enabled
if((WiFiGenericClass::getMode() & WIFI_MODE_STA) == 0) {
return WL_DISCONNECTED;
}
int i = 0;
while((!status() || status() >= WL_DISCONNECTED) && i++ < 100) {
delay(100);
}
return status();
}
/**
* Get the station interface IP address.
* @return IPAddress station IP
*/
IPAddress WiFiSTAClass::localIP()
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return IPAddress();
}
tcpip_adapter_ip_info_t ip;
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
return IPAddress(ip.ip.addr);
}
/**
* Get the station interface MAC address.
* @param mac pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
* @return pointer to uint8_t *
*/
uint8_t* WiFiSTAClass::macAddress(uint8_t* mac)
{
if(WiFiGenericClass::getMode() != WIFI_MODE_NULL){
esp_wifi_get_mac(WIFI_IF_STA, mac);
}
else{
esp_read_mac(mac, ESP_MAC_WIFI_STA);
}
return mac;
}
/**
* Get the station interface MAC address.
* @return String mac
*/
String WiFiSTAClass::macAddress(void)
{
uint8_t mac[6];
char macStr[18] = { 0 };
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
esp_read_mac(mac, ESP_MAC_WIFI_STA);
}
else{
esp_wifi_get_mac(WIFI_IF_STA, mac);
}
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr);
}
/**
* Get the interface subnet mask address.
* @return IPAddress subnetMask
*/
IPAddress WiFiSTAClass::subnetMask()
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return IPAddress();
}
tcpip_adapter_ip_info_t ip;
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
return IPAddress(ip.netmask.addr);
}
/**
* Get the gateway ip address.
* @return IPAddress gatewayIP
*/
IPAddress WiFiSTAClass::gatewayIP()
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return IPAddress();
}
tcpip_adapter_ip_info_t ip;
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
return IPAddress(ip.gw.addr);
}
/**
* Get the DNS ip address.
* @param dns_no
* @return IPAddress DNS Server IP
*/
IPAddress WiFiSTAClass::dnsIP(uint8_t dns_no)
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return IPAddress();
}
ip_addr_t dns_ip = dns_getserver(dns_no);
return IPAddress(dns_ip.u_addr.ip4.addr);
}
/**
* Return the current SSID associated with the network
* @return SSID
*/
String WiFiSTAClass::SSID() const
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return String();
}
wifi_ap_record_t info;
if(!esp_wifi_sta_get_ap_info(&info)) {
return String(reinterpret_cast<char*>(info.ssid));
}
return String();
}
/**
* Return the current pre shared key associated with the network
* @return psk string
*/
String WiFiSTAClass::psk() const
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return String();
}
wifi_config_t conf;
esp_wifi_get_config(WIFI_IF_STA, &conf);
return String(reinterpret_cast<char*>(conf.sta.password));
}
/**
* Return the current bssid / mac associated with the network if configured
* @return bssid uint8_t *
*/
uint8_t* WiFiSTAClass::BSSID(void)
{
static uint8_t bssid[6];
wifi_ap_record_t info;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return NULL;
}
if(!esp_wifi_sta_get_ap_info(&info)) {
memcpy(bssid, info.bssid, 6);
return reinterpret_cast<uint8_t*>(bssid);
}
return NULL;
}
/**
* Return the current bssid / mac associated with the network if configured
* @return String bssid mac
*/
String WiFiSTAClass::BSSIDstr(void)
{
uint8_t* bssid = BSSID();
if(!bssid){
return String();
}
char mac[18] = { 0 };
sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
return String(mac);
}
/**
* Return the current network RSSI.
* @return RSSI value
*/
int8_t WiFiSTAClass::RSSI(void)
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return 0;
}
wifi_ap_record_t info;
if(!esp_wifi_sta_get_ap_info(&info)) {
return info.rssi;
}
return 0;
}
/**
* Get the station interface Host name.
* @return char array hostname
*/
const char * WiFiSTAClass::getHostname()
{
const char * hostname = NULL;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return hostname;
}
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname)){
return NULL;
}
return hostname;
}
/**
* Set the station interface Host name.
* @param hostname pointer to const string
* @return true on success
*/
bool WiFiSTAClass::setHostname(const char * hostname)
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return false;
}
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname) == 0;
}
/**
* Enable IPv6 on the station interface.
* @return true on success
*/
bool WiFiSTAClass::enableIpV6()
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return false;
}
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA) == 0;
}
/**
* Get the station interface IPv6 address.
* @return IPv6Address
*/
IPv6Address WiFiSTAClass::localIPv6()
{
static ip6_addr_t addr;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return IPv6Address();
}
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_STA, &addr)){
return IPv6Address();
}
return IPv6Address(addr.addr);
}
bool WiFiSTAClass::_smartConfigStarted = false;
bool WiFiSTAClass::_smartConfigDone = false;
bool WiFiSTAClass::beginSmartConfig() {
if (_smartConfigStarted) {
return false;
}
if (!WiFi.mode(WIFI_STA)) {
return false;
}
esp_wifi_disconnect();
esp_err_t err;
err = esp_smartconfig_start(reinterpret_cast<sc_callback_t>(&WiFiSTAClass::_smartConfigCallback), 1);
if (err == ESP_OK) {
_smartConfigStarted = true;
_smartConfigDone = false;
return true;
}
return false;
}
bool WiFiSTAClass::stopSmartConfig() {
if (!_smartConfigStarted) {
return true;
}
if (esp_smartconfig_stop() == ESP_OK) {
_smartConfigStarted = false;
return true;
}
return false;
}
bool WiFiSTAClass::smartConfigDone() {
if (!_smartConfigStarted) {
return false;
}
return _smartConfigDone;
}
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
const char * sc_status_strings[] = {
"WAIT",
"FIND_CHANNEL",
"GETTING_SSID_PSWD",
"LINK",
"LINK_OVER"
};
const char * sc_type_strings[] = {
"ESPTOUCH",
"AIRKISS",
"ESPTOUCH_AIRKISS"
};
#endif
void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) {
smartconfig_status_t status = (smartconfig_status_t) st;
log_d("Status: %s", sc_status_strings[st % 5]);
if (status == SC_STATUS_GETTING_SSID_PSWD) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
smartconfig_type_t * type = (smartconfig_type_t *)result;
log_d("Type: %s", sc_type_strings[*type % 3]);
#endif
} else if (status == SC_STATUS_LINK) {
wifi_sta_config_t *sta_conf = reinterpret_cast<wifi_sta_config_t *>(result);
log_d("SSID: %s", (char *)(sta_conf->ssid));
sta_conf->bssid_set = 0;
esp_wifi_set_config(WIFI_IF_STA, (wifi_config_t *)sta_conf);
esp_wifi_connect();
_smartConfigDone = true;
} else if (status == SC_STATUS_LINK_OVER) {
if(result){
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
ip4_addr_t * ip = (ip4_addr_t *)result;
log_d("Sender IP: " IPSTR, IP2STR(ip));
#endif
}
WiFi.stopSmartConfig();
}
}