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)
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
151 
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;
173 
177 typedef struct {
178  uint16_t opt_num;
179  uint16_t offset;
181 
185 typedef struct {
186  coap_hdr_t *hdr;
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);
218 typedef uint16_t coap_method_flags_t;
223 typedef struct {
224  const char *path;
225  coap_method_flags_t methods;
226  coap_handler_t handler;
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 
261 
276 static inline uint8_t coap_code(unsigned cls, unsigned detail)
277 {
278  return (cls << 5) | detail;
279 }
280 
288 static inline unsigned coap_get_code_class(coap_pkt_t *pkt)
289 {
290  return pkt->hdr->code >> 5;
291 }
292 
300 static inline unsigned coap_get_code_detail(coap_pkt_t *pkt)
301 {
302  return pkt->hdr->code & 0x1f;
303 }
304 
312 static inline unsigned coap_get_code(coap_pkt_t *pkt)
313 {
314  return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
315 }
316 
324 static inline unsigned coap_get_code_raw(coap_pkt_t *pkt)
325 {
326  return (unsigned)pkt->hdr->code;
327 }
328 
336 static inline unsigned coap_get_id(coap_pkt_t *pkt)
337 {
338  return ntohs(pkt->hdr->id);
339 }
340 
348 static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
349 {
350  return (pkt->hdr->ver_t_tkl & 0xf);
351 }
352 
360 static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
361 {
362  return sizeof(coap_hdr_t) + coap_get_token_len(pkt);
363 }
364 
375 static inline unsigned coap_get_type(coap_pkt_t *pkt)
376 {
377  return (pkt->hdr->ver_t_tkl & 0x30) >> 4;
378 }
379 
387 static inline unsigned coap_get_ver(coap_pkt_t *pkt)
388 {
389  return (pkt->hdr->ver_t_tkl & 0x60) >> 6;
390 }
391 
399 static inline uint8_t *coap_hdr_data_ptr(coap_hdr_t *hdr)
400 {
401  return ((uint8_t *)hdr) + sizeof(coap_hdr_t);
402 }
403 
410 static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
411 {
412  hdr->code = code;
413 }
414 
423 static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
424 {
425  /* assert correct range of type */
426  assert(!(type & ~0x3));
427 
428  hdr->ver_t_tkl &= ~0x30;
429  hdr->ver_t_tkl |= type << 4;
430 }
448 unsigned coap_get_content_type(coap_pkt_t *pkt);
449 
462 int coap_opt_get_uint(const coap_pkt_t *pkt, uint16_t optnum, uint32_t *value);
463 
481 ssize_t coap_opt_get_string(const coap_pkt_t *pkt, uint16_t optnum,
482  uint8_t *target, size_t max_len, char separator);
483 
499 static inline ssize_t coap_get_location_path(const coap_pkt_t *pkt,
500  uint8_t *target, size_t max_len)
501 {
502  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
503  target, max_len, '/');
504 }
505 
521 static inline ssize_t coap_get_location_query(const coap_pkt_t *pkt,
522  uint8_t *target, size_t max_len)
523 {
524  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
525  target, max_len, '&');
526 }
527 
542 static inline ssize_t coap_get_uri_path(const coap_pkt_t *pkt, uint8_t *target)
543 {
544  return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
546 }
547 
562 static inline ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target)
563 {
564  return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY, target,
566 }
567 
597 ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt,
598  uint8_t **value, bool init_opt);
599 
616 ssize_t coap_opt_get_opaque(const coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
631 static inline ssize_t coap_get_proxy_uri(const coap_pkt_t *pkt, char **target)
632 {
633  return coap_opt_get_opaque(pkt, COAP_OPT_PROXY_URI, (uint8_t **)target);
634 }
635 
653 void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
654  int more);
655 
668 void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
669 
681 static inline void coap_block1_finish(coap_block_slicer_t *slicer)
682 {
683  coap_block_finish(slicer, COAP_OPT_BLOCK1);
684 }
685 
697 static inline void coap_block2_finish(coap_block_slicer_t *slicer)
698 {
699  coap_block_finish(slicer, COAP_OPT_BLOCK2);
700 }
701 
712 
722 void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum,
723  size_t blksize);
724 
739 size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
740  const uint8_t *c, size_t len);
741 
755 size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
756 
775 int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
776 
794 static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
795 {
796  return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
797 }
798 
808 static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
809 {
810  return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
811 }
812 
825 int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx);
826 
834 static inline unsigned coap_szx2size(unsigned szx)
835 {
836  return (1 << (szx + 4));
837 }
871  bool more, uint16_t option);
872 
891 static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
892  coap_block_slicer_t *slicer, bool more)
893 {
894  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
895 }
896 
915 static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
916  coap_block_slicer_t *slicer, bool more)
917 {
918  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
919 }
934 ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
935 
949 static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
950  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
951  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
952 }
953 
967 static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
968  /* block.more must be zero, so no need to 'or' it in */
969  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
970  (block->blknum << 4) | block->szx);
971 }
972 
986 static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
987 {
988  return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
989 }
990 
1006 ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len);
1007 
1025 ssize_t coap_opt_add_uri_query2(coap_pkt_t *pkt, const char *key, size_t key_len,
1026  const char *val, size_t val_len);
1027 
1044 static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key,
1045  const char *val)
1046 {
1047  return coap_opt_add_uri_query2(pkt, key, strlen(key), val, val ? strlen(val) : 0);
1048 }
1049 
1064 ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri);
1065 
1084 ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars,
1085  size_t chars_len, char separator);
1086 
1104 static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum,
1105  const char *string, char separator)
1106 {
1107  return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator);
1108 }
1109 
1124 static inline ssize_t coap_opt_add_uri_path(coap_pkt_t *pkt, const char *path)
1126  return coap_opt_add_string(pkt, COAP_OPT_URI_PATH, path, '/');
1127 }
1128 
1141 ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1170 size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1171  bool more, uint16_t option);
1172 
1188 static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1189  coap_block_slicer_t *slicer, bool more)
1190 {
1191  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1192 }
1193 
1209 static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1210  coap_block_slicer_t *slicer, bool more)
1211 {
1212  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1213 }
1214 
1226 size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1227  uint32_t value);
1228 
1238 static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1240 {
1241  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1242  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1243 }
1244 
1256 static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1258 {
1259  /* block.more must be zero, so no need to 'or' it in */
1260  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1261  (block->blknum << 4) | block->szx);
1262 }
1263 
1276 size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1277  const char *string, char separator);
1278 
1289 static inline size_t coap_opt_put_location_path(uint8_t *buf,
1290  uint16_t lastonum,
1291  const char *location)
1292 {
1293  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1294  location, '/');
1295 }
1296 
1307 static inline size_t coap_opt_put_location_query(uint8_t *buf,
1308  uint16_t lastonum,
1309  const char *location)
1310 {
1311  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
1312  location, '&');
1313 }
1314 
1325 static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
1326  const char *uri)
1327 {
1328  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
1329 }
1330 
1341 static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
1342  const char *uri)
1343 {
1344  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
1345 }
1346 
1357 static inline size_t coap_opt_put_proxy_uri(uint8_t *buf, uint16_t lastonum,
1358  const char *uri)
1359 {
1360  return coap_opt_put_string(buf, lastonum, COAP_OPT_PROXY_URI, uri, '\0');
1361 }
1362 
1363 
1381 size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
1382 
1399 size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uint8_t *odata, size_t olen);
1400 
1413 static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
1414  unsigned blknum, unsigned szx, int more)
1415 {
1416  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1417  (blknum << 4) | szx | (more ? 0x8 : 0));
1418 }
1419 
1430 static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
1431  uint16_t content_type)
1432 {
1433  return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
1434 }
1461 ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
1462  uint8_t *rbuf, unsigned rlen, unsigned payload_len,
1463  coap_block_slicer_t *slicer);
1464 
1479 ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, uint8_t *token,
1480  size_t token_len, unsigned code, uint16_t id);
1481 
1503 ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
1504  uint8_t *rbuf, unsigned rlen, unsigned payload_len);
1505 
1519 ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len);
1520 
1536 ssize_t coap_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf,
1537  unsigned resp_buf_len,
1538  const coap_resource_t *resources,
1539  size_t resources_numof);
1540 
1548 static inline coap_method_flags_t coap_method2flag(unsigned code)
1550  return (1 << (code - 1));
1551 }
1552 
1567 int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
1568 
1583 void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
1584 
1598 static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
1600  pkt->payload += len;
1601  pkt->payload_len -= len;
1602 }
1603 
1625 ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
1626 
1640 ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
1641 
1663 ssize_t coap_reply_simple(coap_pkt_t *pkt,
1664  unsigned code,
1665  uint8_t *buf, size_t len,
1666  unsigned ct,
1667  const uint8_t *payload, uint8_t payload_len);
1668 
1673 extern ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, \
1674  uint8_t *buf, size_t len,
1675  void *context);
1694 int coap_match_path(const coap_resource_t *resource, uint8_t *uri);
1695 
1696 #if defined(MODULE_GCOAP) || defined(DOXYGEN)
1697 
1709 static inline bool coap_has_observe(coap_pkt_t *pkt)
1711  return pkt->observe_value != UINT32_MAX;
1712 }
1713 
1719 static inline void coap_clear_observe(coap_pkt_t *pkt)
1721  pkt->observe_value = UINT32_MAX;
1722 }
1723 
1731 static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
1733  return pkt->observe_value;
1734 }
1736 #endif
1737 
1741 #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
1742  { \
1743  .path = "/.well-known/core", \
1744  .methods = COAP_GET, \
1745  .handler = coap_well_known_core_default_handler \
1746  }
1747 
1748 #ifdef __cplusplus
1749 }
1750 #endif
1751 #endif /* NET_NANOCOAP_H */
1752 
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition: nanocoap.h:424
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:892
static unsigned coap_get_code_raw(coap_pkt_t *pkt)
Get a message&#39;s raw code (class + detail)
Definition: nanocoap.h:325
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.
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:1414
static void coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition: nanocoap.h:698
static unsigned coap_get_code_detail(coap_pkt_t *pkt)
Get a message&#39;s code detail (5 least significant bits of code)
Definition: nanocoap.h:301
int coap_match_path(const coap_resource_t *resource, uint8_t *uri)
Checks if a CoAP resource path matches a given URI.
int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx)
Generic 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&#39;s options.
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition: nanocoap.h:809
const unsigned coap_resources_numof
Number of entries in global CoAP resource list.
uint32_t blknum
block number
Definition: nanocoap.h:236
static ssize_t coap_get_proxy_uri(const coap_pkt_t *pkt, char **target)
Convenience function for getting the packet&#39;s Proxy-Uri option.
Definition: nanocoap.h:632
static unsigned coap_get_code_class(coap_pkt_t *pkt)
Get a message&#39;s code class (3 most significant bits of code)
Definition: nanocoap.h:289
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 &#39;/path&#39; into pkt.
Definition: nanocoap.h:1125
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.
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:1326
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:1308
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:950
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len)
Handle incoming CoAP request.
Raw CoAP PDU header structure.
Definition: nanocoap.h:169
Generic CoAP values as defined by RFC7252.
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.
static unsigned coap_szx2size(unsigned szx)
Helper to decode SZX value to size in bytes.
Definition: nanocoap.h:835
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:987
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message&#39;s token length [in byte].
Definition: nanocoap.h:349
uint16_t payload_len
length of payload
Definition: nanocoap.h:190
CoAP option array entry.
Definition: nanocoap.h:178
static ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target)
Convenience function for getting the packet&#39;s URI_QUERY option.
Definition: nanocoap.h:563
Blockwise transfer helper struct.
Definition: nanocoap.h:245
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:1105
uint8_t code
CoAP code (e.g.m 205)
Definition: nanocoap.h:171
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:1210
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:1257
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&#39;s LOCATION_PATH option.
Definition: nanocoap.h:500
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 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:1290
Type for CoAP resource entry.
Definition: nanocoap.h:224
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition: nanocoap.h:1549
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.
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition: nanocoap.h:138
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:1239
POSIX.1-2008 compliant version of the assert macro.
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
coap_hdr_t * hdr
pointer to raw packet
Definition: nanocoap.h:187
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition: nanocoap.h:1720
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 &#39;key=value&#39; into pkt.
unsigned coap_get_content_type(coap_pkt_t *pkt)
Get content type from packet.
ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri)
Adds a single Proxy-URI option into pkt.
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:411
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
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 unsigned coap_get_ver(coap_pkt_t *pkt)
Get the CoAP version number.
Definition: nanocoap.h:388
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
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_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:916
void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
Finish a block request (block1 or block2)
uint8_t ver_t_tkl
version, token, token length
Definition: nanocoap.h:170
Block1 helper struct.
Definition: nanocoap.h:234
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition: nanocoap.h:277
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.
int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU.
void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
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:361
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition: nanocoap.h:795
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 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:968
static unsigned coap_get_id(coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition: nanocoap.h:337
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)
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
Definition: nanocoap.h:130
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 uint32_t coap_get_observe(coap_pkt_t *pkt)
Get the value of the observe option from the given packet.
Definition: nanocoap.h:1732
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:1342
Functions to work with different byte orders.
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 uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:488
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.
Definitions for internet operations.
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.
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)
int coap_opt_get_uint(const coap_pkt_t *pkt, uint16_t optnum, uint32_t *value)
Get a uint32 option value.
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.
uint8_t * payload
pointer to payload
Definition: nanocoap.h:189
size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum, uint32_t value)
Encode the given uint option into buffer.
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more)
Initialize a block struct from content information.
static ssize_t coap_get_uri_path(const coap_pkt_t *pkt, uint8_t *target)
Convenience function for getting the packet&#39;s URI_PATH.
Definition: nanocoap.h:543
ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
Encode the given uint option into pkt.
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition: nanocoap.h:1599
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:1189
static uint8_t * coap_hdr_data_ptr(coap_hdr_t *hdr)
Get the start of data after the header.
Definition: nanocoap.h:400
unsigned szx
szx value
Definition: nanocoap.h:237
uint16_t id
Req/resp ID.
Definition: nanocoap.h:172
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition: nanocoap.h:1710
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_proxy_uri(uint8_t *buf, uint16_t lastonum, const char *uri)
Convenience function for inserting PROXY_URI option into buffer.
Definition: nanocoap.h:1358
uint16_t coap_method_flags_t
Method flag type.
Definition: nanocoap.h:219
static unsigned coap_get_type(coap_pkt_t *pkt)
Get the message type.
Definition: nanocoap.h:376
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Resource handler type.
Definition: nanocoap.h:212
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.
int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option)
Block option getter.
static void coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition: nanocoap.h:682
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.
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:1431
int more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition: nanocoap.h:238
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 &#39;key=value&#39; into pkt.
Definition: nanocoap.h:1045
static unsigned coap_get_code(coap_pkt_t *pkt)
Get a message&#39;s code in decimal format ((class * 100) + detail)
Definition: nanocoap.h:313
CoAP PDU parsing context structure.
Definition: nanocoap.h:186
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 ssize_t coap_get_location_query(const coap_pkt_t *pkt, uint8_t *target, size_t max_len)
Convenience function for getting the packet&#39;s LOCATION_QUERY option.
Definition: nanocoap.h:522
const coap_resource_t coap_resources[]
Global CoAP resource list.