Skip to content

Commit 6460331

Browse files
committed
net: wifi: add P2P iface type and shell command routing
Introduce WIFI_TYPE_P2P to the wifi_nm_iface_type enum and register a corresponding net_if_get_wifi_p2p() lookup function. All P2P shell commands are updated to use IFACE_TYPE_P2P instead of IFACE_TYPE_STA, with automatic fallback to the STA interface when no dedicated P2P interface is registered. Fix the supplicant context default-interface selection in add_interface(): use == 1 instead of > 0 so that the first registered interface (STA) is captured as ctx->iface rather than being overwritten by subsequent UAP/P2P registrations. All new symbols are guarded by CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P. Signed-off-by: Hui Bai <hui.bai@nxp.com>
1 parent 25249c3 commit 6460331

6 files changed

Lines changed: 77 additions & 10 deletions

File tree

include/zephyr/net/net_if.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3410,6 +3410,18 @@ struct net_if *net_if_get_wifi_sta(void);
34103410
*/
34113411
struct net_if *net_if_get_wifi_sap(void);
34123412

3413+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P
3414+
/**
3415+
* @brief Get Wi-Fi P2P (Wi-Fi Direct) network interface.
3416+
*
3417+
* Returns the iface registered as WIFI_TYPE_P2P, or falls back to
3418+
* the STA iface for drivers that run P2P on the STA interface.
3419+
*
3420+
* @return Pointer to network interface, NULL if not found.
3421+
*/
3422+
struct net_if *net_if_get_wifi_p2p(void);
3423+
#endif
3424+
34133425
/**
34143426
* @brief Get network interface name.
34153427
*

include/zephyr/net/wifi_nm.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ enum wifi_nm_iface_type {
3838
WIFI_TYPE_STA = 0,
3939
/** IEEE 802.11 Wi-Fi Soft AP */
4040
WIFI_TYPE_SAP,
41+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P
42+
/** IEEE 802.11 Wi-Fi P2P (Wi-Fi Direct) */
43+
WIFI_TYPE_P2P,
44+
#endif
4145
};
4246

4347
/**
@@ -119,6 +123,18 @@ bool wifi_nm_iface_is_sta(struct net_if *iface);
119123
*/
120124
bool wifi_nm_iface_is_sap(struct net_if *iface);
121125

126+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P
127+
/**
128+
* @brief Check if the interface is a Wi-Fi P2P (Wi-Fi Direct) interface
129+
*
130+
* @param iface Interface
131+
*
132+
* @retval true If the interface is a Wi-Fi P2P interface.
133+
*
134+
*/
135+
bool wifi_nm_iface_is_p2p(struct net_if *iface);
136+
#endif
137+
122138
/**
123139
* @brief Register a managed interface
124140
*

modules/hostap/src/supp_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static int add_interface(struct supplicant_context *ctx, struct net_if *iface)
378378
wpa_s->conf->ap_scan = 1;
379379

380380
/* Default interface, kick start supplicant */
381-
if (get_iface_count(ctx) > 0) {
381+
if (get_iface_count(ctx) == 1) {
382382
ctx->iface = iface;
383383
net_if_get_name(iface, ctx->if_name, CONFIG_NET_INTERFACE_NAME_LEN);
384384
}

subsys/net/ip/net_if.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6670,6 +6670,26 @@ struct net_if *net_if_get_wifi_sap(void)
66706670
return net_if_get_first_wifi();
66716671
}
66726672

6673+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P
6674+
struct net_if *net_if_get_wifi_p2p(void)
6675+
{
6676+
STRUCT_SECTION_FOREACH(net_if, iface) {
6677+
if (net_if_is_wifi(iface)
6678+
#ifdef CONFIG_WIFI_NM
6679+
&& wifi_nm_iface_is_p2p(iface)
6680+
#endif
6681+
) {
6682+
return iface;
6683+
}
6684+
}
6685+
6686+
/* No dedicated P2P iface registered.
6687+
* Return the first WiFi interface.
6688+
*/
6689+
return net_if_get_first_wifi();
6690+
}
6691+
#endif
6692+
66736693
int net_if_get_name(struct net_if *iface, char *buf, int len)
66746694
{
66756695
#if defined(CONFIG_NET_INTERFACE_NAME)

subsys/net/l2/wifi/wifi_nm.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ bool wifi_nm_iface_is_sap(struct net_if *iface)
7474
return wifi_nm_get_type_iface(iface) & BIT(WIFI_TYPE_SAP);
7575
}
7676

