From 9aabd7f813f82f4c7f1ed30fbbe45d7898d6b13c Mon Sep 17 00:00:00 2001 From: Giedrius Date: Tue, 9 May 2017 22:56:59 +0300 Subject: [PATCH] Generic uint CoAP option support (#1736) --- include/openthread/coap.h | 15 +++++++++++++ src/core/api/coap_api.cpp | 5 +++++ src/core/coap/coap_header.cpp | 42 +++++++++++------------------------ src/core/coap/coap_header.hpp | 14 ++++++++++++ 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/include/openthread/coap.h b/include/openthread/coap.h index 252294749..da0704303 100644 --- a/include/openthread/coap.h +++ b/include/openthread/coap.h @@ -257,6 +257,21 @@ void otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength); */ ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *aOption); +/** + * This function appends an unsigned integer CoAP option as specified in + * https://tools.ietf.org/html/rfc7252#section-3.2 + * + * @param[inout] aHeader A pointer to the CoAP header. + * @param[in] aNumber The CoAP Option number. + * @param[in] aValue The CoAP Option unsigned integer 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 otCoapHeaderAppendUintOption(otCoapHeader *aHeader, uint16_t aNumber, uint32_t aValue); + /** * This function appends an Observe option. * diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index 69e625c9f..66ea20d59 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -61,6 +61,11 @@ ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption * return static_cast(aHeader)->AppendOption(*static_cast(aOption)); } +ThreadError otCoapHeaderAppendUintOption(otCoapHeader *aHeader, uint16_t aNumber, uint32_t aValue) +{ + return static_cast(aHeader)->AppendUintOption(aNumber, aValue); +} + ThreadError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve) { return static_cast(aHeader)->AppendObserveOption(aObserve); diff --git a/src/core/coap/coap_header.cpp b/src/core/coap/coap_header.cpp index d09f91a90..ec6add2a8 100644 --- a/src/core/coap/coap_header.cpp +++ b/src/core/coap/coap_header.cpp @@ -268,17 +268,17 @@ exit: return error; } -ThreadError Header::AppendObserveOption(uint32_t aObserve) +ThreadError Header::AppendUintOption(uint16_t aNumber, uint32_t aValue) { Option coapOption; - aObserve = Encoding::BigEndian::HostSwap32(aObserve & 0xFFFFFF); - coapOption.mNumber = kCoapOptionObserve; + aValue = Encoding::BigEndian::HostSwap32(aValue); + coapOption.mNumber = aNumber; coapOption.mLength = 4; - coapOption.mValue = reinterpret_cast(&aObserve); + coapOption.mValue = reinterpret_cast(&aValue); - // skip preceding zeros, but make sure mLength is at least 1 - while (coapOption.mValue[0] == 0 && coapOption.mLength > 1) + // skip preceding zeros + while (coapOption.mValue[0] == 0 && coapOption.mLength > 0) { coapOption.mValue++; coapOption.mLength--; @@ -287,6 +287,11 @@ ThreadError Header::AppendObserveOption(uint32_t aObserve) return AppendOption(coapOption); } +ThreadError Header::AppendObserveOption(uint32_t aObserve) +{ + return AppendUintOption(kCoapOptionObserve, aObserve & 0xFFFFFF); +} + ThreadError Header::AppendUriPathOptions(const char *aUriPath) { ThreadError error = kThreadError_None; @@ -314,33 +319,12 @@ exit: ThreadError Header::AppendContentFormatOption(MediaType aType) { - Option coapOption; - uint8_t type = static_cast(aType); - - coapOption.mNumber = kCoapOptionContentFormat; - coapOption.mLength = 1; - coapOption.mValue = &type; - - return AppendOption(coapOption); + return AppendUintOption(kCoapOptionContentFormat, aType); } ThreadError Header::AppendMaxAgeOption(uint32_t aMaxAge) { - Option coapOption; - - aMaxAge = Encoding::BigEndian::HostSwap32(aMaxAge); - coapOption.mNumber = kCoapOptionMaxAge; - coapOption.mLength = 4; - coapOption.mValue = reinterpret_cast(&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); + return AppendUintOption(kCoapOptionMaxAge, aMaxAge); } ThreadError Header::AppendUriQueryOption(const char *aUriQuery) diff --git a/src/core/coap/coap_header.hpp b/src/core/coap/coap_header.hpp index 5748cdbe8..389812249 100644 --- a/src/core/coap/coap_header.hpp +++ b/src/core/coap/coap_header.hpp @@ -273,6 +273,20 @@ public: */ ThreadError AppendOption(const Option &aOption); + /** + * This method appends an unsigned integer CoAP option as specified in + * https://tools.ietf.org/html/rfc7252#section-3.2 + * + * @param[in] aNumber The CoAP Option number. + * @param[in] aValue The CoAP Option unsigned integer 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 AppendUintOption(uint16_t aNumber, uint32_t aValue); + /** * This method appends an Observe option. *