Skip to content

Commit a3b3c72

Browse files
committed
[nrf fromlist] net: wifi: Add Wi-Fi direct P2P connect shell command support
Add shell command support for P2P connect. Upstream PR #: 97183 Signed-off-by: Kapil Bhatt <[email protected]>
1 parent 2b943ba commit a3b3c72

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

subsys/net/l2/wifi/wifi_shell.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3726,6 +3726,106 @@ static int cmd_wifi_p2p_stop_find(const struct shell *sh, size_t argc, char *arg
37263726
PR("P2P find stopped\n");
37273727
return 0;
37283728
}
3729+
3730+
static int cmd_wifi_p2p_connect(const struct shell *sh, size_t argc, char *argv[])
3731+
{
3732+
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
3733+
struct wifi_p2p_params params = {0};
3734+
uint8_t mac_addr[WIFI_MAC_ADDR_LEN];
3735+
const char *method_arg = NULL;
3736+
int opt;
3737+
int opt_index = 0;
3738+
struct sys_getopt_state *state;
3739+
static const struct sys_getopt_option long_options[] = {
3740+
{"go-intent", sys_getopt_required_argument, 0, 'g'},
3741+
{"freq", sys_getopt_required_argument, 0, 'f'},
3742+
{"iface", sys_getopt_required_argument, 0, 'i'},
3743+
{"help", sys_getopt_no_argument, 0, 'h'},
3744+
{0, 0, 0, 0}
3745+
};
3746+
long val;
3747+
3748+
context.sh = sh;
3749+
3750+
if (argc < 3) {
3751+
PR_ERROR("Usage: wifi p2p connect <MAC address> <pbc|pin> [PIN] "
3752+
"[--go-intent=<0-15>] [--freq=<frequency>]\n");
3753+
return -EINVAL;
3754+
}
3755+
3756+
/* Parse MAC address */
3757+
if (sscanf(argv[1], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
3758+
&mac_addr[0], &mac_addr[1], &mac_addr[2],
3759+
&mac_addr[3], &mac_addr[4], &mac_addr[5]) != WIFI_MAC_ADDR_LEN) {
3760+
PR_ERROR("Invalid MAC address format. Use: XX:XX:XX:XX:XX:XX\n");
3761+
return -EINVAL;
3762+
}
3763+
memcpy(params.peer_addr, mac_addr, WIFI_MAC_ADDR_LEN);
3764+
3765+
method_arg = argv[2];
3766+
if (strcmp(method_arg, "pbc") == 0) {
3767+
params.connect.method = WIFI_P2P_METHOD_PBC;
3768+
} else if (strcmp(method_arg, "pin") == 0) {
3769+
if (argc > 3 && argv[3][0] != '-') {
3770+
params.connect.method = WIFI_P2P_METHOD_KEYPAD;
3771+
strncpy(params.connect.pin, argv[3], WIFI_WPS_PIN_MAX_LEN);
3772+
params.connect.pin[WIFI_WPS_PIN_MAX_LEN] = '\0';
3773+
} else {
3774+
params.connect.method = WIFI_P2P_METHOD_DISPLAY;
3775+
params.connect.pin[0] = '\0';
3776+
}
3777+
} else {
3778+
PR_ERROR("Invalid connection method. Use: pbc or pin\n");
3779+
return -EINVAL;
3780+
}
3781+
3782+
/* Set default GO intent */
3783+
params.connect.go_intent = 0;
3784+
/* Set default frequency to 2462 MHz (channel 11, 2.4 GHz) */
3785+
params.connect.freq = 2462;
3786+
3787+
while ((opt = sys_getopt_long(argc, argv, "g:f:i:h", long_options, &opt_index)) != -1) {
3788+
state = sys_getopt_state_get();
3789+
switch (opt) {
3790+
case 'g':
3791+
if (!parse_number(sh, &val, state->optarg, "go-intent", 0, 15)) {
3792+
return -EINVAL;
3793+
}
3794+
params.connect.go_intent = (uint8_t)val;
3795+
break;
3796+
case 'f':
3797+
if (!parse_number(sh, &val, state->optarg, "freq", 0, 6000)) {
3798+
return -EINVAL;
3799+
}
3800+
params.connect.freq = (unsigned int)val;
3801+
break;
3802+
case 'i':
3803+
/* Unused, but parsing to avoid unknown option error */
3804+
break;
3805+
case 'h':
3806+
shell_help(sh);
3807+
return -ENOEXEC;
3808+
default:
3809+
PR_ERROR("Invalid option %c\n", state->optopt);
3810+
return -EINVAL;
3811+
}
3812+
}
3813+
3814+
params.oper = WIFI_P2P_CONNECT;
3815+
3816+
if (net_mgmt(NET_REQUEST_WIFI_P2P_OPER, iface, &params, sizeof(params))) {
3817+
PR_WARNING("P2P connect request failed\n");
3818+
return -ENOEXEC;
3819+
}
3820+
3821+
/* Display the generated PIN for DISPLAY method */
3822+
if (params.connect.method == WIFI_P2P_METHOD_DISPLAY && params.connect.pin[0] != '\0') {
3823+
PR("%s\n", params.connect.pin);
3824+
} else {
3825+
PR("P2P connection initiated\n");
3826+
}
3827+
return 0;
3828+
}
37293829
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P */
37303830

37313831
static int cmd_wifi_pmksa_flush(const struct shell *sh, size_t argc, char *argv[])
@@ -4455,6 +4555,23 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
44554555
"<MAC address>: Show detailed info for specific peer (format: XX:XX:XX:XX:XX:XX)\n"
44564556
"[-i, --iface=<interface index>]: Interface index\n",
44574557
cmd_wifi_p2p_peer, 1, 3),
4558+
SHELL_CMD_ARG(connect, NULL,
4559+
"Connect to a P2P peer\n"
4560+
"Usage: connect <MAC address> <pbc|pin> [PIN] [options]\n"
4561+
"<MAC address>: Peer device MAC address (format: XX:XX:XX:XX:XX:XX)\n"
4562+
"<pbc>: Use Push Button Configuration\n"
4563+
"<pin>: Use PIN method\n"
4564+
" - Without PIN: Device displays generated PIN for peer to enter\n"
4565+
" - With PIN: Device uses provided PIN to connect\n"
4566+
"[PIN]: 8-digit PIN (optional, generates if omitted)\n"
4567+
"[-g, --go-intent=<0-15>]: GO intent (0=client, 15=GO, default: 0)\n"
4568+
"[-f, --freq=<frequency>]: Frequency in MHz (default: 2462)\n"
4569+
"[-i, --iface=<interface index>]: Interface index\n"
4570+
"[-h, --help]: Show help\n"
4571+
"Examples:\n"
4572+
" wifi p2p connect 9c:b1:50:e3:81:96 pin -g 0 (displays PIN)\n"
4573+
" wifi p2p connect 9c:b1:50:e3:81:96 pin 12345670 -g 0 (uses PIN)\n",
4574+
cmd_wifi_p2p_connect, 3, 5),
44584575
SHELL_SUBCMD_SET_END
44594576
);
44604577

0 commit comments

Comments
 (0)