Skip to content

Commit

Permalink
[bbr-local] simplify AddService() and its use (openthread#9477)
Browse files Browse the repository at this point in the history
This commit contains smaller changes in `BackbonRouter::Local`:

- `AddService()` now gets a `RegisterationMode` enum as its input
  either decide based on current state or force registration.
- We remove the `NetworkData::Notifier::HandleServerDataUpdated()`
  calls after calling `AddService()` since `AddService()` method
  itself will already call this (when successfully adds the service
  to Network Data)
- We also remove the extra state check before calling `AddService()`
  as it is done by the `AddService()` method itself.
  • Loading branch information
abtink authored Oct 3, 2023
1 parent becba6b commit 3043f2e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/core/api/backbone_router_ftd_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ otError otBackboneRouterSetConfig(otInstance *aInstance, const otBackboneRouterC

otError otBackboneRouterRegister(otInstance *aInstance)
{
return AsCoreType(aInstance).Get<BackboneRouter::Local>().AddService(true /* Force registration */);
return AsCoreType(aInstance).Get<BackboneRouter::Local>().AddService(BackboneRouter::Local::kForceRegistration);
}

uint8_t otBackboneRouterGetRegistrationJitter(otInstance *aInstance)
Expand Down
25 changes: 13 additions & 12 deletions src/core/backbone_router/bbr_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void Local::SetEnabled(bool aEnable)
{
SetState(kStateSecondary);
AddDomainPrefixToNetworkData();
IgnoreError(AddService());
IgnoreError(AddService(kDecideBasedOnState));
}
else
{
Expand Down Expand Up @@ -163,26 +163,30 @@ Error Local::SetConfig(const Config &aConfig)
{
Get<Notifier>().Signal(kEventThreadBackboneRouterLocalChanged);

IgnoreError(AddService());
IgnoreError(AddService(kDecideBasedOnState));
}

exit:
LogBackboneRouterService("Set", error);
return error;
}

Error Local::AddService(bool aForce)
Error Local::AddService(RegisterMode aMode)
{
Error error = kErrorInvalidState;
NetworkData::Service::BackboneRouter::ServerData serverData;

VerifyOrExit(mState != kStateDisabled && Get<Mle::Mle>().IsAttached());

VerifyOrExit(aForce /* if register by force */ ||
!Get<BackboneRouter::Leader>().HasPrimary() /* if no available Backbone Router service */ ||
Get<BackboneRouter::Leader>().GetServer16() == Get<Mle::MleRouter>().GetRloc16()
/* If the device itself should be BBR. */
);
switch (aMode)
{
case kDecideBasedOnState:
VerifyOrExit(!Get<BackboneRouter::Leader>().HasPrimary() ||
Get<BackboneRouter::Leader>().GetServer16() == Get<Mle::MleRouter>().GetRloc16());
break;
case kForceRegistration:
break;
}

serverData.SetSequenceNumber(mSequenceNumber);
serverData.SetReregistrationDelay(mReregistrationDelay);
Expand Down Expand Up @@ -272,10 +276,7 @@ void Local::HandleBackboneRouterPrimaryUpdate(Leader::State aState, const Config
mMlrTimeout = aConfig.mMlrTimeout;
SequenceNumberIncrease();
Get<Notifier>().Signal(kEventThreadBackboneRouterLocalChanged);
if (AddService(true /* Force registration to refresh and restore Primary state */) == kErrorNone)
{
Get<NetworkData::Notifier>().HandleServerDataUpdated();
}
IgnoreError(AddService(kForceRegistration));
}
else
{
Expand Down
16 changes: 12 additions & 4 deletions src/core/backbone_router/bbr_local.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ class Local : public InstanceLocator, private NonCopyable
kStatePrimary = OT_BACKBONE_ROUTER_STATE_PRIMARY, ///< The Primary Backbone Router.
};

/**
* Represents registration mode used as input to `AddService()` method.
*
*/
enum RegisterMode : uint8_t
{
kDecideBasedOnState, ///< Decide based on current state.
kForceRegistration, ///< Force registration regardless of current state.
};

/**
* Initializes the local Backbone Router.
*
Expand Down Expand Up @@ -137,16 +147,14 @@ class Local : public InstanceLocator, private NonCopyable
/**
* Registers Backbone Router Dataset to Leader.
*
* @param[in] aForce True to force registration regardless of current state.
* False to decide based on current state.
*
* @param[in] aMode The registration mode to use (decide based on current state or force registration).
*
* @retval kErrorNone Successfully added the Service entry.
* @retval kErrorInvalidState Not in the ready state to register.
* @retval kErrorNoBufs Insufficient space to add the Service entry.
*
*/
Error AddService(bool aForce = false);
Error AddService(RegisterMode aMode);

/**
* Indicates whether or not the Backbone Router is Primary.
Expand Down
9 changes: 1 addition & 8 deletions src/core/thread/mle_router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1550,14 +1550,7 @@ void MleRouter::HandleTimeTick(void)

if (mBackboneRouterRegistrationDelay == 0)
{
// If no Backbone Router service after jitter, try to register its own Backbone Router Service.
if (!Get<BackboneRouter::Leader>().HasPrimary())
{
if (Get<BackboneRouter::Local>().AddService() == kErrorNone)
{
Get<NetworkData::Notifier>().HandleServerDataUpdated();
}
}
IgnoreError(Get<BackboneRouter::Local>().AddService(BackboneRouter::Local::kDecideBasedOnState));
}
}
#endif
Expand Down

0 comments on commit 3043f2e

Please sign in to comment.