CoAP

This module includes functions that control CoAP communication.

Summary

The functions in this module are available when application-coap feature (OPENTHREAD_ENABLE_APPLICATION_COAP) is enabled.

Enumerations

otCoapCode{
  OT_COAP_CODE_EMPTY = OT_COAP_CODE(0, 0),
  OT_COAP_CODE_GET = OT_COAP_CODE(0, 1),
  OT_COAP_CODE_POST = OT_COAP_CODE(0, 2),
  OT_COAP_CODE_PUT = OT_COAP_CODE(0, 3),
  OT_COAP_CODE_DELETE = OT_COAP_CODE(0, 4),
  OT_COAP_CODE_RESPONSE_MIN = OT_COAP_CODE(2, 0),
  OT_COAP_CODE_CREATED = OT_COAP_CODE(2, 1),
  OT_COAP_CODE_DELETED = OT_COAP_CODE(2, 2),
  OT_COAP_CODE_VALID = OT_COAP_CODE(2, 3),
  OT_COAP_CODE_CHANGED = OT_COAP_CODE(2, 4),
  OT_COAP_CODE_CONTENT = OT_COAP_CODE(2, 5),
  OT_COAP_CODE_BAD_REQUEST = OT_COAP_CODE(4, 0),
  OT_COAP_CODE_UNAUTHORIZED = OT_COAP_CODE(4, 1),
  OT_COAP_CODE_BAD_OPTION = OT_COAP_CODE(4, 2),
  OT_COAP_CODE_FORBIDDEN = OT_COAP_CODE(4, 3),
  OT_COAP_CODE_NOT_FOUND = OT_COAP_CODE(4, 4),
  OT_COAP_CODE_METHOD_NOT_ALLOWED = OT_COAP_CODE(4, 5),
  OT_COAP_CODE_NOT_ACCEPTABLE = OT_COAP_CODE(4, 6),
  OT_COAP_CODE_PRECONDITION_FAILED = OT_COAP_CODE(4, 12),
  OT_COAP_CODE_REQUEST_TOO_LARGE = OT_COAP_CODE(4, 13),
  OT_COAP_CODE_UNSUPPORTED_FORMAT = OT_COAP_CODE(4, 15),
  OT_COAP_CODE_INTERNAL_ERROR = OT_COAP_CODE(5, 0),
  OT_COAP_CODE_NOT_IMPLEMENTED = OT_COAP_CODE(5, 1),
  OT_COAP_CODE_BAD_GATEWAY = OT_COAP_CODE(5, 2),
  OT_COAP_CODE_SERVICE_UNAVAILABLE = OT_COAP_CODE(5, 3),
  OT_COAP_CODE_GATEWAY_TIMEOUT = OT_COAP_CODE(5, 4),
  OT_COAP_CODE_PROXY_NOT_SUPPORTED = OT_COAP_CODE(5, 5)
}
enum
CoAP Code values.
otCoapOptionContentFormat{
  OT_COAP_OPTION_CONTENT_FORMAT_TEXT_PLAIN = 0,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_ENCRYPT0 = 16,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_MAC0 = 17,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_SIGN1 = 18,
  OT_COAP_OPTION_CONTENT_FORMAT_LINK_FORMAT = 40,
  OT_COAP_OPTION_CONTENT_FORMAT_XML = 41,
  OT_COAP_OPTION_CONTENT_FORMAT_OCTET_STREAM = 42,
  OT_COAP_OPTION_CONTENT_FORMAT_EXI = 47,
  OT_COAP_OPTION_CONTENT_FORMAT_JSON = 50,
  OT_COAP_OPTION_CONTENT_FORMAT_JSON_PATCH_JSON = 51,
  OT_COAP_OPTION_CONTENT_FORMAT_MERGE_PATCH_JSON = 52,
  OT_COAP_OPTION_CONTENT_FORMAT_CBOR = 60,
  OT_COAP_OPTION_CONTENT_FORMAT_CWT = 61,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_ENCRYPT = 96,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_MAC = 97,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_SIGN = 98,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_KEY = 101,
  OT_COAP_OPTION_CONTENT_FORMAT_COSE_KEY_SET = 102,
  OT_COAP_OPTION_CONTENT_FORMAT_SENML_JSON = 110,
  OT_COAP_OPTION_CONTENT_FORMAT_SENSML_JSON = 111,
  OT_COAP_OPTION_CONTENT_FORMAT_SENML_CBOR = 112,
  OT_COAP_OPTION_CONTENT_FORMAT_SENSML_CBOR = 113,
  OT_COAP_OPTION_CONTENT_FORMAT_SENML_EXI = 114,
  OT_COAP_OPTION_CONTENT_FORMAT_SENSML_EXI = 115,
  OT_COAP_OPTION_CONTENT_FORMAT_COAP_GROUP_JSON = 256,
  OT_COAP_OPTION_CONTENT_FORMAT_SENML_XML = 310,
  OT_COAP_OPTION_CONTENT_FORMAT_SENSML_XML = 311
}
enum
CoAP Content Format codes.
otCoapOptionType{
  OT_COAP_OPTION_IF_MATCH = 1,
  OT_COAP_OPTION_URI_HOST = 3,
  OT_COAP_OPTION_E_TAG = 4,
  OT_COAP_OPTION_IF_NONE_MATCH = 5,
  OT_COAP_OPTION_OBSERVE = 6,
  OT_COAP_OPTION_URI_PORT = 7,
  OT_COAP_OPTION_LOCATION_PATH = 8,
  OT_COAP_OPTION_URI_PATH = 11,
  OT_COAP_OPTION_CONTENT_FORMAT = 12,
  OT_COAP_OPTION_MAX_AGE = 14,
  OT_COAP_OPTION_URI_QUERY = 15,
  OT_COAP_OPTION_ACCEPT = 17,
  OT_COAP_OPTION_LOCATION_QUERY = 20,
  OT_COAP_OPTION_PROXY_URI = 35,
  OT_COAP_OPTION_PROXY_SCHEME = 39,
  OT_COAP_OPTION_SIZE1 = 60
}
enum
CoAP Option Numbers.
otCoapType{
  OT_COAP_TYPE_CONFIRMABLE = 0x00,
  OT_COAP_TYPE_NON_CONFIRMABLE = 0x10,
  OT_COAP_TYPE_ACKNOWLEDGMENT = 0x20,
  OT_COAP_TYPE_RESET = 0x30
}
enum
CoAP Type values.

