mirror of
https://github.com/espressif/openthread.git
synced 2026-08-07 03:07:47 +00:00
[notifier] auto-register callback from its constructor (#3555)
This commit helps simplify the implementation and use of `Notifier` by having the `Callback` constructor registering the callback with the `Notifier`.
This commit is contained in:
committed by
Jonathan Hui
parent
ede320712e
commit
d2e9bdfca3
@@ -36,16 +36,19 @@
|
||||
#include "notifier.hpp"
|
||||
|
||||
#include "common/code_utils.hpp"
|
||||
#include "common/debug.hpp"
|
||||
#include "common/logging.hpp"
|
||||
#include "common/owner-locator.hpp"
|
||||
|
||||
namespace ot {
|
||||
|
||||
Notifier::Callback::Callback(Handler aHandler, void *aOwner)
|
||||
Notifier::Callback::Callback(Instance &aInstance, Handler aHandler, void *aOwner)
|
||||
: OwnerLocator(aOwner)
|
||||
, mHandler(aHandler)
|
||||
, mNext(this)
|
||||
, mNext(NULL)
|
||||
{
|
||||
assert(aHandler != NULL);
|
||||
aInstance.GetNotifier().RegisterCallback(*this);
|
||||
}
|
||||
|
||||
Notifier::Notifier(Instance &aInstance)
|
||||
@@ -62,40 +65,10 @@ Notifier::Notifier(Instance &aInstance)
|
||||
}
|
||||
}
|
||||
|
||||
otError Notifier::RegisterCallback(Callback &aCallback)
|
||||
void Notifier::RegisterCallback(Callback &aCallback)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
VerifyOrExit(aCallback.mNext == &aCallback, error = OT_ERROR_ALREADY);
|
||||
|
||||
aCallback.mNext = mCallbacks;
|
||||
mCallbacks = &aCallback;
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
void Notifier::RemoveCallback(Callback &aCallback)
|
||||
{
|
||||
VerifyOrExit(mCallbacks != NULL);
|
||||
|
||||
if (mCallbacks == &aCallback)
|
||||
{
|
||||
mCallbacks = mCallbacks->mNext;
|
||||
ExitNow();
|
||||
}
|
||||
|
||||
for (Callback *callback = mCallbacks; callback->mNext != NULL; callback = callback->mNext)
|
||||
{
|
||||
if (callback->mNext == &aCallback)
|
||||
{
|
||||
callback->mNext = aCallback.mNext;
|
||||
ExitNow();
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
aCallback.mNext = &aCallback;
|
||||
}
|
||||
|
||||
otError Notifier::RegisterCallback(otStateChangedCallback aCallback, void *aContext)
|
||||
@@ -182,10 +155,7 @@ void Notifier::HandleStateChanged(void)
|
||||
|
||||
for (Callback *callback = mCallbacks; callback != NULL; callback = callback->mNext)
|
||||
{
|
||||
if (callback->mHandler != NULL)
|
||||
{
|
||||
callback->mHandler(*callback, flags);
|
||||
}
|
||||
callback->Invoke(flags);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < kMaxExternalHandlers; i++)
|
||||
|
||||
@@ -87,13 +87,16 @@ public:
|
||||
/**
|
||||
* This constructor initializes a `Callback` instance
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
*/
|
||||
Callback(Handler aHandler, void *aOwner);
|
||||
Callback(Instance &aInstance, Handler aHandler, void *aOwner);
|
||||
|
||||
private:
|
||||
void Invoke(otChangedFlags aFlags) { mHandler(*this, aFlags); }
|
||||
|
||||
Handler mHandler;
|
||||
Callback *mNext;
|
||||
};
|
||||
@@ -106,25 +109,6 @@ public:
|
||||
*/
|
||||
explicit Notifier(Instance &aInstance);
|
||||
|
||||
/**
|
||||
* This method registers a callback.
|
||||
*
|
||||
* @param[in] aCallback A reference to the callback instance.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully registered the callback.
|
||||
* @retval OT_ERROR_ALREADY The callback was already registered.
|
||||
*
|
||||
*/
|
||||
otError RegisterCallback(Callback &aCallback);
|
||||
|
||||
/**
|
||||
* This method removes a previously registered callback.
|
||||
*
|
||||
* @param[in] aCallback A reference to the callback instance.
|
||||
*
|
||||
*/
|
||||
void RemoveCallback(Callback &aCallback);
|
||||
|
||||
/**
|
||||
* This method registers an `otStateChangedCallback` handler.
|
||||
*
|
||||
@@ -198,6 +182,7 @@ private:
|
||||
void * mContext;
|
||||
};
|
||||
|
||||
void RegisterCallback(Callback &aCallback);
|
||||
static void HandleStateChanged(Tasklet &aTasklet);
|
||||
void HandleStateChanged(void);
|
||||
|
||||
|
||||
@@ -61,13 +61,12 @@ JoinerRouter::JoinerRouter(Instance &aInstance)
|
||||
, mSocket(aInstance.GetThreadNetif().GetIp6().GetUdp())
|
||||
, mRelayTransmit(OT_URI_PATH_RELAY_TX, &JoinerRouter::HandleRelayTransmit, this)
|
||||
, mTimer(aInstance, &JoinerRouter::HandleTimer, this)
|
||||
, mNotifierCallback(&JoinerRouter::HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, &JoinerRouter::HandleStateChanged, this)
|
||||
, mJoinerUdpPort(0)
|
||||
, mIsJoinerPortConfigured(false)
|
||||
, mExpectJoinEntRsp(false)
|
||||
{
|
||||
GetNetif().GetCoap().AddResource(mRelayTransmit);
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
}
|
||||
|
||||
void JoinerRouter::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags)
|
||||
|
||||
@@ -112,9 +112,8 @@ exit:
|
||||
|
||||
AnnounceSender::AnnounceSender(Instance &aInstance)
|
||||
: AnnounceSenderBase(aInstance, &AnnounceSender::HandleTimer)
|
||||
, mNotifierCallback(HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, HandleStateChanged, this)
|
||||
{
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
}
|
||||
|
||||
void AnnounceSender::HandleTimer(Timer &aTimer)
|
||||
|
||||
@@ -60,10 +60,9 @@ EnergyScanServer::EnergyScanServer(Instance &aInstance)
|
||||
, mActive(false)
|
||||
, mScanResultsLength(0)
|
||||
, mTimer(aInstance, &EnergyScanServer::HandleTimer, this)
|
||||
, mNotifierCallback(&EnergyScanServer::HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, &EnergyScanServer::HandleStateChanged, this)
|
||||
, mEnergyScan(OT_URI_PATH_ENERGY_SCAN, &EnergyScanServer::HandleRequest, this)
|
||||
{
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
GetNetif().GetCoap().AddResource(mEnergyScan);
|
||||
}
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ Mle::Mle(Instance &aInstance)
|
||||
, mAlternateChannel(0)
|
||||
, mAlternatePanId(Mac::kPanIdBroadcast)
|
||||
, mAlternateTimestamp(0)
|
||||
, mNotifierCallback(&Mle::HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, &Mle::HandleStateChanged, this)
|
||||
, mParentResponseCb(NULL)
|
||||
, mParentResponseCbContext(NULL)
|
||||
{
|
||||
@@ -203,8 +203,6 @@ Mle::Mle(Instance &aInstance)
|
||||
// `SetMeshLocalPrefix()` also adds the Mesh-Local EID and subscribes
|
||||
// to the Link- and Realm-Local All Thread Nodes multicast addresses.
|
||||
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
|
||||
#if OPENTHREAD_CONFIG_ENABLE_PERIODIC_PARENT_SEARCH
|
||||
StartParentSearchTimer();
|
||||
#endif
|
||||
|
||||
@@ -62,12 +62,10 @@ TimeSync::TimeSync(Instance &aInstance)
|
||||
, mNetworkTimeOffset(0)
|
||||
, mTimeSyncCallback(NULL)
|
||||
, mTimeSyncCallbackContext(NULL)
|
||||
, mNotifierCallback(&TimeSync::HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, &TimeSync::HandleStateChanged, this)
|
||||
, mTimer(aInstance, HandleTimeout, this)
|
||||
, mCurrentStatus(OT_NETWORK_TIME_UNSYNCHRONIZED)
|
||||
{
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
|
||||
CheckAndHandleChanges(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ ChannelManager::ChannelManager(Instance &aInstance)
|
||||
, mSupportedChannelMask(0)
|
||||
, mFavoredChannelMask(0)
|
||||
, mActiveTimestamp(0)
|
||||
, mNotifierCallback(&ChannelManager::HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, &ChannelManager::HandleStateChanged, this)
|
||||
, mDelay(kMinimumDelay)
|
||||
, mChannel(0)
|
||||
, mState(kStateIdle)
|
||||
@@ -60,7 +60,6 @@ ChannelManager::ChannelManager(Instance &aInstance)
|
||||
, mAutoSelectInterval(kDefaultAutoSelectInterval)
|
||||
, mAutoSelectEnabled(false)
|
||||
{
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
}
|
||||
|
||||
void ChannelManager::RequestChannelChange(uint8_t aChannel)
|
||||
|
||||
@@ -51,9 +51,8 @@ ChildSupervisor::ChildSupervisor(Instance &aInstance)
|
||||
: InstanceLocator(aInstance)
|
||||
, mSupervisionInterval(kDefaultSupervisionInterval)
|
||||
, mTimer(aInstance, &ChildSupervisor::HandleTimer, this)
|
||||
, mNotifierCallback(&ChildSupervisor::HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, &ChildSupervisor::HandleStateChanged, this)
|
||||
{
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
}
|
||||
|
||||
void ChildSupervisor::SetSupervisionInterval(uint16_t aInterval)
|
||||
|
||||
@@ -49,7 +49,7 @@ JamDetector::JamDetector(Instance &aInstance)
|
||||
: InstanceLocator(aInstance)
|
||||
, mHandler(NULL)
|
||||
, mContext(NULL)
|
||||
, mNotifierCallback(HandleStateChanged, this)
|
||||
, mNotifierCallback(aInstance, HandleStateChanged, this)
|
||||
, mTimer(aInstance, &JamDetector::HandleTimer, this)
|
||||
, mHistoryBitmap(0)
|
||||
, mCurSecondStartTime(0)
|
||||
@@ -61,7 +61,6 @@ JamDetector::JamDetector(Instance &aInstance)
|
||||
, mJamState(false)
|
||||
, mRssiThreshold(kDefaultRssiThreshold)
|
||||
{
|
||||
aInstance.GetNotifier().RegisterCallback(mNotifierCallback);
|
||||
}
|
||||
|
||||
otError JamDetector::Start(Handler aHandler, void *aContext)
|
||||
|
||||
Reference in New Issue
Block a user