Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion subsys/epacket/interfaces/epacket_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <zephyr/device.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/net/conn_mgr_connectivity.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/net/socket.h>
Expand Down Expand Up @@ -84,6 +85,16 @@ static void if_admin_event_handler(struct net_mgmt_event_callback *cb, uint32_t
{
struct udp_state *state = CONTAINER_OF(cb, struct udp_state, iface_admin_cb);

/* Interface is not intended to be persistent, so don't restart the
* watchdog on every interface cycle. Instead, start it on the first
* event and keep it running as a global watchdog.
*/
if (conn_mgr_if_is_bound(iface) && !conn_mgr_if_get_flag(iface, CONN_MGR_IF_PERSISTENT) &&
k_work_delayable_busy_get(&state->downlink_watchdog)) {
LOG_DBG("Ignoring %08x on non-persistent interface", event);
return;
}

if (event == NET_EVENT_IF_ADMIN_UP) {
/* Application wants the interface connected, start the watchdog */
LOG_INF("Downlink watchdog started (%d sec)",
Expand Down Expand Up @@ -227,12 +238,13 @@ static int epacket_udp_loop(void *a, void *b, void *c)
/* Wait for data to arrive */
rc = zsock_poll(pollfds, 1, SYS_FOREVER_MS);
if (pollfds[0].revents & (ZSOCK_POLLHUP | ZSOCK_POLLNVAL)) {
LOG_ERR("Socket closed");
LOG_WRN("Socket closed (0x%02X)", pollfds[0].revents);
break;
}

/* Allocate buffer and receive data */
buf = epacket_alloc_rx(K_FOREVER);
from_len = sizeof(from);
received = zsock_recvfrom(udp_state.sock, buf->data, buf->size, 0, &from,
&from_len);
if (received < 0) {
Expand Down
42 changes: 39 additions & 3 deletions subsys/net/conn_mgr/conn_mgr_wifi_kv_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ static struct net_mgmt_event_callback wifi_mgmt_cb;
static struct k_work_delayable conn_config_changed;
static struct k_work_delayable conn_create;
static struct k_work_delayable conn_timeout;
static struct k_work_delayable idle_timeout;
static struct k_work conn_terminate;
static struct net_if *wifi_if;
static bool connection_requested;
static bool did_idle_timeout;

LOG_MODULE_REGISTER(wifi_mgmt, LOG_LEVEL_INF);

Expand Down Expand Up @@ -102,6 +104,11 @@ static void conn_timeout_worker(struct k_work *work)
net_mgmt_event_notify(NET_EVENT_CONN_IF_TIMEOUT, wifi_if);
}

static void wifi_mgmt_used(struct conn_mgr_conn_binding *const binding)
{
k_work_reschedule(&idle_timeout, K_SECONDS(binding->idle_timeout));
}

static void conn_config_changed_worker(struct k_work *work)
{
int timeout;
Expand Down Expand Up @@ -135,6 +142,7 @@ static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t
{
const struct wifi_status *status = cb->info;
bool persistent = conn_mgr_if_get_flag(wifi_if, CONN_MGR_IF_PERSISTENT);
struct conn_mgr_conn_binding *const binding = conn_mgr_if_get_binding(iface);
int timeout;

if (iface != wifi_if) {
Expand All @@ -147,6 +155,11 @@ static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t
/* Cancel any pending work timeout */
LOG_INF("Connection successful");
(void)k_work_cancel_delayable(&conn_timeout);
/* Start idle timeout if configured */
did_idle_timeout = false;
if (binding->idle_timeout != CONN_MGR_IF_NO_TIMEOUT) {
wifi_mgmt_used(binding);
}
} else {
/* Attempt to schedule the connection again */
LOG_WRN("Connection failed, retrying (%d)", status->conn_status);
Expand All @@ -161,9 +174,20 @@ static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t
}
LOG_INF("Connection lost (%d)%s", status->disconn_reason,
persistent ? ", retrying" : "");

/* Cancel any pending idle timeouts */
if (binding->idle_timeout != CONN_MGR_IF_NO_TIMEOUT) {
k_work_cancel_delayable(&idle_timeout);
}

if (persistent) {
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT
/* WPA SUPP automatically attempts to reconnect */
/* WPA SUPP automatically attempts to reconnect.
* Unless we requested the disconnect in idle timeout.
*/
if (did_idle_timeout) {
k_work_schedule(&conn_create, K_SECONDS(1));
}
#else
/* Schedule reconnection attempt */
k_work_schedule(&conn_create, K_SECONDS(1));
Expand All @@ -175,8 +199,10 @@ static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t
}
} else {
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT
/* Stop reconnection attempts */
(void)net_mgmt(NET_REQUEST_WIFI_DISCONNECT, wifi_if, NULL, 0);
/* Stop reconnection attempts (unless idle timeout already did) */
if (!did_idle_timeout) {
(void)net_mgmt(NET_REQUEST_WIFI_DISCONNECT, wifi_if, NULL, 0);
}
#endif /* CONFIG_WIFI_NM_WPA_SUPPLICANT */
}
break;
Expand All @@ -200,6 +226,14 @@ static int wifi_mgmt_connect(struct conn_mgr_conn_binding *const binding)
return 0;
}

static void conn_idle_worker(struct k_work *work)
{
LOG_INF("Interface idle");
did_idle_timeout = true;
(void)net_mgmt(NET_REQUEST_WIFI_DISCONNECT, wifi_if, NULL, 0);
net_mgmt_event_notify(NET_EVENT_CONN_IF_IDLE_TIMEOUT, wifi_if);
}

static void conn_terminate_worker(struct k_work *work)
{
(void)net_mgmt(NET_REQUEST_WIFI_DISCONNECT, wifi_if, NULL, 0);
Expand Down Expand Up @@ -247,6 +281,7 @@ static void wifi_mgmt_init(struct conn_mgr_conn_binding *const binding)

k_work_init_delayable(&conn_create, conn_create_worker);
k_work_init_delayable(&conn_timeout, conn_timeout_worker);
k_work_init_delayable(&idle_timeout, conn_idle_worker);
k_work_init_delayable(&conn_config_changed, conn_config_changed_worker);
k_work_init(&conn_terminate, conn_terminate_worker);

Expand All @@ -265,6 +300,7 @@ static void wifi_mgmt_init(struct conn_mgr_conn_binding *const binding)

static struct conn_mgr_conn_api l2_wifi_conn_api = {
.connect = wifi_mgmt_connect,
.used = wifi_mgmt_used,
.disconnect = wifi_mgmt_disconnect,
.init = wifi_mgmt_init,
};
Expand Down
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ manifest:

projects:
- name: zephyr
revision: fe8ef68db4ece8467d1b1f09ea3aeb29771c8f68
revision: 90f15195292948672e185374cf5ec2e85859bef5
# Limit imported repositories to reduce clone time
import:
name-allowlist:
Expand Down
Loading