2018-02-26 00:11:31 +00:00
|
|
|
#include <WiFiScanner.h>
|
|
|
|
|
2019-05-29 22:48:16 +00:00
|
|
|
WiFiScanner::WiFiScanner(AsyncWebServer *server, SecurityManager* securityManager) : _server(server) {
|
|
|
|
_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)
|
|
|
|
);
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiScanner::scanNetworks(AsyncWebServerRequest *request) {
|
|
|
|
if (WiFi.scanComplete() != -1){
|
|
|
|
WiFi.scanDelete();
|
|
|
|
WiFi.scanNetworks(true);
|
|
|
|
}
|
|
|
|
request->send(202);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiScanner::listNetworks(AsyncWebServerRequest *request) {
|
|
|
|
int numNetworks = WiFi.scanComplete();
|
|
|
|
if (numNetworks > -1){
|
2019-04-14 07:52:40 +00:00
|
|
|
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_WIFI_SCANNER_SIZE);
|
|
|
|
JsonObject root = response->getRoot();
|
|
|
|
JsonArray networks = root.createNestedArray("networks");
|
2018-02-26 00:11:31 +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);
|
2018-11-11 17:47:44 +00:00
|
|
|
#if defined(ESP8266)
|
2018-11-11 18:24:59 +00:00
|
|
|
network["encryption_type"] = convertEncryptionType(WiFi.encryptionType(i));
|
2018-11-11 17:47:44 +00:00
|
|
|
#elif defined(ESP_PLATFORM)
|
|
|
|
network["encryption_type"] = (uint8_t) WiFi.encryptionType(i);
|
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
response->setLength();
|
|
|
|
request->send(response);
|
|
|
|
} else if (numNetworks == -1){
|
|
|
|
request->send(202);
|
|
|
|
}else{
|
|
|
|
scanNetworks(request);
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 18:24:59 +00:00
|
|
|
|
2018-11-11 20:10:21 +00:00
|
|
|
#if defined(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.
|
|
|
|
*
|
|
|
|
* This allows us to use a single set of mappings in the UI.
|
|
|
|
*/
|
2018-11-11 18:35:33 +00:00
|
|
|
uint8_t WiFiScanner::convertEncryptionType(uint8_t encryptionType){
|
2018-11-11 18:24:59 +00:00
|
|
|
switch (encryptionType){
|
|
|
|
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;
|
|
|
|
}
|
2018-11-11 20:10:21 +00:00
|
|
|
#endif
|