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_CONTIKI)
28#include "memb.h"
29
30MEMB(subscription_storage, coap_subscription_t, COAP_MAX_SUBSCRIBERS);
31
32void
33coap_resources_init() {
34 memb_init(&subscription_storage);
35}
36
38coap_malloc_subscription() {
39 return memb_alloc(&subscription_storage);
40}
41
43coap_free_subscription(coap_subscription_t *subscription) {
44 memb_free(&subscription_storage, subscription);
45}
46
47#endif
48
49#define COAP_PRINT_STATUS_MAX (~COAP_PRINT_STATUS_MASK)
50
51#ifndef min
52#define min(a,b) ((a) < (b) ? (a) : (b))
53#endif
54
55/* Helper functions for conditional output of character sequences into
56 * a given buffer. The first Offset characters are skipped.
57 */
58
63#define PRINT_WITH_OFFSET(Buf,Offset,Char) \
64 if ((Offset) == 0) { \
65 (*(Buf)++) = (Char); \
66 } else { \
67 (Offset)--; \
68 } \
69
73#define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
74 if ((Buf) < (Bufend)) { \
75 PRINT_WITH_OFFSET(Buf,Offset,Char); \
76 } \
77 (Result)++; \
78 }
79
85#define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
86 size_t i; \
87 for (i = 0; i < (Length); i++) { \
88 PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
89 } \
90 }
91
92static int
93match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix,
94 int match_substring
95) {
96 assert(text); assert(pattern);
97
98 if (text->length < pattern->length)
99 return 0;
100
101 if (match_substring) {
102 const uint8_t *next_token = text->s;
103 size_t remaining_length = text->length;
104 while (remaining_length) {
105 size_t token_length;
106 const uint8_t *token = next_token;
107 next_token = (unsigned char *)memchr(token, ' ', remaining_length);
108
109 if (next_token) {
110 token_length = next_token - token;
111 remaining_length -= (token_length + 1);
112 next_token++;
113 } else {
114 token_length = remaining_length;
115 remaining_length = 0;
116 }
117
118 if ((match_prefix || pattern->length == token_length) &&
119 memcmp(token, pattern->s, pattern->length) == 0)
120 return 1;
121 }
122 return 0;
123 }
124
125 return (match_prefix || pattern->length == text->length) &&
126 memcmp(text->s, pattern->s, pattern->length) == 0;
127}
128
150#if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
152coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
153 size_t offset,
154 const coap_string_t *query_filter COAP_UNUSED) {
155#else /* not a GCC */
157coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
158 size_t offset, const coap_string_t *query_filter) {
159#endif /* GCC */
160 size_t output_length = 0;
161 unsigned char *p = buf;
162 const uint8_t *bufend = buf + *buflen;
163 size_t left, written = 0;
164 coap_print_status_t result;
165 const size_t old_offset = offset;
166 int subsequent_resource = 0;
167#ifndef WITHOUT_QUERY_FILTER
168 coap_str_const_t resource_param = { 0, NULL }, query_pattern = { 0, NULL };
169 int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
170#define MATCH_URI 0x01
171#define MATCH_PREFIX 0x02
172#define MATCH_SUBSTRING 0x04
173 static const coap_str_const_t _rt_attributes[] = {
174 {2, (const uint8_t *)"rt"},
175 {2, (const uint8_t *)"if"},
176 {3, (const uint8_t *)"rel"},
177 {0, NULL}};
178#endif /* WITHOUT_QUERY_FILTER */
179
180#ifndef WITHOUT_QUERY_FILTER
181 /* split query filter, if any */
182 if (query_filter) {
183 resource_param.s = query_filter->s;
184 while (resource_param.length < query_filter->length &&
185 resource_param.s[resource_param.length] != '=')
186 resource_param.length++;
187
188 if (resource_param.length < query_filter->length) {
189 const coap_str_const_t *rt_attributes;
190 if (resource_param.length == 4 &&
191 memcmp(resource_param.s, "href", 4) == 0)
192 flags |= MATCH_URI;
193
194 for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
195 if (resource_param.length == rt_attributes->length &&
196 memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
197 flags |= MATCH_SUBSTRING;
198 break;
199 }
200 }
201
202 /* rest is query-pattern */
203 query_pattern.s =
204 query_filter->s + resource_param.length + 1;
205
206 assert((resource_param.length + 1) <= query_filter->length);
207 query_pattern.length =
208 query_filter->length - (resource_param.length + 1);
209
210 if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
211 query_pattern.s++;
212 query_pattern.length--;
213 }
214
215 if (query_pattern.length &&
216 query_pattern.s[query_pattern.length-1] == '*') {
217 query_pattern.length--;
218 flags |= MATCH_PREFIX;
219 }
220 }
221 }
222#endif /* WITHOUT_QUERY_FILTER */
223
224 RESOURCES_ITER(context->resources, r) {
225
226#ifndef WITHOUT_QUERY_FILTER
227 if (resource_param.length) { /* there is a query filter */
228
229 if (flags & MATCH_URI) { /* match resource URI */
230 if (!match(r->uri_path, &query_pattern, (flags & MATCH_PREFIX) != 0,
231 (flags & MATCH_SUBSTRING) != 0))
232 continue;
233 } else { /* match attribute */
234 coap_attr_t *attr;
235 coap_str_const_t unquoted_val;
236 attr = coap_find_attr(r, &resource_param);
237 if (!attr || !attr->value) continue;
238 unquoted_val = *attr->value;
239 if (attr->value->s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
240 unquoted_val.length -= 2;
241 unquoted_val.s += 1;
242 }
243 if (!(match(&unquoted_val, &query_pattern,
244 (flags & MATCH_PREFIX) != 0,
245 (flags & MATCH_SUBSTRING) != 0)))
246 continue;
247 }
248 }
249#endif /* WITHOUT_QUERY_FILTER */
250
251 if (!subsequent_resource) { /* this is the first resource */
252 subsequent_resource = 1;
253 } else {
254 PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
255 }
256
257 left = bufend - p; /* calculate available space */
258 result = coap_print_link(r, p, &left, &offset);
259
260 if (result & COAP_PRINT_STATUS_ERROR) {
261 break;
262 }
263
264 /* coap_print_link() returns the number of characters that
265 * where actually written to p. Now advance to its end. */
266 p += COAP_PRINT_OUTPUT_LENGTH(result);
267 written += left;
268 }
269
270 *buflen = written;
271 output_length = p - buf;
272
273 if (output_length > COAP_PRINT_STATUS_MAX) {
275 }
276
277 result = (coap_print_status_t)output_length;
278
279 if (result + old_offset - offset < *buflen) {
280 result |= COAP_PRINT_STATUS_TRUNC;
281 }
282 return result;
283}
284
285static coap_str_const_t null_path_value = {0, (const uint8_t*)""};
286static coap_str_const_t *null_path = &null_path_value;
287
289coap_resource_init(coap_str_const_t *uri_path, int flags) {
291
293 if (r) {
294 memset(r, 0, sizeof(coap_resource_t));
295
296 if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) {
297 /* Need to take a copy if caller is not providing a release request */
298 if (uri_path)
299 uri_path = coap_new_str_const(uri_path->s, uri_path->length);
300 else
301 uri_path = coap_new_str_const(null_path->s, null_path->length);
302 }
303 else if (!uri_path) {
304 /* Do not expect this, but ... */
305 uri_path = coap_new_str_const(null_path->s, null_path->length);
306 }
307
308 if (uri_path)
309 r->uri_path = uri_path;
310
311 r->flags = flags;
312 r->observe = 2;
313 } else {
314 coap_log_debug("coap_resource_init: no memory left\n");
315 }
316
317 return r;
318}
319
320static const uint8_t coap_unknown_resource_uri[] =
321 "- Unknown -";
322
326
328 if (r) {
329 memset(r, 0, sizeof(coap_resource_t));
330 r->is_unknown = 1;
331 /* Something unlikely to be used, but it shows up in the logs */
332 r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1);
335 } else {
336 coap_log_debug("coap_resource_unknown_init: no memory left\n");
337 }
338
339 return r;
340}
341
344 return coap_resource_unknown_init2(put_handler, 0);
345}
346
347static const uint8_t coap_proxy_resource_uri[] =
348 "- Proxy URI -";
349
352 size_t host_name_count,
353 const char *host_name_list[], int flags) {
355
356 if (host_name_count == 0) {
358 "coap_resource_proxy_uri_init: Must have one or more host names defined\n");
359 return NULL;
360 }
362 if (r) {
363 size_t i;
364 memset(r, 0, sizeof(coap_resource_t));
365 r->is_proxy_uri = 1;
366 /* Something unlikely to be used, but it shows up in the logs */
367 r->uri_path = coap_new_str_const(coap_proxy_resource_uri, sizeof(coap_proxy_resource_uri)-1);
368 /* Preset all the handlers */
369 for (i = 0; i < (sizeof(r->handler) / sizeof(r->handler[0])); i++) {
370 r->handler[i] = handler;
371 }
372 if (host_name_count) {
373 r->proxy_name_list = coap_malloc_type(COAP_STRING, host_name_count *
374 sizeof(coap_str_const_t*));
375 if (r->proxy_name_list) {
376 for (i = 0; i < host_name_count; i++) {
377 r->proxy_name_list[i] =
378 coap_new_str_const((const uint8_t*)host_name_list[i],
379 strlen(host_name_list[i]));
380 if (!r->proxy_name_list[i]) {
382 "coap_resource_proxy_uri_init: unable to add host name\n");
383 if (i == 0) {
385 r->proxy_name_list = NULL;
386 }
387 break;
388 }
389 }
390 r->proxy_name_count = i;
391 }
392 }
394 } else {
395 coap_log_debug("coap_resource_proxy_uri_init2: no memory left\n");
396 }
397
398 return r;
399}
400
403 size_t host_name_count, const char *host_name_list[]) {
404 return coap_resource_proxy_uri_init2(handler, host_name_count,
405 host_name_list, 0);
406}
407
410 coap_str_const_t *name,
411 coap_str_const_t *val,
412 int flags) {
413 coap_attr_t *attr;
414
415 if (!resource || !name)
416 return NULL;
417
419
420 if (attr) {
421 if (!(flags & COAP_ATTR_FLAGS_RELEASE_NAME)) {
422 /* Need to take a copy if caller is not providing a release request */
423 name = coap_new_str_const(name->s, name->length);
424 }
425 attr->name = name;
426 if (val) {
427 if (!(flags & COAP_ATTR_FLAGS_RELEASE_VALUE)) {
428 /* Need to take a copy if caller is not providing a release request */
429 val = coap_new_str_const(val->s, val->length);
430 }
431 }
432 attr->value = val;
433
434 attr->flags = flags;
435
436 /* add attribute to resource list */
437 LL_PREPEND(resource->link_attr, attr);
438 } else {
439 coap_log_debug("coap_add_attr: no memory left\n");
440 }
441
442 return attr;
443}
444
447 coap_str_const_t *name) {
448 coap_attr_t *attr;
449
450 if (!resource || !name)
451 return NULL;
452
453 LL_FOREACH(resource->link_attr, attr) {
454 if (attr->name->length == name->length &&
455 memcmp(attr->name->s, name->s, name->length) == 0)
456 return attr;
457 }
458
459 return NULL;
460}
461
464 if (attr)
465 return attr->value;
466 return NULL;
467}
468
469void
471 if (!attr)
472 return;
474 if (attr->value) {
476 }
477
479}
480
481typedef enum coap_deleting_resource_t {
482 COAP_DELETING_RESOURCE,
483 COAP_NOT_DELETING_RESOURCE
484} coap_deleting_resource_t;
485
486static void coap_notify_observers(coap_context_t *context, coap_resource_t *r,
487 coap_deleting_resource_t deleting);
488
489static void
490coap_free_resource(coap_resource_t *resource) {
491 coap_attr_t *attr, *tmp;
492 coap_subscription_t *obs, *otmp;
493
494 assert(resource);
495
496 coap_resource_notify_observers(resource, NULL);
497 coap_notify_observers(resource->context, resource, COAP_DELETING_RESOURCE);
498
499 if (resource->context->release_userdata && resource->user_data)
500 resource->context->release_userdata(resource->user_data);
501
502 /* delete registered attributes */
503 LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
504
505 /* Either the application provided or libcoap copied - need to delete it */
507
508 /* free all elements from resource->subscribers */
509 LL_FOREACH_SAFE( resource->subscribers, obs, otmp ) {
511 coap_delete_pdu(obs->pdu);
514 }
515 if (resource->proxy_name_count && resource->proxy_name_list) {
516 size_t i;
517
518 for (i = 0; i < resource->proxy_name_count; i++) {
520 }
522 }
523
524 coap_free_type(COAP_RESOURCE, resource);
525}
526
527void
529 if (resource->is_unknown) {
530 if (context->unknown_resource)
531 coap_free_resource(context->unknown_resource);
532 context->unknown_resource = resource;
533 }
534 else if (resource->is_proxy_uri) {
535 if (context->proxy_uri_resource)
536 coap_free_resource(context->proxy_uri_resource);
537 context->proxy_uri_resource = resource;
538 }
539 else {
541 resource->uri_path);
542
543 if (r) {
545 "coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
546 (int)resource->uri_path->length, (int)resource->uri_path->length,
547 resource->uri_path->s);
548 coap_delete_resource(context, r);
549 }
550 RESOURCES_ADD(context->resources, resource);
551 }
552 assert(resource->context == NULL);
553 resource->context = context;
554}
555
556/*
557 * Input context is ignored, but param left there to keep API consistent
558 */
559int
561 if (!resource)
562 return 0;
563
564 context = resource->context;
565
566 if (resource->is_unknown) {
567 if (context && context->unknown_resource == resource) {
568 context->unknown_resource = NULL;
569 }
570 } else if (resource->is_proxy_uri) {
571 if (context && context->proxy_uri_resource == resource) {
572 context->proxy_uri_resource = NULL;
573 }
574 } else if (context) {
575 /* remove resource from list */
576 RESOURCES_DELETE(context->resources, resource);
577 }
578
579 /* and free its allocated memory */
580 coap_free_resource(resource);
581
582 return 1;
583}
584
585void
587 coap_resource_t *res;
588 coap_resource_t *rtmp;
589
590 /* Cannot call RESOURCES_ITER because coap_free_resource() releases
591 * the allocated storage. */
592
593 HASH_ITER(hh, context->resources, res, rtmp) {
594 HASH_DELETE(hh, context->resources, res);
595 coap_free_resource(res);
596 }
597
598 context->resources = NULL;
599
600 if (context->unknown_resource) {
601 coap_free_resource(context->unknown_resource);
602 context->unknown_resource = NULL;
603 }
604 if (context->proxy_uri_resource) {
605 coap_free_resource(context->proxy_uri_resource);
606 context->proxy_uri_resource = NULL;
607 }
608}
609
612 coap_resource_t *result;
613
614 RESOURCES_FIND(context->resources, uri_path, result);
615
616 return result;
617}
618
620coap_print_link(const coap_resource_t *resource,
621 unsigned char *buf, size_t *len, size_t *offset) {
622 unsigned char *p = buf;
623 const uint8_t *bufend = buf + *len;
624 coap_attr_t *attr;
625 coap_print_status_t result = 0;
626 size_t output_length = 0;
627 const size_t old_offset = *offset;
628
629 *len = 0;
630 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
631 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
632
633 COPY_COND_WITH_OFFSET(p, bufend, *offset,
634 resource->uri_path->s, resource->uri_path->length, *len);
635
636 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
637
638 LL_FOREACH(resource->link_attr, attr) {
639
640 PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
641
642 COPY_COND_WITH_OFFSET(p, bufend, *offset,
643 attr->name->s, attr->name->length, *len);
644
645 if (attr->value && attr->value->s) {
646 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
647
648 COPY_COND_WITH_OFFSET(p, bufend, *offset,
649 attr->value->s, attr->value->length, *len);
650 }
651
652 }
653 if (resource->observable) {
654 COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
655 }
656
657 output_length = p - buf;
658
659 if (output_length > COAP_PRINT_STATUS_MAX) {
661 }
662
663 result = (coap_print_status_t)output_length;
664
665 if (result + old_offset - *offset < *len) {
666 result |= COAP_PRINT_STATUS_TRUNC;
667 }
668
669 return result;
670}
671
672void
674 coap_request_t method,
675 coap_method_handler_t handler) {
676 coap_register_request_handler(resource, method, handler);
677}
678
679void
681 coap_request_t method,
682 coap_method_handler_t handler) {
683 assert(resource);
684 assert(method > 0 && (size_t)(method-1) <
685 sizeof(resource->handler)/sizeof(coap_method_handler_t));
686 resource->handler[method-1] = handler;
687}
688
691 const coap_binary_t *token) {
693
694 assert(resource);
695 assert(session);
696
697 LL_FOREACH(resource->subscribers, s) {
698 if (s->session == session
699 && (!token || (token->length == s->pdu->token_length
700 && memcmp(token->s, s->pdu->token, token->length) == 0)))
701 return s;
702 }
703
704 return NULL;
705}
706
707static coap_subscription_t *
708coap_find_observer_cache_key(coap_resource_t *resource, coap_session_t *session,
709 const coap_cache_key_t *cache_key) {
711
712 assert(resource);
713 assert(session);
714
715 LL_FOREACH(resource->subscribers, s) {
716 if (s->session == session
717 && (memcmp(cache_key, s->cache_key, sizeof(coap_cache_key_t)) == 0))
718 return s;
719 }
720
721 return NULL;
722}
723
726 coap_session_t *session,
727 const coap_binary_t *token,
728 const coap_pdu_t *request) {
730 coap_cache_key_t *cache_key = NULL;
731 size_t len;
732 const uint8_t *data;
733/* https://tools.ietf.org/html/rfc7641#section-3.6 */
734static const uint16_t cache_ignore_options[] = { COAP_OPTION_ETAG };
735
736 assert( session );
737
738 /* Check if there is already a subscription for this peer. */
739 s = coap_find_observer(resource, session, token);
740 if (!s) {
741 /*
742 * Cannot allow a duplicate to be created for the same query as application
743 * may not be cleaning up duplicates. If duplicate found, then original
744 * observer is deleted and a new one created with the new token
745 */
746 cache_key = coap_cache_derive_key_w_ignore(session, request,
748 cache_ignore_options,
749 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
750 if (cache_key) {
751 s = coap_find_observer_cache_key(resource, session, cache_key);
752 if (s) {
753 /* Delete old entry with old token */
754 coap_binary_t tmp_token = { s->pdu->token_length, s->pdu->token };
755 coap_delete_observer(resource, session, &tmp_token);
756 s = NULL;
757 }
758 }
759 }
760
761 /* We are done if subscription was found. */
762 if (s) {
763 return s;
764 }
765
766 /* Create a new subscription */
768
769 if (!s) {
770 coap_delete_cache_key(cache_key);
771 return NULL;
772 }
773
775 s->pdu = coap_pdu_duplicate(request, session, request->token_length,
776 request->token, NULL);
777 if (s->pdu == NULL) {
778 coap_delete_cache_key(cache_key);
780 return NULL;
781 }
782 if (coap_get_data(request, &len, &data)) {
783 /* This could be a large bodied FETCH */
784 s->pdu->max_size = 0;
785 coap_add_data(s->pdu, len, data);
786 }
787 if (cache_key == NULL) {
788 cache_key = coap_cache_derive_key_w_ignore(session, request,
790 cache_ignore_options,
791 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
792 if (cache_key == NULL) {
794 coap_delete_cache_key(cache_key);
796 return NULL;
797 }
798 }
799 s->cache_key = cache_key;
800 s->session = coap_session_reference(session);
801
802 /* add subscriber to resource */
803 LL_PREPEND(resource->subscribers, s);
804
805 coap_log_debug("create new subscription %p key 0x%02x%02x%02x%02x\n",
806 (void*)s, s->cache_key->key[0], s->cache_key->key[1],
807 s->cache_key->key[2], s->cache_key->key[3]);
808
809 return s;
810}
811
812void
814 const coap_binary_t *token) {
816
817 RESOURCES_ITER(context->resources, r) {
818 s = coap_find_observer(r, session, token);
819 if (s) {
820 s->fail_cnt = 0;
821 }
822 }
823}
824
825int
827 const coap_binary_t *token) {
829
830 s = coap_find_observer(resource, session, token);
831
832 if ( s && coap_get_log_level() >= LOG_DEBUG ) {
833 char outbuf[2 * 8 + 1] = "";
834 unsigned int i;
835 for ( i = 0; i < s->pdu->token_length; i++ )
836 snprintf( &outbuf[2 * i], 3, "%02x", s->pdu->token[i] );
838 "removed subscription %p with token '%s' key 0x%02x%02x%02x%02x\n",
839 (void*)s, outbuf, s->cache_key->key[0], s->cache_key->key[1],
840 s->cache_key->key[2], s-> cache_key->key[3]);
841 }
842
843 if (resource->subscribers && s) {
844 LL_DELETE(resource->subscribers, s);
845 coap_session_release( session );
849 }
850
851 return s != NULL;
852}
853
854void
856 RESOURCES_ITER(context->resources, resource) {
857 coap_subscription_t *s, *tmp;
858 LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
859 if (s->session == session) {
860 LL_DELETE(resource->subscribers, s);
861 coap_session_release(session);
865 }
866 }
867 }
868}
869
870static void
871coap_notify_observers(coap_context_t *context, coap_resource_t *r,
872 coap_deleting_resource_t deleting) {
874 coap_subscription_t *obs, *otmp;
875 coap_binary_t token;
876 coap_pdu_t *response;
877 uint8_t buf[4];
878 coap_string_t *query;
879 coap_block_b_t block;
880 coap_tick_t now;
881 coap_session_t *obs_session;
882
883 if (r->observable && (r->dirty || r->partiallydirty)) {
884 r->partiallydirty = 0;
885
886 LL_FOREACH_SAFE(r->subscribers, obs, otmp) {
887 obs_session = obs->session;
888 if (r->dirty == 0 && obs->dirty == 0) {
889 /*
890 * running this resource due to partiallydirty, but this observation's
891 * notification was already enqueued
892 */
893 context->observe_pending = 1;
894 continue;
895 }
896 if (obs->session->con_active >= COAP_NSTART(obs->session) &&
898 (obs->non_cnt >= COAP_OBS_MAX_NON))) {
899 /* Waiting for the previous unsolicited response to finish */
900 r->partiallydirty = 1;
901 obs->dirty = 1;
902 context->observe_pending = 1;
903 continue;
904 }
905 coap_ticks(&now);
906 if (obs->session->lg_xmit && obs->session->lg_xmit->last_all_sent == 0 &&
907 obs->session->lg_xmit->last_obs &&
908 (obs->session->lg_xmit->last_obs + 2*COAP_TICKS_PER_SECOND) > now) {
909 /* Waiting for the previous blocked unsolicited response to finish */
910 r->partiallydirty = 1;
911 obs->dirty = 1;
912 context->observe_pending = 1;
913 continue;
914 }
915
917 obs->dirty = 0;
918 /* initialize response */
920 if (!response) {
921 obs->dirty = 1;
922 r->partiallydirty = 1;
923 context->observe_pending = 1;
925 "coap_check_notify: pdu init failed, resource stays "
926 "partially dirty\n");
927 continue;
928 }
929
930 if (!coap_add_token(response, obs->pdu->token_length, obs->pdu->token)) {
931 obs->dirty = 1;
932 r->partiallydirty = 1;
933 context->observe_pending = 1;
935 "coap_check_notify: cannot add token, resource stays "
936 "partially dirty\n");
937 coap_delete_pdu(response);
938 continue;
939 }
940
941 token.length = obs->pdu->token_length;
942 token.s = obs->pdu->token;
943
944 obs->pdu->mid = response->mid = coap_new_message_id(obs->session);
945 /* A lot of the reliable code assumes type is CON */
949 obs->non_cnt < COAP_OBS_MAX_NON)) {
950 response->type = COAP_MESSAGE_NON;
951 } else {
952 response->type = COAP_MESSAGE_CON;
953 }
954 switch (deleting) {
955 case COAP_NOT_DELETING_RESOURCE:
956 /* fill with observer-specific data */
958 coap_encode_var_safe(buf, sizeof (buf),
959 r->observe),
960 buf);
962 &block)) {
963 /* Will get updated later (e.g. M bit) if appropriate */
965 coap_encode_var_safe(buf, sizeof(buf),
966 ((0 << 4) |
967 (0 << 3) |
968 block.aszx)),
969 buf);
970 }
971
972 h = r->handler[obs->pdu->code - 1];
973 assert(h); /* we do not allow subscriptions if no
974 * GET/FETCH handler is defined */
975 query = coap_get_query(obs->pdu);
976 coap_log_debug("Observe PDU presented to app.\n");
978 coap_log_debug("call custom handler for resource '%*.*s'\n",
979 (int)r->uri_path->length, (int)r->uri_path->length,
980 r->uri_path->s);
981 h(r, obs->session, obs->pdu, query, response);
982 /* Check if lg_xmit generated and update PDU code if so */
983 coap_check_code_lg_xmit(obs->session, obs->pdu, response, r, query);
984 coap_delete_string(query);
985 if (COAP_RESPONSE_CLASS(response->code) != 2) {
987 }
988 if (COAP_RESPONSE_CLASS(response->code) > 2) {
989 coap_delete_observer(r, obs->session, &token);
990 obs = NULL;
991 }
992 break;
993 case COAP_DELETING_RESOURCE:
994 default:
995 /* Don't worry if it does not get there */
996 response->type = COAP_MESSAGE_NON;
997 response->code = COAP_RESPONSE_CODE(404);
998 break;
999 }
1000
1001 if (obs) {
1002 if (response->type == COAP_MESSAGE_CON ||
1004 obs->non_cnt = 0;
1005 } else {
1006 obs->non_cnt++;
1007 }
1008 }
1009
1010 mid = coap_send_internal(obs_session, response);
1011
1012 if (COAP_INVALID_MID == mid && obs) {
1015 "coap_check_notify: sending failed, resource stays "
1016 "partially dirty\n");
1017 LL_FOREACH(r->subscribers, s) {
1018 if (s == obs) {
1019 /* obs not deleted during coap_send_internal() */
1020 obs->dirty = 1;
1021 break;
1022 }
1023 }
1024 r->partiallydirty = 1;
1025 context->observe_pending = 1;
1026 }
1027 }
1028 }
1029 r->dirty = 0;
1030}
1031
1032int
1034 return coap_resource_notify_observers(r, query);
1035}
1036
1037int
1039 const coap_string_t *query COAP_UNUSED) {
1040 if (!r->observable)
1041 return 0;
1042 if ( !r->subscribers )
1043 return 0;
1044 r->dirty = 1;
1045
1046 /* Increment value for next Observe use. Observe value must be < 2^24 */
1047 r->observe = (r->observe + 1) & 0xFFFFFF;
1048
1049 assert(r->context);
1050 r->context->observe_pending = 1;
1051#ifdef COAP_EPOLL_SUPPORT
1053#endif /* COAP_EPOLL_SUPPORT */
1054 return 1;
1055}
1056
1057void
1058coap_resource_set_mode(coap_resource_t *resource, int mode) {
1059 resource->flags = (resource->flags &
1062}
1063
1064void
1065coap_resource_set_userdata(coap_resource_t *resource, void *data) {
1066 resource->user_data = data;
1067}
1068
1069void *
1071 return resource->user_data;
1072}
1073
1074void
1077 context->release_userdata = callback;
1078}
1079
1080void
1082 resource->observable = mode ? 1 : 0;
1083}
1084
1087 if (resource)
1088 return resource->uri_path;
1089 return NULL;
1090}
1091
1092void
1094
1095 if (context->observe_pending) {
1096 context->observe_pending = 0;
1097 RESOURCES_ITER(context->resources, r) {
1098 coap_notify_observers(context, r, COAP_NOT_DELETING_RESOURCE);
1099 }
1100 }
1101}
1102
1113static void
1114coap_remove_failed_observers(coap_context_t *context,
1115 coap_resource_t *resource,
1116 coap_session_t *session,
1117 const coap_binary_t *token) {
1118 coap_subscription_t *obs, *otmp;
1119
1120 LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
1121 if ( obs->session == session &&
1122 token->length == obs->pdu->token_length &&
1123 memcmp(token->s, obs->pdu->token, token->length) == 0) {
1124
1125 /* count failed notifies and remove when
1126 * COAP_OBS_MAX_FAIL is reached */
1127 obs->fail_cnt++;
1128 if (obs->fail_cnt >= COAP_OBS_MAX_FAIL) {
1129 coap_cancel_all_messages(context, obs->session,
1130 obs->pdu->token, obs->pdu->token_length);
1131 coap_delete_observer(resource, session, token);
1132 }
1133 break; /* break loop if observer was found */
1134 }
1135 }
1136}
1137
1138void
1140 coap_session_t *session,
1141 const coap_binary_t *token) {
1142
1143 RESOURCES_ITER(context->resources, r) {
1144 coap_remove_failed_observers(context, r, session, token);
1145 }
1146}
1147
1148#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(const coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_resource_t *resource, const coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
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)
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Observe Notifications will be sent non-confirmable by default.
Definition: resource.h:61
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.
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_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.
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...
#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.
#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.
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
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.
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_set_userdata(coap_resource_t *resource, void *data)
Sets the user_data.
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:449
coap_str_const_t * coap_resource_get_uri_path(coap_resource_t *resource)
Get the uri_path from a resource.
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_str_const_t * coap_attr_get_value(coap_attr_t *attribute)
Returns attribute's value.
#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.
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.
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_init(coap_method_handler_t put_handler)
Creates a new resource object for the unknown resource handler with support for PUT.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1232
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:2144
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
#define coap_log_debug(...)
Definition: coap_debug.h:48
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:588
#define LOG_DEBUG
Definition: coap_debug.h:105
#define coap_log_warn(...)
Definition: coap_debug.h:45
#define coap_log_err(...)
Definition: coap_debug.h:44
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:343
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:625
#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:164
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:285
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:723
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:177
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
#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
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:692
@ 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:372
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:36
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:132
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:126
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.
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_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.
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:623
#define COAP_UNUSED
Definition: libcoap.h:60
#define COAP_STATIC_INLINE
Definition: libcoap.h:45
@ COAP_RESOURCE
Definition: mem.h:46
@ COAP_RESOURCEATTR
Definition: mem.h:47
@ COAP_SUBSCRIPTION
Definition: mem.h:57
@ COAP_STRING
Definition: mem.h:37
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.
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: coap_str.h:56
size_t length
length of binary data
Definition: coap_str.h:57
uint8_t * s
binary data
Definition: coap_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
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
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
coap_proto_t proto
protocol used
uint8_t con_active
Active CON request sent.
CoAP string data definition with const data.
Definition: coap_str.h:46
const uint8_t * s
read-only string data
Definition: coap_str.h:48
size_t length
length of string
Definition: coap_str.h:47
CoAP string data definition.
Definition: coap_str.h:38
uint8_t * s
string data
Definition: coap_str.h:40
size_t length
length of string
Definition: coap_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