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 
591 ssize_t coap_opt_get_opaque(coap_pkt_t *pkt, unsigned opt_num, uint8_t **value);
612 void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
613  int more);
614 
627 void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option);
628 
640 static inline void coap_block1_finish(coap_block_slicer_t *slicer)
641 {
642  coap_block_finish(slicer, COAP_OPT_BLOCK1);
643 }
644 
656 static inline void coap_block2_finish(coap_block_slicer_t *slicer)
657 {
658  coap_block_finish(slicer, COAP_OPT_BLOCK2);
659 }
660 
671 
681 void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum,
682  size_t blksize);
683 
698 size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos,
699  const uint8_t *c, size_t len);
700 
714 size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c);
715 
734 int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option);
735 
753 static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
754 {
755  return coap_get_block(pkt, block, COAP_OPT_BLOCK1);
756 }
757 
767 static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
768 {
769  return coap_get_block(pkt, block, COAP_OPT_BLOCK2);
770 }
771 
784 int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx);
785 
793 static inline unsigned coap_szx2size(unsigned szx)
794 {
795  return (1 << (szx + 4));
796 }
830  bool more, uint16_t option);
831 
850 static inline ssize_t coap_opt_add_block1(coap_pkt_t *pkt,
851  coap_block_slicer_t *slicer, bool more)
852 {
853  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK1);
854 }
855 
874 static inline ssize_t coap_opt_add_block2(coap_pkt_t *pkt,
875  coap_block_slicer_t *slicer, bool more)
876 {
877  return coap_opt_add_block(pkt, slicer, more, COAP_OPT_BLOCK2);
878 }
893 ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value);
894 
908 static inline ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block) {
909  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK1,
910  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
911 }
912 
926 static inline ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block) {
927  /* block.more must be zero, so no need to 'or' it in */
928  return coap_opt_add_uint(pkt, COAP_OPT_BLOCK2,
929  (block->blknum << 4) | block->szx);
930 }
931 
945 static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
946 {
947  return coap_opt_add_uint(pkt, COAP_OPT_CONTENT_FORMAT, format);
948 }
949 
965 ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len);
966 
984 ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string, char separator);
985 
998 ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags);
1027 size_t coap_opt_put_block(uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer,
1028  bool more, uint16_t option);
1029 
1045 static inline size_t coap_opt_put_block1(uint8_t *buf, uint16_t lastonum,
1046  coap_block_slicer_t *slicer, bool more)
1047 {
1048  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK1);
1049 }
1050 
1066 static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum,
1067  coap_block_slicer_t *slicer, bool more)
1068 {
1069  return coap_opt_put_block(buf, lastonum, slicer, more, COAP_OPT_BLOCK2);
1070 }
1071 
1083 size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum,
1084  uint32_t value);
1085 
1095 static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum,
1097 {
1098  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1099  (block->blknum << 4) | block->szx | (block->more ? 0x8 : 0));
1100 }
1101 
1113 static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum,
1115 {
1116  /* block.more must be zero, so no need to 'or' it in */
1117  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2,
1118  (block->blknum << 4) | block->szx);
1119 }
1120 
1133 size_t coap_opt_put_string(uint8_t *buf, uint16_t lastonum, uint16_t optnum,
1134  const char *string, char separator);
1135 
1146 static inline size_t coap_opt_put_location_path(uint8_t *buf,
1147  uint16_t lastonum,
1148  const char *location)
1149 {
1150  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_PATH,
1151  location, '/');
1152 }
1153 
1164 static inline size_t coap_opt_put_location_query(uint8_t *buf,
1165  uint16_t lastonum,
1166  const char *location)
1167 {
1168  return coap_opt_put_string(buf, lastonum, COAP_OPT_LOCATION_QUERY,
1169  location, '&');
1170 }
1171 
1182 static inline size_t coap_opt_put_uri_path(uint8_t *buf, uint16_t lastonum,
1183  const char *uri)
1184 {
1185  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_PATH, uri, '/');
1186 }
1187 
1198 static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum,
1199  const char *uri)
1200 {
1201  return coap_opt_put_string(buf, lastonum, COAP_OPT_URI_QUERY, uri, '&');
1202 }
1203 
1221 size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum);
1222 
1239 size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uint8_t *odata, size_t olen);
1240 
1253 static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum,
1254  unsigned blknum, unsigned szx, int more)
1255 {
1256  return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1,
1257  (blknum << 4) | szx | (more ? 0x8 : 0));
1258 }
1259 
1270 static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum,
1271  uint16_t content_type)
1272 {
1273  return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type);
1274 }
1301 ssize_t coap_block2_build_reply(coap_pkt_t *pkt, unsigned code,
1302  uint8_t *rbuf, unsigned rlen, unsigned payload_len,
1303  coap_block_slicer_t *slicer);
1304 
1319 ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, uint8_t *token,
1320  size_t token_len, unsigned code, uint16_t id);
1321 
1343 ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code,
1344  uint8_t *rbuf, unsigned rlen, unsigned payload_len);
1345 
1359 ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len);
1360 
1368 static inline coap_method_flags_t coap_method2flag(unsigned code)
1370  return (1 << (code - 1));
1371 }
1372 
1387 int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
1388 
1403 void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
1404 
1426 ssize_t coap_reply_simple(coap_pkt_t *pkt,
1427  unsigned code,
1428  uint8_t *buf, size_t len,
1429  unsigned ct,
1430  const uint8_t *payload, uint8_t payload_len);
1431 
1436 extern ssize_t coap_well_known_core_default_handler(coap_pkt_t *pkt, \
1437  uint8_t *buf, size_t len,
1438  void *context);
1457 int coap_match_path(const coap_resource_t *resource, uint8_t *uri);
1458 
1459 #if defined(MODULE_GCOAP) || defined(DOXYGEN)
1460 
1472 static inline bool coap_has_observe(coap_pkt_t *pkt)
1474  return pkt->observe_value != UINT32_MAX;
1475 }
1476 
1482 static inline void coap_clear_observe(coap_pkt_t *pkt)
1484  pkt->observe_value = UINT32_MAX;
1485 }
1486 
1494 static inline uint32_t coap_get_observe(coap_pkt_t *pkt)
1496  return pkt->observe_value;
1497 }
1499 #endif
1500 
1504 #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
1505  { \
1506  .path = "/.well-known/core", \
1507  .methods = COAP_GET, \
1508  .handler = coap_well_known_core_default_handler \
1509  }
1510 
1511 #ifdef __cplusplus
1512 }
1513 #endif
1514 #endif /* NET_NANOCOAP_H */
1515 
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:851
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:1254
static void coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
Definition: nanocoap.h:657
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:768
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:1183
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:1165
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:909
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:794
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:946
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:1067
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:1114
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:1147
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:1369
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:1096
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:1483
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:875
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:754
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:927
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:1495
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:1199
#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:1046
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:1473
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:641
#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:1271
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.