77+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P
78+
bool wifi_nm_iface_is_p2p(struct net_if *iface)
79+
{
80+
return wifi_nm_get_type_iface(iface) & BIT(WIFI_TYPE_P2P);
81+
}
82+
#endif
83+
7784
int wifi_nm_register_mgd_iface(struct wifi_nm_instance *nm, struct net_if *iface)
7885
{
7986
if (!nm || !iface) {

subsys/net/l2/wifi/wifi_shell.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static struct wifi_ap_sta_node sta_list[CONFIG_WIFI_SHELL_MAX_AP_STA];
108108
enum iface_type {
109109
IFACE_TYPE_STA,
110110
IFACE_TYPE_SAP,
111+
IFACE_TYPE_P2P,
111112
};
112113

113114
static struct net_if *get_iface(enum iface_type type, int argc, char *argv[])
@@ -148,10 +149,21 @@ static struct net_if *get_iface(enum iface_type type, int argc, char *argv[])
148149
} else if (type == IFACE_TYPE_SAP) {
149150
iface = net_if_get_wifi_sap();
150151
}
152+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P
153+
else if (type == IFACE_TYPE_P2P) {
154+
iface = net_if_get_wifi_p2p();
155+
}
156+
#endif
151157

152158
if (iface == NULL) {
153159
LOG_ERR("No default interface found for type: %s",
154-
type == IFACE_TYPE_STA ? "STA" : "SAP");
160+
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_P2P
161+
type == IFACE_TYPE_STA ? "STA" :
162+
type == IFACE_TYPE_SAP ? "SAP" : "P2P"
163+
#else
164+
type == IFACE_TYPE_STA ? "STA" : "SAP"
165+
#endif
166+
);
155167
return NULL;
156168
}
157169
}
@@ -3937,7 +3949,7 @@ static void print_peer_info(const struct shell *sh, int index,
39373949

39383950
static int cmd_wifi_p2p_peer(const struct shell *sh, size_t argc, char *argv[])
39393951
{
3940-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
3952+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
39413953
struct wifi_p2p_params params = {0};
39423954
uint8_t mac_addr[WIFI_MAC_ADDR_LEN];
39433955
static struct wifi_p2p_device_info peers[WIFI_P2P_MAX_PEERS];
@@ -3992,7 +4004,7 @@ static int cmd_wifi_p2p_peer(const struct shell *sh, size_t argc, char *argv[])
39924004

39934005
static int cmd_wifi_p2p_find(const struct shell *sh, size_t argc, char *argv[])
39944006
{
3995-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
4007+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
39964008
struct wifi_p2p_params params = {0};
39974009

39984010
context.sh = sh;
@@ -4065,7 +4077,7 @@ static int cmd_wifi_p2p_find(const struct shell *sh, size_t argc, char *argv[])
40654077

40664078
static int cmd_wifi_p2p_stop_find(const struct shell *sh, size_t argc, char *argv[])
40674079
{
4068-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
4080+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
40694081
struct wifi_p2p_params params = {0};
40704082

40714083
context.sh = sh;
@@ -4082,7 +4094,7 @@ static int cmd_wifi_p2p_stop_find(const struct shell *sh, size_t argc, char *arg
40824094

40834095
static int cmd_wifi_p2p_connect(const struct shell *sh, size_t argc, char *argv[])
40844096
{
4085-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
4097+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
40864098
struct wifi_p2p_params params = {0};
40874099
uint8_t mac_addr[WIFI_MAC_ADDR_LEN];
40884100
const char *method_arg = NULL;
@@ -4188,7 +4200,7 @@ static int cmd_wifi_p2p_connect(const struct shell *sh, size_t argc, char *argv[
41884200

41894201
static int cmd_wifi_p2p_group_add(const struct shell *sh, size_t argc, char *argv[])
41904202
{
4191-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
4203+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
41924204
struct wifi_p2p_params params = {0};
41934205
int opt;
41944206
int opt_index = 0;
@@ -4280,7 +4292,7 @@ static int cmd_wifi_p2p_group_add(const struct shell *sh, size_t argc, char *arg
42804292

42814293
static int cmd_wifi_p2p_group_remove(const struct shell *sh, size_t argc, char *argv[])
42824294
{
4283-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
4295+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
42844296
struct wifi_p2p_params params = {0};
42854297

42864298
context.sh = sh;
@@ -4305,7 +4317,7 @@ static int cmd_wifi_p2p_group_remove(const struct shell *sh, size_t argc, char *
43054317

43064318
static int cmd_wifi_p2p_invite(const struct shell *sh, size_t argc, char *argv[])
43074319
{
4308-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
4320+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
43094321
struct wifi_p2p_params params = {0};
43104322
uint8_t mac_addr[WIFI_MAC_ADDR_LEN];
43114323
int opt;
@@ -4438,7 +4450,7 @@ static int cmd_wifi_p2p_invite(const struct shell *sh, size_t argc, char *argv[]
44384450

44394451
static int cmd_wifi_p2p_power_save(const struct shell *sh, size_t argc, char *argv[])
44404452
{
4441-
struct net_if *iface = get_iface(IFACE_TYPE_STA, argc, argv);
4453+
struct net_if *iface = get_iface(IFACE_TYPE_P2P, argc, argv);
44424454
struct wifi_p2p_params params = {0};
44434455
bool power_save_enable = false;
44444456

0 commit comments

Comments
 (0)