@@ -41,12 +41,6 @@ BUILD_ASSERT(0, "Either IPv4 or IPv6 SMP must be enabled for the MCUmgr UDP SMP
41
41
BUILD_ASSERT (sizeof (struct sockaddr ) <= CONFIG_MCUMGR_TRANSPORT_NETBUF_USER_DATA_SIZE ,
42
42
"CONFIG_MCUMGR_TRANSPORT_NETBUF_USER_DATA_SIZE must be >= sizeof(struct sockaddr)" );
43
43
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
-
50
44
enum proto_type {
51
45
PROTOCOL_IPV4 = 0 ,
52
46
PROTOCOL_IPV6 ,
@@ -77,6 +71,8 @@ struct configs {
77
71
#endif
78
72
};
79
73
74
+ static bool threads_created ;
75
+
80
76
static struct configs smp_udp_configs ;
81
77
82
78
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)
276
272
if (net_if_is_up (iface )) {
277
273
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
278
274
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 ) {
280
276
k_sem_give (& smp_udp_configs .ipv4 .network_ready_sem );
281
277
}
282
278
#endif
283
279
284
280
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6
285
281
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 ) {
287
283
k_sem_give (& smp_udp_configs .ipv6 .network_ready_sem );
288
284
}
289
285
#endif
@@ -316,7 +312,8 @@ int smp_udp_open(void)
316
312
bool started = false;
317
313
318
314
#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) {
320
317
(void )k_sem_reset (& smp_udp_configs .ipv4 .network_ready_sem );
321
318
create_thread (& smp_udp_configs .ipv4 , "smp_udp4" );
322
319
started = true;
@@ -326,7 +323,8 @@ int smp_udp_open(void)
326
323
#endif
327
324
328
325
#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) {
330
328
(void )k_sem_reset (& smp_udp_configs .ipv6 .network_ready_sem );
331
329
create_thread (& smp_udp_configs .ipv6 , "smp_udp6" );
332
330
started = true;
@@ -337,6 +335,7 @@ int smp_udp_open(void)
337
335
338
336
if (started ) {
339
337
/* One or more threads were started, check existing interfaces */
338
+ threads_created = true;
340
339
net_if_foreach (smp_udp_open_iface , NULL );
341
340
}
342
341
@@ -346,7 +345,7 @@ int smp_udp_open(void)
346
345
int smp_udp_close (void )
347
346
{
348
347
#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 ) {
350
349
k_thread_abort (& (smp_udp_configs .ipv4 .thread ));
351
350
352
351
if (smp_udp_configs .ipv4 .sock >= 0 ) {
@@ -359,7 +358,7 @@ int smp_udp_close(void)
359
358
#endif
360
359
361
360
#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 ) {
363
362
k_thread_abort (& (smp_udp_configs .ipv6 .thread ));
364
363
365
364
if (smp_udp_configs .ipv6 .sock >= 0 ) {
@@ -378,6 +377,8 @@ static void smp_udp_start(void)
378
377
{
379
378
int rc ;
380
379
380
+ threads_created = false;
381
+
381
382
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
382
383
smp_udp_configs .ipv4 .proto = PROTOCOL_IPV4 ;
383
384
smp_udp_configs .ipv4 .sock = -1 ;
0 commit comments