2018-04-16 16:34:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-03-03 15:49:59 +01:00
|
|
|
* @file WiFiMulti.cpp
|
2018-04-16 16:34:39 +02:00
|
|
|
* @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();
|
2019-02-14 12:52:52 +01:00
|
|
|
if(status == WL_CONNECTED) {
|
2019-03-03 15:49:59 +01:00
|
|
|
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();
|
2019-02-14 12:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
scanResult = WiFi.scanNetworks();
|
|
|
|
if(scanResult == WIFI_SCAN_RUNNING) {
|
|
|
|
// scan is running
|
|
|
|
return WL_NO_SSID_AVAIL;
|
2019-03-03 15:49:16 +01:00
|
|
|
} else if(scanResult >= 0) {
|
2019-02-14 12:52:52 +01:00
|
|
|
// scan done analyze
|
|
|
|
WifiAPlist_t bestNetwork { NULL, NULL };
|
|
|
|
int bestNetworkDb = INT_MIN;
|
|
|
|
uint8_t bestBSSID[6];
|
|
|
|
int32_t bestChannel = 0;
|
|
|
|
|
|
|
|
log_i("[WIFI] scan done");
|
|
|
|
|
2019-03-03 15:49:16 +01:00
|
|
|
if(scanResult == 0) {
|
2019-02-14 12:52:52 +01:00
|
|
|
log_e("[WIFI] no networks found");
|
|
|
|
} else {
|
|
|
|
log_i("[WIFI] %d networks found", scanResult);
|
|
|
|
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);
|
|
|
|
|
|
|
|
bool known = false;
|
|
|
|
for(uint32_t x = 0; x < APlist.size(); x++) {
|
|
|
|
WifiAPlist_t entry = APlist[x];
|
|
|
|
|
|
|
|
if(ssid_scan == entry.ssid) { // SSID match
|
|
|
|
known = true;
|
|
|
|
if(rssi_scan > bestNetworkDb) { // best network
|
|
|
|
if(sec_scan == WIFI_AUTH_OPEN || entry.passphrase) { // check for passphrase if not open wlan
|
|
|
|
bestNetworkDb = rssi_scan;
|
|
|
|
bestChannel = chan_scan;
|
|
|
|
memcpy((void*) &bestNetwork, (void*) &entry, sizeof(bestNetwork));
|
|
|
|
memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID));
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 12:52:52 +01:00
|
|
|
break;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
2019-02-14 12:52:52 +01:00
|
|
|
}
|
2018-04-16 16:34:39 +02:00
|
|
|
|
2019-02-14 12:52:52 +01:00
|
|
|
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) ? ' ' : '*');
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-14 12:52:52 +01:00
|
|
|
}
|
2018-04-16 16:34:39 +02:00
|
|
|
|
2019-02-14 12:52:52 +01:00
|
|
|
// clean up ram
|
|
|
|
WiFi.scanDelete();
|
2018-04-16 16:34:39 +02:00
|
|
|
|
2019-02-14 12:52:52 +01:00
|
|
|
if(bestNetwork.ssid) {
|
|
|
|
log_i("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channal: %d (%d)", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
|
2018-04-16 16:34:39 +02:00
|
|
|
|
2019-02-14 12:52:52 +01:00
|
|
|
WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase, bestChannel, bestBSSID);
|
|
|
|
status = WiFi.status();
|
|
|
|
|
|
|
|
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(10);
|
2018-04-16 16:34:39 +02:00
|
|
|
status = WiFi.status();
|
2019-02-14 12:52:52 +01:00
|
|
|
}
|
2018-04-16 16:34:39 +02:00
|
|
|
|
2019-02-14 12:52:52 +01:00
|
|
|
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());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
log_e("[WIFI] Connecting Failed AP not found.");
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
log_e("[WIFI] Connecting Failed.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_e("[WIFI] Connecting Failed (%d).", status);
|
|
|
|
break;
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-14 12:52:52 +01:00
|
|
|
log_e("[WIFI] no matching wifi found!");
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
2019-02-14 12:52:52 +01:00
|
|
|
} else {
|
|
|
|
// start scan
|
|
|
|
log_d("[WIFI] delete old wifi config...");
|
|
|
|
WiFi.disconnect();
|
|
|
|
|
|
|
|
log_d("[WIFI] start scan");
|
|
|
|
// scan wifi async mode
|
|
|
|
WiFi.scanNetworks(true);
|
2018-04-16 16:34:39 +02:00
|
|
|
}
|
2019-02-14 12:52:52 +01:00
|
|
|
|
2018-04-16 16:34:39 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ##################################################################################
|
|
|
|
|
|
|
|
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!
|
2018-07-30 09:18:48 +02:00
|
|
|
log_e("[WIFI][APlistAdd] no ssid or ssid to long");
|
2018-04-16 16:34:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(passphrase && strlen(passphrase) > 63) {
|
|
|
|
// fail passphrase to long!
|
2018-07-30 09:18:48 +02:00
|
|
|
log_e("[WIFI][APlistAdd] passphrase to long");
|
2018-04-16 16:34:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
newAP.ssid = strdup(ssid);
|
|
|
|
|
|
|
|
if(!newAP.ssid) {
|
2018-07-30 09:18:48 +02:00
|
|
|
log_e("[WIFI][APlistAdd] fail newAP.ssid == 0");
|
2018-04-16 16:34:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(passphrase && *passphrase != 0x00) {
|
|
|
|
newAP.passphrase = strdup(passphrase);
|
|
|
|
if(!newAP.passphrase) {
|
2018-07-30 09:18:48 +02:00
|
|
|
log_e("[WIFI][APlistAdd] fail newAP.passphrase == 0");
|
2018-04-16 16:34:39 +02:00
|
|
|
free(newAP.ssid);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
APlist.push_back(newAP);
|
2018-07-30 09:18:48 +02:00
|
|
|
log_i("[WIFI][APlistAdd] add SSID: %s", newAP.ssid);
|
2018-04-16 16:34:39 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
APlist.clear();
|
|
|
|
}
|
|
|
|
|