1 /* resource.c -- generic resource handling
3 * Copyright (C) 2010--2014 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
13 #include "subscribe.h"
17 /* mem.h is only needed for the string free calls for
18 * COAP_ATTR_FLAGS_RELEASE_NAME / COAP_ATTR_FLAGS_RELEASE_VALUE /
19 * COAP_RESOURCE_FLAGS_RELEASE_URI. not sure what those lines should actually
23 #include <lwip/memp.h>
25 #define COAP_MALLOC_TYPE(Type) \
26 ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
27 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
30 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
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)
38 #endif /* WITH_POSIX || WITH_ARDUINO */
42 MEMB(resource_storage, coap_resource_t, COAP_MAX_RESOURCES);
43 MEMB(attribute_storage, coap_attr_t, COAP_MAX_ATTRIBUTES);
44 MEMB(subscription_storage, coap_subscription_t, COAP_MAX_SUBSCRIBERS);
47 coap_resources_init() {
48 memb_init(&resource_storage);
49 memb_init(&attribute_storage);
50 memb_init(&subscription_storage);
53 static inline coap_subscription_t *
54 coap_malloc_subscription() {
55 return memb_alloc(&subscription_storage);
59 coap_free_subscription(coap_subscription_t *subscription) {
60 memb_free(&subscription_storage, subscription);
62 #endif /* WITH_CONTIKI */
64 #define min(a,b) ((a) < (b) ? (a) : (b))
66 /* Helper functions for conditional output of character sequences into
67 * a given buffer. The first Offset characters are skipped.
71 * Adds Char to Buf if Offset is zero. Otherwise, Char is not written
72 * and Offset is decremented.
74 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \
75 if ((Offset) == 0) { \
76 (*(Buf)++) = (Char); \
82 * Adds Char to Buf if Offset is zero and Buf is less than Bufend.
84 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
85 if ((Buf) < (Bufend)) { \
86 PRINT_WITH_OFFSET(Buf,Offset,Char); \
92 * Copies at most Length characters of Str to Buf. The first Offset
93 * characters are skipped. Output may be truncated to Bufend - Buf
96 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
98 for (i = 0; i < (Length); i++) { \
99 PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
104 match(const str *text, const str *pattern, int match_prefix, int match_substring) {
105 assert(text); assert(pattern);
107 if (text->length < pattern->length)
110 if (match_substring) {
111 unsigned char *next_token = text->s;
112 size_t remaining_length = text->length;
113 while (remaining_length) {
115 unsigned char *token = next_token;
116 next_token = (unsigned char *)memchr(token, ' ', remaining_length);
119 token_length = next_token - token;
120 remaining_length -= (token_length + 1);
123 token_length = remaining_length;
124 remaining_length = 0;
127 if ((match_prefix || pattern->length == token_length) &&
128 memcmp(token, pattern->s, pattern->length) == 0)
134 return (match_prefix || pattern->length == text->length) &&
135 memcmp(text->s, pattern->s, pattern->length) == 0;
139 * Prints the names of all known resources to @p buf. This function
140 * sets @p buflen to the number of bytes actually written and returns
141 * @c 1 on succes. On error, the value in @p buflen is undefined and
142 * the return value will be @c 0.
144 * @param context The context with the resource map.
145 * @param buf The buffer to write the result.
146 * @param buflen Must be initialized to the maximum length of @p buf and will be
147 * set to the length of the well-known response on return.
148 * @param offset The offset in bytes where the output shall start and is
149 * shifted accordingly with the characters that have been
150 * processed. This parameter is used to support the block
152 * @param query_filter A filter query according to <a href="http://tools.ietf.org/html/draft-ietf-core-link-format-11#section-4.1">Link Format</a>
154 * @return COAP_PRINT_STATUS_ERROR on error. Otherwise, the lower 28 bits are
155 * set to the number of bytes that have actually been written to
156 * @p buf. COAP_PRINT_STATUS_TRUNC is set when the output has been
159 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
161 print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
163 coap_opt_t *query_filter __attribute__ ((unused))) {
164 #else /* not a GCC */
166 print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
167 size_t offset, coap_opt_t *query_filter) {
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 COAP_RESOURCES_NOHASH
177 coap_resource_t *tmp;
179 #ifndef WITHOUT_QUERY_FILTER
180 str 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 str _rt_attributes[] = {
186 {2, (unsigned char *)"rt"},
187 {2, (unsigned char *)"if"},
188 {3, (unsigned char *)"rel"},
190 #endif /* WITHOUT_QUERY_FILTER */
194 #endif /* WITH_CONTIKI */
196 #ifndef WITHOUT_QUERY_FILTER
197 /* split query filter, if any */
199 resource_param.s = COAP_OPT_VALUE(query_filter);
200 while (resource_param.length < COAP_OPT_LENGTH(query_filter)
201 && resource_param.s[resource_param.length] != '=')
202 resource_param.length++;
204 if (resource_param.length < COAP_OPT_LENGTH(query_filter)) {
205 const str *rt_attributes;
206 if (resource_param.length == 4 &&
207 memcmp(resource_param.s, "href", 4) == 0)
210 for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
211 if (resource_param.length == rt_attributes->length &&
212 memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
213 flags |= MATCH_SUBSTRING;
218 /* rest is query-pattern */
220 COAP_OPT_VALUE(query_filter) + resource_param.length + 1;
222 assert((resource_param.length + 1) <= COAP_OPT_LENGTH(query_filter));
223 query_pattern.length =
224 COAP_OPT_LENGTH(query_filter) - (resource_param.length + 1);
226 if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
228 query_pattern.length--;
231 if (query_pattern.length &&
232 query_pattern.s[query_pattern.length-1] == '*') {
233 query_pattern.length--;
234 flags |= MATCH_PREFIX;
238 #endif /* WITHOUT_QUERY_FILTER */
242 #ifdef COAP_RESOURCES_NOHASH
243 LL_FOREACH(context->resources, r) {
245 HASH_ITER(hh, context->resources, r, tmp) {
247 #else /* WITH_CONTIKI */
248 r = (coap_resource_t *)resource_storage.mem;
249 for (i = 0; i < resource_storage.num; ++i, ++r) {
250 if (!resource_storage.count[i])
252 #endif /* WITH_CONTIKI */
254 #ifndef WITHOUT_QUERY_FILTER
255 if (resource_param.length) { /* there is a query filter */
257 if (flags & MATCH_URI) { /* match resource URI */
258 if (!match(&r->uri, &query_pattern, (flags & MATCH_PREFIX) != 0, (flags & MATCH_SUBSTRING) != 0))
260 } else { /* match attribute */
263 attr = coap_find_attr(r, resource_param.s, resource_param.length);
265 if (attr->value.s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
266 unquoted_val.length = attr->value.length - 2;
267 unquoted_val.s = attr->value.s + 1;
269 unquoted_val = attr->value;
271 if (!(match(&unquoted_val, &query_pattern,
272 (flags & MATCH_PREFIX) != 0,
273 (flags & MATCH_SUBSTRING) != 0)))
277 #endif /* WITHOUT_QUERY_FILTER */
279 if (!subsequent_resource) { /* this is the first resource */
280 subsequent_resource = 1;
282 PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
285 left = bufend - p; /* calculate available space */
286 result = coap_print_link(r, p, &left, &offset);
288 if (result & COAP_PRINT_STATUS_ERROR) {
292 /* coap_print_link() returns the number of characters that
293 * where actually written to p. Now advance to its end. */
294 p += COAP_PRINT_OUTPUT_LENGTH(result);
300 if (result + old_offset - offset < *buflen) {
301 result |= COAP_PRINT_STATUS_TRUNC;
307 coap_resource_init(const unsigned char *uri, size_t len, int flags) {
310 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
311 r = (coap_resource_t *)coap_malloc(sizeof(coap_resource_t));
314 r = (coap_resource_t *)memp_malloc(MEMP_COAP_RESOURCE);
317 r = (coap_resource_t *)memb_alloc(&resource_storage);
320 memset(r, 0, sizeof(coap_resource_t));
323 LIST_STRUCT_INIT(r, link_attr);
324 #endif /* WITH_CONTIKI */
325 LIST_STRUCT_INIT(r, subscribers);
327 r->uri.s = (unsigned char *)uri;
330 coap_hash_path(r->uri.s, r->uri.length, r->key);
334 debug("coap_resource_init: no memory left\n");
341 coap_add_attr(coap_resource_t *resource,
342 const unsigned char *name, size_t nlen,
343 const unsigned char *val, size_t vlen,
347 if (!resource || !name)
350 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
351 attr = (coap_attr_t *)coap_malloc(sizeof(coap_attr_t));
354 attr = (coap_attr_t *)memp_malloc(MEMP_COAP_RESOURCEATTR);
357 attr = (coap_attr_t *)memb_alloc(&attribute_storage);
361 attr->name.length = nlen;
362 attr->value.length = val ? vlen : 0;
364 attr->name.s = (unsigned char *)name;
365 attr->value.s = (unsigned char *)val;
369 /* add attribute to resource list */
371 LL_PREPEND(resource->link_attr, attr);
372 #else /* WITH_CONTIKI */
373 list_add(resource->link_attr, attr);
374 #endif /* WITH_CONTIKI */
376 debug("coap_add_attr: no memory left\n");
383 coap_find_attr(coap_resource_t *resource,
384 const unsigned char *name, size_t nlen) {
387 if (!resource || !name)
391 LL_FOREACH(resource->link_attr, attr) {
392 #else /* WITH_CONTIKI */
393 for (attr = list_head(resource->link_attr); attr;
394 attr = list_item_next(attr)) {
395 #endif /* WITH_CONTIKI */
396 if (attr->name.length == nlen &&
397 memcmp(attr->name.s, name, nlen) == 0)
405 coap_delete_attr(coap_attr_t *attr) {
408 if (attr->flags & COAP_ATTR_FLAGS_RELEASE_NAME)
409 coap_free(attr->name.s);
410 if (attr->flags & COAP_ATTR_FLAGS_RELEASE_VALUE)
411 coap_free(attr->value.s);
416 memp_free(MEMP_COAP_RESOURCEATTR, attr);
419 /* FIXME it looks like this was never implemented */
424 coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key) {
425 coap_opt_iterator_t opt_iter;
426 coap_opt_filter_t filter;
429 memset(key, 0, sizeof(coap_key_t));
431 coap_option_filter_clear(filter);
432 coap_option_setb(filter, COAP_OPTION_URI_PATH);
434 coap_option_iterator_init((coap_pdu_t *)request, &opt_iter, filter);
435 while ((option = coap_option_next(&opt_iter)))
436 coap_hash(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option), key);
440 coap_add_resource(coap_context_t *context, coap_resource_t *resource) {
442 #ifdef COAP_RESOURCES_NOHASH
443 LL_PREPEND(context->resources, resource);
445 HASH_ADD(hh, context->resources, key, sizeof(coap_key_t), resource);
447 #endif /* WITH_CONTIKI */
451 coap_delete_resource(coap_context_t *context, coap_key_t key) {
452 coap_resource_t *resource;
453 coap_attr_t *attr, *tmp;
455 coap_subscription_t *obs;
461 resource = coap_get_resource_from_key(context, key);
466 #if defined(WITH_POSIX) || defined(WITH_LWIP) || defined(WITH_ARDUINO)
467 #ifdef COAP_RESOURCES_NOHASH
468 LL_DELETE(context->resources, resource);
470 HASH_DELETE(hh, context->resources, resource);
473 /* delete registered attributes */
474 LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
476 if (resource->flags & COAP_RESOURCE_FLAGS_RELEASE_URI)
477 coap_free(resource->uri.s);
479 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
483 memp_free(MEMP_COAP_RESOURCE, resource);
485 #else /* not (WITH_POSIX || WITH_LWIP || WITH_ARDUINO) */
486 /* delete registered attributes */
487 while ( (attr = list_pop(resource->link_attr)) )
488 memb_free(&attribute_storage, attr);
490 /* delete subscribers */
491 while ( (obs = list_pop(resource->subscribers)) ) {
492 /* FIXME: notify observer that its subscription has been removed */
493 memb_free(&subscription_storage, obs);
496 memb_free(&resource_storage, resource);
497 #endif /* WITH_CONTIKI */
503 coap_get_resource_from_key(coap_context_t *context, coap_key_t key) {
505 coap_resource_t *resource;
506 #ifdef COAP_RESOURCES_NOHASH
508 LL_FOREACH(context->resources, resource) {
509 /* if you think you can outspart the compiler and speed things up by (eg by
510 * casting to uint32* and comparing alues), increment this counter: 1 */
511 if (memcmp(key, resource->key, sizeof(coap_key_t)) == 0)
516 HASH_FIND(hh, context->resources, key, sizeof(coap_key_t), resource);
520 #else /* WITH_CONTIKI */
522 coap_resource_t *ptr2;
524 /* the search function is basically taken from memb.c */
525 ptr2 = (coap_resource_t *)resource_storage.mem;
526 for (i = 0; i < resource_storage.num; ++i) {
527 if (resource_storage.count[i] &&
528 (memcmp(ptr2->key, key, sizeof(coap_key_t)) == 0))
529 return (coap_resource_t *)ptr2;
534 #endif /* WITH_CONTIKI */
538 coap_print_link(const coap_resource_t *resource,
539 unsigned char *buf, size_t *len, size_t *offset) {
540 unsigned char *p = buf;
541 const unsigned char *bufend = buf + *len;
543 coap_print_status_t result = 0;
544 const size_t old_offset = *offset;
547 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
548 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
550 COPY_COND_WITH_OFFSET(p, bufend, *offset,
551 resource->uri.s, resource->uri.length, *len);
553 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
556 LL_FOREACH(resource->link_attr, attr) {
557 #else /* WITH_CONTIKI */
558 for (attr = list_head(resource->link_attr); attr;
559 attr = list_item_next(attr)) {
560 #endif /* WITH_CONTIKI */
562 PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
564 COPY_COND_WITH_OFFSET(p, bufend, *offset,
565 attr->name.s, attr->name.length, *len);
568 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
570 COPY_COND_WITH_OFFSET(p, bufend, *offset,
571 attr->value.s, attr->value.length, *len);
575 if (resource->observable) {
576 COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
580 if (result + old_offset - *offset < *len) {
581 result |= COAP_PRINT_STATUS_TRUNC;
587 #ifndef WITHOUT_OBSERVE
588 coap_subscription_t *
589 coap_find_observer(coap_resource_t *resource, const coap_address_t *peer,
591 coap_subscription_t *s;
596 for (s = (coap_subscription_t*)list_head(resource->subscribers); s;
597 s = (coap_subscription_t*)list_item_next(s)) {
598 if (coap_address_equals(&s->subscriber, peer)
599 && (!token || (token->length == s->token_length
600 && memcmp(token->s, s->token, token->length) == 0)))
607 coap_subscription_t *
608 coap_add_observer(coap_resource_t *resource,
609 const coap_address_t *observer,
611 coap_subscription_t *s;
615 /* Check if there is already a subscription for this peer. */
616 s = coap_find_observer(resource, observer, token);
618 /* We are done if subscription was found. */
622 /* s points to a different subscription, so we have to create
624 s = COAP_MALLOC_TYPE(subscription);
629 coap_subscription_init(s);
630 memcpy(&s->subscriber, observer, sizeof(coap_address_t));
632 if (token && token->length) {
633 s->token_length = token->length;
634 memcpy(s->token, token->s, min(s->token_length, 8));
637 /* add subscriber to resource */
638 list_add(resource->subscribers, s);
644 coap_touch_observer(coap_context_t *context, const coap_address_t *observer,
647 coap_subscription_t *s;
650 #ifdef COAP_RESOURCES_NOHASH
651 LL_FOREACH(context->resources, r) {
653 coap_resource_t *tmp;
654 HASH_ITER(hh, context->resources, r, tmp) {
656 s = coap_find_observer(r, observer, token);
661 #else /* WITH_CONTIKI */
662 r = (coap_resource_t *)resource_storage.mem;
663 for (i = 0; i < resource_storage.num; ++i, ++r) {
664 if (resource_storage.count[i]) {
665 s = coap_find_observer(r, observer, token);
671 #endif /* WITH_CONTIKI */
675 coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer,
677 coap_subscription_t *s;
679 s = coap_find_observer(resource, observer, token);
682 list_remove(resource->subscribers, s);
684 COAP_FREE_TYPE(subscription,s);
689 coap_notify_observers(coap_context_t *context, coap_resource_t *r) {
690 coap_method_handler_t h;
691 coap_subscription_t *obs;
693 coap_pdu_t *response;
695 if (r->observable && (r->dirty || r->partiallydirty)) {
696 r->partiallydirty = 0;
698 /* retrieve GET handler, prepare response */
699 h = r->handler[COAP_REQUEST_GET - 1];
700 assert(h); /* we do not allow subscriptions if no
701 * GET handler is defined */
703 for (obs = (coap_subscription_t*)list_head(r->subscribers); obs;
704 obs = (coap_subscription_t*)list_item_next(obs)) {
705 if (r->dirty == 0 && obs->dirty == 0)
706 /* running this resource due to partiallydirty, but this observation's notification was already enqueued */
709 coap_tid_t tid = COAP_INVALID_TID;
711 /* initialize response */
712 response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE);
715 r->partiallydirty = 1;
716 debug("coap_check_notify: pdu init failed, resource stays partially dirty\n");
720 if (!coap_add_token(response, obs->token_length, obs->token)) {
722 r->partiallydirty = 1;
723 debug("coap_check_notify: cannot add token, resource stays partially dirty\n");
724 coap_delete_pdu(response);
728 token.length = obs->token_length;
729 token.s = obs->token;
731 response->hdr->id = coap_new_message_id(context);
732 if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON) {
733 response->hdr->type = COAP_MESSAGE_NON;
735 response->hdr->type = COAP_MESSAGE_CON;
737 /* fill with observer-specific data */
738 h(context, r, &obs->subscriber, NULL, &token, response);
740 if (response->hdr->type == COAP_MESSAGE_CON) {
741 tid = coap_send(context, &obs->subscriber, response, SEND_NOW_CON);
744 tid = coap_send(context, &obs->subscriber, response, SEND_NOW);
748 if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON)
749 coap_delete_pdu(response);
750 if (COAP_INVALID_TID == tid)
752 debug("coap_check_notify: sending failed, resource stays partially dirty\n");
754 r->partiallydirty = 1;
759 /* Increment value for next Observe use. */
766 coap_check_notify(coap_context_t *context) {
770 #ifdef COAP_RESOURCES_NOHASH
771 LL_FOREACH(context->resources, r) {
773 coap_resource_t *tmp;
774 HASH_ITER(hh, context->resources, r, tmp) {
776 coap_notify_observers(context, r);
778 #else /* WITH_CONTIKI */
781 r = (coap_resource_t *)resource_storage.mem;
782 for (i = 0; i < resource_storage.num; ++i, ++r) {
783 if (resource_storage.count[i]) {
784 coap_notify_observers(context, r);
787 #endif /* WITH_CONTIKI */
791 * Checks the failure counter for (peer, token) and removes peer from
792 * the list of observers for the given resource when COAP_OBS_MAX_FAIL
795 * @param context The CoAP context to use
796 * @param resource The resource to check for (peer, token)
797 * @param peer The observer's address
798 * @param token The token that has been used for subscription.
801 coap_remove_failed_observers(coap_context_t *context,
802 coap_resource_t *resource,
803 const coap_address_t *peer,
805 coap_subscription_t *obs;
807 for (obs = (coap_subscription_t*)list_head(resource->subscribers); obs;
808 obs = (coap_subscription_t*)list_item_next(obs)) {
809 if (coap_address_equals(peer, &obs->subscriber) &&
810 token->length == obs->token_length &&
811 memcmp(token->s, obs->token, token->length) == 0) {
813 /* count failed notifies and remove when
814 * COAP_MAX_FAILED_NOTIFY is reached */
815 if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
818 list_remove(resource->subscribers, obs);
822 if (LOG_DEBUG <= coap_get_log_level()) {
823 #ifndef INET6_ADDRSTRLEN
824 #define INET6_ADDRSTRLEN 40
826 unsigned char addr[INET6_ADDRSTRLEN+8];
828 if (coap_print_addr(&obs->subscriber, addr, INET6_ADDRSTRLEN+8))
829 debug("** removed observer %s\n", addr);
832 coap_cancel_all_messages(context, &obs->subscriber,
833 obs->token, obs->token_length);
835 COAP_FREE_TYPE(subscription, obs);
838 break; /* break loop if observer was found */
843 coap_handle_failed_notify(coap_context_t *context,
844 const coap_address_t *peer,
850 #ifdef COAP_RESOURCES_NOHASH
851 LL_FOREACH(context->resources, r) {
853 coap_resource_t *tmp;
854 HASH_ITER(hh, context->resources, r, tmp) {
856 coap_remove_failed_observers(context, r, peer, token);
858 #else /* WITH_CONTIKI */
861 r = (coap_resource_t *)resource_storage.mem;
862 for (i = 0; i < resource_storage.num; ++i, ++r) {
863 if (resource_storage.count[i]) {
864 coap_remove_failed_observers(context, r, peer, token);
867 #endif /* WITH_CONTIKI */
869 #endif /* WITHOUT_NOTIFY */