[coap] reduce stack occupation (#3385)

In #3210, CoAP header is increased from 128 to 512. Multiple
local CoAP header variables are declared in nested calls during
CoAP process, easily causing limited stack space overflow.

This commit tries to reuse existing local header variable to
reduce stack usage.
This commit is contained in:
Rongli Sun
2018-12-18 09:29:39 -08:00
committed by Jonathan Hui
parent 2a75d30684
commit 3e1b9aacb7
2 changed files with 5 additions and 37 deletions
+3 -21
View File
@@ -171,7 +171,7 @@ otError Coap::SendMessage(Message & aMessage,
if ((header.GetType() == OT_COAP_TYPE_ACKNOWLEDGMENT || header.GetType() == OT_COAP_TYPE_RESET) &&
header.GetCode() != OT_COAP_CODE_EMPTY)
{
mResponsesQueue.EnqueueResponse(aMessage, aMessageInfo);
mResponsesQueue.EnqueueResponse(header, aMessage, aMessageInfo);
}
// Set Message Id if it was not already set.
@@ -809,32 +809,14 @@ exit:
return error;
}
otError ResponsesQueue::GetMatchedResponseCopy(const Message & aRequest,
const Ip6::MessageInfo &aMessageInfo,
Message ** aResponse)
void ResponsesQueue::EnqueueResponse(const Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
otError error = OT_ERROR_NONE;
Header header;
SuccessOrExit(error = header.FromMessage(aRequest, 0));
error = GetMatchedResponseCopy(header, aMessageInfo, aResponse);
exit:
return error;
}
void ResponsesQueue::EnqueueResponse(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
Header header;
Message * copy;
EnqueuedResponseHeader enqueuedResponseHeader(aMessageInfo);
uint16_t messageCount;
uint16_t bufferCount;
SuccessOrExit(header.FromMessage(aMessage, 0));
switch (GetMatchedResponseCopy(aMessage, aMessageInfo, &copy))
switch (GetMatchedResponseCopy(aHeader, aMessageInfo, &copy))
{
case OT_ERROR_NOT_FOUND:
break;
+2 -16
View File
@@ -363,11 +363,12 @@ public:
* response is not added.
* The CoAP response is copied before it is added to the cache.
*
* @param[in] aHeader A reference to a CoAP header.
* @param[in] aMessage The CoAP response to add to the cache.
* @param[in] aMessageInfo The message info corresponding to @p aMessage.
*
*/
void EnqueueResponse(Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void EnqueueResponse(const Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
/**
* Remove the oldest response from the cache.
@@ -395,21 +396,6 @@ public:
*/
otError GetMatchedResponseCopy(const Header &aHeader, const Ip6::MessageInfo &aMessageInfo, Message **aResponse);
/**
* Get a copy of CoAP response from the cache that matches given Message ID and source endpoint.
*
* @param[in] aRequest The CoAP message containing Message ID.
* @param[in] aMessageInfo The message info containing source endpoint address and port.
* @param[out] aResponse A pointer to a copy of a cached CoAP response matching given arguments.
*
* @retval OT_ERROR_NONE Matching response found and successfully created a copy.
* @retval OT_ERROR_NO_BUFS Matching response found but there is not sufficient buffer to create a copy.
* @retval OT_ERROR_NOT_FOUND Matching response not found.
* @retval OT_ERROR_PARSE Could not parse CoAP header in the request message.
*
*/
otError GetMatchedResponseCopy(const Message &aRequest, const Ip6::MessageInfo &aMessageInfo, Message **aResponse);
/**
* Get a reference to the cached CoAP responses queue.
*