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 <stdint.h>
82 #include <stdbool.h>
83 #include <stddef.h>
84 #include <unistd.h>
85 
86 #ifdef RIOT_VERSION
87 #include "byteorder.h"
88 #include "net/coap.h"
89 #else
90 #include "coap.h"
91 #include <arpa/inet.h>
92 #endif
93 
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97 
103 #define COAP_GET (0x01)
104 #define COAP_POST (0x02)
105 #define COAP_PUT (0x04)
106 #define COAP_DELETE (0x08)
107 #define COAP_FETCH (0x10)
108 #define COAP_PATCH (0x20)
109 #define COAP_IPATCH (0x40)
110 #define COAP_MATCH_SUBTREE (0x8000)
117 #define COAP_FORMAT_NONE (UINT16_MAX)
126 #ifndef NANOCOAP_NOPTS_MAX
127 #define NANOCOAP_NOPTS_MAX (16)
128 #endif
129 
134 #ifndef NANOCOAP_URI_MAX
135 #define NANOCOAP_URI_MAX (64)
136 #endif
137 
141 #ifndef NANOCOAP_BLOCK_SIZE_EXP_MAX
142 #define NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
143 #endif
144 
145 #if defined(MODULE_GCOAP) || defined(DOXYGEN)
146 
147 #ifndef NANOCOAP_QS_MAX
148 #define NANOCOAP_QS_MAX (64)
149 #endif
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 
200 typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context);
207 typedef uint16_t coap_method_flags_t;
212 typedef struct {
213  const char *path;
214  coap_method_flags_t methods;
215  coap_handler_t handler;
216  void *context;
218 
222 typedef struct {
223  size_t offset;
224  uint32_t blknum;
225  unsigned szx;
226  int more;
228 } coap_block1_t;
229 
233 typedef struct {
234  size_t start;
235  size_t end;
236  size_t cur;
237  uint8_t *opt;
239 
243 extern const coap_resource_t coap_resources[];
244 
248 extern const unsigned coap_resources_numof;
249 
250 
265 static inline uint8_t coap_code(unsigned cls, unsigned detail)
266 {
267  return (cls << 5) | detail;
268 }
269 
277 static inline unsigned coap_get_code_class(coap_pkt_t *pkt)
278 {
279  return pkt->hdr->code >> 5;
280 }
281 
289 static inline unsigned coap_get_code_detail(coap_pkt_t *pkt)
290 {
291  return pkt->hdr->code & 0x1f;
292 }
293 
301 static inline unsigned coap_get_code(coap_pkt_t *pkt)
302 {
303  return (coap_get_code_class(pkt) * 100) + coap_get_code_detail(pkt);
304 }
305 
313 static inline unsigned coap_get_code_raw(coap_pkt_t *pkt)
314 {
315  return (unsigned)pkt->hdr->code;
316 }
317 
325 static inline unsigned coap_get_id(coap_pkt_t *pkt)
326 {
327  return ntohs(pkt->hdr->id);
328 }
329 
337 static inline unsigned coap_get_token_len(const coap_pkt_t *pkt)
338 {
339  return (pkt->hdr->ver_t_tkl & 0xf);
340 }
341 
349 static inline unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
350 {
351  return sizeof(coap_hdr_t) + coap_get_token_len(pkt);
352 }
353 
364 static inline unsigned coap_get_type(coap_pkt_t *pkt)
365 {
366  return (pkt->hdr->ver_t_tkl & 0x30) >> 4;
367 }
368 
376 static inline unsigned coap_get_ver(coap_pkt_t *pkt)
377 {
378  return (pkt->hdr->ver_t_tkl & 0x60) >> 6;
379 }
380 
388 static inline uint8_t *coap_hdr_data_ptr(coap_hdr_t *hdr)
389 {
390  return ((uint8_t *)hdr) + sizeof(coap_hdr_t);
391 }
392 
399 static inline void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
400 {
401  hdr->code = code;
402 }
403 
412 static inline void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
413 {
414  /* assert correct range of type */
415  assert(!(type & ~0x3));
416 
417  hdr->ver_t_tkl &= ~0x30;
418  hdr->ver_t_tkl |= type << 4;
419 }
437 unsigned coap_get_content_type(coap_pkt_t *pkt);
438 
456 ssize_t coap_opt_get_string(const coap_pkt_t *pkt, uint16_t optnum,
457  uint8_t *target, size_t max_len, char separator);
458 
474 static inline ssize_t coap_get_location_path(const coap_pkt_t *pkt,
475  uint8_t *target, size_t max_len)
476 {
477  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_PATH,
478  target, max_len, '/');
479 }
480 
496 static inline ssize_t coap_get_location_query(const coap_pkt_t *pkt,
497  uint8_t *target, size_t max_len)
498 {
499  return coap_opt_get_string(pkt, COAP_OPT_LOCATION_QUERY,
500  target, max_len, '&');
501 }
502 
517 static inline ssize_t coap_get_uri_path(const coap_pkt_t *pkt, uint8_t *target)
518 {
519  return coap_opt_get_string(pkt, COAP_OPT_URI_PATH, target,
520  NANOCOAP_URI_MAX, '/');
521 }
522 
537 static inline ssize_t coap_get_uri_query(const coap_pkt_t *pkt, uint8_t *target)
538 {
539  return coap_opt_get_string(pkt, COAP_OPT_URI_QUERY, target,
540  NANOCOAP_URI_MAX, '&');
541 }
542 
572 ssize_t coap_opt_get_next(const coap_pkt_t *pkt, coap_optpos_t *opt,
573  uint8_t **value, bool init_opt);
574 
590 ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
611 void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
612  int more);
613 
626 void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
627 
639 static inline void coap_block1_finish(coap_block_slicer_t *slicer)
640 {
641  coap_block_finish(slicer, COAP_OPT_BLOCK1);
642 }
643 
655 static inline void coap_block2_finish(coap_block_slicer_t *slicer)
656 {
657  coap_block_finish(slicer, COAP_OPT_BLOCK2);
658 }
659 
670 
680 void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum,
681  size_t blksize);
682 
697 size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
698  const uint8_t *c, size_t len);
699 
713 size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
714 
733 int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
734 
752 static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
753 {
754  return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
755 }
756 
766 static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
767 {
768  return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
769 }
770 
783 int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx);
784 
792 static inline unsigned coap_szx2size(unsigned szx)
793 {
794  return (1 << (szx + 4));
795 }
829  bool more, uint16_t option);
830 
849 static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
850  coap_block_slicer_t *slicer, bool more)
851 {
852  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
853 }
854 
873 static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
874  coap_block_slicer_t *slicer, bool more)
875 {
876  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
877 }
892 ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
893 
907 static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
908  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
909  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
910 }
911 
925 static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
926  /* block.more must be zero, so no need to 'or' it in */
927  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
928  (block->blknum << 4) | block->szx);
929 }
930 
944 static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
945 {
946  return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
947 }
948 
964 ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len);
965 
983 ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string, char separator);
984 
997 ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1026 size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1027  bool more, uint16_t option);
1028 
1044 static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1045  coap_block_slicer_t *slicer, bool more)
1046 {
1047  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1048 }
1049 
1065 static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1066  coap_block_slicer_t *slicer, bool more)
1067 {
1068  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1069 }
1070 
1082 size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1083  uint32_t value);
1084 
1094 static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1096 {
1097  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1098  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1099 }
1100 
1112 static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1114 {
1115  /* block.more must be zero, so no need to 'or' it in */
1116  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1117  (block->blknum << 4) | block->szx);
1118 }
1119 
1132 size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1133  const char *string, char separator);
1134 
1145 static inline size_t coap_opt_put_location_path(uint8_t *buf,
1146  uint16_t lastonum,
1147  const char *location)
1148 {
1149  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1150  location, '/');
1151 }
1152 
1163 static inline size_t coap_opt_put_location_query(uint8_t *buf,
1164  uint16_t lastonum,
1165  const char *location)
1166 {
1167  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
1168  location, '&');
1169 }
1170 
1181 static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
1182  const char *uri)
1183 {
1184  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
1185 }
1186 
1197 static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
1198  const char *uri)
1199 {
1200  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
1201 }
1202 
1220 size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
1221 
1238 size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uint8_t *odata, size_t olen);
1239 
1252 static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
1253  unsigned blknum, unsigned szx, int more)
1254 {
1255  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1256  (blknum << 4) | szx | (more ? 0x8 : 0));
1257 }
1258 
1269 static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
1270  uint16_t content_type)
1271 {
1272  return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
1273 }
1300 ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
1301  uint8_t *rbuf, unsigned rlen, unsigned payload_len,
1302  coap_block_slicer_t *slicer);
1303 
1318 ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, uint8_t *token,
1319  size_t token_len, unsigned code, uint16_t id);
1320 
1342 ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
1343  uint8_t *rbuf, unsigned rlen, unsigned payload_len);
1344 
1358 ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len);
1359 
1367 static inline coap_method_flags_t coap_method2flag(unsigned code)
1369  return (1 << (code - 1));
1370 }
1371 
1386 int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
1387 
1402 void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
1403 
1425 ssize_t coap_reply_simple(coap_pkt_t *pkt,
1426  unsigned code,
1427  uint8_t *buf, size_t len,
1428  unsigned ct,
1429  const uint8_t *payload, uint8_t payload_len);
1430 
1435 extern ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, \
1436  uint8_t *buf, size_t len,
1437  void *context);
1456 int coap_match_path(const coap_resource_t *resource, uint8_t *uri);
1457 
1458 #if defined(MODULE_GCOAP) || defined(DOXYGEN)
1459 
1471 static inline bool coap_has_observe(coap_pkt_t *pkt)
1473  return pkt->observe_value != UINT32_MAX;
1474 }
1475 
1481 static inline void coap_clear_observe(coap_pkt_t *pkt)
1483  pkt->observe_value = UINT32_MAX;
1484 }
1485 
1493 static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
1495  return pkt->observe_value;
1496 }
1498 #endif
1499 
1503 #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
1504  { \
1505  .path = "/.well-known/core", \
1506  .methods = COAP_GET, \
1507  .handler = coap_well_known_core_default_handler \
1508  }
1509 
1510 #ifdef __cplusplus
1511 }
1512 #endif
1513 #endif /* NET_NANOCOAP_H */
1514 
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
Definition: nanocoap.h:413
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:850
static unsigned coap_get_code_raw(coap_pkt_t *pkt)
Get a message&#39;s raw code (class + detail)
Definition: nanocoap.h:314
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:1253
static void coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition: nanocoap.h:656
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:290
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:767
const unsigned coap_resources_numof
Number of entries in global CoAP resource list.
uint32_t blknum
block number
Definition: nanocoap.h:225
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:278
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:1182
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:1164
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:908
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.
static unsigned coap_szx2size(unsigned szx)
Helper to decode SZX value to size in bytes.
Definition: nanocoap.h:793
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:945
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.
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message&#39;s token length [in byte].
Definition: nanocoap.h:338
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:538
Blockwise transfer helper struct.
Definition: nanocoap.h:234
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:1066
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:1113
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:475
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:1146
Type for CoAP resource entry.
Definition: nanocoap.h:213
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
Definition: nanocoap.h:1368
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 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:1095
POSIX.1-2008 compliant version of the assert macro.
signed int ssize_t
Used for a count of bytes or an error indication.
Definition: msp430_types.h:89
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:1482
unsigned coap_get_content_type(coap_pkt_t *pkt)
Get content type from packet.
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:400
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:377
#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:874
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:223
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
Definition: nanocoap.h:266
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:350
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
Definition: nanocoap.h:753
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:926
static unsigned coap_get_id(coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
Definition: nanocoap.h:326
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_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:1494
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:1198
#define NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
Definition: nanocoap.h:128
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:428
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)
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.
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:518
ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
Encode the given uint option into pkt.
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:1045
static uint8_t * coap_hdr_data_ptr(coap_hdr_t *hdr)
Get the start of data after the header.
Definition: nanocoap.h:389
unsigned szx
szx value
Definition: nanocoap.h:226
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:1472
size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c)
Add a single character to a block2 reply.
ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value)
Retrieve the value for an option as an opaque array of bytes.
uint16_t coap_method_flags_t
Method flag type.
Definition: nanocoap.h:208
static unsigned coap_get_type(coap_pkt_t *pkt)
Get the message type.
Definition: nanocoap.h:365
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Resource handler type.
Definition: nanocoap.h:201
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:640
#define NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
Definition: nanocoap.h:136
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:1270
int more
-1 for no option, 0 for last block, 1 for more blocks coming
Definition: nanocoap.h:227
static unsigned coap_get_code(coap_pkt_t *pkt)
Get a message&#39;s code in decimal format ((class * 100) + detail)
Definition: nanocoap.h:302
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:497
const coap_resource_t coap_resources[]
Global CoAP resource list.