diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index c8b629714..75ea6744a 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -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 diff --git a/src/core/thread/mle_constants.hpp b/src/core/thread/mle_constants.hpp index 2378b2396..c10ca376a 100644 --- a/src/core/thread/mle_constants.hpp +++ b/src/core/thread/mle_constants.hpp @@ -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 }; /** diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 7112b53ff..dcdeed3a7 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -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); }