Skip to content

Commit f77a05a

Browse files
krish2718rado17
authored andcommitted
[nrf fromtree] modules: hostap: Add interface arg to cli commands
Now that we support multiple VIFs, need to add argument for the interface for any command. Signed-off-by: Chaitanya Tata <[email protected]> (cherry picked from commit e53e5baa22d394efaed03ab76a4708cc36e8c986)
1 parent af97f30 commit f77a05a

File tree

2 files changed

+108
-18
lines changed

2 files changed

+108
-18
lines changed

modules/hostap/src/hapd_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ bool hostapd_ap_reg_domain(const struct device *dev,
376376
return false;
377377
}
378378

379-
return hostapd_cli_cmd_v(iface->ctrl_conn, "set country_code %s", reg_domain->country_code);
379+
return hostapd_cli_cmd_v("set country_code %s", reg_domain->country_code);
380380
}
381381

382382
static int hapd_config_chan_center_seg0(struct hostapd_iface *iface,

modules/hostap/src/wpa_cli.c

Lines changed: 107 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
*/
1010

1111
#include <stdlib.h>
12+
#include <sys/types.h>
1213
#include <zephyr/kernel.h>
1314
#include <zephyr/shell/shell.h>
1415
#include <zephyr/net/net_if.h>
16+
#include <zephyr/net/socket.h>
1517
#include <zephyr/init.h>
1618

1719

@@ -21,28 +23,64 @@
2123
#include "wpa_supplicant_i.h"
2224
#include "wpa_cli_zephyr.h"
2325
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
26+
#include "hostapd.h"
27+
#include "hapd_main.h"
2428
#include "hostapd_cli_zephyr.h"
2529
#endif
2630

27-
static int cmd_wpa_cli(const struct shell *sh,
28-
size_t argc,
29-
const char *argv[])
31+
static int cmd_wpa_cli(const struct shell *sh, size_t argc, const char *argv[])
3032
{
31-
struct net_if *iface = net_if_get_first_wifi();
33+
struct net_if *iface = NULL;
3234
char if_name[CONFIG_NET_INTERFACE_NAME_LEN + 1];
3335
struct wpa_supplicant *wpa_s = NULL;
36+
size_t arg_offset = 1;
37+
int idx = -1;
38+
bool iface_found = false;
39+
40+
if (argc > 2 &&
41+
((strcmp(argv[1], "-i") == 0) ||
42+
(strncmp(argv[1], "-i", 2) == 0 && argv[1][2] != '\0'))) {
43+
/* Handle both "-i 2" and "-i2" */
44+
if (strcmp(argv[1], "-i") == 0) {
45+
idx = strtol(argv[2], NULL, 10);
46+
arg_offset = 3;
47+
} else {
48+
idx = strtol(&argv[1][2], NULL, 10);
49+
arg_offset = 2;
50+
}
51+
iface = net_if_get_by_index(idx);
52+
if (!iface) {
53+
shell_error(sh, "Interface index %d not found", idx);
54+
return -ENODEV;
55+
}
56+
net_if_get_name(iface, if_name, sizeof(if_name));
57+
if_name[sizeof(if_name) - 1] = '\0';
58+
iface_found = true;
59+
} else {
60+
/* Default to first Wi-Fi interface */
61+
iface = net_if_get_first_wifi();
62+
if (!iface) {
63+
shell_error(sh, "No Wi-Fi interface found");
64+
return -ENOENT;
65+
}
66+
net_if_get_name(iface, if_name, sizeof(if_name));
67+
if_name[sizeof(if_name) - 1] = '\0';
68+
arg_offset = 1;
69+
iface_found = true;
70+
}
3471

35-
ARG_UNUSED(sh);
36-
37-
if (iface == NULL) {
38-
shell_error(sh, "No Wifi interface found");
39-
return -ENOENT;
72+
if (!iface_found) {
73+
shell_error(sh, "No interface found");
74+
return -ENODEV;
4075
}
4176

42-
net_if_get_name(iface, if_name, sizeof(if_name));
4377
wpa_s = zephyr_get_handle_by_ifname(if_name);
78+
if (!wpa_s) {
79+
shell_error(sh, "No wpa_supplicant context for interface '%s'", if_name);
80+
return -ENODEV;
81+
}
4482

45-
if (argc == 1) {
83+
if (argc <= arg_offset) {
4684
shell_error(sh, "Missing argument");
4785
return -EINVAL;
4886
}
@@ -51,15 +89,65 @@ static int cmd_wpa_cli(const struct shell *sh,
5189
argc++;
5290

5391
/* Remove wpa_cli from the argument list */
54-
return zephyr_wpa_ctrl_zephyr_cmd(wpa_s->ctrl_conn, argc - 1, &argv[1]);
92+
return zephyr_wpa_ctrl_zephyr_cmd(wpa_s->ctrl_conn, argc - arg_offset, &argv[arg_offset]);
5593
}
5694

5795
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
5896
static int cmd_hostapd_cli(const struct shell *sh, size_t argc, const char *argv[])
5997
{
60-
ARG_UNUSED(sh);
98+
struct net_if *iface = NULL;
99+
size_t arg_offset = 1;
100+
struct hostapd_iface *hapd_iface;
101+
int idx = -1;
102+
bool iface_found = false;
103+
char if_name[CONFIG_NET_INTERFACE_NAME_LEN + 1];
104+
int ret;
105+
106+
if (argc > 2 &&
107+
((strcmp(argv[1], "-i") == 0) ||
108+
(strncmp(argv[1], "-i", 2) == 0 && argv[1][2] != '\0'))) {
109+
/* Handle both "-i 2" and "-i2" */
110+
if (strcmp(argv[1], "-i") == 0) {
111+
idx = strtol(argv[2], NULL, 10);
112+
arg_offset = 3;
113+
} else {
114+
idx = strtol(&argv[1][2], NULL, 10);
115+
arg_offset = 2;
116+
}
117+
iface = net_if_get_by_index(idx);
118+
if (!iface) {
119+
shell_error(sh, "Interface index %d not found", idx);
120+
return -ENODEV;
121+
}
122+
iface_found = true;
123+
} else {
124+
iface = net_if_get_first_wifi();
125+
if (!iface) {
126+
shell_error(sh, "No Wi-Fi interface found");
127+
return -ENOENT;
128+
}
129+
arg_offset = 1;
130+
iface_found = true;
131+
}
132+
133+
if (!iface_found) {
134+
shell_error(sh, "No interface found");
135+
return -ENODEV;
136+
}
137+
138+
ret = net_if_get_name(iface, if_name, sizeof(if_name));
139+
if (!ret) {
140+
shell_error(sh, "Cannot get interface name (%d)", ret);
141+
return -ENODEV;
142+
}
143+
144+
hapd_iface = zephyr_get_hapd_handle_by_ifname(if_name);
145+
if (!hapd_iface) {
146+
shell_error(sh, "No hostapd context for interface '%s'", if_name);
147+
return -ENODEV;
148+
}
61149

62-
if (argc == 1) {
150+
if (argc <= arg_offset) {
63151
shell_error(sh, "Missing argument");
64152
return -EINVAL;
65153
}
@@ -68,7 +156,8 @@ static int cmd_hostapd_cli(const struct shell *sh, size_t argc, const char *argv
68156
argc++;
69157

70158
/* Remove hostapd_cli from the argument list */
71-
return zephyr_hostapd_ctrl_zephyr_cmd(argc - 1, &argv[1]);
159+
return zephyr_hostapd_ctrl_zephyr_cmd(hapd_iface->ctrl_conn, argc - arg_offset,
160+
&argv[arg_offset]);
72161
}
73162
#endif
74163

@@ -77,9 +166,10 @@ static int cmd_hostapd_cli(const struct shell *sh, size_t argc, const char *argv
77166
*/
78167
SHELL_CMD_REGISTER(wpa_cli,
79168
NULL,
80-
"wpa_cli commands (only for internal use)",
169+
"wpa_cli [-i idx] <command> (only for internal use)",
81170
cmd_wpa_cli);
82171
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
83-
SHELL_CMD_REGISTER(hostapd_cli, NULL, "hostapd_cli commands (only for internal use)",
172+
SHELL_CMD_REGISTER(hostapd_cli, NULL,
173+
"hostapd_cli [-i idx] <command> (only for internal use)",
84174
cmd_hostapd_cli);
85175
#endif

0 commit comments

Comments
 (0)