Skip to content

Commit 6d42ded

Browse files
Juha Heiskanennashif
authored andcommitted
net: lwm2m: LwM2M message allocation update
Allocated own message buffer for RD client interface. This helps to cover if all messages are queued and need to do registration or update. Signed-off-by: Juha Heiskanen <juha.heiskanen@nordicsemi.no> (cherry picked from commit 8dca91109d73a4a697e074c58ee9430d56c01a51)
1 parent ad19606 commit 6d42ded

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

include/zephyr/net/lwm2m.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ struct lwm2m_ctx {
113113
/** Destination address storage */
114114
struct sockaddr remote_addr;
115115

116-
/** Private CoAP and networking structures */
117-
struct coap_pending pendings[CONFIG_LWM2M_ENGINE_MAX_PENDING];
118-
struct coap_reply replies[CONFIG_LWM2M_ENGINE_MAX_REPLIES];
116+
/** Private CoAP and networking structures + 1 is for RD Client own message */
117+
struct coap_pending pendings[CONFIG_LWM2M_ENGINE_MAX_PENDING + 1];
118+
struct coap_reply replies[CONFIG_LWM2M_ENGINE_MAX_REPLIES + 1];
119119
sys_slist_t pending_sends;
120120
#if defined(CONFIG_LWM2M_QUEUE_MODE_ENABLED)
121121
sys_slist_t queued_messages;

subsys/net/lib/lwm2m/lwm2m_engine.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,11 +1292,23 @@ static struct lwm2m_message *find_msg(struct coap_pending *pending,
12921292
struct coap_reply *reply)
12931293
{
12941294
size_t i;
1295+
struct lwm2m_message *msg;
12951296

12961297
if (!pending && !reply) {
12971298
return NULL;
12981299
}
12991300

1301+
msg = lwm2m_get_ongoing_rd_msg();
1302+
if (msg) {
1303+
if (pending != NULL && msg->pending == pending) {
1304+
return msg;
1305+
}
1306+
1307+
if (reply != NULL && msg->reply == reply) {
1308+
return msg;
1309+
}
1310+
}
1311+
13001312
for (i = 0; i < CONFIG_LWM2M_ENGINE_MAX_MESSAGES; i++) {
13011313
if (pending != NULL && messages[i].ctx &&
13021314
messages[i].pending == pending) {
@@ -1326,7 +1338,7 @@ struct lwm2m_message *lwm2m_get_message(struct lwm2m_ctx *client_ctx)
13261338
return NULL;
13271339
}
13281340

1329-
static void lm2m_message_clear_allocations(struct lwm2m_message *msg)
1341+
void lm2m_message_clear_allocations(struct lwm2m_message *msg)
13301342
{
13311343
if (msg->pending) {
13321344
coap_pending_clear(msg->pending);
@@ -1399,7 +1411,7 @@ int lwm2m_init_message(struct lwm2m_message *msg)
13991411

14001412
msg->pending = coap_pending_next_unused(
14011413
msg->ctx->pendings,
1402-
CONFIG_LWM2M_ENGINE_MAX_PENDING);
1414+
ARRAY_SIZE(msg->ctx->pendings));
14031415
if (!msg->pending) {
14041416
LOG_ERR("Unable to find a free pending to track "
14051417
"retransmissions.");
@@ -1418,7 +1430,7 @@ int lwm2m_init_message(struct lwm2m_message *msg)
14181430
if (msg->reply_cb) {
14191431
msg->reply = coap_reply_next_unused(
14201432
msg->ctx->replies,
1421-
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
1433+
ARRAY_SIZE(msg->ctx->replies));
14221434
if (!msg->reply) {
14231435
LOG_ERR("No resources for waiting for replies.");
14241436
r = -ENOMEM;
@@ -5094,7 +5106,7 @@ static int lwm2m_response_promote_to_con(struct lwm2m_message *msg)
50945106
/* Add the packet to the pending list. */
50955107
msg->pending = coap_pending_next_unused(
50965108
msg->ctx->pendings,
5097-
CONFIG_LWM2M_ENGINE_MAX_PENDING);
5109+
ARRAY_SIZE(msg->ctx->pendings));
50985110
if (!msg->pending) {
50995111
LOG_ERR("Unable to find a free pending to track "
51005112
"retransmissions.");
@@ -5133,7 +5145,7 @@ static void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx,
51335145

51345146
tkl = coap_header_get_token(&response, token);
51355147
pending = coap_pending_received(&response, client_ctx->pendings,
5136-
CONFIG_LWM2M_ENGINE_MAX_PENDING);
5148+
ARRAY_SIZE(client_ctx->pendings));
51375149
if (pending && coap_header_get_type(&response) == COAP_TYPE_ACK) {
51385150
msg = find_msg(pending, NULL);
51395151
if (msg == NULL) {
@@ -5165,7 +5177,7 @@ static void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx,
51655177
log_strdup(lwm2m_sprint_ip_addr(from_addr)));
51665178
reply = coap_response_received(&response, from_addr,
51675179
client_ctx->replies,
5168-
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
5180+
ARRAY_SIZE(client_ctx->replies));
51695181
if (reply) {
51705182
msg = find_msg(NULL, reply);
51715183

@@ -5251,7 +5263,7 @@ static int32_t retransmit_request(struct lwm2m_ctx *client_ctx,
52515263
int i;
52525264

52535265
for (i = 0, p = client_ctx->pendings;
5254-
i < CONFIG_LWM2M_ENGINE_MAX_PENDING; i++, p++) {
5266+
i < ARRAY_SIZE(client_ctx->pendings); i++, p++) {
52555267
if (!p->timeout) {
52565268
continue;
52575269
}
@@ -5607,9 +5619,10 @@ int lwm2m_engine_context_close(struct lwm2m_ctx *client_ctx)
56075619
}
56085620

56095621
coap_pendings_clear(client_ctx->pendings,
5610-
CONFIG_LWM2M_ENGINE_MAX_PENDING);
5622+
ARRAY_SIZE(client_ctx->pendings));
56115623
coap_replies_clear(client_ctx->replies,
5612-
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
5624+
ARRAY_SIZE(client_ctx->replies));
5625+
56135626
#if defined(CONFIG_LWM2M_QUEUE_MODE_ENABLED)
56145627
client_ctx->connection_suspended = false;
56155628
client_ctx->buffer_client_messages = true;

subsys/net/lib/lwm2m/lwm2m_engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void lwm2m_engine_clear_duplicate_path(sys_slist_t *lwm2m_path_list, sys_slist_t
122122
/* LwM2M message functions */
123123
struct lwm2m_message *lwm2m_get_message(struct lwm2m_ctx *client_ctx);
124124
void lwm2m_reset_message(struct lwm2m_message *msg, bool release);
125+
void lm2m_message_clear_allocations(struct lwm2m_message *msg);
125126
int lwm2m_init_message(struct lwm2m_message *msg);
126127
int lwm2m_send_message_async(struct lwm2m_message *msg);
127128
/* Notification and Send operation */

subsys/net/lib/lwm2m/lwm2m_rd_client.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ enum sm_engine_state {
100100

101101
struct lwm2m_rd_client_info {
102102
struct k_mutex mutex;
103-
104-
uint32_t lifetime;
103+
struct lwm2m_message rd_message;
105104
struct lwm2m_ctx *ctx;
105+
uint32_t lifetime;
106106
uint8_t engine_state;
107107
uint8_t retries;
108108
uint8_t retry_delay;
@@ -127,6 +127,25 @@ struct lwm2m_rd_client_info {
127127
*/
128128
static char query_buffer[MAX(32, sizeof("ep=") + CLIENT_EP_LEN)];
129129

130+
static struct lwm2m_message *rd_get_message(void)
131+
{
132+
if (client.rd_message.ctx) {
133+
return NULL;
134+
}
135+
136+
client.rd_message.ctx = client.ctx;
137+
return &client.rd_message;
138+
139+
}
140+
141+
struct lwm2m_message *lwm2m_get_ongoing_rd_msg(void)
142+
{
143+
if (!client.ctx || !client.rd_message.ctx) {
144+
return NULL;
145+
}
146+
return &client.rd_message;
147+
}
148+
130149
void engine_update_tx_time(void)
131150
{
132151
client.last_tx = k_uptime_get();
@@ -618,7 +637,7 @@ static int sm_send_bootstrap_registration(void)
618637
struct lwm2m_message *msg;
619638
int ret;
620639

621-
msg = lwm2m_get_message(client.ctx);
640+
msg = rd_get_message();
622641
if (!msg) {
623642
LOG_ERR("Unable to get a lwm2m message!");
624643
return -ENOMEM;
@@ -745,7 +764,7 @@ static int sm_send_registration(bool send_obj_support_data,
745764
char binding[CLIENT_BINDING_LEN];
746765
char queue[CLIENT_QUEUE_LEN];
747766

748-
msg = lwm2m_get_message(client.ctx);
767+
msg = rd_get_message();
749768
if (!msg) {
750769
LOG_ERR("Unable to get a lwm2m message!");
751770
return -ENOMEM;
@@ -1001,7 +1020,7 @@ static int sm_do_deregister(void)
10011020
struct lwm2m_message *msg;
10021021
int ret;
10031022

1004-
msg = lwm2m_get_message(client.ctx);
1023+
msg = rd_get_message();
10051024
if (!msg) {
10061025
LOG_ERR("Unable to get a lwm2m message!");
10071026
ret = -ENOMEM;
@@ -1278,6 +1297,7 @@ bool lwm2m_rd_client_is_registred(struct lwm2m_ctx *client_ctx)
12781297
static int lwm2m_rd_client_init(const struct device *dev)
12791298
{
12801299
client.ctx = NULL;
1300+
client.rd_message.ctx = NULL;
12811301
client.engine_state = ENGINE_IDLE;
12821302
k_mutex_init(&client.mutex);
12831303

subsys/net/lib/lwm2m/lwm2m_rd_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ void engine_bootstrap_finish(void);
5050
int lwm2m_rd_client_connection_resume(struct lwm2m_ctx *client_ctx);
5151
#endif
5252
void engine_update_tx_time(void);
53+
struct lwm2m_message *lwm2m_get_ongoing_rd_msg(void);
5354

5455
#endif /* LWM2M_RD_CLIENT_H */

0 commit comments

Comments
 (0)