libcoap  4.1.2
resource.c
Go to the documentation of this file.
1 /* resource.c -- generic resource handling
2  *
3  * Copyright (C) 2010--2015 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "coap_config.h"
10 #include "coap.h"
11 #include "debug.h"
12 #include "mem.h"
13 #include "net.h"
14 #include "resource.h"
15 #include "subscribe.h"
16 #include "utlist.h"
17 
18 #ifdef WITH_LWIP
19 /* mem.h is only needed for the string free calls for
20  * COAP_ATTR_FLAGS_RELEASE_NAME / COAP_ATTR_FLAGS_RELEASE_VALUE /
21  * COAP_RESOURCE_FLAGS_RELEASE_URI. not sure what those lines should actually
22  * do on lwip. */
23 
24 #include <lwip/memp.h>
25 
26 #define COAP_MALLOC_TYPE(Type) \
27  ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
28 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
29 
30 #endif
31 
32 #ifdef WITH_POSIX
33 
34 #define COAP_MALLOC_TYPE(Type) \
35  ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
36 #define COAP_FREE_TYPE(Type, Object) coap_free(Object)
37 
38 #endif /* WITH_POSIX */
39 #ifdef 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 
53 static inline coap_subscription_t *
54 coap_malloc_subscription() {
55  return memb_alloc(&subscription_storage);
56 }
57 
58 static inline void
59 coap_free_subscription(coap_subscription_t *subscription) {
60  memb_free(&subscription_storage, subscription);
61 }
62 
63 #endif /* WITH_CONTIKI */
64 
65 #define min(a,b) ((a) < (b) ? (a) : (b))
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 str *text, const str *pattern, int match_prefix, int match_substring) {
106  assert(text); assert(pattern);
107 
108  if (text->length < pattern->length)
109  return 0;
110 
111  if (match_substring) {
112  unsigned char *next_token = text->s;
113  size_t remaining_length = text->length;
114  while (remaining_length) {
115  size_t token_length;
116  unsigned char *token = next_token;
117  next_token = memchr(token, ' ', remaining_length);
118 
119  if (next_token) {
120  token_length = next_token - token;
121  remaining_length -= (token_length + 1);
122  next_token++;
123  } else {
124  token_length = remaining_length;
125  remaining_length = 0;
126  }
127 
128  if ((match_prefix || pattern->length == token_length) &&
129  memcmp(token, pattern->s, pattern->length) == 0)
130  return 1;
131  }
132  return 0;
133  }
134 
135  return (match_prefix || pattern->length == text->length) &&
136  memcmp(text->s, pattern->s, pattern->length) == 0;
137 }
138 
160 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
162 coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
163  size_t offset,
164  coap_opt_t *query_filter __attribute__ ((unused))) {
165 #else /* not a GCC */
167 coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
168  size_t offset, coap_opt_t *query_filter) {
169 #endif /* GCC */
170  unsigned char *p = buf;
171  const unsigned char *bufend = buf + *buflen;
172  size_t left, written = 0;
173  coap_print_status_t result;
174  const size_t old_offset = offset;
175  int subsequent_resource = 0;
176 #ifndef WITHOUT_QUERY_FILTER
177  str resource_param = { 0, NULL }, query_pattern = { 0, NULL };
178  int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
179 #define MATCH_URI 0x01
180 #define MATCH_PREFIX 0x02
181 #define MATCH_SUBSTRING 0x04
182  static const str _rt_attributes[] = {
183  {2, (unsigned char *)"rt"},
184  {2, (unsigned char *)"if"},
185  {3, (unsigned char *)"rel"},
186  {0, NULL}};
187 #endif /* WITHOUT_QUERY_FILTER */
188 
189 #ifndef WITHOUT_QUERY_FILTER
190  /* split query filter, if any */
191  if (query_filter) {
192  resource_param.s = COAP_OPT_VALUE(query_filter);
193  while (resource_param.length < COAP_OPT_LENGTH(query_filter)
194  && resource_param.s[resource_param.length] != '=')
195  resource_param.length++;
196 
197  if (resource_param.length < COAP_OPT_LENGTH(query_filter)) {
198  const str *rt_attributes;
199  if (resource_param.length == 4 &&
200  memcmp(resource_param.s, "href", 4) == 0)
201  flags |= MATCH_URI;
202 
203  for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
204  if (resource_param.length == rt_attributes->length &&
205  memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
206  flags |= MATCH_SUBSTRING;
207  break;
208  }
209  }
210 
211  /* rest is query-pattern */
212  query_pattern.s =
213  COAP_OPT_VALUE(query_filter) + resource_param.length + 1;
214 
215  assert((resource_param.length + 1) <= COAP_OPT_LENGTH(query_filter));
216  query_pattern.length =
217  COAP_OPT_LENGTH(query_filter) - (resource_param.length + 1);
218 
219  if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
220  query_pattern.s++;
221  query_pattern.length--;
222  }
223 
224  if (query_pattern.length &&
225  query_pattern.s[query_pattern.length-1] == '*') {
226  query_pattern.length--;
227  flags |= MATCH_PREFIX;
228  }
229  }
230  }
231 #endif /* WITHOUT_QUERY_FILTER */
232 
233  RESOURCES_ITER(context->resources, r) {
234 
235 #ifndef WITHOUT_QUERY_FILTER
236  if (resource_param.length) { /* there is a query filter */
237 
238  if (flags & MATCH_URI) { /* match resource URI */
239  if (!match(&r->uri, &query_pattern, (flags & MATCH_PREFIX) != 0, (flags & MATCH_SUBSTRING) != 0))
240  continue;
241  } else { /* match attribute */
242  coap_attr_t *attr;
243  str unquoted_val;
244  attr = coap_find_attr(r, resource_param.s, resource_param.length);
245  if (!attr) continue;
246  if (attr->value.s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
247  unquoted_val.length = attr->value.length - 2;
248  unquoted_val.s = attr->value.s + 1;
249  } else {
250  unquoted_val = attr->value;
251  }
252  if (!(match(&unquoted_val, &query_pattern,
253  (flags & MATCH_PREFIX) != 0,
254  (flags & MATCH_SUBSTRING) != 0)))
255  continue;
256  }
257  }
258 #endif /* WITHOUT_QUERY_FILTER */
259 
260  if (!subsequent_resource) { /* this is the first resource */
261  subsequent_resource = 1;
262  } else {
263  PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
264  }
265 
266  left = bufend - p; /* calculate available space */
267  result = coap_print_link(r, p, &left, &offset);
268 
269  if (result & COAP_PRINT_STATUS_ERROR) {
270  break;
271  }
272 
273  /* coap_print_link() returns the number of characters that
274  * where actually written to p. Now advance to its end. */
275  p += COAP_PRINT_OUTPUT_LENGTH(result);
276  written += left;
277  }
278 
279  *buflen = written;
280  result = p - buf;
281  if (result + old_offset - offset < *buflen) {
282  result |= COAP_PRINT_STATUS_TRUNC;
283  }
284  return result;
285 }
286 
288 coap_resource_init(const unsigned char *uri, size_t len, int flags) {
289  coap_resource_t *r;
290 
291 #ifdef WITH_LWIP
292  r = (coap_resource_t *)memp_malloc(MEMP_COAP_RESOURCE);
293 #endif
294 #ifndef WITH_LWIP
296 #endif
297  if (r) {
298  memset(r, 0, sizeof(coap_resource_t));
299 
300  r->uri.s = (unsigned char *)uri;
301  r->uri.length = len;
302 
303  coap_hash_path(r->uri.s, r->uri.length, r->key);
304 
305  r->flags = flags;
306  } else {
307  debug("coap_resource_init: no memory left\n");
308  }
309 
310  return r;
311 }
312 
313 coap_attr_t *
315  const unsigned char *name, size_t nlen,
316  const unsigned char *val, size_t vlen,
317  int flags) {
318  coap_attr_t *attr;
319 
320  if (!resource || !name)
321  return NULL;
322 
323 #ifdef WITH_LWIP
324  attr = (coap_attr_t *)memp_malloc(MEMP_COAP_RESOURCEATTR);
325 #endif
326 #ifndef WITH_LWIP
328 #endif
329 
330  if (attr) {
331  attr->name.length = nlen;
332  attr->value.length = val ? vlen : 0;
333 
334  attr->name.s = (unsigned char *)name;
335  attr->value.s = (unsigned char *)val;
336 
337  attr->flags = flags;
338 
339  /* add attribute to resource list */
340  LL_PREPEND(resource->link_attr, attr);
341  } else {
342  debug("coap_add_attr: no memory left\n");
343  }
344 
345  return attr;
346 }
347 
348 coap_attr_t *
350  const unsigned char *name, size_t nlen) {
351  coap_attr_t *attr;
352 
353  if (!resource || !name)
354  return NULL;
355 
356  LL_FOREACH(resource->link_attr, attr) {
357  if (attr->name.length == nlen &&
358  memcmp(attr->name.s, name, nlen) == 0)
359  return attr;
360  }
361 
362  return NULL;
363 }
364 
365 void
367  if (!attr)
368  return;
370  coap_free(attr->name.s);
372  coap_free(attr->value.s);
373 
374 #ifdef WITH_LWIP
375  memp_free(MEMP_COAP_RESOURCEATTR, attr);
376 #endif
377 #ifndef WITH_LWIP
379 #endif
380 }
381 
382 void
384  coap_opt_iterator_t opt_iter;
385  coap_opt_filter_t filter;
386  coap_opt_t *option;
387 
388  memset(key, 0, sizeof(coap_key_t));
389 
390  coap_option_filter_clear(filter);
392 
393  coap_option_iterator_init((coap_pdu_t *)request, &opt_iter, filter);
394  while ((option = coap_option_next(&opt_iter)))
395  coap_hash(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option), key);
396 }
397 
398 void
400  RESOURCES_ADD(context->resources, resource);
401 }
402 
403 static void
405  coap_attr_t *attr, *tmp;
406  coap_subscription_t *obs, *otmp;
407 
408  assert(resource);
409 
410  /* delete registered attributes */
411  LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
412 
413  if (resource->flags & COAP_RESOURCE_FLAGS_RELEASE_URI)
414  coap_free(resource->uri.s);
415 
416  /* free all elements from resource->subscribers */
417  LL_FOREACH_SAFE(resource->subscribers, obs, otmp) COAP_FREE_TYPE(subscription, obs);
418 
419 #ifdef WITH_LWIP
420  memp_free(MEMP_COAP_RESOURCE, resource);
421 #endif
422 #ifndef WITH_LWIP
423  coap_free_type(COAP_RESOURCE, resource);
424 #endif /* WITH_CONTIKI */
425 }
426 
427 int
429  coap_resource_t *resource;
430 
431  if (!context)
432  return 0;
433 
434  resource = coap_get_resource_from_key(context, key);
435 
436  if (!resource)
437  return 0;
438 
439  /* remove resource from list */
440  RESOURCES_DELETE(context->resources, resource);
441 
442  /* and free its allocated memory */
443  coap_free_resource(resource);
444 
445  return 1;
446 }
447 
448 void
450  coap_resource_t *res;
451  coap_resource_t *rtmp;
452 
453  /* Cannot call RESOURCES_ITER because coap_free_resource() releases
454  * the allocated storage. */
455 
456 #ifdef COAP_RESOURCES_NOHASH
457  LL_FOREACH_SAFE(context->resources, res, rtmp) {
458 #else
459  HASH_ITER(hh, context->resources, res, rtmp) {
460  HASH_DELETE(hh, context->resources, res);
461 #endif
462  coap_free_resource(res);
463  }
464 
465  context->resources = NULL;
466 }
467 
470  coap_resource_t *result;
471 
472  RESOURCES_FIND(context->resources, key, result);
473 
474  return result;
475 }
476 
479  unsigned char *buf, size_t *len, size_t *offset) {
480  unsigned char *p = buf;
481  const unsigned char *bufend = buf + *len;
482  coap_attr_t *attr;
483  coap_print_status_t result = 0;
484  const size_t old_offset = *offset;
485 
486  *len = 0;
487  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
488  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
489 
490  COPY_COND_WITH_OFFSET(p, bufend, *offset,
491  resource->uri.s, resource->uri.length, *len);
492 
493  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
494 
495  LL_FOREACH(resource->link_attr, attr) {
496 
497  PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
498 
499  COPY_COND_WITH_OFFSET(p, bufend, *offset,
500  attr->name.s, attr->name.length, *len);
501 
502  if (attr->value.s) {
503  PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
504 
505  COPY_COND_WITH_OFFSET(p, bufend, *offset,
506  attr->value.s, attr->value.length, *len);
507  }
508 
509  }
510  if (resource->observable) {
511  COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
512  }
513 
514  result = p - buf;
515  if (result + old_offset - *offset < *len) {
516  result |= COAP_PRINT_STATUS_TRUNC;
517  }
518 
519  return result;
520 }
521 
522 #ifndef WITHOUT_OBSERVE
525  const str *token) {
527 
528  assert(resource);
529  assert(peer);
530 
531  LL_FOREACH(resource->subscribers, s) {
532  if (coap_address_equals(&s->subscriber, peer)
533  && (!token || (token->length == s->token_length
534  && memcmp(token->s, s->token, token->length) == 0)))
535  return s;
536  }
537 
538  return NULL;
539 }
540 
543  const coap_endpoint_t *local_interface,
544  const coap_address_t *observer,
545  const str *token) {
547 
548  assert(observer);
549 
550  /* Check if there is already a subscription for this peer. */
551  s = coap_find_observer(resource, observer, token);
552 
553  /* We are done if subscription was found. */
554  if (s)
555  return s;
556 
557  /* s points to a different subscription, so we have to create
558  * another one. */
559  s = COAP_MALLOC_TYPE(subscription);
560 
561  if (!s)
562  return NULL;
563 
565  s->local_if = *local_interface;
566  memcpy(&s->subscriber, observer, sizeof(coap_address_t));
567 
568  if (token && token->length) {
569  s->token_length = token->length;
570  memcpy(s->token, token->s, min(s->token_length, 8));
571  }
572 
573  /* add subscriber to resource */
574  LL_PREPEND(resource->subscribers, s);
575 
576  return s;
577 }
578 
579 void
581  const str *token) {
583 
584  RESOURCES_ITER(context->resources, r) {
585  s = coap_find_observer(r, observer, token);
586  if (s) {
587  s->fail_cnt = 0;
588  }
589  }
590 }
591 
592 int
594  const str *token) {
596 
597  s = coap_find_observer(resource, observer, token);
598 
599  if (resource->subscribers && s) {
600  LL_DELETE(resource->subscribers, s);
601 
602  COAP_FREE_TYPE(subscription,s);
603  }
604 
605  return s != NULL;
606 }
607 
608 static void
611  coap_subscription_t *obs;
612  str token;
613  coap_pdu_t *response;
614 
615  if (r->observable && (r->dirty || r->partiallydirty)) {
616  r->partiallydirty = 0;
617 
618  /* retrieve GET handler, prepare response */
619  h = r->handler[COAP_REQUEST_GET - 1];
620  assert(h); /* we do not allow subscriptions if no
621  * GET handler is defined */
622 
623  LL_FOREACH(r->subscribers, obs) {
624  if (r->dirty == 0 && obs->dirty == 0)
625  /* running this resource due to partiallydirty, but this observation's notification was already enqueued */
626  continue;
627 
629  obs->dirty = 0;
630  /* initialize response */
632  if (!response) {
633  obs->dirty = 1;
634  r->partiallydirty = 1;
635  debug("coap_check_notify: pdu init failed, resource stays partially dirty\n");
636  continue;
637  }
638 
639  if (!coap_add_token(response, obs->token_length, obs->token)) {
640  obs->dirty = 1;
641  r->partiallydirty = 1;
642  debug("coap_check_notify: cannot add token, resource stays partially dirty\n");
643  coap_delete_pdu(response);
644  continue;
645  }
646 
647  token.length = obs->token_length;
648  token.s = obs->token;
649 
650  response->hdr->id = coap_new_message_id(context);
651  if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0
652  && obs->non_cnt < COAP_OBS_MAX_NON) {
653  response->hdr->type = COAP_MESSAGE_NON;
654  } else {
655  response->hdr->type = COAP_MESSAGE_CON;
656  }
657  /* fill with observer-specific data */
658  h(context, r, &obs->local_if, &obs->subscriber, NULL, &token, response);
659 
660  /* TODO: do not send response and remove observer when
661  * COAP_RESPONSE_CLASS(response->hdr->code) > 2
662  */
663  if (response->hdr->type == COAP_MESSAGE_CON) {
664  tid = coap_send_confirmed(context, &obs->local_if, &obs->subscriber, response);
665  obs->non_cnt = 0;
666  } else {
667  tid = coap_send(context, &obs->local_if, &obs->subscriber, response);
668  obs->non_cnt++;
669  }
670 
671  if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON)
672  coap_delete_pdu(response);
673  if (COAP_INVALID_TID == tid)
674  {
675  debug("coap_check_notify: sending failed, resource stays partially dirty\n");
676  obs->dirty = 1;
677  r->partiallydirty = 1;
678  }
679 
680  }
681 
682  /* Increment value for next Observe use. */
683  context->observe++;
684  }
685  r->dirty = 0;
686 }
687 
688 void
690 
691  RESOURCES_ITER(context->resources, r) {
692  coap_notify_observers(context, r);
693  }
694 }
695 
706 static void
708  coap_resource_t *resource,
709  const coap_address_t *peer,
710  const str *token) {
711  coap_subscription_t *obs, *otmp;
712 
713  LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
714  if (coap_address_equals(peer, &obs->subscriber) &&
715  token->length == obs->token_length &&
716  memcmp(token->s, obs->token, token->length) == 0) {
717 
718  /* count failed notifies and remove when
719  * COAP_MAX_FAILED_NOTIFY is reached */
720  if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
721  obs->fail_cnt++;
722  else {
723  LL_DELETE(resource->subscribers, obs);
724  obs->fail_cnt = 0;
725 
726 #ifndef NDEBUG
727  if (LOG_DEBUG <= coap_get_log_level()) {
728 #ifndef INET6_ADDRSTRLEN
729 #define INET6_ADDRSTRLEN 40
730 #endif
731  unsigned char addr[INET6_ADDRSTRLEN+8];
732 
733  if (coap_print_addr(&obs->subscriber, addr, INET6_ADDRSTRLEN+8))
734  debug("** removed observer %s\n", addr);
735  }
736 #endif
737  coap_cancel_all_messages(context, &obs->subscriber,
738  obs->token, obs->token_length);
739 
740  COAP_FREE_TYPE(subscription, obs);
741  }
742  }
743  break; /* break loop if observer was found */
744  }
745 }
746 
747 void
749  const coap_address_t *peer,
750  const str *token) {
751 
752  RESOURCES_ITER(context->resources, r) {
753  coap_remove_failed_observers(context, r, peer, token);
754  }
755 }
756 #endif /* WITHOUT_NOTIFY */
#define LL_FOREACH(head, el)
Definition: utlist.h:428
unsigned int observe
The next value to be used for Observe.
Definition: net.h:120
coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a confirmed CoAP message to given destination.
Definition: net.c:699
#define RESOURCES_DELETE(r, obj)
Definition: resource.h:384
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:689
unsigned char coap_key_t[4]
Definition: hashkey.h:20
int flags
Definition: resource.h:57
int coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
Removes any subscription for observer from resource and releases the allocated storage.
Definition: resource.c:593
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
Definition: resource.c:428
#define INET6_ADDRSTRLEN
multi-purpose address abstraction
Definition: address.h:59
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:220
unsigned short id
Definition: pdu.h:191
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
#define LL_PREPEND(head, add)
Definition: utlist.h:309
#define COAP_REQUEST_GET
Definition: pdu.h:50
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:886
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
Definition: pdu.c:153
#define COAP_MESSAGE_NON
Definition: pdu.h:44
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:98
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition: resource.h:50
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:77
size_t length
Definition: str.h:16
static void coap_remove_failed_observers(coap_context_t *context, coap_resource_t *resource, const coap_address_t *peer, const str *token)
Checks the failure counter for (peer, token) and removes peer from the list of observers for the give...
Definition: resource.c:707
#define MATCH_URI
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:159
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:199
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
Definition: pdu.h:229
static int match(const str *text, const str *pattern, int match_prefix, int match_substring)
Definition: resource.c:105
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
Definition: subscribe.h:50
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:35
void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key)
Calculates the hash key for the resource requested by the Uri-Options of request. ...
Definition: resource.c:383
#define MATCH_PREFIX
#define MATCH_SUBSTRING
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Definition: debug.c:162
#define debug(...)
Definition: debug.h:66
str name
Definition: resource.h:55
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:224
#define COAP_INVALID_TID
Indicates an invalid transaction id.
Definition: pdu.h:166
coap_attr_t * coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen, const unsigned char *val, size_t vlen, int flags)
Registers a new attribute with the given resource.
Definition: resource.c:314
size_t token_length
actual length of token
Definition: subscribe.h:64
void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst, const unsigned char *token, size_t token_length)
Cancels all outstanding messages for peer dst that have the specified token.
Definition: net.c:980
str uri
Request URI for this resource.
Definition: resource.h:105
#define RESOURCES_ITER(r, tmp)
Definition: resource.h:387
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu&#39;s option list...
Definition: option.c:120
coap_key_t key
the actual key bytes for this resource
Definition: resource.h:90
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:76
#define COAP_MALLOC_TYPE(Type)
Definition: resource.c:34
coap_tid_t coap_send(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:613
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:51
unsigned int observable
can be observed
Definition: resource.h:79
#define assert(...)
Definition: mem.c:17
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:399
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
Definition: subscribe.h:59
Iterator to run through PDU options.
Definition: option.h:253
#define COAP_MESSAGE_CON
Definition: pdu.h:43
str value
Definition: resource.h:56
#define RESOURCES_ADD(r, obj)
Definition: resource.h:381
Generic resource handling.
#define COAP_MAX_PDU_SIZE
Definition: pdu.h:27
coap_subscription_t * coap_add_observer(coap_resource_t *resource, const coap_endpoint_t *local_interface, const coap_address_t *observer, const str *token)
Adds the specified peer as observer for resource.
Definition: resource.c:542
#define coap_hash(String, Length, Result)
Definition: hashkey.h:34
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
Definition: resource.c:609
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:99
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:135
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:18
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:73
coap_subscription_t * coap_find_observer(coap_resource_t *resource, const coap_address_t *peer, const str *token)
Returns a subscription object for given peer.
Definition: resource.c:524
Subscriber information.
Definition: subscribe.h:54
void coap_delete_pdu(coap_pdu_t *pdu)
Definition: pdu.c:136
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
Definition: pdu.c:75
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:61
#define RESOURCES_FIND(r, k, res)
Definition: resource.h:391
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:146
int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key)
Calculates a hash over the given path and stores the result in key.
Definition: uri.c:483
Definition: str.h:15
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:434
#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
#define COAP_OPT_LENGTH(opt)
Definition: option.h:392
static unsigned short coap_new_message_id(coap_context_t *context)
Returns a new message id and updates context->message_id accordingly.
Definition: net.h:189
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:449
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:478
coap_attr_t * coap_find_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen)
Returns resource&#39;s coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:349
coap_resource_t * coap_resource_init(const unsigned char *uri, size_t len, int flags)
Creates a new resource object and initializes the link field to the string of length len...
Definition: resource.c:288
#define COAP_OPTION_URI_PATH
Definition: pdu.h:63
void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token)
Marks an observer as alive.
Definition: resource.c:580
static void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:77
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
#define LL_DELETE(head, del)
Definition: utlist.h:349
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:221
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:217
unsigned int type
Definition: pdu.h:187
unsigned char 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
#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:60
#define COAP_FREE_TYPE(Type, Object)
Definition: resource.c:36
#define min(a, b)
Definition: resource.c:65
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:167
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:65
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:366
unsigned char * s
Definition: str.h:17
coap_endpoint_t local_if
local communication interface
Definition: subscribe.h:56
#define COAP_PRINT_STATUS_TRUNC
Definition: resource.h:222
void coap_subscription_init(coap_subscription_t *s)
Definition: subscribe.c:20
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free&#39;d by coap_delete_resource().
Definition: resource.h:61
coap_log_t coap_get_log_level(void)
Returns the current log level.
Definition: debug.c:60
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, const coap_endpoint_t *, coap_address_t *, coap_pdu_t *, str *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:42
void coap_handle_failed_notify(coap_context_t *context, const coap_address_t *peer, const str *token)
Definition: resource.c:748
coap_address_t subscriber
address and port of subscriber
Definition: subscribe.h:57
#define COAP_OPT_VALUE(opt)
Definition: option.h:406
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76
static void coap_free_resource(coap_resource_t *resource)
Definition: resource.c:404
#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:41
coap_resource_t * coap_get_resource_from_key(coap_context_t *context, coap_key_t key)
Returns the resource identified by the unique string key.
Definition: resource.c:469
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:78
coap_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
Definition: resource.h:88