mirror of
https://github.com/espressif/openthread.git
synced 2026-09-07 01:30:11 +00:00
[notifier] add Update method (#4274)
This commit adds a new `Notifier::Update()` method which performs a commonly used pattern in the code where a variable is updated with a new given value and if the variable value does change a related notifier `OT_CHANGED_<NAME>` is signaled.
This commit is contained in:
committed by
Jonathan Hui
parent
fd57343641
commit
efd9ebaa9c
@@ -176,6 +176,40 @@ public:
|
||||
*/
|
||||
bool HasSignaled(otChangedFlags aFlags) const { return (mSignaledFlags & aFlags) == aFlags; }
|
||||
|
||||
/**
|
||||
* This template method updates a variable of a type `Type` with a new value and signals the given changed flags.
|
||||
*
|
||||
* If the variable is already set to the same value, this method returns `OT_ERROR_ALREADY` and the changed flags
|
||||
* is signaled using `SignalIfFirst()` (i.e. signal is scheduled only if the flag has not been signaled before).
|
||||
*
|
||||
* The template `Type` should support comparison operator `==` and assignment operator `=`.
|
||||
*
|
||||
* @param[inout] aVariable A reference to the variable to update.
|
||||
* @param[in] aNewValue The new value.
|
||||
* @param[in] aFlags The changed flags to signal.
|
||||
*
|
||||
* @retval OT_ERROR_NONE The variable was update successfully and @p aFlags was signaled.
|
||||
* @retval OT_ERROR_ALREADY The variable was already set to the same value.
|
||||
*
|
||||
*/
|
||||
template <typename Type> otError Update(Type &aVariable, const Type &aNewValue, otChangedFlags aFlags)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
if (aVariable == aNewValue)
|
||||
{
|
||||
SignalIfFirst(aFlags);
|
||||
error = OT_ERROR_ALREADY;
|
||||
}
|
||||
else
|
||||
{
|
||||
aVariable = aNewValue;
|
||||
Signal(aFlags);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
|
||||
+4
-21
@@ -373,14 +373,11 @@ otError Mac::SetPanChannel(uint8_t aChannel)
|
||||
|
||||
VerifyOrExit(mSupportedChannelMask.ContainsChannel(aChannel), error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
VerifyOrExit(mPanChannel != aChannel, Get<Notifier>().SignalIfFirst(OT_CHANGED_THREAD_CHANNEL));
|
||||
SuccessOrExit(Get<Notifier>().Update(mPanChannel, aChannel, OT_CHANGED_THREAD_CHANNEL));
|
||||
|
||||
mPanChannel = aChannel;
|
||||
mCcaSuccessRateTracker.Reset();
|
||||
Get<Notifier>().Signal(OT_CHANGED_THREAD_CHANNEL);
|
||||
|
||||
VerifyOrExit(!mRadioChannelAcquisitionId);
|
||||
|
||||
mRadioChannel = mPanChannel;
|
||||
|
||||
UpdateIdleMode();
|
||||
@@ -441,13 +438,7 @@ void Mac::SetSupportedChannelMask(const ChannelMask &aMask)
|
||||
ChannelMask newMask = aMask;
|
||||
|
||||
newMask.Intersect(ChannelMask(Get<Radio>().GetSupportedChannelMask()));
|
||||
VerifyOrExit(newMask != mSupportedChannelMask, Get<Notifier>().SignalIfFirst(OT_CHANGED_SUPPORTED_CHANNEL_MASK));
|
||||
|
||||
mSupportedChannelMask = newMask;
|
||||
Get<Notifier>().Signal(OT_CHANGED_SUPPORTED_CHANNEL_MASK);
|
||||
|
||||
exit:
|
||||
return;
|
||||
Get<Notifier>().Update(mSupportedChannelMask, newMask, OT_CHANGED_SUPPORTED_CHANNEL_MASK);
|
||||
}
|
||||
|
||||
otError Mac::SetNetworkName(const char *aNameString)
|
||||
@@ -485,10 +476,8 @@ exit:
|
||||
|
||||
void Mac::SetPanId(PanId aPanId)
|
||||
{
|
||||
VerifyOrExit(mPanId != aPanId, Get<Notifier>().SignalIfFirst(OT_CHANGED_THREAD_PANID));
|
||||
mPanId = aPanId;
|
||||
SuccessOrExit(Get<Notifier>().Update(mPanId, aPanId, OT_CHANGED_THREAD_PANID));
|
||||
mSubMac.SetPanId(mPanId);
|
||||
Get<Notifier>().Signal(OT_CHANGED_THREAD_PANID);
|
||||
|
||||
exit:
|
||||
return;
|
||||
@@ -496,13 +485,7 @@ exit:
|
||||
|
||||
void Mac::SetExtendedPanId(const ExtendedPanId &aExtendedPanId)
|
||||
{
|
||||
VerifyOrExit(mExtendedPanId != aExtendedPanId, Get<Notifier>().SignalIfFirst(OT_CHANGED_THREAD_EXT_PANID));
|
||||
|
||||
mExtendedPanId = aExtendedPanId;
|
||||
Get<Notifier>().Signal(OT_CHANGED_THREAD_EXT_PANID);
|
||||
|
||||
exit:
|
||||
return;
|
||||
Get<Notifier>().Update(mExtendedPanId, aExtendedPanId, OT_CHANGED_THREAD_EXT_PANID);
|
||||
}
|
||||
|
||||
otError Mac::RequestDirectFrameTransmission(void)
|
||||
|
||||
@@ -102,11 +102,7 @@ void KeyManager::Stop(void)
|
||||
#if OPENTHREAD_MTD || OPENTHREAD_FTD
|
||||
void KeyManager::SetPskc(const Pskc &aPskc)
|
||||
{
|
||||
VerifyOrExit(mPskc != aPskc, Get<Notifier>().SignalIfFirst(OT_CHANGED_PSKC));
|
||||
mPskc = aPskc;
|
||||
Get<Notifier>().Signal(OT_CHANGED_PSKC);
|
||||
|
||||
exit:
|
||||
Get<Notifier>().Update(mPskc, aPskc, OT_CHANGED_PSKC);
|
||||
mIsPskcSet = true;
|
||||
}
|
||||
#endif // OPENTHREAD_MTD || OPENTHREAD_FTD
|
||||
@@ -116,9 +112,9 @@ otError KeyManager::SetMasterKey(const MasterKey &aKey)
|
||||
otError error = OT_ERROR_NONE;
|
||||
Router *parent;
|
||||
|
||||
VerifyOrExit(mMasterKey != aKey, Get<Notifier>().SignalIfFirst(OT_CHANGED_MASTER_KEY));
|
||||
SuccessOrExit(
|
||||
Get<Notifier>().Update(mMasterKey, aKey, OT_CHANGED_MASTER_KEY | OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER));
|
||||
|
||||
mMasterKey = aKey;
|
||||
mKeySequence = 0;
|
||||
ComputeKey(mKeySequence, mKey);
|
||||
|
||||
@@ -144,8 +140,6 @@ otError KeyManager::SetMasterKey(const MasterKey &aKey)
|
||||
iter.GetChild()->SetMleFrameCounter(0);
|
||||
}
|
||||
|
||||
Get<Notifier>().Signal(OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER | OT_CHANGED_MASTER_KEY);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
@@ -316,12 +316,11 @@ exit:
|
||||
|
||||
void Mle::SetRole(otDeviceRole aRole)
|
||||
{
|
||||
VerifyOrExit(aRole != mRole, Get<Notifier>().SignalIfFirst(OT_CHANGED_THREAD_ROLE));
|
||||
otDeviceRole oldRole = mRole;
|
||||
|
||||
otLogNoteMle("Role %s -> %s", RoleToString(mRole), RoleToString(aRole));
|
||||
SuccessOrExit(Get<Notifier>().Update(mRole, aRole, OT_CHANGED_THREAD_ROLE));
|
||||
|
||||
mRole = aRole;
|
||||
Get<Notifier>().Signal(OT_CHANGED_THREAD_ROLE);
|
||||
otLogNoteMle("Role %s -> %s", RoleToString(oldRole), RoleToString(mRole));
|
||||
|
||||
switch (mRole)
|
||||
{
|
||||
@@ -354,7 +353,7 @@ void Mle::SetRole(otDeviceRole aRole)
|
||||
#endif
|
||||
|
||||
exit:
|
||||
return;
|
||||
OT_UNUSED_VARIABLE(oldRole);
|
||||
}
|
||||
|
||||
void Mle::SetAttachState(AttachState aState)
|
||||
|
||||
Reference in New Issue
Block a user