2014-04-22 08:31:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
|
|
|
|
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* CoAP module for observing resources (draft-ietf-core-observe-11).
|
|
|
|
* \author
|
|
|
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
|
|
|
*/
|
|
|
|
|
2017-12-21 17:54:28 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup coap
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-09-01 14:37:51 +00:00
|
|
|
#include "coap-observe.h"
|
2017-10-24 23:46:28 +00:00
|
|
|
#include "coap-engine.h"
|
|
|
|
#include "lib/memb.h"
|
|
|
|
#include "lib/list.h"
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
|
|
|
#define LOG_MODULE "coap-observe"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_COAP
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS);
|
|
|
|
LIST(observers_list);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Internal API ------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-05-06 16:14:29 +00:00
|
|
|
static coap_observer_t *
|
2017-10-24 23:46:28 +00:00
|
|
|
add_observer(const coap_endpoint_t *endpoint, const uint8_t *token,
|
2015-05-06 16:14:29 +00:00
|
|
|
size_t token_len, const char *uri, int uri_len)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
/* Remove existing observe relationship, if any. */
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_remove_observer_by_uri(endpoint, uri);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_observer_t *o = memb_alloc(&observers_memb);
|
|
|
|
|
|
|
|
if(o) {
|
2015-05-06 16:14:29 +00:00
|
|
|
int max = sizeof(o->url) - 1;
|
|
|
|
if(max > uri_len) {
|
|
|
|
max = uri_len;
|
|
|
|
}
|
|
|
|
memcpy(o->url, uri, max);
|
|
|
|
o->url[max] = 0;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_endpoint_copy(&o->endpoint, endpoint);
|
2014-04-22 08:31:43 +00:00
|
|
|
o->token_len = token_len;
|
|
|
|
memcpy(o->token, token, token_len);
|
|
|
|
o->last_mid = 0;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("Adding observer (%u/%u) for /%s [0x%02X%02X]\n",
|
|
|
|
list_length(observers_list) + 1, COAP_MAX_OBSERVERS,
|
|
|
|
o->url, o->token[0], o->token[1]);
|
2014-04-22 08:31:43 +00:00
|
|
|
list_add(observers_list, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Removal -----------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
coap_remove_observer(coap_observer_t *o)
|
|
|
|
{
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0],
|
|
|
|
o->token[1]);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
memb_free(&observers_memb, o);
|
|
|
|
list_remove(observers_list, o);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_remove_observer_by_client(const coap_endpoint_t *endpoint)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
coap_observer_t *obs = NULL;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Remove check client ");
|
|
|
|
LOG_DBG_COAP_EP(endpoint);
|
|
|
|
LOG_DBG_("\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
for(obs = (coap_observer_t *)list_head(observers_list); obs;
|
|
|
|
obs = obs->next) {
|
2017-10-24 23:46:28 +00:00
|
|
|
if(coap_endpoint_cmp(&obs->endpoint, endpoint)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_remove_observer_by_token(const coap_endpoint_t *endpoint,
|
2014-04-22 08:31:43 +00:00
|
|
|
uint8_t *token, size_t token_len)
|
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
coap_observer_t *obs = NULL;
|
|
|
|
|
|
|
|
for(obs = (coap_observer_t *)list_head(observers_list); obs;
|
|
|
|
obs = obs->next) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Remove check Token 0x%02X%02X\n", token[0], token[1]);
|
2017-10-24 23:46:28 +00:00
|
|
|
if(coap_endpoint_cmp(&obs->endpoint, endpoint)
|
2014-04-22 08:31:43 +00:00
|
|
|
&& obs->token_len == token_len
|
|
|
|
&& memcmp(obs->token, token, token_len) == 0) {
|
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_remove_observer_by_uri(const coap_endpoint_t *endpoint,
|
2014-04-22 08:31:43 +00:00
|
|
|
const char *uri)
|
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
coap_observer_t *obs = NULL;
|
|
|
|
|
|
|
|
for(obs = (coap_observer_t *)list_head(observers_list); obs;
|
|
|
|
obs = obs->next) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Remove check URL %p\n", uri);
|
2017-10-24 23:46:28 +00:00
|
|
|
if((endpoint == NULL
|
|
|
|
|| (coap_endpoint_cmp(&obs->endpoint, endpoint)))
|
2014-04-22 08:31:43 +00:00
|
|
|
&& (obs->url == uri || memcmp(obs->url, uri, strlen(obs->url)) == 0)) {
|
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_remove_observer_by_mid(const coap_endpoint_t *endpoint, uint16_t mid)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
coap_observer_t *obs = NULL;
|
|
|
|
|
|
|
|
for(obs = (coap_observer_t *)list_head(observers_list); obs;
|
|
|
|
obs = obs->next) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Remove check MID %u\n", mid);
|
2017-10-24 23:46:28 +00:00
|
|
|
if(coap_endpoint_cmp(&obs->endpoint, endpoint)
|
2014-04-22 08:31:43 +00:00
|
|
|
&& obs->last_mid == mid) {
|
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Notification ------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_notify_observers(coap_resource_t *resource)
|
2015-05-06 16:14:29 +00:00
|
|
|
{
|
|
|
|
coap_notify_observers_sub(resource, NULL);
|
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
/* Can be used either for sub - or when there is not resource - just
|
|
|
|
a handler */
|
2015-05-06 16:14:29 +00:00
|
|
|
void
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_notify_observers_sub(coap_resource_t *resource, const char *subpath)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
/* build notification */
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_message_t notification[1]; /* this way the message can be treated as pointer as usual */
|
|
|
|
coap_message_t request[1]; /* this way the message can be treated as pointer as usual */
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_observer_t *obs = NULL;
|
2015-05-06 16:14:29 +00:00
|
|
|
int url_len, obs_url_len;
|
|
|
|
char url[COAP_OBSERVER_URL_LEN];
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t sub_ok = 0;
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
if(resource != NULL) {
|
|
|
|
url_len = strlen(resource->url);
|
|
|
|
strncpy(url, resource->url, COAP_OBSERVER_URL_LEN - 1);
|
|
|
|
if(url_len < COAP_OBSERVER_URL_LEN - 1 && subpath != NULL) {
|
|
|
|
strncpy(&url[url_len], subpath, COAP_OBSERVER_URL_LEN - url_len - 1);
|
|
|
|
}
|
|
|
|
} else if(subpath != NULL) {
|
|
|
|
strncpy(url, subpath, COAP_OBSERVER_URL_LEN - 1);
|
|
|
|
} else {
|
|
|
|
/* No resource, no subpath */
|
|
|
|
return;
|
2015-05-06 16:14:29 +00:00
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2015-06-04 19:13:50 +00:00
|
|
|
/* Ensure url is null terminated because strncpy does not guarantee this */
|
|
|
|
url[COAP_OBSERVER_URL_LEN - 1] = '\0';
|
2015-05-06 16:14:29 +00:00
|
|
|
/* url now contains the notify URL that needs to match the observer */
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("Notification from %s\n", url);
|
2015-05-06 16:14:29 +00:00
|
|
|
|
|
|
|
coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0);
|
|
|
|
/* create a "fake" request for the URI */
|
|
|
|
coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);
|
|
|
|
coap_set_header_uri_path(request, url);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* iterate over observers */
|
2015-05-06 16:14:29 +00:00
|
|
|
url_len = strlen(url);
|
2017-10-24 23:46:28 +00:00
|
|
|
/* Assumes lazy evaluation... */
|
|
|
|
sub_ok = (resource == NULL) || (resource->flags & HAS_SUB_RESOURCES);
|
2014-04-22 08:31:43 +00:00
|
|
|
for(obs = (coap_observer_t *)list_head(observers_list); obs;
|
|
|
|
obs = obs->next) {
|
2015-05-06 16:14:29 +00:00
|
|
|
obs_url_len = strlen(obs->url);
|
|
|
|
|
|
|
|
/* Do a match based on the parent/sub-resource match so that it is
|
|
|
|
possible to do parent-node observe */
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
/***** TODO fix here so that we handle the notofication correctly ******/
|
|
|
|
/* All the new-style ... is assuming that the URL might be within */
|
2015-05-06 16:14:29 +00:00
|
|
|
if((obs_url_len == url_len
|
|
|
|
|| (obs_url_len > url_len
|
2017-10-24 23:46:28 +00:00
|
|
|
&& sub_ok
|
2015-05-06 16:14:29 +00:00
|
|
|
&& obs->url[url_len] == '/'))
|
|
|
|
&& strncmp(url, obs->url, url_len) == 0) {
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_transaction_t *transaction = NULL;
|
|
|
|
|
|
|
|
/*TODO implement special transaction for CON, sharing the same buffer to allow for more observers */
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
if((transaction = coap_new_transaction(coap_get_mid(), &obs->endpoint))) {
|
2014-05-15 12:32:08 +00:00
|
|
|
if(obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG(" Force Confirmable for\n");
|
2014-04-30 09:48:37 +00:00
|
|
|
notification->type = COAP_TYPE_CON;
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG(" Observer ");
|
|
|
|
LOG_DBG_COAP_EP(&obs->endpoint);
|
|
|
|
LOG_DBG_("\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* update last MID for RST matching */
|
|
|
|
obs->last_mid = transaction->mid;
|
|
|
|
|
|
|
|
/* prepare response */
|
|
|
|
notification->mid = transaction->mid;
|
|
|
|
|
2018-02-23 08:41:30 +00:00
|
|
|
int32_t new_offset = 0;
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* Either old style get_handler or the full handler */
|
2017-11-14 20:32:43 +00:00
|
|
|
if(coap_call_handlers(request, notification, transaction->message +
|
2017-10-24 23:46:28 +00:00
|
|
|
COAP_MAX_HEADER_SIZE, COAP_MAX_CHUNK_SIZE,
|
2018-02-23 08:41:30 +00:00
|
|
|
&new_offset) > 0) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Notification on new handlers\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
} else {
|
|
|
|
if(resource != NULL) {
|
|
|
|
resource->get_handler(request, notification,
|
2017-11-14 20:32:43 +00:00
|
|
|
transaction->message + COAP_MAX_HEADER_SIZE,
|
2018-02-23 08:41:30 +00:00
|
|
|
COAP_MAX_CHUNK_SIZE, &new_offset);
|
2017-10-24 23:46:28 +00:00
|
|
|
} else {
|
|
|
|
/* What to do here? */
|
|
|
|
notification->code = BAD_REQUEST_4_00;
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
if(notification->code < BAD_REQUEST_4_00) {
|
|
|
|
coap_set_header_observe(notification, (obs->obs_counter)++);
|
2017-04-08 14:02:52 +00:00
|
|
|
/* mask out to keep the CoAP observe option length <= 3 bytes */
|
|
|
|
obs->obs_counter &= 0xffffff;
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
coap_set_token(notification, obs->token, obs->token_len);
|
|
|
|
|
2018-02-23 08:41:30 +00:00
|
|
|
if(new_offset != 0) {
|
|
|
|
coap_set_header_block2(notification,
|
|
|
|
0,
|
|
|
|
new_offset != -1,
|
|
|
|
COAP_MAX_BLOCK_SIZE);
|
|
|
|
coap_set_payload(notification,
|
|
|
|
notification->payload,
|
|
|
|
MIN(notification->payload_len,
|
|
|
|
COAP_MAX_BLOCK_SIZE));
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:32:43 +00:00
|
|
|
transaction->message_len =
|
|
|
|
coap_serialize_message(notification, transaction->message);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_send_transaction(transaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_observe_handler(coap_resource_t *resource, coap_message_t *coap_req,
|
|
|
|
coap_message_t *coap_res)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
const coap_endpoint_t *src_ep;
|
2016-12-07 16:09:52 +00:00
|
|
|
coap_observer_t *obs;
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("CoAP observer handler rsc: %d\n", resource != NULL);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
if(coap_req->code == COAP_GET && coap_res->code < 128) { /* GET request and response without error code */
|
2017-10-24 23:46:28 +00:00
|
|
|
if(coap_is_option(coap_req, COAP_OPTION_OBSERVE)) {
|
|
|
|
src_ep = coap_get_src_endpoint(coap_req);
|
|
|
|
if(src_ep == NULL) {
|
|
|
|
/* No source endpoint, can not add */
|
|
|
|
} else if(coap_req->observe == 0) {
|
|
|
|
obs = add_observer(src_ep,
|
2015-05-06 16:14:29 +00:00
|
|
|
coap_req->token, coap_req->token_len,
|
|
|
|
coap_req->uri_path, coap_req->uri_path_len);
|
2016-12-07 16:09:52 +00:00
|
|
|
if(obs) {
|
2014-08-24 16:19:56 +00:00
|
|
|
coap_set_header_observe(coap_res, (obs->obs_counter)++);
|
2017-04-08 14:02:52 +00:00
|
|
|
/* mask out to keep the CoAP observe option length <= 3 bytes */
|
|
|
|
obs->obs_counter &= 0xffffff;
|
2014-05-15 12:32:08 +00:00
|
|
|
/*
|
|
|
|
* Following payload is for demonstration purposes only.
|
|
|
|
* A subscription should return the same representation as a normal GET.
|
|
|
|
* Uncomment if you want an information about the avaiable observers.
|
|
|
|
*/
|
2015-07-16 09:25:10 +00:00
|
|
|
#if 0
|
|
|
|
static char content[16];
|
|
|
|
coap_set_payload(coap_res,
|
|
|
|
content,
|
|
|
|
snprintf(content, sizeof(content), "Added %u/%u",
|
|
|
|
list_length(observers_list),
|
|
|
|
COAP_MAX_OBSERVERS));
|
|
|
|
#endif
|
2014-05-15 12:32:08 +00:00
|
|
|
} else {
|
|
|
|
coap_res->code = SERVICE_UNAVAILABLE_5_03;
|
|
|
|
coap_set_payload(coap_res, "TooManyObservers", 16);
|
|
|
|
}
|
|
|
|
} else if(coap_req->observe == 1) {
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2014-05-15 12:32:08 +00:00
|
|
|
/* remove client if it is currently observe */
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_remove_observer_by_token(src_ep,
|
|
|
|
coap_req->token, coap_req->token_len);
|
2014-05-15 12:32:08 +00:00
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-21 17:54:28 +00:00
|
|
|
/** @} */
|