libcoap  4.3.1
resource.c
Go to the documentation of this file.
1 /* resource.c -- generic resource handling
2  *
3  * Copyright (C) 2010--2022 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * This file is part of the CoAP library libcoap. Please see
8  * README for terms of use.
9  */
10 
16 #include "coap3/coap_internal.h"
17 
18 #if COAP_SERVER_SUPPORT
19 #include <stdio.h>
20 #include <errno.h>
21 
22 #ifdef COAP_EPOLL_SUPPORT
23 #include <sys/epoll.h>
24 #include <sys/timerfd.h>
25 #endif /* COAP_EPOLL_SUPPORT */
26 
27 #if defined(WITH_LWIP)
28 /* mem.h is only needed for the string free calls for
29  * COAP_ATTR_FLAGS_RELEASE_NAME / COAP_ATTR_FLAGS_RELEASE_VALUE /
30  * COAP_RESOURCE_FLAGS_RELEASE_URI. not sure what those lines should actually
31  * do on lwip. */
32 
33 #include <lwip/memp.h>
34 
35 #define COAP_MALLOC_TYPE(Type) \
36  ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
37 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
38 
39 #elif defined(WITH_CONTIKI)
40 #include "memb.h"
41 
42 #define COAP_MALLOC_TYPE(Type) \
43  ((coap_##Type##_t *)memb_alloc(&(Type##_storage)))
44 #define COAP_FREE_TYPE(Type, Object) memb_free(&(Type##_storage), (Object))
45 
46 MEMB(subscription_storage, coap_subscription_t, COAP_MAX_SUBSCRIBERS);
47 
48 void
49 coap_resources_init() {
50  memb_init(&subscription_storage);
51 }
52 
54 coap_malloc_subscription() {
55  return memb_alloc(&subscription_storage);
56 }
57 
59 coap_free_subscription(coap_subscription_t *subscription) {
60  memb_free(&subscription_storage, subscription);
61 }
62 
63 #else
64 #define COAP_MALLOC_TYPE(Type) \
65  ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
66 #define COAP_FREE_TYPE(Type, Object) coap_free(Object)
67 #endif
68 
69 #define COAP_PRINT_STATUS_MAX (~COAP_PRINT_STATUS_MASK)
70 
71 #ifndef min
72 #define min(a,b) ((a) < (b) ? (a) : (b))
73 #endif
74 
75 /* Helper functions for conditional output of character sequences into
76  * a given buffer. The first Offset characters are skipped.
77  */
78 
83 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \
84  if ((Offset) == 0) { \
85  (*(Buf)++) = (Char); \
86  } else { \
87  (Offset)--; \
88  } \
89 
93 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
94  if ((Buf) < (Bufend)) { \
95  PRINT_WITH_OFFSET(Buf,Offset,Char); \
96  } \
97  (Result)++; \
98  }
99 
105 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
106  size_t i; \
107  for (i = 0; i < (Length); i++) { \
108  PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
109  } \
110  }
111 
112 static int
113 match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix,
114  int match_substring
115 ) {
116  assert(text); assert(pattern);
117 
118  if (text->length < pattern->length)
119  return 0;
120 
121  if (match_substring) {
122  const uint8_t *next_token = text->s;
123  size_t remaining_length = text->length;
124  while (remaining_length) {
125  size_t token_length;
126  const uint8_t *token = next_token;
127  next_token = (unsigned char *)memchr(token, ' ', remaining_length);
128 
129  if (next_token) {
130  token_length = next_token - token;
131  remaining_length -= (token_length + 1);
132  next_token++;
133  } else {
134  token_length = remaining_length;
135  remaining_length = 0;
136  }
137 
138  if ((match_prefix || pattern->length == token_length) &&
139  memcmp(token, pattern->s, pattern->length) == 0)
140  return 1;
141  }
142  return 0;
143  }
144 
145  return (match_prefix || pattern->length == text->length) &&
146  memcmp(text->s, pattern->s, pattern->length) == 0;
147 }
148 
170 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
172 coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
173  size_t offset,
174  const coap_string_t *query_filter COAP_UNUSED) {
175 #else /* not a GCC */
177 coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
178  size_t offset, const coap_string_t *query_filter) {
179 #endif /* GCC */
180  size_t output_length = 0;
181  unsigned char *p = buf;
182  const uint8_t *bufend = buf + *buflen;
183  size_t left, written = 0;
184  coap_print_status_t result;
185  const size_t old_offset = offset;
186  int subsequent_resource = 0;
187 #ifndef WITHOUT_QUERY_FILTER
188  coap_str_const_t resource_param = { 0, NULL }, query_pattern = { 0, NULL };
189  int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
190 #define MATCH_URI 0x01
191 #define MATCH_PREFIX 0x02
192 #define MATCH_SUBSTRING 0x04
193  static const coap_str_const_t _rt_attributes[] = {
194  {2, (const uint8_t *)"rt"},
195  {2, (const uint8_t *)"if"},
196  {3, (const uint8_t *)"rel"},
197  {0, NULL}};
198 #endif /* WITHOUT_QUERY_FILTER */
199 
200 #ifndef WITHOUT_QUERY_FILTER
201  /* split query filter, if any */
202  if (query_filter) {
203  resource_param.s = query_filter->s;
204  while (resource_param.length < query_filter->length &&
205  resource_param.s[resource_param.length] != '=')
206  resource_param.length++;
207 
208  if (resource_param.length < query_filter->length) {
209  const coap_str_const_t *rt_attributes;
210  if (resource_param.length == 4 &&
211  memcmp(resource_param.s, "href", 4) == 0)
212  flags |= MATCH_URI;
213 
214  for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
215  if (resource_param.length == rt_attributes->length &&
216  memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
217  flags |= MATCH_SUBSTRING;
218  break;
219  }
220  }
221 
222  /* rest is query-pattern */
223  query_pattern.s =
224  query_filter->s + resource_param.length + 1;
225 
226  assert((resource_param.length + 1) <= query_filter->length);
227  query_pattern.length =
228  query_filter->length - (resource_param.length + 1);
229 
230  if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
231  query_pattern.s++;
232  query_pattern.length--;
233  }
234 
235  if (query_pattern.length &&
236  query_pattern.s[query_pattern.length-1] == '*') {
237  query_pattern.length--;
238  flags |= MATCH_PREFIX;
239  }
240  }
241  }
242 #endif /* WITHOUT_QUERY_FILTER */
243 
244  RESOURCES_ITER(context->resources, r) {
245 
246 #ifndef WITHOUT_QUERY_FILTER
247  if (resource_param.length) { /* there is a query filter */
248 
249  if (flags & MATCH_URI) { /* match resource URI */
250  if (!match(r->uri_path, &query_pattern, (flags & MATCH_PREFIX) != 0,
251  (flags & MATCH_SUBSTRING) != 0))
252  continue;
253  } else { /* match attribute */
254  coap_attr_t *attr;
255  coap_str_const_t unquoted_val;
256  attr = coap_find_attr(r, &resource_param);
257  if (!attr || !attr->value) continue;
258  unquoted_val = *attr->value;
259  if (attr->value->s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
260  unquoted_val.length -= 2;
261  unquoted_val.s += 1;
262  }
263  if (!(match(&unquoted_val, &query_pattern,
264  (flags & MATCH_PREFIX) != 0,
265  (flags & MATCH_SUBSTRING) != 0)))
266  continue;
267  }
268  }
269 #endif /* WITHOUT_QUERY_FILTER */
270 
271  if (!subsequent_resource) { /* this is the first resource */
272  subsequent_resource = 1;
273  } else {
274  PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
275  }
276 
277  left = bufend - p; /* calculate available space */
278  result = coap_print_link(r, p, &left, &offset);
279 
280  if (result & COAP_PRINT_STATUS_ERROR) {
281  break;
282  }
283 
284  /* coap_print_link() returns the number of characters that
285  * where actually written to p. Now advance to its end. */
286  p += COAP_PRINT_OUTPUT_LENGTH(result);
287  written += left;
288  }
289 
290  *buflen = written;
291  output_length = p - buf;
292 
293  if (output_length > COAP_PRINT_STATUS_MAX) {
295  }
296 
297  result = (coap_print_status_t)output_length;
298 
299  if (result + old_offset - offset < *buflen) {
300  result |= COAP_PRINT_STATUS_TRUNC;
301  }
302  return result;
303 }
304 
305 static coap_str_const_t null_path_value = {0, (const uint8_t*)""};
306 static coap_str_const_t *null_path = &null_path_value;
307 
309 coap_resource_init(coap_str_const_t *uri_path, int flags) {
310  coap_resource_t *r;
311 
313  if (r) {
314  memset(r, 0, sizeof(coap_resource_t));
315 
316  if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) {
317  /* Need to take a copy if caller is not providing a release request */
318  if (uri_path)
319  uri_path = coap_new_str_const(uri_path->s, uri_path->length);
320  else
321  uri_path = coap_new_str_const(null_path->s, null_path->length);
322  }
323  else if (!uri_path) {
324  /* Do not expect this, but ... */
325  uri_path = coap_new_str_const(null_path->s, null_path->length);
326  }
327 
328  if (uri_path)
329  r->uri_path = uri_path;
330 
331  r->flags = flags;
332  r->observe = 2;
333  } else {
334  coap_log(LOG_DEBUG, "coap_resource_init: no memory left\n");
335  }
336 
337  return r;
338 }
339 
340 static const uint8_t coap_unknown_resource_uri[] =
341  "- Unknown -";
342 
344 coap_resource_unknown_init2(coap_method_handler_t put_handler, int flags) {
345  coap_resource_t *r;
346 
348  if (r) {
349  memset(r, 0, sizeof(coap_resource_t));
350  r->is_unknown = 1;
351  /* Something unlikely to be used, but it shows up in the logs */
352  r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1);
354  coap_register_handler(r, COAP_REQUEST_PUT, put_handler);
355  } else {
356  coap_log(LOG_DEBUG, "coap_resource_unknown_init: no memory left\n");
357  }
358 
359  return r;
360 }
361 
364  return coap_resource_unknown_init2(put_handler, 0);
365 }
366 
367 static const uint8_t coap_proxy_resource_uri[] =
368  "- Proxy URI -";
369 
372  size_t host_name_count,
373  const char *host_name_list[], int flags) {
374  coap_resource_t *r;
375 
376  if (host_name_count == 0) {
378  "coap_resource_proxy_uri_init: Must have one or more host names defined\n");
379  return NULL;
380  }
382  if (r) {
383  size_t i;
384  memset(r, 0, sizeof(coap_resource_t));
385  r->is_proxy_uri = 1;
386  /* Something unlikely to be used, but it shows up in the logs */
387  r->uri_path = coap_new_str_const(coap_proxy_resource_uri, sizeof(coap_proxy_resource_uri)-1);
388  /* Preset all the handlers */
389  for (i = 0; i < (sizeof(r->handler) / sizeof(r->handler[0])); i++) {
390  r->handler[i] = handler;
391  }
392  if (host_name_count) {
393  r->proxy_name_list = coap_malloc(host_name_count *
394  sizeof(coap_str_const_t*));
395  if (r->proxy_name_list) {
396  for (i = 0; i < host_name_count; i++) {
397  r->proxy_name_list[i] =
398  coap_new_str_const((const uint8_t*)host_name_list[i],
399  strlen(host_name_list[i]));
400  if (!r->proxy_name_list[i]) {
402  "coap_resource_proxy_uri_init: unable to add host name\n");
403  if (i == 0) {
405  r->proxy_name_list = NULL;
406  }
407  break;
408  }
409  }
410  r->proxy_name_count = i;
411  }
412  }
414  } else {
415  coap_log(LOG_DEBUG, "coap_resource_proxy_uri_init2: no memory left\n");
416  }
417 
418  return r;
419 }
420 
423  size_t host_name_count, const char *host_name_list[]) {
424  return coap_resource_proxy_uri_init2(handler, host_name_count,
425  host_name_list, 0);
426 }
427 
428 coap_attr_t *
430  coap_str_const_t *name,
431  coap_str_const_t *val,
432  int flags) {
433  coap_attr_t *attr;
434 
435  if (!resource || !name)
436  return NULL;
437 
439 
440  if (attr) {
441  if (!(flags & COAP_ATTR_FLAGS_RELEASE_NAME)) {
442  /* Need to take a copy if caller is not providing a release request */
443  name = coap_new_str_const(name->s, name->length);
444  }
445  attr->name = name;
446  if (val) {
447  if (!(flags & COAP_ATTR_FLAGS_RELEASE_VALUE)) {
448  /* Need to take a copy if caller is not providing a release request */
449  val = coap_new_str_const(val->s, val->length);
450  }
451  }
452  attr->value = val;
453 
454  attr->flags = flags;
455 
456  /* add attribute to resource list */
457  LL_PREPEND(resource->link_attr, attr);
458  } else {
459  coap_log(LOG_DEBUG, "coap_add_attr: no memory left\n");
460  }
461 
462  return attr;
463 }
464 
465 coap_attr_t *
467  coap_str_const_t *name) {
468  coap_attr_t *attr;
469 
470  if (!resource || !name)
471  return NULL;
472 
473  LL_FOREACH(resource->link_attr, attr) {
474  if (attr->name->length == name->length &&
475  memcmp(attr->name->s, name->s, name->length) == 0)
476  return attr;
477  }
478 
479  return NULL;
480 }
481 
484  if (attr)
485  return attr->value;
486  return NULL;
487 }
488 
489 void
491  if (!attr)
492  return;
494  if (attr->value) {
496  }
497 
498 #ifdef WITH_LWIP
499  memp_free(MEMP_COAP_RESOURCEATTR, attr);
500 #endif
501 #ifndef WITH_LWIP
503 #endif
504 }
505 
506 typedef enum coap_deleting_resource_t {
507  COAP_DELETING_RESOURCE,
508  COAP_NOT_DELETING_RESOURCE
509 } coap_deleting_resource_t;
510 
511 static void coap_notify_observers(coap_context_t *context, coap_resource_t *r,
512  coap_deleting_resource_t deleting);
513 
514 static void
515 coap_free_resource(coap_resource_t *resource) {
516  coap_attr_t *attr, *tmp;
517  coap_subscription_t *obs, *otmp;
518 
519  assert(resource);
520 
521  coap_resource_notify_observers(resource, NULL);
522  coap_notify_observers(resource->context, resource, COAP_DELETING_RESOURCE);
523 
524  if (resource->context->release_userdata && resource->user_data)
525  resource->context->release_userdata(resource->user_data);
526 
527  /* delete registered attributes */
528  LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
529 
530  /* Either the application provided or libcoap copied - need to delete it */
531  coap_delete_str_const(resource->uri_path);
532 
533  /* free all elements from resource->subscribers */
534  LL_FOREACH_SAFE( resource->subscribers, obs, otmp ) {
536  coap_delete_pdu(obs->pdu);
538  COAP_FREE_TYPE( subscription, obs );
539  }
540  if (resource->proxy_name_count && resource->proxy_name_list) {
541  size_t i;
542 
543  for (i = 0; i < resource->proxy_name_count; i++) {
545  }
546  coap_free(resource->proxy_name_list);
547  }
548 
549 #ifdef WITH_LWIP
550  memp_free(MEMP_COAP_RESOURCE, resource);
551 #endif
552 #ifndef WITH_LWIP
553  coap_free_type(COAP_RESOURCE, resource);
554 #endif /* WITH_CONTIKI */
555 }
556 
557 void
558 coap_add_resource(coap_context_t *context, coap_resource_t *resource) {
559  if (resource->is_unknown) {
560  if (context->unknown_resource)
561  coap_free_resource(context->unknown_resource);
562  context->unknown_resource = resource;
563  }
564  else if (resource->is_proxy_uri) {
565  if (context->proxy_uri_resource)
566  coap_free_resource(context->proxy_uri_resource);
567  context->proxy_uri_resource = resource;
568  }
569  else {
571  resource->uri_path);
572 
573  if (r) {
575  "coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
576  (int)resource->uri_path->length, (int)resource->uri_path->length,
577  resource->uri_path->s);
578  coap_delete_resource(context, r);
579  }
580  RESOURCES_ADD(context->resources, resource);
581  }
582  assert(resource->context == NULL);
583  resource->context = context;
584 }
585 
586 /*
587  * Input context is ignored, but param left there to keep API consistent
588  */
589 int
591  if (!resource)
592  return 0;
593 
594  context = resource->context;
595 
596  if (resource->is_unknown) {
597  if (context && context->unknown_resource == resource) {
598  context->unknown_resource = NULL;
599  }
600  } else if (resource->is_proxy_uri) {
601  if (context && context->proxy_uri_resource == resource) {
602  context->proxy_uri_resource = NULL;
603  }
604  } else if (context) {
605  /* remove resource from list */
606  RESOURCES_DELETE(context->resources, resource);
607  }
608 
609  /* and free its allocated memory */
610  coap_free_resource(resource);
611 
612  return 1;
613 }
614 
615 void
617  coap_resource_t *res;
618  coap_resource_t *rtmp;
619 
620  /* Cannot call RESOURCES_ITER because coap_free_resource() releases
621  * the allocated storage. */
622 
623  HASH_ITER(hh, context->resources, res, rtmp) {
624  HASH_DELETE(hh, context->resources, res);
625  coap_free_resource(res);
626  }
627 
628  context->resources = NULL;
629 
630  if (context->unknown_resource) {
631  coap_free_resource(context->unknown_resource);
632  context->unknown_resource = NULL;
633  }
634  if (context->proxy_uri_resource) {
635  coap_free_resource(context->proxy_uri_resource);
636  context->proxy_uri_resource = NULL;
637  }
638 }
639 
642  coap_resource_t *result;
643 
644  RESOURCES_FIND(context->resources, uri_path, result);
645 
646  return result;
647 }
648 
650 coap_print_link(const coap_resource_t *resource,
651  unsigned char *buf, size_t *len, size_t *offset) {
652  unsigned char *p = buf;
653  const uint8_t *bufend = buf + *len;
654  coap_attr_t *attr;
655  coap_print_status_t result = 0;
656  size_t output_length = 0;
657  const size_t old_offset = *offset;
658 
659  *len = 0;
660  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
661  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
662 
663  COPY_COND_WITH_OFFSET(p, bufend, *offset,
664  resource->uri_path->s, resource->uri_path->length, *len);
665 
666  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
667 
668  LL_FOREACH(resource->link_attr, attr) {
669 
670  PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
671 
672  COPY_COND_WITH_OFFSET(p, bufend, *offset,
673  attr->name->s, attr->name->length, *len);
674 
675  if (attr->value && attr->value->s) {
676  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
677 
678  COPY_COND_WITH_OFFSET(p, bufend, *offset,
679  attr->value->s, attr->value->length, *len);
680  }
681 
682  }
683  if (resource->observable) {
684  COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
685  }
686 
687  output_length = p - buf;
688 
689  if (output_length > COAP_PRINT_STATUS_MAX) {
691  }
692 
693  result = (coap_print_status_t)output_length;
694 
695  if (result + old_offset - *offset < *len) {
696  result |= COAP_PRINT_STATUS_TRUNC;
697  }
698 
699  return result;
700 }
701 
702 void
704  coap_request_t method,
705  coap_method_handler_t handler) {
706  coap_register_request_handler(resource, method, handler);
707 }
708 
709 void
711  coap_request_t method,
712  coap_method_handler_t handler) {
713  assert(resource);
714  assert(method > 0 && (size_t)(method-1) <
715  sizeof(resource->handler)/sizeof(coap_method_handler_t));
716  resource->handler[method-1] = handler;
717 }
718 
721  const coap_binary_t *token) {
723 
724  assert(resource);
725  assert(session);
726 
727  LL_FOREACH(resource->subscribers, s) {
728  if (s->session == session
729  && (!token || (token->length == s->pdu->token_length
730  && memcmp(token->s, s->pdu->token, token->length) == 0)))
731  return s;
732  }
733 
734  return NULL;
735 }
736 
737 static coap_subscription_t *
738 coap_find_observer_cache_key(coap_resource_t *resource, coap_session_t *session,
739  const coap_cache_key_t *cache_key) {
741 
742  assert(resource);
743  assert(session);
744 
745  LL_FOREACH(resource->subscribers, s) {
746  if (s->session == session
747  && (memcmp(cache_key, s->cache_key, sizeof(coap_cache_key_t)) == 0))
748  return s;
749  }
750 
751  return NULL;
752 }
753 
756  coap_session_t *session,
757  const coap_binary_t *token,
758  const coap_pdu_t *request) {
760  coap_cache_key_t *cache_key = NULL;
761  size_t len;
762  const uint8_t *data;
763 /* https://tools.ietf.org/html/rfc7641#section-3.6 */
764 static const uint16_t cache_ignore_options[] = { COAP_OPTION_ETAG };
765 
766  assert( session );
767 
768  /* Check if there is already a subscription for this peer. */
769  s = coap_find_observer(resource, session, token);
770  if (!s) {
771  /*
772  * Cannot allow a duplicate to be created for the same query as application
773  * may not be cleaning up duplicates. If duplicate found, then original
774  * observer is deleted and a new one created with the new token
775  */
776  cache_key = coap_cache_derive_key_w_ignore(session, request,
778  cache_ignore_options,
779  sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
780  if (cache_key) {
781  s = coap_find_observer_cache_key(resource, session, cache_key);
782  if (s) {
783  /* Delete old entry with old token */
784  coap_binary_t tmp_token = { s->pdu->token_length, s->pdu->token };
785  coap_delete_observer(resource, session, &tmp_token);
786  s = NULL;
787  }
788  }
789  }
790 
791  /* We are done if subscription was found. */
792  if (s) {
793  return s;
794  }
795 
796  /* Create a new subscription */
797  s = COAP_MALLOC_TYPE(subscription);
798 
799  if (!s) {
800  coap_delete_cache_key(cache_key);
801  return NULL;
802  }
803 
805  s->pdu = coap_pdu_duplicate(request, session, request->token_length,
806  request->token, NULL);
807  if (s->pdu == NULL) {
808  coap_delete_cache_key(cache_key);
809  COAP_FREE_TYPE(subscription, s);
810  return NULL;
811  }
812  if (coap_get_data(request, &len, &data)) {
813  coap_add_data(s->pdu, len, data);
814  }
815  if (cache_key == NULL) {
816  cache_key = coap_cache_derive_key_w_ignore(session, request,
818  cache_ignore_options,
819  sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
820  if (cache_key == NULL) {
821  coap_delete_pdu(s->pdu);
822  coap_delete_cache_key(cache_key);
823  COAP_FREE_TYPE(subscription, s);
824  return NULL;
825  }
826  }
827  s->cache_key = cache_key;
828  s->session = coap_session_reference(session);
829 
830  /* add subscriber to resource */
831  LL_PREPEND(resource->subscribers, s);
832 
833  coap_log(LOG_DEBUG, "create new subscription %p key 0x%02x%02x%02x%02x\n",
834  (void*)s, s->cache_key->key[0], s->cache_key->key[1],
835  s->cache_key->key[2], s->cache_key->key[3]);
836 
837  return s;
838 }
839 
840 void
842  const coap_binary_t *token) {
844 
845  RESOURCES_ITER(context->resources, r) {
846  s = coap_find_observer(r, session, token);
847  if (s) {
848  s->fail_cnt = 0;
849  }
850  }
851 }
852 
853 int
855  const coap_binary_t *token) {
857 
858  s = coap_find_observer(resource, session, token);
859 
860  if ( s && coap_get_log_level() >= LOG_DEBUG ) {
861  char outbuf[2 * 8 + 1] = "";
862  unsigned int i;
863  for ( i = 0; i < s->pdu->token_length; i++ )
864  snprintf( &outbuf[2 * i], 3, "%02x", s->pdu->token[i] );
866  "removed subscription %p with token '%s' key 0x%02x%02x%02x%02x\n",
867  (void*)s, outbuf, s->cache_key->key[0], s->cache_key->key[1],
868  s->cache_key->key[2], s-> cache_key->key[3]);
869  }
870 
871  if (resource->subscribers && s) {
872  LL_DELETE(resource->subscribers, s);
873  coap_session_release( session );
874  coap_delete_pdu(s->pdu);
876  COAP_FREE_TYPE(subscription,s);
877  }
878 
879  return s != NULL;
880 }
881 
882 void
884  RESOURCES_ITER(context->resources, resource) {
885  coap_subscription_t *s, *tmp;
886  LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
887  if (s->session == session) {
888  LL_DELETE(resource->subscribers, s);
889  coap_session_release(session);
890  coap_delete_pdu(s->pdu);
892  COAP_FREE_TYPE(subscription, s);
893  }
894  }
895  }
896 }
897 
898 static void
899 coap_notify_observers(coap_context_t *context, coap_resource_t *r,
900  coap_deleting_resource_t deleting) {
902  coap_subscription_t *obs, *otmp;
903  coap_binary_t token;
904  coap_pdu_t *response;
905  uint8_t buf[4];
906  coap_string_t *query;
907  coap_block_b_t block;
908  coap_tick_t now;
909 
910  if (r->observable && (r->dirty || r->partiallydirty)) {
911  r->partiallydirty = 0;
912 
913  LL_FOREACH_SAFE(r->subscribers, obs, otmp) {
914  if (r->dirty == 0 && obs->dirty == 0) {
915  /*
916  * running this resource due to partiallydirty, but this observation's
917  * notification was already enqueued
918  */
919  context->observe_pending = 1;
920  continue;
921  }
922  if (obs->session->con_active >= COAP_NSTART(obs->session) &&
924  (obs->non_cnt >= COAP_OBS_MAX_NON))) {
925  /* Waiting for the previous unsolicited response to finish */
926  r->partiallydirty = 1;
927  obs->dirty = 1;
928  context->observe_pending = 1;
929  continue;
930  }
931  coap_ticks(&now);
932  if (obs->session->lg_xmit && obs->session->lg_xmit->last_all_sent == 0 &&
933  obs->session->lg_xmit->last_obs &&
934  (obs->session->lg_xmit->last_obs + 2*COAP_TICKS_PER_SECOND) > now) {
935  /* Waiting for the previous blocked unsolicited response to finish */
936  r->partiallydirty = 1;
937  obs->dirty = 1;
938  context->observe_pending = 1;
939  continue;
940  }
941 
943  obs->dirty = 0;
944  /* initialize response */
946  if (!response) {
947  obs->dirty = 1;
948  r->partiallydirty = 1;
949  context->observe_pending = 1;
951  "coap_check_notify: pdu init failed, resource stays "
952  "partially dirty\n");
953  continue;
954  }
955 
956  if (!coap_add_token(response, obs->pdu->token_length, obs->pdu->token)) {
957  obs->dirty = 1;
958  r->partiallydirty = 1;
959  context->observe_pending = 1;
961  "coap_check_notify: cannot add token, resource stays "
962  "partially dirty\n");
963  coap_delete_pdu(response);
964  continue;
965  }
966 
967  token.length = obs->pdu->token_length;
968  token.s = obs->pdu->token;
969 
970  obs->pdu->mid = response->mid = coap_new_message_id(obs->session);
971  if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 &&
973  obs->non_cnt < COAP_OBS_MAX_NON)) {
974  response->type = COAP_MESSAGE_NON;
975  } else {
976  response->type = COAP_MESSAGE_CON;
977  }
978  switch (deleting) {
979  case COAP_NOT_DELETING_RESOURCE:
980  /* fill with observer-specific data */
982  coap_encode_var_safe(buf, sizeof (buf),
983  r->observe),
984  buf);
986  &block)) {
987  /* Will get updated later (e.g. M bit) if appropriate */
989  coap_encode_var_safe(buf, sizeof(buf),
990  ((0 << 4) |
991  (0 << 3) |
992  block.aszx)),
993  buf);
994  }
995 
996  h = r->handler[obs->pdu->code - 1];
997  assert(h); /* we do not allow subscriptions if no
998  * GET/FETCH handler is defined */
999  query = coap_get_query(obs->pdu);
1000  coap_log(LOG_DEBUG, "Observe PDU presented to app.\n");
1001  coap_show_pdu(LOG_DEBUG, obs->pdu);
1002  coap_log(LOG_DEBUG, "call custom handler for resource '%*.*s'\n",
1003  (int)r->uri_path->length, (int)r->uri_path->length,
1004  r->uri_path->s);
1005  h(r, obs->session, obs->pdu, query, response);
1006  /* Check if lg_xmit generated and update PDU code if so */
1007  coap_check_code_lg_xmit(obs->session, response, r, query,
1008  obs->pdu->code);
1009  coap_delete_string(query);
1010  if (COAP_RESPONSE_CLASS(response->code) != 2) {
1012  }
1013  if (COAP_RESPONSE_CLASS(response->code) > 2) {
1014  coap_delete_observer(r, obs->session, &token);
1015  }
1016  break;
1017  case COAP_DELETING_RESOURCE:
1018  default:
1019  /* Don't worry if it does not get there */
1020  response->type = COAP_MESSAGE_NON;
1021  response->code = COAP_RESPONSE_CODE(404);
1022  break;
1023  }
1024 
1025  if (response->type == COAP_MESSAGE_CON ||
1027  obs->non_cnt = 0;
1028  } else {
1029  obs->non_cnt++;
1030  }
1031 
1032  mid = coap_send_internal( obs->session, response );
1033 
1034  if (COAP_INVALID_MID == mid) {
1037  "coap_check_notify: sending failed, resource stays "
1038  "partially dirty\n");
1039  LL_FOREACH(r->subscribers, s) {
1040  if (s == obs) {
1041  /* obs not deleted during coap_send_internal() */
1042  obs->dirty = 1;
1043  break;
1044  }
1045  }
1046  r->partiallydirty = 1;
1047  context->observe_pending = 1;
1048  }
1049  }
1050  }
1051  r->dirty = 0;
1052 }
1053 
1054 int
1056  return coap_resource_notify_observers(r, query);
1057 }
1058 
1059 int
1061  const coap_string_t *query COAP_UNUSED) {
1062  if (!r->observable)
1063  return 0;
1064  if ( !r->subscribers )
1065  return 0;
1066  r->dirty = 1;
1067 
1068  /* Increment value for next Observe use. Observe value must be < 2^24 */
1069  r->observe = (r->observe + 1) & 0xFFFFFF;
1070 
1071  assert(r->context);
1072  r->context->observe_pending = 1;
1073 #ifdef COAP_EPOLL_SUPPORT
1075 #endif /* COAP_EPOLL_SUPPORT */
1076  return 1;
1077 }
1078 
1079 void
1080 coap_resource_set_mode(coap_resource_t *resource, int mode) {
1081  resource->flags = (resource->flags &
1084 }
1085 
1086 void
1087 coap_resource_set_userdata(coap_resource_t *resource, void *data) {
1088  resource->user_data = data;
1089 }
1090 
1091 void *
1093  return resource->user_data;
1094 }
1095 
1096 void
1099  context->release_userdata = callback;
1100 }
1101 
1102 void
1104  resource->observable = mode ? 1 : 0;
1105 }
1106 
1109  if (resource)
1110  return resource->uri_path;
1111  return NULL;
1112 }
1113 
1114 void
1116 
1117  if (context->observe_pending) {
1118  context->observe_pending = 0;
1119  RESOURCES_ITER(context->resources, r) {
1120  coap_notify_observers(context, r, COAP_NOT_DELETING_RESOURCE);
1121  }
1122  }
1123 }
1124 
1135 static void
1136 coap_remove_failed_observers(coap_context_t *context,
1137  coap_resource_t *resource,
1138  coap_session_t *session,
1139  const coap_binary_t *token) {
1140  coap_subscription_t *obs, *otmp;
1141 
1142  LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
1143  if ( obs->session == session &&
1144  token->length == obs->pdu->token_length &&
1145  memcmp(token->s, obs->pdu->token, token->length) == 0) {
1146 
1147  /* count failed notifies and remove when
1148  * COAP_OBS_MAX_FAIL is reached */
1149  obs->fail_cnt++;
1150  if (obs->fail_cnt >= COAP_OBS_MAX_FAIL) {
1151  coap_cancel_all_messages(context, obs->session,
1152  obs->pdu->token, obs->pdu->token_length);
1153  coap_delete_observer(resource, session, token);
1154  }
1155  break; /* break loop if observer was found */
1156  }
1157  }
1158 }
1159 
1160 void
1162  coap_session_t *session,
1163  const coap_binary_t *token) {
1164 
1165  RESOURCES_ITER(context->resources, r) {
1166  coap_remove_failed_observers(context, r, session, token);
1167  }
1168 }
1169 
1170 #endif /* ! COAP_SERVER_SUPPORT */
Pulls together all the internal only header files.
void coap_update_epoll_timer(coap_context_t *context, coap_tick_t delay)
Update the epoll timer fd as to when it is to trigger.
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query, coap_pdu_code_t request_method)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
Definition: block.c:2430
int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, coap_option_num_t number, coap_block_b_t *block)
Initializes block from pdu.
Definition: block.c:46
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
@ COAP_CACHE_IS_SESSION_BASED
Definition: coap_cache.h:39
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:127
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:142
#define RESOURCES_ADD(r, obj)
coap_print_status_t coap_print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, const coap_string_t *)
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
#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.
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Observe Notifications will be sent non-confirmable by default.
Definition: resource.h:61
#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.
#define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS
Observe Notifications will always be sent non-confirmable.
Definition: resource.h:77
#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.
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.
coap_str_const_t * coap_resource_get_uri_path(coap_resource_t *resource)
Get the uri_path from a resource.
coap_str_const_t * coap_attr_get_value(coap_attr_t *attribute)
Returns attribute's value.
#define COAP_PRINT_STATUS_TRUNC
Definition: resource.h:450
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 ...
#define COAP_RESOURCE_FLAGS_MCAST_LIST
Definition: resource.h:136
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Observe Notifications will be sent confirmable.
Definition: resource.h:67
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.
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.
coap_resource_t * coap_resource_proxy_uri_init2(coap_method_handler_t handler, size_t host_name_count, const char *host_name_list[], int flags)
Creates a new resource object for handling proxy URIs with configurable control over multicast reques...
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.
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:445
void(* coap_method_handler_t)(coap_resource_t *, coap_session_t *, const coap_pdu_t *, const coap_string_t *, coap_pdu_t *)
Definition of message handler function.
Definition: resource.h:43
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.
void(* coap_resource_release_userdata_handler_t)(void *user_data)
Definition of release resource user_data callback function.
Definition: resource.h:320
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
void coap_register_request_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.
void coap_resource_set_userdata(coap_resource_t *resource, void *data)
Sets the user_data.
coap_attr_t * coap_add_attr(coap_resource_t *resource, coap_str_const_t *name, coap_str_const_t *value, int flags)
Registers a new attribute with the given resource.
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:449
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:448
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
void * coap_resource_get_userdata(coap_resource_t *resource)
Gets the user_data.
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free'd by coap_delete_resource().
Definition: resource.h:53
coap_resource_t * coap_resource_unknown_init2(coap_method_handler_t put_handler, int flags)
Creates a new resource object for the unknown resource handler with support for PUT and configurable ...
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1242
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:2158
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:45
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:76
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:523
#define LOG_DEBUG
Definition: coap_debug.h:81
#define LOG_ERR
Definition: coap_debug.h:69
#define LOG_WARNING
Definition: coap_debug.h:72
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:165
int coap_resource_notify_observers(coap_resource_t *resource, const coap_string_t *query)
Initiate the sending of an Observe packet for all observers of resource, optionally matching query if...
void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition: pdu.c:333
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition: pdu.c:615
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
Definition: pdu.c:167
#define COAP_OPTION_BLOCK2
Definition: pdu.h:128
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:154
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:243
coap_request_t
CoAP PDU Request methods.
Definition: pdu.h:70
#define COAP_RESPONSE_CODE(N)
Definition: pdu.h:146
#define COAP_RESPONSE_CLASS(C)
Definition: pdu.h:149
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:275
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition: pdu.c:713
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: pdu.h:246
#define COAP_OPTION_ETAG
Definition: pdu.h:113
#define COAP_OPTION_OBSERVE
Definition: pdu.h:115
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:99
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition: pdu.c:682
@ COAP_REQUEST_PUT
Definition: pdu.h:73
@ COAP_MESSAGE_NON
Definition: pdu.h:62
@ COAP_MESSAGE_CON
Definition: pdu.h:61
#define COAP_NSTART(s)
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:361
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:126
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:132
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:58
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:49
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:45
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.
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token, const coap_pdu_t *pdu)
Adds the specified peer as observer for resource.
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.
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
void coap_check_notify(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Handles a failed observe notify.
void coap_subscription_init(coap_subscription_t *)
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.
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Definition: uri.c:561
#define COAP_UNUSED
Definition: libcoap.h:60
#define COAP_STATIC_INLINE
Definition: libcoap.h:45
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:103
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:110
@ COAP_RESOURCE
Definition: mem.h:46
@ COAP_RESOURCEATTR
Definition: mem.h:47
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
COAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
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:56
size_t length
length of binary data
Definition: str.h:57
uint8_t * s
binary data
Definition: str.h:58
Structure of Block options with BERT support.
Definition: block.h:51
unsigned int aszx
block size (0-7 including BERT
Definition: block.h:55
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
coap_tick_t last_all_sent
Last time all data sent or 0.
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
structure for CoAP PDUs
uint8_t * token
first byte of token, if any, or options
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t token_length
length of Token
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_lg_xmit_t * lg_xmit
list of large transmissions
uint8_t con_active
Active CON request sent.
CoAP string data definition with const data.
Definition: str.h:46
const uint8_t * s
read-only string data
Definition: str.h:48
size_t length
length of string
Definition: str.h:47
CoAP string data definition.
Definition: str.h:38
uint8_t * s
string data
Definition: str.h:40
size_t length
length of string
Definition: str.h:39
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
uint8_t dirty
set if the notification temporarily could not be sent (in that case, the resource's partially dirty f...
uint8_t non_cnt
up to 255 non-confirmable notifies allowed
coap_cache_key_t * cache_key
struct coap_session_t * session
subscriber session
uint8_t fail_cnt
up to 255 confirmable notifies can fail
coap_pdu_t * pdu
cache_key to identify requester