Typedefs

otCoapCode typedef
enum otCoapCode
CoAP Code values.
otCoapHeader typedef
struct otCoapHeader
This structure represents a CoAP header.
otCoapOption typedef
struct otCoapOption
This structure represents a CoAP option.
otCoapOptionContentFormat typedef
CoAP Content Format codes.
otCoapOptionType typedef
CoAP Option Numbers.
otCoapRequestHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo) typedef
void(*
This function pointer is called when a CoAP request with a given Uri-Path is received.
otCoapResource typedef
This structure represents a CoAP resource.
otCoapResponseHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult) typedef
void(*
This function pointer is called when a CoAP response is received or on the request timeout.
otCoapType typedef
enum otCoapType
CoAP Type values.

Functions

otCoapAddResource(otInstance *aInstance, otCoapResource *aResource)
This function adds a resource to the CoAP server.
otCoapHeaderAppendContentFormatOption(otCoapHeader *aHeader, otCoapOptionContentFormat aContentFormat)
This function appends the Content Format CoAP option as specified in https://tools.ietf.org/html/rfc7252#page-92.
otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge)
This function appends a Max-Age option.
otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve)
This function appends an Observe option.
otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *aOption)
This function appends a CoAP option in a header.
otCoapHeaderAppendProxyUriOption(otCoapHeader *aHeader, const char *aUriPath)
This function appends a Proxy-Uri option.
otCoapHeaderAppendUintOption(otCoapHeader *aHeader, uint16_t aNumber, uint32_t aValue)
This function appends an unsigned integer CoAP option as specified in https://tools.ietf.org/html/rfc7252#section-3.2.
otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath)
This function appends a Uri-Path option.
otCoapHeaderAppendUriQueryOption(otCoapHeader *aHeader, const char *aUriQuery)
This function appends a single Uri-Query option.
otCoapHeaderCodeToString(const otCoapHeader *aHeader)
const char *
This method returns the CoAP Code as human readable string.
otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength)
void
This function sets the Token length and randomizes its value.
otCoapHeaderGetCode(const otCoapHeader *aHeader)
This function returns the Code value.
otCoapHeaderGetFirstOption(otCoapHeader *aHeader)
const otCoapOption *
This function returns a pointer to the first option.
otCoapHeaderGetMessageId(const otCoapHeader *aHeader)
uint16_t
This function returns the Message ID value.
otCoapHeaderGetNextOption(otCoapHeader *aHeader)
const otCoapOption *
This function returns a pointer to the next option.
otCoapHeaderGetToken(const otCoapHeader *aHeader)
const uint8_t *
This function returns a pointer to the Token value.
otCoapHeaderGetTokenLength(const otCoapHeader *aHeader)
uint8_t
This function returns the Token length.
otCoapHeaderGetType(const otCoapHeader *aHeader)
This function returns the Type value.
otCoapHeaderInit(otCoapHeader *aHeader, otCoapType aType, otCoapCode aCode)
void
This function initializes the CoAP header.
otCoapHeaderSetMessageId(otCoapHeader *aHeader, uint16_t aMessageId)
void
This function sets the Message ID value.
otCoapHeaderSetPayloadMarker(otCoapHeader *aHeader)
This function adds Payload Marker indicating beginning of the payload to the CoAP header.
otCoapHeaderSetToken(otCoapHeader *aHeader, const uint8_t *aToken, uint8_t aTokenLength)
void
This function sets the Token value and length in a header.
otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader, const otMessageSettings *aSettings)
This function creates a new message with a CoAP header.
otCoapRemoveResource(otInstance *aInstance, otCoapResource *aResource)
void
This function removes a resource from the CoAP server.
otCoapSendRequest(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo, otCoapResponseHandler aHandler, void *aContext)
This function sends a CoAP request.
otCoapSendResponse(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo)
This function sends a CoAP response from the server.
otCoapSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
void
This function sets the default handler for unhandled CoAP requests.
otCoapStart(otInstance *aInstance, uint16_t aPort)
This function starts the CoAP server.
otCoapStop(otInstance *aInstance)
This function stops the CoAP server.

