diff --git a/src/core/backbone_router/bbr_leader.hpp b/src/core/backbone_router/bbr_leader.hpp index 465765d45..4102651f2 100644 --- a/src/core/backbone_router/bbr_leader.hpp +++ b/src/core/backbone_router/bbr_leader.hpp @@ -39,6 +39,7 @@ #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) #include +#include #include #include "coap/coap.hpp" @@ -72,10 +73,10 @@ static_assert(kParentAggregateDelay > 1, "kParentAggregateDelay should be larger */ enum DomainPrefixEvent : uint8_t { - kDomainPrefixAdded, ///< Domain Prefix Added. - kDomainPrefixRemoved, ///< Domain Prefix Removed. - kDomainPrefixRefreshed, ///< Domain Prefix Changed. - kDomainPrefixUnchanged, ///< Domain Prefix did not change. + kDomainPrefixAdded = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_ADDED, ///< Domain Prefix Added. + kDomainPrefixRemoved = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_REMOVED, ///< Domain Prefix Removed. + kDomainPrefixRefreshed = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_CHANGED, ///< Domain Prefix Changed. + kDomainPrefixUnchanged, ///< Domain Prefix did not change. }; /** diff --git a/src/core/backbone_router/bbr_local.cpp b/src/core/backbone_router/bbr_local.cpp index ffdc4757b..66cb8f519 100644 --- a/src/core/backbone_router/bbr_local.cpp +++ b/src/core/backbone_router/bbr_local.cpp @@ -51,19 +51,19 @@ RegisterLogModule("BbrLocal"); Local::Local(Instance &aInstance) : InstanceLocator(aInstance) + , mIsServiceAdded(false) , mState(kStateDisabled) - , mMlrTimeout(kDefaultMlrTimeout) - , mReregistrationDelay(kDefaultRegistrationDelay) - , mRegistrationTimeout(0) , mSequenceNumber(Random::NonCrypto::GetUint8() % 127) , mRegistrationJitter(kDefaultRegistrationJitter) - , mIsServiceAdded(false) + , mReregistrationDelay(kDefaultRegistrationDelay) + , mRegistrationTimeout(0) + , mMlrTimeout(kDefaultMlrTimeout) { mDomainPrefixConfig.GetPrefix().SetLength(0); // Primary Backbone Router Aloc - mBackboneRouterPrimaryAloc.InitAsThreadOriginMeshLocal(); - mBackboneRouterPrimaryAloc.GetAddress().GetIid().SetToLocator(Mle::kAloc16BackboneRouterPrimary); + mBbrPrimaryAloc.InitAsThreadOriginMeshLocal(); + mBbrPrimaryAloc.GetAddress().GetIid().SetToLocator(Mle::kAloc16BackboneRouterPrimary); // All Network Backbone Routers Multicast Address. mAllNetworkBackboneRouters.Clear(); @@ -110,7 +110,7 @@ void Local::Reset(void) if (mState == kStatePrimary) { // Increase sequence number when changing from Primary to Secondary. - SequenceNumberIncrease(); + IncrementSequenceNumber(); Get().Signal(kEventThreadBackboneRouterLocalChanged); SetState(kStateSecondary); } @@ -168,7 +168,7 @@ Error Local::SetConfig(const Config &aConfig) } exit: - LogBackboneRouterService("Set", error); + LogService(kActionSet, error); return error; } @@ -199,7 +199,7 @@ Error Local::AddService(RegisterMode aMode) mIsServiceAdded = true; exit: - LogBackboneRouterService("Add", error); + LogService(kActionAdd, error); return error; } @@ -212,28 +212,31 @@ void Local::RemoveService(void) mIsServiceAdded = false; exit: - LogBackboneRouterService("Remove", error); + LogService(kActionRemove, error); } void Local::SetState(State aState) { VerifyOrExit(mState != aState); - if (mState == kStateDisabled) + switch (mState) { + case kStateDisabled: // Update All Network Backbone Routers Multicast Address for both Secondary and Primary state. mAllNetworkBackboneRouters.SetMulticastNetworkPrefix(Get().GetMeshLocalPrefix()); + break; + case kStateSecondary: + break; + case kStatePrimary: + Get().RemoveUnicastAddress(mBbrPrimaryAloc); + break; } - if (mState == kStatePrimary) + if (aState == kStatePrimary) { - Get().RemoveUnicastAddress(mBackboneRouterPrimaryAloc); - } - else if (aState == kStatePrimary) - { - // Add Primary Backbone Router Aloc for Primary Backbone Router. - mBackboneRouterPrimaryAloc.GetAddress().SetPrefix(Get().GetMeshLocalPrefix()); - Get().AddUnicastAddress(mBackboneRouterPrimaryAloc); + // Add Primary Backbone Router ALOC for Primary Backbone Router. + mBbrPrimaryAloc.GetAddress().SetPrefix(Get().GetMeshLocalPrefix()); + Get().AddUnicastAddress(mBbrPrimaryAloc); } mState = aState; @@ -274,7 +277,7 @@ void Local::HandleBackboneRouterPrimaryUpdate(Leader::State aState, const Config mSequenceNumber = aConfig.mSequenceNumber; mReregistrationDelay = aConfig.mReregistrationDelay; mMlrTimeout = aConfig.mMlrTimeout; - SequenceNumberIncrease(); + IncrementSequenceNumber(); Get().Signal(kEventThreadBackboneRouterLocalChanged); IgnoreError(AddService(kForceRegistration)); } @@ -353,7 +356,7 @@ Error Local::SetDomainPrefix(const NetworkData::OnMeshPrefixConfig &aConfig) } mDomainPrefixConfig = aConfig; - LogDomainPrefix("Set", kErrorNone); + LogDomainPrefix(kActionSet, kErrorNone); if (IsEnabled()) { @@ -394,22 +397,10 @@ void Local::HandleDomainPrefixUpdate(DomainPrefixEvent aEvent) Get().SubscribeMulticast(mAllDomainBackboneRouters); } - if (mDomainPrefixCallback.IsSet()) + if (aEvent != kDomainPrefixUnchanged) { - switch (aEvent) - { - case kDomainPrefixAdded: - mDomainPrefixCallback.Invoke(OT_BACKBONE_ROUTER_DOMAIN_PREFIX_ADDED, Get().GetDomainPrefix()); - break; - case kDomainPrefixRemoved: - mDomainPrefixCallback.Invoke(OT_BACKBONE_ROUTER_DOMAIN_PREFIX_REMOVED, Get().GetDomainPrefix()); - break; - case kDomainPrefixRefreshed: - mDomainPrefixCallback.Invoke(OT_BACKBONE_ROUTER_DOMAIN_PREFIX_CHANGED, Get().GetDomainPrefix()); - break; - default: - break; - } + mDomainPrefixCallback.InvokeIfSet(static_cast(aEvent), + Get().GetDomainPrefix()); } exit: @@ -425,10 +416,10 @@ void Local::RemoveDomainPrefixFromNetworkData(void) error = Get().RemoveOnMeshPrefix(mDomainPrefixConfig.GetPrefix()); } - LogDomainPrefix("Remove", error); + LogDomainPrefix(kActionRemove, error); } -void Local::SequenceNumberIncrease(void) +void Local::IncrementSequenceNumber(void) { switch (mSequenceNumber) { @@ -455,21 +446,38 @@ void Local::AddDomainPrefixToNetworkData(void) error = Get().AddOnMeshPrefix(mDomainPrefixConfig); } - LogDomainPrefix("Add", error); + LogDomainPrefix(kActionAdd, error); } #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) -void Local::LogDomainPrefix(const char *aAction, Error aError) + +const char *Local::ActionToString(Action aAction) { - LogInfo("%s Domain Prefix: %s, %s", aAction, mDomainPrefixConfig.GetPrefix().ToString().AsCString(), + static const char *const kActionStrings[] = { + "Set", // (0) kActionSet + "Add", // (1) kActionAdd + "Remove", // (2) kActionRemove + }; + + static_assert(0 == kActionSet, "kActionSet value is incorrect"); + static_assert(1 == kActionAdd, "kActionAdd value is incorrect"); + static_assert(2 == kActionRemove, "kActionRemove value is incorrect"); + + return kActionStrings[aAction]; +} + +void Local::LogDomainPrefix(Action aAction, Error aError) +{ + LogInfo("%s Domain Prefix: %s, %s", ActionToString(aAction), mDomainPrefixConfig.GetPrefix().ToString().AsCString(), ErrorToString(aError)); } -void Local::LogBackboneRouterService(const char *aAction, Error aError) +void Local::LogService(Action aAction, Error aError) { - LogInfo("%s BBR Service: seqno (%u), delay (%us), timeout (%lus), %s", aAction, mSequenceNumber, + LogInfo("%s BBR Service: seqno (%u), delay (%us), timeout (%lus), %s", ActionToString(aAction), mSequenceNumber, mReregistrationDelay, ToUlong(mMlrTimeout), ErrorToString(aError)); } + #endif } // namespace BackboneRouter diff --git a/src/core/backbone_router/bbr_local.hpp b/src/core/backbone_router/bbr_local.hpp index 90b2d894d..754f5a2a1 100644 --- a/src/core/backbone_router/bbr_local.hpp +++ b/src/core/backbone_router/bbr_local.hpp @@ -76,6 +76,8 @@ class Local : public InstanceLocator, private NonCopyable friend class ot::TimeTicker; public: + typedef otBackboneRouterDomainPrefixCallback DomainPrefixCallback; ///< Domain Prefix callback. + /** * Represents Backbone Router state. * @@ -273,44 +275,49 @@ public: * @param[in] aContext A user context pointer. * */ - void SetDomainPrefixCallback(otBackboneRouterDomainPrefixCallback aCallback, void *aContext) + void SetDomainPrefixCallback(DomainPrefixCallback aCallback, void *aContext) { mDomainPrefixCallback.Set(aCallback, aContext); } private: + enum Action : uint8_t + { + kActionSet, + kActionAdd, + kActionRemove, + }; + void SetState(State aState); void RemoveService(void); void HandleTimeTick(void); void AddDomainPrefixToNetworkData(void); void RemoveDomainPrefixFromNetworkData(void); - void SequenceNumberIncrease(void); + void IncrementSequenceNumber(void); #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) - void LogBackboneRouterService(const char *aAction, Error aError); - void LogDomainPrefix(const char *aAction, Error aError); + static const char *ActionToString(Action aAction); + void LogService(Action aAction, Error aError); + void LogDomainPrefix(Action aAction, Error aError); #else - void LogBackboneRouterService(const char *, Error) {} - void LogDomainPrefix(const char *, Error) {} + void LogService(Action, Error) {} + void LogDomainPrefix(Action, Error) {} #endif - State mState; - uint32_t mMlrTimeout; - uint16_t mReregistrationDelay; - uint16_t mRegistrationTimeout; - uint8_t mSequenceNumber; - uint8_t mRegistrationJitter; - // Indicates whether or not already add Backbone Router Service to local server data. // Used to check whether or not in restore stage after reset or whether to remove // Backbone Router service for Secondary Backbone Router if it was added by force. - bool mIsServiceAdded; - + bool mIsServiceAdded; + State mState; + uint8_t mSequenceNumber; + uint8_t mRegistrationJitter; + uint16_t mReregistrationDelay; + uint16_t mRegistrationTimeout; + uint32_t mMlrTimeout; NetworkData::OnMeshPrefixConfig mDomainPrefixConfig; - - Ip6::Netif::UnicastAddress mBackboneRouterPrimaryAloc; - Ip6::Address mAllNetworkBackboneRouters; - Ip6::Address mAllDomainBackboneRouters; - Callback mDomainPrefixCallback; + Ip6::Netif::UnicastAddress mBbrPrimaryAloc; + Ip6::Address mAllNetworkBackboneRouters; + Ip6::Address mAllDomainBackboneRouters; + Callback mDomainPrefixCallback; }; } // namespace BackboneRouter