Skip to content

Commit

Permalink
net: mgmt: Support for forced Passive scan
Browse files Browse the repository at this point in the history
Default scan mode is Active. User can force the scan mode to passive
through Kconfig option or using 'passive' option from shell.
Using either of this option will override regulatory settings and
forces all scan channels to be passive only.

Signed-off-by: Ajay Parida <[email protected]>
  • Loading branch information
ajayparida committed Jun 29, 2023
1 parent 2f5068f commit ad39003
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 9 deletions.
9 changes: 8 additions & 1 deletion drivers/wifi/esp32/src/esp_wifi_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ static int esp32_wifi_connect(const struct device *dev,
return 0;
}

static int esp32_wifi_scan(const struct device *dev, scan_result_cb_t cb)
static int esp32_wifi_scan(const struct device *dev,
struct wifi_scan_params *params,
scan_result_cb_t cb)
{
struct esp32_wifi_runtime *data = dev->data;
int ret = 0;
Expand All @@ -402,6 +404,11 @@ static int esp32_wifi_scan(const struct device *dev, scan_result_cb_t cb)

wifi_scan_config_t scan_config = { 0 };

if (params) {
/* The enum values are same, so, no conversion needed */
scan_config->scan_type = params->scan_type;
}

ret = esp_wifi_set_mode(ESP32_WIFI_MODE_STA);
ret |= esp_wifi_scan_start(&scan_config, false);

Expand Down
6 changes: 5 additions & 1 deletion drivers/wifi/esp_at/esp.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,14 @@ static void esp_mgmt_scan_work(struct k_work *work)
dev->scan_cb = NULL;
}

static int esp_mgmt_scan(const struct device *dev, scan_result_cb_t cb)
static int esp_mgmt_scan(const struct device *dev,
struct wifi_scan_params *params,
scan_result_cb_t cb)
{
struct esp_data *data = dev->data;

ARG_UNUSED(params);

if (data->scan_cb != NULL) {
return -EINPROGRESS;
}
Expand Down
6 changes: 5 additions & 1 deletion drivers/wifi/eswifi/eswifi_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,14 @@ int eswifi_mgmt_iface_status(const struct device *dev,
return 0;
}

static int eswifi_mgmt_scan(const struct device *dev, scan_result_cb_t cb)
static int eswifi_mgmt_scan(const struct device *dev,
struct wifi_scan_params *params,
scan_result_cb_t cb)
{
struct eswifi_dev *eswifi = dev->data;

ARG_UNUSED(params);

LOG_DBG("");

eswifi_lock(eswifi);
Expand Down
6 changes: 5 additions & 1 deletion drivers/wifi/simplelink/simplelink.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,15 @@ static void simplelink_scan_work_handler(struct k_work *work)
}
}

static int simplelink_mgmt_scan(const struct device *dev, scan_result_cb_t cb)
static int simplelink_mgmt_scan(const struct device *dev,
struct wifi_scan_params *params,
scan_result_cb_t cb)
{
int err;
int status;

ARG_UNUSED(params);

/* Cancel any previous scan processing in progress: */
k_work_cancel_delayable(&simplelink_data.work);

Expand Down
6 changes: 5 additions & 1 deletion drivers/wifi/winc1500/wifi_winc1500.c
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,12 @@ static void winc1500_thread(void)
}
}

static int winc1500_mgmt_scan(const struct device *dev, scan_result_cb_t cb)
static int winc1500_mgmt_scan(const struct device *dev,
struct wifi_scan_params *params,
scan_result_cb_t cb)
{
ARG_UNUSED(params);

if (w1500_data.scan_cb) {
return -EALREADY;
}
Expand Down
5 changes: 5 additions & 0 deletions include/zephyr/net/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ static inline const char *wifi_link_mode_txt(enum wifi_link_mode link_mode)
}
}

enum wifi_scan_type {
WIFI_SCAN_TYPE_ACTIVE = 0,
WIFI_SCAN_TYPE_PASSIVE,
};

