net: lwm2m: add timeout callbacks to registration client

Callbacks are setup for the following states:
- ENGINE_DO_BOOTSTRAP
- ENGINE_DO_REGISTRATION (first registration)
- ENGINE_REGISTRATION_DONE (subsequent client updates)
- ENGINE_DEREGISTER

In most cases, if a timeout occurs the registration engine goes back to
ENGINE_INIT.  The exception is a timeout during client update, which
forces the state machine back to ENGINE_DO_REGISTRATION (skipping a
boostrap).

Signed-off-by: Michael Scott <michael.scott@linaro.org>
This commit is contained in:
Michael Scott 2017-09-01 01:22:30 -07:00 committed by Jukka Rissanen
parent 897dbffe7c
commit b3e58c7874

View file

@ -177,6 +177,39 @@ static int find_clients_index(const struct sockaddr *addr)
return index;
}
static int find_rd_client_from_msg(struct lwm2m_message *msg,
struct lwm2m_rd_client_info *rd_clients,
size_t len)
{
size_t i;
if (!msg || !rd_clients) {
return -1;
}
for (i = 0; i < len; i++) {
if (rd_clients[i].ctx && rd_clients[i].ctx == msg->ctx) {
return i;
}
}
return -1;
}
static void sm_handle_timeout_state(struct lwm2m_message *msg,
enum sm_engine_state sm_state)
{
int index;
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;
}
set_sm_state(index, sm_state);
}
/* force re-update with remote peer(s) */
void engine_trigger_update(void)
{
@ -228,6 +261,14 @@ static int do_bootstrap_reply_cb(const struct zoap_packet *response,
return 0;
}
static void do_bootstrap_timeout_cb(struct lwm2m_message *msg)
{
SYS_LOG_WRN("Bootstrap Timeout");
/* Restart from scratch */
sm_handle_timeout_state(msg, ENGINE_INIT);
}
static int do_registration_reply_cb(const struct zoap_packet *response,
struct zoap_reply *reply,
const struct sockaddr *from)
@ -297,6 +338,14 @@ static int do_registration_reply_cb(const struct zoap_packet *response,
return 0;
}
static void do_registration_timeout_cb(struct lwm2m_message *msg)
{
SYS_LOG_WRN("Registration Timeout");
/* Restart from scratch */
sm_handle_timeout_state(msg, ENGINE_INIT);
}
static int do_update_reply_cb(const struct zoap_packet *response,
struct zoap_reply *reply,
const struct sockaddr *from)
@ -333,6 +382,14 @@ static int do_update_reply_cb(const struct zoap_packet *response,
return 0;
}
static void do_update_timeout_cb(struct lwm2m_message *msg)
{
SYS_LOG_WRN("Registration Update Timeout");
/* Re-do registration */
sm_handle_timeout_state(msg, ENGINE_DO_REGISTRATION);
}
static int do_deregister_reply_cb(const struct zoap_packet *response,
struct zoap_reply *reply,
const struct sockaddr *from)
@ -366,6 +423,14 @@ static int do_deregister_reply_cb(const struct zoap_packet *response,
return 0;
}
static void do_deregister_timeout_cb(struct lwm2m_message *msg)
{
SYS_LOG_WRN("De-Registration Timeout");
/* Abort de-registration and start from scratch */
sm_handle_timeout_state(msg, ENGINE_INIT);
}
/* state machine step functions */
static int sm_do_init(int index)
@ -416,6 +481,7 @@ static int sm_do_bootstrap(int index)
msg->code = ZOAP_METHOD_POST;
msg->mid = 0;
msg->reply_cb = do_bootstrap_reply_cb;
msg->message_timeout_cb = do_bootstrap_timeout_cb;
ret = lwm2m_init_message(msg);
if (ret) {
@ -599,7 +665,8 @@ static int sm_do_registration(int index)
!sm_is_registered(index) &&
clients[index].has_registration_info) {
ret = sm_send_registration(index, true,
do_registration_reply_cb, NULL);
do_registration_reply_cb,
do_registration_timeout_cb);
if (!ret) {
set_sm_state(index, ENGINE_REGISTRATION_SENT);
} else {
@ -623,7 +690,8 @@ static int sm_registration_done(int index)
forced_update = clients[index].trigger_update;
clients[index].trigger_update = 0;
ret = sm_send_registration(index, forced_update,
do_update_reply_cb, NULL);
do_update_reply_cb,
do_update_timeout_cb);
if (!ret) {
set_sm_state(index, ENGINE_UPDATE_SENT);
} else {
@ -651,6 +719,7 @@ static int sm_do_deregister(int index)
msg->code = ZOAP_METHOD_DELETE;
msg->mid = 0;
msg->reply_cb = do_deregister_reply_cb;
msg->message_timeout_cb = do_deregister_timeout_cb;
ret = lwm2m_init_message(msg);
if (ret) {
@ -695,7 +764,7 @@ static void lwm2m_rd_client_service(void)
break;
case ENGINE_BOOTSTRAP_SENT:
/* wait for bootstrap to be done */
/* wait for bootstrap to be done or timeout */
break;
case ENGINE_BOOTSTRAP_DONE:
@ -707,7 +776,7 @@ static void lwm2m_rd_client_service(void)
break;
case ENGINE_REGISTRATION_SENT:
/* wait registration to be done */
/* wait registration to be done or timeout */
break;
case ENGINE_REGISTRATION_DONE:
@ -715,7 +784,7 @@ static void lwm2m_rd_client_service(void)
break;
case ENGINE_UPDATE_SENT:
/* wait update to be done */
/* wait update to be done or abort */
break;
case ENGINE_DEREGISTER:
@ -723,6 +792,7 @@ static void lwm2m_rd_client_service(void)
break;
case ENGINE_DEREGISTER_SENT:
/* wait for deregister to be done or reset */
break;
case ENGINE_DEREGISTER_FAILED: