Nanocoap small CoAP library¶
CoAP library optimized for minimal resource usage.
nanocoap provides a granular, low-level interface for writing CoAP messages via RIOT’s sock networking API.
Server Operation¶
See the nanocoap_server example, which is built on the nanocoap_sock.h::nanocoap_server()
function. A server must define an array of coap_resource_t resources for which it responds. See the declarations of coap_resources
and coap_resources_numof
. The array contents must be ordered by the resource path, specifically the ASCII encoding of the path characters (digit and capital precede lower case). nanocoap provides the COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER entry for /.well-known/core
.
Handler functions¶
For each resource, you must implement a nanocoap.h::coap_handler_t
handler function. nanocoap provides functions to help implement the handler. If the handler is called via nanocoap_sock.h::nanocoap_server()
, the response buffer provided to the handler reuses the buffer for the request. So, your handler must read the request thoroughly before writing the response.
To read the request, use the coap_get_xxx() functions to read the header and options. Use the coap_opt_get_xxx() functions to read an option generically by data type. If the pkt payload_len attribute is a positive value, start to read it at the payload pointer attribute.
If a response does not require specific CoAP options, use nanocoap.h::coap_reply_simple()
. If there is a payload, it writes a Content-Format option with the provided value.
For a response with additional CoAP options, start by calling nanocoap.h::coap_build_reply()
. Then choose either the minimal API or the struct-based API to write the rest of the response. See the instructions in the section Write Options and Payload below.
Handler functions¶
For each resource, you must implement a nanocoap.h::coap_handler_t
handler function. nanocoap provides functions to help implement the handler. If the handler is called via nanocoap_sock.h::nanocoap_server()
, the response buffer provided to the handler reuses the buffer for the request. So, your handler must read the request thoroughly before writing the response.
To read the request, use the coap_get_xxx() functions to read the header and options. Use the coap_opt_get_xxx() functions to read an option generically by data type. If the pkt payload_len attribute is a positive value, start to read it at the payload pointer attribute.
If a response does not require specific CoAP options, use nanocoap.h::coap_reply_simple()
. If there is a payload, it writes a Content-Format option with the provided value.
For a response with additional CoAP options, start by calling nanocoap.h::coap_build_reply()
. Then choose either the minimal API or the struct-based API to write the rest of the response. See the instructions in the section Write Options and Payload below.
Client Operation¶
Choose either the minimal API or the struct-based API to write a request. Follow the instructions in the section Write Options and Payload below.
To send the message and await the response, see nanocoap_sock.h::nanocoap_request()
as well as nanocoap_sock.h::nanocoap_get()
, which additionally copies the response payload to a user supplied buffer. Finally, read the response as described above in the server Handler functions section for reading a request.
Write Options and Payload¶
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 provides two APIs for writing CoAP options:
- minimal API requires only a reference to the buffer for the message. However, the caller must provide the last option number written as well as the buffer position.
- struct-based API uses a coap_pkt_t struct to conveniently track each option as it is written and prepare for any payload.
You must use one API exclusively for a given message. For either API, the caller must write options in order by option number (see “CoAP option numbers” in CoAP defines).
Minimal API¶
Before starting, ensure the CoAP header has been initialized with nanocoap.h::coap_build_hdr()
. For a response, nanocoap.h::coap_build_reply()
includes a call to nanocoap.h::coap_build_hdr()
. Use the returned length to track the next position in the buffer to write and remaining length.
Next, use the coap_opt_put_xxx() and coap_put_xxx() functions to write each option. These functions require the position in the buffer to start writing, and return the number of bytes written.
If there is a payload, append a payload marker (0xFF). Then write the payload to within the maximum length remaining in the buffer.
Struct-based API¶
As with the minimal API, first ensure the CoAP header has been initialized with nanocoap.h::coap_build_hdr()
. Then use nanocoap.h::coap_pkt_init()
to initialize the coap_pkt_t struct.
Next, use the coap_opt_add_xxx() functions to write each option, like nanocoap.h::coap_opt_add_uint()
. When all options have been added, call nanocoap.h::coap_opt_finish()
.
Finally, write any message payload at the coap_pkt_t payload pointer attribute. The payload_len attribute provides the available length in the buffer. The option functions keep these values current as they are used.
Minimal API¶
Before starting, ensure the CoAP header has been initialized with nanocoap.h::coap_build_hdr()
. For a response, nanocoap.h::coap_build_reply()
includes a call to nanocoap.h::coap_build_hdr()
. Use the returned length to track the next position in the buffer to write and remaining length.
Next, use the coap_opt_put_xxx() and coap_put_xxx() functions to write each option. These functions require the position in the buffer to start writing, and return the number of bytes written.
If there is a payload, append a payload marker (0xFF). Then write the payload to within the maximum length remaining in the buffer.
Struct-based API¶
As with the minimal API, first ensure the CoAP header has been initialized with nanocoap.h::coap_build_hdr()
. Then use nanocoap.h::coap_pkt_init()
to initialize the coap_pkt_t struct.
Next, use the coap_opt_add_xxx() functions to write each option, like nanocoap.h::coap_opt_add_uint()
. When all options have been added, call nanocoap.h::coap_opt_finish()
.
Finally, write any message payload at the coap_pkt_t payload pointer attribute. The payload_len attribute provides the available length in the buffer. The option functions keep these values current as they are used.
Create a Block-wise Response (Block2)¶
Block-wise is a CoAP extension (RFC 7959) to divide a large payload across multiple physical packets. This section describes how to write a block-wise payload for a response, and is known as Block2. (Block1 is for a block-wise payload in a request.) See _riot_board_handler() in the nanocoap_server example for an example handler implementation.
Start with nanocoap.h::coap_block2_init()
to read the client request and initialize a coap_slicer_t struct with the size and location for this slice of the overall payload. Then write the block2 option in the response with nanocoap.h::coap_opt_put_block2()
. The option includes an indicator (“more”) that a slice completes the overall payload transfer. You may not know the value for more at this point, but you must initialize the space in the packet for the option before writing the payload. The option is rewritten later.
Next, use the coap_blockwise_put_xxx() functions to write the payload content. These functions use the coap_block_slicer_t to enable or disable actually writing the content, depending on the current position within the overall payload transfer.
Finally, use the convenience function nanocoap.h::coap_block2_build_reply()
, which finalizes the packet and calls nanocoap.h::coap_block2_finish()
internally to update the block2 option.
-
COAP_GET
¶ 1
(0x1)
-
COAP_POST
¶ 1
(0x2)
-
COAP_PUT
¶ 1
(0x4)
-
COAP_DELETE
¶ 1
(0x8)
-
NANOCOAP_NOPTS_MAX
¶ 1
(16)
-
NANOCOAP_URI_MAX
¶ 1
(64)
-
NANOCOAP_BLOCK_SIZE_EXP_MAX
¶ Maximum size for a blockwise transfer as power of 2.
1
(6)
-
COAP_OPT_FINISH_NONE
¶ no special handling required
1
(0x0000)
-
COAP_OPT_FINISH_PAYLOAD
¶ expect a payload to follow
1
(0x0001)
-
msp430_types.h::ssize_t
(*coap_handler_t
()¶ Resource handler type.
-
const coap_resource_t
coap_resources
()¶ Global CoAP resource list.
-
const unsigned
coap_resources_numof
¶ Number of entries in global CoAP resource list.
-
int
coap_parse
(coap_pkt_t * pkt, uint8_t * buf,msp430_types.h::size_t
len)¶ Parse a CoAP PDU.
This function parses a raw CoAP PDU from
buf
with sizelen
and fills the structure pointed to bypkt
.pkt
must point to a preallocated coap_pkt_t structure.Parameters
pkt: structure to parse into buf: pointer to raw packet data len: length of packet at buf
Return values
- 0 on success
- <0 on error
-
msp430_types.h::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.Parameters
pkt: packet to reply to code: reply code (e.g., COAP_CODE_204) rbuf: buffer to write reply to rlen: size of rbuf
payload_len: length of payload Return values
- size of reply packet on success
- <0 on error
-
msp430_types.h::ssize_t
coap_reply_simple
(coap_pkt_t * pkt, unsigned code, uint8_t * buf,msp430_types.h::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
. Ispayload
andpayload_len
non-zero, the payload will be copied into the resulting reply packet.Parameters
pkt: packet to reply to code: reply code (e.g., COAP_CODE_204) buf: buffer to write reply to len: size of buf
ct: content type of payload payload: ptr to payload payload_len: length of payload Return values
- size of reply packet on success
- <0 on error
-
msp430_types.h::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
pkt: pointer to (parsed) CoAP packet resp_buf: buffer for response resp_buf_len: size of response buffer Return values
- size of reply packet on success
- <0 on error
-
msp430_types.h::ssize_t
coap_build_hdr
(coap_hdr_t * hdr, unsigned type, uint8_t * token,msp430_types.h::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
hdr: hdr to fill type: CoAP packet type (e.g., COAP_TYPE_CON, …) token: token token_len: length of token
code: CoAP code (e.g., COAP_CODE_204, …) id: CoAP request id Return values
- length of resulting header
-
void
coap_pkt_init
(coap_pkt_t * pkt, uint8_t * buf,msp430_types.h::size_t
len,msp430_types.h::size_t
header_len)¶ Initialize a packet struct, to build a message buffer.
Parameters
pkt: pkt to initialize buf: buffer to write for pkt, with CoAP header already initialized len: length of buf header_len: length of header in buf, including token
-
msp430_types.h::size_t
coap_put_option
(uint8_t * buf, uint16_t lastonum, uint16_t onum, uint8_t * odata,msp430_types.h::size_t
olen)¶ Insert a CoAP option into buffer.
This function writes a CoAP option with nr.
onum
tobuf
. It handles calculating the option delta (fromlastonum
), encoding the length fromolen
and copying the option data fromodata
.Parameters
buf: buffer to write to lastonum: number of previous option (for delta calculation), or 0 for first option onum: number of option odata: ptr to raw option data (or NULL) olen: length of odata
(if any)Return values
- amount of bytes written to
buf
- amount of bytes written to
-
msp430_types.h::size_t
coap_put_option_ct
(uint8_t * buf, uint16_t lastonum, uint16_t content_type)¶ Insert content type option into buffer.
Parameters
buf: buffer to write to lastonum: number of previous option (for delta calculation), or 0 if first option content_type: content type to set Return values
- amount of bytes written to
buf
- amount of bytes written to
-
msp430_types.h::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
buf: buffer to write to lastonum: number of previous option (for delta calculation), or 0 if first option optnum: option number to use string: string to encode as option separator: character used in string
to separate partsReturn values
- number of bytes written to
buf
- number of bytes written to
-
msp430_types.h::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.
Parameters
buf: buffer to write to lastonum: number of previous option (for delta calculation), or 0 if first option uri: ptr to source URI Return values
- amount of bytes written to
buf
- amount of bytes written to
-
msp430_types.h::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.
Parameters
buf: buffer to write to lastonum: number of previous option (for delta calculation), or 0 if first option uri: ptr to source URI Return values
- amount of bytes written to
buf
- amount of bytes written to
-
msp430_types.h::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.
Parameters
buf: buffer to write to lastonum: number of previous option (for delta calculation), or 0 if first option location: ptr to string holding the location Return values
- amount of bytes written to
buf
- amount of bytes written to
-
msp430_types.h::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.
Parameters
buf: buffer to write to lastonum: number of previous option (for delta calculation), or 0 if first option location: ptr to string holding the location Return values
- amount of bytes written to
buf
- amount of bytes written to
-
int
coap_get_blockopt
(coap_pkt_t * pkt, uint16_t option, uint32_t * blknum, unsigned * szx)¶ Generic block option getter.
Parameters
pkt: pkt to work on option: actual block option number to get blknum: block number szx: SZX value Return values
- -1 if option not found
- 0 if more flag is not set
- 1 if more flag is set
-
int
coap_get_block1
(coap_pkt_t * pkt, coap_block1_t * 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 inblock1
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
pkt: pkt to work on block1: ptr to preallocated coap_block1_t structure Return values
- 0 if block1 option not present
- 1 if structure has been filled
-
int
coap_get_block2
(coap_pkt_t * pkt, coap_block1_t * block2)¶ Block2 option getter.
Parameters
pkt: pkt to work on block2: ptr to preallocated coap_block1_t structure Return values
- 0 if block2 option not present
- 1 if structure has been filled
-
msp430_types.h::size_t
coap_put_option_block1
(uint8_t * buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more)¶ Insert block1 option into buffer.
Parameters
buf: buffer to write to lastonum: number of previous option (for delta calculation), must be < 27 blknum: block number szx: SXZ value more: more flag (1 or 0) Return values
- amount of bytes written to
buf
- amount of bytes written to
-
msp430_types.h::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
nanocoap.h::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
pkt_pos: buffer to write to block1: ptr to block1 struct (created by nanocoap.h::coap_get_block1()
)lastonum: last option number (must be < 27) Return values
- amount of bytes written to
pkt_pos
- amount of bytes written to
-
msp430_types.h::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.
Use separator to split string into multiple options.
Parameters
pkt: pkt referencing target buffer optnum: option number to use string: string to encode as option separator: character used in string
to separate partsReturn values
- number of bytes written to buffer
- -ENOSPC if no available options
-
msp430_types.h::ssize_t
coap_opt_add_uint
(coap_pkt_t * pkt, uint16_t optnum, uint32_t value)¶ Encode the given uint option into pkt.
Parameters
pkt: pkt referencing target buffer optnum: option number to use value: uint to encode Return values
- number of bytes written to buffer
- <0 reserved for error but not implemented yet
-
msp430_types.h::ssize_t
coap_opt_finish
(coap_pkt_t * pkt, uint16_t flags)¶ Finalizes options as required and prepares for payload.
Parameters
pkt: pkt to update flags: see COAP_OPT_FINISH… macros Return values
- total number of bytes written to buffer
-
msp430_types.h::size_t
coap_opt_put_block2
(uint8_t * buf, uint16_t lastonum, coap_block_slicer_t * slicer, bool more)¶ 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
buf: buffer to write to lastonum: number of previous option (for delta calculation), must be < 23 slicer: coap blockwise slicer helper struct more: more flag (1 or 0) Return values
- amount of bytes written to
buf
- amount of bytes written to
-
unsigned
coap_get_content_type
(coap_pkt_t * pkt)¶ Get content type from packet.
Parameters
pkt: packet to work on Return values
- the packet’s content type value if included, COAP_FORMAT_NONE otherwise
-
msp430_types.h::ssize_t
coap_opt_get_string
(const coap_pkt_t * pkt, uint16_t optnum, uint8_t * target,msp430_types.h::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
pkt: packet to read from optnum: absolute option number target: target buffer max_len: size of target
separator: character used for separating the option parts Return values
- -ENOSPC if the complete option does not fit into
target
- nr of bytes written to
target
(including ‘\0’)
- -ENOSPC if the complete option does not fit into
-
msp430_types.h::ssize_t
coap_get_uri_path
(const coap_pkt_t * pkt, uint8_t * target)¶ 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 NANOCOAP_URI_MAX bytes!Parameters
pkt: pkt to work on target: buffer for target URI Return values
- -ENOSPC if URI option is larger than NANOCOAP_URI_MAX
- nr of bytes written to
target
(including ‘\0’)
-
msp430_types.h::ssize_t
coap_get_uri_query
(const coap_pkt_t * pkt, uint8_t * target)¶ 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 NANOCOAP_URI_MAX bytes!Parameters
pkt: pkt to work on target: buffer for target URI Return values
- -ENOSPC if URI option is larger than NANOCOAP_URI_MAX
- nr of bytes written to
target
(including ‘\0’)
-
msp430_types.h::ssize_t
coap_get_location_path
(const coap_pkt_t * pkt, uint8_t * target,msp430_types.h::size_t
max_len)¶ 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
pkt: pkt to work on target: buffer for location path max_len: size of target
in bytesReturn values
- -ENOSPC if URI option is larger than
max_len
- nr of bytes written to
target
(including ‘\0’)
- -ENOSPC if URI option is larger than
-
msp430_types.h::ssize_t
coap_get_location_query
(const coap_pkt_t * pkt, uint8_t * target,msp430_types.h::size_t
max_len)¶ 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
pkt: pkt to work on target: buffer for location path max_len: size of target
in bytesReturn values
- -ENOSPC if URI option is larger than
max_len
- nr of bytes written to
target
(including ‘\0’)
- -ENOSPC if URI option is larger than
-
void
coap_block2_init
(coap_pkt_t * pkt, coap_block_slicer_t * slicer)¶ 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
pkt: packet to work on slicer: Preallocated slicer struct to fill
-
void
coap_block2_finish
(coap_block_slicer_t * slicer)¶ 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 overwrites bytes in the packet, it doesn’t add new bytes to the packet.Parameters
slicer: Preallocated slicer struct to use
-
msp430_types.h::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
nanocoap.h::coap_build_reply()
, this function checks the block2 option and returns an error message to the client if necessary.Parameters
pkt: packet to reply to code: reply code (e.g., COAP_CODE_204) rbuf: buffer to write reply to rlen: size of rbuf
payload_len: length of payload slicer: slicer to use Return values
- size of reply packet on success
- <0 on error
-
msp430_types.h::size_t
coap_blockwise_put_char
(coap_block_slicer_t * slicer, uint8_t * bufpos, char c)¶ 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
slicer: slicer to use bufpos: pointer to the current payload buffer position c: character to write Return values
- Number of bytes writen to
bufpos
- Number of bytes writen to
-
msp430_types.h::size_t
coap_blockwise_put_bytes
(coap_block_slicer_t * slicer, uint8_t * bufpos, const uint8_t * c,msp430_types.h::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
slicer: slicer to use bufpos: pointer to the current payload buffer position c: byte array to copy len: length of the byte array Return values
- Number of bytes writen to
bufpos
- Number of bytes writen to
-
unsigned
coap_szx2size
(unsigned szx)¶ Helper to decode SZX value to size in bytes.
Parameters
szx: SZX value to decode Return values
- SZX value decoded to bytes
-
unsigned
coap_get_ver
(coap_pkt_t * pkt)¶ Get the CoAP version number.
Parameters
pkt: CoAP packet Return values
- CoAP version number
-
unsigned
coap_get_type
(coap_pkt_t * pkt)¶ Get the message type.
Parameters
pkt: CoAP packet Return values
- COAP_TYPE_CON
- COAP_TYPE_NON
- COAP_TYPE_ACK
- COAP_TYPE_RST
-
unsigned
coap_get_token_len
(coap_pkt_t * pkt)¶ Get a message’s token length [in byte].
Parameters
pkt: CoAP packet Return values
- length of token in the given message (0-8 byte)
-
unsigned
coap_get_code_class
(coap_pkt_t * pkt)¶ Get a message’s code class (3 most significant bits of code)
Parameters
pkt: CoAP packet Return values
- message code class
-
unsigned
coap_get_code_detail
(coap_pkt_t * pkt)¶ Get a message’s code detail (5 least significant bits of code)
Parameters
pkt: CoAP packet Return values
- message code detail
-
unsigned
coap_get_code_raw
(coap_pkt_t * pkt)¶ Get a message’s raw code (class + detail)
Parameters
pkt: CoAP packet Return values
- raw message code
-
unsigned
coap_get_code
(coap_pkt_t * pkt)¶ Get a message’s code in decimal format ((class * 100) + detail)
Parameters
pkt: CoAP packet Return values
- message code in decimal format
-
unsigned
coap_get_id
(coap_pkt_t * pkt)¶ Get the message ID of the given CoAP packet.
Parameters
pkt: CoAP packet Return values
- message ID
-
uint8_t *
coap_hdr_data_ptr
(coap_hdr_t * hdr)¶ Get the start of data after the header.
Parameters
hdr: Header of CoAP packet in contiguous memory Return values
- pointer to first byte after the header
-
unsigned
coap_get_total_hdr_len
(coap_pkt_t * pkt)¶ Get the total header length (4-byte header + token length)
Parameters
pkt: CoAP packet Return values
- total header length
-
uint8_t
coap_code
(unsigned cls, unsigned detail)¶ Encode given code class and code detail to raw code.
Parameters
cls: message code class detail: message code detail Return values
- raw message code
-
void
coap_hdr_set_code
(coap_hdr_t * hdr, uint8_t code)¶ Write the given raw message code to given CoAP header.
Parameters
hdr: CoAP header to write to code: raw message code
-
void
coap_hdr_set_type
(coap_hdr_t * hdr, unsigned type)¶ Set the message type for the given CoAP header.
Parameters
hdr: CoAP header to write type: message type as integer value [0-3]
-
unsigned
coap_method2flag
(unsigned code)¶ Convert message code (request method) into a corresponding bit field.
Parameters
code: request code denoting the request method Return values
- bit field corresponding to the given request method
-
bool
coap_has_observe
(coap_pkt_t * pkt)¶ Identifies a packet containing an observe option.
Parameters
pkt: CoAP packet Return values
- true if observe value is set
- false if not
-
void
coap_clear_observe
(coap_pkt_t * pkt)¶ Clears the observe option value from a packet.
Parameters
pkt: CoAP packet
-
uint32_t
coap_get_observe
(coap_pkt_t * pkt)¶ Get the value of the observe option from the given packet.
Parameters
pkt: CoAP packet Return values
- value of the observe option
-
msp430_types.h::ssize_t
coap_well_known_core_default_handler
(coap_pkt_t * pkt, uint8_t * buf,msp430_types.h::size_t
len, void * context)¶ Reference to the default .well-known/core handler defined by the application.
-
COAP_FORMAT_NONE
¶ Nanocoap-specific value to indicate no format specified.
1
(UINT16_MAX)
-
COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER
¶ Resource definition for the default .well-known/core handler.
1 2 3 4 5
{ \ .path = "/.well-known/core", \ .methods = COAP_GET, \ .handler = coap_well_known_core_default_handler \ }
-
struct
coap_hdr_t
¶ 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
id
¶ Req/resp ID.
-
uint8_t
-
struct
coap_optpos_t
¶ CoAP option array entry.
-
uint16_t
opt_num
¶ full CoAP option number
-
uint16_t
offset
¶ offset in packet
-
uint16_t
-
struct
coap_pkt_t
¶ CoAP PDU parsing context structure.
-
coap_hdr_t *
hdr
¶ pointer to raw packet
-
uint8_t *
token
¶ pointer to token
-
uint8_t *
payload
¶ pointer to payload
-
uint16_t
payload_len
¶ length of payload
-
uint16_t
options_len
¶ length of options array
-
coap_optpos_t
options
()¶ option offset array
-
coap_hdr_t *
-
struct
coap_resource_t
¶ Type for CoAP resource entry.
-
const char *
path
¶ URI path of resource.
-
unsigned
methods
¶ OR’ed methods this resource allows.
-
nanocoap.h::coap_handler_t
handler
¶ ptr to resource handler
-
void *
context
¶ ptr to user defined context data
-
const char *