diff --git a/src/core/common/notifier.cpp b/src/core/common/notifier.cpp index 837ebe9ad..d1fd8c9b9 100644 --- a/src/core/common/notifier.cpp +++ b/src/core/common/notifier.cpp @@ -40,13 +40,11 @@ namespace ot { -Notifier::Callback::Callback(Instance &aInstance, Handler aHandler, void *aOwner) - : OwnerLocator(aOwner) - , mHandler(aHandler) +Notifier::Receiver::Receiver(Instance &aInstance, Handler aHandler) + : mHandler(aHandler) , mNext(NULL) { - OT_ASSERT(aHandler != NULL); - aInstance.Get().RegisterCallback(*this); + aInstance.Get().RegisterReceiver(*this); } Notifier::Notifier(Instance &aInstance) @@ -54,7 +52,7 @@ Notifier::Notifier(Instance &aInstance) , mEventsToSignal() , mSignaledEvents() , mTask(aInstance, &Notifier::EmitEvents, this) - , mCallbacks() + , mReceivers() { for (unsigned int i = 0; i < kMaxExternalHandlers; i++) { @@ -63,9 +61,9 @@ Notifier::Notifier(Instance &aInstance) } } -void Notifier::RegisterCallback(Callback &aCallback) +void Notifier::RegisterReceiver(Receiver &aReceiver) { - mCallbacks.Push(aCallback); + mReceivers.Push(aReceiver); } otError Notifier::RegisterCallback(otStateChangedCallback aCallback, void *aContext) @@ -154,9 +152,9 @@ void Notifier::EmitEvents(void) LogEvents(events); - for (Callback *callback = mCallbacks.GetHead(); callback != NULL; callback = callback->GetNext()) + for (Receiver *receiver = mReceivers.GetHead(); receiver != NULL; receiver = receiver->GetNext()) { - callback->Invoke(events); + receiver->Emit(events); } for (unsigned int i = 0; i < kMaxExternalHandlers; i++) diff --git a/src/core/common/notifier.hpp b/src/core/common/notifier.hpp index c89611864..b3c6bcf70 100644 --- a/src/core/common/notifier.hpp +++ b/src/core/common/notifier.hpp @@ -188,8 +188,8 @@ private: * * Two callback models are provided: * - * - A `Notifier::Callback` object that upon initialization (from its constructor) auto-registers itself with - * the `Notifier`. This model is mainly used by OpenThread core modules. + * - A `Notifier::Receiver` class which should be inherited by OpenThread core types to register themselves as a + * receiver of `Notifier` events. * * - A `otStateChangedCallback` callback handler which needs to be explicitly registered with the `Notifier`. This is * commonly used by external users (provided as an OpenThread public API). Max number of such callbacks that can be @@ -200,39 +200,38 @@ class Notifier : public InstanceLocator, private NonCopyable { public: /** - * This class defines a `Notifier` callback instance. + * This class defines a `Notifier::Receiver` instance. * */ - class Callback : public OwnerLocator, public LinkedListEntry + class Receiver : public LinkedListEntry { friend class Notifier; - friend class LinkedListEntry; + friend class LinkedListEntry; public: /** - * This type defines the function pointer which is called to notify of events (state/configuration changes).. + * This type defines the function reference which is invoked to notify of events (state/configuration changes).. * - * @param[in] aCallback A reference to callback instance. + * @param[in] aReceiver A reference to `Receiver` instance. * @param[in] aEvents The list of events. * */ - typedef void (*Handler)(Callback &aCallback, Events aEvents); + typedef void (&Handler)(Receiver &aReceiver, Events aEvents); /** - * This constructor initializes a `Callback` instance and registers it with `Notifier`. + * This constructor initializes a `Receiver` instance and registers it with `Notifier`. * * @param[in] aInstance A reference to OpenThread instance. - * @param[in] aHandler A function pointer to the callback handler. - * @param[in] aOwner A pointer to the owner of the `Callback` instance. + * @param[in] aHandler The handler function reference. * */ - Callback(Instance &aInstance, Handler aHandler, void *aOwner); + Receiver(Instance &aInstance, Handler aHandler); private: - void Invoke(const Events aEvents) { mHandler(*this, aEvents); } + void Emit(Events aEvents) { mHandler(*this, aEvents); } Handler mHandler; - Callback *mNext; + Receiver *mNext; }; /** @@ -349,7 +348,7 @@ private: void * mContext; }; - void RegisterCallback(Callback &aCallback); + void RegisterReceiver(Receiver &aReceiver); static void EmitEvents(Tasklet &aTasklet); void EmitEvents(void); @@ -359,7 +358,7 @@ private: Events mEventsToSignal; Events mSignaledEvents; Tasklet mTask; - LinkedList mCallbacks; + LinkedList mReceivers; ExternalCallback mExternalCallbacks[kMaxExternalHandlers]; }; diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 21d6248d1..cdeb0d42b 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -333,6 +333,7 @@ void BorderAgent::HandleRequest<&BorderAgent::mProxyTransmit>(void * BorderAgent::BorderAgent(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, BorderAgent::HandleNotifierEvents) , mCommissionerPetition(OT_URI_PATH_COMMISSIONER_PETITION, BorderAgent::HandleRequest<&BorderAgent::mCommissionerPetition>, this) @@ -351,7 +352,6 @@ BorderAgent::BorderAgent(Instance &aInstance) , mUdpReceiver(BorderAgent::HandleUdpReceive, this) , mTimer(aInstance, HandleTimeout, this) , mState(OT_BORDER_AGENT_STATE_STOPPED) - , mNotifierCallback(aInstance, &BorderAgent::HandleNotifierEvents, this) { mCommissionerAloc.Clear(); mCommissionerAloc.mPrefixLength = 64; @@ -361,9 +361,9 @@ BorderAgent::BorderAgent(Instance &aInstance) mCommissionerAloc.mScopeOverrideValid = true; } -void BorderAgent::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void BorderAgent::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void BorderAgent::HandleNotifierEvents(Events aEvents) diff --git a/src/core/meshcop/border_agent.hpp b/src/core/meshcop/border_agent.hpp index 2ef3e1895..6937c6b95 100644 --- a/src/core/meshcop/border_agent.hpp +++ b/src/core/meshcop/border_agent.hpp @@ -49,7 +49,7 @@ class ThreadNetif; namespace MeshCoP { -class BorderAgent : public InstanceLocator +class BorderAgent : public InstanceLocator, public Notifier::Receiver { public: /** @@ -93,7 +93,7 @@ public: void ApplyMeshLocalPrefix(void); private: - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); static void HandleConnected(bool aConnected, void *aContext) @@ -162,8 +162,6 @@ private: TimerMilli mTimer; otBorderAgentState mState; - - Notifier::Callback mNotifierCallback; }; } // namespace MeshCoP diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 75fbb23ad..da9d49292 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -55,10 +55,10 @@ namespace MeshCoP { JoinerRouter::JoinerRouter(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, JoinerRouter::HandleNotifierEvents) , mSocket(aInstance.Get()) , mRelayTransmit(OT_URI_PATH_RELAY_TX, &JoinerRouter::HandleRelayTransmit, this) , mTimer(aInstance, JoinerRouter::HandleTimer, this) - , mNotifierCallback(aInstance, &JoinerRouter::HandleNotifierEvents, this) , mJoinerUdpPort(0) , mIsJoinerPortConfigured(false) , mExpectJoinEntRsp(false) @@ -66,9 +66,9 @@ JoinerRouter::JoinerRouter(Instance &aInstance) Get().AddResource(mRelayTransmit); } -void JoinerRouter::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void JoinerRouter::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void JoinerRouter::HandleNotifierEvents(Events aEvents) diff --git a/src/core/meshcop/joiner_router.hpp b/src/core/meshcop/joiner_router.hpp index 96fc71950..0197fb187 100644 --- a/src/core/meshcop/joiner_router.hpp +++ b/src/core/meshcop/joiner_router.hpp @@ -51,7 +51,7 @@ namespace ot { namespace MeshCoP { -class JoinerRouter : public InstanceLocator +class JoinerRouter : public InstanceLocator, public Notifier::Receiver { public: /** @@ -94,7 +94,7 @@ private: Kek mKek; // KEK used by MAC layer to encode this message. }; - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); @@ -124,8 +124,6 @@ private: TimerMilli mTimer; MessageQueue mDelayedJoinEnts; - Notifier::Callback mNotifierCallback; - uint16_t mJoinerUdpPort; bool mIsJoinerPortConfigured : 1; diff --git a/src/core/thread/announce_sender.cpp b/src/core/thread/announce_sender.cpp index 3a1ace9f2..d31635793 100644 --- a/src/core/thread/announce_sender.cpp +++ b/src/core/thread/announce_sender.cpp @@ -112,7 +112,7 @@ exit: AnnounceSender::AnnounceSender(Instance &aInstance) : AnnounceSenderBase(aInstance, AnnounceSender::HandleTimer) - , mNotifierCallback(aInstance, HandleNotifierEvents, this) + , Notifier::Receiver(aInstance, AnnounceSender::HandleNotifierEvents) { } @@ -175,9 +175,9 @@ void AnnounceSender::Stop(void) otLogInfoMle("Stopping periodic MLE Announcements tx"); } -void AnnounceSender::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void AnnounceSender::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void AnnounceSender::HandleNotifierEvents(Events aEvents) diff --git a/src/core/thread/announce_sender.hpp b/src/core/thread/announce_sender.hpp index 4c2683515..bf20a25dd 100644 --- a/src/core/thread/announce_sender.hpp +++ b/src/core/thread/announce_sender.hpp @@ -134,7 +134,7 @@ private: * This class implements an AnnounceSender. * */ -class AnnounceSender : public AnnounceSenderBase +class AnnounceSender : public AnnounceSenderBase, public Notifier::Receiver { public: /** @@ -157,10 +157,8 @@ private: void CheckState(void); void Stop(void); static void HandleTimer(Timer &aTimer); - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); - - Notifier::Callback mNotifierCallback; }; #endif // OPENTHREAD_CONFIG_ANNOUNCE_SENDER_ENABLE diff --git a/src/core/thread/energy_scan_server.cpp b/src/core/thread/energy_scan_server.cpp index b8c6e1c78..19d21dee4 100644 --- a/src/core/thread/energy_scan_server.cpp +++ b/src/core/thread/energy_scan_server.cpp @@ -48,6 +48,7 @@ namespace ot { EnergyScanServer::EnergyScanServer(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, EnergyScanServer::HandleNotifierEvents) , mChannelMask(0) , mChannelMaskCurrent(0) , mPeriod(0) @@ -56,7 +57,6 @@ EnergyScanServer::EnergyScanServer(Instance &aInstance) , mActive(false) , mScanResultsLength(0) , mTimer(aInstance, EnergyScanServer::HandleTimer, this) - , mNotifierCallback(aInstance, &EnergyScanServer::HandleNotifierEvents, this) , mEnergyScan(OT_URI_PATH_ENERGY_SCAN, &EnergyScanServer::HandleRequest, this) { Get().AddResource(mEnergyScan); @@ -212,9 +212,9 @@ exit: mActive = false; } -void EnergyScanServer::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void EnergyScanServer::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void EnergyScanServer::HandleNotifierEvents(Events aEvents) diff --git a/src/core/thread/energy_scan_server.hpp b/src/core/thread/energy_scan_server.hpp index dec67751f..5f316eba1 100644 --- a/src/core/thread/energy_scan_server.hpp +++ b/src/core/thread/energy_scan_server.hpp @@ -50,7 +50,7 @@ namespace ot { * This class implements handling Energy Scan Requests. * */ -class EnergyScanServer : public InstanceLocator +class EnergyScanServer : public InstanceLocator, public Notifier::Receiver { public: /** @@ -75,7 +75,7 @@ private: static void HandleTimer(Timer &aTimer); void HandleTimer(void); - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); void SendReport(void); @@ -93,8 +93,6 @@ private: TimerMilli mTimer; - Notifier::Callback mNotifierCallback; - Coap::Resource mEnergyScan; }; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 27c511b74..0f4bb70a3 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -62,6 +62,7 @@ namespace Mle { Mle::Mle(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, Mle::HandleNotifierEvents) , mRetrieveNewNetworkData(false) , mRole(kRoleDisabled) , mDeviceMode(DeviceMode::kModeRxOnWhenIdle | DeviceMode::kModeSecureDataRequest) @@ -108,7 +109,6 @@ Mle::Mle(Instance &aInstance) , mAlternateChannel(0) , mAlternatePanId(Mac::kPanIdBroadcast) , mAlternateTimestamp(0) - , mNotifierCallback(aInstance, &Mle::HandleNotifierEvents, this) , mParentResponseCb(NULL) , mParentResponseCbContext(NULL) { @@ -1486,9 +1486,9 @@ exit: return error; } -void Mle::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void Mle::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void Mle::HandleNotifierEvents(Events aEvents) diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index e14c333a2..8183780a4 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -305,7 +305,7 @@ private: * This class implements MLE functionality required by the Thread EndDevices, Router, and Leader roles. * */ -class Mle : public InstanceLocator +class Mle : public InstanceLocator, public Notifier::Receiver { public: /** @@ -1696,7 +1696,7 @@ private: TimeMilli mSendTime; // Time when the message shall be sent. }; - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); static void HandleAttachTimer(Timer &aTimer); void HandleAttachTimer(void); @@ -1823,8 +1823,6 @@ private: Ip6::NetifMulticastAddress mLinkLocalAllThreadNodes; Ip6::NetifMulticastAddress mRealmLocalAllThreadNodes; - Notifier::Callback mNotifierCallback; - otThreadParentResponseCallback mParentResponseCb; void * mParentResponseCbContext; }; diff --git a/src/core/thread/network_data_notifier.cpp b/src/core/thread/network_data_notifier.cpp index 64339d6dd..261789c5c 100644 --- a/src/core/thread/network_data_notifier.cpp +++ b/src/core/thread/network_data_notifier.cpp @@ -46,7 +46,7 @@ namespace NetworkData { Notifier::Notifier(Instance &aInstance) : InstanceLocator(aInstance) - , mNotifierCallback(aInstance, &Notifier::HandleNotifierEvents, this) + , ot::Notifier::Receiver(aInstance, Notifier::HandleNotifierEvents) , mTimer(aInstance, Notifier::HandleTimer, this) , mNextDelay(0) , mWaitingForResponse(false) @@ -101,9 +101,9 @@ exit: } } -void Notifier::HandleNotifierEvents(ot::Notifier::Callback &aCallback, Events aEvents) +void Notifier::HandleNotifierEvents(ot::Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void Notifier::HandleNotifierEvents(Events aEvents) diff --git a/src/core/thread/network_data_notifier.hpp b/src/core/thread/network_data_notifier.hpp index 4a907e174..0d64959dc 100644 --- a/src/core/thread/network_data_notifier.hpp +++ b/src/core/thread/network_data_notifier.hpp @@ -49,7 +49,7 @@ namespace NetworkData { * This class implements the SVR_DATA.ntf transmission logic. * */ -class Notifier : public InstanceLocator +class Notifier : public InstanceLocator, public ot::Notifier::Receiver { public: /** @@ -74,7 +74,7 @@ private: kDelaySynchronizeServerData = 300000, ///< milliseconds }; - static void HandleNotifierEvents(ot::Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(ot::Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); static void HandleTimer(Timer &aTimer); @@ -88,10 +88,9 @@ private: void SynchronizeServerData(void); - ot::Notifier::Callback mNotifierCallback; - TimerMilli mTimer; - uint32_t mNextDelay; - bool mWaitingForResponse; + TimerMilli mTimer; + uint32_t mNextDelay; + bool mWaitingForResponse; }; } // namespace NetworkData diff --git a/src/core/thread/time_sync_service.cpp b/src/core/thread/time_sync_service.cpp index 90dfb0e6f..51c1a1434 100644 --- a/src/core/thread/time_sync_service.cpp +++ b/src/core/thread/time_sync_service.cpp @@ -51,6 +51,7 @@ namespace ot { TimeSync::TimeSync(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, TimeSync::HandleNotifierEvents) , mTimeSyncRequired(false) , mTimeSyncSeq(OT_TIME_SYNC_INVALID_SEQ) , mTimeSyncPeriod(OPENTHREAD_CONFIG_TIME_SYNC_PERIOD) @@ -62,7 +63,6 @@ TimeSync::TimeSync(Instance &aInstance) , mNetworkTimeOffset(0) , mTimeSyncCallback(NULL) , mTimeSyncCallbackContext(NULL) - , mNotifierCallback(aInstance, &TimeSync::HandleNotifierEvents, this) , mTimer(aInstance, HandleTimeout, this) , mCurrentStatus(OT_NETWORK_TIME_UNSYNCHRONIZED) { @@ -209,9 +209,9 @@ void TimeSync::HandleTimeout(void) CheckAndHandleChanges(false); } -void TimeSync::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void TimeSync::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void TimeSync::HandleTimeout(Timer &aTimer) diff --git a/src/core/thread/time_sync_service.hpp b/src/core/thread/time_sync_service.hpp index 74785656b..fad0f96fc 100644 --- a/src/core/thread/time_sync_service.hpp +++ b/src/core/thread/time_sync_service.hpp @@ -50,7 +50,7 @@ namespace ot { * This class implements OpenThread Time Synchronization Service. * */ -class TimeSync : public InstanceLocator +class TimeSync : public InstanceLocator, public Notifier::Receiver { public: /** @@ -173,7 +173,7 @@ private: * @param[in] aFlags Flags that denote the state change events. * */ - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); /** * Callback to be called when timer expires. @@ -216,7 +216,6 @@ private: otNetworkTimeSyncCallbackFn mTimeSyncCallback; ///< The callback to be called when time sync is handled or status updated. void * mTimeSyncCallbackContext; ///< The context to be passed to callback. - Notifier::Callback mNotifierCallback; ///< Callback for thread state changes. TimerMilli mTimer; ///< Timer for checking if a resync is required. otNetworkTimeStatus mCurrentStatus; ///< Current network time status. }; diff --git a/src/core/utils/channel_manager.cpp b/src/core/utils/channel_manager.cpp index dfef0bb3f..e76a96c77 100644 --- a/src/core/utils/channel_manager.cpp +++ b/src/core/utils/channel_manager.cpp @@ -48,10 +48,10 @@ namespace Utils { ChannelManager::ChannelManager(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, ChannelManager::HandleNotifierEvents) , mSupportedChannelMask(0) , mFavoredChannelMask(0) , mActiveTimestamp(0) - , mNotifierCallback(aInstance, &ChannelManager::HandleNotifierEvents, this) , mDelay(kMinimumDelay) , mChannel(0) , mState(kStateIdle) @@ -252,9 +252,9 @@ void ChannelManager::HandleTimer(void) } } -void ChannelManager::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void ChannelManager::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void ChannelManager::HandleNotifierEvents(Events aEvents) diff --git a/src/core/utils/channel_manager.hpp b/src/core/utils/channel_manager.hpp index 21416fbdc..4d65a84be 100644 --- a/src/core/utils/channel_manager.hpp +++ b/src/core/utils/channel_manager.hpp @@ -64,7 +64,7 @@ namespace Utils { * This class implements the Channel Manager. * */ -class ChannelManager : public InstanceLocator, private NonCopyable +class ChannelManager : public InstanceLocator, public Notifier::Receiver, private NonCopyable { public: enum @@ -272,7 +272,7 @@ private: static void HandleTimer(Timer &aTimer); void HandleTimer(void); - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); void PreparePendingDataset(void); void StartAutoSelectTimer(void); @@ -282,16 +282,15 @@ private: bool ShouldAttemptChannelChange(void); #endif - Mac::ChannelMask mSupportedChannelMask; - Mac::ChannelMask mFavoredChannelMask; - uint64_t mActiveTimestamp; - Notifier::Callback mNotifierCallback; - uint16_t mDelay; - uint8_t mChannel; - State mState; - TimerMilli mTimer; - uint32_t mAutoSelectInterval; - bool mAutoSelectEnabled; + Mac::ChannelMask mSupportedChannelMask; + Mac::ChannelMask mFavoredChannelMask; + uint64_t mActiveTimestamp; + uint16_t mDelay; + uint8_t mChannel; + State mState; + TimerMilli mTimer; + uint32_t mAutoSelectInterval; + bool mAutoSelectEnabled; }; #else // OPENTHREAD_FTD diff --git a/src/core/utils/child_supervision.cpp b/src/core/utils/child_supervision.cpp index f81ac9330..09e3128e7 100644 --- a/src/core/utils/child_supervision.cpp +++ b/src/core/utils/child_supervision.cpp @@ -49,9 +49,9 @@ namespace Utils { ChildSupervisor::ChildSupervisor(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, ChildSupervisor::HandleNotifierEvents) , mSupervisionInterval(kDefaultSupervisionInterval) , mTimer(aInstance, ChildSupervisor::HandleTimer, this) - , mNotifierCallback(aInstance, &ChildSupervisor::HandleNotifierEvents, this) { } @@ -162,9 +162,9 @@ void ChildSupervisor::CheckState(void) } } -void ChildSupervisor::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void ChildSupervisor::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void ChildSupervisor::HandleNotifierEvents(Events aEvents) diff --git a/src/core/utils/child_supervision.hpp b/src/core/utils/child_supervision.hpp index 180fed98e..cd8a2190a 100644 --- a/src/core/utils/child_supervision.hpp +++ b/src/core/utils/child_supervision.hpp @@ -88,7 +88,7 @@ namespace Utils { * This class implements a child supervisor. * */ -class ChildSupervisor : public InstanceLocator +class ChildSupervisor : public InstanceLocator, public Notifier::Receiver { public: /** @@ -160,12 +160,11 @@ private: void CheckState(void); static void HandleTimer(Timer &aTimer); void HandleTimer(void); - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); - uint16_t mSupervisionInterval; - TimerMilli mTimer; - Notifier::Callback mNotifierCallback; + uint16_t mSupervisionInterval; + TimerMilli mTimer; }; #else // #if OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE && OPENTHREAD_FTD diff --git a/src/core/utils/jam_detector.cpp b/src/core/utils/jam_detector.cpp index a2e645d52..6abc68820 100644 --- a/src/core/utils/jam_detector.cpp +++ b/src/core/utils/jam_detector.cpp @@ -47,9 +47,9 @@ namespace Utils { JamDetector::JamDetector(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, JamDetector::HandleNotifierEvents) , mHandler(NULL) , mContext(NULL) - , mNotifierCallback(aInstance, HandleNotifierEvents, this) , mTimer(aInstance, JamDetector::HandleTimer, this) , mHistoryBitmap(0) , mCurSecondStartTime(0) @@ -271,9 +271,9 @@ void JamDetector::SetJamState(bool aNewState) } } -void JamDetector::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void JamDetector::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void JamDetector::HandleNotifierEvents(Events aEvents) diff --git a/src/core/utils/jam_detector.hpp b/src/core/utils/jam_detector.hpp index 3713affcc..5162cea05 100644 --- a/src/core/utils/jam_detector.hpp +++ b/src/core/utils/jam_detector.hpp @@ -48,7 +48,7 @@ class ThreadNetif; namespace Utils { -class JamDetector : public InstanceLocator +class JamDetector : public InstanceLocator, public Notifier::Receiver { public: /** @@ -190,22 +190,21 @@ private: void HandleTimer(void); void UpdateHistory(bool aDidExceedThreshold); void UpdateJamState(void); - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); - Handler mHandler; // Handler/callback to inform about jamming state - void * mContext; // Context for handler/callback - Notifier::Callback mNotifierCallback; // Notifier callback - TimerMilli mTimer; // RSSI sample timer - uint64_t mHistoryBitmap; // History bitmap, each bit correspond to 1 sec interval - TimeMilli mCurSecondStartTime; // Start time for current 1 sec interval - uint16_t mSampleInterval; // Current sample interval - uint8_t mWindow : 6; // Window (in sec) to monitor jamming - uint8_t mBusyPeriod : 6; // BusyPeriod (in sec) with mWindow to alert jamming - bool mEnabled : 1; // If jam detection is enabled - bool mAlwaysAboveThreshold : 1; // State for current 1 sec interval - bool mJamState : 1; // Current jam state - int8_t mRssiThreshold; // RSSI threshold for jam detection + Handler mHandler; // Handler/callback to inform about jamming state + void * mContext; // Context for handler/callback + TimerMilli mTimer; // RSSI sample timer + uint64_t mHistoryBitmap; // History bitmap, each bit correspond to 1 sec interval + TimeMilli mCurSecondStartTime; // Start time for current 1 sec interval + uint16_t mSampleInterval; // Current sample interval + uint8_t mWindow : 6; // Window (in sec) to monitor jamming + uint8_t mBusyPeriod : 6; // BusyPeriod (in sec) with mWindow to alert jamming + bool mEnabled : 1; // If jam detection is enabled + bool mAlwaysAboveThreshold : 1; // State for current 1 sec interval + bool mJamState : 1; // Current jam state + int8_t mRssiThreshold; // RSSI threshold for jam detection }; /** diff --git a/src/core/utils/otns.cpp b/src/core/utils/otns.cpp index 567019792..db68b3d11 100644 --- a/src/core/utils/otns.cpp +++ b/src/core/utils/otns.cpp @@ -86,9 +86,9 @@ void Otns::EmitStatus(const char *aFmt, ...) otPlatOtnsStatus(statusStr); } -void Otns::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void Otns::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void Otns::HandleNotifierEvents(Events aEvents) diff --git a/src/core/utils/otns.hpp b/src/core/utils/otns.hpp index 21692c58c..65fc1a38c 100644 --- a/src/core/utils/otns.hpp +++ b/src/core/utils/otns.hpp @@ -56,7 +56,7 @@ namespace Utils { * This class implements the OTNS Stub that interacts with OTNS. * */ -class Otns : public InstanceLocator, private NonCopyable +class Otns : public InstanceLocator, public Notifier::Receiver, private NonCopyable { public: /** @@ -67,7 +67,7 @@ public: */ explicit Otns(Instance &aInstance) : InstanceLocator(aInstance) - , mNotifierCallback(aInstance, &Otns::HandleNotifierEvents, this) + , Notifier::Receiver(aInstance, Otns::HandleNotifierEvents) { } @@ -127,10 +127,8 @@ public: private: static void EmitStatus(const char *aFmt, ...); - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); - - Notifier::Callback mNotifierCallback; }; } // namespace Utils diff --git a/src/core/utils/slaac_address.cpp b/src/core/utils/slaac_address.cpp index e126f1490..097f7206c 100644 --- a/src/core/utils/slaac_address.cpp +++ b/src/core/utils/slaac_address.cpp @@ -49,9 +49,9 @@ namespace Utils { Slaac::Slaac(Instance &aInstance) : InstanceLocator(aInstance) + , Notifier::Receiver(aInstance, Slaac::HandleNotifierEvents) , mEnabled(true) , mFilter(NULL) - , mNotifierCallback(aInstance, &Slaac::HandleNotifierEvents, this) { memset(mAddresses, 0, sizeof(mAddresses)); } @@ -99,9 +99,9 @@ bool Slaac::ShouldFilter(const otIp6Prefix &aPrefix) const return (mFilter != NULL) && mFilter(&GetInstance(), &aPrefix); } -void Slaac::HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents) +void Slaac::HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents) { - aCallback.GetOwner().HandleNotifierEvents(aEvents); + static_cast(aReceiver).HandleNotifierEvents(aEvents); } void Slaac::HandleNotifierEvents(Events aEvents) diff --git a/src/core/utils/slaac_address.hpp b/src/core/utils/slaac_address.hpp index 9744e36ff..5e209a3a1 100644 --- a/src/core/utils/slaac_address.hpp +++ b/src/core/utils/slaac_address.hpp @@ -56,7 +56,7 @@ namespace Utils { * This class implements the SLAAC utility for Thread protocol. * */ -class Slaac : public InstanceLocator +class Slaac : public InstanceLocator, public Notifier::Receiver { public: enum @@ -161,12 +161,11 @@ private: bool ShouldFilter(const otIp6Prefix &aPrefix) const; void Update(UpdateMode aMode); void GetIidSecretKey(IidSecretKey &aKey) const; - static void HandleNotifierEvents(Notifier::Callback &aCallback, Events aEvents); + static void HandleNotifierEvents(Notifier::Receiver &aReceiver, Events aEvents); void HandleNotifierEvents(Events aEvents); bool mEnabled; otIp6SlaacPrefixFilter mFilter; - Notifier::Callback mNotifierCallback; Ip6::NetifUnicastAddress mAddresses[OPENTHREAD_CONFIG_IP6_SLAAC_NUM_ADDRESSES]; };