libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
resource.c
Go to the documentation of this file.
1 /* resource.c -- generic resource handling
2  *
3  * Copyright (C) 2010--2014 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "config.h"
10 #include "net.h"
11 #include "debug.h"
12 #include "resource.h"
13 #include "subscribe.h"
14 
15 #ifdef WITH_LWIP
16 #include "utlist.h"
17 /* mem.h is only needed for the string free calls for
18  * COAP_ATTR_FLAGS_RELEASE_NAME / COAP_ATTR_FLAGS_RELEASE_VALUE /
19  * COAP_RESOURCE_FLAGS_RELEASE_URI. not sure what those lines should actually
20  * do on lwip. */
21 #include "mem.h"
22 
23 #include <lwip/memp.h>
24 
25 #define COAP_MALLOC_TYPE(Type) \
26  ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
27 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
28 
29 #endif
30 #ifdef WITH_POSIX
31 #include "utlist.h"
32 #include "mem.h"
33 
34 #define COAP_MALLOC_TYPE(Type) \
35  ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
36 #define COAP_FREE_TYPE(Type, Object) coap_free(Object)
37 
38 #endif /* WITH_POSIX */
39 #ifdef WITH_CONTIKI
40 #include "memb.h"
41 
42 MEMB(resource_storage, coap_resource_t, COAP_MAX_RESOURCES);
43 MEMB(attribute_storage, coap_attr_t, COAP_MAX_ATTRIBUTES);
44 MEMB(subscription_storage, coap_subscription_t, COAP_MAX_SUBSCRIBERS);
45 
46 void
47 coap_resources_init() {
48  memb_init(&resource_storage);
49  memb_init(&attribute_storage);
50  memb_init(&subscription_storage);
51 }
52 
53 static inline coap_subscription_t *
54 coap_malloc_subscription() {
55  return memb_alloc(&subscription_storage);
56 }
57 
58 static inline void
59 coap_free_subscription(coap_subscription_t *subscription) {
60  memb_free(&subscription_storage, subscription);
61 }
62 #endif /* WITH_CONTIKI */
63 
64 #define min(a,b) ((a) < (b) ? (a) : (b))
65 
66 /* Helper functions for conditional output of character sequences into
67  * a given buffer. The first Offset characters are skipped.
68  */
69 
74 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \
75  if ((Offset) == 0) { \
76  (*(Buf)++) = (Char); \
77  } else { \
78  (Offset)--; \
79  } \
80 
81 
84 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
85  if ((Buf) < (Bufend)) { \
86  PRINT_WITH_OFFSET(Buf,Offset,Char); \
87  } \
88  (Result)++; \
89  }
90 
96 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
97  size_t i; \
98  for (i = 0; i < (Length); i++) { \
99  PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
100  } \
101  }
102 
103 int
104 match(const str *text, const str *pattern, int match_prefix, int match_substring) {
105  assert(text); assert(pattern);
106 
107  if (text->length < pattern->length)
108  return 0;
109 
110  if (match_substring) {
111  unsigned char *next_token = text->s;
112  size_t remaining_length = text->length;
113  while (remaining_length) {
114  size_t token_length;
115  unsigned char *token = next_token;
116  next_token = memchr(token, ' ', remaining_length);
117 
118  if (next_token) {
119  token_length = next_token - token;
120  remaining_length -= (token_length + 1);
121  next_token++;
122  } else {
123  token_length = remaining_length;
124  remaining_length = 0;
125  }
126 
127  if ((match_prefix || pattern->length == token_length) &&
128  memcmp(token, pattern->s, pattern->length) == 0)
129  return 1;
130  }
131  return 0;
132  }
133 
134  return (match_prefix || pattern->length == text->length) &&
135  memcmp(text->s, pattern->s, pattern->length) == 0;
136 }
137 
159 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
161 print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
162  size_t offset,
163  coap_opt_t *query_filter __attribute__ ((unused))) {
164 #else /* not a GCC */
166 print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
167  size_t offset, coap_opt_t *query_filter) {
168 #endif /* GCC */
169  coap_resource_t *r;
170  unsigned char *p = buf;
171  const unsigned char *bufend = buf + *buflen;
172  size_t left, written = 0;
173  coap_print_status_t result;
174  const size_t old_offset = offset;
175  int subsequent_resource = 0;
176 #ifndef COAP_RESOURCES_NOHASH
177  coap_resource_t *tmp;
178 #endif
179 #ifndef WITHOUT_QUERY_FILTER
180  str resource_param = { 0, NULL }, query_pattern = { 0, NULL };
181  int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
182 #define MATCH_URI 0x01
183 #define MATCH_PREFIX 0x02
184 #define MATCH_SUBSTRING 0x04
185  static const str _rt_attributes[] = {
186  {2, (unsigned char *)"rt"},
187  {2, (unsigned char *)"if"},
188  {3, (unsigned char *)"rel"},
189  {0, NULL}};
190 #endif /* WITHOUT_QUERY_FILTER */
191 
192 #ifdef WITH_CONTIKI
193  int i;
194 #endif /* WITH_CONTIKI */
195 
196 #ifndef WITHOUT_QUERY_FILTER
197  /* split query filter, if any */
198  if (query_filter) {
199  resource_param.s = COAP_OPT_VALUE(query_filter);
200  while (resource_param.length < COAP_OPT_LENGTH(query_filter)
201  && resource_param.s[resource_param.length] != '=')
202  resource_param.length++;
203 
204  if (resource_param.length < COAP_OPT_LENGTH(query_filter)) {
205  const str *rt_attributes;
206  if (resource_param.length == 4 &&
207  memcmp(resource_param.s, "href", 4) == 0)
208  flags |= MATCH_URI;
209 
210  for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
211  if (resource_param.length == rt_attributes->length &&
212  memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
213  flags |= MATCH_SUBSTRING;
214  break;
215  }
216  }
217 
218  /* rest is query-pattern */
219  query_pattern.s =
220  COAP_OPT_VALUE(query_filter) + resource_param.length + 1;
221 
222  assert((resource_param.length + 1) <= COAP_OPT_LENGTH(query_filter));
223  query_pattern.length =
224  COAP_OPT_LENGTH(query_filter) - (resource_param.length + 1);
225 
226  if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
227  query_pattern.s++;
228  query_pattern.length--;
229  }
230 
231  if (query_pattern.length &&
232  query_pattern.s[query_pattern.length-1] == '*') {
233  query_pattern.length--;
234  flags |= MATCH_PREFIX;
235  }
236  }
237  }
238 #endif /* WITHOUT_QUERY_FILTER */
239 
240 #ifndef WITH_CONTIKI
241 
242 #ifdef COAP_RESOURCES_NOHASH
243  LL_FOREACH(context->resources, r) {
244 #else
245  HASH_ITER(hh, context->resources, r, tmp) {
246 #endif
247 #else /* WITH_CONTIKI */
248  r = (coap_resource_t *)resource_storage.mem;
249  for (i = 0; i < resource_storage.num; ++i, ++r) {
250  if (!resource_storage.count[i])
251  continue;
252 #endif /* WITH_CONTIKI */
253 
254 #ifndef WITHOUT_QUERY_FILTER
255  if (resource_param.length) { /* there is a query filter */
256 
257  if (flags & MATCH_URI) { /* match resource URI */
258  if (!match(&r->uri, &query_pattern, (flags & MATCH_PREFIX) != 0, (flags & MATCH_SUBSTRING) != 0))
259  continue;
260  } else { /* match attribute */
261  coap_attr_t *attr;
262  str unquoted_val;
263  attr = coap_find_attr(r, resource_param.s, resource_param.length);
264  if (!attr) continue;
265  if (attr->value.s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
266  unquoted_val.length = attr->value.length - 2;
267  unquoted_val.s = attr->value.s + 1;
268  } else {
269  unquoted_val = attr->value;
270  }
271  if (!(match(&unquoted_val, &query_pattern,
272  (flags & MATCH_PREFIX) != 0,
273  (flags & MATCH_SUBSTRING) != 0)))
274  continue;
275  }
276  }
277 #endif /* WITHOUT_QUERY_FILTER */
278 
279  if (!subsequent_resource) { /* this is the first resource */
280  subsequent_resource = 1;
281  } else {
282  PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
283  }
284 
285  left = bufend - p; /* calculate available space */
286  result = coap_print_link(r, p, &left, &offset);
287 
288  if (result & COAP_PRINT_STATUS_ERROR) {
289  break;
290  }
291 
292  /* coap_print_link() returns the number of characters that
293  * where actually written to p. Now advance to its end. */
294  p += COAP_PRINT_OUTPUT_LENGTH(result);
295  written += left;
296  }
297 
298  *buflen = written;
299  result = p - buf;
300  if (result + old_offset - offset < *buflen) {
301  result |= COAP_PRINT_STATUS_TRUNC;
302  }
303  return result;
304 }
305 
307 coap_resource_init(const unsigned char *uri, size_t len, int flags) {
308  coap_resource_t *r;
309 
310 #ifdef WITH_POSIX
312 #endif
313 #ifdef WITH_LWIP
314  r = (coap_resource_t *)memp_malloc(MEMP_COAP_RESOURCE);
315 #endif
316 #ifdef WITH_CONTIKI
317  r = (coap_resource_t *)memb_alloc(&resource_storage);
318 #endif
319  if (r) {
320  memset(r, 0, sizeof(coap_resource_t));
321 
322 #ifdef WITH_CONTIKI
323  LIST_STRUCT_INIT(r, link_attr);
324 #endif /* WITH_CONTIKI */
325  LIST_STRUCT_INIT(r, subscribers);
326 
327  r->uri.s = (unsigned char *)uri;
328  r->uri.length = len;
329 
330  coap_hash_path(r->uri.s, r->uri.length, r->key);
331 
332  r->flags = flags;
333  } else {
334  debug("coap_resource_init: no memory left\n");
335  }
336 
337  return r;
338 }
339 
340 coap_attr_t *
342  const unsigned char *name, size_t nlen,
343  const unsigned char *val, size_t vlen,
344  int flags) {
345  coap_attr_t *attr;
346 
347  if (!resource || !name)
348  return NULL;
349 
350 #ifdef WITH_POSIX
351  attr = (coap_attr_t *)coap_malloc(sizeof(coap_attr_t));
352 #endif
353 #ifdef WITH_LWIP
354  attr = (coap_attr_t *)memp_malloc(MEMP_COAP_RESOURCEATTR);
355 #endif
356 #ifdef WITH_CONTIKI
357  attr = (coap_attr_t *)memb_alloc(&attribute_storage);
358 #endif
359 
360  if (attr) {
361  attr->name.length = nlen;
362  attr->value.length = val ? vlen : 0;
363 
364  attr->name.s = (unsigned char *)name;
365  attr->value.s = (unsigned char *)val;
366 
367  attr->flags = flags;
368 
369  /* add attribute to resource list */
370 #ifndef WITH_CONTIKI
371  LL_PREPEND(resource->link_attr, attr);
372 #else /* WITH_CONTIKI */
373  list_add(resource->link_attr, attr);
374 #endif /* WITH_CONTIKI */
375  } else {
376  debug("coap_add_attr: no memory left\n");
377  }
378 
379  return attr;
380 }
381 
382 coap_attr_t *
384  const unsigned char *name, size_t nlen) {
385  coap_attr_t *attr;
386 
387  if (!resource || !name)
388  return NULL;
389 
390 #ifndef WITH_CONTIKI
391  LL_FOREACH(resource->link_attr, attr) {
392 #else /* WITH_CONTIKI */
393  for (attr = list_head(resource->link_attr); attr;
394  attr = list_item_next(attr)) {
395 #endif /* WITH_CONTIKI */
396  if (attr->name.length == nlen &&
397  memcmp(attr->name.s, name, nlen) == 0)
398  return attr;
399  }
400 
401  return NULL;
402 }
403 
404 void
406  if (!attr)
407  return;
409  coap_free(attr->name.s);
411  coap_free(attr->value.s);
412 #ifdef POSIX
413  coap_free(attr);
414 #endif
415 #ifdef WITH_LWIP
416  memp_free(MEMP_COAP_RESOURCEATTR, attr);
417 #endif
418 #ifdef WITH_CONTIKI
419  /* FIXME it looks like this was never implemented */
420 #endif
421 }
422 
423 void
425  coap_opt_iterator_t opt_iter;
426  coap_opt_filter_t filter;
427  coap_opt_t *option;
428 
429  memset(key, 0, sizeof(coap_key_t));
430 
431  coap_option_filter_clear(filter);
433 
434  coap_option_iterator_init((coap_pdu_t *)request, &opt_iter, filter);
435  while ((option = coap_option_next(&opt_iter)))
436  coap_hash(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option), key);
437 }
438 
439 void
441 #ifndef WITH_CONTIKI
442 #ifdef COAP_RESOURCES_NOHASH
443  LL_PREPEND(context->resources, resource);
444 #else
445  HASH_ADD(hh, context->resources, key, sizeof(coap_key_t), resource);
446 #endif
447 #endif /* WITH_CONTIKI */
448 }
449 
450 int
452  coap_resource_t *resource;
453  coap_attr_t *attr, *tmp;
454 #ifdef WITH_CONTIKI
455  coap_subscription_t *obs;
456 #endif
457 
458  if (!context)
459  return 0;
460 
461  resource = coap_get_resource_from_key(context, key);
462 
463  if (!resource)
464  return 0;
465 
466 #if defined(WITH_POSIX) || defined(WITH_LWIP)
467 #ifdef COAP_RESOURCES_NOHASH
468  LL_DELETE(context->resources, resource);
469 #else
470  HASH_DELETE(hh, context->resources, resource);
471 #endif
472 
473  /* delete registered attributes */
474  LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
475 
476  if (resource->flags & COAP_RESOURCE_FLAGS_RELEASE_URI)
477  coap_free(resource->uri.s);
478 
479 #ifdef WITH_POSIX
480  coap_free(resource);
481 #endif
482 #ifdef WITH_LWIP
483  memp_free(MEMP_COAP_RESOURCE, resource);
484 #endif
485 #else /* not (WITH_POSIX || WITH_LWIP) */
486  /* delete registered attributes */
487  while ( (attr = list_pop(resource->link_attr)) )
488  memb_free(&attribute_storage, attr);
489 
490  /* delete subscribers */
491  while ( (obs = list_pop(resource->subscribers)) ) {
492  /* FIXME: notify observer that its subscription has been removed */
493  memb_free(&subscription_storage, obs);
494  }
495 
496  memb_free(&resource_storage, resource);
497 #endif /* WITH_CONTIKI */
498 
499  return 1;
500 }
501 
504 #ifndef WITH_CONTIKI
505  coap_resource_t *resource;
506 #ifdef COAP_RESOURCES_NOHASH
507  resource = NULL;
508  LL_FOREACH(context->resources, resource) {
509  /* if you think you can outspart the compiler and speed things up by (eg by
510  * casting to uint32* and comparing alues), increment this counter: 1 */
511  if (memcmp(key, resource->key, sizeof(coap_key_t)) == 0)
512  return resource;
513  }
514  return NULL;
515 #else
516  HASH_FIND(hh, context->resources, key, sizeof(coap_key_t), resource);
517 
518  return resource;
519 #endif
520 #else /* WITH_CONTIKI */
521  int i;
522  coap_resource_t *ptr2;
523 
524  /* the search function is basically taken from memb.c */
525  ptr2 = (coap_resource_t *)resource_storage.mem;
526  for (i = 0; i < resource_storage.num; ++i) {
527  if (resource_storage.count[i] &&
528  (memcmp(ptr2->key, key, sizeof(coap_key_t)) == 0))
529  return (coap_resource_t *)ptr2;
530  ++ptr2;
531  }
532 
533  return NULL;
534 #endif /* WITH_CONTIKI */
535 }
536 
539  unsigned char *buf, size_t *len, size_t *offset) {
540  unsigned char *p = buf;
541  const unsigned char *bufend = buf + *len;
542  coap_attr_t *attr;
543  coap_print_status_t result = 0;
544  const size_t old_offset = *offset;
545 
546  *len = 0;
547  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
548  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
549 
550  COPY_COND_WITH_OFFSET(p, bufend, *offset,
551  resource->uri.s, resource->uri.length, *len);
552 
553  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
554 
555 #ifndef WITH_CONTIKI
556  LL_FOREACH(resource->link_attr, attr) {
557 #else /* WITH_CONTIKI */
558  for (attr = list_head(resource->link_attr); attr;
559  attr = list_item_next(attr)) {
560 #endif /* WITH_CONTIKI */
561 
562  PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
563 
564  COPY_COND_WITH_OFFSET(p, bufend, *offset,
565  attr->name.s, attr->name.length, *len);
566 
567  if (attr->value.s) {
568  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
569 
570  COPY_COND_WITH_OFFSET(p, bufend, *offset,
571  attr->value.s, attr->value.length, *len);
572  }
573 
574  }
575  if (resource->observable) {
576  COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
577  }
578 
579  result = p - buf;
580  if (result + old_offset - *offset < *len) {
581  result |= COAP_PRINT_STATUS_TRUNC;
582  }
583 
584  return result;
585 }
586 
587 #ifndef WITHOUT_OBSERVE
589 coap_find_observer(coap_resource_t *resource, const coap_address_t *peer,
590  const str *token) {
592 
593  assert(resource);
594  assert(peer);
595 
596  for (s = list_head(resource->subscribers); s; s = list_item_next(s)) {
597  if (coap_address_equals(&s->subscriber, peer)
598  && (!token || (token->length == s->token_length
599  && memcmp(token->s, s->token, token->length) == 0)))
600  return s;
601  }
602 
603  return NULL;
604 }
605 
608  const coap_address_t *observer,
609  const str *token) {
611 
612  assert(observer);
613 
614  /* Check if there is already a subscription for this peer. */
615  s = coap_find_observer(resource, observer, token);
616 
617  /* We are done if subscription was found. */
618  if (s)
619  return s;
620 
621  /* s points to a different subscription, so we have to create
622  * another one. */
623  s = COAP_MALLOC_TYPE(subscription);
624 
625  if (!s)
626  return NULL;
627 
629  memcpy(&s->subscriber, observer, sizeof(coap_address_t));
630 
631  if (token && token->length) {
632  s->token_length = token->length;
633  memcpy(s->token, token->s, min(s->token_length, 8));
634  }
635 
636  /* add subscriber to resource */
637  list_add(resource->subscribers, s);
638 
639  return s;
640 }
641 
642 void
643 coap_touch_observer(coap_context_t *context, const coap_address_t *observer,
644  const str *token) {
645  coap_resource_t *r;
647 
648 #ifndef WITH_CONTIKI
649 #ifdef COAP_RESOURCES_NOHASH
650  LL_FOREACH(context->resources, r) {
651 #else
652  coap_resource_t *tmp;
653  HASH_ITER(hh, context->resources, r, tmp) {
654 #endif
655  s = coap_find_observer(r, observer, token);
656  if (s) {
657  s->fail_cnt = 0;
658  }
659  }
660 #else /* WITH_CONTIKI */
661  r = (coap_resource_t *)resource_storage.mem;
662  for (i = 0; i < resource_storage.num; ++i, ++r) {
663  if (resource_storage.count[i]) {
664  s = coap_find_observer(r, observer, token);
665  if (s) {
666  s->fail_cnt = 0;
667  }
668  }
669  }
670 #endif /* WITH_CONTIKI */
671 }
672 
673 void
674 coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer,
675  const str *token) {
677 
678  s = coap_find_observer(resource, observer, token);
679 
680  if (s) {
681  list_remove(resource->subscribers, s);
682 
683  COAP_FREE_TYPE(subscription,s);
684  }
685 }
686 
687 static void
690  coap_subscription_t *obs;
691  str token;
692  coap_pdu_t *response;
693 
694  if (r->observable && (r->dirty || r->partiallydirty)) {
695  r->partiallydirty = 0;
696 
697  /* retrieve GET handler, prepare response */
698  h = r->handler[COAP_REQUEST_GET - 1];
699  assert(h); /* we do not allow subscriptions if no
700  * GET handler is defined */
701 
702  for (obs = list_head(r->subscribers); obs; obs = list_item_next(obs)) {
703  if (r->dirty == 0 && obs->dirty == 0)
704  /* running this resource due to partiallydirty, but this observation's notification was already enqueued */
705  continue;
706 
708  obs->dirty = 0;
709  /* initialize response */
711  if (!response) {
712  obs->dirty = 1;
713  r->partiallydirty = 1;
714  debug("coap_check_notify: pdu init failed, resource stays partially dirty\n");
715  continue;
716  }
717 
718  if (!coap_add_token(response, obs->token_length, obs->token)) {
719  obs->dirty = 1;
720  r->partiallydirty = 1;
721  debug("coap_check_notify: cannot add token, resource stays partially dirty\n");
722  coap_delete_pdu(response);
723  continue;
724  }
725 
726  token.length = obs->token_length;
727  token.s = obs->token;
728 
729  response->hdr->id = coap_new_message_id(context);
730  if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON) {
731  response->hdr->type = COAP_MESSAGE_NON;
732  } else {
733  response->hdr->type = COAP_MESSAGE_CON;
734  }
735  /* fill with observer-specific data */
736  h(context, r, &obs->subscriber, NULL, &token, response);
737 
738  if (response->hdr->type == COAP_MESSAGE_CON) {
739  tid = coap_send_confirmed(context, &obs->subscriber, response);
740  obs->non_cnt = 0;
741  } else {
742  tid = coap_send(context, &obs->subscriber, response);
743  obs->non_cnt++;
744  }
745 
746  if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON)
747  coap_delete_pdu(response);
748  if (COAP_INVALID_TID == tid)
749  {
750  debug("coap_check_notify: sending failed, resource stays partially dirty\n");
751  obs->dirty = 1;
752  r->partiallydirty = 1;
753  }
754 
755  }
756 
757  /* Increment value for next Observe use. */
758  context->observe++;
759  }
760  r->dirty = 0;
761 }
762 
763 void
765  coap_resource_t *r;
766 #ifndef WITH_CONTIKI
767 
768 #ifdef COAP_RESOURCES_NOHASH
769  LL_FOREACH(context->resources, r) {
770 #else
771  coap_resource_t *tmp;
772  HASH_ITER(hh, context->resources, r, tmp) {
773 #endif
774  coap_notify_observers(context, r);
775  }
776 #else /* WITH_CONTIKI */
777  int i;
778 
779  r = (coap_resource_t *)resource_storage.mem;
780  for (i = 0; i < resource_storage.num; ++i, ++r) {
781  if (resource_storage.count[i]) {
782  coap_notify_observers(context, r);
783  }
784  }
785 #endif /* WITH_CONTIKI */
786 }
787 
798 static void
800  coap_resource_t *resource,
801  const coap_address_t *peer,
802  const str *token) {
803  coap_subscription_t *obs;
804 
805  for (obs = list_head(resource->subscribers); obs;
806  obs = list_item_next(obs)) {
807  if (coap_address_equals(peer, &obs->subscriber) &&
808  token->length == obs->token_length &&
809  memcmp(token->s, obs->token, token->length) == 0) {
810 
811  /* count failed notifies and remove when
812  * COAP_MAX_FAILED_NOTIFY is reached */
813  if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
814  obs->fail_cnt++;
815  else {
816  list_remove(resource->subscribers, obs);
817  obs->fail_cnt = 0;
818 
819 #ifndef NDEBUG
820  if (LOG_DEBUG <= coap_get_log_level()) {
821 #ifndef INET6_ADDRSTRLEN
822 #define INET6_ADDRSTRLEN 40
823 #endif
824  unsigned char addr[INET6_ADDRSTRLEN+8];
825 
826  if (coap_print_addr(&obs->subscriber, addr, INET6_ADDRSTRLEN+8))
827  debug("** removed observer %s\n", addr);
828  }
829 #endif
830  coap_cancel_all_messages(context, &obs->subscriber,
831  obs->token, obs->token_length);
832 
833  COAP_FREE_TYPE(subscription, obs);
834  }
835  }
836  break; /* break loop if observer was found */
837  }
838 }
839 
840 void
842  const coap_address_t *peer,
843  const str *token) {
844  coap_resource_t *r;
845 
846 #ifndef WITH_CONTIKI
847 
848 #ifdef COAP_RESOURCES_NOHASH
849  LL_FOREACH(context->resources, r) {
850 #else
851  coap_resource_t *tmp;
852  HASH_ITER(hh, context->resources, r, tmp) {
853 #endif
854  coap_remove_failed_observers(context, r, peer, token);
855  }
856 #else /* WITH_CONTIKI */
857  int i;
858 
859  r = (coap_resource_t *)resource_storage.mem;
860  for (i = 0; i < resource_storage.num; ++i, ++r) {
861  if (resource_storage.count[i]) {
862  coap_remove_failed_observers(context, r, peer, token);
863  }
864  }
865 #endif /* WITH_CONTIKI */
866 }
867 #endif /* WITHOUT_NOTIFY */
#define LL_FOREACH(head, el)
Definition: utlist.h:361
unsigned int observe
The next value to be used for Observe.
Definition: net.h:141
coap_subscription_t * coap_add_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
Adds the specified peer as observer for resource.
Definition: resource.c:607
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:764
unsigned char coap_key_t[4]
Definition: hashkey.h:19
int flags
Definition: resource.h:56
#define LIST_STRUCT_INIT(struct_ptr, name)
Definition: t_list.h:88
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
Definition: resource.c:451
#define INET6_ADDRSTRLEN
coap_print_status_t print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, size_t offset, coap_opt_t *query_filter)
Prints the names of all known resources to buf.
Definition: resource.c:166
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:189
coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a confirmed CoAP message to given destination.
Definition: net.c:699
unsigned short id
Definition: pdu.h:173
int coap_tid_t
Definition: pdu.h:155
#define LL_PREPEND(head, add)
Definition: utlist.h:287
#define COAP_REQUEST_GET
Definition: pdu.h:50
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:897
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
Definition: pdu.c:157
#define COAP_MESSAGE_NON
Definition: pdu.h:44
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:86
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition: resource.h:49
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:63
#define coap_malloc(size)
Definition: mem.h:15
size_t length
Definition: str.h:15
static void coap_remove_failed_observers(coap_context_t *context, coap_resource_t *resource, const coap_address_t *peer, const str *token)
Checks the failure counter for (peer, token) and removes peer from the list of observers for the give...
Definition: resource.c:799
#define MATCH_URI
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:157
coap_log_t coap_get_log_level()
Returns the current log level.
Definition: debug.c:41
static coap_uri_t uri
Definition: client.c:36
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:119
coap_hdr_t * hdr
Definition: pdu.h:209
static void * list_item_next(void *item)
Definition: t_list.h:136
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
Definition: subscribe.h:37
void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key)
Calculates the hash key for the resource requested by the Uri-Options of request. ...
Definition: resource.c:424
#define MATCH_PREFIX
#define MATCH_SUBSTRING
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Definition: debug.c:140
#define debug(...)
Definition: debug.h:55
str name
Definition: resource.h:54
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:194
#define COAP_INVALID_TID
Definition: pdu.h:156
coap_attr_t * coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen, const unsigned char *val, size_t vlen, int flags)
Registers a new attribute with the given resource.
Definition: resource.c:341
size_t token_length
actual length of token
Definition: subscribe.h:50
void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst, const unsigned char *token, size_t token_length)
Cancels all outstanding messages for peer dst that have the specified token.
Definition: net.c:1002
str uri
Request URI for this resource.
Definition: resource.h:96
#define coap_free(size)
Definition: mem.h:16
Header structure for CoAP PDUs.
Definition: pdu.h:206
coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list...
Definition: option.c:118
static void list_add(list_t the_list, void *item)
Definition: t_list.h:105
coap_key_t key
the actual key bytes for this resource
Definition: resource.h:75
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:62
static void list_remove(list_t the_list, void *item)
Definition: t_list.h:99
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:50
unsigned int observable
can be observed
Definition: resource.h:64
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:440
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
Definition: subscribe.h:46
Iterator to run through PDU options.
Definition: option.h:169
#define COAP_MESSAGE_CON
Definition: pdu.h:43
str value
Definition: resource.h:55
generic resource handling
#define COAP_MAX_PDU_SIZE
Definition: pdu.h:27
unsigned char coap_opt_filter_t[(COAP_MAX_OPT >> 3)+1]
Fixed-size bit-vector we use for option filtering.
Definition: option.h:93
#define coap_hash(String, Length, Result)
Definition: hashkey.h:34
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
Definition: resource.c:688
static void * list_pop(list_t the_list)
Definition: t_list.h:116
coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:653
int match(const str *text, const str *pattern, int match_prefix, int match_substring)
Definition: resource.c:104
int flags
Definition: client.c:26
coap_subscription_t * coap_find_observer(coap_resource_t *resource, const coap_address_t *peer, const str *token)
Returns a subscription object for given peer.
Definition: resource.c:589
Subscriber information.
Definition: subscribe.h:41
void coap_delete_pdu(coap_pdu_t *pdu)
Definition: pdu.c:143
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
Definition: pdu.c:82
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource's partiallydirty fl...
Definition: subscribe.h:48
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:104
int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key)
Calculates a hash over the given path and stores the result in key.
Definition: uri.c:454
Definition: str.h:14
static void * list_head(list_t the_list)
Definition: t_list.h:94
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:364
#define PRINT_COND_WITH_OFFSET(Buf, Bufend, Offset, Char, Result)
Adds Char to Buf if Offset is zero and Buf is less than Bufend.
Definition: resource.c:84
#define COAP_OPT_LENGTH(opt)
Definition: option.h:305
static unsigned short coap_new_message_id(coap_context_t *context)
Returns a new message id and updates context->message_id accordingly.
Definition: net.h:197
coap_print_status_t coap_print_link(const coap_resource_t *resource, unsigned char *buf, size_t *len, size_t *offset)
Writes a description of this resource in link-format to given text buffer.
Definition: resource.c:538
coap_attr_t * coap_find_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen)
Returns resource's coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:383
coap_resource_t * coap_resource_init(const unsigned char *uri, size_t len, int flags)
Creates a new resource object and initializes the link field to the string of length len...
Definition: resource.c:307
#define COAP_OPTION_URI_PATH
Definition: pdu.h:63
void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token)
Marks an observer as alive.
Definition: resource.c:643
#define LL_DELETE(head, del)
Definition: utlist.h:306
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:190
#define HASH_ADD(hh, head, fieldname, keylen_in, add)
Definition: uthash.h:149
unsigned int coap_print_status_t
Status word to encode the result of conditional print or copy operations such as coap_print_link().
Definition: resource.h:186
unsigned int type
Definition: pdu.h:170
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:26
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
Definition: resource.c:96
unsigned int fail_cnt
up to 3 confirmable notifies can fail
Definition: subscribe.h:47
#define min(a, b)
Definition: resource.c:64
unsigned char token[8]
token used for subscription
Definition: subscribe.h:51
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:405
void coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
Removes any subscription for observer from resource and releases the allocated storage.
Definition: resource.c:674
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *, str *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:46
#define HASH_FIND(hh, head, keyptr, keylen, out)
Definition: uthash.h:84
unsigned char * s
Definition: str.h:16
#define COAP_PRINT_STATUS_TRUNC
Definition: resource.h:191
void coap_subscription_init(coap_subscription_t *s)
Definition: subscribe.c:30
#define COAP_RESOURCE_FLAGS_RELEASE_URI
Definition: resource.h:59
void coap_handle_failed_notify(coap_context_t *context, const coap_address_t *peer, const str *token)
Definition: resource.c:841
unsigned int non
send non-confirmable notifies if 1
Definition: subscribe.h:45
coap_address_t subscriber
address and port of subscriber
Definition: subscribe.h:43
#define COAP_OPT_VALUE(opt)
Definition: option.h:318
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:97
static int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.h:149
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
Definition: subscribe.h:28
coap_resource_t * coap_get_resource_from_key(coap_context_t *context, coap_key_t key)
Returns the resource identified by the unique string key.
Definition: resource.c:503
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:100
coap_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
Definition: resource.h:73