From f7801c7fabc3c3f770a06082f0cd1814ccad6a09 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Sat, 12 Oct 2019 00:53:41 +0800 Subject: [PATCH] [coap] remove otCoapMessageSetMessageId() (#4227) Message ID is managed by CoAP agent. Allowing user setting it may result in invalid usage. This commit removes this API and adds a new API to initialize CoAP response message. --- include/openthread/coap.h | 25 ++++++++++++++++--------- src/cli/cli_coap.cpp | 6 ++---- src/cli/cli_coap_secure.cpp | 12 ++++-------- src/core/api/coap_api.cpp | 16 +++++++++++----- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/include/openthread/coap.h b/include/openthread/coap.h index b6fd55583..b7fe938fb 100644 --- a/include/openthread/coap.h +++ b/include/openthread/coap.h @@ -342,6 +342,22 @@ typedef struct otCoapResource */ void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode); +/** + * This function initializes a response message. + * + * @note Both message ID and token are set according to @p aRequest. + * + * @param[inout] aResponse A pointer to the CoAP response message. + * @param[in] aRequest A pointer to the CoAP request message. + * @param[in] aType CoAP message type. + * @param[in] aCode CoAP message code. + * + * @retval OT_ERROR_NONE Successfully initialized the response message. + * @retval OT_ERROR_NO_BUFS Insufficient message buffers available to initialize the response message. + * + */ +otError otCoapMessageInitResponse(otMessage *aResponse, const otMessage *aRequest, otCoapType aType, otCoapCode aCode); + /** * This function sets the Token value and length in a header. * @@ -490,15 +506,6 @@ otError otCoapMessageAppendUriQueryOption(otMessage *aMessage, const char *aUriQ */ otError otCoapMessageSetPayloadMarker(otMessage *aMessage); -/** - * This function sets the Message ID value. - * - * @param[in] aMessage A pointer to the CoAP message. - * @param[in] aMessageId The Message ID value. - * - */ -void otCoapMessageSetMessageId(otMessage *aMessage, uint16_t aMessageId); - /** * This function returns the Type value. * diff --git a/src/cli/cli_coap.cpp b/src/cli/cli_coap.cpp index 15128ae1e..e9e660dc8 100644 --- a/src/cli/cli_coap.cpp +++ b/src/cli/cli_coap.cpp @@ -330,10 +330,8 @@ void Coap::HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo) responseMessage = otCoapNewMessage(mInterpreter.mInstance, NULL); VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS); - otCoapMessageInit(responseMessage, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode); - otCoapMessageSetMessageId(responseMessage, otCoapMessageGetMessageId(aMessage)); - SuccessOrExit(error = otCoapMessageSetToken(responseMessage, otCoapMessageGetToken(aMessage), - otCoapMessageGetTokenLength(aMessage))); + SuccessOrExit( + error = otCoapMessageInitResponse(responseMessage, aMessage, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode)); if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET) { diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index c5a780e71..a8d3ec640 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -496,10 +496,8 @@ void CoapSecure::HandleRequest(otMessage *aMessage, const otMessageInfo *aMessag responseMessage = otCoapNewMessage(mInterpreter.mInstance, NULL); VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS); - otCoapMessageInit(responseMessage, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode); - otCoapMessageSetMessageId(responseMessage, otCoapMessageGetMessageId(aMessage)); - SuccessOrExit(error = otCoapMessageSetToken(responseMessage, otCoapMessageGetToken(aMessage), - otCoapMessageGetTokenLength(aMessage))); + SuccessOrExit( + error = otCoapMessageInitResponse(responseMessage, aMessage, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode)); if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_GET) { @@ -571,10 +569,8 @@ void CoapSecure::DefaultHandler(otMessage *aMessage, const otMessageInfo *aMessa responseMessage = otCoapNewMessage(mInterpreter.mInstance, NULL); VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS); - otCoapMessageInit(responseMessage, OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_NOT_FOUND); - otCoapMessageSetMessageId(responseMessage, otCoapMessageGetMessageId(aMessage)); - SuccessOrExit(error = otCoapMessageSetToken(responseMessage, otCoapMessageGetToken(aMessage), - otCoapMessageGetTokenLength(aMessage))); + SuccessOrExit(error = otCoapMessageInitResponse(responseMessage, aMessage, OT_COAP_TYPE_NON_CONFIRMABLE, + OT_COAP_CODE_NOT_FOUND)); SuccessOrExit(error = otCoapSecureSendResponse(mInterpreter.mInstance, responseMessage, aMessageInfo)); } diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index faf7c8d2b..60ddaca99 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -64,6 +64,17 @@ void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode) static_cast(aMessage)->Init(aType, aCode); } +otError otCoapMessageInitResponse(otMessage *aResponse, const otMessage *aRequest, otCoapType aType, otCoapCode aCode) +{ + Coap::Message & response = *static_cast(aResponse); + const Coap::Message &request = *static_cast(aRequest); + + response.Init(aType, aCode); + response.SetMessageId(request.GetMessageId()); + + return response.SetToken(request.GetToken(), request.GetTokenLength()); +} + otError otCoapMessageSetToken(otMessage *aMessage, const uint8_t *aToken, uint8_t aTokenLength) { return static_cast(aMessage)->SetToken(aToken, aTokenLength); @@ -119,11 +130,6 @@ otError otCoapMessageSetPayloadMarker(otMessage *aMessage) return static_cast(aMessage)->SetPayloadMarker(); } -void otCoapMessageSetMessageId(otMessage *aMessage, uint16_t aMessageId) -{ - return static_cast(aMessage)->SetMessageId(aMessageId); -} - otCoapType otCoapMessageGetType(const otMessage *aMessage) { return static_cast(aMessage)->GetType();