From 079852b67e9b42107c7d2b82f79db385660a5a2f Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 25 Mar 2025 13:03:43 -0700 Subject: [PATCH] [uptime] enforce `UPTIME` feature for MTD and FTD builds (#11354) This commit makes `OPENTHREAD_CONFIG_UPTIME_ENABLE` mandatory for FTD and MTD builds. This requirement is now explicitly enforced in the `uptime.hpp` header file. Consequently, this configuration is only applicable for RADIO/RCP builds. This commit also removes unnecessary `#if` checks for this configuration within core modules used in MTD or FTD builds. The OpenThread API and CLI command documentation are updated accordingly. --- include/openthread/instance.h | 2 +- include/openthread/thread.h | 7 --- include/openthread/thread_ftd.h | 2 +- src/cli/README.md | 3 -- src/cli/cli.cpp | 49 ++++++++----------- src/core/api/thread_api.cpp | 2 - src/core/border_router/routing_manager.hpp | 4 -- src/core/common/uptime.hpp | 4 +- src/core/config/misc.h | 4 ++ src/core/meshcop/border_agent.hpp | 4 -- src/core/thread/child.cpp | 2 - src/core/thread/mle.cpp | 10 ---- src/core/thread/mle.hpp | 6 --- src/core/thread/neighbor.cpp | 8 +-- src/core/thread/neighbor.hpp | 4 -- .../platform/openthread-core-posix-config.h | 6 +-- 16 files changed, 30 insertions(+), 87 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index b2ab4c3f0..7a9dbb7bb 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -52,7 +52,7 @@ extern "C" { * * @note This number versions both OpenThread platform and user APIs. */ -#define OPENTHREAD_API_VERSION (486) +#define OPENTHREAD_API_VERSION (487) /** * @addtogroup api-instance diff --git a/include/openthread/thread.h b/include/openthread/thread.h index 173fa0920..8be4cec6e 100644 --- a/include/openthread/thread.h +++ b/include/openthread/thread.h @@ -178,11 +178,6 @@ typedef struct otMleCounters uint16_t mBetterPartitionAttachAttempts; ///< Number of attempts to attach to a better partition. uint16_t mBetterParentAttachAttempts; ///< Number of attempts to attach to find a better parent (parent search). - /** - * Role time tracking. - * - * When uptime feature is enabled (OPENTHREAD_CONFIG_UPTIME_ENABLE = 1) time spent in each MLE role is tracked. - */ uint64_t mDisabledTime; ///< Number of milliseconds device has been in OT_DEVICE_ROLE_DISABLED role. uint64_t mDetachedTime; ///< Number of milliseconds device has been in OT_DEVICE_ROLE_DETACHED role. uint64_t mChildTime; ///< Number of milliseconds device has been in OT_DEVICE_ROLE_CHILD role. @@ -934,8 +929,6 @@ void otThreadResetMleCounters(otInstance *aInstance); /** * Gets the current attach duration (number of seconds since the device last attached). * - * Requires the `OPENTHREAD_CONFIG_UPTIME_ENABLE` feature. - * * If the device is not currently attached, zero will be returned. * * Unlike the role-tracking variables in `otMleCounters`, which track the cumulative time the device is in each role, diff --git a/include/openthread/thread_ftd.h b/include/openthread/thread_ftd.h index dbe06c341..4571a0bf1 100644 --- a/include/openthread/thread_ftd.h +++ b/include/openthread/thread_ftd.h @@ -57,7 +57,7 @@ typedef struct otExtAddress mExtAddress; ///< IEEE 802.15.4 Extended Address uint32_t mTimeout; ///< Timeout uint32_t mAge; ///< Seconds since last heard - uint64_t mConnectionTime; ///< Seconds since attach (requires `OPENTHREAD_CONFIG_UPTIME_ENABLE`) + uint64_t mConnectionTime; ///< Seconds since attach uint16_t mRloc16; ///< RLOC16 uint16_t mChildId; ///< Child ID uint8_t mNetworkDataVersion; ///< Network Data Version diff --git a/src/cli/README.md b/src/cli/README.md index 319d0b994..92ebf1621 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -143,8 +143,6 @@ Done Prints the attach time (duration since device was last attached). -Requires `OPENTHREAD_CONFIG_UPTIME_ENABLE`. - Duration is formatted as `{hh}:{mm}:{ss}` for hours, minutes, and seconds if it is less than one day. If the duration is longer than one day, the format is `{dd}d.{hh}:{mm}:{ss}`. ```bash @@ -1103,7 +1101,6 @@ Get the counter value. Note: -- `OPENTHREAD_CONFIG_UPTIME_ENABLE` is required for MLE role time tracking in `counters mle` - `OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE` is required for `counters br` ```bash diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 79f34728b..b1ec724a1 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -410,7 +410,6 @@ otError Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLen #if OPENTHREAD_FTD || OPENTHREAD_MTD -#if OPENTHREAD_CONFIG_UPTIME_ENABLE /** * @cli attachtime * @code @@ -419,7 +418,7 @@ otError Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLen * Done * @endcode * @par - * Prints the current attach time (duration since device was last attached). Requires `OPENTHREAD_CONFIG_UPTIME_ENABLE`. + * Prints the current attach time (duration since device was last attached). * Duration is formatted as `{hh}:{mm}:{ss}` for hours, minutes, and seconds if it is less than one day. If the * duration is longer than one day, the format is `{dd}d.{hh}:{mm}:{ss}`. */ @@ -436,7 +435,6 @@ template <> otError Interpreter::Process(Arg aArgs[]) exit: return error; } -#endif #if OPENTHREAD_CONFIG_HISTORY_TRACKER_ENABLE template <> otError Interpreter::Process(Arg aArgs[]) { return mHistory.Process(aArgs); } @@ -2459,6 +2457,12 @@ template <> otError Interpreter::Process(Arg aArgs[]) const char *mName; }; + struct MleTimeCounterName + { + const uint64_t otMleCounters::*mValuePtr; + const char *mName; + }; + static const MleCounterName kCounterNames[] = { {&otMleCounters::mDisabledRole, "Role Disabled"}, {&otMleCounters::mDetachedRole, "Role Detached"}, @@ -2472,36 +2476,27 @@ template <> otError Interpreter::Process(Arg aArgs[]) {&otMleCounters::mParentChanges, "Parent Changes"}, }; + static const MleTimeCounterName kTimeCounterNames[] = { + {&otMleCounters::mDisabledTime, "Disabled"}, {&otMleCounters::mDetachedTime, "Detached"}, + {&otMleCounters::mChildTime, "Child"}, {&otMleCounters::mRouterTime, "Router"}, + {&otMleCounters::mLeaderTime, "Leader"}, + }; + const otMleCounters *mleCounters = otThreadGetMleCounters(GetInstancePtr()); for (const MleCounterName &counter : kCounterNames) { OutputLine("%s: %u", counter.mName, mleCounters->*counter.mValuePtr); } -#if OPENTHREAD_CONFIG_UPTIME_ENABLE + + for (const MleTimeCounterName &counter : kTimeCounterNames) { - struct MleTimeCounterName - { - const uint64_t otMleCounters::*mValuePtr; - const char *mName; - }; - - static const MleTimeCounterName kTimeCounterNames[] = { - {&otMleCounters::mDisabledTime, "Disabled"}, {&otMleCounters::mDetachedTime, "Detached"}, - {&otMleCounters::mChildTime, "Child"}, {&otMleCounters::mRouterTime, "Router"}, - {&otMleCounters::mLeaderTime, "Leader"}, - }; - - for (const MleTimeCounterName &counter : kTimeCounterNames) - { - OutputFormat("Time %s Milli: ", counter.mName); - OutputUint64Line(mleCounters->*counter.mValuePtr); - } - - OutputFormat("Time Tracked Milli: "); - OutputUint64Line(mleCounters->mTrackedTime); + OutputFormat("Time %s Milli: ", counter.mName); + OutputUint64Line(mleCounters->*counter.mValuePtr); } -#endif + + OutputFormat("Time Tracked Milli: "); + OutputUint64Line(mleCounters->mTrackedTime); } /** * @cli counters mle reset @@ -4570,7 +4565,6 @@ template <> otError Interpreter::Process(Arg aArgs[]) OutputLine("| %5lu |", ToUlong(neighborInfo.mAge)); } } -#if OPENTHREAD_CONFIG_UPTIME_ENABLE /** * @cli neighbor conntime * @code @@ -4649,7 +4643,6 @@ template <> otError Interpreter::Process(Arg aArgs[]) } } } -#endif else { error = OT_ERROR_INVALID_ARGS; @@ -8149,9 +8142,7 @@ otError Interpreter::ProcessCommand(Arg aArgs[]) static constexpr Command kCommands[] = { #if OPENTHREAD_FTD || OPENTHREAD_MTD -#if OPENTHREAD_CONFIG_UPTIME_ENABLE CmdEntry("attachtime"), -#endif #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE CmdEntry("ba"), #endif diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index 0f89e0afa..9065ea449 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -475,12 +475,10 @@ const otMleCounters *otThreadGetMleCounters(otInstance *aInstance) void otThreadResetMleCounters(otInstance *aInstance) { AsCoreType(aInstance).Get().ResetCounters(); } -#if OPENTHREAD_CONFIG_UPTIME_ENABLE uint32_t otThreadGetCurrentAttachDuration(otInstance *aInstance) { return AsCoreType(aInstance).Get().GetCurrentAttachDuration(); } -#endif #if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE void otThreadRegisterParentResponseCallback(otInstance *aInstance, diff --git a/src/core/border_router/routing_manager.hpp b/src/core/border_router/routing_manager.hpp index 086e1e12f..d96a3a07d 100644 --- a/src/core/border_router/routing_manager.hpp +++ b/src/core/border_router/routing_manager.hpp @@ -46,10 +46,6 @@ #error "OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE is required for OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE." #endif -#if !OPENTHREAD_CONFIG_UPTIME_ENABLE -#error "OPENTHREAD_CONFIG_UPTIME_ENABLE is required for OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE" -#endif - #if OPENTHREAD_CONFIG_BORDER_ROUTING_TRACK_PEER_BR_INFO_ENABLE && !OPENTHREAD_CONFIG_BORDER_ROUTING_USE_HEAP_ENABLE #error "TRACK_PEER_BR_INFO_ENABLE feature requires OPENTHREAD_CONFIG_BORDER_ROUTING_USE_HEAP_ENABLE" #endif diff --git a/src/core/common/uptime.hpp b/src/core/common/uptime.hpp index f238f11d7..3b74a66c1 100644 --- a/src/core/common/uptime.hpp +++ b/src/core/common/uptime.hpp @@ -36,8 +36,8 @@ #include "openthread-core-config.h" -#if !OPENTHREAD_CONFIG_UPTIME_ENABLE && OPENTHREAD_FTD -#error "OPENTHREAD_CONFIG_UPTIME_ENABLE is required for FTD" +#if (OPENTHREAD_FTD || OPENTHREAD_MTD) && !OPENTHREAD_CONFIG_UPTIME_ENABLE +#error "OPENTHREAD_CONFIG_UPTIME_ENABLE is required for FTD or MTD builds" #endif #if OPENTHREAD_CONFIG_UPTIME_ENABLE diff --git a/src/core/config/misc.h b/src/core/config/misc.h index 7fb9139d7..f7e986830 100644 --- a/src/core/config/misc.h +++ b/src/core/config/misc.h @@ -117,6 +117,10 @@ * @def OPENTHREAD_CONFIG_UPTIME_ENABLE * * Define to 1 to enable tracking the uptime of OpenThread instance. + * + * On FTD/MTD builds this feature is now mandatory and MUST be enabled. This config is therefore only applicable for + * RADIO/RCP builds. + * */ #ifndef OPENTHREAD_CONFIG_UPTIME_ENABLE #define OPENTHREAD_CONFIG_UPTIME_ENABLE (OPENTHREAD_FTD || OPENTHREAD_MTD) diff --git a/src/core/meshcop/border_agent.hpp b/src/core/meshcop/border_agent.hpp index d289af216..1b036d5e3 100644 --- a/src/core/meshcop/border_agent.hpp +++ b/src/core/meshcop/border_agent.hpp @@ -66,10 +66,6 @@ namespace MeshCoP { #error "Border Agent feature requires `OPENTHREAD_CONFIG_SECURE_TRANSPORT_ENABLE`" #endif -#if !OPENTHREAD_CONFIG_UPTIME_ENABLE -#error "Border Agent feature requires `OPENTHREAD_CONFIG_UPTIME_ENABLE`" -#endif - class BorderAgent : public InstanceLocator, private NonCopyable { #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE diff --git a/src/core/thread/child.cpp b/src/core/thread/child.cpp index 28a1b4b1d..be2f2ca86 100644 --- a/src/core/thread/child.cpp +++ b/src/core/thread/child.cpp @@ -68,9 +68,7 @@ void Child::Info::SetFrom(const Child &aChild) #else mIsCslSynced = false; #endif -#if OPENTHREAD_CONFIG_UPTIME_ENABLE mConnectionTime = aChild.GetConnectionTime(); -#endif } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index d831452b7..bb975222f 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -243,9 +243,7 @@ exit: const Counters &Mle::GetCounters(void) { -#if OPENTHREAD_CONFIG_UPTIME_ENABLE UpdateRoleTimeCounters(mRole); -#endif return mCounters; } @@ -253,13 +251,9 @@ const Counters &Mle::GetCounters(void) void Mle::ResetCounters(void) { ClearAllBytes(mCounters); -#if OPENTHREAD_CONFIG_UPTIME_ENABLE mLastUpdatedTimestamp = Get().GetUptime(); -#endif } -#if OPENTHREAD_CONFIG_UPTIME_ENABLE - uint32_t Mle::GetCurrentAttachDuration(void) const { return IsAttached() ? Uptime::MsecToSec(Get().GetUptime()) - mLastAttachTime : 0; @@ -294,8 +288,6 @@ void Mle::UpdateRoleTimeCounters(DeviceRole aRole) } } -#endif // OPENTHREAD_CONFIG_UPTIME_ENABLE - void Mle::SetRole(DeviceRole aRole) { DeviceRole oldRole = mRole; @@ -304,14 +296,12 @@ void Mle::SetRole(DeviceRole aRole) LogNote("Role %s -> %s", RoleToString(oldRole), RoleToString(mRole)); -#if OPENTHREAD_CONFIG_UPTIME_ENABLE if ((oldRole == kRoleDetached) && IsAttached()) { mLastAttachTime = Uptime::MsecToSec(Get().GetUptime()); } UpdateRoleTimeCounters(oldRole); -#endif switch (mRole) { diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 7405109ec..a688a0e56 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -605,14 +605,12 @@ public: */ void ResetCounters(void); -#if OPENTHREAD_CONFIG_UPTIME_ENABLE /** * Determines the current attach duration (number of seconds since the device last attached). * * @returns Current attach duration in seconds. */ uint32_t GetCurrentAttachDuration(void) const; -#endif #if OPENTHREAD_CONFIG_MLE_PARENT_RESPONSE_CALLBACK_API_ENABLE /** @@ -1389,9 +1387,7 @@ private: void InformPreviousParent(void); #endif -#if OPENTHREAD_CONFIG_UPTIME_ENABLE void UpdateRoleTimeCounters(DeviceRole aRole); -#endif #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE ServiceAloc *FindInServiceAlocs(uint16_t aAloc16); @@ -1500,10 +1496,8 @@ private: #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE uint32_t mCslTimeout; #endif -#if OPENTHREAD_CONFIG_UPTIME_ENABLE uint32_t mLastAttachTime; uint64_t mLastUpdatedTimestamp; -#endif uint64_t mAlternateTimestamp; LeaderData mLeaderData; diff --git a/src/core/thread/neighbor.cpp b/src/core/thread/neighbor.cpp index 804ed4cfa..98c513c5b 100644 --- a/src/core/thread/neighbor.cpp +++ b/src/core/thread/neighbor.cpp @@ -42,23 +42,19 @@ void Neighbor::SetState(State aState) VerifyOrExit(mState != aState); mState = static_cast(aState); -#if OPENTHREAD_CONFIG_UPTIME_ENABLE if (mState == kStateValid) { mConnectionStart = Uptime::MsecToSec(Get().GetUptime()); } -#endif exit: return; } -#if OPENTHREAD_CONFIG_UPTIME_ENABLE uint32_t Neighbor::GetConnectionTime(void) const { return IsStateValid() ? Uptime::MsecToSec(Get().GetUptime()) - mConnectionStart : 0; } -#endif bool Neighbor::AddressMatcher::Matches(const Neighbor &aNeighbor) const { @@ -101,9 +97,7 @@ void Neighbor::Info::SetFrom(const Neighbor &aNeighbor) mFullThreadDevice = aNeighbor.IsFullThreadDevice(); mFullNetworkData = (aNeighbor.GetNetworkDataType() == NetworkData::kFullSet); mVersion = aNeighbor.GetVersion(); -#if OPENTHREAD_CONFIG_UPTIME_ENABLE - mConnectionTime = aNeighbor.GetConnectionTime(); -#endif + mConnectionTime = aNeighbor.GetConnectionTime(); } void Neighbor::Init(Instance &aInstance) diff --git a/src/core/thread/neighbor.hpp b/src/core/thread/neighbor.hpp index ddb4fa5a2..65480acf6 100644 --- a/src/core/thread/neighbor.hpp +++ b/src/core/thread/neighbor.hpp @@ -597,14 +597,12 @@ public: */ const Mle::TxChallenge &GetChallenge(void) const { return mValidPending.mPending.mChallenge; } -#if OPENTHREAD_CONFIG_UPTIME_ENABLE /** * Returns the connection time (in seconds) of the neighbor (seconds since entering `kStateValid`). * * @returns The connection time (in seconds), zero if device is not currently in `kStateValid`. */ uint32_t GetConnectionTime(void) const; -#endif #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE /** @@ -762,9 +760,7 @@ private: // and this neighbor is the Subject. LinkMetrics::Metrics mEnhAckProbingMetrics; #endif -#if OPENTHREAD_CONFIG_UPTIME_ENABLE uint32_t mConnectionStart; -#endif }; DefineCoreType(otNeighborInfo, Neighbor::Info); diff --git a/src/posix/platform/openthread-core-posix-config.h b/src/posix/platform/openthread-core-posix-config.h index 582117494..352646a1c 100644 --- a/src/posix/platform/openthread-core-posix-config.h +++ b/src/posix/platform/openthread-core-posix-config.h @@ -136,12 +136,8 @@ #define OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE 640 #endif -#ifndef OPENTHREAD_CONFIG_UPTIME_ENABLE -#define OPENTHREAD_CONFIG_UPTIME_ENABLE !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE -#endif - #ifndef OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME -#define OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME 1 +#define OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE #endif #ifndef OPENTHREAD_CONFIG_SRP_CLIENT_BUFFERS_MAX_SERVICES