mirror of
https://github.com/espressif/openthread.git
synced 2026-08-10 04:37:47 +00:00
Make sure Child ID is not being used before allocating. (#244)
This commit is contained in:
@@ -374,7 +374,7 @@ ThreadError Mle::SetMeshLocalPrefix(const uint8_t *aMeshLocalPrefix)
|
||||
|
||||
const uint8_t Mle::GetChildId(uint16_t aRloc16) const
|
||||
{
|
||||
return aRloc16 & kChildIdMask;
|
||||
return aRloc16 & kMaxChildId;
|
||||
}
|
||||
|
||||
const uint8_t Mle::GetRouterId(uint16_t aRloc16) const
|
||||
|
||||
@@ -61,9 +61,10 @@ enum
|
||||
|
||||
enum
|
||||
{
|
||||
kChildIdMask = 0x1ff,
|
||||
kRouterIdOffset = 10,
|
||||
kRlocPrefixLength = 14, ///< Prefix length of RLOC in bytes
|
||||
kMinChildId = 1, ///< Minimum Child ID
|
||||
kMaxChildId = 511, ///< Maximum Child ID
|
||||
kRouterIdOffset = 10, ///< Bit offset of Router ID in RLOC16
|
||||
kRlocPrefixLength = 14, ///< Prefix length of RLOC in bytes
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -56,7 +56,7 @@ MleRouter::MleRouter(ThreadNetif &aThreadNetif):
|
||||
mAddressRelease(OPENTHREAD_URI_ADDRESS_RELEASE, &HandleAddressRelease, this),
|
||||
mCoapServer(aThreadNetif.GetCoapServer())
|
||||
{
|
||||
mNextChildId = 1;
|
||||
mNextChildId = kMaxChildId;
|
||||
mRouterIdSequence = 0;
|
||||
memset(mChildren, 0, sizeof(mChildren));
|
||||
memset(mRouters, 0, sizeof(mRouters));
|
||||
@@ -1857,14 +1857,20 @@ ThreadError MleRouter::SendChildIdResponse(Child *aChild)
|
||||
SuccessOrExit(error = AppendSourceAddress(*message));
|
||||
SuccessOrExit(error = AppendLeaderData(*message));
|
||||
|
||||
aChild->mValid.mRloc16 = mMac.GetShortAddress() | mNextChildId;
|
||||
|
||||
mNextChildId++;
|
||||
|
||||
if (mNextChildId >= 512)
|
||||
// pick next Child ID that is not being used
|
||||
do
|
||||
{
|
||||
mNextChildId = 1;
|
||||
mNextChildId++;
|
||||
|
||||
if (mNextChildId > kMaxChildId)
|
||||
{
|
||||
mNextChildId = kMinChildId;
|
||||
}
|
||||
}
|
||||
while (FindChild(mNextChildId) != NULL);
|
||||
|
||||
// allocate Child ID
|
||||
aChild->mValid.mRloc16 = mMac.GetShortAddress() | mNextChildId;
|
||||
|
||||
SuccessOrExit(error = AppendAddress16(*message, aChild->mValid.mRloc16));
|
||||
|
||||
@@ -2287,7 +2293,7 @@ ThreadError MleRouter::GetChildInfoById(uint16_t aChildId, otChildInfo &aChildIn
|
||||
ThreadError error = kThreadError_None;
|
||||
Child *child;
|
||||
|
||||
if ((aChildId & ~kChildIdMask) != 0)
|
||||
if ((aChildId & ~kMaxChildId) != 0)
|
||||
{
|
||||
aChildId = GetChildId(aChildId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user