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 "bitfield.h"
90 #include "byteorder.h"
91 #include "iolist.h"
92 #include "net/coap.h"
93 #else
94 #include "coap.h"
95 #include <arpa/inet.h>
96 #endif
97 
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101 
107 #define COAP_GET (0x01)
108 #define COAP_POST (0x02)
109 #define COAP_PUT (0x04)
110 #define COAP_DELETE (0x08)
111 #define COAP_FETCH (0x10)
112 #define COAP_PATCH (0x20)
113 #define COAP_IPATCH (0x40)
114 #define COAP_IGNORE (0xFF)
116 #define COAP_MATCH_SUBTREE (0x8000)
123 #define COAP_FORMAT_NONE (UINT16_MAX)
124 
132 #ifndef CONFIG_NANOCOAP_NOPTS_MAX
133 #define CONFIG_NANOCOAP_NOPTS_MAX (16)
134 #endif
135 
140 #ifndef CONFIG_NANOCOAP_URI_MAX
141 #define CONFIG_NANOCOAP_URI_MAX (64)
142 #endif
143 
147 #ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
148 #define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
149 #endif
150 
154 #ifndef CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT
155 #define CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT COAP_BLOCKSIZE_64
156 #endif
157 
159 #ifndef CONFIG_NANOCOAP_QS_MAX
160 #define CONFIG_NANOCOAP_QS_MAX (64)
161 #endif
169 #ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX
170 #define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (80)
171 #endif
172 
180 #define COAP_OPT_FINISH_NONE (0x0000)
182 #define COAP_OPT_FINISH_PAYLOAD (0x0001)
188 typedef struct __attribute__((packed)) {
189  uint8_t ver_t_tkl;
190  uint8_t code;
191  uint16_t id;
192 } coap_hdr_t;
193 
197 typedef struct {
198  uint16_t opt_num;
199  uint16_t offset;
200 } coap_optpos_t;
201 
219 typedef struct {
221  uint8_t *token;
224  uint8_t *payload;
226  uint16_t payload_len;
227  uint16_t options_len;
230 #ifdef MODULE_GCOAP
231  uint32_t observe_value;
238  uint32_t tl_type;
239 #endif
240 } coap_pkt_t;
241 
256 typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context);
257 
270 typedef int (*coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more);
271 
282 typedef int (*coap_request_cb_t)(void *arg, coap_pkt_t *pkt);
283 
289 typedef uint16_t coap_method_flags_t;
290 
294 typedef struct {
295  const char *path;
298  void *context;
300 
304 typedef const struct {
306  const size_t resources_numof;
308 
312 typedef struct {
313  size_t offset;
314  uint32_t blknum;
315  unsigned szx;
316  int more;
318 } coap_block1_t;
319 
323 typedef struct {
324  size_t start;
325  size_t end;
326  size_t cur;
327  uint8_t *opt;
329 
333 extern const coap_resource_t coap_resources[];
334 
338 extern const unsigned coap_resources_numof;
339 
354 static inline uint8_t coap_code(unsigned cls, unsigned detail)
355 {
356  return (cls << 5) | detail;
357 }
358 
366 static inline unsigned coap_get_code_class(const coap_pkt_t *pkt)
367 {
368  return pkt->hdr->code >> 5;
369 }
370 
378 static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
379 {
380  return pkt->hdr->code & 0x1f;
381 }
382 
390 static inline unsigned coap_get_code(const coap_pkt_t *pkt)
391 {
392  return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
393 }
394 
402 static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt)
403 {
404  return (unsigned)pkt->hdr->code;
405 }
406 
414 static inline unsigned coap_get_id(const coap_pkt_t *pkt)
415 {
416  return ntohs(pkt->hdr->id);
417 }
418 
426 static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
427 {
428  return (pkt->hdr->ver_t_tkl & 0xf);
429 }
430 
438 static inline void *coap_get_token(const coap_pkt_t *pkt)
439 {
440  return (uint8_t*)pkt->hdr + sizeof(coap_hdr_t);
441 }
442 
450 static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
451 {
452  return sizeof(coap_hdr_t) + coap_get_token_len(pkt);
453 }
454 
464 static inline unsigned coap_get_total_len(const coap_pkt_t *pkt)
465 {
466  return (uintptr_t)pkt->payload - (uintptr_t)pkt->hdr + pkt->payload_len;
467 }
468 
479 static inline unsigned coap_get_type(const coap_pkt_t *pkt)
480 {
481  return (pkt->hdr->ver_t_tkl & 0x30) >> 4;
482 }
483 
491 static inline unsigned coap_get_ver(const coap_pkt_t *pkt)
492 {
493  return (pkt->hdr->ver_t_tkl & 0x60) >> 6;
494 }
495 
503 static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr)
504 {
505  return ((uint8_t *)hdr) + sizeof(coap_hdr_t);
506 }
507 
514 static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
515 {
516  hdr->code = code;
517 }
518 
527 static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
528 {
529  /* assert correct range of type */
530  assert(!(type & ~0x3));
531 
532  hdr->ver_t_tkl &= ~0x30;
533  hdr->ver_t_tkl |= type << 4;
534 }
563 uint8_t *coap_find_option(coap_pkt_t *pkt, unsigned opt_num);
564 
574 
587 int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
588 
606 ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum,
607  uint8_t *target, size_t max_len, char separator);
608 
624 static inline ssize_t coap_get_location_path(coap_pkt_t *pkt,
625  uint8_t *target, size_t max_len)
626 {
627  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
628  target, max_len, '/');
629 }
630 
646 static inline ssize_t coap_get_location_query(coap_pkt_t *pkt,
647  uint8_t *target, size_t max_len)
648 {
649  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
650  target, max_len, '&');
651 }
652 
667 static inline ssize_t coap_get_uri_path(coap_pkt_t *pkt, uint8_t *target)
668 {
669  return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
671 }
672 
687 static inline ssize_t coap_get_uri_query(coap_pkt_t *pkt, uint8_t *target)
688 {
689  return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY, target,
691 }
692 
722 ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt,
723  uint8_t **value, bool init_opt);
724 
741 ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
756 static inline ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
757 {
758  return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
759 }
760 
778 void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
779  int more);
780 
796 bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
797 
812 static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
813 {
814  return coap_block_finish(slicer, COAP_OPT_BLOCK1);
815 }
816 
831 static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
832 {
833  return coap_block_finish(slicer, COAP_OPT_BLOCK2);
834 }
835 
846 
856 void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum,
857  size_t blksize);
858 
873 size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
874  const void *c, size_t len);
875 
889 size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
890 
909 int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
910 
928 static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
929 {
930  return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
931 }
932 
942 static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
943 {
944  return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
945 }
946 
959 int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx);
960 
979 
987 static inline unsigned coap_szx2size(unsigned szx)
988 {
989  return (1 << (szx + 4));
990 }
1024  bool more, uint16_t option);
1025 
1044 static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
1045  coap_block_slicer_t *slicer, bool more)
1046 {
1047  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
1048 }
1049 
1068 static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
1069  coap_block_slicer_t *slicer, bool more)
1070 {
1071  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
1072 }
1087 ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
1088 
1102 static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
1103  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
1104  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1105 }
1106 
1120 static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
1121  /* block.more must be zero, so no need to 'or' it in */
1122  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
1123  (block->blknum << 4) | block->szx);
1124 }
1125 
1139 static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
1140 {
1141  return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
1142 }
1143 
1157 static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1158 {
1159  return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1160 }
1161 
1177 ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len);
1178 
1196 ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1197  const char *val, size_t val_len);
1198 
1215 static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1216  const char *val)
1217 {
1218  return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1219 }
1220 
1235 ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1236 
1255 ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1256  size_t chars_len, char separator);
1257 
1275 static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1276  const char *string, char separator)
1277 {
1278  return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1279 }
1280 
1295 static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1296 {
1297  return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1298 }
1299 
1315 static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1316  const char *path,
1317  size_t path_len)
1318 {
1319  return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1320 }
1321 
1334 ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1335 
1353 ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum);
1381 size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1382  bool more, uint16_t option);
1383 
1399 static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1400  coap_block_slicer_t *slicer, bool more)
1401 {
1402  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1403 }
1404 
1420 static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1421  coap_block_slicer_t *slicer, bool more)
1422 {
1423  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1424 }
1425 
1437 size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1438  uint32_t value);
1439 
1449 static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1450  coap_block1_t *block)
1451 {
1452  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1453  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1454 }
1455 
1467 static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1468  coap_block1_t *block)
1469 {
1470  /* block.more must be zero, so no need to 'or' it in */
1471  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1472  (block->blknum << 4) | block->szx);
1473 }
1474 
1488 size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1489  const char *string, size_t len, char separator);
1502 static inline size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum,
1503  uint16_t optnum,
1504  const char *string, char separator)
1505 {
1506  return coap_opt_put_string_with_len(buf, lastonum, optnum,
1507  string, strlen(string), separator);
1508 }
1509 
1520 static inline size_t coap_opt_put_location_path(uint8_t *buf,
1521  uint16_t lastonum,
1522  const char *location)
1523 {
1524  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1525  location, '/');
1526 }
1527 
1538 static inline size_t coap_opt_put_location_query(uint8_t *buf,
1539  uint16_t lastonum,
1540  const char *location)
1541 {
1542  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
1543  location, '&');
1544 }
1545 
1556 static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
1557  const char *uri)
1558 {
1559  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
1560 }
1561 
1572 static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
1573  const char *uri)
1574 {
1575  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
1576 }
1577 
1595 size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri);
1596 
1607 static inline size_t coap_opt_put_proxy_uri(uint8_t *buf, uint16_t lastonum,
1608  const char *uri)
1609 {
1610  return coap_opt_put_string(buf, lastonum, COAP_OPT_PROXY_URI, uri, '\0');
1611 }
1612 
1630 size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
1631 
1648 size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen);
1649 
1662 static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
1663  unsigned blknum, unsigned szx, int more)
1664 {
1665  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1666  (blknum << 4) | szx | (more ? 0x8 : 0));
1667 }
1668 
1679 static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
1680  uint16_t content_type)
1681 {
1682  return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
1683 }
1709 ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
1710  uint8_t *rbuf, unsigned rlen, unsigned payload_len,
1711  coap_block_slicer_t *slicer);
1712 
1727 ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, uint8_t *token,
1728  size_t token_len, unsigned code, uint16_t id);
1729 
1751 ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
1752  uint8_t *rbuf, unsigned rlen, unsigned payload_len);
1753 
1767 ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len);
1768 
1784 ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
1785  unsigned resp_buf_len,
1786  const coap_resource_t *resources,
1787  size_t resources_numof);
1788 
1805 ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
1806  size_t resp_buf_len, void *context);
1807 
1815 static inline coap_method_flags_t coap_method2flag(unsigned code)
1816 {
1817  return (1 << (code - 1));
1818 }
1819 
1834 int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
1835 
1850 void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
1851 
1865 static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
1866 {
1867  pkt->payload += len;
1868  pkt->payload_len -= len;
1869 }
1870 
1892 ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
1893 
1907 ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
1908 
1931  unsigned code,
1932  uint8_t *buf, size_t len,
1933  unsigned ct,
1934  const void *payload, size_t payload_len);
1935 
1941  uint8_t *buf, size_t len,
1942  void *context);
1960 int coap_match_path(const coap_resource_t *resource, uint8_t *uri);
1961 
1962 #if defined(MODULE_GCOAP) || defined(DOXYGEN)
1975 static inline bool coap_has_observe(coap_pkt_t *pkt)
1976 {
1977  return pkt->observe_value != UINT32_MAX;
1978 }
1979 
1985 static inline void coap_clear_observe(coap_pkt_t *pkt)
1986 {
1987  pkt->observe_value = UINT32_MAX;
1988 }
1989 
1997 static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
1998 {
1999  return pkt->observe_value;
2000 }
2002 #endif
2003 
2007 #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2008  { \
2009  .path = "/.well-known/core", \
2010  .methods = COAP_GET, \
2011  .handler = coap_well_known_core_default_handler \
2012  }
2013 
2014 #ifdef __cplusplus
2015 }
2016 #endif
2017 #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:113
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:576
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
Definition: nanocoap.h:133
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition: nanocoap.h:141
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len)
Handle incoming CoAP request.
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:1215
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:1572
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:1467
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:1068
static unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition: nanocoap.h:402
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 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:1399
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:1157
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition: nanocoap.h:354
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:1295
static unsigned coap_get_id(const coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition: nanocoap.h:414
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:646
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:1556
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:1139
ssize_t coap_opt_get_string(coap_pkt_t *pkt, uint16_t optnum, uint8_t *target, size_t max_len, char separator)
Read a full option as null terminated string into the target buffer.
static ssize_t coap_get_uri_query(coap_pkt_t *pkt, uint8_t *target)
Convenience function for getting the packet's URI_QUERY option.
Definition: nanocoap.h:687
int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx)
Generic block option getter.
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.
const coap_resource_t coap_resources[]
Global CoAP resource list.
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:514
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
static uint8_t * coap_hdr_data_ptr(const coap_hdr_t *hdr)
Get the start of data after the header.
Definition: nanocoap.h:503
uint8_t * coap_find_option(coap_pkt_t *pkt, unsigned opt_num)
Get pointer to an option field by type.
static unsigned coap_get_code(const coap_pkt_t *pkt)
Get a message's code in decimal format ((class * 100) + detail)
Definition: nanocoap.h:390
int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value)
Get a uint32 option value.
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:1502
uint16_t coap_method_flags_t
Method flag type.
Definition: nanocoap.h:289
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:491
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:1420
int coap_match_path(const coap_resource_t *resource, uint8_t *uri)
Checks if a CoAP resource path matches a given URI.
static bool coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition: nanocoap.h:831
const unsigned coap_resources_numof
Number of entries in global CoAP resource list.
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 unsigned coap_szx2size(unsigned szx)
Helper to decode SZX value to size in bytes.
Definition: nanocoap.h:987
ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Reference to the default .well-known/core handler defined by the application.
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:450
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Resource handler type.
Definition: nanocoap.h:256
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:366
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition: nanocoap.h:942
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:624
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:1275
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:378
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:928
void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c)
Add a single character to the payload data of the CoAP request.
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:1607
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition: nanocoap.h:527
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition: nanocoap.h:1865
static uint32_t coap_get_observe(coap_pkt_t *pkt)
Get the value of the observe option from the given packet.
Definition: nanocoap.h:1997
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:1662
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:1120
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
Definition: nanocoap.h:479
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:1315
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:1538
ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, uint8_t *token, size_t token_len, unsigned code, uint16_t id)
Builds a CoAP header.
ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, size_t resp_buf_len, void *context)
Generic coap subtree handler.
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:1520
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition: nanocoap.h:812
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:1679
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:270
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap equest callback descriptor.
Definition: nanocoap.h:282
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:1449
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.
int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU.
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_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len, const coap_resource_t *resources, size_t resources_numof)
Pass a coap request to a matching handler.
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:756
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:667
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:464
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition: nanocoap.h:1975
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:1044
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition: nanocoap.h:1815
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:1102
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition: nanocoap.h:426
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
Definition: nanocoap.h:438
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition: nanocoap.h:1985
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.
Definitions for internet operations.
iolist scatter / gather IO
Generic CoAP values as defined by RFC7252.
Block1 helper struct.
Definition: nanocoap.h:312
uint32_t blknum
block number
Definition: nanocoap.h:314
size_t offset
offset of received data
Definition: nanocoap.h:313
int more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition: nanocoap.h:316
unsigned szx
szx value
Definition: nanocoap.h:315
Blockwise transfer helper struct.
Definition: nanocoap.h:323
uint8_t * opt
Pointer to the placed option
Definition: nanocoap.h:327
size_t end
End offset of the current block
Definition: nanocoap.h:325
size_t start
Start offset of the current block
Definition: nanocoap.h:324
size_t cur
Offset of the generated content
Definition: nanocoap.h:326
Raw CoAP PDU header structure.
Definition: nanocoap.h:188
uint8_t ver_t_tkl
version, token, token length
Definition: nanocoap.h:189
uint8_t code
CoAP code (e.g.m 205)
Definition: nanocoap.h:190
uint16_t id
Req/resp ID
Definition: nanocoap.h:191
CoAP option array entry.
Definition: nanocoap.h:197
uint16_t offset
offset in packet
Definition: nanocoap.h:199
uint16_t opt_num
full CoAP option number
Definition: nanocoap.h:198
CoAP PDU parsing context structure.
Definition: nanocoap.h:219
uint8_t * payload
pointer to end of the header
Definition: nanocoap.h:224
uint8_t * token
pointer to token
Definition: nanocoap.h:221
uint16_t payload_len
length of payload
Definition: nanocoap.h:226
coap_hdr_t * hdr
pointer to raw packet
Definition: nanocoap.h:220
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:304
const size_t resources_numof
number of entries in array
Definition: nanocoap.h:306
const coap_resource_t * resources
ptr to resource array
Definition: nanocoap.h:305
Type for CoAP resource entry.
Definition: nanocoap.h:294
const char * path
URI path of resource
Definition: nanocoap.h:295
coap_method_flags_t methods
OR'ed methods this resource allows.
Definition: nanocoap.h:296
coap_handler_t handler
ptr to resource handler
Definition: nanocoap.h:297
void * context
ptr to user defined context data
Definition: nanocoap.h:298
iolist structure definition
Definition: iolist.h:39