From b29ec90223a40bc361b5f64c9c9ef8dde8b7865a Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Wed, 23 Aug 2017 22:33:38 -0700 Subject: [PATCH 01/10] net: lwm2m: introduce lwm2m context structure The LwM2M library does not use net_app APIs internally. To help this effort let's establish a user facing structure "lwm2m_ctx" (similar to http_client_ctx and mqtt_ctx) and start it off by wrappering the net_context structure. Future patches will add user setup options to this structure and eventually remove the net_context structure in favor of a net_app_ctx. Signed-off-by: Michael Scott --- include/net/lwm2m.h | 14 +++++++-- samples/net/lwm2m_client/src/lwm2m-client.c | 31 ++++++++++--------- subsys/net/lib/lwm2m/lwm2m_engine.c | 4 +-- .../net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 21 +++++++------ subsys/net/lib/lwm2m/lwm2m_rd_client.c | 12 +++---- 5 files changed, 47 insertions(+), 35 deletions(-) diff --git a/include/net/lwm2m.h b/include/net/lwm2m.h index 1f99380f9d4ceb..d700b093bb9899 100644 --- a/include/net/lwm2m.h +++ b/include/net/lwm2m.h @@ -25,6 +25,16 @@ #define IPSO_OBJECT_TEMP_SENSOR_ID 3303 #define IPSO_OBJECT_LIGHT_CONTROL_ID 3311 +/** + * LwM2M context structure + * + * @details Context structure for the LwM2M high-level API. + */ +struct lwm2m_ctx { + /** Net context structure */ + struct net_context *net_ctx; +}; + /* callback can return 1 if handled (don't update value) */ typedef void *(*lwm2m_engine_get_data_cb_t)(u16_t obj_inst_id, size_t *data_len); @@ -149,11 +159,11 @@ int lwm2m_engine_register_post_write_callback(char *path, int lwm2m_engine_register_exec_callback(char *path, lwm2m_engine_exec_cb_t cb); -int lwm2m_engine_start(struct net_context *net_ctx); +int lwm2m_engine_start(struct lwm2m_ctx *client_ctx); /* LWM2M RD Client */ -int lwm2m_rd_client_start(struct net_context *net_ctx, +int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, struct sockaddr *peer_addr, const char *ep_name); diff --git a/samples/net/lwm2m_client/src/lwm2m-client.c b/samples/net/lwm2m_client/src/lwm2m-client.c index 650ac11b764202..cfe4eddff5e72b 100644 --- a/samples/net/lwm2m_client/src/lwm2m-client.c +++ b/samples/net/lwm2m_client/src/lwm2m-client.c @@ -57,10 +57,12 @@ static struct device *led_dev; static u32_t led_state; #if defined(CONFIG_NET_IPV6) -static struct net_app_ctx udp6; +static struct net_app_ctx app_udp6; +static struct lwm2m_ctx udp6; #endif #if defined(CONFIG_NET_IPV4) -static struct net_app_ctx udp4; +static struct net_app_ctx app_udp4; +static struct lwm2m_ctx udp4; #endif static struct k_sem quit_lock; @@ -284,7 +286,7 @@ void main(void) } #if defined(CONFIG_NET_IPV6) - ret = setup_net_app_ctx(&udp6, CONFIG_NET_APP_PEER_IPV6_ADDR); + ret = setup_net_app_ctx(&app_udp6, CONFIG_NET_APP_PEER_IPV6_ADDR); if (ret < 0) { goto cleanup_ipv6; } @@ -295,15 +297,14 @@ void main(void) goto cleanup_ipv6; } - - ret = lwm2m_engine_start(udp6.ipv6.ctx); + udp6.net_ctx = app_udp6.ipv6.ctx; + ret = lwm2m_engine_start(&udp6); if (ret < 0) { SYS_LOG_ERR("Cannot init LWM2M IPv6 engine (%d)", ret); goto cleanup_ipv6; } - ret = lwm2m_rd_client_start(udp6.ipv6.ctx, &udp6.ipv6.remote, - ep_name); + ret = lwm2m_rd_client_start(&udp6, &app_udp6.ipv6.remote, ep_name); if (ret < 0) { SYS_LOG_ERR("LWM2M init LWM2M IPv6 RD client error (%d)", ret); @@ -314,7 +315,7 @@ void main(void) #endif #if defined(CONFIG_NET_IPV4) - ret = setup_net_app_ctx(&udp4, CONFIG_NET_APP_PEER_IPV4_ADDR); + ret = setup_net_app_ctx(&app_udp4, CONFIG_NET_APP_PEER_IPV4_ADDR); if (ret < 0) { goto cleanup_ipv4; } @@ -325,14 +326,14 @@ void main(void) goto cleanup_ipv4; } - ret = lwm2m_engine_start(udp4.ipv4.ctx); + udp4.net_ctx = app_udp4.ipv4.ctx; + ret = lwm2m_engine_start(&udp4); if (ret < 0) { SYS_LOG_ERR("Cannot init LWM2M IPv4 engine (%d)", ret); goto cleanup_ipv4; } - ret = lwm2m_rd_client_start(udp4.ipv4.ctx, &udp4.ipv4.remote, - ep_name); + ret = lwm2m_rd_client_start(&udp4, &app_udp4.ipv4.remote, ep_name); if (ret < 0) { SYS_LOG_ERR("LWM2M init LWM2M IPv4 RD client error (%d)", ret); @@ -346,13 +347,13 @@ void main(void) #if defined(CONFIG_NET_IPV4) cleanup_ipv4: - net_app_close(&udp4); - net_app_release(&udp4); + net_app_close(&app_udp4); + net_app_release(&app_udp4); #endif #if defined(CONFIG_NET_IPV6) cleanup_ipv6: - net_app_close(&udp6); - net_app_release(&udp6); + net_app_close(&app_udp6); + net_app_release(&app_udp6); #endif } diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 1737ccb125fd46..e93545a4f77231 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -2592,12 +2592,12 @@ static void lwm2m_engine_service(void) } } -int lwm2m_engine_start(struct net_context *net_ctx) +int lwm2m_engine_start(struct lwm2m_ctx *client_ctx) { int ret = 0; /* set callback */ - ret = net_context_recv(net_ctx, udp_receive, 0, NULL); + ret = net_context_recv(client_ctx->net_ctx, udp_receive, 0, NULL); if (ret) { SYS_LOG_ERR("Could not set receive for net context (err:%d)", ret); diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index 3a8e29e057a67d..39a3141052090f 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -33,7 +33,7 @@ static u8_t transfer_state; static struct k_work firmware_work; static char firmware_uri[PACKAGE_URI_LEN]; static struct sockaddr firmware_addr; -static struct net_context *firmware_net_ctx; +static struct lwm2m_ctx firmware_ctx; static struct k_delayed_work retransmit_work; #define NUM_PENDINGS CONFIG_LWM2M_ENGINE_MAX_PENDING @@ -83,7 +83,7 @@ static int transfer_request(struct zoap_block_context *ctx, struct zoap_reply *reply = NULL; int ret; - ret = lwm2m_init_message(firmware_net_ctx, &request, &pkt, + ret = lwm2m_init_message(firmware_ctx.net_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_GET, 0, token, tkl); if (ret) { @@ -256,7 +256,7 @@ static void firmware_transfer(struct k_work *work) #endif ret = net_context_get(firmware_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP, - &firmware_net_ctx); + &firmware_ctx.net_ctx); if (ret) { NET_ERR("Could not get an UDP context (err:%d)", ret); return; @@ -270,7 +270,7 @@ static void firmware_transfer(struct k_work *work) #if defined(CONFIG_NET_IPV6) if (firmware_addr.sa_family == AF_INET6) { - ret = net_context_bind(firmware_net_ctx, + ret = net_context_bind(firmware_ctx.net_ctx, (struct sockaddr *)&any_addr6, sizeof(any_addr6)); } @@ -278,7 +278,7 @@ static void firmware_transfer(struct k_work *work) #if defined(CONFIG_NET_IPV4) if (firmware_addr.sa_family == AF_INET) { - ret = net_context_bind(firmware_net_ctx, + ret = net_context_bind(firmware_ctx.net_ctx, (struct sockaddr *)&any_addr4, sizeof(any_addr4)); } @@ -290,7 +290,8 @@ static void firmware_transfer(struct k_work *work) } SYS_LOG_DBG("Attached to port: %d", port); - ret = net_context_recv(firmware_net_ctx, firmware_udp_receive, 0, NULL); + ret = net_context_recv(firmware_ctx.net_ctx, + firmware_udp_receive, 0, NULL); if (ret) { SYS_LOG_ERR("Could not set receive for net context (err:%d)", ret); @@ -305,8 +306,8 @@ static void firmware_transfer(struct k_work *work) return; cleanup: - if (firmware_net_ctx) { - net_context_put(firmware_net_ctx); + if (firmware_ctx.net_ctx) { + net_context_put(firmware_ctx.net_ctx); } } @@ -319,8 +320,8 @@ int lwm2m_firmware_cancel_transfer(void) int lwm2m_firmware_start_transfer(char *package_uri) { /* free up old context */ - if (firmware_net_ctx) { - net_context_put(firmware_net_ctx); + if (firmware_ctx.net_ctx) { + net_context_put(firmware_ctx.net_ctx); } if (transfer_state == STATE_IDLE) { diff --git a/subsys/net/lib/lwm2m/lwm2m_rd_client.c b/subsys/net/lib/lwm2m/lwm2m_rd_client.c index 91e0bb2d0db2ae..d7ee624496766a 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rd_client.c +++ b/subsys/net/lib/lwm2m/lwm2m_rd_client.c @@ -93,7 +93,7 @@ enum sm_engine_state { struct lwm2m_rd_client_info { u16_t lifetime; - struct net_context *net_ctx; + struct lwm2m_ctx *ctx; struct sockaddr bs_server; struct sockaddr reg_server; u8_t engine_state; @@ -402,7 +402,7 @@ static int sm_do_bootstrap(int index) clients[index].bootstrapped == 0 && clients[index].has_bs_server_info) { - ret = lwm2m_init_message(clients[index].net_ctx, + ret = lwm2m_init_message(clients[index].ctx->net_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_POST, 0, NULL, 0); if (ret) { @@ -516,7 +516,7 @@ static int sm_send_registration(int index, bool send_obj_support_data, /* remember the last reg time */ clients[index].last_update = k_uptime_get(); - ret = lwm2m_init_message(clients[index].net_ctx, + ret = lwm2m_init_message(clients[index].ctx->net_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_POST, 0, NULL, 0); if (ret) { @@ -658,7 +658,7 @@ static int sm_do_deregister(int index) struct zoap_reply *reply = NULL; int ret; - ret = lwm2m_init_message(clients[index].net_ctx, + ret = lwm2m_init_message(clients[index].ctx->net_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_DELETE, 0, NULL, 0); if (ret) { @@ -846,7 +846,7 @@ static void set_ep_ports(int index) #endif } -int lwm2m_rd_client_start(struct net_context *net_ctx, +int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, struct sockaddr *peer_addr, const char *ep_name) { @@ -864,7 +864,7 @@ int lwm2m_rd_client_start(struct net_context *net_ctx, /* TODO: use server URI data from security */ index = client_count; client_count++; - clients[index].net_ctx = net_ctx; + clients[index].ctx = client_ctx; memcpy(&clients[index].reg_server, peer_addr, sizeof(struct sockaddr)); memcpy(&clients[index].bs_server, peer_addr, sizeof(struct sockaddr)); set_ep_ports(index); From 7ce57da25fbded954a99bf8ca0c0c834e9b3c000 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Wed, 23 Aug 2017 22:46:53 -0700 Subject: [PATCH 02/10] samples: lwm2m: initialize lwm2m_ctx prior to use Signed-off-by: Michael Scott --- samples/net/lwm2m_client/src/lwm2m-client.c | 12 +++++++++--- subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/samples/net/lwm2m_client/src/lwm2m-client.c b/samples/net/lwm2m_client/src/lwm2m-client.c index cfe4eddff5e72b..3ea4289e30324d 100644 --- a/samples/net/lwm2m_client/src/lwm2m-client.c +++ b/samples/net/lwm2m_client/src/lwm2m-client.c @@ -252,10 +252,14 @@ static int lwm2m_setup(void) return 0; } -int setup_net_app_ctx(struct net_app_ctx *ctx, const char *peer) +int setup_net_app_ctx(struct lwm2m_ctx *client_ctx, + struct net_app_ctx *ctx, const char *peer) { int ret; + /* Set everything to 0 and later just assign the required fields. */ + memset(client_ctx, 0x00, sizeof(*client_ctx)); + ret = net_app_init_udp_client(ctx, NULL, NULL, peer, CONFIG_LWM2M_PEER_PORT, WAIT_TIME, NULL); if (ret < 0) { @@ -286,7 +290,8 @@ void main(void) } #if defined(CONFIG_NET_IPV6) - ret = setup_net_app_ctx(&app_udp6, CONFIG_NET_APP_PEER_IPV6_ADDR); + ret = setup_net_app_ctx(&udp6, &app_udp6, + CONFIG_NET_APP_PEER_IPV6_ADDR); if (ret < 0) { goto cleanup_ipv6; } @@ -315,7 +320,8 @@ void main(void) #endif #if defined(CONFIG_NET_IPV4) - ret = setup_net_app_ctx(&app_udp4, CONFIG_NET_APP_PEER_IPV4_ADDR); + ret = setup_net_app_ctx(&udp4, &app_udp4, + CONFIG_NET_APP_PEER_IPV4_ADDR); if (ret < 0) { goto cleanup_ipv4; } diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index 39a3141052090f..4c502be50774fb 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -325,6 +325,7 @@ int lwm2m_firmware_start_transfer(char *package_uri) } if (transfer_state == STATE_IDLE) { + memset(&firmware_ctx, 0, sizeof(struct lwm2m_ctx)); k_work_init(&firmware_work, firmware_transfer); k_delayed_work_init(&retransmit_work, retransmit_request); From af5bc52cc65439083d901e468f9f48bb9ec50419 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Wed, 23 Aug 2017 23:14:06 -0700 Subject: [PATCH 03/10] net: lwm2m: move retransmit_work to lwm2m_ctx Signed-off-by: Michael Scott --- include/net/lwm2m.h | 3 +++ subsys/net/lib/lwm2m/lwm2m_engine.c | 17 +++++++++++++---- subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 8 ++++---- subsys/net/lib/lwm2m/lwm2m_rd_client.c | 10 ++++++---- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/include/net/lwm2m.h b/include/net/lwm2m.h index d700b093bb9899..69bc2426493f07 100644 --- a/include/net/lwm2m.h +++ b/include/net/lwm2m.h @@ -33,6 +33,9 @@ struct lwm2m_ctx { /** Net context structure */ struct net_context *net_ctx; + + /** Private ZoAP and networking structures */ + struct k_delayed_work retransmit_work; }; /* callback can return 1 if handled (don't update value) */ diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index e93545a4f77231..4a3bd36c93a4c9 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -99,7 +99,6 @@ struct observe_node { struct zoap_pending pendings[NUM_PENDINGS]; struct zoap_reply replies[NUM_REPLIES]; -struct k_delayed_work retransmit_work; static struct observe_node observe_node_data[CONFIG_LWM2M_ENGINE_MAX_OBSERVER]; @@ -2383,9 +2382,11 @@ static void udp_receive(struct net_context *ctx, struct net_pkt *pkt, static void retransmit_request(struct k_work *work) { + struct lwm2m_ctx *client_ctx; struct zoap_pending *pending; int r; + client_ctx = CONTAINER_OF(work, struct lwm2m_ctx, retransmit_work); pending = zoap_pending_next_to_expire(pendings, NUM_PENDINGS); if (!pending) { return; @@ -2401,7 +2402,7 @@ static void retransmit_request(struct k_work *work) return; } - k_delayed_work_submit(&retransmit_work, pending->timeout); + k_delayed_work_submit(&client_ctx->retransmit_work, pending->timeout); } static int notify_message_reply_cb(const struct zoap_packet *response, @@ -2446,8 +2447,16 @@ static int generate_notify_message(struct observe_node *obs, struct lwm2m_output_context out; struct lwm2m_engine_context context; struct lwm2m_obj_path path; + struct lwm2m_ctx *client_ctx; int ret = 0; + client_ctx = CONTAINER_OF(obs->net_ctx, + struct lwm2m_ctx, net_ctx); + if (!client_ctx) { + SYS_LOG_ERR("observer has no valid LwM2M ctx!"); + return -EINVAL; + } + /* setup engine context */ memset(&context, 0, sizeof(struct lwm2m_engine_context)); context.out = &out; @@ -2542,7 +2551,7 @@ static int generate_notify_message(struct observe_node *obs, SYS_LOG_DBG("NOTIFY MSG: SENT"); zoap_pending_cycle(pending); - k_delayed_work_submit(&retransmit_work, pending->timeout); + k_delayed_work_submit(&client_ctx->retransmit_work, pending->timeout); return ret; cleanup: @@ -2603,6 +2612,7 @@ int lwm2m_engine_start(struct lwm2m_ctx *client_ctx) ret); } + k_delayed_work_init(&client_ctx->retransmit_work, retransmit_request); return ret; } @@ -2614,7 +2624,6 @@ static int lwm2m_engine_init(struct device *dev) K_THREAD_STACK_SIZEOF(engine_thread_stack), (k_thread_entry_t) lwm2m_engine_service, NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT); - k_delayed_work_init(&retransmit_work, retransmit_request); SYS_LOG_DBG("LWM2M engine thread started"); return 0; } diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index 4c502be50774fb..7c7cdd8c1dfe62 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -34,7 +34,6 @@ static struct k_work firmware_work; static char firmware_uri[PACKAGE_URI_LEN]; static struct sockaddr firmware_addr; static struct lwm2m_ctx firmware_ctx; -static struct k_delayed_work retransmit_work; #define NUM_PENDINGS CONFIG_LWM2M_ENGINE_MAX_PENDING #define NUM_REPLIES CONFIG_LWM2M_ENGINE_MAX_REPLIES @@ -70,7 +69,7 @@ static void retransmit_request(struct k_work *work) return; } - k_delayed_work_submit(&retransmit_work, pending->timeout); + k_delayed_work_submit(&firmware_ctx.retransmit_work, pending->timeout); } static int transfer_request(struct zoap_block_context *ctx, @@ -135,7 +134,7 @@ static int transfer_request(struct zoap_block_context *ctx, } zoap_pending_cycle(pending); - k_delayed_work_submit(&retransmit_work, pending->timeout); + k_delayed_work_submit(&firmware_ctx.retransmit_work, pending->timeout); return 0; cleanup: @@ -327,7 +326,8 @@ int lwm2m_firmware_start_transfer(char *package_uri) if (transfer_state == STATE_IDLE) { memset(&firmware_ctx, 0, sizeof(struct lwm2m_ctx)); k_work_init(&firmware_work, firmware_transfer); - k_delayed_work_init(&retransmit_work, retransmit_request); + k_delayed_work_init(&firmware_ctx.retransmit_work, + retransmit_request); /* start file transfer work */ strncpy(firmware_uri, package_uri, PACKAGE_URI_LEN - 1); diff --git a/subsys/net/lib/lwm2m/lwm2m_rd_client.c b/subsys/net/lib/lwm2m/lwm2m_rd_client.c index d7ee624496766a..2dd3dd56cbc77a 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rd_client.c +++ b/subsys/net/lib/lwm2m/lwm2m_rd_client.c @@ -121,7 +121,6 @@ struct k_thread lwm2m_rd_client_thread_data; extern struct zoap_pending pendings[NUM_PENDINGS]; extern struct zoap_reply replies[NUM_REPLIES]; -extern struct k_delayed_work retransmit_work; /* buffers */ static char query_buffer[64]; /* allocate some data for queries and updates */ @@ -448,7 +447,8 @@ static int sm_do_bootstrap(int index) } zoap_pending_cycle(pending); - k_delayed_work_submit(&retransmit_work, pending->timeout); + k_delayed_work_submit(&clients[index].ctx->retransmit_work, + pending->timeout); set_sm_state(index, ENGINE_BOOTSTRAP_SENT); } return ret; @@ -599,7 +599,8 @@ static int sm_send_registration(int index, bool send_obj_support_data, } zoap_pending_cycle(pending); - k_delayed_work_submit(&retransmit_work, pending->timeout); + k_delayed_work_submit(&clients[index].ctx->retransmit_work, + pending->timeout); return ret; cleanup: @@ -697,7 +698,8 @@ static int sm_do_deregister(int index) } zoap_pending_cycle(pending); - k_delayed_work_submit(&retransmit_work, pending->timeout); + k_delayed_work_submit(&clients[index].ctx->retransmit_work, + pending->timeout); set_sm_state(index, ENGINE_DEREGISTER_SENT); return ret; From 353a7334f20ef057f84c3f5aab5aacab45bb7125 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Thu, 24 Aug 2017 08:51:25 -0700 Subject: [PATCH 04/10] 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 69bc2426493f07..1d82b76b2f708a 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 4a3bd36c93a4c9..9b5f57cfa576a9 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 @@ void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt, } } -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 37c53d527fb37e..88188bf5ccf8e9 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 7c7cdd8c1dfe62..43698f6ae6b1d8 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 2dd3dd56cbc77a..b57c3a0ec70e17 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; From 8110f43ec7b0ab031204d8222b9cb10948b9131b Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Thu, 24 Aug 2017 09:30:39 -0700 Subject: [PATCH 05/10] net: lwm2m: remove net_context parameter from lwm2m_udp_receive() This is part of lwm2m_ctx and is not needed. Signed-off-by: Michael Scott --- subsys/net/lib/lwm2m/lwm2m_engine.c | 8 ++++---- subsys/net/lib/lwm2m/lwm2m_engine.h | 3 +-- subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 9b5f57cfa576a9..b7af98f0646f8b 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -2236,8 +2236,7 @@ int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr) NULL, K_NO_WAIT, NULL, NULL); } -void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, - struct net_context *net_ctx, struct net_pkt *pkt, +void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, bool handle_separate_response, int (*udp_request_handler)(struct zoap_packet *, struct zoap_packet *, @@ -2313,7 +2312,8 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, 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(net_ctx, &response2, &pkt2, + r = lwm2m_init_message(client_ctx->net_ctx, + &response2, &pkt2, ZOAP_TYPE_ACK, zoap_header_get_code(&response), zoap_header_get_id(&response), @@ -2375,7 +2375,7 @@ static void udp_receive(struct net_context *net_ctx, struct net_pkt *pkt, struct lwm2m_ctx, net_ctx); - lwm2m_udp_receive(client_ctx, net_ctx, pkt, false, handle_request); + lwm2m_udp_receive(client_ctx, pkt, false, handle_request); } static void retransmit_request(struct k_work *work) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.h b/subsys/net/lib/lwm2m/lwm2m_engine.h index 88188bf5ccf8e9..24dd9b7373ede7 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.h +++ b/subsys/net/lib/lwm2m/lwm2m_engine.h @@ -66,8 +66,7 @@ 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 lwm2m_ctx *client_ctx, - struct net_context *ctx, struct net_pkt *pkt, +void lwm2m_udp_receive(struct lwm2m_ctx *client_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 43698f6ae6b1d8..edd586110a03b5 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -40,7 +40,7 @@ static void firmware_udp_receive(struct net_context *ctx, struct net_pkt *pkt, int status, void *user_data) { - lwm2m_udp_receive(&firmware_ctx, ctx, pkt, true, NULL); + lwm2m_udp_receive(&firmware_ctx, pkt, true, NULL); } static void retransmit_request(struct k_work *work) From aa35705937b1d283b4992ab4a9c443f90d2c5716 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Thu, 24 Aug 2017 09:39:33 -0700 Subject: [PATCH 06/10] net: lwm2m: add net_context parameters to functions In preparation for the move to net_app APIs, we will need to pass net_app_ctx structures around to the following functions: lwm2m_udp_sendto() udp_request_handler() Let's add the parameter as net_context for now so the transition will be smoother later. Signed-off-by: Michael Scott --- subsys/net/lib/lwm2m/lwm2m_engine.c | 26 ++++++++++++------- subsys/net/lib/lwm2m/lwm2m_engine.h | 6 +++-- .../net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 6 +++-- subsys/net/lib/lwm2m/lwm2m_rd_client.c | 9 ++++--- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index b7af98f0646f8b..bd10d6537aa1f3 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -1997,7 +1997,8 @@ static int get_observe_option(const struct zoap_packet *zpkt) return zoap_option_value_to_int(&option); } -static int handle_request(struct zoap_packet *request, +static int handle_request(struct net_context *net_ctx, + struct zoap_packet *request, struct zoap_packet *response, struct sockaddr *from_addr) { @@ -2141,9 +2142,9 @@ static int handle_request(struct zoap_packet *request, r); } - r = engine_add_observer( - net_pkt_context(in.in_zpkt->pkt), - from_addr, token, tkl, &path, accept); + r = engine_add_observer(net_ctx, from_addr, + token, tkl, &path, + accept); if (r < 0) { SYS_LOG_ERR("add OBSERVE error: %d", r); } @@ -2230,7 +2231,8 @@ static int handle_request(struct zoap_packet *request, return r; } -int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr) +int lwm2m_udp_sendto(struct net_context *net_ctx, struct net_pkt *pkt, + const struct sockaddr *dst_addr) { return net_context_sendto(pkt, dst_addr, NET_SOCKADDR_MAX_SIZE, NULL, K_NO_WAIT, NULL, NULL); @@ -2238,7 +2240,8 @@ int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr) void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, bool handle_separate_response, - int (*udp_request_handler)(struct zoap_packet *, + int (*udp_request_handler)(struct net_context *net_ctx, + struct zoap_packet *, struct zoap_packet *, struct sockaddr *)) { @@ -2328,12 +2331,14 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, /* * The "response" here is actually a new request */ - r = udp_request_handler(&response, &response2, + r = udp_request_handler(client_ctx->net_ctx, + &response, &response2, &from_addr); if (r < 0) { SYS_LOG_ERR("Request handler error: %d", r); } else { - r = lwm2m_udp_sendto(pkt2, &from_addr); + r = lwm2m_udp_sendto(client_ctx->net_ctx, + pkt2, &from_addr); if (r < 0) { SYS_LOG_ERR("Err sending response: %d", r); @@ -2391,7 +2396,8 @@ static void retransmit_request(struct k_work *work) return; } - r = lwm2m_udp_sendto(pending->pkt, &pending->addr); + r = lwm2m_udp_sendto(client_ctx->net_ctx, + pending->pkt, &pending->addr); if (r < 0) { return; } @@ -2542,7 +2548,7 @@ static int generate_notify_message(struct observe_node *obs, zoap_reply_init(reply, &request); reply->reply = notify_message_reply_cb; - ret = lwm2m_udp_sendto(pkt, &obs->addr); + ret = lwm2m_udp_sendto(obs->net_ctx, pkt, &obs->addr); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); goto cleanup; diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.h b/subsys/net/lib/lwm2m/lwm2m_engine.h index 24dd9b7373ede7..d705b0e31e4b48 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.h +++ b/subsys/net/lib/lwm2m/lwm2m_engine.h @@ -65,10 +65,12 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst, struct lwm2m_engine_obj_field *obj_field, struct lwm2m_engine_context *context); -int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr); +int lwm2m_udp_sendto(struct net_context *net_ctx, struct net_pkt *pkt, + const struct sockaddr *dst_addr); void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, bool handle_separate_response, - int (*udp_request_handler)(struct zoap_packet *request, + int (*udp_request_handler)(struct net_context *net_ctx, + struct zoap_packet *request, struct zoap_packet *response, struct sockaddr *from_addr)); diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index edd586110a03b5..58242da03df85f 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -54,7 +54,8 @@ static void retransmit_request(struct k_work *work) return; } - r = lwm2m_udp_sendto(pending->pkt, &pending->addr); + r = lwm2m_udp_sendto(firmware_ctx.net_ctx, + pending->pkt, &pending->addr); if (r < 0) { return; } @@ -122,7 +123,8 @@ static int transfer_request(struct zoap_block_context *ctx, } /* send request */ - ret = lwm2m_udp_sendto(pkt, &firmware_addr); + ret = lwm2m_udp_sendto(firmware_ctx.net_ctx, + pkt, &firmware_addr); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); diff --git a/subsys/net/lib/lwm2m/lwm2m_rd_client.c b/subsys/net/lib/lwm2m/lwm2m_rd_client.c index b57c3a0ec70e17..93d7aedb5a069d 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rd_client.c +++ b/subsys/net/lib/lwm2m/lwm2m_rd_client.c @@ -433,7 +433,8 @@ static int sm_do_bootstrap(int index) lwm2m_sprint_ip_addr(&clients[index].bs_server), query_buffer); - ret = lwm2m_udp_sendto(pkt, &clients[index].bs_server); + ret = lwm2m_udp_sendto(clients[index].ctx->net_ctx, + pkt, &clients[index].bs_server); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); @@ -585,7 +586,8 @@ static int sm_send_registration(int index, bool send_obj_support_data, SYS_LOG_DBG("registration sent [%s]", lwm2m_sprint_ip_addr(&clients[index].reg_server)); - ret = lwm2m_udp_sendto(pkt, &clients[index].reg_server); + ret = lwm2m_udp_sendto(clients[index].ctx->net_ctx, + pkt, &clients[index].reg_server); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); @@ -684,7 +686,8 @@ static int sm_do_deregister(int index) SYS_LOG_INF("Deregister from '%s'", clients[index].server_ep); - ret = lwm2m_udp_sendto(pkt, &clients[index].reg_server); + ret = lwm2m_udp_sendto(clients[index].ctx->net_ctx, + pkt, &clients[index].reg_server); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); From 754095c9dcb169b4c80fd33f76b7f4d47694222f Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Mon, 28 Aug 2017 14:39:50 -0700 Subject: [PATCH 07/10] net: lwm2m: move library internals to net_app APIs This is the final stage of moving the LwM2M library internals to the net_app APIs. This means we can support DTLS and other built-in features in the future. All of the logic for establishing the network connection is removed from the sample app. Signed-off-by: Michael Scott --- include/net/lwm2m.h | 30 +++- samples/net/lwm2m_client/src/lwm2m-client.c | 134 +++--------------- subsys/net/lib/lwm2m/lwm2m_engine.c | 104 ++++++++++---- subsys/net/lib/lwm2m/lwm2m_engine.h | 6 +- .../net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 80 ++++------- subsys/net/lib/lwm2m/lwm2m_rd_client.c | 75 +++++----- 6 files changed, 184 insertions(+), 245 deletions(-) diff --git a/include/net/lwm2m.h b/include/net/lwm2m.h index 1d82b76b2f708a..ae730e093dcbed 100644 --- a/include/net/lwm2m.h +++ b/include/net/lwm2m.h @@ -7,7 +7,7 @@ #ifndef __LWM2M_H__ #define __LWM2M_H__ -#include +#include #include /* LWM2M Objects defined by OMA */ @@ -32,8 +32,22 @@ * @details Context structure for the LwM2M high-level API. */ struct lwm2m_ctx { - /** Net context structure */ - struct net_context *net_ctx; + /** Net app context structure */ + struct net_app_ctx net_app_ctx; + s32_t net_init_timeout; + s32_t net_timeout; + +#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) + /** Network packet (net_pkt) memory pool for network contexts attached + * to this LwM2M context. + */ + net_pkt_get_slab_func_t tx_slab; + + /** Network data net_buf pool for network contexts attached to this + * LwM2M context. + */ + net_pkt_get_pool_func_t data_pool; +#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */ /** Private ZoAP and networking structures */ struct zoap_pending pendings[CONFIG_LWM2M_ENGINE_MAX_PENDING]; @@ -165,12 +179,18 @@ int lwm2m_engine_register_post_write_callback(char *path, int lwm2m_engine_register_exec_callback(char *path, lwm2m_engine_exec_cb_t cb); -int lwm2m_engine_start(struct lwm2m_ctx *client_ctx); +#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) +int lwm2m_engine_set_net_pkt_pool(struct lwm2m_ctx *ctx, + net_pkt_get_slab_func_t tx_slab, + net_pkt_get_pool_func_t data_pool); +#endif +int lwm2m_engine_start(struct lwm2m_ctx *client_ctx, + char *peer_str, u16_t peer_port); /* LWM2M RD Client */ int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, - struct sockaddr *peer_addr, + char *peer_str, u16_t peer_port, const char *ep_name); #endif /* __LWM2M_H__ */ diff --git a/samples/net/lwm2m_client/src/lwm2m-client.c b/samples/net/lwm2m_client/src/lwm2m-client.c index 3ea4289e30324d..c95f449176d13e 100644 --- a/samples/net/lwm2m_client/src/lwm2m-client.c +++ b/samples/net/lwm2m_client/src/lwm2m-client.c @@ -11,9 +11,6 @@ #include #include #include -#include -#include -#include #include #define APP_BANNER "Run LWM2M client" @@ -56,14 +53,7 @@ static int usb_current = 900; static struct device *led_dev; static u32_t led_state; -#if defined(CONFIG_NET_IPV6) -static struct net_app_ctx app_udp6; -static struct lwm2m_ctx udp6; -#endif -#if defined(CONFIG_NET_IPV4) -static struct net_app_ctx app_udp4; -static struct lwm2m_ctx udp4; -#endif +static struct lwm2m_ctx client; static struct k_sem quit_lock; #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) @@ -172,21 +162,6 @@ static int firmware_block_received_cb(u16_t obj_inst_id, return 1; } -static int set_endpoint_name(char *ep_name, sa_family_t family) -{ - int ret; - - ret = snprintk(ep_name, ENDPOINT_LEN, "%s-%s-%u", - CONFIG_BOARD, (family == AF_INET6 ? "ipv6" : "ipv4"), - sys_rand32_get()); - if (ret < 0 || ret >= ENDPOINT_LEN) { - SYS_LOG_ERR("Can't fill name buffer"); - return -EINVAL; - } - - return 0; -} - static int lwm2m_setup(void) { struct float32_value float_value; @@ -252,32 +227,9 @@ static int lwm2m_setup(void) return 0; } -int setup_net_app_ctx(struct lwm2m_ctx *client_ctx, - struct net_app_ctx *ctx, const char *peer) -{ - int ret; - - /* Set everything to 0 and later just assign the required fields. */ - memset(client_ctx, 0x00, sizeof(*client_ctx)); - - ret = net_app_init_udp_client(ctx, NULL, NULL, peer, - CONFIG_LWM2M_PEER_PORT, WAIT_TIME, NULL); - if (ret < 0) { - SYS_LOG_ERR("Cannot init UDP client (%d)", ret); - return ret; - } - -#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) - net_app_set_net_pkt_pool(ctx, tx_udp_slab, data_udp_pool); -#endif - - return ret; -} - void main(void) { int ret; - char ep_name[ENDPOINT_LEN]; SYS_LOG_INF(APP_BANNER); @@ -289,77 +241,29 @@ void main(void) return; } -#if defined(CONFIG_NET_IPV6) - ret = setup_net_app_ctx(&udp6, &app_udp6, - CONFIG_NET_APP_PEER_IPV6_ADDR); - if (ret < 0) { - goto cleanup_ipv6; - } - - ret = set_endpoint_name(ep_name, udp6.ipv6.local.sa_family); - if (ret < 0) { - SYS_LOG_ERR("Cannot set IPv6 endpoint name (%d)", ret); - goto cleanup_ipv6; - } - - udp6.net_ctx = app_udp6.ipv6.ctx; - ret = lwm2m_engine_start(&udp6); - if (ret < 0) { - SYS_LOG_ERR("Cannot init LWM2M IPv6 engine (%d)", ret); - goto cleanup_ipv6; - } - - ret = lwm2m_rd_client_start(&udp6, &app_udp6.ipv6.remote, ep_name); - if (ret < 0) { - SYS_LOG_ERR("LWM2M init LWM2M IPv6 RD client error (%d)", - ret); - goto cleanup_ipv6; - } - - SYS_LOG_INF("IPv6 setup complete."); + memset(&client, 0x0, sizeof(client)); + client.net_init_timeout = WAIT_TIME; + client.net_timeout = CONNECT_TIME; +#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) + client.tx_slab = tx_udp_slab; + client.data_pool = data_udp_pool; #endif -#if defined(CONFIG_NET_IPV4) - ret = setup_net_app_ctx(&udp4, &app_udp4, - CONFIG_NET_APP_PEER_IPV4_ADDR); - if (ret < 0) { - goto cleanup_ipv4; - } - - ret = set_endpoint_name(ep_name, udp4.ipv4.local.sa_family); - if (ret < 0) { - SYS_LOG_ERR("Cannot set IPv4 endpoint name (%d)", ret); - goto cleanup_ipv4; - } - - udp4.net_ctx = app_udp4.ipv4.ctx; - ret = lwm2m_engine_start(&udp4); - if (ret < 0) { - SYS_LOG_ERR("Cannot init LWM2M IPv4 engine (%d)", ret); - goto cleanup_ipv4; - } - - ret = lwm2m_rd_client_start(&udp4, &app_udp4.ipv4.remote, ep_name); +#if defined(CONFIG_NET_IPV6) + ret = lwm2m_rd_client_start(&client, CONFIG_NET_APP_PEER_IPV6_ADDR, + CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD); +#elif defined(CONFIG_NET_IPV4) + ret = lwm2m_rd_client_start(&client, CONFIG_NET_APP_PEER_IPV4_ADDR, + CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD); +#else + SYS_LOG_ERR("LwM2M client requires IPv4 or IPv6."); + ret = -EPROTONOSUPPORT; +#endif if (ret < 0) { - SYS_LOG_ERR("LWM2M init LWM2M IPv4 RD client error (%d)", + SYS_LOG_ERR("LWM2M init LWM2M RD client error (%d)", ret); - goto cleanup_ipv4; + return; } - SYS_LOG_INF("IPv4 setup complete."); -#endif - k_sem_take(&quit_lock, K_FOREVER); - -#if defined(CONFIG_NET_IPV4) -cleanup_ipv4: - net_app_close(&app_udp4); - net_app_release(&app_udp4); -#endif - -#if defined(CONFIG_NET_IPV6) -cleanup_ipv6: - net_app_close(&app_udp6); - net_app_release(&app_udp6); -#endif } diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index bd10d6537aa1f3..18959daf9828e4 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -80,7 +81,7 @@ struct observe_node { sys_snode_t node; - struct net_context *net_ctx; + struct net_app_ctx *net_app_ctx; struct sockaddr addr; struct lwm2m_obj_path path; u8_t token[8]; @@ -178,7 +179,7 @@ int lwm2m_notify_observer_path(struct lwm2m_obj_path *path) path->res_id); } -static int engine_add_observer(struct net_context *net_ctx, +static int engine_add_observer(struct net_app_ctx *app_ctx, struct sockaddr *addr, const u8_t *token, u8_t tkl, struct lwm2m_obj_path *path, @@ -257,7 +258,7 @@ static int engine_add_observer(struct net_context *net_ctx, /* copy the values and add it to the list */ observe_node_data[i].used = true; - observe_node_data[i].net_ctx = net_ctx; + observe_node_data[i].net_app_ctx = app_ctx; memcpy(&observe_node_data[i].addr, addr, sizeof(*addr)); memcpy(&observe_node_data[i].path, path, sizeof(*path)); memcpy(observe_node_data[i].token, token, tkl); @@ -573,20 +574,21 @@ static void zoap_options_to_path(struct zoap_option *opt, int options_count, } } -int lwm2m_init_message(struct net_context *net_ctx, struct zoap_packet *zpkt, +int lwm2m_init_message(struct net_app_ctx *app_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 net_buf *frag; int r; - *pkt = net_pkt_get_tx(net_ctx, BUF_ALLOC_TIMEOUT); + *pkt = net_app_get_net_pkt(app_ctx, AF_UNSPEC, BUF_ALLOC_TIMEOUT); if (!*pkt) { SYS_LOG_ERR("Unable to get TX packet, not enough memory."); return -ENOMEM; } - frag = net_pkt_get_data(net_ctx, BUF_ALLOC_TIMEOUT); + frag = net_app_get_net_buf(app_ctx, *pkt, + BUF_ALLOC_TIMEOUT); if (!frag) { SYS_LOG_ERR("Unable to get DATA buffer, not enough memory."); net_pkt_unref(*pkt); @@ -594,8 +596,6 @@ int lwm2m_init_message(struct net_context *net_ctx, struct zoap_packet *zpkt, return -ENOMEM; } - net_pkt_frag_add(*pkt, frag); - r = zoap_packet_init(zpkt, *pkt); if (r < 0) { SYS_LOG_ERR("zoap packet init error (err:%d)", r); @@ -1997,7 +1997,7 @@ static int get_observe_option(const struct zoap_packet *zpkt) return zoap_option_value_to_int(&option); } -static int handle_request(struct net_context *net_ctx, +static int handle_request(struct net_app_ctx *app_ctx, struct zoap_packet *request, struct zoap_packet *response, struct sockaddr *from_addr) @@ -2142,7 +2142,7 @@ static int handle_request(struct net_context *net_ctx, r); } - r = engine_add_observer(net_ctx, from_addr, + r = engine_add_observer(app_ctx, from_addr, token, tkl, &path, accept); if (r < 0) { @@ -2231,16 +2231,16 @@ static int handle_request(struct net_context *net_ctx, return r; } -int lwm2m_udp_sendto(struct net_context *net_ctx, struct net_pkt *pkt, +int lwm2m_udp_sendto(struct net_app_ctx *app_ctx, struct net_pkt *pkt, const struct sockaddr *dst_addr) { - return net_context_sendto(pkt, dst_addr, NET_SOCKADDR_MAX_SIZE, - NULL, K_NO_WAIT, NULL, NULL); + return net_app_send_pkt(app_ctx, pkt, dst_addr, NET_SOCKADDR_MAX_SIZE, + K_NO_WAIT, NULL); } void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, bool handle_separate_response, - int (*udp_request_handler)(struct net_context *net_ctx, + int (*udp_request_handler)(struct net_app_ctx *app_ctx, struct zoap_packet *, struct zoap_packet *, struct sockaddr *)) @@ -2315,7 +2315,7 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_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(client_ctx->net_ctx, + r = lwm2m_init_message(&client_ctx->net_app_ctx, &response2, &pkt2, ZOAP_TYPE_ACK, zoap_header_get_code(&response), @@ -2331,13 +2331,13 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, /* * The "response" here is actually a new request */ - r = udp_request_handler(client_ctx->net_ctx, + r = udp_request_handler(&client_ctx->net_app_ctx, &response, &response2, &from_addr); if (r < 0) { SYS_LOG_ERR("Request handler error: %d", r); } else { - r = lwm2m_udp_sendto(client_ctx->net_ctx, + r = lwm2m_udp_sendto(&client_ctx->net_app_ctx, pkt2, &from_addr); if (r < 0) { SYS_LOG_ERR("Err sending response: %d", @@ -2373,12 +2373,12 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, } } -static void udp_receive(struct net_context *net_ctx, struct net_pkt *pkt, +static void udp_receive(struct net_app_ctx *app_ctx, struct net_pkt *pkt, int status, void *user_data) { - struct lwm2m_ctx *client_ctx = CONTAINER_OF(net_ctx, + struct lwm2m_ctx *client_ctx = CONTAINER_OF(app_ctx, struct lwm2m_ctx, - net_ctx); + net_app_ctx); lwm2m_udp_receive(client_ctx, pkt, false, handle_request); } @@ -2396,7 +2396,7 @@ static void retransmit_request(struct k_work *work) return; } - r = lwm2m_udp_sendto(client_ctx->net_ctx, + r = lwm2m_udp_sendto(&client_ctx->net_app_ctx, pending->pkt, &pending->addr); if (r < 0) { return; @@ -2455,8 +2455,8 @@ static int generate_notify_message(struct observe_node *obs, struct lwm2m_ctx *client_ctx; int ret = 0; - client_ctx = CONTAINER_OF(obs->net_ctx, - struct lwm2m_ctx, net_ctx); + client_ctx = CONTAINER_OF(obs->net_app_ctx, + struct lwm2m_ctx, net_app_ctx); if (!client_ctx) { SYS_LOG_ERR("observer has no valid LwM2M ctx!"); return -EINVAL; @@ -2491,7 +2491,7 @@ static int generate_notify_message(struct observe_node *obs, return -EINVAL; } - ret = lwm2m_init_message(obs->net_ctx, out.out_zpkt, &pkt, + ret = lwm2m_init_message(obs->net_app_ctx, out.out_zpkt, &pkt, ZOAP_TYPE_CON, ZOAP_RESPONSE_CODE_CONTENT, 0, obs->token, obs->tkl); if (ret) { @@ -2548,7 +2548,7 @@ static int generate_notify_message(struct observe_node *obs, zoap_reply_init(reply, &request); reply->reply = notify_message_reply_cb; - ret = lwm2m_udp_sendto(obs->net_ctx, pkt, &obs->addr); + ret = lwm2m_udp_sendto(obs->net_app_ctx, pkt, &obs->addr); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); goto cleanup; @@ -2606,18 +2606,62 @@ static void lwm2m_engine_service(void) } } -int lwm2m_engine_start(struct lwm2m_ctx *client_ctx) +#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) +int lwm2m_engine_set_net_pkt_pool(struct lwm2m_ctx *ctx, + net_pkt_get_slab_func_t tx_slab, + net_pkt_get_pool_func_t data_pool) +{ + ctx->tx_slab = tx_slab; + ctx->data_pool = data_pool; + + return 0; +} +#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */ + +int lwm2m_engine_start(struct lwm2m_ctx *client_ctx, + char *peer_str, u16_t peer_port) { int ret = 0; - /* set callback */ - ret = net_context_recv(client_ctx->net_ctx, udp_receive, 0, NULL); + /* TODO: use security object for initial setup */ + ret = net_app_init_udp_client(&client_ctx->net_app_ctx, + NULL, NULL, + peer_str, + peer_port, + client_ctx->net_init_timeout, + client_ctx); if (ret) { - SYS_LOG_ERR("Could not set receive for net context (err:%d)", - ret); + SYS_LOG_ERR("net_app_init_udp_client err:%d", ret); + goto error_start; } k_delayed_work_init(&client_ctx->retransmit_work, retransmit_request); + +#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) + net_app_set_net_pkt_pool(&client_ctx->net_app_ctx, + client_ctx->tx_slab, client_ctx->data_pool); +#endif + + /* set net_app callbacks */ + ret = net_app_set_cb(&client_ctx->net_app_ctx, + NULL, udp_receive, NULL, NULL); + if (ret) { + SYS_LOG_ERR("Could not set receive callback (err:%d)", ret); + goto error_start; + } + + ret = net_app_connect(&client_ctx->net_app_ctx, + client_ctx->net_timeout); + if (ret < 0) { + SYS_LOG_ERR("Cannot connect UDP (%d)", ret); + goto error_start; + } + + return 0; + +error_start: + net_app_close(&client_ctx->net_app_ctx); + net_app_release(&client_ctx->net_app_ctx); return ret; } diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.h b/subsys/net/lib/lwm2m/lwm2m_engine.h index d705b0e31e4b48..12189d840c43a2 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.h +++ b/subsys/net/lib/lwm2m/lwm2m_engine.h @@ -48,7 +48,7 @@ int lwm2m_get_or_create_engine_obj(struct lwm2m_engine_context *context, struct lwm2m_engine_obj_inst **obj_inst, u8_t *created); -int lwm2m_init_message(struct net_context *net_ctx, struct zoap_packet *zpkt, +int lwm2m_init_message(struct net_app_ctx *app_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 lwm2m_ctx *client_ctx, @@ -65,11 +65,11 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst, struct lwm2m_engine_obj_field *obj_field, struct lwm2m_engine_context *context); -int lwm2m_udp_sendto(struct net_context *net_ctx, struct net_pkt *pkt, +int lwm2m_udp_sendto(struct net_app_ctx *app_ctx, struct net_pkt *pkt, const struct sockaddr *dst_addr); void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, bool handle_separate_response, - int (*udp_request_handler)(struct net_context *net_ctx, + int (*udp_request_handler)(struct net_app_ctx *app_ctx, struct zoap_packet *request, struct zoap_packet *response, struct sockaddr *from_addr)); diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index 58242da03df85f..aa30a6a474acb4 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -25,9 +26,11 @@ #define STATE_IDLE 0 #define STATE_CONNECTING 1 -#define PACKAGE_URI_LEN 255 +#define PACKAGE_URI_LEN 255 -#define BUF_ALLOC_TIMEOUT K_SECONDS(1) +#define BUF_ALLOC_TIMEOUT K_SECONDS(1) +#define NETWORK_INIT_TIMEOUT K_SECONDS(10) +#define NETWORK_CONNECT_TIMEOUT K_SECONDS(10) static u8_t transfer_state; static struct k_work firmware_work; @@ -37,8 +40,8 @@ static struct lwm2m_ctx firmware_ctx; 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) +firmware_udp_receive(struct net_app_ctx *app_ctx, struct net_pkt *pkt, + int status, void *user_data) { lwm2m_udp_receive(&firmware_ctx, pkt, true, NULL); } @@ -54,7 +57,7 @@ static void retransmit_request(struct k_work *work) return; } - r = lwm2m_udp_sendto(firmware_ctx.net_ctx, + r = lwm2m_udp_sendto(&firmware_ctx.net_app_ctx, pending->pkt, &pending->addr); if (r < 0) { return; @@ -78,7 +81,7 @@ static int transfer_request(struct zoap_block_context *ctx, struct zoap_reply *reply = NULL; int ret; - ret = lwm2m_init_message(firmware_ctx.net_ctx, &request, &pkt, + ret = lwm2m_init_message(&firmware_ctx.net_app_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_GET, 0, token, tkl); if (ret) { @@ -123,7 +126,7 @@ static int transfer_request(struct zoap_block_context *ctx, } /* send request */ - ret = lwm2m_udp_sendto(firmware_ctx.net_ctx, + ret = lwm2m_udp_sendto(&firmware_ctx.net_app_ctx, pkt, &firmware_addr); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", @@ -216,15 +219,6 @@ static enum zoap_block_size default_block_size(void) static void firmware_transfer(struct k_work *work) { -#if defined(CONFIG_NET_IPV6) - static struct sockaddr_in6 any_addr6 = { .sin6_addr = IN6ADDR_ANY_INIT, - .sin6_family = AF_INET6 }; -#endif -#if defined(CONFIG_NET_IPV4) - static struct sockaddr_in any_addr4 = { .sin_addr = INADDR_ANY_INIT, - .sin_family = AF_INET }; -#endif - struct net_if *iface; int ret, port, family; /* Server Peer IP information */ @@ -252,46 +246,28 @@ static void firmware_transfer(struct k_work *work) } #endif - ret = net_context_get(firmware_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP, - &firmware_ctx.net_ctx); + ret = net_app_init_udp_client(&firmware_ctx.net_app_ctx, NULL, + &firmware_addr, NULL, port, + firmware_ctx.net_init_timeout, NULL); if (ret) { NET_ERR("Could not get an UDP context (err:%d)", ret); return; } - iface = net_if_get_default(); - if (!iface) { - NET_ERR("Could not find default interface"); - goto cleanup; - } - -#if defined(CONFIG_NET_IPV6) - if (firmware_addr.sa_family == AF_INET6) { - ret = net_context_bind(firmware_ctx.net_ctx, - (struct sockaddr *)&any_addr6, - sizeof(any_addr6)); - } -#endif - -#if defined(CONFIG_NET_IPV4) - if (firmware_addr.sa_family == AF_INET) { - ret = net_context_bind(firmware_ctx.net_ctx, - (struct sockaddr *)&any_addr4, - sizeof(any_addr4)); - } -#endif + SYS_LOG_DBG("Attached to port: %d", port); + /* set net_app callbacks */ + ret = net_app_set_cb(&firmware_ctx.net_app_ctx, NULL, + firmware_udp_receive, NULL, NULL); if (ret) { - NET_ERR("Could not bind the UDP context (err:%d)", ret); + SYS_LOG_ERR("Could not set receive callback (err:%d)", ret); goto cleanup; } - SYS_LOG_DBG("Attached to port: %d", port); - ret = net_context_recv(firmware_ctx.net_ctx, - firmware_udp_receive, 0, NULL); - if (ret) { - SYS_LOG_ERR("Could not set receive for net context (err:%d)", - ret); + ret = net_app_connect(&firmware_ctx.net_app_ctx, + firmware_ctx.net_timeout); + if (ret < 0) { + SYS_LOG_ERR("Cannot connect UDP (%d)", ret); goto cleanup; } @@ -303,9 +279,8 @@ static void firmware_transfer(struct k_work *work) return; cleanup: - if (firmware_ctx.net_ctx) { - net_context_put(firmware_ctx.net_ctx); - } + net_app_close(&firmware_ctx.net_app_ctx); + net_app_release(&firmware_ctx.net_app_ctx); } /* TODO: */ @@ -317,12 +292,15 @@ int lwm2m_firmware_cancel_transfer(void) int lwm2m_firmware_start_transfer(char *package_uri) { /* free up old context */ - if (firmware_ctx.net_ctx) { - net_context_put(firmware_ctx.net_ctx); + if (firmware_ctx.net_app_ctx.is_init) { + net_app_close(&firmware_ctx.net_app_ctx); + net_app_release(&firmware_ctx.net_app_ctx); } if (transfer_state == STATE_IDLE) { memset(&firmware_ctx, 0, sizeof(struct lwm2m_ctx)); + firmware_ctx.net_init_timeout = NETWORK_INIT_TIMEOUT; + firmware_ctx.net_timeout = NETWORK_CONNECT_TIMEOUT; k_work_init(&firmware_work, firmware_transfer); k_delayed_work_init(&firmware_ctx.retransmit_work, retransmit_request); diff --git a/subsys/net/lib/lwm2m/lwm2m_rd_client.c b/subsys/net/lib/lwm2m/lwm2m_rd_client.c index 93d7aedb5a069d..07f064f1fbc45f 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rd_client.c +++ b/subsys/net/lib/lwm2m/lwm2m_rd_client.c @@ -394,7 +394,7 @@ static int sm_do_bootstrap(int index) clients[index].bootstrapped == 0 && clients[index].has_bs_server_info) { - ret = lwm2m_init_message(clients[index].ctx->net_ctx, + ret = lwm2m_init_message(&clients[index].ctx->net_app_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_POST, 0, NULL, 0); if (ret) { @@ -433,7 +433,7 @@ static int sm_do_bootstrap(int index) lwm2m_sprint_ip_addr(&clients[index].bs_server), query_buffer); - ret = lwm2m_udp_sendto(clients[index].ctx->net_ctx, + ret = lwm2m_udp_sendto(&clients[index].ctx->net_app_ctx, pkt, &clients[index].bs_server); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", @@ -511,7 +511,7 @@ static int sm_send_registration(int index, bool send_obj_support_data, /* remember the last reg time */ clients[index].last_update = k_uptime_get(); - ret = lwm2m_init_message(clients[index].ctx->net_ctx, + ret = lwm2m_init_message(&clients[index].ctx->net_app_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_POST, 0, NULL, 0); if (ret) { @@ -586,7 +586,7 @@ static int sm_send_registration(int index, bool send_obj_support_data, SYS_LOG_DBG("registration sent [%s]", lwm2m_sprint_ip_addr(&clients[index].reg_server)); - ret = lwm2m_udp_sendto(clients[index].ctx->net_ctx, + ret = lwm2m_udp_sendto(&clients[index].ctx->net_app_ctx, pkt, &clients[index].reg_server); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", @@ -655,7 +655,7 @@ static int sm_do_deregister(int index) struct zoap_reply *reply = NULL; int ret; - ret = lwm2m_init_message(clients[index].ctx->net_ctx, + ret = lwm2m_init_message(&clients[index].ctx->net_app_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_DELETE, 0, NULL, 0); if (ret) { @@ -686,7 +686,7 @@ static int sm_do_deregister(int index) SYS_LOG_INF("Deregister from '%s'", clients[index].server_ep); - ret = lwm2m_udp_sendto(clients[index].ctx->net_ctx, + ret = lwm2m_udp_sendto(&clients[index].ctx->net_app_ctx, pkt, &clients[index].reg_server); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", @@ -818,60 +818,53 @@ static bool peer_addr_exist(struct sockaddr *peer_addr) return ret; } -static void set_ep_ports(int index) -{ -#if defined(CONFIG_NET_IPV6) - if (clients[index].bs_server.sa_family == AF_INET6) { - net_sin6(&clients[index].bs_server)->sin6_port = - htons(LWM2M_BOOTSTRAP_PORT); - } - - if (clients[index].reg_server.sa_family == AF_INET6) { - net_sin6(&clients[index].reg_server)->sin6_port = - htons(LWM2M_PEER_PORT); - } -#endif - -#if defined(CONFIG_NET_IPV4) - if (clients[index].bs_server.sa_family == AF_INET) { - net_sin(&clients[index].bs_server)->sin_port = - htons(LWM2M_BOOTSTRAP_PORT); - } - - if (clients[index].reg_server.sa_family == AF_INET) { - net_sin(&clients[index].reg_server)->sin_port = - htons(LWM2M_PEER_PORT); - } -#endif -} - int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, - struct sockaddr *peer_addr, + char *peer_str, u16_t peer_port, const char *ep_name) { - int index; + int index, ret = 0; if (client_count + 1 > CLIENT_INSTANCE_COUNT) { return -ENOMEM; } - if (peer_addr_exist(peer_addr)) { - return -EEXIST; + ret = lwm2m_engine_start(client_ctx, peer_str, peer_port); + if (ret < 0) { + SYS_LOG_ERR("Cannot init LWM2M engine (%d)", ret); + goto cleanup; + } + + if (!client_ctx->net_app_ctx.default_ctx) { + SYS_LOG_ERR("Default net_app_ctx not selected!"); + return -EINVAL; + } + + if (peer_addr_exist(&client_ctx->net_app_ctx.default_ctx->remote)) { + SYS_LOG_ERR("Remote exists already"); + ret = -EEXIST; + goto cleanup; } - /* Server Peer IP information */ /* TODO: use server URI data from security */ index = client_count; client_count++; clients[index].ctx = client_ctx; - memcpy(&clients[index].reg_server, peer_addr, sizeof(struct sockaddr)); - memcpy(&clients[index].bs_server, peer_addr, sizeof(struct sockaddr)); - set_ep_ports(index); + memcpy(&clients[index].reg_server, + &client_ctx->net_app_ctx.default_ctx->remote, + sizeof(struct sockaddr)); + memcpy(&clients[index].bs_server, + &client_ctx->net_app_ctx.default_ctx->remote, + sizeof(struct sockaddr)); set_sm_state(index, ENGINE_INIT); strncpy(clients[index].ep_name, ep_name, CLIENT_EP_LEN - 1); SYS_LOG_INF("LWM2M Client: %s", clients[index].ep_name); return 0; + +cleanup: + net_app_close(&client_ctx->net_app_ctx); + net_app_release(&client_ctx->net_app_ctx); + return ret; } static int lwm2m_rd_client_init(struct device *dev) From 0fd0f5315995a665204a1fcee327f08a80f85895 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Fri, 25 Aug 2017 14:14:04 -0700 Subject: [PATCH 08/10] net: lwm2m: remove registration client "registered" field Replace with a check of the state machine's state instead. Signed-off-by: Michael Scott --- subsys/net/lib/lwm2m/lwm2m_rd_client.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_rd_client.c b/subsys/net/lib/lwm2m/lwm2m_rd_client.c index 07f064f1fbc45f..88d18ffde1b9ef 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rd_client.c +++ b/subsys/net/lib/lwm2m/lwm2m_rd_client.c @@ -101,7 +101,6 @@ struct lwm2m_rd_client_info { u8_t has_bs_server_info; u8_t use_registration; u8_t has_registration_info; - u8_t registered; u8_t bootstrapped; /* bootstrap done */ u8_t trigger_update; @@ -129,6 +128,12 @@ static void set_sm_state(int index, u8_t state) clients[index].engine_state = state; } +static bool sm_is_registered(int index) +{ + return (clients[index].engine_state >= ENGINE_REGISTRATION_DONE && + clients[index].engine_state <= ENGINE_DEREGISTER_FAILED); +} + static u8_t get_sm_state(int index) { /* TODO: add locking? */ @@ -256,7 +261,6 @@ static int do_registration_reply_cb(const struct zoap_packet *response, options[1].len); clients[index].server_ep[options[1].len] = '\0'; set_sm_state(index, ENGINE_REGISTRATION_DONE); - clients[index].registered = 1; SYS_LOG_INF("Registration Done (EP='%s')", clients[index].server_ep); @@ -311,7 +315,6 @@ static int do_update_reply_cb(const struct zoap_packet *response, SYS_LOG_ERR("Failed with code %u.%u. Retrying registration", ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); - clients[index].registered = 0; set_sm_state(index, ENGINE_DO_REGISTRATION); return 0; @@ -336,7 +339,6 @@ static int do_deregister_reply_cb(const struct zoap_packet *response, } if (code == ZOAP_RESPONSE_CODE_DELETED) { - clients[index].registered = 0; SYS_LOG_DBG("Deregistration success"); set_sm_state(index, ENGINE_DEREGISTERED); } else { @@ -361,7 +363,6 @@ static int sm_do_init(int index) clients[index].lifetime); /* Zephyr has joined network already */ clients[index].has_registration_info = 1; - clients[index].registered = 0; clients[index].bootstrapped = 0; clients[index].trigger_update = 0; #if defined(CONFIG_LWM2M_BOOTSTRAP_SERVER) @@ -476,7 +477,6 @@ static int sm_bootstrap_done(int index) SYS_LOG_ERR("Failed to parse URI!"); } else { clients[index].has_registration_info = 1; - clients[index].registered = 0; clients[index].bootstrapped++; } } else { @@ -522,7 +522,7 @@ static int sm_send_registration(int index, bool send_obj_support_data, LWM2M_RD_CLIENT_URI, strlen(LWM2M_RD_CLIENT_URI)); - if (!clients[index].registered) { + if (!sm_is_registered(index)) { /* include client endpoint in URI QUERY on 1st registration */ zoap_add_option_int(&request, ZOAP_OPTION_CONTENT_FORMAT, LWM2M_FORMAT_APP_LINK_FORMAT); @@ -609,7 +609,7 @@ static int sm_do_registration(int index) int ret = 0; if (clients[index].use_registration && - !clients[index].registered && + !sm_is_registered(index) && clients[index].has_registration_info) { ret = sm_send_registration(index, true, do_registration_reply_cb); @@ -629,7 +629,7 @@ static int sm_registration_done(int index) bool forced_update; /* check for lifetime seconds - 1 so that we can update early */ - if (clients[index].registered && + if (sm_is_registered(index) && (clients[index].trigger_update || ((clients[index].lifetime - SECONDS_TO_UPDATE_EARLY) <= (k_uptime_get() - clients[index].last_update) / 1000))) { From d44ed028c81ffb2f595fcc2bfe7869d3b6b2e483 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Fri, 1 Sep 2017 00:27:10 -0700 Subject: [PATCH 09/10] net: lwm2m: remove extra sockaddr values All throughout the LwM2M library we use sockaddr values which are basically the same as the net_app_ctx's remote addr. There's no reason to keep these extra sockaddr values around. The net_app framework client won't accept incoming requests on sockaddr other than the one we're connected to. Signed-off-by: Michael Scott --- subsys/net/lib/lwm2m/lwm2m_engine.c | 48 +++---- subsys/net/lib/lwm2m/lwm2m_engine.h | 9 +- .../net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 9 +- subsys/net/lib/lwm2m/lwm2m_rd_client.c | 134 ++++++------------ 4 files changed, 73 insertions(+), 127 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 18959daf9828e4..c3b8d07322cb28 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -82,7 +82,6 @@ struct observe_node { sys_snode_t node; struct net_app_ctx *net_app_ctx; - struct sockaddr addr; struct lwm2m_obj_path path; u8_t token[8]; s64_t event_timestamp; @@ -180,19 +179,17 @@ int lwm2m_notify_observer_path(struct lwm2m_obj_path *path) } static int engine_add_observer(struct net_app_ctx *app_ctx, - struct sockaddr *addr, const u8_t *token, u8_t tkl, struct lwm2m_obj_path *path, u16_t format) { struct lwm2m_engine_obj_inst *obj_inst = NULL; struct observe_node *obs; + struct sockaddr *addr; int i; - if (!addr) { - SYS_LOG_ERR("sockaddr is required"); - return -EINVAL; - } + /* remote addr */ + addr = &app_ctx->default_ctx->remote; /* check if object exists */ if (!get_engine_obj(path->obj_id)) { @@ -229,7 +226,7 @@ static int engine_add_observer(struct net_app_ctx *app_ctx, /* make sure this observer doesn't exist already */ SYS_SLIST_FOR_EACH_CONTAINER(&engine_observer_list, obs, node) { - if (memcmp(&obs->addr, addr, sizeof(addr)) == 0 && + if (obs->net_app_ctx == app_ctx && memcmp(&obs->path, path, sizeof(*path)) == 0) { /* quietly update the token information */ memcpy(obs->token, token, tkl); @@ -259,7 +256,6 @@ static int engine_add_observer(struct net_app_ctx *app_ctx, /* copy the values and add it to the list */ observe_node_data[i].used = true; observe_node_data[i].net_app_ctx = app_ctx; - memcpy(&observe_node_data[i].addr, addr, sizeof(*addr)); memcpy(&observe_node_data[i].path, path, sizeof(*path)); memcpy(observe_node_data[i].token, token, tkl); observe_node_data[i].tkl = tkl; @@ -624,8 +620,7 @@ int lwm2m_init_message(struct net_app_ctx *app_ctx, struct zoap_packet *zpkt, } struct zoap_pending *lwm2m_init_message_pending(struct lwm2m_ctx *client_ctx, - struct zoap_packet *zpkt, - struct sockaddr *addr) + struct zoap_packet *zpkt) { struct zoap_pending *pending = NULL; int ret; @@ -638,7 +633,8 @@ struct zoap_pending *lwm2m_init_message_pending(struct lwm2m_ctx *client_ctx, return NULL; } - ret = zoap_pending_init(pending, zpkt, addr); + ret = zoap_pending_init(pending, zpkt, + &client_ctx->net_app_ctx.default_ctx->remote); if (ret < 0) { SYS_LOG_ERR("Unable to initialize a pending " "retransmission (err:%d).", ret); @@ -1999,8 +1995,7 @@ static int get_observe_option(const struct zoap_packet *zpkt) static int handle_request(struct net_app_ctx *app_ctx, struct zoap_packet *request, - struct zoap_packet *response, - struct sockaddr *from_addr) + struct zoap_packet *response) { int r; u8_t code; @@ -2142,7 +2137,7 @@ static int handle_request(struct net_app_ctx *app_ctx, r); } - r = engine_add_observer(app_ctx, from_addr, + r = engine_add_observer(app_ctx, token, tkl, &path, accept); if (r < 0) { @@ -2231,19 +2226,17 @@ static int handle_request(struct net_app_ctx *app_ctx, return r; } -int lwm2m_udp_sendto(struct net_app_ctx *app_ctx, struct net_pkt *pkt, - const struct sockaddr *dst_addr) +int lwm2m_udp_sendto(struct net_app_ctx *app_ctx, struct net_pkt *pkt) { - return net_app_send_pkt(app_ctx, pkt, dst_addr, NET_SOCKADDR_MAX_SIZE, - K_NO_WAIT, NULL); + return net_app_send_pkt(app_ctx, pkt, &app_ctx->default_ctx->remote, + NET_SOCKADDR_MAX_SIZE, K_NO_WAIT, NULL); } void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, bool handle_separate_response, int (*udp_request_handler)(struct net_app_ctx *app_ctx, struct zoap_packet *, - struct zoap_packet *, - struct sockaddr *)) + struct zoap_packet *)) { struct net_udp_hdr hdr, *udp_hdr; struct zoap_pending *pending; @@ -2332,13 +2325,12 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, * The "response" here is actually a new request */ r = udp_request_handler(&client_ctx->net_app_ctx, - &response, &response2, - &from_addr); + &response, &response2); if (r < 0) { SYS_LOG_ERR("Request handler error: %d", r); } else { r = lwm2m_udp_sendto(&client_ctx->net_app_ctx, - pkt2, &from_addr); + pkt2); if (r < 0) { SYS_LOG_ERR("Err sending response: %d", r); @@ -2397,7 +2389,7 @@ static void retransmit_request(struct k_work *work) } r = lwm2m_udp_sendto(&client_ctx->net_app_ctx, - pending->pkt, &pending->addr); + pending->pkt); if (r < 0) { return; } @@ -2479,7 +2471,8 @@ 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->addr), + lwm2m_sprint_ip_addr( + &obs->net_app_ctx->default_ctx->remote), k_uptime_get()); obj_inst = get_engine_obj_inst(obs->path.obj_id, @@ -2530,8 +2523,7 @@ static int generate_notify_message(struct observe_node *obs, goto cleanup; } - pending = lwm2m_init_message_pending(client_ctx, out.out_zpkt, - &obs->addr); + pending = lwm2m_init_message_pending(client_ctx, out.out_zpkt); if (!pending) { ret = -ENOMEM; goto cleanup; @@ -2548,7 +2540,7 @@ static int generate_notify_message(struct observe_node *obs, zoap_reply_init(reply, &request); reply->reply = notify_message_reply_cb; - ret = lwm2m_udp_sendto(obs->net_app_ctx, pkt, &obs->addr); + ret = lwm2m_udp_sendto(obs->net_app_ctx, pkt); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); goto cleanup; diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.h b/subsys/net/lib/lwm2m/lwm2m_engine.h index 12189d840c43a2..829977989a608a 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.h +++ b/subsys/net/lib/lwm2m/lwm2m_engine.h @@ -52,8 +52,7 @@ int lwm2m_init_message(struct net_app_ctx *app_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 lwm2m_ctx *client_ctx, - struct zoap_packet *zpkt, - struct sockaddr *addr); + struct zoap_packet *zpkt); void lwm2m_init_message_cleanup(struct net_pkt *pkt, struct zoap_pending *pending, struct zoap_reply *reply); @@ -65,13 +64,11 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst, struct lwm2m_engine_obj_field *obj_field, struct lwm2m_engine_context *context); -int lwm2m_udp_sendto(struct net_app_ctx *app_ctx, struct net_pkt *pkt, - const struct sockaddr *dst_addr); +int lwm2m_udp_sendto(struct net_app_ctx *app_ctx, struct net_pkt *pkt); void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, bool handle_separate_response, int (*udp_request_handler)(struct net_app_ctx *app_ctx, struct zoap_packet *request, - struct zoap_packet *response, - struct sockaddr *from_addr)); + struct zoap_packet *response)); #endif /* LWM2M_ENGINE_H */ diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index aa30a6a474acb4..bb28b919054d94 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -57,8 +57,7 @@ static void retransmit_request(struct k_work *work) return; } - r = lwm2m_udp_sendto(&firmware_ctx.net_app_ctx, - pending->pkt, &pending->addr); + r = lwm2m_udp_sendto(&firmware_ctx.net_app_ctx, pending->pkt); if (r < 0) { return; } @@ -104,8 +103,7 @@ static int transfer_request(struct zoap_block_context *ctx, goto cleanup; } - pending = lwm2m_init_message_pending(&firmware_ctx, &request, - &firmware_addr); + pending = lwm2m_init_message_pending(&firmware_ctx, &request); if (!pending) { ret = -ENOMEM; goto cleanup; @@ -126,8 +124,7 @@ static int transfer_request(struct zoap_block_context *ctx, } /* send request */ - ret = lwm2m_udp_sendto(&firmware_ctx.net_app_ctx, - pkt, &firmware_addr); + ret = lwm2m_udp_sendto(&firmware_ctx.net_app_ctx, pkt); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); diff --git a/subsys/net/lib/lwm2m/lwm2m_rd_client.c b/subsys/net/lib/lwm2m/lwm2m_rd_client.c index 88d18ffde1b9ef..6ea1a7c6275e2a 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rd_client.c +++ b/subsys/net/lib/lwm2m/lwm2m_rd_client.c @@ -94,8 +94,6 @@ enum sm_engine_state { struct lwm2m_rd_client_info { u16_t lifetime; struct lwm2m_ctx *ctx; - struct sockaddr bs_server; - struct sockaddr reg_server; u8_t engine_state; u8_t use_bootstrap; u8_t has_bs_server_info; @@ -140,24 +138,39 @@ static u8_t get_sm_state(int index) return clients[index].engine_state; } -static int find_clients_index(const struct sockaddr *addr, - bool check_bs_server) +static int find_clients_index(const struct sockaddr *addr) { int index = -1, i; + struct sockaddr *remote; for (i = 0; i < client_count; i++) { - if (check_bs_server) { - if (memcmp(addr, &clients[i].bs_server, - sizeof(addr)) == 0) { + remote = &clients[i].ctx->net_app_ctx.default_ctx->remote; + if (clients[i].ctx) { + if (remote->sa_family != addr->sa_family) { + continue; + } + +#if defined(CONFIG_NET_IPV6) + if (remote->sa_family == AF_INET6 && + net_ipv6_addr_cmp(&net_sin6(remote)->sin6_addr, + &net_sin6(addr)->sin6_addr) && + net_sin6(remote)->sin6_port == + net_sin6(addr)->sin6_port) { index = i; break; } - } else { - if (memcmp(addr, &clients[i].reg_server, - sizeof(addr)) == 0) { +#endif + +#if defined(CONFIG_NET_IPV4) + if (remote->sa_family == AF_INET && + net_ipv4_addr_cmp(&net_sin(remote)->sin_addr, + &net_sin(addr)->sin_addr) && + net_sin(remote)->sin_port == + net_sin(addr)->sin_port) { index = i; break; } +#endif } } @@ -189,7 +202,7 @@ static int do_bootstrap_reply_cb(const struct zoap_packet *response, ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); - index = find_clients_index(from, true); + index = find_clients_index(from); if (index < 0) { SYS_LOG_ERR("Bootstrap client index not found."); return 0; @@ -228,7 +241,7 @@ static int do_registration_reply_cb(const struct zoap_packet *response, ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); - index = find_clients_index(from, false); + index = find_clients_index(from); if (index < 0) { SYS_LOG_ERR("Registration client index not found."); return 0; @@ -296,7 +309,7 @@ static int do_update_reply_cb(const struct zoap_packet *response, ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); - index = find_clients_index(from, false); + index = find_clients_index(from); if (index < 0) { SYS_LOG_ERR("Registration client index not found."); return 0; @@ -332,7 +345,7 @@ static int do_deregister_reply_cb(const struct zoap_packet *response, ZOAP_RESPONSE_CODE_CLASS(code), ZOAP_RESPONSE_CODE_DETAIL(code)); - index = find_clients_index(from, false); + index = find_clients_index(from); if (index < 0) { SYS_LOG_ERR("Registration clients index not found."); return 0; @@ -389,13 +402,14 @@ static int sm_do_bootstrap(int index) struct net_pkt *pkt = NULL; struct zoap_pending *pending = NULL; struct zoap_reply *reply = NULL; + struct net_app_ctx *app_ctx = NULL; int ret = 0; if (clients[index].use_bootstrap && clients[index].bootstrapped == 0 && clients[index].has_bs_server_info) { - - ret = lwm2m_init_message(&clients[index].ctx->net_app_ctx, + app_ctx = &clients[index].ctx->net_app_ctx; + ret = lwm2m_init_message(app_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_POST, 0, NULL, 0); if (ret) { @@ -411,8 +425,7 @@ static int sm_do_bootstrap(int index) query_buffer, strlen(query_buffer)); pending = lwm2m_init_message_pending(clients[index].ctx, - &request, - &clients[index].bs_server); + &request); if (!pending) { ret = -ENOMEM; goto cleanup; @@ -431,11 +444,11 @@ static int sm_do_bootstrap(int index) /* log the bootstrap attempt */ SYS_LOG_DBG("Register ID with bootstrap server [%s] as '%s'", - lwm2m_sprint_ip_addr(&clients[index].bs_server), + lwm2m_sprint_ip_addr( + &app_ctx->default_ctx->remote), query_buffer); - ret = lwm2m_udp_sendto(&clients[index].ctx->net_app_ctx, - pkt, &clients[index].bs_server); + ret = lwm2m_udp_sendto(app_ctx, pkt); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); @@ -501,6 +514,7 @@ static int sm_bootstrap_done(int index) static int sm_send_registration(int index, bool send_obj_support_data, zoap_reply_t reply_cb) { + struct net_app_ctx *app_ctx = NULL; struct zoap_packet request; struct net_pkt *pkt = NULL; struct zoap_pending *pending = NULL; @@ -509,9 +523,11 @@ static int sm_send_registration(int index, bool send_obj_support_data, u16_t client_data_len, len; int ret = 0; + app_ctx = &clients[index].ctx->net_app_ctx; + /* remember the last reg time */ clients[index].last_update = k_uptime_get(); - ret = lwm2m_init_message(&clients[index].ctx->net_app_ctx, + ret = lwm2m_init_message(app_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_POST, 0, NULL, 0); if (ret) { @@ -564,8 +580,7 @@ static int sm_send_registration(int index, bool send_obj_support_data, } } - pending = lwm2m_init_message_pending(clients[index].ctx, &request, - &clients[index].reg_server); + pending = lwm2m_init_message_pending(clients[index].ctx, &request); if (!pending) { ret = -ENOMEM; goto cleanup; @@ -584,10 +599,9 @@ static int sm_send_registration(int index, bool send_obj_support_data, /* log the registration attempt */ SYS_LOG_DBG("registration sent [%s]", - lwm2m_sprint_ip_addr(&clients[index].reg_server)); + lwm2m_sprint_ip_addr(&app_ctx->default_ctx->remote)); - ret = lwm2m_udp_sendto(&clients[index].ctx->net_app_ctx, - pkt, &clients[index].reg_server); + ret = lwm2m_udp_sendto(app_ctx, pkt); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); @@ -649,13 +663,16 @@ static int sm_registration_done(int index) static int sm_do_deregister(int index) { + struct net_app_ctx *app_ctx = NULL; struct zoap_packet request; struct net_pkt *pkt = NULL; struct zoap_pending *pending = NULL; struct zoap_reply *reply = NULL; int ret; - ret = lwm2m_init_message(&clients[index].ctx->net_app_ctx, + app_ctx = &clients[index].ctx->net_app_ctx; + + ret = lwm2m_init_message(app_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_DELETE, 0, NULL, 0); if (ret) { @@ -666,8 +683,7 @@ static int sm_do_deregister(int index) clients[index].server_ep, strlen(clients[index].server_ep)); - pending = lwm2m_init_message_pending(clients[index].ctx, &request, - &clients[index].reg_server); + pending = lwm2m_init_message_pending(clients[index].ctx, &request); if (!pending) { ret = -ENOMEM; goto cleanup; @@ -686,8 +702,7 @@ static int sm_do_deregister(int index) SYS_LOG_INF("Deregister from '%s'", clients[index].server_ep); - ret = lwm2m_udp_sendto(&clients[index].ctx->net_app_ctx, - pkt, &clients[index].reg_server); + ret = lwm2m_udp_sendto(app_ctx, pkt); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); @@ -775,49 +790,6 @@ static void lwm2m_rd_client_service(void) } } -static bool peer_addr_exist(struct sockaddr *peer_addr) -{ - bool ret = false; - int i; - - /* look for duplicate peer_addr */ - for (i = 0; i < client_count; i++) { -#if defined(CONFIG_NET_IPV6) - if (peer_addr->sa_family == AF_INET6 && net_ipv6_addr_cmp( - &net_sin6(&clients[i].bs_server)->sin6_addr, - &net_sin6(peer_addr)->sin6_addr)) { - ret = true; - break; - } - - if (peer_addr->sa_family == AF_INET6 && net_ipv6_addr_cmp( - &net_sin6(&clients[i].reg_server)->sin6_addr, - &net_sin6(peer_addr)->sin6_addr)) { - ret = true; - break; - } -#endif - -#if defined(CONFIG_NET_IPV4) - if (peer_addr->sa_family == AF_INET && net_ipv4_addr_cmp( - &net_sin(&clients[i].bs_server)->sin_addr, - &net_sin(peer_addr)->sin_addr)) { - ret = true; - break; - } - - if (peer_addr->sa_family == AF_INET && net_ipv4_addr_cmp( - &net_sin(&clients[i].reg_server)->sin_addr, - &net_sin(peer_addr)->sin_addr)) { - ret = true; - break; - } -#endif - } - - return ret; -} - int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, char *peer_str, u16_t peer_port, const char *ep_name) @@ -839,22 +811,10 @@ int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx, return -EINVAL; } - if (peer_addr_exist(&client_ctx->net_app_ctx.default_ctx->remote)) { - SYS_LOG_ERR("Remote exists already"); - ret = -EEXIST; - goto cleanup; - } - /* TODO: use server URI data from security */ index = client_count; client_count++; clients[index].ctx = client_ctx; - memcpy(&clients[index].reg_server, - &client_ctx->net_app_ctx.default_ctx->remote, - sizeof(struct sockaddr)); - memcpy(&clients[index].bs_server, - &client_ctx->net_app_ctx.default_ctx->remote, - sizeof(struct sockaddr)); set_sm_state(index, ENGINE_INIT); strncpy(clients[index].ep_name, ep_name, CLIENT_EP_LEN - 1); SYS_LOG_INF("LWM2M Client: %s", clients[index].ep_name); From 644e55731cf9c310a8c4a06a56a729ee856bdbfa Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Fri, 1 Sep 2017 00:42:05 -0700 Subject: [PATCH 10/10] net: lwm2m: save lwm2m_ctx instead of net_app_ctx in observer This is the first part of a large refactoring of LwM2M library message functions and will simplify observer handling later. Signed-off-by: Michael Scott --- subsys/net/lib/lwm2m/lwm2m_engine.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index c3b8d07322cb28..f7514a3e1ac6aa 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -81,7 +81,7 @@ struct observe_node { sys_snode_t node; - struct net_app_ctx *net_app_ctx; + struct lwm2m_ctx *ctx; struct lwm2m_obj_path path; u8_t token[8]; s64_t event_timestamp; @@ -226,7 +226,7 @@ static int engine_add_observer(struct net_app_ctx *app_ctx, /* make sure this observer doesn't exist already */ SYS_SLIST_FOR_EACH_CONTAINER(&engine_observer_list, obs, node) { - if (obs->net_app_ctx == app_ctx && + if (&obs->ctx->net_app_ctx == app_ctx && memcmp(&obs->path, path, sizeof(*path)) == 0) { /* quietly update the token information */ memcpy(obs->token, token, tkl); @@ -255,7 +255,8 @@ static int engine_add_observer(struct net_app_ctx *app_ctx, /* copy the values and add it to the list */ observe_node_data[i].used = true; - observe_node_data[i].net_app_ctx = app_ctx; + observe_node_data[i].ctx = CONTAINER_OF(app_ctx, + struct lwm2m_ctx, net_app_ctx); memcpy(&observe_node_data[i].path, path, sizeof(*path)); memcpy(observe_node_data[i].token, token, tkl); observe_node_data[i].tkl = tkl; @@ -2444,12 +2445,9 @@ static int generate_notify_message(struct observe_node *obs, struct lwm2m_output_context out; struct lwm2m_engine_context context; struct lwm2m_obj_path path; - struct lwm2m_ctx *client_ctx; int ret = 0; - client_ctx = CONTAINER_OF(obs->net_app_ctx, - struct lwm2m_ctx, net_app_ctx); - if (!client_ctx) { + if (!obs->ctx) { SYS_LOG_ERR("observer has no valid LwM2M ctx!"); return -EINVAL; } @@ -2472,7 +2470,7 @@ static int generate_notify_message(struct observe_node *obs, obs->path.level, sprint_token(obs->token, obs->tkl), lwm2m_sprint_ip_addr( - &obs->net_app_ctx->default_ctx->remote), + &obs->ctx->net_app_ctx.default_ctx->remote), k_uptime_get()); obj_inst = get_engine_obj_inst(obs->path.obj_id, @@ -2484,7 +2482,7 @@ static int generate_notify_message(struct observe_node *obs, return -EINVAL; } - ret = lwm2m_init_message(obs->net_app_ctx, out.out_zpkt, &pkt, + ret = lwm2m_init_message(&obs->ctx->net_app_ctx, out.out_zpkt, &pkt, ZOAP_TYPE_CON, ZOAP_RESPONSE_CODE_CONTENT, 0, obs->token, obs->tkl); if (ret) { @@ -2523,13 +2521,13 @@ static int generate_notify_message(struct observe_node *obs, goto cleanup; } - pending = lwm2m_init_message_pending(client_ctx, out.out_zpkt); + pending = lwm2m_init_message_pending(obs->ctx, out.out_zpkt); if (!pending) { ret = -ENOMEM; goto cleanup; } - reply = zoap_reply_next_unused(client_ctx->replies, + reply = zoap_reply_next_unused(obs->ctx->replies, CONFIG_LWM2M_ENGINE_MAX_REPLIES); if (!reply) { SYS_LOG_ERR("No resources for waiting for replies."); @@ -2540,7 +2538,7 @@ static int generate_notify_message(struct observe_node *obs, zoap_reply_init(reply, &request); reply->reply = notify_message_reply_cb; - ret = lwm2m_udp_sendto(obs->net_app_ctx, pkt); + ret = lwm2m_udp_sendto(&obs->ctx->net_app_ctx, pkt); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); goto cleanup; @@ -2548,7 +2546,7 @@ static int generate_notify_message(struct observe_node *obs, SYS_LOG_DBG("NOTIFY MSG: SENT"); zoap_pending_cycle(pending); - k_delayed_work_submit(&client_ctx->retransmit_work, pending->timeout); + k_delayed_work_submit(&obs->ctx->retransmit_work, pending->timeout); return ret; cleanup: