iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / libcoap-4.1.1 / resource.c
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 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
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 || WITH_ARDUINO */
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
70 /**
71  * Adds Char to Buf if Offset is zero. Otherwise, Char is not written
72  * and Offset is decremented.
73  */
74 #define PRINT_WITH_OFFSET(Buf,Offset,Char)        \
75   if ((Offset) == 0) {                    \
76     (*(Buf)++) = (Char);                \
77   } else {                        \
78     (Offset)--;                        \
79   }                            \
80
81 /**
82  * Adds Char to Buf if Offset is zero and Buf is less than Bufend.
83  */
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
91 /**
92  * Copies at most Length characters of Str to Buf. The first Offset
93  * characters are skipped. Output may be truncated to Bufend - Buf
94  * characters.
95  */
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 = (unsigned char *)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
138 /**
139  * Prints the names of all known resources to @p buf. This function
140  * sets @p buflen to the number of bytes actually written and returns
141  * @c 1 on succes. On error, the value in @p buflen is undefined and
142  * the return value will be @c 0.
143  *
144  * @param context The context with the resource map.
145  * @param buf     The buffer to write the result.
146  * @param buflen  Must be initialized to the maximum length of @p buf and will be
147  *                set to the length of the well-known response on return.
148  * @param offset  The offset in bytes where the output shall start and is
149  *                shifted accordingly with the characters that have been
150  *                processed. This parameter is used to support the block
151  *                option.
152  * @param query_filter A filter query according to <a href="http://tools.ietf.org/html/draft-ietf-core-link-format-11#section-4.1">Link Format</a>
153  *
154  * @return COAP_PRINT_STATUS_ERROR on error. Otherwise, the lower 28 bits are
155  *         set to the number of bytes that have actually been written to
156  *         @p buf. COAP_PRINT_STATUS_TRUNC is set when the output has been
157  *         truncated.
158  */
159 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
160 coap_print_status_t
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 */
165 coap_print_status_t
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
306 coap_resource_t *
307 coap_resource_init(const unsigned char *uri, size_t len, int flags) {
308   coap_resource_t *r;
309
310 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
311   r = (coap_resource_t *)coap_malloc(sizeof(coap_resource_t));
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 *
341 coap_add_attr(coap_resource_t *resource,
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 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
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 *
383 coap_find_attr(coap_resource_t *resource,
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
405 coap_delete_attr(coap_attr_t *attr) {
406   if (!attr)
407     return;
408   if (attr->flags & COAP_ATTR_FLAGS_RELEASE_NAME)
409     coap_free(attr->name.s);
410   if (attr->flags & COAP_ATTR_FLAGS_RELEASE_VALUE)
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
424 coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key) {
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);
432   coap_option_setb(filter, COAP_OPTION_URI_PATH);
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
440 coap_add_resource(coap_context_t *context, coap_resource_t *resource) {
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
451 coap_delete_resource(coap_context_t *context, coap_key_t key) {
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) || defined(WITH_ARDUINO)
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 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
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 || WITH_ARDUINO) */
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
502 coap_resource_t *
503 coap_get_resource_from_key(coap_context_t *context, coap_key_t key) {
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
537 coap_print_status_t
538 coap_print_link(const coap_resource_t *resource,
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
588 coap_subscription_t *
589 coap_find_observer(coap_resource_t *resource, const coap_address_t *peer,
590              const str *token) {
591   coap_subscription_t *s;
592
593   assert(resource);
594   assert(peer);
595
596   for (s = (coap_subscription_t*)list_head(resource->subscribers); s;
597         s = (coap_subscription_t*)list_item_next(s)) {
598     if (coap_address_equals(&s->subscriber, peer)
599     && (!token || (token->length == s->token_length
600                && memcmp(token->s, s->token, token->length) == 0)))
601       return s;
602   }
603
604   return NULL;
605 }
606
607 coap_subscription_t *
608 coap_add_observer(coap_resource_t *resource,
609           const coap_address_t *observer,
610           const str *token) {
611   coap_subscription_t *s;
612
613   assert(observer);
614
615   /* Check if there is already a subscription for this peer. */
616   s = coap_find_observer(resource, observer, token);
617
618   /* We are done if subscription was found. */
619   if (s)
620     return s;
621
622   /* s points to a different subscription, so we have to create
623    * another one. */
624   s = COAP_MALLOC_TYPE(subscription);
625
626   if (!s)
627     return NULL;
628
629   coap_subscription_init(s);
630   memcpy(&s->subscriber, observer, sizeof(coap_address_t));
631
632   if (token && token->length) {
633     s->token_length = token->length;
634     memcpy(s->token, token->s, min(s->token_length, 8));
635   }
636
637   /* add subscriber to resource */
638   list_add(resource->subscribers, s);
639
640   return s;
641 }
642
643 void
644 coap_touch_observer(coap_context_t *context, const coap_address_t *observer,
645             const str *token) {
646   coap_resource_t *r;
647   coap_subscription_t *s;
648
649 #ifndef WITH_CONTIKI
650 #ifdef COAP_RESOURCES_NOHASH
651   LL_FOREACH(context->resources, r) {
652 #else
653   coap_resource_t *tmp;
654   HASH_ITER(hh, context->resources, r, tmp) {
655 #endif
656     s = coap_find_observer(r, observer, token);
657     if (s) {
658       s->fail_cnt = 0;
659     }
660   }
661 #else /* WITH_CONTIKI */
662   r = (coap_resource_t *)resource_storage.mem;
663   for (i = 0; i < resource_storage.num; ++i, ++r) {
664     if (resource_storage.count[i]) {
665       s = coap_find_observer(r, observer, token);
666       if (s) {
667     s->fail_cnt = 0;
668       }
669     }
670   }
671 #endif /* WITH_CONTIKI */
672 }
673
674 void
675 coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer,
676              const str *token) {
677   coap_subscription_t *s;
678
679   s = coap_find_observer(resource, observer, token);
680
681   if (s) {
682     list_remove(resource->subscribers, s);
683
684     COAP_FREE_TYPE(subscription,s);
685   }
686 }
687
688 static void
689 coap_notify_observers(coap_context_t *context, coap_resource_t *r) {
690   coap_method_handler_t h;
691   coap_subscription_t *obs;
692   str token;
693   coap_pdu_t *response;
694
695   if (r->observable && (r->dirty || r->partiallydirty)) {
696     r->partiallydirty = 0;
697
698     /* retrieve GET handler, prepare response */
699     h = r->handler[COAP_REQUEST_GET - 1];
700     assert(h);        /* we do not allow subscriptions if no
701              * GET handler is defined */
702
703     for (obs = (coap_subscription_t*)list_head(r->subscribers); obs;
704             obs = (coap_subscription_t*)list_item_next(obs)) {
705       if (r->dirty == 0 && obs->dirty == 0)
706         /* running this resource due to partiallydirty, but this observation's notification was already enqueued */
707         continue;
708
709       coap_tid_t tid = COAP_INVALID_TID;
710       obs->dirty = 0;
711       /* initialize response */
712       response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE);
713       if (!response) {
714         obs->dirty = 1;
715         r->partiallydirty = 1;
716     debug("coap_check_notify: pdu init failed, resource stays partially dirty\n");
717     continue;
718       }
719
720       if (!coap_add_token(response, obs->token_length, obs->token)) {
721         obs->dirty = 1;
722         r->partiallydirty = 1;
723     debug("coap_check_notify: cannot add token, resource stays partially dirty\n");
724     coap_delete_pdu(response);
725     continue;
726       }
727
728       token.length = obs->token_length;
729       token.s = obs->token;
730
731       response->hdr->id = coap_new_message_id(context);
732       if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON) {
733     response->hdr->type = COAP_MESSAGE_NON;
734       } else {
735     response->hdr->type = COAP_MESSAGE_CON;
736       }
737       /* fill with observer-specific data */
738       h(context, r, &obs->subscriber, NULL, &token, response);
739
740       if (response->hdr->type == COAP_MESSAGE_CON) {
741     tid = coap_send(context, &obs->subscriber, response, SEND_NOW_CON);
742     obs->non_cnt = 0;
743       } else {
744     tid = coap_send(context, &obs->subscriber, response, SEND_NOW);
745     obs->non_cnt++;
746       }
747
748       if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON)
749     coap_delete_pdu(response);
750       if (COAP_INVALID_TID == tid)
751       {
752     debug("coap_check_notify: sending failed, resource stays partially dirty\n");
753         obs->dirty = 1;
754         r->partiallydirty = 1;
755       }
756
757     }
758
759     /* Increment value for next Observe use. */
760     context->observe++;
761   }
762   r->dirty = 0;
763 }
764
765 void
766 coap_check_notify(coap_context_t *context) {
767   coap_resource_t *r;
768 #ifndef WITH_CONTIKI
769
770 #ifdef COAP_RESOURCES_NOHASH
771   LL_FOREACH(context->resources, r) {
772 #else
773   coap_resource_t *tmp;
774   HASH_ITER(hh, context->resources, r, tmp) {
775 #endif
776     coap_notify_observers(context, r);
777   }
778 #else /* WITH_CONTIKI */
779   int i;
780
781   r = (coap_resource_t *)resource_storage.mem;
782   for (i = 0; i < resource_storage.num; ++i, ++r) {
783     if (resource_storage.count[i]) {
784       coap_notify_observers(context, r);
785     }
786   }
787 #endif /* WITH_CONTIKI */
788 }
789
790 /**
791  * Checks the failure counter for (peer, token) and removes peer from
792  * the list of observers for the given resource when COAP_OBS_MAX_FAIL
793  * is reached.
794  *
795  * @param context  The CoAP context to use
796  * @param resource The resource to check for (peer, token)
797  * @param peer     The observer's address
798  * @param token    The token that has been used for subscription.
799  */
800 static void
801 coap_remove_failed_observers(coap_context_t *context,
802                  coap_resource_t *resource,
803                  const coap_address_t *peer,
804                  const str *token) {
805   coap_subscription_t *obs;
806
807   for (obs = (coap_subscription_t*)list_head(resource->subscribers); obs;
808        obs = (coap_subscription_t*)list_item_next(obs)) {
809     if (coap_address_equals(peer, &obs->subscriber) &&
810     token->length == obs->token_length &&
811     memcmp(token->s, obs->token, token->length) == 0) {
812
813       /* count failed notifies and remove when
814        * COAP_MAX_FAILED_NOTIFY is reached */
815       if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
816     obs->fail_cnt++;
817       else {
818     list_remove(resource->subscribers, obs);
819     obs->fail_cnt = 0;
820
821 #ifndef NDEBUG
822     if (LOG_DEBUG <= coap_get_log_level()) {
823 #ifndef INET6_ADDRSTRLEN
824 #define INET6_ADDRSTRLEN 40
825 #endif
826       unsigned char addr[INET6_ADDRSTRLEN+8];
827
828       if (coap_print_addr(&obs->subscriber, addr, INET6_ADDRSTRLEN+8))
829         debug("** removed observer %s\n", addr);
830     }
831 #endif
832     coap_cancel_all_messages(context, &obs->subscriber,
833                  obs->token, obs->token_length);
834
835     COAP_FREE_TYPE(subscription, obs);
836       }
837     }
838     break;            /* break loop if observer was found */
839   }
840 }
841
842 void
843 coap_handle_failed_notify(coap_context_t *context,
844               const coap_address_t *peer,
845               const str *token) {
846   coap_resource_t *r;
847
848 #ifndef WITH_CONTIKI
849
850 #ifdef COAP_RESOURCES_NOHASH
851   LL_FOREACH(context->resources, r) {
852 #else
853   coap_resource_t *tmp;
854   HASH_ITER(hh, context->resources, r, tmp) {
855 #endif
856     coap_remove_failed_observers(context, r, peer, token);
857   }
858 #else /* WITH_CONTIKI */
859   int i;
860
861   r = (coap_resource_t *)resource_storage.mem;
862   for (i = 0; i < resource_storage.num; ++i, ++r) {
863     if (resource_storage.count[i]) {
864       coap_remove_failed_observers(context, r, peer, token);
865     }
866   }
867 #endif /* WITH_CONTIKI */
868 }
869 #endif /* WITHOUT_NOTIFY */