diff --git a/src/core/api/network_time_api.cpp b/src/core/api/network_time_api.cpp index add205416..903ed8661 100644 --- a/src/core/api/network_time_api.cpp +++ b/src/core/api/network_time_api.cpp @@ -46,7 +46,7 @@ otNetworkTimeStatus otNetworkTimeGet(otInstance *aInstance, uint64_t *aNetworkTi { AssertPointerIsNotNull(aNetworkTime); - return AsCoreType(aInstance).Get().GetTime(*aNetworkTime); + return MapEnum(AsCoreType(aInstance).Get().GetTime(*aNetworkTime)); } otError otNetworkTimeSetSyncPeriod(otInstance *aInstance, uint16_t aTimeSyncPeriod) diff --git a/src/core/thread/time_sync_service.cpp b/src/core/thread/time_sync_service.cpp index 9aacfda0d..138c1aa74 100644 --- a/src/core/thread/time_sync_service.cpp +++ b/src/core/thread/time_sync_service.cpp @@ -63,12 +63,12 @@ TimeSync::TimeSync(Instance &aInstance) , mLastTimeSyncReceived(0) , mNetworkTimeOffset(0) , mTimer(aInstance) - , mCurrentStatus(OT_NETWORK_TIME_UNSYNCHRONIZED) + , mCurrentStatus(kUnsynchronized) { CheckAndHandleChanges(false); } -otNetworkTimeStatus TimeSync::GetTime(uint64_t &aNetworkTime) const +TimeSync::Status TimeSync::GetTime(uint64_t &aNetworkTime) const { aNetworkTime = static_cast(static_cast(otPlatTimeGet()) + mNetworkTimeOffset); @@ -181,7 +181,7 @@ void TimeSync::HandleNotifierEvents(Events aEvents) mTimeSyncSeq = OT_TIME_SYNC_INVALID_SEQ; mTimeSyncRequired = false; - // Network time status will become OT_NETWORK_TIME_UNSYNCHRONIZED because no network time has yet been received + // Network time status will become kUnsychronized because no network time has yet been received // on the new partition. mLastTimeSyncReceived.SetValue(0); @@ -200,9 +200,9 @@ void TimeSync::HandleTimeout(void) { CheckAndHandleChanges(false); } void TimeSync::CheckAndHandleChanges(bool aTimeUpdated) { - otNetworkTimeStatus networkTimeStatus = OT_NETWORK_TIME_SYNCHRONIZED; - const uint32_t resyncNeededThresholdMs = 2 * Time::SecToMsec(mTimeSyncPeriod); - const uint32_t timeSyncLastSyncMs = TimerMilli::GetNow() - mLastTimeSyncReceived; + Status networkTimeStatus = kSynchronized; + const uint32_t resyncNeededThresholdMs = 2 * Time::SecToMsec(mTimeSyncPeriod); + const uint32_t timeSyncLastSyncMs = TimerMilli::GetNow() - mLastTimeSyncReceived; mTimer.Stop(); @@ -210,7 +210,7 @@ void TimeSync::CheckAndHandleChanges(bool aTimeUpdated) { case Mle::kRoleDisabled: case Mle::kRoleDetached: - networkTimeStatus = OT_NETWORK_TIME_UNSYNCHRONIZED; + networkTimeStatus = kUnsynchronized; LogInfo("Time sync status UNSYNCHRONIZED as role:DISABLED/DETACHED"); break; @@ -219,13 +219,13 @@ void TimeSync::CheckAndHandleChanges(bool aTimeUpdated) if (mLastTimeSyncReceived.GetValue() == 0) { // Haven't yet received any time sync - networkTimeStatus = OT_NETWORK_TIME_UNSYNCHRONIZED; + networkTimeStatus = kUnsynchronized; LogInfo("Time sync status UNSYNCHRONIZED as mLastTimeSyncReceived:0"); } else if (timeSyncLastSyncMs > resyncNeededThresholdMs) { // The device hasn’t received time sync for more than two periods time. - networkTimeStatus = OT_NETWORK_TIME_RESYNC_NEEDED; + networkTimeStatus = kResyncNeeded; LogInfo("Time sync status RESYNC_NEEDED as timeSyncLastSyncMs:%lu > resyncNeededThresholdMs:%lu", ToUlong(timeSyncLastSyncMs), ToUlong(resyncNeededThresholdMs)); } diff --git a/src/core/thread/time_sync_service.hpp b/src/core/thread/time_sync_service.hpp index 3456ef0a5..d0ad1acf8 100644 --- a/src/core/thread/time_sync_service.hpp +++ b/src/core/thread/time_sync_service.hpp @@ -40,6 +40,7 @@ #include +#include "common/as_core_type.hpp" #include "common/callback.hpp" #include "common/locator.hpp" #include "common/message.hpp" @@ -58,6 +59,17 @@ class TimeSync : public InstanceLocator, private NonCopyable friend class ot::Notifier; public: + /** + * This enumeration represents Network Time Status + * + */ + enum Status : int8_t + { + kUnsynchronized = OT_NETWORK_TIME_UNSYNCHRONIZED, ///< The device hasn't attached to a network. + kResyncNeeded = OT_NETWORK_TIME_RESYNC_NEEDED, ///< The device hasn’t received time sync for 2 periods. + kSynchronized = OT_NETWORK_TIME_SYNCHRONIZED, ///< The device network time is synchronized. + }; + /** * This constructor initializes the object. * @@ -72,7 +84,7 @@ public: * @returns The time synchronization status. * */ - otNetworkTimeStatus GetTime(uint64_t &aNetworkTime) const; + Status GetTime(uint64_t &aNetworkTime) const; /** * Handle the message which includes time synchronization information. @@ -205,9 +217,11 @@ private: Callback mTimeSyncCallback; ///< Callback when time sync is handled or status updated. SyncTimer mTimer; ///< Timer for checking if a resync is required. - otNetworkTimeStatus mCurrentStatus; ///< Current network time status. + Status mCurrentStatus; ///< Current network time status. }; +DefineMapEnum(otNetworkTimeStatus, TimeSync::Status); + /** * @} */