Skip to content

Commit 0c54a3f

Browse files
GaofengZhangNXPnashif
authored andcommitted
hostapd: add ap status in l2 wifi
add ap status in l2 wifi Signed-off-by: Gaofeng Zhang <[email protected]>
1 parent 193bfab commit 0c54a3f

File tree

6 files changed

+188
-36
lines changed

6 files changed

+188
-36
lines changed

include/zephyr/net/wifi_mgmt.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,18 @@ struct wifi_wps_config_params {
10721072
char pin[WIFI_WPS_PIN_MAX_LEN + 1];
10731073
};
10741074

1075+
/** Wi-Fi AP status
1076+
*/
1077+
enum wifi_hostapd_iface_state {
1078+
WIFI_HAPD_IFACE_UNINITIALIZED,
1079+
WIFI_HAPD_IFACE_DISABLED,
1080+
WIFI_HAPD_IFACE_COUNTRY_UPDATE,
1081+
WIFI_HAPD_IFACE_ACS,
1082+
WIFI_HAPD_IFACE_HT_SCAN,
1083+
WIFI_HAPD_IFACE_DFS,
1084+
WIFI_HAPD_IFACE_ENABLED
1085+
};
1086+
10751087
#include <zephyr/net/net_if.h>
10761088

10771089
/** Scan result callback

modules/hostap/src/supp_api.c

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ static inline enum wifi_security_type wpas_key_mgmt_to_zephyr(int key_mgmt, int
378378
return WIFI_SECURITY_TYPE_PSK_SHA256;
379379
case WPA_KEY_MGMT_SAE:
380380
return WIFI_SECURITY_TYPE_SAE;
381+
case WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_PSK:
382+
return WIFI_SECURITY_TYPE_WPA_AUTO_PERSONAL;
381383
default:
382384
return WIFI_SECURITY_TYPE_UNKNOWN;
383385
}
@@ -1570,7 +1572,7 @@ int hapd_config_network(struct hostapd_iface *iface,
15701572
if (!hostapd_cli_cmd_v("set wpa 0")) {
15711573
goto out;
15721574
}
1573-
iface->bss[0]->conf->wpa_key_mgmt = 0;
1575+
iface->bss[0]->conf->wpa_key_mgmt = WPA_KEY_MGMT_NONE;
15741576
}
15751577

15761578
if (!hostapd_cli_cmd_v("set ieee80211w %d", params->mfp)) {
@@ -1632,6 +1634,80 @@ int supplicant_ap_config_params(const struct device *dev, struct wifi_ap_config_
16321634
}
16331635
#endif
16341636

1637+
int supplicant_ap_status(const struct device *dev, struct wifi_iface_status *status)
1638+
{
1639+
int ret = 0;
1640+
struct hostapd_iface *iface;
1641+
struct hostapd_config *conf;
1642+
struct hostapd_data *hapd;
1643+
struct hostapd_bss_config *bss;
1644+
struct hostapd_ssid *ssid;
1645+
struct hostapd_hw_modes *hw_mode;
1646+
int proto; /* Wi-Fi secure protocol */
1647+
int key_mgmt; /* Wi-Fi key management */
1648+
1649+
k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER);
1650+
1651+
iface = get_hostapd_handle(dev);
1652+
if (!iface) {
1653+
ret = -1;
1654+
wpa_printf(MSG_ERROR, "Interface %s not found", dev->name);
1655+
goto out;
1656+
}
1657+
1658+
conf = iface->conf;
1659+
if (!conf) {
1660+
ret = -1;
1661+
wpa_printf(MSG_ERROR, "Conf %s not found", dev->name);
1662+
goto out;
1663+
}
1664+
1665+
bss = conf->bss[0];
1666+
if (!bss) {
1667+
ret = -1;
1668+
wpa_printf(MSG_ERROR, "Bss_conf %s not found", dev->name);
1669+
goto out;
1670+
}
1671+
1672+
hapd = iface->bss[0];
1673+
if (!hapd) {
1674+
ret = -1;
1675+
wpa_printf(MSG_ERROR, "Bss %s not found", dev->name);
1676+
goto out;
1677+
}
1678+
1679+
status->state = iface->state;
1680+
ssid = &bss->ssid;
1681+
1682+
os_memcpy(status->bssid, hapd->own_addr, WIFI_MAC_ADDR_LEN);
1683+
status->iface_mode = WPAS_MODE_AP;
1684+
status->band = wpas_band_to_zephyr(wpas_freq_to_band(iface->freq));
1685+
key_mgmt = bss->wpa_key_mgmt;
1686+
proto = bss->wpa;
1687+
status->security = wpas_key_mgmt_to_zephyr(key_mgmt, proto);
1688+
status->mfp = bss->ieee80211w;
1689+
status->channel = conf->channel;
1690+
os_memcpy(status->ssid, ssid->ssid, ssid->ssid_len);
1691+
1692+
status->dtim_period = bss->dtim_period;
1693+
status->beacon_interval = conf->beacon_int;
1694+
1695+
hw_mode = iface->current_mode;
1696+
1697+
status->link_mode = conf->ieee80211ax ? WIFI_6
1698+
: conf->ieee80211ac ? WIFI_5
1699+
: conf->ieee80211n ? WIFI_4
1700+
: hw_mode->mode == HOSTAPD_MODE_IEEE80211G ? WIFI_3
1701+
: hw_mode->mode == HOSTAPD_MODE_IEEE80211A ? WIFI_2
1702+
: hw_mode->mode == HOSTAPD_MODE_IEEE80211B ? WIFI_1
1703+
: WIFI_0;
1704+
status->twt_capable = (hw_mode->he_capab[IEEE80211_MODE_AP].mac_cap[0] & 0x04);
1705+
1706+
out:
1707+
k_mutex_unlock(&wpa_supplicant_mutex);
1708+
return ret;
1709+
}
1710+
16351711
int supplicant_ap_enable(const struct device *dev,
16361712
struct wifi_connect_req_params *params)
16371713
{
@@ -1711,7 +1787,7 @@ int supplicant_ap_enable(const struct device *dev,
17111787
goto out;
17121788
}
17131789

1714-
/* No need to check for existing network to join for SoftAP*/
1790+
/* No need to check for existing network to join for SoftAP */
17151791
wpa_s->conf->ap_scan = 2;
17161792
/* Set BSS parameter max_num_sta to default configured value */
17171793
wpa_s->conf->max_num_sta = CONFIG_WIFI_MGMT_AP_MAX_NUM_STA;

modules/hostap/src/supp_api.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ static inline int hapd_state(const struct device *dev, int *state)
250250
}
251251
#endif
252252

