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
433typedef struct {
434 size_t offset;
435 uint32_t blknum;
436 uint8_t szx;
437 int8_t more;
440
444typedef struct {
445 size_t start;
446 size_t end;
447 size_t cur;
448 uint8_t *opt;
450
451#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
457#define NANOCOAP_RESOURCE(name) \
458 XFA_CONST(coap_resource_t, coap_resources_xfa, 0) CONCAT(coap_resource_, name) =
459#else
465extern const coap_resource_t coap_resources[];
466
472extern const unsigned coap_resources_numof;
473#endif
474
488{
489 /* currently only UDP as transport supported */
490 return (coap_udp_hdr_t *)pkt->buf;
491}
492
496static inline const coap_udp_hdr_t *coap_get_udp_hdr_const(const coap_pkt_t *pkt)
497{
498 /* currently only UDP as transport supported */
499 return (const coap_udp_hdr_t *)pkt->buf;
500}
501
510static inline uint8_t coap_code(unsigned cls, unsigned detail)
511{
512 return (cls << 5) | detail;
513}
514
522static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt)
523{
524 return coap_get_udp_hdr_const(pkt)->code;
525}
526
534static inline unsigned coap_get_code_class(const coap_pkt_t *pkt)
535{
536 return coap_get_code_raw(pkt) >> 5;
537}
538
546static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
547{
548 return coap_get_code_raw(pkt) & 0x1f;
549}
550
558static inline unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
559{
560 return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
561}
562
570static inline coap_method_t coap_get_method(const coap_pkt_t *pkt)
571{
572 return coap_get_code_raw(pkt);
573}
574
582static inline unsigned coap_get_id(const coap_pkt_t *pkt)
583{
584 return ntohs(coap_get_udp_hdr_const(pkt)->id);
585}
586
593static inline void coap_set_id(coap_pkt_t *pkt, uint16_t id)
594{
595 coap_get_udp_hdr(pkt)->id = htons(id);
596}
597
608static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
609{
610 return coap_hdr_get_token_len((const coap_udp_hdr_t *)pkt->buf);
611}
612
620static inline void *coap_get_token(const coap_pkt_t *pkt)
621{
623}
624
634static inline unsigned coap_get_total_len(const coap_pkt_t *pkt)
635{
636 return (uintptr_t)pkt->payload - (uintptr_t)pkt->buf + pkt->payload_len;
637}
638
649static inline unsigned coap_get_type(const coap_pkt_t *pkt)
650{
651 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x30) >> 4;
652}
653
661static inline unsigned coap_get_ver(const coap_pkt_t *pkt)
662{
663 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x60) >> 6;
664}
665
678static inline uint8_t coap_hdr_tkl_ext_len(const coap_udp_hdr_t *hdr)
679{
680 if (!IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
681 return 0;
682 }
683
684 switch (hdr->ver_t_tkl & 0xf) {
685 case 13:
686 return 1;
687 case 14:
688 return 2;
689 case 15:
690 assert(0);
691 /* fall-through */
692 default:
693 return 0;
694 }
695}
696
707static inline uint8_t coap_pkt_tkl_ext_len(const coap_pkt_t *pkt)
708{
710}
711
721static inline uint8_t *coap_hdr_data_ptr(const coap_udp_hdr_t *hdr)
722{
723 return ((uint8_t *)hdr) + sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(hdr);
724}
725
733static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
734{
735 return sizeof(coap_udp_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) +
737}
738
750static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
751{
752 return coap_get_total_hdr_len(pkt);
753}
754
763static inline void coap_hdr_set_code(coap_udp_hdr_t *hdr, uint8_t code)
764{
765 hdr->code = code;
766}
767
774static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
775{
777}
778
789static inline void coap_hdr_set_type(coap_udp_hdr_t *hdr, unsigned type)
790{
791 /* assert correct range of type */
792 assert(!(type & ~0x3));
793
794 hdr->ver_t_tkl &= ~0x30;
795 hdr->ver_t_tkl |= type << 4;
796}
797
813static inline size_t coap_hdr_get_token_len(const coap_udp_hdr_t *hdr)
814{
815 const uint8_t *buf = (const void *)hdr;
816 /* Regarding use unnamed magic numbers 13 and 269:
817 * - If token length is < 13 it fits into TKL field (4 bit)
818 * - If token length is < 269 it fits into 8-bit extended TKL field
819 * - Otherwise token length goes into 16-bit extended TKL field.
820 *
821 * (Not using named constants here, as RFC 8974 also has no names for those
822 * magic numbers.)
823 *
824 * See: https://www.rfc-editor.org/rfc/rfc8974#name-extended-token-length-tkl-f
825 */
826 switch (coap_hdr_tkl_ext_len(hdr)) {
827 case 0:
828 return hdr->ver_t_tkl & 0xf;
829 case 1:
830 return buf[sizeof(coap_udp_hdr_t)] + 13;
831 case 2:
832 return byteorder_bebuftohs(buf + sizeof(coap_udp_hdr_t)) + 269;
833 }
834
835 return 0;
836}
837
853static inline const void * coap_hdr_get_token(const coap_udp_hdr_t *hdr)
854{
855 uint8_t *token = (void *)hdr;
856 token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr);
857 return token;
858}
859
874static inline size_t coap_hdr_len(const coap_udp_hdr_t *hdr)
875{
876 return sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr) + coap_hdr_get_token_len(hdr);
877}
878
887static inline void coap_pkt_set_type(coap_pkt_t *pkt, unsigned type)
888{
890
891 if (hdr) {
892 coap_hdr_set_type(hdr, type);
893 }
894}
895
906static inline void coap_pkt_set_tkl(coap_pkt_t *pkt, uint8_t tkl)
907{
908 coap_udp_hdr_t * hdr = coap_get_udp_hdr(pkt);
909 hdr->ver_t_tkl &= 0xf0;
910 hdr->ver_t_tkl |= (tkl & 0x0f);
911}
912
913
930
940uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num);
941
954uint8_t *coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num,
955 uint8_t **opt_pos, int *opt_len);
956
966
976
989int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
990
1008ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum,
1009 uint8_t *target, size_t max_len, char separator);
1010
1026static inline ssize_t coap_get_location_path(coap_pkt_t *pkt,
1027 uint8_t *target, size_t max_len)
1028{
1029 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
1030 target, max_len, '/');
1031}
1032
1048static inline ssize_t coap_get_location_query(coap_pkt_t *pkt,
1049 uint8_t *target, size_t max_len)
1050{
1051 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
1052 target, max_len, '&');
1053}
1054
1069static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, uint8_t *target)
1070{
1071 return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
1073}
1074
1088static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
1089 size_t max_len)
1090{
1091 return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY,
1092 (uint8_t *)target, max_len, '&');
1093}
1094
1109bool coap_find_uri_query(coap_pkt_t *pkt, const char *key,
1110 const char **value, size_t *len);
1111
1134 char *key, size_t key_len_max,
1135 char *value, size_t value_len_max);
1136
1167 uint8_t **value, bool init_opt);
1168
1185ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
1187
1200static inline ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
1201{
1202 return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
1203}
1204
1222void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
1223 int more);
1224
1240bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
1241
1256static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
1257{
1258 return coap_block_finish(slicer, COAP_OPT_BLOCK1);
1259}
1260
1275static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
1276{
1277 return coap_block_finish(slicer, COAP_OPT_BLOCK2);
1278}
1279
1290
1301 size_t blksize);
1302
1317size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
1318 const void *c, size_t len);
1319
1333size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
1334
1353int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
1354
1372static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
1373{
1374 return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
1375}
1376
1386static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
1387{
1388 return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
1389}
1390
1403int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx);
1404
1423
1431#define coap_szx2size(szx) (1U << ((szx) + 4))
1432
1440static inline unsigned coap_size2szx(unsigned len)
1441{
1442 assert(len >= 16);
1443 return bitarithm_msb(len >> 4);
1444}
1445
1446
1478 bool more, uint16_t option);
1479
1498static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
1499 coap_block_slicer_t *slicer, bool more)
1500{
1501 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
1502}
1503
1522static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
1523 coap_block_slicer_t *slicer, bool more)
1524{
1525 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
1526}
1527
1541ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
1542
1556static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
1557 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
1558 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1559}
1560
1574static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
1575 /* block.more must be zero, so no need to 'or' it in */
1576 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
1577 (block->blknum << 4) | block->szx);
1578}
1579
1593static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
1594{
1595 return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
1596}
1597
1611static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1612{
1613 return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1614}
1615
1631ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len);
1632
1650ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1651 const char *val, size_t val_len);
1652
1669static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1670 const char *val)
1671{
1672 return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1673}
1674
1689ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1690
1709ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1710 size_t chars_len, char separator);
1711
1729static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1730 const char *string, char separator)
1731{
1732 return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1733}
1734
1749static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1750{
1751 return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1752}
1753
1769static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1770 const char *path,
1771 size_t path_len)
1772{
1773 return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1774}
1775
1788ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1789
1807ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum);
1809
1835size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1836 bool more, uint16_t option);
1837
1853static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1854 coap_block_slicer_t *slicer, bool more)
1855{
1856 return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1857}
1858
1874static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1875 coap_block_slicer_t *slicer, bool more)
1876{
1877 return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1878}
1879
1891size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1892 uint32_t value);
1893
1903static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1904 coap_block1_t *block)
1905{
1906 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1907 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1908}
1909
1921static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1922 coap_block1_t *block)
1923{
1924 /* block.more must be zero, so no need to 'or' it in */
1925 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1926 (block->blknum << 4) | block->szx);
1927}
1928
1938static inline size_t coap_opt_put_observe(uint8_t *buf, uint16_t lastonum,
1939 uint32_t obs)
1940{
1941 obs &= COAP_OBS_MAX_VALUE_MASK; /* trim obs down to 24 bit */
1942 return coap_opt_put_uint(buf, lastonum, COAP_OPT_OBSERVE, obs);
1943}
1944
1958size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1959 const char *string, size_t len, char separator);
1972static inline size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum,
1973 uint16_t optnum,
1974 const char *string, char separator)
1975{
1976 return coap_opt_put_string_with_len(buf, lastonum, optnum,
1977 string, strlen(string), separator);
1978}
1979
1990static inline size_t coap_opt_put_location_path(uint8_t *buf,
1991 uint16_t lastonum,
1992 const char *location)
1993{
1994 return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1995 location, '/');
1996}
1997
2008static inline size_t coap_opt_put_location_query(uint8_t *buf,
2009 uint16_t lastonum,
2010 const char *location)
2011{
2012 return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
2013 location, '&');
2014}
2015
2026static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
2027 const char *uri)
2028{
2029 return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
2030}
2031
2042static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
2043 const char *uri)
2044{
2045 return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
2046}
2047
2066size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri);
2067
2078static inline size_t coap_opt_put_proxy_uri(uint8_t *buf, uint16_t lastonum,
2079 const char *uri)
2080{
2081 return coap_opt_put_string(buf, lastonum, COAP_OPT_PROXY_URI, uri, '\0');
2082}
2083
2101size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
2102
2119size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen);
2120
2133static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
2134 unsigned blknum, unsigned szx, int more)
2135{
2136 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
2137 (blknum << 4) | szx | (more ? 0x8 : 0));
2138}
2139
2150static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
2151 uint16_t content_type)
2152{
2153 return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
2154}
2155
2156
2183ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
2184 uint8_t *rbuf, unsigned rlen, unsigned payload_len,
2185 coap_block_slicer_t *slicer);
2186
2200ssize_t coap_build_udp_hdr(void *buf, size_t buf_len, uint8_t type, const void *token,
2201 size_t token_len, uint8_t code, uint16_t id);
2223static inline ssize_t coap_build_hdr(coap_udp_hdr_t *hdr, unsigned type, const void *token,
2224 size_t token_len, unsigned code, uint16_t id)
2225{
2226 size_t fingers_crossed_size = sizeof(*hdr) + token_len;
2227
2228 if (IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
2229 /* With RFC 8974, we have an additional extended TKL
2230 * field of 0-4 bytes in length */
2231 fingers_crossed_size += 4;
2232 }
2233
2234 return coap_build_udp_hdr(hdr, fingers_crossed_size, type, token, token_len, code, id);
2235}
2236
2296ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
2297 uint8_t *rbuf, unsigned rlen, unsigned max_data_len);
2298
2314
2329ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
2330 coap_request_ctx_t *ctx);
2331
2348ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2349 unsigned resp_buf_len, coap_request_ctx_t *ctx,
2350 const coap_resource_t *resources,
2351 size_t resources_numof);
2352
2370ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2371 size_t resp_buf_len, coap_request_ctx_t *context);
2372
2380static inline coap_method_flags_t coap_method2flag(unsigned code)
2381{
2382 return (1 << (code - 1));
2383}
2384
2400ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len);
2401
2407static inline ssize_t coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
2408{
2409 return coap_parse_udp(pkt, buf, len);
2410}
2411
2426void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
2427
2441static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
2442{
2443 pkt->payload += len;
2444 pkt->payload_len -= len;
2445}
2446
2468ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
2469
2483ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
2484
2510ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code,
2511 void *buf, size_t len, uint16_t ct,
2512 void **payload, size_t *payload_len_max);
2513
2538 unsigned code,
2539 uint8_t *buf, size_t len,
2540 uint16_t ct,
2541 const void *payload, size_t payload_len);
2542
2548 uint8_t *buf, size_t len,
2549 coap_request_ctx_t *context);
2551
2555#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
2556#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE !IS_USED(MODULE_GCOAP)
2557#endif
2558
2574int coap_match_path(const coap_resource_t *resource, const uint8_t *uri);
2575
2576#if defined(MODULE_GCOAP) || defined(DOXYGEN)
2589static inline bool coap_has_observe(coap_pkt_t *pkt)
2590{
2591 return pkt->observe_value != UINT32_MAX;
2592}
2593
2599static inline void coap_clear_observe(coap_pkt_t *pkt)
2600{
2601 pkt->observe_value = UINT32_MAX;
2602}
2603
2611static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
2612{
2613 return pkt->observe_value;
2614}
2615
2616#endif
2617
2621#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2622 { \
2623 .path = "/.well-known/core", \
2624 .methods = COAP_GET, \
2625 .handler = coap_well_known_core_default_handler \
2626 }
2627
2628#ifdef __cplusplus
2629}
2630#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
Various helper macros.
#define COAP_OBS_MAX_VALUE_MASK
observe value is 24 bits
Definition coap.h:521
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:1669
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:774
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:2223
static size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum, const char *uri)
Convenience function for inserting URI_QUERY option into buffer.
Definition nanocoap.h:2042
size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer, bool more, uint16_t option)
Insert block option into buffer.
static size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum, coap_block1_t *block)
Insert block2 option into buffer in control usage.
Definition nanocoap.h:1921
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:1522
static unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition nanocoap.h:522
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.
int coap_match_path(const coap_resource_t *resource, const uint8_t *uri)
Checks if a CoAP resource path matches a given URI.
static size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer, bool more)
Insert block1 option into buffer.
Definition nanocoap.h:1853
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:1611
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition nanocoap.h:510
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:1749
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:1088
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 unsigned coap_get_id(const coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition nanocoap.h:582
static ssize_t coap_get_location_query(coap_pkt_t *pkt, uint8_t *target, size_t max_len)
Convenience function for getting the packet's LOCATION_QUERY option.
Definition nanocoap.h:1048
static size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum, const char *uri)
Convenience function for inserting URI_PATH option into buffer.
Definition nanocoap.h:2026
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:1593
ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum, uint8_t *target, size_t max_len, char separator)
Read a full option as null terminated string into the target buffer.
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:707
size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum, uint32_t value)
Encode the given uint option into buffer.
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.
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:853
static size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum, uint16_t optnum, const char *string, char separator)
Encode the given string as multi-part option into buffer.
Definition nanocoap.h:1972
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:487
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:661
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.
static size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer, bool more)
Insert block2 option into buffer.
Definition nanocoap.h:1874
static size_t coap_opt_put_observe(uint8_t *buf, uint16_t lastonum, uint32_t obs)
Insert an CoAP Observe Option into the buffer.
Definition nanocoap.h:1938
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:1275
size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, const void *c, size_t len)
Add a byte array to a block2 reply.
static coap_method_t coap_get_method(const coap_pkt_t *pkt)
Get a request's method type.
Definition nanocoap.h:570
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:733
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:887
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:534
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition nanocoap.h:1386
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:678
ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len, coap_block_slicer_t *slicer)
Build reply to CoAP block2 request.
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:813
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_get_location_path(coap_pkt_t *pkt, uint8_t *target, size_t max_len)
Convenience function for getting the packet's LOCATION_PATH option.
Definition nanocoap.h:1026
size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t optnum, const char *string, size_t len, char separator)
Encode the given string as multi-part option into buffer.
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:1729
bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
Finish a block request (block1 or block2)
ssize_t coap_build_empty_ack(const coap_pkt_t *pkt, coap_udp_hdr_t *ack)
Build empty reply to CoAP request.
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:546
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition nanocoap.h:1372
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 size_t coap_opt_put_proxy_uri(uint8_t *buf, uint16_t lastonum, const char *uri)
Convenience function for inserting PROXY_URI option into buffer.
Definition nanocoap.h:2078
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition nanocoap.h:2441
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:2611
static size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more)
Insert block1 option into buffer.
Definition nanocoap.h:2133
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:1574
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
Definition nanocoap.h:649
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:558
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:906
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:750
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.
size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen)
Insert a CoAP option into buffer.
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:1769
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
Definition nanocoap.h:620
static size_t coap_opt_put_location_query(uint8_t *buf, uint16_t lastonum, const char *location)
Convenience function for inserting LOCATION_QUERY option into buffer.
Definition nanocoap.h:2008
int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx)
Generic block option getter.
size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c)
Add a single character to a block2 reply.
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:496
static size_t coap_opt_put_location_path(uint8_t *buf, uint16_t lastonum, const char *location)
Convenience function for inserting LOCATION_PATH option into buffer.
Definition nanocoap.h:1990
static uint8_t * coap_hdr_data_ptr(const coap_udp_hdr_t *hdr)
Get the start of data after the header.
Definition nanocoap.h:721
static ssize_t coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Alias for coap_parse_udp.
Definition nanocoap.h:2407
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition nanocoap.h:1256
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)
static size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type)
Insert content type option into buffer.
Definition nanocoap.h:2150
ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri)
Adds a single Proxy-URI option into pkt.
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:1440
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:315
static size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum, coap_block1_t *block)
Insert block1 option into buffer in control usage.
Definition nanocoap.h:1903
ssize_t coap_reply_simple(coap_pkt_t *pkt, unsigned code, uint8_t *buf, size_t len, uint16_t ct, const void *payload, size_t payload_len)
Create CoAP reply (convenience function)
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.
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:789
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:763
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:1200
static ssize_t coap_get_uri_path(coap_pkt_t *pkt, uint8_t *target)
Convenience function for getting the packet's URI_PATH.
Definition nanocoap.h:1069
static size_t coap_hdr_len(const coap_udp_hdr_t *hdr)
Get the header length of a CoAP packet.
Definition nanocoap.h:874
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:634
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:2589
size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum)
Insert block1 option into buffer (from coap_block1_t)
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:1498
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition nanocoap.h:2380
size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri)
Convenience function for inserting URI_PATH and URI_QUERY into buffer This function will automaticall...
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:1556
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:593
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition nanocoap.h:608
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition nanocoap.h:2599
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
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.
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:433
int8_t more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition nanocoap.h:437
uint32_t blknum
block number
Definition nanocoap.h:435
size_t offset
offset of received data
Definition nanocoap.h:434
uint8_t szx
szx value
Definition nanocoap.h:436
Blockwise transfer helper struct.
Definition nanocoap.h:444
uint8_t * opt
Pointer to the placed option.
Definition nanocoap.h:448
size_t end
End offset of the current block.
Definition nanocoap.h:446
size_t start
Start offset of the current block.
Definition nanocoap.h:445
size_t cur
Offset of the generated content.
Definition nanocoap.h:447
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.