Skip to content

Commit

Permalink
net: lwm2m: Synchronized functions mutating the RD client's state
Browse files Browse the repository at this point in the history
A mutex is used to syncrhonize the start, stop and service() functions
of the RD client. Previously it could happen that while service() was
working on e.g. bootstrapping, a stop() call by another thread would
close the socket. Then the bootstrapping process would detect it as a
network error, and restart the process.

Fixes #37170.

Signed-off-by: Maik Vermeulen <maik.vermeulen@innotractor.com>
  • Loading branch information
Maik Vermeulen authored and nashif committed Aug 3, 2021
1 parent 36a3853 commit 7a2a28b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_rd_client.c
Expand Up @@ -98,6 +98,8 @@ enum sm_engine_state {
};

struct lwm2m_rd_client_info {
struct k_mutex mutex;

uint32_t lifetime;
struct lwm2m_ctx *ctx;
uint8_t engine_state;
Expand Down Expand Up @@ -949,6 +951,8 @@ static void sm_do_network_error(void)

static void lwm2m_rd_client_service(struct k_work *work)
{
k_mutex_lock(&client.mutex, K_FOREVER);

if (client.ctx) {
switch (get_sm_state()) {
case ENGINE_IDLE:
Expand Down Expand Up @@ -1018,15 +1022,21 @@ static void lwm2m_rd_client_service(struct k_work *work)

}
}

k_mutex_unlock(&client.mutex);
}

void lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, const char *ep_name,
uint32_t flags, lwm2m_ctx_event_cb_t event_cb)
{
k_mutex_lock(&client.mutex, K_FOREVER);

if (!IS_ENABLED(CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP) &&
(flags & LWM2M_RD_CLIENT_FLAG_BOOTSTRAP)) {
LOG_ERR("Bootstrap support is disabled. Please enable "
"CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP.");

k_mutex_unlock(&client.mutex);
return;
}

Expand All @@ -1040,11 +1050,15 @@ void lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, const char *ep_name,
strncpy(client.ep_name, ep_name, CLIENT_EP_LEN - 1);
client.ep_name[CLIENT_EP_LEN - 1] = '\0';
LOG_INF("Start LWM2M Client: %s", log_strdup(client.ep_name));

k_mutex_unlock(&client.mutex);
}

void lwm2m_rd_client_stop(struct lwm2m_ctx *client_ctx,
lwm2m_ctx_event_cb_t event_cb)
{
k_mutex_lock(&client.mutex, K_FOREVER);

client.ctx = client_ctx;
client.event_cb = event_cb;

Expand All @@ -1058,6 +1072,8 @@ void lwm2m_rd_client_stop(struct lwm2m_ctx *client_ctx,
}

LOG_INF("Stop LWM2M Client: %s", log_strdup(client.ep_name));

k_mutex_unlock(&client.mutex);
}

void lwm2m_rd_client_update(void)
Expand All @@ -1067,6 +1083,8 @@ void lwm2m_rd_client_update(void)

static int lwm2m_rd_client_init(const struct device *dev)
{
k_mutex_init(&client.mutex);

return lwm2m_engine_add_service(lwm2m_rd_client_service,
STATE_MACHINE_UPDATE_INTERVAL_MS);

Expand Down

0 comments on commit 7a2a28b

Please sign in to comment.