A CoAP message. More...
#include <ProsecCoAP.h>
Public Member Functions | |
| void | setId (uint16_t id) |
| Set the message ID. | |
| Message () | |
| Builds a default CoAP message. | |
| Message (MessageType type, MessageCode code) | |
| Builds a CoAP message with the given type and code. | |
| Message (MessageType type, MessageCode code, uint16_t id) | |
| Builds a CoAP message explictly specifying type, code and message ID. | |
| const uint8_t * | asRaw () const |
| Get the raw binary representation of the message. | |
| ErrorCode | intoResponse (const Message &request, MessageCode code, MessageType type) |
| Convert the message into a response to the given request. | |
| ErrorCode | intoResponse (const Message &request, MessageCode code) |
| Convert the message into an ACK response to the given request. | |
| uint8_t | getVersion () const |
| Get the CoAP version of this message. | |
| size_t | getLength () const |
| Get the message length. | |
| void | setType (MessageType type) |
| Set the message type. | |
| MessageType | getType () const |
| Get the message type. | |
| void | setCode (MessageCode code) |
| Set the message code. | |
| MessageCode | getCode () const |
| Get the message code. | |
| uint16_t | getId () const |
| Get the message ID. | |
| size_t | getTokenLength () const |
| Get the current token length. | |
| ErrorCode | setToken (const uint8_t *token, size_t length) |
| Sets the token of the message with the given value and length. | |
| ErrorCode | addRandomToken (size_t length) |
| Add a token of the given length to the message. | |
| const uint8_t * | getToken () const |
| Get the pointer to the current token. | |
| bool | matchesToken (const uint8_t *token, size_t length) const |
| Check if the message token matches the given token and length. | |
| OptionIterator | getOptionIterator () const |
| Return an iterator over the message options. | |
| ErrorCode | getOption (OptionNumber number, Option &option) const |
| Return the first occurrence of the specified option. | |
| ErrorCode | addOption (OptionNumber number, const uint8_t *value, size_t length) |
| Add an option to the message. | |
| ErrorCode | addOption (Option option) |
| Add an option to the message. | |
| ErrorCode | addHost (IPAddress ip) |
| Add the Uri-Host option to the message. | |
| ErrorCode | addPort (uint16_t port) |
| Add the Uri-Port option to the message. | |
| ErrorCode | addPath (const char *path) |
| Add the URI path and query to the message. | |
| ErrorCode | getPath (String &path) const |
| Retrieve all the URI path and URI query options from the message and concatenate them into a single string. | |
| ErrorCode | getPayload (const uint8_t *&payload, size_t &length) const |
| Get the payload from the message. | |
| ErrorCode | addPayload (const uint8_t *payload, size_t length) |
| Add a payload to the message. | |
| ErrorCode | addPayload (const uint8_t *payload, size_t length, ContentFormat format) |
| Add a payload and the Content-Format option to the message. | |
| ErrorCode | getObserveValue (uint32_t &observeValue) |
| Extract the Observe option value, if present. | |
| ErrorCode | getMaxAge (uint32_t &age) |
| Get the MAX_AGE option value from the message. | |
| ErrorCode | setMaxAge (uint32_t age) |
| Add the MAX_AGE option to the message. | |
| bool | isObserveRegister () |
| Check if the message is an Observe register GET request. | |
| bool | isObserveDeregister () |
| Check if the message is an Observe deregister GET request. | |
| ErrorCode | intoNotification (Observer &observer) |
| Convert the message into a notification message for the given observer. | |
Static Public Member Functions | |
| static ErrorCode | fromUdp (UDP *udp, Message &message) |
| Build a CoAP message reading from a UDP instance. | |
A CoAP message.
This is a view on the binary representation of a CoAP message. The intended use is to build a CoAP message to be sent, or to parse a received CoAP message.
As a minimum, a Message instance has a valid header.
|
inline |
Builds a default CoAP message.
The version is set to COAP_VERSION. The type is set to COAP_NON. The code is set to COAP_EMPTY. The token length is set to 0. The message ID is assigned automatically.
|
inline |
Builds a CoAP message with the given type and code.
The version is set to COAP_VERSION. The token length is set to 0. The message ID is assigned automatically.
| type | The message type. |
| code | The message code. |
| Coap::Message::Message | ( | MessageType | type, |
| MessageCode | code, | ||
| uint16_t | id ) |
Builds a CoAP message explictly specifying type, code and message ID.
The version is set to COAP_VERSION. The token length is set to 0.
| type | The message type. |
| code | The message code. |
| id | The message ID. |
| ErrorCode Coap::Message::addHost | ( | IPAddress | ip | ) |
Add the Uri-Host option to the message.
It converts the given IP address to string format and adds it as the Uri-Host option, as per RFC 7252 Section 6.4. The Uri-Host option is not compulsory. As per Section 6.5, if not present, the the destination IP address where the message is being sent will be used.
| ip | The IP address to set as the Uri-Host. |
Add an option to the message.
| ErrorCode Coap::Message::addOption | ( | OptionNumber | number, |
| const uint8_t * | value, | ||
| size_t | length ) |
Add an option to the message.
The option is added according to the CoAP option encoding rules. For options that can be added at most once, this function follows a "first add wins" policy. Any subsequent addition of the same option number will return ErrorCode::NOT_SUPPORTED. For options that can appear multiple times, this function appends the new option to the existing ones.
If adding the option will result in exceeding the limits specified by RFC 7252 Section 5.10, the error code ErrorCode::NOT_SUPPORTED is returned. For options that can appear multiple times, the option is appended after the existing ones.
Prefer using specialized methods for common options like COAP_CONTENT_FORMAT, COAP_URI_PATH, or COAP_URI_QUERY when available.
| number | Option number, as defined in the CoAP specification. |
| value | Pointer to the option value. |
| length | Length of the option value. |
| ErrorCode Coap::Message::addPath | ( | const char * | path | ) |
Add the URI path and query to the message.
It follows section "Decomposing URIs into Options" https://datatracker.ietf.org/doc/html/rfc7252#section-6.4 to encode the path into the necessary Uri-Path and Uri-Query options.
| path | The URI path + query associated with the recipient, null terminated. Initial and trailing slashes are ignored. Valid examples are: sensors/temp /sensors/temp?unit=celsius ` Individual Uri-Path and Uri-Query segments cannot exceed 255 bytes, as per specification. |
| ErrorCode Coap::Message::addPayload | ( | const uint8_t * | payload, |
| size_t | length ) |
Add a payload to the message.
If a payload is already present, this function will return an error.
| payload | Pointer to the payload data. |
| length | Length of the payload data. |
| format | The content format of the payload. |
| ErrorCode Coap::Message::addPayload | ( | const uint8_t * | payload, |
| size_t | length, | ||
| ContentFormat | format ) |
Add a payload and the Content-Format option to the message.
If a payload or a Content-Format option are already present, this function will return an error.
| payload | Pointer to the payload data. |
| length | Length of the payload data. |
| format | The content format of the payload. |
| ErrorCode Coap::Message::addPort | ( | uint16_t | port | ) |
Add the Uri-Port option to the message.
It adds the Uri-Port option as per RFC 7252 Section 6.4. The Uri-Port option is not compulsory. As per Section 6.5, if not present, the the default CoAP port COAP_DEFAULT_PORT will be used.
Any existing Uri-Port option is removed before adding the new one.
| port | The port number to set as the Uri-Port. |
| ErrorCode Coap::Message::addRandomToken | ( | size_t | length | ) |
Add a token of the given length to the message.
A random token is generated and added to the message as per specifications. If present, the previous token is replaced.
The token is an optional field in a CoAP message. It is intended for use as a client-local identifier for differentiating between concurrent requests.
| length | The length (in bytes) of the token. The maximum length is COAP_MAX_TOKEN_LENGTH bytes. |
Example:
|
inline |
Get the raw binary representation of the message.
The returned pointer is valid as long as the message exists.
Build a CoAP message reading from a UDP instance.
| udp | A pointer to the UDP instance containing the received message. |
| message | The CoAP message to populate. |
| MessageCode Coap::Message::getCode | ( | ) | const |
Get the message code.
The code is always present in a CoAP message.
| uint16_t Coap::Message::getId | ( | ) | const |
Get the message ID.
The message ID is always present in a CoAP message.
|
inline |
Get the message length.
| ErrorCode Coap::Message::getMaxAge | ( | uint32_t & | age | ) |
Get the MAX_AGE option value from the message.
The MAX_AGE option indicates the maximum age of the resource representation in seconds, as per RFC 7252 Section 5.10.1. This is used for caching purposes.
If the MAX_AGE option is not present in the message, the default value of 60 seconds is returned, as per RFC 7252 Section 5.10.1: "If the Max-Age option is not present, the default value is 60 seconds."
| [out] | age | The output parameter to store the MAX_AGE option value in seconds. |
| ErrorCode Coap::Message::getObserveValue | ( | uint32_t & | observeValue | ) |
Extract the Observe option value, if present.
On a request, the observe value is either the register or deregister value, ObserveValue. On a notification, the observe value is a 24-bit sequential number that is incremented for each notification sent to an observer.
| [out] | observeValue | The output parameter to store the Observe option value. The Observe option value is a 24-bit unsigned integer, but it is stored in a 32-bit variable. The value is valid only if the function returns ErrorCode::OK. |
| ErrorCode Coap::Message::getOption | ( | OptionNumber | number, |
| Option & | option ) const |
Return the first occurrence of the specified option.
| number | The option number to search for. | |
| [out] | option | The output parameter to store the option if found. |
| OptionIterator Coap::Message::getOptionIterator | ( | ) | const |
Return an iterator over the message options.
| ErrorCode Coap::Message::getPath | ( | String & | path | ) | const |
Retrieve all the URI path and URI query options from the message and concatenate them into a single string.
| [out] | path | The String object where the path (and query) will be stored. |
Example usage:
| ErrorCode Coap::Message::getPayload | ( | const uint8_t *& | payload, |
| size_t & | length ) const |
Get the payload from the message.
The payload is a raw set of bytes. To interpret it, refer to the Content-Format option, if present.
| [out] | payload | Pointer to the payload within the message. |
| [out] | length | The payload length. |
Example:
| const uint8_t * Coap::Message::getToken | ( | ) | const |
Get the pointer to the current token.
| [out] | buffer | Pointer to the token within the message. |
| [out] | length | The token length. |
Example:
| size_t Coap::Message::getTokenLength | ( | ) | const |
Get the current token length.
| MessageType Coap::Message::getType | ( | ) | const |
Get the message type.
The type is always present in a CoAP message.
|
inline |
Get the CoAP version of this message.
The version is always present in a CoAP message. Messages built with the library should always have version COAP_VERSION.
Convert the message into a notification message for the given observer.
The message will be converted to notification, modifying:
| observer | The observer to notify. The observer incremental value will be updated accordingly. |
|
inline |
Convert the message into an ACK response to the given request.
| request | The request message. |
| code | The response message code. |
| ErrorCode Coap::Message::intoResponse | ( | const Message & | request, |
| MessageCode | code, | ||
| MessageType | type ) |
Convert the message into a response to the given request.
The message ID is copied from the request. The type is set to MessageType::ACK. The response code is set to the given input code. If the request has a token, the same token is copied to the response.
| request | The request message. |
| code | The response message code. |
| type | The response message type. |
| bool Coap::Message::isObserveDeregister | ( | ) |
Check if the message is an Observe deregister GET request.
A client may explicitly cancel an observation relationship by sending a GET request with the Observe option set to ObserveValue::DEREGISTER.
The caller has the responsibility to cancel the observer if all other conditions are met (e.g. Uri-Path, token, etc.).
| bool Coap::Message::isObserveRegister | ( | ) |
Check if the message is an Observe register GET request.
As per https://datatracker.ietf.org/doc/html/rfc7641#section-3.1 an Observe register request is a GET request that includes the Observe option with the value ObserveValue::REGISTER.
The caller has the responsibility to register the observer to the specific resource.
| bool Coap::Message::matchesToken | ( | const uint8_t * | token, |
| size_t | length ) const |
Check if the message token matches the given token and length.
| token | Pointer to the token to match. |
| length | Length of the token to match in bytes. |
| void Coap::Message::setCode | ( | MessageCode | code | ) |
Set the message code.
| code | The message code to set. |
| void Coap::Message::setId | ( | uint16_t | id | ) |
Set the message ID.
Writes on byte 2 and 3 of the message to set the message ID.
| id | The message ID to set. |
| ErrorCode Coap::Message::setMaxAge | ( | uint32_t | age | ) |
Add the MAX_AGE option to the message.
The MAX_AGE option indicates the maximum age of the resource representation in seconds, as per RFC 7252 Section 5.10.1. This is used for caching purposes.
If a MAX_AGE option is already present, this function will return an error.
| age | The maximum age value in seconds. |
| ErrorCode Coap::Message::setToken | ( | const uint8_t * | token, |
| size_t | length ) |
Sets the token of the message with the given value and length.
Any existing token is removed before setting the new one.
| token | Pointer to the token data. |
| length | Length of the token in bytes. |
The token matches the request to the response (application level).
| void Coap::Message::setType | ( | MessageType | type | ) |
Set the message type.
| type | The message type to set. |