From 3e1b9aacb7e93e0071392ace6cb4dab6589c3203 Mon Sep 17 00:00:00 2001 From: Rongli Sun Date: Mon, 17 Dec 2018 22:44:18 +0800 Subject: [PATCH] [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. --- src/core/coap/coap.cpp | 24 +++--------------------- src/core/coap/coap.hpp | 18 ++---------------- 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 625f0905d..aa1855584 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -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, ©)) + switch (GetMatchedResponseCopy(aHeader, aMessageInfo, ©)) { case OT_ERROR_NOT_FOUND: break; diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index f7522416a..a0b9a7f47 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -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. *