From 8160f3423b2480290ddfbab343459bdf8b9b3d37 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 18 May 2020 21:44:30 -0700 Subject: [PATCH] [message] adding Message::Settings (#4977) The `Message::Settings` mirrors `otMessageSettings` and is used when allocating a new `Message`. --- src/core/api/coap_api.cpp | 11 +--- src/core/api/ip6_api.cpp | 21 ++++---- src/core/api/udp_api.cpp | 11 +--- src/core/coap/coap.cpp | 2 +- src/core/coap/coap.hpp | 10 ++-- src/core/common/message.cpp | 34 ++++++------- src/core/common/message.hpp | 82 +++++++++++++++++++++++++++--- src/core/meshcop/joiner_router.cpp | 6 +-- src/core/meshcop/meshcop.hpp | 8 ++- src/core/net/icmp6.cpp | 4 +- src/core/net/ip6.cpp | 29 ++++++----- src/core/net/ip6.hpp | 28 ++++++---- src/core/net/udp6.cpp | 4 +- src/core/net/udp6.hpp | 11 ++-- src/core/thread/mle.cpp | 4 +- src/core/thread/mle.hpp | 3 +- 16 files changed, 157 insertions(+), 111 deletions(-) diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index d4dcf86a7..d0579acb3 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -45,18 +45,9 @@ using namespace ot; otMessage *otCoapNewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { - Message * message; Instance &instance = *static_cast(aInstance); - if (aSettings != NULL) - { - VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL); - } - - message = instance.GetApplicationCoap().NewMessage(aSettings); - -exit: - return message; + return instance.GetApplicationCoap().NewMessage(Message::Settings(aSettings)); } void otCoapMessageInit(otMessage *aMessage, otCoapType aType, otCoapCode aCode) diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index e6f4f4017..86b1cd29f 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -170,17 +170,8 @@ otError otIp6Send(otInstance *aInstance, otMessage *aMessage) otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { Instance &instance = *static_cast(aInstance); - Message * message; - if (aSettings != NULL) - { - VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL); - } - - message = instance.Get().NewMessage(0, aSettings); - -exit: - return message; + return instance.Get().NewMessage(0, Message::Settings(aSettings)); } otMessage *otIp6NewMessageFromBuffer(otInstance * aInstance, @@ -191,9 +182,15 @@ otMessage *otIp6NewMessageFromBuffer(otInstance * aInstance, Instance &instance = *static_cast(aInstance); Message * message; - VerifyOrExit((message = instance.Get().NewMessage(aData, aDataLength, aSettings)) != NULL, OT_NOOP); + if (aSettings != NULL) + { + message = instance.Get().NewMessage(aData, aDataLength, Message::Settings(aSettings)); + } + else + { + message = instance.Get().NewMessage(aData, aDataLength); + } -exit: return message; } diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp index f5178b09d..144dd4c1a 100644 --- a/src/core/api/udp_api.cpp +++ b/src/core/api/udp_api.cpp @@ -45,17 +45,8 @@ using namespace ot; otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { Instance &instance = *static_cast(aInstance); - Message * message; - if (aSettings != NULL) - { - VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL); - } - - message = instance.Get().NewMessage(0, aSettings); - -exit: - return message; + return instance.Get().NewMessage(0, Message::Settings(aSettings)); } otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 8158e5f0e..a9361a23e 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -113,7 +113,7 @@ void CoapBase::SetInterceptor(Interceptor aInterceptor, void *aContext) mContext = aContext; } -Message *CoapBase::NewMessage(const otMessageSettings *aSettings) +Message *CoapBase::NewMessage(const Message::Settings &aSettings) { Message *message = NULL; diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 834cb3e69..00ba15cde 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -330,15 +330,12 @@ public: /** * This method creates a new message with a CoAP header. * - * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to - * OT_MESSAGE_PRIORITY_NORMAL by default. - * - * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * @param[in] aSettings The message settings. * * @returns A pointer to the message or NULL if failed to allocate message. * */ - Message *NewMessage(const otMessageSettings *aSettings = NULL); + Message *NewMessage(const Message::Settings &aSettings = Message::Settings::GetDefault()); /** * This method creates a new message with a CoAP header that has Network Control priority level. @@ -348,8 +345,7 @@ public: */ Message *NewPriorityMessage(void) { - otMessageSettings settings = {true, static_cast(Message::kPriorityNet)}; - return NewMessage(&settings); + return NewMessage(Message::Settings(Message::kWithLinkSecurity, Message::kPriorityNet)); } /** diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index ea26d9b0f..1d5ff3409 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -87,27 +87,13 @@ exit: return message; } -Message *MessagePool::New(Message::Type aType, uint16_t aReserveHeader, const otMessageSettings *aSettings) +Message *MessagePool::New(Message::Type aType, uint16_t aReserveHeader, const Message::Settings &aSettings) { - Message * message; - bool linkSecurityEnabled; - Message::Priority priority; + Message *message = New(aType, aReserveHeader, aSettings.GetPriority()); - if (aSettings == NULL) - { - linkSecurityEnabled = true; - priority = Message::kPriorityNormal; - } - else - { - linkSecurityEnabled = aSettings->mLinkSecurityEnabled; - priority = static_cast(aSettings->mPriority); - } - - message = New(aType, aReserveHeader, priority); if (message) { - message->SetLinkSecurityEnabled(linkSecurityEnabled); + message->SetLinkSecurityEnabled(aSettings.IsLinkSecurityEnabled()); } return message; @@ -198,6 +184,20 @@ uint16_t MessagePool::GetFreeBufferCount(void) const return rval; } +const Message::Settings Message::Settings::kDefault(Message::kWithLinkSecurity, Message::kPriorityNormal); + +Message::Settings::Settings(LinkSecurityMode aSecurityMode, Priority aPriority) + : mLinkSecurityEnabled(aSecurityMode == kWithLinkSecurity) + , mPriority(aPriority) +{ +} + +Message::Settings::Settings(const otMessageSettings *aSettings) + : mLinkSecurityEnabled((aSettings != NULL) ? aSettings->mLinkSecurityEnabled : true) + , mPriority((aSettings != NULL) ? static_cast(aSettings->mPriority) : kPriorityNormal) +{ +} + otError Message::ResizeMessage(uint16_t aLength) { otError error = OT_ERROR_NONE; diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 118a89742..3e1f0bc0d 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -274,6 +274,72 @@ public: kNumPriorities = 4, ///< Number of priority levels. }; + /** + * This enumeration represents the link security mode (used by `Settings` constructor). + * + */ + enum LinkSecurityMode + { + kNoLinkSecurity, ///< Link security disabled (no link security). + kWithLinkSecurity, ///< Link security enabled. + }; + + /** + * This class represents settings used for creating a new message. + * + */ + class Settings + { + public: + /** + * This constructor initializes the Settings object. + * + * @param[in] aSecurityMode A link security mode. + * @param[in] aPriority A message priority. + * + */ + Settings(LinkSecurityMode aSecurityMode, Priority aPriority); + + /** + * This constructor initializes the `Settings` object from a given `otMessageSettings`. + * + * @param[in] aSettings A pointer to `otMessageSettings` to covert from. If NULL default settings (link + * security enabled with `kPriorityNormal` priority) would be used. + * + */ + Settings(const otMessageSettings *aSettings); + + /** + * This method gets the message priority. + * + * @returns The message priority. + * + */ + Priority GetPriority(void) const { return mPriority; } + + /** + * This method indicates whether the link security should be enabled. + * + * @returns TRUE if link security should be enabled, FALSE otherwise. + * + */ + bool IsLinkSecurityEnabled(void) const { return mLinkSecurityEnabled; } + + /** + * This static method returns the default settings with link security enabled and `kPriorityNormal` priority. + * + * @returns A reference to the default settings (link security enable and `kPriorityNormal` priority). + * + */ + static const Settings &GetDefault(void) { return kDefault; } + + private: + static const Settings kDefault; + + bool mLinkSecurityEnabled; + Priority mPriority; + }; + /** * This method frees this message buffer. * @@ -1138,29 +1204,31 @@ public: /** * This method is used to obtain a new message. * + * The link security is enabled by default on the newly obtained message. + * * @param[in] aType The message type. * @param[in] aReserveHeader The number of header bytes to reserve. - * @param[in] aPriority The priority level of the message (default value is `Message::kPriorityNormal`). + * @param[in] aPriority The priority level of the message. * * @returns A pointer to the message or NULL if no message buffers are available. * */ - Message *New(Message::Type aType, uint16_t aReserveHeader, Message::Priority aPriority = Message::kPriorityNormal); + Message *New(Message::Type aType, uint16_t aReserveHeader, Message::Priority aPriority); +public: /** * This method is used to obtain a new message with specified settings. * - * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to - * OT_MESSAGE_PRIORITY_NORMAL by default. - * * @param[in] aType The message type. * @param[in] aReserveHeader The number of header bytes to reserve. - * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * @param[in] aSettings The message settings. * * @returns A pointer to the message or NULL if no message buffers are available. * */ - Message *New(Message::Type aType, uint16_t aReserveHeader, const otMessageSettings *aSettings); + Message *New(Message::Type aType, + uint16_t aReserveHeader, + const Message::Settings &aSettings = Message::Settings::GetDefault()); /** * This method is used to free a message and return all message buffers to the buffer pool. diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 703a8bd7a..65bf6ef62 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -195,8 +195,8 @@ void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::Messa Kek kek; uint16_t offset; uint16_t length; - Message * message = NULL; - otMessageSettings settings = {false, static_cast(kMeshCoPMessagePriority)}; + Message * message = NULL; + Message::Settings settings(Message::kNoLinkSecurity, Message::kPriorityNet); Ip6::MessageInfo messageInfo; VerifyOrExit(aMessage.IsNonConfirmable() && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); @@ -208,7 +208,7 @@ void JoinerRouter::HandleRelayTransmit(Coap::Message &aMessage, const Ip6::Messa SuccessOrExit(error = Tlv::GetValueOffset(aMessage, Tlv::kJoinerDtlsEncapsulation, offset, length)); - VerifyOrExit((message = mSocket.NewMessage(0, &settings)) != NULL, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = mSocket.NewMessage(0, settings)) != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetLength(length)); aMessage.CopyTo(offset, 0, length, *message); diff --git a/src/core/meshcop/meshcop.hpp b/src/core/meshcop/meshcop.hpp index 1515d55a4..1598fbe7c 100644 --- a/src/core/meshcop/meshcop.hpp +++ b/src/core/meshcop/meshcop.hpp @@ -52,18 +52,16 @@ namespace MeshCoP { enum { - kMeshCoPMessagePriority = Message::kPriorityNet, ///< The priority for MeshCoP message - kBorderAgentUdpPort = 49191, ///< UDP port of border agent service. + kBorderAgentUdpPort = 49191, ///< UDP port of border agent service. }; /** - * This function create Message for MeshCoP + * This function creates Message for MeshCoP. * */ inline Coap::Message *NewMeshCoPMessage(Coap::CoapBase &aCoap) { - otMessageSettings settings = {true, static_cast(kMeshCoPMessagePriority)}; - return aCoap.NewMessage(&settings); + return aCoap.NewMessage(Message::Settings(Message::kWithLinkSecurity, Message::kPriorityNet)); } /** diff --git a/src/core/net/icmp6.cpp b/src/core/net/icmp6.cpp index c61cf64a8..f9cb56a0e 100644 --- a/src/core/net/icmp6.cpp +++ b/src/core/net/icmp6.cpp @@ -97,7 +97,7 @@ otError Icmp::SendError(IcmpHeader::Type aType, Message * message = NULL; IcmpHeader icmp6Header; Header ip6Header; - otMessageSettings settings = {true, static_cast(Message::kPriorityNet)}; + Message::Settings settings(Message::kWithLinkSecurity, Message::kPriorityNet); VerifyOrExit(aMessage.GetLength() >= sizeof(ip6Header), error = OT_ERROR_INVALID_ARGS); @@ -113,7 +113,7 @@ otError Icmp::SendError(IcmpHeader::Type aType, messageInfoLocal = aMessageInfo; - VerifyOrExit((message = Get().NewMessage(0, &settings)) != NULL, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage(0, settings)) != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetLength(sizeof(icmp6Header) + sizeof(ip6Header))); message->Write(sizeof(icmp6Header), sizeof(ip6Header), &ip6Header); diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 7d3110441..0f46f9e2a 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -67,26 +67,17 @@ Ip6::Ip6(Instance &aInstance) { } -Message *Ip6::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings) +Message *Ip6::NewMessage(uint16_t aReserved, const Message::Settings &aSettings) { return Get().New(Message::kTypeIp6, sizeof(Header) + sizeof(HopByHopHeader) + sizeof(OptionMpl) + aReserved, aSettings); } -Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength, const otMessageSettings *aSettings) +Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings) { - otMessageSettings settings = {true, OT_MESSAGE_PRIORITY_NORMAL}; - Message * message = NULL; - Message::Priority priority; + Message *message = Get().New(Message::kTypeIp6, 0, aSettings); - if (aSettings != NULL) - { - settings = *aSettings; - } - - SuccessOrExit(GetDatagramPriority(aData, aDataLength, priority)); - settings.mPriority = static_cast(priority); - VerifyOrExit((message = Get().New(Message::kTypeIp6, 0, &settings)) != NULL, OT_NOOP); + VerifyOrExit(message != NULL, OT_NOOP); if (message->Append(aData, aDataLength) != OT_ERROR_NONE) { @@ -98,6 +89,18 @@ exit: return message; } +Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength) +{ + Message * message = NULL; + Message::Priority priority; + + SuccessOrExit(GetDatagramPriority(aData, aDataLength, priority)); + message = NewMessage(aData, aDataLength, Message::Settings(Message::kWithLinkSecurity, priority)); + +exit: + return message; +} + Message::Priority Ip6::DscpToPriority(uint8_t aDscp) { Message::Priority priority; diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index f6b111672..e8bf8bf13 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -117,32 +117,38 @@ public: /** * This method allocates a new message buffer from the buffer pool. * - * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to - * OT_MESSAGE_PRIORITY_NORMAL by default. - * * @param[in] aReserved The number of header bytes to reserve following the IPv6 header. - * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * @param[in] aSettings The message settings. * * @returns A pointer to the message or NULL if insufficient message buffers are available. * */ - Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL); + Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings = Message::Settings::GetDefault()); /** * This method allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message. * - * @note If @p aSettings is NULL, the link layer security is enabled and the message priority is obtained from - * IPv6 message itself. - * If @p aSettings is not NULL, the @p aSetting->mPriority is ignored and obtained from IPv6 message itself. - * * @param[in] aData A pointer to the IPv6 datagram buffer. * @param[in] aDataLength The size of the IPV6 datagram buffer pointed by @p aData. - * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * @param[in] aSettings The message settings. * * @returns A pointer to the message or NULL if malformed IPv6 header or insufficient message buffers are available. * */ - Message *NewMessage(const uint8_t *aData, uint16_t aDataLength, const otMessageSettings *aSettings); + Message *NewMessage(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings); + + /** + * This method allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message. + * + * @note The link layer security is enabled and the message priority is obtained from IPv6 message itself. + * + * @param[in] aData A pointer to the IPv6 datagram buffer. + * @param[in] aDataLength The size of the IPV6 datagram buffer pointed by @p aData. + * + * @returns A pointer to the message or NULL if malformed IPv6 header or insufficient message buffers are available. + * + */ + Message *NewMessage(const uint8_t *aData, uint16_t aDataLength); /** * This method converts the message priority level to IPv6 DSCP value. diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index 25c5c8774..a616b183a 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -66,7 +66,7 @@ UdpSocket::UdpSocket(Udp &aUdp) mHandle = NULL; } -Message *UdpSocket::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings) +Message *UdpSocket::NewMessage(uint16_t aReserved, const Message::Settings &aSettings) { return Get().NewMessage(aReserved, aSettings); } @@ -260,7 +260,7 @@ uint16_t Udp::GetEphemeralPort(void) return rval; } -Message *Udp::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings) +Message *Udp::NewMessage(uint16_t aReserved, const Message::Settings &aSettings) { return Get().NewMessage(sizeof(UdpHeader) + aReserved, aSettings); } diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index 645a42b0d..e48496fa5 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -107,16 +107,13 @@ public: /** * This method returns a new UDP message with sufficient header space reserved. * - * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to - * OT_MESSAGE_PRIORITY_NORMAL by default. - * * @param[in] aReserved The number of header bytes to reserve after the UDP header. - * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * @param[in] aSettings The message settings (default is used if not provided). * * @returns A pointer to the message or NULL if no buffers are available. * */ - Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL); + Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings = Message::Settings::GetDefault()); /** * This method opens the UDP socket. @@ -273,12 +270,12 @@ public: * This method returns a new UDP message with sufficient header space reserved. * * @param[in] aReserved The number of header bytes to reserve after the UDP header. - * @param[in] aPriority The priority of the message. + * @param[in] aSettings The message settings. * * @returns A pointer to the message or NULL if no buffers are available. * */ - Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL); + Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings = Message::Settings::GetDefault()); /** * This method sends an IPv6 datagram. diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 3176a297f..c6bb1b733 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1092,9 +1092,9 @@ const LeaderData &Mle::GetLeaderData(void) Message *Mle::NewMleMessage(void) { Message * message; - otMessageSettings settings = {false, static_cast(kMleMessagePriority)}; + Message::Settings settings(Message::kNoLinkSecurity, Message::kPriorityNet); - message = mSocket.NewMessage(0, &settings); + message = mSocket.NewMessage(0, settings); VerifyOrExit(message != NULL, OT_NOOP); message->SetSubType(Message::kSubTypeMleGeneral); diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 10ee1fafe..24d155538 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -1650,8 +1650,7 @@ protected: private: enum { - kMleMessagePriority = Message::kPriorityNet, - kMleHopLimit = 255, + kMleHopLimit = 255, // Parameters related to "periodic parent search" feature (CONFIG_ENABLE_PERIODIC_PARENT_SEARCH). // All timer intervals are converted to milliseconds.