OpenThread_app
Loading...
Searching...
No Matches
CoAP Server Core

Core CoAP infrastructure for request handling, response generation, and resource management. More...

Classes

struct  otapp_coap_uri_t

Macros

#define OTAPP_COAP_PORT   5683
 Default CoAP port, as specified in RFC 7252.
#define OTAPP_COAP_URI_OK   (-1)
#define OTAPP_COAP_URI_ERROR   (-2)
#define OTAPP_COAP_OK   OTAPP_COAP_URI_OK
#define OTAPP_COAP_ERROR   OTAPP_COAP_URI_ERROR
#define OTAPP_COAP_OK_OBSERVER_REQUEST   (-3)

Typedefs

typedef struct ot_app_devDrv_t ot_app_devDrv_t
typedef struct otapp_coap_uri_t otapp_coap_uri_t

Enumerations

enum  otapp_coap_uriIndex_t {
  OTAPP_URI_NO_URI_INDEX = 0 , OTAPP_URI_WELL_KNOWN_CORE , OTAPP_URI_PARING_SERVICES , OTAPP_URI_SUBSCRIBED_URIS ,
  OTAPP_URI_TEST , OTAPP_URI_TEST_LED , OTAPP_URI_END_OF_INDEX
}
enum  otapp_coap_messageId_t { OTAPP_MESSAGE_OK = 0 , OTAPP_MESSAGE_ERROR , OTAPP_MESSAGE_TEST }

Functions

int8_t otapp_coap_init (ot_app_devDrv_t *devDriver)
 Initializes the CoAP service.
void otapp_coap_sendResponse (otMessage *requestMessage, const otMessageInfo *aMessageInfo, const uint8_t *responseContent, uint16_t responseLength)
 Sends a generic CoAP response with a payload.
void otapp_coap_sendResponseOK (otMessage *aMessage, const otMessageInfo *aMessageInfo)
 Sends a simple "OK" response (2.04 Changed).
void otapp_coap_sendResponseERROR (otMessage *aMessage, const otMessageInfo *aMessageInfo)
 Sends a "Not Found" error response (4.04).
void otapp_coap_printSenderIP (const otMessageInfo *aMessageInfo)
 Prints the sender's IP address from message metadata to the log.
const char * otapp_coap_getMessage (otapp_coap_messageId_t msgID)
 Converts an internal message ID/Type to a string representation.
void otapp_coapSendtoTestPut ()
 Sends a test PUT message (Debug function).
void otapp_coapSendtoTestGet ()
 Sends a test GET message (Debug function).
void otapp_coapSendDeviceNamePut ()
 Sends a request to update the device name on the network.
void otapp_coapSendGetUri_Well_known (const otIp6Address *ipAddr, otCoapResponseHandler responseHandler, void *aContext)
 Sends a GET request to the standard discovery URI .well-known/core.
void otapp_coapSendPutUri_subscribed_uris (const otIp6Address *ipAddr, const uint8_t *data, uint16_t dataSize)
 Sends a PUT request to update a resource on a remote device.
void otapp_coapSendSubscribeRequest (const otIp6Address *ipAddr, const char *aUriPath, uint8_t *tokenOut)
 Initiates a CoAP Observe subscription (RFC 7641).
void otapp_coapSendSubscribeRequestUpdate (const otIp6Address *ipAddr, const char *aUriPath, uint8_t *tokenIn)
 Refreshes or updates an existing subscription.
void otapp_coap_clientSendPutByte (const otIp6Address *peer_addr, const char *aUriPath, const uint8_t *payloadMsg, const uint16_t payloadMsgSize, otCoapResponseHandler responseHandler, void *aContext)
 send coap message bytes using the PUT method
void otapp_coap_clientSendGetByte (const otIp6Address *peer_addr, const char *aUriPath, otCoapResponseHandler responseHandler, void *aContext)
 send coap request using the GET method. Response will contain bytes
const char * otapp_coap_getUriNameFromDefault (otapp_coap_uriIndex_t uriIndex)
 Retrieves the string name of a default URI by its index.
int8_t otapp_coapReadPayload (otMessage *aMessage, uint8_t *bufferOut, uint16_t bufferSize, uint16_t *readBytesOut)
 Extracts the payload from an OpenThread CoAP message.
int8_t otapp_coap_processUriRequest (otMessage *aMessage, const otMessageInfo *aMessageInfo, uint8_t uriId, uint8_t *bufOut, uint16_t bufSize)
 main function for HARDWARE DEVICE URIS. This function processing incoming request. Request can be concern adding device to subscribe list or general uri request to next processing. If it is general uri request, then the given bufOut contains data to processing.

Detailed Description

Core CoAP infrastructure for request handling, response generation, and resource management.

This module wraps the OpenThread CoAP API to provide a simplified interface for application developers. It handles the low-level details of message buffer management, header parsing, and response code generation. Key Functionalities:

  • Initialization: Starts the CoAP service on the default UDP port.
  • Resource Management: Registers URI paths and their callback handlers.
  • Response Helpers: Provides ready-to-use functions for sending ACK (2.04 Changed), ERROR (4.04 Not Found), or Data payloads.
  • Payload Extraction: Safely copies data from OpenThread messages (Packet Buffers) to flat C buffers.
