From 728ab4229a7854d63705e137b81035385bb4459c Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Wed, 23 Aug 2017 22:33:38 -0700 Subject: [PATCH] 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 1f99380f9d4..d700b093bb9 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 650ac11b764..cfe4eddff5e 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 1737ccb125f..e93545a4f77 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 3a8e29e057a..39a31410520 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 91e0bb2d0db..d7ee6244967 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);