Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mgmt: mcumgr: transport: smp_udp: Use k_thread_join #82212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading