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 <mike@foundries.io>
This commit is contained in:
parent
3bfb7debb3
commit
a433af6e05
4 changed files with 42 additions and 52 deletions
|
@ -32,6 +32,7 @@
|
|||
*
|
||||
* @details Context structure for the LwM2M high-level API.
|
||||
*
|
||||
* @param remote_addr Stored remote IP address of the LwM2M client
|
||||
* @param net_app_ctx Related network application context.
|
||||
* @param net_init_timeout Used if the net_app API needs to do some time
|
||||
* consuming operation, like resolving DNS address.
|
||||
|
@ -39,6 +40,9 @@
|
|||
* giving up.
|
||||
*/
|
||||
struct lwm2m_ctx {
|
||||
/** destination address storage */
|
||||
struct sockaddr remote_addr;
|
||||
|
||||
/** Net app context structure */
|
||||
struct net_app_ctx net_app_ctx;
|
||||
s32_t net_init_timeout;
|
||||
|
|
|
@ -414,7 +414,6 @@ static int engine_add_observer(struct lwm2m_message *msg,
|
|||
struct lwm2m_engine_obj_field *obj_field = NULL;
|
||||
struct lwm2m_engine_obj_inst *obj_inst = NULL;
|
||||
struct observe_node *obs;
|
||||
struct sockaddr *addr;
|
||||
struct notification_attrs attrs = {
|
||||
.flags = BIT(LWM2M_ATTR_PMIN) || BIT(LWM2M_ATTR_PMAX),
|
||||
.pmin = DEFAULT_SERVER_PMIN,
|
||||
|
@ -433,9 +432,6 @@ static int engine_add_observer(struct lwm2m_message *msg,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* remote addr */
|
||||
addr = &msg->ctx->net_app_ctx.default_ctx->remote;
|
||||
|
||||
/* TODO: get server object for default pmin/pmax
|
||||
* and observe dup checking
|
||||
*/
|
||||
|
@ -452,7 +448,7 @@ static int engine_add_observer(struct lwm2m_message *msg,
|
|||
LOG_DBG("OBSERVER DUPLICATE %u/%u/%u(%u) [%s]",
|
||||
msg->path.obj_id, msg->path.obj_inst_id,
|
||||
msg->path.res_id, msg->path.level,
|
||||
lwm2m_sprint_ip_addr(addr));
|
||||
lwm2m_sprint_ip_addr(&msg->ctx->remote_addr));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -552,7 +548,8 @@ static int engine_add_observer(struct lwm2m_message *msg,
|
|||
LOG_DBG("OBSERVER ADDED %u/%u/%u(%u) token:'%s' addr:%s",
|
||||
msg->path.obj_id, msg->path.obj_inst_id,
|
||||
msg->path.res_id, msg->path.level,
|
||||
sprint_token(token, tkl), lwm2m_sprint_ip_addr(addr));
|
||||
sprint_token(token, tkl),
|
||||
lwm2m_sprint_ip_addr(&msg->ctx->remote_addr));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -999,8 +996,7 @@ int lwm2m_init_message(struct lwm2m_message *msg)
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
r = coap_pending_init(msg->pending, &msg->cpkt,
|
||||
&msg->ctx->net_app_ctx.default_ctx->remote);
|
||||
r = coap_pending_init(msg->pending, &msg->cpkt, &msg->ctx->remote_addr);
|
||||
if (r < 0) {
|
||||
LOG_ERR("Unable to initialize a pending "
|
||||
"retransmission (err:%d).", r);
|
||||
|
@ -1062,7 +1058,7 @@ int lwm2m_send_message(struct lwm2m_message *msg)
|
|||
}
|
||||
|
||||
ret = net_app_send_pkt(&msg->ctx->net_app_ctx, pkt,
|
||||
&msg->ctx->net_app_ctx.default_ctx->remote,
|
||||
&msg->ctx->remote_addr,
|
||||
NET_SOCKADDR_MAX_SIZE, K_NO_WAIT, NULL);
|
||||
if (ret < 0) {
|
||||
if (msg->type == COAP_TYPE_CON) {
|
||||
|
@ -3626,7 +3622,7 @@ static void retransmit_request(struct k_work *work)
|
|||
* directly here.
|
||||
*/
|
||||
r = net_app_send_pkt(&msg->ctx->net_app_ctx, pkt,
|
||||
&msg->ctx->net_app_ctx.default_ctx->remote,
|
||||
&msg->ctx->remote_addr,
|
||||
NET_SOCKADDR_MAX_SIZE, K_NO_WAIT, NULL);
|
||||
if (r < 0) {
|
||||
LOG_ERR("Error sending lwm2m message: %d", r);
|
||||
|
@ -3703,8 +3699,7 @@ static int generate_notify_message(struct observe_node *obs,
|
|||
obs->path.res_id,
|
||||
obs->path.level,
|
||||
sprint_token(obs->token, obs->tkl),
|
||||
lwm2m_sprint_ip_addr(
|
||||
&obs->ctx->net_app_ctx.default_ctx->remote),
|
||||
lwm2m_sprint_ip_addr(&obs->ctx->remote_addr),
|
||||
k_uptime_get());
|
||||
|
||||
obj_inst = get_engine_obj_inst(obs->path.obj_id,
|
||||
|
@ -3964,6 +3959,20 @@ int lwm2m_engine_start(struct lwm2m_ctx *client_ctx,
|
|||
goto error_start;
|
||||
}
|
||||
|
||||
/* save remote addr */
|
||||
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
|
||||
if (client_ctx->net_app_ctx.dtls.ctx) {
|
||||
memcpy(&client_ctx->remote_addr,
|
||||
&client_ctx->net_app_ctx.dtls.ctx->remote,
|
||||
sizeof(client_ctx->remote_addr));
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
memcpy(&client_ctx->remote_addr,
|
||||
&client_ctx->net_app_ctx.default_ctx->remote,
|
||||
sizeof(client_ctx->remote_addr));
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_start:
|
||||
|
|
|
@ -14,11 +14,9 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
|||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <net/net_app.h>
|
||||
#include <net/net_core.h>
|
||||
|
||||
#include <net/http_parser.h>
|
||||
#include <net/net_pkt.h>
|
||||
#include <net/udp.h>
|
||||
#include <net/net_app.h>
|
||||
|
||||
#include "lwm2m_object.h"
|
||||
#include "lwm2m_engine.h"
|
||||
|
@ -520,6 +518,19 @@ static void firmware_transfer(struct k_work *work)
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
/* save remote addr */
|
||||
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
|
||||
if (firmware_ctx.net_app_ctx.dtls.ctx) {
|
||||
memcpy(&firmware_ctx.remote_addr,
|
||||
&firmware_ctx.net_app_ctx.dtls.ctx->remote,
|
||||
sizeof(firmware_ctx.remote_addr));
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
memcpy(&firmware_ctx.remote_addr,
|
||||
&firmware_ctx.net_app_ctx.default_ctx->remote,
|
||||
sizeof(firmware_ctx.remote_addr));
|
||||
}
|
||||
/* reset block transfer context */
|
||||
coap_block_transfer_init(&firmware_block_ctx,
|
||||
lwm2m_default_block_size(), 0);
|
||||
|
|
|
@ -55,8 +55,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
|||
#include <errno.h>
|
||||
#include <init.h>
|
||||
#include <misc/printk.h>
|
||||
#include <net/net_pkt.h>
|
||||
#include <net/lwm2m.h>
|
||||
|
||||
#include "lwm2m_object.h"
|
||||
#include "lwm2m_engine.h"
|
||||
|
@ -395,14 +393,11 @@ static int sm_do_init(void)
|
|||
static int sm_do_bootstrap(void)
|
||||
{
|
||||
struct lwm2m_message *msg;
|
||||
struct net_app_ctx *app_ctx = NULL;
|
||||
int ret;
|
||||
struct sockaddr *remote = NULL;
|
||||
|
||||
if (client.use_bootstrap &&
|
||||
client.bootstrapped == 0 &&
|
||||
client.has_bs_server_info) {
|
||||
app_ctx = &client.ctx->net_app_ctx;
|
||||
msg = lwm2m_get_message(client.ctx);
|
||||
if (!msg) {
|
||||
LOG_ERR("Unable to get a lwm2m message!");
|
||||
|
@ -431,18 +426,9 @@ static int sm_do_bootstrap(void)
|
|||
query_buffer, strlen(query_buffer));
|
||||
|
||||
/* log the bootstrap attempt */
|
||||
#if defined(CONFIG_NET_APP_DTLS)
|
||||
if (app_ctx->dtls.ctx) {
|
||||
remote = &app_ctx->dtls.ctx->remote;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!remote) {
|
||||
remote = &app_ctx->default_ctx->remote;
|
||||
}
|
||||
|
||||
LOG_DBG("Register ID with bootstrap server [%s] as '%s'",
|
||||
lwm2m_sprint_ip_addr(remote),
|
||||
lwm2m_sprint_ip_addr(&client.ctx->remote_addr),
|
||||
query_buffer);
|
||||
|
||||
ret = lwm2m_send_message(msg);
|
||||
|
@ -508,13 +494,10 @@ static int sm_send_registration(bool send_obj_support_data,
|
|||
coap_reply_t reply_cb,
|
||||
lwm2m_message_timeout_cb_t timeout_cb)
|
||||
{
|
||||
struct net_app_ctx *app_ctx = NULL;
|
||||
struct lwm2m_message *msg;
|
||||
u16_t client_data_len;
|
||||
int ret;
|
||||
struct sockaddr *remote = NULL;
|
||||
|
||||
app_ctx = &client.ctx->net_app_ctx;
|
||||
msg = lwm2m_get_message(client.ctx);
|
||||
if (!msg) {
|
||||
LOG_ERR("Unable to get a lwm2m message!");
|
||||
|
@ -593,18 +576,8 @@ static int sm_send_registration(bool send_obj_support_data,
|
|||
}
|
||||
|
||||
/* log the registration attempt */
|
||||
#if defined(CONFIG_NET_APP_DTLS)
|
||||
if (app_ctx->dtls.ctx) {
|
||||
remote = &app_ctx->dtls.ctx->remote;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!remote) {
|
||||
remote = &app_ctx->default_ctx->remote;
|
||||
}
|
||||
|
||||
LOG_DBG("registration sent [%s]",
|
||||
lwm2m_sprint_ip_addr(remote));
|
||||
lwm2m_sprint_ip_addr(&client.ctx->remote_addr));
|
||||
|
||||
return 0;
|
||||
|
||||
|
@ -663,11 +636,9 @@ static int sm_registration_done(void)
|
|||
|
||||
static int sm_do_deregister(void)
|
||||
{
|
||||
struct net_app_ctx *app_ctx = NULL;
|
||||
struct lwm2m_message *msg;
|
||||
int ret;
|
||||
|
||||
app_ctx = &client.ctx->net_app_ctx;
|
||||
msg = lwm2m_get_message(client.ctx);
|
||||
if (!msg) {
|
||||
LOG_ERR("Unable to get a lwm2m message!");
|
||||
|
@ -777,11 +748,6 @@ int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx,
|
|||
return ret;
|
||||
}
|
||||
|
||||
if (!client_ctx->net_app_ctx.default_ctx) {
|
||||
LOG_ERR("Default net_app_ctx not selected!");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* TODO: use server URI data from security */
|
||||
client.ctx = client_ctx;
|
||||
client.event_cb = event_cb;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue