77 #ifndef NET_NANOCOAP_H 78 #define NET_NANOCOAP_H 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) 134 #ifndef NANOCOAP_URI_MAX 135 #define NANOCOAP_URI_MAX (64) 141 #ifndef NANOCOAP_BLOCK_SIZE_EXP_MAX 142 #define NANOCOAP_BLOCK_SIZE_EXP_MAX (6) 145 #if defined(MODULE_GCOAP) || defined(DOXYGEN) 147 #ifndef NANOCOAP_QS_MAX 148 #define NANOCOAP_QS_MAX (64) 160 #define COAP_OPT_FINISH_NONE (0x0000) 162 #define COAP_OPT_FINISH_PAYLOAD (0x0001) 168 typedef struct __attribute__((packed)) {
189 uint16_t payload_len;
190 uint16_t options_len;
193 uint32_t observe_value;
214 coap_method_flags_t methods;
265 static inline uint8_t
coap_code(
unsigned cls,
unsigned detail)
267 return (cls << 5) | detail;
315 return (
unsigned)pkt->
hdr->
code;
457 uint8_t *target,
size_t max_len,
char separator);
475 uint8_t *target,
size_t max_len)
478 target, max_len,
'/');
497 uint8_t *target,
size_t max_len)
500 target, max_len,
'&');
573 uint8_t **value,
bool init_opt);
699 const uint8_t *c,
size_t len);
795 return (1 << (szx + 4));
830 bool more, uint16_t option);
910 (block->
blknum << 4) | block->
szx | (block->
more ? 0x8 : 0));
1028 bool more, uint16_t option);
1099 (block->
blknum << 4) | block->
szx | (block->
more ? 0x8 : 0));
1134 const char *
string,
char separator);
1148 const char *location)
1166 const char *location)
1239 size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum,
const uint8_t *odata,
size_t olen);
1254 unsigned blknum,
unsigned szx,
int more)
1257 (blknum << 4) | szx | (more ? 0x8 : 0));
1271 uint16_t content_type)
1302 uint8_t *rbuf,
unsigned rlen,
unsigned payload_len,
1320 size_t token_len,
unsigned code, uint16_t
id);
1344 uint8_t *rbuf,
unsigned rlen,
unsigned payload_len);
1370 return (1 << (code - 1));
1428 uint8_t *buf,
size_t len,
1430 const uint8_t *payload, uint8_t payload_len);
1437 uint8_t *buf,
size_t len,
1459 #if defined(MODULE_GCOAP) || defined(DOXYGEN) 1474 return pkt->observe_value != UINT32_MAX;
1484 pkt->observe_value = UINT32_MAX;
1496 return pkt->observe_value;
1504 #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \ 1506 .path = "/.well-known/core", \ 1507 .methods = COAP_GET, \ 1508 .handler = coap_well_known_core_default_handler \ static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
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.
static unsigned coap_get_code_raw(coap_pkt_t *pkt)
Get a message's raw code (class + detail)
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.
static void coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
static unsigned coap_get_code_detail(coap_pkt_t *pkt)
Get a message's code detail (5 least significant bits of code)
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's options.
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
const unsigned coap_resources_numof
Number of entries in global CoAP resource list.
uint32_t blknum
block number
static unsigned coap_get_code_class(coap_pkt_t *pkt)
Get a message's code class (3 most significant bits of code)
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.
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.
static ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block)
Encode the given block1 option in control use.
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.
Generic CoAP values as defined by RFC7252.
static unsigned coap_szx2size(unsigned szx)
Helper to decode SZX value to size in bytes.
static ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
Append a Content-Format option to the pkt buffer.
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's token length [in byte].
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.
Blockwise transfer helper struct.
uint8_t code
CoAP code (e.g.m 205)
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.
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.
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.
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.
Type for CoAP resource entry.
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
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.
POSIX.1-2008 compliant version of the assert macro.
signed int ssize_t
Used for a count of bytes or an error indication.
coap_hdr_t * hdr
pointer to raw packet
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
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.
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.
#define assert(cond)
abort the program if assertion is false
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.
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
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
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)
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
static ssize_t coap_opt_add_block2_control(coap_pkt_t *pkt, coap_block1_t *block)
Encode the given block2 option in control use.
static unsigned coap_get_id(coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
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.
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.
#define NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
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.
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's URI_PATH.
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.
static uint8_t * coap_hdr_data_ptr(coap_hdr_t *hdr)
Get the start of data after the header.
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
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.
static unsigned coap_get_type(coap_pkt_t *pkt)
Get the message type.
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Resource handler type.
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.
#define NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
static size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type)
Insert content type option into buffer.
int more
-1 for no option, 0 for last block, 1 for more blocks coming
static unsigned coap_get_code(coap_pkt_t *pkt)
Get a message's code in decimal format ((class * 100) + detail)
CoAP PDU parsing context structure.
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's LOCATION_QUERY option.
const coap_resource_t coap_resources[]
Global CoAP resource list.