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 "byteorder.h"
90 #include "net/coap.h"
91 #else
92 #include "coap.h"
93 #include <arpa/inet.h>
94 #endif
95 
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99 
105 #define COAP_GET (0x01)
106 #define COAP_POST (0x02)
107 #define COAP_PUT (0x04)
108 #define COAP_DELETE (0x08)
109 #define COAP_FETCH (0x10)
110 #define COAP_PATCH (0x20)
111 #define COAP_IPATCH (0x40)
112 #define COAP_MATCH_SUBTREE (0x8000)
119 #define COAP_FORMAT_NONE (UINT16_MAX)
120 
128 #ifndef CONFIG_NANOCOAP_NOPTS_MAX
129 #define CONFIG_NANOCOAP_NOPTS_MAX (16)
130 #endif
131 
136 #ifndef CONFIG_NANOCOAP_URI_MAX
137 #define CONFIG_NANOCOAP_URI_MAX (64)
138 #endif
139 
143 #ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
144 #define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
145 #endif
146 
148 #ifndef CONFIG_NANOCOAP_QS_MAX
149 #define CONFIG_NANOCOAP_QS_MAX (64)
150 #endif
160 #define COAP_OPT_FINISH_NONE (0x0000)
162 #define COAP_OPT_FINISH_PAYLOAD (0x0001)
168 typedef struct __attribute__((packed)) {
169  uint8_t ver_t_tkl;
170  uint8_t code;
171  uint16_t id;
172 } coap_hdr_t;
173 
177 typedef struct {
178  uint16_t opt_num;
179  uint16_t offset;
180 } coap_optpos_t;
181 
185 typedef struct {
187  uint8_t *token;
188  uint8_t *payload;
189  uint16_t payload_len;
190  uint16_t options_len;
192 #ifdef MODULE_GCOAP
193  uint32_t observe_value;
194 #endif
195 } coap_pkt_t;
196 
211 typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context);
212 
218 typedef uint16_t coap_method_flags_t;
219 
223 typedef struct {
224  const char *path;
227  void *context;
229 
233 typedef struct {
234  size_t offset;
235  uint32_t blknum;
236  unsigned szx;
237  int more;
239 } coap_block1_t;
240 
244 typedef struct {
245  size_t start;
246  size_t end;
247  size_t cur;
248  uint8_t *opt;
250 
254 extern const coap_resource_t coap_resources[];
255 
259 extern const unsigned coap_resources_numof;
260 
275 static inline uint8_t coap_code(unsigned cls, unsigned detail)
276 {
277  return (cls << 5) | detail;
278 }
279 
287 static inline unsigned coap_get_code_class(coap_pkt_t *pkt)
288 {
289  return pkt->hdr->code >> 5;
290 }
291 
299 static inline unsigned coap_get_code_detail(const coap_pkt_t *pkt)
300 {
301  return pkt->hdr->code & 0x1f;
302 }
303 
311 static inline unsigned coap_get_code(coap_pkt_t *pkt)
312 {
313  return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
314 }
315 
323 static inline unsigned coap_get_code_raw(coap_pkt_t *pkt)
324 {
325  return (unsigned)pkt->hdr->code;
326 }
327 
335 static inline unsigned coap_get_id(coap_pkt_t *pkt)
336 {
337  return ntohs(pkt->hdr->id);
338 }
339 
347 static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
348 {
349  return (pkt->hdr->ver_t_tkl & 0xf);
350 }
351 
359 static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
360 {
361  return sizeof(coap_hdr_t) + coap_get_token_len(pkt);
362 }
363 
374 static inline unsigned coap_get_type(coap_pkt_t *pkt)
375 {
376  return (pkt->hdr->ver_t_tkl & 0x30) >> 4;
377 }
378 
386 static inline unsigned coap_get_ver(coap_pkt_t *pkt)
387 {
388  return (pkt->hdr->ver_t_tkl & 0x60) >> 6;
389 }
390 
398 static inline uint8_t *coap_hdr_data_ptr(coap_hdr_t *hdr)
399 {
400  return ((uint8_t *)hdr) + sizeof(coap_hdr_t);
401 }
402 
409 static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
410 {
411  hdr->code = code;
412 }
413 
422 static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
423 {
424  /* assert correct range of type */
425  assert(!(type & ~0x3));
426 
427  hdr->ver_t_tkl &= ~0x30;
428  hdr->ver_t_tkl |= type << 4;
429 }
447 
460 int coap_opt_get_uint(const coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
461 
479 ssize_t coap_opt_get_string(const coap_pkt_t *pkt, uint16_t optnum,
480  uint8_t *target, size_t max_len, char separator);
481 
497 static inline ssize_t coap_get_location_path(const coap_pkt_t *pkt,
498  uint8_t *target, size_t max_len)
499 {
500  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
501  target, max_len, '/');
502 }
503 
519 static inline ssize_t coap_get_location_query(const coap_pkt_t *pkt,
520  uint8_t *target, size_t max_len)
521 {
522  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
523  target, max_len, '&');
524 }
525 
540 static inline ssize_t coap_get_uri_path(const coap_pkt_t *pkt, uint8_t *target)
541 {
542  return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
544 }
545 
560 static inline ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target)
561 {
562  return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY, target,
564 }
565 
595 ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt,
596  uint8_t **value, bool init_opt);
597 
614 ssize_t coap_opt_get_opaque(const coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
629 static inline ssize_t coap_get_proxy_uri(const coap_pkt_t *pkt, char **target)
630 {
631  return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
632 }
633 
651 void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
652  int more);
653 
669 bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
670 
685 static inline bool coap_block1_finish(coap_block_slicer_t *slicer)
686 {
687  return coap_block_finish(slicer, COAP_OPT_BLOCK1);
688 }
689 
704 static inline bool coap_block2_finish(coap_block_slicer_t *slicer)
705 {
706  return coap_block_finish(slicer, COAP_OPT_BLOCK2);
707 }
708 
719 
729 void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum,
730  size_t blksize);
731 
746 size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
747  const uint8_t *c, size_t len);
748 
762 size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
763 
782 int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
783 
801 static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
802 {
803  return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
804 }
805 
815 static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
816 {
817  return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
818 }
819 
832 int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx);
833 
841 static inline unsigned coap_szx2size(unsigned szx)
842 {
843  return (1 << (szx + 4));
844 }
877  bool more, uint16_t option);
878 
897 static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
898  coap_block_slicer_t *slicer, bool more)
899 {
900  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
901 }
902 
921 static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
922  coap_block_slicer_t *slicer, bool more)
923 {
924  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
925 }
940 ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
941 
955 static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
956  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
957  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
958 }
959 
973 static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
974  /* block.more must be zero, so no need to 'or' it in */
975  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
976  (block->blknum << 4) | block->szx);
977 }
978 
992 static inline ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
993 {
994  return coap_opt_add_uint(pkt, COAP_OPT_ACCEPT, format);
995 }
996 
1010 static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
1011 {
1012  return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
1013 }
1014 
1030 ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len);
1031 
1049 ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1050  const char *val, size_t val_len);
1051 
1068 static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1069  const char *val)
1070 {
1071  return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1072 }
1073 
1088 ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1089 
1108 ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1109  size_t chars_len, char separator);
1110 
1128 static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1129  const char *string, char separator)
1130 {
1131  return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1132 }
1133 
1148 static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1149 {
1150  return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1151 }
1152 
1168 static inline ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt,
1169  const char *path,
1170  size_t path_len)
1171 {
1172  return coap_opt_add_chars(pkt, COAP_OPT_URI_PATH, path, path_len, '/');
1173 }
1174 
1187 ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1215 size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1216  bool more, uint16_t option);
1217 
1233 static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1234  coap_block_slicer_t *slicer, bool more)
1235 {
1236  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1237 }
1238 
1254 static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1255  coap_block_slicer_t *slicer, bool more)
1256 {
1257  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1258 }
1259 
1271 size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1272  uint32_t value);
1273 
1283 static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1284  coap_block1_t *block)
1285 {
1286  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1287  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1288 }
1289 
1301 static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1302  coap_block1_t *block)
1303 {
1304  /* block.more must be zero, so no need to 'or' it in */
1305  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1306  (block->blknum << 4) | block->szx);
1307 }
1308 
1322 size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1323  const char *string, size_t len, char separator);
1336 static inline size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum,
1337  uint16_t optnum,
1338  const char *string, char separator)
1339 {
1340  return coap_opt_put_string_with_len(buf, lastonum, optnum,
1341  string, strlen(string), separator);
1342 }
1343 
1354 static inline size_t coap_opt_put_location_path(uint8_t *buf,
1355  uint16_t lastonum,
1356  const char *location)
1357 {
1358  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1359  location, '/');
1360 }
1361 
1372 static inline size_t coap_opt_put_location_query(uint8_t *buf,
1373  uint16_t lastonum,
1374  const char *location)
1375 {
1376  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
1377  location, '&');
1378 }
1379 
1390 static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
1391  const char *uri)
1392 {
1393  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
1394 }
1395 
1406 static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
1407  const char *uri)
1408 {
1409  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
1410 }
1411 
1429 size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri);
1430 
1441 static inline size_t coap_opt_put_proxy_uri(uint8_t *buf, uint16_t lastonum,
1442  const char *uri)
1443 {
1444  return coap_opt_put_string(buf, lastonum, COAP_OPT_PROXY_URI, uri, '\0');
1445 }
1446 
1464 size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
1465 
1482 size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uint8_t *odata, size_t olen);
1483 
1496 static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
1497  unsigned blknum, unsigned szx, int more)
1498 {
1499  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1500  (blknum << 4) | szx | (more ? 0x8 : 0));
1501 }
1502 
1513 static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
1514  uint16_t content_type)
1515 {
1516  return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
1517 }
1543 ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
1544  uint8_t *rbuf, unsigned rlen, unsigned payload_len,
1545  coap_block_slicer_t *slicer);
1546 
1561 ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, uint8_t *token,
1562  size_t token_len, unsigned code, uint16_t id);
1563 
1585 ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
1586  uint8_t *rbuf, unsigned rlen, unsigned payload_len);
1587 
1601 ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len);
1602 
1618 ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
1619  unsigned resp_buf_len,
1620  const coap_resource_t *resources,
1621  size_t resources_numof);
1622 
1630 static inline coap_method_flags_t coap_method2flag(unsigned code)
1631 {
1632  return (1 << (code - 1));
1633 }
1634 
1649 int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
1650 
1665 void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
1666 
1680 static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
1681 {
1682  pkt->payload += len;
1683  pkt->payload_len -= len;
1684 }
1685 
1707 ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
1708 
1722 ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
1723 
1746  unsigned code,
1747  uint8_t *buf, size_t len,
1748  unsigned ct,
1749  const uint8_t *payload, uint8_t payload_len);
1750 
1756  uint8_t *buf, size_t len,
1757  void *context);
1775 int coap_match_path(const coap_resource_t *resource, uint8_t *uri);
1776 
1777 #if defined(MODULE_GCOAP) || defined(DOXYGEN)
1790 static inline bool coap_has_observe(coap_pkt_t *pkt)
1791 {
1792  return pkt->observe_value != UINT32_MAX;
1793 }
1794 
1800 static inline void coap_clear_observe(coap_pkt_t *pkt)
1801 {
1802  pkt->observe_value = UINT32_MAX;
1803 }
1804 
1812 static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
1813 {
1814  return pkt->observe_value;
1815 }
1817 #endif
1818 
1822 #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
1823  { \
1824  .path = "/.well-known/core", \
1825  .methods = COAP_GET, \
1826  .handler = coap_well_known_core_default_handler \
1827  }
1828 
1829 #ifdef __cplusplus
1830 }
1831 #endif
1832 #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:104
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:129
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition: nanocoap.h:137
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:1068
static ssize_t coap_get_location_query(const coap_pkt_t *pkt, uint8_t *target, size_t max_len)
Convenience function for getting the packet's LOCATION_QUERY option.
Definition: nanocoap.h:519
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:1406
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:1301
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:921
static unsigned coap_get_code_class(coap_pkt_t *pkt)
Get a message's code class (3 most significant bits of code)
Definition: nanocoap.h:287
ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len)
Encode the given buffer as an opaque data option into pkt.
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:1233
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:1010
static unsigned coap_get_id(coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition: nanocoap.h:335
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition: nanocoap.h:275
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:1148
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:1390
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:992
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.
static unsigned coap_get_code_raw(coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition: nanocoap.h:323
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:409
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
static ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target)
Convenience function for getting the packet's URI_QUERY option.
Definition: nanocoap.h:560
static unsigned coap_get_ver(coap_pkt_t *pkt)
Get the CoAP version number.
Definition: nanocoap.h:386
static uint8_t * coap_hdr_data_ptr(coap_hdr_t *hdr)
Get the start of data after the header.
Definition: nanocoap.h:398
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:1336
uint16_t coap_method_flags_t
Method flag type.
Definition: nanocoap.h:218
int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option)
Block option getter.
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:1254
int coap_match_path(const coap_resource_t *resource, uint8_t *uri)
Checks if a CoAP resource path matches a given URI.
size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uint8_t *odata, size_t olen)
Insert a CoAP option into buffer.
static bool coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition: nanocoap.h:704
const unsigned coap_resources_numof
Number of entries in global CoAP resource list.
static unsigned coap_szx2size(unsigned szx)
Helper to decode SZX value to size in bytes.
Definition: nanocoap.h:841
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:359
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Resource handler type.
Definition: nanocoap.h:211
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition: nanocoap.h:815
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.
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:1128
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:299
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition: nanocoap.h:801
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_reply_simple(coap_pkt_t *pkt, unsigned code, uint8_t *buf, size_t len, unsigned ct, const uint8_t *payload, uint8_t payload_len)
Create CoAP reply (convenience function)
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 unsigned coap_get_code(coap_pkt_t *pkt)
Get a message's code in decimal format ((class * 100) + detail)
Definition: nanocoap.h:311
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:1441
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition: nanocoap.h:422
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition: nanocoap.h:1680
static uint32_t coap_get_observe(coap_pkt_t *pkt)
Get the value of the observe option from the given packet.
Definition: nanocoap.h:1812
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:1496
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:973
ssize_t coap_opt_get_string(const 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_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:1168
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:1372
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.
static unsigned coap_get_type(coap_pkt_t *pkt)
Get the message type.
Definition: nanocoap.h:374
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:1354
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition: nanocoap.h:685
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:1513
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_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:1283
size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, const uint8_t *c, size_t len)
Add a byte array to a block2 reply.
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.
ssize_t coap_opt_get_opaque(const coap_pkt_t *pkt, unsigned opt_num, uint8_t **value)
Retrieve the value for an option as an opaque array of bytes.
int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU.
static ssize_t coap_get_location_path(const coap_pkt_t *pkt, uint8_t *target, size_t max_len)
Convenience function for getting the packet's LOCATION_PATH option.
Definition: nanocoap.h:497
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.
unsigned coap_get_content_type(coap_pkt_t *pkt)
Get content type from packet.
static ssize_t coap_get_proxy_uri(const coap_pkt_t *pkt, char **target)
Convenience function for getting the packet's Proxy-Uri option.
Definition: nanocoap.h:629
ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
Encode the given uint option into pkt.
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition: nanocoap.h:1790
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:897
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition: nanocoap.h:1630
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_get_uri_path(const coap_pkt_t *pkt, uint8_t *target)
Convenience function for getting the packet's URI_PATH.
Definition: nanocoap.h:540
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:955
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition: nanocoap.h:347
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition: nanocoap.h:1800
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.
int coap_opt_get_uint(const coap_pkt_t *pkt, uint16_t optnum, uint32_t *value)
Get a uint32 option value.
Definitions for internet operations.
Generic CoAP values as defined by RFC7252.
Block1 helper struct.
Definition: nanocoap.h:233
uint32_t blknum
block number
Definition: nanocoap.h:235
size_t offset
offset of received data
Definition: nanocoap.h:234
int more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition: nanocoap.h:237
unsigned szx
szx value
Definition: nanocoap.h:236
Blockwise transfer helper struct.
Definition: nanocoap.h:244
uint8_t * opt
Pointer to the placed option
Definition: nanocoap.h:248
size_t end
End offset of the current block
Definition: nanocoap.h:246
size_t start
Start offset of the current block
Definition: nanocoap.h:245
size_t cur
Offset of the generated content
Definition: nanocoap.h:247
Raw CoAP PDU header structure.
Definition: nanocoap.h:168
uint8_t ver_t_tkl
version, token, token length
Definition: nanocoap.h:169
uint8_t code
CoAP code (e.g.m 205)
Definition: nanocoap.h:170
uint16_t id
Req/resp ID
Definition: nanocoap.h:171
CoAP option array entry.
Definition: nanocoap.h:177
uint16_t offset
offset in packet
Definition: nanocoap.h:179
uint16_t opt_num
full CoAP option number
Definition: nanocoap.h:178
CoAP PDU parsing context structure.
Definition: nanocoap.h:185
uint8_t * payload
pointer to payload
Definition: nanocoap.h:188
uint8_t * token
pointer to token
Definition: nanocoap.h:187
uint16_t payload_len
length of payload
Definition: nanocoap.h:189
coap_hdr_t * hdr
pointer to raw packet
Definition: nanocoap.h:186
uint16_t options_len
length of options array
Definition: nanocoap.h:190
Type for CoAP resource entry.
Definition: nanocoap.h:223
const char * path
URI path of resource
Definition: nanocoap.h:224
coap_method_flags_t methods
OR'ed methods this resource allows.
Definition: nanocoap.h:225
coap_handler_t handler
ptr to resource handler
Definition: nanocoap.h:226
void * context
ptr to user defined context data
Definition: nanocoap.h:227