From 8785a9f4247c830748a2a4b1bbf90ddefc20842b Mon Sep 17 00:00:00 2001 From: Georges Oates_Larsen Date: Fri, 28 Apr 2023 14:38:23 -0700 Subject: [PATCH] net: conn_mgr: Minor fixes - Add missing event sleep after taking all ifaces up in test_connect_disconnect. - Add missing event sleep after resetting ifaces in conn_mgr_conn_before - Fix typo in comment for internal state flags. - Add missing NET_MGMT_EVENT_BIT to conn_mgr_connectivity event definitions. - Missing net_mgmt.h include in conn_mgr_connectivity.h - Split conn_mgr_conn iface reset into network and state resets, before and after event sleep, so that triggered events do not corrupt the state reset. - Reduce SIMULATED_EVENT_DELAY to 100ms to avoid timeouts on real-time targets. - Use macro for simulated event wait times. Signed-off-by: Georges Oates_Larsen --- include/zephyr/net/conn_mgr_connectivity.h | 4 +- subsys/net/conn_mgr/conn_mgr_private.h | 2 +- tests/net/conn_mgr_conn/src/main.c | 42 +++++++++++++++----- tests/net/conn_mgr_conn/src/test_conn_impl.c | 2 +- tests/net/conn_mgr_conn/src/test_conn_impl.h | 4 +- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/zephyr/net/conn_mgr_connectivity.h b/include/zephyr/net/conn_mgr_connectivity.h index 34b885d863..e877ad97fa 100644 --- a/include/zephyr/net/conn_mgr_connectivity.h +++ b/include/zephyr/net/conn_mgr_connectivity.h @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -34,7 +35,8 @@ extern "C" { /* Connectivity Events */ #define _NET_MGMT_CONN_LAYER NET_MGMT_LAYER(NET_MGMT_LAYER_L2) #define _NET_MGMT_CONN_CODE NET_MGMT_LAYER_CODE(0x207) -#define _NET_MGMT_CONN_BASE (_NET_MGMT_CONN_LAYER | _NET_MGMT_CONN_CODE) +#define _NET_MGMT_CONN_BASE (_NET_MGMT_CONN_LAYER | _NET_MGMT_CONN_CODE | \ + NET_MGMT_EVENT_BIT) #define _NET_MGMT_CONN_IF_EVENT (NET_MGMT_IFACE_BIT | _NET_MGMT_CONN_BASE) enum net_event_ethernet_cmd { diff --git a/subsys/net/conn_mgr/conn_mgr_private.h b/subsys/net/conn_mgr/conn_mgr_private.h index 9e14765382..94e7a0b795 100644 --- a/subsys/net/conn_mgr/conn_mgr_private.h +++ b/subsys/net/conn_mgr/conn_mgr_private.h @@ -27,7 +27,7 @@ /* Configuration flags */ #define CONN_MGR_IF_IGNORED BIT(7) -/* Internal state flags*/ +/* Internal state flags */ #define CONN_MGR_IF_READY BIT(14) /* Event flags */ diff --git a/tests/net/conn_mgr_conn/src/main.c b/tests/net/conn_mgr_conn/src/main.c index dbe1d4cb33..373dcdd042 100644 --- a/tests/net/conn_mgr_conn/src/main.c +++ b/tests/net/conn_mgr_conn/src/main.c @@ -29,7 +29,12 @@ static inline struct test_conn_data *conn_mgr_if_get_data(struct net_if *iface) return binding->ctx; } -static void reset_test_iface(struct net_if *iface) +/** + * @brief Reset the network state of the provided iface. + * + * @param iface - iface to reset. + */ +static void reset_test_iface_networking(struct net_if *iface) { if (net_if_is_admin_up(iface)) { (void)net_if_down(iface); @@ -37,7 +42,15 @@ static void reset_test_iface(struct net_if *iface) /* Some tests can leave the iface in a bad state where it is admin-down but not dormant */ net_if_dormant_on(iface); +} +/** + * @brief Reset testing state for the provided iface. + * + * @param iface - iface to reset. + */ +static void reset_test_iface_state(struct net_if *iface) +{ struct conn_mgr_conn_binding *iface_binding = conn_mgr_if_get_binding(iface); struct test_conn_data *iface_data = conn_mgr_if_get_data(iface); @@ -98,12 +111,22 @@ static void conn_mgr_conn_handler(struct net_mgmt_event_callback *cb, static void conn_mgr_conn_before(void *data) { ARG_UNUSED(data); - reset_test_iface(ifa1); - reset_test_iface(ifa2); - reset_test_iface(ifb); - reset_test_iface(ifni); - reset_test_iface(ifnone); - reset_test_iface(ifnull); + reset_test_iface_networking(ifa1); + reset_test_iface_networking(ifa2); + reset_test_iface_networking(ifb); + reset_test_iface_networking(ifni); + reset_test_iface_networking(ifnone); + reset_test_iface_networking(ifnull); + + /* Allow any triggered events to shake out */ + k_sleep(SIMULATED_EVENT_WAIT_TIME); + + reset_test_iface_state(ifa1); + reset_test_iface_state(ifa2); + reset_test_iface_state(ifb); + reset_test_iface_state(ifni); + reset_test_iface_state(ifnone); + reset_test_iface_state(ifnull); k_mutex_lock(&event_mutex, K_FOREVER); @@ -171,6 +194,7 @@ ZTEST(conn_mgr_conn, test_connect_disconnect) zassert_equal(net_if_up(ifa1), 0, "net_if_up should not fail"); zassert_equal(net_if_up(ifa2), 0, "net_if_up should not fail"); zassert_equal(net_if_up(ifb), 0, "net_if_up should not fail"); + k_sleep(K_MSEC(1)); /* Verify ifaces are still disconnected */ zassert_false(net_if_is_up(ifa1), "Ifaces must be disconnected before test"); @@ -526,7 +550,7 @@ ZTEST(conn_mgr_conn, test_connect_timeout) zassert_false(net_if_is_up(ifa1), "ifa1 should not be up if instructed to time out"); /* Ensure timeout event is fired */ - k_sleep(K_SECONDS(SIMULATED_EVENT_DELAY_SECONDS + 1)); + k_sleep(SIMULATED_EVENT_WAIT_TIME); k_mutex_lock(&event_mutex, K_FOREVER); stats = test_event_stats; @@ -557,7 +581,7 @@ ZTEST(conn_mgr_conn, test_connect_fatal_error) zassert_false(net_if_is_up(ifa1), "ifa1 should not be up if instructed to time out"); /* Ensure fatal_error event is fired */ - k_sleep(K_SECONDS(SIMULATED_EVENT_DELAY_SECONDS + 1)); + k_sleep(SIMULATED_EVENT_WAIT_TIME); k_mutex_lock(&event_mutex, K_FOREVER); stats = test_event_stats; diff --git a/tests/net/conn_mgr_conn/src/test_conn_impl.c b/tests/net/conn_mgr_conn/src/test_conn_impl.c index 8d23951605..c6e4cff6a2 100644 --- a/tests/net/conn_mgr_conn/src/test_conn_impl.c +++ b/tests/net/conn_mgr_conn/src/test_conn_impl.c @@ -57,7 +57,7 @@ static void simulate_event(struct net_if *target, int event) simulated_event = event; simulated_event_iface = target; - k_work_reschedule(&simulate_event_work, K_SECONDS(SIMULATED_EVENT_DELAY_SECONDS)); + k_work_reschedule(&simulate_event_work, SIMULATED_EVENT_DELAY_TIME); k_mutex_unlock(&simulated_event_mutex); } diff --git a/tests/net/conn_mgr_conn/src/test_conn_impl.h b/tests/net/conn_mgr_conn/src/test_conn_impl.h index 6408bb802c..1df7e6d020 100644 --- a/tests/net/conn_mgr_conn/src/test_conn_impl.h +++ b/tests/net/conn_mgr_conn/src/test_conn_impl.h @@ -76,7 +76,9 @@ CONN_MGR_CONN_DECLARE_PUBLIC(TEST_L2_CONN_IMPL_N); #define TEST_L2_CONN_IMPL_NI_CTX_TYPE struct test_conn_data CONN_MGR_CONN_DECLARE_PUBLIC(TEST_L2_CONN_IMPL_NI); -#define SIMULATED_EVENT_DELAY_SECONDS 5 +#define SIMULATED_EVENT_DELAY_MS 100 +#define SIMULATED_EVENT_DELAY_TIME K_MSEC(SIMULATED_EVENT_DELAY_MS) +#define SIMULATED_EVENT_WAIT_TIME K_MSEC(SIMULATED_EVENT_DELAY_MS + 10) #ifdef __cplusplus }