Skip to content

Commit

Permalink
modules: hostap: Implement AP mode events
Browse files Browse the repository at this point in the history
These events are used by AP mode, tied to AP-ENABLED and AP-DISABLED
events in the hostapd/wpa_supplicant.

Implements SHEL-2343.

Signed-off-by: Chaitanya Tata <[email protected]>
  • Loading branch information
krish2718 authored and nordicjm committed Jan 8, 2024
1 parent 075ca49 commit 15a9a26
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
26 changes: 24 additions & 2 deletions modules/hostap/src/supp_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <zephyr/logging/log.h>
#include <zephyr/kernel.h>
#include <zephyr/net/wifi_mgmt.h>

#include "includes.h"
#include "common.h"
Expand Down Expand Up @@ -400,15 +401,28 @@ static int wpas_disconnect_network(const struct device *dev)
{
int ret = 0;
struct net_if *iface = net_if_lookup_by_dev(dev);
struct wpa_supplicant *wpa_s;
bool is_ap = false;

if (!iface) {
ret = -EINVAL;
wpa_printf(MSG_ERROR, "Device %s not found", dev->name);
goto out;
}

wpa_s = get_wpa_s_handle(dev);
if (!wpa_s) {
ret = -1;
wpa_printf(MSG_ERROR, "Interface %s not found", dev->name);
goto out;
}

k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER);

if (wpa_s->current_ssid && wpa_s->current_ssid->mode == WPAS_MODE_AP) {
is_ap = true;
}

wpa_supp_api_ctrl.dev = dev;
wpa_supp_api_ctrl.requested_op = DISCONNECT;
_wpa_cli_cmd_v("disconnect");
Expand All @@ -425,8 +439,16 @@ static int wpas_disconnect_network(const struct device *dev)
wpa_supp_restart_status_work();

ret = wait_for_disconnect_complete(dev);

wifi_mgmt_raise_disconnect_complete_event(iface, ret);
#ifdef CONFIG_AP
if (is_ap) {
send_wifi_mgmt_ap_status(wpa_s, NET_EVENT_WIFI_CMD_AP_DISABLE_RESULT,
ret == 0 ? WIFI_STATUS_AP_SUCCESS : WIFI_STATUS_AP_FAIL);
} else {
#else
{
#endif /* CONFIG_AP */
wifi_mgmt_raise_disconnect_complete_event(iface, ret);
}

return ret;
}
Expand Down
53 changes: 50 additions & 3 deletions modules/hostap/src/supp_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,20 @@ int send_wifi_mgmt_conn_event(void *ctx, int status_code)
{
struct wpa_supplicant *wpa_s = ctx;
int status = wpas_to_wifi_mgmt_conn_status(status_code);
enum net_event_wifi_cmd event;

if (!wpa_s || !wpa_s->current_ssid) {
return -EINVAL;
}

if (wpa_s->current_ssid->mode == WPAS_MODE_AP) {
event = NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT;
} else {
event = NET_EVENT_WIFI_CMD_CONNECT_RESULT;
}

return send_wifi_mgmt_event(wpa_s->ifname,
NET_EVENT_WIFI_CMD_CONNECT_RESULT,
event,
(void *)&status,
sizeof(int));
}
Expand All @@ -204,10 +215,22 @@ int send_wifi_mgmt_disc_event(void *ctx, int reason_code)
int status = wpas_to_wifi_mgmt_diconn_status(reason_code);
enum net_event_wifi_cmd event;

if (!wpa_s || !wpa_s->current_ssid) {
return -EINVAL;
}

if (wpa_s->wpa_state >= WPA_COMPLETED) {
event = NET_EVENT_WIFI_CMD_DISCONNECT_RESULT;
if (wpa_s->current_ssid->mode == WPAS_MODE_AP) {
event = NET_EVENT_WIFI_CMD_AP_DISABLE_RESULT;
} else {
event = NET_EVENT_WIFI_CMD_DISCONNECT_RESULT;
}
} else {
event = NET_EVENT_WIFI_CMD_CONNECT_RESULT;
if (wpa_s->current_ssid->mode == WPAS_MODE_AP) {
event = NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT;
} else {
event = NET_EVENT_WIFI_CMD_CONNECT_RESULT;
}
}

return send_wifi_mgmt_event(wpa_s->ifname,
Expand All @@ -216,6 +239,20 @@ int send_wifi_mgmt_disc_event(void *ctx, int reason_code)
sizeof(int));
}

#ifdef CONFIG_AP
int send_wifi_mgmt_ap_status(void *ctx,
enum net_event_wifi_cmd event, enum wifi_ap_status ap_status)
{
struct wpa_supplicant *wpa_s = ctx;
int status = ap_status;

return send_wifi_mgmt_event(wpa_s->ifname,
event,
(void *)&status,
sizeof(int));
}
#endif /* CONFIG_AP */

int send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event,
void *wpa_supp_status, size_t len)
{
Expand All @@ -238,6 +275,16 @@ int send_wifi_mgmt_event(const char *ifname, enum net_event_wifi_cmd event,
wifi_mgmt_raise_disconnect_result_event(iface,
*(int *)wpa_supp_status);
break;
#ifdef CONFIG_AP
case NET_EVENT_WIFI_CMD_AP_ENABLE_RESULT:
wifi_mgmt_raise_ap_enable_result_event(iface,
*(int *)wpa_supp_status);
break;
case NET_EVENT_WIFI_CMD_AP_DISABLE_RESULT:
wifi_mgmt_raise_ap_disable_result_event(iface,
*(int *)wpa_supp_status);
break;
#endif /* CONFIG_AP */
case NET_EVENT_WPA_SUPP_CMD_INT_EVENT:
event_data.data = &data;
if (wpa_supp_process_status(&event_data, (char *)wpa_supp_status) > 0) {
Expand Down
4 changes: 4 additions & 0 deletions modules/hostap/src/supp_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ int send_wifi_mgmt_disc_event(void *ctx, int reason_code);
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);
#ifdef CONFIG_AP
int send_wifi_mgmt_ap_status(void *ctx,
enum net_event_wifi_cmd event, enum wifi_ap_status);
#endif /* CONFIG_AP */

#define REASON_CODE_LEN 18
#define NM_WIFI_EVENT_STR_LEN 64
Expand Down

0 comments on commit 15a9a26

Please sign in to comment.