mirror of
https://github.com/espressif/openthread.git
synced 2026-10-17 13:17:44 +00:00
Implement more CoAP Options (#1362)
* Implement more CoAP Options * Observe * Max-Age * Uri-Query
This commit is contained in:
@@ -91,8 +91,11 @@ typedef enum otCoapCode
|
||||
*/
|
||||
typedef enum otCoapOptionType
|
||||
{
|
||||
kCoapOptionObserve = 6, ///< Observe
|
||||
kCoapOptionUriPath = 11, ///< Uri-Path
|
||||
kCoapOptionContentFormat = 12, ///< Content-Format
|
||||
kCoapOptionMaxAge = 14, ///< Max-Age
|
||||
kCoapOptionUriQuery = 15, ///< Uri-Query
|
||||
} otCoapOptionType;
|
||||
|
||||
/**
|
||||
@@ -214,6 +217,18 @@ void otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength);
|
||||
*/
|
||||
ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *aOption);
|
||||
|
||||
/**
|
||||
* This function appends an Observe option.
|
||||
*
|
||||
* @param[inout] aHeader A pointer to the CoAP header.
|
||||
* @param[in] aObserve Observe field value.
|
||||
*
|
||||
* @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 otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve);
|
||||
|
||||
/**
|
||||
* This function appends an Uri-Path option.
|
||||
*
|
||||
@@ -227,6 +242,30 @@ ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *
|
||||
*/
|
||||
ThreadError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath);
|
||||
|
||||
/**
|
||||
* This function appends a Max-Age option.
|
||||
*
|
||||
* @param[inout] aHeader A pointer to the CoAP header.
|
||||
* @param[in] aMaxAge The Max-Age value.
|
||||
*
|
||||
* @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 otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge);
|
||||
|
||||
/**
|
||||
* This function appends a single Uri-Query option.
|
||||
*
|
||||
* @param[inout] aHeader A pointer to the CoAP header.
|
||||
* @param[in] aUriQuery A pointer to NULL-terminated string, which should contain a single key=value pair.
|
||||
*
|
||||
* @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 otCoapHeaderAppendUriQueryOption(otCoapHeader *aHeader, const char *aUriQuery);
|
||||
|
||||
/**
|
||||
* This function adds Payload Marker indicating beginning of the payload to the CoAP header.
|
||||
*
|
||||
|
||||
@@ -244,6 +244,25 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
ThreadError Header::AppendObserveOption(uint32_t aObserve)
|
||||
{
|
||||
Option coapOption;
|
||||
|
||||
aObserve = Encoding::BigEndian::HostSwap32(aObserve & 0xFFFFFF);
|
||||
coapOption.mNumber = kCoapOptionObserve;
|
||||
coapOption.mLength = 4;
|
||||
coapOption.mValue = reinterpret_cast<uint8_t *>(&aObserve);
|
||||
|
||||
// skip preceding zeros, but make sure mLength is at least 1
|
||||
while (coapOption.mValue[0] == 0 && coapOption.mLength > 1)
|
||||
{
|
||||
coapOption.mValue++;
|
||||
coapOption.mLength--;
|
||||
}
|
||||
|
||||
return AppendOption(coapOption);
|
||||
}
|
||||
|
||||
ThreadError Header::AppendUriPathOptions(const char *aUriPath)
|
||||
{
|
||||
ThreadError error = kThreadError_None;
|
||||
@@ -281,6 +300,36 @@ ThreadError Header::AppendContentFormatOption(MediaType aType)
|
||||
return AppendOption(coapOption);
|
||||
}
|
||||
|
||||
ThreadError Header::AppendMaxAgeOption(uint32_t aMaxAge)
|
||||
{
|
||||
Option coapOption;
|
||||
|
||||
aMaxAge = Encoding::BigEndian::HostSwap32(aMaxAge);
|
||||
coapOption.mNumber = kCoapOptionMaxAge;
|
||||
coapOption.mLength = 4;
|
||||
coapOption.mValue = reinterpret_cast<uint8_t *>(&aMaxAge);
|
||||
|
||||
// skip preceding zeros, but make sure mLength is at least 1
|
||||
while (coapOption.mValue[0] == 0 && coapOption.mLength > 1)
|
||||
{
|
||||
coapOption.mValue++;
|
||||
coapOption.mLength--;
|
||||
}
|
||||
|
||||
return AppendOption(coapOption);
|
||||
}
|
||||
|
||||
ThreadError Header::AppendUriQueryOption(const char *aUriQuery)
|
||||
{
|
||||
Option coapOption;
|
||||
|
||||
coapOption.mNumber = kCoapOptionUriQuery;
|
||||
coapOption.mLength = static_cast<uint16_t>(strlen(aUriQuery));
|
||||
coapOption.mValue = reinterpret_cast<const uint8_t *>(aUriQuery);
|
||||
|
||||
return AppendOption(coapOption);
|
||||
}
|
||||
|
||||
const Header::Option *Header::GetCurrentOption(void) const
|
||||
{
|
||||
return static_cast<const Header::Option *>(&mOption);
|
||||
|
||||
@@ -273,6 +273,17 @@ public:
|
||||
*/
|
||||
ThreadError AppendOption(const Option &aOption);
|
||||
|
||||
/**
|
||||
* This method appends an Observe option.
|
||||
*
|
||||
* @param[in] aObserve Observe field value.
|
||||
*
|
||||
* @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 AppendObserveOption(uint32_t aObserve);
|
||||
|
||||
/**
|
||||
* This method appends a Uri-Path option.
|
||||
*
|
||||
@@ -306,6 +317,28 @@ public:
|
||||
*/
|
||||
ThreadError AppendContentFormatOption(MediaType aType);
|
||||
|
||||
/**
|
||||
* This method appends a Max-Age option.
|
||||
*
|
||||
* @param[in] aMaxAge The Max-Age value.
|
||||
*
|
||||
* @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 AppendMaxAgeOption(uint32_t aMaxAge);
|
||||
|
||||
/**
|
||||
* This method appends a single Uri-Query option.
|
||||
*
|
||||
* @param[in] aUriQuery A pointer to NULL-terminated string, which should contain a single key=value pair.
|
||||
*
|
||||
* @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 AppendUriQueryOption(const char *aUriQuery);
|
||||
|
||||
/**
|
||||
* This method returns a pointer to the current option.
|
||||
*
|
||||
|
||||
@@ -1987,11 +1987,26 @@ ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *
|
||||
return static_cast<Coap::Header *>(aHeader)->AppendOption(*static_cast<const Coap::Header::Option *>(aOption));
|
||||
}
|
||||
|
||||
ThreadError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve)
|
||||
{
|
||||
return static_cast<Coap::Header *>(aHeader)->AppendObserveOption(aObserve);
|
||||
}
|
||||
|
||||
ThreadError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath)
|
||||
{
|
||||
return static_cast<Coap::Header *>(aHeader)->AppendUriPathOptions(aUriPath);
|
||||
}
|
||||
|
||||
ThreadError otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge)
|
||||
{
|
||||
return static_cast<Coap::Header *>(aHeader)->AppendMaxAgeOption(aMaxAge);
|
||||
}
|
||||
|
||||
ThreadError otCoapHeaderAppendUriQueryOption(otCoapHeader *aHeader, const char *aUriQuery)
|
||||
{
|
||||
return static_cast<Coap::Header *>(aHeader)->AppendUriQueryOption(aUriQuery);
|
||||
}
|
||||
|
||||
void otCoapHeaderSetPayloadMarker(otCoapHeader *aHeader)
|
||||
{
|
||||
static_cast<Coap::Header *>(aHeader)->SetPayloadMarker();
|
||||
|
||||
Reference in New Issue
Block a user