From d1c83d362595d9aee2797fad8695d0de8d0bbe07 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 28 Sep 2016 15:35:03 -0700 Subject: [PATCH] Add support for setting preferred Router Id (#716) This commit adds a new ot API to set preferred Router Id for the node. Upon becoming a router/leader the node attempts to use the preferred Router Id. If it is not set or if it can not be used, a randomly generated Router Id is picked. --- include/openthread.h | 17 +++++++++++++++++ src/core/openthread.cpp | 5 +++++ src/core/thread/mle_router.cpp | 18 +++++++++++++++++- src/core/thread/mle_router.hpp | 14 ++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/openthread.h b/include/openthread.h index 8c372bbf7..3c0c346fc 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -932,6 +932,23 @@ uint32_t otGetPollPeriod(otInstance *aInstance); */ void otSetPollPeriod(otInstance *aInstance, uint32_t aPollPeriod); +/** + * Set the preferred Router Id. + * + * Upon becoming a router/leader the node attempts to use this Router Id. If the + * preferred Router Id is not set or if it can not be used, a randomly generated + * router id is picked. This property can be set only when the device role is + * either detached or disabled. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aRouterId The preferred Router Id. + * + * @retval kThreadError_None Successfully set the preferred Router Id. + * @retval kThreadError_InvalidState Could not set (role is not detached or disabled) + * + */ +ThreadError otSetPreferredRouterId(otInstance *aInstance, uint8_t aRouterId); + /** * @} */ diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index 365a3ba4d..577f83c81 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -938,6 +938,11 @@ void otSetPollPeriod(otInstance *, uint32_t aPollPeriod) sThreadNetif->GetMeshForwarder().SetAssignPollPeriod(aPollPeriod); } +ThreadError otSetPreferredRouterId(otInstance *, uint8_t aRouterId) +{ + return sThreadNetif->GetMle().SetPreferredRouterId(aRouterId); +} + #ifdef OPENTHREAD_MULTIPLE_INSTANCE otInstance *otInstanceInit(void *aInstanceBuffer, uint64_t *aInstanceBufferSize) diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 8142b8d14..8a57c91d1 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -245,7 +245,8 @@ ThreadError MleRouter::BecomeLeader(void) routerId = IsRouterIdValid(mPreviousRouterId) ? AllocateRouterId(mPreviousRouterId) : AllocateRouterId(); VerifyOrExit(IsRouterIdValid(routerId), error = kThreadError_NoBufs); - mRouterId = static_cast(routerId); + + mRouterId = routerId; mPreviousRouterId = mRouterId; memcpy(&mRouters[mRouterId].mMacAddr, mMac.GetExtAddress(), sizeof(mRouters[mRouterId].mMacAddr)); @@ -2721,6 +2722,21 @@ void MleRouter::SetLeaderPartitionId(uint32_t aPartitionId) mFixedLeaderPartitionId = aPartitionId; } +ThreadError MleRouter::SetPreferredRouterId(uint8_t aRouterId) +{ + ThreadError error = kThreadError_None; + + VerifyOrExit( + (mDeviceState == kDeviceStateDetached) || (mDeviceState == kDeviceStateDisabled), + error = kThreadError_InvalidState + ); + + mPreviousRouterId = aRouterId; + +exit: + return error; +} + void MleRouter::HandleMacDataRequest(const Child &aChild) { static const uint8_t tlvs[] = {Tlv::kLeaderData, Tlv::kNetworkData}; diff --git a/src/core/thread/mle_router.hpp b/src/core/thread/mle_router.hpp index 35bbd3472..8f6cd768d 100644 --- a/src/core/thread/mle_router.hpp +++ b/src/core/thread/mle_router.hpp @@ -175,6 +175,20 @@ public: */ void SetLeaderPartitionId(uint32_t aPartitionId); + /** + * This method sets the preferred Router Id. Upon becoming a router/leader the node + * attempts to use this Router Id. If the preferred Router Id is not set or if it + * can not be used, a randomly generated router Id is picked. + * This property can be set when he device role is detached or disabled. + * + * @param[in] aRouterId The preferred Router Id. + * + * @retval kThreadError_None Successfully set the preferred Router Id. + * @retval kThreadError_InvalidState Could not set (role is other than detached and disabled) + * + */ + ThreadError SetPreferredRouterId(uint8_t aRouterId); + /** * This method returns the next hop towards an RLOC16 destination. *