Go to the documentation of this file.
77 #ifndef NET_NANOCOAP_H
78 #define NET_NANOCOAP_H
105 #define COAP_GET (0x01)
106 #define COAP_POST (0x02)
107 #define COAP_PUT (0x04)
108 #define COAP_DELETE (0x08)
109 #define COAP_FETCH (0x10)
110 #define COAP_PATCH (0x20)
111 #define COAP_IPATCH (0x40)
112 #define COAP_MATCH_SUBTREE (0x8000)
119 #define COAP_FORMAT_NONE (UINT16_MAX)
128 #ifndef CONFIG_NANOCOAP_NOPTS_MAX
129 #define CONFIG_NANOCOAP_NOPTS_MAX (16)
136 #ifndef CONFIG_NANOCOAP_URI_MAX
137 #define CONFIG_NANOCOAP_URI_MAX (64)
143 #ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
144 #define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
148 #ifndef CONFIG_NANOCOAP_QS_MAX
149 #define CONFIG_NANOCOAP_QS_MAX (64)
160 #define COAP_OPT_FINISH_NONE (0x0000)
162 #define COAP_OPT_FINISH_PAYLOAD (0x0001)
168 typedef struct __attribute__((packed)) {
193 uint32_t observe_value;
276 static inline uint8_t
coap_code(
unsigned cls,
unsigned detail)
278 return (cls << 5) | detail;
326 return (
unsigned)pkt->
hdr->
code;
482 uint8_t *target,
size_t max_len,
char separator);
500 uint8_t *target,
size_t max_len)
503 target, max_len,
'/');
522 uint8_t *target,
size_t max_len)
525 target, max_len,
'&');
598 uint8_t **value,
bool init_opt);
740 const uint8_t *c,
size_t len);
836 return (1 << (szx + 4));
871 bool more, uint16_t option);
951 (block->
blknum << 4) | block->
szx | (block->
more ? 0x8 : 0));
1026 const char *val,
size_t val_len);
1085 size_t chars_len,
char separator);
1105 const char *
string,
char separator)
1171 bool more, uint16_t option);
1242 (block->
blknum << 4) | block->
szx | (block->
more ? 0x8 : 0));
1277 const char *
string,
char separator);
1291 const char *location)
1309 const char *location)
1399 size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum,
const uint8_t *odata,
size_t olen);
1414 unsigned blknum,
unsigned szx,
int more)
1417 (blknum << 4) | szx | (more ? 0x8 : 0));
1431 uint16_t content_type)
1462 uint8_t *rbuf,
unsigned rlen,
unsigned payload_len,
1480 size_t token_len,
unsigned code, uint16_t
id);
1504 uint8_t *rbuf,
unsigned rlen,
unsigned payload_len);
1537 unsigned resp_buf_len,
1539 size_t resources_numof);
1550 return (1 << (code - 1));
1665 uint8_t *buf,
size_t len,
1667 const uint8_t *payload, uint8_t payload_len);
1674 uint8_t *buf,
size_t len,
1696 #if defined(MODULE_GCOAP) || defined(DOXYGEN)
1711 return pkt->observe_value != UINT32_MAX;
1721 pkt->observe_value = UINT32_MAX;
1733 return pkt->observe_value;
1741 #define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
1743 .path = "/.well-known/core", \
1744 .methods = COAP_GET, \
1745 .handler = coap_well_known_core_default_handler \
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.
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.
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
Resource handler type.
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.
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.
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.
uint16_t options_len
length of options array
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
uint16_t offset
offset in packet
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.
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.
Definitions for internet operations.
int more
-1 for no option, 0 for last block, 1 for more blocks coming
int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU.
uint16_t opt_num
full CoAP option number
Generic CoAP values as defined by RFC7252.
static unsigned coap_get_id(coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
uint8_t ver_t_tkl
version, token, token length
static unsigned coap_get_code_detail(const coap_pkt_t *pkt)
Get a message's code detail (5 least significant bits of code)
Functions to work with different byte orders.
#define assert(cond)
abort the program if assertion is false
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.
static void coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
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.
void * context
ptr to user defined context data
void coap_block_slicer_init(coap_block_slicer_t *slicer, size_t blknum, size_t blksize)
Initialize a block slicer struct from content information.
size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c)
Add a single character to a block2 reply.
static coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
size_t start
Start offset of the current block
POSIX.1-2008 compliant version of the assert macro.
const coap_resource_t coap_resources[]
Global CoAP resource list.
static ssize_t coap_get_uri_path(const coap_pkt_t *pkt, uint8_t *target)
Convenience function for getting the packet's URI_PATH.
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 uint8_t * coap_hdr_data_ptr(coap_hdr_t *hdr)
Get the start of data after the header.
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
static size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type)
Insert content type option into buffer.
unsigned coap_get_content_type(coap_pkt_t *pkt)
Get content type from packet.
ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri)
Adds a single Proxy-URI option into pkt.
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.
const char * path
URI path of resource
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.
ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c)
Add a single character to the payload data of the CoAP request.
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.
uint8_t * opt
Pointer to the placed option
int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx)
Generic block option getter.
uint32_t blknum
block number
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 unsigned coap_szx2size(unsigned szx)
Helper to decode SZX value to size in bytes.
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
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 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.
static unsigned coap_get_type(coap_pkt_t *pkt)
Get the message type.
uint16_t coap_method_flags_t
Method flag type.
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.
coap_handler_t handler
ptr to resource handler
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.
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_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.
static ssize_t coap_opt_add_block1_control(coap_pkt_t *pkt, coap_block1_t *block)
Encode the given block1 option in control use.
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.
ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
Encode the given uint option into pkt.
uint8_t code
CoAP code (e.g.m 205)
int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option)
Block option getter.
static void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
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.
int coap_opt_get_uint(const coap_pkt_t *pkt, uint16_t optnum, uint32_t *value)
Get a uint32 option value.
Type for CoAP resource entry.
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 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.
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
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.
coap_hdr_t * hdr
pointer to raw packet
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.
int coap_match_path(const coap_resource_t *resource, uint8_t *uri)
Checks if a CoAP resource path matches a given URI.
static void coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len)
Handle incoming CoAP request.
static unsigned coap_get_ver(coap_pkt_t *pkt)
Get the CoAP version number.
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
static unsigned coap_get_code(coap_pkt_t *pkt)
Get a message's code in decimal format ((class * 100) + detail)
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.
const unsigned coap_resources_numof
Number of entries in global CoAP resource list.
void coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
Finish a block request (block1 or block2)
Raw CoAP PDU header structure.
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
uint16_t payload_len
length of payload
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 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.
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a 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.
static uint32_t coap_get_observe(coap_pkt_t *pkt)
Get the value of the observe option from the given packet.
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.
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)
static unsigned coap_get_code_raw(coap_pkt_t *pkt)
Get a message's raw code (class + detail)
ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars, size_t chars_len, char separator)
Encode the given array of characters as option(s) into pkt.
static 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.
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.
static void coap_hdr_set_code(coap_hdr_t *hdr, uint8_t code)
Write the given raw message code to given CoAP header.
coap_method_flags_t methods
OR'ed methods this resource allows.
void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
CoAP PDU parsing context structure.
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.
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
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.
uint8_t * token
pointer to token
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
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 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_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more)
Initialize a block struct from content information.
Blockwise transfer helper struct.
size_t offset
offset of received data
size_t end
End offset of the current block
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.
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.
static ssize_t coap_get_proxy_uri(const coap_pkt_t *pkt, char **target)
Convenience function for getting the packet's Proxy-Uri option.
static unsigned coap_get_code_class(coap_pkt_t *pkt)
Get a message's code class (3 most significant bits of code)
size_t cur
Offset of the generated content
uint8_t * payload
pointer to payload