2018-02-26 00:11:31 +00:00
|
|
|
#include <WiFiScanner.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFiScanner::WiFiScanner(AsyncWebServer* server, SecurityManager* securityManager) {
|
|
|
|
server->on(SCAN_NETWORKS_SERVICE_PATH,
|
|
|
|
HTTP_GET,
|
|
|
|
securityManager->wrapRequest(std::bind(&WiFiScanner::scanNetworks, this, std::placeholders::_1),
|
|
|
|
AuthenticationPredicates::IS_ADMIN));
|
|
|
|
server->on(LIST_NETWORKS_SERVICE_PATH,
|
|
|
|
HTTP_GET,
|
|
|
|
securityManager->wrapRequest(std::bind(&WiFiScanner::listNetworks, this, std::placeholders::_1),
|
|
|
|
AuthenticationPredicates::IS_ADMIN));
|
2019-09-28 20:29:46 +00:00
|
|
|
};
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
void WiFiScanner::scanNetworks(AsyncWebServerRequest* request) {
|
|
|
|
if (WiFi.scanComplete() != -1) {
|
|
|
|
WiFi.scanDelete();
|
|
|
|
WiFi.scanNetworks(true);
|
|
|
|
}
|
|
|
|
request->send(202);
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
void WiFiScanner::listNetworks(AsyncWebServerRequest* request) {
|
2018-02-26 00:11:31 +00:00
|
|
|
int numNetworks = WiFi.scanComplete();
|
2019-12-03 23:16:06 +00:00
|
|
|
if (numNetworks > -1) {
|
|
|
|
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_WIFI_SCANNER_SIZE);
|
2019-04-14 07:52:40 +00:00
|
|
|
JsonObject root = response->getRoot();
|
|
|
|
JsonArray networks = root.createNestedArray("networks");
|
2019-12-03 23:16:06 +00:00
|
|
|
for (int i = 0; i < numNetworks; i++) {
|
2019-04-14 07:52:40 +00:00
|
|
|
JsonObject network = networks.createNestedObject();
|
2018-02-26 00:11:31 +00:00
|
|
|
network["rssi"] = WiFi.RSSI(i);
|
|
|
|
network["ssid"] = WiFi.SSID(i);
|
|
|
|
network["bssid"] = WiFi.BSSIDstr(i);
|
|
|
|
network["channel"] = WiFi.channel(i);
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
2019-12-03 23:16:06 +00:00
|
|
|
network["encryption_type"] = (uint8_t)WiFi.encryptionType(i);
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
network["encryption_type"] = convertEncryptionType(WiFi.encryptionType(i));
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
response->setLength();
|
|
|
|
request->send(response);
|
2019-12-03 23:16:06 +00:00
|
|
|
} else if (numNetworks == -1) {
|
2018-02-26 00:11:31 +00:00
|
|
|
request->send(202);
|
2019-12-03 23:16:06 +00:00
|
|
|
} else {
|
2018-02-26 00:11:31 +00:00
|
|
|
scanNetworks(request);
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 18:24:59 +00:00
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP8266
|
2018-11-11 18:24:59 +00:00
|
|
|
/*
|
|
|
|
* Convert encryption type to standard used by ESP32 rather than the translated form which the esp8266 libaries expose.
|
2019-12-03 23:16:06 +00:00
|
|
|
*
|
2018-11-11 18:24:59 +00:00
|
|
|
* This allows us to use a single set of mappings in the UI.
|
|
|
|
*/
|
2019-12-03 23:16:06 +00:00
|
|
|
uint8_t WiFiScanner::convertEncryptionType(uint8_t encryptionType) {
|
|
|
|
switch (encryptionType) {
|
2018-11-11 18:24:59 +00:00
|
|
|
case ENC_TYPE_NONE:
|
|
|
|
return AUTH_OPEN;
|
|
|
|
case ENC_TYPE_WEP:
|
|
|
|
return AUTH_WEP;
|
|
|
|
case ENC_TYPE_TKIP:
|
|
|
|
return AUTH_WPA_PSK;
|
|
|
|
case ENC_TYPE_CCMP:
|
|
|
|
return AUTH_WPA2_PSK;
|
|
|
|
case ENC_TYPE_AUTO:
|
|
|
|
return AUTH_WPA_WPA2_PSK;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-09 20:57:44 +00:00
|
|
|
#endif
|