Implement more CoAP Options (#1362)

* Implement more CoAP Options

* Observe
* Max-Age
* Uri-Query
This commit is contained in:
Giedrius
2017-02-27 09:08:50 -08:00
committed by Jonathan Hui
parent 250249ac38
commit b8ae672251
4 changed files with 136 additions and 0 deletions
+39
View File
@@ -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.
*
+49
View File
@@ -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);
+33
View File
@@ -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.
*
+15
View File
@@ -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();