[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.
This commit is contained in:
Yakun Xu
2019-10-11 09:53:41 -07:00
committed by Jonathan Hui
parent f691ea3edf
commit f7801c7fab
4 changed files with 33 additions and 26 deletions
+16 -9
View File
@@ -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.
*
+2 -4
View File
@@ -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)
{
+4 -8
View File
@@ -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));
}
+11 -5
View File
@@ -64,6 +64,17 @@ void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode)
static_cast<Coap::Message *>(aMessage)->Init(aType, aCode);
}
otError otCoapMessageInitResponse(otMessage *aResponse, const otMessage *aRequest, otCoapType aType, otCoapCode aCode)
{
Coap::Message & response = *static_cast<Coap::Message *>(aResponse);
const Coap::Message &request = *static_cast<const Coap::Message *>(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<Coap::Message *>(aMessage)->SetToken(aToken, aTokenLength);
@@ -119,11 +130,6 @@ otError otCoapMessageSetPayloadMarker(otMessage *aMessage)
return static_cast<Coap::Message *>(aMessage)->SetPayloadMarker();
}
void otCoapMessageSetMessageId(otMessage *aMessage, uint16_t aMessageId)
{
return static_cast<Coap::Message *>(aMessage)->SetMessageId(aMessageId);
}
otCoapType otCoapMessageGetType(const otMessage *aMessage)
{
return static_cast<const Coap::Message *>(aMessage)->GetType();