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
151 
160 #define COAP_OPT_FINISH_NONE (0x0000)
161 
162 #define COAP_OPT_FINISH_PAYLOAD (0x0001)
163 
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 
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(const 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)
1125 {
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,
1239  coap_block1_t *block)
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,
1257  coap_block1_t *block)
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)
1549 {
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)
1599 {
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)
1710 {
1711  return pkt->observe_value != UINT32_MAX;
1712 }
1713 
1719 static inline void coap_clear_observe(coap_pkt_t *pkt)
1720 {
1721  pkt->observe_value = UINT32_MAX;
1722 }
1723 
1731 static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
1732 {
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 
coap_opt_put_uint
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.
coap_get_uri_query
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:562
coap_handler_t
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Resource handler type.
Definition: nanocoap.h:211
coap_opt_put_block1_control
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:1238
coap_pkt_init
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.
coap_opt_add_opaque
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.
coap_pkt_t::options_len
uint16_t options_len
length of options array
Definition: nanocoap.h:190
coap_payload_put_bytes
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
coap_optpos_t::offset
uint16_t offset
offset in packet
Definition: nanocoap.h:179
coap_build_hdr
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.
coap_block2_build_reply
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.
inet.h
Definitions for internet operations.
coap_block1_t::more
int more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition: nanocoap.h:237
coap_parse
int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU.
coap_optpos_t::opt_num
uint16_t opt_num
full CoAP option number
Definition: nanocoap.h:178
coap.h
Generic CoAP values as defined by RFC7252.
coap_get_id
static unsigned coap_get_id(coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition: nanocoap.h:336
coap_hdr_t::ver_t_tkl
uint8_t ver_t_tkl
version, token, token length
Definition: nanocoap.h:169
coap_get_code_detail
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:300
byteorder.h
Functions to work with different byte orders.
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
coap_get_location_path
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:499
coap_block1_finish
static void coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
Definition: nanocoap.h:681
coap_opt_add_uri_query
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:1044
coap_block1_t
Block1 helper struct.
Definition: nanocoap.h:233
coap_resource_t::context
void * context
ptr to user defined context data
Definition: nanocoap.h:227
coap_block_slicer_init
void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, size_t blksize)
Initialize a block slicer struct from content information.
coap_blockwise_put_char
size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c)
Add a single character to a block2 reply.
coap_method2flag
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition: nanocoap.h:1548
ntohs
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:488
coap_block_slicer_t::start
size_t start
Start offset of the current block
Definition: nanocoap.h:245
assert.h
POSIX.1-2008 compliant version of the assert macro.
coap_resources
const coap_resource_t coap_resources[]
Global CoAP resource list.
coap_get_uri_path
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:542
coap_opt_put_block
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.
coap_hdr_data_ptr
static uint8_t * coap_hdr_data_ptr(coap_hdr_t *hdr)
Get the start of data after the header.
Definition: nanocoap.h:399
coap_payload_advance_bytes
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
Definition: nanocoap.h:1598
coap_put_option_ct
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:1430
coap_get_content_type
unsigned coap_get_content_type(coap_pkt_t *pkt)
Get content type from packet.
coap_opt_add_proxy_uri
ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri)
Adds a single Proxy-URI option into pkt.
coap_opt_get_next
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.
coap_resource_t::path
const char * path
URI path of resource
Definition: nanocoap.h:224
coap_blockwise_put_bytes
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.
coap_payload_put_char
ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c)
Add a single character to the payload data of the CoAP request.
coap_opt_add_block
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.
coap_block_slicer_t::opt
uint8_t * opt
Pointer to the placed option
Definition: nanocoap.h:248
coap_get_blockopt
int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx)
Generic block option getter.
coap_block1_t::blknum
uint32_t blknum
block number
Definition: nanocoap.h:235
coap_build_reply
ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len)
Build reply to CoAP request.
coap_szx2size
static unsigned coap_szx2size(unsigned szx)
Helper to decode SZX value to size in bytes.
Definition: nanocoap.h:834
coap_has_observe
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
Definition: nanocoap.h:1709
coap_get_total_hdr_len
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:360
coap_get_block1
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition: nanocoap.h:794
coap_opt_put_proxy_uri
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:1357
coap_get_type
static unsigned coap_get_type(coap_pkt_t *pkt)
Get the message type.
Definition: nanocoap.h:375
coap_method_flags_t
uint16_t coap_method_flags_t
Method flag type.
Definition: nanocoap.h:218
coap_opt_put_location_query
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:1307
coap_resource_t::handler
coap_handler_t handler
ptr to resource handler
Definition: nanocoap.h:226
coap_opt_get_opaque
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.
coap_put_block1_ok
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)
coap_well_known_core_default_handler
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.
coap_opt_add_block1_control
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:949
coap_opt_add_block1
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:891
coap_opt_add_uint
ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
Encode the given uint option into pkt.
coap_hdr_t::code
uint8_t code
CoAP code (e.g.m 205)
Definition: nanocoap.h:170
coap_get_block
int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option)
Block option getter.
coap_hdr_set_type
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition: nanocoap.h:423
coap_opt_get_string
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.
coap_opt_get_uint
int coap_opt_get_uint(const coap_pkt_t *pkt, uint16_t optnum, uint32_t *value)
Get a uint32 option value.
coap_resource_t
Type for CoAP resource entry.
Definition: nanocoap.h:223
coap_put_option
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.
coap_opt_put_block2_control
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:1256
CONFIG_NANOCOAP_NOPTS_MAX
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
Definition: nanocoap.h:129
coap_opt_put_block1
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:1188
coap_pkt_t::hdr
coap_hdr_t * hdr
pointer to raw packet
Definition: nanocoap.h:186
coap_opt_put_uri_path
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:1325
coap_match_path
int coap_match_path(const coap_resource_t *resource, uint8_t *uri)
Checks if a CoAP resource path matches a given URI.
coap_block2_finish
static void coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition: nanocoap.h:697
coap_handle_req
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len)
Handle incoming CoAP request.
coap_get_ver
static unsigned coap_get_ver(coap_pkt_t *pkt)
Get the CoAP version number.
Definition: nanocoap.h:387
CONFIG_NANOCOAP_URI_MAX
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition: nanocoap.h:137
coap_get_code
static unsigned coap_get_code(coap_pkt_t *pkt)
Get a message's code in decimal format ((class * 100) + detail)
Definition: nanocoap.h:312
coap_tree_handler
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.
coap_resources_numof
const unsigned coap_resources_numof
Number of entries in global CoAP resource list.
coap_block_finish
void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
Finish a block request (block1 or block2)
coap_hdr_t
Raw CoAP PDU header structure.
Definition: nanocoap.h:168
coap_get_token_len
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
Definition: nanocoap.h:348
coap_pkt_t::payload_len
uint16_t payload_len
length of payload
Definition: nanocoap.h:189
coap_opt_add_block2_control
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:967
coap_opt_add_block2
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:915
coap_clear_observe
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
Definition: nanocoap.h:1719
coap_opt_put_uri_query
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:1341
coap_get_observe
static uint32_t coap_get_observe(coap_pkt_t *pkt)
Get the value of the observe option from the given packet.
Definition: nanocoap.h:1731
coap_opt_add_uri_path
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:1124
coap_hdr_t::id
uint16_t id
Req/resp ID
Definition: nanocoap.h:171
coap_reply_simple
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)
coap_get_code_raw
static unsigned coap_get_code_raw(coap_pkt_t *pkt)
Get a message's raw code (class + detail)
Definition: nanocoap.h:324
coap_opt_add_chars
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.
coap_get_location_query
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:521
coap_opt_put_string
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.
coap_hdr_set_code
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:410
coap_resource_t::methods
coap_method_flags_t methods
OR'ed methods this resource allows.
Definition: nanocoap.h:225
coap_block2_init
void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
coap_pkt_t
CoAP PDU parsing context structure.
Definition: nanocoap.h:185
coap_opt_add_string
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:1104
coap_get_block2
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
Definition: nanocoap.h:808
coap_opt_put_location_path
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:1289
coap_pkt_t::token
uint8_t * token
pointer to token
Definition: nanocoap.h:187
coap_code
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition: nanocoap.h:276
coap_optpos_t
CoAP option array entry.
Definition: nanocoap.h:177
coap_put_option_block1
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:1413
coap_opt_add_format
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:986
coap_opt_finish
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
coap_block_object_init
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more)
Initialize a block struct from content information.
coap_block1_t::szx
unsigned szx
szx value
Definition: nanocoap.h:236
coap_block_slicer_t
Blockwise transfer helper struct.
Definition: nanocoap.h:244
coap_block1_t::offset
size_t offset
offset of received data
Definition: nanocoap.h:234
coap_block_slicer_t::end
size_t end
End offset of the current block
Definition: nanocoap.h:246
errno.h
coap_opt_put_block2
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:1209
coap_opt_add_uri_query2
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.
coap_get_proxy_uri
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:631
coap_get_code_class
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:288
coap_block_slicer_t::cur
size_t cur
Offset of the generated content
Definition: nanocoap.h:247
coap_pkt_t::payload
uint8_t * payload
pointer to payload
Definition: nanocoap.h:188