Async scan + bugfixes (#947)

* Support asynchronous WiFi scan

* Fixed buffer overflow in getChipId

* ESP compatibility fixes

* fixup! ESP compatibility fixes
This commit is contained in:
Drzony 2022-11-01 01:01:10 +01:00 committed by GitHub
parent 44eeebbc6f
commit 6bbaf64daf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 26 deletions

View file

@ -325,7 +325,7 @@ public:
}
const char *getChipID() {
static char id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1] = { 0 };
static char id[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1] = { 0 };
if (!id[0]) {
pico_get_unique_board_id_string(id, sizeof(id));
}

View file

@ -110,7 +110,7 @@ void ArduinoOTAClass::begin(bool useMDNS) {
_useMDNS = useMDNS;
if (!_hostname.length()) {
char tmp[15];
char tmp[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 6];
sprintf(tmp, "pico-%s", rp2040.getChipID());
_hostname = tmp;
}

View file

@ -44,17 +44,20 @@ const char* WiFiClass::firmwareVersion() {
return PICO_SDK_VERSION_STRING;
}
void WiFiClass::mode(_wifiModeESP m) {
void WiFiClass::mode(WiFiMode_t m) {
_calledESP = true;
switch (m) {
case WIFI_OFF:
case WiFiMode_t::WIFI_OFF:
end();
break;
case WIFI_AP:
_modeESP = WIFI_AP;
case WiFiMode_t::WIFI_AP:
_modeESP = WiFiMode_t::WIFI_AP;
break;
case WIFI_STA:
_modeESP = WIFI_STA;
case WiFiMode_t::WIFI_STA:
_modeESP = WiFiMode_t::WIFI_STA;
break;
case WiFiMode_t::WIFI_AP_STA:
_modeESP = WiFiMode_t::WIFI_STA;
break;
}
}
@ -86,7 +89,7 @@ int WiFiClass::begin(const char* ssid, const char *passphrase) {
_ssid = ssid;
_password = passphrase;
_wifi.setSSID(ssid);
_wifi.setSSID(_ssid.c_str());
_wifi.setPassword(passphrase);
_wifi.setTimeout(_timeout);
_wifi.setSTA();
@ -123,7 +126,7 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase) {
_ssid = ssid;
_password = passphrase;
_wifi.setSSID(ssid);
_wifi.setSSID(_ssid.c_str());
_wifi.setPassword(passphrase);
_wifi.setTimeout(_timeout);
_wifi.setAP();
@ -234,7 +237,7 @@ const char *WiFiClass::getHostname() {
return: one value of wl_status_t enum
*/
int WiFiClass::disconnect(void) {
int WiFiClass::disconnect(bool wifi_off __unused) {
if (_dhcpServer) {
dhcp_server_deinit(_dhcpServer);
free(_dhcpServer);
@ -300,7 +303,7 @@ IPAddress WiFiClass::gatewayIP() {
return: ssid string
*/
const char* WiFiClass::SSID() {
const String &WiFiClass::SSID() {
return _ssid;
}
@ -396,7 +399,7 @@ int WiFiClass::_scanCB(void *env, const cyw43_ev_scan_result_t *result) {
return: Number of discovered networks
*/
int8_t WiFiClass::scanNetworks() {
int8_t WiFiClass::scanNetworks(bool async) {
cyw43_wifi_scan_options_t scan_options;
memset(&scan_options, 0, sizeof(scan_options));
_scan.clear();
@ -409,11 +412,27 @@ int8_t WiFiClass::scanNetworks() {
if (err) {
return 0;
}
if (!async) {
uint32_t now = millis();
while (cyw43_wifi_scan_active(&cyw43_state) && (millis() - now < 10000)) {
delay(10);
}
return _scan.size();
} else {
return -1;
}
}
int8_t WiFiClass::scanComplete() {
if (cyw43_wifi_scan_active(&cyw43_state)) {
return -1;
} else {
return _scan.size();
}
}
void WiFiClass::scanDelete() {
_scan.clear();
}
/*

View file

@ -35,7 +35,7 @@
typedef void(*FeedHostProcessorWatchdogFuncPointer)();
typedef enum { WIFI_STA, WIFI_AP, WIFI_OFF } _wifiModeESP; // For ESP8266 compatibility
typedef enum { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 } WiFiMode_t; // For ESP8266 compatibility
class WiFiClass {
public:
@ -46,7 +46,18 @@ public:
*/
static const char* firmwareVersion();
void mode(_wifiModeESP m); // For ESP8266 compatibility
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
@ -138,7 +149,7 @@ public:
}
String softAPSSID() {
return String(SSID());
return SSID();
}
@ -205,7 +216,7 @@ public:
return: one value of wl_status_t enum
*/
int disconnect(void);
int disconnect(bool wifi_off = false);
void end(void);
@ -249,7 +260,7 @@ public:
return: ssid string
*/
const char* SSID();
const String &SSID();
/*
Return the current BSSID associated with the network.
@ -281,9 +292,23 @@ public:
/*
Start scan WiFi networks available
param async: whether to perform asynchronous scan
return: Number of discovered networks
*/
int8_t scanNetworks();
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.
@ -357,8 +382,8 @@ public:
private:
int _timeout = 10000;
const char * _ssid = nullptr;
const char * _password = nullptr;
String _ssid;
String _password;
bool _wifiHWInitted = false;
bool _apMode = false;
@ -371,7 +396,7 @@ private:
// ESP compat
bool _calledESP = false; // Should we behave like the ESP8266 for connect?
_wifiModeESP _modeESP = WIFI_STA;
WiFiMode_t _modeESP = WIFI_STA;
};
extern WiFiClass WiFi;