Author
Jan Łukaszewicz (plhar.nosp@m.eo@g.nosp@m.mail..nosp@m.com)
Version
0.1
Date
21-07-2025

Macro Definition Documentation

◆ OTAPP_COAP_ERROR

#define OTAPP_COAP_ERROR   OTAPP_COAP_URI_ERROR

◆ OTAPP_COAP_OK

#define OTAPP_COAP_OK   OTAPP_COAP_URI_OK

◆ OTAPP_COAP_OK_OBSERVER_REQUEST

#define OTAPP_COAP_OK_OBSERVER_REQUEST   (-3)

◆ OTAPP_COAP_PORT

#define OTAPP_COAP_PORT   5683

Default CoAP port, as specified in RFC 7252.

◆ OTAPP_COAP_URI_ERROR

#define OTAPP_COAP_URI_ERROR   (-2)

◆ OTAPP_COAP_URI_OK

#define OTAPP_COAP_URI_OK   (-1)

Typedef Documentation

◆ ot_app_devDrv_t

typedef struct ot_app_devDrv_t ot_app_devDrv_t

◆ otapp_coap_uri_t

typedef struct otapp_coap_uri_t otapp_coap_uri_t

Enumeration Type Documentation

◆ otapp_coap_messageId_t

Enumerator
OTAPP_MESSAGE_OK 
OTAPP_MESSAGE_ERROR 
OTAPP_MESSAGE_TEST 

◆ otapp_coap_uriIndex_t

Enumerator
OTAPP_URI_NO_URI_INDEX 
OTAPP_URI_WELL_KNOWN_CORE 
OTAPP_URI_PARING_SERVICES 
OTAPP_URI_SUBSCRIBED_URIS 
OTAPP_URI_TEST 
OTAPP_URI_TEST_LED 
OTAPP_URI_END_OF_INDEX 

Function Documentation

◆ otapp_coap_clientSendGetByte()

void otapp_coap_clientSendGetByte ( const otIp6Address * peer_addr,
const char * aUriPath,
otCoapResponseHandler responseHandler,
void * aContext )

send coap request using the GET method. Response will contain bytes

Parameters
peer_addr[in] ptr to device IPv6 address
aUriPath[in] string ptr to uri
responseHandler[in] callback to response handler
aContext[in] content will be provided with responseHandler

◆ otapp_coap_clientSendPutByte()

void otapp_coap_clientSendPutByte ( const otIp6Address * peer_addr,
const char * aUriPath,
const uint8_t * payloadMsg,
const uint16_t payloadMsgSize,
otCoapResponseHandler responseHandler,
void * aContext )

send coap message bytes using the PUT method

Parameters
peer_addr[in] ptr to device IPv6 address
aUriPath[in] string ptr to uri
payloadMsg[in] ptr to payload data
payloadMsgSize[in] payload size
responseHandler[in] callback to response handler
aContext[in] content will be provided with responseHandler

◆ otapp_coap_getMessage()

const char * otapp_coap_getMessage ( otapp_coap_messageId_t msgID)

Converts an internal message ID/Type to a string representation.

Parameters
msgID[in] Internal message identifier.
Returns
const char* String literal (e.g., "GET", "PUT", "ACK").

◆ otapp_coap_getUriNameFromDefault()

const char * otapp_coap_getUriNameFromDefault ( otapp_coap_uriIndex_t uriIndex)

Retrieves the string name of a default URI by its index.

Helper to map integer IDs (e.g., from config) to actual URI strings.

Parameters
uriIndex[in] Index of the URI.
Returns
const char* URI path string (e.g., "light/dimm") or NULL if invalid.

◆ otapp_coap_init()

int8_t otapp_coap_init ( ot_app_devDrv_t * devDriver)

Initializes the CoAP service.

Starts the CoAP engine on the default UDP port and sets up the internal driver reference.

Parameters
devDriverPointer to the main device driver instance.
Returns
int8_t OTAPP_COAP_OK on success.

◆ otapp_coap_printSenderIP()

void otapp_coap_printSenderIP ( const otMessageInfo * aMessageInfo)

Prints the sender's IP address from message metadata to the log.

Parameters
aMessageInfo[in] Pointer to the message info structure.

◆ otapp_coap_processUriRequest()

int8_t otapp_coap_processUriRequest ( otMessage * aMessage,
const otMessageInfo * aMessageInfo,
uint8_t uriId,
uint8_t * bufOut,
uint16_t bufSize )

main function for HARDWARE DEVICE URIS. This function processing incoming request. Request can be concern adding device to subscribe list or general uri request to next processing. If it is general uri request, then the given bufOut contains data to processing.

Parameters
aMessage[in] ptr to incoming message
aMessageInfo[in] ptr to message info
uriId[in] specific HARDWARE DEVICE URI ID that was passed in uriGetListSize callback
bufOut[out] ptr to buffer. Incomming data will be saved there.
bufSize[in] size of bufOut
Returns
int8_t [out] OTAPP_COAP_OK when same data was saved in buffer, OAC_URI_OBS_ADDED_NEW_DEVICE or OAC_URI_OBS_NO_NEED_UPDATE when observer function processed, OTAPP_COAP_ERROR

