diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 2798edd0e..dc3f88d35 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -250,6 +250,13 @@ enum OT_CHANGED_CHANNEL_MANAGER_NEW_CHANNEL = 1 << 23, ///< Channel Manager new pending Thread channel changed }; +/** + * This type represents a bit-field indicating specific state/configuration that has changed. See `OT_CHANGED_*` + * definitions. + * + */ +typedef uint32_t otChangedFlags; + /** * This function pointer is called to notify certain configuration or state changes within OpenThread. * @@ -257,7 +264,7 @@ enum * @param[in] aContext A pointer to application-specific context. * */ -typedef void(OTCALL *otStateChangedCallback)(uint32_t aFlags, void *aContext); +typedef void(OTCALL *otStateChangedCallback)(otChangedFlags aFlags, void *aContext); /** * This function registers a callback to indicate when certain configuration or state changes within OpenThread. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index a6cb3d3ce..f4beb8a98 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -3478,7 +3478,7 @@ exit: return; } -void OTCALL Interpreter::s_HandleNetifStateChanged(uint32_t aFlags, void *aContext) +void OTCALL Interpreter::s_HandleNetifStateChanged(otChangedFlags aFlags, void *aContext) { #ifdef OTDLL otCliContext *cliContext = static_cast(aContext); @@ -3489,9 +3489,9 @@ void OTCALL Interpreter::s_HandleNetifStateChanged(uint32_t aFlags, void *aConte } #ifdef OTDLL -void Interpreter::HandleNetifStateChanged(otInstance *mInstance, uint32_t aFlags) +void Interpreter::HandleNetifStateChanged(otInstance *mInstance, otChangedFlags aFlags) #else -void Interpreter::HandleNetifStateChanged(uint32_t aFlags) +void Interpreter::HandleNetifStateChanged(otChangedFlags aFlags) #endif { VerifyOrExit((aFlags & OT_CHANGED_THREAD_NETDATA) != 0); diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 347007deb..560c607e7 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -330,7 +330,7 @@ private: static void s_HandlePingTimer(Timer &aTimer); #endif static void OTCALL s_HandleActiveScanResult(otActiveScanResult *aResult, void *aContext); - static void OTCALL s_HandleNetifStateChanged(uint32_t aFlags, void *aContext); + static void OTCALL s_HandleNetifStateChanged(otChangedFlags aFlags, void *aContext); #ifndef OTDLL static void s_HandleLinkPcapReceive(const otRadioFrame *aFrame, void *aContext); #endif @@ -360,9 +360,9 @@ private: #endif void HandleActiveScanResult(otActiveScanResult *aResult); #ifdef OTDLL - void HandleNetifStateChanged(otInstance *aInstance, uint32_t aFlags); + void HandleNetifStateChanged(otInstance *aInstance, otChangedFlags aFlags); #else - void HandleNetifStateChanged(uint32_t aFlags); + void HandleNetifStateChanged(otChangedFlags aFlags); #endif #ifndef OTDLL void HandleLinkPcapReceive(const otRadioFrame *aFrame); diff --git a/src/core/common/notifier.cpp b/src/core/common/notifier.cpp index 29e414c95..70ac44ad9 100644 --- a/src/core/common/notifier.cpp +++ b/src/core/common/notifier.cpp @@ -50,7 +50,8 @@ Notifier::Callback::Callback(Handler aHandler, void *aOwner) Notifier::Notifier(Instance &aInstance) : InstanceLocator(aInstance) - , mFlags(0) + , mFlagsToSignal(0) + , mSignaledFlags(0) , mTask(aInstance, &Notifier::HandleStateChanged, this) , mCallbacks(NULL) { @@ -149,12 +150,21 @@ exit: return; } -void Notifier::SetFlags(uint32_t aFlags) +void Notifier::Signal(otChangedFlags aFlags) { - mFlags |= aFlags; + mFlagsToSignal |= aFlags; + mSignaledFlags |= aFlags; mTask.Post(); } +void Notifier::SignalIfFirst(otChangedFlags aFlags) +{ + if (!HasSignaled(aFlags)) + { + Signal(aFlags); + } +} + void Notifier::HandleStateChanged(Tasklet &aTasklet) { aTasklet.GetOwner().HandleStateChanged(); @@ -162,11 +172,11 @@ void Notifier::HandleStateChanged(Tasklet &aTasklet) void Notifier::HandleStateChanged(void) { - uint32_t flags = mFlags; + otChangedFlags flags = mFlagsToSignal; VerifyOrExit(flags != 0); - mFlags = 0; + mFlagsToSignal = 0; LogChangedFlags(flags); @@ -194,35 +204,29 @@ exit: #if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_INFO) && (OPENTHREAD_CONFIG_LOG_MAC == 1) -void Notifier::LogChangedFlags(uint32_t aFlags) const +void Notifier::LogChangedFlags(otChangedFlags aFlags) const { - uint32_t flags = aFlags; - char stringBuffer[kFlagsStringBufferSize]; - char * buf = stringBuffer; - int len = sizeof(stringBuffer) - 1; - int charsWritten; + otChangedFlags flags = aFlags; + bool isFirst = true; + String string; - for (uint8_t bit = 0; bit < 32; bit++) + for (uint8_t bit = 0; bit < sizeof(otChangedFlags) * CHAR_BIT; bit++) { VerifyOrExit(flags != 0); if (flags & (1 << bit)) { - charsWritten = snprintf(buf, static_cast(len), "%s ", FlagToString(1 << bit)); - VerifyOrExit(charsWritten >= 0 && charsWritten < len); - buf += charsWritten; - len -= charsWritten; - + SuccessOrExit(string.Append("%s%s", isFirst ? "" : " ", FlagToString(1 << bit))); + isFirst = false; flags ^= (1 << bit); } } exit: - stringBuffer[sizeof(stringBuffer) - 1] = 0; - otLogInfoCore(GetInstance(), "Notifier: StateChanged (0x%04x) [ %s] ", aFlags, stringBuffer); + otLogInfoCore(GetInstance(), "Notifier: StateChanged (0x%04x) [%s] ", aFlags, string.AsCString()); } -const char *Notifier::FlagToString(uint32_t aFlag) const +const char *Notifier::FlagToString(otChangedFlags aFlag) const { const char *retval = "(unknown)"; @@ -333,15 +337,13 @@ const char *Notifier::FlagToString(uint32_t aFlag) const #else // #if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_INFO) && (OPENTHREAD_CONFIG_LOG_MAC == 1) -void Notifier::LogChangedFlags(uint32_t aFlags) const +void Notifier::LogChangedFlags(otChangedFlags) const { - OT_UNUSED_VARIABLE(aFlags); } -const char *Notifier::FlagToString(uint32_t aFlag) const +const char *Notifier::FlagToString(otChangedFlags) const { - OT_UNUSED_VARIABLE(aFlag); - return NULL; + return ""; } #endif // #if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_INFO) && (OPENTHREAD_CONFIG_LOG_MAC == 1) diff --git a/src/core/common/notifier.hpp b/src/core/common/notifier.hpp index 0db7040f0..c1428079d 100644 --- a/src/core/common/notifier.hpp +++ b/src/core/common/notifier.hpp @@ -79,11 +79,10 @@ public: * This type defines the function pointer which is called to notify of state or configuration changes. * * @param[in] aCallback A reference to callback instance. - * @param[in] aFlags A bit-field indicating specific state that has changed. See `OT_CHANGED_` - * definitions in `instance.h`. + * @param[in] aFlags A bit-field indicating specific state or configuration that has changed. * */ - typedef void (*Handler)(Callback &aCallback, uint32_t aFlags); + typedef void (*Handler)(Callback &aCallback, otChangedFlags aFlags); /** * This constructor initializes a `Callback` instance @@ -149,22 +148,40 @@ public: void RemoveCallback(otStateChangedCallback aCallback, void *aContext); /** - * This method schedules notification of changed flags. - * - * The @p aFlags are combined (bitwise-or) with other flags that have not been provided in a callback yet. + * This method schedules signaling of changed flags. * * @param[in] aFlags A bit-field indicating what configuration or state has changed. * */ - void SetFlags(uint32_t aFlags); + void Signal(otChangedFlags aFlags); /** - * This method indicates whether or not a state changed callback is pending. + * This method schedules signaling of changed flags only if the set of flags has not been signaled before (first + * time signal). * - * @retval TRUE if a state changed callback is pending, FALSE otherwise. + * @param[in] aFlags A bit-field indicating what configuration or state has changed. * */ - bool IsPending(void) const { return (mFlags != 0); } + void SignalIfFirst(otChangedFlags aFlags); + + /** + * This method indicates whether or not a changed callback is pending. + * + * @returns TRUE if a state changed callback is pending, FALSE otherwise. + * + */ + bool IsPending(void) const { return (mFlagsToSignal != 0); } + + /** + * This method indicates whether or not a changed notification for a given set of flags has been signaled before. + * + * @param[in] aFlags A bit-field containing the flag-bits to check. + * + * @retval TRUE All flag bits in @p aFlags have been signaled before. + * @retval FALSE At least one flag bit in @p aFlags has not been signaled before. + * + */ + bool HasSignaled(otChangedFlags aFlags) const { return (mSignaledFlags & aFlags) == aFlags; } private: enum @@ -182,10 +199,11 @@ private: static void HandleStateChanged(Tasklet &aTasklet); void HandleStateChanged(void); - void LogChangedFlags(uint32_t aFlags) const; - const char *FlagToString(uint32_t aFlag) const; + void LogChangedFlags(otChangedFlags aFlags) const; + const char *FlagToString(otChangedFlags aFlag) const; - uint32_t mFlags; + otChangedFlags mFlagsToSignal; + otChangedFlags mSignaledFlags; Tasklet mTask; Callback * mCallbacks; ExternalCallback mExternalCallbacks[kMaxExternalHandlers]; diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index fcbc840a1..9934915b8 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -514,7 +514,7 @@ otError Mac::SetPanChannel(uint8_t aChannel) VerifyOrExit(OT_RADIO_CHANNEL_MIN <= aChannel && aChannel <= OT_RADIO_CHANNEL_MAX, error = OT_ERROR_INVALID_ARGS); - VerifyOrExit(mPanChannel != aChannel); + VerifyOrExit(mPanChannel != aChannel, GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_CHANNEL)); mPanChannel = aChannel; mCcaSuccessRateTracker.Reset(); @@ -525,6 +525,8 @@ otError Mac::SetPanChannel(uint8_t aChannel) UpdateIdleMode(); + GetNotifier().Signal(OT_CHANGED_THREAD_CHANNEL); + exit: return error; } @@ -576,12 +578,22 @@ exit: } otError Mac::SetNetworkName(const char *aNetworkName) +{ + return SetNetworkName(aNetworkName, static_cast(strnlen(aNetworkName, OT_NETWORK_NAME_MAX_SIZE + 1))); +} + +otError Mac::SetNetworkName(const char *aBuffer, uint8_t aLength) { otError error = OT_ERROR_NONE; + uint8_t len = static_cast(strnlen(aBuffer, aLength)); - VerifyOrExit(strlen(aNetworkName) <= OT_NETWORK_NAME_MAX_SIZE, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(len <= OT_NETWORK_NAME_MAX_SIZE, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(memcmp(mNetworkName.m8, aBuffer, len) != 0, + GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_NETWORK_NAME)); - (void)strlcpy(mNetworkName.m8, aNetworkName, sizeof(mNetworkName)); + memcpy(mNetworkName.m8, aBuffer, len); + mNetworkName.m8[len] = 0; + GetNotifier().Signal(OT_CHANGED_THREAD_NETWORK_NAME); exit: return error; @@ -589,15 +601,24 @@ exit: otError Mac::SetPanId(PanId aPanId) { + VerifyOrExit(mPanId != aPanId, GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_PANID)); mPanId = aPanId; otPlatRadioSetPanId(&GetInstance(), mPanId); + GetNotifier().Signal(OT_CHANGED_THREAD_PANID); +exit: return OT_ERROR_NONE; } otError Mac::SetExtendedPanId(const uint8_t *aExtPanId) { + VerifyOrExit(memcmp(mExtendedPanId.m8, aExtPanId, sizeof(mExtendedPanId)) != 0, + GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_EXT_PANID)); + memcpy(mExtendedPanId.m8, aExtPanId, sizeof(mExtendedPanId)); + GetNotifier().Signal(OT_CHANGED_THREAD_EXT_PANID); + +exit: return OT_ERROR_NONE; } diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index 538cf218f..05e5701c3 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -653,13 +653,26 @@ public: /** * This method sets the IEEE 802.15.4 Network Name. * - * @param[in] aNetworkName A pointer to the IEEE 802.15.4 Network Name. + * @param[in] aNetworkName A pointer to the string. Must be null terminated. * - * @retval OT_ERROR_NONE Successfully set the IEEE 802.15.4 Network Name. + * @retval OT_ERROR_NONE Successfully set the IEEE 802.15.4 Network Name. + * @retval OT_ERROR_INVALID_ARGS Given name is too long. * */ otError SetNetworkName(const char *aNetworkName); + /** + * This method sets the IEEE 802.15.4 Network Name. + * + * @param[in] aBuffer A pointer to the char buffer containing the name. Does not need to be null terminated. + * @param[in] aLength Number of chars in the buffer. + * + * @retval OT_ERROR_NONE Successfully set the IEEE 802.15.4 Network Name. + * @retval OT_ERROR_INVALID_ARGS Given name is too long. + * + */ + otError SetNetworkName(const char *aBuffer, uint8_t aLength); + /** * This method returns the IEEE 802.15.4 PAN ID. * diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 252aff8fd..612b20186 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -122,7 +122,7 @@ otError Commissioner::Stop(void) GetNetif().GetCoapSecure().Stop(); mState = OT_COMMISSIONER_STATE_DISABLED; - GetNotifier().SetFlags(OT_CHANGED_COMMISSIONER_STATE); + GetNotifier().Signal(OT_CHANGED_COMMISSIONER_STATE); RemoveCoapResources(); ClearJoiners(); mTransmitAttempts = 0; @@ -681,7 +681,7 @@ exit: } } - GetNotifier().SetFlags(OT_CHANGED_COMMISSIONER_STATE); + GetNotifier().Signal(OT_CHANGED_COMMISSIONER_STATE); } otError Commissioner::SendKeepAlive(void) diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 79602d507..8c1aae574 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -522,12 +522,11 @@ void Dataset::Remove(uint8_t *aStart, uint8_t aLength) otError Dataset::ApplyConfiguration(Instance &aInstance) const { - ThreadNetif &netif = aInstance.GetThreadNetif(); - Notifier & notifier = aInstance.GetNotifier(); - Mac::Mac & mac = netif.GetMac(); - otError error = OT_ERROR_NONE; - const Tlv * cur = reinterpret_cast(mTlvs); - const Tlv * end = reinterpret_cast(mTlvs + mLength); + ThreadNetif &netif = aInstance.GetThreadNetif(); + Mac::Mac & mac = netif.GetMac(); + otError error = OT_ERROR_NONE; + const Tlv * cur = reinterpret_cast(mTlvs); + const Tlv * end = reinterpret_cast(mTlvs + mLength); VerifyOrExit(IsValid(), error = OT_ERROR_PARSE); @@ -539,63 +538,30 @@ otError Dataset::ApplyConfiguration(Instance &aInstance) const { uint8_t channel = static_cast(static_cast(cur)->GetChannel()); - if (mac.GetPanChannel() != channel) + error = mac.SetPanChannel(channel); + + if (error != OT_ERROR_NONE) { - error = mac.SetPanChannel(channel); - - if (error != OT_ERROR_NONE) - { - otLogWarnMeshCoP(aInstance, "DatasetManager::ApplyConfiguration() Failed to set channel to %d (%s)", - channel, otThreadErrorToString(error)); - ExitNow(); - } - - notifier.SetFlags(OT_CHANGED_THREAD_CHANNEL); + otLogWarnMeshCoP(aInstance, "DatasetManager::ApplyConfiguration() Failed to set channel to %d (%s)", + channel, otThreadErrorToString(error)); + ExitNow(); } break; } case Tlv::kPanId: - { - uint16_t panid = static_cast(cur)->GetPanId(); - - if (mac.GetPanId() != panid) - { - mac.SetPanId(panid); - notifier.SetFlags(OT_CHANGED_THREAD_PANID); - } - + mac.SetPanId(static_cast(cur)->GetPanId()); break; - } case Tlv::kExtendedPanId: - { - const ExtendedPanIdTlv *extpanid = static_cast(cur); - - if (memcmp(mac.GetExtendedPanId(), extpanid->GetExtendedPanId(), OT_EXT_PAN_ID_SIZE) != 0) - { - mac.SetExtendedPanId(extpanid->GetExtendedPanId()); - notifier.SetFlags(OT_CHANGED_THREAD_EXT_PANID); - } - + mac.SetExtendedPanId(static_cast(cur)->GetExtendedPanId()); break; - } case Tlv::kNetworkName: { const NetworkNameTlv *name = static_cast(cur); - otNetworkName networkName; - - memcpy(networkName.m8, name->GetNetworkName(), name->GetLength()); - networkName.m8[name->GetLength()] = '\0'; - - if (strcmp(networkName.m8, mac.GetNetworkName()) != 0) - { - mac.SetNetworkName(networkName.m8); - notifier.SetFlags(OT_CHANGED_THREAD_NETWORK_NAME); - } - + mac.SetNetworkName(name->GetNetworkName(), name->GetLength()); break; } diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 394e7c037..e6cc91fe4 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -100,7 +100,7 @@ otError Joiner::Start(const char * aPSKd, VerifyOrExit(mState == OT_JOINER_STATE_IDLE, error = OT_ERROR_BUSY); - GetNotifier().SetFlags(OT_CHANGED_JOINER_STATE); + GetNotifier().Signal(OT_CHANGED_JOINER_STATE); // use extended address based on factory-assigned IEEE EUI-64 GetJoinerId(joinerId); @@ -168,7 +168,7 @@ void Joiner::Complete(otError aError) ThreadNetif &netif = GetNetif(); mState = OT_JOINER_STATE_IDLE; otError error = OT_ERROR_NOT_FOUND; - GetNotifier().SetFlags(OT_CHANGED_JOINER_STATE); + GetNotifier().Signal(OT_CHANGED_JOINER_STATE); netif.GetCoapSecure().Disconnect(); diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 990d220c6..25a3e67ce 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -71,12 +71,12 @@ JoinerRouter::JoinerRouter(Instance &aInstance) aInstance.GetNotifier().RegisterCallback(mNotifierCallback); } -void JoinerRouter::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags) +void JoinerRouter::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags) { aCallback.GetOwner().HandleStateChanged(aFlags); } -void JoinerRouter::HandleStateChanged(uint32_t aFlags) +void JoinerRouter::HandleStateChanged(otChangedFlags aFlags) { ThreadNetif &netif = GetNetif(); diff --git a/src/core/meshcop/joiner_router.hpp b/src/core/meshcop/joiner_router.hpp index e82b4e6ec..1cd629629 100644 --- a/src/core/meshcop/joiner_router.hpp +++ b/src/core/meshcop/joiner_router.hpp @@ -88,8 +88,8 @@ private: kDelayJoinEnt = 50, ///< milliseconds }; - static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags); - void HandleStateChanged(uint32_t aFlags); + static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags); + void HandleStateChanged(otChangedFlags aFlags); static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo); diff --git a/src/core/net/netif.cpp b/src/core/net/netif.cpp index 0bab86ec8..322a6b724 100644 --- a/src/core/net/netif.cpp +++ b/src/core/net/netif.cpp @@ -146,7 +146,7 @@ otError Netif::SubscribeAllRoutersMulticast(void) } } - GetNotifier().SetFlags(OT_CHANGED_IP6_MULTICAST_SUBSRCRIBED); + GetNotifier().Signal(OT_CHANGED_IP6_MULTICAST_SUBSRCRIBED); exit: return error; @@ -178,7 +178,7 @@ exit: if (error != OT_ERROR_NOT_FOUND) { - GetNotifier().SetFlags(OT_CHANGED_IP6_MULTICAST_UNSUBSRCRIBED); + GetNotifier().Signal(OT_CHANGED_IP6_MULTICAST_UNSUBSRCRIBED); } return error; @@ -198,7 +198,7 @@ otError Netif::SubscribeMulticast(NetifMulticastAddress &aAddress) aAddress.mNext = mMulticastAddresses; mMulticastAddresses = &aAddress; - GetNotifier().SetFlags(OT_CHANGED_IP6_MULTICAST_SUBSRCRIBED); + GetNotifier().Signal(OT_CHANGED_IP6_MULTICAST_SUBSRCRIBED); exit: return error; @@ -231,7 +231,7 @@ exit: if (error != OT_ERROR_NOT_FOUND) { - GetNotifier().SetFlags(OT_CHANGED_IP6_MULTICAST_UNSUBSRCRIBED); + GetNotifier().Signal(OT_CHANGED_IP6_MULTICAST_UNSUBSRCRIBED); } return error; @@ -290,7 +290,7 @@ otError Netif::SubscribeExternalMulticast(const Address &aAddress) entry->mAddress = aAddress; entry->mNext = mMulticastAddresses; mMulticastAddresses = entry; - GetNotifier().SetFlags(OT_CHANGED_IP6_MULTICAST_SUBSRCRIBED); + GetNotifier().Signal(OT_CHANGED_IP6_MULTICAST_SUBSRCRIBED); exit: return error; @@ -330,7 +330,7 @@ otError Netif::UnsubscribeExternalMulticast(const Address &aAddress) // To mark the address entry as unused/available, set the `mNext` pointer back to the entry itself. entry->mNext = entry; - GetNotifier().SetFlags(OT_CHANGED_IP6_MULTICAST_UNSUBSRCRIBED); + GetNotifier().Signal(OT_CHANGED_IP6_MULTICAST_UNSUBSRCRIBED); exit: return error; @@ -365,7 +365,7 @@ otError Netif::AddUnicastAddress(NetifUnicastAddress &aAddress) aAddress.mNext = mUnicastAddresses; mUnicastAddresses = &aAddress; - GetNotifier().SetFlags(aAddress.mRloc ? OT_CHANGED_THREAD_RLOC_ADDED : OT_CHANGED_IP6_ADDRESS_ADDED); + GetNotifier().Signal(aAddress.mRloc ? OT_CHANGED_THREAD_RLOC_ADDED : OT_CHANGED_IP6_ADDRESS_ADDED); exit: return error; @@ -398,7 +398,7 @@ exit: if (error != OT_ERROR_NOT_FOUND) { - GetNotifier().SetFlags(aAddress.mRloc ? OT_CHANGED_THREAD_RLOC_REMOVED : OT_CHANGED_IP6_ADDRESS_REMOVED); + GetNotifier().Signal(aAddress.mRloc ? OT_CHANGED_THREAD_RLOC_REMOVED : OT_CHANGED_IP6_ADDRESS_REMOVED); } return error; @@ -443,7 +443,7 @@ otError Netif::AddExternalUnicastAddress(const NetifUnicastAddress &aAddress) entry->mNext = mUnicastAddresses; mUnicastAddresses = entry; - GetNotifier().SetFlags(OT_CHANGED_IP6_ADDRESS_ADDED); + GetNotifier().Signal(OT_CHANGED_IP6_ADDRESS_ADDED); exit: return error; @@ -483,7 +483,7 @@ otError Netif::RemoveExternalUnicastAddress(const Address &aAddress) // To mark the address entry as unused/available, set the `mNext` pointer back to the entry itself. entry->mNext = entry; - GetNotifier().SetFlags(OT_CHANGED_IP6_ADDRESS_REMOVED); + GetNotifier().Signal(OT_CHANGED_IP6_ADDRESS_REMOVED); exit: return error; diff --git a/src/core/thread/announce_sender.cpp b/src/core/thread/announce_sender.cpp index 8de079923..ba13dd6dd 100644 --- a/src/core/thread/announce_sender.cpp +++ b/src/core/thread/announce_sender.cpp @@ -196,12 +196,12 @@ void AnnounceSender::Stop(void) otLogInfoMle(GetInstance(), "Stopping periodic MLE Announcements tx"); } -void AnnounceSender::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags) +void AnnounceSender::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags) { aCallback.GetOwner().HandleStateChanged(aFlags); } -void AnnounceSender::HandleStateChanged(uint32_t aFlags) +void AnnounceSender::HandleStateChanged(otChangedFlags aFlags) { if ((aFlags & OT_CHANGED_THREAD_ROLE) != 0) { diff --git a/src/core/thread/announce_sender.hpp b/src/core/thread/announce_sender.hpp index 17f76564f..fe76d3976 100644 --- a/src/core/thread/announce_sender.hpp +++ b/src/core/thread/announce_sender.hpp @@ -162,8 +162,8 @@ private: void CheckState(void); void Stop(void); static void HandleTimer(Timer &aTimer); - static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags); - void HandleStateChanged(uint32_t aFlags); + static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags); + void HandleStateChanged(otChangedFlags aFlags); Notifier::Callback mNotifierCallback; }; diff --git a/src/core/thread/energy_scan_server.cpp b/src/core/thread/energy_scan_server.cpp index e57d6ff95..4b8c1ee58 100644 --- a/src/core/thread/energy_scan_server.cpp +++ b/src/core/thread/energy_scan_server.cpp @@ -227,12 +227,12 @@ exit: return error; } -void EnergyScanServer::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags) +void EnergyScanServer::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags) { aCallback.GetOwner().HandleStateChanged(aFlags); } -void EnergyScanServer::HandleStateChanged(uint32_t aFlags) +void EnergyScanServer::HandleStateChanged(otChangedFlags aFlags) { if ((aFlags & OT_CHANGED_THREAD_NETDATA) != 0 && !mActive && GetNetif().GetNetworkDataLeader().GetCommissioningData() == NULL) diff --git a/src/core/thread/energy_scan_server.hpp b/src/core/thread/energy_scan_server.hpp index 4574e23f6..32d31b5da 100644 --- a/src/core/thread/energy_scan_server.hpp +++ b/src/core/thread/energy_scan_server.hpp @@ -80,8 +80,8 @@ private: static void HandleTimer(Timer &aTimer); void HandleTimer(void); - static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags); - void HandleStateChanged(uint32_t aFlags); + static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags); + void HandleStateChanged(otChangedFlags aFlags); otError SendReport(void); diff --git a/src/core/thread/key_manager.cpp b/src/core/thread/key_manager.cpp index e7350b95c..ee5fe81e1 100644 --- a/src/core/thread/key_manager.cpp +++ b/src/core/thread/key_manager.cpp @@ -104,11 +104,12 @@ const uint8_t *KeyManager::GetPSKc(void) const void KeyManager::SetPSKc(const uint8_t *aPSKc) { - if (memcmp(mPSKc, aPSKc, sizeof(mPSKc)) != 0) - { - memcpy(mPSKc, aPSKc, sizeof(mPSKc)); - GetNotifier().SetFlags(OT_CHANGED_PSKC); - } + VerifyOrExit(memcmp(mPSKc, aPSKc, sizeof(mPSKc)) != 0, GetNotifier().SignalIfFirst(OT_CHANGED_PSKC)); + memcpy(mPSKc, aPSKc, sizeof(mPSKc)); + GetNotifier().Signal(OT_CHANGED_PSKC); + +exit: + return; } #endif @@ -123,7 +124,8 @@ otError KeyManager::SetMasterKey(const otMasterKey &aKey) otError error = OT_ERROR_NONE; Router * routers; - VerifyOrExit(memcmp(&mMasterKey, &aKey, sizeof(mMasterKey)) != 0); + VerifyOrExit(memcmp(&mMasterKey, &aKey, sizeof(mMasterKey)) != 0, + GetNotifier().SignalIfFirst(OT_CHANGED_MASTER_KEY)); mMasterKey = aKey; mKeySequence = 0; @@ -151,7 +153,7 @@ otError KeyManager::SetMasterKey(const otMasterKey &aKey) iter.GetChild()->SetMleFrameCounter(0); } - GetNotifier().SetFlags(OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER | OT_CHANGED_MASTER_KEY); + GetNotifier().Signal(OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER | OT_CHANGED_MASTER_KEY); exit: return error; @@ -178,10 +180,7 @@ otError KeyManager::ComputeKey(uint32_t aKeySequence, uint8_t *aKey) void KeyManager::SetCurrentKeySequence(uint32_t aKeySequence) { - if (aKeySequence == mKeySequence) - { - ExitNow(); - } + VerifyOrExit(aKeySequence != mKeySequence, GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER)); // Check if the guard timer has expired if key rotation is requested. if ((aKeySequence == (mKeySequence + 1)) && (mKeySwitchGuardTime != 0) && mKeyRotationTimer.IsRunning() && @@ -202,7 +201,7 @@ void KeyManager::SetCurrentKeySequence(uint32_t aKeySequence) StartKeyRotationTimer(); } - GetNotifier().SetFlags(OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER); + GetNotifier().Signal(OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER); exit: return; @@ -260,10 +259,12 @@ exit: void KeyManager::SetSecurityPolicyFlags(uint8_t aSecurityPolicyFlags) { - if (mSecurityPolicyFlags != aSecurityPolicyFlags) + Notifier ¬ifier = GetNotifier(); + + if (!notifier.HasSignaled(OT_CHANGED_SECURITY_POLICY) || (mSecurityPolicyFlags != aSecurityPolicyFlags)) { mSecurityPolicyFlags = aSecurityPolicyFlags; - GetNotifier().SetFlags(OT_CHANGED_SECURITY_POLICY); + notifier.Signal(OT_CHANGED_SECURITY_POLICY); } } diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 84e60aa1e..0d5473296 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -294,12 +294,12 @@ exit: void Mle::SetRole(otDeviceRole aRole) { - VerifyOrExit(aRole != mRole); + VerifyOrExit(aRole != mRole, GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_ROLE)); otLogInfoMle(GetInstance(), "Role %s -> %s", RoleToString(mRole), RoleToString(aRole)); mRole = aRole; - GetNotifier().SetFlags(OT_CHANGED_THREAD_ROLE); + GetNotifier().Signal(OT_CHANGED_THREAD_ROLE); exit: return; @@ -805,7 +805,7 @@ otError Mle::UpdateLinkLocalAddress(void) mLinkLocal64.GetAddress().SetIid(netif.GetMac().GetExtAddress()); netif.AddUnicastAddress(mLinkLocal64); - GetNotifier().SetFlags(OT_CHANGED_THREAD_LL_ADDR); + GetNotifier().Signal(OT_CHANGED_THREAD_LL_ADDR); return OT_ERROR_NONE; } @@ -821,6 +821,7 @@ otError Mle::SetMeshLocalPrefix(const uint8_t *aMeshLocalPrefix) if (memcmp(mMeshLocal64.GetAddress().mFields.m8, aMeshLocalPrefix, 8) == 0) { + GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_ML_ADDR); ExitNow(); } @@ -872,7 +873,7 @@ otError Mle::SetMeshLocalPrefix(const uint8_t *aMeshLocalPrefix) } // Changing the prefix also causes the mesh local address to be different. - GetNotifier().SetFlags(OT_CHANGED_THREAD_ML_ADDR); + GetNotifier().Signal(OT_CHANGED_THREAD_ML_ADDR); exit: return OT_ERROR_NONE; @@ -922,7 +923,11 @@ void Mle::SetLeaderData(uint32_t aPartitionId, uint8_t aWeighting, uint8_t aLead if (mLeaderData.GetPartitionId() != aPartitionId) { GetNetif().GetMle().HandlePartitionChange(); - GetNotifier().SetFlags(OT_CHANGED_THREAD_PARTITION_ID); + GetNotifier().Signal(OT_CHANGED_THREAD_PARTITION_ID); + } + else + { + GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_PARTITION_ID); } mLeaderData.SetPartitionId(aPartitionId); @@ -1374,12 +1379,12 @@ exit: return error; } -void Mle::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags) +void Mle::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags) { aCallback.GetOwner().HandleStateChanged(aFlags); } -void Mle::HandleStateChanged(uint32_t aFlags) +void Mle::HandleStateChanged(otChangedFlags aFlags) { ThreadNetif &netif = GetNetif(); VerifyOrExit(mRole != OT_DEVICE_ROLE_DISABLED); @@ -1393,7 +1398,7 @@ void Mle::HandleStateChanged(uint32_t aFlags) OT_IP6_ADDRESS_SIZE - OT_IP6_PREFIX_SIZE); netif.AddUnicastAddress(mMeshLocal64); - GetNotifier().SetFlags(OT_CHANGED_THREAD_ML_ADDR); + GetNotifier().Signal(OT_CHANGED_THREAD_ML_ADDR); } if (mRole == OT_DEVICE_ROLE_CHILD && !IsFullThreadDevice()) diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index cf1da67a8..dadc71ea7 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -1545,8 +1545,8 @@ private: uint8_t aSecurityLevel, uint8_t * aNonce); - static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags); - void HandleStateChanged(uint32_t aFlags); + static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags); + void HandleStateChanged(otChangedFlags aFlags); static void HandleAttachTimer(Timer &aTimer); void HandleAttachTimer(void); static void HandleDelayedResponseTimer(Timer &aTimer); diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 696cf39d0..834a388bc 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -4517,11 +4517,11 @@ void MleRouter::SignalChildUpdated(otThreadChildTableEvent aEvent, Child &aChild switch (aEvent) { case OT_THREAD_CHILD_TABLE_EVENT_CHILD_ADDED: - GetNotifier().SetFlags(OT_CHANGED_THREAD_CHILD_ADDED); + GetNotifier().Signal(OT_CHANGED_THREAD_CHILD_ADDED); break; case OT_THREAD_CHILD_TABLE_EVENT_CHILD_REMOVED: - GetNotifier().SetFlags(OT_CHANGED_THREAD_CHILD_REMOVED); + GetNotifier().Signal(OT_CHANGED_THREAD_CHILD_REMOVED); break; } } diff --git a/src/core/thread/network_data_leader.cpp b/src/core/thread/network_data_leader.cpp index 21555d765..5a1f26b76 100644 --- a/src/core/thread/network_data_leader.cpp +++ b/src/core/thread/network_data_leader.cpp @@ -68,7 +68,7 @@ void LeaderBase::Reset(void) mVersion = static_cast(otPlatRandomGet()); mStableVersion = static_cast(otPlatRandomGet()); mLength = 0; - GetNotifier().SetFlags(OT_CHANGED_THREAD_NETDATA); + GetNotifier().Signal(OT_CHANGED_THREAD_NETDATA); } otError LeaderBase::GetContext(const Ip6::Address &aAddress, Lowpan::Context &aContext) @@ -431,7 +431,7 @@ otError LeaderBase::SetNetworkData(uint8_t aVersion, otDumpDebgNetData(GetInstance(), "set network data", mTlvs, mLength); - GetNotifier().SetFlags(OT_CHANGED_THREAD_NETDATA); + GetNotifier().Signal(OT_CHANGED_THREAD_NETDATA); exit: return error; @@ -457,7 +457,7 @@ otError LeaderBase::SetCommissioningData(const uint8_t *aValue, uint8_t aValueLe } mVersion++; - GetNotifier().SetFlags(OT_CHANGED_THREAD_NETDATA); + GetNotifier().Signal(OT_CHANGED_THREAD_NETDATA); exit: return error; diff --git a/src/core/thread/network_data_leader_ftd.cpp b/src/core/thread/network_data_leader_ftd.cpp index 8f3414b29..320ec35b2 100644 --- a/src/core/thread/network_data_leader_ftd.cpp +++ b/src/core/thread/network_data_leader_ftd.cpp @@ -103,7 +103,7 @@ void Leader::IncrementVersion(void) if (GetNetif().GetMle().GetRole() == OT_DEVICE_ROLE_LEADER) { mVersion++; - GetNotifier().SetFlags(OT_CHANGED_THREAD_NETDATA); + GetNotifier().Signal(OT_CHANGED_THREAD_NETDATA); } } @@ -142,7 +142,7 @@ void Leader::RemoveBorderRouter(uint16_t aRloc16) mStableVersion++; } - GetNotifier().SetFlags(OT_CHANGED_THREAD_NETDATA); + GetNotifier().Signal(OT_CHANGED_THREAD_NETDATA); exit: return; @@ -772,7 +772,7 @@ otError Leader::RegisterNetworkData(uint16_t aRloc16, uint8_t *aTlvs, uint8_t aT } } - GetNotifier().SetFlags(OT_CHANGED_THREAD_NETDATA); + GetNotifier().Signal(OT_CHANGED_THREAD_NETDATA); exit: return error; @@ -1179,7 +1179,7 @@ otError Leader::FreeContext(uint8_t aContextId) mContextUsed &= ~(1 << aContextId); mVersion++; mStableVersion++; - GetNotifier().SetFlags(OT_CHANGED_THREAD_NETDATA); + GetNotifier().Signal(OT_CHANGED_THREAD_NETDATA); return OT_ERROR_NONE; } diff --git a/src/core/utils/channel_manager.cpp b/src/core/utils/channel_manager.cpp index 925bbae85..30653e45d 100644 --- a/src/core/utils/channel_manager.cpp +++ b/src/core/utils/channel_manager.cpp @@ -79,7 +79,7 @@ void ChannelManager::RequestChannelChange(uint8_t aChannel) mTimer.Start(1 + Random::GetUint32InRange(0, kRequestStartJitterInterval)); - GetNotifier().SetFlags(OT_CHANGED_CHANNEL_MANAGER_NEW_CHANNEL); + GetNotifier().Signal(OT_CHANGED_CHANNEL_MANAGER_NEW_CHANNEL); exit: return; @@ -256,12 +256,12 @@ void ChannelManager::HandleTimer(void) } } -void ChannelManager::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags) +void ChannelManager::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags) { aCallback.GetOwner().HandleStateChanged(aFlags); } -void ChannelManager::HandleStateChanged(uint32_t aFlags) +void ChannelManager::HandleStateChanged(otChangedFlags aFlags) { VerifyOrExit((aFlags & OT_CHANGED_THREAD_CHANNEL) != 0); VerifyOrExit(mChannel == GetInstance().Get().GetPanChannel()); diff --git a/src/core/utils/channel_manager.hpp b/src/core/utils/channel_manager.hpp index bb9b1b3d7..24742d018 100644 --- a/src/core/utils/channel_manager.hpp +++ b/src/core/utils/channel_manager.hpp @@ -273,8 +273,8 @@ private: static void HandleTimer(Timer &aTimer); void HandleTimer(void); - static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aChangedFlags); - void HandleStateChanged(uint32_t aChangedFlags); + static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aChangedFlags); + void HandleStateChanged(otChangedFlags aChangedFlags); void PreparePendingDataset(void); void StartAutoSelectTimer(void); diff --git a/src/core/utils/child_supervision.cpp b/src/core/utils/child_supervision.cpp index 49dec21b9..a7e1dfd28 100644 --- a/src/core/utils/child_supervision.cpp +++ b/src/core/utils/child_supervision.cpp @@ -167,12 +167,12 @@ void ChildSupervisor::CheckState(void) } } -void ChildSupervisor::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags) +void ChildSupervisor::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags) { aCallback.GetOwner().HandleStateChanged(aFlags); } -void ChildSupervisor::HandleStateChanged(uint32_t aFlags) +void ChildSupervisor::HandleStateChanged(otChangedFlags aFlags) { if ((aFlags & (OT_CHANGED_THREAD_ROLE | OT_CHANGED_THREAD_CHILD_ADDED | OT_CHANGED_THREAD_CHILD_REMOVED)) != 0) { diff --git a/src/core/utils/child_supervision.hpp b/src/core/utils/child_supervision.hpp index 2fbbf1d26..98f8bc6f2 100644 --- a/src/core/utils/child_supervision.hpp +++ b/src/core/utils/child_supervision.hpp @@ -159,8 +159,8 @@ private: void CheckState(void); static void HandleTimer(Timer &aTimer); void HandleTimer(void); - static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags); - void HandleStateChanged(uint32_t aFlags); + static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags); + void HandleStateChanged(otChangedFlags aFlags); uint16_t mSupervisionInterval; TimerMilli mTimer; diff --git a/src/core/utils/jam_detector.cpp b/src/core/utils/jam_detector.cpp index e92fc271f..1ec2d22f2 100644 --- a/src/core/utils/jam_detector.cpp +++ b/src/core/utils/jam_detector.cpp @@ -278,12 +278,12 @@ void JamDetector::SetJamState(bool aNewState) } } -void JamDetector::HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags) +void JamDetector::HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags) { aCallback.GetOwner().HandleStateChanged(aFlags); } -void JamDetector::HandleStateChanged(uint32_t aFlags) +void JamDetector::HandleStateChanged(otChangedFlags aFlags) { if (aFlags & OT_CHANGED_THREAD_ROLE) { diff --git a/src/core/utils/jam_detector.hpp b/src/core/utils/jam_detector.hpp index 0a46fef80..593651737 100644 --- a/src/core/utils/jam_detector.hpp +++ b/src/core/utils/jam_detector.hpp @@ -191,8 +191,8 @@ private: void HandleTimer(void); void UpdateHistory(bool aThresholdExceeded); void UpdateJamState(void); - static void HandleStateChanged(Notifier::Callback &aCallback, uint32_t aFlags); - void HandleStateChanged(uint32_t aFlags); + static void HandleStateChanged(Notifier::Callback &aCallback, otChangedFlags aFlags); + void HandleStateChanged(otChangedFlags aFlags); Handler mHandler; // Handler/callback to inform about jamming state void * mContext; // Context for handler/callback diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index 220a88978..ac7bb51a1 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -269,7 +269,7 @@ protected: #endif // OPENTHREAD_RADIO || OPENTHREAD_ENABLE_RAW_LINK_API #if OPENTHREAD_MTD || OPENTHREAD_FTD - static void HandleStateChanged(uint32_t aFlags, void *aContext); + static void HandleStateChanged(otChangedFlags aFlags, void *aContext); void ProcessThreadChangedFlags(void); #if OPENTHREAD_FTD diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index a2964e6a5..2d02e66f3 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -2884,7 +2884,7 @@ exit: // MARK: Property/Status Changed // ---------------------------------------------------------------------------- -void NcpBase::HandleStateChanged(uint32_t aFlags, void *aContext) +void NcpBase::HandleStateChanged(otChangedFlags aFlags, void *aContext) { NcpBase *ncp = static_cast(aContext); @@ -2896,7 +2896,7 @@ void NcpBase::ProcessThreadChangedFlags(void) { static const struct { - uint32_t mThreadFlag; + otChangedFlags mThreadFlag; spinel_prop_key_t mPropKey; } kFlags[] = { {OT_CHANGED_IP6_ADDRESS_ADDED, SPINEL_PROP_IPV6_ADDRESS_TABLE},