Skip to content

Commit b5f3fcf

Browse files
author
Veijo Pesonen
committed
ESP8266: Wi-Fi scan results as OOB
1 parent 943254c commit b5f3fcf

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
7575
_parser.oob("ALREADY CONNECTED", callback(this, &ESP8266::_oob_conn_already));
7676
_parser.oob("ERROR", callback(this, &ESP8266::_oob_err));
7777
_parser.oob("ready", callback(this, &ESP8266::_oob_ready));
78+
_parser.oob("+CWLAP:", callback(this, &ESP8266::_oob_scan_results));
7879
// Don't expect to find anything about the watchdog reset in official documentation
7980
//https://techtutorialsx.com/2017/01/21/esp8266-watchdog-functions/
8081
_parser.oob("wdt reset", callback(this, &ESP8266::_oob_watchdog_reset));
@@ -446,27 +447,25 @@ int8_t ESP8266::rssi()
446447

447448
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
448449
{
449-
unsigned cnt = 0;
450-
nsapi_wifi_ap_t ap;
451-
452450
_smutex.lock();
453-
set_timeout(ESP8266_CONNECT_TIMEOUT);
454451

455-
if (!_parser.send("AT+CWLAP")) {
456-
_smutex.unlock();
457-
return NSAPI_ERROR_DEVICE_ERROR;
458-
}
452+
set_timeout(ESP8266_CONNECT_TIMEOUT);
459453

460-
while (_recv_ap(&ap)) {
461-
if (cnt < limit) {
462-
res[cnt] = WiFiAccessPoint(ap);
463-
}
454+
_scan_r.res = res;
455+
_scan_r.limit = limit;
456+
_scan_r.cnt = 0;
464457

465-
cnt++;
466-
if (limit != 0 && cnt >= limit) {
467-
break;
458+
if (!(_parser.send("AT+CWLAP") && _parser.recv("OK\n"))) {
459+
tr_warning("scan(): AP info parsing aborted");
460+
// Lets be happy about partial success and not return NSAPI_ERROR_DEVICE_ERROR
461+
if (!_scan_r.cnt) {
462+
_scan_r.cnt = NSAPI_ERROR_DEVICE_ERROR;
468463
}
469464
}
465+
466+
int cnt = _scan_r.cnt;
467+
_scan_r.res = NULL;
468+
470469
set_timeout();
471470
_smutex.unlock();
472471

@@ -966,7 +965,7 @@ void ESP8266::attach(Callback<void()> status_cb)
966965

967966
bool ESP8266::_recv_ap(nsapi_wifi_ap_t *ap)
968967
{
969-
int sec;
968+
int sec = NSAPI_SECURITY_UNKNOWN;
970969
int dummy;
971970
bool ret;
972971

@@ -992,7 +991,11 @@ bool ESP8266::_recv_ap(nsapi_wifi_ap_t *ap)
992991
&ap->channel,
993992
&dummy,
994993
&dummy);
994+
}
995995

996+
if (!ret) {
997+
_parser.abort();
998+
tr_warning("_recv_ap(): AP info missing");
996999
}
9971000

9981001
ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
@@ -1063,6 +1066,19 @@ void ESP8266::_oob_tcp_data_hdlr()
10631066
_sock_i[_sock_active_id].tcp_data_rcvd = len;
10641067
}
10651068

1069+
void ESP8266::_oob_scan_results()
1070+
{
1071+
nsapi_wifi_ap_t ap;
1072+
1073+
if (_recv_ap(&ap)) {
1074+
if (_scan_r.res && _scan_r.cnt < _scan_r.limit) {
1075+
_scan_r.res[_scan_r.cnt] = WiFiAccessPoint(ap);
1076+
}
1077+
1078+
_scan_r.cnt++;
1079+
}
1080+
}
1081+
10661082
void ESP8266::_oob_connect_err()
10671083
{
10681084
_fail = false;

components/wifi/esp8266-driver/ESP8266/ESP8266.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ class ESP8266 {
438438
void _oob_busy();
439439
void _oob_tcp_data_hdlr();
440440
void _oob_ready();
441+
void _oob_scan_results();
441442

442443
// OOB state variables
443444
int _connect_error;
@@ -466,6 +467,14 @@ class ESP8266 {
466467
};
467468
struct _sock_info _sock_i[SOCKET_COUNT];
468469

470+
// Scan results
471+
struct _scan_results {
472+
WiFiAccessPoint *res;
473+
unsigned limit;
474+
unsigned cnt;
475+
};
476+
struct _scan_results _scan_r;
477+
469478
// Connection state reporting
470479
nsapi_connection_status_t _conn_status;
471480
mbed::Callback<void()> _conn_stat_cb; // ESP8266Interface registered

0 commit comments

Comments
 (0)