Skip to content

Commit a433af6

Browse files
mike-scottnashif
authored andcommitted
net: lwm2m: save remote address during setup
net_app contexts save the remote address and we use this during observe notifications and pending handling. If we move to another network layer such as sockets, then the remote address becomes harder to reference. Let's save it as a part of the client context. Signed-off-by: Michael Scott <[email protected]>
1 parent 3bfb7de commit a433af6

File tree

4 files changed

+42
-52
lines changed

4 files changed

+42
-52
lines changed

include/net/lwm2m.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@
3232
*
3333
* @details Context structure for the LwM2M high-level API.
3434
*
35+
* @param remote_addr Stored remote IP address of the LwM2M client
3536
* @param net_app_ctx Related network application context.
3637
* @param net_init_timeout Used if the net_app API needs to do some time
3738
* consuming operation, like resolving DNS address.
3839
* @param net_timeout How long to wait for the network connection before
3940
* giving up.
4041
*/
4142
struct lwm2m_ctx {
43+
/** destination address storage */
44+
struct sockaddr remote_addr;
45+
4246
/** Net app context structure */
4347
struct net_app_ctx net_app_ctx;
4448
s32_t net_init_timeout;

subsys/net/lib/lwm2m/lwm2m_engine.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ static int engine_add_observer(struct lwm2m_message *msg,
414414
struct lwm2m_engine_obj_field *obj_field = NULL;
415415
struct lwm2m_engine_obj_inst *obj_inst = NULL;
416416
struct observe_node *obs;
417-
struct sockaddr *addr;
418417
struct notification_attrs attrs = {
419418
.flags = BIT(LWM2M_ATTR_PMIN) || BIT(LWM2M_ATTR_PMAX),
420419
.pmin = DEFAULT_SERVER_PMIN,
@@ -433,9 +432,6 @@ static int engine_add_observer(struct lwm2m_message *msg,
433432
return -EINVAL;
434433
}
435434

436-
/* remote addr */
437-
addr = &msg->ctx->net_app_ctx.default_ctx->remote;
438-
439435
/* TODO: get server object for default pmin/pmax
440436
* and observe dup checking
441437
*/
@@ -452,7 +448,7 @@ static int engine_add_observer(struct lwm2m_message *msg,
452448
LOG_DBG("OBSERVER DUPLICATE %u/%u/%u(%u) [%s]",
453449
msg->path.obj_id, msg->path.obj_inst_id,
454450
msg->path.res_id, msg->path.level,
455-
lwm2m_sprint_ip_addr(addr));
451+
lwm2m_sprint_ip_addr(&msg->ctx->remote_addr));
456452

457453
return 0;
458454
}
@@ -552,7 +548,8 @@ static int engine_add_observer(struct lwm2m_message *msg,
552548
LOG_DBG("OBSERVER ADDED %u/%u/%u(%u) token:'%s' addr:%s",
553549
msg->path.obj_id, msg->path.obj_inst_id,
554550
msg->path.res_id, msg->path.level,
555-
sprint_token(token, tkl), lwm2m_sprint_ip_addr(addr));
551+
sprint_token(token, tkl),
552+
lwm2m_sprint_ip_addr(&msg->ctx->remote_addr));
556553

557554
return 0;
558555
}
@@ -999,8 +996,7 @@ int lwm2m_init_message(struct lwm2m_message *msg)
999996
goto cleanup;
1000997
}
1001998

1002-
r = coap_pending_init(msg->pending, &msg->cpkt,
1003-
&msg->ctx->net_app_ctx.default_ctx->remote);
999+
r = coap_pending_init(msg->pending, &msg->cpkt, &msg->ctx->remote_addr);
10041000
if (r < 0) {
10051001
LOG_ERR("Unable to initialize a pending "
10061002
"retransmission (err:%d).", r);
@@ -1062,7 +1058,7 @@ int lwm2m_send_message(struct lwm2m_message *msg)
10621058
}
10631059

