libcoap 4.3.5-develop-0a1d1cf
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
15
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
67#ifndef WITHOUT_QUERY_FILTER
68static int
69match(const coap_str_const_t *text, const coap_str_const_t *pattern,
70 int match_prefix) {
71 const uint8_t *next_token = text->s;
72 size_t remaining_length = text->length;
73 assert(text);
74 assert(pattern);
75
76 if (text->length < pattern->length || !pattern->s)
77 return 0;
78
79 while (remaining_length) {
80 size_t token_length;
81 const uint8_t *token = next_token;
82 next_token = (const 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 (token_length >= pattern->length &&
94 (match_prefix || pattern->length == token_length) &&
95 memcmp(token, pattern->s, pattern->length) == 0)
96 return 1;
97 }
98 return 0;
99}
100#endif /* WITHOUT_QUERY_FILTER */
101
103coap_print_wellknown(coap_context_t *context, unsigned char *buf,
104 size_t *buflen, size_t offset,
105 const coap_string_t *query_filter) {
106 coap_print_status_t result;
108 result = coap_print_wellknown_lkd(context, buf, buflen, offset, query_filter);
110 return result;
111}
112
113static coap_str_const_t coap_default_uri_wellknown = {
115 (const uint8_t *)COAP_DEFAULT_URI_WELLKNOWN
116};
117
119coap_print_wellknown_lkd(coap_context_t *context, unsigned char *buf,
120 size_t *buflen, size_t offset,
121 const coap_string_t *query_filterm) {
122 coap_print_status_t output_length = 0;
123 unsigned char *p = buf;
124 const uint8_t *bufend = buf + *buflen;
125 size_t left, written = 0;
126 coap_print_status_t result;
127 const size_t old_offset = offset;
128 int subsequent_resource = 0;
129#ifdef WITHOUT_QUERY_FILTER
130 (void)query_filterm;
131#endif /* WITHOUT_QUERY_FILTER */
132
134#ifndef WITHOUT_QUERY_FILTER
135 /* split query filter, if any */
136 if (query_filterm) {
137 unsigned char *next_query = (unsigned char *)memchr(query_filterm->s, '&', query_filterm->length);
138 size_t query_filterm_rem;
139 coap_string_t query_filter;
140
141 /* There could be multiple queries */
142 query_filter.s = query_filterm->s;
143 if (next_query) {
144 query_filter.length = next_query - query_filter.s;
145 next_query++;
146 query_filterm_rem = query_filterm->length - query_filter.length - 1;
147 } else {
148 query_filter.length = query_filterm->length;
149 query_filterm_rem = 0;
150 }
151
152 while (query_filter.length) {
153 coap_str_const_t resource_param = { 0, NULL }, query_pattern = { 0, NULL };
154 /*
155 * If query starts with href=, then MATCH_URI is set in flags.
156 * If query value contains a '*', then MATCH_PREFIX is set in flags to
157 * match up to the *.
158 */
159 int flags = 0; /* MATCH_PREFIX, MATCH_URI */
160#define MATCH_URI 0x01
161#define MATCH_PREFIX 0x02
162
163 resource_param.s = query_filter.s;
164 while (resource_param.length < query_filter.length &&
165 resource_param.s[resource_param.length] != '=')
166 resource_param.length++;
167
168 if (resource_param.length < query_filter.length) {
169 if (resource_param.length == 4 &&
170 memcmp(resource_param.s, "href", 4) == 0)
171 flags |= MATCH_URI;
172
173 /* rest is query-pattern (resource may have multiple values to match */
174 query_pattern.s =
175 query_filter.s + resource_param.length + 1;
176
177 assert((resource_param.length + 1) <= query_filter.length);
178 query_pattern.length =
179 query_filter.length - (resource_param.length + 1);
180
181 if (query_pattern.length &&
182 (query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
183 query_pattern.s++;
184 query_pattern.length--;
185 }
186
187 if (query_pattern.length &&
188 query_pattern.s[query_pattern.length-1] == '*') {
189 query_pattern.length--;
190 flags |= MATCH_PREFIX;
191 }
192 }
193
194 RESOURCES_ITER(context->resources, r) {
195
196 if (coap_string_equal(r->uri_path, &coap_default_uri_wellknown)) {
197 /* server app has defined a resource for .well-known/core - ignore */
198 continue;
199 }
200 if (r->flags & COAP_RESOURCE_HIDE_WELLKNOWN_CORE) {
201 continue;
202 }
203 if (resource_param.length) { /* there is a query filter */
204
205 if ((flags & MATCH_PREFIX) && query_pattern.length == 0) {
206 int present = (flags & MATCH_URI) ||
207 (r->observable && resource_param.length == 3 &&
208 memcmp(resource_param.s, "obs", 3) == 0) ||
209#if COAP_OSCORE_SUPPORT
210 ((r->flags & COAP_RESOURCE_FLAGS_OSCORE_ONLY) &&
211 resource_param.length == 3 && memcmp(resource_param.s, "osc", 3) == 0) ||
212#if COAP_OSCORE_GROUP_SUPPORT
213 ((r->flags & COAP_RESOURCE_FLAGS_OSCORE_ONLY) &&
214 resource_param.length == 4 && memcmp(resource_param.s, "gosc", 4) == 0) ||
215#endif /* COAP_OSCORE_GROUP_SUPPORT */
216#endif /* COAP_OSCORE_SUPPORT */
217 coap_find_attr(r, &resource_param);
218 if (!present)
219 continue;
220 } else if (flags & MATCH_URI) { /* match resource URI */
221 if (!match(r->uri_path, &query_pattern, (flags & MATCH_PREFIX) != 0))
222 continue;
223 } else { /* match attribute */
224 coap_attr_t *attr;
225 coap_str_const_t unquoted_val;
226 attr = coap_find_attr(r, &resource_param);
227 if (!attr || !attr->value)
228 continue;
229 unquoted_val = *attr->value;
230 /* if attribute has a quoted value, remove double quotes */
231 if (attr->value->length >= 2 && attr->value->s[0] == '"') {
232 unquoted_val.length -= 2;
233 unquoted_val.s += 1;
234 }
235 if (!match(&unquoted_val, &query_pattern, (flags & MATCH_PREFIX) != 0))
236 continue;
237 }
238 }
239
240 if (!subsequent_resource) { /* this is the first resource */
241 subsequent_resource = 1;
242 } else {
243 PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
244 }
245
246 left = bufend - p; /* calculate available space */
247 result = coap_print_link(r, p, &left, &offset);
248
249 if (result & COAP_PRINT_STATUS_ERROR) {
250 break;
251 }
252
253 /* coap_print_link() returns the number of characters that
254 * where actually written to p. Now advance to its end. */
255 p += COAP_PRINT_OUTPUT_LENGTH(result);
256 written += left;
257 }
258 query_filter.s = next_query;
259 if (next_query) {
260 next_query = (unsigned char *)memchr(next_query, '&', query_filterm_rem);
261 if (next_query) {
262 query_filter.length = next_query - query_filter.s;
263 next_query++;
264 query_filterm_rem = query_filterm_rem - query_filter.length - 1;
265 } else {
266 query_filter.length = query_filterm_rem;
267 query_filterm_rem = 0;
268 }
269 } else {
270 query_filter.length = query_filterm_rem;
271 query_filterm_rem = 0;
272 }
273 }
274 } else {
275#endif /* WITHOUT_QUERY_FILTER */
276 RESOURCES_ITER(context->resources, r) {
277
278 if (coap_string_equal(r->uri_path, &coap_default_uri_wellknown)) {
279 /* server app has defined a resource for .well-known/core - ignore */
280 continue;
281 }
282 if (r->flags & COAP_RESOURCE_HIDE_WELLKNOWN_CORE) {
283 continue;
284 }
285
286 if (!subsequent_resource) { /* this is the first resource */
287 subsequent_resource = 1;
288 } else {
289 PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
290 }
291
292 left = bufend - p; /* calculate available space */
293 result = coap_print_link(r, p, &left, &offset);
294
295 if (result & COAP_PRINT_STATUS_ERROR) {
296 break;
297 }
298
299 /* coap_print_link() returns the number of characters that
300 * where actually written to p. Now advance to its end. */
301 p += COAP_PRINT_OUTPUT_LENGTH(result);
302 written += left;
303 }
304#ifndef WITHOUT_QUERY_FILTER
305 }
306#endif /* WITHOUT_QUERY_FILTER */
307
308 *buflen = written;
309 output_length = (coap_print_status_t)(p - buf);
310
311 if (output_length > COAP_PRINT_STATUS_MAX) {
313 }
314
315 result = (coap_print_status_t)output_length;
316
317 if (result + old_offset - offset < *buflen) {
318 result |= COAP_PRINT_STATUS_TRUNC;
319 }
320 return result;
321}
322
323static coap_str_const_t null_path_value = {0, (const uint8_t *)""};
324static coap_str_const_t *null_path = &null_path_value;
325
327coap_resource_init(coap_str_const_t *uri_path, int flags) {
329
331 if (r) {
332 memset(r, 0, sizeof(coap_resource_t));
333#if COAP_THREAD_SAFE
334 coap_lock_init(&r->lock);
335#endif /* COAP_THREAD_SAFE */
336
337 if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) {
338 /* Need to take a copy if caller is not providing a release request */
339 if (uri_path)
340 uri_path = coap_new_str_const(uri_path->s, uri_path->length);
341 else
342 uri_path = coap_new_str_const(null_path->s, null_path->length);
343 } else if (!uri_path) {
344 /* Do not expect this, but ... */
345 uri_path = coap_new_str_const(null_path->s, null_path->length);
346 }
347
348 if (uri_path)
349 r->uri_path = uri_path;
350
351 r->flags = flags;
352 r->observe = 2;
353 } else {
354 coap_log_debug("coap_resource_init: no memory left\n");
355 }
356
357 return r;
358}
359
360static const uint8_t coap_unknown_resource_uri[] =
361 "- Unknown -";
362
366
368 if (r) {
369 memset(r, 0, sizeof(coap_resource_t));
370#if COAP_THREAD_SAFE
371 coap_lock_init(&r->lock);
372#endif /* COAP_THREAD_SAFE */
373 r->is_unknown = 1;
374 /* Something unlikely to be used, but it shows up in the logs */
375 r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1);
376 r->flags = flags & ~COAP_RESOURCE_FLAGS_RELEASE_URI;
378 } else {
379 coap_log_debug("coap_resource_unknown_init2: no memory left\n");
380 }
381
382 return r;
383}
384
387 return coap_resource_unknown_init2(put_handler, 0);
388}
389
390static const uint8_t coap_proxy_resource_uri[] =
391 "- Proxy URI -";
392
395 size_t host_name_count,
396 const char *host_name_list[], int flags) {
398
400 if (r) {
401 size_t i;
402 memset(r, 0, sizeof(coap_resource_t));
403#if COAP_THREAD_SAFE
404 coap_lock_init(&r->lock);
405#endif /* COAP_THREAD_SAFE */
406 r->is_proxy_uri = 1;
407 /* Something unlikely to be used, but it shows up in the logs */
408 r->uri_path = coap_new_str_const(coap_proxy_resource_uri, sizeof(coap_proxy_resource_uri)-1);
409 /* Preset all the handlers */
410 for (i = 0; i < (sizeof(r->handler) / sizeof(r->handler[0])); i++) {
411 r->handler[i] = handler;
412 }
413 if (host_name_count) {
414 r->proxy_name_list = coap_malloc_type(COAP_STRING, host_name_count *
415 sizeof(coap_str_const_t *));
416 if (r->proxy_name_list) {
417 for (i = 0; i < host_name_count; i++) {
418 r->proxy_name_list[i] =
419 coap_new_str_const((const uint8_t *)host_name_list[i],
420 strlen(host_name_list[i]));
421 if (!r->proxy_name_list[i]) {
422 coap_log_err("coap_resource_proxy_uri_init: unable to add host name\n");
423 if (i == 0) {
424 coap_free_type(COAP_STRING, r->proxy_name_list);
425 r->proxy_name_list = NULL;
426 }
427 break;
428 }
429 }
430 r->proxy_name_count = i;
431 }
432 }
433 r->flags = flags & ~COAP_RESOURCE_FLAGS_RELEASE_URI;
435 } else {
436 coap_log_debug("coap_resource_proxy_uri_init2: no memory left\n");
437 }
438
439 return r;
440}
441
444 size_t host_name_count, const char *host_name_list[]) {
445 return coap_resource_proxy_uri_init2(handler, host_name_count,
446 host_name_list, 0);
447}
448
449static const uint8_t coap_rev_proxy_resource_uri[] =
450 "- Rev Proxy -";
451
455
457 if (r) {
458 memset(r, 0, sizeof(coap_resource_t));
459#if COAP_THREAD_SAFE
460 coap_lock_init(&r->lock);
461#endif /* COAP_THREAD_SAFE */
462 r->is_unknown = 1;
463 r->is_reverse_proxy = 1;
464 /* Something unlikely to be used, but it shows up in the logs */
465 r->uri_path = coap_new_str_const(coap_rev_proxy_resource_uri,
466 sizeof(coap_rev_proxy_resource_uri)-1);
467 r->flags = flags & ~COAP_RESOURCE_FLAGS_RELEASE_URI;
476 } else {
477 coap_log_debug("coap_resource_rev_proxy_init: no memory left\n");
478 }
479
480 return r;
481}
482
485 coap_str_const_t *name,
486 coap_str_const_t *val,
487 int flags) {
488 coap_attr_t *attr;
489
490 if (!resource || !name)
491 return NULL;
493
494 if (attr) {
495 if (!(flags & COAP_ATTR_FLAGS_RELEASE_NAME)) {
496 /* Need to take a copy if caller is not providing a release request */
497 name = coap_new_str_const(name->s, name->length);
498 }
499 attr->name = name;
500 if (val) {
501 if (!(flags & COAP_ATTR_FLAGS_RELEASE_VALUE)) {
502 /* Need to take a copy if caller is not providing a release request */
503 val = coap_new_str_const(val->s, val->length);
504 }
505 }
506 attr->value = val;
507
508 attr->flags = flags;
509
510 /* add attribute to resource list */
511 LL_PREPEND(resource->link_attr, attr);
512 } else {
513 coap_log_debug("coap_add_attr: no memory left\n");
514 }
515
516 return attr;
517}
518
521 coap_str_const_t *name) {
522 coap_attr_t *attr;
523
524 if (!resource || !name)
525 return NULL;
526
527 LL_FOREACH(resource->link_attr, attr) {
528 if (attr->name->length == name->length &&
529 memcmp(attr->name->s, name->s, name->length) == 0)
530 return attr;
531 }
532
533 return NULL;
534}
535
538 if (attr)
539 return attr->value;
540 return NULL;
541}
542
543void
544coap_delete_attr(coap_attr_t *attr) {
545 if (!attr)
546 return;
547 coap_delete_str_const(attr->name);
548 if (attr->value) {
549 coap_delete_str_const(attr->value);
550 }
551
553}
554
555static void coap_notify_observers(coap_context_t *context, coap_resource_t *r,
556 coap_deleting_resource_t deleting);
557
558static void
559coap_free_resource(coap_resource_t *resource, coap_deleting_resource_t deleting) {
560 coap_attr_t *attr, *tmp;
561 coap_subscription_t *obs, *otmp;
562 coap_context_t *context;
563
564 assert(resource);
565
566 context = resource->context;
567 if (context) {
568 if (!context->observe_no_clear) {
569 coap_resource_notify_observers_lkd(resource, deleting);
570 coap_notify_observers(context, resource, deleting);
571 }
572
573 if (context->resource_deleted_cb)
574 coap_lock_callback(context->resource_deleted_cb(context,
575 resource->uri_path,
576 context->observe_user_data));
577
578 if (context->release_userdata_cb && resource->user_data) {
579 coap_lock_callback(context->release_userdata_cb(resource->user_data));
580 }
581 }
582
583 /* delete registered attributes */
584 LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
585
586 /* Either the application provided or libcoap copied - need to delete it */
587 coap_delete_str_const(resource->uri_path);
588
589 /* free all elements from resource->subscribers */
590 LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
591 coap_delete_observer_internal(resource, obs->session, obs);
592 }
593 if (resource->proxy_name_count && resource->proxy_name_list) {
594 size_t i;
595
596 for (i = 0; i < resource->proxy_name_count; i++) {
597 coap_delete_str_const(resource->proxy_name_list[i]);
598 }
599 coap_free_type(COAP_STRING, resource->proxy_name_list);
600 }
601
602 coap_free_type(COAP_RESOURCE, resource);
603}
604
605COAP_API void
607 coap_lock_lock(return);
608 coap_add_resource_lkd(context, resource);
610}
611
612void
613coap_add_resource_lkd(coap_context_t *context, coap_resource_t *resource) {
615 if (resource->is_unknown) {
616 if (context->unknown_resource)
617 coap_free_resource(context->unknown_resource, COAP_DELETING_RESOURCE);
618 context->unknown_resource = resource;
619 } else if (resource->is_proxy_uri) {
620 if (context->proxy_uri_resource)
621 coap_free_resource(context->proxy_uri_resource, COAP_DELETING_RESOURCE);
622 context->proxy_uri_resource = resource;
623 } else {
624 coap_resource_t *r = coap_get_resource_from_uri_path_lkd(context,
625 resource->uri_path);
626
627 if (r) {
628 coap_log_warn("coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
629 (int)resource->uri_path->length, (int)resource->uri_path->length,
630 resource->uri_path->s);
631 coap_delete_resource_lkd(r);
632 }
633 RESOURCES_ADD(context->resources, resource);
634#if COAP_WITH_OBSERVE_PERSIST
635 if (context->unknown_pdu && context->dyn_resource_save_file &&
636 context->dyn_resource_added_cb && resource->observable) {
637 coap_bin_const_t raw_packet;
638
639 raw_packet.s = context->unknown_pdu->token -
640 context->unknown_pdu->hdr_size;
641 raw_packet.length = context->unknown_pdu->used_size +
642 context->unknown_pdu->hdr_size;
643 coap_lock_callback(context->dyn_resource_added_cb(context->unknown_session,
644 resource->uri_path,
645 &raw_packet,
646 context->observe_user_data));
647 }
648#endif /* COAP_WITH_OBSERVE_PERSIST */
649 }
650 assert(resource->context == NULL);
651 resource->context = context;
652}
653
654COAP_API int
656 int ret;
657
658 (void)context;
659 if (!resource)
660 return 0;
661
662 coap_lock_lock(return 0);
663 ret = coap_delete_resource_lkd(resource);
665 return ret;
666}
667
668/*
669 * Input context is ignored, but param left there to keep API consistent
670 */
671int
672coap_delete_resource_lkd(coap_resource_t *resource) {
673 coap_context_t *context;
674 coap_deleting_resource_t deleting;
675
676 if (!resource)
677 return 0;
678
679 context = resource->context;
681
682 if (resource->ref) {
683 resource->ref--;
684 return 1;
685 }
686 if (context && context->context_going_away) {
687 deleting = COAP_DELETING_RESOURCE_ON_EXIT;
688 } else {
689 deleting = COAP_DELETING_RESOURCE;
690 }
691 if (resource->is_unknown) {
692 if (context && context->unknown_resource == resource) {
693 context->unknown_resource = NULL;
694 }
695 } else if (resource->is_proxy_uri) {
696 if (context && context->proxy_uri_resource == resource) {
697 context->proxy_uri_resource = NULL;
698 }
699 } else if (context) {
700 /* remove resource from list */
701 RESOURCES_DELETE(context->resources, resource);
702 }
703 if (resource->is_dynamic) {
704 if (context) {
705 assert(context->dynamic_cur);
706 context->dynamic_cur--;
707 }
708 }
709
710 /* and free its allocated memory */
711 coap_free_resource(resource, deleting);
712
713 return 1;
714}
715
716int
717coap_resource_release_lkd(coap_resource_t *resource) {
718 return coap_delete_resource_lkd(resource);
719}
720
721void
722coap_delete_all_resources(coap_context_t *context) {
724 coap_resource_t *tmp;
725
726 RESOURCE_ITER_SAFE(context->resources, r, tmp) {
727 coap_delete_resource_lkd(r);
728 }
729
730 context->resources = NULL;
731
732 if (context->unknown_resource) {
733 coap_delete_resource_lkd(context->unknown_resource);
734 context->unknown_resource = NULL;
735 }
736 if (context->proxy_uri_resource) {
737 coap_delete_resource_lkd(context->proxy_uri_resource);
738 context->proxy_uri_resource = NULL;
739 }
740}
741
744 coap_resource_t *result;
745
746 coap_lock_lock(return NULL);
747 result = coap_get_resource_from_uri_path_lkd(context, uri_path);
749
750 return result;
751}
752
754coap_get_resource_from_uri_path_lkd(coap_context_t *context,
755 coap_str_const_t *uri_path) {
756 coap_resource_t *result;
757
759
760 RESOURCES_FIND(context->resources, uri_path, result);
761
762 return result;
763}
764
766coap_print_link(const coap_resource_t *resource,
767 unsigned char *buf, size_t *len, size_t *offset) {
768 unsigned char *p = buf;
769 const uint8_t *bufend = buf + *len;
770 coap_attr_t *attr;
771 coap_print_status_t result = 0;
772 coap_print_status_t output_length = 0;
773 const size_t old_offset = *offset;
774
775 *len = 0;
776 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
777 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
778
779 COPY_COND_WITH_OFFSET(p, bufend, *offset,
780 resource->uri_path->s, resource->uri_path->length, *len);
781
782 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
783
784 LL_FOREACH(resource->link_attr, attr) {
785
786 PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
787
788 COPY_COND_WITH_OFFSET(p, bufend, *offset,
789 attr->name->s, attr->name->length, *len);
790
791 if (attr->value && attr->value->s) {
792 PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
793
794 COPY_COND_WITH_OFFSET(p, bufend, *offset,
795 attr->value->s, attr->value->length, *len);
796 }
797
798 }
799 /* Keep implicit attributes in sync with coap_print_wellknown_lkd(). */
800 if (resource->observable) {
801 COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
802 }
803
804#if COAP_OSCORE_SUPPORT
805 if (resource->flags & COAP_RESOURCE_FLAGS_OSCORE_ONLY) {
806#if COAP_OSCORE_GROUP_SUPPORT
807 COPY_COND_WITH_OFFSET(p, bufend, *offset, ";gosc;osc", 9, *len);
808#else /* ! COAP_OSCORE_GROUP_SUPPORT */
809 COPY_COND_WITH_OFFSET(p, bufend, *offset, ";osc", 4, *len);
810#endif /* ! COAP_OSCORE_GROUP_SUPPORT */
811 }
812#endif /* COAP_OSCORE_SUPPORT */
813
814 output_length = (coap_print_status_t)(p - buf);
815
816 if (output_length > COAP_PRINT_STATUS_MAX) {
818 }
819
820 result = (coap_print_status_t)output_length;
821
822 if (result + old_offset - *offset < *len) {
823 result |= COAP_PRINT_STATUS_TRUNC;
824 }
825
826 return result;
827}
828
829void
831 coap_request_t method,
832 coap_method_handler_t handler) {
833 coap_register_request_handler(resource, method, handler);
834}
835
836void
838 coap_request_t method,
839 coap_method_handler_t handler) {
840 assert(resource);
841 assert(method > 0 && (size_t)(method-1) <
842 sizeof(resource->handler)/sizeof(coap_method_handler_t));
843 resource->handler[method-1] = handler;
844}
845
847coap_find_observer(coap_resource_t *resource, coap_session_t *session,
848 const coap_bin_const_t *token) {
850
851 assert(resource);
852 assert(session);
853
854 LL_FOREACH(resource->subscribers, s) {
855 if (s->session == session &&
856 (!token || coap_binary_equal(token, &s->pdu->actual_token)))
857 return s;
858 }
859
860 return NULL;
861}
862
863static coap_subscription_t *
864coap_find_observer_cache_key(coap_resource_t *resource, coap_session_t *session,
865 const coap_cache_key_t *cache_key) {
867
868 assert(resource);
869 assert(session);
870
871 LL_FOREACH(resource->subscribers, s) {
872 if (s->session == session
873 && (memcmp(cache_key, s->cache_key, sizeof(coap_cache_key_t)) == 0))
874 return s;
875 }
876
877 return NULL;
878}
879
880/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
881static const uint16_t cache_ignore_options[] = { COAP_OPTION_ETAG,
883 };
885coap_add_observer(coap_resource_t *resource,
886 coap_session_t *session,
887 const coap_bin_const_t *token,
888 const coap_pdu_t *request) {
890 coap_cache_key_t *cache_key = NULL;
891 size_t len;
892 const uint8_t *data;
893
894 assert(session);
895
896 /* Check if there is already a subscription for this peer. */
897 s = coap_find_observer(resource, session, token);
898 if (!s) {
899 /*
900 * Cannot allow a duplicate to be created for the same query as application
901 * may not be cleaning up duplicates. If duplicate found, then original
902 * observer is deleted and a new one created with the new token
903 */
904 cache_key = coap_cache_derive_key_w_ignore(session, request,
906 cache_ignore_options,
907 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
908 if (cache_key) {
909 s = coap_find_observer_cache_key(resource, session, cache_key);
910 if (s) {
911 /* Delete old entry with old token */
912 coap_delete_observer(resource, session, &s->pdu->actual_token);
913 s = NULL;
914 }
915 }
916 }
917
918 /* Refresh the destination when an existing observation is renewed. */
919 if (s) {
920 coap_address_copy(&s->local_if, &session->addr_info.local);
921 return s;
922 }
923
924 /* Check if there is already maximum number of subscribers present */
925#if (COAP_RESOURCE_MAX_SUBSCRIBER > 0)
926 uint32_t subscriber_count = 0;
927 LL_COUNT(resource->subscribers, s, subscriber_count);
928 if (subscriber_count >= COAP_RESOURCE_MAX_SUBSCRIBER) {
929 return NULL; /* Signal error */
930 }
931#endif /* COAP_RESOURCE_MAX_SUBSCRIBER */
932
933 /* Create a new subscription */
935
936 if (!s) {
937 coap_delete_cache_key(cache_key);
938 return NULL;
939 }
940
941 coap_subscription_init(s);
942 s->pdu = coap_pdu_duplicate_lkd(request, session, token->length,
943 token->s, NULL, COAP_BOOL_FALSE);
944 if (s->pdu == NULL) {
945 coap_delete_cache_key(cache_key);
947 return NULL;
948 }
949 if (coap_get_data(request, &len, &data)) {
950 /* This could be a large bodied FETCH */
951 s->pdu->max_size = 0;
952 coap_add_data(s->pdu, len, data);
953 }
954 if (cache_key == NULL) {
955 cache_key = coap_cache_derive_key_w_ignore(session, request,
957 cache_ignore_options,
958 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
959 if (cache_key == NULL) {
960 coap_delete_pdu_lkd(s->pdu);
961 coap_delete_cache_key(cache_key);
963 return NULL;
964 }
965 }
966 s->cache_key = cache_key;
967 s->session = coap_session_reference_lkd(session);
968 coap_address_copy(&s->local_if, &session->addr_info.local);
969 session->ref_subscriptions++;
970
971 /* add subscriber to resource */
972 LL_PREPEND(resource->subscribers, s);
973
974 coap_log_debug("create new subscription %p key 0x%02x%02x%02x%02x\n",
975 (void *)s, s->cache_key->key[0], s->cache_key->key[1],
976 s->cache_key->key[2], s->cache_key->key[3]);
977
978 if (session->context->observe_added_cb && session->proto == COAP_PROTO_UDP &&
979 !coap_is_af_unix(&session->addr_info.local)) {
980 coap_bin_const_t raw_packet;
981 coap_bin_const_t *oscore_info = NULL;
982#if COAP_OSCORE_SUPPORT
983 oscore_association_t *association;
984
985 if (session->recipient_ctx && session->recipient_ctx->recipient_id) {
986 /*
987 * Need to track the association used for tracking this observe, done as
988 * a CBOR array. Read in coap_persist_observe_add().
989 *
990 * If an entry is null, then use nil, else a set of bytes
991 *
992 * Currently tracking 5 items
993 * recipient_id
994 * id_context
995 * aad (from oscore_association_t)
996 * partial_iv (from oscore_association_t)
997 * nonce (from oscore_association_t)
998 */
999 uint8_t info_buffer[60];
1000 uint8_t *info_buf = info_buffer;
1001 size_t info_len = sizeof(info_buffer);
1002 size_t ret = 0;
1003 coap_bin_const_t ctoken = { token->length, token->s };
1004
1005 ret += oscore_cbor_put_array(&info_buf, &info_len, 5);
1006 ret += oscore_cbor_put_bytes(&info_buf,
1007 &info_len,
1008 session->recipient_ctx->recipient_id->s,
1009 session->recipient_ctx->recipient_id->length);
1010 if (session->recipient_ctx->osc_ctx &&
1011 session->recipient_ctx->osc_ctx->id_context) {
1012 ret += oscore_cbor_put_bytes(&info_buf,
1013 &info_len,
1014 session->recipient_ctx->osc_ctx->id_context->s,
1015 session->recipient_ctx->osc_ctx->id_context->length);
1016 } else {
1017 ret += oscore_cbor_put_nil(&info_buf, &info_len);
1018 }
1019 association = oscore_find_association(session, &ctoken);
1020 if (association) {
1021 if (association->aad) {
1022 ret += oscore_cbor_put_bytes(&info_buf,
1023 &info_len,
1024 association->aad->s,
1025 association->aad->length);
1026 } else {
1027 ret += oscore_cbor_put_nil(&info_buf, &info_len);
1028 }
1029 if (association->partial_iv) {
1030 ret += oscore_cbor_put_bytes(&info_buf,
1031 &info_len,
1032 association->partial_iv->s,
1033 association->partial_iv->length);
1034 } else {
1035 ret += oscore_cbor_put_nil(&info_buf, &info_len);
1036 }
1037 if (association->nonce) {
1038 ret += oscore_cbor_put_bytes(&info_buf,
1039 &info_len,
1040 association->nonce->s,
1041 association->nonce->length);
1042 } else {
1043 ret += oscore_cbor_put_nil(&info_buf, &info_len);
1044 }
1045 } else {
1046 ret += oscore_cbor_put_nil(&info_buf, &info_len);
1047 ret += oscore_cbor_put_nil(&info_buf, &info_len);
1048 }
1049 if (ret > sizeof(info_buffer)) {
1050 /* Should have been caught by assert() inoscborput_* functions */
1051 coap_log_warn("coap_add_observer overrun of info_buffer (%" PRIuS ")\n", ret);
1052 ret = sizeof(info_buffer);
1053 }
1054 oscore_info = coap_new_bin_const(info_buffer, ret);
1055 }
1056#endif /* COAP_OSCORE_SUPPORT */
1057
1058 /* s->pdu header is not currently encoded */
1059 memcpy(s->pdu->token - request->hdr_size,
1060 request->token - request->hdr_size, request->hdr_size);
1061 raw_packet.s = s->pdu->token - request->hdr_size;
1062 raw_packet.length = s->pdu->used_size + request->hdr_size;
1063 coap_lock_callback(session->context->observe_added_cb(session, s, session->proto,
1064 &session->endpoint->bind_addr,
1065 &session->addr_info,
1066 &raw_packet,
1067 oscore_info,
1068 session->context->observe_user_data));
1069#if COAP_OSCORE_SUPPORT
1070 coap_delete_bin_const(oscore_info);
1071#endif /* COAP_OSCORE_SUPPORT */
1072 }
1073 if (resource->context->track_observe_value_cb) {
1074 /* Track last used observe value (as app handler is called) */
1075 coap_lock_callback(resource->context->track_observe_value_cb(resource->context,resource->uri_path,
1076 resource->observe,
1077 resource->context->observe_user_data));
1078 }
1079
1080 return s;
1081}
1082
1083void
1084coap_touch_observer(coap_context_t *context, coap_session_t *session,
1085 const coap_bin_const_t *token) {
1087
1088 RESOURCES_ITER(context->resources, r) {
1089 s = coap_find_observer(r, session, token);
1090 if (s) {
1091 s->fail_cnt = 0;
1092 }
1093 }
1094}
1095
1096void
1097coap_delete_observer_internal(coap_resource_t *resource, coap_session_t *session,
1099 if (!s)
1100 return;
1101
1103 char outbuf[2 * 8 + 1] = "";
1104 unsigned int i;
1105 coap_string_t *uri_path;
1106 coap_string_t *uri_query;
1107
1108 for (i = 0; i < s->pdu->actual_token.length; i++) {
1109 size_t size = strlen(outbuf);
1110
1111 snprintf(&outbuf[size], sizeof(outbuf)-size, "%02x",
1112 s->pdu->actual_token.s[i]);
1113 }
1114 uri_path = coap_get_uri_path(s->pdu);
1115 uri_query = coap_get_query(s->pdu);
1116 coap_log_debug("removed subscription '/%*.*s%s%*.*s' (%p) with token '%s' key 0x%02x%02x%02x%02x\n",
1117 uri_path ? (int)uri_path->length : 0, uri_path ? (int)uri_path->length : 0,
1118 uri_path ? (char *)uri_path->s : "",
1119 uri_query ? "?" : "",
1120 uri_query ? (int)uri_query->length : 0, uri_query ? (int)uri_query->length : 0,
1121 uri_query ? (char *)uri_query->s : "",
1122 (void *)s, outbuf, s->cache_key->key[0], s->cache_key->key[1],
1123 s->cache_key->key[2], s-> cache_key->key[3]);
1124 coap_delete_string(uri_path);
1125 coap_delete_string(uri_query);
1126 }
1127 if (session->context->observe_deleted_cb)
1128 coap_lock_callback(session->context->observe_deleted_cb(session, s,
1129 session->context->observe_user_data));
1130
1131 if (resource->subscribers) {
1132 LL_DELETE(resource->subscribers, s);
1133 assert(session->ref_subscriptions > 0);
1134 session->ref_subscriptions--;
1135 coap_session_release_lkd(session);
1136 coap_delete_pdu_lkd(s->pdu);
1137 coap_delete_cache_key(s->cache_key);
1139 }
1140
1141 return;
1142}
1143
1144int
1145coap_delete_observer(coap_resource_t *resource, coap_session_t *session,
1146 const coap_bin_const_t *token) {
1148
1149 s = coap_find_observer(resource, session, token);
1150 if (s)
1151 coap_delete_observer_internal(resource, session, s);
1152
1153 return s != NULL;
1154}
1155
1156int
1157coap_delete_observer_request(coap_resource_t *resource, coap_session_t *session,
1158 const coap_bin_const_t *token, coap_pdu_t *request, int large_fetch) {
1160 int ret = 0;
1161
1162 s = coap_find_observer(resource, session, token);
1163 if (!s && large_fetch) {
1164 /*
1165 * It is possible that the client is using the wrong token.
1166 * An example being a large FETCH spanning multiple blocks.
1167 */
1168 coap_cache_key_t *cache_key;
1169
1170 cache_key = coap_cache_derive_key_w_ignore(session, request,
1172 cache_ignore_options,
1173 sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
1174 if (cache_key) {
1175 s = coap_find_observer_cache_key(resource, session, cache_key);
1176 if (s) {
1177 /* Delete entry with setup token */
1178 ret = coap_delete_observer(resource, session, &s->pdu->actual_token);
1179 }
1180 coap_delete_cache_key(cache_key);
1181 }
1182 } else {
1183 coap_delete_observer_internal(resource, session, s);
1184 ret = 1;
1185 }
1186 return ret;
1187}
1188
1189void
1190coap_delete_observers(coap_context_t *context, coap_session_t *session) {
1191 RESOURCES_ITER(context->resources, resource) {
1192 coap_subscription_t *s, *tmp;
1193 LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
1194 if (s->session == session) {
1195 if (context->observe_deleted_cb)
1196 coap_lock_callback(context->observe_deleted_cb(session, s, context->observe_user_data));
1197 assert(resource->subscribers);
1198 LL_DELETE(resource->subscribers, s);
1199 coap_session_release_lkd(session);
1200 coap_delete_pdu_lkd(s->pdu);
1201 coap_delete_cache_key(s->cache_key);
1203 }
1204 }
1205 }
1206}
1207
1208static void
1209coap_notify_observers(coap_context_t *context, coap_resource_t *r,
1210 coap_deleting_resource_t deleting) {
1213 coap_pdu_t *response;
1214 uint8_t buf[4];
1215 coap_string_t *query;
1216 coap_block_b_t block;
1217 coap_tick_t now;
1218
1220
1221 if (r->observable && (r->dirty || r->partiallydirty)) {
1222 if (r->list_being_traversed)
1223 return;
1224 r->list_being_traversed = 1;
1225
1226 coap_resource_reference_lkd(r);
1227
1228 r->partiallydirty = 0;
1229
1230 /*
1231 * Any subscription (not just the one being notified) may get deleted
1232 * in the callback, by another running thread when executing the
1233 * callback, or when sending a response (e.g. the session fails and all
1234 * of its subscriptions are deleted). So no pointer into the list is kept
1235 * over a notification: the next subscription to visit is looked up again
1236 * from the head of the list each time.
1237 */
1238 LL_FOREACH(r->subscribers, obs) {
1239 obs->to_visit = 1;
1240 }
1241 for (;;) {
1242 coap_session_t *obs_session;
1243 coap_pdu_t *obs_pdu;
1245
1246 LL_FOREACH(r->subscribers, obs) {
1247 if (obs->to_visit)
1248 break;
1249 }
1250 if (obs == NULL)
1251 break;
1252 obs->to_visit = 0;
1253
1254 if ((r->dirty == 0 && obs->dirty == 0) || obs->session->is_rate_limiting) {
1255 /*
1256 * running this resource due to partiallydirty, but this observation's
1257 * notification was already enqueued
1258 */
1259 context->observe_pending = 1;
1260 continue;
1261 }
1262
1263 /*
1264 * obs may get deleted in the callback, or by another running
1265 * thread when executing the callback or when sending a response.
1266 */
1267 obs_session = obs->session;
1268 obs_pdu = obs->pdu;
1269 coap_session_hold_lkd(obs_session);
1270 coap_pdu_reference_lkd(obs_pdu);
1271
1272 if (obs->session->con_active >= COAP_NSTART(obs->session) &&
1273 ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) ||
1274 (obs->non_cnt >= COAP_OBS_MAX_NON))) {
1275 /* Waiting for the previous unsolicited response to finish */
1276 goto next_one_fail;
1277 }
1278 coap_ticks(&now);
1279 if (obs->session->lg_xmit && obs->session->lg_xmit->last_all_sent == 0 &&
1280 obs->session->lg_xmit->last_obs &&
1281 (obs->session->lg_xmit->last_obs + 2*COAP_TICKS_PER_SECOND) > now) {
1282 /* Waiting for the previous blocked unsolicited response to finish */
1283 goto next_one_fail;
1284 }
1285
1286 /* Other packets on this session may have changed the local address,
1287 * including multicast requests from the same peer and source port.
1288 * Restore it before the handler can register an async response. */
1289 coap_address_copy(&obs_session->addr_info.local, &obs->local_if);
1290
1291 obs->dirty = 0;
1292 /* initialize response */
1293 response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0,
1294 coap_session_max_pdu_size_lkd(obs->session));
1295 if (!response) {
1296 coap_log_debug("coap_check_notify: pdu init failed, resource stays "
1297 "partially dirty\n");
1298 goto next_one_fail_no_pending;
1299 }
1300
1301 if (!coap_add_token(response, obs->pdu->actual_token.length,
1302 obs->pdu->actual_token.s)) {
1303 coap_log_debug("coap_check_notify: cannot add token, resource stays "
1304 "partially dirty\n");
1305 coap_delete_pdu_lkd(response);
1306 goto next_one_fail_no_pending;
1307 }
1308
1309 obs->pdu->mid = response->mid = coap_new_message_id_lkd(obs->session);
1310 /* A lot of the reliable code assumes type is CON */
1311 if (COAP_PROTO_NOT_RELIABLE(obs->session->proto) &&
1312 (r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 &&
1314 obs->non_cnt < COAP_OBS_MAX_NON)) {
1315 response->type = COAP_MESSAGE_NON;
1316 } else {
1317 response->type = COAP_MESSAGE_CON;
1318 }
1319 switch (deleting) {
1320 case COAP_NOT_DELETING_RESOURCE:
1321 /* fill with observer-specific data */
1323 coap_encode_var_safe(buf, sizeof(buf),
1324 r->observe),
1325 buf);
1326 if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_BLOCK2,
1327 &block)) {
1328 /* Will get updated later (e.g. M bit) if appropriate */
1330 coap_encode_var_safe(buf, sizeof(buf),
1331 ((0 << 4) |
1332 (0 << 3) |
1333 block.aszx)),
1334 buf);
1335 }
1336#if COAP_Q_BLOCK_SUPPORT
1337 else if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_Q_BLOCK2,
1338 &block)) {
1339 /* Will get updated later (e.g. M bit) if appropriate */
1341 coap_encode_var_safe(buf, sizeof(buf),
1342 ((0 << 4) |
1343 (0 << 3) |
1344 block.szx)),
1345 buf);
1346 }
1347#endif /* COAP_Q_BLOCK_SUPPORT */
1348
1349 h = r->handler[obs->pdu->code - 1];
1350 assert(h); /* we do not allow subscriptions if no
1351 * GET/FETCH handler is defined */
1352 query = coap_get_query(obs->pdu);
1353 coap_log_debug("Observe PDU presented to app.\n");
1354 coap_show_pdu(COAP_LOG_DEBUG, obs->pdu);
1355 coap_log_debug("call custom handler for resource '%*.*s' (4)\n",
1356 (int)r->uri_path->length, (int)r->uri_path->length,
1357 r->uri_path->s);
1358
1359 /* obs may get deleted during callback (potentially by another thread) */
1360 if (r->flags & COAP_RESOURCE_SAFE_REQUEST_HANDLER) {
1361 coap_lock_callback_release(h(r, obs_session, obs_pdu, query, response),
1362 /* context is being freed off */
1363 coap_delete_string(query);
1364 coap_delete_pdu_lkd(response);
1365 coap_session_unhold_lkd(obs_session);
1366 coap_pdu_release_lkd(obs_pdu);
1367 r->list_being_traversed = 0;
1368 coap_resource_release_lkd(r);
1369 return);
1370 } else {
1372 h(r, obs_session, obs_pdu, query, response),
1373 /* context is being freed off */
1374 coap_delete_string(query);
1375 coap_delete_pdu_lkd(response);
1376 coap_session_unhold_lkd(obs_session);
1377 coap_pdu_release_lkd(obs_pdu);
1378 r->list_being_traversed = 0;
1379 coap_resource_release_lkd(r);
1380 return);
1381 }
1382
1383 /* Check validity of response code */
1384 if (!coap_check_code_class(obs_session, response)) {
1385 coap_log_warn("handle_request: Invalid PDU response code (%d.%02d)\n",
1386 COAP_RESPONSE_CLASS(response->code),
1387 response->code & 0x1f);
1388 coap_delete_string(query);
1389 coap_delete_pdu_lkd(response);
1390 coap_session_unhold_lkd(obs_session);
1391 coap_pdu_release_lkd(obs_pdu);
1392 r->list_being_traversed = 0;
1393 coap_resource_release_lkd(r);
1394 return;
1395 }
1396
1397 /* Check if lg_xmit generated and update PDU code if so */
1398 coap_check_code_lg_xmit(obs_session, obs_pdu, response, r, query);
1399 coap_delete_string(query);
1400 if (COAP_RESPONSE_CLASS(response->code) != 2) {
1402 }
1403 if (COAP_RESPONSE_CLASS(response->code) > 2) {
1404 coap_delete_observer(r, obs_session, &obs_pdu->actual_token);
1405 obs = NULL;
1406 }
1407 break;
1408 case COAP_DELETING_RESOURCE_ON_EXIT:
1409 /* Don't worry if it does not get there */
1410 response->type = COAP_MESSAGE_NON;
1411 response->code = COAP_RESPONSE_CODE(503);
1413 coap_encode_var_safe(buf, sizeof(buf),
1414 30),
1415 buf);
1416 break;
1417 case COAP_DELETING_RESOURCE:
1418 default:
1419 /* Don't worry if it does not get there */
1420 response->type = COAP_MESSAGE_NON;
1421 response->code = COAP_RESPONSE_CODE(404);
1422 break;
1423 }
1424
1425 if (obs) {
1427 /*
1428 * obs may have been deleted in the callback, or by another running
1429 * thread when executing the callback.
1430 */
1431 LL_FOREACH(r->subscribers, s) {
1432 if (s == obs) {
1433 break;
1434 }
1435 }
1436 if (s == NULL)
1437 obs = NULL;
1438 }
1439 if (obs) {
1440 if (response->type == COAP_MESSAGE_CON ||
1442 obs->non_cnt = 0;
1443 } else {
1444 obs->non_cnt++;
1445 }
1446
1447#if COAP_Q_BLOCK_SUPPORT
1448 if (response->code == COAP_RESPONSE_CODE(205) &&
1449 coap_get_block_b(obs_session, response, COAP_OPTION_Q_BLOCK2,
1450 &block) &&
1451 block.m) {
1452 query = coap_get_query(obs_pdu);
1453 mid = coap_send_q_block2(obs_session, r, query, obs_pdu->code,
1454 block, response, 1);
1455 coap_delete_string(query);
1456 goto finish;
1457 }
1458#endif /* COAP_Q_BLOCK_SUPPORT */
1459 }
1460 if (response->code == COAP_EMPTY_CODE &&
1461 (response->type == COAP_MESSAGE_NON ||
1462 response->type == COAP_MESSAGE_CON) &&
1463 response->actual_token.length != 0) {
1464 /*
1465 * If a resource has been updated, triggering the sending of an
1466 * observer response and the request handler does not set a response
1467 * code (a coding error condition), then this PDU must be dropped as
1468 * it is illegally formatted.
1469 */
1470 coap_delete_pdu_lkd(response);
1472 } else {
1473 mid = coap_send_internal(obs_session, response, NULL);
1474 }
1475
1476#if COAP_Q_BLOCK_SUPPORT
1477finish:
1478#endif /* COAP_Q_BLOCK_SUPPORT */
1479 if (COAP_INVALID_MID == mid) {
1480 coap_log_debug("* %s: coap_check_notify: sending failed, resource stays "
1481 "partially dirty\n", coap_session_str(obs_session));
1482 if (obs) {
1484 /*
1485 * obs may have been deleted in coap_send_internal() or
1486 * coap_send_q_block2().
1487 */
1488 LL_FOREACH(r->subscribers, s) {
1489 if (s == obs) {
1490 break;
1491 }
1492 }
1493 if (s == NULL)
1494 obs = NULL;
1495 }
1496 if (obs)
1497 obs->dirty = 1;
1498 r->partiallydirty = 1;
1499 }
1500 goto cleanup;
1501
1502next_one_fail:
1503 context->observe_pending = 1;
1504next_one_fail_no_pending:
1505 r->partiallydirty = 1;
1506 if (obs)
1507 obs->dirty = 1;
1508cleanup:
1509 coap_session_unhold_lkd(obs_session);
1510 coap_pdu_release_lkd(obs_pdu);
1511 }
1512 r->list_being_traversed = 0;
1513 r->dirty = 0;
1514 coap_resource_release_lkd(r);
1515 /* r may be no more if elsewhere coap_free_resource() has been called */
1516 } else {
1517 r->dirty = 0;
1518 }
1519}
1520
1521COAP_API int
1523 int ret;
1524 (void)query;
1525
1526 coap_lock_lock(return 0);
1527 ret = coap_resource_notify_observers_lkd(r, COAP_NOT_DELETING_RESOURCE);
1529 return ret;
1530}
1531
1532COAP_API int
1534 const coap_string_t *query) {
1535 int ret;
1536
1537 (void)query;
1538 coap_lock_lock(return 0);
1539 ret = coap_resource_notify_observers_lkd(r, COAP_NOT_DELETING_RESOURCE);
1541 return ret;
1542}
1543
1544int
1545coap_resource_notify_observers_lkd(coap_resource_t *r,
1546 coap_deleting_resource_t deleting) {
1548 if (!r->observable)
1549 return 0;
1550 if (!r->subscribers)
1551 return 0;
1552 r->dirty = 1;
1553
1554 /* Increment value for next Observe use. Observe value must be < 2^24 */
1555 r->observe = (r->observe + 1) & 0xFFFFFF;
1556
1557 assert(r->context);
1558
1559 if (r->context->track_observe_value_cb) {
1560 /* Track last used observe value */
1561 if ((r->observe % r->context->observe_save_freq) == 0)
1562 coap_lock_callback(r->context->track_observe_value_cb(r->context, r->uri_path,
1563 r->observe,
1564 r->context->observe_user_data));
1565 }
1566
1567 coap_notify_observers(r->context, r, deleting);
1568 return 1;
1569}
1570
1571void
1572coap_resource_set_mode(coap_resource_t *resource, int mode) {
1573 resource->flags = (resource->flags &
1576}
1577
1578void
1579coap_resource_set_userdata(coap_resource_t *resource, void *data) {
1580 resource->user_data = data;
1581}
1582
1583void *
1585 return resource->user_data;
1586}
1587
1588void
1591 context->release_userdata_cb = callback;
1592}
1593
1594void
1596 if (resource->is_unknown || resource->is_proxy_uri) {
1597 /* We cannot observe these */
1598 coap_log_debug("coap_resource_set_get_observable: Not supported for Unknown or Proxy URIs\n");
1599 resource->observable = 0;
1600 } else {
1601 resource->observable = mode ? 1 : 0;
1602 }
1603}
1604
1607 if (resource)
1608 return resource->uri_path;
1609 return NULL;
1610}
1611
1612COAP_API void
1614 coap_lock_lock(return);
1615 coap_check_notify_lkd(context);
1617}
1618
1619void
1620coap_check_notify_lkd(coap_context_t *context) {
1621
1623 if (context->observe_pending) {
1624 context->observe_pending = 0;
1625 RESOURCES_ITER(context->resources, r) {
1626 coap_notify_observers(context, r, COAP_NOT_DELETING_RESOURCE);
1627 }
1628 }
1629}
1630
1631void
1633 uint32_t start_observe_no) {
1634 if (!resource)
1635 return;
1636
1637 resource->observe = start_observe_no & 0xffffff;
1638}
1639
1650static void
1651coap_remove_failed_observers(coap_context_t *context,
1652 coap_resource_t *resource,
1653 coap_session_t *session,
1654 const coap_bin_const_t *token) {
1655 coap_subscription_t *obs, *otmp;
1656
1657 LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
1658 if (obs->session == session &&
1659 coap_binary_equal(token, &obs->pdu->actual_token)) {
1660 /* count failed notifies and remove when
1661 * COAP_OBS_MAX_FAIL is reached */
1662 obs->fail_cnt++;
1663 if (obs->fail_cnt >= COAP_OBS_MAX_FAIL) {
1664 coap_cancel_all_messages(context, obs->session,
1665 &obs->pdu->actual_token);
1666 coap_delete_observer(resource, session, token);
1667 }
1668 break; /* break loop if observer was found */
1669 }
1670 }
1671}
1672
1673void
1674coap_handle_failed_notify(coap_context_t *context,
1675 coap_session_t *session,
1676 const coap_bin_const_t *token) {
1677
1678 RESOURCES_ITER(context->resources, r) {
1679 coap_remove_failed_observers(context, r, session, token);
1680 }
1681}
1682
1683void
1684coap_resource_reference_lkd(coap_resource_t *resource) {
1685 resource->ref++;
1686}
1687
1689coap_add_dynamic_resource(coap_session_t *session, coap_pdu_t *pdu) {
1690 coap_context_t *context = session->context;
1691 coap_resource_t *resource;
1692
1693 if ((context->dyn_create_handler != NULL) &&
1695 /* Above test must be the same as in coap_op_dyn_resource_load_disk() */
1696 if (context->dynamic_cur < context->dynamic_max || context->dynamic_max == 0) {
1697#if COAP_WITH_OBSERVE_PERSIST
1698 /* If we are maintaining Observe persist */
1699 context->unknown_pdu = pdu;
1700 context->unknown_session = session;
1701#endif /* COAP_WITH_OBSERVE_PERSIST */
1702 coap_lock_callback_ret(resource, context->dyn_create_handler(session, pdu));
1703#if COAP_WITH_OBSERVE_PERSIST
1704 /* If we are maintaining Observe persist */
1705 context->unknown_pdu = NULL;
1706 context->unknown_session = NULL;
1707#endif /* COAP_WITH_OBSERVE_PERSIST */
1708 if (resource) {
1709 context->dynamic_cur++;
1710 resource->is_dynamic = 1;
1711 }
1712 return resource;
1713 }
1714 }
1715 return NULL;
1716}
1717
1718#endif /* COAP_SERVER_SUPPORT */
int coap_is_af_unix(const coap_address_t *a)
Checks if given address a denotes a AF_UNIX address.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
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_OPTION_OBSERVE
Definition coap_option.h:75
@ COAP_OPTION_ETAG
Definition coap_option.h:73
@ COAP_OPTION_MAXAGE
Definition coap_option.h:83
@ COAP_OPTION_Q_BLOCK2
Definition coap_option.h:93
@ COAP_OPTION_BLOCK2
Definition coap_option.h:90
@ COAP_OPTION_OSCORE
Definition coap_option.h:78
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:71
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 ...
#define COAP_RESOURCE_FLAGS_FORCE_SINGLE_BODY
Force all large traffic to this resource to be presented as a single body to the request handler.
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:2172
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:1586
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:3487
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_callback_ret(r, func)
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:845
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:1769
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:193
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:545
#define COAP_DROPPED_RESPONSE
Indicates that a response is suppressed.
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:233
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:858
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:195
coap_request_t
CoAP PDU Request methods.
Definition coap_pdu.h:80
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:96
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:99
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:413
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:961
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:105
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:198
#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:930
@ COAP_BOOL_FALSE
Definition coap_pdu.h:306
@ 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:247
@ COAP_REQUEST_CODE_PUT
Definition coap_pdu.h:264
@ COAP_REQUEST_CODE_POST
Definition coap_pdu.h:263
@ COAP_EMPTY_CODE
Definition coap_pdu.h:260
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
void coap_session_hold_lkd(coap_session_t *session)
Mark the session as in use for a unit of work (reading and dispatching received data,...
void coap_session_unhold_lkd(coap_session_t *session)
End a unit of work started by coap_session_hold_lkd().
#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:1202
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:1108
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.
coap_resource_dynamic_create_t dyn_create_handler
Dynamic resource create handler.
uint32_t dynamic_max
Max number of dynamic resources or 0 is unlimited.
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