enum wifi_ps {
WIFI_PS_DISABLED = 0,
WIFI_PS_ENABLED,
Expand Down
14 changes: 13 additions & 1 deletion include/zephyr/net/wifi_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ enum net_event_wifi_cmd {

#define NET_EVENT_WIFI_DISCONNECT_COMPLETE \
(_NET_WIFI_EVENT | NET_EVENT_WIFI_CMD_DISCONNECT_COMPLETE)

struct wifi_scan_params {
/* The scan_type is only a hint to the underlying Wi-Fi chip for the
* preferred mode of scan. The actual mode of scan can depend on factors
* such as the Wi-Fi chip implementation support, regulatory domain
* restrictions etc.
*/
enum wifi_scan_type scan_type;
};

/* Each result is provided to the net_mgmt_event_callback
* via its info attribute (see net_mgmt.h)
*/
Expand Down Expand Up @@ -321,7 +331,9 @@ struct net_wifi_mgmt_offload {
* result by the driver. The wifi mgmt part will take care of
* raising the necessary event etc...
*/
int (*scan)(const struct device *dev, scan_result_cb_t cb);
int (*scan)(const struct device *dev,
struct wifi_scan_params *params,
scan_result_cb_t cb);
int (*connect)(const struct device *dev,
struct wifi_connect_req_params *params);
int (*disconnect)(const struct device *dev);
Expand Down
8 changes: 8 additions & 0 deletions subsys/net/l2/wifi/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ config WIFI_MGMT_TWT_CHECK_IP
being unreachable (IP Level) or unable to receive down link traffic
even when it is awake intervals. Rejecting TWT setup till Wi-Fi
interface has a valid IP address might be desirable in most scenarios.

config WIFI_MGMT_FORCED_PASSIVE_SCAN
bool "Force Passive scan"
help
Force passive scan (typically used to reduce power consumption),
the scan type is always sent as passive.
This doesn't guarantee that passive scan will be used, it depends
on the underlying chip implementation to support and honour scan type.
9 changes: 8 additions & 1 deletion subsys/net/l2/wifi/wifi_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,19 @@ static int wifi_scan(uint32_t mgmt_request, struct net_if *iface,
const struct device *dev = net_if_get_device(iface);
struct net_wifi_mgmt_offload *off_api =
(struct net_wifi_mgmt_offload *) dev->api;
struct wifi_scan_params *params = data;

if (off_api == NULL || off_api->scan == NULL) {
return -ENOTSUP;
}

return off_api->scan(dev, scan_result_cb);
if (data && (len == sizeof(*params))) {
#ifdef CONFIG_WIFI_MGMT_FORCED_PASSIVE_SCAN
params->scan_type = WIFI_SCAN_TYPE_PASSIVE;
#endif
}

return off_api->scan(dev, params, scan_result_cb);
}

NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_WIFI_SCAN, wifi_scan);
Expand Down
26 changes: 24 additions & 2 deletions subsys/net/l2/wifi/wifi_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,29 @@ static int cmd_wifi_disconnect(const struct shell *sh, size_t argc,
static int cmd_wifi_scan(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_scan_params params = { 0 };

context.sh = sh;

if (net_mgmt(NET_REQUEST_WIFI_SCAN, iface, NULL, 0)) {
if (argc > 2) {
shell_fprintf(sh, SHELL_WARNING, "Invalid number of arguments\n");
return -ENOEXEC;
}

if (argc == 2) {
if (!strcmp(argv[1], "passive")) {
params.scan_type = WIFI_SCAN_TYPE_PASSIVE;
} else if (!strcmp(argv[1], "active")) {
params.scan_type = WIFI_SCAN_TYPE_ACTIVE;
} else {
shell_fprintf(sh, SHELL_WARNING, "Invalid argument\n");
shell_fprintf(sh, SHELL_INFO,
"Valid argument : <active> / <passive>\n");
return -ENOEXEC;
}
}

if (net_mgmt(NET_REQUEST_WIFI_SCAN, iface, &params, sizeof(params))) {
shell_fprintf(sh, SHELL_WARNING, "Scan request failed\n");

return -ENOEXEC;
Expand Down Expand Up @@ -1114,7 +1133,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(wifi_commands,
cmd_wifi_ps_mode,
2,
0),
SHELL_CMD(scan, NULL, "Scan for Wi-Fi APs", cmd_wifi_scan),
SHELL_CMD(scan, NULL,
"Scan for Wi-Fi APs\n"
"<scan type (optional): <active> : <passive>>\n",
cmd_wifi_scan),
SHELL_CMD(statistics, NULL, "Wi-Fi interface statistics", cmd_wifi_stats),
SHELL_CMD(status, NULL, "Status of the Wi-Fi interface", cmd_wifi_status),
SHELL_CMD(twt, &wifi_twt_ops, "Manage TWT flows", NULL),
Expand Down

0 comments on commit ad39003

Please sign in to comment.