101#if defined(MODULE_SOCK_UDP) || defined(DOXYGEN)
107#if defined(MODULE_NANOCOAP_RESOURCES)
120#define COAP_GET (0x01)
121#define COAP_POST (0x02)
122#define COAP_PUT (0x04)
123#define COAP_DELETE (0x08)
124#define COAP_FETCH (0x10)
125#define COAP_PATCH (0x20)
126#define COAP_IPATCH (0x40)
127#define COAP_IGNORE (0xFF)
129#define COAP_MATCH_SUBTREE (0x8000)
136#define COAP_FORMAT_NONE (UINT16_MAX)
145#ifndef CONFIG_NANOCOAP_NOPTS_MAX
146#define CONFIG_NANOCOAP_NOPTS_MAX (16)
153#ifndef CONFIG_NANOCOAP_URI_MAX
154#define CONFIG_NANOCOAP_URI_MAX (64)
160#ifndef CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX
161#define CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
167#ifndef CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT
168#define CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT COAP_BLOCKSIZE_64
172#ifndef CONFIG_NANOCOAP_QS_MAX
173#define CONFIG_NANOCOAP_QS_MAX (64)
182#ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX
183#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (80)
193#define COAP_OPT_FINISH_NONE (0x0000)
195#define COAP_OPT_FINISH_PAYLOAD (0x0001)
201typedef struct __attribute__((packed)) {
241 uint32_t observe_value;
321typedef const struct {
341#if defined(MODULE_SOCK_AUX_LOCAL) || DOXYGEN
344#if defined(MODULE_GCOAP) || DOXYGEN
428#if defined(MODULE_NANOCOAP_RESOURCES) || DOXYGEN
434#define NANOCOAP_RESOURCE(name) \
435 XFA_CONST(coap_resource_t, coap_resources_xfa, 0) CONCAT(coap_resource_, name) =
449extern const unsigned coap_resources_numof;
466static inline uint8_t
coap_code(
unsigned cls,
unsigned detail)
468 return (cls << 5) | detail;
516 return (
unsigned)pkt->
hdr->
code;
623 if (!
IS_USED(MODULE_NANOCOAP_TOKEN_EXT)) {
737 const uint8_t *buf = (
const void *)hdr;
821 uint8_t **opt_pos,
int *opt_len);
875 uint8_t *target,
size_t max_len,
char separator);
893 uint8_t *target,
size_t max_len)
896 target, max_len,
'/');
915 uint8_t *target,
size_t max_len)
918 target, max_len,
'&');
958 (uint8_t *)target, max_len,
'&');
976 const char **value,
size_t *len);
1008 uint8_t **value,
bool init_opt);
1159 const void *c,
size_t len);
1272#define coap_szx2size(szx) (1U << ((szx) + 4))
1319 bool more, uint16_t option);
1399 (block->
blknum << 4) | block->
szx | (block->
more ? 0x8 : 0));
1492 const char *val,
size_t val_len);
1551 size_t chars_len,
char separator);
1571 const char *
string,
char separator)
1677 bool more, uint16_t option);
1748 (block->
blknum << 4) | block->
szx | (block->
more ? 0x8 : 0));
1784 const char *
string,
size_t len,
char separator);
1799 const char *
string,
char separator)
1802 string, strlen(
string), separator);
1817 const char *location)
1835 const char *location)
1944size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum,
const void *odata,
size_t olen);
1959 unsigned blknum,
unsigned szx,
int more)
1962 (blknum << 4) | szx | (more ? 0x8 : 0));
1976 uint16_t content_type)
2009 uint8_t *rbuf,
unsigned rlen,
unsigned payload_len,
2032 size_t token_len,
unsigned code, uint16_t
id);
2063 uint8_t *rbuf,
unsigned rlen,
unsigned payload_len);
2117 size_t resources_numof);
2148 return (1 << (code - 1));
2266 void *buf,
size_t len,
2268 void **payload,
size_t *payload_len_max);
2293 uint8_t *buf,
size_t len,
2295 const void *payload,
size_t payload_len);
2302 uint8_t *buf,
size_t len,
2309#ifndef CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE
2310#define CONFIG_NANOCOAP_SERVER_WELL_KNOWN_CORE !IS_USED(MODULE_GCOAP)
2330#if defined(MODULE_GCOAP) || defined(DOXYGEN)
2345 return pkt->observe_value != UINT32_MAX;
2355 pkt->observe_value = UINT32_MAX;
2367 return pkt->observe_value;
2375#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER \
2377 .path = "/.well-known/core", \
2378 .methods = COAP_GET, \
2379 .handler = coap_well_known_core_default_handler \
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Helper functions for bit arithmetic.
static unsigned bitarithm_msb(unsigned v)
Returns the number of the highest '1' bit in a value.
bitfields operations on bitfields of arbitrary length
Functions to work with different byte orders.
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
static uint16_t byteorder_bebuftohs(const uint8_t *buf)
Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable,...
coap_method_t
CoAP method codes used in request.
#define CONFIG_NANOCOAP_NOPTS_MAX
Maximum number of Options in a message.
#define CONFIG_NANOCOAP_URI_MAX
Maximum length of a resource path string read from or written to a message.
static size_t coap_hdr_get_token_len(const coap_hdr_t *hdr)
Get the token length of a CoAP over UDP (DTLS) packet.
uint32_t coap_request_ctx_get_tl_type(const coap_request_ctx_t *ctx)
Get transport the packet was received over.
uint8_t * coap_find_option(coap_pkt_t *pkt, unsigned opt_num)
Get pointer to an option field by type.
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.
static void coap_pkt_set_code(coap_pkt_t *pkt, uint8_t code)
Write the given raw message code to given CoAP pkt.
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.
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 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_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 unsigned coap_get_code_raw(const coap_pkt_t *pkt)
Get a message's raw code (class + detail)
static size_t coap_hdr_len(const coap_hdr_t *hdr)
Get the header length of a CoAP packet.
ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code, void *buf, size_t len, int ct, void **payload, size_t *payload_len_max)
Create CoAP reply header (convenience function)
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.
int coap_match_path(const coap_resource_t *resource, const uint8_t *uri)
Checks if a CoAP resource path matches a given URI.
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 ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
Append a Content-Format option to the pkt buffer.
static uint8_t coap_code(unsigned cls, unsigned detail)
Encode given code class and code detail to raw code.
bool coap_has_unprocessed_critical_options(const coap_pkt_t *pkt)
Check whether any of the packet's options that are critical.
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.
static ssize_t coap_get_uri_query_string(coap_pkt_t *pkt, char *target, size_t max_len)
Convenience function for getting the packet's URI_QUERY option.
ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len, coap_request_ctx_t *ctx)
Handle incoming CoAP request.
static unsigned coap_get_id(const coap_pkt_t *pkt)
Get the message ID of the given CoAP packet.
static ssize_t coap_get_location_query(coap_pkt_t *pkt, uint8_t *target, size_t max_len)
Convenience function for getting the packet's LOCATION_QUERY option.
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.
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 ssize_t coap_opt_add_accept(coap_pkt_t *pkt, uint16_t format)
Append an Accept option to the pkt buffer.
ssize_t coap_opt_get_string(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.
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_request_ctx_init(coap_request_ctx_t *ctx, sock_udp_ep_t *remote)
Initialize CoAP request context.
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_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
Add payload data to the CoAP request.
uint8_t * coap_iterate_option(coap_pkt_t *pkt, unsigned opt_num, uint8_t **opt_pos, int *opt_len)
Get pointer to an option field, can be called in a loop if there are multiple options with the same n...
int coap_opt_get_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t *value)
Get a uint32 option value.
unsigned coap_get_accept(coap_pkt_t *pkt)
Get the Accept option value from a packet if present.
static 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.
uint16_t coap_method_flags_t
Method flag type.
int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option)
Block option getter.
static unsigned coap_get_ver(const coap_pkt_t *pkt)
Get the CoAP version number.
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 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 bool coap_block2_finish(coap_block_slicer_t *slicer)
Finish a block2 response.
size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, const void *c, size_t len)
Add a byte array to a block2 reply.
static uint8_t coap_hdr_tkl_ext_len(const coap_hdr_t *hdr)
Get the size of the extended Token length field (RFC 8974)
static coap_method_t coap_get_method(const coap_pkt_t *pkt)
Get a request's method type.
static unsigned coap_get_total_hdr_len(const coap_pkt_t *pkt)
Get the total header length (4-byte header + token length)
ssize_t coap_subtree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, size_t resp_buf_len, coap_request_ctx_t *context)
Generic coap subtree handler.
static unsigned coap_get_code_class(const coap_pkt_t *pkt)
Get a message's code class (3 most significant bits of code)
static int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block)
Block2 option getter.
ssize_t coap_build_empty_ack(coap_pkt_t *pkt, coap_hdr_t *ack)
Build empty reply to CoAP request.
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.
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 ssize_t coap_get_location_path(coap_pkt_t *pkt, uint8_t *target, size_t max_len)
Convenience function for getting the packet's LOCATION_PATH option.
size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t optnum, const char *string, size_t len, char separator)
Encode the given string as multi-part option into buffer.
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.
bool coap_block_finish(coap_block_slicer_t *slicer, uint16_t option)
Finish a block request (block1 or block2)
static unsigned coap_get_code_detail(const coap_pkt_t *pkt)
Get a message's code detail (5 least significant bits of code)
ssize_t coap_reply_simple(coap_pkt_t *pkt, unsigned code, uint8_t *buf, size_t len, unsigned ct, const void *payload, size_t payload_len)
Create CoAP reply (convenience function)
static int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block)
Block1 option getter.
void coap_block2_init(coap_pkt_t *pkt, coap_block_slicer_t *slicer)
Initialize a block2 slicer struct for writing the payload.
static uint8_t * coap_hdr_data_ptr(const coap_hdr_t *hdr)
Get the start of data after the header.
ssize_t(* coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
Resource handler type.
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_well_known_core_default_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)
Reference to the default .well-known/core handler defined by the application.
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 void coap_hdr_set_type(coap_hdr_t *hdr, unsigned type)
Set the message type for the given CoAP header.
static void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
Advance the payload pointer.
bool coap_find_uri_query(coap_pkt_t *pkt, const char *key, const char **value, size_t *len)
Find a URI query option of the packet.
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_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_block2_control(coap_pkt_t *pkt, coap_block1_t *block)
Encode the given block2 option in control use.
static unsigned coap_get_type(const coap_pkt_t *pkt)
Get the message type.
static unsigned coap_get_code_decimal(const coap_pkt_t *pkt)
Get a message's code in decimal format ((class * 100) + detail)
static unsigned coap_get_response_hdr_len(const coap_pkt_t *pkt)
Get the header length a response to the given packet will have.
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.
size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen)
Insert a CoAP option into buffer.
ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const void *val, size_t val_len)
Encode the given buffer as an opaque data option into pkt.
static ssize_t coap_opt_add_uri_path_buffer(coap_pkt_t *pkt, const char *path, size_t path_len)
Adds one or multiple Uri-Path options in the form '/path' into pkt.
static void * coap_get_token(const coap_pkt_t *pkt)
Get pointer to a message's token.
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.
int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, uint8_t *szx)
Generic block option getter.
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 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.
static bool coap_block1_finish(coap_block_slicer_t *slicer)
Finish a block1 request.
void * coap_request_ctx_get_context(const coap_request_ctx_t *ctx)
Get resource context associated with a CoAP request.
static size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type)
Insert content type option into buffer.
ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri)
Adds a single Proxy-URI option into pkt.
int(* coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
Coap blockwise request callback descriptor.
static unsigned coap_size2szx(unsigned len)
Helper to encode byte size into next equal or smaller SZX value.
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
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.
const char * coap_request_ctx_get_path(const coap_request_ctx_t *ctx)
Get resource path associated with a 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.
const sock_udp_ep_t * coap_request_ctx_get_remote_udp(const coap_request_ctx_t *ctx)
Get the remote endpoint from which the request was received.
int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len)
Parse a CoAP PDU.
const sock_udp_ep_t * coap_request_ctx_get_local_udp(const coap_request_ctx_t *ctx)
Get the local endpoint on which the request has been received.
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize, int more)
Initialize a block struct from content information.
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.
ssize_t coap_opt_remove(coap_pkt_t *pkt, uint16_t optnum)
Removes an option previously added with function in the coap_opt_add_...() group.
unsigned coap_get_content_type(coap_pkt_t *pkt)
Get content type from packet.
static ssize_t coap_get_proxy_uri(coap_pkt_t *pkt, char **target)
Convenience function for getting the packet's Proxy-Uri option.
static ssize_t coap_get_uri_path(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 unsigned coap_get_total_len(const coap_pkt_t *pkt)
Get the total length of a CoAP packet in the packet buffer.
static bool coap_has_observe(coap_pkt_t *pkt)
Identifies a packet containing an observe option.
ssize_t coap_build_hdr(coap_hdr_t *hdr, unsigned type, const void *token, size_t token_len, unsigned code, uint16_t id)
Builds a CoAP header.
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)
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 coap_method_flags_t coap_method2flag(unsigned code)
Convert message code (request method) into a corresponding bit field.
size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri)
Convenience function for inserting URI_PATH and URI_QUERY into buffer This function will automaticall...
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_tree_handler(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len, coap_request_ctx_t *ctx, const coap_resource_t *resources, size_t resources_numof)
Pass a coap request to a matching handler.
static unsigned coap_get_token_len(const coap_pkt_t *pkt)
Get a message's token length [in byte].
static void coap_clear_observe(coap_pkt_t *pkt)
Clears the observe option value from a packet.
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
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.
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definitions for internet operations.
iolist scatter / gather IO
Common macros and compiler attributes/pragmas configuration.
#define IS_USED(module)
Checks whether a module is being used or not.
Generic CoAP values as defined by RFC7252.
CoAP resource request handler context.
sock_udp_ep_t * local
local endpoint of the request
const coap_resource_t * resource
resource of the request
sock_udp_ep_t * remote
remote endpoint of the request
uint32_t tl_type
transport the packet was received over
Common IP-based transport layer end point.
int8_t more
-1 for no option, 0 for last block, 1 for more blocks coming
uint32_t blknum
block number
size_t offset
offset of received data
Blockwise transfer helper struct.
uint8_t * opt
Pointer to the placed option
size_t end
End offset of the current block
size_t start
Start offset of the current block
size_t cur
Offset of the generated content
Raw CoAP PDU header structure.
uint8_t ver_t_tkl
version, token, token length
uint8_t code
CoAP code (e.g.m 205)
uint16_t offset
offset in packet
uint16_t opt_num
full CoAP option number
CoAP PDU parsing context structure.
uint8_t * payload
pointer to end of the header
uint16_t payload_len
length of payload
coap_hdr_t * hdr
pointer to raw packet
uint16_t options_len
length of options array
BITFIELD(opt_crit, CONFIG_NANOCOAP_NOPTS_MAX)
unhandled critical option
iolist_t * snips
payload snips (optional)
Type for CoAP resource subtrees.
const size_t resources_numof
number of entries in array
const coap_resource_t * resources
ptr to resource array
Type for CoAP resource entry.
const char * path
URI path of resource
coap_method_flags_t methods
OR'ed methods this resource allows.
coap_handler_t handler
ptr to resource handler
void * context
ptr to user defined context data
iolist structure definition