253+
/**
254+
* @brief Get Wi-Fi SAP status
255+
*
256+
* @param dev Wi-Fi device
257+
* @param status SAP status
258+
* @return 0 for OK; -1 for ERROR
259+
*/
260+
int supplicant_ap_status(const struct device *dev, struct wifi_iface_status *status);
261+
253262
/**
254263
* @brief Set Wi-Fi AP configuration
255264
*

modules/hostap/src/supp_main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ static const struct wifi_mgmt_ops mgmt_ap_ops = {
9999
.ap_disable = supplicant_ap_disable,
100100
.ap_sta_disconnect = supplicant_ap_sta_disconnect,
101101
.ap_bandwidth = supplicant_ap_bandwidth,
102+
.iface_status = supplicant_ap_status,
102103
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_DPP
103104
.dpp_dispatch = hapd_dpp_dispatch,
104105
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT_DPP */

subsys/net/l2/wifi/wifi_mgmt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const char *wifi_security_txt(enum wifi_security_type security)
4141
return "WAPI";
4242
case WIFI_SECURITY_TYPE_EAP_TLS:
4343
return "EAP";
44+
case WIFI_SECURITY_TYPE_WPA_AUTO_PERSONAL:
45+
return "WPA/WPA2/WPA3 PSK";
4446
case WIFI_SECURITY_TYPE_UNKNOWN:
4547
default:
4648
return "UNKNOWN";

