-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
WiFiSTA.cpp
484 lines (418 loc) · 12 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
/*
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"
#if SOC_WIFI_SUPPORTED || CONFIG_ESP_WIFI_REMOTE_ENABLED
#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 <esp32-hal.h>
#include <lwip/ip_addr.h>
#include "lwip/err.h"
#include "lwip/dns.h"
#include <esp_smartconfig.h>
#include <esp_netif.h>
#include "esp_mac.h"
#if __has_include("esp_eap_client.h")
#include "esp_eap_client.h"
#else
#include "esp_wpa2.h"
#endif
// -----------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------- STA function -----------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
/**
* Return Connection status.
* @return one of the value defined in wl_status_t
*
*/
wl_status_t WiFiSTAClass::status() {
return STA.status();
}
#if CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT
wl_status_t WiFiSTAClass::begin(
const char *wpa2_ssid, wpa2_auth_method_t method, const char *wpa2_identity, const char *wpa2_username, const char *wpa2_password, const char *ca_pem,
const char *client_crt, const char *client_key, int ttls_phase2_type, int32_t channel, const uint8_t *bssid, bool connect
) {
if (!STA.begin()) {
return WL_CONNECT_FAILED;
}
if (!STA.connect(wpa2_ssid, method, wpa2_identity, wpa2_username, wpa2_password, ca_pem, client_crt, client_key, ttls_phase2_type, channel, bssid, connect)) {
return WL_CONNECT_FAILED;
}
return STA.status();
}
#endif /* CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT */
wl_status_t WiFiSTAClass::begin(const char *ssid, const char *passphrase, int32_t channel, const uint8_t *bssid, bool connect) {
if (!STA.begin()) {
return WL_CONNECT_FAILED;
}
if (!STA.connect(ssid, passphrase, channel, bssid, connect)) {
return WL_CONNECT_FAILED;
}
return STA.status();
}
/**
* Use to connect to SDK config.
* @return wl_status_t
*/
wl_status_t WiFiSTAClass::begin() {
if (!STA.begin(true)) {
return WL_CONNECT_FAILED;
}
return STA.status();
}
/**
* will force a disconnect and then start reconnecting to AP
* @return true when successful
*/
bool WiFiSTAClass::reconnect() {
return STA.reconnect();
}
/**
* Disconnect from the network.
* @param wifioff `true` to turn the Wi-Fi radio off.
* @param eraseap `true` to erase the AP configuration from the NVS memory.
* @return `true` when successful.
*/
bool WiFiSTAClass::disconnectAsync(bool wifioff, bool eraseap) {
return disconnect(wifioff, eraseap, 0);
}
/**
* Disconnect from the network.
* @param wifioff `true` to turn the Wi-Fi radio off.
* @param eraseap `true` to erase the AP configuration from the NVS memory.
* @param timeoutLength timeout to wait for status change
* @return `true` when successful.
*/
bool WiFiSTAClass::disconnect(bool wifioff, bool eraseap, unsigned long timeoutLength) {
if (!STA.disconnect(eraseap, timeoutLength)) {
return false;
}
if (wifioff) {
return STA.end();
}
return true;
}
/**
* @brief Reset WiFi settings in NVS to default values.
*
* This function will reset settings made using the following APIs:
* - esp_wifi_set_bandwidth,
* - esp_wifi_set_protocol,
* - esp_wifi_set_config related
* - esp_wifi_set_mode
*
* @return true if erase succeeded
* @note: Resets SSID, password, protocol, mode, etc.
* These settings are maintained by WiFi driver in IDF.
* WiFi driver must be initialized.
*/
bool WiFiSTAClass::eraseAP(void) {
return STA.erase();
}
/**
* 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) {
// handle Arduino ordering of parameters: ip, dns, gw, subnet
if (local_ip.type() == IPv4 && local_ip != INADDR_NONE && subnet[0] != 255) {
IPAddress tmp = dns1;
dns1 = gateway;
gateway = subnet;
subnet = (tmp != INADDR_NONE) ? tmp : IPAddress(255, 255, 255, 0);
}
return STA.begin() && STA.config(local_ip, gateway, subnet, dns1, dns2);
}
bool WiFiSTAClass::config(IPAddress local_ip, IPAddress dns) {
if (local_ip == INADDR_NONE) {
return config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
}
if (local_ip.type() != IPv4) {
return false;
}
IPAddress gw(local_ip);
gw[3] = 1;
if (dns == INADDR_NONE) {
dns = gw;
}
return config(local_ip, gw, IPAddress(255, 255, 255, 0), dns);
}
/**
* Change DNS server for static IP configuration
* @param dns1 Static DNS server 1
* @param dns2 Static DNS server 2 (optional)
*/
bool WiFiSTAClass::setDNS(IPAddress dns1, IPAddress dns2) {
return STA.begin() && STA.dnsIP(0, dns1) && STA.dnsIP(1, dns2);
}
/**
* Sets the working bandwidth of the STA mode
* @param m wifi_bandwidth_t
*/
bool WiFiSTAClass::bandwidth(wifi_bandwidth_t bandwidth) {
return STA.bandwidth(bandwidth);
}
/**
* is STA interface connected?
* @return true if STA is connected to an AP
*/
bool WiFiSTAClass::isConnected() {
return STA.connected() && STA.hasIP();
}
/**
* Set the minimum security for AP to be considered connectable.
* Must be called before WiFi.begin().
* @param minSecurity wifi_auth_mode_t
*/
void WiFiSTAClass::setMinSecurity(wifi_auth_mode_t minSecurity) {
return STA.setMinSecurity(minSecurity);
}
/**
* Set the way that AP is chosen.
* First SSID match[WIFI_FAST_SCAN] or Sorted[WIFI_ALL_CHANNEL_SCAN] (RSSI or Security)
* Must be called before WiFi.begin()
* @param scanMethod wifi_scan_method_t
*/
void WiFiSTAClass::setScanMethod(wifi_scan_method_t scanMethod) {
return STA.setScanMethod(scanMethod);
}
/**
* Set the way that AP is sorted. (requires scanMethod WIFI_ALL_CHANNEL_SCAN)
* By SSID[WIFI_CONNECT_AP_BY_SIGNAL] or Security[WIFI_CONNECT_AP_BY_SECURITY]
* Must be called before WiFi.begin()
* @param sortMethod wifi_sort_method_t
*/
void WiFiSTAClass::setSortMethod(wifi_sort_method_t sortMethod) {
return STA.setSortMethod(sortMethod);
}
/**
* Function used to set the automatic reconnection if the connection is lost.
* @param autoReconnect `true` to enable this option.
* @return true
*/
bool WiFiSTAClass::setAutoReconnect(bool autoReconnect) {
return STA.setAutoReconnect(autoReconnect);
}
/**
* Function used to get the automatic reconnection if the connection is lost.
* @return The function will return `true` if this setting is enabled.
*/
bool WiFiSTAClass::getAutoReconnect() {
return STA.getAutoReconnect();
}
/**
* 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(unsigned long timeoutLength) {
return STA.waitForConnectResult(timeoutLength);
}
/**
* Get the station interface IP address.
* @return IPAddress station IP
*/
IPAddress WiFiSTAClass::localIP() {
return STA.localIP();
}
/**
* 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) {
return STA.macAddress(mac);
}
/**
* Get the station interface MAC address.
* @return String mac
*/
String WiFiSTAClass::macAddress(void) {
return STA.macAddress();
}
/**
* Get the interface subnet mask address.
* @return IPAddress subnetMask
*/
IPAddress WiFiSTAClass::subnetMask() {
return STA.subnetMask();
}
/**
* Get the gateway ip address.
* @return IPAddress gatewayIP
*/
IPAddress WiFiSTAClass::gatewayIP() {
return STA.gatewayIP();
}
/**
* Get the DNS ip address.
* @param dns_no
* @return IPAddress DNS Server IP
*/
IPAddress WiFiSTAClass::dnsIP(uint8_t dns_no) {
return STA.dnsIP(dns_no);
}
/**
* Get the broadcast ip address.
* @return IPAddress broadcastIP
*/
IPAddress WiFiSTAClass::broadcastIP() {
return STA.broadcastIP();
}
/**
* Get the network id.
* @return IPAddress networkID
*/
IPAddress WiFiSTAClass::networkID() {
return STA.networkID();
}
/**
* Get the subnet CIDR.
* @return uint8_t subnetCIDR
*/
uint8_t WiFiSTAClass::subnetCIDR() {
return STA.subnetCIDR();
}
/**
* Return the current SSID associated with the network
* @return SSID
*/
String WiFiSTAClass::SSID() const {
return STA.SSID();
}
/**
* Return the current pre shared key associated with the network
* @return psk string
*/
String WiFiSTAClass::psk() const {
return STA.psk();
}
/**
* Return the current bssid / mac associated with the network if configured
* @return bssid uint8_t *
*/
uint8_t *WiFiSTAClass::BSSID(uint8_t *buff) {
return STA.BSSID(buff);
}
/**
* Return the current bssid / mac associated with the network if configured
* @return String bssid mac
*/
String WiFiSTAClass::BSSIDstr(void) {
return STA.BSSIDstr();
}
/**
* Return the current network RSSI.
* @return RSSI value
*/
int8_t WiFiSTAClass::RSSI(void) {
return STA.RSSI();
}
#if CONFIG_LWIP_IPV6
/**
* Enable IPv6 on the station interface.
* Should be called before WiFi.begin()
*
* @return true on success
*/
bool WiFiSTAClass::enableIPv6(bool en) {
return STA.enableIPv6(en);
}
/**
* Get the station interface link-local IPv6 address.
* @return IPAddress
*/
IPAddress WiFiSTAClass::linkLocalIPv6() {
return STA.linkLocalIPv6();
}
/**
* Get the station interface global IPv6 address.
* @return IPAddress
*/
IPAddress WiFiSTAClass::globalIPv6() {
return STA.globalIPv6();
}
#endif
bool WiFiSTAClass::_smartConfigStarted = false;
bool WiFiSTAClass::_smartConfigDone = false;
/**
* @brief
*
* @param type Select type of SmartConfig. Default type is SC_TYPE_ESPTOUCH
* @param crypt_key When using type SC_TYPE_ESPTOUTCH_V2 crypt key needed, else ignored. Length should be 16 chars.
* @return true if configuration is successful.
* @return false if configuration fails.
*/
bool WiFiSTAClass::beginSmartConfig(smartconfig_type_t type, char *crypt_key) {
esp_err_t err;
if (_smartConfigStarted) {
return false;
}
if (!WiFi.mode(WIFI_STA)) {
return false;
}
esp_wifi_disconnect();
smartconfig_start_config_t conf = SMARTCONFIG_START_CONFIG_DEFAULT();
if (type == SC_TYPE_ESPTOUCH_V2) {
conf.esp_touch_v2_enable_crypt = true;
conf.esp_touch_v2_key = crypt_key;
}
err = esp_smartconfig_set_type(type);
if (err != ESP_OK) {
log_e("SmartConfig Set Type Failed!");
return false;
}
err = esp_smartconfig_start(&conf);
if (err != ESP_OK) {
log_e("SmartConfig Start Failed!");
return false;
}
_smartConfigStarted = true;
_smartConfigDone = false;
return true;
}
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;
}
#endif /* SOC_WIFI_SUPPORTED */