Skip to content

Commit

Permalink
[nrf fromtree] [Zephyr] Save total operational hours on Read (#37259)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
maciejbaczmanski committed Jan 31, 2025
1 parent c63ff3f commit 60d9439
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
15 changes: 2 additions & 13 deletions src/platform/Zephyr/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(upTimeS / 3600) : UINT32_MAX;
const uint64_t deltaTime = upTimeH - PlatformMgrImpl().GetSavedOperationalHoursSinceBoot();

ReturnErrorOnFailure(ConfigurationMgr().GetTotalOperationalHours(reinterpret_cast<uint32_t &>(totalHours)));

totalOperationalHours = static_cast<uint32_t>(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)
Expand Down
40 changes: 24 additions & 16 deletions src/platform/Zephyr/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(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<uint32_t>(upTimeS / 3600) : UINT32_MAX;
const uint64_t deltaTime = upTimeH - sInstance.mSavedOperationalHoursSinceBoot;
totalTime = totalTime + deltaTime < UINT32_MAX ? static_cast<uint32_t>(totalTime + deltaTime) : UINT32_MAX;

if (ConfigurationMgr().GetTotalOperationalHours(reinterpret_cast<uint32_t &>(totalOperationalHours)) == CHIP_NO_ERROR)
if (deltaTime > 0)
{
ConfigurationMgr().StoreTotalOperationalHours(
static_cast<uint32_t>(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)
Expand Down
3 changes: 1 addition & 2 deletions src/platform/Zephyr/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ 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.

CHIP_ERROR _InitChipStack(void);

static void OperationalHoursSavingTimerEventHandler(k_timer * timer);
static void UpdateOperationalHours(intptr_t arg);

// ===== Members for internal use by the following friends.

Expand Down

0 comments on commit 60d9439

Please sign in to comment.