10641060
ret = net_app_send_pkt(&msg->ctx->net_app_ctx, pkt,
1065-
&msg->ctx->net_app_ctx.default_ctx->remote,
1061+
&msg->ctx->remote_addr,
10661062
NET_SOCKADDR_MAX_SIZE, K_NO_WAIT, NULL);
10671063
if (ret < 0) {
10681064
if (msg->type == COAP_TYPE_CON) {
@@ -3626,7 +3622,7 @@ static void retransmit_request(struct k_work *work)
36263622
* directly here.
36273623
*/
36283624
r = net_app_send_pkt(&msg->ctx->net_app_ctx, pkt,
3629-
&msg->ctx->net_app_ctx.default_ctx->remote,
3625+
&msg->ctx->remote_addr,
36303626
NET_SOCKADDR_MAX_SIZE, K_NO_WAIT, NULL);
36313627
if (r < 0) {
36323628
LOG_ERR("Error sending lwm2m message: %d", r);
@@ -3703,8 +3699,7 @@ static int generate_notify_message(struct observe_node *obs,
37033699
obs->path.res_id,
37043700
obs->path.level,
37053701
sprint_token(obs->token, obs->tkl),
3706-
lwm2m_sprint_ip_addr(
3707-
&obs->ctx->net_app_ctx.default_ctx->remote),
3702+
lwm2m_sprint_ip_addr(&obs->ctx->remote_addr),
37083703
k_uptime_get());
37093704

37103705
obj_inst = get_engine_obj_inst(obs->path.obj_id,
@@ -3964,6 +3959,20 @@ int lwm2m_engine_start(struct lwm2m_ctx *client_ctx,
39643959
goto error_start;
39653960
}
39663961

3962+
/* save remote addr */
3963+
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
3964+
if (client_ctx->net_app_ctx.dtls.ctx) {
3965+
memcpy(&client_ctx->remote_addr,
3966+
&client_ctx->net_app_ctx.dtls.ctx->remote,
3967+
sizeof(client_ctx->remote_addr));
3968+
} else
3969+
#endif
3970+
{
3971+
memcpy(&client_ctx->remote_addr,
3972+
&client_ctx->net_app_ctx.default_ctx->remote,
3973+
sizeof(client_ctx->remote_addr));
3974+
}
3975+
39673976
return 0;
39683977

39693978
error_start:

subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
1414
#include <ctype.h>
1515
#include <stdio.h>
1616
#include <string.h>
17-
#include <net/net_app.h>
18-
#include <net/net_core.h>
17+
1918
#include <net/http_parser.h>
20-
#include <net/net_pkt.h>
21-
#include <net/udp.h>
19+
#include <net/net_app.h>
2220

2321
#include "lwm2m_object.h"
2422
#include "lwm2m_engine.h"
@@ -520,6 +518,19 @@ static void firmware_transfer(struct k_work *work)
520518
goto cleanup;
521519
}
522520

521+
/* save remote addr */
522+
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
523+
if (firmware_ctx.net_app_ctx.dtls.ctx) {
524+
memcpy(&firmware_ctx.remote_addr,
525+
&firmware_ctx.net_app_ctx.dtls.ctx->remote,
526+
sizeof(firmware_ctx.remote_addr));
527+
} else
528+
#endif
529+
{
530+
memcpy(&firmware_ctx.remote_addr,
531+
&firmware_ctx.net_app_ctx.default_ctx->remote,
532+
sizeof(firmware_ctx.remote_addr));
533+
}
523534
/* reset block transfer context */
524535
coap_block_transfer_init(&firmware_block_ctx,
525536
lwm2m_default_block_size(), 0);

subsys/net/lib/lwm2m/lwm2m_rd_client.c

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
5555
#include <errno.h>
5656
#include <init.h>
5757
#include <misc/printk.h>
58-
#include <net/net_pkt.h>
59-
#include <net/lwm2m.h>
6058

6159
#include "lwm2m_object.h"
6260
#include "lwm2m_engine.h"
@@ -395,14 +393,11 @@ static int sm_do_init(void)
395393
static int sm_do_bootstrap(void)
396394
{
397395
struct lwm2m_message *msg;
398-
struct net_app_ctx *app_ctx = NULL;
399396
int ret;
400-
struct sockaddr *remote = NULL;
401397

402398
if (client.use_bootstrap &&
403399
client.bootstrapped == 0 &&
404400
client.has_bs_server_info) {
405-
app_ctx = &client.ctx->net_app_ctx;
406401
msg = lwm2m_get_message(client.ctx);
407402
if (!msg) {
408403
LOG_ERR("Unable to get a lwm2m message!");
@@ -431,18 +426,9 @@ static int sm_do_bootstrap(void)
431426
query_buffer, strlen(query_buffer));
432427

433428
/* log the bootstrap attempt */
434-
#if defined(CONFIG_NET_APP_DTLS)
435-
if (app_ctx->dtls.ctx) {
436-
remote = &app_ctx->dtls.ctx->remote;
437-
}
438-
#endif
439-
440-
if (!remote) {
441-
remote = &app_ctx->default_ctx->remote;
442-
}
443429

444430
LOG_DBG("Register ID with bootstrap server [%s] as '%s'",
445-
lwm2m_sprint_ip_addr(remote),
431+
lwm2m_sprint_ip_addr(&client.ctx->remote_addr),
446432
query_buffer);
447433

448434
ret = lwm2m_send_message(msg);
@@ -508,13 +494,10 @@ static int sm_send_registration(bool send_obj_support_data,
508494
coap_reply_t reply_cb,
509495
lwm2m_message_timeout_cb_t timeout_cb)
510496
{
511-
struct net_app_ctx *app_ctx = NULL;
512497
struct lwm2m_message *msg;
513498
u16_t client_data_len;
514499
int ret;
515-
struct sockaddr *remote = NULL;
516500

517-
app_ctx = &client.ctx->net_app_ctx;
518501
msg = lwm2m_get_message(client.ctx);
519502
if (!msg) {
520503
LOG_ERR("Unable to get a lwm2m message!");
@@ -593,18 +576,8 @@ static int sm_send_registration(bool send_obj_support_data,
593576
}
594577

595578
/* log the registration attempt */
596-
#if defined(CONFIG_NET_APP_DTLS)
597-
if (app_ctx->dtls.ctx) {
598-
remote = &app_ctx->dtls.ctx->remote;
599-
}
600-
#endif
601-
602-
if (!remote) {
603-
remote = &app_ctx->default_ctx->remote;
604-
}
605-
606579
LOG_DBG("registration sent [%s]",
607-
lwm2m_sprint_ip_addr(remote));
580+
lwm2m_sprint_ip_addr(&client.ctx->remote_addr));
608581

609582
return 0;
610583

@@ -663,11 +636,9 @@ static int sm_registration_done(void)
663636

664637
static int sm_do_deregister(void)
665638
{
666-
struct net_app_ctx *app_ctx = NULL;
667639
struct lwm2m_message *msg;
668640
int ret;
669641

670-
app_ctx = &client.ctx->net_app_ctx;
671642
msg = lwm2m_get_message(client.ctx);
672643
if (!msg) {
673644
LOG_ERR("Unable to get a lwm2m message!");
@@ -777,11 +748,6 @@ int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx,
777748
return ret;
778749
}
779750

780-
if (!client_ctx->net_app_ctx.default_ctx) {
781-
LOG_ERR("Default net_app_ctx not selected!");
782-
return -EINVAL;
783-
}
784-
785751
/* TODO: use server URI data from security */
786752
client.ctx = client_ctx;
787753
client.event_cb = event_cb;

0 commit comments

Comments
 (0)