Skip to content

Commit

Permalink
net: lwm2m: add RD client callbacks for app
Browse files Browse the repository at this point in the history
Applications may want to be notified when various events
happen in the LwM2M rd client.  Let's implement an event
callback which sends: connect, disconnect and update events.

Signed-off-by: Michael Scott <michael.scott@linaro.org>
  • Loading branch information
Michael Scott committed Oct 17, 2017
1 parent c7eb9d2 commit 0a21766
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 6 deletions.
20 changes: 19 additions & 1 deletion include/net/lwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,26 @@ int lwm2m_engine_start(struct lwm2m_ctx *client_ctx,

/* LWM2M RD Client */

/* Client events */
enum lwm2m_rd_client_event {
LWM2M_RD_CLIENT_EVENT_NONE,
LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_FAILURE,
LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_COMPLETE,
LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE,
LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE,
LWM2M_RD_CLIENT_EVENT_REG_UPDATE_FAILURE,
LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE,
LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE,
LWM2M_RD_CLIENT_EVENT_DISCONNECT
};

/* Event callback */
typedef void (*lwm2m_ctx_event_cb_t)(struct lwm2m_ctx *ctx,
enum lwm2m_rd_client_event event);

int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx,
char *peer_str, u16_t peer_port,
const char *ep_name);
const char *ep_name,
lwm2m_ctx_event_cb_t event_cb);

#endif /* __LWM2M_H__ */
50 changes: 48 additions & 2 deletions samples/net/lwm2m_client/src/lwm2m-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,50 @@ static int lwm2m_setup(void)
return 0;
}

static void rd_client_event(struct lwm2m_ctx *client,
enum lwm2m_rd_client_event client_event)
{
switch (client_event) {

case LWM2M_RD_CLIENT_EVENT_NONE:
/* do nothing */
break;

case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_FAILURE:
SYS_LOG_DBG("Bootstrap failure!");
break;

case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_COMPLETE:
SYS_LOG_DBG("Bootstrap complete");
break;

case LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE:
SYS_LOG_DBG("Registration failure!");
break;

case LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE:
SYS_LOG_DBG("Registration complete");
break;

case LWM2M_RD_CLIENT_EVENT_REG_UPDATE_FAILURE:
SYS_LOG_DBG("Registration update failure!");
break;

case LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE:
SYS_LOG_DBG("Registration update complete");
break;

case LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE:
SYS_LOG_DBG("Deregister failure!");
break;

case LWM2M_RD_CLIENT_EVENT_DISCONNECT:
SYS_LOG_DBG("Disconnected");
break;

}
}

void main(void)
{
int ret;
Expand All @@ -265,10 +309,12 @@ void main(void)

#if defined(CONFIG_NET_IPV6)
ret = lwm2m_rd_client_start(&client, CONFIG_NET_APP_PEER_IPV6_ADDR,
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD);
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD,
rd_client_event);
#elif defined(CONFIG_NET_IPV4)
ret = lwm2m_rd_client_start(&client, CONFIG_NET_APP_PEER_IPV4_ADDR,
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD);
CONFIG_LWM2M_PEER_PORT, CONFIG_BOARD,
rd_client_event);
#else
SYS_LOG_ERR("LwM2M client requires IPv4 or IPv6.");
ret = -EPROTONOSUPPORT;
Expand Down
48 changes: 45 additions & 3 deletions subsys/net/lib/lwm2m/lwm2m_rd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ struct lwm2m_rd_client_info {

char ep_name[CLIENT_EP_LEN];
char server_ep[CLIENT_EP_LEN];

lwm2m_ctx_event_cb_t event_cb;
};

static K_THREAD_STACK_DEFINE(lwm2m_rd_client_thread_stack,
Expand All @@ -120,10 +122,31 @@ static u8_t client_data[256]; /* allocate some data for the RD */
static struct lwm2m_rd_client_info clients[CLIENT_INSTANCE_COUNT];
static int client_count;

static void set_sm_state(int index, u8_t state)
static void set_sm_state(int index, u8_t sm_state)
{
enum lwm2m_rd_client_event event = LWM2M_RD_CLIENT_EVENT_NONE;

/* Determine if a callback to the app is needed */
if (sm_state == ENGINE_BOOTSTRAP_DONE) {
event = LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_COMPLETE;
} else if (clients[index].engine_state == ENGINE_UPDATE_SENT &&
sm_state == ENGINE_REGISTRATION_DONE) {
event = LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE;
} else if (sm_state == ENGINE_REGISTRATION_DONE) {
event = LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE;
} else if ((sm_state == ENGINE_INIT ||
sm_state == ENGINE_DEREGISTERED) &&
(clients[index].engine_state > ENGINE_BOOTSTRAP_DONE &&
clients[index].engine_state < ENGINE_DEREGISTER)) {
event = LWM2M_RD_CLIENT_EVENT_DISCONNECT;
}

/* TODO: add locking? */
clients[index].engine_state = state;
clients[index].engine_state = sm_state;

if (event > LWM2M_RD_CLIENT_EVENT_NONE && clients[index].event_cb) {
clients[index].event_cb(clients[index].ctx, event);
}
}

static bool sm_is_registered(int index)
Expand Down Expand Up @@ -200,14 +223,31 @@ static void sm_handle_timeout_state(struct lwm2m_message *msg,
enum sm_engine_state sm_state)
{
int index;
enum lwm2m_rd_client_event event = LWM2M_RD_CLIENT_EVENT_NONE;

index = find_rd_client_from_msg(msg, clients, CLIENT_INSTANCE_COUNT);
if (index < 0) {
SYS_LOG_ERR("Can't find RD client from msg: %p!", msg);
return;
}

if (clients[index].engine_state == ENGINE_BOOTSTRAP_SENT) {
event = LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_FAILURE;
} else if (clients[index].engine_state == ENGINE_REGISTRATION_SENT) {
event = LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE;
} else if (clients[index].engine_state == ENGINE_UPDATE_SENT) {
event = LWM2M_RD_CLIENT_EVENT_REG_UPDATE_FAILURE;
} else if (clients[index].engine_state == ENGINE_DEREGISTER_SENT) {
event = LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE;
} else {
/* TODO: unknown timeout state */
}

set_sm_state(index, sm_state);

if (event > LWM2M_RD_CLIENT_EVENT_NONE && clients[index].event_cb) {
clients[index].event_cb(clients[index].ctx, event);
}
}

/* force re-update with remote peer(s) */
Expand Down Expand Up @@ -820,7 +860,8 @@ static void lwm2m_rd_client_service(void)

int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx,
char *peer_str, u16_t peer_port,
const char *ep_name)
const char *ep_name,
lwm2m_ctx_event_cb_t event_cb)
{
int index, ret = 0;

Expand All @@ -843,6 +884,7 @@ int lwm2m_rd_client_start(struct lwm2m_ctx *client_ctx,
index = client_count;
client_count++;
clients[index].ctx = client_ctx;
clients[index].event_cb = event_cb;
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);
Expand Down

0 comments on commit 0a21766

Please sign in to comment.