From 3313b663429addbcf6a987aeb76295badadf2bd9 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Mon, 25 Jul 2016 19:14:50 -0700 Subject: [PATCH] Add API to constrain the use of the Router/Leader role. (#285) --- include/openthread.h | 17 +++++++++++++++++ src/core/openthread.cpp | 10 ++++++++++ src/core/thread/mle_router.cpp | 18 +++++++++++++++++- src/core/thread/mle_router.hpp | 21 +++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/include/openthread.h b/include/openthread.h index 5fd68d1f2..78eccbbd8 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -478,6 +478,23 @@ otPanId otGetPanId(void); */ ThreadError otSetPanId(otPanId aPanId); +/** + * This function indicates whether or not the Router Role is enabled. + * + * @retval TRUE If the Router Role is enabled. + * @retval FALSE If the Router Role is not enabled. + * + */ +bool otIsRouterRoleEnabled(void); + +/** + * This function sets whether or not the Router Role is enabled. + * + * @param[in] aEnabled TRUE if the Router Role is enabled, FALSE otherwise. + * + */ +void otSetRouterRoleEnabled(bool aEnabled); + /** * Get the IEEE 802.15.4 Short Address. * diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index 569fad5bb..e5a7c180f 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -250,6 +250,16 @@ ThreadError otSetPanId(otPanId aPanId) return sThreadNetif->GetMac().SetPanId(aPanId); } +bool otIsRouterRoleEnabled(void) +{ + return sThreadNetif->GetMle().IsRouterRoleEnabled(); +} + +void otSetRouterRoleEnabled(bool aEnabled) +{ + sThreadNetif->GetMle().SetRouterRoleEnabled(aEnabled); +} + otShortAddress otGetShortAddress(void) { return sThreadNetif->GetMac().GetShortAddress(); diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 940aa85d4..a008cc4e5 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -68,10 +68,26 @@ MleRouter::MleRouter(ThreadNetif &aThreadNetif): mPreviousRouterId = kMaxRouterId; mAdvertiseInterval = kAdvertiseIntervalMin; mRouterIdSequenceLastUpdated = 0; + mRouterRoleEnabled = true; mCoapMessageId = otPlatRandomGet(); } +bool MleRouter::IsRouterRoleEnabled(void) const +{ + return mRouterRoleEnabled && (mDeviceMode & ModeTlv::kModeFFD); +} + +void MleRouter::SetRouterRoleEnabled(bool aEnabled) +{ + mRouterRoleEnabled = aEnabled; + + if (!mRouterRoleEnabled && (mDeviceState == kDeviceStateRouter || mDeviceState == kDeviceStateLeader)) + { + BecomeDetached(); + } +} + int MleRouter::AllocateRouterId(void) { int rval = -1; @@ -167,7 +183,7 @@ ThreadError MleRouter::BecomeRouter(void) VerifyOrExit(mDeviceState == kDeviceStateDetached || mDeviceState == kDeviceStateChild, error = kThreadError_Busy); - VerifyOrExit(mDeviceMode & ModeTlv::kModeFFD, ;); + VerifyOrExit(mRouterRoleEnabled && (mDeviceMode & ModeTlv::kModeFFD), ;); for (int i = 0; i < kMaxRouterId; i++) { diff --git a/src/core/thread/mle_router.hpp b/src/core/thread/mle_router.hpp index 009d75c15..ca5e0d3bb 100644 --- a/src/core/thread/mle_router.hpp +++ b/src/core/thread/mle_router.hpp @@ -76,6 +76,26 @@ public: */ explicit MleRouter(ThreadNetif &aThreadNetif); + /** + * This method indicates whether or not the Router Role is enabled. + * + * @retval true If the Router Role is enabled. + * @retval false If the Router Role is not enabled. + * + */ + bool IsRouterRoleEnabled(void) const; + + /** + * This method sets whether or not the Router Role is enabled. + * + * If @p aEnable is false and the device is currently operating as a router, this call will cause the device to + * detach and attempt to reattach as a child. + * + * @param[in] aEnabled TRUE to enable the Router Role, FALSE otherwise. + * + */ + void SetRouterRoleEnabled(bool aEnabled); + /** * This method generates an Address Solicit request for a Router ID. * @@ -436,6 +456,7 @@ private: uint8_t mNetworkIdTimeout; uint8_t mRouterUpgradeThreshold; uint8_t mLeaderWeight; + bool mRouterRoleEnabled; int8_t mRouterId; int8_t mPreviousRouterId;