libcoap  4.2.0
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 <stdio.h>
10 #include "coap_config.h"
11 #include "coap.h"
12 #include "coap_debug.h"
13 #include "mem.h"
14 #include "net.h"
15 #include "resource.h"
16 #include "subscribe.h"
17 #include "utlist.h"
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 
82 
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 __attribute__ ((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 
298 
300 coap_resource_init(coap_str_const_t *uri_path, int flags) {
301  coap_resource_t *r;
302 
304  if (r) {
305  memset(r, 0, sizeof(coap_resource_t));
306 
307  if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) {
308  /* Need to take a copy if caller is not providing a release request */
309  if (uri_path)
310  uri_path = coap_new_str_const(uri_path->s, uri_path->length);
311  else
312  uri_path = coap_new_str_const(null_path->s, null_path->length);
313  }
314  else if (!uri_path) {
315  /* Do not expecte this, but ... */
316  uri_path = coap_new_str_const(null_path->s, null_path->length);
317  }
318 
319  if (uri_path)
320  r->uri_path = uri_path;
321 
322  r->flags = flags;
323  } else {
324  coap_log(LOG_DEBUG, "coap_resource_init: no memory left\n");
325  }
326 
327  return r;
328 }
329 
331  "- Unknown -";
332 
335  coap_resource_t *r;
336 
338  if (r) {
339  memset(r, 0, sizeof(coap_resource_t));
340  r->is_unknown = 1;
341  /* Something unlikely to be used, but it shows up in the logs */
342  r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1);
343  coap_register_handler(r, COAP_REQUEST_PUT, put_handler);
344  } else {
345  coap_log(LOG_DEBUG, "coap_resource_unknown_init: no memory left\n");
346  }
347 
348  return r;
349 }
350 
351 coap_attr_t *
353  coap_str_const_t *name,
354  coap_str_const_t *val,
355  int flags) {
356  coap_attr_t *attr;
357 
358  if (!resource || !name)
359  return NULL;
360 
362 
363  if (attr) {
364  if (!(flags & COAP_ATTR_FLAGS_RELEASE_NAME)) {
365  /* Need to take a copy if caller is not providing a release request */
366  name = coap_new_str_const(name->s, name->length);
367  }
368  attr->name = name;
369  if (val) {
370  if (!(flags & COAP_ATTR_FLAGS_RELEASE_VALUE)) {
371  /* Need to take a copy if caller is not providing a release request */
372  val = coap_new_str_const(val->s, val->length);
373  }
374  }
375  attr->value = val;
376 
377  attr->flags = flags;
378 
379  /* add attribute to resource list */
380  LL_PREPEND(resource->link_attr, attr);
381  } else {
382  coap_log(LOG_DEBUG, "coap_add_attr: no memory left\n");
383  }
384 
385  return attr;
386 }
387 
388 coap_attr_t *
390  coap_str_const_t *name) {
391  coap_attr_t *attr;
392 
393  if (!resource || !name)
394  return NULL;
395 
396  LL_FOREACH(resource->link_attr, attr) {
397  if (attr->name->length == name->length &&
398  memcmp(attr->name->s, name->s, name->length) == 0)
399  return attr;
400  }
401 
402  return NULL;
403 }
404 
405 void
407  if (!attr)
408  return;
410  if (attr->value) {
412  }
413 
414 #ifdef WITH_LWIP
415  memp_free(MEMP_COAP_RESOURCEATTR, attr);
416 #endif
417 #ifndef WITH_LWIP
419 #endif
420 }
421 
422 static void
424  coap_attr_t *attr, *tmp;
425  coap_subscription_t *obs, *otmp;
426 
427  assert(resource);
428 
429  /* delete registered attributes */
430  LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
431 
432  /* Either the application provided or libcoap copied - need to delete it */
433  coap_delete_str_const(resource->uri_path);
434 
435  /* free all elements from resource->subscribers */
436  LL_FOREACH_SAFE( resource->subscribers, obs, otmp ) {
438  if (obs->query)
440  COAP_FREE_TYPE( subscription, obs );
441  }
442 
443 #ifdef WITH_LWIP
444  memp_free(MEMP_COAP_RESOURCE, resource);
445 #endif
446 #ifndef WITH_LWIP
447  coap_free_type(COAP_RESOURCE, resource);
448 #endif /* WITH_CONTIKI */
449 }
450 
451 void
453  if (resource->is_unknown) {
454  if (context->unknown_resource)
456  context->unknown_resource = resource;
457  }
458  else {
460  resource->uri_path);
461 
462  if (r) {
464  "coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
465  (int)resource->uri_path->length, (int)resource->uri_path->length,
466  resource->uri_path->s);
467  coap_delete_resource(context, r);
468  }
469  RESOURCES_ADD(context->resources, resource);
470  }
471 }
472 
473 int
475  if (!context || !resource)
476  return 0;
477 
478  if (resource->is_unknown && (context->unknown_resource == resource)) {
480  context->unknown_resource = NULL;
481  return 1;
482  }
483 
484  /* remove resource from list */
485  RESOURCES_DELETE(context->resources, resource);
486 
487  /* and free its allocated memory */
488  coap_free_resource(resource);
489 
490  return 1;
491 }
492 
493 void
495  coap_resource_t *res;
496  coap_resource_t *rtmp;
497 
498  /* Cannot call RESOURCES_ITER because coap_free_resource() releases
499  * the allocated storage. */
500 
501  HASH_ITER(hh, context->resources, res, rtmp) {
502  HASH_DELETE(hh, context->resources, res);
503  coap_free_resource(res);
504  }
505 
506  context->resources = NULL;
507 
508  if (context->unknown_resource) {
510  context->unknown_resource = NULL;
511  }
512 }
513 
516  coap_resource_t *result;
517 
518  RESOURCES_FIND(context->resources, uri_path, result);
519 
520  return result;
521 }
522 
525  unsigned char *buf, size_t *len, size_t *offset) {
526  unsigned char *p = buf;
527  const uint8_t *bufend = buf + *len;
528  coap_attr_t *attr;
529  coap_print_status_t result = 0;
530  size_t output_length = 0;
531  const size_t old_offset = *offset;
532 
533  *len = 0;
534  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
535  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
536 
537  COPY_COND_WITH_OFFSET(p, bufend, *offset,
538  resource->uri_path->s, resource->uri_path->length, *len);
539 
540  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
541 
542  LL_FOREACH(resource->link_attr, attr) {
543 
544  PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
545 
546  COPY_COND_WITH_OFFSET(p, bufend, *offset,
547  attr->name->s, attr->name->length, *len);
548 
549  if (attr->value && attr->value->s) {
550  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
551 
552  COPY_COND_WITH_OFFSET(p, bufend, *offset,
553  attr->value->s, attr->value->length, *len);
554  }
555 
556  }
557  if (resource->observable) {
558  COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
559  }
560 
561  output_length = p - buf;
562 
563  if (output_length > COAP_PRINT_STATUS_MAX) {
565  }
566 
567  result = (coap_print_status_t)output_length;
568 
569  if (result + old_offset - *offset < *len) {
570  result |= COAP_PRINT_STATUS_TRUNC;
571  }
572 
573  return result;
574 }
575 
576 void
578  unsigned char method,
579  coap_method_handler_t handler) {
580  assert(resource);
581  assert(method > 0 && (size_t)(method-1) < sizeof(resource->handler)/sizeof(coap_method_handler_t));
582  resource->handler[method-1] = handler;
583 }
584 
585 #ifndef WITHOUT_OBSERVE
588  const coap_binary_t *token) {
590 
591  assert(resource);
592  assert(session);
593 
594  LL_FOREACH(resource->subscribers, s) {
595  if (s->session == session
596  && (!token || (token->length == s->token_length
597  && memcmp(token->s, s->token, token->length) == 0)))
598  return s;
599  }
600 
601  return NULL;
602 }
603 
604 static coap_subscription_t *
606  const coap_string_t *query) {
608 
609  assert(resource);
610  assert(session);
611 
612  LL_FOREACH(resource->subscribers, s) {
613  if (s->session == session
614  && ((!query && !s->query)
615  || (query && s->query && coap_string_equal(query, s->query))))
616  return s;
617  }
618 
619  return NULL;
620 }
621 
624  coap_session_t *session,
625  const coap_binary_t *token,
626  coap_string_t *query,
627  int has_block2,
628  coap_block_t block2) {
630 
631  assert( session );
632 
633  /* Check if there is already a subscription for this peer. */
634  s = coap_find_observer(resource, session, token);
635  if (!s) {
636  /*
637  * Cannot allow a duplicate to be created for the same query as application
638  * may not be cleaning up duplicates. If duplicate found, then original
639  * observer is deleted and a new one created with the new token
640  */
641  s = coap_find_observer_query(resource, session, query);
642  if (s) {
643  /* Delete old entry with old token */
644  coap_binary_t tmp_token = { s->token_length, s->token };
645  coap_delete_observer(resource, session, &tmp_token);
646  s = NULL;
647  }
648  }
649 
650  /* We are done if subscription was found. */
651  if (s) {
652  if (s->query)
654  s->query = query;
655  return s;
656  }
657 
658  /* s points to a different subscription, so we have to create
659  * another one. */
660  s = COAP_MALLOC_TYPE(subscription);
661 
662  if (!s) {
663  if (query)
664  coap_delete_string(query);
665  return NULL;
666  }
667 
669  s->session = coap_session_reference( session );
670 
671  if (token && token->length) {
672  s->token_length = token->length;
673  memcpy(s->token, token->s, min(s->token_length, 8));
674  }
675 
676  s->query = query;
677 
678  s->has_block2 = has_block2;
679  s->block2 = block2;
680 
681  /* add subscriber to resource */
682  LL_PREPEND(resource->subscribers, s);
683 
684  coap_log(LOG_DEBUG, "create new subscription\n");
685 
686  return s;
687 }
688 
689 void
691  const coap_binary_t *token) {
693 
694  RESOURCES_ITER(context->resources, r) {
695  s = coap_find_observer(r, session, token);
696  if (s) {
697  s->fail_cnt = 0;
698  }
699  }
700 }
701 
702 int
704  const coap_binary_t *token) {
706 
707  s = coap_find_observer(resource, session, token);
708 
709  if ( s && coap_get_log_level() >= LOG_DEBUG ) {
710  char outbuf[2 * 8 + 1] = "";
711  unsigned int i;
712  for ( i = 0; i < s->token_length; i++ )
713  snprintf( &outbuf[2 * i], 3, "%02x", s->token[i] );
714  coap_log(LOG_DEBUG, "removed observer tid %s\n", outbuf);
715  }
716 
717  if (resource->subscribers && s) {
718  LL_DELETE(resource->subscribers, s);
719  coap_session_release( session );
720  if (s->query)
722  COAP_FREE_TYPE(subscription,s);
723  }
724 
725  return s != NULL;
726 }
727 
728 void
730  RESOURCES_ITER(context->resources, resource) {
731  coap_subscription_t *s, *tmp;
732  LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
733  if (s->session == session) {
734  LL_DELETE(resource->subscribers, s);
735  coap_session_release(session);
736  if (s->query)
738  COAP_FREE_TYPE(subscription, s);
739  }
740  }
741  }
742 }
743 
744 static void
747  coap_subscription_t *obs;
748  coap_binary_t token;
749  coap_pdu_t *response;
750 
751  if (r->observable && (r->dirty || r->partiallydirty)) {
752  r->partiallydirty = 0;
753 
754  /* retrieve GET handler, prepare response */
755  h = r->handler[COAP_REQUEST_GET - 1];
756  assert(h); /* we do not allow subscriptions if no
757  * GET handler is defined */
758 
759  LL_FOREACH(r->subscribers, obs) {
760  if (r->dirty == 0 && obs->dirty == 0)
761  /*
762  * running this resource due to partiallydirty, but this observation's
763  * notification was already enqueued
764  */
765  continue;
766  if (obs->session->con_active >= COAP_DEFAULT_NSTART &&
768  (obs->non_cnt >= COAP_OBS_MAX_NON)))
769  continue;
770 
772  obs->dirty = 0;
773  /* initialize response */
775  if (!response) {
776  obs->dirty = 1;
777  r->partiallydirty = 1;
779  "coap_check_notify: pdu init failed, resource stays "
780  "partially dirty\n");
781  continue;
782  }
783 
784  if (!coap_add_token(response, obs->token_length, obs->token)) {
785  obs->dirty = 1;
786  r->partiallydirty = 1;
788  "coap_check_notify: cannot add token, resource stays "
789  "partially dirty\n");
790  coap_delete_pdu(response);
791  continue;
792  }
793 
794  token.length = obs->token_length;
795  token.s = obs->token;
796 
797  response->tid = coap_new_message_id(obs->session);
798  if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0
799  && obs->non_cnt < COAP_OBS_MAX_NON) {
800  response->type = COAP_MESSAGE_NON;
801  } else {
802  response->type = COAP_MESSAGE_CON;
803  }
804  /* fill with observer-specific data */
805  h(context, r, obs->session, NULL, &token, obs->query, response);
806 
807  /* TODO: do not send response and remove observer when
808  * COAP_RESPONSE_CLASS(response->hdr->code) > 2
809  */
810  if (response->type == COAP_MESSAGE_CON) {
811  obs->non_cnt = 0;
812  } else {
813  obs->non_cnt++;
814  }
815 
816  tid = coap_send( obs->session, response );
817 
818  if (COAP_INVALID_TID == tid) {
820  "coap_check_notify: sending failed, resource stays "
821  "partially dirty\n");
822  obs->dirty = 1;
823  r->partiallydirty = 1;
824  }
825 
826  }
827  }
828  r->dirty = 0;
829 }
830 
831 int
833  return coap_resource_notify_observers(r, query);
834 }
835 
836 int
838  if (!r->observable)
839  return 0;
840  if (query) {
841  coap_subscription_t *obs;
842  int found = 0;
843  LL_FOREACH(r->subscribers, obs) {
844  if (obs->query
845  && obs->query->length==query->length
846  && memcmp(obs->query->s, query->s, query->length)==0 ) {
847  found = 1;
848  if (!r->dirty && !obs->dirty) {
849  obs->dirty = 1;
850  r->partiallydirty = 1;
851  }
852  }
853  }
854  if (!found)
855  return 0;
856  } else {
857  if ( !r->subscribers )
858  return 0;
859  r->dirty = 1;
860  }
861 
862  /* Increment value for next Observe use. Observe value must be < 2^24 */
863  r->observe = (r->observe + 1) & 0xFFFFFF;
864 
865  return 1;
866 }
867 
868 void
870 
871  RESOURCES_ITER(context->resources, r) {
872  coap_notify_observers(context, r);
873  }
874 }
875 
886 static void
888  coap_resource_t *resource,
889  coap_session_t *session,
890  const coap_binary_t *token) {
891  coap_subscription_t *obs, *otmp;
892 
893  LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
894  if ( obs->session == session &&
895  token->length == obs->token_length &&
896  memcmp(token->s, obs->token, token->length) == 0) {
897 
898  /* count failed notifies and remove when
899  * COAP_MAX_FAILED_NOTIFY is reached */
900  if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
901  obs->fail_cnt++;
902  else {
903  LL_DELETE(resource->subscribers, obs);
904  obs->fail_cnt = 0;
905 
906 #ifndef NDEBUG
907  if (LOG_DEBUG <= coap_get_log_level()) {
908 #ifndef INET6_ADDRSTRLEN
909 #define INET6_ADDRSTRLEN 40
910 #endif
911  unsigned char addr[INET6_ADDRSTRLEN+8];
912 
914  coap_log(LOG_DEBUG, "** removed observer %s\n", addr);
915  }
916 #endif
917  coap_cancel_all_messages(context, obs->session,
918  obs->token, obs->token_length);
920  if (obs->query)
922  COAP_FREE_TYPE(subscription, obs);
923  }
924  break; /* break loop if observer was found */
925  }
926  }
927 }
928 
929 void
931  coap_session_t *session,
932  const coap_binary_t *token) {
933 
934  RESOURCES_ITER(context->resources, r) {
935  coap_remove_failed_observers(context, r, session, token);
936  }
937 }
938 #endif /* WITHOUT_NOTIFY */
uint8_t type
message type
Definition: pdu.h:288
#define LL_FOREACH(head, el)
Definition: utlist.h:413
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:73
#define RESOURCES_DELETE(r, obj)
Definition: resource.h:452
static coap_subscription_t * coap_find_observer_query(coap_resource_t *resource, coap_session_t *session, const coap_string_t *query)
Definition: resource.c:605
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:869
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:389
int flags
Definition: resource.h:51
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:334
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:352
#define INET6_ADDRSTRLEN
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:314
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:690
#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:171
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:876
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:88
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:729
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:837
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition: resource.h:44
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:71
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:703
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:189
unsigned int is_unknown
resource created for unknown handler
Definition: resource.h:75
Debug.
Definition: coap_debug.h:49
#define MATCH_URI
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:71
size_t length
length of string
Definition: str.h:34
uint8_t * s
string data
Definition: str.h:27
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
Definition: subscribe.h:52
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:379
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:205
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:587
#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:832
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:115
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:887
coap_str_const_t * name
Definition: resource.h:49
#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
Definition: subscribe.h:67
coap_address_t remote_addr
remote address and port
Definition: coap_session.h:65
#define RESOURCES_ITER(r, tmp)
Definition: resource.h:455
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:249
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:70
#define COAP_MALLOC_TYPE(Type)
Definition: resource.c:56
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:45
unsigned int observable
can be observed
Definition: resource.h:73
Warning.
Definition: coap_debug.h:46
#define assert(...)
Definition: mem.c:18
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:452
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
Definition: subscribe.h:60
#define COAP_PRINT_STATUS_MAX
Definition: resource.c:61
#define COAP_MESSAGE_CON
Definition: pdu.h:72
#define RESOURCES_ADD(r, obj)
Definition: resource.h:449
Coap binary data definition.
Definition: str.h:43
Generic resource handling.
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:34
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server...
Definition: coap_session.h:408
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:47
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
Definition: resource.c:745
coap_block_t block2
GET request Block2 definition.
Definition: subscribe.h:66
#define coap_make_str_const(string)
Take the specified byte array (text) and create a coap_str_const_t *.
Definition: str.h:102
coap_str_const_t * value
Definition: resource.h:50
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:77
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:89
static coap_str_const_t * null_path
Definition: resource.c:297
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:1484
#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:67
Subscriber information.
Definition: subscribe.h:56
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:141
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...
Definition: subscribe.h:62
#define RESOURCES_FIND(r, k, res)
Definition: resource.h:459
coap_session_t * session
subscriber session
Definition: subscribe.h:58
unsigned int has_block2
GET request had Block2 definition.
Definition: subscribe.h:65
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
Definition: net.h:152
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:286
#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:85
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:515
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:494
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:623
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:524
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_string_t * query
query string used for subscription, if any
Definition: subscribe.h:69
#define LL_DELETE(head, del)
Definition: utlist.h:385
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:315
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:71
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:311
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
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:577
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
Definition: resource.c:97
unsigned int fail_cnt
up to 3 confirmable notifies can fail
Definition: subscribe.h:61
#define COAP_FREE_TYPE(Type, Object)
Definition: resource.c:58
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:122
#define min(a, b)
Definition: resource.c:64
#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:169
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:38
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:91
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:300
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Definition: resource.c:930
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
Definition: subscribe.h:68
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:406
coap_str_const_t * uri_path
Request URI Path for this resource.
Definition: resource.h:96
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:316
void coap_subscription_init(coap_subscription_t *s)
Definition: subscribe.c:20
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:36
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free&#39;d by coap_delete_resource().
Definition: resource.h:55
unsigned int observe
The next value for the Observe option.
Definition: resource.h:103
uint8_t * s
binary data
Definition: str.h:45
static const uint8_t coap_unknown_resource_uri[]
Definition: resource.c:330
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:84
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
Definition: resource.c:474
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:148
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:423
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
Definition: subscribe.h:43
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:150