ProsecCoAP 🥂
 
Loading...
Searching...
No Matches
Coap::Node Class Reference

The CoAP node that runs on this device. More...

#include <ProsecCoAP.h>

Public Member Functions

 Node (UDP &udp)
 Build a CoAP node using the given UDP instance.
 
 Node (UDP &udp, uint16_t port)
 Build a CoAP node using the given UDP instance and port.
 
uint16_t getPort () const
 Get the local port used by this CoAP node.
 
ErrorCode start ()
 Start the CoAP instance.
 
void setResponseHandler (Callback handler)
 Set the response callback.
 
ErrorCode serve (const char *path, Callback callback)
 Serve a given URI path with the specified callback.
 
ErrorCode loop ()
 Perform a single iteration of the CoAP event loop.
 
ErrorCode sendMessage (const Message &message, IPAddress ip, uint16_t port)
 Send a CoAP message to the specified IP address and port.
 

Detailed Description

The CoAP node that runs on this device.

It uses an underlying UDP instance for communication. It provides methods to send and receive CoAP messages,

See also
Message.

Constructor & Destructor Documentation

◆ Node() [1/2]

Coap::Node::Node ( UDP & udp)
inline

Build a CoAP node using the given UDP instance.

This instance will use the default CoAP port (COAP_DEFAULT_PORT).

Parameters
udpThe UDP instance to use for communication.

◆ Node() [2/2]

Coap::Node::Node ( UDP & udp,
uint16_t port )
inline

Build a CoAP node using the given UDP instance and port.

Parameters
udpThe UDP instance to use for communication.
portThe local UDP port to use for communication.

Member Function Documentation

◆ getPort()

uint16_t Coap::Node::getPort ( ) const
inline

Get the local port used by this CoAP node.

Returns
The local UDP port number.

◆ loop()

ErrorCode Coap::Node::loop ( )

Perform a single iteration of the CoAP event loop.

This method MUST be called in the main loop() of your application.

It is responsible for processing incoming messages, handling retransmissions, and invoking the response handler for received messages.

Returns
An error code indicating success or failure.

◆ sendMessage()

ErrorCode Coap::Node::sendMessage ( const Message & message,
IPAddress ip,
uint16_t port )

Send a CoAP message to the specified IP address and port.

If the message is of type COAP_CONFIRMABLE, it is added to the retransmission queue. The retransmission queue behaves as per RFC 7252 and is processed in the loop() method. Its behaviour can be configured via COAP_CONFIRMABLE_MESSAGE_QUEUE_SIZE, COAP_MAX_RETRANSMIT and COAP_ACK_TIMEOUT_MS.

Parameters
messageThe CoAP message to send.
ipThe destination IP address.
portThe destination UDP port. This may be different from the local or the default port.
Returns
An error code indicating success or failure.

◆ serve()

ErrorCode Coap::Node::serve ( const char * path,
Callback callback )

Serve a given URI path with the specified callback.

The callback is invoked when a request message targeting the given path is received.

Parameters
pathThe URI path to serve with no leading slash, no trailing slash and no other special characters. If the path already exists, the callback is updated. Paths are *case-sensitive. Examples of valid paths are:
  • test
  • sensors/temp
callbackThe callback function to handle requests to the given path.
Returns
An error code indicating success or failure.
Warning
A path that does not respect the format won't work.
The path string pointer must remain valid for the entire lifetime of the registry. It is recommended to use constant strings.

◆ setResponseHandler()

void Coap::Node::setResponseHandler ( Callback handler)
inline

Set the response callback.

The response handler is invoked when a message of type MessageType::ACK or MessageType::RST is received, allowing the application to handle the response. The callback is unique for all the responses sent to this Coap instance.

Note that transmission ACK are also received internally by the retrasmission queue, so that retransmission can be stopped.

Responses to different requests must be differentiated by matching the message ID.

Parameters
handlerThe callback function to handle acknowledgements.

◆ start()

ErrorCode Coap::Node::start ( )

Start the CoAP instance.

It starts the underlying UDP instance, enabling communication. The UDP instance is bound to the local port specified at construction time.

Returns
ErrorCode::OK on success, or an error code on failure.

The documentation for this class was generated from the following files: