From 0208d10563aa21c518092985c78ecf9cd223ab74 Mon Sep 17 00:00:00 2001 From: "Duda, Lukasz" Date: Thu, 25 Apr 2019 22:15:42 +0200 Subject: [PATCH] [metadata] enhance ReadFrom and RemoveFrom methods (#3786) --- src/core/coap/coap.cpp | 5 ++-- src/core/coap/coap.hpp | 37 ++++++++++-------------------- src/core/meshcop/joiner_router.hpp | 16 ++++++------- src/core/net/dns_client.hpp | 20 ++++++++-------- src/core/net/ip6_mpl.cpp | 2 +- src/core/net/ip6_mpl.hpp | 25 +++++++++++--------- src/core/net/sntp_client.cpp | 4 +--- src/core/net/sntp_client.hpp | 10 ++++---- src/core/thread/mle.hpp | 20 ++++++++-------- 9 files changed, 63 insertions(+), 76 deletions(-) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 150332d53..f0833857e 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -732,9 +732,8 @@ otError ResponsesQueue::GetMatchedResponseCopy(const Message & aRequest, continue; } - VerifyOrExit((*aResponse = message->Clone()) != NULL, error = OT_ERROR_NO_BUFS); - - EnqueuedResponseHeader::RemoveFrom(**aResponse); + VerifyOrExit((*aResponse = message->Clone(message->GetLength() - sizeof(EnqueuedResponseHeader))) != NULL, + error = OT_ERROR_NO_BUFS); error = OT_ERROR_NONE; break; diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 6d95af45a..25a2943fc 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -131,20 +131,20 @@ public: * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. * */ - otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); }; + otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); } /** * This method reads request data from the message. * * @param[in] aMessage A reference to the message. * - * @returns The number of bytes read. - * */ - uint16_t ReadFrom(const Message &aMessage) + void ReadFrom(const Message &aMessage) { - return aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - }; + uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + assert(length == sizeof(*this)); + OT_UNUSED_VARIABLE(length); + } /** * This method updates request data in the message. @@ -167,7 +167,7 @@ public: * @retval TRUE If the message shall be sent before the given time. * @retval FALSE Otherwise. */ - bool IsEarlier(uint32_t aTime) const { return (static_cast(aTime - mNextTimerShot) >= 0); }; + bool IsEarlier(uint32_t aTime) const { return (static_cast(aTime - mNextTimerShot) >= 0); } /** * This method checks if the message shall be sent after the given time. @@ -177,7 +177,7 @@ public: * @retval TRUE If the message shall be sent after the given time. * @retval FALSE Otherwise. */ - bool IsLater(uint32_t aTime) const { return (static_cast(aTime - mNextTimerShot) < 0); }; + bool IsLater(uint32_t aTime) const { return (static_cast(aTime - mNextTimerShot) < 0); } private: Ip6::Address mSourceAddress; ///< IPv6 address of the message source. @@ -288,25 +288,12 @@ public: * * @param[in] aMessage A reference to the message. * - * @returns The number of bytes read. - * */ - uint16_t ReadFrom(const Message &aMessage) + void ReadFrom(const Message &aMessage) { - return aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - } - - /** - * This method removes metadata from the message. - * - * @param[in] aMessage A reference to the message. - * - */ - static void RemoveFrom(Message &aMessage) - { - otError error = aMessage.SetLength(aMessage.GetLength() - sizeof(EnqueuedResponseHeader)); - assert(error == OT_ERROR_NONE); - OT_UNUSED_VARIABLE(error); + uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + assert(length == sizeof(*this)); + OT_UNUSED_VARIABLE(length); } /** diff --git a/src/core/meshcop/joiner_router.hpp b/src/core/meshcop/joiner_router.hpp index 62c6b5555..11777ecc9 100644 --- a/src/core/meshcop/joiner_router.hpp +++ b/src/core/meshcop/joiner_router.hpp @@ -163,12 +163,12 @@ public: * * @param[in] aMessage A reference to the message. * - * @returns The number of bytes read. - * */ - uint16_t ReadFrom(Message &aMessage) + void ReadFrom(const Message &aMessage) { - return aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + assert(length == sizeof(*this)); + OT_UNUSED_VARIABLE(length); } /** @@ -176,12 +176,12 @@ public: * * @param[in] aMessage A reference to the message. * - * @retval OT_ERROR_NONE Successfully removed the header. - * */ - static otError RemoveFrom(Message &aMessage) + static void RemoveFrom(Message &aMessage) { - return aMessage.SetLength(aMessage.GetLength() - sizeof(DelayedJoinEntHeader)); + otError error = aMessage.SetLength(aMessage.GetLength() - sizeof(DelayedJoinEntHeader)); + assert(error == OT_ERROR_NONE); + OT_UNUSED_VARIABLE(error); } /** diff --git a/src/core/net/dns_client.hpp b/src/core/net/dns_client.hpp index 56efc941c..90bf75fa6 100644 --- a/src/core/net/dns_client.hpp +++ b/src/core/net/dns_client.hpp @@ -61,7 +61,7 @@ public: * Default constructor for the object. * */ - QueryMetadata(void) { memset(this, 0, sizeof(*this)); }; + QueryMetadata(void) { memset(this, 0, sizeof(*this)); } /** * This constructor initializes the object with specific values. @@ -75,7 +75,7 @@ public: memset(this, 0, sizeof(*this)); mResponseHandler = aHandler; mResponseContext = aContext; - }; + } /** * This method appends request data to the message. @@ -86,20 +86,20 @@ public: * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. * */ - otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); }; + otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); } /** * This method reads request data from the message. * * @param[in] aMessage A reference to the message. * - * @returns The number of bytes read. - * */ - uint16_t ReadFrom(const Message &aMessage) + void ReadFrom(const Message &aMessage) { - return aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - }; + uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + assert(length == sizeof(*this)); + OT_UNUSED_VARIABLE(length); + } /** * This method updates request data in the message. @@ -122,7 +122,7 @@ public: * @retval TRUE If the message shall be sent before the given time. * @retval FALSE Otherwise. */ - bool IsEarlier(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) > 0); }; + bool IsEarlier(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) > 0); } /** * This method checks if the message shall be sent after the given time. @@ -132,7 +132,7 @@ public: * @retval TRUE If the message shall be sent after the given time. * @retval FALSE Otherwise. */ - bool IsLater(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) < 0); }; + bool IsLater(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) < 0); } private: const char * mHostname; ///< A hostname to be find. diff --git a/src/core/net/ip6_mpl.cpp b/src/core/net/ip6_mpl.cpp index 64270a973..dcc4fb0b8 100644 --- a/src/core/net/ip6_mpl.cpp +++ b/src/core/net/ip6_mpl.cpp @@ -393,7 +393,7 @@ void Mpl::HandleRetransmissionTimer(void) } // Remove the extra metadata from the MPL Data Message. - messageMetadata.RemoveFrom(*message); + MplBufferedMessageMetadata::RemoveFrom(*message); Get().EnqueueDatagram(*message); } else diff --git a/src/core/net/ip6_mpl.hpp b/src/core/net/ip6_mpl.hpp index e4dd4f537..de5783a22 100644 --- a/src/core/net/ip6_mpl.hpp +++ b/src/core/net/ip6_mpl.hpp @@ -271,30 +271,33 @@ public: * @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message. * */ - otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); }; + otError AppendTo(Message &aMessage) const { return aMessage.Append(this, sizeof(*this)); } /** * This method reads request data from the message. * * @param[in] aMessage A reference to the message. * - * @returns The number of bytes that have been read. - * */ - uint16_t ReadFrom(const Message &aMessage) + void ReadFrom(const Message &aMessage) { - return aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - }; + uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + assert(length == sizeof(*this)); + OT_UNUSED_VARIABLE(length); + } /** * This method removes MPL Buffered Message metadata from the message. * * @param[in] aMessage A reference to the message. * - * @retval OT_ERROR_NONE Successfully removed the header. - * */ - otError RemoveFrom(Message &aMessage) { return aMessage.SetLength(aMessage.GetLength() - sizeof(*this)); }; + static void RemoveFrom(Message &aMessage) + { + otError error = aMessage.SetLength(aMessage.GetLength() - sizeof(MplBufferedMessageMetadata)); + assert(error == OT_ERROR_NONE); + OT_UNUSED_VARIABLE(error); + } /** * This method updates MPL Buffered Message metadata in the message. @@ -317,7 +320,7 @@ public: * @retval TRUE If the message shall be sent before the given time. * @retval FALSE Otherwise. */ - bool IsEarlier(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) > 0); }; + bool IsEarlier(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) > 0); } /** * This method checks if the message shall be sent after the given time. @@ -327,7 +330,7 @@ public: * @retval TRUE If the message shall be sent after the given time. * @retval FALSE Otherwise. */ - bool IsLater(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) < 0); }; + bool IsLater(uint32_t aTime) const { return (static_cast(aTime - mTransmissionTime) < 0); } /** * This method returns the MPL Seed Id value. diff --git a/src/core/net/sntp_client.cpp b/src/core/net/sntp_client.cpp index ccd58d851..a1ec489f0 100644 --- a/src/core/net/sntp_client.cpp +++ b/src/core/net/sntp_client.cpp @@ -234,12 +234,10 @@ Message *Client::FindRelatedQuery(const Header &aResponseHeader, QueryMetadata & while (message != NULL) { // Read originate timestamp. - uint16_t count = aQueryMetadata.ReadFrom(*message); - assert(count == sizeof(aQueryMetadata)); + aQueryMetadata.ReadFrom(*message); if (aQueryMetadata.mTransmitTimestamp == aResponseHeader.GetOriginateTimestampSeconds()) { - aQueryMetadata.ReadFrom(*message); ExitNow(); } diff --git a/src/core/net/sntp_client.hpp b/src/core/net/sntp_client.hpp index d4ecaf5f8..05f0385bd 100644 --- a/src/core/net/sntp_client.hpp +++ b/src/core/net/sntp_client.hpp @@ -463,13 +463,13 @@ public: * * @param[in] aMessage A reference to the message. * - * @returns The number of bytes read. - * */ - uint16_t ReadFrom(const Message &aMessage) + void ReadFrom(const Message &aMessage) { - return aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - }; + uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + assert(length == sizeof(*this)); + OT_UNUSED_VARIABLE(length); + } /** * This method updates request data in the message. diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 4b3481dbb..83c6319c3 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -398,26 +398,26 @@ public: * * @param[in] aMessage A reference to the message. * - * @returns The number of bytes read. - * */ - uint16_t ReadFrom(Message &aMessage) + void ReadFrom(const Message &aMessage) { - return aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); - }; + uint16_t length = aMessage.Read(aMessage.GetLength() - sizeof(*this), sizeof(*this), this); + assert(length == sizeof(*this)); + OT_UNUSED_VARIABLE(length); + } /** * This method removes delayed response header from the message. * * @param[in] aMessage A reference to the message. * - * @retval OT_ERROR_NONE Successfully removed the header. - * */ - static otError RemoveFrom(Message &aMessage) + static void RemoveFrom(Message &aMessage) { - return aMessage.SetLength(aMessage.GetLength() - sizeof(DelayedResponseHeader)); - }; + otError error = aMessage.SetLength(aMessage.GetLength() - sizeof(DelayedResponseHeader)); + assert(error == OT_ERROR_NONE); + OT_UNUSED_VARIABLE(error); + } /** * This method returns a time when the message shall be sent.