[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.
This commit is contained in:
Abtin Keshavarzian
2020-02-20 09:36:34 -08:00
committed by Jonathan Hui
parent 67c5081d14
commit fb65c77c1a
6 changed files with 54 additions and 6 deletions
+5 -1
View File
@@ -102,7 +102,11 @@ ChildTable::ChildTable(Instance &aInstance)
: InstanceLocator(aInstance) : InstanceLocator(aInstance)
, mMaxChildrenAllowed(kMaxChildren) , mMaxChildrenAllowed(kMaxChildren)
{ {
Clear(); for (Child *child = &mChildren[0]; child < OT_ARRAY_END(mChildren); child++)
{
child->Init(aInstance);
child->Clear();
}
} }
void ChildTable::Clear(void) void ChildTable::Clear(void)
+3
View File
@@ -114,6 +114,9 @@ Mle::Mle(Instance &aInstance)
{ {
otMeshLocalPrefix meshLocalPrefix; otMeshLocalPrefix meshLocalPrefix;
mParent.Init(aInstance);
mParentCandidate.Init(aInstance);
mLeaderData.Clear(); mLeaderData.Clear();
mParentLeaderData.Clear(); mParentLeaderData.Clear();
mParent.Clear(); mParent.Clear();
+5
View File
@@ -65,6 +65,11 @@ RouterTable::RouterTable(Instance &aInstance)
, mRouterIdSequence(Random::NonCrypto::GetUint8()) , mRouterIdSequence(Random::NonCrypto::GetUint8())
, mActiveRouterCount(0) , mActiveRouterCount(0)
{ {
for (uint8_t index = 0; index < Mle::kMaxRouters; index++)
{
mRouters[index].Init(aInstance);
}
Clear(); Clear();
} }
+12 -2
View File
@@ -41,6 +41,12 @@
namespace ot { namespace ot {
void Neighbor::Init(Instance &aInstance)
{
InstanceLocatorInit::Init(aInstance);
SetState(kStateInvalid);
}
bool Neighbor::IsStateValidOrAttaching(void) const bool Neighbor::IsStateValidOrAttaching(void) const
{ {
bool rval = false; bool rval = false;
@@ -105,8 +111,10 @@ void Neighbor::GenerateChallenge(void)
void Child::Clear(void) void Child::Clear(void)
{ {
Instance &instance = GetInstance();
memset(reinterpret_cast<void *>(this), 0, sizeof(Child)); memset(reinterpret_cast<void *>(this), 0, sizeof(Child));
SetState(kStateInvalid); Init(instance);
} }
void Child::ClearIp6Addresses(void) void Child::ClearIp6Addresses(void)
@@ -282,8 +290,10 @@ void Child::GenerateChallenge(void)
void Router::Clear(void) void Router::Clear(void)
{ {
Instance &instance = GetInstance();
memset(reinterpret_cast<void *>(this), 0, sizeof(Router)); memset(reinterpret_cast<void *>(this), 0, sizeof(Router));
SetState(kStateInvalid); Init(instance);
} }
} // namespace ot } // namespace ot
+27 -3
View File
@@ -38,6 +38,7 @@
#include <openthread/thread_ftd.h> #include <openthread/thread_ftd.h>
#include "common/locator.hpp"
#include "common/message.hpp" #include "common/message.hpp"
#include "common/random.hpp" #include "common/random.hpp"
#include "common/timer.hpp" #include "common/timer.hpp"
@@ -50,13 +51,11 @@
namespace ot { namespace ot {
class Instance;
/** /**
* This class represents a Thread neighbor. * This class represents a Thread neighbor.
* *
*/ */
class Neighbor class Neighbor : public InstanceLocatorInit
{ {
public: public:
/** /**
@@ -420,6 +419,15 @@ public:
void SetTimeSyncEnabled(bool aEnabled) { mTimeSyncEnabled = aEnabled; } void SetTimeSyncEnabled(bool aEnabled) { mTimeSyncEnabled = aEnabled; }
#endif #endif
protected:
/**
* This method initializes the `Neighbor` object.
*
* @param[in] aInstance A reference to OpenThread instance.
*
*/
void Init(Instance &aInstance);
private: private:
Mac::ExtAddress mMacAddr; ///< The IEEE 802.15.4 Extended Address Mac::ExtAddress mMacAddr; ///< The IEEE 802.15.4 Extended Address
TimeMilli mLastHeard; ///< Time when last heard. TimeMilli mLastHeard; ///< Time when last heard.
@@ -511,6 +519,14 @@ public:
otChildIp6AddressIterator mIndex; 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. * This method clears the child entry.
* *
@@ -726,6 +742,14 @@ private:
class Router : public Neighbor class Router : public Neighbor
{ {
public: 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. * This method clears the router entry.
* *
+2
View File
@@ -120,6 +120,8 @@ void TestChildIp6Address(void)
sInstance = testInitInstance(); sInstance = testInitInstance();
VerifyOrQuit(sInstance != NULL, "Null instance"); VerifyOrQuit(sInstance != NULL, "Null instance");
child.Init(*sInstance);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
printf("\nConverting IPv6 addresses from string"); printf("\nConverting IPv6 addresses from string");