Skip to content

Commit 712c6cc

Browse files
kapbhrado17
authored andcommitted
[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 76f1972 commit 712c6cc

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

subsys/net/l2/wifi/wifi_shell.c

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

37273818
static int cmd_wifi_pmksa_flush(const struct shell *sh, size_t argc, char *argv[])
@@ -4449,6 +4540,22 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
44494540
"<MAC address>: Show detailed info for specific peer (format: XX:XX:XX:XX:XX:XX)\n"
44504541
"[-i, --iface=<interface index>]: Interface index\n",
44514542
cmd_wifi_p2p_peer, 1, 3),
4543+
SHELL_CMD_ARG(connect, NULL,
4544+
"Connect to a P2P peer\n"
4545+
"Usage: connect <MAC address> <pbc|pin> [PIN] [options]\n"
4546+
"<MAC address>: Peer device MAC address (format: XX:XX:XX:XX:XX:XX)\n"
4547+
"<pbc>: Use Push Button Configuration\n"
4548+
"<pin>: Use PIN method\n"
4549+
" - Without PIN: Device displays generated PIN for peer to enter\n"
4550+
" - With PIN: Device uses provided PIN to connect\n"
4551+
"[PIN]: 8-digit PIN (optional, generates if omitted)\n"
4552+
"[-g, --go-intent=<0-15>]: GO intent (0=client, 15=GO, default: 0)\n"
4553+
"[-i, --iface=<interface index>]: Interface index\n"
4554+
"[-h, --help]: Show help\n"
4555+
"Examples:\n"
4556+
" wifi p2p connect 9c:b1:50:e3:81:96 pin -g 0 (displays PIN)\n"
4557+
" wifi p2p connect 9c:b1:50:e3:81:96 pin 12345670 -g 0 (uses PIN)\n",
4558+
cmd_wifi_p2p_connect, 3, 7),
44524559
SHELL_SUBCMD_SET_END
44534560
);
44544561

0 commit comments

Comments
 (0)