Skip to content

Commit 82ec1d7

Browse files
Rex-Chen-NXPnashif
authored andcommitted
net: wifi: shell: add wps support
Add wps pin and wps pbc L2 layer cmd support. Signed-off-by: Rex Chen <rex.chen_1@nxp.com>
1 parent 7d19539 commit 82ec1d7

File tree

7 files changed

+207
-2
lines changed

7 files changed

+207
-2
lines changed

include/zephyr/net/wifi_mgmt.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ enum net_request_wifi_cmd {
106106
NET_REQUEST_WIFI_CMD_ENTERPRISE_CREDS,
107107
/** Get RTS threshold */
108108
NET_REQUEST_WIFI_CMD_RTS_THRESHOLD_CONFIG,
109-
/** @cond INTERNAL_HIDDEN */
109+
/** WPS config */
110+
NET_REQUEST_WIFI_CMD_WPS_CONFIG,
111+
/** @cond INTERNAL_HIDDEN */
110112
NET_REQUEST_WIFI_CMD_MAX
111113
/** @endcond */
112114
};
@@ -250,6 +252,10 @@ NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_ENTERPRISE_CREDS);
250252

251253
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_RTS_THRESHOLD_CONFIG);
252254

255+
#define NET_REQUEST_WIFI_WPS_CONFIG (_NET_WIFI_BASE | NET_REQUEST_WIFI_CMD_WPS_CONFIG)
256+
257+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_WPS_CONFIG);
258+
253259
/** @brief Wi-Fi management events */
254260
enum net_event_wifi_cmd {
255261
/** Scan results available */
@@ -1022,6 +1028,26 @@ struct wifi_dpp_params {
10221028
};
10231029
};
10241030

1031+
#define WIFI_WPS_PIN_MAX_LEN 8
1032+
1033+
/** Operation for WPS */
1034+
enum wifi_wps_op {
1035+
/** WPS pbc */
1036+
WIFI_WPS_PBC = 0,
1037+
/** Get WPS pin number */
1038+
WIFI_WPS_PIN_GET = 1,
1039+
/** Set WPS pin number */
1040+
WIFI_WPS_PIN_SET = 2,
1041+
};
1042+
1043+
/** Wi-Fi wps setup */
1044+
struct wifi_wps_config_params {
1045+
/** wps operation */
1046+
enum wifi_wps_op oper;
1047+
/** pin value*/
1048+
char pin[WIFI_WPS_PIN_MAX_LEN + 1];
1049+
};
1050+
10251051
#include <zephyr/net/net_if.h>
10261052

10271053
/** Scan result callback
@@ -1262,6 +1288,14 @@ struct wifi_mgmt_ops {
12621288
* @return 0 if ok, < 0 if error
12631289
*/
12641290
int (*get_rts_threshold)(const struct device *dev, unsigned int *rts_threshold);
1291+
/** Start a WPS PBC/PIN connection
1292+
*
1293+
* @param dev Pointer to the device structure for the driver instance
1294+
* @param params wps operarion parameters
1295+
*
1296+
* @return 0 if ok, < 0 if error
1297+
*/
1298+
int (*wps_config)(const struct device *dev, struct wifi_wps_config_params *params);
12651299
};
12661300

12671301
/** Wi-Fi management offload API */

modules/hostap/src/supp_api.c

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ static struct wifi_connect_req_params last_wifi_conn_params;
3535

3636
enum requested_ops {
3737
CONNECT = 0,
38-
DISCONNECT
38+
DISCONNECT,
39+
WPS_PBC,
40+
WPS_PIN,
3941
};
4042

