Skip to content

Commit

Permalink
net: wifi: shell: add wps support
Browse files Browse the repository at this point in the history
Add wps pin and wps pbc L2 layer cmd support.

Signed-off-by: Rex Chen <[email protected]>
  • Loading branch information
Rex-Chen-NXP authored and nashif committed Oct 5, 2024
1 parent 7d19539 commit 82ec1d7
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 2 deletions.
36 changes: 35 additions & 1 deletion include/zephyr/net/wifi_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ enum net_request_wifi_cmd {
NET_REQUEST_WIFI_CMD_ENTERPRISE_CREDS,
/** Get RTS threshold */
NET_REQUEST_WIFI_CMD_RTS_THRESHOLD_CONFIG,
/** @cond INTERNAL_HIDDEN */
/** WPS config */
NET_REQUEST_WIFI_CMD_WPS_CONFIG,
/** @cond INTERNAL_HIDDEN */
NET_REQUEST_WIFI_CMD_MAX
/** @endcond */
};
Expand Down Expand Up @@ -250,6 +252,10 @@ NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_ENTERPRISE_CREDS);

NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_RTS_THRESHOLD_CONFIG);

#define NET_REQUEST_WIFI_WPS_CONFIG (_NET_WIFI_BASE | NET_REQUEST_WIFI_CMD_WPS_CONFIG)

NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_WPS_CONFIG);

/** @brief Wi-Fi management events */
enum net_event_wifi_cmd {
/** Scan results available */
Expand Down Expand Up @@ -1022,6 +1028,26 @@ struct wifi_dpp_params {
};
};

#define WIFI_WPS_PIN_MAX_LEN 8

/** Operation for WPS */
enum wifi_wps_op {
/** WPS pbc */
WIFI_WPS_PBC = 0,
/** Get WPS pin number */
WIFI_WPS_PIN_GET = 1,
/** Set WPS pin number */
WIFI_WPS_PIN_SET = 2,
};

/** Wi-Fi wps setup */
struct wifi_wps_config_params {
/** wps operation */
enum wifi_wps_op oper;
/** pin value*/
char pin[WIFI_WPS_PIN_MAX_LEN + 1];
};

#include <zephyr/net/net_if.h>

/** Scan result callback
Expand Down Expand Up @@ -1262,6 +1288,14 @@ struct wifi_mgmt_ops {
* @return 0 if ok, < 0 if error
*/
int (*get_rts_threshold)(const struct device *dev, unsigned int *rts_threshold);
/** Start a WPS PBC/PIN connection
*
* @param dev Pointer to the device structure for the driver instance
* @param params wps operarion parameters
*
* @return 0 if ok, < 0 if error
*/
int (*wps_config)(const struct device *dev, struct wifi_wps_config_params *params);
};

/** Wi-Fi management offload API */
Expand Down
89 changes: 88 additions & 1 deletion modules/hostap/src/supp_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ static struct wifi_connect_req_params last_wifi_conn_params;

enum requested_ops {
CONNECT = 0,
DISCONNECT
DISCONNECT,
WPS_PBC,
WPS_PIN,
};

enum status_thread_state {
Expand Down Expand Up @@ -1259,6 +1261,91 @@ int supplicant_get_wifi_conn_params(const struct device *dev,
return ret;
}

static int supplicant_wps_pbc(const struct device *dev)
{
struct wpa_supplicant *wpa_s;
int ret = -1;

k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER);

wpa_s = get_wpa_s_handle(dev);
if (!wpa_s) {
ret = -1;
wpa_printf(MSG_ERROR, "Interface %s not found", dev->name);
goto out;
}

if (!wpa_cli_cmd_v("wps_pbc")) {
goto out;
}

wpas_api_ctrl.dev = dev;
wpas_api_ctrl.requested_op = WPS_PBC;

ret = 0;

out:
k_mutex_unlock(&wpa_supplicant_mutex);

return ret;
}

static int supplicant_wps_pin(const struct device *dev, struct wifi_wps_config_params *params)
{
struct wpa_supplicant *wpa_s;
char *get_pin_cmd = "WPS_PIN get";
int ret = -1;

k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER);

wpa_s = get_wpa_s_handle(dev);
if (!wpa_s) {
ret = -1;
wpa_printf(MSG_ERROR, "Interface %s not found", dev->name);
goto out;
}

if (params->oper == WIFI_WPS_PIN_GET) {
if (zephyr_wpa_cli_cmd_resp(get_pin_cmd, params->pin)) {
goto out;
}
} else if (params->oper == WIFI_WPS_PIN_SET) {
if (!wpa_cli_cmd_v("wps_check_pin %s", params->pin)) {
goto out;
}

if (!wpa_cli_cmd_v("wps_pin any %s", params->pin)) {
goto out;
}

wpas_api_ctrl.dev = dev;
wpas_api_ctrl.requested_op = WPS_PIN;
} else {
wpa_printf(MSG_ERROR, "Error wps pin operation : %d", params->oper);
goto out;
}

ret = 0;

out:
k_mutex_unlock(&wpa_supplicant_mutex);

return ret;
}

