From d2c20a035a618f81cacdc4675f5e5899b09ea276 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 31 Oct 2023 15:14:41 +0200 Subject: [PATCH] hostap: Various changes to the API to wpa_supplicant Changed the names of the API functions between wpa_supplicant and Zephyr upper layers. For example replaced the z_ prefix by zephyr_ as the z_ is only meant for internal kernel functions. Signed-off-by: Jukka Rissanen --- modules/hostap/Kconfig | 7 + modules/hostap/src/supp_api.c | 179 +++++---- modules/hostap/src/supp_api.h | 33 +- modules/hostap/src/supp_events.c | 168 +++++---- modules/hostap/src/supp_events.h | 124 +++---- modules/hostap/src/supp_main.c | 613 ++++++++++++++++--------------- modules/hostap/src/supp_main.h | 7 +- 7 files changed, 594 insertions(+), 537 deletions(-) diff --git a/modules/hostap/Kconfig b/modules/hostap/Kconfig index 8dbe18d796701ec..e136bb074a495cb 100644 --- a/modules/hostap/Kconfig +++ b/modules/hostap/Kconfig @@ -29,6 +29,10 @@ config WIFI_NM_WPA_SUPPLICANT_WQ_STACK_SIZE int "Stack size for wpa_supplicant iface workqueue" default 4096 +config WIFI_NM_WPA_SUPPLICANT_WQ_PRIO + int "Thread priority of wpa_supplicant iface workqueue" + default 7 + # Currently we default POSIX_MAX_FDS to 16 in lib/posix/Kconfig # l2_packet - 1 # ctrl_iface - 2 * socketpairs = 4(local and global) @@ -206,4 +210,7 @@ config NO_PBKDF2 bool default y +config SAE_PK + bool + endif # WIFI_NM_WPA_SUPPLICANT diff --git a/modules/hostap/src/supp_api.c b/modules/hostap/src/supp_api.c index 0f07db37387b095..a7a35c25a40172c 100644 --- a/modules/hostap/src/supp_api.c +++ b/modules/hostap/src/supp_api.c @@ -41,9 +41,9 @@ enum status_thread_state { #define DISCONNECT_TIMEOUT_MS 5000 -#define _wpa_cli_cmd_v(cmd, ...) \ +#define __wpa_cli_cmd_v(cmd, ...) \ do { \ - if (z_wpa_cli_cmd_v(cmd, ##__VA_ARGS__) < 0) { \ + if (zephyr_wpa_cli_cmd_v(cmd, ##__VA_ARGS__) < 0) { \ wpa_printf(MSG_ERROR, "Failed to execute wpa_cli command: %s", cmd); \ goto out; \ } \ @@ -68,9 +68,31 @@ static void supp_shell_connect_status(struct k_work *work); static K_WORK_DELAYABLE_DEFINE(wpa_supp_status_work, supp_shell_connect_status); -static inline struct wpa_supplicant *get_wpa_s_handle(const struct device *dev) +static struct wpa_supplicant *get_wpa_s_handle(const struct device *dev) { - return z_wpas_get_handle_by_ifname(dev->name); + struct net_if *iface = net_if_lookup_by_dev(dev); + char if_name[CONFIG_NET_INTERFACE_NAME_LEN + 1]; + struct wpa_supplicant *wpa_s; + int ret; + + if (!iface) { + wpa_printf(MSG_ERROR, "Interface for device %s not found", dev->name); + return NULL; + } + + ret = net_if_get_name(iface, if_name, sizeof(if_name)); + if (!ret) { + wpa_printf(MSG_ERROR, "Cannot get interface name (%d)", ret); + return NULL; + } + + wpa_s = zephyr_get_handle_by_ifname(if_name); + if (!wpa_s) { + wpa_printf(MSG_ERROR, "Interface %s not found", if_name); + return NULL; + } + + return wpa_s; } static int wait_for_disconnect_complete(const struct device *dev) @@ -120,11 +142,13 @@ static void supp_shell_connect_status(struct k_work *work) } if (ctrl->requested_op == CONNECT && wpa_s->wpa_state != WPA_COMPLETED) { - if (ctrl->connection_timeout > 0 && seconds_counter++ > ctrl->connection_timeout) { - _wpa_cli_cmd_v("disconnect"); + if (ctrl->connection_timeout > 0 && + seconds_counter++ > ctrl->connection_timeout) { + __wpa_cli_cmd_v("disconnect"); conn_result = -ETIMEDOUT; - send_wifi_mgmt_event(wpa_s->ifname, NET_EVENT_WIFI_CMD_CONNECT_RESULT, - (void *)&conn_result, sizeof(int)); + supplicant_send_wifi_mgmt_event(wpa_s->ifname, + NET_EVENT_WIFI_CMD_CONNECT_RESULT, + (void *)&conn_result, sizeof(int)); status = CONNECTION_FAILURE; goto out; } @@ -145,13 +169,11 @@ static inline void wpa_supp_restart_status_work(void) { /* Terminate synchronously */ wpa_supp_api_ctrl.terminate = 1; - k_work_flush_delayable(&wpa_supp_status_work, - &wpa_supp_api_ctrl.sync); + k_work_flush_delayable(&wpa_supp_status_work, &wpa_supp_api_ctrl.sync); wpa_supp_api_ctrl.terminate = 0; /* Start afresh */ - k_work_reschedule(&wpa_supp_status_work, - K_MSEC(10)); + k_work_reschedule(&wpa_supp_status_work, K_MSEC(10)); } static inline int chan_to_freq(int chan) @@ -203,12 +225,11 @@ static inline enum wifi_security_type wpas_key_mgmt_to_zephyr(int key_mgmt) } /* Public API */ -int z_wpa_supplicant_connect(const struct device *dev, - struct wifi_connect_req_params *params) +int supplicant_connect(const struct device *dev, struct wifi_connect_req_params *params) { + struct add_network_resp resp = {0}; struct wpa_supplicant *wpa_s; int ret = 0; - struct add_network_resp resp = {0}; if (!net_if_is_admin_up(net_if_lookup_by_dev(dev))) { wpa_printf(MSG_ERROR, @@ -222,11 +243,11 @@ int z_wpa_supplicant_connect(const struct device *dev, wpa_s = get_wpa_s_handle(dev); if (!wpa_s) { ret = -1; - wpa_printf(MSG_ERROR, "Interface %s not found", dev->name); + wpa_printf(MSG_ERROR, "Device %s not found", dev->name); goto out; } - _wpa_cli_cmd_v("remove_network all"); + __wpa_cli_cmd_v("remove_network all"); ret = z_wpa_ctrl_add_network(&resp); if (ret) { wpa_printf(MSG_ERROR, "Failed to add network"); @@ -235,31 +256,30 @@ int z_wpa_supplicant_connect(const struct device *dev, wpa_printf(MSG_DEBUG, "NET added: %d\n", resp.network_id); - _wpa_cli_cmd_v("set_network %d ssid \"%s\"", resp.network_id, params->ssid); - _wpa_cli_cmd_v("set_network %d scan_ssid 1", resp.network_id); - _wpa_cli_cmd_v("set_network %d key_mgmt NONE", resp.network_id); - _wpa_cli_cmd_v("set_network %d ieee80211w 0", resp.network_id); + __wpa_cli_cmd_v("set_network %d ssid \"%s\"", resp.network_id, params->ssid); + __wpa_cli_cmd_v("set_network %d scan_ssid 1", resp.network_id); + __wpa_cli_cmd_v("set_network %d key_mgmt NONE", resp.network_id); + __wpa_cli_cmd_v("set_network %d ieee80211w 0", resp.network_id); if (params->security != WIFI_SECURITY_TYPE_NONE) { if (params->security == WIFI_SECURITY_TYPE_SAE) { if (params->sae_password) { - _wpa_cli_cmd_v("set_network %d sae_password \"%s\"", - resp.network_id, params->sae_password); + __wpa_cli_cmd_v("set_network %d sae_password \"%s\"", + resp.network_id, params->sae_password); } else { - _wpa_cli_cmd_v("set_network %d sae_password \"%s\"", - resp.network_id, params->psk); + __wpa_cli_cmd_v("set_network %d sae_password \"%s\"", + resp.network_id, params->psk); } - _wpa_cli_cmd_v("set_network %d key_mgmt SAE", - resp.network_id); + + __wpa_cli_cmd_v("set_network %d key_mgmt SAE", resp.network_id); } else if (params->security == WIFI_SECURITY_TYPE_PSK_SHA256) { - _wpa_cli_cmd_v("set_network %d psk \"%s\"", - resp.network_id, params->psk); - _wpa_cli_cmd_v("set_network %d key_mgmt WPA-PSK-SHA256", - resp.network_id); + __wpa_cli_cmd_v("set_network %d psk \"%s\"", + resp.network_id, params->psk); + __wpa_cli_cmd_v("set_network %d key_mgmt WPA-PSK-SHA256", + resp.network_id); } else if (params->security == WIFI_SECURITY_TYPE_PSK) { - _wpa_cli_cmd_v("set_network %d psk \"%s\"", + __wpa_cli_cmd_v("set_network %d psk \"%s\"", resp.network_id, params->psk); - _wpa_cli_cmd_v("set_network %d key_mgmt WPA-PSK", - resp.network_id); + __wpa_cli_cmd_v("set_network %d key_mgmt WPA-PSK", resp.network_id); } else { ret = -1; wpa_printf(MSG_ERROR, "Unsupported security type: %d", @@ -268,13 +288,13 @@ int z_wpa_supplicant_connect(const struct device *dev, } if (params->mfp) { - _wpa_cli_cmd_v("set_network %d ieee80211w %d", - resp.network_id, params->mfp); + __wpa_cli_cmd_v("set_network %d ieee80211w %d", + resp.network_id, params->mfp); } } /* enable and select network */ - _wpa_cli_cmd_v("enable_network %d", resp.network_id); + __wpa_cli_cmd_v("enable_network %d", resp.network_id); if (params->channel != WIFI_CHANNEL_ANY) { int freq = chan_to_freq(params->channel); @@ -285,12 +305,12 @@ int z_wpa_supplicant_connect(const struct device *dev, params->channel); goto out; } - z_wpa_cli_cmd_v("set_network %d scan_freq %d", - resp.network_id, freq); + zephyr_wpa_cli_cmd_v("set_network %d scan_freq %d", + resp.network_id, freq); } - _wpa_cli_cmd_v("select_network %d", resp.network_id); + __wpa_cli_cmd_v("select_network %d", resp.network_id); - z_wpa_cli_cmd_v("select_network %d", resp.network_id); + zephyr_wpa_cli_cmd_v("select_network %d", resp.network_id); wpa_supp_api_ctrl.dev = dev; wpa_supp_api_ctrl.requested_op = CONNECT; wpa_supp_api_ctrl.connection_timeout = params->timeout; @@ -305,16 +325,16 @@ int z_wpa_supplicant_connect(const struct device *dev, return ret; } -int z_wpa_supplicant_disconnect(const struct device *dev) +int supplicant_disconnect(const struct device *dev) { - struct wpa_supplicant *wpa_s; - int ret = 0; struct net_if *iface = net_if_lookup_by_dev(dev); + struct wpa_supplicant *wpa_s; + int ret; if (!iface) { - ret = -EINVAL; - wpa_printf(MSG_ERROR, "Device %s not found", dev->name); - goto out; + ret = -ENOENT; + wpa_printf(MSG_ERROR, "Interface for device %s not found", dev->name); + return ret; } k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER); @@ -322,19 +342,18 @@ int z_wpa_supplicant_disconnect(const struct device *dev) wpa_s = get_wpa_s_handle(dev); if (!wpa_s) { ret = -EINVAL; - wpa_printf(MSG_ERROR, "Interface %s not found", dev->name); + wpa_printf(MSG_ERROR, "Device %s not found", dev->name); goto out; } wpa_supp_api_ctrl.dev = dev; wpa_supp_api_ctrl.requested_op = DISCONNECT; - _wpa_cli_cmd_v("disconnect"); + __wpa_cli_cmd_v("disconnect"); out: k_mutex_unlock(&wpa_supplicant_mutex); if (ret) { - wpa_printf(MSG_ERROR, "Disconnect failed: %s", - strerror(-ret)); + wpa_printf(MSG_ERROR, "Disconnect failed: %s", strerror(-ret)); return ret; } @@ -347,19 +366,25 @@ int z_wpa_supplicant_disconnect(const struct device *dev) return ret; } -int z_wpa_supplicant_status(const struct device *dev, - struct wifi_iface_status *status) +int supplicant_status(const struct device *dev, struct wifi_iface_status *status) { + struct net_if *iface = net_if_lookup_by_dev(dev); struct wpa_supplicant *wpa_s; int ret = -1; struct wpa_signal_info *si = NULL; struct wpa_conn_info *conn_info = NULL; + if (!iface) { + ret = -ENOENT; + wpa_printf(MSG_ERROR, "Interface for device %s not found", dev->name); + return ret; + } + k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER); wpa_s = get_wpa_s_handle(dev); if (!wpa_s) { - wpa_printf(MSG_ERROR, "Interface %s not found", dev->name); + wpa_printf(MSG_ERROR, "Device %s not found", dev->name); goto out; } @@ -401,17 +426,19 @@ int z_wpa_supplicant_status(const struct device *dev, status->ssid_len = ssid_len; status->iface_mode = ssid->mode; if (wpa_s->connection_set == 1) { - status->link_mode = wpa_s->connection_he ? WIFI_6 : - wpa_s->connection_vht ? WIFI_5 : - wpa_s->connection_ht ? WIFI_4 : - wpa_s->connection_g ? WIFI_3 : - wpa_s->connection_a ? WIFI_2 : - wpa_s->connection_b ? WIFI_1 : - WIFI_0; + status->link_mode = + wpa_s->connection_he ? WIFI_6 : + wpa_s->connection_vht ? WIFI_5 : + wpa_s->connection_ht ? WIFI_4 : + wpa_s->connection_g ? WIFI_3 : + wpa_s->connection_a ? WIFI_2 : + wpa_s->connection_b ? WIFI_1 : + WIFI_0; } else { status->link_mode = WIFI_LINK_MODE_UNKNOWN; } } + ret = z_wpa_ctrl_signal_poll(&signal_poll); if (!ret) { status->rssi = signal_poll.rssi; @@ -429,6 +456,7 @@ int z_wpa_supplicant_status(const struct device *dev, ret = -ENOMEM; goto out; } + ret = wpa_drv_get_conn_info(wpa_s, conn_info); if (!ret) { status->beacon_interval = conn_info->beacon_interval; @@ -442,10 +470,12 @@ int z_wpa_supplicant_status(const struct device *dev, status->twt_capable = false; ret = 0; } + os_free(conn_info); } else { ret = 0; } + out: os_free(si); k_mutex_unlock(&wpa_supplicant_mutex); @@ -462,14 +492,14 @@ int z_wpa_supplicant_status(const struct device *dev, static const struct wifi_mgmt_ops *const get_wifi_mgmt_api(const struct device *dev) { - struct net_wifi_mgmt_offload *off_api = - (struct net_wifi_mgmt_offload *) dev->api; + struct net_wifi_mgmt_native *api = + (struct net_wifi_mgmt_native *)dev->api; - return off_api ? off_api->wifi_mgmt_api : NULL; + return api ? api->wifi_mgmt_api : NULL; } -int z_wpa_supplicant_scan(const struct device *dev, struct wifi_scan_params *params, - scan_result_cb_t cb) +int supplicant_scan(const struct device *dev, struct wifi_scan_params *params, + scan_result_cb_t cb) { const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_mgmt_api(dev); @@ -482,8 +512,7 @@ int z_wpa_supplicant_scan(const struct device *dev, struct wifi_scan_params *par } #ifdef CONFIG_NET_STATISTICS_WIFI -int z_wpa_supplicant_get_stats(const struct device *dev, - struct net_stats_wifi *stats) +int supplicant_get_stats(const struct device *dev, struct net_stats_wifi *stats) { const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_mgmt_api(dev); @@ -496,8 +525,7 @@ int z_wpa_supplicant_get_stats(const struct device *dev, } #endif /* CONFIG_NET_STATISTICS_WIFI */ -int z_wpa_supplicant_set_power_save(const struct device *dev, - struct wifi_ps_params *params) +int supplicant_set_power_save(const struct device *dev, struct wifi_ps_params *params) { const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_mgmt_api(dev); @@ -509,8 +537,7 @@ int z_wpa_supplicant_set_power_save(const struct device *dev, return wifi_mgmt_api->set_power_save(dev, params); } -int z_wpa_supplicant_set_twt(const struct device *dev, - struct wifi_twt_params *params) +int supplicant_set_twt(const struct device *dev, struct wifi_twt_params *params) { const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_mgmt_api(dev); @@ -522,8 +549,8 @@ int z_wpa_supplicant_set_twt(const struct device *dev, return wifi_mgmt_api->set_twt(dev, params); } -int z_wpa_supplicant_get_power_save_config(const struct device *dev, - struct wifi_ps_config *config) +int supplicant_get_power_save_config(const struct device *dev, + struct wifi_ps_config *config) { const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_mgmt_api(dev); @@ -535,8 +562,8 @@ int z_wpa_supplicant_get_power_save_config(const struct device *dev, return wifi_mgmt_api->get_power_save_config(dev, config); } -int z_wpa_supplicant_reg_domain(const struct device *dev, - struct wifi_reg_domain *reg_domain) +int supplicant_reg_domain(const struct device *dev, + struct wifi_reg_domain *reg_domain) { const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_mgmt_api(dev); diff --git a/modules/hostap/src/supp_api.h b/modules/hostap/src/supp_api.h index 29e3d0717e55ccc..9be68e59d6e9860 100644 --- a/modules/hostap/src/supp_api.h +++ b/modules/hostap/src/supp_api.h @@ -9,8 +9,12 @@ #include +#ifndef MAX_SSID_LEN #define MAX_SSID_LEN 32 +#endif +#ifndef MAC_ADDR_LEN #define MAC_ADDR_LEN 6 +#endif /** * @brief Request a connection @@ -20,8 +24,8 @@ * * @return: 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_connect(const struct device *dev, - struct wifi_connect_req_params *params); +int supplicant_connect(const struct device *dev, struct wifi_connect_req_params *params); + /** * @brief Forces station to disconnect and stops any subsequent scan * or connection attempts @@ -30,7 +34,7 @@ int z_wpa_supplicant_connect(const struct device *dev, * * @return: 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_disconnect(const struct device *dev); +int supplicant_disconnect(const struct device *dev); /** * @brief @@ -40,8 +44,7 @@ int z_wpa_supplicant_disconnect(const struct device *dev); * * @return: 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_status(const struct device *dev, - struct wifi_iface_status *status); +int supplicant_status(const struct device *dev, struct wifi_iface_status *status); /** * @brief Request a scan @@ -52,8 +55,8 @@ int z_wpa_supplicant_status(const struct device *dev, * * @return 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_scan(const struct device *dev, struct wifi_scan_params *params, - scan_result_cb_t cb); +int supplicant_scan(const struct device *dev, struct wifi_scan_params *params, + scan_result_cb_t cb); #if defined(CONFIG_NET_STATISTICS_WIFI) || defined(__DOXYGEN__) /** @@ -64,8 +67,7 @@ int z_wpa_supplicant_scan(const struct device *dev, struct wifi_scan_params *par * * @return 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_get_stats(const struct device *dev, - struct net_stats_wifi *stats); +int supplicant_get_stats(const struct device *dev, struct net_stats_wifi *stats); #endif /* CONFIG_NET_STATISTICS_WIFI || __DOXYGEN__ */ /** @@ -76,8 +78,7 @@ int z_wpa_supplicant_get_stats(const struct device *dev, * * @return 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_set_power_save(const struct device *dev, - struct wifi_ps_params *params); +int supplicant_set_power_save(const struct device *dev, struct wifi_ps_params *params); /** * @brief Set Wi-Fi TWT parameters @@ -86,8 +87,7 @@ int z_wpa_supplicant_set_power_save(const struct device *dev, * @param params TWT parameters to set * @return 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_set_twt(const struct device *dev, - struct wifi_twt_params *params); +int supplicant_set_twt(const struct device *dev, struct wifi_twt_params *params); /** * @brief Get Wi-Fi power save configuration @@ -96,8 +96,7 @@ int z_wpa_supplicant_set_twt(const struct device *dev, * @param config Address of power save configuration to fill * @return 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_get_power_save_config(const struct device *dev, - struct wifi_ps_config *config); +int supplicant_get_power_save_config(const struct device *dev, struct wifi_ps_config *config); /** * @brief Set Wi-Fi Regulatory domain @@ -106,6 +105,6 @@ int z_wpa_supplicant_get_power_save_config(const struct device *dev, * @param reg_domain Regulatory domain to set * @return 0 for OK; -1 for ERROR */ -int z_wpa_supplicant_reg_domain(const struct device *dev, - struct wifi_reg_domain *reg_domain); +int supplicant_reg_domain(const struct device *dev, struct wifi_reg_domain *reg_domain); + #endif /* ZEPHYR_SUPP_MGMT_H */ diff --git a/modules/hostap/src/supp_events.c b/modules/hostap/src/supp_events.c index 542944e103928da..c6156594f6a6433 100644 --- a/modules/hostap/src/supp_events.c +++ b/modules/hostap/src/supp_events.c @@ -11,7 +11,7 @@ #define MAC_STR_FORMAT "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx" -static char *wpa_supp_event_map[] = { +static char *supplicant_event_map[] = { "CTRL-EVENT-CONNECTED", "CTRL-EVENT-DISCONNECTED", "CTRL-EVENT-ASSOC-REJECT", @@ -30,98 +30,102 @@ static char *wpa_supp_event_map[] = { "CTRL-EVENT-DSCP-POLICY", }; -static int wpa_supp_process_status(struct supp_int_event_data *event_data, char *wpa_supp_status) +static int supplicant_process_status(struct supplicant_int_event_data *event_data, char *supplicant_status) { int ret = 1; /* For cases where parsing is not being done*/ int event = -1; int i; unsigned char *mac; - union supp_event_data *data; + union supplicant_event_data *data; - data = (union supp_event_data *)event_data->data; + data = (union supplicant_event_data *)event_data->data; - for (i = 0; i < ARRAY_SIZE(wpa_supp_event_map); i++) { - if (strncmp(wpa_supp_status, wpa_supp_event_map[i], - strlen(wpa_supp_event_map[i])) == 0) { + for (i = 0; i < ARRAY_SIZE(supplicant_event_map); i++) { + if (strncmp(supplicant_status, supplicant_event_map[i], + strlen(supplicant_event_map[i])) == 0) { event = i; break; } } - if (i >= ARRAY_SIZE(wpa_supp_event_map)) { - wpa_printf(MSG_ERROR, "Event not supported: %s\n", wpa_supp_status); + if (i >= ARRAY_SIZE(supplicant_event_map)) { + wpa_printf(MSG_ERROR, "Event not supported: %s\n", supplicant_status); return -ENOTSUP; } event_data->event = event; switch (event_data->event) { - case WPA_SUPP_EVENT_CONNECTED: + case SUPPLICANT_EVENT_CONNECTED: mac = data->connected.bssid; - ret = sscanf(wpa_supp_status + strlen("CTRL-EVENT-CONNECTED - Connection to"), - MAC_STR_FORMAT, &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); + ret = sscanf(supplicant_status + + sizeof("CTRL-EVENT-CONNECTED - Connection to") - 1, + MAC_STR_FORMAT, + &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); event_data->data_len = sizeof(data->connected); break; - case WPA_SUPP_EVENT_DISCONNECTED: + case SUPPLICANT_EVENT_DISCONNECTED: mac = data->disconnected.bssid; - ret = sscanf(wpa_supp_status + strlen("CTRL-EVENT-DISCONNECTED bssid="), - MAC_STR_FORMAT" reason=%d", &mac[0], &mac[1], &mac[2], - &mac[3], &mac[4], &mac[5], &data->disconnected.reason_code); + ret = sscanf(supplicant_status + sizeof("CTRL-EVENT-DISCONNECTED bssid=") - 1, + MAC_STR_FORMAT" reason=%d", &mac[0], &mac[1], &mac[2], + &mac[3], &mac[4], &mac[5], &data->disconnected.reason_code); event_data->data_len = sizeof(data->disconnected); break; - case WPA_SUPP_EVENT_ASSOC_REJECT: + case SUPPLICANT_EVENT_ASSOC_REJECT: /* TODO */ break; - case WPA_SUPP_EVENT_AUTH_REJECT: + case SUPPLICANT_EVENT_AUTH_REJECT: mac = data->auth_reject.bssid; - ret = sscanf(wpa_supp_status + strlen("CTRL-EVENT-AUTH-REJECT "), MAC_STR_FORMAT - " auth_type=%u auth_transaction=%u status_code=%u", - &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5], - &data->auth_reject.auth_type, - &data->auth_reject.auth_transaction, - &data->auth_reject.status_code); + ret = sscanf(supplicant_status + sizeof("CTRL-EVENT-AUTH-REJECT ") - 1, + MAC_STR_FORMAT + " auth_type=%u auth_transaction=%u status_code=%u", + &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5], + &data->auth_reject.auth_type, + &data->auth_reject.auth_transaction, + &data->auth_reject.status_code); event_data->data_len = sizeof(data->auth_reject); break; - case WPA_SUPP_EVENT_SSID_TEMP_DISABLED: - ret = sscanf(wpa_supp_status + strlen("CTRL-EVENT-SSID-TEMP-DISABLED "), - "id=%d ssid=%s auth_failures=%u duration=%d reason=%s", - &data->temp_disabled.id, data->temp_disabled.ssid, - &data->temp_disabled.auth_failures, - &data->temp_disabled.duration, - data->temp_disabled.reason_code); + case SUPPLICANT_EVENT_SSID_TEMP_DISABLED: + ret = sscanf(supplicant_status + sizeof("CTRL-EVENT-SSID-TEMP-DISABLED ") - 1, + "id=%d ssid=%s auth_failures=%u duration=%d reason=%s", + &data->temp_disabled.id, data->temp_disabled.ssid, + &data->temp_disabled.auth_failures, + &data->temp_disabled.duration, + data->temp_disabled.reason_code); event_data->data_len = sizeof(data->temp_disabled); break; - case WPA_SUPP_EVENT_SSID_REENABLED: - ret = sscanf(wpa_supp_status + strlen("CTRL-EVENT-SSID-REENABLED "), - "id=%d ssid=%s", &data->reenabled.id, - data->reenabled.ssid); + case SUPPLICANT_EVENT_SSID_REENABLED: + ret = sscanf(supplicant_status + sizeof("CTRL-EVENT-SSID-REENABLED ") - 1, + "id=%d ssid=%s", &data->reenabled.id, + data->reenabled.ssid); event_data->data_len = sizeof(data->reenabled); break; - case WPA_SUPP_EVENT_BSS_ADDED: + case SUPPLICANT_EVENT_BSS_ADDED: mac = data->bss_added.bssid; - ret = sscanf(wpa_supp_status + strlen("CTRL-EVENT-BSS-ADDED "), "%u "MAC_STR_FORMAT, - &data->bss_added.id, - &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); + ret = sscanf(supplicant_status + sizeof("CTRL-EVENT-BSS-ADDED ") - 1, + "%u "MAC_STR_FORMAT, + &data->bss_added.id, + &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); event_data->data_len = sizeof(data->bss_added); break; - case WPA_SUPP_EVENT_BSS_REMOVED: + case SUPPLICANT_EVENT_BSS_REMOVED: mac = data->bss_removed.bssid; - ret = sscanf(wpa_supp_status + strlen("CTRL-EVENT-BSS-REMOVED "), - "%u "MAC_STR_FORMAT, - &data->bss_removed.id, - &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); + ret = sscanf(supplicant_status + sizeof("CTRL-EVENT-BSS-REMOVED ") - 1, + "%u "MAC_STR_FORMAT, + &data->bss_removed.id, + &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); event_data->data_len = sizeof(data->bss_removed); break; - case WPA_SUPP_EVENT_TERMINATING: - case WPA_SUPP_EVENT_SCAN_STARTED: - case WPA_SUPP_EVENT_SCAN_FAILED: - case WPA_SUPP_EVENT_NETWORK_NOT_FOUND: - case WPA_SUPP_EVENT_NETWORK_ADDED: - case WPA_SUPP_EVENT_NETWORK_REMOVED: - strncpy(data->supp_event_str, wpa_supp_event_map[event], - sizeof(data->supp_event_str)); - event_data->data_len = strlen(data->supp_event_str) + 1; - case WPA_SUPP_EVENT_DSCP_POLICY: + case SUPPLICANT_EVENT_TERMINATING: + case SUPPLICANT_EVENT_SCAN_STARTED: + case SUPPLICANT_EVENT_SCAN_FAILED: + case SUPPLICANT_EVENT_NETWORK_NOT_FOUND: + case SUPPLICANT_EVENT_NETWORK_ADDED: + case SUPPLICANT_EVENT_NETWORK_REMOVED: + strncpy(data->supplicant_event_str, supplicant_event_map[event], + sizeof(data->supplicant_event_str)); + event_data->data_len = strlen(data->supplicant_event_str) + 1; + case SUPPLICANT_EVENT_DSCP_POLICY: /* TODO */ break; default: @@ -130,19 +134,18 @@ static int wpa_supp_process_status(struct supp_int_event_data *event_data, char if (ret <= 0) { wpa_printf(MSG_ERROR, "%s Parse failed: %s", - wpa_supp_event_map[event_data->event], strerror(errno)); + supplicant_event_map[event_data->event], strerror(errno)); } return ret; } -int send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event, - void *wpa_supp_status, size_t len) +int supplicant_send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event, + void *supplicant_status, size_t len) { - const struct device *dev = device_get_binding(ifname); - struct net_if *iface = net_if_lookup_by_dev(dev); - union supp_event_data data; - struct supp_int_event_data event_data; + struct net_if *iface = net_if_get_by_index(net_if_get_by_name(ifname)); + union supplicant_event_data data; + struct supplicant_int_event_data event_data; if (!iface) { wpa_printf(MSG_ERROR, "Could not find iface for %s", ifname); @@ -151,15 +154,15 @@ int send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event, switch (event) { case NET_EVENT_WIFI_CMD_CONNECT_RESULT: - wifi_mgmt_raise_connect_result_event(iface, *(int *)wpa_supp_status); + wifi_mgmt_raise_connect_result_event(iface, *(int *)supplicant_status); break; case NET_EVENT_WIFI_CMD_DISCONNECT_RESULT: - wifi_mgmt_raise_disconnect_result_event(iface, *(int *)wpa_supp_status); + wifi_mgmt_raise_disconnect_result_event(iface, *(int *)supplicant_status); break; - case NET_EVENT_WPA_SUPP_CMD_INT_EVENT: + case NET_EVENT_SUPPLICANT_CMD_INT_EVENT: event_data.data = &data; - if (wpa_supp_process_status(&event_data, (char *)wpa_supp_status) > 0) { - net_mgmt_event_notify_with_info(NET_EVENT_WPA_SUPP_INT_EVENT, + if (supplicant_process_status(&event_data, (char *)supplicant_status) > 0) { + net_mgmt_event_notify_with_info(NET_EVENT_SUPPLICANT_INT_EVENT, iface, &event_data, sizeof(event_data)); } break; @@ -171,38 +174,31 @@ int send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event, return 0; } -int generate_supp_state_event(const char *ifname, enum net_event_wpa_supp_cmd event, int status) +int supplicant_generate_state_event(const char *ifname, enum net_event_supplicant_cmd event, int status) { - /* TODO: Replace device_get_binding. */ - const struct device *dev = device_get_binding(ifname); - - if (!dev) { - wpa_printf(MSG_ERROR, "Could not find device for %s", ifname); - return -ENODEV; - } - - struct net_if *iface = net_if_lookup_by_dev(dev); + struct net_if *iface; + iface = net_if_get_by_index(net_if_get_by_name(ifname)); if (!iface) { wpa_printf(MSG_ERROR, "Could not find iface for %s", ifname); return -ENODEV; } switch (event) { - case NET_EVENT_WPA_SUPP_CMD_READY: - net_mgmt_event_notify(NET_EVENT_WPA_SUPP_READY, iface); + case NET_EVENT_SUPPLICANT_CMD_READY: + net_mgmt_event_notify(NET_EVENT_SUPPLICANT_READY, iface); break; - case NET_EVENT_WPA_SUPP_CMD_NOT_READY: - net_mgmt_event_notify(NET_EVENT_WPA_SUPP_NOT_READY, iface); + case NET_EVENT_SUPPLICANT_CMD_NOT_READY: + net_mgmt_event_notify(NET_EVENT_SUPPLICANT_NOT_READY, iface); break; - case NET_EVENT_WPA_SUPP_CMD_IFACE_ADDED: - net_mgmt_event_notify(NET_EVENT_WPA_SUPP_IFACE_ADDED, iface); + case NET_EVENT_SUPPLICANT_CMD_IFACE_ADDED: + net_mgmt_event_notify(NET_EVENT_SUPPLICANT_IFACE_ADDED, iface); break; - case NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVING: - net_mgmt_event_notify(NET_EVENT_WPA_SUPP_IFACE_REMOVING, iface); + case NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVING: + net_mgmt_event_notify(NET_EVENT_SUPPLICANT_IFACE_REMOVING, iface); break; - case NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED: - net_mgmt_event_notify_with_info(NET_EVENT_WPA_SUPP_IFACE_REMOVED, + case NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVED: + net_mgmt_event_notify_with_info(NET_EVENT_SUPPLICANT_IFACE_REMOVED, iface, &status, sizeof(status)); break; default: diff --git a/modules/hostap/src/supp_events.h b/modules/hostap/src/supp_events.h index 5296aad2da3ea30..25513e5c65307df 100644 --- a/modules/hostap/src/supp_events.h +++ b/modules/hostap/src/supp_events.h @@ -10,75 +10,75 @@ #include /* Connectivity Events */ -#define _NET_MGMT_WPA_SUPP_LAYER NET_MGMT_LAYER_L2 -#define _NET_MGMT_WPA_SUPP_CODE 0x157 -#define _NET_MGMT_WPA_SUPP_BASE (NET_MGMT_LAYER(_NET_MGMT_WPA_SUPP_LAYER) | \ - NET_MGMT_LAYER_CODE(_NET_MGMT_WPA_SUPP_CODE) | \ - NET_MGMT_IFACE_BIT) -#define _NET_MGMT_WPA_SUPP_EVENT (NET_MGMT_EVENT_BIT | _NET_MGMT_WPA_SUPP_BASE) - -enum net_event_wpa_supp_cmd { - NET_EVENT_WPA_SUPP_CMD_READY = 1, - NET_EVENT_WPA_SUPP_CMD_NOT_READY, - NET_EVENT_WPA_SUPP_CMD_IFACE_ADDED, - NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVING, - NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED, - NET_EVENT_WPA_SUPP_CMD_INT_EVENT, - NET_EVENT_WPA_SUPP_CMD_MAX +#define _NET_MGMT_SUPPLICANT_LAYER NET_MGMT_LAYER_L3 +#define _NET_MGMT_SUPPLICANT_CODE 0x157 +#define _NET_MGMT_SUPPLICANT_BASE (NET_MGMT_LAYER(_NET_MGMT_SUPPLICANT_LAYER) | \ + NET_MGMT_LAYER_CODE(_NET_MGMT_SUPPLICANT_CODE) | \ + NET_MGMT_IFACE_BIT) +#define _NET_MGMT_SUPPLICANT_EVENT (NET_MGMT_EVENT_BIT | _NET_MGMT_SUPPLICANT_BASE) + +enum net_event_supplicant_cmd { + NET_EVENT_SUPPLICANT_CMD_READY = 1, + NET_EVENT_SUPPLICANT_CMD_NOT_READY, + NET_EVENT_SUPPLICANT_CMD_IFACE_ADDED, + NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVING, + NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVED, + NET_EVENT_SUPPLICANT_CMD_INT_EVENT, + NET_EVENT_WIFI_CMD_MAX }; -#define NET_EVENT_WPA_SUPP_READY \ - (_NET_MGMT_WPA_SUPP_EVENT | NET_EVENT_WPA_SUPP_CMD_READY) +#define NET_EVENT_SUPPLICANT_READY \ + (_NET_MGMT_SUPPLICANT_EVENT | NET_EVENT_SUPPLICANT_CMD_READY) -#define NET_EVENT_WPA_SUPP_NOT_READY \ - (_NET_MGMT_WPA_SUPP_EVENT | NET_EVENT_WPA_SUPP_CMD_NOT_READY) +#define NET_EVENT_SUPPLICANT_NOT_READY \ + (_NET_MGMT_SUPPLICANT_EVENT | NET_EVENT_SUPPLICANT_CMD_NOT_READY) -#define NET_EVENT_WPA_SUPP_IFACE_ADDED \ - (_NET_MGMT_WPA_SUPP_EVENT | NET_EVENT_WPA_SUPP_CMD_IFACE_ADDED) +#define NET_EVENT_SUPPLICANT_IFACE_ADDED \ + (_NET_MGMT_SUPPLICANT_EVENT | NET_EVENT_SUPPLICANT_CMD_IFACE_ADDED) -#define NET_EVENT_WPA_SUPP_IFACE_REMOVED \ - (_NET_MGMT_WPA_SUPP_EVENT | NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED) +#define NET_EVENT_SUPPLICANT_IFACE_REMOVED \ + (_NET_MGMT_SUPPLICANT_EVENT | NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVED) -#define NET_EVENT_WPA_SUPP_IFACE_REMOVING \ - (_NET_MGMT_WPA_SUPP_EVENT | NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVING) +#define NET_EVENT_SUPPLICANT_IFACE_REMOVING \ + (_NET_MGMT_SUPPLICANT_EVENT | NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVING) -#define NET_EVENT_WPA_SUPP_INT_EVENT \ - (_NET_MGMT_WPA_SUPP_EVENT | NET_EVENT_WPA_SUPP_CMD_INT_EVENT) +#define NET_EVENT_SUPPLICANT_INT_EVENT \ + (_NET_MGMT_SUPPLICANT_EVENT | NET_EVENT_SUPPLICANT_CMD_INT_EVENT) -int send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event, void *status, - size_t len); -int generate_supp_state_event(const char *ifname, enum net_event_wpa_supp_cmd event, int status); +int supplicant_send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event, void *status, + size_t len); +int supplicant_generate_state_event(const char *ifname, enum net_event_supplicant_cmd event, int status); #define REASON_CODE_LEN 18 #define NM_WIFI_EVENT_STR_LEN 64 #define ETH_ALEN 6 -union supp_event_data { - struct supp_event_auth_reject { +union supplicant_event_data { + struct supplicant_event_auth_reject { int auth_type; int auth_transaction; int status_code; uint8_t bssid[ETH_ALEN]; } auth_reject; - struct supp_event_connected { + struct supplicant_event_connected { uint8_t bssid[ETH_ALEN]; char ssid[WIFI_SSID_MAX_LEN]; int id; } connected; - struct supp_event_disconnected { + struct supplicant_event_disconnected { uint8_t bssid[ETH_ALEN]; int reason_code; int locally_generated; } disconnected; - struct supp_event_assoc_reject { + struct supplicant_event_assoc_reject { int status_code; int reason_code; } assoc_reject; - struct supp_event_temp_disabled { + struct supplicant_event_temp_disabled { int id; char ssid[WIFI_SSID_MAX_LEN]; unsigned int auth_failures; @@ -86,55 +86,53 @@ union supp_event_data { char reason_code[REASON_CODE_LEN]; } temp_disabled; - struct supp_event_reenabled { + struct supplicant_event_reenabled { int id; char ssid[WIFI_SSID_MAX_LEN]; } reenabled; - struct supp_event_bss_added { + struct supplicant_event_bss_added { unsigned int id; uint8_t bssid[ETH_ALEN]; } bss_added; - struct supp_event_bss_removed { + struct supplicant_event_bss_removed { unsigned int id; uint8_t bssid[ETH_ALEN]; } bss_removed; - struct supp_event_network_added { + struct supplicant_event_network_added { unsigned int id; } network_added; - struct supp_event_network_removed { + struct supplicant_event_network_removed { unsigned int id; } network_removed; - char supp_event_str[NM_WIFI_EVENT_STR_LEN]; + char supplicant_event_str[NM_WIFI_EVENT_STR_LEN]; }; -enum supp_event_num { - WPA_SUPP_EVENT_CONNECTED, - WPA_SUPP_EVENT_DISCONNECTED, - WPA_SUPP_EVENT_ASSOC_REJECT, - WPA_SUPP_EVENT_AUTH_REJECT, - WPA_SUPP_EVENT_TERMINATING, - WPA_SUPP_EVENT_SSID_TEMP_DISABLED, - WPA_SUPP_EVENT_SSID_REENABLED, - WPA_SUPP_EVENT_SCAN_STARTED, - WPA_SUPP_EVENT_SCAN_RESULTS, - WPA_SUPP_EVENT_SCAN_FAILED, - WPA_SUPP_EVENT_BSS_ADDED, - WPA_SUPP_EVENT_BSS_REMOVED, - WPA_SUPP_EVENT_NETWORK_NOT_FOUND, - WPA_SUPP_EVENT_NETWORK_ADDED, - WPA_SUPP_EVENT_NETWORK_REMOVED, - WPA_SUPP_EVENT_DSCP_POLICY, +enum supplicant_event_num { + SUPPLICANT_EVENT_CONNECTED, + SUPPLICANT_EVENT_DISCONNECTED, + SUPPLICANT_EVENT_ASSOC_REJECT, + SUPPLICANT_EVENT_AUTH_REJECT, + SUPPLICANT_EVENT_TERMINATING, + SUPPLICANT_EVENT_SSID_TEMP_DISABLED, + SUPPLICANT_EVENT_SSID_REENABLED, + SUPPLICANT_EVENT_SCAN_STARTED, + SUPPLICANT_EVENT_SCAN_RESULTS, + SUPPLICANT_EVENT_SCAN_FAILED, + SUPPLICANT_EVENT_BSS_ADDED, + SUPPLICANT_EVENT_BSS_REMOVED, + SUPPLICANT_EVENT_NETWORK_NOT_FOUND, + SUPPLICANT_EVENT_NETWORK_ADDED, + SUPPLICANT_EVENT_NETWORK_REMOVED, + SUPPLICANT_EVENT_DSCP_POLICY, }; - - -struct supp_int_event_data { - enum supp_event_num event; +struct supplicant_int_event_data { + enum supplicant_event_num event; void *data; size_t data_len; }; diff --git a/modules/hostap/src/supp_main.c b/modules/hostap/src/supp_main.c index 034151b18463ba8..d3b109b60e2dabb 100644 --- a/modules/hostap/src/supp_main.c +++ b/modules/hostap/src/supp_main.c @@ -1,388 +1,425 @@ /* - * Copyright (c) 2023 Nordic Semiconductor ASA + * Copyright (c) 2023 Nordic Semiconductor ASA. * * SPDX-License-Identifier: Apache-2.0 */ #include -#include -LOG_MODULE_REGISTER(wpa_supplicant, LOG_LEVEL_DBG); +LOG_MODULE_REGISTER(wifi_supplicant, CONFIG_WIFI_NM_WPA_SUPPLICANT_LOG_LEVEL); -#if defined(CONFIG_WPA_SUPP_CRYPTO) && !defined(CONFIG_MBEDTLS_ENABLE_HEAP) -#include -#endif /* CONFIG_WPA_SUPP_CRYPTO */ +#include +#include +#include -#include -#include +#include #include +#include + +static K_THREAD_STACK_DEFINE(supplicant_thread_stack, + CONFIG_WIFI_NM_WPA_SUPPLICANT_THREAD_STACK_SIZE); +static struct k_thread tid; + +static K_THREAD_STACK_DEFINE(iface_wq_stack, CONFIG_WIFI_NM_WPA_SUPPLICANT_WQ_STACK_SIZE); + +static K_SEM_DEFINE(interface_ready, 0, 1); + +#define IFACE_NOTIFY_TIMEOUT_MS 1000 +#define IFACE_NOTIFY_RETRY_MS 10 + +#include "supp_main.h" +#include "supp_api.h" +#include "supp_events.h" #include "includes.h" #include "common.h" #include "eloop.h" #include "wpa_supplicant/config.h" #include "wpa_supplicant_i.h" - #include "fst/fst.h" #include "includes.h" -#include "p2p_supplicant.h" -#include "driver_i.h" - -#include "supp_main.h" -#include "supp_events.h" +//#include "p2p_supplicant.h" +//#include "driver_i.h" #include "wpa_cli_zephyr.h" -#include "supp_api.h" +static const struct wifi_mgmt_ops mgmt_ops = { + .scan = supplicant_scan, + .connect = supplicant_connect, + .disconnect = supplicant_disconnect, + .iface_status = supplicant_status, +#ifdef CONFIG_NET_STATISTICS_WIFI + .get_stats = supplicant_get_stats, +#endif + .set_power_save = supplicant_set_power_save, + .set_twt = supplicant_set_twt, + .get_power_save_config = supplicant_get_power_save_config, + .reg_domain = supplicant_reg_domain, +}; -K_SEM_DEFINE(z_wpas_ready_sem, 0, 1); -#include +DEFINE_WIFI_NM_INSTANCE(wifi_supplicant, &mgmt_ops); -/* Should match with the driver name */ -#define DEFAULT_IFACE_NAME "wlan0" -#define IFACE_MATCHING_PREFIX "wlan" +#define WRITE_TIMEOUT 100 /* ms */ +#define INTERFACE_EVENT_MASK (NET_EVENT_IF_ADMIN_UP | NET_EVENT_IF_ADMIN_DOWN) -static struct net_mgmt_event_callback cb; -struct k_mutex iface_up_mutex; +struct supplicant_context { + struct wpa_global *supplicant; + struct net_mgmt_event_callback cb; + struct net_if *iface; + char if_name[CONFIG_NET_INTERFACE_NAME_LEN + 1]; + int event_socketpair[2]; + struct k_work iface_work; + struct k_work_q iface_wq; + int (*iface_handler)(struct supplicant_context *ctx, struct net_if *iface); +}; -struct wpa_global *global; +static struct supplicant_context *get_default_context(void) +{ + static struct supplicant_context ctx; -static int z_wpas_event_sockpair[2]; + return &ctx; +} -static void z_wpas_start(void); -static void z_wpas_iface_work_handler(struct k_work *item); +struct wpa_global *zephyr_get_default_supplicant_context(void) +{ + return get_default_context()->supplicant; +} -static K_THREAD_STACK_DEFINE(z_wpa_s_thread_stack, - CONFIG_WIFI_NM_WPA_SUPPLICANT_THREAD_STACK_SIZE); -static struct k_thread z_wpa_s_tid; +int zephyr_wifi_send_event(const struct wpa_supplicant_event_msg *msg) +{ + struct supplicant_context *ctx; + struct pollfd fds[1]; + int ret; -static K_THREAD_STACK_DEFINE(z_wpas_iface_wq_stack, - CONFIG_WIFI_NM_WPA_SUPPLICANT_WQ_STACK_SIZE); + /* TODO: Fix this to get the correct container */ + ctx = get_default_context(); + if (ctx->supplicant != msg->ctx) { + LOG_WRN("Supplicant context mismatch!"); + return -ENOENT; + } -/* TODO: Debug why wsing system workqueue blocks the driver dedicated - * workqueue? - */ -static struct k_work_q z_wpas_iface_wq; -static K_WORK_DEFINE(z_wpas_iface_work, - z_wpas_iface_work_handler); + if (ctx->event_socketpair[1] < 0) { + ret = -ENOENT; + goto out; + } -K_MUTEX_DEFINE(z_wpas_event_mutex); + fds[0].fd = ctx->event_socketpair[0]; + fds[0].events = POLLOUT; + fds[0].revents = 0; -static const struct wifi_mgmt_ops wpa_supp_ops = { - .scan = z_wpa_supplicant_scan, - .connect = z_wpa_supplicant_connect, - .disconnect = z_wpa_supplicant_disconnect, - .iface_status = z_wpa_supplicant_status, -#ifdef CONFIG_NET_STATISTICS_WIFI - .get_stats = z_wpa_supplicant_get_stats, -#endif - .set_power_save = z_wpa_supplicant_set_power_save, - .set_twt = z_wpa_supplicant_set_twt, - .get_power_save_config = z_wpa_supplicant_get_power_save_config, - .reg_domain = z_wpa_supplicant_reg_domain, -}; + ret = zsock_poll(fds, 1, WRITE_TIMEOUT); + if (ret < 0) { + ret = -errno; + LOG_ERR("Cannot write event (%d)", ret); + goto out; + } -DEFINE_WIFI_NM_INSTANCE(wpa_supplicant, &wpa_supp_ops); + ret = zsock_send(ctx->event_socketpair[1], msg, sizeof(*msg), 0); + if (ret < 0) { + ret = -errno; + LOG_WRN("Event send failed (%d)", ret); + goto out; + } -#ifdef CONFIG_MATCH_IFACE -static int z_wpas_init_match(struct wpa_global *global) + if (ret != sizeof(*msg)) { + ret = -EMSGSIZE; + LOG_WRN("Event partial send (%d)", ret); + goto out; + } + + ret = 0; + +out: + return ret; +} + +static int send_event(const struct wpa_supplicant_event_msg *msg) { - /* - * The assumption is that the first driver is the primary driver and - * will handle the arrival / departure of interfaces. - */ - if (wpa_drivers[0]->global_init && !global->drv_priv[0]) { - global->drv_priv[0] = wpa_drivers[0]->global_init(global); - if (!global->drv_priv[0]) { - wpa_printf(MSG_ERROR, - "Failed to initialize driver '%s'", - wpa_drivers[0]->name); - return -1; - } + return zephyr_wifi_send_event(msg); +} + +static bool is_wanted_interface(struct net_if *iface) +{ + if (!net_if_is_wifi(iface)) { + return false; } - return 0; + /* TODO: check against a list of valid interfaces */ + + return true; } -#endif /* CONFIG_MATCH_IFACE */ -struct wpa_supplicant *z_wpas_get_handle_by_ifname(const char *ifname) +struct wpa_supplicant *zephyr_get_handle_by_ifname(const char *ifname) { struct wpa_supplicant *wpa_s = NULL; - int ret = k_sem_take(&z_wpas_ready_sem, K_SECONDS(2)); + struct supplicant_context *ctx = get_default_context(); + int ret; + ret = k_sem_take(&interface_ready, K_SECONDS(2)); if (ret) { wpa_printf(MSG_ERROR, "%s: WPA supplicant not ready: %d", __func__, ret); + k_sem_give(&interface_ready); return NULL; } - k_sem_give(&z_wpas_ready_sem); + k_sem_give(&interface_ready); - wpa_s = wpa_supplicant_get_iface(global, ifname); + wpa_s = wpa_supplicant_get_iface(ctx->supplicant, ifname); if (!wpa_s) { - wpa_printf(MSG_ERROR, - "%s: Unable to get wpa_s handle for %s\n", __func__, ifname); + wpa_printf(MSG_ERROR, "%s: Unable to get wpa_s handle for %s\n", __func__, ifname); return NULL; } return wpa_s; } -static int z_wpas_get_iface_count(void) +static int get_iface_count(struct supplicant_context *ctx) { + // FIXME, cannot access ifaces as it is supplicant internal data struct wpa_supplicant *wpa_s; unsigned int count = 0; - for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { + for (wpa_s = ctx->supplicant->ifaces; wpa_s; wpa_s = wpa_s->next) { count += 1; } + return count; } -#define Z_WPA_S_IFACE_NOTIFY_TIMEOUT_MS 1000 -#define Z_WPA_S_IFACE_NOTIFY_RETRY_MS 10 - -static int z_wpas_add_interface(const char *ifname) +static int add_interface(struct supplicant_context *ctx, struct net_if *iface) { struct wpa_supplicant *wpa_s; - struct net_if *iface; - int ret = -1; - int retry = 0, count = Z_WPA_S_IFACE_NOTIFY_TIMEOUT_MS / Z_WPA_S_IFACE_NOTIFY_RETRY_MS; - - wpa_printf(MSG_DEBUG, "Adding interface %s\n", ifname); + char ifname[IFNAMSIZ + 1] = { 0 }; + int ret, retry = 0, count = IFACE_NOTIFY_TIMEOUT_MS / IFACE_NOTIFY_RETRY_MS; - iface = net_if_lookup_by_dev(device_get_binding(ifname)); - if (!iface) { - wpa_printf(MSG_ERROR, "Failed to get net_if handle for %s", ifname); - goto err; + ret = net_if_get_name(iface, ifname, sizeof(ifname) - 1); + if (ret < 0) { + LOG_ERR("Cannot get interface %d (%p) name", net_if_get_by_iface(iface), iface); + goto out; } - ret = z_wpa_cli_global_cmd_v("interface_add %s %s %s %s", - ifname, "zephyr", "zephyr", "zephyr"); + LOG_DBG("Adding interface %s %d (%p)", ifname, net_if_get_by_iface(iface), iface); + + ret = zephyr_wpa_cli_global_cmd_v("interface_add %s %s %s %s", + ifname, "zephyr", "zephyr", "zephyr"); if (ret) { - wpa_printf(MSG_ERROR, "Failed to add interface: %s", ifname); - goto err; + LOG_ERR("Failed to add interface %s", ifname); + goto out; } - /* This cannot be through control interface as need the handle */ - while (retry++ < count && !wpa_supplicant_get_iface(global, ifname)) { - k_sleep(K_MSEC(Z_WPA_S_IFACE_NOTIFY_RETRY_MS)); + while (retry++ < count && !wpa_supplicant_get_iface(ctx->supplicant, ifname)) { + k_sleep(K_MSEC(IFACE_NOTIFY_RETRY_MS)); } - wpa_s = wpa_supplicant_get_iface(global, ifname); + wpa_s = wpa_supplicant_get_iface(ctx->supplicant, ifname); if (wpa_s == NULL) { - wpa_printf(MSG_ERROR, "Failed to add iface: %s", ifname); - goto err; + LOG_ERR("Failed to add iface %s", ifname); + goto out; } wpa_s->conf->filter_ssids = 1; wpa_s->conf->ap_scan = 1; - /* Default interface, kick start wpa_supplicant */ - if (z_wpas_get_iface_count() == 1) { - k_mutex_unlock(&iface_up_mutex); + /* Default interface, kick start supplicant */ + if (get_iface_count(ctx) > 0) { + ctx->iface = iface; + net_if_get_name(iface, ctx->if_name, CONFIG_NET_INTERFACE_NAME_LEN); + + k_sem_give(&interface_ready); } - ret = z_wpa_ctrl_init(wpa_s); + ret = zephyr_wpa_ctrl_init(wpa_s); if (ret) { - wpa_printf(MSG_ERROR, "Failed to initialize control interface"); - goto err; + LOG_ERR("Failed to initialize supplicant control interface"); + goto out; } ret = wifi_nm_register_mgd_iface(wifi_nm_get_instance("wpa_supplicant"), iface); - if (ret) { - wpa_printf(MSG_ERROR, "Failed to register mgd iface with native stack: %s (%d)", + LOG_ERR("Failed to register mgd iface with native stack %s (%d)", ifname, ret); - goto err; + goto out; } - generate_supp_state_event(ifname, NET_EVENT_WPA_SUPP_CMD_IFACE_ADDED, 0); + supplicant_generate_state_event(ifname, NET_EVENT_SUPPLICANT_CMD_IFACE_ADDED, 0); - if (z_wpas_get_iface_count() == 1) { - generate_supp_state_event(ifname, NET_EVENT_WPA_SUPP_CMD_READY, 0); + if (get_iface_count(ctx) == 1) { + supplicant_generate_state_event(ifname, NET_EVENT_SUPPLICANT_CMD_READY, 0); } - return 0; -err: + ret = 0; + +out: return ret; } -static int z_wpas_remove_interface(const char *ifname) +static int del_interface(struct supplicant_context *ctx, struct net_if *iface) { - int ret = -1; - union wpa_event_data *event = os_zalloc(sizeof(*event)); - struct wpa_supplicant *wpa_s = wpa_supplicant_get_iface(global, ifname); - struct net_if *iface = net_if_lookup_by_dev(device_get_binding(ifname)); + struct wpa_supplicant_event_msg msg; + struct wpa_supplicant *wpa_s; + union wpa_event_data *event = NULL; + int ret, retry = 0, count = IFACE_NOTIFY_TIMEOUT_MS / IFACE_NOTIFY_RETRY_MS; + char ifname[IFNAMSIZ + 1] = { 0 }; - int retry = 0, count = Z_WPA_S_IFACE_NOTIFY_TIMEOUT_MS / Z_WPA_S_IFACE_NOTIFY_RETRY_MS; + ret = net_if_get_name(iface, ifname, sizeof(ifname) - 1); + if (ret < 0) { + LOG_ERR("Cannot get interface %d (%p) name", net_if_get_by_iface(iface), iface); + goto out; + } + + LOG_DBG("Removing interface %s %d (%p)", ifname, net_if_get_by_iface(iface), iface); + event = os_zalloc(sizeof(*event)); if (!event) { - wpa_printf(MSG_ERROR, "Failed to allocate event data"); - goto err; + ret = -ENOMEM; + LOG_ERR("Failed to allocate event data"); + goto out; } + wpa_s = wpa_supplicant_get_iface(ctx->supplicant, ifname); if (!wpa_s) { - wpa_printf(MSG_ERROR, "Failed to get wpa_s handle for %s", ifname); - goto err; - } - - if (!iface) { - wpa_printf(MSG_ERROR, "Failed to get net_if handle for %s", ifname); - goto err; + ret = -ENOENT; + LOG_ERR("Failed to get wpa_s handle for %s", ifname); + goto out; } - generate_supp_state_event(ifname, NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVING, 0); - wpa_printf(MSG_DEBUG, "Remove interface %s\n", ifname); + supplicant_generate_state_event(ifname, NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVING, 0); os_memcpy(event->interface_status.ifname, ifname, IFNAMSIZ); event->interface_status.ievent = EVENT_INTERFACE_REMOVED; - struct wpa_supplicant_event_msg msg = { - .global = true, - .ctx = global, - .event = EVENT_INTERFACE_STATUS, - .data = event, - }; + msg.global = true; + msg.ctx = ctx->supplicant; + msg.event = EVENT_INTERFACE_STATUS; + msg.data = event; - z_wpas_send_event(&msg); + send_event(&msg); - while (retry++ < count && - wpa_s->wpa_state != WPA_INTERFACE_DISABLED) { - k_sleep(K_MSEC(Z_WPA_S_IFACE_NOTIFY_RETRY_MS)); + while (retry++ < count && wpa_s->wpa_state != WPA_INTERFACE_DISABLED) { + k_sleep(K_MSEC(IFACE_NOTIFY_RETRY_MS)); } if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED) { - wpa_printf(MSG_ERROR, "Failed to notify remove interface: %s", ifname); - generate_supp_state_event(ifname, NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED, - -1); - goto err; + LOG_ERR("Failed to notify remove interface %s", ifname); + supplicant_generate_state_event(ifname, NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVED, -1); + goto out; } - z_wpa_ctrl_deinit(wpa_s); - - ret = z_wpa_cli_global_cmd_v("interface_remove %s", ifname); + ret = zephyr_wpa_cli_global_cmd_v("interface_remove %s", ifname); if (ret) { - wpa_printf(MSG_ERROR, "Failed to remove interface: %s", ifname); - generate_supp_state_event(ifname, NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED, - -EINVAL); - goto err; + LOG_ERR("Failed to remove interface %s", ifname); + supplicant_generate_state_event(ifname, NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVED, + -EINVAL); + goto out; } ret = wifi_nm_unregister_mgd_iface(wifi_nm_get_instance("wpa_supplicant"), iface); if (ret) { - wpa_printf(MSG_ERROR, "Failed to unregister mgd iface with native stack: %s (%d)", + LOG_ERR("Failed to unregister mgd iface %s with native stack (%d)", ifname, ret); - goto err; + goto out; } - - if (z_wpas_get_iface_count() == 0) { - generate_supp_state_event(ifname, NET_EVENT_WPA_SUPP_CMD_NOT_READY, 0); + if (get_iface_count(ctx) == 0) { + supplicant_generate_state_event(ifname, NET_EVENT_SUPPLICANT_CMD_NOT_READY, 0); } - generate_supp_state_event(ifname, NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED, 0); + supplicant_generate_state_event(ifname, NET_EVENT_SUPPLICANT_CMD_IFACE_REMOVED, 0); - return 0; -err: +out: if (event) { os_free(event); } + return ret; } -static void iface_event_handler(struct net_mgmt_event_callback *cb, - uint32_t mgmt_event, struct net_if *iface) +static void iface_work_handler(struct k_work *work) { - const char *ifname = iface->if_dev->dev->name; - - if (strncmp(ifname, IFACE_MATCHING_PREFIX, sizeof(IFACE_MATCHING_PREFIX) - 1) != 0) { - return; - } + struct supplicant_context *ctx = CONTAINER_OF(work, struct supplicant_context, + iface_work); + int ret; - wpa_printf(MSG_DEBUG, "Event: %d", mgmt_event); - if (mgmt_event == NET_EVENT_IF_ADMIN_UP) { - z_wpas_add_interface(ifname); - } else if (mgmt_event == NET_EVENT_IF_ADMIN_DOWN) { - z_wpas_remove_interface(ifname); + ret = (*ctx->iface_handler)(ctx, ctx->iface); + if (ret < 0) { + LOG_ERR("Interface %d (%p) handler failed (%d)", + net_if_get_by_iface(ctx->iface), ctx->iface, ret); } } -static void register_iface_events(void) +/* As the mgmt thread stack is limited, use a separate work queue for any network + * interface add/delete. + */ +static void submit_iface_work(struct supplicant_context *ctx, + struct net_if *iface, + int (*handler)(struct supplicant_context *ctx, + struct net_if *iface)) { - k_mutex_init(&iface_up_mutex); - - k_mutex_lock(&iface_up_mutex, K_FOREVER); - net_mgmt_init_event_callback(&cb, iface_event_handler, - NET_EVENT_IF_ADMIN_UP | NET_EVENT_IF_ADMIN_DOWN); - net_mgmt_add_event_callback(&cb); -} + ctx->iface_handler = handler; -static void wait_for_interface_up(const char *iface_name) -{ - if (z_wpas_get_iface_count() == 0) { - k_mutex_lock(&iface_up_mutex, K_FOREVER); - } + k_work_submit_to_queue(&ctx->iface_wq, &ctx->iface_work); } -#include "config.h" -static void iface_cb(struct net_if *iface, void *user_data) +static void interface_handler(struct net_mgmt_event_callback *cb, + uint32_t mgmt_event, struct net_if *iface) { - const char *ifname = iface->if_dev->dev->name; + struct supplicant_context *ctx = CONTAINER_OF(cb, struct supplicant_context, + cb); - if (ifname == NULL) { + if ((mgmt_event & INTERFACE_EVENT_MASK) != mgmt_event) { return; } - if (strncmp(ifname, DEFAULT_IFACE_NAME, strlen(ifname)) != 0) { + if (!is_wanted_interface(iface)) { + LOG_DBG("Ignoring event (0x%02x) from interface %d (%p)", + mgmt_event, net_if_get_by_iface(iface), iface); return; } - /* Check default interface */ - if (net_if_is_admin_up(iface)) { - z_wpas_add_interface(ifname); + if (mgmt_event == NET_EVENT_IF_ADMIN_UP) { + LOG_INF("Network interface %d (%p) up", net_if_get_by_iface(iface), iface); + submit_iface_work(ctx, iface, add_interface); + return; } - register_iface_events(); + if (mgmt_event == NET_EVENT_IF_ADMIN_DOWN) { + LOG_INF("Network interface %d (%p) down", net_if_get_by_iface(iface), iface); + submit_iface_work(ctx, iface, del_interface); + return; + } } - -static void z_wpas_iface_work_handler(struct k_work *item) +static int setup_interface_monitoring(struct supplicant_context *ctx, struct net_if *iface) { - ARG_UNUSED(item); + ARG_UNUSED(iface); - int ret = k_sem_take(&z_wpas_ready_sem, K_SECONDS(5)); + net_mgmt_init_event_callback(&ctx->cb, interface_handler, + INTERFACE_EVENT_MASK); + net_mgmt_add_event_callback(&ctx->cb); - if (ret) { - wpa_printf(MSG_ERROR, "Timed out waiting for wpa_supplicant"); - return; - } - - net_if_foreach(iface_cb, NULL); - wait_for_interface_up(DEFAULT_IFACE_NAME); - - k_sem_give(&z_wpas_ready_sem); + return 0; } -static void z_wpas_event_sock_handler(int sock, void *eloop_ctx, void *sock_ctx) +static void event_socket_handler(int sock, void *eloop_ctx, void *user_data) { + struct supplicant_context *ctx = user_data; + struct wpa_supplicant_event_msg msg; int ret; ARG_UNUSED(eloop_ctx); - ARG_UNUSED(sock_ctx); - struct wpa_supplicant_event_msg msg; - - ret = recv(sock, &msg, sizeof(msg), 0); + ARG_UNUSED(ctx); + ret = zsock_recv(sock, &msg, sizeof(msg), 0); if (ret < 0) { - wpa_printf(MSG_ERROR, "Failed to recv the message: %s", strerror(errno)); + LOG_ERR("Failed to recv the message (%d)", -errno); return; } if (ret != sizeof(msg)) { - wpa_printf(MSG_ERROR, "Received incomplete message: got: %d, expected:%d", + LOG_ERR("Received incomplete message: got: %d, expected:%d", ret, sizeof(msg)); return; } - wpa_printf(MSG_DEBUG, "Passing message %d to wpa_supplicant", msg.event); + LOG_DBG("Passing message %d to wpa_supplicant", msg.event); if (msg.global) { wpa_supplicant_event_global(msg.ctx, msg.event, msg.data); @@ -396,64 +433,50 @@ static void z_wpas_event_sock_handler(int sock, void *eloop_ctx, void *sock_ctx) os_free((char *)data->auth.ies); } + os_free(msg.data); } } -static int register_wpa_event_sock(void) +static int register_supplicant_event_socket(struct supplicant_context *ctx) { int ret; - ret = socketpair(AF_UNIX, SOCK_STREAM, 0, z_wpas_event_sockpair); - - if (ret != 0) { - wpa_printf(MSG_ERROR, "Failed to initialize socket: %s", strerror(errno)); - return -1; + ret = socketpair(AF_UNIX, SOCK_STREAM, 0, ctx->event_socketpair); + if (ret < 0) { + ret = -errno; + LOG_ERR("Failed to initialize socket (%d)", ret); + return ret; } - eloop_register_read_sock(z_wpas_event_sockpair[0], z_wpas_event_sock_handler, NULL, NULL); + eloop_register_read_sock(ctx->event_socketpair[0], event_socket_handler, NULL, ctx); return 0; } -int z_wpas_send_event(const struct wpa_supplicant_event_msg *msg) +#ifdef CONFIG_MATCH_IFACE +static int init_match(struct wpa_global *global) { - int ret; - unsigned int retry = 0; - - k_mutex_lock(&z_wpas_event_mutex, K_FOREVER); - - if (z_wpas_event_sockpair[1] < 0) { - goto err; - } - -retry_send: - ret = send(z_wpas_event_sockpair[1], msg, sizeof(*msg), 0); - if (ret < 0) { - if (errno == EINTR || errno == EAGAIN || errno == EBUSY || errno == EWOULDBLOCK) { - k_msleep(2); - if (retry++ < 3) { - goto retry_send; - } else { - wpa_printf(MSG_WARNING, "Event send fail (max retries): %s", - strerror(errno)); - goto err; - } - } else { - wpa_printf(MSG_WARNING, "Event send fail: %s", - strerror(errno)); - goto err; + /* + * The assumption is that the first driver is the primary driver and + * will handle the arrival / departure of interfaces. + */ + if (wpa_drivers[0]->global_init && !global->drv_priv[0]) { + global->drv_priv[0] = wpa_drivers[0]->global_init(global); + if (!global->drv_priv[0]) { + LOG_ERR("Failed to initialize driver '%s'", + wpa_drivers[0]->name); + return -1; } } - ret = 0; -err: - k_mutex_unlock(&z_wpas_event_mutex); - return -1; + return 0; } +#endif /* CONFIG_MATCH_IFACE */ -static void z_wpas_start(void) +static void handler(void) { + struct supplicant_context *ctx; struct wpa_params params; int exitcode = -1; @@ -462,85 +485,91 @@ static void z_wpas_start(void) mbedtls_platform_set_calloc_free(calloc, free); #endif /* CONFIG_WPA_CRYPTO */ - k_work_queue_init(&z_wpas_iface_wq); + ctx = get_default_context(); + + k_work_queue_init(&ctx->iface_wq); + k_work_queue_start(&ctx->iface_wq, iface_wq_stack, + K_THREAD_STACK_SIZEOF(iface_wq_stack), + CONFIG_WIFI_NM_WPA_SUPPLICANT_WQ_PRIO, + NULL); - k_work_queue_start(&z_wpas_iface_wq, - z_wpas_iface_wq_stack, - K_THREAD_STACK_SIZEOF(z_wpas_iface_wq_stack), - 7, - NULL); + k_work_init(&ctx->iface_work, iface_work_handler); - os_memset(¶ms, 0, sizeof(params)); + memset(¶ms, 0, sizeof(params)); params.wpa_debug_level = CONFIG_WIFI_NM_WPA_SUPPLICANT_DEBUG_LEVEL; - exitcode = 0; - global = wpa_supplicant_init(¶ms); - if (global == NULL) { - wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant"); - exitcode = -1; - goto out; - } else { - wpa_printf(MSG_INFO, "Successfully initialized " - "wpa_supplicant"); + ctx->supplicant = wpa_supplicant_init(¶ms); + if (ctx->supplicant == NULL) { + LOG_ERR("Failed to initialize %s", "wpa_supplicant"); + goto err; } + LOG_INF("%s initialized", "wpa_supplicant"); + if (fst_global_init()) { - wpa_printf(MSG_ERROR, "Failed to initialize FST"); - exitcode = -1; + LOG_ERR("Failed to initialize %s", "FST"); goto out; } #if defined(CONFIG_FST) && defined(CONFIG_CTRL_IFACE) if (!fst_global_add_ctrl(fst_ctrl_cli)) { - wpa_printf(MSG_WARNING, "Failed to add CLI FST ctrl"); + LOG_WARN("Failed to add CLI FST ctrl"); } #endif - z_global_wpa_ctrl_init(); + zephyr_global_wpa_ctrl_init(); + + register_supplicant_event_socket(ctx); - register_wpa_event_sock(); + submit_iface_work(ctx, NULL, setup_interface_monitoring); - k_work_submit_to_queue(&z_wpas_iface_wq, &z_wpas_iface_work); + (void)k_sem_take(&interface_ready, K_FOREVER); #ifdef CONFIG_MATCH_IFACE if (exitcode == 0) { - exitcode = z_wpas_init_match(global); + exitcode = init_match(ctx->supplicant); } #endif /* CONFIG_MATCH_IFACE */ if (exitcode == 0) { - k_sem_give(&z_wpas_ready_sem); - exitcode = wpa_supplicant_run(global); + k_sem_give(&interface_ready); + exitcode = wpa_supplicant_run(ctx->supplicant); } - generate_supp_state_event(DEFAULT_IFACE_NAME, NET_EVENT_WPA_SUPP_CMD_NOT_READY, 0); - eloop_unregister_read_sock(z_wpas_event_sockpair[0]); + supplicant_generate_state_event(ctx->if_name, NET_EVENT_SUPPLICANT_CMD_NOT_READY, 0); - z_global_wpa_ctrl_deinit(); - wpa_supplicant_deinit(global); + eloop_unregister_read_sock(ctx->event_socketpair[0]); - fst_global_deinit(); + zephyr_wpa_ctrl_deinit(ctx->supplicant); + zephyr_global_wpa_ctrl_deinit(); - close(z_wpas_event_sockpair[0]); - close(z_wpas_event_sockpair[1]); + fst_global_deinit(); out: + wpa_supplicant_deinit(ctx->supplicant); + + zsock_close(ctx->event_socketpair[0]); + zsock_close(ctx->event_socketpair[1]); + +err: #ifdef CONFIG_MATCH_IFACE os_free(params.match_ifaces); #endif /* CONFIG_MATCH_IFACE */ os_free(params.pid_file); - wpa_printf(MSG_INFO, "z_wpas_start: exitcode %d", exitcode); + LOG_INF("%d", exitcode); } -static int z_wpas_init(void) +static int init(void) { - k_thread_create(&z_wpa_s_tid, z_wpa_s_thread_stack, - CONFIG_WIFI_NM_WPA_SUPPLICANT_THREAD_STACK_SIZE, - (k_thread_entry_t)z_wpas_start, - NULL, NULL, NULL, - 0, 0, K_NO_WAIT); + /* We create a thread that handles all supplicant connections */ + k_thread_create(&tid, supplicant_thread_stack, + K_THREAD_STACK_SIZEOF(supplicant_thread_stack), + (k_thread_entry_t)handler, NULL, NULL, NULL, + 0, 0, + K_MSEC(3000)); + //K_NO_WAIT); return 0; } -SYS_INIT(z_wpas_init, APPLICATION, 0); +SYS_INIT(init, APPLICATION, 0); diff --git a/modules/hostap/src/supp_main.h b/modules/hostap/src/supp_main.h index bd00202575008ca..f4efc311a513bc6 100644 --- a/modules/hostap/src/supp_main.h +++ b/modules/hostap/src/supp_main.h @@ -6,14 +6,15 @@ #ifndef __SUPP_MAIN_H_ #define __SUPP_MAIN_H_ -#include "wpa_supplicant_i.h" +//#include "wpa_supplicant_i.h" -struct wpa_supplicant *z_wpas_get_handle_by_ifname(const char *ifname); +struct wpa_global *zephyr_get_default_supplicant_context(void); +struct wpa_supplicant *zephyr_get_handle_by_ifname(const char *ifname); struct wpa_supplicant_event_msg { bool global; void *ctx; unsigned int event; void *data; }; -int z_wpas_send_event(const struct wpa_supplicant_event_msg *msg); +int zephyr_wifi_send_event(const struct wpa_supplicant_event_msg *msg); #endif /* __SUPP_MAIN_H_ */