net: lwm2m: add ZoAP pendings/replies to lwm2m_ctx

This allows use to associate easily the replies / pending operations
with a specific network connection.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
This commit is contained in:
Michael Scott 2017-08-24 08:51:25 -07:00 committed by Jukka Rissanen
commit 148b264255
5 changed files with 52 additions and 62 deletions

View file

@ -8,6 +8,7 @@
#define __LWM2M_H__ #define __LWM2M_H__
#include <net/net_context.h> #include <net/net_context.h>
#include <net/zoap.h>
/* LWM2M Objects defined by OMA */ /* LWM2M Objects defined by OMA */
@ -35,6 +36,8 @@ struct lwm2m_ctx {
struct net_context *net_ctx; struct net_context *net_ctx;
/** Private ZoAP and networking structures */ /** Private ZoAP and networking structures */
struct zoap_pending pendings[CONFIG_LWM2M_ENGINE_MAX_PENDING];
struct zoap_reply replies[CONFIG_LWM2M_ENGINE_MAX_REPLIES];
struct k_delayed_work retransmit_work; struct k_delayed_work retransmit_work;
}; };

View file

@ -94,12 +94,6 @@ struct observe_node {
u8_t tkl; u8_t tkl;
}; };
#define NUM_PENDINGS CONFIG_LWM2M_ENGINE_MAX_PENDING
#define NUM_REPLIES CONFIG_LWM2M_ENGINE_MAX_REPLIES
struct zoap_pending pendings[NUM_PENDINGS];
struct zoap_reply replies[NUM_REPLIES];
static struct observe_node observe_node_data[CONFIG_LWM2M_ENGINE_MAX_OBSERVER]; static struct observe_node observe_node_data[CONFIG_LWM2M_ENGINE_MAX_OBSERVER];
static sys_slist_t engine_obj_list; static sys_slist_t engine_obj_list;
@ -629,15 +623,15 @@ int lwm2m_init_message(struct net_context *net_ctx, struct zoap_packet *zpkt,
return 0; return 0;
} }
struct zoap_pending *lwm2m_init_message_pending(struct zoap_packet *zpkt, struct zoap_pending *lwm2m_init_message_pending(struct lwm2m_ctx *client_ctx,
struct sockaddr *addr, struct zoap_packet *zpkt,
struct zoap_pending *zpendings, struct sockaddr *addr)
int num_zpendings)
{ {
struct zoap_pending *pending = NULL; struct zoap_pending *pending = NULL;
int ret; int ret;
pending = zoap_pending_next_unused(zpendings, num_zpendings); pending = zoap_pending_next_unused(client_ctx->pendings,
CONFIG_LWM2M_ENGINE_MAX_PENDING);
if (!pending) { if (!pending) {
SYS_LOG_ERR("Unable to find a free pending to track " SYS_LOG_ERR("Unable to find a free pending to track "
"retransmissions."); "retransmissions.");
@ -2242,9 +2236,8 @@ int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr)
NULL, K_NO_WAIT, NULL, NULL); NULL, K_NO_WAIT, NULL, NULL);
} }
void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt, void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx,
struct zoap_pending *zpendings, int num_zpendings, struct net_context *net_ctx, struct net_pkt *pkt,
struct zoap_reply *zreplies, int num_zreplies,
bool handle_separate_response, bool handle_separate_response,
int (*udp_request_handler)(struct zoap_packet *, int (*udp_request_handler)(struct zoap_packet *,
struct zoap_packet *, struct zoap_packet *,
@ -2300,7 +2293,8 @@ void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt,
} }
token = zoap_header_get_token(&response, &tkl); token = zoap_header_get_token(&response, &tkl);
pending = zoap_pending_received(&response, zpendings, num_zpendings); pending = zoap_pending_received(&response, client_ctx->pendings,
CONFIG_LWM2M_ENGINE_MAX_PENDING);
if (pending) { if (pending) {
/* TODO: If necessary cancel retransmissions */ /* TODO: If necessary cancel retransmissions */
} }
@ -2308,7 +2302,8 @@ void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt,
SYS_LOG_DBG("checking for reply from [%s]", SYS_LOG_DBG("checking for reply from [%s]",
lwm2m_sprint_ip_addr(&from_addr)); lwm2m_sprint_ip_addr(&from_addr));
reply = zoap_response_received(&response, &from_addr, reply = zoap_response_received(&response, &from_addr,
zreplies, num_zreplies); client_ctx->replies,
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
if (!reply) { if (!reply) {
/* /*
* If no normal response handler is found, then this is * If no normal response handler is found, then this is
@ -2318,7 +2313,7 @@ void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt,
if (udp_request_handler && if (udp_request_handler &&
zoap_header_get_type(&response) == ZOAP_TYPE_CON) { zoap_header_get_type(&response) == ZOAP_TYPE_CON) {
/* Create a response packet if we reach this point */ /* Create a response packet if we reach this point */
r = lwm2m_init_message(ctx, &response2, &pkt2, r = lwm2m_init_message(net_ctx, &response2, &pkt2,
ZOAP_TYPE_ACK, ZOAP_TYPE_ACK,
zoap_header_get_code(&response), zoap_header_get_code(&response),
zoap_header_get_id(&response), zoap_header_get_id(&response),
@ -2373,11 +2368,14 @@ cleanup:
} }
} }
static void udp_receive(struct net_context *ctx, struct net_pkt *pkt, static void udp_receive(struct net_context *net_ctx, struct net_pkt *pkt,
int status, void *user_data) int status, void *user_data)
{ {
lwm2m_udp_receive(ctx, pkt, pendings, NUM_PENDINGS, struct lwm2m_ctx *client_ctx = CONTAINER_OF(net_ctx,
replies, NUM_REPLIES, false, handle_request); struct lwm2m_ctx,
net_ctx);
lwm2m_udp_receive(client_ctx, net_ctx, pkt, false, handle_request);
} }
static void retransmit_request(struct k_work *work) static void retransmit_request(struct k_work *work)
@ -2387,7 +2385,8 @@ static void retransmit_request(struct k_work *work)
int r; int r;
client_ctx = CONTAINER_OF(work, struct lwm2m_ctx, retransmit_work); client_ctx = CONTAINER_OF(work, struct lwm2m_ctx, retransmit_work);
pending = zoap_pending_next_to_expire(pendings, NUM_PENDINGS); pending = zoap_pending_next_to_expire(client_ctx->pendings,
CONFIG_LWM2M_ENGINE_MAX_PENDING);
if (!pending) { if (!pending) {
return; return;
} }
@ -2525,15 +2524,15 @@ static int generate_notify_message(struct observe_node *obs,
goto cleanup; goto cleanup;
} }
pending = lwm2m_init_message_pending(out.out_zpkt, pending = lwm2m_init_message_pending(client_ctx, out.out_zpkt,
&obs->addr, &obs->addr);
pendings, NUM_PENDINGS);
if (!pending) { if (!pending) {
ret = -ENOMEM; ret = -ENOMEM;
goto cleanup; goto cleanup;
} }
reply = zoap_reply_next_unused(replies, NUM_REPLIES); reply = zoap_reply_next_unused(client_ctx->replies,
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
if (!reply) { if (!reply) {
SYS_LOG_ERR("No resources for waiting for replies."); SYS_LOG_ERR("No resources for waiting for replies.");
ret = -ENOMEM; ret = -ENOMEM;

View file

@ -51,10 +51,9 @@ int lwm2m_get_or_create_engine_obj(struct lwm2m_engine_context *context,
int lwm2m_init_message(struct net_context *net_ctx, struct zoap_packet *zpkt, int lwm2m_init_message(struct net_context *net_ctx, struct zoap_packet *zpkt,
struct net_pkt **pkt, u8_t type, u8_t code, u16_t mid, struct net_pkt **pkt, u8_t type, u8_t code, u16_t mid,
const u8_t *token, u8_t tkl); const u8_t *token, u8_t tkl);
struct zoap_pending *lwm2m_init_message_pending(struct zoap_packet *zpkt, struct zoap_pending *lwm2m_init_message_pending(struct lwm2m_ctx *client_ctx,
struct sockaddr *addr, struct zoap_packet *zpkt,
struct zoap_pending *zpendings, struct sockaddr *addr);
int num_zpendings);
void lwm2m_init_message_cleanup(struct net_pkt *pkt, void lwm2m_init_message_cleanup(struct net_pkt *pkt,
struct zoap_pending *pending, struct zoap_pending *pending,
struct zoap_reply *reply); struct zoap_reply *reply);
@ -67,9 +66,8 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
struct lwm2m_engine_context *context); struct lwm2m_engine_context *context);
int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr); int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr);
void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt, void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx,
struct zoap_pending *zpendings, int num_zpendings, struct net_context *ctx, struct net_pkt *pkt,
struct zoap_reply *zreplies, int num_zreplies,
bool handle_separate_response, bool handle_separate_response,
int (*udp_request_handler)(struct zoap_packet *request, int (*udp_request_handler)(struct zoap_packet *request,
struct zoap_packet *response, struct zoap_packet *response,

View file

@ -34,19 +34,13 @@ static struct k_work firmware_work;
static char firmware_uri[PACKAGE_URI_LEN]; static char firmware_uri[PACKAGE_URI_LEN];
static struct sockaddr firmware_addr; static struct sockaddr firmware_addr;
static struct lwm2m_ctx firmware_ctx; static struct lwm2m_ctx firmware_ctx;
#define NUM_PENDINGS CONFIG_LWM2M_ENGINE_MAX_PENDING
#define NUM_REPLIES CONFIG_LWM2M_ENGINE_MAX_REPLIES
static struct zoap_pending pendings[NUM_PENDINGS];
static struct zoap_reply replies[NUM_REPLIES];
static struct zoap_block_context firmware_block_ctx; static struct zoap_block_context firmware_block_ctx;
static void static void
firmware_udp_receive(struct net_context *ctx, struct net_pkt *pkt, int status, firmware_udp_receive(struct net_context *ctx, struct net_pkt *pkt, int status,
void *user_data) void *user_data)
{ {
lwm2m_udp_receive(ctx, pkt, pendings, NUM_PENDINGS, lwm2m_udp_receive(&firmware_ctx, ctx, pkt, true, NULL);
replies, NUM_REPLIES, true, NULL);
} }
static void retransmit_request(struct k_work *work) static void retransmit_request(struct k_work *work)
@ -54,7 +48,8 @@ static void retransmit_request(struct k_work *work)
struct zoap_pending *pending; struct zoap_pending *pending;
int r; int r;
pending = zoap_pending_next_to_expire(pendings, NUM_PENDINGS); pending = zoap_pending_next_to_expire(firmware_ctx.pendings,
CONFIG_LWM2M_ENGINE_MAX_PENDING);
if (!pending) { if (!pending) {
return; return;
} }
@ -105,8 +100,8 @@ static int transfer_request(struct zoap_block_context *ctx,
goto cleanup; goto cleanup;
} }
pending = lwm2m_init_message_pending(&request, &firmware_addr, pending = lwm2m_init_message_pending(&firmware_ctx, &request,
pendings, NUM_PENDINGS); &firmware_addr);
if (!pending) { if (!pending) {
ret = -ENOMEM; ret = -ENOMEM;
goto cleanup; goto cleanup;
@ -114,7 +109,8 @@ static int transfer_request(struct zoap_block_context *ctx,
/* set the reply handler */ /* set the reply handler */
if (reply_cb) { if (reply_cb) {
reply = zoap_reply_next_unused(replies, NUM_REPLIES); reply = zoap_reply_next_unused(firmware_ctx.replies,
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
if (!reply) { if (!reply) {
SYS_LOG_ERR("No resources for waiting for replies."); SYS_LOG_ERR("No resources for waiting for replies.");
ret = -ENOMEM; ret = -ENOMEM;

View file

@ -115,13 +115,6 @@ static K_THREAD_STACK_DEFINE(lwm2m_rd_client_thread_stack,
CONFIG_LWM2M_RD_CLIENT_STACK_SIZE); CONFIG_LWM2M_RD_CLIENT_STACK_SIZE);
struct k_thread lwm2m_rd_client_thread_data; struct k_thread lwm2m_rd_client_thread_data;
/* HACK: remove when engine transactions are ready */
#define NUM_PENDINGS CONFIG_LWM2M_ENGINE_MAX_PENDING
#define NUM_REPLIES CONFIG_LWM2M_ENGINE_MAX_REPLIES
extern struct zoap_pending pendings[NUM_PENDINGS];
extern struct zoap_reply replies[NUM_REPLIES];
/* buffers */ /* buffers */
static char query_buffer[64]; /* allocate some data for queries and updates */ static char query_buffer[64]; /* allocate some data for queries and updates */
static u8_t client_data[256]; /* allocate some data for the RD */ static u8_t client_data[256]; /* allocate some data for the RD */
@ -416,15 +409,16 @@ static int sm_do_bootstrap(int index)
zoap_add_option(&request, ZOAP_OPTION_URI_QUERY, zoap_add_option(&request, ZOAP_OPTION_URI_QUERY,
query_buffer, strlen(query_buffer)); query_buffer, strlen(query_buffer));
pending = lwm2m_init_message_pending(&request, pending = lwm2m_init_message_pending(clients[index].ctx,
&clients[index].bs_server, &request,
pendings, NUM_PENDINGS); &clients[index].bs_server);
if (!pending) { if (!pending) {
ret = -ENOMEM; ret = -ENOMEM;
goto cleanup; goto cleanup;
} }
reply = zoap_reply_next_unused(replies, NUM_REPLIES); reply = zoap_reply_next_unused(clients[index].ctx->replies,
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
if (!reply) { if (!reply) {
SYS_LOG_ERR("No resources for waiting for replies."); SYS_LOG_ERR("No resources for waiting for replies.");
ret = -ENOMEM; ret = -ENOMEM;
@ -569,15 +563,15 @@ static int sm_send_registration(int index, bool send_obj_support_data,
} }
} }
pending = lwm2m_init_message_pending(&request, pending = lwm2m_init_message_pending(clients[index].ctx, &request,
&clients[index].reg_server, &clients[index].reg_server);
pendings, NUM_PENDINGS);
if (!pending) { if (!pending) {
ret = -ENOMEM; ret = -ENOMEM;
goto cleanup; goto cleanup;
} }
reply = zoap_reply_next_unused(replies, NUM_REPLIES); reply = zoap_reply_next_unused(clients[index].ctx->replies,
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
if (!reply) { if (!reply) {
SYS_LOG_ERR("No resources for waiting for replies."); SYS_LOG_ERR("No resources for waiting for replies.");
ret = -ENOMEM; ret = -ENOMEM;
@ -670,15 +664,15 @@ static int sm_do_deregister(int index)
clients[index].server_ep, clients[index].server_ep,
strlen(clients[index].server_ep)); strlen(clients[index].server_ep));
pending = lwm2m_init_message_pending(&request, pending = lwm2m_init_message_pending(clients[index].ctx, &request,
&clients[index].reg_server, &clients[index].reg_server);
pendings, NUM_PENDINGS);
if (!pending) { if (!pending) {
ret = -ENOMEM; ret = -ENOMEM;
goto cleanup; goto cleanup;
} }
reply = zoap_reply_next_unused(replies, NUM_REPLIES); reply = zoap_reply_next_unused(clients[index].ctx->replies,
CONFIG_LWM2M_ENGINE_MAX_REPLIES);
if (!reply) { if (!reply) {
SYS_LOG_ERR("No resources for waiting for replies."); SYS_LOG_ERR("No resources for waiting for replies.");
ret = -ENOMEM; ret = -ENOMEM;