From 17a0405c51088c3c52369f6cd30f9b72bc61a6d7 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 18 May 2020 17:34:53 -0700 Subject: [PATCH] [message] add Message::Priority enumeration type (#4977) --- src/core/common/message.cpp | 49 +++++++++++++------------- src/core/common/message.hpp | 29 +++++++-------- src/core/net/ip6.cpp | 13 +++---- src/core/net/ip6.hpp | 6 ++-- src/core/thread/mesh_forwarder.cpp | 8 ++--- src/core/thread/mesh_forwarder.hpp | 19 +++++----- src/core/thread/mesh_forwarder_ftd.cpp | 17 ++++----- src/core/thread/mesh_forwarder_mtd.cpp | 4 +-- tests/unit/test_priority_queue.cpp | 15 +++----- 9 files changed, 77 insertions(+), 83 deletions(-) diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index d24ae7e7f..ea26d9b0f 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -61,7 +61,7 @@ MessagePool::MessagePool(Instance &aInstance) #endif } -Message *MessagePool::New(Message::Type aType, uint16_t aReserveHeader, uint8_t aPriority) +Message *MessagePool::New(Message::Type aType, uint16_t aReserveHeader, Message::Priority aPriority) { otError error = OT_ERROR_NONE; Message *message; @@ -89,19 +89,19 @@ exit: Message *MessagePool::New(Message::Type aType, uint16_t aReserveHeader, const otMessageSettings *aSettings) { - Message *message; - bool linkSecurityEnabled; - uint8_t priority; + Message * message; + bool linkSecurityEnabled; + Message::Priority priority; if (aSettings == NULL) { linkSecurityEnabled = true; - priority = OT_MESSAGE_PRIORITY_NORMAL; + priority = Message::kPriorityNormal; } else { linkSecurityEnabled = aSettings->mLinkSecurityEnabled; - priority = aSettings->mPriority; + priority = static_cast(aSettings->mPriority); } message = New(aType, aReserveHeader, priority); @@ -120,7 +120,7 @@ void MessagePool::Free(Message *aMessage) FreeBuffers(static_cast(aMessage)); } -Buffer *MessagePool::NewBuffer(uint8_t aPriority) +Buffer *MessagePool::NewBuffer(Message::Priority aPriority) { Buffer *buffer = NULL; @@ -166,7 +166,7 @@ void MessagePool::FreeBuffers(Buffer *aBuffer) } } -otError MessagePool::ReclaimBuffers(int aNumBuffers, uint8_t aPriority) +otError MessagePool::ReclaimBuffers(int aNumBuffers, Message::Priority aPriority) { #if OPENTHREAD_MTD || OPENTHREAD_FTD while (aNumBuffers > GetFreeBufferCount()) @@ -342,15 +342,16 @@ bool Message::IsSubTypeMle(void) const return rval; } -otError Message::SetPriority(uint8_t aPriority) +otError Message::SetPriority(Priority aPriority) { otError error = OT_ERROR_NONE; + uint8_t priority = static_cast(aPriority); PriorityQueue *priorityQueue = NULL; - VerifyOrExit(aPriority < kNumPriorities, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(priority < kNumPriorities, error = OT_ERROR_INVALID_ARGS); - VerifyOrExit(IsInAQueue(), GetMetadata().mPriority = aPriority); - VerifyOrExit(GetMetadata().mPriority != aPriority, OT_NOOP); + VerifyOrExit(IsInAQueue(), GetMetadata().mPriority = priority); + VerifyOrExit(GetMetadata().mPriority != priority, OT_NOOP); if (GetMetadata().mInPriorityQ) { @@ -358,7 +359,7 @@ otError Message::SetPriority(uint8_t aPriority) priorityQueue->Dequeue(*this); } - GetMetadata().mPriority = aPriority; + GetMetadata().mPriority = priority; if (priorityQueue != NULL) { @@ -859,12 +860,12 @@ PriorityQueue::PriorityQueue(void) } } -Message *PriorityQueue::FindFirstNonNullTail(uint8_t aStartPriorityLevel) const +Message *PriorityQueue::FindFirstNonNullTail(Message::Priority aStartPriorityLevel) const { Message *tail = NULL; uint8_t priority; - priority = aStartPriorityLevel; + priority = static_cast(aStartPriorityLevel); do { @@ -884,19 +885,19 @@ Message *PriorityQueue::GetHead(void) const { Message *tail; - tail = FindFirstNonNullTail(0); + tail = FindFirstNonNullTail(Message::kPriorityLow); return (tail == NULL) ? NULL : tail->Next(); } -Message *PriorityQueue::GetHeadForPriority(uint8_t aPriority) const +Message *PriorityQueue::GetHeadForPriority(Message::Priority aPriority) const { Message *head; Message *previousTail; if (mTails[aPriority] != NULL) { - previousTail = FindFirstNonNullTail(PrevPriority(aPriority)); + previousTail = FindFirstNonNullTail(static_cast(PrevPriority(aPriority))); OT_ASSERT(previousTail != NULL); @@ -912,14 +913,14 @@ Message *PriorityQueue::GetHeadForPriority(uint8_t aPriority) const Message *PriorityQueue::GetTail(void) const { - return FindFirstNonNullTail(0); + return FindFirstNonNullTail(Message::kPriorityLow); } void PriorityQueue::Enqueue(Message &aMessage) { - uint8_t priority; - Message *tail; - Message *next; + Message::Priority priority; + Message * tail; + Message * next; OT_ASSERT(!aMessage.IsInAQueue()); @@ -949,8 +950,8 @@ void PriorityQueue::Enqueue(Message &aMessage) void PriorityQueue::Dequeue(Message &aMessage) { - uint8_t priority; - Message *tail; + Message::Priority priority; + Message * tail; OT_ASSERT(aMessage.GetPriorityQueue() == this); diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 2a9c1e3b0..118a89742 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -261,13 +261,16 @@ public: kSubTypeMleChildIdRequest = 10, ///< MLE Child ID Request }; - enum + enum Priority { kPriorityLow = OT_MESSAGE_PRIORITY_LOW, ///< Low priority level. kPriorityNormal = OT_MESSAGE_PRIORITY_NORMAL, ///< Normal priority level. kPriorityHigh = OT_MESSAGE_PRIORITY_HIGH, ///< High priority level. kPriorityNet = OT_MESSAGE_PRIORITY_HIGH + 1, ///< Network Control priority level. + }; + enum + { kNumPriorities = 4, ///< Number of priority levels. }; @@ -380,7 +383,7 @@ public: * @returns The priority level associated with this message. * */ - uint8_t GetPriority(void) const { return GetMetadata().mPriority; } + Priority GetPriority(void) const { return static_cast(GetMetadata().mPriority); } /** * This method sets the messages priority. @@ -393,7 +396,7 @@ public: * @retval OT_ERROR_INVALID_ARGS Priority level is not invalid. * */ - otError SetPriority(uint8_t aPriority); + otError SetPriority(Priority aPriority); /** * This method prepends bytes to the front of the message. @@ -1051,7 +1054,7 @@ public: * this priority level. * */ - Message *GetHeadForPriority(uint8_t aPriority) const; + Message *GetHeadForPriority(Message::Priority aPriority) const; /** * This method adds a message to the queue. @@ -1109,7 +1112,7 @@ private: * @returns The first non-NULL tail pointer, or NULL if all the * */ - Message *FindFirstNonNullTail(uint8_t aStartPriorityLevel) const; + Message *FindFirstNonNullTail(Message::Priority aStartPriorityLevel) const; private: Message *mTails[Message::kNumPriorities]; ///< Tail pointers associated with different priority levels. @@ -1133,17 +1136,16 @@ public: explicit MessagePool(Instance &aInstance); /** - * This method is used to obtain a new message. The default priority `kDefaultMessagePriority` - * is assigned to the message. + * This method is used to obtain a new 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. + * @param[in] aPriority The priority level of the message (default value is `Message::kPriorityNormal`). * * @returns A pointer to the message or NULL if no message buffers are available. * */ - Message *New(Message::Type aType, uint16_t aReserveHeader, uint8_t aPriority = kDefaultMessagePriority); + Message *New(Message::Type aType, uint16_t aReserveHeader, Message::Priority aPriority = Message::kPriorityNormal); /** * This method is used to obtain a new message with specified settings. @@ -1177,14 +1179,9 @@ public: uint16_t GetFreeBufferCount(void) const; private: - enum - { - kDefaultMessagePriority = Message::kPriorityNormal, - }; - - Buffer *NewBuffer(uint8_t aPriority); + Buffer *NewBuffer(Message::Priority aPriority); void FreeBuffers(Buffer *aBuffer); - otError ReclaimBuffers(int aNumBuffers, uint8_t aPriority); + otError ReclaimBuffers(int aNumBuffers, Message::Priority aPriority); #if OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT == 0 uint16_t mNumFreeBuffers; diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 66b8bc8dd..7d3110441 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -77,7 +77,7 @@ Message *Ip6::NewMessage(const uint8_t *aData, uint16_t aDataLength, const otMes { otMessageSettings settings = {true, OT_MESSAGE_PRIORITY_NORMAL}; Message * message = NULL; - uint8_t priority; + Message::Priority priority; if (aSettings != NULL) { @@ -98,10 +98,10 @@ exit: return message; } -uint8_t Ip6::DscpToPriority(uint8_t aDscp) +Message::Priority Ip6::DscpToPriority(uint8_t aDscp) { - uint8_t priority; - uint8_t cs = aDscp & kDscpCsMask; + Message::Priority priority; + uint8_t cs = aDscp & kDscpCsMask; switch (cs) { @@ -130,7 +130,7 @@ uint8_t Ip6::DscpToPriority(uint8_t aDscp) return priority; } -uint8_t Ip6::PriorityToDscp(uint8_t aPriority) +uint8_t Ip6::PriorityToDscp(Message::Priority aPriority) { uint8_t dscp = kDscpCs0; @@ -141,6 +141,7 @@ uint8_t Ip6::PriorityToDscp(uint8_t aPriority) break; case Message::kPriorityNormal: + case Message::kPriorityNet: dscp = kDscpCs0; break; @@ -152,7 +153,7 @@ uint8_t Ip6::PriorityToDscp(uint8_t aPriority) return dscp; } -otError Ip6::GetDatagramPriority(const uint8_t *aData, uint16_t aDataLen, uint8_t &aPriority) +otError Ip6::GetDatagramPriority(const uint8_t *aData, uint16_t aDataLen, Message::Priority &aPriority) { otError error = OT_ERROR_NONE; const Header *header; diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 66cda0039..f6b111672 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -152,7 +152,7 @@ public: * @returns The IPv6 DSCP value. * */ - static uint8_t PriorityToDscp(uint8_t aPriority); + static uint8_t PriorityToDscp(Message::Priority aPriority); /** * This method converts the IPv6 DSCP value to message priority level. @@ -162,7 +162,7 @@ public: * @returns The message priority level. * */ - static uint8_t DscpToPriority(uint8_t aDscp); + static Message::Priority DscpToPriority(uint8_t aDscp); /** * This constructor initializes the object. @@ -344,7 +344,7 @@ private: static void HandleSendQueue(Tasklet &aTasklet); void HandleSendQueue(void); - static otError GetDatagramPriority(const uint8_t *aData, uint16_t aDataLen, uint8_t &aPriority); + static otError GetDatagramPriority(const uint8_t *aData, uint16_t aDataLen, Message::Priority &aPriority); otError ProcessReceiveCallback(const Message & aMessage, const MessageInfo &aMessageInfo, diff --git a/src/core/thread/mesh_forwarder.cpp b/src/core/thread/mesh_forwarder.cpp index a70d67297..424e55977 100644 --- a/src/core/thread/mesh_forwarder.cpp +++ b/src/core/thread/mesh_forwarder.cpp @@ -1258,9 +1258,9 @@ otError MeshForwarder::FrameToMessage(const uint8_t * aFrame, const Mac::Address &aMacDest, Message *& aMessage) { - otError error = OT_ERROR_NONE; - int headerLength; - uint8_t priority; + otError error = OT_ERROR_NONE; + int headerLength; + Message::Priority priority; error = GetFramePriority(aFrame, aFrameLength, aMacSource, aMacDest, priority); SuccessOrExit(error); @@ -1350,7 +1350,7 @@ otError MeshForwarder::GetFramePriority(const uint8_t * aFrame, uint16_t aFrameLength, const Mac::Address &aMacSource, const Mac::Address &aMacDest, - uint8_t & aPriority) + Message::Priority & aPriority) { otError error = OT_ERROR_NONE; Ip6::Header ip6Header; diff --git a/src/core/thread/mesh_forwarder.hpp b/src/core/thread/mesh_forwarder.hpp index 268ef761d..7e9e9ed70 100644 --- a/src/core/thread/mesh_forwarder.hpp +++ b/src/core/thread/mesh_forwarder.hpp @@ -109,7 +109,7 @@ public: * @returns The fragment priority value. * */ - uint8_t GetPriority(void) const { return mPriority; } + Message::Priority GetPriority(void) const { return static_cast(mPriority); } /** * This method sets the fragment priority value. @@ -117,7 +117,7 @@ public: * @param[in] aPriority The fragment priority value. * */ - void SetPriority(uint8_t aPriority) { mPriority = aPriority; } + void SetPriority(Message::Priority aPriority) { mPriority = aPriority; } /** * This method returns the fragment priority entry's remaining lifetime. @@ -271,7 +271,7 @@ public: * @retval OT_ERROR_NOT_FOUND No low priority messages available to evict. * */ - otError EvictMessage(uint8_t aPriority); + otError EvictMessage(Message::Priority aPriority); /** * This method returns a reference to the send queue. @@ -316,8 +316,7 @@ public: private: enum { - kStateUpdatePeriod = 1000, ///< State update period in milliseconds. - kDefaultMsgPriority = Message::kPriorityNormal, ///< Default message priority. + kStateUpdatePeriod = 1000, ///< State update period in milliseconds. /** * The number of fragment priority entries. @@ -402,7 +401,7 @@ private: void UpdateFragmentPriority(Lowpan::FragmentHeader &aFragmentHeader, uint16_t aFragmentLength, uint16_t aSrcRloc16, - uint8_t aPriority); + Message::Priority aPriority); otError HandleDatagram(Message &aMessage, const otThreadLinkInfo &aLinkInfo, const Mac::Address &aMacSource); void ClearReassemblyList(void); void RemoveMessage(Message &aMessage); @@ -424,13 +423,15 @@ private: uint16_t aFrameLength, const Mac::Address &aMacSource, const Mac::Address &aMacDest, - uint8_t & aPriority); - otError GetFragmentPriority(Lowpan::FragmentHeader &aFragmentHeader, uint16_t aSrcRloc16, uint8_t &aPriority); + Message::Priority & aPriority); + otError GetFragmentPriority(Lowpan::FragmentHeader &aFragmentHeader, + uint16_t aSrcRloc16, + Message::Priority & aPriority); void GetForwardFramePriority(const uint8_t * aFrame, uint16_t aFrameLength, const Mac::Address &aMeshSource, const Mac::Address &aMeshDest, - uint8_t & aPriority); + Message::Priority & aPriority); FragmentPriorityEntry *FindFragmentPriorityEntry(uint16_t aTag, uint16_t aSrcRloc16); FragmentPriorityEntry *GetUnusedFragmentPriorityEntry(void); diff --git a/src/core/thread/mesh_forwarder_ftd.cpp b/src/core/thread/mesh_forwarder_ftd.cpp index d0f621790..a756452ec 100644 --- a/src/core/thread/mesh_forwarder_ftd.cpp +++ b/src/core/thread/mesh_forwarder_ftd.cpp @@ -181,7 +181,7 @@ void MeshForwarder::HandleResolved(const Ip6::Address &aEid, otError aError) } } -otError MeshForwarder::EvictMessage(uint8_t aPriority) +otError MeshForwarder::EvictMessage(Message::Priority aPriority) { otError error = OT_ERROR_NOT_FOUND; PriorityQueue *queues[] = {&mResolvingQueue, &mSendQueue}; @@ -192,7 +192,8 @@ otError MeshForwarder::EvictMessage(uint8_t aPriority) { for (uint8_t priority = 0; priority < aPriority; priority++) { - for (Message *message = queues[index]->GetHeadForPriority(priority); message; message = message->GetNext()) + for (Message *message = queues[index]->GetHeadForPriority(static_cast(priority)); + message; message = message->GetNext()) { if (message->GetPriority() != priority) { @@ -205,7 +206,7 @@ otError MeshForwarder::EvictMessage(uint8_t aPriority) } evict = message; - aPriority = priority; + aPriority = static_cast(priority); break; } } @@ -648,8 +649,8 @@ void MeshForwarder::HandleMesh(uint8_t * aFrame, } else if (meshHeader.GetHopsLeft() > 0) { - uint8_t priority = kDefaultMsgPriority; - uint16_t offset = 0; + Message::Priority priority = Message::kPriorityNormal; + uint16_t offset = 0; Get().ResolveRoutingLoops(aMacSource.GetShort(), meshDest.GetShort()); @@ -755,7 +756,7 @@ bool MeshForwarder::UpdateFragmentLifetime(void) void MeshForwarder::UpdateFragmentPriority(Lowpan::FragmentHeader &aFragmentHeader, uint16_t aFragmentLength, uint16_t aSrcRloc16, - uint8_t aPriority) + Message::Priority aPriority) { FragmentPriorityEntry *entry; @@ -828,7 +829,7 @@ exit: otError MeshForwarder::GetFragmentPriority(Lowpan::FragmentHeader &aFragmentHeader, uint16_t aSrcRloc16, - uint8_t & aPriority) + Message::Priority & aPriority) { otError error = OT_ERROR_NONE; FragmentPriorityEntry *entry; @@ -845,7 +846,7 @@ void MeshForwarder::GetForwardFramePriority(const uint8_t * aFrame, uint16_t aFrameLength, const Mac::Address &aMeshSource, const Mac::Address &aMeshDest, - uint8_t & aPriority) + Message::Priority & aPriority) { otError error = OT_ERROR_NONE; bool isFragment = false; diff --git a/src/core/thread/mesh_forwarder_mtd.cpp b/src/core/thread/mesh_forwarder_mtd.cpp index 99b75eaf7..40444b5ac 100644 --- a/src/core/thread/mesh_forwarder_mtd.cpp +++ b/src/core/thread/mesh_forwarder_mtd.cpp @@ -49,14 +49,14 @@ otError MeshForwarder::SendMessage(Message &aMessage) return OT_ERROR_NONE; } -otError MeshForwarder::EvictMessage(uint8_t aPriority) +otError MeshForwarder::EvictMessage(Message::Priority aPriority) { otError error = OT_ERROR_NOT_FOUND; Message *message; VerifyOrExit((message = mSendQueue.GetTail()) != NULL, OT_NOOP); - if (message->GetPriority() < aPriority) + if (message->GetPriority() < static_cast(aPriority)) { RemoveMessage(*message); ExitNow(error = OT_ERROR_NONE); diff --git a/tests/unit/test_priority_queue.cpp b/tests/unit/test_priority_queue.cpp index d6578d777..89d2f381a 100644 --- a/tests/unit/test_priority_queue.cpp +++ b/tests/unit/test_priority_queue.cpp @@ -83,12 +83,13 @@ void VerifyPriorityQueueContent(ot::PriorityQueue &aPriorityQueue, int aExpected { // Check the `GetHeadForPriority` is NULL if there are no expected message for this priority level. VerifyOrQuit( - aPriorityQueue.GetHeadForPriority(static_cast(curPriority)) == NULL, + aPriorityQueue.GetHeadForPriority(static_cast(curPriority)) == NULL, "PriorityQueue::GetHeadForPriority is non-NULL when no expected msg for this priority."); } // Check the `GetHeadForPriority`. - VerifyOrQuit(aPriorityQueue.GetHeadForPriority(static_cast(curPriority)) == msgArg, + VerifyOrQuit(aPriorityQueue.GetHeadForPriority(static_cast(curPriority)) == + msgArg, "PriorityQueue::GetHeadForPriority failed."); } @@ -103,7 +104,7 @@ void VerifyPriorityQueueContent(ot::PriorityQueue &aPriorityQueue, int aExpected // Check the `GetHeadForPriority` is NULL if there are no expected message for any remaining priority level. for (curPriority--; curPriority >= 0; curPriority--) { - VerifyOrQuit(aPriorityQueue.GetHeadForPriority(static_cast(curPriority)) == NULL, + VerifyOrQuit(aPriorityQueue.GetHeadForPriority(static_cast(curPriority)) == NULL, "PriorityQueue::GetHeadForPriority is non-NULL when no expected msg for this priority."); } } @@ -172,10 +173,6 @@ void TestPriorityQueue(void) VerifyOrQuit(msgLow[i] != NULL, "Message::New failed"); } - // Check the failure case for `New()` for invalid argument. - VerifyOrQuit(messagePool->New(ot::Message::kTypeIp6, 0, ot::Message::kNumPriorities) == NULL, - "Message::New() with out of range value did not fail as expected."); - // Use the function "SetPriority()" to allocate messages with different priorities for (int i = kNumNewPriorityTestMessages; i < kNumTestMessages; i++) { @@ -193,10 +190,6 @@ void TestPriorityQueue(void) SuccessOrQuit(msgLow[i]->SetPriority(ot::Message::kPriorityLow), "Message:SetPriority failed"); } - // Check the failure case for `SetPriority()` for invalid argument. - VerifyOrQuit(msgNet[2]->SetPriority(ot::Message::kNumPriorities) == OT_ERROR_INVALID_ARGS, - "Message::SetPriority() with out of range value did not fail as expected."); - // Check the `GetPriority()` for (int i = 0; i < kNumTestMessages; i++) {