Skip to content

Commit 6bbaf64

Browse files
authored
Async scan + bugfixes (#947)
* Support asynchronous WiFi scan * Fixed buffer overflow in getChipId * ESP compatibility fixes * fixup! ESP compatibility fixes
1 parent 44eeebb commit 6bbaf64

File tree

4 files changed

+70
-26
lines changed

4 files changed

+70
-26
lines changed

cores/rp2040/RP2040Support.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class RP2040 {
325325
}
326326

327327
const char *getChipID() {
328-
static char id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1] = { 0 };
328+
static char id[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1] = { 0 };
329329
if (!id[0]) {
330330
pico_get_unique_board_id_string(id, sizeof(id));
331331
}

libraries/ArduinoOTA/src/ArduinoOTA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void ArduinoOTAClass::begin(bool useMDNS) {
110110
_useMDNS = useMDNS;
111111

112112
if (!_hostname.length()) {
113-
char tmp[15];
113+
char tmp[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 6];
114114
sprintf(tmp, "pico-%s", rp2040.getChipID());
115115
_hostname = tmp;
116116
}

libraries/WiFi/src/WiFiClass.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,20 @@ const char* WiFiClass::firmwareVersion() {
4444
return PICO_SDK_VERSION_STRING;
4545
}
4646

47-
void WiFiClass::mode(_wifiModeESP m) {
47+
void WiFiClass::mode(WiFiMode_t m) {
4848
_calledESP = true;
4949
switch (m) {
50-
case WIFI_OFF:
50+
case WiFiMode_t::WIFI_OFF:
5151
end();
5252
break;
53-
case WIFI_AP:
54-
_modeESP = WIFI_AP;
53+
case WiFiMode_t::WIFI_AP:
54+
_modeESP = WiFiMode_t::WIFI_AP;
5555
break;
56-
case WIFI_STA:
57-
_modeESP = WIFI_STA;
56+
case WiFiMode_t::WIFI_STA:
57+
_modeESP = WiFiMode_t::WIFI_STA;
58+
break;
59+
case WiFiMode_t::WIFI_AP_STA:
60+
_modeESP = WiFiMode_t::WIFI_STA;
5861
break;
5962
}
6063
}
@@ -86,7 +89,7 @@ int WiFiClass::begin(const char* ssid, const char *passphrase) {
8689

8790
_ssid = ssid;
8891
_password = passphrase;
89-
_wifi.setSSID(ssid);
92+
_wifi.setSSID(_ssid.c_str());
9093
_wifi.setPassword(passphrase);
9194
_wifi.setTimeout(_timeout);
9295
_wifi.setSTA();
@@ -123,7 +126,7 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase) {
123126

124127
_ssid = ssid;
125128
_password = passphrase;
126-
_wifi.setSSID(ssid);
129+
_wifi.setSSID(_ssid.c_str());
127130
_wifi.setPassword(passphrase);
128131
_wifi.setTimeout(_timeout);
129132
_wifi.setAP();
@@ -234,7 +237,7 @@ const char *WiFiClass::getHostname() {
234237
235238
return: one value of wl_status_t enum
236239
*/
237-
int WiFiClass::disconnect(void) {
240+
int WiFiClass::disconnect(bool wifi_off __unused) {
238241
if (_dhcpServer) {
239242
dhcp_server_deinit(_dhcpServer);
240243
free(_dhcpServer);
@@ -300,7 +303,7 @@ IPAddress WiFiClass::gatewayIP() {
300303
301304
return: ssid string
302305
*/
303-
const char* WiFiClass::SSID() {
306+
const String &WiFiClass::SSID() {
304307
return _ssid;
305308
}
306309

@@ -396,7 +399,7 @@ int WiFiClass::_scanCB(void *env, const cyw43_ev_scan_result_t *result) {
396399
397400
return: Number of discovered networks
398401
*/
399-
int8_t WiFiClass::scanNetworks() {
402+
int8_t WiFiClass::scanNetworks(bool async) {
400403
cyw43_wifi_scan_options_t scan_options;
401404
memset(&scan_options, 0, sizeof(scan_options));
402405
_scan.clear();
@@ -409,11 +412,27 @@ int8_t WiFiClass::scanNetworks() {
409412
if (err) {
410413
return 0;
411414
}
412-
uint32_t now = millis();
413-
while (cyw43_wifi_scan_active(&cyw43_state) && (millis() - now < 10000)) {
414-
delay(10);
415+
if (!async) {
416+
uint32_t now = millis();
417+
while (cyw43_wifi_scan_active(&cyw43_state) && (millis() - now < 10000)) {
418+
delay(10);
419+
}
420+
return _scan.size();
421+
} else {
422+
return -1;
423+
}
424+
}
425+
426+
int8_t WiFiClass::scanComplete() {
427+
if (cyw43_wifi_scan_active(&cyw43_state)) {
428+
return -1;
429+
} else {
430+
return _scan.size();
415431
}
416-
return _scan.size();
432+
}
433+
434+
void WiFiClass::scanDelete() {
435+
_scan.clear();
417436
}
418437

419438
/*

libraries/WiFi/src/WiFiClass.h

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
typedef void(*FeedHostProcessorWatchdogFuncPointer)();
3737

38-
typedef enum { WIFI_STA, WIFI_AP, WIFI_OFF } _wifiModeESP; // For ESP8266 compatibility
38+
typedef enum { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 } WiFiMode_t; // For ESP8266 compatibility
3939

4040
class WiFiClass {
4141
public:
@@ -46,7 +46,18 @@ class WiFiClass {
4646
*/
4747
static const char* firmwareVersion();
4848

49-
void mode(_wifiModeESP m); // For ESP8266 compatibility
49+
void mode(WiFiMode_t m); // For ESP8266 compatibility
50+
51+
WiFiMode_t getMode() {
52+
if (_wifiHWInitted) {
53+
if (_apMode) {
54+
return WiFiMode_t::WIFI_AP;
55+
} else {
56+
return WiFiMode_t::WIFI_STA;
57+
}
58+
}
59+
return WiFiMode_t::WIFI_OFF;
60+
};
5061

5162
/* Start WiFi connection for OPEN networks
5263
@@ -138,7 +149,7 @@ class WiFiClass {
138149
}
139150

140151
String softAPSSID() {
141-
return String(SSID());
152+
return SSID();
142153
}
143154

144155

@@ -205,7 +216,7 @@ class WiFiClass {
205216
206217
return: one value of wl_status_t enum
207218
*/
208-
int disconnect(void);
219+
int disconnect(bool wifi_off = false);
209220

210221
void end(void);
211222

@@ -249,7 +260,7 @@ class WiFiClass {
249260
250261
return: ssid string
251262
*/
252-
const char* SSID();
263+
const String &SSID();
253264

254265
/*
255266
Return the current BSSID associated with the network.
@@ -281,9 +292,23 @@ class WiFiClass {
281292
/*
282293
Start scan WiFi networks available
283294
295+
param async: whether to perform asynchronous scan
296+
297+
return: Number of discovered networks
298+
*/
299+
int8_t scanNetworks(bool async = false);
300+
301+
/*
302+
Return number of scanned WiFi networks
303+
284304
return: Number of discovered networks
285305
*/
286-
int8_t scanNetworks();
306+
int8_t scanComplete();
307+
308+
/*
309+
Delete scan results
310+
*/
311+
void scanDelete();
287312

288313
/*
289314
Return the SSID discovered during the network scan.
@@ -357,8 +382,8 @@ class WiFiClass {
357382

358383
private:
359384
int _timeout = 10000;
360-
const char * _ssid = nullptr;
361-
const char * _password = nullptr;
385+
String _ssid;
386+
String _password;
362387
bool _wifiHWInitted = false;
363388
bool _apMode = false;
364389

@@ -371,7 +396,7 @@ class WiFiClass {
371396

372397
// ESP compat
373398
bool _calledESP = false; // Should we behave like the ESP8266 for connect?
374-
_wifiModeESP _modeESP = WIFI_STA;
399+
WiFiMode_t _modeESP = WIFI_STA;
375400
};
376401

377402
extern WiFiClass WiFi;

0 commit comments

Comments
 (0)