diff --git a/subsys/net/conn_mgr/conn_mgr_monitor.c b/subsys/net/conn_mgr/conn_mgr_monitor.c index 0f8c9567b8ce730..0705130ffb41382 100644 --- a/subsys/net/conn_mgr/conn_mgr_monitor.c +++ b/subsys/net/conn_mgr/conn_mgr_monitor.c @@ -34,8 +34,8 @@ static struct k_thread conn_mgr_mon_thread; */ uint16_t iface_states[CONN_MGR_IFACE_MAX]; -/* Tracks the total number of L4-ready ifaces */ -static uint16_t ready_count; +/* Tracks the most recent total quantity of L4-ready ifaces */ +static uint16_t last_ready_count; /* Tracks the last ifaces to change state in each respective direction */ static struct net_if *last_iface_down; @@ -82,11 +82,8 @@ static void conn_mgr_mon_set_ready(int idx, bool readiness) if (readiness) { iface_states[idx] |= CONN_MGR_IF_READY; - - ready_count += 1; last_iface_up = conn_mgr_mon_get_if_by_index(idx); } else { - ready_count -= 1; last_iface_down = conn_mgr_mon_get_if_by_index(idx); } } @@ -94,7 +91,7 @@ static void conn_mgr_mon_set_ready(int idx, bool readiness) static void conn_mgr_mon_handle_update(void) { int idx; - int original_ready_count; + int ready_count; bool is_ip_ready; bool is_ipv6_ready; bool is_ipv4_ready; @@ -105,21 +102,13 @@ static void conn_mgr_mon_handle_update(void) k_mutex_lock(&conn_mgr_mon_lock, K_FOREVER); - original_ready_count = ready_count; + ready_count = 0; for (idx = 0; idx < ARRAY_SIZE(iface_states); idx++) { if (iface_states[idx] == 0) { /* This interface is not used */ continue; } - if (!(iface_states[idx] & CONN_MGR_IF_CHANGED)) { - /* No changes on this iface */ - continue; - } - - /* Clear the state-change flag */ - iface_states[idx] &= ~CONN_MGR_IF_CHANGED; - /* Detect whether the iface is currently or was L4 ready */ was_l4_ready = iface_states[idx] & CONN_MGR_IF_READY; is_ipv6_ready = iface_states[idx] & CONN_MGR_IF_IPV6_SET; @@ -134,19 +123,27 @@ static void conn_mgr_mon_handle_update(void) /* Track the iface readiness change */ conn_mgr_mon_set_ready(idx, is_l4_ready); } + + /* Track ready iface count */ + if (is_l4_ready) { + ready_count += 1; + } + } /* If the total number of ready ifaces changed, possibly send an event */ - if (ready_count != original_ready_count) { + if (ready_count != last_ready_count) { if (ready_count == 0) { /* We just lost connectivity */ net_mgmt_event_notify(NET_EVENT_L4_DISCONNECTED, last_iface_down); - } else if (original_ready_count == 0) { + } else if (last_ready_count == 0) { /* We just gained connectivity */ net_mgmt_event_notify(NET_EVENT_L4_CONNECTED, last_iface_up); } } + last_ready_count = ready_count; + k_mutex_unlock(&conn_mgr_mon_lock); } @@ -181,8 +178,6 @@ static void conn_mgr_mon_initial_state(struct net_if *iface) } - iface_states[idx] |= CONN_MGR_IF_CHANGED; - k_mutex_unlock(&conn_mgr_mon_lock); } @@ -224,7 +219,7 @@ void conn_mgr_mon_resend_status(void) { k_mutex_lock(&conn_mgr_mon_lock, K_FOREVER); - if (ready_count == 0) { + if (last_ready_count == 0) { net_mgmt_event_notify(NET_EVENT_L4_DISCONNECTED, last_iface_down); } else { net_mgmt_event_notify(NET_EVENT_L4_CONNECTED, last_iface_up); @@ -242,7 +237,6 @@ void conn_mgr_ignore_iface(struct net_if *iface) if (!(iface_states[idx] & CONN_MGR_IF_IGNORED)) { /* Set ignored flag and mark state as changed */ iface_states[idx] |= CONN_MGR_IF_IGNORED; - iface_states[idx] |= CONN_MGR_IF_CHANGED; k_sem_give(&conn_mgr_mon_updated); } @@ -258,7 +252,6 @@ void conn_mgr_watch_iface(struct net_if *iface) if (iface_states[idx] & CONN_MGR_IF_IGNORED) { /* Clear ignored flag and mark state as changed */ iface_states[idx] &= ~CONN_MGR_IF_IGNORED; - iface_states[idx] |= CONN_MGR_IF_CHANGED; k_sem_give(&conn_mgr_mon_updated); } diff --git a/subsys/net/conn_mgr/conn_mgr_private.h b/subsys/net/conn_mgr/conn_mgr_private.h index 99aa98567a31b2b..059d16bd3f7c78f 100644 --- a/subsys/net/conn_mgr/conn_mgr_private.h +++ b/subsys/net/conn_mgr/conn_mgr_private.h @@ -30,8 +30,6 @@ /* Internal state flags */ #define CONN_MGR_IF_READY BIT(14) -/* Event flags */ -#define CONN_MGR_IF_CHANGED BIT(15) /* NET_MGMT event masks */ #define CONN_MGR_IFACE_EVENTS_MASK (NET_EVENT_IF_DOWN | \ diff --git a/subsys/net/conn_mgr/events_handler.c b/subsys/net/conn_mgr/events_handler.c index 21677770ce3c63f..ceaae4afdd4e61b 100644 --- a/subsys/net/conn_mgr/events_handler.c +++ b/subsys/net/conn_mgr/events_handler.c @@ -47,8 +47,6 @@ static void conn_mgr_iface_events_handler(struct net_mgmt_event_callback *cb, default: goto done; } - - iface_states[idx] |= CONN_MGR_IF_CHANGED; k_sem_give(&conn_mgr_mon_updated); done: @@ -95,7 +93,6 @@ static void conn_mgr_ipv6_events_handler(struct net_mgmt_event_callback *cb, goto done; } - iface_states[idx] |= CONN_MGR_IF_CHANGED; k_sem_give(&conn_mgr_mon_updated); done: @@ -148,7 +145,6 @@ static void conn_mgr_ipv4_events_handler(struct net_mgmt_event_callback *cb, goto done; } - iface_states[idx] |= CONN_MGR_IF_CHANGED; k_sem_give(&conn_mgr_mon_updated); done: