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

#include <ProsecCoAP.h>

Public Member Functions

 Coap (UDP &udp, size_t coapBufferSize=COAP_BUF_MAX_SIZE)
 Construct a CoAP instance using the given UDP transport.
 
 ~Coap ()
 Destroy the CoAP instance and free buffers.
 
int notifyObservers (const char *observed_endpoint, const char *payload, int payload_len, COAP_CONTENT_TYPE type)
 Notify all observers of a specific endpoint.
 
int addObserver (Observer **observer_out, const char *endpoint, IPAddress ip, int port, const uint8_t *token, uint8_t tokenLength)
 Add a new observer for a specific URL.
 
bool removeObserver (const char *endpoint, IPAddress ip, int port, const uint8_t *token, uint8_t tokenLength)
 Remove an observer for a specific endpoint.
 
bool start ()
 Start the server on the default port.
 
bool start (int port)
 Start the server on a custom port.
 
void response (CoapCallback c)
 Set the response callback for acknowledgements.
 
void server (CoapCallback c, String url)
 Register a server callback for a URI.
 
uint16_t sendResponse (IPAddress ip, int port, uint16_t messageId)
 Send a basic acknowledgment with empty payload.
 
uint16_t sendResponse (IPAddress ip, int port, uint16_t messageId, const char *payload)
 Send a text acknowledgment response.
 
uint16_t sendResponse (IPAddress ip, int port, uint16_t messageId, const char *payload, size_t payloadLength)
 Send a typed acknowledgment response with explicit length.
 
uint16_t sendResponse (IPAddress ip, int port, uint16_t messageId, const char *payload, size_t payloadLength, COAP_RESPONSE_CODE code, COAP_CONTENT_TYPE type, const uint8_t *token, int tokenLength)
 Send a fully customized acknowledgment response.
 
uint16_t get (IPAddress ip, int port, const char *url)
 Send a confirmable GET request.
 
uint16_t put (IPAddress ip, int port, const char *url, const char *payload)
 Send a confirmable PUT with null-terminated payload.
 
uint16_t put (IPAddress ip, int port, const char *url, const char *payload, size_t payloadLength)
 Send a confirmable PUT with explicit payload length.
 
uint16_t post (IPAddress ip, int port, const char *url, const char *payload)
 Send a confirmable POST with null-terminated payload.
 
uint16_t post (IPAddress ip, int port, const char *url, const char *payload, size_t payloadLength)
 Send a confirmable POST with explicit payload length.
 
uint16_t send (IPAddress ip, int port, const char *url, COAP_TYPE type, COAP_METHOD method, const uint8_t *token, uint8_t tokenLength, const uint8_t *payload, size_t payloadLength)
 Send a CoAP request with optional payload.
 
uint16_t send (IPAddress ip, int port, const char *url, COAP_TYPE type, COAP_METHOD method, const uint8_t *token, uint8_t tokenLength, const uint8_t *payload, size_t payloadLength, COAP_CONTENT_TYPE contentType)
 Send a CoAP request specifying the content format.
 
uint16_t send (IPAddress ip, int port, const char *url, COAP_TYPE type, COAP_METHOD method, const uint8_t *token, uint8_t tokenLength, const uint8_t *payload, size_t payloadLength, COAP_CONTENT_TYPE contentType, uint16_t messageId)
 Send a CoAP request with explicit message ID.
 
bool loop ()
 Process incoming packets and dispatch handlers.
 

Constructor & Destructor Documentation

◆ Coap()

Coap::Coap ( UDP & udp,
size_t coapBufferSize = COAP_BUF_MAX_SIZE )

Construct a CoAP instance using the given UDP transport.

Parameters
udpThe UDP transport to use for sending and receiving CoAP packets.
coapBufferSizeThe size of the internal CoAP buffers. Default is COAP_BUF_MAX_SIZE.

◆ ~Coap()

Coap::~Coap ( )

Destroy the CoAP instance and free buffers.

Member Function Documentation

◆ addObserver()

int Coap::addObserver ( Observer ** observer_out,
const char * endpoint,
IPAddress ip,
int port,
const uint8_t * token,
uint8_t tokenLength )

Add a new observer for a specific URL.

If the observer is already registered, the existing observer is returned.

Parameters
observer_outPointer to an Observer pointer that will be set to the newly added observer, or to the existing observer if already registered.
endpointThe endpoint to observe.
ipThe IP address of the observer.
portThe port of the observer.
tokenThe token used by the observer.
tokenLengthThe length of the token.
Returns
0 if the observer was added successfully. -1 if the url is invalid. -2 if the observer table is full.

◆ get()

uint16_t Coap::get ( IPAddress ip,
int port,
const char * url )

Send a confirmable GET request.

◆ loop()

bool Coap::loop ( )

Process incoming packets and dispatch handlers.

◆ notifyObservers()

int Coap::notifyObservers ( const char * observed_endpoint,
const char * payload,
int payload_len,
COAP_CONTENT_TYPE type )

Notify all observers of a specific endpoint.

This method sends a notification to all registered observers for the given endpoint. As per specifications, the notification includes the Observe option with a sequential number. The notification will be a non-confirmable message (COAP_NONCON).

Parameters
observed_endpointThe endpoint being observed.
payloadThe payload to send to observers.
payload_lenThe length of the payload.
typeThe content type of the payload.
Returns
Number of observers notified successfully.

◆ post() [1/2]

uint16_t Coap::post ( IPAddress ip,
int port,
const char * url,
const char * payload )

Send a confirmable POST with null-terminated payload.

◆ post() [2/2]

uint16_t Coap::post ( IPAddress ip,
int port,
const char * url,
const char * payload,
size_t payloadLength )

Send a confirmable POST with explicit payload length.

◆ put() [1/2]

uint16_t Coap::put ( IPAddress ip,
int port,
const char * url,
const char * payload )

Send a confirmable PUT with null-terminated payload.

◆ put() [2/2]

uint16_t Coap::put ( IPAddress ip,
int port,
const char * url,
const char * payload,
size_t payloadLength )

Send a confirmable PUT with explicit payload length.

◆ removeObserver()

bool Coap::removeObserver ( const char * endpoint,
IPAddress ip,
int port,
const uint8_t * token,
uint8_t tokenLength )

Remove an observer for a specific endpoint.

◆ response()

void Coap::response ( CoapCallback c)
inline

Set the response callback for acknowledgements.

◆ send() [1/3]

uint16_t Coap::send ( IPAddress ip,
int port,
const char * url,
COAP_TYPE type,
COAP_METHOD method,
const uint8_t * token,
uint8_t tokenLength,
const uint8_t * payload,
size_t payloadLength )

Send a CoAP request with optional payload.

◆ send() [2/3]

uint16_t Coap::send ( IPAddress ip,
int port,
const char * url,
COAP_TYPE type,
COAP_METHOD method,
const uint8_t * token,
uint8_t tokenLength,
const uint8_t * payload,
size_t payloadLength,
COAP_CONTENT_TYPE contentType )

Send a CoAP request specifying the content format.

◆ send() [3/3]

uint16_t Coap::send ( IPAddress ip,
int port,
const char * url,
COAP_TYPE type,
COAP_METHOD method,
const uint8_t * token,
uint8_t tokenLength,
const uint8_t * payload,
size_t payloadLength,
COAP_CONTENT_TYPE contentType,
uint16_t messageId )

Send a CoAP request with explicit message ID.

◆ sendResponse() [1/4]

uint16_t Coap::sendResponse ( IPAddress ip,
int port,
uint16_t messageId )

Send a basic acknowledgment with empty payload.

◆ sendResponse() [2/4]

uint16_t Coap::sendResponse ( IPAddress ip,
int port,
uint16_t messageId,
const char * payload )

Send a text acknowledgment response.

◆ sendResponse() [3/4]

uint16_t Coap::sendResponse ( IPAddress ip,
int port,
uint16_t messageId,
const char * payload,
size_t payloadLength )

Send a typed acknowledgment response with explicit length.

◆ sendResponse() [4/4]

uint16_t Coap::sendResponse ( IPAddress ip,
int port,
uint16_t messageId,
const char * payload,
size_t payloadLength,
COAP_RESPONSE_CODE code,
COAP_CONTENT_TYPE type,
const uint8_t * token,
int tokenLength )

Send a fully customized acknowledgment response.

◆ server()

void Coap::server ( CoapCallback c,
String url )
inline

Register a server callback for a URI.

◆ start() [1/2]

bool Coap::start ( )

Start the server on the default port.

◆ start() [2/2]

bool Coap::start ( int port)

Start the server on a custom port.


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