Loading...
Searching...
No Matches
nanocoap.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016-2018 Kaspar Schleiser <kaspar@schleiser.de>
3 * SPDX-FileCopyrightText: 2018 Freie Universität Berlin
4 * SPDX-FileCopyrightText: 2018 Inria
5 * SPDX-FileCopyrightText: 2018 Ken Bannister <kb2ma@runbox.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
7 */
8
9#pragma once
10
75
76#include <assert.h>
77#include <stdint.h>
78#include <stdbool.h>
79#include <stddef.h>
80#include <string.h>
81#include <unistd.h>
82
83#include "bitarithm.h"
84#include "bitfield.h"
85#include "byteorder.h"
86#include "iolist.h"
87#include "macros/utils.h"
88#include "modules.h"
89#include "net/coap.h"
90#include "net/sock/udp.h"
91
92#if defined(MODULE_NANOCOAP_RESOURCES)
93#include "xfa.h"
94#endif
95
96#ifdef __cplusplus
97extern "C" {
98#endif
99
105#define COAP_GET (0x01)
106#define COAP_POST (0x02)
107#define COAP_PUT (0x04)
108#define COAP_DELETE (0x08)
109#define COAP_FETCH (0x10)
110#define COAP_PATCH (0x20)
111#define COAP_IPATCH (0x40)
112#define COAP_IGNORE (0xFF)
114#define COAP_MATCH_SUBTREE (0x8000)
117
121#define COAP_FORMAT_NONE (UINT16_MAX)
122
130#ifndef CONFIG_NANOCOAP_NOPTS_MAX
131#define CONFIG_NANOCOAP_NOPTS_MAX (16)
132#endif
133
138#ifndef CONFIG_NANOCOAP_URI_MAX
139#define CONFIG_NANOCOAP_URI_MAX (64)
140#endif
141
145#ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
146#define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
147#endif
148
152#ifndef CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT
153#define CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT COAP_BLOCKSIZE_64
154#endif
155
157#ifndef CONFIG_NANOCOAP_QS_MAX
158#define CONFIG_NANOCOAP_QS_MAX (64)
159#endif
161
167#ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX
168#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (80)
169#endif
170
178#define COAP_OPT_FINISH_NONE (0x0000)
180#define COAP_OPT_FINISH_PAYLOAD (0x0001)
182
186typedef struct __attribute__((packed)) {
187 uint8_t ver_t_tkl;
188 uint8_t code;
189 uint16_t id;
191
199
203typedef struct {
204 uint16_t opt_num;
205 uint16_t offset;
207
226typedef struct {
227 union {
233 uint8_t *buf;
246 };
247 uint8_t *payload;
249 uint16_t payload_len;
250 uint16_t options_len;
253#ifdef MODULE_GCOAP
254 uint32_t observe_value;
255#endif
256} coap_pkt_t;
257
262
285typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len,
286 coap_request_ctx_t *context);
287
300typedef int (*coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more);
301
312typedef int (*coap_request_cb_t)(void *arg, coap_pkt_t *pkt);
313
319typedef uint16_t coap_method_flags_t;
320
330
334typedef const struct {
336 const size_t resources_numof;
338
346
353 union {
356 };
357#if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN
358 union {
361 };
362#endif
363#if defined(MODULE_GCOAP) || DOXYGEN
370 uint32_t tl_type;
371#endif
372};
373
374/* forward declarations */
375static inline uint8_t *coap_hdr_data_ptr(const coap_udp_hdr_t *hdr);
376static inline size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr);
377static inline const void *coap_hdr_get_token(const coap_udp_hdr_t *hdr);
378
387
396
406
416
426
451typedef struct {
452 uint8_t *buf;
453 uint16_t pos;
454 uint16_t size;
455 uint16_t last_opt_num;
457
461typedef struct {
462 size_t offset;
463 uint32_t blknum;
464 uint8_t szx;
465 int8_t more;
468
472typedef struct {
473 size_t start;
474 size_t end;
475 size_t cur;
476 uint8_t *opt_value;
478
479#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
485#define NANOCOAP_RESOURCE(name) \
486 XFA_CONST(coap_resource_t, coap_resources_xfa, 0) CONCAT(coap_resource_, name) =
487#else
493extern const coap_resource_t coap_resources[];
494
500extern const unsigned coap_resources_numof;
501#endif
502
516{
517 /* currently only UDP as transport supported */
518 return (coap_udp_hdr_t *)pkt->buf;
519}
520
524static inline const coap_udp_hdr_t *coap_get_udp_hdr_const(const coap_pkt_t *pkt)
525{
526 /* currently only UDP as transport supported */
527 return (const coap_udp_hdr_t *)pkt->buf;
528}
529
538static inline uint8_t coap_code(unsigned cls, unsigned detail)
539{
540 return (cls << 5) | detail;
541}
542
550static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt)
551{
552 return coap_get_udp_hdr_const(pkt)->code;
553}
554
562static inline unsigned coap_get_code_class(const coap_pkt_t *pkt)
563{
564 return coap_get_code_raw(pkt) >> 5;
565}
566
574static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
575{
576 return coap_get_code_raw(pkt) & 0x1f;
577}
578
586static inline unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
587{
588 return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
589}
590
598static inline coap_method_t coap_get_method(const coap_pkt_t *pkt)
599{
600 return coap_get_code_raw(pkt);
601}
602
610static inline unsigned coap_get_id(const coap_pkt_t *pkt)
611{
612 return ntohs(coap_get_udp_hdr_const(pkt)->id);
613}
614
621static inline void coap_set_id(coap_pkt_t *pkt, uint16_t id)
622{
623 coap_get_udp_hdr(pkt)->id = htons(id);
624}
625
636static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
637{
638 return coap_hdr_get_token_len((const coap_udp_hdr_t *)pkt->buf);
639}
640
648static inline void *coap_get_token(const coap_pkt_t *pkt)
649{
651}
652
662static inline unsigned coap_get_total_len(const coap_pkt_t *pkt)
663{
664 return (uintptr_t)pkt->payload - (uintptr_t)pkt->buf + pkt->payload_len;
665}
666
677static inline unsigned coap_get_type(const coap_pkt_t *pkt)
678{
679 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x30) >> 4;
680}
681
689static inline unsigned coap_get_ver(const coap_pkt_t *pkt)
690{
691 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x60) >> 6;
692}
693
706static inline uint8_t coap_hdr_tkl_ext_len(const coap_udp_hdr_t *hdr)
707{
708 if (!IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
709 return 0;
710 }
711
712 switch (hdr->ver_t_tkl & 0x0f) {
713 case 13:
714 return 1;
715 case 14:
716 return 2;
717 case 15:
718 assert(0);
719 /* fall-through */
720 default:
721 return 0;
722 }
723}
724
735static inline uint8_t coap_pkt_tkl_ext_len(const coap_pkt_t *pkt)
736{
738}
739
760bool coap_is_hdr_in_bounds(const coap_pkt_t *pkt, size_t len);
761
771static inline uint8_t *coap_hdr_data_ptr(const coap_udp_hdr_t *hdr)
772{
773 return ((uint8_t *)hdr) + sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(hdr);
774}
775
783static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
784{
785 return sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) +
787}
788
796static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
797{
798 return coap_get_total_hdr_len(pkt);
799}
800
809static inline void coap_hdr_set_code(coap_udp_hdr_t *hdr, uint8_t code)
810{
811 hdr->code = code;
812}
813
820static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
821{
823}
824
835static inline void coap_hdr_set_type(coap_udp_hdr_t *hdr, unsigned type)
836{
837 /* assert correct range of type */
838 assert(!(type & ~0x3));
839
840 hdr->ver_t_tkl &= ~0x30;
841 hdr->ver_t_tkl |= type << 4;
842}
843
859static inline size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr)
860{
861 const uint8_t *buf = (const void *)hdr;
862 /* Regarding use unnamed magic numbers 13 and 269:
863 * - If token length is < 13 it fits into TKL field (4 bit)
864 * - If token length is < 269 it fits into 8-bit extended TKL field
865 * - Otherwise token length goes into 16-bit extended TKL field.
866 *
867 * (Not using named constants here, as RFC 8974 also has no names for those
868 * magic numbers.)
869 *
870 * See: https://www.rfc-editor.org/rfc/rfc8974#name-extended-token-length-tkl-f
871 */
872 switch (coap_hdr_tkl_ext_len(hdr)) {
873 case 0:
874 return hdr->ver_t_tkl & 0x0f;
875 case 1:
876 return buf[sizeof(coap_udp_hdr_t)] + 13;
877 case 2:
878 return byteorder_bebuftohs(buf + sizeof(coap_udp_hdr_t)) + 269;
879 }
880
881 return 0;
882}
883
899static inline const void * coap_hdr_get_token(const coap_udp_hdr_t *hdr)
900{
901 uint8_t *token = (void *)hdr;
902 token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr);
903 return token;
904}
905
920static inline size_t coap_hdr_len(const coap_udp_hdr_t *hdr)
921{
922 return sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr) + coap_hdr_get_token_len(hdr);
923}
924
933static inline void coap_pkt_set_type(coap_pkt_t *pkt, unsigned type)
934{
936
937 if (hdr) {
938 coap_hdr_set_type(hdr, type);
939 }
940}
941
952static inline void coap_pkt_set_tkl(coap_pkt_t *pkt, uint8_t tkl)
953{
954 coap_udp_hdr_t * hdr = coap_get_udp_hdr(pkt);
955 hdr->ver_t_tkl &= 0xf0;
956 hdr->ver_t_tkl |= (tkl & 0x0f);
957}
958
959
976
986uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num);
987
1000uint8_t *coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num,
1001 uint8_t **opt_pos, int *opt_len);
1002
1012
1022
1035int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
1036
1054ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum,
1055 char *target, size_t max_len, char separator);
1056
1072static inline ssize_t coap_get_location_path(coap_pkt_t *pkt,
1073 char *target, size_t max_len)
1074{
1075 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
1076 target, max_len, '/');
1077}
1078
1094static inline ssize_t coap_get_location_query(coap_pkt_t *pkt,
1095 char *target, size_t max_len)
1096{
1097 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
1098 target, max_len, '&');
1099}
1100
1115static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, char *target)
1116{
1117 return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
1119}
1120
1134static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
1135 size_t max_len)
1136{
1137 return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY,
1138 target, max_len, '&');
1139}
1140
1155bool coap_find_uri_query(coap_pkt_t *pkt, const char *key,
1156 const char **value, size_t *len);
1157
1180 char *key, size_t key_len_max,
1181 char *value, size_t value_len_max);
1182
1213 uint8_t **value, bool init_opt);
1214
1231ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
1233
1246static inline ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
1247{
1248 return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
1249}
1250
1268void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
1269 int more);
1270
1286
1301static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
1302{
1303 return coap_block_finish(slicer);
1304}
1305
1320static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
1321{
1322 return coap_block_finish(slicer);
1323}
1324
1339
1354 size_t blksize);
1355
1374 const void *c, size_t len);
1375
1394 const void *c, size_t len);
1395
1413
1431
1450int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
1451
1469static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
1470{
1471 return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
1472}
1473
1483static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
1484{
1485 return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
1486}
1487
1500int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx);
1501
1520
1528#define coap_szx2size(szx) (1U << ((szx) + 4))
1529
1537static inline unsigned coap_size2szx(unsigned len)
1538{
1539 assert(len >= 16);
1540 return bitarithm_msb(len >> 4);
1541}
1542
1543
1575 bool more, uint16_t option);
1576
1595static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
1596 coap_block_slicer_t *slicer, bool more)
1597{
1598 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
1599}
1600
1619static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
1620 coap_block_slicer_t *slicer, bool more)
1621{
1622 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
1623}
1624
1638ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
1639
1653static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
1654 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
1655 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1656}
1657
1671static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
1672 /* block.more must be zero, so no need to 'or' it in */
1673 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
1674 (block->blknum << 4) | block->szx);
1675}
1676
1690static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
1691{
1692 return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
1693}
1694
1708static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1709{
1710 return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1711}
1712
1728ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len);
1729
1747ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1748 const char *val, size_t val_len);
1749
1766static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1767 const char *val)
1768{
1769 return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1770}
1771
1786ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1787
1806ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1807 size_t chars_len, char separator);
1808
1826static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1827 const char *string, char separator)
1828{
1829 return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1830}
1831
1846static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1847{
1848 return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1849}
1850
1866static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1867 const char *path,
1868 size_t path_len)
1869{
1870 return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1871}
1872
1885ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1886
1904ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum);
1906
1940int coap_builder_init(coap_builder_t *state, void *buf, size_t buf_len,
1941 size_t header_len);
1942
1951static inline bool coap_builder_has_overflown(const coap_builder_t *state)
1952{
1953 return (state->size == 0);
1954}
1955
1966static inline ssize_t coap_builder_msg_size(const coap_builder_t *state)
1967{
1968 if (coap_builder_has_overflown(state)) {
1969 return -EOVERFLOW;
1970 }
1971
1972 return state->pos;
1973}
1974
1985static inline size_t coap_builder_buf_size(const coap_builder_t *state)
1986{
1987 return state->size;
1988}
1989
1997static inline size_t coap_builder_buf_remaining(const coap_builder_t *state)
1998{
1999 if (state->size > state->pos) {
2000 return state->size - state->pos;
2001 }
2002
2003 return 0;
2004}
2005
2029int coap_builder_init_reply(coap_builder_t *state, void *buf, size_t buf_len,
2030 coap_pkt_t *req, uint8_t code);
2031
2053int coap_builder_add_payload(coap_builder_t *state, const void *pld, size_t pld_len);
2054
2081void * coap_builder_allocate_payload(coap_builder_t *state, size_t pld_len);
2082
2097{
2098 return coap_builder_add_payload(state, NULL, 0);
2099}
2100
2120 uint16_t option);
2121
2138static inline int coap_opt_put_block1(coap_builder_t *state,
2139 coap_block_slicer_t *slicer)
2140{
2141 return coap_opt_put_block(state, slicer, COAP_OPT_BLOCK1);
2142}
2143
2160static inline int coap_opt_put_block2(coap_builder_t *state,
2161 coap_block_slicer_t *slicer)
2162{
2163 return coap_opt_put_block(state, slicer, COAP_OPT_BLOCK2);
2164}
2165
2184int coap_opt_put_uint(coap_builder_t *state, uint16_t onum, uint32_t value);
2185
2203 coap_block1_t *block)
2204{
2205 return coap_opt_put_uint(state, COAP_OPT_BLOCK1,
2206 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
2207}
2208
2228 coap_block1_t *block)
2229{
2230 /* block.more must be zero, so no need to 'or' it in */
2231 return coap_opt_put_uint(state, COAP_OPT_BLOCK2,
2232 (block->blknum << 4) | block->szx);
2233}
2234
2251static inline int coap_opt_put_observe(coap_builder_t *state, uint32_t obs)
2252{
2253 obs &= COAP_OBS_MAX_VALUE_MASK; /* trim obs down to 24 bit */
2254 return coap_opt_put_uint(state, COAP_OPT_OBSERVE, obs);
2255}
2256
2278 const char *string, size_t len, char separator);
2298static inline int coap_opt_put_string(coap_builder_t *state, uint16_t optnum,
2299 const char *string, char separator)
2300{
2301 return coap_opt_put_string_with_len(state, optnum,
2302 string, strlen(string), separator);
2303}
2304
2322 const char *location)
2323{
2324 return coap_opt_put_string(state, COAP_OPT_LOCATION_PATH,
2325 location, '/');
2326}
2327
2345 const char *location)
2346{
2347 return coap_opt_put_string(state, COAP_OPT_LOCATION_QUERY,
2348 location, '&');
2349}
2350
2367static inline int coap_opt_put_uri_path(coap_builder_t *state, const char *uri)
2368{
2369 return coap_opt_put_string(state, COAP_OPT_URI_PATH, uri, '/');
2370}
2371
2389 const char *uri_query)
2390{
2391 return coap_opt_put_string(state, COAP_OPT_URI_QUERY, uri_query, '&');
2392}
2393
2417int coap_opt_put_uri_pathquery(coap_builder_t *state, const char *uri);
2418
2435static inline int coap_opt_put_proxy_uri(coap_builder_t *state, const char *uri)
2436{
2437 return coap_opt_put_string(state, COAP_OPT_PROXY_URI, uri, '\0');
2438}
2439
2465
2490int coap_opt_put(coap_builder_t *state, uint16_t onum, const void *odata, size_t olen);
2491
2511 unsigned blknum, unsigned szx, int more)
2512{
2513 return coap_opt_put_uint(state, COAP_OPT_BLOCK1,
2514 (blknum << 4) | szx | (more ? 0x8 : 0));
2515}
2516
2533static inline int coap_opt_put_ct(coap_builder_t *state, uint16_t content_type)
2534{
2535 return coap_opt_put_uint(state, COAP_OPT_CONTENT_FORMAT, content_type);
2536}
2537
2538
2558ssize_t coap_build_udp_hdr(void *buf, size_t buf_len, uint8_t type, const void *token,
2559 size_t token_len, uint8_t code, uint16_t id);
2581static inline ssize_t coap_build_hdr(coap_udp_hdr_t *hdr, unsigned type, const void *token,
2582 size_t token_len, unsigned code, uint16_t id)
2583{
2584 size_t fingers_crossed_size = sizeof(*hdr) + token_len;
2585
2586 if (IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
2587 /* With RFC 8974, we have an additional extended TKL
2588 * field of 0-4 bytes in length */
2589 fingers_crossed_size += 4;
2590 }
2591
2592 return coap_build_udp_hdr(hdr, fingers_crossed_size, type, token, token_len, code, id);
2593}
2594
2656ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
2657 uint8_t *rbuf, unsigned rlen, unsigned max_data_len);
2658
2674
2689ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
2690 coap_request_ctx_t *ctx);
2691
2708ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2709 unsigned resp_buf_len, coap_request_ctx_t *ctx,
2710 const coap_resource_t *resources,
2711 size_t resources_numof);
2712
2730ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2731 size_t resp_buf_len, coap_request_ctx_t *context);
2732
2740static inline coap_method_flags_t coap_method2flag(unsigned code)
2741{
2742 return (1 << (code - 1));
2743}
2744
2760ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len);
2761
2767static inline ssize_t coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
2768{
2769 return coap_parse_udp(pkt, buf, len);
2770}
2771
2786void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
2787
2801static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
2802{
2803 pkt->payload += len;
2804 pkt->payload_len -= len;
2805}
2806
2828ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
2829
2843ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
2844
2870ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code,
2871 void *buf, size_t len, uint16_t ct,
2872 void **payload, size_t *payload_len_max);
2873
2898 uint8_t code,
2899 uint8_t *buf, size_t len,
2900 uint16_t ct,
2901 const void *payload, size_t payload_len);
2902
2908 uint8_t *buf, size_t len,
2909 coap_request_ctx_t *context);
2911
2915#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
2916#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE !IS_USED(MODULE_GCOAP)
2917#endif
2918
2934int coap_match_path(const coap_resource_t *resource, const char *uri);
2935
2936#if defined(MODULE_GCOAP) || defined(DOXYGEN)
2949static inline bool coap_has_observe(coap_pkt_t *pkt)
2950{
2951 return pkt->observe_value != UINT32_MAX;
2952}
2953
2959static inline void coap_clear_observe(coap_pkt_t *pkt)
2960{
2961 pkt->observe_value = UINT32_MAX;
2962}
2963
2971static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
2972{
2973 return pkt->observe_value;
2974}
2975
2976#endif
2977
2981#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2982 { \
2983 .path = "/.well-known/core", \
2984 .methods = COAP_GET, \
2985 .handler = coap_well_known_core_default_handler \
2986 }
2987
2988#ifdef __cplusplus
2989}
2990#endif
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Helper functions for bit arithmetic.
static unsigned bitarithm_msb(unsigned v)
Returns the number of the highest '1' bit in a value.
Definition bitarithm.h:149
Bitfield operations on bitfields of arbitrary length.
Functions to work with different byte orders.
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:532
static uint16_t byteorder_bebuftohs(const uint8_t *buf)
Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable,...
Definition byteorder.h:547
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:517
#define WARN_UNUSED_RESULT
Attribute to add to a function whose return value should not silently be discarded.
Various helper macros.
#define EOVERFLOW
Value too large to be stored in data type.
Definition errno.h:133
#define COAP_OBS_MAX_VALUE_MASK
observe value is 24 bits
Definition coap.h:529
coap_method_t
CoAP method codes used in request.
Definition coap.h:167
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
Definition nanocoap.h:131
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition nanocoap.h:139
uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx)
Get transport the packet was received over.
uint8_t * coap_find_option(coap_pkt_t *pkt, unsigned opt_num)
Get pointer to an option field by type.
static ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key, const char *val)
Adds a single Uri-Query option in the form 'key=value' into pkt.
Definition nanocoap.h:1766
int coap_blockwise_put_char(coap_builder_t *state, coap_block_slicer_t *slicer, char c)
Add a single character to a block2 reply when building the response using a coap_builder_t structure ...
int coap_opt_put_block(coap_builder_t *state, coap_block_slicer_t *slicer, uint16_t option)
Insert block option into buffer.
ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum, char *target, size_t max_len, char separator)
Read a full option as null terminated string into the target buffer.
int coap_match_path(const coap_resource_t *resource, const char *uri)
Checks if a CoAP resource path matches a given URI.
bool coap_is_hdr_in_bounds(const coap_pkt_t *pkt, size_t len)
Validate that the header of pkt is no longer than len bytes.
static void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
Write the given raw message code to given CoAP pkt.
Definition nanocoap.h:820
static ssize_t coap_build_hdr(coap_udp_hdr_t *hdr, unsigned type, const void *token, size_t token_len, unsigned code, uint16_t id)
Builds a CoAP header.
Definition nanocoap.h:2581
static ssize_t coap_opt_add_block2(coap_pkt_t *pkt, coap_block_slicer_t *slicer, bool more)
Add block2 option in descriptive use from a slicer object.
Definition nanocoap.h:1619
static unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition nanocoap.h:550
void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len)
Initialize a packet struct, to build a message buffer.
static ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
Append a Content-Format option to the pkt buffer.
Definition nanocoap.h:1708
int coap_blockwise_put_char_pkt(coap_pkt_t *pdu, coap_block_slicer_t *slicer, char c)
Add a single character to a block2 reply when building the response using a coap_pkt_t structure to a...
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition nanocoap.h:538
bool coap_has_unprocessed_critical_options(const coap_pkt_t *pkt)
Check whether any of the packet's options that are critical.
static ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
Adds one or multiple Uri-Path options in the form '/path' into pkt.
Definition nanocoap.h:1846
static ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target, size_t max_len)
Convenience function for getting the packet's URI_QUERY option.
Definition nanocoap.h:1134
static ssize_t coap_get_location_path(coap_pkt_t *pkt, char *target, size_t max_len)
Convenience function for getting the packet's LOCATION_PATH option.
Definition nanocoap.h:1072
static int coap_opt_put_block2(coap_builder_t *state, coap_block_slicer_t *slicer)
Insert block2 option into buffer.
Definition nanocoap.h:2160
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len, coap_request_ctx_t *ctx)
Handle incoming CoAP request.
static size_t coap_builder_buf_size(const coap_builder_t *state)
Get the size of the buffer in state.
Definition nanocoap.h:1985
static unsigned coap_get_id(const coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition nanocoap.h:610
bool coap_block_finish(coap_block_slicer_t *slicer)
Finish a block request (block1 or block2)
static int coap_opt_put_ct(coap_builder_t *state, uint16_t content_type)
Insert content type option into buffer.
Definition nanocoap.h:2533
static ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
Append an Accept option to the pkt buffer.
Definition nanocoap.h:1690
static int coap_opt_put_location_path(coap_builder_t *state, const char *location)
Convenience function for inserting LOCATION_PATH option into buffer.
Definition nanocoap.h:2321
static uint8_t coap_pkt_tkl_ext_len(const coap_pkt_t *pkt)
Get the size of the extended Token length field (RFC 8974)
Definition nanocoap.h:735
int coap_builder_add_payload(coap_builder_t *state, const void *pld, size_t pld_len)
Add a payload marker and payload with bounds checking.
static int coap_opt_put_uri_path(coap_builder_t *state, const char *uri)
Convenience function for inserting URI_PATH option into buffer.
Definition nanocoap.h:2367
void * coap_builder_allocate_payload(coap_builder_t *state, size_t pld_len)
Write the payload marker into the current position of state and allocate pld_len bytes of payload aft...
void coap_request_ctx_init(coap_request_ctx_t *ctx, sock_udp_ep_t *remote)
Initialize CoAP request context.
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
static int coap_opt_put_proxy_uri(coap_builder_t *state, const char *uri)
Convenience function for inserting PROXY_URI option into buffer.
Definition nanocoap.h:2435
static ssize_t coap_get_location_query(coap_pkt_t *pkt, char *target, size_t max_len)
Convenience function for getting the packet's LOCATION_QUERY option.
Definition nanocoap.h:1094
uint8_t * coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num, uint8_t **opt_pos, int *opt_len)
Get pointer to an option field, can be called in a loop if there are multiple options with the same n...
int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value)
Get a uint32 option value.
unsigned coap_get_accept(coap_pkt_t *pkt)
Get the Accept option value from a packet if present.
static const void * coap_hdr_get_token(const coap_udp_hdr_t *hdr)
Get the Token of a CoAP over UDP (DTLS) packet.
Definition nanocoap.h:899
uint16_t coap_method_flags_t
Method flag type.
Definition nanocoap.h:319
ssize_t coap_build_udp_hdr(void *buf, size_t buf_len, uint8_t type, const void *token, size_t token_len, uint8_t code, uint16_t id)
Build a CoAP over UDP header.
static coap_udp_hdr_t * coap_get_udp_hdr(coap_pkt_t *pkt)
Get the CoAP header of a CoAP over UDP packet.
Definition nanocoap.h:515
int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option)
Block option getter.
static unsigned coap_get_ver(const coap_pkt_t *pkt)
Get the CoAP version number.
Definition nanocoap.h:689
ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt, uint8_t **value, bool init_opt)
Iterate over a packet's options.
ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU in UDP / DTLS format.
static bool coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition nanocoap.h:1320
int coap_opt_put_uint(coap_builder_t *state, uint16_t onum, uint32_t value)
Encode the given uint option into buffer.
static coap_method_t coap_get_method(const coap_pkt_t *pkt)
Get a request's method type.
Definition nanocoap.h:598
static unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
Get the total header length (4-byte header + token length)
Definition nanocoap.h:783
ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, size_t resp_buf_len, coap_request_ctx_t *context)
Generic coap subtree handler.
static void coap_pkt_set_type(coap_pkt_t *pkt, unsigned type)
Set the message type for the given CoAP packet.
Definition nanocoap.h:933
static unsigned coap_get_code_class(const coap_pkt_t *pkt)
Get a message's code class (3 most significant bits of code)
Definition nanocoap.h:562
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition nanocoap.h:1483
static uint8_t coap_hdr_tkl_ext_len(const coap_udp_hdr_t *hdr)
Get the size of the extended Token length field (RFC 8974)
Definition nanocoap.h:706
int coap_opt_put_uri_pathquery(coap_builder_t *state, const char *uri)
Convenience function for inserting URI_PATH and URI_QUERY into buffer This function will automaticall...
static size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr)
Get the token length of a CoAP over UDP (DTLS) packet.
Definition nanocoap.h:859
static ssize_t coap_builder_msg_size(const coap_builder_t *state)
Get the size of the CoAP message in state.
Definition nanocoap.h:1966
int coap_blockwise_put_bytes(coap_builder_t *state, coap_block_slicer_t *slicer, const void *c, size_t len)
Add a byte array to a block2 reply when building the response using a coap_builder_t structure to ass...
int coap_blockwise_put_bytes_pkt(coap_pkt_t *pdu, coap_block_slicer_t *slicer, const void *c, size_t len)
Add a byte array to a block2 reply when building the response using a coap_pkt_t structure to assembl...
static ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string, char separator)
Encode the given string as option(s) into pkt.
Definition nanocoap.h:1826
ssize_t coap_build_empty_ack(const coap_pkt_t *pkt, coap_udp_hdr_t *ack)
Build empty reply to CoAP request.
WARN_UNUSED_RESULT int coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
int coap_put_block1_ok(coap_builder_t *state, coap_block1_t *block1)
Insert block1 option into buffer (from coap_block1_t)
static unsigned coap_get_code_detail(const coap_pkt_t *pkt)
Get a message's code detail (5 least significant bits of code)
Definition nanocoap.h:574
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition nanocoap.h:1469
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
Resource handler type.
Definition nanocoap.h:285
ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c)
Add a single character to the payload data of the CoAP request.
ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
Reference to the default .well-known/core handler defined by the application.
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition nanocoap.h:2801
bool coap_find_uri_query(coap_pkt_t *pkt, const char *key, const char **value, size_t *len)
Find a URI query option of the packet.
static uint32_t coap_get_observe(coap_pkt_t *pkt)
Get the value of the observe option from the given packet.
Definition nanocoap.h:2971
static ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block)
Encode the given block2 option in control use.
Definition nanocoap.h:1671
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
Definition nanocoap.h:677
static int coap_opt_put_uri_query(coap_builder_t *state, const char *uri_query)
Convenience function for inserting URI_QUERY option into buffer.
Definition nanocoap.h:2388
static int coap_opt_put_string(coap_builder_t *state, uint16_t optnum, const char *string, char separator)
Encode the given string as multi-part option into buffer.
Definition nanocoap.h:2298
static unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
Get a message's code in decimal format ((class * 100) + detail)
Definition nanocoap.h:586
static void coap_pkt_set_tkl(coap_pkt_t *pkt, uint8_t tkl)
Set the message token length for the given CoAP packet.
Definition nanocoap.h:952
static int coap_opt_put_block1(coap_builder_t *state, coap_block_slicer_t *slicer)
Insert block1 option into buffer.
Definition nanocoap.h:2138
static unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
Get the header length a response to the given packet will have.
Definition nanocoap.h:796
ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value)
Retrieve the value for an option as an opaque array of bytes.
static int coap_opt_put_block2_control(coap_builder_t *state, coap_block1_t *block)
Insert block2 option into buffer in control usage.
Definition nanocoap.h:2227
static int coap_opt_put_block1_control(coap_builder_t *state, coap_block1_t *block)
Insert block1 option into buffer in control usage.
Definition nanocoap.h:2202
int coap_iterate_uri_query(coap_pkt_t *pkt, void **ctx, char *key, size_t key_len_max, char *value, size_t value_len_max)
Iterate over a packet's URI Query options.
ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len)
Encode the given buffer as an opaque data option into pkt.
static ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt, const char *path, size_t path_len)
Adds one or multiple Uri-Path options in the form '/path' into pkt.
Definition nanocoap.h:1866
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
Definition nanocoap.h:648
int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx)
Generic block option getter.
ssize_t coap_reply_simple(coap_pkt_t *pkt, uint8_t code, uint8_t *buf, size_t len, uint16_t ct, const void *payload, size_t payload_len)
Create CoAP reply (convenience function)
static const coap_udp_hdr_t * coap_get_udp_hdr_const(const coap_pkt_t *pkt)
Same as coap_get_udp_hdr but for const memory.
Definition nanocoap.h:524
static uint8_t * coap_hdr_data_ptr(const coap_udp_hdr_t *hdr)
Get the start of data after the header.
Definition nanocoap.h:771
static ssize_t coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Alias for coap_parse_udp.
Definition nanocoap.h:2767
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition nanocoap.h:1301
static int coap_opt_put_observe(coap_builder_t *state, uint32_t obs)
Insert an CoAP Observe Option into the buffer.
Definition nanocoap.h:2251
void * coap_request_ctx_get_context(const coap_request_ctx_t *ctx)
Get resource context associated with a CoAP request.
ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code, void *buf, size_t len, uint16_t ct, void **payload, size_t *payload_len_max)
Create CoAP reply header (convenience function)
ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri)
Adds a single Proxy-URI option into pkt.
static size_t coap_builder_buf_remaining(const coap_builder_t *state)
Get the remaining free buffer space in state.
Definition nanocoap.h:1997
int(* coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
Coap blockwise request callback descriptor.
Definition nanocoap.h:300
ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned max_data_len)
Build reply to CoAP request.
static unsigned coap_size2szx(unsigned len)
Helper to encode byte size into next equal or smaller SZX value.
Definition nanocoap.h:1537
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:312
static int coap_opt_put_location_query(coap_builder_t *state, const char *location)
Convenience function for inserting LOCATION_QUERY option into buffer.
Definition nanocoap.h:2344
WARN_UNUSED_RESULT int coap_builder_init_reply(coap_builder_t *state, void *buf, size_t buf_len, coap_pkt_t *req, uint8_t code)
Initialize state for building a response and add the response header with matching token and suitable...
const char * coap_request_ctx_get_path(const coap_request_ctx_t *ctx)
Get resource path associated with a CoAP request.
ssize_t coap_opt_add_block(coap_pkt_t *pkt, coap_block_slicer_t *slicer, bool more, uint16_t option)
Add block option in descriptive use from a slicer object.
static ssize_t coap_get_uri_path(coap_pkt_t *pkt, char *target)
Convenience function for getting the packet's URI_PATH.
Definition nanocoap.h:1115
const sock_udp_ep_t * coap_request_ctx_get_remote_udp(const coap_request_ctx_t *ctx)
Get the remote endpoint from which the request was received.
static void coap_hdr_set_type(coap_udp_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition nanocoap.h:835
static bool coap_builder_has_overflown(const coap_builder_t *state)
Check if building the message failed due to insufficient buffer space.
Definition nanocoap.h:1951
const sock_udp_ep_t * coap_request_ctx_get_local_udp(const coap_request_ctx_t *ctx)
Get the local endpoint on which the request has been received.
WARN_UNUSED_RESULT int coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, size_t blksize)
Initialize a block slicer struct from content information.
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more)
Initialize a block struct from content information.
ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars, size_t chars_len, char separator)
Encode the given array of characters as option(s) into pkt.
static void coap_hdr_set_code(coap_udp_hdr_t *hdr, uint8_t code)
Write the given raw message code to given CoAP header.
Definition nanocoap.h:809
ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum)
Removes an option previously added with function in the coap_opt_add_...() group.
unsigned coap_get_content_type(coap_pkt_t *pkt)
Get content type from packet.
static ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
Convenience function for getting the packet's Proxy-Uri option.
Definition nanocoap.h:1246
static size_t coap_hdr_len(const coap_udp_hdr_t *hdr)
Get the header length of a CoAP packet.
Definition nanocoap.h:920
static int coap_builder_add_payload_marker(coap_builder_t *state)
Add a payload marker with bounds checking.
Definition nanocoap.h:2096
ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
Encode the given uint option into pkt.
static unsigned coap_get_total_len(const coap_pkt_t *pkt)
Get the total length of a CoAP packet in the packet buffer.
Definition nanocoap.h:662
int coap_opt_put_string_with_len(coap_builder_t *state, uint16_t optnum, const char *string, size_t len, char separator)
Encode the given string as multi-part option into buffer.
struct _coap_request_ctx coap_request_ctx_t
Forward declaration of internal CoAP resource request handler context.
Definition nanocoap.h:261
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition nanocoap.h:2949
int coap_opt_put(coap_builder_t *state, uint16_t onum, const void *odata, size_t olen)
Insert a CoAP option into buffer.
static ssize_t coap_opt_add_block1(coap_pkt_t *pkt, coap_block_slicer_t *slicer, bool more)
Add block1 option in descriptive use from a slicer object.
Definition nanocoap.h:1595
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition nanocoap.h:2740
static ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block)
Encode the given block1 option in control use.
Definition nanocoap.h:1653
ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len, coap_request_ctx_t *ctx, const coap_resource_t *resources, size_t resources_numof)
Pass a coap request to a matching handler.
static void coap_set_id(coap_pkt_t *pkt, uint16_t id)
Set the message ID of the given CoAP packet.
Definition nanocoap.h:621
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition nanocoap.h:636
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition nanocoap.h:2959
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
coap_udp_hdr_t coap_hdr_t
Alias for coap_udp_hdr_t for backward compatibility.
Definition nanocoap.h:198
int coap_builder_init(coap_builder_t *state, void *buf, size_t buf_len, size_t header_len)
Initialize state for building a message.
ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len, const char *val, size_t val_len)
Adds a single Uri-Query option in the form 'key=value' into pkt.
static int coap_opt_put_block1_raw(coap_builder_t *state, unsigned blknum, unsigned szx, int more)
Insert block1 option into buffer.
Definition nanocoap.h:2510
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definition udp.h:292
struct iolist iolist_t
iolist forward declaration
Definition iolist.h:30
iolist scatter / gather IO
Common macros and compiler attributes/pragmas configuration.
#define IS_USED(module)
Checks whether a module is being used or not.
Definition modules.h:67
Generic CoAP values as defined by RFC7252.
UDP sock definitions.
CoAP resource request handler context.
Definition nanocoap.h:351
sock_udp_ep_t * local
deprecated alias for local_udp
Definition nanocoap.h:360
const coap_resource_t * resource
resource of the request
Definition nanocoap.h:352
sock_udp_ep_t * local_udp
local UDP endpoint of the request
Definition nanocoap.h:359
sock_udp_ep_t * remote
deprecated alias for request_udp
Definition nanocoap.h:355
uint32_t tl_type
transport the packet was received over
Definition nanocoap.h:370
sock_udp_ep_t * remote_udp
remote UDP endpoint of the request
Definition nanocoap.h:354
Block1 helper struct.
Definition nanocoap.h:461
int8_t more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition nanocoap.h:465
uint32_t blknum
block number
Definition nanocoap.h:463
size_t offset
offset of received data
Definition nanocoap.h:462
uint8_t szx
szx value
Definition nanocoap.h:464
Blockwise transfer helper struct.
Definition nanocoap.h:472
size_t end
End offset of the current block.
Definition nanocoap.h:474
size_t start
Start offset of the current block.
Definition nanocoap.h:473
uint8_t * opt_value
Pointer to the value of the placed option.
Definition nanocoap.h:476
size_t cur
Offset of the generated content.
Definition nanocoap.h:475
Structure to hold the state for building a CoAP message.
Definition nanocoap.h:451
uint16_t last_opt_num
Number of the last option added (for delta-encoding)
Definition nanocoap.h:455
uint16_t pos
Position of the next byte to write within buf
Definition nanocoap.h:453
uint16_t size
Size of buf in bytes.
Definition nanocoap.h:454
uint8_t * buf
Buffer to build the message into.
Definition nanocoap.h:452
CoAP option array entry.
Definition nanocoap.h:203
uint16_t offset
offset in packet
Definition nanocoap.h:205
uint16_t opt_num
full CoAP option number
Definition nanocoap.h:204
CoAP PDU parsing context structure.
Definition nanocoap.h:226
coap_optpos_t options[CONFIG_NANOCOAP_NOPTS_MAX]
option offset array
Definition nanocoap.h:251
coap_udp_hdr_t * hdr
Deprecated alias for coap_pkt_t::buf.
Definition nanocoap.h:245
uint8_t * payload
pointer to end of the header
Definition nanocoap.h:247
uint16_t payload_len
length of payload
Definition nanocoap.h:249
uint16_t options_len
length of options array
Definition nanocoap.h:250
uint8_t * buf
pointer to the beginning of the buffer holding the pkt
Definition nanocoap.h:233
BITFIELD(opt_crit, CONFIG_NANOCOAP_NOPTS_MAX)
unhandled critical option
iolist_t * snips
payload snips (optional)
Definition nanocoap.h:248
Type for CoAP resource subtrees.
Definition nanocoap.h:334
const size_t resources_numof
number of entries in array
Definition nanocoap.h:336
const coap_resource_t * resources
ptr to resource array
Definition nanocoap.h:335
Type for CoAP resource entry.
Definition nanocoap.h:324
const char * path
URI path of resource.
Definition nanocoap.h:325
coap_method_flags_t methods
OR'ed methods this resource allows.
Definition nanocoap.h:326
coap_handler_t handler
ptr to resource handler
Definition nanocoap.h:327
void * context
ptr to user defined context data
Definition nanocoap.h:328
Raw CoAP over UDP PDU header structure.
Definition nanocoap.h:186
uint8_t code
CoAP code (e.g.m 205)
Definition nanocoap.h:188
uint8_t ver_t_tkl
version, token, token length
Definition nanocoap.h:187
uint16_t id
Req/resp ID.
Definition nanocoap.h:189
Cross File Arrays.