From 2bc5eec091811c81cf34e73e417d4027e8ce3795 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 30 May 2025 14:04:33 -0700 Subject: [PATCH] [notifier] streamline event delivery to core modules (#11546) This commit updates the `Notifier` to directly signal events to `BackboneRouter::Leader`, `Dhcp6::Server`, `Dhcp6::Client`, and `NeighborDiscovery::Agent`. These classes were previously notified indirectly through `Mle::HandleNotifierEvent()`. --- src/core/backbone_router/bbr_leader.cpp | 9 ++++++--- src/core/backbone_router/bbr_leader.hpp | 9 ++++----- src/core/common/notifier.cpp | 12 ++++++++++++ src/core/net/dhcp6_client.cpp | 8 ++++++++ src/core/net/dhcp6_client.hpp | 11 ++++++----- src/core/net/dhcp6_server.cpp | 12 +++++++++--- src/core/net/dhcp6_server.hpp | 11 ++++++----- src/core/net/nd_agent.cpp | 8 ++++++++ src/core/net/nd_agent.hpp | 11 ++++++----- src/core/thread/mle.cpp | 15 --------------- 10 files changed, 65 insertions(+), 41 deletions(-) diff --git a/src/core/backbone_router/bbr_leader.cpp b/src/core/backbone_router/bbr_leader.cpp index d455e6f2c..a153fa360 100644 --- a/src/core/backbone_router/bbr_leader.cpp +++ b/src/core/backbone_router/bbr_leader.cpp @@ -143,10 +143,13 @@ const char *Leader::DomainPrefixEventToString(DomainPrefixEvent aEvent) #endif // OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) -void Leader::Update(void) +void Leader::HandleNotifierEvents(Events aEvents) { - UpdateBackboneRouterPrimary(); - UpdateDomainPrefixConfig(); + if (aEvents.Contains(kEventThreadNetdataChanged)) + { + UpdateBackboneRouterPrimary(); + UpdateDomainPrefixConfig(); + } } void Leader::UpdateBackboneRouterPrimary(void) diff --git a/src/core/backbone_router/bbr_leader.hpp b/src/core/backbone_router/bbr_leader.hpp index f8a1ddbc1..6503f40b6 100644 --- a/src/core/backbone_router/bbr_leader.hpp +++ b/src/core/backbone_router/bbr_leader.hpp @@ -47,6 +47,7 @@ #include "common/locator.hpp" #include "common/log.hpp" #include "common/non_copyable.hpp" +#include "common/notifier.hpp" #include "net/ip6_address.hpp" namespace ot { @@ -83,6 +84,8 @@ enum DomainPrefixEvent : uint8_t */ class Leader : public InstanceLocator, private NonCopyable { + friend class ot::Notifier; + public: // Primary Backbone Router Service state or state change. enum State : uint8_t @@ -108,11 +111,6 @@ public: */ void Reset(void); - /** - * Updates the cached Primary Backbone Router if any when new network data is available. - */ - void Update(void); - /** * Gets the Primary Backbone Router in the Thread Network. * @@ -177,6 +175,7 @@ public: bool IsDomainUnicast(const Ip6::Address &aAddress) const; private: + void HandleNotifierEvents(Events aEvents); void UpdateBackboneRouterPrimary(void); void UpdateDomainPrefixConfig(void); #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) diff --git a/src/core/common/notifier.cpp b/src/core/common/notifier.cpp index c6037fc2d..d8794485d 100644 --- a/src/core/common/notifier.cpp +++ b/src/core/common/notifier.cpp @@ -98,6 +98,18 @@ void Notifier::EmitEvents(void) // Emit events to core internal modules Get().HandleNotifierEvents(events); +#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) + Get().HandleNotifierEvents(events); +#endif +#if OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE + Get().HandleNotifierEvents(events); +#endif +#if OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE + Get().HandleNotifierEvents(events); +#endif +#if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE + Get().HandleNotifierEvents(events); +#endif Get().HandleNotifierEvents(events); #if OPENTHREAD_FTD Get().HandleNotifierEvents(events); diff --git a/src/core/net/dhcp6_client.cpp b/src/core/net/dhcp6_client.cpp index d024a21cb..0be16e6b1 100644 --- a/src/core/net/dhcp6_client.cpp +++ b/src/core/net/dhcp6_client.cpp @@ -52,6 +52,14 @@ Client::Client(Instance &aInstance) ClearAllBytes(mIdentityAssociations); } +void Client::HandleNotifierEvents(Events aEvents) +{ + if (aEvents.Contains(kEventThreadNetdataChanged)) + { + UpdateAddresses(); + } +} + void Client::UpdateAddresses(void) { bool found = false; diff --git a/src/core/net/dhcp6_client.hpp b/src/core/net/dhcp6_client.hpp index 2000f17fd..0480184bb 100644 --- a/src/core/net/dhcp6_client.hpp +++ b/src/core/net/dhcp6_client.hpp @@ -41,6 +41,7 @@ #include "common/locator.hpp" #include "common/message.hpp" #include "common/non_copyable.hpp" +#include "common/notifier.hpp" #include "common/timer.hpp" #include "common/trickle_timer.hpp" #include "mac/mac.hpp" @@ -67,6 +68,8 @@ namespace Dhcp6 { */ class Client : public InstanceLocator, private NonCopyable { + friend class ot::Notifier; + public: /** * Initializes the object. @@ -75,11 +78,6 @@ public: */ explicit Client(Instance &aInstance); - /** - * Update addresses that shall be automatically created using DHCP. - */ - void UpdateAddresses(void); - private: static constexpr uint16_t kNumPrefixes = OPENTHREAD_CONFIG_DHCP6_CLIENT_NUM_PREFIXES; static constexpr uint32_t kTrickleTimerImin = 1; @@ -126,6 +124,9 @@ private: Error ProcessStatusCodeOption(Message &aMessage, uint16_t aOffset); Error ProcessIaAddressOption(Message &aMessage, uint16_t aOffset); + void HandleNotifierEvents(Events aEvents); + void UpdateAddresses(void); + static void HandleTrickleTimer(TrickleTimer &aTrickleTimer); void HandleTrickleTimer(void); diff --git a/src/core/net/dhcp6_server.cpp b/src/core/net/dhcp6_server.cpp index 5a37e21cd..db3b353d0 100644 --- a/src/core/net/dhcp6_server.cpp +++ b/src/core/net/dhcp6_server.cpp @@ -51,7 +51,15 @@ Server::Server(Instance &aInstance) ClearAllBytes(mPrefixAgents); } -Error Server::UpdateService(void) +void Server::HandleNotifierEvents(Events aEvents) +{ + if (aEvents.Contains(kEventThreadNetdataChanged)) + { + UpdateService(); + } +} + +void Server::UpdateService(void) { Error error = kErrorNone; uint16_t rloc16 = Get().GetRloc16(); @@ -122,8 +130,6 @@ Error Server::UpdateService(void) { Stop(); } - - return error; } void Server::Start(void) diff --git a/src/core/net/dhcp6_server.hpp b/src/core/net/dhcp6_server.hpp index 8ee423f48..42547ed60 100644 --- a/src/core/net/dhcp6_server.hpp +++ b/src/core/net/dhcp6_server.hpp @@ -40,6 +40,7 @@ #include "common/locator.hpp" #include "common/non_copyable.hpp" +#include "common/notifier.hpp" #include "mac/mac.hpp" #include "mac/mac_types.hpp" #include "net/dhcp6.hpp" @@ -64,6 +65,8 @@ namespace Dhcp6 { class Server : public InstanceLocator, private NonCopyable { + friend class ot::Notifier; + public: /** * Initializes the object. @@ -72,11 +75,6 @@ public: */ explicit Server(Instance &aInstance); - /** - * Updates DHCP Agents and DHCP ALOCs. - */ - Error UpdateService(void); - private: class PrefixAgent { @@ -165,6 +163,9 @@ private: static constexpr uint16_t kNumPrefixes = OPENTHREAD_CONFIG_DHCP6_SERVER_NUM_PREFIXES; + void HandleNotifierEvents(Events aEvents); + void UpdateService(void); + void Start(void); void Stop(void); diff --git a/src/core/net/nd_agent.cpp b/src/core/net/nd_agent.cpp index d6b26042f..5d8a38b84 100644 --- a/src/core/net/nd_agent.cpp +++ b/src/core/net/nd_agent.cpp @@ -40,6 +40,14 @@ namespace ot { namespace NeighborDiscovery { +void Agent::HandleNotifierEvents(Events aEvents) +{ + if (aEvents.Contains(kEventThreadNetdataChanged)) + { + UpdateService(); + } +} + void Agent::UpdateService(void) { Error error; diff --git a/src/core/net/nd_agent.hpp b/src/core/net/nd_agent.hpp index 7fd1880fc..e3e0e7e7f 100644 --- a/src/core/net/nd_agent.hpp +++ b/src/core/net/nd_agent.hpp @@ -40,6 +40,7 @@ #include "common/locator.hpp" #include "common/non_copyable.hpp" +#include "common/notifier.hpp" #include "net/netif.hpp" namespace ot { @@ -47,6 +48,8 @@ namespace NeighborDiscovery { class Agent : public InstanceLocator, private NonCopyable { + friend class ot::Notifier; + public: /** * Initializes the object. @@ -59,15 +62,13 @@ public: FreeAloc(); } - /** - * Updates the Neighbor Discovery Agents using current Thread Network Data. - */ - void UpdateService(void); - private: void FreeAloc(void) { mAloc.mNext = &mAloc; } bool IsAlocInUse(void) const { return mAloc.mNext != &mAloc; } + void HandleNotifierEvents(Events aEvents); + void UpdateService(void); + Ip6::Netif::UnicastAddress mAloc; }; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index f6d14d852..c98343c6a 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1222,24 +1222,9 @@ void Mle::HandleNotifierEvents(Events aEvents) } } -#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) - Get().Update(); -#endif #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE UpdateServiceAlocs(); #endif - -#if OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE - IgnoreError(Get().UpdateService()); -#endif - -#if OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE - Get().UpdateService(); -#endif - -#if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE - Get().UpdateAddresses(); -#endif } if (aEvents.ContainsAny(kEventThreadRoleChanged | kEventThreadKeySeqCounterChanged))