Contiki 3.x
er-coap-observe.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * CoAP module for observing resources (draft-ietf-core-observe-11).
35  * \author
36  * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37  */
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include "er-coap-observe.h"
42 
43 #define DEBUG 0
44 #if DEBUG
45 #include <stdio.h>
46 #define PRINTF(...) printf(__VA_ARGS__)
47 #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])
48 #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])
49 #else
50 #define PRINTF(...)
51 #define PRINT6ADDR(addr)
52 #define PRINTLLADDR(addr)
53 #endif
54 
55 /*---------------------------------------------------------------------------*/
56 MEMB(observers_memb, coap_observer_t, COAP_MAX_OBSERVERS);
57 LIST(observers_list);
58 /*---------------------------------------------------------------------------*/
59 /*- Internal API ------------------------------------------------------------*/
60 /*---------------------------------------------------------------------------*/
61 static coap_observer_t *
62 add_observer(uip_ipaddr_t *addr, uint16_t port, const uint8_t *token,
63  size_t token_len, const char *uri, int uri_len)
64 {
65  /* Remove existing observe relationship, if any. */
66  coap_remove_observer_by_uri(addr, port, uri);
67 
68  coap_observer_t *o = memb_alloc(&observers_memb);
69 
70  if(o) {
71  int max = sizeof(o->url) - 1;
72  if(max > uri_len) {
73  max = uri_len;
74  }
75  memcpy(o->url, uri, max);
76  o->url[max] = 0;
77  uip_ipaddr_copy(&o->addr, addr);
78  o->port = port;
79  o->token_len = token_len;
80  memcpy(o->token, token, token_len);
81  o->last_mid = 0;
82 
83  PRINTF("Adding observer (%u/%u) for /%s [0x%02X%02X]\n",
84  list_length(observers_list) + 1, COAP_MAX_OBSERVERS,
85  o->url, o->token[0], o->token[1]);
86  list_add(observers_list, o);
87  }
88 
89  return o;
90 }
91 /*---------------------------------------------------------------------------*/
92 /*- Removal -----------------------------------------------------------------*/
93 /*---------------------------------------------------------------------------*/
94 void
95 coap_remove_observer(coap_observer_t *o)
96 {
97  PRINTF("Removing observer for /%s [0x%02X%02X]\n", o->url, o->token[0],
98  o->token[1]);
99 
100  memb_free(&observers_memb, o);
101  list_remove(observers_list, o);
102 }
103 /*---------------------------------------------------------------------------*/
104 int
105 coap_remove_observer_by_client(uip_ipaddr_t *addr, uint16_t port)
106 {
107  int removed = 0;
108  coap_observer_t *obs = NULL;
109 
110  for(obs = (coap_observer_t *)list_head(observers_list); obs;
111  obs = obs->next) {
112  PRINTF("Remove check client ");
113  PRINT6ADDR(addr);
114  PRINTF(":%u\n", port);
115  if(uip_ipaddr_cmp(&obs->addr, addr) && obs->port == port) {
116  coap_remove_observer(obs);
117  removed++;
118  }
119  }
120  return removed;
121 }
122 /*---------------------------------------------------------------------------*/
123 int
124 coap_remove_observer_by_token(uip_ipaddr_t *addr, uint16_t port,
125  uint8_t *token, size_t token_len)
126 {
127  int removed = 0;
128  coap_observer_t *obs = NULL;
129 
130  for(obs = (coap_observer_t *)list_head(observers_list); obs;
131  obs = obs->next) {
132  PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]);
133  if(uip_ipaddr_cmp(&obs->addr, addr) && obs->port == port
134  && obs->token_len == token_len
135  && memcmp(obs->token, token, token_len) == 0) {
136  coap_remove_observer(obs);
137  removed++;
138  }
139  }
140  return removed;
141 }
142 /*---------------------------------------------------------------------------*/
143 int
144 coap_remove_observer_by_uri(uip_ipaddr_t *addr, uint16_t port,
145  const char *uri)
146 {
147  int removed = 0;
148  coap_observer_t *obs = NULL;
149 
150  for(obs = (coap_observer_t *)list_head(observers_list); obs;
151  obs = obs->next) {
152  PRINTF("Remove check URL %p\n", uri);
153  if((addr == NULL
154  || (uip_ipaddr_cmp(&obs->addr, addr) && obs->port == port))
155  && (obs->url == uri || memcmp(obs->url, uri, strlen(obs->url)) == 0)) {
156  coap_remove_observer(obs);
157  removed++;
158  }
159  }
160  return removed;
161 }
162 /*---------------------------------------------------------------------------*/
163 int
164 coap_remove_observer_by_mid(uip_ipaddr_t *addr, uint16_t port, uint16_t mid)
165 {
166  int removed = 0;
167  coap_observer_t *obs = NULL;
168 
169  for(obs = (coap_observer_t *)list_head(observers_list); obs;
170  obs = obs->next) {
171  PRINTF("Remove check MID %u\n", mid);
172  if(uip_ipaddr_cmp(&obs->addr, addr) && obs->port == port
173  && obs->last_mid == mid) {
174  coap_remove_observer(obs);
175  removed++;
176  }
177  }
178  return removed;
179 }
180 /*---------------------------------------------------------------------------*/
181 /*- Notification ------------------------------------------------------------*/
182 /*---------------------------------------------------------------------------*/
183 void
184 coap_notify_observers(resource_t *resource)
185 {
186  coap_notify_observers_sub(resource, NULL);
187 }
188 void
189 coap_notify_observers_sub(resource_t *resource, const char *subpath)
190 {
191  /* build notification */
192  coap_packet_t notification[1]; /* this way the packet can be treated as pointer as usual */
193  coap_packet_t request[1]; /* this way the packet can be treated as pointer as usual */
194  coap_observer_t *obs = NULL;
195  int url_len, obs_url_len;
196  char url[COAP_OBSERVER_URL_LEN];
197 
198  url_len = strlen(resource->url);
199  strncpy(url, resource->url, COAP_OBSERVER_URL_LEN - 1);
200  if(url_len < COAP_OBSERVER_URL_LEN - 1 && subpath != NULL) {
201  strncpy(&url[url_len], subpath, COAP_OBSERVER_URL_LEN - url_len - 1);
202  }
203  /* Ensure url is null terminated because strncpy does not guarantee this */
204  url[COAP_OBSERVER_URL_LEN - 1] = '\0';
205  /* url now contains the notify URL that needs to match the observer */
206  PRINTF("Observe: Notification from %s\n", url);
207 
208  coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0);
209  /* create a "fake" request for the URI */
210  coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);
211  coap_set_header_uri_path(request, url);
212 
213  /* iterate over observers */
214  url_len = strlen(url);
215  for(obs = (coap_observer_t *)list_head(observers_list); obs;
216  obs = obs->next) {
217  obs_url_len = strlen(obs->url);
218 
219  /* Do a match based on the parent/sub-resource match so that it is
220  possible to do parent-node observe */
221  if((obs_url_len == url_len
222  || (obs_url_len > url_len
223  && (resource->flags & HAS_SUB_RESOURCES)
224  && obs->url[url_len] == '/'))
225  && strncmp(url, obs->url, url_len) == 0) {
226  coap_transaction_t *transaction = NULL;
227 
228  /*TODO implement special transaction for CON, sharing the same buffer to allow for more observers */
229 
230  if((transaction = coap_new_transaction(coap_get_mid(), &obs->addr, obs->port))) {
231  if(obs->obs_counter % COAP_OBSERVE_REFRESH_INTERVAL == 0) {
232  PRINTF(" Force Confirmable for\n");
233  notification->type = COAP_TYPE_CON;
234  }
235 
236  PRINTF(" Observer ");
237  PRINT6ADDR(&obs->addr);
238  PRINTF(":%u\n", obs->port);
239 
240  /* update last MID for RST matching */
241  obs->last_mid = transaction->mid;
242 
243  /* prepare response */
244  notification->mid = transaction->mid;
245 
246  resource->get_handler(request, notification,
247  transaction->packet + COAP_MAX_HEADER_SIZE,
248  REST_MAX_CHUNK_SIZE, NULL);
249 
250  if(notification->code < BAD_REQUEST_4_00) {
251  coap_set_header_observe(notification, (obs->obs_counter)++);
252  }
253  coap_set_token(notification, obs->token, obs->token_len);
254 
255  transaction->packet_len =
256  coap_serialize_message(notification, transaction->packet);
257 
258  coap_send_transaction(transaction);
259  }
260  }
261  }
262 }
263 /*---------------------------------------------------------------------------*/
264 void
265 coap_observe_handler(resource_t *resource, void *request, void *response)
266 {
267  coap_packet_t *const coap_req = (coap_packet_t *)request;
268  coap_packet_t *const coap_res = (coap_packet_t *)response;
269  coap_observer_t * obs;
270 
271  if(coap_req->code == COAP_GET && coap_res->code < 128) { /* GET request and response without error code */
272  if(IS_OPTION(coap_req, COAP_OPTION_OBSERVE)) {
273  if(coap_req->observe == 0) {
274  obs = add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport,
275  coap_req->token, coap_req->token_len,
276  coap_req->uri_path, coap_req->uri_path_len);
277  if(obs) {
278  coap_set_header_observe(coap_res, (obs->obs_counter)++);
279  /*
280  * Following payload is for demonstration purposes only.
281  * A subscription should return the same representation as a normal GET.
282  * Uncomment if you want an information about the avaiable observers.
283  */
284 #if 0
285  static char content[16];
286  coap_set_payload(coap_res,
287  content,
288  snprintf(content, sizeof(content), "Added %u/%u",
289  list_length(observers_list),
290  COAP_MAX_OBSERVERS));
291 #endif
292  } else {
293  coap_res->code = SERVICE_UNAVAILABLE_5_03;
294  coap_set_payload(coap_res, "TooManyObservers", 16);
295  }
296  } else if(coap_req->observe == 1) {
297 
298  /* remove client if it is currently observe */
299  coap_remove_observer_by_token(&UIP_IP_BUF->srcipaddr,
300  UIP_UDP_BUF->srcport, coap_req->token,
301  coap_req->token_len);
302  }
303  }
304  }
305 }
306 /*---------------------------------------------------------------------------*/
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
#define LIST(name)
Declare a linked list.
Definition: list.h:86
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
CoAP module for observing resources (draft-ietf-core-observe-11).
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:104
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
#define NULL
The null pointer.
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
int list_length(list_t list)
Get the length of a list.
Definition: list.c:275
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79