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.
pull/9955/head
Veijo Pesonen 2019-03-08 17:49:36 +02:00
parent b5f3fcf49b
commit e2bd0644df
4 changed files with 81 additions and 35 deletions

View File

@ -445,17 +445,18 @@ int8_t ESP8266::rssi()
return rssi;
}
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min)
{
_smutex.lock();
set_timeout(ESP8266_CONNECT_TIMEOUT);
// Default timeout plus time spend scanning each channel
set_timeout(ESP8266_MISC_TIMEOUT + 13 * (t_max ? t_max : ESP8266_SCAN_TIME_MAX_DEFAULT));
_scan_r.res = res;
_scan_r.limit = limit;
_scan_r.cnt = 0;
if (!(_parser.send("AT+CWLAP") && _parser.recv("OK\n"))) {
if (!(_parser.send("AT+CWLAP=,,,%u,%u,%u", (mode == SCANMODE_ACTIVE ? 0 : 1), t_min, t_max) && _parser.recv("OK\n"))) {
tr_warning("scan(): AP info parsing aborted");
// Lets be happy about partial success and not return NSAPI_ERROR_DEVICE_ERROR
if (!_scan_r.cnt) {
@ -967,40 +968,40 @@ bool ESP8266::_recv_ap(nsapi_wifi_ap_t *ap)
{
int sec = NSAPI_SECURITY_UNKNOWN;
int dummy;
bool ret;
int ret;
if (FW_AT_LEAST_VERSION(_at_v.major, _at_v.minor, _at_v.patch, 0, ESP8266_AT_VERSION_WIFI_SCAN_CHANGE)) {
ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d,%d,%d,%d,%d)\n",
&sec,
ap->ssid,
&ap->rssi,
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
&ap->channel,
&dummy,
&dummy,
&dummy,
&dummy,
&dummy,
&dummy);
ret = _parser.scanf("(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d,%d,%d,%d,%d)\n",
&sec,
ap->ssid,
&ap->rssi,
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
&ap->channel,
&dummy,
&dummy,
&dummy,
&dummy,
&dummy,
&dummy);
} else {
ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d)\n",
&sec,
ap->ssid,
&ap->rssi,
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
&ap->channel,
&dummy,
&dummy);
ret = _parser.scanf("(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu,%d,%d)\n",
&sec,
ap->ssid,
&ap->rssi,
&ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4], &ap->bssid[5],
&ap->channel,
&dummy,
&dummy);
}
if (!ret) {
if (ret < 0) {
_parser.abort();
tr_warning("_recv_ap(): AP info missing");
}
ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
return ret;
return ret < 0 ? false : true;
}
void ESP8266::_oob_watchdog_reset()

View File

@ -44,6 +44,11 @@
#define ESP8266_MISC_TIMEOUT 2000
#endif
#define ESP8266_SCAN_TIME_MIN 0 // [ms]
#define ESP8266_SCAN_TIME_MAX 1500 // [ms]
#define ESP8266_SCAN_TIME_MIN_DEFAULT 120 // [ms]
#define ESP8266_SCAN_TIME_MAX_DEFAULT 360 // [ms]
// Firmware version
#define ESP8266_SDK_VERSION 2000000
#define ESP8266_SDK_VERSION_MAJOR ESP8266_SDK_VERSION/1000000
@ -193,14 +198,23 @@ public:
*/
int8_t rssi();
/** Scan mode
*/
enum scan_mode {
SCANMODE_ACTIVE = 0, /*!< active mode */
SCANMODE_PASSIVE = 1 /*!< passive mode */
};
/** Scan for available networks
*
* @param ap Pointer to allocated array to store discovered AP
* @param limit Size of allocated @a res array, or 0 to only count available AP
* @param t_max Maximum scan time per channel
* @param t_min Minimum scan time per channel in active mode, can be omitted in passive mode
* @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
*/
int scan(WiFiAccessPoint *res, unsigned limit);
int scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min);
/**Perform a dns query
*

View File

@ -361,14 +361,25 @@ int8_t ESP8266Interface::get_rssi()
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
{
nsapi_error_t status;
return scan(res, count, SCANMODE_ACTIVE, 0, 0);
}
status = _init();
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count, scan_mode mode, unsigned t_max, unsigned t_min)
{
if (t_max > ESP8266_SCAN_TIME_MAX) {
return NSAPI_ERROR_PARAMETER;
}
if (mode == SCANMODE_ACTIVE && t_min > t_max) {
return NSAPI_ERROR_PARAMETER;
}
nsapi_error_t status = _init();
if (status != NSAPI_ERROR_OK) {
return status;
}
return _esp.scan(res, count);
return _esp.scan(res, count, (mode == SCANMODE_ACTIVE ? ESP8266::SCANMODE_ACTIVE : ESP8266::SCANMODE_PASSIVE),
t_min, t_max);
}
bool ESP8266Interface::_get_firmware_ok()

View File

@ -145,18 +145,38 @@ public:
*/
virtual int8_t get_rssi();
/** Scan mode
*/
enum scan_mode {
SCANMODE_ACTIVE, /*!< active mode */
SCANMODE_PASSIVE /*!< passive mode */
};
/** Scan for available networks
*
* This function will block.
*
* @param ap Pointer to allocated array to store discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
* @param ap Pointer to allocated array to store discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
*/
virtual int scan(WiFiAccessPoint *res, unsigned count);
/** Scan for available networks
*
* This function will block.
*
* @param ap Pointer to allocated array to store discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP
* @param t_max Scan time for each channel - 0-1500ms. If 0 - uses default value
* @param t_min Minimum for each channel in active mode - 0-1500ms. If 0 - uses default value. Omit in passive mode
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
*/
virtual int scan(WiFiAccessPoint *res, unsigned count, scan_mode mode = SCANMODE_PASSIVE,
unsigned t_max = 0, unsigned t_min = 0);
/** Translates a hostname to an IP address with specific version
*
* The hostname may be either a domain name or an IP address. If the