Make sure Child ID is not being used before allocating. (#244)

This commit is contained in:
Jonathan Hui
2016-07-07 08:41:35 -07:00
committed by GitHub
parent d6107c5f53
commit 75f7304914
3 changed files with 19 additions and 12 deletions
+1 -1
View File
@@ -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
+4 -3
View File
@@ -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
};
/**
+14 -8
View File
@@ -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);
}