From 40ed24c21ba404f401c9a82fd1efcdb10991d239 Mon Sep 17 00:00:00 2001 From: Jamie McCrae Date: Thu, 28 Nov 2024 08:17:34 +0000 Subject: [PATCH] mgmt: mcumgr: transport: smp_udp: Use k_thread_join Replaces code that peeked directly into the thread by instead calling k_thread_join() to check the state of threads Signed-off-by: Jamie McCrae --- subsys/mgmt/mcumgr/transport/src/smp_udp.c | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/subsys/mgmt/mcumgr/transport/src/smp_udp.c b/subsys/mgmt/mcumgr/transport/src/smp_udp.c index 0f44782bdbd1bd..77734889ac86a1 100644 --- a/subsys/mgmt/mcumgr/transport/src/smp_udp.c +++ b/subsys/mgmt/mcumgr/transport/src/smp_udp.c @@ -41,12 +41,6 @@ BUILD_ASSERT(0, "Either IPv4 or IPv6 SMP must be enabled for the MCUmgr UDP SMP BUILD_ASSERT(sizeof(struct sockaddr) <= CONFIG_MCUMGR_TRANSPORT_NETBUF_USER_DATA_SIZE, "CONFIG_MCUMGR_TRANSPORT_NETBUF_USER_DATA_SIZE must be >= sizeof(struct sockaddr)"); -/* FIXME: dangerous logic, use a kernel API for this */ -#define IS_THREAD_RUNNING(thread) \ - (thread.base.thread_state & (_THREAD_PENDING | \ - _THREAD_SUSPENDED | \ - _THREAD_QUEUED) ? true : false) - enum proto_type { PROTOCOL_IPV4 = 0, PROTOCOL_IPV6, @@ -77,6 +71,8 @@ struct configs { #endif }; +static bool threads_created; + static struct configs smp_udp_configs; static struct net_mgmt_event_callback smp_udp_mgmt_cb; @@ -276,14 +272,14 @@ static void smp_udp_open_iface(struct net_if *iface, void *user_data) if (net_if_is_up(iface)) { #ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4 if (net_if_flag_is_set(iface, NET_IF_IPV4) && - IS_THREAD_RUNNING(smp_udp_configs.ipv4.thread)) { + k_thread_join(&smp_udp_configs.ipv4.thread, K_NO_WAIT) == -EBUSY) { k_sem_give(&smp_udp_configs.ipv4.network_ready_sem); } #endif #ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6 if (net_if_flag_is_set(iface, NET_IF_IPV6) && - IS_THREAD_RUNNING(smp_udp_configs.ipv6.thread)) { + k_thread_join(&smp_udp_configs.ipv6.thread, K_NO_WAIT) == -EBUSY) { k_sem_give(&smp_udp_configs.ipv6.network_ready_sem); } #endif @@ -316,7 +312,8 @@ int smp_udp_open(void) bool started = false; #ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4 - if (!IS_THREAD_RUNNING(smp_udp_configs.ipv4.thread)) { + if (k_thread_join(&smp_udp_configs.ipv4.thread, K_NO_WAIT) == 0 || + threads_created == false) { (void)k_sem_reset(&smp_udp_configs.ipv4.network_ready_sem); create_thread(&smp_udp_configs.ipv4, "smp_udp4"); started = true; @@ -326,7 +323,8 @@ int smp_udp_open(void) #endif #ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6 - if (!IS_THREAD_RUNNING(smp_udp_configs.ipv6.thread)) { + if (k_thread_join(&smp_udp_configs.ipv6.thread, K_NO_WAIT) == 0 || + threads_created == false) { (void)k_sem_reset(&smp_udp_configs.ipv6.network_ready_sem); create_thread(&smp_udp_configs.ipv6, "smp_udp6"); started = true; @@ -337,6 +335,7 @@ int smp_udp_open(void) if (started) { /* One or more threads were started, check existing interfaces */ + threads_created = true; net_if_foreach(smp_udp_open_iface, NULL); } @@ -346,7 +345,7 @@ int smp_udp_open(void) int smp_udp_close(void) { #ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4 - if (IS_THREAD_RUNNING(smp_udp_configs.ipv4.thread)) { + if (k_thread_join(&smp_udp_configs.ipv4.thread, K_NO_WAIT) == -EBUSY) { k_thread_abort(&(smp_udp_configs.ipv4.thread)); if (smp_udp_configs.ipv4.sock >= 0) { @@ -359,7 +358,7 @@ int smp_udp_close(void) #endif #ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6 - if (IS_THREAD_RUNNING(smp_udp_configs.ipv6.thread)) { + if (k_thread_join(&smp_udp_configs.ipv6.thread, K_NO_WAIT) == -EBUSY) { k_thread_abort(&(smp_udp_configs.ipv6.thread)); if (smp_udp_configs.ipv6.sock >= 0) { @@ -378,6 +377,8 @@ static void smp_udp_start(void) { int rc; + threads_created = false; + #ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4 smp_udp_configs.ipv4.proto = PROTOCOL_IPV4; smp_udp_configs.ipv4.sock = -1;