forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFiMulti.cpp
365 lines (317 loc) · 11.9 KB
/
WiFiMulti.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
/**
*
* @file WiFiMulti.cpp
* @date 16.05.2015
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. 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
*
*/
#include "WiFiMulti.h"
#include <limits.h>
#include <string.h>
#include <esp32-hal.h>
WiFiMulti::WiFiMulti()
{
}
WiFiMulti::~WiFiMulti()
{
APlistClean();
}
bool WiFiMulti::addAP(const char* ssid, const char *passphrase)
{
return APlistAdd(ssid, passphrase);
}
uint8_t WiFiMulti::run(uint32_t connectTimeout)
{
int8_t scanResult;
uint8_t status = WiFi.status();
if(status == WL_CONNECTED) {
if (_bTestConnection && !_bWFMInit){
if (testConnection()) {
_bWFMInit = true;
return status;
}
} else if (!_bStrict) {
return status;
} else {
for(uint32_t x = 0; x < APlist.size(); x++) {
if(WiFi.SSID()==APlist[x].ssid){
return status;
}
}
}
WiFi.disconnect(false,false);
delay(10);
status = WiFi.status();
}
scanResult = WiFi.scanNetworks();
if(scanResult == WIFI_SCAN_RUNNING) {
// scan is running
return WL_NO_SSID_AVAIL;
} else if (scanResult >= 0) {
// scan done analyze
int32_t bestIndex = 0;
WifiAPlist_t bestNetwork { NULL, NULL, NULL };
int bestNetworkDb = INT_MIN;
int bestNetworkSec;
uint8_t bestBSSID[6];
int32_t bestChannel = 0;
log_i("[WIFI] scan done");
if(scanResult == 0) {
log_e("[WIFI] no networks found");
} else {
log_i("[WIFI] %d networks found", scanResult);
int8_t failCount = 0;
int8_t foundCount = 0;
for(int8_t i = 0; i < scanResult; ++i) {
String ssid_scan;
int32_t rssi_scan;
uint8_t sec_scan;
uint8_t* BSSID_scan;
int32_t chan_scan;
WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan);
if (_bAllowOpenAP && sec_scan == WIFI_AUTH_OPEN){
bool found = false;
for(uint32_t o = 0; o < APlist.size(); o++) {
WifiAPlist_t check = APlist[o];
if (ssid_scan == check.ssid){
found = true;
}
}
if (!found){
log_i("[WIFI] added %s to APList", ssid_scan);
APlistAdd(ssid_scan.c_str());
// failCount++;
// foundCount++;
}
}
bool known = false;
for(uint32_t x = 0; x < APlist.size(); x++) {
WifiAPlist_t entry = APlist[x];
if(ssid_scan == entry.ssid) { // It's on the list
log_i("known ssid: %s, failed: %i", entry.ssid, entry.fail);
foundCount++;
if (!entry.fail){
known = true;
log_i("rssi_scan: %d, bestNetworkDb: %d", rssi_scan, bestNetworkDb);
if(rssi_scan > bestNetworkDb) { // best network
if(_bAllowOpenAP || (sec_scan == WIFI_AUTH_OPEN || entry.passphrase)) { // check for passphrase if not open wlan
log_i("best network is now: %s", ssid_scan);
bestIndex = x;
bestNetworkSec = sec_scan;
bestNetworkDb = rssi_scan;
bestChannel = chan_scan;
memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork));
memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID));
}
}
break;
} else {
failCount++;
}
}
}
if(known) {
log_d(" ---> %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == WIFI_AUTH_OPEN) ? ' ' : '*');
} else {
log_d(" %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == WIFI_AUTH_OPEN) ? ' ' : '*');
}
}
log_i("foundCount = %d, failCount = %d", foundCount, failCount);
if (foundCount == failCount){
failCount = 0;
resetFails();
}
foundCount = 0;
}
// clean up ram
WiFi.scanDelete();
if(bestNetwork.ssid) {
log_i("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channel: %d (%d)", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
WiFi.begin(bestNetwork.ssid, (_bAllowOpenAP && bestNetworkSec == WIFI_AUTH_OPEN) ? NULL:bestNetwork.passphrase, bestChannel, bestBSSID);
status = WiFi.status();
_bWFMInit = true;
auto startTime = millis();
// wait for connection, fail, or timeout
while(status != WL_CONNECTED && status != WL_NO_SSID_AVAIL && status != WL_CONNECT_FAILED && (millis() - startTime) <= connectTimeout) {
delay(100);
status = WiFi.status();
}
switch(status) {
case 3:
log_i("[WIFI] Connecting done.");
log_d("[WIFI] SSID: %s", WiFi.SSID().c_str());
log_d("[WIFI] IP: %s", WiFi.localIP().toString().c_str());
log_d("[WIFI] MAC: %s", WiFi.BSSIDstr().c_str());
log_d("[WIFI] Channel: %d", WiFi.channel());
if (_bTestConnection){
// We connected to an AP but if it's a captive portal we're not going anywhere. Test it.
if (testConnection()){
resetFails();
} else {
markAsFailed(bestIndex);
WiFi.disconnect(false,false);
delay(100);
status = WiFi.status();
}
} else {
resetFails();
}
break;
case 1:
log_e("[WIFI] Connecting Failed AP not found.");
markAsFailed(bestIndex);
break;
case 4:
log_e("[WIFI] Connecting Failed.");
markAsFailed(bestIndex);
break;
default:
log_e("[WIFI] Connecting Failed (%d).", status);
markAsFailed(bestIndex);
break;
}
} else {
log_e("[WIFI] no matching wifi found!");
}
} else {
// start scan
log_d("[WIFI] delete old wifi config...");
WiFi.disconnect();
log_d("[WIFI] start scan");
// scan wifi async mode
WiFi.scanNetworks(true);
}
return status;
}
void WiFiMulti::markAsFailed(int32_t i) {
APlist[i].fail = true;
log_i("marked %s as failed",APlist[i].ssid);
}
void WiFiMulti::resetFails(){
log_i("resetting failure flags");
for(uint32_t i = 0; i < APlist.size(); i++) {
APlist[i].fail = false;
}
}
void WiFiMulti::setStrictMode(bool bStrict) {
_bStrict = bStrict;
}
void WiFiMulti::setAllowOpenAP(bool bAllowOpenAP) {
_bAllowOpenAP = bAllowOpenAP;
}
void WiFiMulti::setTestConnection(bool bTestConnection){
_bTestConnection = bTestConnection;
}
void WiFiMulti::setTestPhrase(const char* testPhrase){
_testPhrase = testPhrase;
}
void WiFiMulti::setTestURL(String testURL){
_testURL = testURL;
}
bool WiFiMulti::testConnection(){
//parse url
int8_t split = _testURL.indexOf('/',7);
String host = _testURL.substring(7, split);
String url = _testURL.substring(split,_testURL.length());
log_i("Testing connection to %s. Test phrase is \"%s\"",_testURL.c_str(), _testPhrase.c_str());
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host.c_str(), httpPort)) {
log_e("Connection failed");
return false;
} else {
log_i("Connected to test host");
}
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
log_e(">>>Client timeout!");
client.stop();
return false;
}
}
bool bSuccess = false;
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
// String line = client.readStringUntil('\r');
// Serial.print(line);
bSuccess = client.find(_testPhrase.c_str());
if (bSuccess){
log_i("Success. Found test phrase");
} else {
log_e("Failed. Can't find test phrase");
}
return bSuccess;
}
}
// ##################################################################################
bool WiFiMulti::APlistAdd(const char* ssid, const char *passphrase)
{
WifiAPlist_t newAP;
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
// fail SSID to long or missing!
log_e("[WIFI][APlistAdd] no ssid or ssid too long");
return false;
}
if(passphrase && strlen(passphrase) > 63) {
// fail passphrase to long!
log_e("[WIFI][APlistAdd] passphrase too long");
return false;
}
newAP.ssid = strdup(ssid);
if(!newAP.ssid) {
log_e("[WIFI][APlistAdd] fail newAP.ssid == 0");
return false;
}
if(passphrase && *passphrase != 0x00) {
newAP.passphrase = strdup(passphrase);
if(!newAP.passphrase) {
log_e("[WIFI][APlistAdd] fail newAP.passphrase == 0");
free(newAP.ssid);
return false;
}
} else {
newAP.passphrase = NULL;
}
newAP.fail = false;
APlist.push_back(newAP);
log_i("[WIFI][APlistAdd] added SSID: %s", newAP.ssid);
return true;
}
void WiFiMulti::APlistClean(void)
{
for(uint32_t i = 0; i < APlist.size(); i++) {
WifiAPlist_t entry = APlist[i];
if(entry.ssid) {
free(entry.ssid);
}
if(entry.passphrase) {
free(entry.passphrase);
}
//why doesn't this work???
//free(entry.fail);
}
APlist.clear();
}