libcoap  4.3.0rc2
resource.c
Go to the documentation of this file.
1 /* resource.c -- generic resource handling
2  *
3  * Copyright (C) 2010--2021 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 "coap3/coap_internal.h"
10 
11 #include <stdio.h>
12 #include <errno.h>
13 
14 #ifdef COAP_EPOLL_SUPPORT
15 #include <sys/epoll.h>
16 #include <sys/timerfd.h>
17 #endif /* COAP_EPOLL_SUPPORT */
18 
19 #if defined(WITH_LWIP)
20 /* mem.h is only needed for the string free calls for
21  * COAP_ATTR_FLAGS_RELEASE_NAME / COAP_ATTR_FLAGS_RELEASE_VALUE /
22  * COAP_RESOURCE_FLAGS_RELEASE_URI. not sure what those lines should actually
23  * do on lwip. */
24 
25 #include <lwip/memp.h>
26 
27 #define COAP_MALLOC_TYPE(Type) \
28  ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
29 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
30 
31 #elif defined(WITH_CONTIKI)
32 #include "memb.h"
33 
34 #define COAP_MALLOC_TYPE(Type) \
35  ((coap_##Type##_t *)memb_alloc(&(Type##_storage)))
36 #define COAP_FREE_TYPE(Type, Object) memb_free(&(Type##_storage), (Object))
37 
38 MEMB(subscription_storage, coap_subscription_t, COAP_MAX_SUBSCRIBERS);
39 
40 void
41 coap_resources_init() {
42  memb_init(&subscription_storage);
43 }
44 
46 coap_malloc_subscription() {
47  return memb_alloc(&subscription_storage);
48 }
49 
51 coap_free_subscription(coap_subscription_t *subscription) {
52  memb_free(&subscription_storage, subscription);
53 }
54 
55 #else
56 #define COAP_MALLOC_TYPE(Type) \
57  ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
58 #define COAP_FREE_TYPE(Type, Object) coap_free(Object)
59 #endif
60 
61 #define COAP_PRINT_STATUS_MAX (~COAP_PRINT_STATUS_MASK)
62 
63 #ifndef min
64 #define min(a,b) ((a) < (b) ? (a) : (b))
65 #endif
66 
67 /* Helper functions for conditional output of character sequences into
68  * a given buffer. The first Offset characters are skipped.
69  */
70 
75 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \
76  if ((Offset) == 0) { \
77  (*(Buf)++) = (Char); \
78  } else { \
79  (Offset)--; \
80  } \
81 
85 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
86  if ((Buf) < (Bufend)) { \
87  PRINT_WITH_OFFSET(Buf,Offset,Char); \
88  } \
89  (Result)++; \
90  }
91 
97 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
98  size_t i; \
99  for (i = 0; i < (Length); i++) { \
100  PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
101  } \
102  }
103 
104 static int
105 match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix,
106  int match_substring
107 ) {
108  assert(text); assert(pattern);
109 
110  if (text->length < pattern->length)
111  return 0;
112 
113  if (match_substring) {
114  const uint8_t *next_token = text->s;
115  size_t remaining_length = text->length;
116  while (remaining_length) {
117  size_t token_length;
118  const uint8_t *token = next_token;
119  next_token = (unsigned char *)memchr(token, ' ', remaining_length);
120 
121  if (next_token) {
122  token_length = next_token - token;
123  remaining_length -= (token_length + 1);
124  next_token++;
125  } else {
126  token_length = remaining_length;
127  remaining_length = 0;
128  }
129 
130  if ((match_prefix || pattern->length == token_length) &&
131  memcmp(token, pattern->s, pattern->length) == 0)
132  return 1;
133  }
134  return 0;
135  }
136 
137  return (match_prefix || pattern->length == text->length) &&
138  memcmp(text->s, pattern->s, pattern->length) == 0;
139 }
140 
162 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
164 coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
165  size_t offset,
166  coap_opt_t *query_filter COAP_UNUSED) {
167 #else /* not a GCC */
169 coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
170  size_t offset, coap_opt_t *query_filter) {
171 #endif /* GCC */
172  size_t output_length = 0;
173  unsigned char *p = buf;
174  const uint8_t *bufend = buf + *buflen;
175  size_t left, written = 0;
176  coap_print_status_t result;
177  const size_t old_offset = offset;
178  int subsequent_resource = 0;
179 #ifndef WITHOUT_QUERY_FILTER
180  coap_str_const_t resource_param = { 0, NULL }, query_pattern = { 0, NULL };
181  int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
182 #define MATCH_URI 0x01
183 #define MATCH_PREFIX 0x02
184 #define MATCH_SUBSTRING 0x04
185  static const coap_str_const_t _rt_attributes[] = {
186  {2, (const uint8_t *)"rt"},
187  {2, (const uint8_t *)"if"},
188  {3, (const uint8_t *)"rel"},
189  {0, NULL}};
190 #endif /* WITHOUT_QUERY_FILTER */
191 
192 #ifndef WITHOUT_QUERY_FILTER
193  /* split query filter, if any */
194  if (query_filter) {
195  resource_param.s = coap_opt_value(query_filter);
196  while (resource_param.length < coap_opt_length(query_filter)
197  && resource_param.s[resource_param.length] != '=')
198  resource_param.length++;
199 
200  if (resource_param.length < coap_opt_length(query_filter)) {
201  const coap_str_const_t *rt_attributes;
202  if (resource_param.length == 4 &&
203  memcmp(resource_param.s, "href", 4) == 0)
204  flags |= MATCH_URI;
205 
206  for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
207  if (resource_param.length == rt_attributes->length &&
208  memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
209  flags |= MATCH_SUBSTRING;
210  break;
211  }
212  }
213 
214  /* rest is query-pattern */
215  query_pattern.s =
216  coap_opt_value(query_filter) + resource_param.length + 1;
217 
218  assert((resource_param.length + 1) <= coap_opt_length(query_filter));
219  query_pattern.length =
220  coap_opt_length(query_filter) - (resource_param.length + 1);
221 
222  if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
223  query_pattern.s++;
224  query_pattern.length--;
225  }
226 
227  if (query_pattern.length &&
228  query_pattern.s[query_pattern.length-1] == '*') {
229  query_pattern.length--;
230  flags |= MATCH_PREFIX;
231  }
232  }
233  }
234 #endif /* WITHOUT_QUERY_FILTER */
235 
236  RESOURCES_ITER(context->resources, r) {
237 
238 #ifndef WITHOUT_QUERY_FILTER
239  if (resource_param.length) { /* there is a query filter */
240 
241  if (flags & MATCH_URI) { /* match resource URI */
242  if (!match(r->uri_path, &query_pattern, (flags & MATCH_PREFIX) != 0,
243  (flags & MATCH_SUBSTRING) != 0))
244  continue;
245  } else { /* match attribute */
246  coap_attr_t *attr;
247  coap_str_const_t unquoted_val;
248  attr = coap_find_attr(r, &resource_param);
249  if (!attr || !attr->value) continue;
250  unquoted_val = *attr->value;
251  if (attr->value->s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
252  unquoted_val.length -= 2;
253  unquoted_val.s += 1;
254  }
255  if (!(match(&unquoted_val, &query_pattern,
256  (flags & MATCH_PREFIX) != 0,
257  (flags & MATCH_SUBSTRING) != 0)))
258  continue;
259  }
260  }
261 #endif /* WITHOUT_QUERY_FILTER */
262 
263  if (!subsequent_resource) { /* this is the first resource */
264  subsequent_resource = 1;
265  } else {
266  PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
267  }
268 
269  left = bufend - p; /* calculate available space */
270  result = coap_print_link(r, p, &left, &offset);
271 
272  if (result & COAP_PRINT_STATUS_ERROR) {
273  break;
274  }
275 
276  /* coap_print_link() returns the number of characters that
277  * where actually written to p. Now advance to its end. */
278  p += COAP_PRINT_OUTPUT_LENGTH(result);
279  written += left;
280  }
281 
282  *buflen = written;
283  output_length = p - buf;
284 
285  if (output_length > COAP_PRINT_STATUS_MAX) {
287  }
288 
289  result = (coap_print_status_t)output_length;
290 
291  if (result + old_offset - offset < *buflen) {
292  result |= COAP_PRINT_STATUS_TRUNC;
293  }
294  return result;
295 }
296 
297 static coap_str_const_t null_path_value = {0, (const uint8_t*)""};
299 
301 coap_resource_init(coap_str_const_t *uri_path, int flags) {
302  coap_resource_t *r;
303 
305  if (r) {
306  memset(r, 0, sizeof(coap_resource_t));
307 
308  if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) {
309  /* Need to take a copy if caller is not providing a release request */
310  if (uri_path)
311  uri_path = coap_new_str_const(uri_path->s, uri_path->length);
312  else
314  }
315  else if (!uri_path) {
316  /* Do not expecte this, but ... */
318  }
319 
320  if (uri_path)
321  r->uri_path = uri_path;
322 
323  r->flags = flags;
324  } else {
325  coap_log(LOG_DEBUG, "coap_resource_init: no memory left\n");
326  }
327 
328  return r;
329 }
330 
331 static const uint8_t coap_unknown_resource_uri[] =
332  "- Unknown -";
333 
336  coap_resource_t *r;
337 
339  if (r) {
340  memset(r, 0, sizeof(coap_resource_t));
341  r->is_unknown = 1;
342  /* Something unlikely to be used, but it shows up in the logs */
344  coap_register_handler(r, COAP_REQUEST_PUT, put_handler);
345  } else {
346  coap_log(LOG_DEBUG, "coap_resource_unknown_init: no memory left\n");
347  }
348 
349  return r;
350 }
351 
352 static const uint8_t coap_proxy_resource_uri[] =
353  "- Proxy URI -";
354 
357  size_t host_name_count, const char *host_name_list[]) {
358  coap_resource_t *r;
359 
360  if (host_name_count == 0) {
362  "coap_resource_proxy_uri_init: Must have one or more host names defined\n");
363  return NULL;
364  }
366  if (r) {
367  size_t i;
368  memset(r, 0, sizeof(coap_resource_t));
369  r->is_proxy_uri = 1;
370  /* Something unlikely to be used, but it shows up in the logs */
372  /* Preset all the handlers */
373  for (i = 0; i < (sizeof(r->handler) / sizeof(r->handler[0])); i++) {
374  r->handler[i] = handler;
375  }
376  if (host_name_count) {
377  r->proxy_name_list = coap_malloc(host_name_count *
378  sizeof(coap_str_const_t*));
379  if (r->proxy_name_list) {
380  for (i = 0; i < host_name_count; i++) {
381  r->proxy_name_list[i] =
382  coap_new_str_const((const uint8_t*)host_name_list[i],
383  strlen(host_name_list[i]));
384  if (!r->proxy_name_list[i]) {
386  "coap_resource_proxy_uri_init: unable to add host name\n");
387  if (i == 0) {
389  r->proxy_name_list = NULL;
390  }
391  break;
392  }
393  }
394  r->proxy_name_count = i;
395  }
396  }
397  } else {
398  coap_log(LOG_DEBUG, "coap_resource_proxy_uri_init: no memory left\n");
399  }
400 
401  return r;
402 }
403 
404 coap_attr_t *
406  coap_str_const_t *name,
407  coap_str_const_t *val,
408  int flags) {
409  coap_attr_t *attr;
410 
411  if (!resource || !name)
412  return NULL;
413 
415 
416  if (attr) {
417  if (!(flags & COAP_ATTR_FLAGS_RELEASE_NAME)) {
418  /* Need to take a copy if caller is not providing a release request */
419  name = coap_new_str_const(name->s, name->length);
420  }
421  attr->name = name;
422  if (val) {
423  if (!(flags & COAP_ATTR_FLAGS_RELEASE_VALUE)) {
424  /* Need to take a copy if caller is not providing a release request */
425  val = coap_new_str_const(val->s, val->length);
426  }
427  }
428  attr->value = val;
429 
430  attr->flags = flags;
431 
432  /* add attribute to resource list */
433  LL_PREPEND(resource->link_attr, attr);
434  } else {
435  coap_log(LOG_DEBUG, "coap_add_attr: no memory left\n");
436  }
437 
438  return attr;
439 }
440 
441 coap_attr_t *
443  coap_str_const_t *name) {
444  coap_attr_t *attr;
445 
446  if (!resource || !name)
447  return NULL;
448 
449  LL_FOREACH(resource->link_attr, attr) {
450  if (attr->name->length == name->length &&
451  memcmp(attr->name->s, name->s, name->length) == 0)
452  return attr;
453  }
454 
455  return NULL;
456 }
457 
460  if (attr)
461  return attr->value;
462  return NULL;
463 }
464 
465 void
467  if (!attr)
468  return;
470  if (attr->value) {
472  }
473 
474 #ifdef WITH_LWIP
475  memp_free(MEMP_COAP_RESOURCEATTR, attr);
476 #endif
477 #ifndef WITH_LWIP
479 #endif
480 }
481 
486 
487 static void coap_notify_observers(coap_context_t *context, coap_resource_t *r,
488  coap_deleting_resource_t deleting);
489 
490 static void
492  coap_attr_t *attr, *tmp;
493  coap_subscription_t *obs, *otmp;
494 
495  assert(resource);
496 
497  coap_resource_notify_observers(resource, NULL);
499 
500  if (resource->context->release_userdata && resource->user_data)
501  resource->context->release_userdata(resource->user_data);
502 
503  /* delete registered attributes */
504  LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
505 
506  /* Either the application provided or libcoap copied - need to delete it */
507  coap_delete_str_const(resource->uri_path);
508 
509  /* free all elements from resource->subscribers */
510  LL_FOREACH_SAFE( resource->subscribers, obs, otmp ) {
512  if (obs->query)
514  COAP_FREE_TYPE( subscription, obs );
515  }
516  if (resource->proxy_name_count && resource->proxy_name_list) {
517  size_t i;
518 
519  for (i = 0; i < resource->proxy_name_count; i++) {
521  }
522  coap_free(resource->proxy_name_list);
523  }
524 
525 #ifdef WITH_LWIP
526  memp_free(MEMP_COAP_RESOURCE, resource);
527 #endif
528 #ifndef WITH_LWIP
529  coap_free_type(COAP_RESOURCE, resource);
530 #endif /* WITH_CONTIKI */
531 }
532 
533 void
535  if (resource->is_unknown) {
536  if (context->unknown_resource)
538  context->unknown_resource = resource;
539  }
540  else if (resource->is_proxy_uri) {
541  if (context->proxy_uri_resource)
543  context->proxy_uri_resource = resource;
544  }
545  else {
547  resource->uri_path);
548 
549  if (r) {
551  "coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
552  (int)resource->uri_path->length, (int)resource->uri_path->length,
553  resource->uri_path->s);
554  coap_delete_resource(context, r);
555  }
556  RESOURCES_ADD(context->resources, resource);
557  }
558  assert(resource->context == NULL);
559  resource->context = context;
560 }
561 
562 int
564  if (!context || !resource)
565  return 0;
566 
567  if (resource->is_unknown && (context->unknown_resource == resource)) {
569  context->unknown_resource = NULL;
570  return 1;
571  }
572  if (resource->is_proxy_uri && (context->proxy_uri_resource == resource)) {
574  context->proxy_uri_resource = NULL;
575  return 1;
576  }
577 
578  /* remove resource from list */
579  RESOURCES_DELETE(context->resources, resource);
580 
581  /* and free its allocated memory */
582  coap_free_resource(resource);
583 
584  return 1;
585 }
586 
587 void
589  coap_resource_t *res;
590  coap_resource_t *rtmp;
591 
592  /* Cannot call RESOURCES_ITER because coap_free_resource() releases
593  * the allocated storage. */
594 
595  HASH_ITER(hh, context->resources, res, rtmp) {
596  HASH_DELETE(hh, context->resources, res);
597  coap_free_resource(res);
598  }
599 
600  context->resources = NULL;
601 
602  if (context->unknown_resource) {
604  context->unknown_resource = NULL;
605  }
606  if (context->proxy_uri_resource) {
608  context->proxy_uri_resource = NULL;
609  }
610 }
611 
614  coap_resource_t *result;
615 
616  RESOURCES_FIND(context->resources, uri_path, result);
617 
618  return result;
619 }
620 
623  unsigned char *buf, size_t *len, size_t *offset) {
624  unsigned char *p = buf;
625  const uint8_t *bufend = buf + *len;
626  coap_attr_t *attr;
627  coap_print_status_t result = 0;
628  size_t output_length = 0;
629  const size_t old_offset = *offset;
630 
631  *len = 0;
632  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
633  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
634 
635  COPY_COND_WITH_OFFSET(p, bufend, *offset,
636  resource->uri_path->s, resource->uri_path->length, *len);
637 
638  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
639 
640  LL_FOREACH(resource->link_attr, attr) {
641 
642  PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
643 
644  COPY_COND_WITH_OFFSET(p, bufend, *offset,
645  attr->name->s, attr->name->length, *len);
646 
647  if (attr->value && attr->value->s) {
648  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
649 
650  COPY_COND_WITH_OFFSET(p, bufend, *offset,
651  attr->value->s, attr->value->length, *len);
652  }
653 
654  }
655  if (resource->observable) {
656  COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
657  }
658 
659  output_length = p - buf;
660 
661  if (output_length > COAP_PRINT_STATUS_MAX) {
663  }
664 
665  result = (coap_print_status_t)output_length;
666 
667  if (result + old_offset - *offset < *len) {
668  result |= COAP_PRINT_STATUS_TRUNC;
669  }
670 
671  return result;
672 }
673 
674 void
676  coap_request_t method,
677  coap_method_handler_t handler) {
678  assert(resource);
679  assert(method > 0 && (size_t)(method-1) < sizeof(resource->handler)/sizeof(coap_method_handler_t));
680  resource->handler[method-1] = handler;
681 }
682 
685  const coap_binary_t *token) {
687 
688  assert(resource);
689  assert(session);
690 
691  LL_FOREACH(resource->subscribers, s) {
692  if (s->session == session
693  && (!token || (token->length == s->token_length
694  && memcmp(token->s, s->token, token->length) == 0)))
695  return s;
696  }
697 
698  return NULL;
699 }
700 
701 static coap_subscription_t *
703  const coap_string_t *query) {
705 
706  assert(resource);
707  assert(session);
708 
709  LL_FOREACH(resource->subscribers, s) {
710  if (s->session == session
711  && ((!query && !s->query)
712  || (query && s->query && coap_string_equal(query, s->query))))
713  return s;
714  }
715 
716  return NULL;
717 }
718 
721  coap_session_t *session,
722  const coap_binary_t *token,
723  coap_string_t *query,
724  int has_block2,
725  coap_block_t block,
726  coap_pdu_code_t code) {
728 
729  assert( session );
730 
731  /* Check if there is already a subscription for this peer. */
732  s = coap_find_observer(resource, session, token);
733  if (!s) {
734  /*
735  * Cannot allow a duplicate to be created for the same query as application
736  * may not be cleaning up duplicates. If duplicate found, then original
737  * observer is deleted and a new one created with the new token
738  */
739  s = coap_find_observer_query(resource, session, query);
740  if (s) {
741  /* Delete old entry with old token */
742  coap_binary_t tmp_token = { s->token_length, s->token };
743  coap_delete_observer(resource, session, &tmp_token);
744  s = NULL;
745  }
746  }
747 
748  /* We are done if subscription was found. */
749  if (s) {
750  if (s->query)
752  s->query = query;
753  s->code = code;
754  return s;
755  }
756 
757  /* Create a new subscription */
758  s = COAP_MALLOC_TYPE(subscription);
759 
760  if (!s) {
761  /* query is not deleted so it can be used in the calling function
762  * which must give up ownership of query only if this function
763  * does not return NULL. */
764  return NULL;
765  }
766 
768  s->session = coap_session_reference( session );
769 
770  if (token && token->length) {
771  s->token_length = token->length;
772  memcpy(s->token, token->s, min(s->token_length, 8));
773  }
774 
775  s->query = query;
776 
777  s->has_block2 = has_block2;
778  s->block = block;
779 
780  s->code = code;
781 
782  /* add subscriber to resource */
783  LL_PREPEND(resource->subscribers, s);
784 
785  coap_log(LOG_DEBUG, "create new subscription\n");
786 
787  return s;
788 }
789 
790 void
792  const coap_binary_t *token) {
794 
795  RESOURCES_ITER(context->resources, r) {
796  s = coap_find_observer(r, session, token);
797  if (s) {
798  s->fail_cnt = 0;
799  }
800  }
801 }
802 
803 int
805  const coap_binary_t *token) {
807 
808  s = coap_find_observer(resource, session, token);
809 
810  if ( s && coap_get_log_level() >= LOG_DEBUG ) {
811  char outbuf[2 * 8 + 1] = "";
812  unsigned int i;
813  for ( i = 0; i < s->token_length; i++ )
814  snprintf( &outbuf[2 * i], 3, "%02x", s->token[i] );
815  coap_log(LOG_DEBUG, "removed observer with token '%s'\n", outbuf);
816  }
817 
818  if (resource->subscribers && s) {
819  LL_DELETE(resource->subscribers, s);
820  coap_session_release( session );
821  if (s->query)
823  COAP_FREE_TYPE(subscription,s);
824  }
825 
826  return s != NULL;
827 }
828 
829 void
831  RESOURCES_ITER(context->resources, resource) {
832  coap_subscription_t *s, *tmp;
833  LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
834  if (s->session == session) {
835  LL_DELETE(resource->subscribers, s);
836  coap_session_release(session);
837  if (s->query)
839  COAP_FREE_TYPE(subscription, s);
840  }
841  }
842  }
843 }
844 
845 static void
847  coap_deleting_resource_t deleting) {
849  coap_subscription_t *obs;
850  coap_binary_t token;
851  coap_pdu_t *response;
852 
853  if (r->observable && (r->dirty || r->partiallydirty)) {
854  r->partiallydirty = 0;
855 
856  LL_FOREACH(r->subscribers, obs) {
857  if (r->dirty == 0 && obs->dirty == 0) {
858  /*
859  * running this resource due to partiallydirty, but this observation's
860  * notification was already enqueued
861  */
862  context->observe_pending = 1;
863  continue;
864  }
865  if (obs->session->con_active >= COAP_DEFAULT_NSTART &&
867  (obs->non_cnt >= COAP_OBS_MAX_NON))) {
868  r->partiallydirty = 1;
869  obs->dirty = 1;
870  context->observe_pending = 1;
871  continue;
872  }
873 
875  obs->dirty = 0;
876  /* initialize response */
878  if (!response) {
879  obs->dirty = 1;
880  r->partiallydirty = 1;
881  context->observe_pending = 1;
883  "coap_check_notify: pdu init failed, resource stays "
884  "partially dirty\n");
885  continue;
886  }
887 
888  if (!coap_add_token(response, obs->token_length, obs->token)) {
889  obs->dirty = 1;
890  r->partiallydirty = 1;
891  context->observe_pending = 1;
893  "coap_check_notify: cannot add token, resource stays "
894  "partially dirty\n");
895  coap_delete_pdu(response);
896  continue;
897  }
898 
899  token.length = obs->token_length;
900  token.s = obs->token;
901 
902  obs->mid = response->mid = coap_new_message_id(obs->session);
903  if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 &&
905  obs->non_cnt < COAP_OBS_MAX_NON)) {
906  response->type = COAP_MESSAGE_NON;
907  } else {
908  response->type = COAP_MESSAGE_CON;
909  }
910  switch (deleting) {
912  /* fill with observer-specific data */
913 
914  h = r->handler[obs->code - 1];
915  assert(h); /* we do not allow subscriptions if no
916  * GET/FETCH handler is defined */
917  h(context, r, obs->session, NULL, &token, obs->query, response);
918  /* Check if lg_xmit generated and update PDU code if so */
919  coap_check_code_lg_xmit(obs->session, response, r, obs->query);
920  if (COAP_RESPONSE_CLASS(response->code) > 2) {
921  coap_delete_observer(r, obs->session, &token);
922  }
923  break;
925  default:
926  response->type = COAP_MESSAGE_NON;
927  response->code = COAP_RESPONSE_CODE(404);
928  break;
929  }
930 
931  if (response->type == COAP_MESSAGE_CON ||
933  obs->non_cnt = 0;
934  } else {
935  obs->non_cnt++;
936  }
937 
938  mid = coap_send( obs->session, response );
939 
940  if (COAP_INVALID_MID == mid) {
942  "coap_check_notify: sending failed, resource stays "
943  "partially dirty\n");
944  obs->dirty = 1;
945  r->partiallydirty = 1;
946  context->observe_pending = 1;
947  }
948 
949  }
950  }
951  r->dirty = 0;
952 }
953 
954 int
956  return coap_resource_notify_observers(r, query);
957 }
958 
959 int
961  if (!r->observable)
962  return 0;
963  if (query) {
964  coap_subscription_t *obs;
965  int found = 0;
966  LL_FOREACH(r->subscribers, obs) {
967  if (obs->query
968  && obs->query->length==query->length
969  && memcmp(obs->query->s, query->s, query->length)==0 ) {
970  found = 1;
971  if (!r->dirty && !obs->dirty) {
972  obs->dirty = 1;
973  r->partiallydirty = 1;
974  }
975  }
976  }
977  if (!found)
978  return 0;
979  } else {
980  if ( !r->subscribers )
981  return 0;
982  r->dirty = 1;
983  }
984 
985  /* Increment value for next Observe use. Observe value must be < 2^24 */
986  r->observe = (r->observe + 1) & 0xFFFFFF;
987 
988  assert(r->context);
989  r->context->observe_pending = 1;
990 #ifdef COAP_EPOLL_SUPPORT
991  if (r->context->eptimerfd != -1) {
992  /* Need to immediately trigger any epoll_wait() */
993  struct itimerspec new_value;
994  int ret;
995 
996  memset(&new_value, 0, sizeof(new_value));
997  new_value.it_value.tv_nsec = 1; /* small that is not zero */
998  ret = timerfd_settime(r->context->eptimerfd, 0, &new_value, NULL);
999  if (ret == -1) {
1000  coap_log(LOG_ERR,
1001  "%s: timerfd_settime failed: %s (%d)\n",
1002  "coap_resource_notify_observers",
1003  coap_socket_strerror(), errno);
1004  }
1005  }
1006 #endif /* COAP_EPOLL_SUPPORT */
1007  return 1;
1008 }
1009 
1010 void
1012  resource->flags = (resource->flags &
1015 }
1016 
1017 void
1019  resource->user_data = data;
1020 }
1021 
1022 void *
1024  return resource->user_data;
1025 }
1026 
1027 void
1030  context->release_userdata = callback;
1031 }
1032 
1033 void
1035  resource->observable = mode ? 1 : 0;
1036 }
1037 
1040  if (resource)
1041  return resource->uri_path;
1042  return NULL;
1043 }
1044 
1045 void
1047 
1048  if (context->observe_pending) {
1049  context->observe_pending = 0;
1050  RESOURCES_ITER(context->resources, r) {
1052  }
1053  }
1054 }
1055 
1066 static void
1068  coap_resource_t *resource,
1069  coap_session_t *session,
1070  const coap_binary_t *token) {
1071  coap_subscription_t *obs, *otmp;
1072 
1073  LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
1074  if ( obs->session == session &&
1075  token->length == obs->token_length &&
1076  memcmp(token->s, obs->token, token->length) == 0) {
1077 
1078  /* count failed notifies and remove when
1079  * COAP_MAX_FAILED_NOTIFY is reached */
1080  if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
1081  obs->fail_cnt++;
1082  else {
1083  LL_DELETE(resource->subscribers, obs);
1084  obs->fail_cnt = 0;
1085 
1086  if (LOG_DEBUG <= coap_get_log_level()) {
1087 #ifndef INET6_ADDRSTRLEN
1088 #define INET6_ADDRSTRLEN 40
1089 #endif
1090  unsigned char addr[INET6_ADDRSTRLEN+8];
1091 
1093  addr, INET6_ADDRSTRLEN+8))
1094  coap_log(LOG_DEBUG, "** removed observer %s\n", addr);
1095  }
1096  coap_cancel_all_messages(context, obs->session,
1097  obs->token, obs->token_length);
1098  coap_session_release( obs->session );
1099  if (obs->query)
1100  coap_delete_string(obs->query);
1101  COAP_FREE_TYPE(subscription, obs);
1102  }
1103  break; /* break loop if observer was found */
1104  }
1105  }
1106 }
1107 
1108 void
1110  coap_session_t *session,
1111  const coap_binary_t *token) {
1112 
1113  RESOURCES_ITER(context->resources, r) {
1114  coap_remove_failed_observers(context, r, session, token);
1115  }
1116 }
Pulls together all the internal only header files.
const char * coap_socket_strerror(void)
Definition: coap_io.c:1504
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
Definition: block.c:1970
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition: coap_session.h:425
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:169
#define RESOURCES_ADD(r, obj)
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:588
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:466
#define RESOURCES_FIND(r, k, res)
#define RESOURCES_DELETE(r, obj)
#define RESOURCES_ITER(r, tmp)
coap_resource_t * coap_resource_proxy_uri_init(coap_method_handler_t handler, size_t host_name_count, const char *host_name_list[])
Creates a new resource object for handling proxy URIs.
Definition: resource.c:356
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Notifications will be sent non-confirmable by default.
Definition: resource.h:60
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:50
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:622
#define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS
Notifications will always be sent non-confirmable.
Definition: resource.h:76
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition: resource.h:49
void coap_resource_set_mode(coap_resource_t *resource, int mode)
Sets the notification message type of resource resource to given mode.
Definition: resource.c:1011
coap_str_const_t * coap_resource_get_uri_path(coap_resource_t *resource)
Get the uri_path from a resource.
Definition: resource.c:1039
coap_str_const_t * coap_attr_get_value(coap_attr_t *attr)
Returns attribute's value.
Definition: resource.c:459
#define COAP_PRINT_STATUS_TRUNC
Definition: resource.h:330
void coap_resource_release_userdata_handler(coap_context_t *context, coap_resource_release_userdata_handler_t callback)
Defines the context wide callback to use to when the resource is deleted to release the data held in ...
Definition: resource.c:1028
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:613
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable.
Definition: resource.h:66
void(* coap_method_handler_t)(coap_context_t *, 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:41
void coap_register_handler(coap_resource_t *resource, coap_request_t method, coap_method_handler_t handler)
Registers the specified handler as message handler for the request type method.
Definition: resource.c:675
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:335
coap_attr_t * coap_find_attr(coap_resource_t *resource, coap_str_const_t *name)
Returns resource's coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:442
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:325
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:301
void(* coap_resource_release_userdata_handler_t)(void *user_data)
Definition of release resource user_data callback function.
Definition: resource.h:213
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:534
void coap_resource_set_userdata(coap_resource_t *resource, void *data)
Sets the user_data.
Definition: resource.c:1018
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:405
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:329
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:328
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
Definition: resource.c:563
void * coap_resource_get_userdata(coap_resource_t *resource)
Gets the user_data.
Definition: resource.c:1023
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free'd by coap_delete_resource().
Definition: resource.h:53
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:2016
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1154
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:61
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:171
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:150
@ LOG_ERR
Error.
Definition: coap_debug.h:53
@ LOG_WARNING
Warning.
Definition: coap_debug.h:54
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:57
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:960
void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
Definition: resource.c:1034
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:209
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:246
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:140
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:229
coap_request_t
CoAP PDU Request methods.
Definition: pdu.h:64
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:138
#define COAP_RESPONSE_CLASS(C)
Definition: pdu.h:141
coap_pdu_code_t
Set of codes available for a PDU.
Definition: pdu.h:289
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:256
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: pdu.h:232
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: pdu.c:85
@ COAP_REQUEST_PUT
Definition: pdu.h:67
@ COAP_MESSAGE_NON
Definition: pdu.h:56
@ COAP_MESSAGE_CON
Definition: pdu.h:55
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:242
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:51
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: str.h:181
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:42
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:38
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:804
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:684
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:830
void coap_check_notify(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
Definition: resource.c:1046
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 block, coap_pdu_code_t code)
Adds the specified peer as observer for resource.
Definition: resource.c:720
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Handles a failed observe notify.
Definition: resource.c:1109
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
void coap_subscription_init(coap_subscription_t *s)
Definition: subscribe.c:13
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Flags that data is ready to be sent to observers.
Definition: resource.c:791
#define COAP_UNUSED
Definition: libcoap.h:53
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
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.
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:96
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:103
@ COAP_RESOURCE
Definition: mem.h:39
@ COAP_RESOURCEATTR
Definition: mem.h:40
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
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:26
#define MATCH_URI
static const uint8_t coap_unknown_resource_uri[]
Definition: resource.c:331
static const uint8_t coap_proxy_resource_uri[]
Definition: resource.c:352
static void coap_free_resource(coap_resource_t *resource)
Definition: resource.c:491
#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:85
static coap_str_const_t null_path_value
Definition: resource.c:297
int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Definition: resource.c:955
static int match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, int match_substring)
Definition: resource.c:105
#define MATCH_SUBSTRING
coap_deleting_resource_t
Definition: resource.c:482
@ COAP_DELETING_RESOURCE
Definition: resource.c:483
@ COAP_NOT_DELETING_RESOURCE
Definition: resource.c:484
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:1067
#define COAP_PRINT_STATUS_MAX
Definition: resource.c:61
#define COAP_MALLOC_TYPE(Type)
Definition: resource.c:56
static coap_subscription_t * coap_find_observer_query(coap_resource_t *resource, coap_session_t *session, const coap_string_t *query)
Definition: resource.c:702
static coap_str_const_t * null_path
Definition: resource.c:298
#define COAP_FREE_TYPE(Type, Object)
Definition: resource.c:58
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r, coap_deleting_resource_t deleting)
Definition: resource.c:846
#define min(a, b)
Definition: resource.c:64
#define MATCH_PREFIX
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
Definition: resource.c:97
#define INET6_ADDRSTRLEN
coap_address_t remote
remote address and port
Definition: coap_io.h:49
Abstraction of attribute associated with a resource.
coap_str_const_t * value
Value of the attribute (can be NULL)
coap_str_const_t * name
Name of the attribute.
CoAP binary data definition.
Definition: str.h:48
size_t length
length of binary data
Definition: str.h:49
uint8_t * s
binary data
Definition: str.h:50
Structure of Block options.
Definition: block.h:33
The CoAP stack's global state is stored in a coap_context_t object.
coap_resource_t * resources
hash table or list of known resources
coap_resource_release_userdata_handler_t release_userdata
function to release user_data when resource is deleted
uint8_t observe_pending
Observe response pending.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_mid_t mid
message id, if any, in regular host byte order
coap_pdu_type_t type
message type
Abstraction of resource that can be attached to coap_context_t.
unsigned int dirty
set to 1 if resource has changed
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
coap_subscription_t * subscribers
list of observers for this resource
void * user_data
This pointer is under user control.
coap_str_const_t ** proxy_name_list
Array valid names this host is known by (proxy support)
coap_str_const_t * uri_path
Request URI Path for this resource.
unsigned int observe
The next value for the Observe option.
coap_context_t * context
Pointer back to the context that 'owns' this resource.
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH.
unsigned int is_proxy_uri
resource created for proxy URI handler
unsigned int is_unknown
resource created for unknown handler
coap_attr_t * link_attr
attributes to be included with the link format
unsigned int observable
can be observed
size_t proxy_name_count
Count of valid names this host is known by (proxy support)
int flags
zero or more COAP_RESOURCE_FLAGS_* or'd together
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
key: remote/local address info
uint8_t con_active
Active CON request sent.
CoAP string data definition with const data.
Definition: str.h:38
const uint8_t * s
read-only string data
Definition: str.h:40
size_t length
length of string
Definition: str.h:39
CoAP string data definition.
Definition: str.h:30
uint8_t * s
string data
Definition: str.h:32
size_t length
length of string
Definition: str.h:31
Subscriber information.
coap_block_t block
GET/FETCH request Block definition.
unsigned char token[8]
token used for subscription
unsigned int has_block2
GET request had Block2 definition.
coap_mid_t mid
request type code (GET/FETCH)
unsigned int fail_cnt
up to 3 confirmable notifies can fail
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
size_t token_length
actual length of token
struct coap_session_t * session
subscriber session
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource's partially dirty f...
struct coap_string_t * query
query string used for subscription, if any
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:448
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:1059
#define LL_DELETE(head, del)
Definition: utlist.h:385
#define LL_FOREACH(head, el)
Definition: utlist.h:413
#define LL_PREPEND(head, add)
Definition: utlist.h:314
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:419