From 148b264255c73e4e0bdd2cbb67fa20eb59acf24b Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Thu, 24 Aug 2017 08:51:25 -0700 Subject: [PATCH] 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 --- include/net/lwm2m.h | 3 ++ subsys/net/lib/lwm2m/lwm2m_engine.c | 49 +++++++++---------- subsys/net/lib/lwm2m/lwm2m_engine.h | 12 ++--- .../net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 18 +++---- subsys/net/lib/lwm2m/lwm2m_rd_client.c | 32 +++++------- 5 files changed, 52 insertions(+), 62 deletions(-) diff --git a/include/net/lwm2m.h b/include/net/lwm2m.h index 69bc2426493..1d82b76b2f7 100644 --- a/include/net/lwm2m.h +++ b/include/net/lwm2m.h @@ -8,6 +8,7 @@ #define __LWM2M_H__ #include +#include /* LWM2M Objects defined by OMA */ @@ -35,6 +36,8 @@ struct lwm2m_ctx { struct net_context *net_ctx; /** 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; }; diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 4a3bd36c93a..9b5f57cfa57 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -94,12 +94,6 @@ struct observe_node { 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 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; } -struct zoap_pending *lwm2m_init_message_pending(struct zoap_packet *zpkt, - struct sockaddr *addr, - struct zoap_pending *zpendings, - int num_zpendings) +struct zoap_pending *lwm2m_init_message_pending(struct lwm2m_ctx *client_ctx, + struct zoap_packet *zpkt, + struct sockaddr *addr) { struct zoap_pending *pending = NULL; 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) { SYS_LOG_ERR("Unable to find a free pending to track " "retransmissions."); @@ -2242,9 +2236,8 @@ int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr) NULL, K_NO_WAIT, NULL, NULL); } -void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt, - struct zoap_pending *zpendings, int num_zpendings, - struct zoap_reply *zreplies, int num_zreplies, +void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, + struct net_context *net_ctx, struct net_pkt *pkt, bool handle_separate_response, int (*udp_request_handler)(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); - pending = zoap_pending_received(&response, zpendings, num_zpendings); + pending = zoap_pending_received(&response, client_ctx->pendings, + CONFIG_LWM2M_ENGINE_MAX_PENDING); if (pending) { /* 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]", lwm2m_sprint_ip_addr(&from_addr)); reply = zoap_response_received(&response, &from_addr, - zreplies, num_zreplies); + client_ctx->replies, + CONFIG_LWM2M_ENGINE_MAX_REPLIES); if (!reply) { /* * 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 && zoap_header_get_type(&response) == ZOAP_TYPE_CON) { /* 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_header_get_code(&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) { - lwm2m_udp_receive(ctx, pkt, pendings, NUM_PENDINGS, - replies, NUM_REPLIES, false, handle_request); + struct lwm2m_ctx *client_ctx = CONTAINER_OF(net_ctx, + struct lwm2m_ctx, + net_ctx); + + lwm2m_udp_receive(client_ctx, net_ctx, pkt, false, handle_request); } static void retransmit_request(struct k_work *work) @@ -2387,7 +2385,8 @@ static void retransmit_request(struct k_work *work) int r; 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) { return; } @@ -2525,15 +2524,15 @@ static int generate_notify_message(struct observe_node *obs, goto cleanup; } - pending = lwm2m_init_message_pending(out.out_zpkt, - &obs->addr, - pendings, NUM_PENDINGS); + pending = lwm2m_init_message_pending(client_ctx, out.out_zpkt, + &obs->addr); if (!pending) { ret = -ENOMEM; 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) { SYS_LOG_ERR("No resources for waiting for replies."); ret = -ENOMEM; diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.h b/subsys/net/lib/lwm2m/lwm2m_engine.h index 37c53d527fb..88188bf5ccf 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.h +++ b/subsys/net/lib/lwm2m/lwm2m_engine.h @@ -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, struct net_pkt **pkt, u8_t type, u8_t code, u16_t mid, const u8_t *token, u8_t tkl); -struct zoap_pending *lwm2m_init_message_pending(struct zoap_packet *zpkt, - struct sockaddr *addr, - struct zoap_pending *zpendings, - int num_zpendings); +struct zoap_pending *lwm2m_init_message_pending(struct lwm2m_ctx *client_ctx, + struct zoap_packet *zpkt, + struct sockaddr *addr); void lwm2m_init_message_cleanup(struct net_pkt *pkt, struct zoap_pending *pending, struct zoap_reply *reply); @@ -67,9 +66,8 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst, struct lwm2m_engine_context *context); 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, - struct zoap_pending *zpendings, int num_zpendings, - struct zoap_reply *zreplies, int num_zreplies, +void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, + struct net_context *ctx, struct net_pkt *pkt, bool handle_separate_response, int (*udp_request_handler)(struct zoap_packet *request, struct zoap_packet *response, diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index 7c7cdd8c1df..43698f6ae6b 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -34,19 +34,13 @@ static struct k_work firmware_work; static char firmware_uri[PACKAGE_URI_LEN]; static struct sockaddr firmware_addr; 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 void firmware_udp_receive(struct net_context *ctx, struct net_pkt *pkt, int status, void *user_data) { - lwm2m_udp_receive(ctx, pkt, pendings, NUM_PENDINGS, - replies, NUM_REPLIES, true, NULL); + lwm2m_udp_receive(&firmware_ctx, ctx, pkt, true, NULL); } static void retransmit_request(struct k_work *work) @@ -54,7 +48,8 @@ static void retransmit_request(struct k_work *work) struct zoap_pending *pending; 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) { return; } @@ -105,8 +100,8 @@ static int transfer_request(struct zoap_block_context *ctx, goto cleanup; } - pending = lwm2m_init_message_pending(&request, &firmware_addr, - pendings, NUM_PENDINGS); + pending = lwm2m_init_message_pending(&firmware_ctx, &request, + &firmware_addr); if (!pending) { ret = -ENOMEM; goto cleanup; @@ -114,7 +109,8 @@ static int transfer_request(struct zoap_block_context *ctx, /* set the reply handler */ 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) { SYS_LOG_ERR("No resources for waiting for replies."); ret = -ENOMEM; diff --git a/subsys/net/lib/lwm2m/lwm2m_rd_client.c b/subsys/net/lib/lwm2m/lwm2m_rd_client.c index 2dd3dd56cbc..b57c3a0ec70 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rd_client.c +++ b/subsys/net/lib/lwm2m/lwm2m_rd_client.c @@ -115,13 +115,6 @@ static K_THREAD_STACK_DEFINE(lwm2m_rd_client_thread_stack, CONFIG_LWM2M_RD_CLIENT_STACK_SIZE); 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 */ static char query_buffer[64]; /* allocate some data for queries and updates */ 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, query_buffer, strlen(query_buffer)); - pending = lwm2m_init_message_pending(&request, - &clients[index].bs_server, - pendings, NUM_PENDINGS); + pending = lwm2m_init_message_pending(clients[index].ctx, + &request, + &clients[index].bs_server); if (!pending) { ret = -ENOMEM; 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) { SYS_LOG_ERR("No resources for waiting for replies."); ret = -ENOMEM; @@ -569,15 +563,15 @@ static int sm_send_registration(int index, bool send_obj_support_data, } } - pending = lwm2m_init_message_pending(&request, - &clients[index].reg_server, - pendings, NUM_PENDINGS); + pending = lwm2m_init_message_pending(clients[index].ctx, &request, + &clients[index].reg_server); if (!pending) { ret = -ENOMEM; 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) { SYS_LOG_ERR("No resources for waiting for replies."); ret = -ENOMEM; @@ -670,15 +664,15 @@ static int sm_do_deregister(int index) clients[index].server_ep, strlen(clients[index].server_ep)); - pending = lwm2m_init_message_pending(&request, - &clients[index].reg_server, - pendings, NUM_PENDINGS); + pending = lwm2m_init_message_pending(clients[index].ctx, &request, + &clients[index].reg_server); if (!pending) { ret = -ENOMEM; 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) { SYS_LOG_ERR("No resources for waiting for replies."); ret = -ENOMEM;