Skip to content

Commit 2b943ba

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

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

include/zephyr/net/wifi_mgmt.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,8 @@ enum wifi_p2p_op {
14651465
* or specific MAC address to query a single peer
14661466
*/
14671467
WIFI_P2P_PEER,
1468+
/** P2P connect to peer */
1469+
WIFI_P2P_CONNECT,
14681470
};
14691471

14701472
/** Wi-Fi P2P discovery type */
@@ -1477,6 +1479,16 @@ enum wifi_p2p_discovery_type {
14771479
WIFI_P2P_FIND_PROGRESSIVE,
14781480
};
14791481

1482+
/** Wi-Fi P2P connection method */
1483+
enum wifi_p2p_connection_method {
1484+
/** Push Button Configuration */
1485+
WIFI_P2P_METHOD_PBC = 0,
1486+
/** Display PIN (device displays PIN for peer to enter) */
1487+
WIFI_P2P_METHOD_DISPLAY,
1488+
/** Keypad PIN (user enters PIN on device) */
1489+
WIFI_P2P_METHOD_KEYPAD,
1490+
};
1491+
14801492
/** Maximum number of P2P peers that can be returned in a single query */
14811493
#define WIFI_P2P_MAX_PEERS CONFIG_WIFI_P2P_MAX_PEERS
14821494

@@ -1496,6 +1508,20 @@ struct wifi_p2p_params {
14961508
struct wifi_p2p_device_info *peers;
14971509
/** Actual number of peers returned */
14981510
uint16_t peer_count;
1511+
/** Connect specific parameters */
1512+
struct {
1513+
/** Connection method */
1514+
enum wifi_p2p_connection_method method;
1515+
/** PIN for display/keypad methods (8 digits)
1516+
* - For DISPLAY: Leave empty, PIN will be generated and returned
1517+
* - For KEYPAD: Provide the PIN to use for connection
1518+
*/
1519+
char pin[WIFI_WPS_PIN_MAX_LEN + 1];
1520+
/** GO intent (0-15, higher values indicate higher willingness to be GO) */
1521+
uint8_t go_intent;
1522+
/** Frequency in MHz (0 = not specified, use default) */
1523+
unsigned int freq;
1524+
} connect;
14991525
};
15001526
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P */
15011527

modules/hostap/src/supp_api.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,6 +2821,87 @@ int supplicant_p2p_oper(const struct device *dev, struct wifi_p2p_params *params
28212821
break;
28222822
}
28232823

2824+
case WIFI_P2P_CONNECT: {
2825+
char addr_str[18];
2826+
const char *method_str = "";
2827+
char freq_str[32] = "";
2828+
2829+
if (!params) {
2830+
wpa_printf(MSG_ERROR, "P2P connect params are NULL");
2831+
return -EINVAL;
2832+
}
2833+
2834+
snprintk(addr_str, sizeof(addr_str), "%02x:%02x:%02x:%02x:%02x:%02x",
2835+
params->peer_addr[0], params->peer_addr[1], params->peer_addr[2],
2836+
params->peer_addr[3], params->peer_addr[4], params->peer_addr[5]);
2837+
2838+
/* Add frequency parameter if specified */
2839+
if (params->connect.freq > 0) {
2840+
snprintk(freq_str, sizeof(freq_str), " freq=%u", params->connect.freq);
2841+
}
2842+
2843+
switch (params->connect.method) {
2844+
case WIFI_P2P_METHOD_PBC:
2845+
method_str = "pbc";
2846+
snprintk(cmd_buf, sizeof(cmd_buf), "P2P_CONNECT %s %s go_intent=%d%s",
2847+
addr_str, method_str, params->connect.go_intent, freq_str);
2848+
break;
2849+
case WIFI_P2P_METHOD_DISPLAY:
2850+
method_str = "pin";
2851+
snprintk(cmd_buf, sizeof(cmd_buf), "P2P_CONNECT %s %s go_intent=%d%s",
2852+
addr_str, method_str, params->connect.go_intent, freq_str);
2853+
break;
2854+
case WIFI_P2P_METHOD_KEYPAD:
2855+
method_str = "keypad";
2856+
if (params->connect.pin[0] == '\0') {
2857+
wpa_printf(MSG_ERROR, "PIN required for keypad method");
2858+
return -EINVAL;
2859+
}
2860+
snprintk(cmd_buf, sizeof(cmd_buf), "P2P_CONNECT %s %s %s go_intent=%d%s",
2861+
addr_str, method_str, params->connect.pin,
2862+
params->connect.go_intent, freq_str);
2863+
break;
2864+
default:
2865+
wpa_printf(MSG_ERROR, "Unknown P2P connection method: %d",
2866+
params->connect.method);
2867+
return -EINVAL;
2868+
}
2869+
2870+
ret = zephyr_wpa_cli_cmd_resp_noprint(wpa_s->ctrl_conn, cmd_buf, resp_buf);
2871+
if (ret < 0) {
2872+
wpa_printf(MSG_ERROR, "P2P_CONNECT command failed: %d", ret);
2873+
return -EIO;
2874+
}
2875+
if (os_strncmp(resp_buf, "FAIL", 4) == 0) {
2876+
wpa_printf(MSG_ERROR, "P2P connect failed: %s", resp_buf);
2877+
return -ENODEV;
2878+
}
2879+
2880+
/* For DISPLAY method, capture the generated PIN from response */
2881+
if (params->connect.method == WIFI_P2P_METHOD_DISPLAY) {
2882+
size_t len = 0;
2883+
char *pos = resp_buf;
2884+
2885+
while (*pos == ' ' || *pos == '\t' || *pos == '\n') {
2886+
pos++;
2887+
}
2888+
2889+
while (*pos != '\0' && *pos != '\n' && *pos != ' ' &&
2890+
len < WIFI_WPS_PIN_MAX_LEN) {
2891+
params->connect.pin[len++] = *pos++;
2892+
}
2893+
params->connect.pin[len] = '\0';
2894+
2895+
if (params->connect.pin[0] == '\0') {
2896+
wpa_printf(MSG_ERROR, "P2P connect: No PIN returned");
2897+
return -ENODEV;
2898+
}
2899+
}
2900+
2901+
ret = 0;
2902+
break;
2903+
}
2904+
28242905
default:
28252906
wpa_printf(MSG_ERROR, "Unknown P2P operation: %d", params->oper);
28262907
ret = -EINVAL;

0 commit comments

Comments
 (0)