Contiki 3.x
er-coap-observe-client.c
1 /*
2  * Copyright (c) 2014, Daniele Alessandrelli.
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 /*
34  * \file
35  * Extension to Erbium for enabling CoAP observe clients
36  * \author
37  * Daniele Alessandrelli <daniele.alessandrelli@gmail.com>
38  */
39 
40 #include <stdio.h>
41 #include <string.h>
42 
43 #include "er-coap.h"
44 #include "er-coap-observe-client.h"
45 
46 /* Compile this code only if client-side support for CoAP Observe is required */
47 #if COAP_OBSERVE_CLIENT
48 
49 #define DEBUG 1
50 #if DEBUG
51 #define PRINTF(...) printf(__VA_ARGS__)
52 #define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:" \
53  "%02x%02x:%02x%02x:%02x%02x:%02x%02x]", \
54  ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], \
55  ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], \
56  ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], \
57  ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], \
58  ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], \
59  ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], \
60  ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], \
61  ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
62 #define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", \
63  (lladdr)->addr[0], (lladdr)->addr[1], \
64  (lladdr)->addr[2], (lladdr)->addr[3], \
65  (lladdr)->addr[4], (lladdr)->addr[5])
66 #else
67 #define PRINTF(...)
68 #define PRINT6ADDR(addr)
69 #define PRINTLLADDR(addr)
70 #endif
71 
72 MEMB(obs_subjects_memb, coap_observee_t, COAP_MAX_OBSERVEES);
73 LIST(obs_subjects_list);
74 
75 /*----------------------------------------------------------------------------*/
76 static size_t
77 get_token(void *packet, const uint8_t **token)
78 {
79  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
80 
81  *token = coap_pkt->token;
82 
83  return coap_pkt->token_len;
84 }
85 /*----------------------------------------------------------------------------*/
86 static int
87 set_token(void *packet, const uint8_t *token, size_t token_len)
88 {
89  coap_packet_t *const coap_pkt = (coap_packet_t *)packet;
90 
91  coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
92  memcpy(coap_pkt->token, token, coap_pkt->token_len);
93 
94  return coap_pkt->token_len;
95 }
96 /*----------------------------------------------------------------------------*/
97 coap_observee_t *
98 coap_obs_add_observee(uip_ipaddr_t *addr, uint16_t port,
99  const uint8_t *token, size_t token_len, const char *url,
100  notification_callback_t notification_callback,
101  void *data)
102 {
103  coap_observee_t *o;
104 
105  /* Remove existing observe relationship, if any. */
106  coap_obs_remove_observee_by_url(addr, port, url);
107  o = memb_alloc(&obs_subjects_memb);
108  if(o) {
109  o->url = url;
110  uip_ipaddr_copy(&o->addr, addr);
111  o->port = port;
112  o->token_len = token_len;
113  memcpy(o->token, token, token_len);
114  /* o->last_mid = 0; */
115  o->notification_callback = notification_callback;
116  o->data = data;
117  /* stimer_set(&o->refresh_timer, COAP_OBSERVING_REFRESH_INTERVAL); */
118  PRINTF("Adding obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
119  o->token[1]);
120  list_add(obs_subjects_list, o);
121  }
122 
123  return o;
124 }
125 /*----------------------------------------------------------------------------*/
126 void
127 coap_obs_remove_observee(coap_observee_t *o)
128 {
129  PRINTF("Removing obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
130  o->token[1]);
131  memb_free(&obs_subjects_memb, o);
132  list_remove(obs_subjects_list, o);
133 }
134 /*----------------------------------------------------------------------------*/
135 coap_observee_t *
136 coap_get_obs_subject_by_token(const uint8_t *token, size_t token_len)
137 {
138  coap_observee_t *obs = NULL;
139 
140  for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
141  obs = obs->next) {
142  PRINTF("Looking for token 0x%02X%02X\n", token[0], token[1]);
143  if(obs->token_len == token_len
144  && memcmp(obs->token, token, token_len) == 0) {
145  return obs;
146  }
147  }
148 
149  return NULL;
150 }
151 /*----------------------------------------------------------------------------*/
152 int
153 coap_obs_remove_observee_by_token(uip_ipaddr_t *addr, uint16_t port,
154  uint8_t *token, size_t token_len)
155 {
156  int removed = 0;
157  coap_observee_t *obs = NULL;
158 
159  for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
160  obs = obs->next) {
161  PRINTF("Remove check Token 0x%02X%02X\n", token[0], token[1]);
162  if(uip_ipaddr_cmp(&obs->addr, addr)
163  && obs->port == port
164  && obs->token_len == token_len
165  && memcmp(obs->token, token, token_len) == 0) {
166  coap_obs_remove_observee(obs);
167  removed++;
168  }
169  }
170  return removed;
171 }
172 /*----------------------------------------------------------------------------*/
173 int
174 coap_obs_remove_observee_by_url(uip_ipaddr_t *addr, uint16_t port,
175  const char *url)
176 {
177  int removed = 0;
178  coap_observee_t *obs = NULL;
179 
180  for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
181  obs = obs->next) {
182  PRINTF("Remove check URL %s\n", url);
183  if(uip_ipaddr_cmp(&obs->addr, addr)
184  && obs->port == port
185  && (obs->url == url || memcmp(obs->url, url, strlen(obs->url)) == 0)) {
186  coap_obs_remove_observee(obs);
187  removed++;
188  }
189  }
190  return removed;
191 }
192 /*----------------------------------------------------------------------------*/
193 static void
194 simple_reply(coap_message_type_t type, uip_ip6addr_t *addr, uint16_t port,
195  coap_packet_t *notification)
196 {
197  static coap_packet_t response[1];
198  size_t len;
199 
200  coap_init_message(response, type, NO_ERROR, notification->mid);
201  len = coap_serialize_message(response, uip_appdata);
202  coap_send_message(addr, port, uip_appdata, len);
203 }
204 /*----------------------------------------------------------------------------*/
205 static coap_notification_flag_t
206 classify_notification(void *response, int first)
207 {
208  coap_packet_t *pkt;
209 
210  pkt = (coap_packet_t *)response;
211  if(!pkt) {
212  PRINTF("no response\n");
213  return NO_REPLY_FROM_SERVER;
214  }
215  PRINTF("server replied\n");
216  if(!IS_RESPONSE_CODE_2_XX(pkt)) {
217  PRINTF("error response code\n");
218  return ERROR_RESPONSE_CODE;
219  }
220  if(!IS_OPTION(pkt, COAP_OPTION_OBSERVE)) {
221  PRINTF("server does not support observe\n");
222  return OBSERVE_NOT_SUPPORTED;
223  }
224  if(first) {
225  return OBSERVE_OK;
226  }
227  return NOTIFICATION_OK;
228 }
229 /*----------------------------------------------------------------------------*/
230 void
231 coap_handle_notification(uip_ipaddr_t *addr, uint16_t port,
232  coap_packet_t *notification)
233 {
234  coap_packet_t *pkt;
235  const uint8_t *token;
236  int token_len;
237  coap_observee_t *obs;
238  coap_notification_flag_t flag;
239  uint32_t observe;
240 
241  PRINTF("coap_handle_notification()\n");
242  pkt = (coap_packet_t *)notification;
243  token_len = get_token(pkt, &token);
244  PRINTF("Getting token\n");
245  if(0 == token_len) {
246  PRINTF("Error while handling coap observe notification: "
247  "no token in message\n");
248  return;
249  }
250  PRINTF("Getting observee info\n");
251  obs = coap_get_obs_subject_by_token(token, token_len);
252  if(NULL == obs) {
253  PRINTF("Error while handling coap observe notification: "
254  "no matching token found\n");
255  simple_reply(COAP_TYPE_RST, addr, port, notification);
256  return;
257  }
258  if(notification->type == COAP_TYPE_CON) {
259  simple_reply(COAP_TYPE_ACK, addr, port, notification);
260  }
261  if(obs->notification_callback != NULL) {
262  flag = classify_notification(notification, 0);
263  /* TODO: the following mechanism for discarding duplicates is too trivial */
264  /* refer to Observe RFC for a better solution */
265  if(flag == NOTIFICATION_OK) {
266  coap_get_header_observe(notification, &observe);
267  if(observe == obs->last_observe) {
268  PRINTF("Discarding duplicate\n");
269  return;
270  }
271  obs->last_observe = observe;
272  }
273  obs->notification_callback(obs, notification, flag);
274  }
275 }
276 /*----------------------------------------------------------------------------*/
277 static void
278 handle_obs_registration_response(void *data, void *response)
279 {
280  coap_observee_t *obs;
281  notification_callback_t notification_callback;
282  coap_notification_flag_t flag;
283 
284  PRINTF("handle_obs_registration_response(): ");
285  obs = (coap_observee_t *)data;
286  notification_callback = obs->notification_callback;
287  flag = classify_notification(response, 1);
288  if(notification_callback) {
289  notification_callback(obs, response, flag);
290  }
291  if(flag != OBSERVE_OK) {
292  coap_obs_remove_observee(obs);
293  }
294 }
295 /*----------------------------------------------------------------------------*/
296 uint8_t
297 coap_generate_token(uint8_t **token_ptr)
298 {
299  static uint8_t token = 0;
300 
301  token++;
302  /* FIXME: we should check that this token is not already used */
303  *token_ptr = (uint8_t *)&token;
304  return sizeof(token);
305 }
306 /*----------------------------------------------------------------------------*/
307 coap_observee_t *
308 coap_obs_request_registration(uip_ipaddr_t *addr, uint16_t port, char *uri,
309  notification_callback_t notification_callback,
310  void *data)
311 {
312  coap_packet_t request[1];
313  coap_transaction_t *t;
314  uint8_t *token;
315  uint8_t token_len;
316  coap_observee_t *obs;
317 
318  obs = NULL;
319  coap_init_message(request, COAP_TYPE_CON, COAP_GET, coap_get_mid());
320  coap_set_header_uri_path(request, uri);
321  coap_set_header_observe(request, 0);
322  token_len = coap_generate_token(&token);
323  set_token(request, token, token_len);
324  t = coap_new_transaction(request->mid, addr, port);
325  if(t) {
326  obs = coap_obs_add_observee(addr, port, (uint8_t *)token, token_len, uri,
327  notification_callback, data);
328  if(obs) {
329  t->callback = handle_obs_registration_response;
330  t->callback_data = obs;
331  t->packet_len = coap_serialize_message(request, t->packet);
332  coap_send_transaction(t);
333  } else {
334  PRINTF("Could not allocate obs_subject resource buffer");
335  coap_clear_transaction(t);
336  }
337  } else {
338  PRINTF("Could not allocate transaction buffer");
339  }
340  return obs;
341 }
342 #endif /* COAP_OBSERVE_CLIENT */
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
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
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
An implementation of the Constrained Application Protocol (RFC).
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