diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 383612d7e42c413..90f215e79564bf2 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -1338,7 +1338,9 @@ static int lwm2m_engine_init(void) lwm2m_clear_block_contexts(); #if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER) + lwm2m_engine_lock(); (void)memset(output_block_contexts, 0, sizeof(output_block_contexts)); + lwm2m_engine_unlock(); #endif STRUCT_SECTION_FOREACH(lwm2m_init_func, init) { diff --git a/subsys/net/lib/lwm2m/lwm2m_message_handling.c b/subsys/net/lib/lwm2m/lwm2m_message_handling.c index 74f6570ac4b4576..8cdb6429deede61 100644 --- a/subsys/net/lib/lwm2m/lwm2m_message_handling.c +++ b/subsys/net/lib/lwm2m/lwm2m_message_handling.c @@ -117,7 +117,9 @@ enum coap_block_size lwm2m_default_block_size(void) void lwm2m_clear_block_contexts(void) { + lwm2m_engine_lock(); (void)memset(block1_contexts, 0, sizeof(block1_contexts)); + lwm2m_engine_unlock(); } static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_context **ctx) @@ -130,6 +132,8 @@ static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_ return -EFAULT; } + lwm2m_engine_lock(); + *ctx = NULL; timestamp = k_uptime_get(); for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) { @@ -149,6 +153,7 @@ static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_ } if (*ctx == NULL) { + lwm2m_engine_unlock(); LOG_ERR("Cannot find free block context"); return -ENOMEM; } @@ -160,6 +165,8 @@ static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_ (*ctx)->last_block = false; memset(&(*ctx)->opaque, 0, sizeof((*ctx)->opaque)); + lwm2m_engine_unlock(); + return 0; } @@ -174,6 +181,8 @@ static int get_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_c *ctx = NULL; + lwm2m_engine_lock(); + for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) { if (lwm2m_obj_path_equal(path, &block1_contexts[i].path)) { *ctx = &block1_contexts[i]; @@ -183,6 +192,8 @@ static int get_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_c } } + lwm2m_engine_unlock(); + if (*ctx == NULL) { return -ENOENT; } @@ -196,24 +207,31 @@ static void free_block_ctx(struct lwm2m_block_context *ctx) return; } + lwm2m_engine_lock(); memset(&ctx->path, 0, sizeof(struct lwm2m_obj_path)); + lwm2m_engine_unlock(); } #if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER) STATIC int request_output_block_ctx(struct coap_block_context **ctx) { - *ctx = NULL; + int ret = -ENOMEM; int i; + *ctx = NULL; + + lwm2m_engine_lock(); for (i = 0; i < NUM_OUTPUT_BLOCK_CONTEXT; i++) { if (lwm2m_output_block_context()[i].block_size == 0) { *ctx = &lwm2m_output_block_context()[i]; (*ctx)->block_size = OUTPUT_CONTEXT_IN_USE_MARK; - return 0; + ret = 0; + break; } } + lwm2m_engine_unlock(); - return -ENOMEM; + return ret; } STATIC void release_output_block_ctx(struct coap_block_context **ctx) @@ -224,12 +242,14 @@ STATIC void release_output_block_ctx(struct coap_block_context **ctx) return; } + lwm2m_engine_lock(); for (i = 0; i < NUM_OUTPUT_BLOCK_CONTEXT; i++) { if (&lwm2m_output_block_context()[i] == *ctx) { lwm2m_output_block_context()[i].block_size = 0; *ctx = NULL; } } + lwm2m_engine_unlock(); }