Contiki-NG
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 /**
41  * \addtogroup coap
42  * @{
43  */
44 
45 #include "coap.h"
46 #include "coap-observe-client.h"
47 #include "sys/cc.h"
48 #include "lib/memb.h"
49 #include "lib/list.h"
50 #include <stdio.h>
51 #include <string.h>
52 
53 /* Compile this code only if client-side support for CoAP Observe is required */
54 #if COAP_OBSERVE_CLIENT
55 
56 /* Log configuration */
57 #include "coap-log.h"
58 #define LOG_MODULE "coap"
59 #define LOG_LEVEL LOG_LEVEL_COAP
60 
61 MEMB(obs_subjects_memb, coap_observee_t, COAP_MAX_OBSERVEES);
62 LIST(obs_subjects_list);
63 
64 /*----------------------------------------------------------------------------*/
65 static size_t
66 get_token(coap_message_t *coap_pkt, const uint8_t **token)
67 {
68  *token = coap_pkt->token;
69 
70  return coap_pkt->token_len;
71 }
72 /*----------------------------------------------------------------------------*/
73 static int
74 set_token(coap_message_t *coap_pkt, const uint8_t *token, size_t token_len)
75 {
76  coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
77  memcpy(coap_pkt->token, token, coap_pkt->token_len);
78 
79  return coap_pkt->token_len;
80 }
81 /*----------------------------------------------------------------------------*/
82 coap_observee_t *
83 coap_obs_add_observee(const coap_endpoint_t *endpoint,
84  const uint8_t *token, size_t token_len, const char *url,
85  notification_callback_t notification_callback,
86  void *data)
87 {
88  coap_observee_t *o;
89 
90  /* Remove existing observe relationship, if any. */
91  coap_obs_remove_observee_by_url(endpoint, url);
92  o = memb_alloc(&obs_subjects_memb);
93  if(o) {
94  o->url = url;
95  coap_endpoint_copy(&o->endpoint, endpoint);
96  o->token_len = token_len;
97  memcpy(o->token, token, token_len);
98  /* o->last_mid = 0; */
99  o->notification_callback = notification_callback;
100  o->data = data;
101  LOG_DBG("Adding obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
102  o->token[1]);
103  list_add(obs_subjects_list, o);
104  }
105 
106  return o;
107 }
108 /*----------------------------------------------------------------------------*/
109 void
110 coap_obs_remove_observee(coap_observee_t *o)
111 {
112  LOG_DBG("Removing obs_subject for /%s [0x%02X%02X]\n", o->url, o->token[0],
113  o->token[1]);
114  memb_free(&obs_subjects_memb, o);
115  list_remove(obs_subjects_list, o);
116 }
117 /*----------------------------------------------------------------------------*/
118 coap_observee_t *
119 coap_get_obs_subject_by_token(const uint8_t *token, size_t token_len)
120 {
121  coap_observee_t *obs = NULL;
122 
123  for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
124  obs = obs->next) {
125  LOG_DBG("Looking for token 0x%02X%02X\n", token[0], token[1]);
126  if(obs->token_len == token_len
127  && memcmp(obs->token, token, token_len) == 0) {
128  return obs;
129  }
130  }
131 
132  return NULL;
133 }
134 /*----------------------------------------------------------------------------*/
135 int
136 coap_obs_remove_observee_by_token(const coap_endpoint_t *endpoint,
137  uint8_t *token, size_t token_len)
138 {
139  int removed = 0;
140  coap_observee_t *obs = NULL;
141 
142  for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
143  obs = obs->next) {
144  LOG_DBG("Remove check Token 0x%02X%02X\n", token[0], token[1]);
145  if(coap_endpoint_cmp(&obs->endpoint, endpoint)
146  && obs->token_len == token_len
147  && memcmp(obs->token, token, token_len) == 0) {
148  coap_obs_remove_observee(obs);
149  removed++;
150  }
151  }
152  return removed;
153 }
154 /*----------------------------------------------------------------------------*/
155 int
156 coap_obs_remove_observee_by_url(const coap_endpoint_t *endpoint,
157  const char *url)
158 {
159  int removed = 0;
160  coap_observee_t *obs = NULL;
161 
162  for(obs = (coap_observee_t *)list_head(obs_subjects_list); obs;
163  obs = obs->next) {
164  LOG_DBG("Remove check URL %s\n", url);
165  if(coap_endpoint_cmp(&obs->endpoint, endpoint)
166  && (obs->url == url || memcmp(obs->url, url, strlen(obs->url)) == 0)) {
167  coap_obs_remove_observee(obs);
168  removed++;
169  }
170  }
171  return removed;
172 }
173 /*----------------------------------------------------------------------------*/
174 static void
175 simple_reply(coap_message_type_t type, const coap_endpoint_t *endpoint,
176  coap_message_t *notification)
177 {
178  static coap_message_t response[1];
179  size_t len;
180 
181  coap_init_message(response, type, NO_ERROR, notification->mid);
182  len = coap_serialize_message(response, coap_databuf());
183  coap_sendto(endpoint, coap_databuf(), len);
184 }
185 /*----------------------------------------------------------------------------*/
186 static coap_notification_flag_t
187 classify_notification(coap_message_t *response, int first)
188 {
189  if(!response) {
190  LOG_DBG("no response\n");
191  return NO_REPLY_FROM_SERVER;
192  }
193  LOG_DBG("server replied\n");
194  if(!IS_RESPONSE_CODE_2_XX(response)) {
195  LOG_DBG("error response code\n");
196  return ERROR_RESPONSE_CODE;
197  }
198  if(!coap_is_option(response, COAP_OPTION_OBSERVE)) {
199  LOG_DBG("server does not support observe\n");
200  return OBSERVE_NOT_SUPPORTED;
201  }
202  if(first) {
203  return OBSERVE_OK;
204  }
205  return NOTIFICATION_OK;
206 }
207 /*----------------------------------------------------------------------------*/
208 void
209 coap_handle_notification(const coap_endpoint_t *endpoint,
210  coap_message_t *notification)
211 {
212  const uint8_t *token;
213  int token_len;
214  coap_observee_t *obs;
215  coap_notification_flag_t flag;
216  uint32_t observe;
217 
218  LOG_DBG("coap_handle_notification()\n");
219  token_len = get_token(notification, &token);
220  LOG_DBG("Getting token\n");
221  if(0 == token_len) {
222  LOG_DBG("Error while handling coap observe notification: "
223  "no token in message\n");
224  return;
225  }
226  LOG_DBG("Getting observee info\n");
227  obs = coap_get_obs_subject_by_token(token, token_len);
228  if(NULL == obs) {
229  LOG_DBG("Error while handling coap observe notification: "
230  "no matching token found\n");
231  simple_reply(COAP_TYPE_RST, endpoint, notification);
232  return;
233  }
234  if(notification->type == COAP_TYPE_CON) {
235  simple_reply(COAP_TYPE_ACK, endpoint, notification);
236  }
237  if(obs->notification_callback != NULL) {
238  flag = classify_notification(notification, 0);
239  /* TODO: the following mechanism for discarding duplicates is too trivial */
240  /* refer to Observe RFC for a better solution */
241  if(flag == NOTIFICATION_OK) {
242  coap_get_header_observe(notification, &observe);
243  if(observe == obs->last_observe) {
244  LOG_DBG("Discarding duplicate\n");
245  return;
246  }
247  obs->last_observe = observe;
248  }
249  obs->notification_callback(obs, notification, flag);
250  }
251 }
252 /*----------------------------------------------------------------------------*/
253 static void
254 handle_obs_registration_response(void *data, coap_message_t *response)
255 {
256  coap_observee_t *obs;
257  notification_callback_t notification_callback;
258  coap_notification_flag_t flag;
259 
260  LOG_DBG("handle_obs_registration_response()\n");
261  obs = (coap_observee_t *)data;
262  notification_callback = obs->notification_callback;
263  flag = classify_notification(response, 1);
264  if(notification_callback) {
265  notification_callback(obs, response, flag);
266  }
267  if(flag != OBSERVE_OK) {
268  coap_obs_remove_observee(obs);
269  }
270 }
271 /*----------------------------------------------------------------------------*/
272 uint8_t
273 coap_generate_token(uint8_t **token_ptr)
274 {
275  static uint8_t token = 0;
276 
277  token++;
278  /* FIXME: we should check that this token is not already used */
279  *token_ptr = (uint8_t *)&token;
280  return sizeof(token);
281 }
282 /*----------------------------------------------------------------------------*/
283 coap_observee_t *
284 coap_obs_request_registration(const coap_endpoint_t *endpoint, char *uri,
285  notification_callback_t notification_callback,
286  void *data)
287 {
288  coap_message_t request[1];
289  coap_transaction_t *t;
290  uint8_t *token;
291  uint8_t token_len;
292  coap_observee_t *obs;
293 
294  obs = NULL;
295  coap_init_message(request, COAP_TYPE_CON, COAP_GET, coap_get_mid());
296  coap_set_header_uri_path(request, uri);
297  coap_set_header_observe(request, 0);
298  token_len = coap_generate_token(&token);
299  set_token(request, token, token_len);
300  t = coap_new_transaction(request->mid, endpoint);
301  if(t) {
302  obs = coap_obs_add_observee(endpoint, (uint8_t *)token, token_len, uri,
303  notification_callback, data);
304  if(obs) {
305  t->callback = handle_obs_registration_response;
306  t->callback_data = obs;
307  t->message_len = coap_serialize_message(request, t->message);
308  coap_send_transaction(t);
309  } else {
310  LOG_DBG("Could not allocate obs_subject resource buffer\n");
311  coap_clear_transaction(t);
312  }
313  } else {
314  LOG_DBG("Could not allocate transaction buffer\n");
315  }
316  return obs;
317 }
318 #endif /* COAP_OBSERVE_CLIENT */
319 /** @} */
Log support for CoAP
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:78
Linked list manipulation routines.
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
Send a message to the specified CoAP endpoint.
Definition: coap-uip.c:369
Memory block allocation routines.
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:142
#define LIST(name)
Declare a linked list.
Definition: list.h:89
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
void coap_endpoint_copy(coap_endpoint_t *dest, const coap_endpoint_t *src)
Copy a CoAP endpoint from one memory area to another.
Definition: coap-uip.c:154
uint8_t * coap_databuf(void)
Returns a common data buffer that can be used when generating CoAP messages for transmission.
Definition: coap-uip.c:322
An implementation of the Constrained Application Protocol (RFC 7252).
Default definitions of C compiler quirk work-arounds.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:237
int coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2)
Compare two CoAP endpoints.
Definition: coap-uip.c:163
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:90