diff --git a/include/openthread/coap.h b/include/openthread/coap.h index 4cfed8d8d..b7ccfef52 100644 --- a/include/openthread/coap.h +++ b/include/openthread/coap.h @@ -328,7 +328,7 @@ otError otCoapHeaderAppendUintOption(otCoapHeader *aHeader, uint16_t aNumber, ui otError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve); /** - * This function appends an Uri-Path option. + * This function appends a Uri-Path option. * * @param[inout] aHeader A pointer to the CoAP header. * @param[in] aUriPath A pointer to a NULL-terminated string. @@ -340,6 +340,19 @@ otError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve */ otError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath); +/** + * This function appends a Proxy-Uri option. + * + * @param[inout] aHeader A pointer to the CoAP header. + * @param[in] aUriPath A pointer to a NULL-terminated string. + * + * @retval OT_ERROR_NONE Successfully appended the option. + * @retval OT_ERROR_INVALID_ARGS The option type is not equal or greater than the last option type. + * @retval OT_ERROR_NO_BUFS The option length exceeds the buffer size. + * + */ +otError otCoapHeaderAppendProxyUriOption(otCoapHeader *aHeader, const char *aUriPath); + /** * This function appends a Max-Age option. * diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index b2c30bb54..1573181c9 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -83,6 +83,11 @@ otError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUri return static_cast(aHeader)->AppendUriPathOptions(aUriPath); } +otError otCoapHeaderAppendProxyUriOption(otCoapHeader *aHeader, const char *aUriPath) +{ + return static_cast(aHeader)->AppendProxyUriOption(aUriPath); +} + otError otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge) { return static_cast(aHeader)->AppendMaxAgeOption(aMaxAge); diff --git a/src/core/coap/coap_header.cpp b/src/core/coap/coap_header.cpp index 7c86d4889..4114480ae 100644 --- a/src/core/coap/coap_header.cpp +++ b/src/core/coap/coap_header.cpp @@ -275,6 +275,17 @@ otError Header::AppendUintOption(uint16_t aNumber, uint32_t aValue) return AppendOption(coapOption); } +otError Header::AppendStringOption(uint16_t aNumber, const char *aValue) +{ + Option coapOption; + + coapOption.mNumber = aNumber; + coapOption.mLength = static_cast(strlen(aValue)); + coapOption.mValue = reinterpret_cast(aValue); + + return AppendOption(coapOption); +} + otError Header::AppendObserveOption(uint32_t aObserve) { return AppendUintOption(OT_COAP_OPTION_OBSERVE, aObserve & 0xFFFFFF); @@ -297,14 +308,17 @@ otError Header::AppendUriPathOptions(const char *aUriPath) cur = end + 1; } - coapOption.mLength = static_cast(strlen(cur)); - coapOption.mValue = reinterpret_cast(cur); - SuccessOrExit(error = AppendOption(coapOption)); + SuccessOrExit(error = AppendStringOption(OT_COAP_OPTION_URI_PATH, cur)); exit: return error; } +otError Header::AppendProxyUriOption(const char *aProxyUri) +{ + return AppendStringOption(OT_COAP_OPTION_PROXY_URI, aProxyUri); +} + otError Header::AppendContentFormatOption(otCoapOptionContentFormat aContentFormat) { return AppendUintOption(OT_COAP_OPTION_CONTENT_FORMAT, static_cast(aContentFormat)); @@ -317,13 +331,7 @@ otError Header::AppendMaxAgeOption(uint32_t aMaxAge) otError Header::AppendUriQueryOption(const char *aUriQuery) { - Option coapOption; - - coapOption.mNumber = OT_COAP_OPTION_URI_QUERY; - coapOption.mLength = static_cast(strlen(aUriQuery)); - coapOption.mValue = reinterpret_cast(aUriQuery); - - return AppendOption(coapOption); + return AppendStringOption(OT_COAP_OPTION_URI_QUERY, aUriQuery); } const Header::Option *Header::GetFirstOption(void) diff --git a/src/core/coap/coap_header.hpp b/src/core/coap/coap_header.hpp index cf8e30e38..638ce6ff4 100644 --- a/src/core/coap/coap_header.hpp +++ b/src/core/coap/coap_header.hpp @@ -306,6 +306,19 @@ public: */ otError AppendUintOption(uint16_t aNumber, uint32_t aValue); + /** + * This method appends a string CoAP option. + * + * @param[in] aNumber The CoAP Option number. + * @param[in] aValue The CoAP Option string value. + * + * @retval OT_ERROR_NONE Successfully appended the option. + * @retval OT_ERROR_INVALID_ARGS The option type is not equal or greater than the last option type. + * @retval OT_ERROR_NO_BUFS The option length exceeds the buffer size. + * + */ + otError AppendStringOption(uint16_t aNumber, const char *aValue); + /** * This method appends an Observe option. * @@ -320,7 +333,7 @@ public: /** * This method appends a Uri-Path option. * - * @param[in] aUriPath A pointer to a NULL-terminated string. + * @param[in] aUriPath A pointer to a NULL-terminated string. * * @retval OT_ERROR_NONE Successfully appended the option. * @retval OT_ERROR_INVALID_ARGS The option type is not equal or greater than the last option type. @@ -329,6 +342,18 @@ public: */ otError AppendUriPathOptions(const char *aUriPath); + /** + * This method appends a Proxy-Uri option. + * + * @param[in] aProxyUri A pointer to a NULL-terminated string. + * + * @retval OT_ERROR_NONE Successfully appended the option. + * @retval OT_ERROR_INVALID_ARGS The option type is not equal or greater than the last option type. + * @retval OT_ERROR_NO_BUFS The option length exceeds the buffer size. + * + */ + otError AppendProxyUriOption(const char *aProxyUri); + /** * This method appends a Content-Format option. *