mirror of
https://github.com/espressif/openthread.git
synced 2026-08-23 02:39:52 +00:00
[time-sync] define cpp enum/types mirroring public definitions (#8811)
This commit is contained in:
@@ -46,7 +46,7 @@ otNetworkTimeStatus otNetworkTimeGet(otInstance *aInstance, uint64_t *aNetworkTi
|
||||
{
|
||||
AssertPointerIsNotNull(aNetworkTime);
|
||||
|
||||
return AsCoreType(aInstance).Get<TimeSync>().GetTime(*aNetworkTime);
|
||||
return MapEnum(AsCoreType(aInstance).Get<TimeSync>().GetTime(*aNetworkTime));
|
||||
}
|
||||
|
||||
otError otNetworkTimeSetSyncPeriod(otInstance *aInstance, uint16_t aTimeSyncPeriod)
|
||||
|
||||
@@ -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<uint64_t>(static_cast<int64_t>(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));
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
|
||||
#include <openthread/network_time.h>
|
||||
|
||||
#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<otNetworkTimeSyncCallbackFn> 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);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user