◆ otapp_coap_sendResponse()

void otapp_coap_sendResponse ( otMessage * requestMessage,
const otMessageInfo * aMessageInfo,
const uint8_t * responseContent,
uint16_t responseLength )

Sends a generic CoAP response with a payload.

Parameters
request[in] The original request message (used to match Token/Message ID).
messageInfo[in] Source address and port of the requester.
payload[in] Pointer to the data buffer to send.
payload_len[in] Length of the payload data.
coapResponseCode[in] CoAP status code (e.g., OT_COAP_CODE_CONTENT).
coapType[in] Message type (e.g., OT_COAP_TYPE_ACKNOWLEDGMENT).

◆ otapp_coap_sendResponseERROR()

void otapp_coap_sendResponseERROR ( otMessage * aMessage,
const otMessageInfo * aMessageInfo )

Sends a "Not Found" error response (4.04).

Used when the requested URI or resource does not exist.

Parameters
request[in] The original request message.
aMessageInfo[in] Source address and port.

◆ otapp_coap_sendResponseOK()

void otapp_coap_sendResponseOK ( otMessage * aMessage,
const otMessageInfo * aMessageInfo )

Sends a simple "OK" response (2.04 Changed).

Typically used to acknowledge PUT/POST requests that don't require returning data.

Parameters
request[in] The original request message.
aMessageInfo[in] Source address and port.

◆ otapp_coapReadPayload()

int8_t otapp_coapReadPayload ( otMessage * aMessage,
uint8_t * bufferOut,
uint16_t bufferSize,
uint16_t * readBytesOut )

Extracts the payload from an OpenThread CoAP message.

Copies bytes from the opaque otMessage structure into a linear buffer.

Parameters
[in]aMessagePointer to the incoming message.
[out]bufferOutDestination buffer.
[in]bufferSizeMax size of the destination buffer.
[out]readBytesOutPointer to store the actual number of bytes read.
Returns
int8_t OTAPP_COAP_OK on success, or OTAPP_COAP_ERROR if buffer is too small.

◆ otapp_coapSendDeviceNamePut()

void otapp_coapSendDeviceNamePut ( )

Sends a request to update the device name on the network.

Constructs a CoAP PUT message containing the device's current name and sends it to the default service locator or parent.

Returns
int8_t OTAPP_COAP_OK on success.

◆ otapp_coapSendGetUri_Well_known()

void otapp_coapSendGetUri_Well_known ( const otIp6Address * ipAddr,
otCoapResponseHandler responseHandler,
void * aContext )

Sends a GET request to the standard discovery URI .well-known/core.

This function is used for Service Discovery. It queries a remote device to list its available resources (URIs). The response is handled by the provided callback.

Parameters
ipAddr[in] IPv6 address of the target device.
responseHandler[in] Callback function to handle the list of resources returned.
aContext[in] User context to pass to the handler.

◆ otapp_coapSendPutUri_subscribed_uris()

void otapp_coapSendPutUri_subscribed_uris ( const otIp6Address * ipAddr,
const uint8_t * data,
uint16_t dataSize )

Sends a PUT request to update a resource on a remote device.

Sends a Confirmable PUT message with a payload. Use this to control remote devices (e.g., turn on a light).

Parameters
ipAddr[in] IPv6 address of the target device.
data[in] Pointer to the payload data (e.g., new state).
dataSize[in] Size of the payload.

◆ otapp_coapSendSubscribeRequest()

void otapp_coapSendSubscribeRequest ( const otIp6Address * ipAddr,
const char * aUriPath,
uint8_t * tokenOut )

Initiates a CoAP Observe subscription (RFC 7641).

Sends a GET request with the Observe option set to 0 (Register). This tells the server to send notifications whenever the resource state changes.

Parameters
ipAddr[in] IPv6 address of the target device.
aUriPath[in] URI path string to subscribe to (e.g., "light/on_off").
tokenOut[out] Buffer (min 8 bytes) where the generated subscription Token will be saved.

◆ otapp_coapSendSubscribeRequestUpdate()

void otapp_coapSendSubscribeRequestUpdate ( const otIp6Address * ipAddr,
const char * aUriPath,
uint8_t * tokenIn )

Refreshes or updates an existing subscription.

Similar to otapp_coapSendSubscribeRequest, but uses an existing Token instead of generating a new one. Useful for re-confirming interest without resetting state.

Parameters
ipAddr[in] IPv6 address of the target device.
aUriPath[in] URI path string.
tokenIn[in] Existing token to reuse for this request.

◆ otapp_coapSendtoTestGet()

void otapp_coapSendtoTestGet ( )

Sends a test GET message (Debug function).

Hardcoded test routine for verifying transmission logic during development.

◆ otapp_coapSendtoTestPut()

void otapp_coapSendtoTestPut ( )

Sends a test PUT message (Debug function).

Hardcoded test routine for verifying transmission logic during development.