2017-10-25 00:20:05 +00:00
|
|
|
/*
|
2017-12-21 17:54:28 +00:00
|
|
|
* Copyright (c) 2015-2018, Yanzi Networks AB.
|
2017-10-25 00:20:05 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-12-21 17:54:28 +00:00
|
|
|
* \addtogroup lwm2m
|
2017-10-25 00:20:05 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Implementation of the Contiki OMA LWM2M engine
|
|
|
|
* Registration and bootstrap client
|
|
|
|
* \author
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
* Joel Hoglund <joel@sics.se>
|
2018-03-13 08:34:11 +00:00
|
|
|
* Carlos Gonzalo Peces <carlosgp143@gmail.com>
|
2017-10-25 00:20:05 +00:00
|
|
|
*/
|
|
|
|
#include "lwm2m-engine.h"
|
|
|
|
#include "lwm2m-object.h"
|
|
|
|
#include "lwm2m-device.h"
|
|
|
|
#include "lwm2m-plain-text.h"
|
|
|
|
#include "lwm2m-json.h"
|
|
|
|
#include "lwm2m-rd-client.h"
|
|
|
|
#include "coap.h"
|
|
|
|
#include "coap-engine.h"
|
|
|
|
#include "coap-endpoint.h"
|
|
|
|
#include "coap-callback-api.h"
|
|
|
|
#include "lwm2m-security.h"
|
2018-04-30 11:04:23 +00:00
|
|
|
#include "lib/list.h"
|
2017-10-25 00:20:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#if UIP_CONF_IPV6_RPL
|
|
|
|
#include "rpl.h"
|
|
|
|
#endif /* UIP_CONF_IPV6_RPL */
|
|
|
|
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
|
|
|
#include "lwm2m-queue-mode.h"
|
2018-04-06 13:16:42 +00:00
|
|
|
#include "lwm2m-notification-queue.h"
|
2018-05-18 16:22:14 +00:00
|
|
|
#endif /* LWM2M_QUEUE_MODE_ENABLED */
|
2018-04-06 13:16:42 +00:00
|
|
|
|
2017-12-01 01:58:44 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
2018-05-04 09:02:25 +00:00
|
|
|
#define LOG_MODULE "lwm2m-rd"
|
2017-12-01 01:58:44 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_LWM2M
|
2017-10-25 00:20:05 +00:00
|
|
|
|
|
|
|
#ifndef LWM2M_DEFAULT_CLIENT_LIFETIME
|
2017-12-08 12:20:34 +00:00
|
|
|
#define LWM2M_DEFAULT_CLIENT_LIFETIME 30 /* sec */
|
2017-10-25 00:20:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT)
|
|
|
|
#define BS_REMOTE_PORT UIP_HTONS(5685)
|
|
|
|
|
|
|
|
#define STATE_MACHINE_UPDATE_INTERVAL 500
|
|
|
|
|
|
|
|
/* The states for the RD client state machine */
|
|
|
|
/* When node is unregistered it ends up in UNREGISTERED
|
|
|
|
and this is going to be there until use X or Y kicks it
|
|
|
|
back into INIT again */
|
|
|
|
#define INIT 0
|
|
|
|
#define WAIT_NETWORK 1
|
|
|
|
#define DO_BOOTSTRAP 3
|
|
|
|
#define BOOTSTRAP_SENT 4
|
|
|
|
#define BOOTSTRAP_DONE 5
|
|
|
|
#define DO_REGISTRATION 6
|
|
|
|
#define REGISTRATION_SENT 7
|
|
|
|
#define REGISTRATION_DONE 8
|
|
|
|
#define UPDATE_SENT 9
|
|
|
|
#define DEREGISTER 10
|
|
|
|
#define DEREGISTER_SENT 11
|
|
|
|
#define DEREGISTER_FAILED 12
|
|
|
|
#define DEREGISTERED 13
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
|
|
|
#define QUEUE_MODE_AWAKE 14
|
|
|
|
#define QUEUE_MODE_SEND_UPDATE 15
|
2018-03-13 08:34:11 +00:00
|
|
|
#endif
|
2017-10-25 00:20:05 +00:00
|
|
|
|
|
|
|
#define FLAG_RD_DATA_DIRTY 0x01
|
|
|
|
#define FLAG_RD_DATA_UPDATE_TRIGGERED 0x02
|
|
|
|
#define FLAG_RD_DATA_UPDATE_ON_DIRTY 0x10
|
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
LIST(session_info_list);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
/* Shared by all sessions, used by only one at a time in the FSM */
|
2017-10-25 00:20:05 +00:00
|
|
|
static char query_data[64]; /* allocate some data for queries and updates */
|
|
|
|
static uint8_t rd_data[128]; /* allocate some data for the RD */
|
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
static coap_timer_t rd_timer; /* Timer to tick the FSM periodically */
|
2018-09-04 08:58:31 +00:00
|
|
|
static char default_ep[20];
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
2018-04-30 11:04:23 +00:00
|
|
|
static coap_timer_t queue_mode_client_awake_timer; /* Timer to control the client's
|
|
|
|
* awake time
|
|
|
|
*/
|
|
|
|
static uint8_t queue_mode_client_awake; /* 1 - client is awake,
|
|
|
|
* 0 - client is sleeping
|
|
|
|
*/
|
2018-05-18 16:22:14 +00:00
|
|
|
static uint16_t queue_mode_client_awake_time; /* The time to be awake */
|
2018-04-06 13:16:42 +00:00
|
|
|
/* Callback for the client awake timer */
|
2018-04-30 11:04:23 +00:00
|
|
|
static void queue_mode_awake_timer_callback(coap_timer_t *timer);
|
2018-03-13 08:34:11 +00:00
|
|
|
#endif
|
|
|
|
|
2017-10-25 00:20:05 +00:00
|
|
|
static void check_periodic_observations();
|
2018-05-31 14:22:01 +00:00
|
|
|
static void update_callback(coap_callback_request_state_t *callback_state);
|
2018-09-04 08:58:31 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-25 00:20:05 +00:00
|
|
|
static int
|
2018-04-30 11:04:23 +00:00
|
|
|
set_rd_data(lwm2m_session_info_t *session_info)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
|
|
|
lwm2m_buffer_t outbuf;
|
|
|
|
|
|
|
|
/* setup the output buffer */
|
|
|
|
outbuf.buffer = rd_data;
|
|
|
|
outbuf.size = sizeof(rd_data);
|
|
|
|
outbuf.len = 0;
|
|
|
|
|
|
|
|
/* this will also set the request payload */
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_more = lwm2m_engine_set_rd_data(&outbuf, 0);
|
|
|
|
coap_set_payload(session_info->request, rd_data, outbuf.len);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
if(session_info->rd_more) {
|
2017-10-25 00:20:05 +00:00
|
|
|
/* set the first block here */
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("Setting block1 in request\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
coap_set_header_block1(session_info->request, 0, 1, sizeof(rd_data));
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
return outbuf.len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2018-04-30 11:04:23 +00:00
|
|
|
prepare_update(lwm2m_session_info_t *session_info, int triggered)
|
2018-03-13 08:34:11 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
coap_init_message(session_info->request, COAP_TYPE_CON, COAP_POST, 0);
|
|
|
|
coap_set_header_uri_path(session_info->request, session_info->assigned_ep);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
snprintf(query_data, sizeof(query_data) - 1, "?lt=%d&b=%s", session_info->lifetime, session_info->binding);
|
|
|
|
LOG_DBG("UPDATE:%s %s\n", session_info->assigned_ep, query_data);
|
|
|
|
coap_set_header_uri_query(session_info->request, query_data);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
if((triggered || session_info->rd_flags & FLAG_RD_DATA_UPDATE_ON_DIRTY) && (session_info->rd_flags & FLAG_RD_DATA_DIRTY)) {
|
|
|
|
session_info->rd_flags &= ~FLAG_RD_DATA_DIRTY;
|
|
|
|
set_rd_data(session_info);
|
|
|
|
session_info->rd_callback = update_callback;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
has_network_access(void)
|
|
|
|
{
|
|
|
|
#if UIP_CONF_IPV6_RPL
|
2017-11-29 21:44:41 +00:00
|
|
|
/* NATIVE PLATFORM is not really running RPL */
|
|
|
|
#ifndef CONTIKI_TARGET_NATIVE
|
2017-10-25 00:20:05 +00:00
|
|
|
if(rpl_get_any_dag() == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-29 21:44:41 +00:00
|
|
|
#endif
|
2017-10-25 00:20:05 +00:00
|
|
|
#endif /* UIP_CONF_IPV6_RPL */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_is_registered(lwm2m_session_info_t *session_info)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
return session_info->rd_state == REGISTRATION_DONE || session_info->rd_state == UPDATE_SENT;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* will take another argument when we support multiple sessions */
|
|
|
|
void
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_set_session_callback(lwm2m_session_info_t *session_info, session_callback_t cb)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->callback = cb;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2018-04-30 11:04:23 +00:00
|
|
|
perform_session_callback(lwm2m_session_info_t *session_info, int state)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
if(session_info->callback != NULL) {
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("Performing session callback: %d cb:%p\n",
|
2018-04-30 11:04:23 +00:00
|
|
|
state, session_info->callback);
|
|
|
|
session_info->callback(session_info, state);
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-04-30 11:04:23 +00:00
|
|
|
uint16_t
|
|
|
|
lwm2m_rd_client_get_lifetime(lwm2m_session_info_t *session_info)
|
|
|
|
{
|
|
|
|
return session_info->lifetime;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-25 00:20:05 +00:00
|
|
|
void
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_set_lifetime(lwm2m_session_info_t *session_info, uint16_t lifetime)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
if(lifetime > 0) {
|
|
|
|
session_info->lifetime = lifetime;
|
|
|
|
} else {
|
|
|
|
session_info->lifetime = LWM2M_DEFAULT_CLIENT_LIFETIME;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-04-30 11:04:23 +00:00
|
|
|
void
|
|
|
|
lwm2m_rd_client_set_endpoint_name(lwm2m_session_info_t *session_info, const char *endpoint)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
if(endpoint != NULL) {
|
|
|
|
session_info->ep = endpoint;
|
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_set_default_endpoint_name(const char *endpoint)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-09-04 08:58:31 +00:00
|
|
|
strncpy(default_ep, endpoint, sizeof(default_ep) - 1);
|
|
|
|
default_ep[sizeof(default_ep) - 1] = '\0';
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
lwm2m_rd_client_set_update_rd(void)
|
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list);
|
|
|
|
while(session_info != NULL) {
|
|
|
|
session_info->rd_flags |= FLAG_RD_DATA_DIRTY;
|
|
|
|
session_info = session_info->next;
|
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_set_automatic_update(lwm2m_session_info_t *session_info, int update)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_flags = (session_info->rd_flags & ~FLAG_RD_DATA_UPDATE_ON_DIRTY) |
|
2017-10-25 00:20:05 +00:00
|
|
|
(update != 0 ? FLAG_RD_DATA_UPDATE_ON_DIRTY : 0);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-09-04 08:58:31 +00:00
|
|
|
void
|
|
|
|
lwm2m_rd_client_register_with_server(lwm2m_session_info_t *session_info,
|
|
|
|
const coap_endpoint_t *server,
|
|
|
|
lwm2m_rd_client_server_type_t server_type)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
if(session_info->ep == NULL) {
|
|
|
|
session_info->ep = default_ep;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
/* default binding U = UDP, UQ = UDP Q-mode*/
|
|
|
|
#if LWM2M_QUEUE_MODE_CONF_ENABLED
|
|
|
|
session_info->binding = "UQ";
|
|
|
|
/* Enough margin to ensure that the client is not unregistered (we
|
|
|
|
* do not know the time it can stay awake)
|
|
|
|
*/
|
|
|
|
session_info->lifetime = (LWM2M_QUEUE_MODE_DEFAULT_CLIENT_SLEEP_TIME / 1000) * 2;
|
|
|
|
#else
|
|
|
|
session_info->binding = "U";
|
|
|
|
if(session_info->lifetime == 0) {
|
|
|
|
session_info->lifetime = LWM2M_DEFAULT_CLIENT_LIFETIME;
|
|
|
|
}
|
|
|
|
#endif /* LWM2M_QUEUE_MODE_CONF_ENABLED */
|
|
|
|
|
|
|
|
session_info->rd_flags = FLAG_RD_DATA_UPDATE_ON_DIRTY;
|
2018-09-04 08:58:31 +00:00
|
|
|
session_info->has_bs_server_info = 0;
|
|
|
|
session_info->has_registration_server_info = 0;
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->wait_until_network_check = 0;
|
|
|
|
session_info->last_update = 0;
|
|
|
|
session_info->last_rd_progress = 0;
|
2018-09-04 08:58:31 +00:00
|
|
|
session_info->bootstrapped = 0;
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = INIT;
|
2018-09-04 08:58:31 +00:00
|
|
|
|
|
|
|
if(server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER) {
|
|
|
|
coap_endpoint_copy(&session_info->bs_server_ep, server);
|
|
|
|
session_info->has_bs_server_info = 1;
|
|
|
|
session_info->use_server_type = LWM2M_RD_CLIENT_BOOTSTRAP_SERVER;
|
|
|
|
} else {
|
|
|
|
coap_endpoint_copy(&session_info->server_ep, server);
|
|
|
|
session_info->use_server_type = LWM2M_RD_CLIENT_LWM2M_SERVER;
|
|
|
|
session_info->has_registration_server_info = 1;
|
|
|
|
}
|
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
list_add(session_info_list, session_info); /* Add to the list of sessions */
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_deregister(lwm2m_session_info_t *session_info)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
if(lwm2m_rd_client_is_registered(session_info)) {
|
|
|
|
session_info->rd_state = DEREGISTER;
|
2017-10-25 00:20:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* Not registered */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-04-30 11:04:23 +00:00
|
|
|
static lwm2m_session_info_t *
|
2018-09-04 08:58:31 +00:00
|
|
|
get_session_info_from_server_ep(const coap_endpoint_t *server_ep)
|
2018-04-30 11:04:23 +00:00
|
|
|
{
|
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list);
|
|
|
|
while(session_info != NULL) {
|
|
|
|
if(coap_endpoint_cmp(&session_info->server_ep, server_ep)) {
|
|
|
|
return session_info;
|
|
|
|
}
|
|
|
|
session_info = session_info->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-09-04 08:58:31 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-25 00:20:05 +00:00
|
|
|
void
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_update_triggered(const coap_endpoint_t *server_ep)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-09-04 08:58:31 +00:00
|
|
|
lwm2m_session_info_t *session_info = get_session_info_from_server_ep(server_ep);
|
2018-04-30 11:04:23 +00:00
|
|
|
if(session_info) {
|
|
|
|
session_info->rd_flags |= FLAG_RD_DATA_UPDATE_TRIGGERED;
|
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
/* Here we need to do an CoAP timer poll - to get a quick request transmission! */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
|
|
* A client initiated bootstrap starts with a POST to /bs?ep={session_info.ep},
|
|
|
|
* on the bootstrap server. The server should reply with 2.04.
|
|
|
|
* The server will thereafter do DELETE and or PUT to write new client objects.
|
|
|
|
* The bootstrap finishes with the server doing POST to /bs on the client.
|
|
|
|
*
|
|
|
|
* Page 64 in 07 April 2016 spec.
|
|
|
|
*
|
|
|
|
* TODO
|
|
|
|
*/
|
|
|
|
static void
|
2018-05-31 14:22:01 +00:00
|
|
|
bootstrap_callback(coap_callback_request_state_t *callback_state)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_request_state_t *state = &callback_state->state;
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("Bootstrap callback Response: %d, ", state->response != NULL);
|
2018-04-30 11:04:23 +00:00
|
|
|
|
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data;
|
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
if(state->status == COAP_REQUEST_STATUS_RESPONSE) {
|
2017-10-25 00:20:05 +00:00
|
|
|
if(CHANGED_2_04 == state->response->code) {
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG_("Considered done!\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = BOOTSTRAP_DONE;
|
2017-10-25 00:20:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Possible error response codes are 4.00 Bad request & 4.15 Unsupported content format */
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG_("Failed with code %d. Retrying\n", state->response->code);
|
2017-10-25 00:20:05 +00:00
|
|
|
/* TODO Application callback? */
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = INIT;
|
|
|
|
} else if(state->status == COAP_REQUEST_STATUS_TIMEOUT) {
|
2018-05-31 14:22:01 +00:00
|
|
|
LOG_DBG_("Server not responding! Retry?");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = DO_BOOTSTRAP;
|
2018-05-31 14:22:01 +00:00
|
|
|
} else if(state->status == COAP_REQUEST_STATUS_FINISHED) {
|
|
|
|
LOG_DBG_("Request finished. Ignore\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
} else {
|
2018-05-31 14:22:01 +00:00
|
|
|
LOG_DBG_("Unexpected error! Retry?");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = DO_BOOTSTRAP;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2018-04-30 11:04:23 +00:00
|
|
|
produce_more_rd(lwm2m_session_info_t *session_info)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
|
|
|
lwm2m_buffer_t outbuf;
|
|
|
|
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("GOT Continue!\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
|
|
|
|
/* setup the output buffer */
|
|
|
|
outbuf.buffer = rd_data;
|
|
|
|
outbuf.size = sizeof(rd_data);
|
|
|
|
outbuf.len = 0;
|
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_block1++;
|
2017-10-25 00:20:05 +00:00
|
|
|
|
|
|
|
/* this will also set the request payload */
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_more = lwm2m_engine_set_rd_data(&outbuf, session_info->rd_block1);
|
|
|
|
coap_set_payload(session_info->request, rd_data, outbuf.len);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("Setting block1 in request - block: %d more: %d\n",
|
2018-04-30 11:04:23 +00:00
|
|
|
(int)session_info->rd_block1, (int)session_info->rd_more);
|
|
|
|
coap_set_header_block1(session_info->request, session_info->rd_block1, session_info->rd_more, sizeof(rd_data));
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request, session_info->rd_callback);
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
block1_rd_callback(coap_timer_t *timer)
|
|
|
|
{
|
2018-04-30 11:04:23 +00:00
|
|
|
produce_more_rd((lwm2m_session_info_t *)timer->user_data);
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
|
|
* Page 65-66 in 07 April 2016 spec.
|
|
|
|
*/
|
|
|
|
static void
|
2018-05-31 14:22:01 +00:00
|
|
|
registration_callback(coap_callback_request_state_t *callback_state)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_request_state_t *state = &callback_state->state;
|
|
|
|
LOG_DBG("Registration callback. Status: %d. Response: %d, ", state->status, state->response != NULL);
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data;
|
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
if(state->status == COAP_REQUEST_STATUS_RESPONSE) {
|
2017-10-25 00:20:05 +00:00
|
|
|
/* check state and possibly set registration to done */
|
|
|
|
/* If we get a continue - we need to call the rd generator one more time */
|
2018-04-30 11:04:23 +00:00
|
|
|
|
2017-10-25 00:20:05 +00:00
|
|
|
if(CONTINUE_2_31 == state->response->code) {
|
|
|
|
/* We assume that size never change?! */
|
2018-04-30 11:04:23 +00:00
|
|
|
coap_get_header_block1(state->response, &session_info->rd_block1, NULL, NULL, NULL);
|
|
|
|
coap_timer_set_user_data(&session_info->block1_timer, (void *)session_info);
|
|
|
|
coap_timer_set_callback(&session_info->block1_timer, block1_rd_callback);
|
|
|
|
coap_timer_set(&session_info->block1_timer, 1); /* delay 1 ms */
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG_("Continue\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
} else if(CREATED_2_01 == state->response->code) {
|
|
|
|
if(state->response->location_path_len < LWM2M_RD_CLIENT_ASSIGNED_ENDPOINT_MAX_LEN) {
|
2018-04-30 11:04:23 +00:00
|
|
|
memcpy(session_info->assigned_ep, state->response->location_path,
|
2017-10-25 00:20:05 +00:00
|
|
|
state->response->location_path_len);
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->assigned_ep[state->response->location_path_len] = 0;
|
2017-10-25 00:20:05 +00:00
|
|
|
/* if we decide to not pass the lt-argument on registration, we should force an initial "update" to register lifetime with server */
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
|
|
|
#if LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION
|
|
|
|
if(lwm2m_queue_mode_get_dynamic_adaptation_flag()) {
|
2018-05-22 13:34:30 +00:00
|
|
|
lwm2m_queue_mode_set_first_request();
|
2018-03-13 08:34:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_fsm_execute_queue_mode_awake(session_info); /* Avoid 500 ms delay and move directly to the state*/
|
2018-03-13 08:34:11 +00:00
|
|
|
#else
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = REGISTRATION_DONE;
|
2018-03-13 08:34:11 +00:00
|
|
|
#endif
|
2017-10-25 00:20:05 +00:00
|
|
|
/* remember the last reg time */
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->last_update = coap_timer_uptime();
|
|
|
|
LOG_DBG_("Done (assigned EP='%s')!\n", session_info->assigned_ep);
|
|
|
|
perform_session_callback(session_info, LWM2M_RD_CLIENT_REGISTERED);
|
2017-10-25 00:20:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG_("failed to handle assigned EP: '");
|
|
|
|
LOG_DBG_COAP_STRING(state->response->location_path,
|
|
|
|
state->response->location_path_len);
|
|
|
|
LOG_DBG_("'. Re-init network.\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
} else {
|
|
|
|
/* Possible error response codes are 4.00 Bad request & 4.03 Forbidden */
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG_("failed with code %d. Re-init network\n", state->response->code);
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/* TODO Application callback? */
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = INIT;
|
2018-05-31 14:22:01 +00:00
|
|
|
} else if(state->status == COAP_REQUEST_STATUS_TIMEOUT) {
|
|
|
|
LOG_DBG_("Server not responding, trying to reconnect\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = INIT;
|
|
|
|
} else if(state->status == COAP_REQUEST_STATUS_FINISHED) {
|
2018-05-31 14:22:01 +00:00
|
|
|
LOG_DBG_("Request finished. Ignore\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
} else {
|
2018-05-31 14:22:01 +00:00
|
|
|
LOG_DBG_("Unexpected error, trying to reconnect\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = INIT;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
|
|
* Page 65-66 in 07 April 2016 spec.
|
|
|
|
*/
|
|
|
|
static void
|
2018-05-31 14:22:01 +00:00
|
|
|
update_callback(coap_callback_request_state_t *callback_state)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_request_state_t *state = &callback_state->state;
|
|
|
|
LOG_DBG("Update callback. Status: %d. Response: %d, ", state->status, state->response != NULL);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data;
|
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
if(state->status == COAP_REQUEST_STATUS_RESPONSE) {
|
2017-10-25 00:20:05 +00:00
|
|
|
/* If we get a continue - we need to call the rd generator one more time */
|
|
|
|
if(CONTINUE_2_31 == state->response->code) {
|
|
|
|
/* We assume that size never change?! */
|
2017-12-07 22:18:27 +00:00
|
|
|
LOG_DBG_("Continue\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
coap_get_header_block1(state->response, &session_info->rd_block1, NULL, NULL, NULL);
|
|
|
|
coap_timer_set_callback(&session_info->block1_timer, block1_rd_callback);
|
|
|
|
coap_timer_set(&session_info->block1_timer, 1); /* delay 1 ms */
|
2017-10-25 00:20:05 +00:00
|
|
|
} else if(CHANGED_2_04 == state->response->code) {
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG_("Done!\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
/* remember the last reg time */
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->last_update = coap_timer_uptime();
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
2018-03-13 08:34:11 +00:00
|
|
|
/* If it has been waked up by a notification, send the stored notifications in queue */
|
2018-05-22 13:34:30 +00:00
|
|
|
if(lwm2m_queue_mode_is_waked_up_by_notification()) {
|
2018-03-13 08:34:11 +00:00
|
|
|
|
2018-05-22 13:34:30 +00:00
|
|
|
lwm2m_queue_mode_clear_waked_up_by_notification();
|
2018-03-13 08:34:11 +00:00
|
|
|
lwm2m_notification_queue_send_notifications();
|
|
|
|
}
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION
|
|
|
|
if(lwm2m_queue_mode_get_dynamic_adaptation_flag()) {
|
2018-05-22 13:34:30 +00:00
|
|
|
lwm2m_queue_mode_set_first_request();
|
2018-03-13 08:34:11 +00:00
|
|
|
}
|
2018-05-18 16:22:14 +00:00
|
|
|
#endif /* LWM2M_QUEUE_MODE_INCLUDE_DYNAMIC_ADAPTATION */
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_fsm_execute_queue_mode_awake(session_info); /* Avoid 500 ms delay and move directly to the state*/
|
2018-03-13 08:34:11 +00:00
|
|
|
#else
|
2018-04-30 11:04:23 +00:00
|
|
|
|
|
|
|
session_info->rd_state = REGISTRATION_DONE;
|
|
|
|
session_info->rd_flags &= ~FLAG_RD_DATA_UPDATE_TRIGGERED;
|
|
|
|
#endif /* LWM2M_QUEUE_MODE_DEFAULT_CLIENT_SLEEP_TIME */
|
2017-12-07 22:18:27 +00:00
|
|
|
} else {
|
|
|
|
/* Possible error response codes are 4.00 Bad request & 4.04 Not Found */
|
|
|
|
LOG_DBG_("Failed with code %d. Retrying registration\n",
|
|
|
|
state->response->code);
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = DO_REGISTRATION;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-05-31 14:22:01 +00:00
|
|
|
} else if(state->status == COAP_REQUEST_STATUS_TIMEOUT) {
|
|
|
|
LOG_DBG_("Server not responding, trying to reconnect\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = INIT;
|
|
|
|
} else if(state->status == COAP_REQUEST_STATUS_FINISHED) {
|
2018-05-31 14:22:01 +00:00
|
|
|
LOG_DBG_("Request finished. Ignore\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
} else {
|
2018-05-31 14:22:01 +00:00
|
|
|
LOG_DBG_("Unexpected error, trying to reconnect\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = INIT;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2018-05-31 14:22:01 +00:00
|
|
|
deregister_callback(coap_callback_request_state_t *callback_state)
|
2017-10-25 00:20:05 +00:00
|
|
|
{
|
2018-05-31 14:22:01 +00:00
|
|
|
coap_request_state_t *state = &callback_state->state;
|
|
|
|
LOG_DBG("Deregister callback. Status: %d. Response Code: %d\n",
|
|
|
|
state->status,
|
2017-12-01 01:58:44 +00:00
|
|
|
state->response != NULL ? state->response->code : 0);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)state->user_data;
|
|
|
|
|
2018-05-31 14:22:01 +00:00
|
|
|
if(state->status == COAP_REQUEST_STATUS_RESPONSE && (DELETED_2_02 == state->response->code)) {
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("Deregistration success\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = DEREGISTERED;
|
|
|
|
perform_session_callback(session_info, LWM2M_RD_CLIENT_DEREGISTERED);
|
2017-10-25 00:20:05 +00:00
|
|
|
} else {
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("Deregistration failed\n");
|
2018-04-30 11:04:23 +00:00
|
|
|
if(session_info->rd_state == DEREGISTER_SENT) {
|
|
|
|
session_info->rd_state = DEREGISTER_FAILED;
|
|
|
|
perform_session_callback(session_info, LWM2M_RD_CLIENT_DEREGISTER_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
|
|
|
static int
|
|
|
|
all_sessions_in_queue_mode_state(void)
|
|
|
|
{
|
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list);
|
|
|
|
while(session_info != NULL) {
|
|
|
|
if(((session_info->rd_state & 0xF) != 0xE)) {
|
|
|
|
return 0;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info = session_info->next;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
return 1;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-04-30 11:04:23 +00:00
|
|
|
static int
|
|
|
|
all_sessions_in_queue_mode_awake(void)
|
|
|
|
{
|
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list);
|
|
|
|
while(session_info != NULL) {
|
|
|
|
if(session_info->rd_state != QUEUE_MODE_AWAKE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
session_info = session_info->next;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* LWM2M_QUEUE_MODE_ENABLED */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-25 00:20:05 +00:00
|
|
|
/* CoAP timer callback */
|
|
|
|
static void
|
|
|
|
periodic_process(coap_timer_t *timer)
|
|
|
|
{
|
|
|
|
uint64_t now;
|
|
|
|
|
|
|
|
/* reschedule the CoAP timer */
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
2018-03-13 08:34:11 +00:00
|
|
|
/* In Queue Mode, the machine is not executed periodically, but with the awake/sleeping times */
|
2018-04-30 11:04:23 +00:00
|
|
|
if(!all_sessions_in_queue_mode_state()) {
|
2018-03-13 08:34:11 +00:00
|
|
|
coap_timer_reset(&rd_timer, STATE_MACHINE_UPDATE_INTERVAL);
|
|
|
|
}
|
|
|
|
#else
|
2017-10-25 00:20:05 +00:00
|
|
|
coap_timer_reset(&rd_timer, STATE_MACHINE_UPDATE_INTERVAL);
|
2018-03-13 08:34:11 +00:00
|
|
|
#endif
|
|
|
|
|
2017-10-25 00:20:05 +00:00
|
|
|
now = coap_timer_uptime();
|
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list);
|
|
|
|
while(session_info != NULL) {
|
|
|
|
|
|
|
|
LOG_DBG("RD Client with assigned ep: %s - state: %d, ms: %lu\n", session_info->assigned_ep, session_info->rd_state,
|
|
|
|
(unsigned long)coap_timer_uptime());
|
|
|
|
|
|
|
|
switch(session_info->rd_state) {
|
|
|
|
case INIT:
|
|
|
|
LOG_DBG("RD Client started with endpoint '%s' and client lifetime %d\n", session_info->ep, session_info->lifetime);
|
|
|
|
session_info->rd_state = WAIT_NETWORK;
|
|
|
|
break;
|
|
|
|
case WAIT_NETWORK:
|
|
|
|
if(now > session_info->wait_until_network_check) {
|
|
|
|
/* check each 10 seconds before next check */
|
|
|
|
LOG_DBG("Checking for network... %lu\n",
|
|
|
|
(unsigned long)session_info->wait_until_network_check);
|
|
|
|
session_info->wait_until_network_check = now + 10000;
|
|
|
|
if(has_network_access()) {
|
|
|
|
/* Either do bootstrap then registration */
|
2018-09-04 08:58:31 +00:00
|
|
|
if(session_info->use_server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER) {
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = DO_BOOTSTRAP;
|
|
|
|
} else {
|
|
|
|
session_info->rd_state = DO_REGISTRATION;
|
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
/* Otherwise wait until for a network to join */
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
break;
|
|
|
|
case DO_BOOTSTRAP:
|
2018-09-04 08:58:31 +00:00
|
|
|
if(session_info->use_server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER &&
|
2018-09-04 09:45:49 +00:00
|
|
|
session_info->bootstrapped == 0 &&
|
|
|
|
session_info->has_bs_server_info) {
|
|
|
|
|
|
|
|
/* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
|
|
|
|
coap_init_message(session_info->request, COAP_TYPE_CON, COAP_POST, 0);
|
|
|
|
coap_set_header_uri_path(session_info->request, "/bs");
|
|
|
|
|
|
|
|
snprintf(query_data, sizeof(query_data) - 1, "?ep=%s", session_info->ep);
|
|
|
|
coap_set_header_uri_query(session_info->request, query_data);
|
|
|
|
LOG_INFO("Registering ID with bootstrap server [");
|
|
|
|
LOG_INFO_COAP_EP(&session_info->bs_server_ep);
|
|
|
|
LOG_INFO_("] as '%s'\n", query_data);
|
|
|
|
/* Add session info as user data to use it in the callbacks */
|
|
|
|
session_info->rd_request_state.state.user_data = (void *)session_info;
|
|
|
|
if(coap_send_request(&session_info->rd_request_state, &session_info->bs_server_ep,
|
|
|
|
session_info->request, bootstrap_callback)) {
|
|
|
|
session_info->rd_state = BOOTSTRAP_SENT;
|
2018-05-31 14:22:01 +00:00
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
break;
|
|
|
|
case BOOTSTRAP_SENT:
|
|
|
|
/* Just wait for bootstrap to be done... */
|
|
|
|
break;
|
|
|
|
case BOOTSTRAP_DONE:
|
|
|
|
/* check that we should still use bootstrap */
|
2018-09-04 08:58:31 +00:00
|
|
|
if(session_info->use_server_type == LWM2M_RD_CLIENT_BOOTSTRAP_SERVER) {
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_security_server_t *security;
|
|
|
|
LOG_DBG("*** Bootstrap - checking for server info...\n");
|
|
|
|
/* get the security object - ignore bootstrap servers */
|
|
|
|
for(security = lwm2m_security_get_first();
|
|
|
|
security != NULL;
|
|
|
|
security = lwm2m_security_get_next(security)) {
|
|
|
|
if(security->bootstrap == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
if(security != NULL) {
|
|
|
|
/* get the server URI */
|
|
|
|
if(security->server_uri_len > 0) {
|
|
|
|
uint8_t secure = 0;
|
|
|
|
|
|
|
|
LOG_DBG("**** Found security instance using: ");
|
|
|
|
LOG_DBG_COAP_STRING((const char *)security->server_uri,
|
|
|
|
security->server_uri_len);
|
|
|
|
LOG_DBG_(" (len %d) \n", security->server_uri_len);
|
|
|
|
/* TODO Should verify it is a URI */
|
|
|
|
/* Check if secure */
|
|
|
|
secure = strncmp((const char *)security->server_uri,
|
|
|
|
"coaps:", 6) == 0;
|
|
|
|
|
|
|
|
if(!coap_endpoint_parse((const char *)security->server_uri,
|
|
|
|
security->server_uri_len,
|
|
|
|
&session_info->server_ep)) {
|
|
|
|
LOG_DBG("Failed to parse server URI!\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
} else {
|
2018-04-30 11:04:23 +00:00
|
|
|
LOG_DBG("Server address:");
|
|
|
|
LOG_DBG_COAP_EP(&session_info->server_ep);
|
|
|
|
LOG_DBG_("\n");
|
|
|
|
if(secure) {
|
|
|
|
LOG_DBG("Secure CoAP requested but not supported - can not bootstrap\n");
|
|
|
|
} else {
|
2018-09-04 08:58:31 +00:00
|
|
|
lwm2m_rd_client_register_with_server(session_info, &session_info->server_ep, LWM2M_RD_CLIENT_LWM2M_SERVER);
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->bootstrapped++;
|
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
} else {
|
|
|
|
LOG_DBG("** failed to parse URI ");
|
|
|
|
LOG_DBG_COAP_STRING((const char *)security->server_uri,
|
|
|
|
security->server_uri_len);
|
|
|
|
LOG_DBG_("\n");
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if we did not register above - then fail this and restart... */
|
|
|
|
if(session_info->bootstrapped == 0) {
|
|
|
|
/* Not ready. Lets retry with the bootstrap server again */
|
|
|
|
session_info->rd_state = DO_BOOTSTRAP;
|
2017-10-25 00:20:05 +00:00
|
|
|
} else {
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = DO_REGISTRATION;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
break;
|
|
|
|
case DO_REGISTRATION:
|
|
|
|
if(!coap_endpoint_is_connected(&session_info->server_ep)) {
|
|
|
|
/* Not connected... wait a bit... and retry connection */
|
|
|
|
coap_endpoint_connect(&session_info->server_ep);
|
|
|
|
LOG_DBG("Wait until connected... \n");
|
|
|
|
return;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 08:58:31 +00:00
|
|
|
if(session_info->use_server_type == LWM2M_RD_CLIENT_LWM2M_SERVER &&
|
|
|
|
!lwm2m_rd_client_is_registered(session_info) &&
|
2018-09-04 09:45:49 +00:00
|
|
|
session_info->has_registration_server_info) {
|
2018-04-30 11:04:23 +00:00
|
|
|
int len;
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
/* prepare request, TID was set by COAP_BLOCKING_REQUEST() */
|
|
|
|
coap_init_message(session_info->request, COAP_TYPE_CON, COAP_POST, 0);
|
|
|
|
coap_set_header_uri_path(session_info->request, "/rd");
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
snprintf(query_data, sizeof(query_data) - 1, "?ep=%s<=%d&b=%s", session_info->ep, session_info->lifetime, session_info->binding);
|
|
|
|
coap_set_header_uri_query(session_info->request, query_data);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
len = set_rd_data(session_info);
|
|
|
|
session_info->rd_callback = registration_callback;
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
LOG_INFO("Registering with [");
|
|
|
|
LOG_INFO_COAP_EP(&session_info->server_ep);
|
|
|
|
LOG_INFO_("] lwm2m endpoint '%s': '", query_data);
|
|
|
|
if(len) {
|
|
|
|
LOG_INFO_COAP_STRING((const char *)rd_data, len);
|
|
|
|
}
|
|
|
|
LOG_INFO_("' More:%d\n", session_info->rd_more);
|
|
|
|
|
|
|
|
/* Add session info as user data to use it in the callbacks */
|
|
|
|
session_info->rd_request_state.state.user_data = (void *)session_info;
|
|
|
|
if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep,
|
|
|
|
session_info->request, registration_callback)) {
|
|
|
|
|
|
|
|
session_info->rd_state = REGISTRATION_SENT;
|
|
|
|
}
|
|
|
|
session_info->last_rd_progress = coap_timer_uptime();
|
2018-05-31 14:22:01 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
break;
|
|
|
|
case REGISTRATION_SENT:
|
|
|
|
/* just wait until the callback kicks us to the next state... */
|
|
|
|
break;
|
|
|
|
case REGISTRATION_DONE:
|
|
|
|
/* All is done! */
|
|
|
|
|
|
|
|
check_periodic_observations(); /* TODO: manage periodic observations */
|
|
|
|
|
|
|
|
/* check if it is time for the next update */
|
|
|
|
if((session_info->rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED) ||
|
|
|
|
((uint32_t)session_info->lifetime * 500) <= now - session_info->last_update) {
|
|
|
|
/* triggered or time to send an update to the server, at half-time! sec vs ms */
|
|
|
|
prepare_update(session_info, session_info->rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED);
|
|
|
|
|
|
|
|
/* Add session info as user data to use it in the callbacks */
|
|
|
|
session_info->rd_request_state.state.user_data = (void *)session_info;
|
|
|
|
if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request,
|
|
|
|
update_callback)) {
|
|
|
|
session_info->rd_state = UPDATE_SENT;
|
|
|
|
}
|
|
|
|
session_info->last_rd_progress = coap_timer_uptime();
|
2018-05-31 14:22:01 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
2018-04-30 11:04:23 +00:00
|
|
|
case QUEUE_MODE_AWAKE:
|
|
|
|
LOG_DBG("Queue Mode: Client is AWAKE at %lu\n", (unsigned long)coap_timer_uptime());
|
|
|
|
if((queue_mode_client_awake = all_sessions_in_queue_mode_awake())) {
|
|
|
|
queue_mode_client_awake_time = lwm2m_queue_mode_get_awake_time();
|
|
|
|
coap_timer_set(&queue_mode_client_awake_timer, queue_mode_client_awake_time);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QUEUE_MODE_SEND_UPDATE:
|
|
|
|
/* Define this macro to make the necessary actions for waking up,
|
|
|
|
* depending on the platform
|
|
|
|
*/
|
2018-05-18 16:22:14 +00:00
|
|
|
#ifdef LWM2M_QUEUE_MODE_WAKE_UP
|
2018-04-30 11:04:23 +00:00
|
|
|
LWM2M_QUEUE_MODE_WAKE_UP();
|
2018-05-18 16:22:14 +00:00
|
|
|
#endif /* LWM2M_QUEUE_MODE_WAKE_UP */
|
2018-04-30 11:04:23 +00:00
|
|
|
prepare_update(session_info, session_info->rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED);
|
|
|
|
/* Add session info as user data to use it in the callbacks */
|
|
|
|
session_info->rd_request_state.state.user_data = (void *)session_info;
|
|
|
|
if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request,
|
|
|
|
update_callback)) {
|
|
|
|
session_info->rd_state = UPDATE_SENT;
|
|
|
|
}
|
|
|
|
session_info->last_rd_progress = coap_timer_uptime();
|
|
|
|
break;
|
2018-05-18 16:22:14 +00:00
|
|
|
#endif /* LWM2M_QUEUE_MODE_ENABLED */
|
2017-10-25 00:20:05 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
case UPDATE_SENT:
|
|
|
|
/* just wait until the callback kicks us to the next state... */
|
|
|
|
break;
|
|
|
|
case DEREGISTER:
|
|
|
|
LOG_INFO("DEREGISTER %s\n", session_info->assigned_ep);
|
|
|
|
coap_init_message(session_info->request, COAP_TYPE_CON, COAP_DELETE, 0);
|
|
|
|
coap_set_header_uri_path(session_info->request, session_info->assigned_ep);
|
|
|
|
|
|
|
|
/* Add session info as user data to use it in the callbacks */
|
|
|
|
session_info->rd_request_state.state.user_data = (void *)session_info;
|
|
|
|
if(coap_send_request(&session_info->rd_request_state, &session_info->server_ep, session_info->request,
|
|
|
|
deregister_callback)) {
|
|
|
|
session_info->rd_state = DEREGISTER_SENT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DEREGISTER_SENT:
|
|
|
|
break;
|
|
|
|
case DEREGISTER_FAILED:
|
|
|
|
break;
|
|
|
|
case DEREGISTERED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOG_WARN("Unhandled state: %d\n", session_info->rd_state);
|
2018-05-31 14:22:01 +00:00
|
|
|
}
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info = session_info->next;
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
lwm2m_rd_client_init(const char *ep)
|
|
|
|
{
|
2018-09-04 08:58:31 +00:00
|
|
|
lwm2m_rd_client_set_default_endpoint_name(ep);
|
2017-10-25 00:20:05 +00:00
|
|
|
|
|
|
|
/* call the RD client periodically */
|
|
|
|
coap_timer_set_callback(&rd_timer, periodic_process);
|
|
|
|
coap_timer_set(&rd_timer, STATE_MACHINE_UPDATE_INTERVAL);
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
|
|
|
coap_timer_set_callback(&queue_mode_client_awake_timer, queue_mode_awake_timer_callback);
|
2018-03-13 08:34:11 +00:00
|
|
|
#endif
|
2017-10-25 00:20:05 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
check_periodic_observations(void)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-03-13 08:34:11 +00:00
|
|
|
/*
|
2018-09-04 08:58:31 +00:00
|
|
|
* Queue Mode Support
|
2018-03-13 08:34:11 +00:00
|
|
|
*/
|
2018-05-18 16:22:14 +00:00
|
|
|
#if LWM2M_QUEUE_MODE_ENABLED
|
2018-03-13 08:34:11 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
lwm2m_rd_client_restart_client_awake_timer(void)
|
|
|
|
{
|
2018-05-18 16:22:14 +00:00
|
|
|
coap_timer_set(&queue_mode_client_awake_timer, queue_mode_client_awake_time);
|
2018-03-13 08:34:11 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uint8_t
|
|
|
|
lwm2m_rd_client_is_client_awake(void)
|
|
|
|
{
|
2018-05-18 16:22:14 +00:00
|
|
|
return queue_mode_client_awake;
|
2018-03-13 08:34:11 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2018-05-18 16:22:14 +00:00
|
|
|
queue_mode_awake_timer_callback(coap_timer_t *timer)
|
2018-03-13 08:34:11 +00:00
|
|
|
{
|
|
|
|
/* Timer has expired, no requests has been received, client can go to sleep */
|
|
|
|
LOG_DBG("Queue Mode: Client is SLEEPING at %lu\n", (unsigned long)coap_timer_uptime());
|
2018-05-18 16:22:14 +00:00
|
|
|
queue_mode_client_awake = 0;
|
2018-04-06 13:16:42 +00:00
|
|
|
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_session_info_t *session_info = (lwm2m_session_info_t *)list_head(session_info_list);
|
|
|
|
while(session_info != NULL) {
|
|
|
|
session_info->rd_state = QUEUE_MODE_SEND_UPDATE;
|
|
|
|
session_info = session_info->next;
|
|
|
|
}
|
|
|
|
coap_timer_set(&rd_timer, lwm2m_queue_mode_get_sleep_time());
|
|
|
|
/* Define this macro to enter sleep mode depending on the platform */
|
2018-05-18 16:22:14 +00:00
|
|
|
#ifdef LWM2M_QUEUE_MODE_SLEEP_MS
|
|
|
|
LWM2M_QUEUE_MODE_SLEEP_MS(lwm2m_queue_mode_get_sleep_time());
|
|
|
|
#endif /* LWM2M_QUEUE_MODE_SLEEP_MS */
|
2018-03-13 08:34:11 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_fsm_execute_queue_mode_awake(lwm2m_session_info_t *session_info)
|
2018-03-13 08:34:11 +00:00
|
|
|
{
|
|
|
|
coap_timer_stop(&rd_timer);
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = QUEUE_MODE_AWAKE;
|
2018-03-13 08:34:11 +00:00
|
|
|
periodic_process(&rd_timer);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2018-04-30 11:04:23 +00:00
|
|
|
lwm2m_rd_client_fsm_execute_queue_mode_update(lwm2m_session_info_t *session_info)
|
2018-03-13 08:34:11 +00:00
|
|
|
{
|
|
|
|
coap_timer_stop(&rd_timer);
|
2018-04-30 11:04:23 +00:00
|
|
|
session_info->rd_state = QUEUE_MODE_SEND_UPDATE;
|
2018-03-13 08:34:11 +00:00
|
|
|
periodic_process(&rd_timer);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-18 16:22:14 +00:00
|
|
|
#endif /* LWM2M_QUEUE_MODE_ENABLED */
|
2018-03-13 08:34:11 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-26 19:38:55 +00:00
|
|
|
/** @} */
|