Loading...
Searching...
No Matches
nanocoap.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-18 Kaspar Schleiser <kaspar@schleiser.de>
3 * 2018 Freie Universität Berlin
4 * 2018 Inria
5 * 2018 Ken Bannister <kb2ma@runbox.com>
6 *
7 * This file is subject to the terms and conditions of the GNU Lesser
8 * General Public License v2.1. See the file LICENSE in the top level
9 * directory for more details.
10 */
11
12#pragma once
13
78
79#include <assert.h>
80#include <stdint.h>
81#include <stdbool.h>
82#include <stddef.h>
83#include <string.h>
84#include <unistd.h>
85
86#include "bitarithm.h"
87#include "bitfield.h"
88#include "byteorder.h"
89#include "iolist.h"
90#include "macros/utils.h"
91#include "modules.h"
92#include "net/coap.h"
93#include "net/sock/udp.h"
94
95#if defined(MODULE_NANOCOAP_RESOURCES)
96#include "xfa.h"
97#endif
98
99#ifdef __cplusplus
100extern "C" {
101#endif
102
108#define COAP_GET (0x01)
109#define COAP_POST (0x02)
110#define COAP_PUT (0x04)
111#define COAP_DELETE (0x08)
112#define COAP_FETCH (0x10)
113#define COAP_PATCH (0x20)
114#define COAP_IPATCH (0x40)
115#define COAP_IGNORE (0xFF)
117#define COAP_MATCH_SUBTREE (0x8000)
120
124#define COAP_FORMAT_NONE (UINT16_MAX)
125
133#ifndef CONFIG_NANOCOAP_NOPTS_MAX
134#define CONFIG_NANOCOAP_NOPTS_MAX (16)
135#endif
136
141#ifndef CONFIG_NANOCOAP_URI_MAX
142#define CONFIG_NANOCOAP_URI_MAX (64)
143#endif
144
148#ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
149#define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
150#endif
151
155#ifndef CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT
156#define CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT COAP_BLOCKSIZE_64
157#endif
158
160#ifndef CONFIG_NANOCOAP_QS_MAX
161#define CONFIG_NANOCOAP_QS_MAX (64)
162#endif
164
170#ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX
171#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (80)
172#endif
173
181#define COAP_OPT_FINISH_NONE (0x0000)
183#define COAP_OPT_FINISH_PAYLOAD (0x0001)
185
189typedef struct __attribute__((packed)) {
190 uint8_t ver_t_tkl;
191 uint8_t code;
192 uint16_t id;
194
202
206typedef struct {
207 uint16_t opt_num;
208 uint16_t offset;
210
229typedef struct {
230 union {
236 uint8_t *buf;
249 };
250 uint8_t *payload;
252 uint16_t payload_len;
253 uint16_t options_len;
256#ifdef MODULE_GCOAP
257 uint32_t observe_value;
258#endif
259} coap_pkt_t;
260
265
288typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len,
289 coap_request_ctx_t *context);
290
303typedef int (*coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more);
304
315typedef int (*coap_request_cb_t)(void *arg, coap_pkt_t *pkt);
316
322typedef uint16_t coap_method_flags_t;
323
333
337typedef const struct {
339 const size_t resources_numof;
341
349
356 union {
359 };
360#if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN
361 union {
364 };
365#endif
366#if defined(MODULE_GCOAP) || DOXYGEN
373 uint32_t tl_type;
374#endif
375};
376
377/* forward declarations */
378static inline uint8_t *coap_hdr_data_ptr(const coap_udp_hdr_t *hdr);
379static inline size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr);
380static inline const void *coap_hdr_get_token(const coap_udp_hdr_t *hdr);
381
390
399
409
419
429
454typedef struct {
455 uint8_t *buf;
456 uint16_t pos;
457 uint16_t size;
458 uint16_t last_opt_num;
460
464typedef struct {
465 size_t offset;
466 uint32_t blknum;
467 uint8_t szx;
468 int8_t more;
471
475typedef struct {
476 size_t start;
477 size_t end;
478 size_t cur;
479 uint8_t *opt_value;
481
482#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
488#define NANOCOAP_RESOURCE(name) \
489 XFA_CONST(coap_resource_t, coap_resources_xfa, 0) CONCAT(coap_resource_, name) =
490#else
496extern const coap_resource_t coap_resources[];
497
503extern const unsigned coap_resources_numof;
504#endif
505
519{
520 /* currently only UDP as transport supported */
521 return (coap_udp_hdr_t *)pkt->buf;
522}
523
527static inline const coap_udp_hdr_t *coap_get_udp_hdr_const(const coap_pkt_t *pkt)
528{
529 /* currently only UDP as transport supported */
530 return (const coap_udp_hdr_t *)pkt->buf;
531}
532
541static inline uint8_t coap_code(unsigned cls, unsigned detail)
542{
543 return (cls << 5) | detail;
544}
545
553static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt)
554{
555 return coap_get_udp_hdr_const(pkt)->code;
556}
557
565static inline unsigned coap_get_code_class(const coap_pkt_t *pkt)
566{
567 return coap_get_code_raw(pkt) >> 5;
568}
569
577static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
578{
579 return coap_get_code_raw(pkt) & 0x1f;
580}
581
589static inline unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
590{
591 return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
592}
593
601static inline coap_method_t coap_get_method(const coap_pkt_t *pkt)
602{
603 return coap_get_code_raw(pkt);
604}
605
613static inline unsigned coap_get_id(const coap_pkt_t *pkt)
614{
615 return ntohs(coap_get_udp_hdr_const(pkt)->id);
616}
617
624static inline void coap_set_id(coap_pkt_t *pkt, uint16_t id)
625{
626 coap_get_udp_hdr(pkt)->id = htons(id);
627}
628
639static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
640{
641 return coap_hdr_get_token_len((const coap_udp_hdr_t *)pkt->buf);
642}
643
651static inline void *coap_get_token(const coap_pkt_t *pkt)
652{
654}
655
665static inline unsigned coap_get_total_len(const coap_pkt_t *pkt)
666{
667 return (uintptr_t)pkt->payload - (uintptr_t)pkt->buf + pkt->payload_len;
668}
669
680static inline unsigned coap_get_type(const coap_pkt_t *pkt)
681{
682 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x30) >> 4;
683}
684
692static inline unsigned coap_get_ver(const coap_pkt_t *pkt)
693{
694 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x60) >> 6;
695}
696
709static inline uint8_t coap_hdr_tkl_ext_len(const coap_udp_hdr_t *hdr)
710{
711 if (!IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
712 return 0;
713 }
714
715 switch (hdr->ver_t_tkl & 0x0f) {
716 case 13:
717 return 1;
718 case 14:
719 return 2;
720 case 15:
721 assert(0);
722 /* fall-through */
723 default:
724 return 0;
725 }
726}
727
738static inline uint8_t coap_pkt_tkl_ext_len(const coap_pkt_t *pkt)
739{
741}
742
763bool coap_is_hdr_in_bounds(const coap_pkt_t *pkt, size_t len);
764
774static inline uint8_t *coap_hdr_data_ptr(const coap_udp_hdr_t *hdr)
775{
776 return ((uint8_t *)hdr) + sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(hdr);
777}
778
786static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
787{
788 return sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) +
790}
791
799static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
800{
801 return coap_get_total_hdr_len(pkt);
802}
803
812static inline void coap_hdr_set_code(coap_udp_hdr_t *hdr, uint8_t code)
813{
814 hdr->code = code;
815}
816
823static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
824{
826}
827
838static inline void coap_hdr_set_type(coap_udp_hdr_t *hdr, unsigned type)
839{
840 /* assert correct range of type */
841 assert(!(type & ~0x3));
842
843 hdr->ver_t_tkl &= ~0x30;
844 hdr->ver_t_tkl |= type << 4;
845}
846
862static inline size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr)
863{
864 const uint8_t *buf = (const void *)hdr;
865 /* Regarding use unnamed magic numbers 13 and 269:
866 * - If token length is < 13 it fits into TKL field (4 bit)
867 * - If token length is < 269 it fits into 8-bit extended TKL field
868 * - Otherwise token length goes into 16-bit extended TKL field.
869 *
870 * (Not using named constants here, as RFC 8974 also has no names for those
871 * magic numbers.)
872 *
873 * See: https://www.rfc-editor.org/rfc/rfc8974#name-extended-token-length-tkl-f
874 */
875 switch (coap_hdr_tkl_ext_len(hdr)) {
876 case 0:
877 return hdr->ver_t_tkl & 0x0f;
878 case 1:
879 return buf[sizeof(coap_udp_hdr_t)] + 13;
880 case 2:
881 return byteorder_bebuftohs(buf + sizeof(coap_udp_hdr_t)) + 269;
882 }
883
884 return 0;
885}
886
902static inline const void * coap_hdr_get_token(const coap_udp_hdr_t *hdr)
903{
904 uint8_t *token = (void *)hdr;
905 token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr);
906 return token;
907}
908
923static inline size_t coap_hdr_len(const coap_udp_hdr_t *hdr)
924{
925 return sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr) + coap_hdr_get_token_len(hdr);
926}
927
936static inline void coap_pkt_set_type(coap_pkt_t *pkt, unsigned type)
937{
939
940 if (hdr) {
941 coap_hdr_set_type(hdr, type);
942 }
943}
944
955static inline void coap_pkt_set_tkl(coap_pkt_t *pkt, uint8_t tkl)
956{
957 coap_udp_hdr_t * hdr = coap_get_udp_hdr(pkt);
958 hdr->ver_t_tkl &= 0xf0;
959 hdr->ver_t_tkl |= (tkl & 0x0f);
960}
961
962
979
989uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num);
990
1003uint8_t *coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num,
1004 uint8_t **opt_pos, int *opt_len);
1005
1015
1025
1038int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
1039
1057ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum,
1058 char *target, size_t max_len, char separator);
1059
1075static inline ssize_t coap_get_location_path(coap_pkt_t *pkt,
1076 char *target, size_t max_len)
1077{
1078 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
1079 target, max_len, '/');
1080}
1081
1097static inline ssize_t coap_get_location_query(coap_pkt_t *pkt,
1098 char *target, size_t max_len)
1099{
1100 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
1101 target, max_len, '&');
1102}
1103
1118static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, char *target)
1119{
1120 return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
1122}
1123
1137static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
1138 size_t max_len)
1139{
1140 return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY,
1141 target, max_len, '&');
1142}
1143
1158bool coap_find_uri_query(coap_pkt_t *pkt, const char *key,
1159 const char **value, size_t *len);
1160
1183 char *key, size_t key_len_max,
1184 char *value, size_t value_len_max);
1185
1216 uint8_t **value, bool init_opt);
1217
1234ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
1236
1249static inline ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
1250{
1251 return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
1252}
1253
1271void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
1272 int more);
1273
1289
1304static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
1305{
1306 return coap_block_finish(slicer);
1307}
1308
1323static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
1324{
1325 return coap_block_finish(slicer);
1326}
1327
1338
1349 size_t blksize);
1350
1369 const void *c, size_t len);
1370
1389 const void *c, size_t len);
1390
1408
1426
1445int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
1446
1464static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
1465{
1466 return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
1467}
1468
1478static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
1479{
1480 return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
1481}
1482
1495int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx);
1496
1515
1523#define coap_szx2size(szx) (1U << ((szx) + 4))
1524
1532static inline unsigned coap_size2szx(unsigned len)
1533{
1534 assert(len >= 16);
1535 return bitarithm_msb(len >> 4);
1536}
1537
1538
1570 bool more, uint16_t option);
1571
1590static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
1591 coap_block_slicer_t *slicer, bool more)
1592{
1593 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
1594}
1595
1614static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
1615 coap_block_slicer_t *slicer, bool more)
1616{
1617 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
1618}
1619
1633ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
1634
1648static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
1649 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
1650 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1651}
1652
1666static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
1667 /* block.more must be zero, so no need to 'or' it in */
1668 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
1669 (block->blknum << 4) | block->szx);
1670}
1671
1685static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
1686{
1687 return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
1688}
1689
1703static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1704{
1705 return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1706}
1707
1723ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len);
1724
1742ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1743 const char *val, size_t val_len);
1744
1761static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1762 const char *val)
1763{
1764 return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1765}
1766
1781ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1782
1801ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1802 size_t chars_len, char separator);
1803
1821static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1822 const char *string, char separator)
1823{
1824 return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1825}
1826
1841static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1842{
1843 return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1844}
1845
1861static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1862 const char *path,
1863 size_t path_len)
1864{
1865 return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1866}
1867
1880ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1881
1899ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum);
1901
1935int coap_builder_init(coap_builder_t *state, void *buf, size_t buf_len,
1936 size_t header_len);
1937
1946static inline bool coap_builder_has_overflown(const coap_builder_t *state)
1947{
1948 return (state->size == 0);
1949}
1950
1961static inline ssize_t coap_builder_msg_size(const coap_builder_t *state)
1962{
1963 if (coap_builder_has_overflown(state)) {
1964 return -EOVERFLOW;
1965 }
1966
1967 return state->pos;
1968}
1969
1980static inline size_t coap_builder_buf_size(const coap_builder_t *state)
1981{
1982 return state->size;
1983}
1984
1992static inline size_t coap_builder_buf_remaining(const coap_builder_t *state)
1993{
1994 if (state->size > state->pos) {
1995 return state->size - state->pos;
1996 }
1997
1998 return 0;
1999}
2000
2024int coap_builder_init_reply(coap_builder_t *state, void *buf, size_t buf_len,
2025 coap_pkt_t *req, uint8_t code);
2026
2048int coap_builder_add_payload(coap_builder_t *state, const void *pld, size_t pld_len);
2049
2076void * coap_builder_allocate_payload(coap_builder_t *state, size_t pld_len);
2077
2092{
2093 return coap_builder_add_payload(state, NULL, 0);
2094}
2095
2115 uint16_t option);
2116
2133static inline int coap_opt_put_block1(coap_builder_t *state,
2134 coap_block_slicer_t *slicer)
2135{
2136 return coap_opt_put_block(state, slicer, COAP_OPT_BLOCK1);
2137}
2138
2155static inline int coap_opt_put_block2(coap_builder_t *state,
2156 coap_block_slicer_t *slicer)
2157{
2158 return coap_opt_put_block(state, slicer, COAP_OPT_BLOCK2);
2159}
2160
2179int coap_opt_put_uint(coap_builder_t *state, uint16_t onum, uint32_t value);
2180
2198 coap_block1_t *block)
2199{
2200 return coap_opt_put_uint(state, COAP_OPT_BLOCK1,
2201 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
2202}
2203
2223 coap_block1_t *block)
2224{
2225 /* block.more must be zero, so no need to 'or' it in */
2226 return coap_opt_put_uint(state, COAP_OPT_BLOCK2,
2227 (block->blknum << 4) | block->szx);
2228}
2229
2246static inline int coap_opt_put_observe(coap_builder_t *state, uint32_t obs)
2247{
2248 obs &= COAP_OBS_MAX_VALUE_MASK; /* trim obs down to 24 bit */
2249 return coap_opt_put_uint(state, COAP_OPT_OBSERVE, obs);
2250}
2251
2273 const char *string, size_t len, char separator);
2293static inline int coap_opt_put_string(coap_builder_t *state, uint16_t optnum,
2294 const char *string, char separator)
2295{
2296 return coap_opt_put_string_with_len(state, optnum,
2297 string, strlen(string), separator);
2298}
2299
2317 const char *location)
2318{
2319 return coap_opt_put_string(state, COAP_OPT_LOCATION_PATH,
2320 location, '/');
2321}
2322
2340 const char *location)
2341{
2342 return coap_opt_put_string(state, COAP_OPT_LOCATION_QUERY,
2343 location, '&');
2344}
2345
2362static inline int coap_opt_put_uri_path(coap_builder_t *state, const char *uri)
2363{
2364 return coap_opt_put_string(state, COAP_OPT_URI_PATH, uri, '/');
2365}
2366
2384 const char *uri_query)
2385{
2386 return coap_opt_put_string(state, COAP_OPT_URI_QUERY, uri_query, '&');
2387}
2388
2412int coap_opt_put_uri_pathquery(coap_builder_t *state, const char *uri);
2413
2430static inline int coap_opt_put_proxy_uri(coap_builder_t *state, const char *uri)
2431{
2432 return coap_opt_put_string(state, COAP_OPT_PROXY_URI, uri, '\0');
2433}
2434
2460
2485int coap_opt_put(coap_builder_t *state, uint16_t onum, const void *odata, size_t olen);
2486
2506 unsigned blknum, unsigned szx, int more)
2507{
2508 return coap_opt_put_uint(state, COAP_OPT_BLOCK1,
2509 (blknum << 4) | szx | (more ? 0x8 : 0));
2510}
2511
2528static inline int coap_opt_put_ct(coap_builder_t *state, uint16_t content_type)
2529{
2530 return coap_opt_put_uint(state, COAP_OPT_CONTENT_FORMAT, content_type);
2531}
2532
2533
2553ssize_t coap_build_udp_hdr(void *buf, size_t buf_len, uint8_t type, const void *token,
2554 size_t token_len, uint8_t code, uint16_t id);
2576static inline ssize_t coap_build_hdr(coap_udp_hdr_t *hdr, unsigned type, const void *token,
2577 size_t token_len, unsigned code, uint16_t id)
2578{
2579 size_t fingers_crossed_size = sizeof(*hdr) + token_len;
2580
2581 if (IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
2582 /* With RFC 8974, we have an additional extended TKL
2583 * field of 0-4 bytes in length */
2584 fingers_crossed_size += 4;
2585 }
2586
2587 return coap_build_udp_hdr(hdr, fingers_crossed_size, type, token, token_len, code, id);
2588}
2589
2651ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
2652 uint8_t *rbuf, unsigned rlen, unsigned max_data_len);
2653
2669
2684ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
2685 coap_request_ctx_t *ctx);
2686
2703ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2704 unsigned resp_buf_len, coap_request_ctx_t *ctx,
2705 const coap_resource_t *resources,
2706 size_t resources_numof);
2707
2725ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2726 size_t resp_buf_len, coap_request_ctx_t *context);
2727
2735static inline coap_method_flags_t coap_method2flag(unsigned code)
2736{
2737 return (1 << (code - 1));
2738}
2739
2755ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len);
2756
2762static inline ssize_t coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
2763{
2764 return coap_parse_udp(pkt, buf, len);
2765}
2766
2781void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
2782
2796static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
2797{
2798 pkt->payload += len;
2799 pkt->payload_len -= len;
2800}
2801
2823ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
2824
2838ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
2839
2865ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code,
2866 void *buf, size_t len, uint16_t ct,
2867 void **payload, size_t *payload_len_max);
2868
2893 uint8_t code,
2894 uint8_t *buf, size_t len,
2895 uint16_t ct,
2896 const void *payload, size_t payload_len);
2897
2903 uint8_t *buf, size_t len,
2904 coap_request_ctx_t *context);
2906
2910#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
2911#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE !IS_USED(MODULE_GCOAP)
2912#endif
2913
2929int coap_match_path(const coap_resource_t *resource, const char *uri);
2930
2931#if defined(MODULE_GCOAP) || defined(DOXYGEN)
2944static inline bool coap_has_observe(coap_pkt_t *pkt)
2945{
2946 return pkt->observe_value != UINT32_MAX;
2947}
2948
2954static inline void coap_clear_observe(coap_pkt_t *pkt)
2955{
2956 pkt->observe_value = UINT32_MAX;
2957}
2958
2966static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
2967{
2968 return pkt->observe_value;
2969}
2970
2971#endif
2972
2976#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2977 { \
2978 .path = "/.well-known/core", \
2979 .methods = COAP_GET, \
2980 .handler = coap_well_known_core_default_handler \
2981 }
2982
2983#ifdef __cplusplus
2984}
2985#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
bitfields 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:535
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:550
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:520
#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:532
coap_method_t
CoAP method codes used in request.
Definition coap.h:170
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
Definition nanocoap.h:134
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition nanocoap.h:142
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:1761
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:823
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:2576
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:1614
static unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition nanocoap.h:553
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:1703
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:541
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:1841
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:1137
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:1075
static int coap_opt_put_block2(coap_builder_t *state, coap_block_slicer_t *slicer)
Insert block2 option into buffer.
Definition nanocoap.h:2155
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:1980
static unsigned coap_get_id(const coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition nanocoap.h:613
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:2528
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:1685
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:2316
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:738
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:2362
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:2430
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:1097
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:902
uint16_t coap_method_flags_t
Method flag type.
Definition nanocoap.h:322
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:518
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:692
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:1323
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:601
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:786
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:936
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:565
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition nanocoap.h:1478
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:709
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:862
static ssize_t coap_builder_msg_size(const coap_builder_t *state)
Get the size of the CoAP message in state.
Definition nanocoap.h:1961
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...
void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, size_t blksize)
Initialize a block slicer struct from content information.
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:1821
ssize_t coap_build_empty_ack(const coap_pkt_t *pkt, coap_udp_hdr_t *ack)
Build empty reply to CoAP request.
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:577
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition nanocoap.h:1464
void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
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:288
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:2796
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:2966
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:1666
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
Definition nanocoap.h:680
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:2383
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:2293
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:589
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:955
static int coap_opt_put_block1(coap_builder_t *state, coap_block_slicer_t *slicer)
Insert block1 option into buffer.
Definition nanocoap.h:2133
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:799
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:2222
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:2197
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:1861
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
Definition nanocoap.h:651
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:527
static uint8_t * coap_hdr_data_ptr(const coap_udp_hdr_t *hdr)
Get the start of data after the header.
Definition nanocoap.h:774
static ssize_t coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Alias for coap_parse_udp.
Definition nanocoap.h:2762
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition nanocoap.h:1304
static int coap_opt_put_observe(coap_builder_t *state, uint32_t obs)
Insert an CoAP Observe Option into the buffer.
Definition nanocoap.h:2246
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:1992
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:303
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:1532
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:315
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:2339
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:1118
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:838
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:1946
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.
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:812
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:1249
static size_t coap_hdr_len(const coap_udp_hdr_t *hdr)
Get the header length of a CoAP packet.
Definition nanocoap.h:923
static int coap_builder_add_payload_marker(coap_builder_t *state)
Add a payload marker with bounds checking.
Definition nanocoap.h:2091
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:665
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:264
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition nanocoap.h:2944
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:1590
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition nanocoap.h:2735
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:1648
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:624
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition nanocoap.h:639
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition nanocoap.h:2954
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:201
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:2505
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definition udp.h:295
struct iolist iolist_t
iolist forward declaration
Definition iolist.h:33
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:354
sock_udp_ep_t * local
deprecated alias for local_udp
Definition nanocoap.h:363
const coap_resource_t * resource
resource of the request
Definition nanocoap.h:355
sock_udp_ep_t * local_udp
local UDP endpoint of the request
Definition nanocoap.h:362
sock_udp_ep_t * remote
deprecated alias for request_udp
Definition nanocoap.h:358
uint32_t tl_type
transport the packet was received over
Definition nanocoap.h:373
sock_udp_ep_t * remote_udp
remote UDP endpoint of the request
Definition nanocoap.h:357
Block1 helper struct.
Definition nanocoap.h:464
int8_t more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition nanocoap.h:468
uint32_t blknum
block number
Definition nanocoap.h:466
size_t offset
offset of received data
Definition nanocoap.h:465
uint8_t szx
szx value
Definition nanocoap.h:467
Blockwise transfer helper struct.
Definition nanocoap.h:475
size_t end
End offset of the current block.
Definition nanocoap.h:477
size_t start
Start offset of the current block.
Definition nanocoap.h:476
uint8_t * opt_value
Pointer to the value of the placed option.
Definition nanocoap.h:479
size_t cur
Offset of the generated content.
Definition nanocoap.h:478
Structure to hold the state for building a CoAP message.
Definition nanocoap.h:454
uint16_t last_opt_num
Number of the last option added (for delta-encoding)
Definition nanocoap.h:458
uint16_t pos
Position of the next byte to write within buf
Definition nanocoap.h:456
uint16_t size
Size of buf in bytes.
Definition nanocoap.h:457
uint8_t * buf
Buffer to build the message into.
Definition nanocoap.h:455
CoAP option array entry.
Definition nanocoap.h:206
uint16_t offset
offset in packet
Definition nanocoap.h:208
uint16_t opt_num
full CoAP option number
Definition nanocoap.h:207
CoAP PDU parsing context structure.
Definition nanocoap.h:229
coap_optpos_t options[CONFIG_NANOCOAP_NOPTS_MAX]
option offset array
Definition nanocoap.h:254
coap_udp_hdr_t * hdr
Deprecated alias for coap_pkt_t::buf.
Definition nanocoap.h:248
uint8_t * payload
pointer to end of the header
Definition nanocoap.h:250
uint16_t payload_len
length of payload
Definition nanocoap.h:252
uint16_t options_len
length of options array
Definition nanocoap.h:253
uint8_t * buf
pointer to the beginning of the buffer holding the pkt
Definition nanocoap.h:236
BITFIELD(opt_crit, CONFIG_NANOCOAP_NOPTS_MAX)
unhandled critical option
iolist_t * snips
payload snips (optional)
Definition nanocoap.h:251
Type for CoAP resource subtrees.
Definition nanocoap.h:337
const size_t resources_numof
number of entries in array
Definition nanocoap.h:339
const coap_resource_t * resources
ptr to resource array
Definition nanocoap.h:338
Type for CoAP resource entry.
Definition nanocoap.h:327
const char * path
URI path of resource.
Definition nanocoap.h:328
coap_method_flags_t methods
OR'ed methods this resource allows.
Definition nanocoap.h:329
coap_handler_t handler
ptr to resource handler
Definition nanocoap.h:330
void * context
ptr to user defined context data
Definition nanocoap.h:331
Raw CoAP over UDP PDU header structure.
Definition nanocoap.h:189
uint8_t code
CoAP code (e.g.m 205)
Definition nanocoap.h:191
uint8_t ver_t_tkl
version, token, token length
Definition nanocoap.h:190
uint16_t id
Req/resp ID.
Definition nanocoap.h:192
Cross File Arrays.