From 1b84405407b88ced9085be729079e4b11e6dd716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Baczma=C5=84ski?= Date: Thu, 30 Jan 2025 11:15:07 +0100 Subject: [PATCH] [nrf fromtree] [Zephyr] Save total operational hours on Read (#37259) Currently total operational hours are saved to NVM with `CONFIG_CHIP_OPERATIONAL_TIME_SAVE_INTERVAL`. Certification tests require checking the value after 1 hour, restarting the DUT, and checking if the value has not changed, causing failures if given config is higher than 1. This commit additionaly saves total operational hours to NVM everytime it is being read by `GetTotalOperationalHours` (writing only if currently stored value should be updated). This way value is stored with minimum frequency of 1 hour (if read request is sent frequently) and maximum frequency of `CONFIG_CHIP_OPERATIONAL_TIME_SAVE_INTERVAL`. (cherry picked from commit 89e823e5d8065ce85a6e6b4c205e11db413c4535) --- .../Zephyr/DiagnosticDataProviderImpl.cpp | 15 +------ src/platform/Zephyr/PlatformManagerImpl.cpp | 40 +++++++++++-------- src/platform/Zephyr/PlatformManagerImpl.h | 3 +- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp index 3c0a106aea..25267da8b2 100644 --- a/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp +++ b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp @@ -216,19 +216,8 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetUpTime(uint64_t & upTime) CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours) { - uint64_t upTimeS; - - ReturnErrorOnFailure(GetUpTime(upTimeS)); - - uint64_t totalHours = 0; - const uint32_t upTimeH = upTimeS / 3600 < UINT32_MAX ? static_cast(upTimeS / 3600) : UINT32_MAX; - const uint64_t deltaTime = upTimeH - PlatformMgrImpl().GetSavedOperationalHoursSinceBoot(); - - ReturnErrorOnFailure(ConfigurationMgr().GetTotalOperationalHours(reinterpret_cast(totalHours))); - - totalOperationalHours = static_cast(totalHours + deltaTime < UINT32_MAX ? totalHours + deltaTime : UINT32_MAX); - - return CHIP_NO_ERROR; + // Update the total operational hours and get the most recent value. + return PlatformMgrImpl().UpdateOperationalHours(&totalOperationalHours); } CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(BootReasonType & bootReason) diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index df8a40a55d..def109c7ae 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -76,33 +76,41 @@ static int app_entropy_source(void * data, unsigned char * output, size_t len, s void PlatformManagerImpl::OperationalHoursSavingTimerEventHandler(k_timer * timer) { - PlatformMgr().ScheduleWork(UpdateOperationalHours); + PlatformMgr().ScheduleWork([](intptr_t arg) { + CHIP_ERROR error = sInstance.UpdateOperationalHours(nullptr); + if (error != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to update operational hours: %" CHIP_ERROR_FORMAT, error.Format()); + } + }); } -void PlatformManagerImpl::UpdateOperationalHours(intptr_t arg) +CHIP_ERROR PlatformManagerImpl::UpdateOperationalHours(uint32_t * totalOperationalHours) { uint64_t upTimeS; - if (GetDiagnosticDataProvider().GetUpTime(upTimeS) != CHIP_NO_ERROR) - { - ChipLogError(DeviceLayer, "Failed to get up time of the node"); - return; - } + ReturnErrorOnFailure(GetDiagnosticDataProvider().GetUpTime(upTimeS)); + + uint32_t totalTime = 0; + const uint32_t upTimeH = upTimeS / 3600 < UINT32_MAX ? static_cast(upTimeS / 3600) : UINT32_MAX; + const uint64_t deltaTime = upTimeH - mSavedOperationalHoursSinceBoot; + + ReturnErrorOnFailure(ConfigurationMgr().GetTotalOperationalHours(totalTime)); - uint64_t totalOperationalHours = 0; - const uint32_t upTimeH = upTimeS / 3600 < UINT32_MAX ? static_cast(upTimeS / 3600) : UINT32_MAX; - const uint64_t deltaTime = upTimeH - sInstance.mSavedOperationalHoursSinceBoot; + totalTime = totalTime + deltaTime < UINT32_MAX ? static_cast(totalTime + deltaTime) : UINT32_MAX; - if (ConfigurationMgr().GetTotalOperationalHours(reinterpret_cast(totalOperationalHours)) == CHIP_NO_ERROR) + if (deltaTime > 0) { - ConfigurationMgr().StoreTotalOperationalHours( - static_cast(totalOperationalHours + deltaTime < UINT32_MAX ? totalOperationalHours + deltaTime : UINT32_MAX)); - sInstance.mSavedOperationalHoursSinceBoot = upTimeH; + ConfigurationMgr().StoreTotalOperationalHours(totalTime); + mSavedOperationalHoursSinceBoot = upTimeH; } - else + + if (totalOperationalHours != nullptr) { - ChipLogError(DeviceLayer, "Failed to get total operational hours of the node"); + *totalOperationalHours = totalTime; } + + return CHIP_NO_ERROR; } CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) diff --git a/src/platform/Zephyr/PlatformManagerImpl.h b/src/platform/Zephyr/PlatformManagerImpl.h index ca13a45127..d82b8b310a 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.h +++ b/src/platform/Zephyr/PlatformManagerImpl.h @@ -47,7 +47,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener // ===== Platform-specific members that may be accessed directly by the application. System::Clock::Timestamp GetStartTime() { return mStartTime; } - uint32_t GetSavedOperationalHoursSinceBoot() { return mSavedOperationalHoursSinceBoot; } + CHIP_ERROR UpdateOperationalHours(uint32_t * totalOperationalHours); private: // ===== Methods that implement the PlatformManager abstract interface. @@ -55,7 +55,6 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener CHIP_ERROR _InitChipStack(void); static void OperationalHoursSavingTimerEventHandler(k_timer * timer); - static void UpdateOperationalHours(intptr_t arg); // ===== Members for internal use by the following friends.