Skip to content

Commit

Permalink
net: lwm2m: use path as block context retrieval
Browse files Browse the repository at this point in the history
Fix issue #57165

Signed-off-by: RomainPelletant p <[email protected]>
  • Loading branch information
RomainPelletant committed Apr 25, 2023
1 parent d291761 commit 597f161
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
29 changes: 19 additions & 10 deletions subsys/net/lib/lwm2m/lwm2m_message_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ void lwm2m_clear_block_contexts(void)
(void)memset(block1_contexts, 0, sizeof(block1_contexts));
}

static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_context **ctx)
static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_context **ctx)
{
int i;
int64_t timestamp;

if (!path) {
LOG_ERR("Null block ctx path");
return -EFAULT;
}

*ctx = NULL;
timestamp = k_uptime_get();
for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
if (block1_contexts[i].tkl == 0U) {
if (block1_contexts[i].path.level == 0U) {
*ctx = &block1_contexts[i];
break;
}
Expand All @@ -134,8 +139,7 @@ static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_
return -ENOMEM;
}

(*ctx)->tkl = tkl;
memcpy((*ctx)->token, token, tkl);
memcpy(&(*ctx)->path, path, sizeof(struct lwm2m_obj_path));
coap_block_transfer_init(&(*ctx)->ctx, lwm2m_default_block_size(), 0);
(*ctx)->timestamp = timestamp;
(*ctx)->expected = 0;
Expand All @@ -145,15 +149,20 @@ static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_
return 0;
}

static int get_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_context **ctx)
static int get_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_context **ctx)
{
int i;

if (!path) {
LOG_ERR("Null block ctx path");
return -EFAULT;
}

*ctx = NULL;

for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
if (block1_contexts[i].tkl == tkl &&
memcmp(token, block1_contexts[i].token, tkl) == 0) {
if (memcmp(path, &lwm2m_block1_context()[i].path,
sizeof(struct lwm2m_obj_path)) == 0) {
*ctx = &block1_contexts[i];
/* refresh timestamp */
(*ctx)->timestamp = k_uptime_get();
Expand All @@ -174,7 +183,7 @@ static void free_block_ctx(struct lwm2m_block_context *ctx)
return;
}

ctx->tkl = 0U;
memset(&ctx->path, 0, sizeof(struct lwm2m_obj_path));
}

void lwm2m_engine_context_close(struct lwm2m_ctx *client_ctx)
Expand Down Expand Up @@ -2008,9 +2017,9 @@ int handle_request(struct coap_packet *request, struct lwm2m_message *msg)
/* Try to retrieve existing block context. If one not exists,
* and we've received first block, allocate new context.
*/
r = get_block_ctx(token, tkl, &block_ctx);
r = get_block_ctx(&msg->path, &block_ctx);
if (r < 0 && block_num == 0) {
r = init_block_ctx(token, tkl, &block_ctx);
r = init_block_ctx(&msg->path, &block_ctx);
}

if (r < 0) {
Expand Down
6 changes: 1 addition & 5 deletions subsys/net/lib/lwm2m/lwm2m_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,8 @@ struct lwm2m_block_context {
struct lwm2m_opaque_context opaque;
int64_t timestamp;
uint32_t expected;
uint8_t token[8];
uint8_t tkl;
bool last_block : 1;
uint8_t level; /* 3/4 (4 = resource instance) */
uint16_t res_id;
uint16_t res_inst_id;
struct lwm2m_obj_path path;
};

struct lwm2m_output_context {
Expand Down
6 changes: 3 additions & 3 deletions subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,11 +952,11 @@ int do_write_op_senml_cbor(struct lwm2m_message *msg)
* go directly to the message processing
*/
if (msg->in.block_ctx != NULL && msg->in.block_ctx->ctx.current > 0) {
msg->path.res_id = msg->in.block_ctx->res_id;
msg->path.level = msg->in.block_ctx->level;
msg->path.res_id = msg->in.block_ctx->path.res_id;
msg->path.level = msg->in.block_ctx->path.level;

if (msg->path.level == LWM2M_PATH_LEVEL_RESOURCE_INST) {
msg->path.res_inst_id = msg->in.block_ctx->res_inst_id;
msg->path.res_inst_id = msg->in.block_ctx->path.res_inst_id;
}

return do_write_op_item(msg, NULL);
Expand Down

0 comments on commit 597f161

Please sign in to comment.