From e96ddd98305cfe275f9ac27fc7360c51fdc9b5de Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Fri, 6 Sep 2024 18:36:24 +0530 Subject: [PATCH] [ESP32] Support total operational hours (#35425) * [ESP32] support for total operational hour * remove stale TODOs * Restyled by clang-format --------- Co-authored-by: Restyled.io --- .../ESP32/ConfigurationManagerImpl.cpp | 40 ++++++++++++++++--- src/platform/ESP32/ConfigurationManagerImpl.h | 3 ++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/platform/ESP32/ConfigurationManagerImpl.cpp b/src/platform/ESP32/ConfigurationManagerImpl.cpp index e03ec70ce8192b..31c7cb714f96c0 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.cpp +++ b/src/platform/ESP32/ConfigurationManagerImpl.cpp @@ -45,14 +45,26 @@ namespace DeviceLayer { using namespace ::chip::DeviceLayer::Internal; -// TODO: Define a Singleton instance of CHIP Group Key Store here (#1266) - ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance() { static ConfigurationManagerImpl sInstance; return sInstance; } +uint32_t ConfigurationManagerImpl::mTotalOperationalHours = 0; + +void ConfigurationManagerImpl::TotalOperationalHoursTimerCallback(TimerHandle_t timer) +{ + mTotalOperationalHours++; + + CHIP_ERROR err = ConfigurationMgrImpl().StoreTotalOperationalHours(mTotalOperationalHours); + + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to store total operational hours: %" CHIP_ERROR_FORMAT, err.Format()); + } +} + CHIP_ERROR ConfigurationManagerImpl::Init() { CHIP_ERROR err; @@ -161,18 +173,34 @@ CHIP_ERROR ConfigurationManagerImpl::Init() SuccessOrExit(err); } - if (!ESP32Config::ConfigValueExists(ESP32Config::kCounterKey_TotalOperationalHours)) + if (CHIP_NO_ERROR != GetTotalOperationalHours(mTotalOperationalHours)) { - err = StoreTotalOperationalHours(0); + err = StoreTotalOperationalHours(mTotalOperationalHours); SuccessOrExit(err); } + { + // Start a timer which reloads every one hour and bumps the total operational hours + TickType_t reloadPeriod = (1000 * 60 * 60) / portTICK_PERIOD_MS; + TimerHandle_t timerHandle = xTimerCreate("tOpHrs", reloadPeriod, pdPASS, nullptr, TotalOperationalHoursTimerCallback); + if (timerHandle == nullptr) + { + err = CHIP_ERROR_NO_MEMORY; + ExitNow(ChipLogError(DeviceLayer, "total operational hours Timer creation failed")); + } + + BaseType_t timerStartStatus = xTimerStart(timerHandle, 0); + if (timerStartStatus == pdFAIL) + { + err = CHIP_ERROR_INTERNAL; + ExitNow(ChipLogError(DeviceLayer, "total operational hours Timer start failed")); + } + } + // Initialize the generic implementation base class. err = Internal::GenericConfigurationManagerImpl::Init(); SuccessOrExit(err); - // TODO: Initialize the global GroupKeyStore object here (#1266) - err = CHIP_NO_ERROR; exit: diff --git a/src/platform/ESP32/ConfigurationManagerImpl.h b/src/platform/ESP32/ConfigurationManagerImpl.h index 1416df35d93b65..63b3e094763a75 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.h +++ b/src/platform/ESP32/ConfigurationManagerImpl.h @@ -98,6 +98,9 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp // ===== Private members reserved for use by this class only. static void DoFactoryReset(intptr_t arg); + + static uint32_t mTotalOperationalHours; + static void TotalOperationalHoursTimerCallback(TimerHandle_t timer); }; /**