From 3c6b8a3dd202c83f88d6a38c328b382311380b94 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Tue, 27 Aug 2024 11:26:31 +0800 Subject: [PATCH] [time] unify macros for time unit conversion (#10636) This commit unifies macros used for time unit conversion so that we don't need to define it on different components. --- examples/platforms/simulation/alarm.c | 24 ++++++++----------- examples/platforms/simulation/radio.c | 7 ++---- .../simulation/virtual_time/alarm-sim.c | 7 +++--- include/openthread/instance.h | 2 +- include/openthread/platform/time.h | 5 ++++ src/posix/platform/alarm.cpp | 20 ++++++++-------- src/posix/platform/hdlc_interface.cpp | 20 ++++++++-------- src/posix/platform/multicast_routing.cpp | 4 ++-- src/posix/platform/platform-posix.h | 13 ---------- src/posix/platform/radio.cpp | 6 ++--- src/posix/platform/spi_interface.cpp | 4 ++-- 11 files changed, 48 insertions(+), 64 deletions(-) diff --git a/examples/platforms/simulation/alarm.c b/examples/platforms/simulation/alarm.c index d826d111d4b..3a151806437 100644 --- a/examples/platforms/simulation/alarm.c +++ b/examples/platforms/simulation/alarm.c @@ -51,14 +51,10 @@ timer_t sMicroTimer; #include #include #include +#include #include "lib/platform/exit_code.h" -#define MS_PER_S 1000 -#define NS_PER_US 1000 -#define US_PER_MS 1000 -#define US_PER_S 1000000 - #define DEFAULT_TIMEOUT_IN_SEC 10 // seconds #ifdef CLOCK_MONOTONIC_RAW @@ -146,7 +142,7 @@ uint64_t platformGetNow(void) VerifyOrDie(err == 0, OT_EXIT_ERROR_ERRNO); - return (uint64_t)now.tv_sec * sSpeedUpFactor * US_PER_S + (uint64_t)now.tv_nsec * sSpeedUpFactor / NS_PER_US; + return (uint64_t)now.tv_sec * sSpeedUpFactor * OT_US_PER_S + (uint64_t)now.tv_nsec * sSpeedUpFactor / OT_NS_PER_US; } #else uint64_t platformGetNow(void) @@ -158,11 +154,11 @@ uint64_t platformGetNow(void) assert(err == 0); - return (uint64_t)tv.tv_sec * sSpeedUpFactor * US_PER_S + (uint64_t)tv.tv_usec * sSpeedUpFactor; + return (uint64_t)tv.tv_sec * sSpeedUpFactor * OT_US_PER_S + (uint64_t)tv.tv_usec * sSpeedUpFactor; } #endif // defined(CLOCK_MONOTONIC_RAW) || defined(CLOCK_MONOTONIC) -uint32_t otPlatAlarmMilliGetNow(void) { return (uint32_t)(platformGetNow() / US_PER_MS); } +uint32_t otPlatAlarmMilliGetNow(void) { return (uint32_t)(platformGetNow() / OT_US_PER_MS); } void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) { @@ -193,8 +189,8 @@ void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) struct itimerspec its; uint32_t diff = sUsAlarm - otPlatAlarmMicroGetNow(); - its.it_value.tv_sec = diff / US_PER_S; - its.it_value.tv_nsec = (diff % US_PER_S) * NS_PER_US; + its.it_value.tv_sec = diff / OT_US_PER_S; + its.it_value.tv_nsec = (diff % OT_US_PER_S) * OT_NS_PER_US; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; @@ -229,7 +225,7 @@ void otPlatAlarmMicroStop(otInstance *aInstance) void platformAlarmUpdateTimeout(struct timeval *aTimeout) { - uint64_t remaining = DEFAULT_TIMEOUT_IN_SEC * US_PER_S; // in usec. + uint64_t remaining = DEFAULT_TIMEOUT_IN_SEC * OT_US_PER_S; // in usec. assert(aTimeout != NULL); @@ -237,7 +233,7 @@ void platformAlarmUpdateTimeout(struct timeval *aTimeout) { uint32_t msRemaining = calculateDuration(sMsAlarm, otPlatAlarmMilliGetNow()); - remaining = ((uint64_t)msRemaining) * US_PER_MS; + remaining = ((uint64_t)msRemaining) * OT_US_PER_MS; } if (sIsUsRunning) @@ -264,8 +260,8 @@ void platformAlarmUpdateTimeout(struct timeval *aTimeout) remaining = 1; } - aTimeout->tv_sec = (time_t)(remaining / US_PER_S); - aTimeout->tv_usec = remaining % US_PER_S; + aTimeout->tv_sec = (time_t)(remaining / OT_US_PER_S); + aTimeout->tv_usec = remaining % OT_US_PER_S; } } diff --git a/examples/platforms/simulation/radio.c b/examples/platforms/simulation/radio.c index 9e547e1c286..430440bc5a3 100644 --- a/examples/platforms/simulation/radio.c +++ b/examples/platforms/simulation/radio.c @@ -47,9 +47,6 @@ #include "utils/mac_frame.h" #include "utils/soft_source_match_table.h" -#define MS_PER_S 1000 -#define US_PER_MS 1000 - enum { IEEE802154_ACK_LENGTH = 5, @@ -814,8 +811,8 @@ void platformRadioUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct ti { uint32_t remaining = sEnergyScanEndTime - now; - tv.tv_sec = remaining / MS_PER_S; - tv.tv_usec = (remaining % MS_PER_S) * US_PER_MS; + tv.tv_sec = remaining / OT_MS_PER_S; + tv.tv_usec = (remaining % OT_MS_PER_S) * OT_US_PER_MS; } if (timercmp(&tv, aTimeout, <)) diff --git a/examples/platforms/simulation/virtual_time/alarm-sim.c b/examples/platforms/simulation/virtual_time/alarm-sim.c index 7c2cea1e5e8..b0d78621856 100644 --- a/examples/platforms/simulation/virtual_time/alarm-sim.c +++ b/examples/platforms/simulation/virtual_time/alarm-sim.c @@ -37,8 +37,7 @@ #include #include #include - -#define US_PER_MS 1000 +#include extern uint64_t sNow; // microseconds @@ -59,7 +58,7 @@ uint64_t platformAlarmGetNow(void) { return sNow; } void platformAlarmAdvanceNow(uint64_t aDelta) { sNow += aDelta; } -uint32_t otPlatAlarmMilliGetNow(void) { return (uint32_t)(sNow / US_PER_MS); } +uint32_t otPlatAlarmMilliGetNow(void) { return (uint32_t)(sNow / OT_US_PER_MS); } void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) { @@ -108,7 +107,7 @@ uint64_t platformAlarmGetNext(void) else { remaining = (uint64_t)milli; - remaining *= US_PER_MS; + remaining *= OT_US_PER_MS; } } diff --git a/include/openthread/instance.h b/include/openthread/instance.h index bbc4d4d72bd..d3cdebb1205 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (436) +#define OPENTHREAD_API_VERSION (437) /** * @addtogroup api-instance diff --git a/include/openthread/platform/time.h b/include/openthread/platform/time.h index 1766cc55934..7ce36519a79 100644 --- a/include/openthread/platform/time.h +++ b/include/openthread/platform/time.h @@ -41,6 +41,11 @@ extern "C" { #endif +#define OT_MS_PER_S 1000 ///< Number of milliseconds per second +#define OT_US_PER_MS 1000 ///< Number of microseconds per millisecond +#define OT_US_PER_S 1000000 ///< Number of microseconds per second +#define OT_NS_PER_US 1000 ///< Number of nanoseconds per microsecond + /** * @addtogroup plat-time * diff --git a/src/posix/platform/alarm.cpp b/src/posix/platform/alarm.cpp index a739a56b98f..3c2f7012d33 100644 --- a/src/posix/platform/alarm.cpp +++ b/src/posix/platform/alarm.cpp @@ -83,7 +83,7 @@ uint64_t otPlatTimeGet(void) VerifyOrDie(clock_gettime(OT_POSIX_CLOCK_ID, &now) == 0, OT_EXIT_FAILURE); - return static_cast(now.tv_sec) * US_PER_S + static_cast(now.tv_nsec) / NS_PER_US; + return static_cast(now.tv_sec) * OT_US_PER_S + static_cast(now.tv_nsec) / OT_NS_PER_US; } #endif // !OPENTHREAD_POSIX_VIRTUAL_TIME @@ -128,7 +128,7 @@ void platformAlarmInit(uint32_t aSpeedUpFactor, int aRealTimeSignal) } } -uint32_t otPlatAlarmMilliGetNow(void) { return (uint32_t)(platformAlarmGetNow() / US_PER_MS); } +uint32_t otPlatAlarmMilliGetNow(void) { return (uint32_t)(platformAlarmGetNow() / OT_US_PER_MS); } void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) { @@ -161,8 +161,8 @@ void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) struct itimerspec its; uint32_t diff = sUsAlarm - otPlatAlarmMicroGetNow(); - its.it_value.tv_sec = diff / US_PER_S; - its.it_value.tv_nsec = (diff % US_PER_S) * NS_PER_US; + its.it_value.tv_sec = diff / OT_US_PER_S; + its.it_value.tv_nsec = (diff % OT_US_PER_S) * OT_NS_PER_US; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; @@ -204,10 +204,10 @@ void platformAlarmUpdateTimeout(struct timeval *aTimeout) if (sIsMsRunning) { - remaining = (int32_t)(sMsAlarm - (uint32_t)(now / US_PER_MS)); + remaining = (int32_t)(sMsAlarm - (uint32_t)(now / OT_US_PER_MS)); VerifyOrExit(remaining > 0); - remaining *= US_PER_MS; - remaining -= (now % US_PER_MS); + remaining *= OT_US_PER_MS; + remaining -= (now % OT_US_PER_MS); } #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE @@ -237,10 +237,10 @@ void platformAlarmUpdateTimeout(struct timeval *aTimeout) remaining = 1; } - if (remaining < static_cast(aTimeout->tv_sec) * US_PER_S + static_cast(aTimeout->tv_usec)) + if (remaining < static_cast(aTimeout->tv_sec) * OT_US_PER_S + static_cast(aTimeout->tv_usec)) { - aTimeout->tv_sec = static_cast(remaining / US_PER_S); - aTimeout->tv_usec = static_cast(remaining % US_PER_S); + aTimeout->tv_sec = static_cast(remaining / OT_US_PER_S); + aTimeout->tv_usec = static_cast(remaining % OT_US_PER_S); } } } diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index a1a6c8c9ad0..235af9ac87c 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -283,8 +283,8 @@ otError HdlcInterface::WaitForFrame(uint64_t aTimeoutUs) #if OPENTHREAD_POSIX_VIRTUAL_TIME struct VirtualTimeEvent event; - timeout.tv_sec = static_cast(aTimeoutUs / US_PER_S); - timeout.tv_usec = static_cast(aTimeoutUs % US_PER_S); + timeout.tv_sec = static_cast(aTimeoutUs / OT_US_PER_S); + timeout.tv_usec = static_cast(aTimeoutUs % OT_US_PER_S); virtualTimeSendSleepEvent(&timeout); virtualTimeReceiveEvent(&event); @@ -304,8 +304,8 @@ otError HdlcInterface::WaitForFrame(uint64_t aTimeoutUs) break; } #else // OPENTHREAD_POSIX_VIRTUAL_TIME - timeout.tv_sec = static_cast(aTimeoutUs / US_PER_S); - timeout.tv_usec = static_cast(aTimeoutUs % US_PER_S); + timeout.tv_sec = static_cast(aTimeoutUs / OT_US_PER_S); + timeout.tv_usec = static_cast(aTimeoutUs % OT_US_PER_S); fd_set read_fds; fd_set error_fds; @@ -392,7 +392,7 @@ otError HdlcInterface::WaitForWritable(void) otError error = OT_ERROR_NONE; struct timeval timeout = {kMaxWaitTime / 1000, (kMaxWaitTime % 1000) * 1000}; uint64_t now = otPlatTimeGet(); - uint64_t end = now + kMaxWaitTime * US_PER_MS; + uint64_t end = now + kMaxWaitTime * OT_US_PER_MS; fd_set writeFds; fd_set errorFds; int rval; @@ -432,8 +432,8 @@ otError HdlcInterface::WaitForWritable(void) { uint64_t remain = end - now; - timeout.tv_sec = static_cast(remain / US_PER_S); - timeout.tv_usec = static_cast(remain % US_PER_S); + timeout.tv_sec = static_cast(remain / OT_US_PER_S); + timeout.tv_usec = static_cast(remain % OT_US_PER_S); } else { @@ -744,10 +744,10 @@ otError HdlcInterface::ResetConnection(void) if (mRadioUrl.HasParam("uart-reset")) { - usleep(static_cast(kRemoveRcpDelay) * US_PER_MS); + usleep(static_cast(kRemoveRcpDelay) * OT_US_PER_MS); CloseFile(); - end = otPlatTimeGet() + kResetTimeout * US_PER_MS; + end = otPlatTimeGet() + kResetTimeout * OT_US_PER_MS; do { mSockFd = OpenFile(mRadioUrl); @@ -755,7 +755,7 @@ otError HdlcInterface::ResetConnection(void) { ExitNow(); } - usleep(static_cast(kOpenFileDelay) * US_PER_MS); + usleep(static_cast(kOpenFileDelay) * OT_US_PER_MS); } while (end > otPlatTimeGet()); LogCrit("Failed to reopen UART connection after resetting the RCP device."); diff --git a/src/posix/platform/multicast_routing.cpp b/src/posix/platform/multicast_routing.cpp index ffac62157a1..16aab11d5bc 100644 --- a/src/posix/platform/multicast_routing.cpp +++ b/src/posix/platform/multicast_routing.cpp @@ -400,7 +400,7 @@ void MulticastRoutingManager::ExpireMulticastForwardingCache(void) uint64_t now = otPlatTimeGet(); struct mf6cctl mf6cctl; - VerifyOrExit(now >= mLastExpireTime + kMulticastForwardingCacheExpiringInterval * US_PER_S); + VerifyOrExit(now >= mLastExpireTime + kMulticastForwardingCacheExpiringInterval * OT_US_PER_S); mLastExpireTime = now; @@ -409,7 +409,7 @@ void MulticastRoutingManager::ExpireMulticastForwardingCache(void) for (MulticastForwardingCache &mfc : mMulticastForwardingCacheTable) { - if (mfc.IsValid() && mfc.mLastUseTime + kMulticastForwardingCacheExpireTimeout * US_PER_S < now) + if (mfc.IsValid() && mfc.mLastUseTime + kMulticastForwardingCacheExpireTimeout * OT_US_PER_S < now) { if (!UpdateMulticastRouteInfo(mfc)) { diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 822f3f48a3e..177106db7e2 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -127,19 +127,6 @@ void platformAlarmProcess(otInstance *aInstance); */ int32_t platformAlarmGetNext(void); -#ifndef MS_PER_S -#define MS_PER_S 1000 -#endif -#ifndef US_PER_MS -#define US_PER_MS 1000 -#endif -#ifndef US_PER_S -#define US_PER_S (MS_PER_S * US_PER_MS) -#endif -#ifndef NS_PER_US -#define NS_PER_US 1000 -#endif - /** * Advances the alarm time by @p aDelta. * diff --git a/src/posix/platform/radio.cpp b/src/posix/platform/radio.cpp index b76d0aa0892..6bc33c2f005 100644 --- a/src/posix/platform/radio.cpp +++ b/src/posix/platform/radio.cpp @@ -348,11 +348,11 @@ void platformRadioUpdateFdSet(otSysMainloopContext *aContext) { uint64_t remain = deadline - now; - if (remain < (static_cast(aContext->mTimeout.tv_sec) * US_PER_S + + if (remain < (static_cast(aContext->mTimeout.tv_sec) * OT_US_PER_S + static_cast(aContext->mTimeout.tv_usec))) { - aContext->mTimeout.tv_sec = static_cast(remain / US_PER_S); - aContext->mTimeout.tv_usec = static_cast(remain % US_PER_S); + aContext->mTimeout.tv_sec = static_cast(remain / OT_US_PER_S); + aContext->mTimeout.tv_usec = static_cast(remain % OT_US_PER_S); } } else diff --git a/src/posix/platform/spi_interface.cpp b/src/posix/platform/spi_interface.cpp index 2040cd58e62..4c196f19571 100644 --- a/src/posix/platform/spi_interface.cpp +++ b/src/posix/platform/spi_interface.cpp @@ -748,8 +748,8 @@ otError SpiInterface::WaitForFrame(uint64_t aTimeoutUs) int ret; context.mMaxFd = -1; - context.mTimeout.tv_sec = static_cast((end - now) / US_PER_S); - context.mTimeout.tv_usec = static_cast((end - now) % US_PER_S); + context.mTimeout.tv_sec = static_cast((end - now) / OT_US_PER_S); + context.mTimeout.tv_usec = static_cast((end - now) % OT_US_PER_S); FD_ZERO(&context.mReadFdSet); FD_ZERO(&context.mWriteFdSet);