Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LwM2M 1.10 changes: LwM2M library use net_app API internally #1473

Merged
merged 10 commits into from
Sep 18, 2017
44 changes: 40 additions & 4 deletions include/net/lwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#ifndef __LWM2M_H__
#define __LWM2M_H__

#include <net/net_context.h>
#include <net/net_app.h>
#include <net/zoap.h>

/* LWM2M Objects defined by OMA */

Expand All @@ -25,6 +26,35 @@
#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 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];
struct zoap_reply replies[CONFIG_LWM2M_ENGINE_MAX_REPLIES];
struct k_delayed_work retransmit_work;
};

/* 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);
Expand Down Expand Up @@ -149,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 net_context *net_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 net_context *net_ctx,
struct sockaddr *peer_addr,
int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx,
char *peer_str, u16_t peer_port,
const char *ep_name);

#endif /* __LWM2M_H__ */
127 changes: 19 additions & 108 deletions samples/net/lwm2m_client/src/lwm2m-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
#include <board.h>
#include <zephyr.h>
#include <gpio.h>
#include <net/net_core.h>
#include <net/net_pkt.h>
#include <net/net_app.h>
#include <net/lwm2m.h>

#define APP_BANNER "Run LWM2M client"
Expand Down Expand Up @@ -56,12 +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 udp6;
#endif
#if defined(CONFIG_NET_IPV4)
static struct net_app_ctx udp4;
#endif
static struct lwm2m_ctx client;
static struct k_sem quit_lock;

#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
Expand Down Expand Up @@ -170,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;
Expand Down Expand Up @@ -250,28 +227,9 @@ static int lwm2m_setup(void)
return 0;
}

int setup_net_app_ctx(struct net_app_ctx *ctx, const char *peer)
{
int ret;

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);

Expand All @@ -283,76 +241,29 @@ void main(void)
return;
}

#if defined(CONFIG_NET_IPV6)
ret = setup_net_app_ctx(&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;
}


ret = lwm2m_engine_start(udp6.ipv6.ctx);
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);
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, 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;
}

ret = lwm2m_engine_start(udp4.ipv4.ctx);
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);
#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(&udp4);
net_app_release(&udp4);
#endif

#if defined(CONFIG_NET_IPV6)
cleanup_ipv6:
net_app_close(&udp6);
net_app_release(&udp6);
#endif
}
Loading