replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / 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 "include/coap/config.h"
10 #include "include/coap/net.h"
11 #include "include/coap/debug.h"
12 #include "include/coap/resource.h"
13 #include "include/coap/subscribe.h"
14
15 #ifdef WITH_LWIP
16 #include "include/coap/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 "include/coap/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) || defined(_WIN32)
31 #include "include/coap/utlist.h"
32 #include "include/coap/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 {
49     memb_init(&resource_storage);
50     memb_init(&attribute_storage);
51     memb_init(&subscription_storage);
52 }
53
54 static inline coap_subscription_t *
55 coap_malloc_subscription()
56 {
57     return memb_alloc(&subscription_storage);
58 }
59
60 static inline void
61 coap_free_subscription(coap_subscription_t *subscription)
62 {
63     memb_free(&subscription_storage, subscription);
64 }
65 #endif /* WITH_CONTIKI */
66
67 #ifndef min
68 #define min(a,b) ((a) < (b) ? (a) : (b))
69 #endif
70
71 /* Helper functions for conditional output of character sequences into
72  * a given buffer. The first Offset characters are skipped.
73  */
74
75 /**
76  * Adds Char to Buf if Offset is zero. Otherwise, Char is not written
77  * and Offset is decremented.
78  */
79 #define PRINT_WITH_OFFSET(Buf,Offset,Char)      \
80   if ((Offset) == 0) {                  \
81     (*(Buf)++) = (Char);                \
82   } else {                      \
83     (Offset)--;                     \
84   }                         \
85
86 /**
87  * Adds Char to Buf if Offset is zero and Buf is less than Bufend.
88  */
89 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) {     \
90     if ((Buf) < (Bufend)) {                     \
91       PRINT_WITH_OFFSET(Buf,Offset,Char);               \
92     }                                   \
93     (Result)++;                             \
94   }
95
96 /**
97  * Copies at most Length characters of Str to Buf. The first Offset
98  * characters are skipped. Output may be truncated to Bufend - Buf
99  * characters.
100  */
101 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) {    \
102     size_t i;                               \
103     for (i = 0; i < (Length); i++) {                    \
104       PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
105     }                                   \
106   }
107
108 static int match(const str *text, const str *pattern, int match_prefix, int match_substring)
109 {
110     assert(text);
111     assert(pattern);
112
113     if (text->length < pattern->length)
114         return 0;
115
116     if (match_substring)
117     {
118         unsigned char *next_token = text->s;
119         size_t remaining_length = text->length;
120         while (remaining_length)
121         {
122             size_t token_length;
123             unsigned char *token = next_token;
124             next_token = (unsigned char *) memchr(token, ' ', remaining_length);
125
126             if (next_token)
127             {
128                 token_length = next_token - token;
129                 remaining_length -= (token_length + 1);
130                 next_token++;
131             }
132             else
133             {
134                 token_length = remaining_length;
135                 remaining_length = 0;
136             }
137
138             if ((match_prefix || pattern->length == token_length)
139                     && memcmp(token, pattern->s, pattern->length) == 0)
140                 return 1;
141         }
142         return 0;
143     }
144
145     return (match_prefix || pattern->length == text->length)
146             && memcmp(text->s, pattern->s, pattern->length) == 0;
147 }
148
149 /**
150  * Prints the names of all known resources to @p buf. This function
151  * sets @p buflen to the number of bytes actually written and returns
152  * @c 1 on succes. On error, the value in @p buflen is undefined and
153  * the return value will be @c 0.
154  *
155  * @param context The context with the resource map.
156  * @param buf     The buffer to write the result.
157  * @param buflen  Must be initialized to the maximum length of @p buf and will be
158  *                set to the length of the well-known response on return.
159  * @param offset  The offset in bytes where the output shall start and is
160  *                shifted accordingly with the characters that have been
161  *                processed. This parameter is used to support the block
162  *                option.
163  * @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>
164  *
165  * @return COAP_PRINT_STATUS_ERROR on error. Otherwise, the lower 28 bits are
166  *         set to the number of bytes that have actually been written to
167  *         @p buf. COAP_PRINT_STATUS_TRUNC is set when the output has been
168  *         truncated.
169  */
170 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
171 coap_print_status_t
172 print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
173         size_t offset,
174         coap_opt_t *query_filter __attribute__ ((unused)))
175 {
176 #else /* not a GCC */
177 coap_print_status_t print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
178         size_t offset, coap_opt_t *query_filter)
179 {
180 #endif /* GCC */
181     coap_resource_t *r;
182     unsigned char *p = buf;
183     const unsigned char *bufend = buf + *buflen;
184     size_t left, written = 0;
185     coap_print_status_t result;
186     const size_t old_offset = offset;
187     int subsequent_resource = 0;
188 #ifndef COAP_RESOURCES_NOHASH
189     coap_resource_t *tmp;
190 #endif
191 #ifndef WITHOUT_QUERY_FILTER
192     str resource_param =
193     { 0, NULL }, query_pattern =
194     { 0, NULL };
195     int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
196 #define MATCH_URI       0x01
197 #define MATCH_PREFIX    0x02
198 #define MATCH_SUBSTRING 0x04
199     static const str _rt_attributes[] =
200     {
201     { 2, (unsigned char *) "rt" },
202     { 2, (unsigned char *) "if" },
203     { 3, (unsigned char *) "rel" },
204     { 0, NULL } };
205 #endif /* WITHOUT_QUERY_FILTER */
206
207 #ifdef WITH_CONTIKI
208     int i;
209 #endif /* WITH_CONTIKI */
210
211 #ifndef WITHOUT_QUERY_FILTER
212     /* split query filter, if any */
213     if (query_filter)
214     {
215         resource_param.s = COAP_OPT_VALUE(query_filter);
216         while (resource_param.length < COAP_OPT_LENGTH(query_filter)
217                 && resource_param.s[resource_param.length] != '=')
218             resource_param.length++;
219
220         if (resource_param.length < COAP_OPT_LENGTH(query_filter))
221         {
222             const str *rt_attributes;
223             if (resource_param.length == 4 && memcmp(resource_param.s, "href", 4) == 0)
224                 flags |= MATCH_URI;
225
226             for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++)
227             {
228                 if (resource_param.length == rt_attributes->length
229                         && memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0)
230                 {
231                     flags |= MATCH_SUBSTRING;
232                     break;
233                 }
234             }
235
236             /* rest is query-pattern */
237             query_pattern.s = COAP_OPT_VALUE(query_filter) + resource_param.length + 1;
238
239             assert((resource_param.length + 1) <= COAP_OPT_LENGTH(query_filter));
240             query_pattern.length = COAP_OPT_LENGTH(query_filter) - (resource_param.length + 1);
241
242             if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI))
243             {
244                 query_pattern.s++;
245                 query_pattern.length--;
246             }
247
248             if (query_pattern.length && query_pattern.s[query_pattern.length - 1] == '*')
249             {
250                 query_pattern.length--;
251                 flags |= MATCH_PREFIX;
252             }
253         }
254     }
255 #endif /* WITHOUT_QUERY_FILTER */
256
257 #ifndef WITH_CONTIKI
258
259 #ifdef COAP_RESOURCES_NOHASH
260     LL_FOREACH(context->resources, r)
261     {
262 #else
263     HASH_ITER(hh, context->resources, r, tmp)
264     {
265 #endif
266 #else /* WITH_CONTIKI */
267         r = (coap_resource_t *)resource_storage.mem;
268         for (i = 0; i < resource_storage.num; ++i, ++r)
269         {
270             if (!resource_storage.count[i])
271             continue;
272 #endif /* WITH_CONTIKI */
273
274 #ifndef WITHOUT_QUERY_FILTER
275         if (resource_param.length)
276         { /* there is a query filter */
277
278             if (flags & MATCH_URI)
279             { /* match resource URI */
280                 if (!match(&r->uri, &query_pattern, (flags & MATCH_PREFIX) != 0,
281                         (flags & MATCH_SUBSTRING) != 0))
282                     continue;
283             }
284             else
285             { /* match attribute */
286                 coap_attr_t *attr;
287                 str unquoted_val;
288                 attr = coap_find_attr(r, resource_param.s, resource_param.length);
289                 if (!attr)
290                     continue;
291                 if (attr->value.s[0] == '"')
292                 { /* if attribute has a quoted value, remove double quotes */
293                     unquoted_val.length = attr->value.length - 2;
294                     unquoted_val.s = attr->value.s + 1;
295                 }
296                 else
297                 {
298                     unquoted_val = attr->value;
299                 }
300                 if (!(match(&unquoted_val, &query_pattern, (flags & MATCH_PREFIX) != 0,
301                         (flags & MATCH_SUBSTRING) != 0)))
302                     continue;
303             }
304         }
305 #endif /* WITHOUT_QUERY_FILTER */
306
307         if (!subsequent_resource)
308         { /* this is the first resource  */
309             subsequent_resource = 1;
310         }
311         else
312         {
313             PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
314         }
315
316         left = bufend - p; /* calculate available space */
317         result = coap_print_link(r, p, &left, &offset);
318
319         if (result & COAP_PRINT_STATUS_ERROR)
320         {
321             break;
322         }
323
324         /* coap_print_link() returns the number of characters that
325          * where actually written to p. Now advance to its end. */
326         p += COAP_PRINT_OUTPUT_LENGTH(result);
327         written += left;
328     }
329
330     *buflen = written;
331     result = p - buf;
332     if (result + old_offset - offset < *buflen)
333     {
334         result |= COAP_PRINT_STATUS_TRUNC;
335     }
336     return result;
337 }
338
339 coap_resource_t *
340 coap_resource_init(const unsigned char *uri, size_t len, int flags)
341 {
342     coap_resource_t *r;
343
344 #if defined(WITH_POSIX) || defined(WITH_ARDUINO) || defined(_WIN32)
345     r = (coap_resource_t *)coap_malloc(sizeof(coap_resource_t));
346 #endif
347 #ifdef WITH_LWIP
348     r = (coap_resource_t *)memp_malloc(MEMP_COAP_RESOURCE);
349 #endif
350 #ifdef WITH_CONTIKI
351     r = (coap_resource_t *)memb_alloc(&resource_storage);
352 #endif
353     if (r)
354     {
355         memset(r, 0, sizeof(coap_resource_t));
356
357 #ifdef WITH_CONTIKI
358         LIST_STRUCT_INIT(r, link_attr);
359 #endif /* WITH_CONTIKI */
360         LIST_STRUCT_INIT(r, subscribers);
361
362         r->uri.s = (unsigned char *) uri;
363         r->uri.length = len;
364
365         coap_hash_path(r->uri.s, r->uri.length, r->key);
366
367         r->flags = flags;
368     }
369     else
370     {
371         debug("coap_resource_init: no memory left\n");
372     }
373
374     return r;
375 }
376
377 coap_attr_t *
378 coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen,
379         const unsigned char *val, size_t vlen, int flags)
380 {
381     coap_attr_t *attr = NULL;
382
383     if (!resource || !name)
384         return NULL;
385
386 #if defined(WITH_POSIX) || defined(WITH_ARDUINO) || defined(_WIN32)
387     attr = (coap_attr_t *)coap_malloc(sizeof(coap_attr_t));
388 #endif
389 #ifdef WITH_LWIP
390     attr = (coap_attr_t *)memp_malloc(MEMP_COAP_RESOURCEATTR);
391 #endif
392 #ifdef WITH_CONTIKI
393     attr = (coap_attr_t *)memb_alloc(&attribute_storage);
394 #endif
395
396     if (attr)
397     {
398         attr->name.length = nlen;
399         attr->value.length = val ? vlen : 0;
400
401         attr->name.s = (unsigned char *) name;
402         attr->value.s = (unsigned char *) val;
403
404         attr->flags = flags;
405
406         /* add attribute to resource list */
407 #ifndef WITH_CONTIKI
408         LL_PREPEND(resource->link_attr, attr);
409 #else /* WITH_CONTIKI */
410         list_add(resource->link_attr, attr);
411 #endif /* WITH_CONTIKI */
412     }
413     else
414     {
415         debug("coap_add_attr: no memory left\n");
416     }
417
418     return attr;
419 }
420
421 coap_attr_t *
422 coap_find_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen)
423 {
424     coap_attr_t *attr;
425
426     if (!resource || !name)
427         return NULL;
428
429 #ifndef WITH_CONTIKI
430     LL_FOREACH(resource->link_attr, attr)
431     {
432 #else /* WITH_CONTIKI */
433         for (attr = list_head(resource->link_attr); attr;
434                 attr = list_item_next(attr))
435         {
436 #endif /* WITH_CONTIKI */
437         if (attr->name.length == nlen && memcmp(attr->name.s, name, nlen) == 0)
438             return attr;
439     }
440
441     return NULL;
442 }
443
444 void coap_delete_attr(coap_attr_t *attr)
445 {
446     if (!attr)
447         return;
448     if (attr->flags & COAP_ATTR_FLAGS_RELEASE_NAME)
449         coap_free(attr->name.s);
450     if (attr->flags & COAP_ATTR_FLAGS_RELEASE_VALUE)
451         coap_free(attr->value.s);
452 #ifdef POSIX
453     coap_free(attr);
454 #endif
455 #ifdef WITH_LWIP
456     memp_free(MEMP_COAP_RESOURCEATTR, attr);
457 #endif
458 #ifdef WITH_CONTIKI
459     /* FIXME it looks like this was never implemented */
460 #endif
461 }
462
463 void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key)
464 {
465     coap_opt_iterator_t opt_iter;
466     coap_opt_filter_t filter;
467     coap_opt_t *option;
468
469     memset(key, 0, sizeof(coap_key_t));
470
471     coap_option_filter_clear(filter);
472     coap_option_setb(filter, COAP_OPTION_URI_PATH);
473
474     coap_option_iterator_init((coap_pdu_t *) request, &opt_iter, filter);
475     while ((option = coap_option_next(&opt_iter)))
476         coap_hash(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option), key);
477 }
478
479 void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
480 {
481 #ifndef WITH_CONTIKI
482 #ifdef COAP_RESOURCES_NOHASH
483     LL_PREPEND(context->resources, resource);
484 #else
485     HASH_ADD(hh, context->resources, key, sizeof(coap_key_t), resource);
486 #endif
487 #endif /* WITH_CONTIKI */
488 }
489
490 int coap_delete_resource(coap_context_t *context, coap_key_t key)
491 {
492     coap_resource_t *resource = NULL;
493     coap_attr_t *attr = NULL, *tmp = NULL;
494 #ifdef WITH_CONTIKI
495     coap_subscription_t *obs;
496 #endif
497
498     if (!context)
499         return 0;
500
501     resource = coap_get_resource_from_key(context, key);
502
503     if (!resource)
504         return 0;
505
506 #if defined(WITH_POSIX) || defined(WITH_LWIP) || defined(WITH_ARDUINO) || defined(_WIN32)
507 #ifdef COAP_RESOURCES_NOHASH
508     LL_DELETE(context->resources, resource);
509 #else
510     HASH_DELETE(hh, context->resources, resource);
511 #endif
512
513     /* delete registered attributes */
514     LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
515
516     if (resource->flags & COAP_RESOURCE_FLAGS_RELEASE_URI)
517         coap_free(resource->uri.s);
518
519 #if defined(WITH_POSIX) || defined(WITH_ARDUINO) || defined(_WIN32)
520     coap_free(resource);
521 #endif
522 #ifdef WITH_LWIP
523     memp_free(MEMP_COAP_RESOURCE, resource);
524 #endif
525 #else /* not (WITH_POSIX || WITH_LWIP || WITH_ARDUINO) */
526     /* delete registered attributes */
527     while ((attr = list_pop(resource->link_attr)))
528         memb_free(&attribute_storage, attr);
529
530     /* delete subscribers */
531     while ((obs = list_pop(resource->subscribers)))
532     {
533         /* FIXME: notify observer that its subscription has been removed */
534         memb_free(&subscription_storage, obs);
535     }
536
537     memb_free(&resource_storage, resource);
538 #endif /* WITH_CONTIKI */
539
540     return 1;
541 }
542
543 coap_resource_t *
544 coap_get_resource_from_key(coap_context_t *context, coap_key_t key)
545 {
546 #ifndef WITH_CONTIKI
547     coap_resource_t *resource;
548 #ifdef COAP_RESOURCES_NOHASH
549     resource = NULL;
550     LL_FOREACH(context->resources, resource)
551     {
552         /* if you think you can outspart the compiler and speed things up by (eg by
553          * casting to uint32* and comparing alues), increment this counter: 1 */
554         if (memcmp(key, resource->key, sizeof(coap_key_t)) == 0)
555         return resource;
556     }
557     return NULL;
558 #else
559     HASH_FIND(hh, context->resources, key, sizeof(coap_key_t), resource);
560
561     return resource;
562 #endif
563 #else /* WITH_CONTIKI */
564     int i;
565     coap_resource_t *ptr2;
566
567     /* the search function is basically taken from memb.c */
568     ptr2 = (coap_resource_t *)resource_storage.mem;
569     for (i = 0; i < resource_storage.num; ++i)
570     {
571         if (resource_storage.count[i] &&
572                 (memcmp(ptr2->key, key, sizeof(coap_key_t)) == 0))
573         return (coap_resource_t *)ptr2;
574         ++ptr2;
575     }
576
577     return NULL;
578 #endif /* WITH_CONTIKI */
579 }
580
581 coap_print_status_t coap_print_link(const coap_resource_t *resource, unsigned char *buf,
582         size_t *len, size_t *offset)
583 {
584     unsigned char *p = buf;
585     const unsigned char *bufend = buf + *len;
586     coap_attr_t *attr;
587     coap_print_status_t result = 0;
588     const size_t old_offset = *offset;
589
590     *len = 0;
591     PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
592     PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
593
594     COPY_COND_WITH_OFFSET(p, bufend, *offset, resource->uri.s, resource->uri.length, *len);
595
596     PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
597
598 #ifndef WITH_CONTIKI
599     LL_FOREACH(resource->link_attr, attr)
600     {
601 #else /* WITH_CONTIKI */
602         for (attr = list_head(resource->link_attr); attr;
603                 attr = list_item_next(attr))
604         {
605 #endif /* WITH_CONTIKI */
606
607         PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
608
609         COPY_COND_WITH_OFFSET(p, bufend, *offset, attr->name.s, attr->name.length, *len);
610
611         if (attr->value.s)
612         {
613             PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
614
615             COPY_COND_WITH_OFFSET(p, bufend, *offset, attr->value.s, attr->value.length, *len);
616         }
617
618     }
619     if (resource->observable)
620     {
621         COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
622     }
623
624     result = p - buf;
625     if (result + old_offset - *offset < *len)
626     {
627         result |= COAP_PRINT_STATUS_TRUNC;
628     }
629
630     return result;
631 }
632
633 #ifndef WITHOUT_OBSERVE
634 coap_subscription_t *
635 coap_find_observer(coap_resource_t *resource, const coap_address_t *peer, const str *token)
636 {
637     coap_subscription_t *s;
638
639     assert(resource);
640     assert(peer);
641
642     for (s = (coap_subscription_t *) list_head(resource->subscribers);
643             s; s = (coap_subscription_t *) list_item_next((void *) s))
644     {
645         if (coap_address_equals(&s->subscriber, peer)
646                 && (!token
647                         || (token->length == s->token_length
648                                 && memcmp(token->s, s->token, token->length) == 0)))
649             return s;
650     }
651
652     return NULL;
653 }
654
655 coap_subscription_t *
656 coap_add_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
657 {
658     coap_subscription_t *s;
659
660     assert(observer);
661
662     /* Check if there is already a subscription for this peer. */
663     s = coap_find_observer(resource, observer, token);
664
665     /* We are done if subscription was found. */
666     if (s)
667         return s;
668
669     /* s points to a different subscription, so we have to create
670      * another one. */
671     s = COAP_MALLOC_TYPE(subscription);
672
673     if (!s)
674         return NULL;
675
676     coap_subscription_init(s);
677     memcpy(&s->subscriber, observer, sizeof(coap_address_t));
678
679     if (token && token->length)
680     {
681         s->token_length = token->length;
682         memcpy(s->token, token->s, min(s->token_length, 8));
683     }
684
685     /* add subscriber to resource */
686     list_add(resource->subscribers, s);
687
688     return s;
689 }
690
691 void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token)
692 {
693     coap_resource_t *r;
694     coap_subscription_t *s;
695
696 #ifndef WITH_CONTIKI
697 #ifdef COAP_RESOURCES_NOHASH
698     LL_FOREACH(context->resources, r)
699     {
700 #else
701     coap_resource_t *tmp;
702     HASH_ITER(hh, context->resources, r, tmp)
703     {
704 #endif
705         s = coap_find_observer(r, observer, token);
706         if (s)
707         {
708             s->fail_cnt = 0;
709         }
710     }
711 #else /* WITH_CONTIKI */
712     r = (coap_resource_t *)resource_storage.mem;
713     for (i = 0; i < resource_storage.num; ++i, ++r)
714     {
715         if (resource_storage.count[i])
716         {
717             s = coap_find_observer(r, observer, token);
718             if (s)
719             {
720                 s->fail_cnt = 0;
721             }
722         }
723     }
724 #endif /* WITH_CONTIKI */
725 }
726
727 void coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer,
728         const str *token)
729 {
730     coap_subscription_t *s;
731
732     s = coap_find_observer(resource, observer, token);
733
734     if (s)
735     {
736         list_remove(resource->subscribers, s);
737
738         COAP_FREE_TYPE(subscription, s);
739     }
740 }
741
742 static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
743 {
744     coap_method_handler_t h;
745     coap_subscription_t *obs;
746     str token;
747     coap_pdu_t *response;
748
749     if (r->observable && (r->dirty || r->partiallydirty))
750     {
751         r->partiallydirty = 0;
752
753         /* retrieve GET handler, prepare response */
754         h = r->handler[COAP_REQUEST_GET - 1];
755         assert(h); /* we do not allow subscriptions if no
756          * GET handler is defined */
757
758         for (obs = (coap_subscription_t *) list_head(r->subscribers);
759                 obs; obs = (coap_subscription_t *) list_item_next((void *) obs))
760         {
761             if (r->dirty == 0 && obs->dirty == 0)
762                 /* running this resource due to partiallydirty,
763                  * but this observation's notification was already enqueued */
764                 continue;
765
766             coap_tid_t tid = COAP_INVALID_TID;
767             obs->dirty = 0;
768             /* initialize response */
769             response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE);
770             if (!response)
771             {
772                 obs->dirty = 1;
773                 r->partiallydirty = 1;
774                 debug("coap_check_notify: pdu init failed, resource stays partially dirty\n");
775                 continue;
776             }
777
778             if (!coap_add_token(response, obs->token_length, obs->token))
779             {
780                 obs->dirty = 1;
781                 r->partiallydirty = 1;
782                 debug("coap_check_notify: cannot add token, resource stays partially dirty\n");
783                 coap_delete_pdu(response);
784                 continue;
785             }
786
787             token.length = obs->token_length;
788             token.s = obs->token;
789
790             response->transport_hdr->udp.id = coap_new_message_id(context);
791             if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON)
792             {
793                 response->transport_hdr->udp.type = COAP_MESSAGE_NON;
794             }
795             else
796             {
797                 response->transport_hdr->udp.type = COAP_MESSAGE_CON;
798             }
799             /* fill with observer-specific data */
800             h(context, r, &obs->subscriber, NULL, &token, response);
801
802             if (response->transport_hdr->udp.type == COAP_MESSAGE_CON)
803             {
804                 tid = coap_send_confirmed(context, &obs->subscriber, response);
805                 obs->non_cnt = 0;
806             }
807             else
808             {
809                 tid = coap_send(context, &obs->subscriber, response);
810                 obs->non_cnt++;
811             }
812
813             if (COAP_INVALID_TID == tid || response->transport_hdr->udp.type != COAP_MESSAGE_CON)
814                 coap_delete_pdu(response);
815             if (COAP_INVALID_TID == tid)
816             {
817                 debug("coap_check_notify: sending failed, resource stays partially dirty\n");
818                 obs->dirty = 1;
819                 r->partiallydirty = 1;
820             }
821
822         }
823
824         /* Increment value for next Observe use. */
825         context->observe++;
826     }
827     r->dirty = 0;
828 }
829
830 void coap_check_notify(coap_context_t *context)
831 {
832     coap_resource_t *r;
833 #ifndef WITH_CONTIKI
834
835 #ifdef COAP_RESOURCES_NOHASH
836     LL_FOREACH(context->resources, r)
837     {
838 #else
839     coap_resource_t *tmp;
840     HASH_ITER(hh, context->resources, r, tmp)
841     {
842 #endif
843         coap_notify_observers(context, r);
844     }
845 #else /* WITH_CONTIKI */
846     int i;
847
848     r = (coap_resource_t *)resource_storage.mem;
849     for (i = 0; i < resource_storage.num; ++i, ++r)
850     {
851         if (resource_storage.count[i])
852         {
853             coap_notify_observers(context, r);
854         }
855     }
856 #endif /* WITH_CONTIKI */
857 }
858
859 /**
860  * Checks the failure counter for (peer, token) and removes peer from
861  * the list of observers for the given resource when COAP_OBS_MAX_FAIL
862  * is reached.
863  *
864  * @param context  The CoAP context to use
865  * @param resource The resource to check for (peer, token)
866  * @param peer     The observer's address
867  * @param token    The token that has been used for subscription.
868  */
869 static void coap_remove_failed_observers(coap_context_t *context, coap_resource_t *resource,
870         const coap_address_t *peer, const str *token)
871 {
872     coap_subscription_t *obs;
873
874     for (obs = (coap_subscription_t *) list_head(resource->subscribers);
875             obs; obs = (coap_subscription_t *) list_item_next((void *) obs))
876     {
877         if (coap_address_equals(peer, &obs->subscriber) && token->length == obs->token_length
878                 && memcmp(token->s, obs->token, token->length) == 0)
879         {
880
881             /* count failed notifies and remove when
882              * COAP_MAX_FAILED_NOTIFY is reached */
883             if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
884                 obs->fail_cnt++;
885             else
886             {
887                 list_remove(resource->subscribers, obs);
888                 obs->fail_cnt = 0;
889
890 #ifndef NDEBUG
891                 if (LOG_DEBUG <= coap_get_log_level())
892                 {
893 #ifndef INET6_ADDRSTRLEN
894 #define INET6_ADDRSTRLEN 40
895 #endif
896                     unsigned char addr[INET6_ADDRSTRLEN + 8];
897
898                     if (coap_print_addr(&obs->subscriber, addr, INET6_ADDRSTRLEN + 8))
899                         debug("** removed observer %s\n", addr);
900                 }
901 #endif
902                 coap_cancel_all_messages(context, &obs->subscriber, obs->token, obs->token_length);
903
904                 COAP_FREE_TYPE(subscription, obs);
905             }
906         }
907         break; /* break loop if observer was found */
908     }
909 }
910
911 void coap_handle_failed_notify(coap_context_t *context, const coap_address_t *peer,
912         const str *token)
913 {
914     coap_resource_t *r;
915
916 #ifndef WITH_CONTIKI
917
918 #ifdef COAP_RESOURCES_NOHASH
919     LL_FOREACH(context->resources, r)
920     {
921 #else
922     coap_resource_t *tmp;
923     HASH_ITER(hh, context->resources, r, tmp)
924     {
925 #endif
926         coap_remove_failed_observers(context, r, peer, token);
927     }
928 #else /* WITH_CONTIKI */
929     int i;
930
931     r = (coap_resource_t *)resource_storage.mem;
932     for (i = 0; i < resource_storage.num; ++i, ++r)
933     {
934         if (resource_storage.count[i])
935         {
936             coap_remove_failed_observers(context, r, peer, token);
937         }
938     }
939 #endif /* WITH_CONTIKI */
940 }
941 #endif /* WITHOUT_NOTIFY */