subsys/net/l2/wifi/wifi_shell.c

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,65 @@ static int cmd_wifi_status(const struct shell *sh, size_t argc, char *argv[])
953953
return 0;
954954
}
955955

956+
static int cmd_wifi_ap_status(const struct shell *sh, size_t argc, char *argv[])
957+
{
958+
struct net_if *iface = net_if_get_wifi_sap();
959+
struct wifi_iface_status status = {0};
960+
uint8_t mac_string_buf[sizeof("xx:xx:xx:xx:xx:xx")];
961+
962+
context.sh = sh;
963+
964+
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, iface, &status,
965+
sizeof(struct wifi_iface_status))) {
966+
PR_WARNING("Status request failed\n");
967+
968+
return -ENOEXEC;
969+
}
970+
971+
switch (status.state) {
972+
case WIFI_HAPD_IFACE_UNINITIALIZED:
973+
PR("State: %s\n", "HAPD_IFACE_UNINITIALIZED");
974+
return 0;
975+
case WIFI_HAPD_IFACE_DISABLED:
976+
PR("State: %s\n", "HAPD_IFACE_DISABLED");
977+
return 0;
978+
case WIFI_HAPD_IFACE_COUNTRY_UPDATE:
979+
PR("State: %s\n", "HAPD_IFACE_DISABLED");
980+
return 0;
981+
case WIFI_HAPD_IFACE_ACS:
982+
PR("State: %s\n", "HAPD_IFACE_DISABLED");
983+
return 0;
984+
case WIFI_HAPD_IFACE_HT_SCAN:
985+
PR("State: %s\n", "HAPD_IFACE_DISABLED");
986+
return 0;
987+
case WIFI_HAPD_IFACE_DFS:
988+
PR("State: %s\n", "HAPD_IFACE_DISABLED");
989+
break;
990+
case WIFI_HAPD_IFACE_ENABLED:
991+
break;
992+
default:
993+
return 0;
994+
}
995+
996+
PR("Interface Mode: %s\n", wifi_mode_txt(status.iface_mode));
997+
PR("Link Mode: %s\n", wifi_link_mode_txt(status.link_mode));
998+
PR("SSID: %.32s\n", status.ssid);
999+
PR("BSSID: %s\n", net_sprint_ll_addr_buf(status.bssid, WIFI_MAC_ADDR_LEN, mac_string_buf,
1000+
sizeof(mac_string_buf)));
1001+
PR("Band: %s\n", wifi_band_txt(status.band));
1002+
PR("Channel: %d\n", status.channel);
1003+
PR("Security: %s\n", wifi_security_txt(status.security));
1004+
PR("MFP: %s\n", wifi_mfp_txt(status.mfp));
1005+
if (status.iface_mode == WIFI_MODE_INFRA) {
1006+
PR("RSSI: %d\n", status.rssi);
1007+
}
1008+
PR("Beacon Interval: %d\n", status.beacon_interval);
1009+
PR("DTIM: %d\n", status.dtim_period);
1010+
PR("TWT: %s\n", status.twt_capable ? "Supported" : "Not supported");
1011+
1012+
return 0;
1013+
}
1014+
9561015
#if defined(CONFIG_NET_STATISTICS_WIFI) && \
9571016
defined(CONFIG_NET_STATISTICS_USER_API)
9581017
static void print_wifi_stats(struct net_if *iface, struct net_stats_wifi *data,
@@ -2716,43 +2775,36 @@ static int cmd_wifi_pmksa_flush(const struct shell *sh, size_t argc, char *argv[
27162775
return 0;
27172776
}
27182777

2719-
SHELL_STATIC_SUBCMD_SET_CREATE(wifi_cmd_ap,
2720-
SHELL_CMD_ARG(disable, NULL,
2721-
"Disable Access Point mode.\n",
2722-
cmd_wifi_ap_disable,
2723-
1, 0),
2778+
SHELL_STATIC_SUBCMD_SET_CREATE(
2779+
wifi_cmd_ap,
2780+
SHELL_CMD_ARG(disable, NULL, "Disable Access Point mode.\n", cmd_wifi_ap_disable, 1, 0),
27242781
SHELL_CMD_ARG(enable, NULL,
2725-
"-s --ssid=<SSID>\n"
2726-
"-c --channel=<channel number>\n"
2727-
"-p --passphrase=<PSK> (valid only for secure SSIDs)\n"
2728-
"-k --key-mgmt=<Security type> (valid only for secure SSIDs)\n"
2729-
"0:None, 1:WPA2-PSK, 2:WPA2-PSK-256, 3:SAE, 4:WAPI, 5:EAP-TLS, 6:WEP\n"
2730-
"7: WPA-PSK, 11: DPP\n"
2731-
"-w --ieee-80211w=<MFP> (optional: needs security type to be specified)\n"
2732-
"0:Disable, 1:Optional, 2:Required\n"
2733-
"-b --band=<band> (2 -2.6GHz, 5 - 5Ghz, 6 - 6GHz)\n"
2734-
"-m --bssid=<BSSID>\n"
2735-
"-h --help (prints help)",
2736-
cmd_wifi_ap_enable,
2737-
2, 13),
2738-
SHELL_CMD_ARG(stations, NULL,
2739-
"List stations connected to the AP",
2740-
cmd_wifi_ap_stations,
2741-
1, 0),
2782+
"-s --ssid=<SSID>\n"
2783+
"-c --channel=<channel number>\n"
2784+
"-p --passphrase=<PSK> (valid only for secure SSIDs)\n"
2785+
"-k --key-mgmt=<Security type> (valid only for secure SSIDs)\n"
2786+
"0:None, 1:WPA2-PSK, 2:WPA2-PSK-256, 3:SAE, 4:WAPI, 5:EAP-TLS, 6:WEP\n"
2787+
"7: WPA-PSK, 11: DPP\n"
2788+
"-w --ieee-80211w=<MFP> (optional: needs security type to be specified)\n"
2789+
"0:Disable, 1:Optional, 2:Required\n"
2790+
"-b --band=<band> (2 -2.6GHz, 5 - 5Ghz, 6 - 6GHz)\n"
2791+
"-m --bssid=<BSSID>\n"
2792+
"-h --help (prints help)",
2793+
cmd_wifi_ap_enable, 2, 13),
2794+
SHELL_CMD_ARG(stations, NULL, "List stations connected to the AP", cmd_wifi_ap_stations, 1,
2795+
0),
27422796
SHELL_CMD_ARG(disconnect, NULL,
2743-
"Disconnect a station from the AP\n"
2744-
"<MAC address of the station>\n",
2745-
cmd_wifi_ap_sta_disconnect,
2746-
2, 0),
2797+
"Disconnect a station from the AP\n"
2798+
"<MAC address of the station>\n",
2799+
cmd_wifi_ap_sta_disconnect, 2, 0),
27472800
SHELL_CMD_ARG(config, NULL,
2748-
"Configure AP parameters.\n"
2749-
"-i --max_inactivity=<time duration (in seconds)>\n"
2750-
"-s --max_num_sta=<maximum number of stations>\n"
2751-
"-h --help (prints help)",
2752-
cmd_wifi_ap_config_params,
2753-
2, 5),
2754-
SHELL_SUBCMD_SET_END
2755-
);
2801+
"Configure AP parameters.\n"
2802+
"-i --max_inactivity=<time duration (in seconds)>\n"
2803+
"-s --max_num_sta=<maximum number of stations>\n"
2804+
"-h --help (prints help)",
2805+
cmd_wifi_ap_config_params, 2, 5),
2806+
SHELL_CMD_ARG(status, NULL, "Status of Wi-Fi SAP\n", cmd_wifi_ap_status, 1, 0),
2807+
SHELL_SUBCMD_SET_END);
27562808

27572809
SHELL_SUBCMD_ADD((wifi), ap, &wifi_cmd_ap,
27582810
"Access Point mode commands.",

0 commit comments

Comments
 (0)