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#include "bitarithm.h"
89#include "bitfield.h"
90#include "byteorder.h"
91#include "iolist.h"
92#include "macros/utils.h"
93#include "modules.h"
94#include "net/coap.h"
95#include "net/sock/udp.h"
96
97#if defined(MODULE_NANOCOAP_RESOURCES)
98#include "xfa.h"
99#endif
100
101#ifdef __cplusplus
102extern "C" {
103#endif
104
110#define COAP_GET (0x01)
111#define COAP_POST (0x02)
112#define COAP_PUT (0x04)
113#define COAP_DELETE (0x08)
114#define COAP_FETCH (0x10)
115#define COAP_PATCH (0x20)
116#define COAP_IPATCH (0x40)
117#define COAP_IGNORE (0xFF)
119#define COAP_MATCH_SUBTREE (0x8000)
126#define COAP_FORMAT_NONE (UINT16_MAX)
127
135#ifndef CONFIG_NANOCOAP_NOPTS_MAX
136#define CONFIG_NANOCOAP_NOPTS_MAX (16)
137#endif
138
143#ifndef CONFIG_NANOCOAP_URI_MAX
144#define CONFIG_NANOCOAP_URI_MAX (64)
145#endif
146
150#ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
151#define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
152#endif
153
157#ifndef CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT
158#define CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT COAP_BLOCKSIZE_64
159#endif
160
162#ifndef CONFIG_NANOCOAP_QS_MAX
163#define CONFIG_NANOCOAP_QS_MAX (64)
164#endif
172#ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX
173#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (80)
174#endif
175
183#define COAP_OPT_FINISH_NONE (0x0000)
185#define COAP_OPT_FINISH_PAYLOAD (0x0001)
191typedef struct __attribute__((packed)) {
192 uint8_t ver_t_tkl;
193 uint8_t code;
194 uint16_t id;
195} coap_hdr_t;
196
200typedef struct {
201 uint16_t opt_num;
202 uint16_t offset;
204
222typedef struct {
224 uint8_t *payload;
226 uint16_t payload_len;
227 uint16_t options_len;
230#ifdef MODULE_GCOAP
231 uint32_t observe_value;
232#endif
233} coap_pkt_t;
234
239
262typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len,
263 coap_request_ctx_t *context);
264
277typedef int (*coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more);
278
289typedef int (*coap_request_cb_t)(void *arg, coap_pkt_t *pkt);
290
296typedef uint16_t coap_method_flags_t;
297
307
311typedef const struct {
313 const size_t resources_numof;
315
323
331#if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN
333#endif
334#if defined(MODULE_GCOAP) || DOXYGEN
341 uint32_t tl_type;
342#endif
343};
344
345/* forward declarations */
346static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr);
347static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr);
348static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr);
349
358
367
377
387
397
401typedef struct {
402 size_t offset;
403 uint32_t blknum;
404 uint8_t szx;
405 int8_t more;
408
412typedef struct {
413 size_t start;
414 size_t end;
415 size_t cur;
416 uint8_t *opt;
418
419#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
425#define NANOCOAP_RESOURCE(name) \
426 XFA_CONST(coap_resource_t, coap_resources_xfa, 0) CONCAT(coap_resource_, name) =
427#else
433extern const coap_resource_t coap_resources[];
434
440extern const unsigned coap_resources_numof;
441#endif
442
457static inline uint8_t coap_code(unsigned cls, unsigned detail)
458{
459 return (cls << 5) | detail;
460}
461
469static inline unsigned coap_get_code_class(const coap_pkt_t *pkt)
470{
471 return pkt->hdr->code >> 5;
472}
473
481static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
482{
483 return pkt->hdr->code & 0x1f;
484}
485
493static inline unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
494{
495 return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
496}
497
505static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt)
506{
507 return (unsigned)pkt->hdr->code;
508}
509
517static inline coap_method_t coap_get_method(const coap_pkt_t *pkt)
518{
519 return pkt->hdr->code;
520}
521
529static inline unsigned coap_get_id(const coap_pkt_t *pkt)
530{
531 return ntohs(pkt->hdr->id);
532}
533
544static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
545{
546 return coap_hdr_get_token_len(pkt->hdr);
547}
548
556static inline void *coap_get_token(const coap_pkt_t *pkt)
557{
558 return coap_hdr_data_ptr(pkt->hdr);
559}
560
570static inline unsigned coap_get_total_len(const coap_pkt_t *pkt)
571{
572 return (uintptr_t)pkt->payload - (uintptr_t)pkt->hdr + pkt->payload_len;
573}
574
585static inline unsigned coap_get_type(const coap_pkt_t *pkt)
586{
587 return (pkt->hdr->ver_t_tkl & 0x30) >> 4;
588}
589
597static inline unsigned coap_get_ver(const coap_pkt_t *pkt)
598{
599 return (pkt->hdr->ver_t_tkl & 0x60) >> 6;
600}
601
612static inline uint8_t coap_hdr_tkl_ext_len(const coap_hdr_t *hdr)
613{
614 if (!IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
615 return 0;
616 }
617
618 switch (hdr->ver_t_tkl & 0xf) {
619 case 13:
620 return 1;
621 case 14:
622 return 2;
623 case 15:
624 assert(0);
625 /* fall-through */
626 default:
627 return 0;
628 }
629}
630
638static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr)
639{
640 return ((uint8_t *)hdr) + sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(hdr);
641}
642
650static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
651{
652 return sizeof(coap_hdr_t) + coap_hdr_tkl_ext_len(pkt->hdr) +
654}
655
667static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
668{
669 return coap_get_total_hdr_len(pkt);
670}
671
678static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
679{
680 hdr->code = code;
681}
682
689static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
690{
691 coap_hdr_set_code(pkt->hdr, code);
692}
693
702static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
703{
704 /* assert correct range of type */
705 assert(!(type & ~0x3));
706
707 hdr->ver_t_tkl &= ~0x30;
708 hdr->ver_t_tkl |= type << 4;
709}
710
726static inline size_t coap_hdr_get_token_len(const coap_hdr_t *hdr)
727{
728 const uint8_t *buf = (const void *)hdr;
729 /* Regarding use unnamed magic numbers 13 and 269:
730 * - If token length is < 13 it fits into TKL field (4 bit)
731 * - If token length is < 269 it fits into 8-bit extended TKL field
732 * - Otherwise token length goes into 16-bit extended TKL field.
733 *
734 * (Not using named constants here, as RFC 8974 also has no names for those
735 * magic numbers.)
736 *
737 * See: https://www.rfc-editor.org/rfc/rfc8974#name-extended-token-length-tkl-f
738 */
739 switch (coap_hdr_tkl_ext_len(hdr)) {
740 case 0:
741 return hdr->ver_t_tkl & 0xf;
742 case 1:
743 return buf[sizeof(coap_hdr_t)] + 13;
744 case 2:
745 return byteorder_bebuftohs(buf + sizeof(coap_hdr_t)) + 269;
746 }
747
748 return 0;
749}
750
766static inline const void * coap_hdr_get_token(const coap_hdr_t *hdr)
767{
768 uint8_t *token = (void *)hdr;
769 token += sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr);
770 return token;
771}
772
787static inline size_t coap_hdr_len(const coap_hdr_t *hdr)
788{
789 return sizeof(*hdr) + coap_hdr_tkl_ext_len(hdr) + coap_hdr_get_token_len(hdr);
790}
819uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num);
820
833uint8_t *coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num,
834 uint8_t **opt_pos, int *opt_len);
835
845
855
868int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
869
887ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum,
888 uint8_t *target, size_t max_len, char separator);
889
905static inline ssize_t coap_get_location_path(coap_pkt_t *pkt,
906 uint8_t *target, size_t max_len)
907{
908 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
909 target, max_len, '/');
910}
911
927static inline ssize_t coap_get_location_query(coap_pkt_t *pkt,
928 uint8_t *target, size_t max_len)
929{
930 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
931 target, max_len, '&');
932}
933
948static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, uint8_t *target)
949{
950 return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
952}
953
967static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
968 size_t max_len)
969{
970 return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY,
971 (uint8_t *)target, max_len, '&');
972}
973
988bool coap_find_uri_query(coap_pkt_t *pkt, const char *key,
989 const char **value, size_t *len);
990
1021 uint8_t **value, bool init_opt);
1022
1039ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
1054static inline ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
1055{
1056 return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
1057}
1058
1076void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
1077 int more);
1078
1094bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
1095
1110static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
1111{
1112 return coap_block_finish(slicer, COAP_OPT_BLOCK1);
1113}
1114
1129static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
1130{
1131 return coap_block_finish(slicer, COAP_OPT_BLOCK2);
1132}
1133
1144
1155 size_t blksize);
1156
1171size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
1172 const void *c, size_t len);
1173
1187size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
1188
1207int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
1208
1226static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
1227{
1228 return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
1229}
1230
1240static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
1241{
1242 return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
1243}
1244
1257int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx);
1258
1277
1285#define coap_szx2size(szx) (1U << ((szx) + 4))
1286
1294static inline unsigned coap_size2szx(unsigned len)
1295{
1296 assert(len >= 16);
1297 return bitarithm_msb(len >> 4);
1298}
1332 bool more, uint16_t option);
1333
1352static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
1353 coap_block_slicer_t *slicer, bool more)
1354{
1355 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
1356}
1357
1376static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
1377 coap_block_slicer_t *slicer, bool more)
1378{
1379 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
1380}
1395ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
1396
1410static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
1411 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
1412 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1413}
1414
1428static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
1429 /* block.more must be zero, so no need to 'or' it in */
1430 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
1431 (block->blknum << 4) | block->szx);
1432}
1433
1447static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
1448{
1449 return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
1450}
1451
1465static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1466{
1467 return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1468}
1469
1485ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len);
1486
1504ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1505 const char *val, size_t val_len);
1506
1523static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1524 const char *val)
1525{
1526 return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1527}
1528
1543ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1544
1563ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1564 size_t chars_len, char separator);
1565
1583static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1584 const char *string, char separator)
1585{
1586 return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1587}
1588
1603static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1604{
1605 return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1606}
1607
1623static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1624 const char *path,
1625 size_t path_len)
1626{
1627 return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1628}
1629
1642ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1643
1661ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum);
1689size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1690 bool more, uint16_t option);
1691
1707static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1708 coap_block_slicer_t *slicer, bool more)
1709{
1710 return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1711}
1712
1728static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1729 coap_block_slicer_t *slicer, bool more)
1730{
1731 return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1732}
1733
1745size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1746 uint32_t value);
1747
1757static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1758 coap_block1_t *block)
1759{
1760 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1761 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1762}
1763
1775static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1776 coap_block1_t *block)
1777{
1778 /* block.more must be zero, so no need to 'or' it in */
1779 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1780 (block->blknum << 4) | block->szx);
1781}
1782
1792static inline size_t coap_opt_put_observe(uint8_t *buf, uint16_t lastonum,
1793 uint32_t obs)
1794{
1795 obs &= COAP_OBS_MAX_VALUE_MASK; /* trim obs down to 24 bit */
1796 return coap_opt_put_uint(buf, lastonum, COAP_OPT_OBSERVE, obs);
1797}
1798
1812size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1813 const char *string, size_t len, char separator);
1826static inline size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum,
1827 uint16_t optnum,
1828 const char *string, char separator)
1829{
1830 return coap_opt_put_string_with_len(buf, lastonum, optnum,
1831 string, strlen(string), separator);
1832}
1833
1844static inline size_t coap_opt_put_location_path(uint8_t *buf,
1845 uint16_t lastonum,
1846 const char *location)
1847{
1848 return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1849 location, '/');
1850}
1851
1862static inline size_t coap_opt_put_location_query(uint8_t *buf,
1863 uint16_t lastonum,
1864 const char *location)
1865{
1866 return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
1867 location, '&');
1868}
1869
1880static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
1881 const char *uri)
1882{
1883 return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
1884}
1885
1896static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
1897 const char *uri)
1898{
1899 return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
1900}
1901
1920size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri);
1921
1932static inline size_t coap_opt_put_proxy_uri(uint8_t *buf, uint16_t lastonum,
1933 const char *uri)
1934{
1935 return coap_opt_put_string(buf, lastonum, COAP_OPT_PROXY_URI, uri, '\0');
1936}
1937
1955size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
1956
1973size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen);
1974
1987static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
1988 unsigned blknum, unsigned szx, int more)
1989{
1990 return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1991 (blknum << 4) | szx | (more ? 0x8 : 0));
1992}
1993
2004static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
2005 uint16_t content_type)
2006{
2007 return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
2008}
2037ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
2038 uint8_t *rbuf, unsigned rlen, unsigned payload_len,
2039 coap_block_slicer_t *slicer);
2040
2060ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token,
2061 size_t token_len, unsigned code, uint16_t id);
2062
2091ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
2092 uint8_t *rbuf, unsigned rlen, unsigned payload_len);
2093
2109
2124ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
2125 coap_request_ctx_t *ctx);
2126
2143ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2144 unsigned resp_buf_len, coap_request_ctx_t *ctx,
2145 const coap_resource_t *resources,
2146 size_t resources_numof);
2147
2165ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2166 size_t resp_buf_len, coap_request_ctx_t *context);
2167
2175static inline coap_method_flags_t coap_method2flag(unsigned code)
2176{
2177 return (1 << (code - 1));
2178}
2179
2194int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
2195
2210void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
2211
2225static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
2226{
2227 pkt->payload += len;
2228 pkt->payload_len -= len;
2229}
2230
2252ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
2253
2267ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
2268
2294ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code,
2295 void *buf, size_t len,
2296 int ct,
2297 void **payload, size_t *payload_len_max);
2298
2321 unsigned code,
2322 uint8_t *buf, size_t len,
2323 unsigned ct,
2324 const void *payload, size_t payload_len);
2325
2331 uint8_t *buf, size_t len,
2332 coap_request_ctx_t *context);
2338#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
2339#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE !IS_USED(MODULE_GCOAP)
2340#endif
2341
2357int coap_match_path(const coap_resource_t *resource, const uint8_t *uri);
2358
2359#if defined(MODULE_GCOAP) || defined(DOXYGEN)
2372static inline bool coap_has_observe(coap_pkt_t *pkt)
2373{
2374 return pkt->observe_value != UINT32_MAX;
2375}
2376
2382static inline void coap_clear_observe(coap_pkt_t *pkt)
2383{
2384 pkt->observe_value = UINT32_MAX;
2385}
2386
2394static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
2395{
2396 return pkt->observe_value;
2397}
2399#endif
2400
2404#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2405 { \
2406 .path = "/.well-known/core", \
2407 .methods = COAP_GET, \
2408 .handler = coap_well_known_core_default_handler \
2409 }
2410
2411#ifdef __cplusplus
2412}
2413#endif
2414#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.
#define COAP_OBS_MAX_VALUE_MASK
observe value is 24 bits
Definition coap.h:522
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:136
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition nanocoap.h:144
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:726
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:1523
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:689
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:1896
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:1775
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:1376
static unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition nanocoap.h:505
static size_t coap_hdr_len(const coap_hdr_t *hdr)
Get the header length of a CoAP packet.
Definition nanocoap.h:787
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:1707
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:1465
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition nanocoap.h:457
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:1603
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:967
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:529
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:927
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:1880
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:1447
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:678
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:1826
uint16_t coap_method_flags_t
Method flag type.
Definition nanocoap.h:296
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:597
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:1728
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:1792
static bool coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition nanocoap.h:1129
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:612
static coap_method_t coap_get_method(const coap_pkt_t *pkt)
Get a request's method type.
Definition nanocoap.h:517
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:650
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:469
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition nanocoap.h:1240
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:905
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:1583
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:481
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:1226
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:638
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:262
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:1932
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition nanocoap.h:702
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition nanocoap.h:2225
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:2394
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:766
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:1987
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:1428
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
Definition nanocoap.h:585
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:493
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:667
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:1623
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
Definition nanocoap.h:556
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:1862
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:1844
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition nanocoap.h:1110
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:2004
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:277
static unsigned coap_size2szx(unsigned len)
Helper to encode byte size into next equal or smaller SZX value.
Definition nanocoap.h:1294
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:289
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:1757
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:1054
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:948
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:570
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition nanocoap.h:2372
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:1352
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition nanocoap.h:2175
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:1410
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:544
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition nanocoap.h:2382
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.
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:328
sock_udp_ep_t * local
local endpoint of the request
Definition nanocoap.h:332
const coap_resource_t * resource
resource of the request
Definition nanocoap.h:329
sock_udp_ep_t * remote
remote endpoint of the request
Definition nanocoap.h:330
uint32_t tl_type
transport the packet was received over
Definition nanocoap.h:341
Common IP-based transport layer end point.
Definition sock.h:215
Block1 helper struct.
Definition nanocoap.h:401
int8_t more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition nanocoap.h:405
uint32_t blknum
block number
Definition nanocoap.h:403
size_t offset
offset of received data
Definition nanocoap.h:402
uint8_t szx
szx value
Definition nanocoap.h:404
Blockwise transfer helper struct.
Definition nanocoap.h:412
uint8_t * opt
Pointer to the placed option
Definition nanocoap.h:416
size_t end
End offset of the current block
Definition nanocoap.h:414
size_t start
Start offset of the current block
Definition nanocoap.h:413
size_t cur
Offset of the generated content
Definition nanocoap.h:415
Raw CoAP PDU header structure.
Definition nanocoap.h:191
uint8_t ver_t_tkl
version, token, token length
Definition nanocoap.h:192
uint8_t code
CoAP code (e.g.m 205)
Definition nanocoap.h:193
uint16_t id
Req/resp ID
Definition nanocoap.h:194
CoAP option array entry.
Definition nanocoap.h:200
uint16_t offset
offset in packet
Definition nanocoap.h:202
uint16_t opt_num
full CoAP option number
Definition nanocoap.h:201
CoAP PDU parsing context structure.
Definition nanocoap.h:222
uint8_t * payload
pointer to end of the header
Definition nanocoap.h:224
uint16_t payload_len
length of payload
Definition nanocoap.h:226
coap_hdr_t * hdr
pointer to raw packet
Definition nanocoap.h:223
uint16_t options_len
length of options array
Definition nanocoap.h:227
BITFIELD(opt_crit, CONFIG_NANOCOAP_NOPTS_MAX)
unhandled critical option
iolist_t * snips
payload snips (optional)
Definition nanocoap.h:225
Type for CoAP resource subtrees.
Definition nanocoap.h:311
const size_t resources_numof
number of entries in array
Definition nanocoap.h:313
const coap_resource_t * resources
ptr to resource array
Definition nanocoap.h:312
Type for CoAP resource entry.
Definition nanocoap.h:301
const char * path
URI path of resource
Definition nanocoap.h:302
coap_method_flags_t methods
OR'ed methods this resource allows.
Definition nanocoap.h:303
coap_handler_t handler
ptr to resource handler
Definition nanocoap.h:304
void * context
ptr to user defined context data
Definition nanocoap.h:305
iolist structure definition
Definition iolist.h:39
Cross File Arrays.