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