Structs

otCoapHeader

This structure represents a CoAP header.

otCoapOption

This structure represents a CoAP option.

otCoapResource

This structure represents a CoAP resource.

Enumerations

otCoapOptionContentFormat

 otCoapOptionContentFormat

CoAP Content Format codes.

The full list is documented at https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#content-formats

Properties
OT_COAP_OPTION_CONTENT_FORMAT_CBOR

application/cbor: [RFC7049]

OT_COAP_OPTION_CONTENT_FORMAT_COAP_GROUP_JSON

application/coap-group+json: [RFC7390]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_ENCRYPT

application/cose; cose-type="cose-encrypt": [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_ENCRYPT0

application/cose; cose-type="cose-encrypt0": [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_KEY

application/cose-key: [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_KEY_SET

application/cose-key-set: [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_MAC

application/cose; cose-type="cose-mac": [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_MAC0

application/cose; cose-type="cose-mac0": [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_SIGN

application/cose; cose-type="cose-sign": [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_COSE_SIGN1

application/cose; cose-type="cose-sign1": [RFC8152]

OT_COAP_OPTION_CONTENT_FORMAT_CWT

application/cwt: [RFC8392]

OT_COAP_OPTION_CONTENT_FORMAT_EXI

application/exi: ["Efficient XML Interchange (EXI) Format 1.0 (Second Edition)", February 2014]

OT_COAP_OPTION_CONTENT_FORMAT_JSON

application/json: [RFC7159]

OT_COAP_OPTION_CONTENT_FORMAT_JSON_PATCH_JSON

application/json-patch+json: [RFC6902]

OT_COAP_OPTION_CONTENT_FORMAT_LINK_FORMAT

application/link-format: [RFC6690]

OT_COAP_OPTION_CONTENT_FORMAT_MERGE_PATCH_JSON

application/merge-patch+json: [RFC7396]

OT_COAP_OPTION_CONTENT_FORMAT_OCTET_STREAM

application/octet-stream: [RFC2045][RFC2046]

OT_COAP_OPTION_CONTENT_FORMAT_SENML_CBOR

application/senml+cbor: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_SENML_EXI

application/senml-exi: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_SENML_JSON

application/senml+json: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_SENML_XML

application/senml+xml: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_SENSML_CBOR

application/sensml+cbor: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_SENSML_EXI

application/sensml-exi: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_SENSML_JSON

application/sensml+json: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_SENSML_XML

application/sensml+xml: [RFC8428]

OT_COAP_OPTION_CONTENT_FORMAT_TEXT_PLAIN

text/plain; charset=utf-8: [RFC2046][RFC3676][RFC5147]

OT_COAP_OPTION_CONTENT_FORMAT_XML

application/xml: [RFC3023]

otCoapType

 otCoapType

CoAP Type values.

Properties
OT_COAP_TYPE_ACKNOWLEDGMENT

Acknowledgment.

OT_COAP_TYPE_CONFIRMABLE

Confirmable.

OT_COAP_TYPE_NON_CONFIRMABLE

Non-confirmable.

OT_COAP_TYPE_RESET

Reset.

Typedefs

otCoapCode

enum otCoapCode otCoapCode

CoAP Code values.

otCoapHeader

struct otCoapHeader otCoapHeader

This structure represents a CoAP header.

otCoapOption

struct otCoapOption otCoapOption

This structure represents a CoAP option.

otCoapOptionContentFormat

enum otCoapOptionContentFormat otCoapOptionContentFormat

CoAP Content Format codes.

The full list is documented at https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#content-formats

otCoapOptionType

enum otCoapOptionType otCoapOptionType

CoAP Option Numbers.

otCoapRequestHandler

void(* otCoapRequestHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo)

This function pointer is called when a CoAP request with a given Uri-Path is received.

Details
Parameters
[in] aContext
A pointer to arbitrary context information.
[in] aHeader
A pointer to the CoAP header.
[in] aMessage
A pointer to the message.
[in] aMessageInfo
A pointer to the message info for aMessage.

otCoapResource

struct otCoapResource otCoapResource

This structure represents a CoAP resource.

otCoapResponseHandler

void(* otCoapResponseHandler)(void *aContext, otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aResult)

This function pointer is called when a CoAP response is received or on the request timeout.

Details
Parameters
[in] aContext
A pointer to application-specific context.
[in] aHeader
A pointer to the received CoAP header. NULL if no response was received.
[in] aMessage
A pointer to the message buffer containing the response. NULL if no response was received.
[in] aMessageInfo
A pointer to the message info for aMessage. NULL if no response was received.
[in] aResult
A result of the CoAP transaction.
Return Values
OT_ERROR_NONE
A response was received successfully.
OT_ERROR_ABORT
A CoAP transaction was reseted by peer.
OT_ERROR_RESPONSE_TIMEOUT
No response or acknowledgment received during timeout period.

otCoapType

enum otCoapType otCoapType

CoAP Type values.

Functions

otCoapAddResource

otError otCoapAddResource(
  otInstance *aInstance,
  otCoapResource *aResource
)

This function adds a resource to the CoAP server.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
[in] aResource
A pointer to the resource.
Return Values
OT_ERROR_NONE
Successfully added aResource.
OT_ERROR_ALREADY
The aResource was already added.

otCoapHeaderAppendContentFormatOption

otError otCoapHeaderAppendContentFormatOption(
  otCoapHeader *aHeader,
  otCoapOptionContentFormat aContentFormat
)

This function appends the Content Format CoAP option as specified in https://tools.ietf.org/html/rfc7252#page-92.

This must be called before setting otCoapHeaderSetPayloadMarker if a payload is to be included in the message.

The function is a convenience wrapper around otCoapHeaderAppendUintOption, and if the desired format type code isn't listed in otCoapOptionContentFormat, this base function should be used instead.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aContentFormat
One of the content formats listed in otCoapOptionContentFormat above.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderAppendMaxAgeOption

otError otCoapHeaderAppendMaxAgeOption(
  otCoapHeader *aHeader,
  uint32_t aMaxAge
)

This function appends a Max-Age option.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aMaxAge
The Max-Age value.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderAppendObserveOption

otError otCoapHeaderAppendObserveOption(
  otCoapHeader *aHeader,
  uint32_t aObserve
)

This function appends an Observe option.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aObserve
Observe field value.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderAppendOption

otError otCoapHeaderAppendOption(
  otCoapHeader *aHeader,
  const otCoapOption *aOption
)

This function appends a CoAP option in a header.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aOption
A pointer to the CoAP option.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderAppendProxyUriOption

otError otCoapHeaderAppendProxyUriOption(
  otCoapHeader *aHeader,
  const char *aUriPath
)

This function appends a Proxy-Uri option.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aUriPath
A pointer to a NULL-terminated string.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderAppendUintOption

otError otCoapHeaderAppendUintOption(
  otCoapHeader *aHeader,
  uint16_t aNumber,
  uint32_t aValue
)

This function appends an unsigned integer CoAP option as specified in https://tools.ietf.org/html/rfc7252#section-3.2.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aNumber
The CoAP Option number.
[in] aValue
The CoAP Option unsigned integer value.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderAppendUriPathOptions

otError otCoapHeaderAppendUriPathOptions(
  otCoapHeader *aHeader,
  const char *aUriPath
)

This function appends a Uri-Path option.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aUriPath
A pointer to a NULL-terminated string.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderAppendUriQueryOption

otError otCoapHeaderAppendUriQueryOption(
  otCoapHeader *aHeader,
  const char *aUriQuery
)

This function appends a single Uri-Query option.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aUriQuery
A pointer to NULL-terminated string, which should contain a single key=value pair.
Return Values
OT_ERROR_NONE
Successfully appended the option.
OT_ERROR_INVALID_ARGS
The option type is not equal or greater than the last option type.
OT_ERROR_NO_BUFS
The option length exceeds the buffer size.

otCoapHeaderCodeToString

const char * otCoapHeaderCodeToString(
  const otCoapHeader *aHeader
)

This method returns the CoAP Code as human readable string.

@ returns The CoAP Code as string.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.

otCoapHeaderGenerateToken

void otCoapHeaderGenerateToken(
  otCoapHeader *aHeader,
  uint8_t aTokenLength
)

This function sets the Token length and randomizes its value.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aTokenLength
The Length of a Token to set.

otCoapHeaderGetCode

otCoapCode otCoapHeaderGetCode(
  const otCoapHeader *aHeader
)

This function returns the Code value.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
Returns
The Code value.

otCoapHeaderGetFirstOption

const otCoapOption * otCoapHeaderGetFirstOption(
  otCoapHeader *aHeader
)

This function returns a pointer to the first option.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
Returns
A pointer to the first option. If no option is present NULL pointer is returned.

otCoapHeaderGetMessageId

uint16_t otCoapHeaderGetMessageId(
  const otCoapHeader *aHeader
)

This function returns the Message ID value.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
Returns
The Message ID value.

otCoapHeaderGetNextOption

const otCoapOption * otCoapHeaderGetNextOption(
  otCoapHeader *aHeader
)

This function returns a pointer to the next option.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
Returns
A pointer to the next option. If no more options are present NULL pointer is returned.

otCoapHeaderGetToken

const uint8_t * otCoapHeaderGetToken(
  const otCoapHeader *aHeader
)

This function returns a pointer to the Token value.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
Returns
A pointer to the Token value.

otCoapHeaderGetTokenLength

uint8_t otCoapHeaderGetTokenLength(
  const otCoapHeader *aHeader
)

This function returns the Token length.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
Returns
The Token length.

otCoapHeaderGetType

otCoapType otCoapHeaderGetType(
  const otCoapHeader *aHeader
)

This function returns the Type value.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
Returns
The Type value.

otCoapHeaderInit

void otCoapHeaderInit(
  otCoapHeader *aHeader,
  otCoapType aType,
  otCoapCode aCode
)

This function initializes the CoAP header.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header to initialize.
[in] aType
CoAP message type.
[in] aCode
CoAP message code.

otCoapHeaderSetMessageId

void otCoapHeaderSetMessageId(
  otCoapHeader *aHeader,
  uint16_t aMessageId
)

This function sets the Message ID value.

Details
Parameters
[in] aHeader
A pointer to the CoAP header.
[in] aMessageId
The Message ID value.

otCoapHeaderSetPayloadMarker

otError otCoapHeaderSetPayloadMarker(
  otCoapHeader *aHeader
)

This function adds Payload Marker indicating beginning of the payload to the CoAP header.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
Return Values
OT_ERROR_NONE
Payload Marker successfully added.
OT_ERROR_NO_BUFS
Header Payload Marker exceeds the buffer size.

otCoapHeaderSetToken

void otCoapHeaderSetToken(
  otCoapHeader *aHeader,
  const uint8_t *aToken,
  uint8_t aTokenLength
)

This function sets the Token value and length in a header.

Details
Parameters
[in,out] aHeader
A pointer to the CoAP header.
[in] aToken
A pointer to the Token value.
[in] aTokenLength
The Length of aToken.

otCoapNewMessage

otMessage * otCoapNewMessage(
  otInstance *aInstance,
  const otCoapHeader *aHeader,
  const otMessageSettings *aSettings
)

This function creates a new message with a CoAP header.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
[in] aHeader
A pointer to a CoAP header that is used to create the message.
[in] aSettings
A pointer to the message settings or NULL to set default settings.
Returns
A pointer to the message buffer or NULL if no message buffers are available or parameters are invalid.

otCoapRemoveResource

void otCoapRemoveResource(
  otInstance *aInstance,
  otCoapResource *aResource
)

This function removes a resource from the CoAP server.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
[in] aResource
A pointer to the resource.

otCoapSendRequest

otError otCoapSendRequest(
  otInstance *aInstance,
  otMessage *aMessage,
  const otMessageInfo *aMessageInfo,
  otCoapResponseHandler aHandler,
  void *aContext
)

This function sends a CoAP request.

If a response for a request is expected, respective function and context information should be provided. If no response is expected, these arguments should be NULL pointers.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
[in] aMessage
A pointer to the message to send.
[in] aMessageInfo
A pointer to the message info associated with aMessage.
[in] aHandler
A function pointer that shall be called on response reception or timeout.
[in] aContext
A pointer to arbitrary context information. May be NULL if not used.
Return Values
OT_ERROR_NONE
Successfully sent CoAP message.
OT_ERROR_NO_BUFS
Failed to allocate retransmission data.

otCoapSendResponse

otError otCoapSendResponse(
  otInstance *aInstance,
  otMessage *aMessage,
  const otMessageInfo *aMessageInfo
)

This function sends a CoAP response from the server.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
[in] aMessage
A pointer to the CoAP response to send.
[in] aMessageInfo
A pointer to the message info associated with aMessage.
Return Values
OT_ERROR_NONE
Successfully enqueued the CoAP response message.
OT_ERROR_NO_BUFS
Insufficient buffers available to send the CoAP response.

otCoapSetDefaultHandler

void otCoapSetDefaultHandler(
  otInstance *aInstance,
  otCoapRequestHandler aHandler,
  void *aContext
)

This function sets the default handler for unhandled CoAP requests.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
[in] aHandler
A function pointer that shall be called when an unhandled request arrives.
[in] aContext
A pointer to arbitrary context information. May be NULL if not used.

otCoapStart

otError otCoapStart(
  otInstance *aInstance,
  uint16_t aPort
)

This function starts the CoAP server.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
[in] aPort
The local UDP port to bind to.
Return Values
OT_ERROR_NONE
Successfully started the CoAP server.

otCoapStop

otError otCoapStop(
  otInstance *aInstance
)

This function stops the CoAP server.

Details
Parameters
[in] aInstance
A pointer to an OpenThread instance.
Return Values
OT_ERROR_NONE
Successfully stopped the CoAP server.

Macros

OT_COAP_CODE

 OT_COAP_CODE ((((c)&0x7) << 5) | ((d)&0x1f))

Helper macro to define CoAP Code values.

OT_COAP_HEADER_MAX_LENGTH

 OT_COAP_HEADER_MAX_LENGTH 512

Max CoAP header length (bytes)

OT_COAP_MAX_TOKEN_LENGTH

 OT_COAP_MAX_TOKEN_LENGTH 8

Max token length as specified (RFC 7252).

OT_DEFAULT_COAP_PORT

 OT_DEFAULT_COAP_PORT 5683

Default CoAP port, as specified in RFC 7252.