4143
enum status_thread_state {
@@ -1259,6 +1261,91 @@ int supplicant_get_wifi_conn_params(const struct device *dev,
12591261
return ret;
12601262
}
12611263

1264+
static int supplicant_wps_pbc(const struct device *dev)
1265+
{
1266+
struct wpa_supplicant *wpa_s;
1267+
int ret = -1;
1268+
1269+
k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER);
1270+
1271+
wpa_s = get_wpa_s_handle(dev);
1272+
if (!wpa_s) {
1273+
ret = -1;
1274+
wpa_printf(MSG_ERROR, "Interface %s not found", dev->name);
1275+
goto out;
1276+
}
1277+
1278+
if (!wpa_cli_cmd_v("wps_pbc")) {
1279+
goto out;
1280+
}
1281+
1282+
wpas_api_ctrl.dev = dev;
1283+
wpas_api_ctrl.requested_op = WPS_PBC;
1284+
1285+
ret = 0;
1286+
1287+
out:
1288+
k_mutex_unlock(&wpa_supplicant_mutex);
1289+
1290+
return ret;
1291+
}
1292+
1293+
static int supplicant_wps_pin(const struct device *dev, struct wifi_wps_config_params *params)
1294+
{
1295+
struct wpa_supplicant *wpa_s;
1296+
char *get_pin_cmd = "WPS_PIN get";
1297+
int ret = -1;
1298+
1299+
k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER);
1300+
1301+
wpa_s = get_wpa_s_handle(dev);
1302+
if (!wpa_s) {
1303+
ret = -1;
1304+
wpa_printf(MSG_ERROR, "Interface %s not found", dev->name);
1305+
goto out;
1306+
}
1307+
1308+
if (params->oper == WIFI_WPS_PIN_GET) {
1309+
if (zephyr_wpa_cli_cmd_resp(get_pin_cmd, params->pin)) {
1310+
goto out;
1311+
}
1312+
} else if (params->oper == WIFI_WPS_PIN_SET) {
1313+
if (!wpa_cli_cmd_v("wps_check_pin %s", params->pin)) {
1314+
goto out;
1315+
}
1316+
1317+
if (!wpa_cli_cmd_v("wps_pin any %s", params->pin)) {
1318+
goto out;
1319+
}
1320+
1321+
wpas_api_ctrl.dev = dev;
1322+
wpas_api_ctrl.requested_op = WPS_PIN;
1323+
} else {
1324+
wpa_printf(MSG_ERROR, "Error wps pin operation : %d", params->oper);
1325+
goto out;
1326+
}
1327+
1328+
ret = 0;
1329+
1330+
out:
1331+
k_mutex_unlock(&wpa_supplicant_mutex);
1332+
1333+
return ret;
1334+
}
1335+
1336+
int supplicant_wps_config(const struct device *dev, struct wifi_wps_config_params *params)
1337+
{
1338+
int ret = 0;
1339+
1340+
if (params->oper == WIFI_WPS_PBC) {
1341+
ret = supplicant_wps_pbc(dev);
1342+
} else if (params->oper == WIFI_WPS_PIN_GET || params->oper == WIFI_WPS_PIN_SET) {
1343+
ret = supplicant_wps_pin(dev, params);
1344+
}
1345+
1346+
return ret;
1347+
}
1348+
12621349
#ifdef CONFIG_AP
12631350
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
12641351
int hapd_state(const struct device *dev, int *state)

modules/hostap/src/supp_api.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ int supplicant_btm_query(const struct device *dev, uint8_t reason);
215215
int supplicant_get_wifi_conn_params(const struct device *dev,
216216
struct wifi_connect_req_params *params);
217217

