[mle] move child ID allocation logic to ChildTable (#11764)

This commit moves the child ID allocation logic from `Mle` to
`ChildTable`.

A new `AllocateNewChildRloc16()` method is added to `ChildTable` to
contain the allocation logic, and the `mNextChildId` counter is moved
to `ChildTable`. `Mle` is updated to use this new method.

This refactoring improves code encapsulation by making the
`ChildTable` responsible for managing all aspects of the children it
contains, including ID allocation.
This commit is contained in:
Abtin Keshavarzian
2025-07-29 17:03:02 -07:00
committed by GitHub
parent 72ee935dec
commit 6c55d53a50
5 changed files with 36 additions and 19 deletions
+27
View File
@@ -39,6 +39,9 @@
namespace ot {
//---------------------------------------------------------------------------------------------------------------------
// `ChildTable::Iterator`
ChildTable::Iterator::Iterator(Instance &aInstance, Child::StateFilter aFilter)
: InstanceLocator(aInstance)
, ItemPtrIterator(nullptr)
@@ -71,8 +74,12 @@ exit:
return;
}
//---------------------------------------------------------------------------------------------------------------------
// `ChildTable`
ChildTable::ChildTable(Instance &aInstance)
: InstanceLocator(aInstance)
, mNextChildId(Mle::kMaxChildId)
, mMaxChildrenAllowed(kMaxChildren)
{
for (Child &child : mChildren)
@@ -112,6 +119,26 @@ exit:
return child;
}
uint16_t ChildTable::AllocateNewChildRloc16(void)
{
uint16_t rloc16;
do
{
mNextChildId++;
if (mNextChildId > Mle::kMaxChildId)
{
mNextChildId = Mle::kMinChildId;
}
rloc16 = Get<Mle::Mle>().GetRloc16() | mNextChildId;
} while (FindChild(rloc16, Child::kInStateAnyExceptInvalid) != nullptr);
return rloc16;
}
const Child *ChildTable::FindChild(const Child::AddressMatcher &aMatcher) const
{
const Child *child = mChildren;
+8
View File
@@ -136,6 +136,13 @@ public:
*/
Child *GetNewChild(void);
/**
* Allocates a new child ID and returns the corresponding RLOC16.
*
* @returns The allocated child RLOC16.
*/
uint16_t AllocateNewChildRloc16(void);
/**
* Searches the child table for a `Child` with a given RLOC16 also matching a given state filter.
*
@@ -330,6 +337,7 @@ private:
const Child *FindChild(const Child::AddressMatcher &aMatcher) const;
void RefreshStoredChildren(void);
uint16_t mNextChildId;
uint16_t mMaxChildrenAllowed;
Child mChildren[kMaxChildren];
};
-1
View File
@@ -106,7 +106,6 @@ Mle::Mle(Instance &aInstance)
, mMaxChildIpAddresses(0)
#endif
, mParentPriority(kParentPriorityUnspecified)
, mNextChildId(kMaxChildId)
, mPreviousPartitionIdRouter(0)
, mPreviousPartitionId(0)
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
-1
View File
@@ -2324,7 +2324,6 @@ private:
uint8_t mMaxChildIpAddresses;
#endif
int8_t mParentPriority;
uint16_t mNextChildId;
uint32_t mPreviousPartitionIdRouter;
uint32_t mPreviousPartitionId;
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
+1 -17
View File
@@ -2855,23 +2855,7 @@ Error Mle::SendChildIdResponse(Child &aChild)
if ((aChild.GetRloc16() == 0) || !HasMatchingRouterIdWith(aChild.GetRloc16()))
{
uint16_t rloc16;
// Pick next Child ID that is not being used
do
{
mNextChildId++;
if (mNextChildId > kMaxChildId)
{
mNextChildId = kMinChildId;
}
rloc16 = Get<Mac::Mac>().GetShortAddress() | mNextChildId;
} while (mChildTable.FindChild(rloc16, Child::kInStateAnyExceptInvalid) != nullptr);
aChild.SetRloc16(rloc16);
aChild.SetRloc16(mChildTable.AllocateNewChildRloc16());
}
SuccessOrExit(error = message->AppendAddress16Tlv(aChild.GetRloc16()));