-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathWiFiClass.h
451 lines (341 loc) · 13 KB
/
WiFiClass.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
/*
WiFi.h - WiFi class "compat" w/WiFiNINA for Raspberry Pi Pico W
Copyright (c) 2022 Earle F. Philhower, III. All rights reserved.
Implements the API defined by the Arduino WiFiNINA library,
copyright (c) 2018 Arduino SA. All rights reserved.
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
*/
#pragma once
#include <Arduino.h>
#if defined(PICO_CYW43_SUPPORTED)
#include <lwIP_CYW43.h>
#elif defined(ESPHOSTSPI)
#include <lwIP_ESPHost.h>
#elif defined(WINC1501_SPI)
#include <lwIP_WINC1500.h>
#else
#include "utility/lwIP_nodriver.h"
#endif
#include "WiFi.h"
#include <inttypes.h>
#include <map>
#include "dhcpserver/dhcpserver.h"
#define WIFI_FIRMWARE_LATEST_VERSION PICO_SDK_VERSION_STRING
typedef void(*FeedHostProcessorWatchdogFuncPointer)();
typedef enum { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 } WiFiMode_t; // For ESP8266 compatibility
class WiFiClass {
public:
WiFiClass();
/*
Get firmware version
*/
static const char* firmwareVersion();
void mode(WiFiMode_t m); // For ESP8266 compatibility
WiFiMode_t getMode() {
if (_wifiHWInitted) {
if (_apMode) {
return WiFiMode_t::WIFI_AP;
} else {
return WiFiMode_t::WIFI_STA;
}
}
return WiFiMode_t::WIFI_OFF;
};
/* Start WiFi connection for OPEN networks
param ssid: Pointer to the SSID string.
*/
int begin(const char* ssid);
/* Start WiFi connection for OPEN networks, without blocking
param ssid: Pointer to the SSID string.
*/
int beginNoBlock(const char* ssid);
int beginBSSID(const char* ssid, const uint8_t *bssid);
/* Start WiFi connection with WEP encryption.
Configure a key into the device. The key type (WEP-40, WEP-104)
is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104).
param ssid: Pointer to the SSID string.
param key_idx: The key index to set. Valid values are 0-3.
param key: Key input buffer.
*/
// TODO - WEP is not supported in the driver
// int begin(const char* ssid, uint8_t key_idx, const char* key);
/* Start WiFi connection with passphrase
the most secure supported mode will be automatically selected
param ssid: Pointer to the SSID string.
param passphrase: Passphrase. Valid characters in a passphrase
must be between ASCII 32-126 (decimal).
param bssid: If non-null, the BSSID associated w/the SSID to connect to
*/
int begin(const char* ssid, const char *passphrase, const uint8_t *bssid = nullptr);
/* Start WiFi connection with passphrase, without blocking. Check for .connected() for a connection
the most secure supported mode will be automatically selected
param ssid: Pointer to the SSID string.
param passphrase: Passphrase. Valid characters in a passphrase
must be between ASCII 32-126 (decimal).
param bssid: If non-null, the BSSID associated w/the SSID to connect to
*/
int beginNoBlock(const char* ssid, const char *passphrase, const uint8_t *bssid = nullptr);
bool connected();
bool isConnected() {
return connected();
}
int8_t waitForConnectResult(unsigned long timeoutLength = 60000) {
uint32_t now = millis();
while (millis() - now < timeoutLength) {
if (status() != WL_DISCONNECTED) {
return status();
}
delay(10);
}
return -1;
}
uint8_t beginAP(const char *ssid);
uint8_t beginAP(const char *ssid, uint8_t channel);
uint8_t beginAP(const char *ssid, const char* passphrase);
uint8_t beginAP(const char *ssid, const char* passphrase, uint8_t channel);
// ESP8266 compatible calls
bool softAP(const char* ssid, const char* psk = nullptr, int channel = 1, int ssid_hidden = 0, int max_connection = 4) {
(void) ssid_hidden;
(void) max_connection;
return beginAP(ssid, psk, channel) == WL_CONNECTED;
}
bool softAP(const String& ssid, const String& psk = "", int channel = 1, int ssid_hidden = 0, int max_connection = 4) {
(void) ssid_hidden;
(void) max_connection;
if (psk != "") {
return beginAP(ssid.c_str(), psk.c_str(), channel) == WL_CONNECTED;
} else {
return beginAP(ssid.c_str(), channel) == WL_CONNECTED;
}
}
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
config(local_ip, local_ip, gateway, subnet);
return true;
}
bool softAPdisconnect(bool wifioff = false) {
(void) wifioff;
disconnect();
return true;
}
#if defined(PICO_CYW43_SUPPORTED)
uint8_t softAPgetStationNum();
#endif
IPAddress softAPIP() {
return localIP();
}
uint8_t* softAPmacAddress(uint8_t* mac) {
return macAddress(mac);
}
String softAPmacAddress(void) {
uint8_t mac[6];
macAddress(mac);
char buff[32];
sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(buff);
}
String softAPSSID() {
return SSID();
}
// TODO - EAP is not supported by the driver. Maybe some way of user-level wap-supplicant in the future?
//uint8_t beginEnterprise(const char* ssid, const char* username, const char* password);
//uint8_t beginEnterprise(const char* ssid, const char* username, const char* password, const char* identity);
//uint8_t beginEnterprise(const char* ssid, const char* username, const char* password, const char* identity, const char* ca);
/* Change Ip configuration settings disabling the dhcp client
param local_ip: Static ip configuration
*/
void config(IPAddress local_ip);
/* Change Ip configuration settings disabling the dhcp client
param local_ip: Static ip configuration
param dns_server: IP configuration for DNS server 1
*/
void config(IPAddress local_ip, IPAddress dns_server);
/* Change Ip configuration settings disabling the dhcp client
param local_ip: Static ip configuration
param dns_server: IP configuration for DNS server 1
param gateway : Static gateway configuration
*/
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
/* Change Ip configuration settings disabling the dhcp client
param local_ip: Static ip configuration
param dns_server: IP configuration for DNS server 1
param gateway: Static gateway configuration
param subnet: Static Subnet mask
*/
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
/* Change DNS Ip configuration
param dns_server1: ip configuration for DNS server 1
*/
void setDNS(IPAddress dns_server1);
/* Change DNS Ip configuration
param dns_server1: ip configuration for DNS server 1
param dns_server2: ip configuration for DNS server 2
*/
void setDNS(IPAddress dns_server1, IPAddress dns_server2);
/* Set the hostname used for DHCP requests
param name: hostname to set
*/
void setHostname(const char* name);
const char *getHostname();
/*
Disconnect from the network
return: one value of wl_status_t enum
*/
int disconnect(bool wifi_off = false);
void end(void);
/*
Get the interface MAC address.
return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
uint8_t* macAddress(uint8_t* mac);
String macAddress(void) {
uint8_t mac[6];
macAddress(mac);
char buff[32];
sprintf(buff, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(buff);
}
/*
Get the interface IP address.
return: Ip address value
*/
IPAddress localIP();
/*
Get the interface subnet mask address.
return: subnet mask address value
*/
IPAddress subnetMask();
/*
Get the gateway ip address.
return: gateway ip address value
*/
IPAddress gatewayIP();
/*
Get the DNS ip address.
return: IPAddress DNS Server IP
*/
IPAddress dnsIP(uint8_t dns_no = 0);
/*
Return the current SSID associated with the network
return: ssid string
*/
const String &SSID();
/*
Return the current BSSID associated with the network.
It is the MAC address of the Access Point
return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
uint8_t* BSSID(uint8_t* bssid);
/*
Return the current RSSI /Received Signal Strength in dBm)
associated with the network
return: signed value
*/
int32_t RSSI();
/* Return the current network channel */
int channel();
/*
Return the Encryption Type associated with the network
return: one value of wl_enc_type enum
*/
uint8_t encryptionType();
/*
Start scan WiFi networks available
param async: whether to perform asynchronous scan
return: Number of discovered networks
*/
int8_t scanNetworks(bool async = false);
/*
Return number of scanned WiFi networks
return: Number of discovered networks
*/
int8_t scanComplete();
/*
Delete scan results
*/
void scanDelete();
/*
Return the SSID discovered during the network scan.
param networkItem: specify from which network item want to get the information
return: ssid string of the specified item on the networks scanned list
*/
const char* SSID(uint8_t networkItem);
/*
Return the encryption type of the networks discovered during the scanNetworks
param networkItem: specify from which network item want to get the information
return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
*/
uint8_t encryptionType(uint8_t networkItem);
uint8_t* BSSID(uint8_t networkItem, uint8_t* bssid);
uint8_t channel(uint8_t networkItem);
/*
Return the RSSI of the networks discovered during the scanNetworks
param networkItem: specify from which network item want to get the information
return: signed value of RSSI of the specified item on the networks scanned list
*/
int32_t RSSI(uint8_t networkItem);
/*
Return Connection status.
return: one of the value defined in wl_status_t
*/
uint8_t status();
/*
Return The deauthentication reason code.
return: the deauthentication reason code
*/
uint8_t reasonCode();
/*
Resolve the given hostname to an IP address.
param aHostname: Name to be resolved
param aResult: IPAddress structure to store the returned IP address
result: 1 if aIPAddrString was successfully converted to an IP address,
else error code
*/
int hostByName(const char* aHostname, IPAddress& aResult) {
return hostByName(aHostname, aResult, _timeout);
}
int hostByName(const char* aHostname, IPAddress& aResult, int timeout);
unsigned long getTime();
#if defined(PICO_CYW43_SUPPORTED)
void aggressiveLowPowerMode();
void defaultLowPowerMode();
#endif
void noLowPowerMode();
int ping(const char* hostname, uint8_t ttl = 128);
int ping(const String &hostname, uint8_t ttl = 128);
int ping(IPAddress host, uint8_t ttl = 128);
void setTimeout(unsigned long timeout);
void setFeedWatchdogFunc(FeedHostProcessorWatchdogFuncPointer func);
void feedWatchdog();
// ESP8266 compatibility
void persistent(bool unused) {
(void) unused;
}
void hostname(const char *name) {
setHostname(name);
}
private:
// Internal wifi begin. Returns true on success
bool _beginInternal(const char* ssid, const char *passphrase, const uint8_t *bssid = nullptr);
int _timeout = 15000;
String _ssid;
uint8_t _bssid[6];
String _password;
bool _wifiHWInitted = false;
bool _apMode = false;
// DHCP for AP mode
dhcp_server_t *_dhcpServer = nullptr;
// ESP compat
bool _calledESP = false; // Should we behave like the ESP8266 for connect?
WiFiMode_t _modeESP = WIFI_STA;
};
extern WiFiClass WiFi;