From fb65c77c1ade4a7bdb34efad0a08fef502dfb2e0 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Sun, 16 Feb 2020 13:31:09 -0800 Subject: [PATCH] [topology] have Neighbor/Child/Router inherit from InstanceLocatorInit (#4569) This commit changes the `Neighbor` class (and therefore its sub-classes `Child` and `Router`) to inherit from `InstanceLoatorInit`. It adds `Init()` method to them as well and ensures that they are initialized before use from `ChildTable`, `RouterTable` and `Mle` classes. --- src/core/thread/child_table.cpp | 6 +++++- src/core/thread/mle.cpp | 3 +++ src/core/thread/router_table.cpp | 5 +++++ src/core/thread/topology.cpp | 14 ++++++++++++-- src/core/thread/topology.hpp | 30 +++++++++++++++++++++++++++--- tests/unit/test_child.cpp | 2 ++ 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/core/thread/child_table.cpp b/src/core/thread/child_table.cpp index e15281bd7..f0e1fc51b 100644 --- a/src/core/thread/child_table.cpp +++ b/src/core/thread/child_table.cpp @@ -102,7 +102,11 @@ ChildTable::ChildTable(Instance &aInstance) : InstanceLocator(aInstance) , mMaxChildrenAllowed(kMaxChildren) { - Clear(); + for (Child *child = &mChildren[0]; child < OT_ARRAY_END(mChildren); child++) + { + child->Init(aInstance); + child->Clear(); + } } void ChildTable::Clear(void) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 57575bbca..8851929f2 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -114,6 +114,9 @@ Mle::Mle(Instance &aInstance) { otMeshLocalPrefix meshLocalPrefix; + mParent.Init(aInstance); + mParentCandidate.Init(aInstance); + mLeaderData.Clear(); mParentLeaderData.Clear(); mParent.Clear(); diff --git a/src/core/thread/router_table.cpp b/src/core/thread/router_table.cpp index c4955d39e..b0586af50 100644 --- a/src/core/thread/router_table.cpp +++ b/src/core/thread/router_table.cpp @@ -65,6 +65,11 @@ RouterTable::RouterTable(Instance &aInstance) , mRouterIdSequence(Random::NonCrypto::GetUint8()) , mActiveRouterCount(0) { + for (uint8_t index = 0; index < Mle::kMaxRouters; index++) + { + mRouters[index].Init(aInstance); + } + Clear(); } diff --git a/src/core/thread/topology.cpp b/src/core/thread/topology.cpp index fc7f6b1fe..e4671b058 100644 --- a/src/core/thread/topology.cpp +++ b/src/core/thread/topology.cpp @@ -41,6 +41,12 @@ namespace ot { +void Neighbor::Init(Instance &aInstance) +{ + InstanceLocatorInit::Init(aInstance); + SetState(kStateInvalid); +} + bool Neighbor::IsStateValidOrAttaching(void) const { bool rval = false; @@ -105,8 +111,10 @@ void Neighbor::GenerateChallenge(void) void Child::Clear(void) { + Instance &instance = GetInstance(); + memset(reinterpret_cast(this), 0, sizeof(Child)); - SetState(kStateInvalid); + Init(instance); } void Child::ClearIp6Addresses(void) @@ -282,8 +290,10 @@ void Child::GenerateChallenge(void) void Router::Clear(void) { + Instance &instance = GetInstance(); + memset(reinterpret_cast(this), 0, sizeof(Router)); - SetState(kStateInvalid); + Init(instance); } } // namespace ot diff --git a/src/core/thread/topology.hpp b/src/core/thread/topology.hpp index 3512ec6f1..9e6f53689 100644 --- a/src/core/thread/topology.hpp +++ b/src/core/thread/topology.hpp @@ -38,6 +38,7 @@ #include +#include "common/locator.hpp" #include "common/message.hpp" #include "common/random.hpp" #include "common/timer.hpp" @@ -50,13 +51,11 @@ namespace ot { -class Instance; - /** * This class represents a Thread neighbor. * */ -class Neighbor +class Neighbor : public InstanceLocatorInit { public: /** @@ -420,6 +419,15 @@ public: void SetTimeSyncEnabled(bool aEnabled) { mTimeSyncEnabled = aEnabled; } #endif +protected: + /** + * This method initializes the `Neighbor` object. + * + * @param[in] aInstance A reference to OpenThread instance. + * + */ + void Init(Instance &aInstance); + private: Mac::ExtAddress mMacAddr; ///< The IEEE 802.15.4 Extended Address TimeMilli mLastHeard; ///< Time when last heard. @@ -511,6 +519,14 @@ public: otChildIp6AddressIterator mIndex; }; + /** + * This method initializes the `Child` object. + * + * @param[in] aInstance A reference to OpenThread instance. + * + */ + void Init(Instance &aInstance) { Neighbor::Init(aInstance); } + /** * This method clears the child entry. * @@ -726,6 +742,14 @@ private: class Router : public Neighbor { public: + /** + * This method initializes the `Router` object. + * + * @param[in] aInstance A reference to OpenThread instance. + * + */ + void Init(Instance &aInstance) { Neighbor::Init(aInstance); } + /** * This method clears the router entry. * diff --git a/tests/unit/test_child.cpp b/tests/unit/test_child.cpp index 06ac80194..f5dec7873 100644 --- a/tests/unit/test_child.cpp +++ b/tests/unit/test_child.cpp @@ -120,6 +120,8 @@ void TestChildIp6Address(void) sInstance = testInitInstance(); VerifyOrQuit(sInstance != NULL, "Null instance"); + child.Init(*sInstance); + //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - printf("\nConverting IPv6 addresses from string");