libcoap 4.3.5-develop-c99bf12
Loading...
Searching...
No Matches
coap_resource.c
Go to the documentation of this file.
1/* coap_resource.c -- generic resource handling
2 *
3 * Copyright (C) 2010--2026 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
17
18#if COAP_SERVER_SUPPORT
19#include <stdio.h>
20
21#ifdef COAP_EPOLL_SUPPORT
22#include <sys/epoll.h>
23#include <sys/timerfd.h>
24#endif /* COAP_EPOLL_SUPPORT */
25
26#ifndef min
27#define min(a,b) ((a) < (b) ? (a) : (b))
28#endif
29
30/* Helper functions for conditional output of character sequences into
31 * a given buffer. The first Offset characters are skipped.
32 */
33
38#define PRINT_WITH_OFFSET(Buf,Offset,Char) \
39 if ((Offset) == 0) { \
40 (*(Buf)++) = (Char); \
41 } else { \
42 (Offset)--; \
43 } \
44
48#define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
49 if ((Buf) < (Bufend)) { \
50 PRINT_WITH_OFFSET(Buf,Offset,Char); \
51 } \
52 (Result)++; \
53 }
54
60#define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
61 size_t i; \
62 for (i = 0; i < (Length); i++) { \
63 PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
64 } \
65 }
66
67static int
68match(const coap_str_const_t *text, const coap_str_const_t *pattern,
69 int match_prefix, int match_substring) {
70 assert(text);
71 assert(pattern);
72
73 if (text->length < pattern->length || !pattern->s)
74 return 0;
75
76 if (match_substring) {
77 const uint8_t *next_token = text->s;
78 size_t remaining_length = text->length;
79 while (remaining_length) {
80 size_t token_length;
81 const uint8_t *token = next_token;
82 next_token = (unsigned char *)memchr(token, ' ', remaining_length);
83
84 if (next_token) {
85 token_length = next_token - token;
86 remaining_length -= (token_length + 1);
87 next_token++;
88 } else {
89 token_length = remaining_length;
90 remaining_length = 0;
91 }
92
93 if ((match_prefix || pattern->length == token_length) &&
94 memcmp(token, pattern->s, pattern->length) == 0)
95 return 1;
96 }
97 return 0;
98 }
99
100 return (match_prefix || pattern->length == text->length) &&
101 memcmp(text->s, pattern->s, pattern->length) == 0;
102}
103
105coap_print_wellknown(coap_context_t *context, unsigned char *buf,
106 size_t *buflen, size_t offset,
107 const coap_string_t *query_filter) {
108 coap_print_status_t result;
110 result = coap_print_wellknown_lkd(context, buf, buflen, offset, query_filter);
112 return result;
113}
114
115static coap_str_const_t coap_default_uri_wellknown = {
117 (const uint8_t *)COAP_DEFAULT_URI_WELLKNOWN
118};
119
121coap_print_wellknown_lkd(coap_context_t *context, unsigned char *buf,
122 size_t *buflen, size_t offset,
123 const coap_string_t *query_filter) {
124 coap_print_status_t output_length = 0;
125 unsigned char *p = buf;
126 const uint8_t *bufend = buf + *buflen;
127 size_t left, written = 0;
128 coap_print_status_t result;
129 const size_t old_offset = offset;
130 int subsequent_resource = 0;
131#ifdef WITHOUT_QUERY_FILTER
132 (void)query_filter;
133#else
134 coap_str_const_t resource_param = { 0, NULL }, query_pattern = { 0, NULL };
135 int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
136#define MATCH_URI 0x01
137#define MATCH_PREFIX 0x02
138#define MATCH_SUBSTRING 0x04
139 static const coap_str_const_t _rt_attributes[] = {
140 {2, (const uint8_t *)"rt"},
141 {2, (const uint8_t *)"if"},
142 {3, (const uint8_t *)"rel"},
143 {0, NULL}
144 };
145#endif /* WITHOUT_QUERY_FILTER */
146
148#ifndef WITHOUT_QUERY_FILTER
149 /* split query filter, if any */
150 if (query_filter) {
151 resource_param.s = query_filter->s;
152 while (resource_param.length < query_filter->length &&
153 resource_param.s[resource_param.length] != '=')
154 resource_param.length++;
155
156 if (resource_param.length < query_filter->length) {
157 const coap_str_const_t *rt_attributes;
158 if (resource_param.length == 4 &&
159 memcmp(resource_param.s, "href", 4) == 0)
160 flags |= MATCH_URI;
161
162 for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
163 if (resource_param.length == rt_attributes->length &&
164 memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
165 flags |= MATCH_SUBSTRING;
166 break;
167 }
168 }
169
170 /* rest is query-pattern */
171 query_pattern.s =
172 query_filter->s + resource_param.length + 1;
173
174 assert((resource_param.length + 1) <= query_filter->length);
175 query_pattern.length =
176 query_filter->length - (resource_param.length + 1);
177
178 if (query_pattern.length &&
179 (query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
180 query_pattern.s++;
181 query_pattern.length--;
182 }
183
184 if (query_pattern.length &&
185 query_pattern.s[query_pattern.length-1] == '*') {
186 query_pattern.length--;
187 flags |= MATCH_PREFIX;
188 }
189 }
190 }
191#endif /* WITHOUT_QUERY_FILTER */
192
193 RESOURCES_ITER(context->resources, r) {
194
195 if (coap_string_equal(r->uri_path, &coap_default_uri_wellknown)) {
196 /* server app has defined a resource for .well-known/core - ignore */
197 continue;
198 }
199 if (r->flags & COAP_RESOURCE_HIDE_WELLKNOWN_CORE) {
200 continue;
201 }
202#ifndef WITHOUT_QUERY_FILTER
203 if (resource_param.length) { /* there is a query filter */
204
205 if (flags & MATCH_URI) { /* match resource URI */
206 if (!match(r->uri_path, &query_pattern, (flags & MATCH_PREFIX) != 0,
207 (flags & MATCH_SUBSTRING) != 0))
208 continue;
209 } else { /* match attribute */
210 coap_attr_t *attr;
211 coap_str_const_t unquoted_val;
212 attr = coap_find_attr(r, &resource_param);
213 if (!attr || !attr->value)
214 continue;
215 unquoted_val = *attr->value;
216 if (attr->value->s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
217 unquoted_val.length -= 2;
218 unquoted_val.s += 1;
219 }
220 if (!(match(&unquoted_val, &query_pattern,
221 (flags & MATCH_PREFIX) != 0,
222 (flags & MATCH_SUBSTRING) != 0)))
223 continue;
224 }
225 }
226#endif /* WITHOUT_QUERY_FILTER */
227
228 if (!subsequent_resource) { /* this is the first resource */
229 subsequent_resource = 1;
230 } else {
231 PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
232 }
233
234 left = bufend - p; /* calculate available space */
235 result = coap_print_link(r, p, &left, &offset);
236
237 if (result & COAP_PRINT_STATUS_ERROR) {
238 break;
239 }
240
241 /* coap_print_link() returns the number of characters that
242 * where actually written to p. Now advance to its end. */
243 p += COAP_PRINT_OUTPUT_LENGTH(result);
244 written += left;
245 }
246
247 *buflen = written;
248 output_length = (coap_print_status_t)(p - buf);
249
250 if (output_length > COAP_PRINT_STATUS_MAX) {
252 }
253
254 result = (coap_print_status_t)output_length;
255
256 if (result + old_offset - offset < *buflen) {
257 result |= COAP_PRINT_STATUS_TRUNC;
258 }
259 return result;
260}
261
262static coap_str_const_t null_path_value = {0, (const uint8_t *)""};
263static coap_str_const_t *null_path = &null_path_value;
264
266coap_resource_init(coap_str_const_t *uri_path, int flags) {
268
270 if (r) {
271 memset(r, 0, sizeof(coap_resource_t));
272#if COAP_THREAD_SAFE
273 coap_lock_init(&r->lock);
274#endif /* COAP_THREAD_SAFE */
275
276 if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) {
277 /* Need to take a copy if caller is not providing a release request */
278 if (uri_path)
279 uri_path = coap_new_str_const(uri_path->s, uri_path->length);
280 else
281 uri_path = coap_new_str_const(null_path->s, null_path->length);
282 } else if (!uri_path) {
283 /* Do not expect this, but ... */
284 uri_path = coap_new_str_const(null_path->s, null_path->length);
285 }
286
287 if (uri_path)
288 r->uri_path = uri_path;
289
290 r->flags = flags;
291 r->observe = 2;
292 } else {
293 coap_log_debug("coap_resource_init: no memory left\n");
294 }
295
296 return r;
297}
298
299static const uint8_t coap_unknown_resource_uri[] =
300 "- Unknown -";
301
305
307 if (r) {
308 memset(r, 0, sizeof(coap_resource_t));
309#if COAP_THREAD_SAFE
310 coap_lock_init(&r->lock);
311#endif /* COAP_THREAD_SAFE */
312 r->is_unknown = 1;
313 /* Something unlikely to be used, but it shows up in the logs */
314 r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1);
315 r->flags = flags & ~COAP_RESOURCE_FLAGS_RELEASE_URI;
317 } else {
318 coap_log_debug("coap_resource_unknown_init2: no memory left\n");
319 }
320
321 return r;
322}
323
326 return coap_resource_unknown_init2(put_handler, 0);
327}
328
329static const uint8_t coap_proxy_resource_uri[] =
330 "- Proxy URI -";
331
334 size_t host_name_count,
335 const char *host_name_list[], int flags) {
337
339 if (r) {
340 size_t i;
341 memset(r, 0, sizeof(coap_resource_t));
342#if COAP_THREAD_SAFE
343 coap_lock_init(&r->lock);
344#endif /* COAP_THREAD_SAFE */
345 r->is_proxy_uri = 1;
346 /* Something unlikely to be used, but it shows up in the logs */
347 r->uri_path = coap_new_str_const(coap_proxy_resource_uri, sizeof(coap_proxy_resource_uri)-1);
348 /* Preset all the handlers */
349 for (i = 0; i < (sizeof(r->handler) / sizeof(r->handler[0])); i++) {
350 r->handler[i] = handler;
351 }
352 if (host_name_count) {
353 r->proxy_name_list = coap_malloc_type(COAP_STRING, host_name_count *
354 sizeof(coap_str_const_t *));
355 if (r->proxy_name_list) {
356 for (i = 0; i < host_name_count; i++) {
357 r->proxy_name_list[i] =
358 coap_new_str_const((const uint8_t *)host_name_list[i],
359 strlen(host_name_list[i]));
360 if (!r->proxy_name_list[i]) {
361 coap_log_err("coap_resource_proxy_uri_init: unable to add host name\n");
362 if (i == 0) {
363 coap_free_type(COAP_STRING, r->proxy_name_list);
364 r->proxy_name_list = NULL;
365 }
366 break;
367 }
368 }
369 r->proxy_name_count = i;
370 }
371 }
372 r->flags = flags & ~COAP_RESOURCE_FLAGS_RELEASE_URI;
373 } else {
374 coap_log_debug("coap_resource_proxy_uri_init2: no memory left\n");
375 }
376
377 return r;
378}
379
382 size_t host_name_count, const char *host_name_list[]) {
383 return coap_resource_proxy_uri_init2(handler, host_name_count,
384 host_name_list, 0);
385}
386
387static const uint8_t coap_rev_proxy_resource_uri[] =
388 "- Rev Proxy -";
389
393
395 if (r) {
396 memset(r, 0, sizeof(coap_resource_t));
397#if COAP_THREAD_SAFE
398 coap_lock_init(&r->lock);
399#endif /* COAP_THREAD_SAFE */
400 r->is_unknown = 1;
401 r->is_reverse_proxy = 1;
402 /* Something unlikely to be used, but it shows up in the logs */
403 r->uri_path = coap_new_str_const(coap_rev_proxy_resource_uri,
404 sizeof(coap_rev_proxy_resource_uri)-1);
405 r->flags = flags & ~COAP_RESOURCE_FLAGS_RELEASE_URI;
414 } else {
415 coap_log_debug("coap_resource_rev_proxy_init: no memory left\n");
416 }
417
418 return r;
419}
420
423 coap_str_const_t *name,
424 coap_str_const_t *val,
425 int flags) {
426 coap_attr_t *attr;
427
428 if (!resource || !name)
429 return NULL;
431
432 if (attr) {
433 if (!(flags & COAP_ATTR_FLAGS_RELEASE_NAME)) {
434 /* Need to take a copy if caller is not providing a release request */
435 name = coap_new_str_const(name->s, name->length);
436 }
437 attr->name = name;
438 if (val) {
439 if (!(flags & COAP_ATTR_FLAGS_RELEASE_VALUE)) {
440 /* Need to take a copy if caller is not providing a release request */
441 val = coap_new_str_const(val->s, val->length);
442 }
443 }
444 attr->value = val;
445
446 attr->flags = flags;
447
448 /* add attribute to resource list */
449 LL_PREPEND(resource->link_attr, attr);
450 } else {
451 coap_log_debug("coap_add_attr: no memory left\n");
452 }
453
454 return attr;
455}
456
459 coap_str_const_t *name) {
460 coap_attr_t *attr;
461
462 if (!resource || !name)
463 return NULL;
464
465 LL_FOREACH(resource->link_attr, attr) {
466 if (attr->name->length == name->length &&
467 memcmp(attr->name->s, name->s, name->length) == 0)
468 return attr;
469 }
470
471 return NULL;
472}
473
476 if (attr)
477 return attr->value;
478 return NULL;
479}
480
481void
482coap_delete_attr(coap_attr_t *attr) {
483 if (!attr)
484 return;
485 coap_delete_str_const(attr->name);
486 if (attr->value) {
487 coap_delete_str_const(attr->value);
488 }
489
491}
492
493static void coap_notify_observers(coap_context_t *context, coap_resource_t *r,
494 coap_deleting_resource_t deleting);
495
496static void
497coap_free_resource(coap_resource_t *resource, coap_deleting_resource_t deleting) {
498 coap_attr_t *attr, *tmp;
499 coap_subscription_t *obs, *otmp;
500 coap_context_t *context;
501
502 assert(resource);
503
504 context = resource->context;
505 if (context) {
506 if (!context->observe_no_clear) {
507 coap_resource_notify_observers_lkd(resource, deleting);
508 coap_notify_observers(context, resource, deleting);
509 }
510
511 if (context->resource_deleted_cb)
512 coap_lock_callback(context->resource_deleted_cb(context,
513 resource->uri_path,
514 context->observe_user_data));
515
516 if (context->release_userdata_cb && resource->user_data) {
517 coap_lock_callback(context->release_userdata_cb(resource->user_data));
518 }
519 }
520
521 /* delete registered attributes */
522 LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
523
524 /* Either the application provided or libcoap copied - need to delete it */
525 coap_delete_str_const(resource->uri_path);
526
527 /* free all elements from resource->subscribers */
528 LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
529 coap_delete_observer_internal(resource, obs->session, obs);
530 }
531 if (resource->proxy_name_count && resource->proxy_name_list) {
532 size_t i;
533
534 for (i = 0; i < resource->proxy_name_count; i++) {
535 coap_delete_str_const(resource->proxy_name_list[i]);
536 }
537 coap_free_type(COAP_STRING, resource->proxy_name_list);
538 }
539
540 coap_free_type(COAP_RESOURCE, resource);
541}
542
543COAP_API void
545 coap_lock_lock(return);
546 coap_add_resource_lkd(context, resource);
548}
549
550void
551coap_add_resource_lkd(coap_context_t *context, coap_resource_t *resource) {
553 if (resource->is_unknown) {
554 if (context->unknown_resource)
555 coap_free_resource(context->unknown_resource, COAP_DELETING_RESOURCE);
556 context->unknown_resource = resource;
557 } else if (resource->is_proxy_uri) {
558 if (context->proxy_uri_resource)
559 coap_free_resource(context->proxy_uri_resource, COAP_DELETING_RESOURCE);
560 context->proxy_uri_resource = resource;
561 } else {
562 coap_resource_t *r = coap_get_resource_from_uri_path_lkd(context,
563 resource->uri_path);
564
565 if (r) {
566 coap_log_warn("coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
567 (int)resource->uri_path->length, (int)resource->uri_path->length,
568 resource->uri_path->s);
569 coap_delete_resource_lkd(r);
570 }
571 RESOURCES_ADD(context->resources, resource);
572#if COAP_WITH_OBSERVE_PERSIST
573 if (context->unknown_pdu && context->dyn_resource_save_file &&
574 context->dyn_resource_added_cb && resource->observable) {
575 coap_bin_const_t raw_packet;
576
577 raw_packet.s = context->unknown_pdu->token -
578 context->unknown_pdu->hdr_size;
579 raw_packet.length = context->unknown_pdu->used_size +
580 context->unknown_pdu->hdr_size;
581 coap_lock_callback(context->dyn_resource_added_cb(context->unknown_session,
582 resource->uri_path,
583 &raw_packet,
584 context->observe_user_data));
585 }
586#endif /* COAP_WITH_OBSERVE_PERSIST */
587 }
588 assert(resource->context == NULL);
589 resource->context = context;
590}
591
592COAP_API int
594 int ret;
595
596 (void)context;
597 if (!resource)
598 return 0;
599
600 coap_lock_lock(return 0);
601 ret = coap_delete_resource_lkd(resource);
603 return ret;
604}
605
606/*
607 * Input context is ignored, but param left there to keep API consistent
608 */
609int
610coap_delete_resource_lkd(coap_resource_t *resource) {
611 coap_context_t *context;
612 coap_deleting_resource_t deleting;
613
614 if (!resource)
615 return 0;
616
617 context = resource->context;
619
620 if (resource->ref) {
621 resource->ref--;
622 return 1;
623 }
624 if (context && context->context_going_away) {
625 deleting = COAP_DELETING_RESOURCE_ON_EXIT;
626 } else {
627 deleting = COAP_DELETING_RESOURCE;
628 }
629 if (resource->is_unknown) {
630 if (context && context->unknown_resource == resource) {
631 context->unknown_resource = NULL;
632 }
633 } else if (resource->is_proxy_uri) {
634 if (context && context->proxy_uri_resource == resource) {
635 context->proxy_uri_resource = NULL;
636 }
637 } else if (context) {
638 /* remove resource from list */
639 RESOURCES_DELETE(context->resources, resource);
640 }
641 if (resource->is_dynamic) {
642 if (context) {
643 assert(context->dynamic_cur);
644 context->dynamic_cur--;
645 }
646 }
647
648 /* and free its allocated memory */
649 coap_free_resource(resource, deleting);
650
651 return 1;
652}
653
654void
655coap_delete_all_resources(coap_context_t *context) {
657 coap_resource_t *tmp;
658
659 RESOURCE_ITER_SAFE(context->resources, r, tmp) {
660 coap_delete_resource_lkd(r);
661 }
662
663 context->resources = NULL;
664
665 if (context->unknown_resource) {
666 coap_delete_resource_lkd(context->unknown_resource);
667 context->unknown_resource = NULL;
668 }
669 if (context->proxy_uri_resource) {
670 coap_delete_resource_lkd(context->proxy_uri_resource);
671 context->proxy_uri_resource = NULL;
672 }
673}
674
677 coap_resource_t *result;
678
679 coap_lock_lock(return NULL);
680 result = coap_get_resource_from_uri_path_lkd(context, uri_path);
682
683 return result;
684}
685
687coap_get_resource_from_uri_path_lkd(coap_context_t *context,
688 coap_str_const_t *uri_path) {
689 coap_resource_t *result;
690
692
693 RESOURCES_FIND(context->resources, uri_path, result);
694
695 return result;
696}
697
699coap_print_link(const coap_resource_t *resource,
700 unsigned char *buf, size_t *len, size_t *offset) {
701 unsigned char *p = buf;
702 const uint8_t *bufend = buf + *len;
703 coap_attr_t *attr;
704 coap_print_status_t result = 0;
705 coap_print_status_t output_length = 0;
706 const size_t old_offset = *offset;
707
708 *len = 0;
709 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
710 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
711
712 COPY_COND_WITH_OFFSET(p, bufend, *offset,
713 resource->uri_path->s, resource->uri_path->length, *len);
714
715 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
716
717 LL_FOREACH(resource->link_attr, attr) {
718
719 PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
720
721 COPY_COND_WITH_OFFSET(p, bufend, *offset,
722 attr->name->s, attr->name->length, *len);
723
724 if (attr->value && attr->value->s) {
725 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
726
727 COPY_COND_WITH_OFFSET(p, bufend, *offset,
728 attr->value->s, attr->value->length, *len);
729 }
730
731 }
732 if (resource->observable) {
733 COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
734 }
735
736#if COAP_OSCORE_SUPPORT
737 /* If oscore is enabled */
738 if (resource->flags & COAP_RESOURCE_FLAGS_OSCORE_ONLY)
739 COPY_COND_WITH_OFFSET(p, bufend, *offset, ";osc", 4, *len);
740#endif /* COAP_OSCORE_SUPPORT */
741
742 output_length = (coap_print_status_t)(p - buf);
743
744 if (output_length > COAP_PRINT_STATUS_MAX) {
746 }
747
748 result = (coap_print_status_t)output_length;
749
750 if (result + old_offset - *offset < *len) {
751 result |= COAP_PRINT_STATUS_TRUNC;
752 }
753
754 return result;
755}
756
757void
759 coap_request_t method,
760 coap_method_handler_t handler) {
761 coap_register_request_handler(resource, method, handler);
762}
763
764void
766 coap_request_t method,
767 coap_method_handler_t handler) {
768 assert(resource);
769 assert(method > 0 && (size_t)(method-1) <
770 sizeof(resource->handler)/sizeof(coap_method_handler_t));
771 resource->handler[method-1] = handler;
772}
773
775coap_find_observer(coap_resource_t *resource, coap_session_t *session,
776 const coap_bin_const_t *token) {
778
779 assert(resource);
780 assert(session);
781
782 LL_FOREACH(resource->subscribers, s) {
783 if (s->session == session &&
784 (!token || coap_binary_equal(token, &s->pdu->actual_token)))
785 return s;
786 }
787
788 return NULL;
789}
790
791static coap_subscription_t *
792coap_find_observer_cache_key(coap_resource_t *resource, coap_session_t *session,
793 const coap_cache_key_t *cache_key) {
795
796 assert(resource);
797 assert(session);
798
799 LL_FOREACH(resource->subscribers, s) {
800 if (s->session == session
801 && (memcmp(cache_key, s->cache_key, sizeof(coap_cache_key_t)) == 0))
802 return s;
803 }
804
805 return NULL;
806}
807
808/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
809static const uint16_t cache_ignore_options[] = { COAP_OPTION_ETAG,
811 };
813coap_add_observer(coap_resource_t *resource,
814 coap_session_t *session,
815 const coap_bin_const_t *token,
816 const coap_pdu_t *request) {
818 coap_cache_key_t *cache_key = NULL;
819 size_t len;
820 const uint8_t *data;
821
822 assert(session);
823
824 /* Check if there is already a subscription for this peer. */
825 s = coap_find_observer(resource, session, token);
826 if (!s) {
827 /*
828 * Cannot allow a duplicate to be created for the same query as application
829 * may not be cleaning up duplicates. If duplicate found, then original
830 * observer is deleted and a new one created with the new token
831 */
832 cache_key = coap_cache_derive_key_w_ignore(session, request,
834 cache_ignore_options,
835 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
836 if (cache_key) {
837 s = coap_find_observer_cache_key(resource, session, cache_key);
838 if (s) {
839 /* Delete old entry with old token */
840 coap_delete_observer(resource, session, &s->pdu->actual_token);
841 s = NULL;
842 }
843 }
844 }
845
846 /* We are done if subscription was found. */
847 if (s) {
848 return s;
849 }
850
851 /* Check if there is already maximum number of subscribers present */
852#if (COAP_RESOURCE_MAX_SUBSCRIBER > 0)
853 uint32_t subscriber_count = 0;
854 LL_COUNT(resource->subscribers, s, subscriber_count);
855 if (subscriber_count >= COAP_RESOURCE_MAX_SUBSCRIBER) {
856 return NULL; /* Signal error */
857 }
858#endif /* COAP_RESOURCE_MAX_SUBSCRIBER */
859
860 /* Create a new subscription */
862
863 if (!s) {
864 coap_delete_cache_key(cache_key);
865 return NULL;
866 }
867
868 coap_subscription_init(s);
869 s->pdu = coap_pdu_duplicate_lkd(request, session, token->length,
870 token->s, NULL, COAP_BOOL_FALSE);
871 if (s->pdu == NULL) {
872 coap_delete_cache_key(cache_key);
874 return NULL;
875 }
876 if (coap_get_data(request, &len, &data)) {
877 /* This could be a large bodied FETCH */
878 s->pdu->max_size = 0;
879 coap_add_data(s->pdu, len, data);
880 }
881 if (cache_key == NULL) {
882 cache_key = coap_cache_derive_key_w_ignore(session, request,
884 cache_ignore_options,
885 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
886 if (cache_key == NULL) {
887 coap_delete_pdu_lkd(s->pdu);
888 coap_delete_cache_key(cache_key);
890 return NULL;
891 }
892 }
893 s->cache_key = cache_key;
894 s->session = coap_session_reference_lkd(session);
895 session->ref_subscriptions++;
896
897 /* add subscriber to resource */
898 LL_PREPEND(resource->subscribers, s);
899
900 coap_log_debug("create new subscription %p key 0x%02x%02x%02x%02x\n",
901 (void *)s, s->cache_key->key[0], s->cache_key->key[1],
902 s->cache_key->key[2], s->cache_key->key[3]);
903
904 if (session->context->observe_added_cb && session->proto == COAP_PROTO_UDP &&
905 !coap_is_af_unix(&session->addr_info.local)) {
906 coap_bin_const_t raw_packet;
907 coap_bin_const_t *oscore_info = NULL;
908#if COAP_OSCORE_SUPPORT
909 oscore_association_t *association;
910
911 if (session->recipient_ctx && session->recipient_ctx->recipient_id) {
912 /*
913 * Need to track the association used for tracking this observe, done as
914 * a CBOR array. Read in coap_persist_observe_add().
915 *
916 * If an entry is null, then use nil, else a set of bytes
917 *
918 * Currently tracking 5 items
919 * recipient_id
920 * id_context
921 * aad (from oscore_association_t)
922 * partial_iv (from oscore_association_t)
923 * nonce (from oscore_association_t)
924 */
925 uint8_t info_buffer[60];
926 uint8_t *info_buf = info_buffer;
927 size_t info_len = sizeof(info_buffer);
928 size_t ret = 0;
929 coap_bin_const_t ctoken = { token->length, token->s };
930
931 ret += oscore_cbor_put_array(&info_buf, &info_len, 5);
932 ret += oscore_cbor_put_bytes(&info_buf,
933 &info_len,
934 session->recipient_ctx->recipient_id->s,
935 session->recipient_ctx->recipient_id->length);
936 if (session->recipient_ctx->osc_ctx &&
937 session->recipient_ctx->osc_ctx->id_context) {
938 ret += oscore_cbor_put_bytes(&info_buf,
939 &info_len,
940 session->recipient_ctx->osc_ctx->id_context->s,
941 session->recipient_ctx->osc_ctx->id_context->length);
942 } else {
943 ret += oscore_cbor_put_nil(&info_buf, &info_len);
944 }
945 association = oscore_find_association(session, &ctoken);
946 if (association) {
947 if (association->aad) {
948 ret += oscore_cbor_put_bytes(&info_buf,
949 &info_len,
950 association->aad->s,
951 association->aad->length);
952 } else {
953 ret += oscore_cbor_put_nil(&info_buf, &info_len);
954 }
955 if (association->partial_iv) {
956 ret += oscore_cbor_put_bytes(&info_buf,
957 &info_len,
958 association->partial_iv->s,
959 association->partial_iv->length);
960 } else {
961 ret += oscore_cbor_put_nil(&info_buf, &info_len);
962 }
963 if (association->nonce) {
964 ret += oscore_cbor_put_bytes(&info_buf,
965 &info_len,
966 association->nonce->s,
967 association->nonce->length);
968 } else {
969 ret += oscore_cbor_put_nil(&info_buf, &info_len);
970 }
971 } else {
972 ret += oscore_cbor_put_nil(&info_buf, &info_len);
973 ret += oscore_cbor_put_nil(&info_buf, &info_len);
974 }
975 if (ret > sizeof(info_buffer)) {
976 /* Should have been caught by assert() inoscborput_* functions */
977 coap_log_warn("coap_add_observer overrun of info_buffer (%" PRIuS ")\n", ret);
978 ret = sizeof(info_buffer);
979 }
980 oscore_info = coap_new_bin_const(info_buffer, ret);
981 }
982#endif /* COAP_OSCORE_SUPPORT */
983
984 /* s->pdu header is not currently encoded */
985 memcpy(s->pdu->token - request->hdr_size,
986 request->token - request->hdr_size, request->hdr_size);
987 raw_packet.s = s->pdu->token - request->hdr_size;
988 raw_packet.length = s->pdu->used_size + request->hdr_size;
989 coap_lock_callback(session->context->observe_added_cb(session, s, session->proto,
990 &session->endpoint->bind_addr,
991 &session->addr_info,
992 &raw_packet,
993 oscore_info,
994 session->context->observe_user_data));
995#if COAP_OSCORE_SUPPORT
996 coap_delete_bin_const(oscore_info);
997#endif /* COAP_OSCORE_SUPPORT */
998 }
999 if (resource->context->track_observe_value_cb) {
1000 /* Track last used observe value (as app handler is called) */
1001 coap_lock_callback(resource->context->track_observe_value_cb(resource->context,resource->uri_path,
1002 resource->observe,
1003 resource->context->observe_user_data));
1004 }
1005
1006 return s;
1007}
1008
1009void
1010coap_touch_observer(coap_context_t *context, coap_session_t *session,
1011 const coap_bin_const_t *token) {
1013
1014 RESOURCES_ITER(context->resources, r) {
1015 s = coap_find_observer(r, session, token);
1016 if (s) {
1017 s->fail_cnt = 0;
1018 }
1019 }
1020}
1021
1022void
1023coap_delete_observer_internal(coap_resource_t *resource, coap_session_t *session,
1025 if (!s)
1026 return;
1027
1029 char outbuf[2 * 8 + 1] = "";
1030 unsigned int i;
1031 coap_string_t *uri_path;
1032 coap_string_t *uri_query;
1033
1034 for (i = 0; i < s->pdu->actual_token.length; i++) {
1035 size_t size = strlen(outbuf);
1036
1037 snprintf(&outbuf[size], sizeof(outbuf)-size, "%02x",
1038 s->pdu->actual_token.s[i]);
1039 }
1040 uri_path = coap_get_uri_path(s->pdu);
1041 uri_query = coap_get_query(s->pdu);
1042 coap_log_debug("removed subscription '/%*.*s%s%*.*s' (%p) with token '%s' key 0x%02x%02x%02x%02x\n",
1043 uri_path ? (int)uri_path->length : 0, uri_path ? (int)uri_path->length : 0,
1044 uri_path ? (char *)uri_path->s : "",
1045 uri_query ? "?" : "",
1046 uri_query ? (int)uri_query->length : 0, uri_query ? (int)uri_query->length : 0,
1047 uri_query ? (char *)uri_query->s : "",
1048 (void *)s, outbuf, s->cache_key->key[0], s->cache_key->key[1],
1049 s->cache_key->key[2], s-> cache_key->key[3]);
1050 coap_delete_string(uri_path);
1051 coap_delete_string(uri_query);
1052 }
1053 if (session->context->observe_deleted_cb)
1054 coap_lock_callback(session->context->observe_deleted_cb(session, s,
1055 session->context->observe_user_data));
1056
1057 if (resource->subscribers) {
1058 LL_DELETE(resource->subscribers, s);
1059 assert(session->ref_subscriptions > 0);
1060 session->ref_subscriptions--;
1061 coap_session_release_lkd(session);
1062 coap_delete_pdu_lkd(s->pdu);
1063 coap_delete_cache_key(s->cache_key);
1065 }
1066
1067 return;
1068}
1069
1070int
1071coap_delete_observer(coap_resource_t *resource, coap_session_t *session,
1072 const coap_bin_const_t *token) {
1074
1075 s = coap_find_observer(resource, session, token);
1076 if (s)
1077 coap_delete_observer_internal(resource, session, s);
1078
1079 return s != NULL;
1080}
1081
1082int
1083coap_delete_observer_request(coap_resource_t *resource, coap_session_t *session,
1084 const coap_bin_const_t *token, coap_pdu_t *request) {
1086 int ret = 0;
1087
1088 s = coap_find_observer(resource, session, token);
1089 if (!s) {
1090 /*
1091 * It is possible that the client is using the wrong token.
1092 * An example being a large FETCH spanning multiple blocks.
1093 */
1094 coap_cache_key_t *cache_key;
1095
1096 cache_key = coap_cache_derive_key_w_ignore(session, request,
1098 cache_ignore_options,
1099 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
1100 if (cache_key) {
1101 s = coap_find_observer_cache_key(resource, session, cache_key);
1102 if (s) {
1103 /* Delete entry with setup token */
1104 ret = coap_delete_observer(resource, session, &s->pdu->actual_token);
1105 }
1106 coap_delete_cache_key(cache_key);
1107 }
1108 } else {
1109 coap_delete_observer_internal(resource, session, s);
1110 ret = 1;
1111 }
1112 return ret;
1113}
1114
1115void
1116coap_delete_observers(coap_context_t *context, coap_session_t *session) {
1117 RESOURCES_ITER(context->resources, resource) {
1118 coap_subscription_t *s, *tmp;
1119 LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
1120 if (s->session == session) {
1121 if (context->observe_deleted_cb)
1122 coap_lock_callback(context->observe_deleted_cb(session, s, context->observe_user_data));
1123 assert(resource->subscribers);
1124 LL_DELETE(resource->subscribers, s);
1125 coap_session_release_lkd(session);
1126 coap_delete_pdu_lkd(s->pdu);
1127 coap_delete_cache_key(s->cache_key);
1129 }
1130 }
1131 }
1132}
1133
1134static void
1135coap_notify_observers(coap_context_t *context, coap_resource_t *r,
1136 coap_deleting_resource_t deleting) {
1138 coap_subscription_t *obs, *otmp;
1139 coap_pdu_t *response;
1140 uint8_t buf[4];
1141 coap_string_t *query;
1142 coap_block_b_t block;
1143 coap_tick_t now;
1144
1146
1147 if (r->observable && (r->dirty || r->partiallydirty)) {
1148 if (r->list_being_traversed)
1149 return;
1150 r->list_being_traversed = 1;
1151
1152 coap_resource_reference_lkd(r);
1153
1154 r->partiallydirty = 0;
1155
1156 LL_FOREACH_SAFE(r->subscribers, obs, otmp) {
1157 coap_session_t *obs_session;
1158 coap_pdu_t *obs_pdu;
1160
1161 if ((r->dirty == 0 && obs->dirty == 0) || obs->session->is_rate_limiting) {
1162 /*
1163 * running this resource due to partiallydirty, but this observation's
1164 * notification was already enqueued
1165 */
1166 context->observe_pending = 1;
1167 continue;
1168 }
1169
1170 /*
1171 * obs may get deleted in the callback, or by another running
1172 * thread when executing the callback or when sending a response.
1173 */
1174 obs_session = obs->session;
1175 obs_pdu = obs->pdu;
1176 coap_session_reference_lkd(obs_session);
1177 coap_pdu_reference_lkd(obs_pdu);
1178
1179 if (obs->session->con_active >= COAP_NSTART(obs->session) &&
1180 ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) ||
1181 (obs->non_cnt >= COAP_OBS_MAX_NON))) {
1182 /* Waiting for the previous unsolicited response to finish */
1183 goto next_one_fail;
1184 }
1185 coap_ticks(&now);
1186 if (obs->session->lg_xmit && obs->session->lg_xmit->last_all_sent == 0 &&
1187 obs->session->lg_xmit->last_obs &&
1188 (obs->session->lg_xmit->last_obs + 2*COAP_TICKS_PER_SECOND) > now) {
1189 /* Waiting for the previous blocked unsolicited response to finish */
1190 goto next_one_fail;
1191 }
1192
1193 obs->dirty = 0;
1194 /* initialize response */
1195 response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0,
1196 coap_session_max_pdu_size_lkd(obs->session));
1197 if (!response) {
1198 coap_log_debug("coap_check_notify: pdu init failed, resource stays "
1199 "partially dirty\n");
1200 goto next_one_fail_no_pending;
1201 }
1202
1203 if (!coap_add_token(response, obs->pdu->actual_token.length,
1204 obs->pdu->actual_token.s)) {
1205 coap_log_debug("coap_check_notify: cannot add token, resource stays "
1206 "partially dirty\n");
1207 coap_delete_pdu_lkd(response);
1208 goto next_one_fail_no_pending;
1209 }
1210
1211 obs->pdu->mid = response->mid = coap_new_message_id_lkd(obs->session);
1212 /* A lot of the reliable code assumes type is CON */
1213 if (COAP_PROTO_NOT_RELIABLE(obs->session->proto) &&
1214 (r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 &&
1216 obs->non_cnt < COAP_OBS_MAX_NON)) {
1217 response->type = COAP_MESSAGE_NON;
1218 } else {
1219 response->type = COAP_MESSAGE_CON;
1220 }
1221 switch (deleting) {
1222 case COAP_NOT_DELETING_RESOURCE:
1223 /* fill with observer-specific data */
1225 coap_encode_var_safe(buf, sizeof(buf),
1226 r->observe),
1227 buf);
1228 if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_BLOCK2,
1229 &block)) {
1230 /* Will get updated later (e.g. M bit) if appropriate */
1232 coap_encode_var_safe(buf, sizeof(buf),
1233 ((0 << 4) |
1234 (0 << 3) |
1235 block.aszx)),
1236 buf);
1237 }
1238#if COAP_Q_BLOCK_SUPPORT
1239 else if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_Q_BLOCK2,
1240 &block)) {
1241 /* Will get updated later (e.g. M bit) if appropriate */
1243 coap_encode_var_safe(buf, sizeof(buf),
1244 ((0 << 4) |
1245 (0 << 3) |
1246 block.szx)),
1247 buf);
1248 }
1249#endif /* COAP_Q_BLOCK_SUPPORT */
1250
1251 h = r->handler[obs->pdu->code - 1];
1252 assert(h); /* we do not allow subscriptions if no
1253 * GET/FETCH handler is defined */
1254 query = coap_get_query(obs->pdu);
1255 coap_log_debug("Observe PDU presented to app.\n");
1256 coap_show_pdu(COAP_LOG_DEBUG, obs->pdu);
1257 coap_log_debug("call custom handler for resource '%*.*s' (4)\n",
1258 (int)r->uri_path->length, (int)r->uri_path->length,
1259 r->uri_path->s);
1260
1261 /* obs may get deleted during callback (potentially by another thread) */
1262 if (r->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) {
1263 coap_lock_callback_release(h(r, obs->session, obs->pdu, query, response),
1264 /* context is being freed off */
1265 coap_delete_string(query);
1266 coap_delete_pdu_lkd(response);
1267 coap_session_release_lkd(obs_session);
1268 coap_pdu_release_lkd(obs_pdu);
1269 r->list_being_traversed = 0;
1270 coap_resource_release_lkd(r);
1271 return);
1272 } else {
1274 h(r, obs->session, obs->pdu, query, response),
1275 /* context is being freed off */
1276 coap_delete_string(query);
1277 coap_delete_pdu_lkd(response);
1278 coap_session_release_lkd(obs_session);
1279 coap_pdu_release_lkd(obs_pdu);
1280 r->list_being_traversed = 0;
1281 coap_resource_release_lkd(r);
1282 return);
1283 }
1284
1285 /* Check validity of response code */
1286 if (!coap_check_code_class(obs_session, response)) {
1287 coap_log_warn("handle_request: Invalid PDU response code (%d.%02d)\n",
1288 COAP_RESPONSE_CLASS(response->code),
1289 response->code & 0x1f);
1290 coap_delete_string(query);
1291 coap_delete_pdu_lkd(response);
1292 coap_session_release_lkd(obs_session);
1293 coap_pdu_release_lkd(obs_pdu);
1294 r->list_being_traversed = 0;
1295 coap_resource_release_lkd(r);
1296 return;
1297 }
1298
1299 /* Check if lg_xmit generated and update PDU code if so */
1300 coap_check_code_lg_xmit(obs_session, obs_pdu, response, r, query);
1301 coap_delete_string(query);
1302 if (COAP_RESPONSE_CLASS(response->code) != 2) {
1304 }
1305 if (COAP_RESPONSE_CLASS(response->code) > 2) {
1306 coap_delete_observer(r, obs_session, &obs_pdu->actual_token);
1307 obs = NULL;
1308 }
1309 break;
1310 case COAP_DELETING_RESOURCE_ON_EXIT:
1311 /* Don't worry if it does not get there */
1312 response->type = COAP_MESSAGE_NON;
1313 response->code = COAP_RESPONSE_CODE(503);
1315 coap_encode_var_safe(buf, sizeof(buf),
1316 30),
1317 buf);
1318 break;
1319 case COAP_DELETING_RESOURCE:
1320 default:
1321 /* Don't worry if it does not get there */
1322 response->type = COAP_MESSAGE_NON;
1323 response->code = COAP_RESPONSE_CODE(404);
1324 break;
1325 }
1326
1327 if (obs) {
1329 /*
1330 * obs may have been deleted in the callback, or by another running
1331 * thread when executing the callback.
1332 */
1333 LL_FOREACH(r->subscribers, s) {
1334 if (s == obs) {
1335 break;
1336 }
1337 }
1338 if (s == NULL)
1339 obs = NULL;
1340 }
1341 if (obs) {
1342 if (response->type == COAP_MESSAGE_CON ||
1344 obs->non_cnt = 0;
1345 } else {
1346 obs->non_cnt++;
1347 }
1348
1349#if COAP_Q_BLOCK_SUPPORT
1350 if (response->code == COAP_RESPONSE_CODE(205) &&
1351 coap_get_block_b(obs_session, response, COAP_OPTION_Q_BLOCK2,
1352 &block) &&
1353 block.m) {
1354 query = coap_get_query(obs_pdu);
1355 mid = coap_send_q_block2(obs_session, r, query, obs_pdu->code,
1356 block, response, 1);
1357 coap_delete_string(query);
1358 goto finish;
1359 }
1360#endif /* COAP_Q_BLOCK_SUPPORT */
1361 }
1362 mid = coap_send_internal(obs_session, response, NULL);
1363
1364#if COAP_Q_BLOCK_SUPPORT
1365finish:
1366#endif /* COAP_Q_BLOCK_SUPPORT */
1367 if (COAP_INVALID_MID == mid) {
1368 coap_log_debug("* %s: coap_check_notify: sending failed, resource stays "
1369 "partially dirty\n", coap_session_str(obs_session));
1370 if (obs) {
1372 /*
1373 * obs may have been deleted in coap_send_internal() or
1374 * coap_send_q_block2().
1375 */
1376 LL_FOREACH(r->subscribers, s) {
1377 if (s == obs) {
1378 break;
1379 }
1380 }
1381 if (s == NULL)
1382 obs = NULL;
1383 }
1384 if (obs)
1385 obs->dirty = 1;
1386 r->partiallydirty = 1;
1387 }
1388 goto cleanup;
1389
1390next_one_fail:
1391 context->observe_pending = 1;
1392next_one_fail_no_pending:
1393 r->partiallydirty = 1;
1394 if (obs)
1395 obs->dirty = 1;
1396cleanup:
1397 coap_session_release_lkd(obs_session);
1398 coap_pdu_release_lkd(obs_pdu);
1399 }
1400 r->list_being_traversed = 0;
1401 r->dirty = 0;
1402 coap_resource_release_lkd(r);
1403 /* r may be no more if elsewhere coap_free_resource() has been called */
1404 } else {
1405 r->dirty = 0;
1406 }
1407}
1408
1409COAP_API int
1411 int ret;
1412 (void)query;
1413
1414 coap_lock_lock(return 0);
1415 ret = coap_resource_notify_observers_lkd(r, COAP_NOT_DELETING_RESOURCE);
1417 return ret;
1418}
1419
1420COAP_API int
1422 const coap_string_t *query) {
1423 int ret;
1424
1425 (void)query;
1426 coap_lock_lock(return 0);
1427 ret = coap_resource_notify_observers_lkd(r, COAP_NOT_DELETING_RESOURCE);
1429 return ret;
1430}
1431
1432int
1433coap_resource_notify_observers_lkd(coap_resource_t *r,
1434 coap_deleting_resource_t deleting) {
1436 if (!r->observable)
1437 return 0;
1438 if (!r->subscribers)
1439 return 0;
1440 r->dirty = 1;
1441
1442 /* Increment value for next Observe use. Observe value must be < 2^24 */
1443 r->observe = (r->observe + 1) & 0xFFFFFF;
1444
1445 assert(r->context);
1446
1447 if (r->context->track_observe_value_cb) {
1448 /* Track last used observe value */
1449 if ((r->observe % r->context->observe_save_freq) == 0)
1450 coap_lock_callback(r->context->track_observe_value_cb(r->context, r->uri_path,
1451 r->observe,
1452 r->context->observe_user_data));
1453 }
1454
1455 coap_notify_observers(r->context, r, deleting);
1456 return 1;
1457}
1458
1459void
1460coap_resource_set_mode(coap_resource_t *resource, int mode) {
1461 resource->flags = (resource->flags &
1464}
1465
1466void
1467coap_resource_set_userdata(coap_resource_t *resource, void *data) {
1468 resource->user_data = data;
1469}
1470
1471void *
1473 return resource->user_data;
1474}
1475
1476void
1479 context->release_userdata_cb = callback;
1480}
1481
1482void
1484 if (resource->is_unknown || resource->is_proxy_uri) {
1485 /* We cannot observe these */
1486 coap_log_debug("coap_resource_set_get_observable: Not supported for Unknown or Proxy URIs\n");
1487 resource->observable = 0;
1488 } else {
1489 resource->observable = mode ? 1 : 0;
1490 }
1491}
1492
1495 if (resource)
1496 return resource->uri_path;
1497 return NULL;
1498}
1499
1500COAP_API void
1502 coap_lock_lock(return);
1503 coap_check_notify_lkd(context);
1505}
1506
1507void
1508coap_check_notify_lkd(coap_context_t *context) {
1509
1511 if (context->observe_pending) {
1512 context->observe_pending = 0;
1513 RESOURCES_ITER(context->resources, r) {
1514 coap_notify_observers(context, r, COAP_NOT_DELETING_RESOURCE);
1515 }
1516 }
1517}
1518
1519void
1521 uint32_t start_observe_no) {
1522 if (!resource)
1523 return;
1524
1525 resource->observe = start_observe_no & 0xffffff;
1526}
1527
1538static void
1539coap_remove_failed_observers(coap_context_t *context,
1540 coap_resource_t *resource,
1541 coap_session_t *session,
1542 const coap_bin_const_t *token) {
1543 coap_subscription_t *obs, *otmp;
1544
1545 LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
1546 if (obs->session == session &&
1547 coap_binary_equal(token, &obs->pdu->actual_token)) {
1548 /* count failed notifies and remove when
1549 * COAP_OBS_MAX_FAIL is reached */
1550 obs->fail_cnt++;
1551 if (obs->fail_cnt >= COAP_OBS_MAX_FAIL) {
1552 coap_cancel_all_messages(context, obs->session,
1553 &obs->pdu->actual_token);
1554 coap_delete_observer(resource, session, token);
1555 }
1556 break; /* break loop if observer was found */
1557 }
1558 }
1559}
1560
1561void
1562coap_handle_failed_notify(coap_context_t *context,
1563 coap_session_t *session,
1564 const coap_bin_const_t *token) {
1565
1566 RESOURCES_ITER(context->resources, r) {
1567 coap_remove_failed_observers(context, r, session, token);
1568 }
1569}
1570
1571void
1572coap_resource_reference_lkd(coap_resource_t *resource) {
1573 resource->ref++;
1574}
1575#endif /* ! COAP_SERVER_SUPPORT */
int coap_is_af_unix(const coap_address_t *a)
Checks if given address a denotes a AF_UNIX address.
struct coap_cache_key_t coap_cache_key_t
struct coap_attr_t coap_attr_t
struct coap_subscription_t coap_subscription_t
struct coap_resource_t coap_resource_t
#define PRIuS
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_RESOURCE
Definition coap_mem.h:42
@ COAP_RESOURCEATTR
Definition coap_mem.h:43
@ COAP_SUBSCRIPTION
Definition coap_mem.h:53
@ COAP_STRING
Definition coap_mem.h:33
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().
#define NULL
Definition coap_option.h:30
COAP_DEPRECATED COAP_API int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
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_l...
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 coap_block.c:64
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:41
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
COAP_API 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_RESOURCE_FLAGS_NOTIFY_NON
Observe Notifications will be sent non-confirmable by default.
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
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.
#define COAP_RESOURCE_HANDLE_WELLKNOWN_CORE
Define this when invoking coap_resource_unknown_init2() if .well-known/core is to be passed to the un...
#define COAP_ATTR_FLAGS_RELEASE_NAME
void coap_resource_set_mode(coap_resource_t *resource, int mode)
Sets the notification message type of resource resource to given mode.
#define COAP_RESOURCE_SAFE_REQUEST_HANDLER
Don't lock this resource when calling app call-back handler for requests as handler will not be manip...
#define COAP_PRINT_STATUS_TRUNC
void(* coap_method_handler_t)(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response)
Definition of message handler function.
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_OSCORE_ONLY
Define this resource as an OSCORE enabled access only.
COAP_API coap_print_status_t coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, size_t offset, const coap_string_t *query_filter)
Prints the names of all known resources for context to buf.
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Observe Notifications will be sent confirmable.
coap_resource_t * coap_resource_reverse_proxy_init(coap_method_handler_t handler, int flags)
Creates a new resource object for the reverse-proxy resource handler with control over multicast requ...
COAP_API int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
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.
#define COAP_PRINT_STATUS_MAX
void(* coap_resource_release_userdata_handler_t)(void *user_data)
Definition of release resource user_data callback function.
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.
uint32_t coap_print_status_t
Status word to encode the result of conditional print or copy operations such as coap_print_link().
#define COAP_PRINT_STATUS_ERROR
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)
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.
COAP_API void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free'd by coap_delete_resource().
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.
#define COAP_RESOURCE_HIDE_WELLKNOWN_CORE
Hide this resource from .well-known/core.
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:2029
int coap_check_code_class(coap_session_t *session, coap_pdu_t *pdu)
Check whether the pdu contains a valid code class.
Definition coap_net.c:1444
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, coap_bin_const_t *token)
Cancels all outstanding messages for session session that have the specified token.
Definition coap_net.c:3224
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
#define coap_lock_specific_callback_release(lock, func, failed)
Dummy for no thread-safe code.
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_init(lock)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_callback_release(func, failed)
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:812
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
void coap_persist_set_observe_num(coap_resource_t *resource, uint32_t observe_num)
Sets the current observe number value.
void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
COAP_API void coap_check_notify(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
COAP_API 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...
size_t oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_bytes(uint8_t **buffer, size_t *buf_size, const uint8_t *bytes, size_t bytes_len)
size_t oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements)
oscore_association_t * oscore_find_association(coap_session_t *session, coap_bin_const_t *token)
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
Definition coap_pdu.c:1712
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:194
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition coap_pdu.c:543
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
Definition coap_pdu.c:234
COAP_STATIC_INLINE void coap_pdu_release_lkd(coap_pdu_t *pdu)
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 coap_pdu.c:839
#define COAP_OPTION_BLOCK2
Definition coap_pdu.h:141
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:267
coap_request_t
CoAP PDU Request methods.
Definition coap_pdu.h:80
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:164
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:167
#define COAP_OPTION_OSCORE
Definition coap_pdu.h:128
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition coap_pdu.c:410
#define COAP_OPTION_Q_BLOCK2
Definition coap_pdu.h:144
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 coap_pdu.c:935
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 coap_pdu.c:102
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:270
#define COAP_OPTION_MAXAGE
Definition coap_pdu.h:134
#define COAP_OPTION_ETAG
Definition coap_pdu.h:123
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:125
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
Definition coap_pdu.h:55
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 coap_pdu.c:904
@ COAP_BOOL_FALSE
Definition coap_pdu.h:378
@ COAP_REQUEST_PUT
Definition coap_pdu.h:83
@ COAP_REQUEST_DELETE
Definition coap_pdu.h:84
@ COAP_REQUEST_GET
Definition coap_pdu.h:81
@ COAP_REQUEST_FETCH
Definition coap_pdu.h:85
@ COAP_REQUEST_PATCH
Definition coap_pdu.h:86
@ COAP_REQUEST_IPATCH
Definition coap_pdu.h:87
@ COAP_REQUEST_POST
Definition coap_pdu.h:82
@ COAP_PROTO_UDP
Definition coap_pdu.h:319
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
#define COAP_NSTART(s)
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
coap_session_t * coap_session_reference_lkd(coap_session_t *session)
Increment reference counter on a session.
#define COAP_PROTO_NOT_RELIABLE(p)
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:130
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:65
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:119
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition coap_str.h:208
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 coap_str.c:55
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:50
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
Definition coap_uri.c:1182
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 coap_uri.c:1103
coap_address_t local
local address and port
Definition coap_io.h:59
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
Structure of Block options with BERT support.
Definition coap_block.h:55
unsigned int aszx
block size (0-7 including BERT
Definition coap_block.h:59
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:57
unsigned int szx
block size (0-6)
Definition coap_block.h:58
The CoAP stack's global state is stored in a coap_context_t object.
uint32_t dynamic_cur
Current number of dynamic resources.
structure for CoAP PDUs
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
coap_bin_const_t actual_token
Actual token in pdu.
coap_mid_t mid
message id, if any, in regular host byte order
coap_pdu_type_t type
message type
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
coap_context_t * context
session's context
CoAP string data definition with const data.
Definition coap_str.h:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48
CoAP string data definition.
Definition coap_str.h:39
uint8_t * s
string data
Definition coap_str.h:41
size_t length
length of string
Definition coap_str.h:40
coap_bin_const_t * partial_iv
coap_bin_const_t * aad
coap_bin_const_t * nonce