Skip to content

Commit e2bd064

Browse files
author
Veijo Pesonen
committed
ESP8266: new API for setting Wi-Fi scan active/passive mode
Makes possible to decide between active and passive mode. Makes possible to adjust for how long a single channel is scanned.
1 parent b5f3fcf commit e2bd064

File tree

4 files changed

+81
-35
lines changed

4 files changed

+81
-35
lines changed

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,18 @@ int8_t ESP8266::rssi()
445445
return rssi;
446446
}
447447

448-
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
448+
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min)
449449
{
450450
_smutex.lock();
451451

452-
set_timeout(ESP8266_CONNECT_TIMEOUT);
452+
// Default timeout plus time spend scanning each channel
453+
set_timeout(ESP8266_MISC_TIMEOUT + 13 * (t_max ? t_max : ESP8266_SCAN_TIME_MAX_DEFAULT));
453454

454455
_scan_r.res = res;
455456
_scan_r.limit = limit;
456457
_scan_r.cnt = 0;
457458

458-
if (!(_parser.send("AT+CWLAP") && _parser.recv("OK\n"))) {
459+
if (!(_parser.send("AT+CWLAP=,,,%u,%u,%u", (mode == SCANMODE_ACTIVE ? 0 : 1), t_min, t_max) && _parser.recv("OK\n"))) {
459460
tr_warning("scan(): AP info parsing aborted");
460461
// Lets be happy about partial success and not return NSAPI_ERROR_DEVICE_ERROR
461462
if (!_scan_r.cnt) {
@@ -967,40 +968,40 @@ bool ESP8266::_recv_ap(nsapi_wifi_ap_t *ap)
967968
{
968969
int sec = NSAPI_SECURITY_UNKNOWN;
969970
int dummy;
970-
bool ret;
971+
int ret;
971972

972973
if (FW_AT_LEAST_VERSION(_at_v.major, _at_v.minor, _at_v.patch, 0, ESP8266_AT_VERSION_WIFI_SCAN_CHANGE)) {
973-
ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d,%d,%d,%d,%d)\n",
974-
&sec,
975-
ap->ssid,
976-
&ap->rssi,
977-
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
978-
&ap->channel,
979-
&dummy,
980-
&dummy,
981-
&dummy,
982-
&dummy,
983-
&dummy,
984-
&dummy);
974+
ret = _parser.scanf("(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d,%d,%d,%d,%d)\n",
975+
&sec,
976+
ap->ssid,
977+
&ap->rssi,
978+
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
979+
&ap->channel,
980+
&dummy,
981+
&dummy,
982+
&dummy,
983+
&dummy,
984+
&dummy,
985+
&dummy);
985986
} else {
986-
ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d)\n",
987-
&sec,
988-
ap->ssid,
989-
&ap->rssi,
990-
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
991-
&ap->channel,
992-
&dummy,
993-
&dummy);
987+
ret = _parser.scanf("(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d)\n",
988+
&sec,
989+
ap->ssid,
990+
&ap->rssi,
991+
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
992+
&ap->channel,
993+
&dummy,
994+
&dummy);
994995
}
995996

996-
if (!ret) {
997+
if (ret < 0) {
997998
_parser.abort();
998999
tr_warning("_recv_ap(): AP info missing");
9991000
}
10001001

10011002
ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
10021003

1003-
return ret;
1004+
return ret < 0 ? false : true;
10041005
}
10051006

10061007
void ESP8266::_oob_watchdog_reset()

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
#define ESP8266_MISC_TIMEOUT 2000
4545
#endif
4646

47+
#define ESP8266_SCAN_TIME_MIN 0 // [ms]
48+
#define ESP8266_SCAN_TIME_MAX 1500 // [ms]
49+
#define ESP8266_SCAN_TIME_MIN_DEFAULT 120 // [ms]
50+
#define ESP8266_SCAN_TIME_MAX_DEFAULT 360 // [ms]
51+
4752
// Firmware version
4853
#define ESP8266_SDK_VERSION 2000000
4954
#define ESP8266_SDK_VERSION_MAJOR ESP8266_SDK_VERSION/1000000
@@ -193,14 +198,23 @@ class ESP8266 {
193198
*/
194199
int8_t rssi();
195200

201+
/** Scan mode
202+
*/
203+
enum scan_mode {
204+
SCANMODE_ACTIVE = 0, /*!< active mode */
205+
SCANMODE_PASSIVE = 1 /*!< passive mode */
206+
};
207+
196208
/** Scan for available networks
197209
*
198210
* @param ap Pointer to allocated array to store discovered AP
199211
* @param limit Size of allocated @a res array, or 0 to only count available AP
212+
* @param t_max Maximum scan time per channel
213+
* @param t_min Minimum scan time per channel in active mode, can be omitted in passive mode
200214
* @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
201215
* see @a nsapi_error
202216
*/
203-
int scan(WiFiAccessPoint *res, unsigned limit);
217+
int scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min);
204218

205219
/**Perform a dns query
206220
*

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,25 @@ int8_t ESP8266Interface::get_rssi()
361361

362362
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
363363
{
364-
nsapi_error_t status;
364+
return scan(res, count, SCANMODE_ACTIVE, 0, 0);
365+
}
365366

366-
status = _init();
367+
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count, scan_mode mode, unsigned t_max, unsigned t_min)
368+
{
369+
if (t_max > ESP8266_SCAN_TIME_MAX) {
370+
return NSAPI_ERROR_PARAMETER;
371+
}
372+
if (mode == SCANMODE_ACTIVE && t_min > t_max) {
373+
return NSAPI_ERROR_PARAMETER;
374+
}
375+
376+
nsapi_error_t status = _init();
367377
if (status != NSAPI_ERROR_OK) {
368378
return status;
369379
}
370380

371-
return _esp.scan(res, count);
381+
return _esp.scan(res, count, (mode == SCANMODE_ACTIVE ? ESP8266::SCANMODE_ACTIVE : ESP8266::SCANMODE_PASSIVE),
382+
t_min, t_max);
372383
}
373384

374385
bool ESP8266Interface::_get_firmware_ok()

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,38 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
145145
*/
146146
virtual int8_t get_rssi();
147147

148+
/** Scan mode
149+
*/
150+
enum scan_mode {
151+
SCANMODE_ACTIVE, /*!< active mode */
152+
SCANMODE_PASSIVE /*!< passive mode */
153+
};
154+
148155
/** Scan for available networks
149156
*
150157
* This function will block.
151158
*
152-
* @param ap Pointer to allocated array to store discovered AP
153-
* @param count Size of allocated @a res array, or 0 to only count available AP
154-
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
155-
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
156-
* see @a nsapi_error
159+
* @param ap Pointer to allocated array to store discovered AP
160+
* @param count Size of allocated @a res array, or 0 to only count available AP
161+
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
162+
* see @a nsapi_error
157163
*/
158164
virtual int scan(WiFiAccessPoint *res, unsigned count);
159165

166+
/** Scan for available networks
167+
*
168+
* This function will block.
169+
*
170+
* @param ap Pointer to allocated array to store discovered AP
171+
* @param count Size of allocated @a res array, or 0 to only count available AP
172+
* @param t_max Scan time for each channel - 0-1500ms. If 0 - uses default value
173+
* @param t_min Minimum for each channel in active mode - 0-1500ms. If 0 - uses default value. Omit in passive mode
174+
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
175+
* see @a nsapi_error
176+
*/
177+
virtual int scan(WiFiAccessPoint *res, unsigned count, scan_mode mode = SCANMODE_PASSIVE,
178+
unsigned t_max = 0, unsigned t_min = 0);
179+
160180
/** Translates a hostname to an IP address with specific version
161181
*
162182
* The hostname may be either a domain name or an IP address. If the

0 commit comments

Comments
 (0)