libcoap  4.2.1
resource.c
Go to the documentation of this file.
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
35 coap_resources_init() {
36  memb_init(&subscription_storage);
37 }
38 
40 coap_malloc_subscription() {
41  return memb_alloc(&subscription_storage);
42 }
43 
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 
69 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \
70  if ((Offset) == 0) { \
71  (*(Buf)++) = (Char); \
72  } else { \
73  (Offset)--; \
74  } \
75 
76 
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 
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
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 
156 #if defined(__GNUC__) && defined(WITHOUT_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 */
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) {
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*)""};
293 
295 coap_resource_init(coap_str_const_t *uri_path, int flags) {
296  coap_resource_t *r;
297 
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 
326  "- Unknown -";
327 
330  coap_resource_t *r;
331 
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 *
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 
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 *
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
402  if (!attr)
403  return;
405  if (attr->value) {
407  }
408 
409 #ifdef WITH_LWIP
410  memp_free(MEMP_COAP_RESOURCEATTR, attr);
411 #endif
412 #ifndef WITH_LWIP
414 #endif
415 }
416 
417 static void
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 ) {
433  if (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
448  if (resource->is_unknown) {
449  if (context->unknown_resource)
451  context->unknown_resource = resource;
452  }
453  else {
455  resource->uri_path);
456 
457  if (r) {
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
470  if (!context || !resource)
471  return 0;
472 
473  if (resource->is_unknown && (context->unknown_resource == 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
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) {
505  context->unknown_resource = NULL;
506  }
507 }
508 
511  coap_resource_t *result;
512 
513  RESOURCES_FIND(context->resources, uri_path, result);
514 
515  return result;
516 }
517 
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) {
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
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
583  const coap_binary_t *token) {
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 *
601  const coap_string_t *query) {
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 
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) {
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)
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 
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
686  const coap_binary_t *token) {
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
699  const coap_binary_t *token) {
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)
717  COAP_FREE_TYPE(subscription,s);
718  }
719 
720  return s != NULL;
721 }
722 
723 void
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)
733  COAP_FREE_TYPE(subscription, s);
734  }
735  }
736  }
737 }
738 
739 static void
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 &&
763  (obs->non_cnt >= COAP_OBS_MAX_NON)))
764  continue;
765 
767  obs->dirty = 0;
768  /* initialize response */
770  if (!response) {
771  obs->dirty = 1;
772  r->partiallydirty = 1;
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;
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) {
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
828  return coap_resource_notify_observers(r, query);
829 }
830 
831 int
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
865 
866  RESOURCES_ITER(context->resources, r) {
867  coap_notify_observers(context, r);
868  }
869 }
870 
881 static void
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 
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);
914  if (obs->query)
916  COAP_FREE_TYPE(subscription, obs);
917  }
918  break; /* break loop if observer was found */
919  }
920  }
921 }
922 
923 void
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 */
uint8_t type
message type
Definition: pdu.h:288
#define LL_FOREACH(head, el)
Definition: utlist.h:413
coap_address_t remote
remote address and port
Definition: coap_io.h:50
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:25
uint8_t con_active
Active CON request sent.
Definition: coap_session.h:75
#define RESOURCES_DELETE(r, obj)
Definition: resource.h:451
static coap_subscription_t * coap_find_observer_query(coap_resource_t *resource, coap_session_t *session, const coap_string_t *query)
Definition: resource.c:600
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:864
coap_attr_t * coap_find_attr(coap_resource_t *resource, coap_str_const_t *name)
Returns resource&#39;s coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:384
int flags
Definition: resource.h:50
coap_resource_t * coap_resource_unknown_init(coap_method_handler_t put_handler)
Creates a new resource object for the unknown resource handler with support for PUT.
Definition: resource.c:329
coap_attr_t * coap_add_attr(coap_resource_t *resource, coap_str_const_t *name, coap_str_const_t *val, int flags)
Registers a new attribute with the given resource.
Definition: resource.c:347
#define INET6_ADDRSTRLEN
struct coap_session_t * session
subscriber session
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:313
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:238
#define LL_PREPEND(head, add)
Definition: utlist.h:314
#define COAP_REQUEST_GET
Definition: pdu.h:79
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:1031
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Marks an observer as alive.
Definition: resource.c:685
#define COAP_MESSAGE_NON
Definition: pdu.h:73
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition: coap_debug.c:161
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:966
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:87
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
Definition: resource.c:724
int coap_resource_notify_observers(coap_resource_t *r, const coap_string_t *query)
Initiate the sending of an Observe packet for all observers of resource, optionally matching query if...
Definition: resource.c:832
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition: resource.h:43
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:70
int coap_delete_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Removes any subscription for observer from resource and releases the allocated storage.
Definition: resource.c:698
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:187
unsigned int is_unknown
resource created for unknown handler
Definition: resource.h:74
Debug.
Definition: coap_debug.h:55
#define MATCH_URI
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
size_t length
length of string
Definition: str.h:34
uint8_t * s
string data
Definition: str.h:27
COAP_STATIC_INLINE uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
Definition: net.h:390
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition: pdu.c:191
coap_subscription_t * coap_find_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Returns a subscription object for given peer.
Definition: resource.c:582
void coap_subscription_init(coap_subscription_t *s)
Definition: subscribe.c:13
#define MATCH_PREFIX
#define MATCH_SUBSTRING
Coap string data definition.
Definition: str.h:25
int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Definition: resource.c:827
Coap string data definition with const data.
Definition: str.h:33
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: str.h:109
static void coap_remove_failed_observers(coap_context_t *context, coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Checks the failure counter for (peer, token) and removes peer from the list of observers for the give...
Definition: resource.c:882
coap_str_const_t * name
Definition: resource.h:48
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:368
#define COAP_INVALID_TID
Indicates an invalid transaction id.
Definition: pdu.h:241
size_t token_length
actual length of token
#define RESOURCES_ITER(r, tmp)
Definition: resource.h:454
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
uint16_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:238
struct coap_string_t * query
query string used for subscription, if any
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:69
#define COAP_MALLOC_TYPE(Type)
Definition: resource.c:50
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:44
unsigned int observable
can be observed
Definition: resource.h:72
coap_addr_tuple_t addr_info
key: remote/local address info
Definition: coap_session.h:68
Warning.
Definition: coap_debug.h:52
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:447
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
#define COAP_PRINT_STATUS_MAX
Definition: resource.c:55
#define COAP_MESSAGE_CON
Definition: pdu.h:72
#define RESOURCES_ADD(r, obj)
Definition: resource.h:448
Coap binary data definition.
Definition: str.h:43
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:27
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server...
Definition: coap_session.h:395
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:40
uint16_t tid
transaction id, if any, in regular host byte order
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
Definition: resource.c:740
coap_block_t block2
GET request Block2 definition.
coap_str_const_t * value
Definition: resource.h:49
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:88
static coap_str_const_t * null_path
Definition: resource.c:292
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, const uint8_t *token, size_t token_length)
Cancels all outstanding messages for session session that have the specified token.
Definition: net.c:1672
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
size_t length
length of string
Definition: str.h:26
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:66
Subscriber information.
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:127
Structure of Block options.
Definition: block.h:36
const uint8_t * s
string data
Definition: str.h:35
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource&#39;s partially dirty f...
#define RESOURCES_FIND(r, k, res)
Definition: resource.h:458
unsigned int has_block2
GET request had Block2 definition.
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
Definition: net.h:151
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:275
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:419
#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:79
coap_resource_t * coap_get_resource_from_uri_path(coap_context_t *context, coap_str_const_t *uri_path)
Returns the resource identified by the unique string uri_path.
Definition: resource.c:510
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:489
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token, coap_string_t *query, int has_block2, coap_block_t block2)
Adds the specified peer as observer for resource.
Definition: resource.c:618
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:519
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
#define LL_DELETE(head, del)
Definition: utlist.h:385
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:314
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:61
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:310
static int match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, int match_substring)
Definition: resource.c:99
void coap_register_handler(coap_resource_t *resource, unsigned char method, coap_method_handler_t handler)
Registers the specified handler as message handler for the request type method.
Definition: resource.c:572
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
Definition: resource.c:91
unsigned int fail_cnt
up to 3 confirmable notifies can fail
#define COAP_FREE_TYPE(Type, Object)
Definition: resource.c:52
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:129
#define min(a, b)
Definition: resource.c:58
#define COAP_REQUEST_PUT
Definition: pdu.h:81
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)
Prints the names of all known resources to buf.
Definition: resource.c:163
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition: str.c:31
static coap_str_const_t null_path_value
Definition: resource.c:291
coap_pdu_t * coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size...
Definition: pdu.c:79
coap_resource_t * coap_resource_init(coap_str_const_t *uri_path, int flags)
Creates a new resource object and initializes the link field to the string uri_path.
Definition: resource.c:295
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Definition: resource.c:924
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
unsigned char token[8]
token used for subscription
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:401
coap_str_const_t * uri_path
Request URI Path for this resource.
Definition: resource.h:95
size_t length
length of binary data
Definition: str.h:44
unsigned char uint8_t
Definition: uthash.h:79
#define COAP_PRINT_STATUS_TRUNC
Definition: resource.h:315
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_session_t *, coap_pdu_t *, coap_binary_t *, coap_string_t *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:35
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free&#39;d by coap_delete_resource().
Definition: resource.h:54
unsigned int observe
The next value for the Observe option.
Definition: resource.h:102
uint8_t * s
binary data
Definition: str.h:45
static const uint8_t coap_unknown_resource_uri[]
Definition: resource.c:325
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH...
Definition: resource.h:83
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
Definition: resource.c:469
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:147
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:293
static void coap_free_resource(coap_resource_t *resource)
Definition: resource.c:418
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:149
Pulls together all the internal only header files.