mirror of
https://github.com/espressif/openthread.git
synced 2026-10-08 00:37:40 +00:00
[bbr-local] smaller enhancements (#9657)
This commit contains smaller change in `bbr_local` module: - `LogDomainPrefix()` and `LogService()` methods are updated to use newly added `Action` enumeration. - `HandleDomainPrefixUpdate()` is simplfied to pass the event directly to `mDomainPrefixCallback`. - `SetState()` is updated so we we first remove ALOC/multicast addresses based on the previous state before adding/updating any addresses based on the new state. - Some methods/variables are renamed (e.g., use shorter name).
This commit is contained in:
@@ -39,6 +39,7 @@
|
|||||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||||
|
|
||||||
#include <openthread/backbone_router.h>
|
#include <openthread/backbone_router.h>
|
||||||
|
#include <openthread/backbone_router_ftd.h>
|
||||||
#include <openthread/ip6.h>
|
#include <openthread/ip6.h>
|
||||||
|
|
||||||
#include "coap/coap.hpp"
|
#include "coap/coap.hpp"
|
||||||
@@ -72,10 +73,10 @@ static_assert(kParentAggregateDelay > 1, "kParentAggregateDelay should be larger
|
|||||||
*/
|
*/
|
||||||
enum DomainPrefixEvent : uint8_t
|
enum DomainPrefixEvent : uint8_t
|
||||||
{
|
{
|
||||||
kDomainPrefixAdded, ///< Domain Prefix Added.
|
kDomainPrefixAdded = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_ADDED, ///< Domain Prefix Added.
|
||||||
kDomainPrefixRemoved, ///< Domain Prefix Removed.
|
kDomainPrefixRemoved = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_REMOVED, ///< Domain Prefix Removed.
|
||||||
kDomainPrefixRefreshed, ///< Domain Prefix Changed.
|
kDomainPrefixRefreshed = OT_BACKBONE_ROUTER_DOMAIN_PREFIX_CHANGED, ///< Domain Prefix Changed.
|
||||||
kDomainPrefixUnchanged, ///< Domain Prefix did not change.
|
kDomainPrefixUnchanged, ///< Domain Prefix did not change.
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -51,19 +51,19 @@ RegisterLogModule("BbrLocal");
|
|||||||
|
|
||||||
Local::Local(Instance &aInstance)
|
Local::Local(Instance &aInstance)
|
||||||
: InstanceLocator(aInstance)
|
: InstanceLocator(aInstance)
|
||||||
|
, mIsServiceAdded(false)
|
||||||
, mState(kStateDisabled)
|
, mState(kStateDisabled)
|
||||||
, mMlrTimeout(kDefaultMlrTimeout)
|
|
||||||
, mReregistrationDelay(kDefaultRegistrationDelay)
|
|
||||||
, mRegistrationTimeout(0)
|
|
||||||
, mSequenceNumber(Random::NonCrypto::GetUint8() % 127)
|
, mSequenceNumber(Random::NonCrypto::GetUint8() % 127)
|
||||||
, mRegistrationJitter(kDefaultRegistrationJitter)
|
, mRegistrationJitter(kDefaultRegistrationJitter)
|
||||||
, mIsServiceAdded(false)
|
, mReregistrationDelay(kDefaultRegistrationDelay)
|
||||||
|
, mRegistrationTimeout(0)
|
||||||
|
, mMlrTimeout(kDefaultMlrTimeout)
|
||||||
{
|
{
|
||||||
mDomainPrefixConfig.GetPrefix().SetLength(0);
|
mDomainPrefixConfig.GetPrefix().SetLength(0);
|
||||||
|
|
||||||
// Primary Backbone Router Aloc
|
// Primary Backbone Router Aloc
|
||||||
mBackboneRouterPrimaryAloc.InitAsThreadOriginMeshLocal();
|
mBbrPrimaryAloc.InitAsThreadOriginMeshLocal();
|
||||||
mBackboneRouterPrimaryAloc.GetAddress().GetIid().SetToLocator(Mle::kAloc16BackboneRouterPrimary);
|
mBbrPrimaryAloc.GetAddress().GetIid().SetToLocator(Mle::kAloc16BackboneRouterPrimary);
|
||||||
|
|
||||||
// All Network Backbone Routers Multicast Address.
|
// All Network Backbone Routers Multicast Address.
|
||||||
mAllNetworkBackboneRouters.Clear();
|
mAllNetworkBackboneRouters.Clear();
|
||||||
@@ -110,7 +110,7 @@ void Local::Reset(void)
|
|||||||
if (mState == kStatePrimary)
|
if (mState == kStatePrimary)
|
||||||
{
|
{
|
||||||
// Increase sequence number when changing from Primary to Secondary.
|
// Increase sequence number when changing from Primary to Secondary.
|
||||||
SequenceNumberIncrease();
|
IncrementSequenceNumber();
|
||||||
Get<Notifier>().Signal(kEventThreadBackboneRouterLocalChanged);
|
Get<Notifier>().Signal(kEventThreadBackboneRouterLocalChanged);
|
||||||
SetState(kStateSecondary);
|
SetState(kStateSecondary);
|
||||||
}
|
}
|
||||||
@@ -168,7 +168,7 @@ Error Local::SetConfig(const Config &aConfig)
|
|||||||
}
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
LogBackboneRouterService("Set", error);
|
LogService(kActionSet, error);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,7 +199,7 @@ Error Local::AddService(RegisterMode aMode)
|
|||||||
mIsServiceAdded = true;
|
mIsServiceAdded = true;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
LogBackboneRouterService("Add", error);
|
LogService(kActionAdd, error);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,28 +212,31 @@ void Local::RemoveService(void)
|
|||||||
mIsServiceAdded = false;
|
mIsServiceAdded = false;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
LogBackboneRouterService("Remove", error);
|
LogService(kActionRemove, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Local::SetState(State aState)
|
void Local::SetState(State aState)
|
||||||
{
|
{
|
||||||
VerifyOrExit(mState != aState);
|
VerifyOrExit(mState != aState);
|
||||||
|
|
||||||
if (mState == kStateDisabled)
|
switch (mState)
|
||||||
{
|
{
|
||||||
|
case kStateDisabled:
|
||||||
// Update All Network Backbone Routers Multicast Address for both Secondary and Primary state.
|
// Update All Network Backbone Routers Multicast Address for both Secondary and Primary state.
|
||||||
mAllNetworkBackboneRouters.SetMulticastNetworkPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix());
|
mAllNetworkBackboneRouters.SetMulticastNetworkPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix());
|
||||||
|
break;
|
||||||
|
case kStateSecondary:
|
||||||
|
break;
|
||||||
|
case kStatePrimary:
|
||||||
|
Get<ThreadNetif>().RemoveUnicastAddress(mBbrPrimaryAloc);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mState == kStatePrimary)
|
if (aState == kStatePrimary)
|
||||||
{
|
{
|
||||||
Get<ThreadNetif>().RemoveUnicastAddress(mBackboneRouterPrimaryAloc);
|
// Add Primary Backbone Router ALOC for Primary Backbone Router.
|
||||||
}
|
mBbrPrimaryAloc.GetAddress().SetPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix());
|
||||||
else if (aState == kStatePrimary)
|
Get<ThreadNetif>().AddUnicastAddress(mBbrPrimaryAloc);
|
||||||
{
|
|
||||||
// Add Primary Backbone Router Aloc for Primary Backbone Router.
|
|
||||||
mBackboneRouterPrimaryAloc.GetAddress().SetPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix());
|
|
||||||
Get<ThreadNetif>().AddUnicastAddress(mBackboneRouterPrimaryAloc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mState = aState;
|
mState = aState;
|
||||||
@@ -274,7 +277,7 @@ void Local::HandleBackboneRouterPrimaryUpdate(Leader::State aState, const Config
|
|||||||
mSequenceNumber = aConfig.mSequenceNumber;
|
mSequenceNumber = aConfig.mSequenceNumber;
|
||||||
mReregistrationDelay = aConfig.mReregistrationDelay;
|
mReregistrationDelay = aConfig.mReregistrationDelay;
|
||||||
mMlrTimeout = aConfig.mMlrTimeout;
|
mMlrTimeout = aConfig.mMlrTimeout;
|
||||||
SequenceNumberIncrease();
|
IncrementSequenceNumber();
|
||||||
Get<Notifier>().Signal(kEventThreadBackboneRouterLocalChanged);
|
Get<Notifier>().Signal(kEventThreadBackboneRouterLocalChanged);
|
||||||
IgnoreError(AddService(kForceRegistration));
|
IgnoreError(AddService(kForceRegistration));
|
||||||
}
|
}
|
||||||
@@ -353,7 +356,7 @@ Error Local::SetDomainPrefix(const NetworkData::OnMeshPrefixConfig &aConfig)
|
|||||||
}
|
}
|
||||||
|
|
||||||
mDomainPrefixConfig = aConfig;
|
mDomainPrefixConfig = aConfig;
|
||||||
LogDomainPrefix("Set", kErrorNone);
|
LogDomainPrefix(kActionSet, kErrorNone);
|
||||||
|
|
||||||
if (IsEnabled())
|
if (IsEnabled())
|
||||||
{
|
{
|
||||||
@@ -394,22 +397,10 @@ void Local::HandleDomainPrefixUpdate(DomainPrefixEvent aEvent)
|
|||||||
Get<BackboneTmfAgent>().SubscribeMulticast(mAllDomainBackboneRouters);
|
Get<BackboneTmfAgent>().SubscribeMulticast(mAllDomainBackboneRouters);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mDomainPrefixCallback.IsSet())
|
if (aEvent != kDomainPrefixUnchanged)
|
||||||
{
|
{
|
||||||
switch (aEvent)
|
mDomainPrefixCallback.InvokeIfSet(static_cast<otBackboneRouterDomainPrefixEvent>(aEvent),
|
||||||
{
|
Get<Leader>().GetDomainPrefix());
|
||||||
case kDomainPrefixAdded:
|
|
||||||
mDomainPrefixCallback.Invoke(OT_BACKBONE_ROUTER_DOMAIN_PREFIX_ADDED, Get<Leader>().GetDomainPrefix());
|
|
||||||
break;
|
|
||||||
case kDomainPrefixRemoved:
|
|
||||||
mDomainPrefixCallback.Invoke(OT_BACKBONE_ROUTER_DOMAIN_PREFIX_REMOVED, Get<Leader>().GetDomainPrefix());
|
|
||||||
break;
|
|
||||||
case kDomainPrefixRefreshed:
|
|
||||||
mDomainPrefixCallback.Invoke(OT_BACKBONE_ROUTER_DOMAIN_PREFIX_CHANGED, Get<Leader>().GetDomainPrefix());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
@@ -425,10 +416,10 @@ void Local::RemoveDomainPrefixFromNetworkData(void)
|
|||||||
error = Get<NetworkData::Local>().RemoveOnMeshPrefix(mDomainPrefixConfig.GetPrefix());
|
error = Get<NetworkData::Local>().RemoveOnMeshPrefix(mDomainPrefixConfig.GetPrefix());
|
||||||
}
|
}
|
||||||
|
|
||||||
LogDomainPrefix("Remove", error);
|
LogDomainPrefix(kActionRemove, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Local::SequenceNumberIncrease(void)
|
void Local::IncrementSequenceNumber(void)
|
||||||
{
|
{
|
||||||
switch (mSequenceNumber)
|
switch (mSequenceNumber)
|
||||||
{
|
{
|
||||||
@@ -455,21 +446,38 @@ void Local::AddDomainPrefixToNetworkData(void)
|
|||||||
error = Get<NetworkData::Local>().AddOnMeshPrefix(mDomainPrefixConfig);
|
error = Get<NetworkData::Local>().AddOnMeshPrefix(mDomainPrefixConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
LogDomainPrefix("Add", error);
|
LogDomainPrefix(kActionAdd, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
|
#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));
|
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));
|
mReregistrationDelay, ToUlong(mMlrTimeout), ErrorToString(aError));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace BackboneRouter
|
} // namespace BackboneRouter
|
||||||
|
|||||||
@@ -76,6 +76,8 @@ class Local : public InstanceLocator, private NonCopyable
|
|||||||
friend class ot::TimeTicker;
|
friend class ot::TimeTicker;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
typedef otBackboneRouterDomainPrefixCallback DomainPrefixCallback; ///< Domain Prefix callback.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents Backbone Router state.
|
* Represents Backbone Router state.
|
||||||
*
|
*
|
||||||
@@ -273,44 +275,49 @@ public:
|
|||||||
* @param[in] aContext A user context pointer.
|
* @param[in] aContext A user context pointer.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void SetDomainPrefixCallback(otBackboneRouterDomainPrefixCallback aCallback, void *aContext)
|
void SetDomainPrefixCallback(DomainPrefixCallback aCallback, void *aContext)
|
||||||
{
|
{
|
||||||
mDomainPrefixCallback.Set(aCallback, aContext);
|
mDomainPrefixCallback.Set(aCallback, aContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
enum Action : uint8_t
|
||||||
|
{
|
||||||
|
kActionSet,
|
||||||
|
kActionAdd,
|
||||||
|
kActionRemove,
|
||||||
|
};
|
||||||
|
|
||||||
void SetState(State aState);
|
void SetState(State aState);
|
||||||
void RemoveService(void);
|
void RemoveService(void);
|
||||||
void HandleTimeTick(void);
|
void HandleTimeTick(void);
|
||||||
void AddDomainPrefixToNetworkData(void);
|
void AddDomainPrefixToNetworkData(void);
|
||||||
void RemoveDomainPrefixFromNetworkData(void);
|
void RemoveDomainPrefixFromNetworkData(void);
|
||||||
void SequenceNumberIncrease(void);
|
void IncrementSequenceNumber(void);
|
||||||
#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
|
#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
|
||||||
void LogBackboneRouterService(const char *aAction, Error aError);
|
static const char *ActionToString(Action aAction);
|
||||||
void LogDomainPrefix(const char *aAction, Error aError);
|
void LogService(Action aAction, Error aError);
|
||||||
|
void LogDomainPrefix(Action aAction, Error aError);
|
||||||
#else
|
#else
|
||||||
void LogBackboneRouterService(const char *, Error) {}
|
void LogService(Action, Error) {}
|
||||||
void LogDomainPrefix(const char *, Error) {}
|
void LogDomainPrefix(Action, Error) {}
|
||||||
#endif
|
#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.
|
// 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
|
// 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.
|
// 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;
|
NetworkData::OnMeshPrefixConfig mDomainPrefixConfig;
|
||||||
|
Ip6::Netif::UnicastAddress mBbrPrimaryAloc;
|
||||||
Ip6::Netif::UnicastAddress mBackboneRouterPrimaryAloc;
|
Ip6::Address mAllNetworkBackboneRouters;
|
||||||
Ip6::Address mAllNetworkBackboneRouters;
|
Ip6::Address mAllDomainBackboneRouters;
|
||||||
Ip6::Address mAllDomainBackboneRouters;
|
Callback<DomainPrefixCallback> mDomainPrefixCallback;
|
||||||
Callback<otBackboneRouterDomainPrefixCallback> mDomainPrefixCallback;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace BackboneRouter
|
} // namespace BackboneRouter
|
||||||
|
|||||||
Reference in New Issue
Block a user