Skip to content

Commit

Permalink
mgmt: mcumgr: transport: smp_udp: Use k_thread_join
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nordicjm committed Nov 28, 2024
1 parent 7271000 commit 1dc12d2
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions subsys/mgmt/mcumgr/transport/src/smp_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 1dc12d2

Please sign in to comment.