Loading...
Searching...
No Matches
nanocoap.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016-2018 Kaspar Schleiser <kaspar@schleiser.de>
3 * SPDX-FileCopyrightText: 2018 Freie Universität Berlin
4 * SPDX-FileCopyrightText: 2018 Inria
5 * SPDX-FileCopyrightText: 2018 Ken Bannister <kb2ma@runbox.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
7 */
8
9#pragma once
10
75
76#include <assert.h>
77#include <stdint.h>
78#include <stdbool.h>
79#include <stddef.h>
80#include <string.h>
81#include <unistd.h>
82
83#include "bitfield.h"
84#include "byteorder.h"
85#include "compiler_hints.h"
86#include "iolist.h"
87#include "modules.h"
88#include "net/coap.h"
89#include "net/sock/udp.h"
90
91#if defined(MODULE_NANOCOAP_RESOURCES)
92# include "xfa.h"
93#endif
94
95#ifdef __cplusplus
96extern "C" {
97#endif
98
104#define COAP_GET (0x01)
105#define COAP_POST (0x02)
106#define COAP_PUT (0x04)
107#define COAP_DELETE (0x08)
108#define COAP_FETCH (0x10)
109#define COAP_PATCH (0x20)
110#define COAP_IPATCH (0x40)
111#define COAP_IGNORE (0xFF)
113#define COAP_MATCH_SUBTREE (0x8000)
116
120#define COAP_FORMAT_NONE (UINT16_MAX)
121
129#ifndef CONFIG_NANOCOAP_NOPTS_MAX
130# define CONFIG_NANOCOAP_NOPTS_MAX (16)
131#endif
132
137#ifndef CONFIG_NANOCOAP_URI_MAX
138# define CONFIG_NANOCOAP_URI_MAX (64)
139#endif
140
144#ifndef CONFIG_NANOCOAP_BLOCK_SIZE_MAX
145# define CONFIG_NANOCOAP_BLOCK_SIZE_MAX COAP_BLOCKSIZE_64
146#endif
147
151#ifndef CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT
152# define CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT COAP_BLOCKSIZE_64
153#endif
154
156#ifndef CONFIG_NANOCOAP_QS_MAX
157# define CONFIG_NANOCOAP_QS_MAX (64)
158#endif
160
166#ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX
167# define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (80)
168#endif
169
177#define COAP_OPT_FINISH_NONE (0x0000)
179#define COAP_OPT_FINISH_PAYLOAD (0x0001)
181
185typedef struct __attribute__((packed)) {
186 uint8_t ver_t_tkl;
187 uint8_t code;
188 uint16_t id;
190
194typedef struct {
195 uint16_t opt_num;
196 uint16_t offset;
198
217typedef struct {
223 uint8_t *buf;
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
330 union {
333 };
334#if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN
335 union {
338 };
339#endif
340#if defined(MODULE_GCOAP) || DOXYGEN
347 uint32_t tl_type;
348#endif
349};
350
359
368
378
388
398
423typedef struct {
424 uint8_t *buf;
425 uint16_t pos;
426 uint16_t size;
427 uint16_t last_opt_num;
429
439 size_t offset;
440 uint32_t blknum;
441 uint8_t szx;
442 bool more;
443};
444
449
454
458typedef struct {
459 size_t start;
460 size_t end;
461 size_t cur;
462 uint8_t *opt_value;
464
465#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
471#define NANOCOAP_RESOURCE(name) \
472 XFA_CONST(coap_resource_t, coap_resources_xfa, 0) CONCAT(coap_resource_, name) =
473#else
479extern const coap_resource_t coap_resources[];
480
486extern const unsigned coap_resources_numof;
487#endif
488
502{
503 /* currently only UDP as transport supported */
504 return (coap_udp_hdr_t *)pkt->buf;
505}
506
510static inline const coap_udp_hdr_t *coap_get_udp_hdr_const(const coap_pkt_t *pkt)
511{
512 /* currently only UDP as transport supported */
513 return (const coap_udp_hdr_t *)pkt->buf;
514}
515
524static inline uint8_t coap_code(unsigned cls, unsigned detail)
525{
526 return (cls << 5) | detail;
527}
528
536static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt)
537{
538 return coap_get_udp_hdr_const(pkt)->code;
539}
540
548static inline unsigned coap_get_code_class(const coap_pkt_t *pkt)
549{
550 return coap_get_code_raw(pkt) >> 5;
551}
552
560static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
561{
562 return coap_get_code_raw(pkt) & 0x1f;
563}
564
572static inline unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
573{
574 return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
575}
576
584static inline coap_method_t coap_get_method(const coap_pkt_t *pkt)
585{
586 return coap_get_code_raw(pkt);
587}
588
596static inline unsigned coap_get_id(const coap_pkt_t *pkt)
597{
598 return ntohs(coap_get_udp_hdr_const(pkt)->id);
599}
600
607static inline void coap_set_id(coap_pkt_t *pkt, uint16_t id)
608{
609 coap_get_udp_hdr(pkt)->id = htons(id);
610}
611
619static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
620{
621 const coap_udp_hdr_t *udp = coap_get_udp_hdr_const(pkt);
622 /*
623 * Layout of first byte of header (cut out of Figure 7 of RFC7252)
624 * see https://datatracker.ietf.org/doc/html/rfc7252#section-3
625 * 0 1 2 3 4 5 6 7
626 * +-+-+-+-+-+-+-+-+
627 * |Ver| T | TKL |
628 * +-+-+-+-+-+-+-+-+
629 */
630 return udp->ver_t_tkl & 0x0f;
631}
632
640static inline void *coap_get_token(const coap_pkt_t *pkt)
641{
642 return pkt->buf + sizeof(coap_udp_hdr_t);
643}
644
654static inline unsigned coap_get_total_len(const coap_pkt_t *pkt)
655{
656 return (uintptr_t)pkt->payload - (uintptr_t)pkt->buf + pkt->payload_len;
657}
658
669static inline unsigned coap_get_type(const coap_pkt_t *pkt)
670{
671 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x30) >> 4;
672}
673
681static inline unsigned coap_get_ver(const coap_pkt_t *pkt)
682{
683 return (coap_get_udp_hdr_const(pkt)->ver_t_tkl & 0x60) >> 6;
684}
685
706bool coap_is_hdr_in_bounds(const coap_pkt_t *pkt, size_t len);
707
715static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
716{
717 return sizeof(coap_udp_hdr_t) + coap_get_token_len(pkt);
718}
719
727static inline unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
728{
729 return coap_get_total_hdr_len(pkt);
730}
731
738static inline void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
739{
741 hdr->code = code;
742}
743
752static inline void coap_pkt_set_type(coap_pkt_t *pkt, unsigned type)
753{
754 /* assert correct range of type */
755 assert(!(type & ~0x3));
757
758 if (udp) {
759 udp->ver_t_tkl &= ~0x30;
760 udp->ver_t_tkl |= type << 4;
761 }
762}
763
774static inline void coap_pkt_set_tkl(coap_pkt_t *pkt, uint8_t tkl)
775{
776 coap_udp_hdr_t * hdr = coap_get_udp_hdr(pkt);
777 hdr->ver_t_tkl &= 0xf0;
778 hdr->ver_t_tkl |= (tkl & 0x0f);
779}
780
781
798
808uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num);
809
822uint8_t *coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num,
823 uint8_t **opt_pos, int *opt_len);
824
834
844
857int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
858
876ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum,
877 char *target, size_t max_len, char separator);
878
894static inline ssize_t coap_get_location_path(coap_pkt_t *pkt,
895 char *target, size_t max_len)
896{
897 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
898 target, max_len, '/');
899}
900
916static inline ssize_t coap_get_location_query(coap_pkt_t *pkt,
917 char *target, size_t max_len)
918{
919 return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
920 target, max_len, '&');
921}
922
937static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, char *target)
938{
939 return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
941}
942
956static inline ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target,
957 size_t max_len)
958{
959 return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY,
960 target, max_len, '&');
961}
962
977bool coap_find_uri_query(coap_pkt_t *pkt, const char *key,
978 const char **value, size_t *len);
979
1002 char *key, size_t key_len_max,
1003 char *value, size_t value_len_max);
1004
1035 uint8_t **value, bool init_opt);
1036
1053ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
1055
1068static inline ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
1069{
1070 return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
1071}
1072
1090void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
1091 int more);
1092
1108
1123static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
1124{
1125 return coap_block_finish(slicer);
1126}
1127
1142static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
1143{
1144 return coap_block_finish(slicer);
1145}
1146
1163
1178 size_t blksize);
1179
1198 const void *c, size_t len);
1199
1218 const void *c, size_t len);
1219
1237
1255
1285int coap_get_block(coap_pkt_t *pkt, struct _coap_block *block, uint16_t option);
1286
1311static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
1312{
1313 return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
1314}
1315
1334static inline int coap_get_block2(coap_pkt_t *pkt, coap_block2_t *block)
1335{
1336 return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
1337}
1338
1358
1390 bool more, uint16_t option);
1391
1410static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
1411 coap_block_slicer_t *slicer, bool more)
1412{
1413 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
1414}
1415
1434static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
1435 coap_block_slicer_t *slicer, bool more)
1436{
1437 return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
1438}
1439
1453ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
1454
1468static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
1469 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
1470 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1471}
1472
1486static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
1487 /* block.more must be zero, so no need to 'or' it in */
1488 return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
1489 (block->blknum << 4) | block->szx);
1490}
1491
1505static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
1506{
1507 return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
1508}
1509
1523static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1524{
1525 return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1526}
1527
1543ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len);
1544
1562ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1563 const char *val, size_t val_len);
1564
1581static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1582 const char *val)
1583{
1584 return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1585}
1586
1601ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1602
1621ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1622 size_t chars_len, char separator);
1623
1641static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1642 const char *string, char separator)
1643{
1644 return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1645}
1646
1661static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1662{
1663 return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1664}
1665
1681static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1682 const char *path,
1683 size_t path_len)
1684{
1685 return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1686}
1687
1700ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1701
1719ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum);
1721
1755int coap_builder_init(coap_builder_t *state, void *buf, size_t buf_len,
1756 size_t header_len);
1757
1766static inline bool coap_builder_has_overflown(const coap_builder_t *state)
1767{
1768 return (state->size == 0);
1769}
1770
1781static inline ssize_t coap_builder_msg_size(const coap_builder_t *state)
1782{
1783 if (coap_builder_has_overflown(state)) {
1784 return -EOVERFLOW;
1785 }
1786
1787 return state->pos;
1788}
1789
1800static inline size_t coap_builder_buf_size(const coap_builder_t *state)
1801{
1802 return state->size;
1803}
1804
1812static inline size_t coap_builder_buf_remaining(const coap_builder_t *state)
1813{
1814 if (state->size > state->pos) {
1815 return state->size - state->pos;
1816 }
1817
1818 return 0;
1819}
1820
1844int coap_builder_init_reply(coap_builder_t *state, void *buf, size_t buf_len,
1845 coap_pkt_t *req, uint8_t code);
1846
1868int coap_builder_add_payload(coap_builder_t *state, const void *pld, size_t pld_len);
1869
1896void * coap_builder_allocate_payload(coap_builder_t *state, size_t pld_len);
1897
1912{
1913 return coap_builder_add_payload(state, NULL, 0);
1914}
1915
1935 uint16_t option);
1936
1953static inline int coap_opt_put_block1(coap_builder_t *state,
1954 coap_block_slicer_t *slicer)
1955{
1956 return coap_opt_put_block(state, slicer, COAP_OPT_BLOCK1);
1957}
1958
1975static inline int coap_opt_put_block2(coap_builder_t *state,
1976 coap_block_slicer_t *slicer)
1977{
1978 return coap_opt_put_block(state, slicer, COAP_OPT_BLOCK2);
1979}
1980
1999int coap_opt_put_uint(coap_builder_t *state, uint16_t onum, uint32_t value);
2000
2018 coap_block1_t *block)
2019{
2020 return coap_opt_put_uint(state, COAP_OPT_BLOCK1,
2021 (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
2022}
2023
2043 coap_block1_t *block)
2044{
2045 /* block.more must be zero, so no need to 'or' it in */
2046 return coap_opt_put_uint(state, COAP_OPT_BLOCK2,
2047 (block->blknum << 4) | block->szx);
2048}
2049
2066static inline int coap_opt_put_observe(coap_builder_t *state, uint32_t obs)
2067{
2068 obs &= COAP_OBS_MAX_VALUE_MASK; /* trim obs down to 24 bit */
2069 return coap_opt_put_uint(state, COAP_OPT_OBSERVE, obs);
2070}
2071
2093 const char *string, size_t len, char separator);
2113static inline int coap_opt_put_string(coap_builder_t *state, uint16_t optnum,
2114 const char *string, char separator)
2115{
2116 return coap_opt_put_string_with_len(state, optnum,
2117 string, strlen(string), separator);
2118}
2119
2137 const char *location)
2138{
2139 return coap_opt_put_string(state, COAP_OPT_LOCATION_PATH,
2140 location, '/');
2141}
2142
2160 const char *location)
2161{
2162 return coap_opt_put_string(state, COAP_OPT_LOCATION_QUERY,
2163 location, '&');
2164}
2165
2182static inline int coap_opt_put_uri_path(coap_builder_t *state, const char *uri)
2183{
2184 return coap_opt_put_string(state, COAP_OPT_URI_PATH, uri, '/');
2185}
2186
2204 const char *uri_query)
2205{
2206 return coap_opt_put_string(state, COAP_OPT_URI_QUERY, uri_query, '&');
2207}
2208
2232int coap_opt_put_uri_pathquery(coap_builder_t *state, const char *uri);
2233
2250static inline int coap_opt_put_proxy_uri(coap_builder_t *state, const char *uri)
2251{
2252 return coap_opt_put_string(state, COAP_OPT_PROXY_URI, uri, '\0');
2253}
2254
2280
2305int coap_opt_put(coap_builder_t *state, uint16_t onum, const void *odata, size_t olen);
2306
2326 unsigned blknum, unsigned szx, int more)
2327{
2328 return coap_opt_put_uint(state, COAP_OPT_BLOCK1,
2329 (blknum << 4) | szx | (more ? 0x8 : 0));
2330}
2331
2348static inline int coap_opt_put_ct(coap_builder_t *state, uint16_t content_type)
2349{
2350 return coap_opt_put_uint(state, COAP_OPT_CONTENT_FORMAT, content_type);
2351}
2352
2353
2373ssize_t coap_build_udp_hdr(void *buf, size_t buf_len, uint8_t type, const void *token,
2374 size_t token_len, uint8_t code, uint16_t id);
2375
2437ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
2438 uint8_t *rbuf, unsigned rlen, unsigned max_data_len);
2439
2455
2470ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len,
2471 coap_request_ctx_t *ctx);
2472
2489ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2490 unsigned resp_buf_len, coap_request_ctx_t *ctx,
2491 const coap_resource_t *resources,
2492 size_t resources_numof);
2493
2511ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
2512 size_t resp_buf_len, coap_request_ctx_t *context);
2513
2521static inline coap_method_flags_t coap_method2flag(unsigned code)
2522{
2523 return (1 << (code - 1));
2524}
2525
2541ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len);
2542
2557void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
2558
2572static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
2573{
2574 pkt->payload += len;
2575 pkt->payload_len -= len;
2576}
2577
2599ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
2600
2614ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
2615
2641ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code,
2642 void *buf, size_t len, uint16_t ct,
2643 void **payload, size_t *payload_len_max);
2644
2669 uint8_t code,
2670 uint8_t *buf, size_t len,
2671 uint16_t ct,
2672 const void *payload, size_t payload_len);
2673
2679 uint8_t *buf, size_t len,
2680 coap_request_ctx_t *context);
2682
2686#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
2687#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE !IS_USED(MODULE_GCOAP)
2688#endif
2689
2705int coap_match_path(const coap_resource_t *resource, const char *uri);
2706
2707#if defined(MODULE_GCOAP) || defined(DOXYGEN)
2720static inline bool coap_has_observe(coap_pkt_t *pkt)
2721{
2722 return pkt->observe_value != UINT32_MAX;
2723}
2724
2730static inline void coap_clear_observe(coap_pkt_t *pkt)
2731{
2732 pkt->observe_value = UINT32_MAX;
2733}
2734
2742static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
2743{
2744 return pkt->observe_value;
2745}
2746
2747#endif
2748
2752#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2753 { \
2754 .path = "/.well-known/core", \
2755 .methods = COAP_GET, \
2756 .handler = coap_well_known_core_default_handler \
2757 }
2758
2759#ifdef __cplusplus
2760}
2761#endif
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Bitfield operations on bitfields of arbitrary length.
Functions to work with different byte orders.
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:520
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:505
Common macros and compiler attributes/pragmas configuration.
#define WARN_UNUSED_RESULT
Attribute to add to a function whose return value should not silently be discarded.
#define EOVERFLOW
Value too large to be stored in data type.
Definition errno.h:133
#define COAP_OBS_MAX_VALUE_MASK
observe value is 24 bits
Definition coap.h:533
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:130
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition nanocoap.h:138
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:1581
int coap_blockwise_put_char(coap_builder_t *state, coap_block_slicer_t *slicer, char c)
Add a single character to a block2 reply when building the response using a coap_builder_t structure ...
int coap_opt_put_block(coap_builder_t *state, coap_block_slicer_t *slicer, uint16_t option)
Insert block option into buffer.
ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum, char *target, size_t max_len, char separator)
Read a full option as null terminated string into the target buffer.
int coap_match_path(const coap_resource_t *resource, const char *uri)
Checks if a CoAP resource path matches a given URI.
bool coap_is_hdr_in_bounds(const coap_pkt_t *pkt, size_t len)
Validate that the header of pkt is no longer than len bytes.
WARN_UNUSED_RESULT int coap_get_block(coap_pkt_t *pkt, struct _coap_block *block, uint16_t option)
Internal Block Option getter.
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:738
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:1434
static unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition nanocoap.h:536
void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len)
Initialize a packet struct, to build a message buffer.
static ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
Append a Content-Format option to the pkt buffer.
Definition nanocoap.h:1523
int coap_blockwise_put_char_pkt(coap_pkt_t *pdu, coap_block_slicer_t *slicer, char c)
Add a single character to a block2 reply when building the response using a coap_pkt_t structure to a...
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition nanocoap.h:524
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:1661
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:956
static ssize_t coap_get_location_path(coap_pkt_t *pkt, char *target, size_t max_len)
Convenience function for getting the packet's LOCATION_PATH option.
Definition nanocoap.h:894
static int coap_opt_put_block2(coap_builder_t *state, coap_block_slicer_t *slicer)
Insert block2 option into buffer.
Definition nanocoap.h:1975
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len, coap_request_ctx_t *ctx)
Handle incoming CoAP request.
static size_t coap_builder_buf_size(const coap_builder_t *state)
Get the size of the buffer in state.
Definition nanocoap.h:1800
static unsigned coap_get_id(const coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition nanocoap.h:596
bool coap_block_finish(coap_block_slicer_t *slicer)
Finish a block request (block1 or block2)
static int coap_opt_put_ct(coap_builder_t *state, uint16_t content_type)
Insert content type option into buffer.
Definition nanocoap.h:2348
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:1505
static int coap_opt_put_location_path(coap_builder_t *state, const char *location)
Convenience function for inserting LOCATION_PATH option into buffer.
Definition nanocoap.h:2136
int coap_builder_add_payload(coap_builder_t *state, const void *pld, size_t pld_len)
Add a payload marker and payload with bounds checking.
static int coap_opt_put_uri_path(coap_builder_t *state, const char *uri)
Convenience function for inserting URI_PATH option into buffer.
Definition nanocoap.h:2182
void * coap_builder_allocate_payload(coap_builder_t *state, size_t pld_len)
Write the payload marker into the current position of state and allocate pld_len bytes of payload aft...
void coap_request_ctx_init(coap_request_ctx_t *ctx, sock_udp_ep_t *remote)
Initialize CoAP request context.
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
static int coap_opt_put_proxy_uri(coap_builder_t *state, const char *uri)
Convenience function for inserting PROXY_URI option into buffer.
Definition nanocoap.h:2250
static ssize_t coap_get_location_query(coap_pkt_t *pkt, char *target, size_t max_len)
Convenience function for getting the packet's LOCATION_QUERY option.
Definition nanocoap.h:916
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.
uint16_t coap_method_flags_t
Method flag type.
Definition nanocoap.h:296
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:501
static unsigned coap_get_ver(const coap_pkt_t *pkt)
Get the CoAP version number.
Definition nanocoap.h:681
ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt, uint8_t **value, bool init_opt)
Iterate over a packet's options.
ssize_t coap_parse_udp(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU in UDP / DTLS format.
static bool coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition nanocoap.h:1142
int coap_opt_put_uint(coap_builder_t *state, uint16_t onum, uint32_t value)
Encode the given uint option into buffer.
static coap_method_t coap_get_method(const coap_pkt_t *pkt)
Get a request's method type.
Definition nanocoap.h:584
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:715
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:752
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:548
int coap_opt_put_uri_pathquery(coap_builder_t *state, const char *uri)
Convenience function for inserting URI_PATH and URI_QUERY into buffer This function will automaticall...
static ssize_t coap_builder_msg_size(const coap_builder_t *state)
Get the size of the CoAP message in state.
Definition nanocoap.h:1781
int coap_blockwise_put_bytes(coap_builder_t *state, coap_block_slicer_t *slicer, const void *c, size_t len)
Add a byte array to a block2 reply when building the response using a coap_builder_t structure to ass...
int coap_blockwise_put_bytes_pkt(coap_pkt_t *pdu, coap_block_slicer_t *slicer, const void *c, size_t len)
Add a byte array to a block2 reply when building the response using a coap_pkt_t structure to assembl...
static ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string, char separator)
Encode the given string as option(s) into pkt.
Definition nanocoap.h:1641
ssize_t coap_build_empty_ack(const coap_pkt_t *pkt, coap_udp_hdr_t *ack)
Build empty reply to CoAP request.
WARN_UNUSED_RESULT int coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
int coap_put_block1_ok(coap_builder_t *state, coap_block1_t *block1)
Insert block1 option into buffer (from coap_block1_t)
static unsigned coap_get_code_detail(const coap_pkt_t *pkt)
Get a message's code detail (5 least significant bits of code)
Definition nanocoap.h:560
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 void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition nanocoap.h:2572
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:2742
static WARN_UNUSED_RESULT int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition nanocoap.h:1311
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:1486
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
Definition nanocoap.h:669
static int coap_opt_put_uri_query(coap_builder_t *state, const char *uri_query)
Convenience function for inserting URI_QUERY option into buffer.
Definition nanocoap.h:2203
static int coap_opt_put_string(coap_builder_t *state, uint16_t optnum, const char *string, char separator)
Encode the given string as multi-part option into buffer.
Definition nanocoap.h:2113
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:572
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:774
static int coap_opt_put_block1(coap_builder_t *state, coap_block_slicer_t *slicer)
Insert block1 option into buffer.
Definition nanocoap.h:1953
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:727
ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value)
Retrieve the value for an option as an opaque array of bytes.
static int coap_opt_put_block2_control(coap_builder_t *state, coap_block1_t *block)
Insert block2 option into buffer in control usage.
Definition nanocoap.h:2042
struct _coap_block coap_block1_t
Block1 helper struct.
Definition nanocoap.h:448
static int coap_opt_put_block1_control(coap_builder_t *state, coap_block1_t *block)
Insert block1 option into buffer in control usage.
Definition nanocoap.h:2017
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:1681
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
Definition nanocoap.h:640
ssize_t coap_reply_simple(coap_pkt_t *pkt, uint8_t code, uint8_t *buf, size_t len, uint16_t ct, const void *payload, size_t payload_len)
Create CoAP reply (convenience function)
static const coap_udp_hdr_t * coap_get_udp_hdr_const(const coap_pkt_t *pkt)
Same as coap_get_udp_hdr but for const memory.
Definition nanocoap.h:510
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition nanocoap.h:1123
static int coap_opt_put_observe(coap_builder_t *state, uint32_t obs)
Insert an CoAP Observe Option into the buffer.
Definition nanocoap.h:2066
void * coap_request_ctx_get_context(const coap_request_ctx_t *ctx)
Get resource context associated with a CoAP request.
ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code, void *buf, size_t len, uint16_t ct, void **payload, size_t *payload_len_max)
Create CoAP reply header (convenience function)
ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri)
Adds a single Proxy-URI option into pkt.
static size_t coap_builder_buf_remaining(const coap_builder_t *state)
Get the remaining free buffer space in state.
Definition nanocoap.h:1812
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
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.
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:289
static int coap_opt_put_location_query(coap_builder_t *state, const char *location)
Convenience function for inserting LOCATION_QUERY option into buffer.
Definition nanocoap.h:2159
WARN_UNUSED_RESULT int coap_builder_init_reply(coap_builder_t *state, void *buf, size_t buf_len, coap_pkt_t *req, uint8_t code)
Initialize state for building a response and add the response header with matching token and suitable...
const char * coap_request_ctx_get_path(const coap_request_ctx_t *ctx)
Get resource path associated with a CoAP request.
static WARN_UNUSED_RESULT int coap_get_block2(coap_pkt_t *pkt, coap_block2_t *block)
Block2 option getter.
Definition nanocoap.h:1334
ssize_t coap_opt_add_block(coap_pkt_t *pkt, coap_block_slicer_t *slicer, bool more, uint16_t option)
Add block option in descriptive use from a slicer object.
static ssize_t coap_get_uri_path(coap_pkt_t *pkt, char *target)
Convenience function for getting the packet's URI_PATH.
Definition nanocoap.h:937
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 bool coap_builder_has_overflown(const coap_builder_t *state)
Check if building the message failed due to insufficient buffer space.
Definition nanocoap.h:1766
const sock_udp_ep_t * coap_request_ctx_get_local_udp(const coap_request_ctx_t *ctx)
Get the local endpoint on which the request has been received.
WARN_UNUSED_RESULT int coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, size_t blksize)
Initialize a block slicer struct from content information.
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more)
Initialize a block struct from content information.
ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars, size_t chars_len, char separator)
Encode the given array of characters as option(s) into pkt.
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:1068
static int coap_builder_add_payload_marker(coap_builder_t *state)
Add a payload marker with bounds checking.
Definition nanocoap.h:1911
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:654
int coap_opt_put_string_with_len(coap_builder_t *state, uint16_t optnum, const char *string, size_t len, char separator)
Encode the given string as multi-part option into buffer.
struct _coap_request_ctx coap_request_ctx_t
Forward declaration of internal CoAP resource request handler context.
Definition nanocoap.h:238
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition nanocoap.h:2720
int coap_opt_put(coap_builder_t *state, uint16_t onum, const void *odata, size_t olen)
Insert a CoAP option into buffer.
static ssize_t coap_opt_add_block1(coap_pkt_t *pkt, coap_block_slicer_t *slicer, bool more)
Add block1 option in descriptive use from a slicer object.
Definition nanocoap.h:1410
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition nanocoap.h:2521
struct _coap_block coap_block2_t
Block2 helper struct.
Definition nanocoap.h:453
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:1468
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:607
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition nanocoap.h:619
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition nanocoap.h:2730
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
int coap_builder_init(coap_builder_t *state, void *buf, size_t buf_len, size_t header_len)
Initialize state for building a message.
ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len, const char *val, size_t val_len)
Adds a single Uri-Query option in the form 'key=value' into pkt.
static int coap_opt_put_block1_raw(coap_builder_t *state, unsigned blknum, unsigned szx, int more)
Insert block1 option into buffer.
Definition nanocoap.h:2325
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definition udp.h:292
struct iolist iolist_t
iolist forward declaration
Definition iolist.h:30
iolist scatter / gather IO
Common macros and compiler attributes/pragmas configuration.
Generic CoAP values as defined by RFC7252.
UDP sock definitions.
Block1 helper struct.
Definition nanocoap.h:438
uint32_t blknum
block number
Definition nanocoap.h:440
size_t offset
offset of received data
Definition nanocoap.h:439
bool more
0 for last block, 1 for more blocks coming
Definition nanocoap.h:442
uint8_t szx
szx value
Definition nanocoap.h:441
CoAP resource request handler context.
Definition nanocoap.h:328
sock_udp_ep_t * local
Definition nanocoap.h:337
const coap_resource_t * resource
resource of the request
Definition nanocoap.h:329
sock_udp_ep_t * local_udp
local UDP endpoint of the request
Definition nanocoap.h:336
sock_udp_ep_t * remote
Definition nanocoap.h:332
uint32_t tl_type
transport the packet was received over
Definition nanocoap.h:347
sock_udp_ep_t * remote_udp
remote UDP endpoint of the request
Definition nanocoap.h:331
Blockwise transfer helper struct.
Definition nanocoap.h:458
size_t end
End offset of the current block.
Definition nanocoap.h:460
size_t start
Start offset of the current block.
Definition nanocoap.h:459
uint8_t * opt_value
Pointer to the value of the placed option.
Definition nanocoap.h:462
size_t cur
Offset of the generated content.
Definition nanocoap.h:461
Structure to hold the state for building a CoAP message.
Definition nanocoap.h:423
uint16_t last_opt_num
Number of the last option added (for delta-encoding)
Definition nanocoap.h:427
uint16_t pos
Position of the next byte to write within buf
Definition nanocoap.h:425
uint16_t size
Size of buf in bytes.
Definition nanocoap.h:426
uint8_t * buf
Buffer to build the message into.
Definition nanocoap.h:424
CoAP option array entry.
Definition nanocoap.h:194
uint16_t offset
offset in packet
Definition nanocoap.h:196
uint16_t opt_num
full CoAP option number
Definition nanocoap.h:195
CoAP PDU parsing context structure.
Definition nanocoap.h:217
coap_optpos_t options[CONFIG_NANOCOAP_NOPTS_MAX]
option offset array
Definition nanocoap.h:228
uint8_t * payload
pointer to end of the header
Definition nanocoap.h:224
uint16_t payload_len
length of payload
Definition nanocoap.h:226
uint16_t options_len
length of options array
Definition nanocoap.h:227
uint8_t * buf
pointer to the beginning of the buffer holding the pkt
Definition nanocoap.h:223
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
Raw CoAP over UDP PDU header structure.
Definition nanocoap.h:185
uint8_t code
CoAP code (e.g.m 205)
Definition nanocoap.h:187
uint8_t ver_t_tkl
version, token, token length
Definition nanocoap.h:186
uint16_t id
Req/resp ID.
Definition nanocoap.h:188
Cross File Arrays.