218+
/** Start a WPS PBC/PIN connection
219+
*
220+
* @param dev Pointer to the device structure for the driver instance
221+
* @param params wps operarion parameters
222+
*
223+
* @return 0 if ok, < 0 if error
224+
*/
225+
int supplicant_wps_config(const struct device *dev, struct wifi_wps_config_params *params);
226+
218227
#ifdef CONFIG_AP
219228
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
220229
/**

modules/hostap/src/supp_main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static const struct wifi_mgmt_ops mgmt_ops = {
7575
.btm_query = supplicant_btm_query,
7676
#endif
7777
.get_conn_params = supplicant_get_wifi_conn_params,
78+
.wps_config = supplicant_wps_config,
7879
#ifdef CONFIG_AP
7980
.ap_enable = supplicant_ap_enable,
8081
.ap_disable = supplicant_ap_disable,

samples/net/wifi/boards/rd_rw612_bga.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ CONFIG_WIFI_NM_WPA_SUPPLICANT_INF_MON=n
8585
CONFIG_WIFI_NM_MAX_MANAGED_INTERFACES=2
8686
CONFIG_SAE_PWE_EARLY_EXIT=y
8787
CONFIG_WIFI_NM_HOSTAPD_AP=y
88+
CONFIG_WIFI_NM_WPA_SUPPLICANT_WPS=y
8889

8990
# Enable mbedtls
9091
CONFIG_MBEDTLS=y

subsys/net/l2/wifi/wifi_mgmt.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,21 @@ static int wifi_get_connection_params(uint32_t mgmt_request, struct net_if *ifac
813813

814814
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_WIFI_CONN_PARAMS, wifi_get_connection_params);
815815

816+
static int wifi_wps_config(uint32_t mgmt_request, struct net_if *iface, void *data, size_t len)
817+
{
818+
const struct device *dev = net_if_get_device(iface);
819+
const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_api(iface);
820+
struct wifi_wps_config_params *params = data;
821+
822+
if (wifi_mgmt_api == NULL || wifi_mgmt_api->wps_config == NULL) {
823+
return -ENOTSUP;
824+
}
825+
826+
return wifi_mgmt_api->wps_config(dev, params);
827+
}
828+
829+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_WIFI_WPS_CONFIG, wifi_wps_config);
830+
816831
static int wifi_set_rts_threshold(uint32_t mgmt_request, struct net_if *iface,
817832
void *data, size_t len)
818833
{

subsys/net/l2/wifi/wifi_shell.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,57 @@ static int cmd_wifi_btm_query(const struct shell *sh, size_t argc, char *argv[])
16901690
}
16911691
#endif
16921692

1693+
static int cmd_wifi_wps_pbc(const struct shell *sh, size_t argc, char *argv[])
1694+
{
1695+
struct net_if *iface = net_if_get_first_wifi();
1696+
struct wifi_wps_config_params params = {0};
1697+
1698+
context.sh = sh;
1699+
1700+
if (argc == 1) {
1701+
params.oper = WIFI_WPS_PBC;
1702+
} else {
1703+
shell_help(sh);
1704+
return -ENOEXEC;
1705+
}
1706+
1707+
if (net_mgmt(NET_REQUEST_WIFI_WPS_CONFIG, iface, &params, sizeof(params))) {
1708+
PR_WARNING("Start wps pbc connection failed\n");
1709+
return -ENOEXEC;
1710+
}
1711+
1712+
return 0;
1713+
}
1714+
1715+
static int cmd_wifi_wps_pin(const struct shell *sh, size_t argc, char *argv[])
1716+
{
1717+
struct net_if *iface = net_if_get_first_wifi();
1718+
struct wifi_wps_config_params params = {0};
1719+
1720+
context.sh = sh;
1721+
1722+
if (argc == 1) {
1723+
params.oper = WIFI_WPS_PIN_GET;
1724+
} else if (argc == 2) {
1725+
params.oper = WIFI_WPS_PIN_SET;
1726+
strncpy(params.pin, argv[1], WIFI_WPS_PIN_MAX_LEN);
1727+
} else {
1728+
shell_help(sh);
1729+
return -ENOEXEC;
1730+
}
1731+
1732+
if (net_mgmt(NET_REQUEST_WIFI_WPS_CONFIG, iface, &params, sizeof(params))) {
1733+
PR_WARNING("Start wps pin connection failed\n");
1734+
return -ENOEXEC;
1735+
}
1736+
1737+
if (params.oper == WIFI_WPS_PIN_GET) {
1738+
PR("WPS PIN is: %s\n", params.pin);
1739+
}
1740+
1741+
return 0;
1742+
}
1743+
16931744
static int cmd_wifi_ps_wakeup_mode(const struct shell *sh, size_t argc, char *argv[])
16941745
{
16951746
struct net_if *iface = net_if_get_first_wifi();
@@ -2912,6 +2963,13 @@ SHELL_STATIC_SUBCMD_SET_CREATE(wifi_commands,
29122963
cmd_wifi_btm_query,
29132964
2, 0),
29142965
#endif
2966+
SHELL_CMD_ARG(wps_pbc, NULL,
2967+
"Start a WPS PBC connection.\n",
2968+
cmd_wifi_wps_pbc, 1, 0),
2969+
SHELL_CMD_ARG(wps_pin, NULL,
2970+
"Set and get WPS pin.\n"
2971+
"[pin] Only applicable for set.\n",
2972+
cmd_wifi_wps_pin, 1, 1),
29152973
SHELL_CMD_ARG(ps_timeout,
29162974
NULL,
29172975
"<val> - PS inactivity timer(in ms).\n",

0 commit comments

Comments
 (0)