2011-09-14 16:33:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011, 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
|
|
|
|
* \author
|
|
|
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "er-coap-07-observing.h"
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
2012-02-20 20:13:50 +01:00
|
|
|
#define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
|
2011-09-14 16:33:01 +02:00
|
|
|
#define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#define PRINT6ADDR(addr)
|
|
|
|
#define PRINTLLADDR(addr)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS);
|
|
|
|
LIST(observers_list);
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
coap_observer_t *
|
2012-02-01 03:24:49 +01:00
|
|
|
coap_add_observer(uip_ipaddr_t *addr, uint16_t port, const uint8_t *token, size_t token_len, const char *url)
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
2012-03-29 18:05:07 +02:00
|
|
|
/* Remove existing observe relationship, if any. */
|
|
|
|
coap_remove_observer_by_url(addr, port, url);
|
|
|
|
|
2011-09-14 16:33:01 +02:00
|
|
|
coap_observer_t *o = memb_alloc(&observers_memb);
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
{
|
|
|
|
o->url = url;
|
|
|
|
uip_ipaddr_copy(&o->addr, addr);
|
|
|
|
o->port = port;
|
|
|
|
o->token_len = token_len;
|
|
|
|
memcpy(o->token, token, token_len);
|
2012-02-01 03:58:35 +01:00
|
|
|
o->last_mid = 0;
|
2011-09-14 16:33:01 +02:00
|
|
|
|
|
|
|
stimer_set(&o->refresh_timer, COAP_OBSERVING_REFRESH_INTERVAL);
|
|
|
|
|
|
|
|
PRINTF("Adding observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
|
|
|
list_add(observers_list, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
coap_remove_observer(coap_observer_t *o)
|
|
|
|
{
|
|
|
|
PRINTF("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0], o->token[1]);
|
|
|
|
|
|
|
|
memb_free(&observers_memb, o);
|
|
|
|
list_remove(observers_list, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port)
|
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
coap_observer_t* obs = NULL;
|
|
|
|
|
|
|
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
|
|
|
{
|
|
|
|
PRINTF("Remove check client ");
|
|
|
|
PRINT6ADDR(addr);
|
|
|
|
PRINTF(":%u\n", port);
|
|
|
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port)
|
|
|
|
{
|
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
2012-02-01 03:58:35 +01:00
|
|
|
|
2011-09-14 16:33:01 +02:00
|
|
|
int
|
|
|
|
coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port, 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)
|
|
|
|
{
|
|
|
|
PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]);
|
2012-02-01 05:16:16 +01:00
|
|
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port && obs->token_len==token_len && memcmp(obs->token, token, token_len)==0)
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
2012-02-01 03:58:35 +01:00
|
|
|
|
2011-09-14 16:33:01 +02:00
|
|
|
int
|
2012-02-01 03:24:49 +01:00
|
|
|
coap_remove_observer_by_url(uip_ipaddr_t *addr, uint16_t port, const char *url)
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
coap_observer_t* obs = NULL;
|
|
|
|
|
|
|
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
|
|
|
{
|
|
|
|
PRINTF("Remove check URL %p\n", url);
|
2012-02-01 03:24:49 +01:00
|
|
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port && (obs->url==url || memcmp(obs->url, url, strlen(obs->url))==0))
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
2012-02-01 03:58:35 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
coap_remove_observer_by_mid(uip_ipaddr_t *addr, uint16_t port, uint16_t mid)
|
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
coap_observer_t* obs = NULL;
|
|
|
|
|
|
|
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
|
|
|
{
|
2012-02-01 05:16:16 +01:00
|
|
|
PRINTF("Remove check MID %u\n", mid);
|
2012-02-01 03:58:35 +01:00
|
|
|
if (uip_ipaddr_cmp(&obs->addr, addr) && obs->port==port && obs->last_mid==mid)
|
|
|
|
{
|
|
|
|
coap_remove_observer(obs);
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
2011-09-14 16:33:01 +02:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
2013-03-11 21:49:52 +01:00
|
|
|
coap_notify_observers(resource_t *resource, int32_t obs_counter, void *notification)
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
2012-03-23 16:29:25 +01:00
|
|
|
coap_packet_t *const coap_res = (coap_packet_t *) notification;
|
2011-09-14 16:33:01 +02:00
|
|
|
coap_observer_t* obs = NULL;
|
2012-03-23 16:29:25 +01:00
|
|
|
uint8_t preferred_type = coap_res->type;
|
|
|
|
|
|
|
|
PRINTF("Observing: Notification from %s\n", resource->url);
|
|
|
|
|
|
|
|
/* Iterate over observers. */
|
2011-09-14 16:33:01 +02:00
|
|
|
for (obs = (coap_observer_t*)list_head(observers_list); obs; obs = obs->next)
|
|
|
|
{
|
2012-03-23 16:29:25 +01:00
|
|
|
if (obs->url==resource->url) /* using RESOURCE url pointer as handle */
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
|
|
|
coap_transaction_t *transaction = NULL;
|
|
|
|
|
2012-03-23 16:29:25 +01:00
|
|
|
/*TODO implement special transaction for CON, sharing the same buffer to allow for more observers. */
|
2011-09-14 16:33:01 +02:00
|
|
|
|
2012-01-28 19:21:13 +01:00
|
|
|
if ( (transaction = coap_new_transaction(coap_get_mid(), &obs->addr, obs->port)) )
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
2012-03-23 16:29:25 +01:00
|
|
|
PRINTF(" Observer ");
|
|
|
|
PRINT6ADDR(&obs->addr);
|
|
|
|
PRINTF(":%u\n", obs->port);
|
|
|
|
|
|
|
|
/* Update last MID for RST matching. */
|
|
|
|
obs->last_mid = transaction->mid;
|
|
|
|
|
|
|
|
/* Prepare response */
|
|
|
|
coap_res->mid = transaction->mid;
|
|
|
|
coap_set_header_observe(coap_res, obs_counter);
|
|
|
|
coap_set_header_token(coap_res, obs->token, obs->token_len);
|
|
|
|
|
2011-09-14 16:33:01 +02:00
|
|
|
/* Use CON to check whether client is still there/interested after COAP_OBSERVING_REFRESH_INTERVAL. */
|
|
|
|
if (stimer_expired(&obs->refresh_timer))
|
|
|
|
{
|
2012-03-23 16:29:25 +01:00
|
|
|
PRINTF(" Refreshing with CON\n");
|
|
|
|
coap_res->type = COAP_TYPE_CON;
|
2011-09-14 16:33:01 +02:00
|
|
|
stimer_restart(&obs->refresh_timer);
|
|
|
|
}
|
2012-03-23 16:29:25 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
coap_res->type = preferred_type;
|
|
|
|
}
|
2011-09-14 16:33:01 +02:00
|
|
|
|
2012-03-23 16:29:25 +01:00
|
|
|
transaction->packet_len = coap_serialize_message(coap_res, transaction->packet);
|
2012-02-01 03:58:35 +01:00
|
|
|
|
2011-09-14 16:33:01 +02:00
|
|
|
coap_send_transaction(transaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
coap_observe_handler(resource_t *resource, void *request, void *response)
|
|
|
|
{
|
2012-01-28 18:52:14 +01:00
|
|
|
coap_packet_t *const coap_req = (coap_packet_t *) request;
|
|
|
|
coap_packet_t *const coap_res = (coap_packet_t *) response;
|
|
|
|
|
2012-02-01 03:24:49 +01:00
|
|
|
static char content[16];
|
2011-09-14 16:33:01 +02:00
|
|
|
|
2012-02-01 03:24:49 +01:00
|
|
|
if (coap_req->code==COAP_GET && coap_res->code<128) /* GET request and response without error code */
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
2012-01-28 18:52:14 +01:00
|
|
|
if (IS_OPTION(coap_req, COAP_OPTION_OBSERVE))
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
|
|
|
|
2012-02-01 03:24:49 +01:00
|
|
|
if (coap_add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len, resource->url))
|
2011-09-14 16:33:01 +02:00
|
|
|
{
|
2012-01-28 18:52:14 +01:00
|
|
|
coap_set_header_observe(coap_res, 0);
|
2012-02-01 03:24:49 +01:00
|
|
|
/*
|
|
|
|
* For demonstration purposes only. A subscription should return the same representation as a normal GET.
|
|
|
|
* TODO: Comment the following line for any real application.
|
|
|
|
*/
|
|
|
|
coap_set_payload(coap_res, content, snprintf(content, sizeof(content), "Added %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS));
|
2011-09-14 16:33:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-28 18:52:14 +01:00
|
|
|
coap_res->code = SERVICE_UNAVAILABLE_5_03;
|
|
|
|
coap_set_payload(coap_res, "TooManyObservers", 16);
|
2011-09-14 16:33:01 +02:00
|
|
|
} /* if (added observer) */
|
|
|
|
}
|
|
|
|
else /* if (observe) */
|
|
|
|
{
|
|
|
|
/* Remove client if it is currently observing. */
|
2012-02-01 03:24:49 +01:00
|
|
|
coap_remove_observer_by_url(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, resource->url);
|
2011-09-14 16:33:01 +02:00
|
|
|
} /* if (observe) */
|
|
|
|
}
|
|
|
|
}
|