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
77#ifndef NET_NANOCOAP_H
78#define NET_NANOCOAP_H
79
80#include <assert.h>
81#include <errno.h>
82#include <stdint.h>
83#include <stdbool.h>
84#include <stddef.h>
85#include <string.h>
86#include <unistd.h>
87
88#ifdef RIOT_VERSION
89#include "bitarithm.h"
90#include "bitfield.h"
91#include "byteorder.h"
92#include "iolist.h"
93#include "macros/utils.h"
94#include "net/coap.h"
95#include "modules.h"
96#else
97#include "coap.h"
98#include <arpa/inet.h>
99#endif
100
101#if defined(MODULE_SOCK_UDP) || defined(DOXYGEN)
102#include "net/sock/udp.h"
103#else
104typedef void sock_udp_ep_t;
105#endif
106
107#if defined(MODULE_NANOCOAP_RESOURCES)
108#include "xfa.h"
109#endif
110
111#ifdef __cplusplus
112extern "C" {
113#endif
114
120#define COAP_GET (0x01)
121#define COAP_POST (0x02)
122#define COAP_PUT (0x04)
123#define COAP_DELETE (0x08)
124#define COAP_FETCH (0x10)
125#define COAP_PATCH (0x20)
126#define COAP_IPATCH (0x40)
127#define COAP_IGNORE (0xFF)
129#define COAP_MATCH_SUBTREE (0x8000)
136#define COAP_FORMAT_NONE (UINT16_MAX)
137
145#ifndef CONFIG_NANOCOAP_NOPTS_MAX
146#define CONFIG_NANOCOAP_NOPTS_MAX (16)
147#endif
148
153#ifndef CONFIG_NANOCOAP_URI_MAX
154#define CONFIG_NANOCOAP_URI_MAX (64)
155#endif
156
160#ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
161#define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
162#endif
163
167#ifndef CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT
168#define CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT COAP_BLOCKSIZE_64
169#endif
170
172#ifndef CONFIG_NANOCOAP_QS_MAX
173#define CONFIG_NANOCOAP_QS_MAX (64)
174#endif
182#ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX
183#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (80)
184#endif
185
193#define COAP_OPT_FINISH_NONE (0x0000)
195#define COAP_OPT_FINISH_PAYLOAD (0x0001)
201typedef struct __attribute__((packed)) {
202 uint8_t ver_t_tkl;
203 uint8_t code;
204 uint16_t id;
205} coap_hdr_t;
206
210typedef struct {
211 uint16_t opt_num;
212 uint16_t offset;
214
232typedef struct {
234 uint8_t *payload;
236 uint16_t payload_len;
237 uint16_t options_len;
240#ifdef MODULE_GCOAP
241 uint32_t observe_value;
242#endif
243} coap_pkt_t;
244
249
272typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len,
273 coap_request_ctx_t *context);
274
287typedef int (*coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more);
288
299typedef int (*coap_request_cb_t)(void *arg, coap_pkt_t *pkt);
300
306typedef uint16_t coap_method_flags_t;
307
317
321typedef const struct {
323 const size_t resources_numof;
325
333
341#if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN
343#endif
344#if defined(MODULE_GCOAP) || DOXYGEN
351 uint32_t tl_type;
352#endif
353};
354
355/* forward declarations */
356static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr);
357static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr);
358static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr);
359
368
377
387
397
407
411typedef struct {
412 size_t offset;
413 uint32_t blknum;
414 uint8_t szx;
415 int8_t more;
418
422typedef struct {
423 size_t start;
424 size_t end;
425 size_t cur;
426 uint8_t *opt;
428
429#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
435#define NANOCOAP_RESOURCE(name) \
436 XFA_CONST(coap_resource_t, coap_resources_xfa, 0) CONCAT(coap_resource_, name) =
437#else
443extern const coap_resource_t coap_resources[];
444
450extern const unsigned coap_resources_numof;
451#endif
452
467static inline uint8_t coap_code(unsigned cls, unsigned detail)
468{
469 return (cls << 5) | detail;
470}
471
479static inline unsigned coap_get_code_class(const coap_pkt_t *pkt)
480{
481 return pkt->hdr->code >> 5;
482}
483
491static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
492{
493 return pkt->hdr->code & 0x1f;
494}
495
503static inline unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
504{
505 return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
506}
507
515static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt)
516{
517 return (unsigned)pkt->hdr->code;
518}
519
527static inline coap_method_t coap_get_method(const coap_pkt_t *pkt)
528{
529 return pkt->hdr->code;
530}
531
539static inline unsigned coap_get_id(const coap_pkt_t *pkt)
540{
541 return ntohs(pkt->hdr->id);
542}
543
554static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
555{
556 return coap_hdr_get_token_len(pkt->hdr);
557}
558
566static inline void *coap_get_token(const coap_pkt_t *pkt)
567{
568 return coap_hdr_data_ptr(pkt->hdr);
569}
570
580static inline unsigned coap_get_total_len(const coap_pkt_t *pkt)
581{
582 return (uintptr_t)pkt->payload - (uintptr_t)pkt->hdr + pkt->payload_len;
583}
584
595static inline unsigned coap_get_type(const coap_pkt_t *pkt)
596{
597 return (pkt->hdr->ver_t_tkl & 0x30) >> 4;
598}
599
607static inline unsigned coap_get_ver(const coap_pkt_t *pkt)
608{
609 return (pkt->hdr->ver_t_tkl & 0x60) >> 6;
610}
611
622static inline uint8_t coap_hdr_tkl_ext_len(const coap_hdr_t *hdr)
623{
624 if (!IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
625 return 0;
626 }
627
628 switch (hdr->ver_t_tkl & 0xf) {
629 case 13:
630 return 1;
631 case 14:
632 return 2;
633 case 15:
634 assert(0);
635 /* fall-through */
636 default:
637 return 0;
638 }
639}
640
648static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr)
649{
650 return ((uint8_t *)hdr) + sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(hdr);
651}
652
660static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
661{
662 return sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) +
664}
665
677static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
678{
679 return coap_get_total_hdr_len(pkt);
680}
681
688static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
689{
690 hdr->code = code;
691}
692
699static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
700{
701 coap_hdr_set_code(pkt->hdr, code);
702}
703
712static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
713{
714 /* assert correct range of type */
715 assert(!(type & ~0x3));
716
717 hdr->ver_t_tkl &= ~0x30;
718 hdr->ver_t_tkl |= type << 4;
719}
720
736static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr)
737{
738 const uint8_t *buf = (const void *)hdr;
739 /* Regarding use unnamed magic numbers 13 and 269:
740 * - If token length is < 13 it fits into TKL field (4 bit)
741 * - If token length is < 269 it fits into 8-bit extended TKL field
742 * - Otherwise token length goes into 16-bit extended TKL field.
743 *
744 * (Not using named constants here, as RFC 8974 also has no names for those
745 * magic numbers.)
746 *
747 * See: https://www.rfc-editor.org/rfc/rfc8974#name-extended-token-length-tkl-f
748 */
749 switch (coap_hdr_tkl_ext_len(hdr)) {
750 case 0:
751 return hdr->ver_t_tkl & 0xf;
752 case 1:
753 return buf[sizeof(coap_hdr_t)] + 13;
754 case 2:
755 return byteorder_bebuftohs(buf + sizeof(coap_hdr_t)) + 269;
756 }
757
758 return 0;
759}
760
776static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr)
777{
778 uint8_t *token = (void *)hdr;
779 token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr);
780 return token;
781}
782
797static inline size_t coap_hdr_len(const coap_hdr_t *hdr)
798{
799 return sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr) + coap_hdr_get_token_len(hdr);
800}
829uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num);
830
843uint8_t *coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num,
844 uint8_t **opt_pos, int *opt_len);
845
855
865
878int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
879
897ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum,
898 uint8_t *target, size_t max_len, char separator);
899
915static inline ssize_t coap_get_location_path(coap_pkt_t *pkt,
916 uint8_t *target, size_t max_len)
917{
918 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
919 target, max_len, '/');
920}
921
937static inline ssize_t coap_get_location_query(coap_pkt_t *pkt,
938 uint8_t *target, size_t max_len)
939{
940 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
941 target, max_len, '&');
942}
943
958static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, uint8_t *target)
959{
960 return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
962}
963
977static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
978 size_t max_len)
979{
980 return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY,
981 (uint8_t *)target, max_len, '&');
982}
983
998bool coap_find_uri_query(coap_pkt_t *pkt, const char *key,
999 const char **value, size_t *len);
1000
1031 uint8_t **value, bool init_opt);
1032
1049ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
1064static inline ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
1065{
1066 return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
1067}
1068
1086void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
1087 int more);
1088
1104bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
1105
1120static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
1121{
1122 return coap_block_finish(slicer, COAP_OPT_BLOCK1);
1123}
1124
1139static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
1140{
1141 return coap_block_finish(slicer, COAP_OPT_BLOCK2);
1142}
1143
1154
1165 size_t blksize);
1166
1181size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
1182 const void *c, size_t len);
1183
1197size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
1198
1217int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
1218
1236static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
1237{
1238 return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
1239}
1240
1250static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
1251{
1252 return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
1253}
1254
1267int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx);
1268
1287
1295#define coap_szx2size(szx) (1U << ((szx) + 4))
1296
1304static inline unsigned coap_size2szx(unsigned len)
1305{
1306 assert(len >= 16);
1307 return bitarithm_msb(len >> 4);
1308}
1342 bool more, uint16_t option);
1343
1362static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
1363 coap_block_slicer_t *slicer, bool more)
1364{
1365 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
1366}
1367
1386static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
1387 coap_block_slicer_t *slicer, bool more)
1388{
1389 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
1390}
1405ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
1406
1420static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
1421 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
1422 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1423}
1424
1438static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
1439 /* block.more must be zero, so no need to 'or' it in */
1440 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
1441 (block->blknum << 4) | block->szx);
1442}
1443
1457static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
1458{
1459 return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
1460}
1461
1475static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1476{
1477 return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1478}
1479
1495ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len);
1496
1514ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1515 const char *val, size_t val_len);
1516
1533static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1534 const char *val)
1535{
1536 return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1537}
1538
1553ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1554
1573ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1574 size_t chars_len, char separator);
1575
1593static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1594 const char *string, char separator)
1595{
1596 return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1597}
1598
1613static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1614{
1615 return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1616}
1617
1633static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1634 const char *path,
1635 size_t path_len)
1636{
1637 return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1638}
1639
1652ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1653
1671ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum);
1699size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1700 bool more, uint16_t option);
1701
1717static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1718 coap_block_slicer_t *slicer, bool more)
1719{
1720 return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1721}
1722
1738static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1739 coap_block_slicer_t *slicer, bool more)
1740{
1741 return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1742}
1743
1755size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1756 uint32_t value);
1757
1767static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1768 coap_block1_t *block)
1769{
1770 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1771 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1772}
1773
1785static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1786 coap_block1_t *block)
1787{
1788 /* block.more must be zero, so no need to 'or' it in */
1789 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1790 (block->blknum << 4) | block->szx);
1791}
1792
1806size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1807 const char *string, size_t len, char separator);
1820static inline size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum,
1821 uint16_t optnum,
1822 const char *string, char separator)
1823{
1824 return coap_opt_put_string_with_len(buf, lastonum, optnum,
1825 string, strlen(string), separator);
1826}
1827
1838static inline size_t coap_opt_put_location_path(uint8_t *buf,
1839 uint16_t lastonum,
1840 const char *location)
1841{
1842 return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1843 location, '/');
1844}
1845
1856static inline size_t coap_opt_put_location_query(uint8_t *buf,
1857 uint16_t lastonum,
1858 const char *location)
1859{
1860 return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
1861 location, '&');
1862}
1863
1874static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
1875 const char *uri)
1876{
1877 return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
1878}
1879
1890static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
1891 const char *uri)
1892{
1893 return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
1894}
1895
1914size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri);
1915
1926static inline size_t coap_opt_put_proxy_uri(uint8_t *buf, uint16_t lastonum,
1927 const char *uri)
1928{
1929 return coap_opt_put_string(buf, lastonum, COAP_OPT_PROXY_URI, uri, '\0');
1930}
1931
1949size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
1950
1967size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen);
1968
1981static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
1982 unsigned blknum, unsigned szx, int more)
1983{
1984 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1985 (blknum << 4) | szx | (more ? 0x8 : 0));
1986}
1987
1998static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
1999 uint16_t content_type)
2000{
2001 return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
2002}
2031ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
2032 uint8_t *rbuf, unsigned rlen, unsigned payload_len,
2033 coap_block_slicer_t *slicer);
2034
2054ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token,
2055 size_t token_len, unsigned code, uint16_t id);
2056
2085ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
2086 uint8_t *rbuf, unsigned rlen, unsigned payload_len);
2087
2103
2118ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
2119 coap_request_ctx_t *ctx);
2120
2137ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2138 unsigned resp_buf_len, coap_request_ctx_t *ctx,
2139 const coap_resource_t *resources,
2140 size_t resources_numof);
2141
2159ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2160 size_t resp_buf_len, coap_request_ctx_t *context);
2161
2169static inline coap_method_flags_t coap_method2flag(unsigned code)
2170{
2171 return (1 << (code - 1));
2172}
2173
2188int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
2189
2204void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
2205
2219static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
2220{
2221 pkt->payload += len;
2222 pkt->payload_len -= len;
2223}
2224
2246ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
2247
2261ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
2262
2288ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code,
2289 void *buf, size_t len,
2290 int ct,
2291 void **payload, size_t *payload_len_max);
2292
2315 unsigned code,
2316 uint8_t *buf, size_t len,
2317 unsigned ct,
2318 const void *payload, size_t payload_len);
2319
2325 uint8_t *buf, size_t len,
2326 coap_request_ctx_t *context);
2332#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
2333#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE !IS_USED(MODULE_GCOAP)
2334#endif
2335
2351int coap_match_path(const coap_resource_t *resource, const uint8_t *uri);
2352
2353#if defined(MODULE_GCOAP) || defined(DOXYGEN)
2366static inline bool coap_has_observe(coap_pkt_t *pkt)
2367{
2368 return pkt->observe_value != UINT32_MAX;
2369}
2370
2376static inline void coap_clear_observe(coap_pkt_t *pkt)
2377{
2378 pkt->observe_value = UINT32_MAX;
2379}
2380
2388static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
2389{
2390 return pkt->observe_value;
2391}
2393#endif
2394
2398#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2399 { \
2400 .path = "/.well-known/core", \
2401 .methods = COAP_GET, \
2402 .handler = coap_well_known_core_default_handler \
2403 }
2404
2405#ifdef __cplusplus
2406}
2407#endif
2408#endif /* NET_NANOCOAP_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
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:153
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:536
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:551
Various helper macros.
coap_method_t
CoAP method codes used in request.
Definition coap.h:171
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
Definition nanocoap.h:146
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition nanocoap.h:154
static size_t coap_hdr_get_token_len(const coap_hdr_t *hdr)
Get the token length of a CoAP over UDP (DTLS) packet.
Definition nanocoap.h:736
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:1533
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:699
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:1890
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:1785
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:1386
static unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition nanocoap.h:515
static size_t coap_hdr_len(const coap_hdr_t *hdr)
Get the header length of a CoAP packet.
Definition nanocoap.h:797
ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code, void *buf, size_t len, int ct, void **payload, size_t *payload_len_max)
Create CoAP reply header (convenience function)
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:1717
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:1475
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition nanocoap.h:467
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:1613
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:977
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:539
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:937
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:1874
ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len)
Build reply to CoAP request.
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:1457
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.
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.
static void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
Write the given raw message code to given CoAP header.
Definition nanocoap.h:688
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 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:1820
uint16_t coap_method_flags_t
Method flag type.
Definition nanocoap.h:306
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:607
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:1738
static bool coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition nanocoap.h:1139
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 uint8_t coap_hdr_tkl_ext_len(const coap_hdr_t *hdr)
Get the size of the extended Token length field (RFC 8974)
Definition nanocoap.h:622
static coap_method_t coap_get_method(const coap_pkt_t *pkt)
Get a request's method type.
Definition nanocoap.h:527
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:660
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 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:479
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition nanocoap.h:1250
ssize_t coap_build_empty_ack(coap_pkt_t *pkt, coap_hdr_t *ack)
Build empty reply to CoAP request.
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.
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:915
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:1593
bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
Finish a block request (block1 or block2)
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:491
ssize_t coap_reply_simple(coap_pkt_t *pkt, unsigned code, uint8_t *buf, size_t len, unsigned ct, const void *payload, size_t payload_len)
Create CoAP reply (convenience function)
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition nanocoap.h:1236
void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
static uint8_t * coap_hdr_data_ptr(const coap_hdr_t *hdr)
Get the start of data after the header.
Definition nanocoap.h:648
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:272
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:1926
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition nanocoap.h:712
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition nanocoap.h:2219
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:2388
static const void * coap_hdr_get_token(const coap_hdr_t *hdr)
Get the Token of a CoAP over UDP (DTLS) packet.
Definition nanocoap.h:776
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:1981
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:1438
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
Definition nanocoap.h:595
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:503
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:677
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.
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:1633
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
Definition nanocoap.h:566
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:1856
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 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:1838
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition nanocoap.h:1120
void * coap_request_ctx_get_context(const coap_request_ctx_t *ctx)
Get resource context associated with a CoAP request.
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:1998
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:287
static unsigned coap_size2szx(unsigned len)
Helper to encode byte size into next equal or smaller SZX value.
Definition nanocoap.h:1304
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:299
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:1767
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.
int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU.
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.
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:1064
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:958
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:580
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition nanocoap.h:2366
ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token, size_t token_len, unsigned code, uint16_t id)
Builds a CoAP header.
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:1362
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition nanocoap.h:2169
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:1420
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 unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition nanocoap.h:554
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition nanocoap.h:2376
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
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:293
Definitions for internet operations.
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:71
Generic CoAP values as defined by RFC7252.
UDP sock definitions.
CoAP resource request handler context.
Definition nanocoap.h:338
sock_udp_ep_t * local
local endpoint of the request
Definition nanocoap.h:342
const coap_resource_t * resource
resource of the request
Definition nanocoap.h:339
sock_udp_ep_t * remote
remote endpoint of the request
Definition nanocoap.h:340
uint32_t tl_type
transport the packet was received over
Definition nanocoap.h:351
Common IP-based transport layer end point.
Definition sock.h:215
Block1 helper struct.
Definition nanocoap.h:411
int8_t more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition nanocoap.h:415
uint32_t blknum
block number
Definition nanocoap.h:413
size_t offset
offset of received data
Definition nanocoap.h:412
uint8_t szx
szx value
Definition nanocoap.h:414
Blockwise transfer helper struct.
Definition nanocoap.h:422
uint8_t * opt
Pointer to the placed option
Definition nanocoap.h:426
size_t end
End offset of the current block
Definition nanocoap.h:424
size_t start
Start offset of the current block
Definition nanocoap.h:423
size_t cur
Offset of the generated content
Definition nanocoap.h:425
Raw CoAP PDU header structure.
Definition nanocoap.h:201
uint8_t ver_t_tkl
version, token, token length
Definition nanocoap.h:202
uint8_t code
CoAP code (e.g.m 205)
Definition nanocoap.h:203
uint16_t id
Req/resp ID
Definition nanocoap.h:204
CoAP option array entry.
Definition nanocoap.h:210
uint16_t offset
offset in packet
Definition nanocoap.h:212
uint16_t opt_num
full CoAP option number
Definition nanocoap.h:211
CoAP PDU parsing context structure.
Definition nanocoap.h:232
uint8_t * payload
pointer to end of the header
Definition nanocoap.h:234
uint16_t payload_len
length of payload
Definition nanocoap.h:236
coap_hdr_t * hdr
pointer to raw packet
Definition nanocoap.h:233
uint16_t options_len
length of options array
Definition nanocoap.h:237
BITFIELD(opt_crit, CONFIG_NANOCOAP_NOPTS_MAX)
unhandled critical option
iolist_t * snips
payload snips (optional)
Definition nanocoap.h:235
Type for CoAP resource subtrees.
Definition nanocoap.h:321
const size_t resources_numof
number of entries in array
Definition nanocoap.h:323
const coap_resource_t * resources
ptr to resource array
Definition nanocoap.h:322
Type for CoAP resource entry.
Definition nanocoap.h:311
const char * path
URI path of resource
Definition nanocoap.h:312
coap_method_flags_t methods
OR'ed methods this resource allows.
Definition nanocoap.h:313
coap_handler_t handler
ptr to resource handler
Definition nanocoap.h:314
void * context
ptr to user defined context data
Definition nanocoap.h:315
iolist structure definition
Definition iolist.h:39
Cross File Arrays.