CoAP library optimized for minimal resource usage.
More...
CoAP library optimized for minimal resource usage.
nanocoap is a toolbox for reading and writing CoAP messages. It provides functions for core header attributes like message type and code. It also provides high and low level interfaces to CoAP options, including Block.
nanocoap includes the core structs to store message information. It also provides helper functions for use before sending and after receiving a message, such as coap_parse() to read an incoming message.
Application APIs
This page provides reference documentation for the contents of nanocoap. To use nanocoap in an application, see the functional APIs that are built with it. nanocoap sock is for a targeted client or server with lesser resource requirements, and gcoap provides a more featureful messaging hub.
Option APIs
For both server responses and client requests, CoAP uses an Option mechanism to encode message metadata that is not required for each message. For example, the resource URI path is required only for a request, and is encoded as the Uri-Path option.
nanocoap sock uses the simpler Buffer API, described in the section Options Write Buffer API. gcoap uses the more convenient Packet API, described in the section Options Write Packet API.
For either API, the caller must write options in order by option number (see "CoAP option numbers" in CoAP defines).
Server path matching
By default the URI-path of an incoming request should match exactly one of the registered resources. But also, a resource can be configured to match just a prefix of the URI-path of the request by adding the COAP_MATCH_SUBTREE option to coap_resource_t::methods.
For example, if a resource is configured with a path /resource01
and the COAP_MATCH_SUBTREE option is used it would match any of /resource01/
, /resource01/sub/path
, /resource01alt
.
If the behavior of matching /resource01alt
is not wanted and only subtrees are wanted to match, the path should be /resource01/
.
If in addition just /resource01
is wanted to match, together with any subtrees of /resource01/
, then a first resource with the path /resource01
and exact matching should be register, and then a second one with the path /resource01/
and subtree matching.
|
|
#define | COAP_GET (0x01) |
|
#define | COAP_POST (0x02) |
|
#define | COAP_PUT (0x04) |
|
#define | COAP_DELETE (0x08) |
|
#define | COAP_FETCH (0x10) |
|
#define | COAP_PATCH (0x20) |
|
#define | COAP_IPATCH (0x40) |
|
#define | COAP_MATCH_SUBTREE (0x8000) |
| Path is considered as a prefix when matching.
|
|
|
Read options from a parsed packet.
|
unsigned | coap_get_content_type (coap_pkt_t *pkt) |
| Get content type from packet. More...
|
|
int | coap_opt_get_uint (const coap_pkt_t *pkt, uint16_t optnum, uint32_t *value) |
| Get a uint32 option value. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
static ssize_t | coap_get_uri_path (const coap_pkt_t *pkt, uint8_t *target) |
| Convenience function for getting the packet's URI_PATH. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
|
Read Block1 (POST/PUT request) or Block2 (GET response) options, and generally useful functions to write block options.
|
void | coap_block_object_init (coap_block1_t *block, size_t blknum, size_t blksize, int more) |
| Initialize a block struct from content information. More...
|
|
void | coap_block_finish (coap_block_slicer_t *slicer, uint16_t option) |
| Finish a block request (block1 or block2) More...
|
|
static void | coap_block1_finish (coap_block_slicer_t *slicer) |
| Finish a block1 request. More...
|
|
static void | coap_block2_finish (coap_block_slicer_t *slicer) |
| Finish a block2 response. More...
|
|
void | coap_block2_init (coap_pkt_t *pkt, coap_block_slicer_t *slicer) |
| Initialize a block2 slicer struct for writing the payload. More...
|
|
void | coap_block_slicer_init (coap_block_slicer_t *slicer, size_t blknum, size_t blksize) |
| Initialize a block slicer struct from content information. More...
|
|
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. More...
|
|
size_t | coap_blockwise_put_char (coap_block_slicer_t *slicer, uint8_t *bufpos, char c) |
| Add a single character to a block2 reply. More...
|
|
int | coap_get_block (coap_pkt_t *pkt, coap_block1_t *block, uint16_t option) |
| Block option getter. More...
|
|
static int | coap_get_block1 (coap_pkt_t *pkt, coap_block1_t *block) |
| Block1 option getter. More...
|
|
static int | coap_get_block2 (coap_pkt_t *pkt, coap_block1_t *block) |
| Block2 option getter. More...
|
|
int | coap_get_blockopt (coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx) |
| Generic block option getter. More...
|
|
static unsigned | coap_szx2size (unsigned szx) |
| Helper to decode SZX value to size in bytes. More...
|
|
|
Use a coap_pkt_t struct to manage writing Options to the PDU.
The caller must monitor space remaining in the buffer; however, the API will not write past the end of the buffer, and returns -ENOSPC when it is full.
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
ssize_t | coap_opt_add_uint (coap_pkt_t *pkt, uint16_t optnum, uint32_t value) |
| Encode the given uint option into pkt. More...
|
|
static ssize_t | coap_opt_add_block1_control (coap_pkt_t *pkt, coap_block1_t *block) |
| Encode the given block1 option in control use. More...
|
|
static ssize_t | coap_opt_add_block2_control (coap_pkt_t *pkt, coap_block1_t *block) |
| Encode the given block2 option in control use. More...
|
|
static ssize_t | coap_opt_add_format (coap_pkt_t *pkt, uint16_t format) |
| Append a Content-Format option to the pkt buffer. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
ssize_t | coap_opt_add_proxy_uri (coap_pkt_t *pkt, const char *uri) |
| Adds a single Proxy-URI option into pkt . More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
ssize_t | coap_opt_finish (coap_pkt_t *pkt, uint16_t flags) |
| Finalizes options as required and prepares for payload. More...
|
|
|
Write PDU Options directly to the array of bytes for a message.
The caller must provide the last option number written as well as the buffer position. The caller is primarily responsible for tracking and managing the space remaining in the 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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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. More...
|
|
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) More...
|
|
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. More...
|
|
static size_t | coap_put_option_block1 (uint8_t *buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more) |
| Insert block1 option into buffer. More...
|
|
static size_t | coap_put_option_ct (uint8_t *buf, uint16_t lastonum, uint16_t content_type) |
| Insert content type option into buffer. More...
|
|
|
Functions to support sending and receiving messages.
|
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. More...
|
|
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. More...
|
|
ssize_t | coap_build_reply (coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len) |
| Build reply to CoAP request. More...
|
|
ssize_t | coap_handle_req (coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len) |
| Handle incoming CoAP request. More...
|
|
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. More...
|
|
static coap_method_flags_t | coap_method2flag (unsigned code) |
| Convert message code (request method) into a corresponding bit field. More...
|
|
int | coap_parse (coap_pkt_t *pkt, uint8_t *buf, size_t len) |
| Parse a CoAP PDU. More...
|
|
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. More...
|
|
static void | coap_payload_advance_bytes (coap_pkt_t *pkt, size_t len) |
| Advance the payload pointer. More...
|
|
ssize_t | coap_payload_put_bytes (coap_pkt_t *pkt, const void *data, size_t len) |
| Add payload data to the CoAP request. More...
|
|
ssize_t | coap_payload_put_char (coap_pkt_t *pkt, char c) |
| Add a single character to the payload data of the CoAP request. More...
|
|
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) More...
|
|
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.
|
|
◆ COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER
#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER |
Value: { \
.path = "/.well-known/core", \
.methods = COAP_GET, \
}
Resource definition for the default .well-known/core handler.
Definition at line 1741 of file nanocoap.h.
◆ coap_handler_t
typedef ssize_t(* coap_handler_t) (coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) |
Resource handler type.
Functions that implement this must be prepared to be called multiple times for the same request, as the server implementations do not perform message deduplication. That optimization is described in the CoAP specification.
This should be trivial for requests of the GET, PUT, DELETE, FETCH and iPATCH methods, as they are defined as idempotent methods in CoAP.
For POST, PATCH and other non-idempotent methods, this is an additional requirement introduced by the contract of this type.
Definition at line 211 of file nanocoap.h.
◆ coap_method_flags_t
◆ coap_block1_finish()
Finish a block1 request.
This function finalizes the block1 response header
Checks whether the more
bit should be set in the block1 option and sets/clears it if required. Doesn't return the number of bytes, as this function overwrites bytes in the packet rather than adding new.
- Parameters
-
[in] | slicer | Preallocated slicer struct to use |
Definition at line 681 of file nanocoap.h.
◆ coap_block2_build_reply()
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.
This function can be used to create a reply to a CoAP block2 request packet. In addition to coap_build_reply, this function checks the block2 option and returns an error message to the client if necessary.
- Parameters
-
[in] | pkt | packet to reply to |
[in] | code | reply code (e.g., COAP_CODE_204) |
[out] | rbuf | buffer to write reply to |
[in] | rlen | size of rbuf |
[in] | payload_len | length of payload |
[in] | slicer | slicer to use |
- Returns
- size of reply packet on success
-
<0 on error
◆ coap_block2_finish()
Finish a block2 response.
This function finalizes the block2 response header
Checks whether the more
bit should be set in the block2 option and sets/clears it if required. Doesn't return the number of bytes, as this function overwrites bytes in the packet rather than adding new.
- Parameters
-
[in] | slicer | Preallocated slicer struct to use |
Definition at line 697 of file nanocoap.h.
◆ coap_block2_init()
Initialize a block2 slicer struct for writing the payload.
This function determines the size of the response payload based on the size requested by the client in pkt
.
- Parameters
-
[in] | pkt | packet to work on |
[out] | slicer | Preallocated slicer struct to fill |
◆ coap_block_finish()
Finish a block request (block1 or block2)
This function finalizes the block response header
Checks whether the more
bit should be set in the block option and sets/clears it if required. Doesn't return the number of bytes, as this function overwrites bytes in the packet rather than adding new.
- Parameters
-
[in] | slicer | Preallocated slicer struct to use |
[in] | option | option number (block1 or block2) |
◆ coap_block_object_init()
void coap_block_object_init |
( |
coap_block1_t * |
block, |
|
|
size_t |
blknum, |
|
|
size_t |
blksize, |
|
|
int |
more |
|
) |
| |
Initialize a block struct from content information.
- Parameters
-
[out] | block | block struct to initialize |
[in] | blknum | offset from the beginning of content, in terms of blksize byte blocks |
[in] | blksize | size of each block; must be a power of 2 between 16 and 2 raised to CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX |
[in] | more | more blocks? use 1 if yes; 0 if no or unknown |
◆ coap_block_slicer_init()
Initialize a block slicer struct from content information.
- Parameters
-
[out] | slicer | slicer struct to initialize |
[in] | blknum | offset from the beginning of content, in terms of blksize byte blocks |
[in] | blksize | size of each block; must be a power of 2 between 16 and 2 raised to CONFIG_NANOCOAP_BLOCK_SIZE_EXP_MAX |
◆ coap_blockwise_put_bytes()
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.
This function is used to add an array of bytes to a CoAP block2 reply. it checks which parts of the string should be added to the reply and ignores parts that are outside the current block2 request.
- Parameters
-
[in] | slicer | slicer to use |
[in] | bufpos | pointer to the current payload buffer position |
[in] | c | byte array to copy |
[in] | len | length of the byte array |
- Returns
- Number of bytes written to
bufpos
◆ coap_blockwise_put_char()
Add a single character to a block2 reply.
This function is used to add single characters to a CoAP block2 reply. It checks whether the character should be added to the buffer and ignores it when the character is outside the current block2 request.
- Parameters
-
[in] | slicer | slicer to use |
[in] | bufpos | pointer to the current payload buffer position |
[in] | c | character to write |
- Returns
- Number of bytes written to
bufpos
◆ coap_build_hdr()
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.
Caller must ensure hdr
can hold the header and the full token!
- Parameters
-
[out] | hdr | hdr to fill |
[in] | type | CoAP packet type (e.g., COAP_TYPE_CON, ...) |
[in] | token | token |
[in] | token_len | length of token |
[in] | code | CoAP code (e.g., COAP_CODE_204, ...) |
[in] | id | CoAP request id |
- Returns
- length of resulting header
◆ coap_build_reply()
ssize_t coap_build_reply |
( |
coap_pkt_t * |
pkt, |
|
|
unsigned |
code, |
|
|
uint8_t * |
rbuf, |
|
|
unsigned |
rlen, |
|
|
unsigned |
payload_len |
|
) |
| |
Build reply to CoAP request.
This function can be used to create a reply to any CoAP request packet. It will create the reply packet header based on parameters from the request (e.g., id, token).
Passing a non-zero payload_len
will ensure the payload fits into the buffer along with the header. For this validation, payload_len must include any options, the payload marker, as well as the payload proper.
- Parameters
-
[in] | pkt | packet to reply to |
[in] | code | reply code (e.g., COAP_CODE_204) |
[out] | rbuf | buffer to write reply to |
[in] | rlen | size of rbuf |
[in] | payload_len | length of payload |
- Returns
- size of reply packet on success
-
<0 on error
-
-ENOSPC if
rbuf
too small
◆ coap_clear_observe()
static void coap_clear_observe |
( |
coap_pkt_t * |
pkt | ) |
|
|
inlinestatic |
Clears the observe option value from a packet.
- Parameters
-
Definition at line 1719 of file nanocoap.h.
◆ coap_code()
static uint8_t coap_code |
( |
unsigned |
cls, |
|
|
unsigned |
detail |
|
) |
| |
|
inlinestatic |
Encode given code class and code detail to raw code.
- Parameters
-
[in] | cls | message code class |
[in] | detail | message code detail |
- Returns
- raw message code
Definition at line 276 of file nanocoap.h.
◆ coap_get_block()
Block option getter.
This function gets a CoAP packet's block option and parses it into a helper structure.
If no block option is present in pkt
, the values in block
will be initialized with zero. That implies both block->offset and block->more are also valid in that case, as packet with offset==0 and more==0 means it contains all the payload for the corresponding request.
- Parameters
-
[in] | pkt | pkt to work on |
[out] | block | ptr to preallocated coap_block1_t structure |
[in] | option | block1 or block2 |
- Returns
- 0 if block option not present
-
1 if structure has been filled
◆ coap_get_block1()
Block1 option getter.
This function gets a CoAP packet's block1 option and parses it into a helper structure.
If no block1 option is present in pkt
, the values in block1
will be initialized with zero. That implies both block1->offset and block1->more are also valid in that case, as packet with offset==0 and more==0 means it contains all the payload for the corresponding request.
- Parameters
-
[in] | pkt | pkt to work on |
[out] | block | ptr to preallocated coap_block1_t structure |
- Returns
- 0 if block1 option not present
-
1 if structure has been filled
Definition at line 794 of file nanocoap.h.
◆ coap_get_block2()
Block2 option getter.
- Parameters
-
[in] | pkt | pkt to work on |
[out] | block | ptr to preallocated coap_block1_t structure |
- Returns
- 0 if block2 option not present
-
1 if structure has been filled
Definition at line 808 of file nanocoap.h.
◆ coap_get_blockopt()
int coap_get_blockopt |
( |
coap_pkt_t * |
pkt, |
|
|
uint16_t |
option, |
|
|
uint32_t * |
blknum, |
|
|
unsigned * |
szx |
|
) |
| |
Generic block option getter.
- Parameters
-
[in] | pkt | pkt to work on |
[in] | option | actual block option number to get |
[out] | blknum | block number |
[out] | szx | SZX value |
- Returns
- -1 if option not found
-
0 if more flag is not set
-
1 if more flag is set
◆ coap_get_code()
Get a message's code in decimal format ((class * 100) + detail)
- Parameters
-
- Returns
- message code in decimal format
Definition at line 312 of file nanocoap.h.
◆ coap_get_code_class()
static unsigned coap_get_code_class |
( |
coap_pkt_t * |
pkt | ) |
|
|
inlinestatic |
Get a message's code class (3 most significant bits of code)
- Parameters
-
- Returns
- message code class
Definition at line 288 of file nanocoap.h.
◆ coap_get_code_detail()
static unsigned coap_get_code_detail |
( |
const coap_pkt_t * |
pkt | ) |
|
|
inlinestatic |
Get a message's code detail (5 least significant bits of code)
- Parameters
-
- Returns
- message code detail
Definition at line 300 of file nanocoap.h.
◆ coap_get_code_raw()
static unsigned coap_get_code_raw |
( |
coap_pkt_t * |
pkt | ) |
|
|
inlinestatic |
Get a message's raw code (class + detail)
- Parameters
-
- Returns
- raw message code
Definition at line 324 of file nanocoap.h.
◆ coap_get_content_type()
unsigned coap_get_content_type |
( |
coap_pkt_t * |
pkt | ) |
|
Get content type from packet.
- Parameters
-
- Returns
- the packet's content type value if included, COAP_FORMAT_NONE otherwise
◆ coap_get_id()
Get the message ID of the given CoAP packet.
- Parameters
-
- Returns
- message ID
Definition at line 336 of file nanocoap.h.
◆ coap_get_location_path()
static ssize_t coap_get_location_path |
( |
const coap_pkt_t * |
pkt, |
|
|
uint8_t * |
target, |
|
|
size_t |
max_len |
|
) |
| |
|
inlinestatic |
Convenience function for getting the packet's LOCATION_PATH option.
This function decodes the pkt's LOCATION_PATH option into a '/'-separated and '\0'-terminated string.
Caller must ensure target
can hold at least 2 bytes!
- Parameters
-
[in] | pkt | pkt to work on |
[out] | target | buffer for location path |
[in] | max_len | size of target in bytes |
- Returns
- -ENOSPC if URI option is larger than
max_len
-
nr of bytes written to
target
(including '\0')
Definition at line 499 of file nanocoap.h.
◆ coap_get_location_query()
static ssize_t coap_get_location_query |
( |
const coap_pkt_t * |
pkt, |
|
|
uint8_t * |
target, |
|
|
size_t |
max_len |
|
) |
| |
|
inlinestatic |
Convenience function for getting the packet's LOCATION_QUERY option.
This function decodes the pkt's LOCATION_PATH option into a '&'-separated and '\0'-terminated string.
Caller must ensure target
can hold at least 2 bytes!
- Parameters
-
[in] | pkt | pkt to work on |
[out] | target | buffer for location path |
[in] | max_len | size of target in bytes |
- Returns
- -ENOSPC if URI option is larger than
max_len
-
nr of bytes written to
target
(including '\0')
Definition at line 521 of file nanocoap.h.
◆ coap_get_observe()
static uint32_t coap_get_observe |
( |
coap_pkt_t * |
pkt | ) |
|
|
inlinestatic |
Get the value of the observe option from the given packet.
- Parameters
-
- Returns
- value of the observe option
Definition at line 1731 of file nanocoap.h.
◆ coap_get_proxy_uri()
static ssize_t coap_get_proxy_uri |
( |
const coap_pkt_t * |
pkt, |
|
|
char ** |
target |
|
) |
| |
|
inlinestatic |
Convenience function for getting the packet's Proxy-Uri option.
- Parameters
-
[in] | pkt | pkt to work on |
[out] | target | pointer to the PROXY_URI in pkt |
- Precondition
- ((pkt != NULL) && (target != NULL))
- Returns
- length of the Proxy-Uri option
-
-ENOENT if Proxy-Uri option not found
-
-EINVAL if Proxy-Uri option cannot be parsed
Definition at line 631 of file nanocoap.h.
◆ coap_get_token_len()
static unsigned coap_get_token_len |
( |
const coap_pkt_t * |
pkt | ) |
|
|
inlinestatic |
Get a message's token length [in byte].
- Parameters
-
- Returns
- length of token in the given message (0-8 byte)
Definition at line 348 of file nanocoap.h.
◆ coap_get_total_hdr_len()
static unsigned coap_get_total_hdr_len |
( |
const coap_pkt_t * |
pkt | ) |
|
|
inlinestatic |
Get the total header length (4-byte header + token length)
- Parameters
-
- Returns
- total header length
Definition at line 360 of file nanocoap.h.
◆ coap_get_type()
Get the message type.
- Parameters
-
- Returns
- COAP_TYPE_CON
-
COAP_TYPE_NON
-
COAP_TYPE_ACK
-
COAP_TYPE_RST
Definition at line 375 of file nanocoap.h.
◆ coap_get_uri_path()
static ssize_t coap_get_uri_path |
( |
const coap_pkt_t * |
pkt, |
|
|
uint8_t * |
target |
|
) |
| |
|
inlinestatic |
Convenience function for getting the packet's URI_PATH.
This function decodes the pkt's URI option into a "/"-separated and '\0'-terminated string.
Caller must ensure target
can hold at least CONFIG_NANOCOAP_URI_MAX bytes!
- Parameters
-
[in] | pkt | pkt to work on |
[out] | target | buffer for target URI |
- Returns
- -ENOSPC if URI option is larger than CONFIG_NANOCOAP_URI_MAX
-
nr of bytes written to
target
(including '\0')
Definition at line 542 of file nanocoap.h.
◆ coap_get_uri_query()
static ssize_t coap_get_uri_query |
( |
const coap_pkt_t * |
pkt, |
|
|
uint8_t * |
target |
|
) |
| |
|
inlinestatic |
Convenience function for getting the packet's URI_QUERY option.
This function decodes the pkt's URI_QUERY option into a "&"-separated and '\0'-terminated string.
Caller must ensure target
can hold at least CONFIG_NANOCOAP_URI_MAX bytes!
- Parameters
-
[in] | pkt | pkt to work on |
[out] | target | buffer for target URI |
- Returns
- -ENOSPC if URI option is larger than CONFIG_NANOCOAP_URI_MAX
-
nr of bytes written to
target
(including '\0')
Definition at line 562 of file nanocoap.h.
◆ coap_get_ver()
Get the CoAP version number.
- Parameters
-
- Returns
- CoAP version number
Definition at line 387 of file nanocoap.h.
◆ coap_handle_req()
ssize_t coap_handle_req |
( |
coap_pkt_t * |
pkt, |
|
|
uint8_t * |
resp_buf, |
|
|
unsigned |
resp_buf_len |
|
) |
| |
Handle incoming CoAP request.
This function will find the correct handler, call it and write the reply into resp_buf
.
- Parameters
-
[in] | pkt | pointer to (parsed) CoAP packet |
[out] | resp_buf | buffer for response |
[in] | resp_buf_len | size of response buffer |
- Returns
- size of reply packet on success
-
<0 on error
◆ coap_has_observe()
Identifies a packet containing an observe option.
- Parameters
-
- Returns
- true if observe value is set
-
false if not
Definition at line 1709 of file nanocoap.h.
◆ coap_hdr_data_ptr()
static uint8_t* coap_hdr_data_ptr |
( |
coap_hdr_t * |
hdr | ) |
|
|
inlinestatic |
Get the start of data after the header.
- Parameters
-
[in] | hdr | Header of CoAP packet in contiguous memory |
- Returns
- pointer to first byte after the header
Definition at line 399 of file nanocoap.h.
◆ coap_hdr_set_code()
static void coap_hdr_set_code |
( |
coap_hdr_t * |
hdr, |
|
|
uint8_t |
code |
|
) |
| |
|
inlinestatic |
Write the given raw message code to given CoAP header.
- Parameters
-
[out] | hdr | CoAP header to write to |
[in] | code | raw message code |
Definition at line 410 of file nanocoap.h.
◆ coap_hdr_set_type()
static void coap_hdr_set_type |
( |
coap_hdr_t * |
hdr, |
|
|
unsigned |
type |
|
) |
| |
|
inlinestatic |
Set the message type for the given CoAP header.
- Precondition
- (type := [0-3])
- Parameters
-
[out] | hdr | CoAP header to write |
[in] | type | message type as integer value [0-3] |
Definition at line 423 of file nanocoap.h.
◆ coap_match_path()
Checks if a CoAP resource path matches a given URI.
Builds on strcmp() with rules specific to URI path matching
- Note
- This function is not intended for application use.
◆ coap_method2flag()
Convert message code (request method) into a corresponding bit field.
- Parameters
-
[in] | code | request code denoting the request method |
- Returns
- bit field corresponding to the given request method
Definition at line 1548 of file nanocoap.h.
◆ coap_opt_add_block()
Add block option in descriptive use from a slicer object.
When calling this function to initialize a packet with a block option, the more flag must be set to prevent the creation of an option with a length too small to contain the size bit.
- Postcondition
- pkt.payload advanced to first byte after option
-
pkt.payload_len reduced by option length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | slicer | coap blockwise slicer helper struct |
[in] | more | more flag (1 or 0) |
[in] | option | option number (block1 or block2) |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
◆ coap_opt_add_block1()
Add block1 option in descriptive use from a slicer object.
When calling this function to initialize a packet with a block option, the more flag must be set to prevent the creation of an option with a length too small to contain the size bit.
- Postcondition
- pkt.payload advanced to first byte after option
-
pkt.payload_len reduced by option length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | slicer | coap blockwise slicer helper struct |
[in] | more | more flag (1 or 0) |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
Definition at line 891 of file nanocoap.h.
◆ coap_opt_add_block1_control()
Encode the given block1 option in control use.
- Postcondition
- pkt.payload advanced to first byte after option
-
pkt.payload_len reduced by option length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | block | block to encode |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
Definition at line 949 of file nanocoap.h.
◆ coap_opt_add_block2()
Add block2 option in descriptive use from a slicer object.
When calling this function to initialize a packet with a block option, the more flag must be set to prevent the creation of an option with a length too small to contain the size bit.
- Postcondition
- pkt.payload advanced to first byte after option
-
pkt.payload_len reduced by option length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | slicer | coap blockwise slicer helper struct |
[in] | more | more flag (1 or 0) |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
Definition at line 915 of file nanocoap.h.
◆ coap_opt_add_block2_control()
Encode the given block2 option in control use.
- Postcondition
- pkt.payload advanced to first byte after option
-
pkt.payload_len reduced by option length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | block | block to encode |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
Definition at line 967 of file nanocoap.h.
◆ coap_opt_add_chars()
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.
Use separator to split array of characters into multiple options.
- Postcondition
- pkt.payload advanced to first byte after option(s)
-
pkt.payload_len reduced by option(s) length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | optnum | option number to use |
[in] | chars | array of characters to encode as option |
[in] | chars_len | length of chars |
[in] | separator | character used in string to separate parts |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
◆ coap_opt_add_format()
static ssize_t coap_opt_add_format |
( |
coap_pkt_t * |
pkt, |
|
|
uint16_t |
format |
|
) |
| |
|
inlinestatic |
Append a Content-Format option to the pkt buffer.
- Postcondition
- pkt.payload advanced to first byte after option
-
pkt.payload_len reduced by option length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | format | COAP_FORMAT_xxx to use |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
Definition at line 986 of file nanocoap.h.
◆ coap_opt_add_opaque()
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.
- Postcondition
- pkt.payload advanced to first byte after option(s)
-
pkt.payload_len reduced by option(s) length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | optnum | option number to use |
[in] | val | pointer to the value to be set |
[in] | val_len | length of val |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options
◆ coap_opt_add_proxy_uri()
ssize_t coap_opt_add_proxy_uri |
( |
coap_pkt_t * |
pkt, |
|
|
const char * |
uri |
|
) |
| |
Adds a single Proxy-URI option into pkt
.
- Note
uri
must be a NULL-terminated absolute URI
- Parameters
-
[in,out] | pkt | Packet being built |
[in] | uri | absolute proxy URI |
- Precondition
- ((pkt != NULL) && (uri != NULL))
- Returns
- number of bytes written to pkt buffer
-
<0 on error
-
-ENOSPC if no available options or pkt full
◆ coap_opt_add_string()
static ssize_t coap_opt_add_string |
( |
coap_pkt_t * |
pkt, |
|
|
uint16_t |
optnum, |
|
|
const char * |
string, |
|
|
char |
separator |
|
) |
| |
|
inlinestatic |
Encode the given string as option(s) into pkt.
Use separator to split string into multiple options.
- Postcondition
- pkt.payload advanced to first byte after option(s)
-
pkt.payload_len reduced by option(s) length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | optnum | option number to use |
[in] | string | string to encode as option |
[in] | separator | character used in string to separate parts |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
Definition at line 1104 of file nanocoap.h.
◆ coap_opt_add_uint()
ssize_t coap_opt_add_uint |
( |
coap_pkt_t * |
pkt, |
|
|
uint16_t |
optnum, |
|
|
uint32_t |
value |
|
) |
| |
Encode the given uint option into pkt.
- Postcondition
- pkt.payload advanced to first byte after option
-
pkt.payload_len reduced by option length
- Parameters
-
[in,out] | pkt | pkt referencing target buffer |
[in] | optnum | option number to use |
[in] | value | uint to encode |
- Returns
- number of bytes written to buffer
-
<0 on error
-
-ENOSPC if no available options or insufficient buffer space
◆ coap_opt_add_uri_path()
static ssize_t coap_opt_add_uri_path |
( |
coap_pkt_t * |
pkt, |
|
|
const char * |
path |
|
) |
| |
|
inlinestatic |
Adds one or multiple Uri-Path options in the form '/path' into pkt.
- Note
- Use this only for null-terminated strings.
- Parameters
-
[in,out] | pkt | Packet being built |
[in] | path | Resource (sub)path |
- Precondition
- ((pkt != NULL) && (path != NULL))
- Returns
- number of bytes written to pkt buffer
-
<0 on error
-
-ENOSPC if no available options or pkt full
Definition at line 1124 of file nanocoap.h.
◆ coap_opt_add_uri_query()
static ssize_t coap_opt_add_uri_query |
( |
coap_pkt_t * |
pkt, |
|
|
const char * |
key, |
|
|
const char * |
val |
|
) |
| |
|
inlinestatic |
Adds a single Uri-Query option in the form 'key=value' into pkt.
- Note
- Use this only for null-terminated string. See coap_opt_add_uri_query2() for non null-terminated string.
- Parameters
-
[in,out] | pkt | Packet being built |
[in] | key | Key to add to the query string |
[in] | val | Value to assign to key (may be NULL) |
- Precondition
- ((pkt != NULL) && (key != NULL))
- Returns
- number of bytes written to pkt buffer
-
<0 on error
-
-ENOSPC if no available options or pkt full
Definition at line 1044 of file nanocoap.h.
◆ coap_opt_add_uri_query2()
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.
- Parameters
-
[in,out] | pkt | Packet being built |
[in] | key | Key to add to the query string |
[in] | key_len | Length of key |
[in] | val | Value to assign to key (may be NULL) |
[in] | val_len | Length of val . 0 if val is NULL |
- Precondition
- ((pkt != NULL) && (key != NULL) && (key_len > 0) && ((val_len == 0) || ((val != NULL) && (val_len > 0))))
- Returns
- number of bytes written to pkt buffer
-
<0 on error
-
-ENOSPC if no available options or pkt full
◆ coap_opt_finish()
ssize_t coap_opt_finish |
( |
coap_pkt_t * |
pkt, |
|
|
uint16_t |
flags |
|
) |
| |
Finalizes options as required and prepares for payload.
- Postcondition
- pkt.payload advanced to first available byte after options
-
pkt.payload_len is maximum bytes available for payload
- Parameters
-
[in,out] | pkt | pkt to update |
[in] | flags | see COAP_OPT_FINISH... macros |
- Returns
- total number of bytes written to buffer
-
-ENOSPC if no buffer space for payload marker
◆ coap_opt_get_next()
Iterate over a packet's options.
To start iteration from the first option, set init_opt
to true. To start iteration from a specific option, set init_opt
to false, set opt->offset
to the offset of the desired option from pkt->hdr, and opt->opt_num
as required. See below for how opt->opt_num
is modified.
With each invocation, this function returns the length of the option value and sets value
to point to the start of the value. The value for opt->opt_num
is increased by the delta in the option number value over the preceding option in the packet. So, opt->opt_num
is accurate if iteration started with the first option. Otherwise, it is useful for identification of repeated options. Finally, opt->offset
is set to the offset for any following option, to prepare for the next iteration.
The end of the options is indicated by a -ENOENT return value. In this case value
and opt
are unchanged from their input values.
- Parameters
-
[in] | pkt | packet to read from |
[in,out] | opt | option attributes; read on input if init_opt is false |
[out] | value | start of the option value |
[in] | init_opt | true to retrieve first option; false to retrieve option at opt->offset |
- Returns
- length of option value
-
-ENOENT if option not found
◆ coap_opt_get_opaque()
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.
Retrieves the location and length of the option value of any type. Useful for an opaque option, which essentially is an array of bytes. If more than one option for a given option number, retrieves the first option. To retrieve subsequent options, see coap_opt_get_next().
- Parameters
-
[in] | pkt | packet to read from |
[in] | opt_num | option number to retrieve |
[out] | value | start of the option value |
- Returns
- length of option; 0 if the option exists but is empty
-
-ENOENT if option not found
-
-EINVAL if option cannot be parsed
◆ coap_opt_get_string()
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.
This function is for reading and concatenating string based, multi-part CoAP options like COAP_OPT_URI_PATH or COAP_OPT_LOCATION_PATH. It will write all parts of the given option into the target buffer, separating the parts using the given separator
. The resulting string is \0
terminated.
- Parameters
-
[in] | pkt | packet to read from |
[in] | optnum | absolute option number |
[out] | target | target buffer |
[in] | max_len | size of target |
[in] | separator | character used for separating the option parts |
- Returns
- -ENOSPC if the complete option does not fit into
target
-
nr of bytes written to
target
(including '\0')
◆ coap_opt_get_uint()
int coap_opt_get_uint |
( |
const coap_pkt_t * |
pkt, |
|
|
uint16_t |
optnum, |
|
|
uint32_t * |
value |
|
) |
| |
Get a uint32 option value.
- Parameters
-
[in] | pkt | packet to read from |
[in] | optnum | absolute option number |
[out] | value | the parsed option value |
- Returns
- 0 if the option was found and the value was parsed correctly
-
-ENOENT if the option was not found in
pkt
-
-ENOSPC if option length is greater than 4 octets
-
-EBADMSG if option value is invalid
◆ coap_opt_put_block()
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.
When calling this function to initialize a packet with a block option, the more flag must be set to prevent the creation of an option with a length too small to contain the size bit.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option, must be < option |
[in] | slicer | coap blockwise slicer helper struct |
[in] | more | more flag (1 or 0) |
[in] | option | option number (block1 or block2) |
- Returns
- amount of bytes written to
buf
◆ coap_opt_put_block1()
static size_t coap_opt_put_block1 |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
coap_block_slicer_t * |
slicer, |
|
|
bool |
more |
|
) |
| |
|
inlinestatic |
Insert block1 option into buffer.
When calling this function to initialize a packet with a block1 option, the more flag must be set to prevent the creation of an option with a length too small to contain the size bit.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), must be < 27 |
[in] | slicer | coap blockwise slicer helper struct |
[in] | more | more flag (1 or 0) |
- Returns
- amount of bytes written to
buf
Definition at line 1188 of file nanocoap.h.
◆ coap_opt_put_block1_control()
static size_t coap_opt_put_block1_control |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
coap_block1_t * |
block |
|
) |
| |
|
inlinestatic |
Insert block1 option into buffer in control usage.
- Parameters
-
[in] | buf | buffer to write to |
[in] | lastonum | last option number (must be < 27) |
[in] | block | block option attribute struct |
- Returns
- amount of bytes written to
buf
Definition at line 1238 of file nanocoap.h.
◆ coap_opt_put_block2()
static size_t coap_opt_put_block2 |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
coap_block_slicer_t * |
slicer, |
|
|
bool |
more |
|
) |
| |
|
inlinestatic |
Insert block2 option into buffer.
When calling this function to initialize a packet with a block2 option, the more flag must be set to prevent the creation of an option with a length too small to contain the size bit.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), must be < 23 |
[in] | slicer | coap blockwise slicer helper struct |
[in] | more | more flag (1 or 0) |
- Returns
- amount of bytes written to
buf
Definition at line 1209 of file nanocoap.h.
◆ coap_opt_put_block2_control()
static size_t coap_opt_put_block2_control |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
coap_block1_t * |
block |
|
) |
| |
|
inlinestatic |
Insert block2 option into buffer in control usage.
Forces value of block 'more' attribute to zero, per spec.
- Parameters
-
[in] | buf | buffer to write to |
[in] | lastonum | last option number (must be < 27) |
[in] | block | block option attribute struct |
- Returns
- amount of bytes written to
buf
Definition at line 1256 of file nanocoap.h.
◆ coap_opt_put_location_path()
static size_t coap_opt_put_location_path |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
const char * |
location |
|
) |
| |
|
inlinestatic |
Convenience function for inserting LOCATION_PATH option into buffer.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 if first option |
[in] | location | ptr to string holding the location |
- Returns
- amount of bytes written to
buf
Definition at line 1289 of file nanocoap.h.
◆ coap_opt_put_location_query()
static size_t coap_opt_put_location_query |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
const char * |
location |
|
) |
| |
|
inlinestatic |
Convenience function for inserting LOCATION_QUERY option into buffer.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 if first option |
[in] | location | ptr to string holding the location |
- Returns
- amount of bytes written to
buf
Definition at line 1307 of file nanocoap.h.
◆ coap_opt_put_proxy_uri()
static size_t coap_opt_put_proxy_uri |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
const char * |
uri |
|
) |
| |
|
inlinestatic |
Convenience function for inserting PROXY_URI option into buffer.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 if first option |
[in] | uri | ptr to source URI |
- Returns
- amount of bytes written to
buf
Definition at line 1357 of file nanocoap.h.
◆ coap_opt_put_string()
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.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 if first option |
[in] | optnum | option number to use |
[in] | string | string to encode as option |
[in] | separator | character used in string to separate parts |
- Returns
- number of bytes written to
buf
◆ coap_opt_put_uint()
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.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 for first option |
[in] | onum | number of option |
[in] | value | value to encode |
- Returns
- amount of bytes written to
buf
◆ coap_opt_put_uri_path()
static size_t coap_opt_put_uri_path |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
const char * |
uri |
|
) |
| |
|
inlinestatic |
Convenience function for inserting URI_PATH option into buffer.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 if first option |
[in] | uri | ptr to source URI |
- Returns
- amount of bytes written to
buf
Definition at line 1325 of file nanocoap.h.
◆ coap_opt_put_uri_query()
static size_t coap_opt_put_uri_query |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
const char * |
uri |
|
) |
| |
|
inlinestatic |
Convenience function for inserting URI_QUERY option into buffer.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 if first option |
[in] | uri | ptr to source URI |
- Returns
- amount of bytes written to
buf
Definition at line 1341 of file nanocoap.h.
◆ coap_parse()
int coap_parse |
( |
coap_pkt_t * |
pkt, |
|
|
uint8_t * |
buf, |
|
|
size_t |
len |
|
) |
| |
Parse a CoAP PDU.
This function parses a raw CoAP PDU from buf
with size len
and fills the structure pointed to by pkt
. pkt
must point to a preallocated coap_pkt_t structure.
- Parameters
-
[out] | pkt | structure to parse into |
[in] | buf | pointer to raw packet data |
[in] | len | length of packet at buf |
- Returns
- 0 on success
-
<0 on error
◆ coap_payload_advance_bytes()
static void coap_payload_advance_bytes |
( |
coap_pkt_t * |
pkt, |
|
|
size_t |
len |
|
) |
| |
|
inlinestatic |
Advance the payload pointer.
- Precondition
- You added
len
bytes of data to pkt->payload
.
You can add payload to a CoAP request by writing data directly to pkt->payload
. This convenience function takes care of advancing the payload pointer afterwards.
- Parameters
-
[out] | pkt | pkt to which payload was added |
[in] | len | length of payload |
Definition at line 1598 of file nanocoap.h.
◆ coap_payload_put_bytes()
ssize_t coap_payload_put_bytes |
( |
coap_pkt_t * |
pkt, |
|
|
const void * |
data, |
|
|
size_t |
len |
|
) |
| |
Add payload data to the CoAP request.
- Precondition
- coap_opt_finish must have been called before with the COAP_OPT_FINISH_PAYLOAD option.
The function copies data
into the payload buffer of pkt
and advances the payload pointer.
This is just a convenience function, you can also directly write to pkt->payload
if you have a function that outputs payload to a buffer. In this case you should instead call coap_payload_advance_bytes.
- Parameters
-
[out] | pkt | pkt to add payload to |
[in] | data | payload data |
[in] | len | length of payload |
- Returns
- number of payload bytes added on success
-
< 0 on error
◆ coap_payload_put_char()
ssize_t coap_payload_put_char |
( |
coap_pkt_t * |
pkt, |
|
|
char |
c |
|
) |
| |
Add a single character to the payload data of the CoAP request.
This function is used to add single characters to a CoAP payload data. It checks whether the character can be added to the buffer and ignores if the payload area is already exhausted.
- Parameters
-
[out] | pkt | pkt to add payload to |
[in] | c | character to write |
- Returns
- number of payload bytes added on success (always one)
-
< 0 on error
◆ coap_pkt_init()
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.
- Precondition
- buf CoAP header already initialized
- Postcondition
- pkt.flags all zeroed
-
pkt.payload points to first byte after header
-
pkt.payload_len set to maximum space available for options + payload
- Parameters
-
[out] | pkt | pkt to initialize |
[in] | buf | buffer to write for pkt, with CoAP header already initialized |
[in] | len | length of buf |
[in] | header_len | length of header in buf, including token |
◆ coap_put_block1_ok()
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)
This function is wrapper around coap_put_option_block1(), taking its arguments from a coap_block1_t struct.
It will write option Nr. 27 (COAP_OPT_BLOCK1).
It is safe to be called when block1
was generated for a non-blockwise request.
- Parameters
-
[in] | pkt_pos | buffer to write to |
[in] | block1 | ptr to block1 struct (created by coap_get_block1()) |
[in] | lastonum | last option number (must be < 27) |
- Returns
- amount of bytes written to
pkt_pos
◆ coap_put_option()
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.
This function writes a CoAP option with nr. onum
to buf
. It handles calculating the option delta (from lastonum
), encoding the length from olen
and copying the option data from odata
.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 for first option |
[in] | onum | number of option |
[in] | odata | ptr to raw option data (or NULL) |
[in] | olen | length of odata (if any) |
- Returns
- amount of bytes written to
buf
◆ coap_put_option_block1()
static size_t coap_put_option_block1 |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
unsigned |
blknum, |
|
|
unsigned |
szx, |
|
|
int |
more |
|
) |
| |
|
inlinestatic |
Insert block1 option into buffer.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), must be < 27 |
[in] | blknum | block number |
[in] | szx | SXZ value |
[in] | more | more flag (1 or 0) |
- Returns
- amount of bytes written to
buf
Definition at line 1413 of file nanocoap.h.
◆ coap_put_option_ct()
static size_t coap_put_option_ct |
( |
uint8_t * |
buf, |
|
|
uint16_t |
lastonum, |
|
|
uint16_t |
content_type |
|
) |
| |
|
inlinestatic |
Insert content type option into buffer.
- Parameters
-
[out] | buf | buffer to write to |
[in] | lastonum | number of previous option (for delta calculation), or 0 if first option |
[in] | content_type | content type to set |
- Returns
- amount of bytes written to
buf
Definition at line 1430 of file nanocoap.h.
◆ coap_reply_simple()
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)
This is a simple wrapper that allows for building CoAP replies for simple use-cases.
The reply will be written to buf
. If payload
and payload_len
are non-zero, the payload will be copied into the resulting reply packet.
- Parameters
-
[in] | pkt | packet to reply to |
[in] | code | reply code (e.g., COAP_CODE_204) |
[out] | buf | buffer to write reply to |
[in] | len | size of buf |
[in] | ct | content type of payload |
[in] | payload | ptr to payload |
[in] | payload_len | length of payload |
- Returns
- size of reply packet on success
-
<0 on error
-
-ENOSPC if
buf
too small
◆ coap_szx2size()
static unsigned coap_szx2size |
( |
unsigned |
szx | ) |
|
|
inlinestatic |
Helper to decode SZX value to size in bytes.
- Parameters
-
[in] | szx | SZX value to decode |
- Returns
- SZX value decoded to bytes
Definition at line 834 of file nanocoap.h.
◆ coap_tree_handler()
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.
This function will try to find a matching handler in resources
and call the handler.
- Parameters
-
[in] | pkt | pointer to (parsed) CoAP packet |
[out] | resp_buf | buffer for response |
[in] | resp_buf_len | size of response buffer |
[in] | resources | Array of coap endpoint resources |
[in] | resources_numof | length of the coap endpoint resources |
- Returns
- size of the reply packet on success
-
<0 on error