Skip to content

Commit d68b547

Browse files
nordicjmkartben
authored andcommitted
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 <jamie.mccrae@nordicsemi.no>
1 parent 2befe3a commit d68b547

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

subsys/mgmt/mcumgr/transport/src/smp_udp.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ BUILD_ASSERT(0, "Either IPv4 or IPv6 SMP must be enabled for the MCUmgr UDP SMP
4141
BUILD_ASSERT(sizeof(struct sockaddr) <= CONFIG_MCUMGR_TRANSPORT_NETBUF_USER_DATA_SIZE,
4242
"CONFIG_MCUMGR_TRANSPORT_NETBUF_USER_DATA_SIZE must be >= sizeof(struct sockaddr)");
4343

44-
/* FIXME: dangerous logic, use a kernel API for this */
45-
#define IS_THREAD_RUNNING(thread) \
46-
(thread.base.thread_state & (_THREAD_PENDING | \
47-
_THREAD_SUSPENDED | \
48-
_THREAD_QUEUED) ? true : false)
49-
5044
enum proto_type {
5145
PROTOCOL_IPV4 = 0,
5246
PROTOCOL_IPV6,
@@ -77,6 +71,8 @@ struct configs {
7771
#endif
7872
};
7973

74+
static bool threads_created;
75+
8076
static struct configs smp_udp_configs;
8177

8278
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)
276272
if (net_if_is_up(iface)) {
277273
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
278274
if (net_if_flag_is_set(iface, NET_IF_IPV4) &&
279-
IS_THREAD_RUNNING(smp_udp_configs.ipv4.thread)) {
275+
k_thread_join(&smp_udp_configs.ipv4.thread, K_NO_WAIT) == -EBUSY) {
280276
k_sem_give(&smp_udp_configs.ipv4.network_ready_sem);
281277
}
282278
#endif
283279

284280
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6
285281
if (net_if_flag_is_set(iface, NET_IF_IPV6) &&
286-
IS_THREAD_RUNNING(smp_udp_configs.ipv6.thread)) {
282+
k_thread_join(&smp_udp_configs.ipv6.thread, K_NO_WAIT) == -EBUSY) {
287283
k_sem_give(&smp_udp_configs.ipv6.network_ready_sem);
288284
}
289285
#endif
@@ -316,7 +312,8 @@ int smp_udp_open(void)
316312
bool started = false;
317313

318314
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
319-
if (!IS_THREAD_RUNNING(smp_udp_configs.ipv4.thread)) {
315+
if (k_thread_join(&smp_udp_configs.ipv4.thread, K_NO_WAIT) == 0 ||
316+
threads_created == false) {
320317
(void)k_sem_reset(&smp_udp_configs.ipv4.network_ready_sem);
321318
create_thread(&smp_udp_configs.ipv4, "smp_udp4");
322319
started = true;
@@ -326,7 +323,8 @@ int smp_udp_open(void)
326323
#endif
327324

328325
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6
329-
if (!IS_THREAD_RUNNING(smp_udp_configs.ipv6.thread)) {
326+
if (k_thread_join(&smp_udp_configs.ipv6.thread, K_NO_WAIT) == 0 ||
327+
threads_created == false) {
330328
(void)k_sem_reset(&smp_udp_configs.ipv6.network_ready_sem);
331329
create_thread(&smp_udp_configs.ipv6, "smp_udp6");
332330
started = true;
@@ -337,6 +335,7 @@ int smp_udp_open(void)
337335

338336
if (started) {
339337
/* One or more threads were started, check existing interfaces */
338+
threads_created = true;
340339
net_if_foreach(smp_udp_open_iface, NULL);
341340
}
342341

@@ -346,7 +345,7 @@ int smp_udp_open(void)
346345
int smp_udp_close(void)
347346
{
348347
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
349-
if (IS_THREAD_RUNNING(smp_udp_configs.ipv4.thread)) {
348+
if (k_thread_join(&smp_udp_configs.ipv4.thread, K_NO_WAIT) == -EBUSY) {
350349
k_thread_abort(&(smp_udp_configs.ipv4.thread));
351350

352351
if (smp_udp_configs.ipv4.sock >= 0) {
@@ -359,7 +358,7 @@ int smp_udp_close(void)
359358
#endif
360359

361360
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6
362-
if (IS_THREAD_RUNNING(smp_udp_configs.ipv6.thread)) {
361+
if (k_thread_join(&smp_udp_configs.ipv6.thread, K_NO_WAIT) == -EBUSY) {
363362
k_thread_abort(&(smp_udp_configs.ipv6.thread));
364363

365364
if (smp_udp_configs.ipv6.sock >= 0) {
@@ -378,6 +377,8 @@ static void smp_udp_start(void)
378377
{
379378
int rc;
380379

380+
threads_created = false;
381+
381382
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
382383
smp_udp_configs.ipv4.proto = PROTOCOL_IPV4;
383384
smp_udp_configs.ipv4.sock = -1;

0 commit comments

Comments
 (0)