Skip to content

Commit 75dd2c9

Browse files
RomainPelletantcarlescufi
authored andcommitted
net: lwm2m: use path as block context retrieval
Replace block context retrieval using object path instead of token. Update block context structure Fix issue #57165 Signed-off-by: romain pelletant <romain.pelletant@fullfreqs.com>
1 parent e268f8e commit 75dd2c9

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

subsys/net/lib/lwm2m/lwm2m_message_handling.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,20 @@ void lwm2m_clear_block_contexts(void)
106106
(void)memset(block1_contexts, 0, sizeof(block1_contexts));
107107
}
108108

109-
static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_context **ctx)
109+
static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_context **ctx)
110110
{
111111
int i;
112112
int64_t timestamp;
113113

114+
if (!path) {
115+
LOG_ERR("Null block ctx path");
116+
return -EFAULT;
117+
}
118+
114119
*ctx = NULL;
115120
timestamp = k_uptime_get();
116121
for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
117-
if (block1_contexts[i].tkl == 0U) {
122+
if (block1_contexts[i].path.level == 0U) {
118123
*ctx = &block1_contexts[i];
119124
break;
120125
}
@@ -134,8 +139,7 @@ static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_
134139
return -ENOMEM;
135140
}
136141

137-
(*ctx)->tkl = tkl;
138-
memcpy((*ctx)->token, token, tkl);
142+
memcpy(&(*ctx)->path, path, sizeof(struct lwm2m_obj_path));
139143
coap_block_transfer_init(&(*ctx)->ctx, lwm2m_default_block_size(), 0);
140144
(*ctx)->timestamp = timestamp;
141145
(*ctx)->expected = 0;
@@ -145,15 +149,20 @@ static int init_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_
145149
return 0;
146150
}
147151

148-
static int get_block_ctx(const uint8_t *token, uint8_t tkl, struct lwm2m_block_context **ctx)
152+
static int get_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_context **ctx)
149153
{
150154
int i;
151155

156+
if (!path) {
157+
LOG_ERR("Null block ctx path");
158+
return -EFAULT;
159+
}
160+
152161
*ctx = NULL;
153162

154163
for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
155-
if (block1_contexts[i].tkl == tkl &&
156-
memcmp(token, block1_contexts[i].token, tkl) == 0) {
164+
if (memcmp(path, &block1_contexts[i].path,
165+
sizeof(struct lwm2m_obj_path)) == 0) {
157166
*ctx = &block1_contexts[i];
158167
/* refresh timestamp */
159168
(*ctx)->timestamp = k_uptime_get();
@@ -174,7 +183,7 @@ static void free_block_ctx(struct lwm2m_block_context *ctx)
174183
return;
175184
}
176185

177-
ctx->tkl = 0U;
186+
memset(&ctx->path, 0, sizeof(struct lwm2m_obj_path));
178187
}
179188

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

20162025
if (r < 0) {

subsys/net/lib/lwm2m/lwm2m_object.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,8 @@ struct lwm2m_block_context {
436436
struct lwm2m_opaque_context opaque;
437437
int64_t timestamp;
438438
uint32_t expected;
439-
uint8_t token[8];
440-
uint8_t tkl;
441439
bool last_block : 1;
442-
uint8_t level; /* 3/4 (4 = resource instance) */
443-
uint16_t res_id;
444-
uint16_t res_inst_id;
440+
struct lwm2m_obj_path path;
445441
};
446442

447443
struct lwm2m_output_context {

subsys/net/lib/lwm2m/lwm2m_rw_oma_tlv.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ static int write_tlv_resource(struct lwm2m_message *msg, struct oma_tlv *tlv)
885885
int ret;
886886

887887
if (msg->in.block_ctx) {
888-
msg->in.block_ctx->res_id = tlv->id;
888+
msg->in.block_ctx->path.res_id = tlv->id;
889889
}
890890

891891
msg->path.res_id = tlv->id;
@@ -916,7 +916,7 @@ static int lwm2m_multi_resource_tlv_parse(struct lwm2m_message *msg,
916916
int ret;
917917

918918
if (msg->in.block_ctx) {
919-
msg->in.block_ctx->res_id = multi_resource_tlv->id;
919+
msg->in.block_ctx->path.res_id = multi_resource_tlv->id;
920920
}
921921

922922
if (multi_resource_tlv->length == 0U) {
@@ -964,7 +964,7 @@ int do_write_op_tlv(struct lwm2m_message *msg)
964964
* header.
965965
*/
966966
if (msg->in.block_ctx != NULL && msg->in.block_ctx->ctx.current > 0) {
967-
msg->path.res_id = msg->in.block_ctx->res_id;
967+
msg->path.res_id = msg->in.block_ctx->path.res_id;
968968
msg->path.level = 3U;
969969
ret = do_write_op_tlv_item(msg);
970970
if (ret < 0) {

subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,11 @@ int do_write_op_senml_cbor(struct lwm2m_message *msg)
952952
* go directly to the message processing
953953
*/
954954
if (msg->in.block_ctx != NULL && msg->in.block_ctx->ctx.current > 0) {
955-
msg->path.res_id = msg->in.block_ctx->res_id;
956-
msg->path.level = msg->in.block_ctx->level;
955+
msg->path.res_id = msg->in.block_ctx->path.res_id;
956+
msg->path.level = msg->in.block_ctx->path.level;
957957

958958
if (msg->path.level == LWM2M_PATH_LEVEL_RESOURCE_INST) {
959-
msg->path.res_inst_id = msg->in.block_ctx->res_inst_id;
959+
msg->path.res_inst_id = msg->in.block_ctx->path.res_inst_id;
960960
}
961961

962962
return do_write_op_item(msg, NULL);

0 commit comments

Comments
 (0)