mirror of
https://github.com/espressif/openthread.git
synced 2026-08-05 10:27:49 +00:00
[notifier] update to use Callback<HandlerType> (#8611)
This commit updates `Notifier` class to use the recently added `Callback<HandlerType>` for the external callbacks.
This commit is contained in:
@@ -105,6 +105,21 @@ public:
|
||||
*/
|
||||
void *GetContext(void) const { return mContext; }
|
||||
|
||||
/**
|
||||
* This method indicates whether the callback matches a given handler function pointer and context.
|
||||
*
|
||||
* @param[in] aHandler The handler function pointer to compare with.
|
||||
* @param[in] aContext The context associated with handler.
|
||||
*
|
||||
* @retval TRUE The callback matches @p aHandler and @p aContext.
|
||||
* @retval FALSE The callback does not match @p aHandler and @p aContext.
|
||||
*
|
||||
*/
|
||||
bool Matches(HandlerType aHandler, void *aContext) const
|
||||
{
|
||||
return (mHandler == aHandler) && (mContext == aContext);
|
||||
}
|
||||
|
||||
protected:
|
||||
CallbackBase(void)
|
||||
: mHandler(nullptr)
|
||||
|
||||
@@ -50,8 +50,7 @@ Notifier::Notifier(Instance &aInstance)
|
||||
{
|
||||
for (ExternalCallback &callback : mExternalCallbacks)
|
||||
{
|
||||
callback.mHandler = nullptr;
|
||||
callback.mContext = nullptr;
|
||||
callback.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,23 +63,17 @@ Error Notifier::RegisterCallback(otStateChangedCallback aCallback, void *aContex
|
||||
|
||||
for (ExternalCallback &callback : mExternalCallbacks)
|
||||
{
|
||||
if (callback.mHandler == nullptr)
|
||||
VerifyOrExit(!callback.Matches(aCallback, aContext), error = kErrorAlready);
|
||||
|
||||
if (!callback.IsSet() && (unusedCallback == nullptr))
|
||||
{
|
||||
if (unusedCallback == nullptr)
|
||||
{
|
||||
unusedCallback = &callback;
|
||||
}
|
||||
|
||||
continue;
|
||||
unusedCallback = &callback;
|
||||
}
|
||||
|
||||
VerifyOrExit((callback.mHandler != aCallback) || (callback.mContext != aContext), error = kErrorAlready);
|
||||
}
|
||||
|
||||
VerifyOrExit(unusedCallback != nullptr, error = kErrorNoBufs);
|
||||
|
||||
unusedCallback->mHandler = aCallback;
|
||||
unusedCallback->mContext = aContext;
|
||||
unusedCallback->Set(aCallback, aContext);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -92,10 +85,9 @@ void Notifier::RemoveCallback(otStateChangedCallback aCallback, void *aContext)
|
||||
|
||||
for (ExternalCallback &callback : mExternalCallbacks)
|
||||
{
|
||||
if ((callback.mHandler == aCallback) && (callback.mContext == aContext))
|
||||
if (callback.Matches(aCallback, aContext))
|
||||
{
|
||||
callback.mHandler = nullptr;
|
||||
callback.mContext = nullptr;
|
||||
callback.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,10 +191,7 @@ void Notifier::EmitEvents(void)
|
||||
|
||||
for (ExternalCallback &callback : mExternalCallbacks)
|
||||
{
|
||||
if (callback.mHandler != nullptr)
|
||||
{
|
||||
callback.mHandler(events.GetAsFlags(), callback.mContext);
|
||||
}
|
||||
callback.InvokeIfSet(events.GetAsFlags());
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
#include <openthread/instance.h>
|
||||
#include <openthread/platform/toolchain.h>
|
||||
|
||||
#include "common/callback.hpp"
|
||||
#include "common/error.hpp"
|
||||
#include "common/locator.hpp"
|
||||
#include "common/non_copyable.hpp"
|
||||
@@ -308,11 +309,7 @@ private:
|
||||
|
||||
static constexpr uint16_t kFlagsStringBufferSize = kFlagsStringLineLimit + kMaxFlagNameLength;
|
||||
|
||||
struct ExternalCallback
|
||||
{
|
||||
otStateChangedCallback mHandler;
|
||||
void *mContext;
|
||||
};
|
||||
typedef Callback<otStateChangedCallback> ExternalCallback;
|
||||
|
||||
void EmitEvents(void);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user