int supplicant_wps_config(const struct device *dev, struct wifi_wps_config_params *params)
{
int ret = 0;

if (params->oper == WIFI_WPS_PBC) {
ret = supplicant_wps_pbc(dev);
} else if (params->oper == WIFI_WPS_PIN_GET || params->oper == WIFI_WPS_PIN_SET) {
ret = supplicant_wps_pin(dev, params);
}

return ret;
}

#ifdef CONFIG_AP
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
int hapd_state(const struct device *dev, int *state)
Expand Down
9 changes: 9 additions & 0 deletions modules/hostap/src/supp_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ int supplicant_btm_query(const struct device *dev, uint8_t reason);
int supplicant_get_wifi_conn_params(const struct device *dev,
struct wifi_connect_req_params *params);

/** Start a WPS PBC/PIN connection
*
* @param dev Pointer to the device structure for the driver instance
* @param params wps operarion parameters
*
* @return 0 if ok, < 0 if error
*/
int supplicant_wps_config(const struct device *dev, struct wifi_wps_config_params *params);

#ifdef CONFIG_AP
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
/**
Expand Down
1 change: 1 addition & 0 deletions modules/hostap/src/supp_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static const struct wifi_mgmt_ops mgmt_ops = {
.btm_query = supplicant_btm_query,
#endif
.get_conn_params = supplicant_get_wifi_conn_params,
.wps_config = supplicant_wps_config,
#ifdef CONFIG_AP
.ap_enable = supplicant_ap_enable,
.ap_disable = supplicant_ap_disable,
Expand Down
1 change: 1 addition & 0 deletions samples/net/wifi/boards/rd_rw612_bga.conf
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ CONFIG_WIFI_NM_WPA_SUPPLICANT_INF_MON=n
CONFIG_WIFI_NM_MAX_MANAGED_INTERFACES=2
CONFIG_SAE_PWE_EARLY_EXIT=y
CONFIG_WIFI_NM_HOSTAPD_AP=y
CONFIG_WIFI_NM_WPA_SUPPLICANT_WPS=y

# Enable mbedtls
CONFIG_MBEDTLS=y
Expand Down
15 changes: 15 additions & 0 deletions subsys/net/l2/wifi/wifi_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,21 @@ static int wifi_get_connection_params(uint32_t mgmt_request, struct net_if *ifac

NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_WIFI_CONN_PARAMS, wifi_get_connection_params);

static int wifi_wps_config(uint32_t mgmt_request, struct net_if *iface, void *data, size_t len)
{
const struct device *dev = net_if_get_device(iface);
const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_api(iface);
struct wifi_wps_config_params *params = data;

if (wifi_mgmt_api == NULL || wifi_mgmt_api->wps_config == NULL) {
return -ENOTSUP;
}

return wifi_mgmt_api->wps_config(dev, params);
}

NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_WIFI_WPS_CONFIG, wifi_wps_config);

static int wifi_set_rts_threshold(uint32_t mgmt_request, struct net_if *iface,
void *data, size_t len)
{
Expand Down
58 changes: 58 additions & 0 deletions subsys/net/l2/wifi/wifi_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,57 @@ static int cmd_wifi_btm_query(const struct shell *sh, size_t argc, char *argv[])
}
#endif

static int cmd_wifi_wps_pbc(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_wps_config_params params = {0};

context.sh = sh;

if (argc == 1) {
params.oper = WIFI_WPS_PBC;
} else {
shell_help(sh);
return -ENOEXEC;
}

if (net_mgmt(NET_REQUEST_WIFI_WPS_CONFIG, iface, &params, sizeof(params))) {
PR_WARNING("Start wps pbc connection failed\n");
return -ENOEXEC;
}

return 0;
}

static int cmd_wifi_wps_pin(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_wps_config_params params = {0};

context.sh = sh;

if (argc == 1) {
params.oper = WIFI_WPS_PIN_GET;
} else if (argc == 2) {
params.oper = WIFI_WPS_PIN_SET;
strncpy(params.pin, argv[1], WIFI_WPS_PIN_MAX_LEN);
} else {
shell_help(sh);
return -ENOEXEC;
}

if (net_mgmt(NET_REQUEST_WIFI_WPS_CONFIG, iface, &params, sizeof(params))) {
PR_WARNING("Start wps pin connection failed\n");
return -ENOEXEC;
}

if (params.oper == WIFI_WPS_PIN_GET) {
PR("WPS PIN is: %s\n", params.pin);
}

return 0;
}

static int cmd_wifi_ps_wakeup_mode(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
Expand Down Expand Up @@ -2912,6 +2963,13 @@ SHELL_STATIC_SUBCMD_SET_CREATE(wifi_commands,
cmd_wifi_btm_query,
2, 0),
#endif
SHELL_CMD_ARG(wps_pbc, NULL,
"Start a WPS PBC connection.\n",
cmd_wifi_wps_pbc, 1, 0),
SHELL_CMD_ARG(wps_pin, NULL,
"Set and get WPS pin.\n"
"[pin] Only applicable for set.\n",
cmd_wifi_wps_pin, 1, 1),
SHELL_CMD_ARG(ps_timeout,
NULL,
"<val> - PS inactivity timer(in ms).\n",
Expand Down

0 comments on commit 82ec1d7

Please sign in to comment.