CoAP Server API (#970)

* CoAP server API

* Openthread refactoring to support updated resource handler

* CoAP Client - RequestMetadata bugfix
This commit is contained in:
Robert Lubos
2016-11-15 16:45:39 +08:00
committed by Jonathan Hui
parent f3f05faaf6
commit 208c4d35b9
36 changed files with 527 additions and 177 deletions
+186 -12
View File
@@ -53,6 +53,8 @@ extern "C" {
*
*/
#define OT_DEFAULT_COAP_PORT 5683 ///< Default CoAP port, as specified in RFC 7252
/**
* CoAP Type values.
*
@@ -141,7 +143,32 @@ typedef void (*otCoapResponseHandler)(void *aContext, otCoapHeader *aHeader, otM
ThreadError aResult);
/**
* This method initializes the CoAP header.
* This function pointer is called when a CoAP request with a given Uri-Path is received.
*
* @param[in] aContext A pointer to arbitrary context information.
* @param[in] aHeader A pointer to the CoAP header.
* @param[in] aMessage A pointer to the message.
* @param[in] aMessageInfo A pointer to the message info for @p aMessage.
*
*/
typedef void (*otCoapRequestHandler)(void *aContext, otCoapHeader *aHeader, otMessage aMessage,
const otMessageInfo *aMessageInfo);
/**
* This structure represents a CoAP resource.
*
*/
typedef struct otCoapResource
{
const char *mUriPath;
otCoapRequestHandler mHandler;
void *mContext;
struct otCoapResource *mNext;
} otCoapResource;
#if OPENTHREAD_ENABLE_APPLICATION_COAP
/**
* This function initializes the CoAP header.
*
* @param[inout] aHeader A pointer to the CoAP header to initialize.
* @param[in] aType CoAP message type.
@@ -151,7 +178,7 @@ typedef void (*otCoapResponseHandler)(void *aContext, otCoapHeader *aHeader, otM
void otCoapHeaderInit(otCoapHeader *aHeader, otCoapType aType, otCoapCode aCode);
/**
* This method sets the Token value and length in a header.
* This function sets the Token value and length in a header.
*
* @param[inout] aHeader A pointer to the CoAP header.
* @param[in] aToken A pointer to the Token value.
@@ -161,7 +188,16 @@ void otCoapHeaderInit(otCoapHeader *aHeader, otCoapType aType, otCoapCode aCode)
void otCoapHeaderSetToken(otCoapHeader *aHeader, const uint8_t *aToken, uint8_t aTokenLength);
/**
* This method appends a CoAP option in a header.
* This function sets the Token length and randomizes its value.
*
* @param[inout] aHeader A pointer to the CoAP header.
* @param[in] aTokenLength The Length of a Token to set.
*
*/
void otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength);
/**
* This function appends a CoAP option in a header.
*
* @param[inout] aHeader A pointer to the CoAP header.
* @param[in] aOption A pointer to the CoAP option.
@@ -174,7 +210,20 @@ void otCoapHeaderSetToken(otCoapHeader *aHeader, const uint8_t *aToken, uint8_t
ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *aOption);
/**
* This method adds Payload Marker indicating beginning of the payload to the CoAP header.
* This function appends an Uri-Path option.
*
* @param[inout] aHeader A pointer to the CoAP header.
* @param[in] aUriPath A pointer to a NULL-terminated string.
*
* @retval kThreadError_None Successfully appended the option.
* @retval kThreadError_InvalidArgs The option type is not equal or greater than the last option type.
* @retval kThreadError_NoBufs The option length exceeds the buffer size.
*
*/
ThreadError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath);
/**
* This function adds Payload Marker indicating beginning of the payload to the CoAP header.
*
* @param[inout] aHeader A pointer to the CoAP header.
*
@@ -185,27 +234,86 @@ ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *
void otCoapHeaderSetPayloadMarker(otCoapHeader *aHeader);
/**
* This method returns a pointer to the current option.
* This function sets the Message ID value.
*
* @param[in] aHeader A pointer to the CoAP header.
* @param[in] aMessageId The Message ID value.
*
*/
void otCoapHeaderSetMessageId(otCoapHeader *aHeader, uint16_t aMessageId);
/**
* This function returns the Type value.
*
* @param[in] aHeader A pointer to the CoAP header.
*
* @returns The Type value.
*
*/
otCoapType otCoapHeaderGetType(const otCoapHeader *aHeader);
/**
* This function returns the Code value.
*
* @param[in] aHeader A pointer to the CoAP header.
*
* @returns The Code value.
*
*/
otCoapCode otCoapHeaderGetCode(const otCoapHeader *aHeader);
/**
* This function returns the Message ID value.
*
* @param[in] aHeader A pointer to the CoAP header.
*
* @returns The Message ID value.
*
*/
uint16_t otCoapHeaderGetMessageId(const otCoapHeader *aHeader);
/**
* This function returns the Token length.
*
* @param[in] aHeader A pointer to the CoAP header.
*
* @returns The Token length.
*
*/
uint8_t otCoapHeaderGetTokenLength(const otCoapHeader *aHeader);
/**
* This function returns a pointer to the Token value.
*
* @param[in] aHeader A pointer to the CoAP header.
*
* @returns A pointer to the Token value.
*
*/
const uint8_t *otCoapHeaderGetToken(const otCoapHeader *aHeader);
/**
* This function returns a pointer to the current option.
*
* @param[in] aHeader A pointer to the CoAP header.
*
* @returns A pointer to the current option. If no option is present NULL pointer is returned.
*
*/
const otCoapOption *otCoapGetCurrentOption(const otCoapHeader *aHeader);
const otCoapOption *otCoapHeaderGetCurrentOption(const otCoapHeader *aHeader);
/**
* This method returns a pointer to the next option.
* This function returns a pointer to the next option.
*
* @param[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.
*
*/
const otCoapOption *otCoapGetNextOption(otCoapHeader *aHeader);
const otCoapOption *otCoapHeaderGetNextOption(otCoapHeader *aHeader);
/**
* This method creates a new message with a CoAP header.
* This function creates a new message with a CoAP header.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aHeader A pointer to a CoAP header that is used to create the message.
@@ -213,10 +321,10 @@ const otCoapOption *otCoapGetNextOption(otCoapHeader *aHeader);
* @returns A pointer to the message or NULL if failed to allocate message.
*
*/
otMessage otNewCoapMessage(otInstance *aInstance, const otCoapHeader *aHeader);
otMessage otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader);
/**
* This method sends a CoAP message.
* This function sends a CoAP request.
*
* If a response for a request is expected, respective function and contex information should be provided.
* If no response is expected, these arguments should be NULL pointers.
@@ -231,9 +339,75 @@ otMessage otNewCoapMessage(otInstance *aInstance, const otCoapHeader *aHeader);
* @retval kThreadError_NoBufs Failed to allocate retransmission data.
*
*/
ThreadError otSendCoapMessage(otInstance *aInstance, otMessage aMessage, const otMessageInfo *aMessageInfo,
ThreadError otCoapSendRequest(otInstance *aInstance, otMessage aMessage, const otMessageInfo *aMessageInfo,
otCoapResponseHandler aHandler, void *aContext);
/**
* This function starts the CoAP server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
*
* @retval kThreadError_None Successfully started the CoAP server.
*
*/
ThreadError otCoapServerStart(otInstance *aInstance);
/**
* This function stops the CoAP server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
*
* @retval kThreadError_None Successfully stopped the CoAP server.
*
*/
ThreadError otCoapServerStop(otInstance *aInstance);
/**
* This function sets CoAP server's port number.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aPort A port number to set.
*
* @retval kThreadError_None Binding with a port succeeded.
*
*/
ThreadError otCoapServerSetPort(otInstance *aInstance, uint16_t aPort);
/**
* This function adds a resource to the CoAP server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aResource A pointer to the resource.
*
* @retval kThreadError_None Successfully added @p aResource.
* @retval kThreadError_Already The @p aResource was already added.
*
*/
ThreadError otCoapServerAddResource(otInstance *aInstance, otCoapResource *aResource);
/**
* This function removes a resource from the CoAP server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aResource A pointer to the resource.
*
*/
void otCoapServerRemoveResource(otInstance *aInstance, otCoapResource *aResource);
/**
* This function sends a CoAP response from the server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aMessage A pointer to the CoAP response to send.
* @param[in] aMessageInfo A pointer to the message info associated with @p aMessage.
*
* @retval kThreadError_None Successfully enqueued the CoAP response message.
* @retval kThreadError_NoBufs Insufficient buffers available to send the CoAP response.
*
*/
ThreadError otCoapSendResponse(otInstance *aInstance, otMessage aMessage, const otMessageInfo *aMessageInfo);
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP
/**
* @}
*