Skip to content

Commit 5aadb38

Browse files
shubhamdprestyled-commits
andcommittedSep 12, 2024
[ESP32] Support total operational hours (#35425)
* [ESP32] support for total operational hour * remove stale TODOs * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 6f112ce commit 5aadb38

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed
 

‎src/platform/ESP32/ConfigurationManagerImpl.cpp

+34-6
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,26 @@ namespace DeviceLayer {
4545

4646
using namespace ::chip::DeviceLayer::Internal;
4747

48-
// TODO: Define a Singleton instance of CHIP Group Key Store here (#1266)
49-
5048
ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance()
5149
{
5250
static ConfigurationManagerImpl sInstance;
5351
return sInstance;
5452
}
5553

54+
uint32_t ConfigurationManagerImpl::mTotalOperationalHours = 0;
55+
56+
void ConfigurationManagerImpl::TotalOperationalHoursTimerCallback(TimerHandle_t timer)
57+
{
58+
mTotalOperationalHours++;
59+
60+
CHIP_ERROR err = ConfigurationMgrImpl().StoreTotalOperationalHours(mTotalOperationalHours);
61+
62+
if (err != CHIP_NO_ERROR)
63+
{
64+
ChipLogError(DeviceLayer, "Failed to store total operational hours: %" CHIP_ERROR_FORMAT, err.Format());
65+
}
66+
}
67+
5668
CHIP_ERROR ConfigurationManagerImpl::Init()
5769
{
5870
CHIP_ERROR err;
@@ -161,18 +173,34 @@ CHIP_ERROR ConfigurationManagerImpl::Init()
161173
SuccessOrExit(err);
162174
}
163175

164-
if (!ESP32Config::ConfigValueExists(ESP32Config::kCounterKey_TotalOperationalHours))
176+
if (CHIP_NO_ERROR != GetTotalOperationalHours(mTotalOperationalHours))
165177
{
166-
err = StoreTotalOperationalHours(0);
178+
err = StoreTotalOperationalHours(mTotalOperationalHours);
167179
SuccessOrExit(err);
168180
}
169181

182+
{
183+
// Start a timer which reloads every one hour and bumps the total operational hours
184+
TickType_t reloadPeriod = (1000 * 60 * 60) / portTICK_PERIOD_MS;
185+
TimerHandle_t timerHandle = xTimerCreate("tOpHrs", reloadPeriod, pdPASS, nullptr, TotalOperationalHoursTimerCallback);
186+
if (timerHandle == nullptr)
187+
{
188+
err = CHIP_ERROR_NO_MEMORY;
189+
ExitNow(ChipLogError(DeviceLayer, "total operational hours Timer creation failed"));
190+
}
191+
192+
BaseType_t timerStartStatus = xTimerStart(timerHandle, 0);
193+
if (timerStartStatus == pdFAIL)
194+
{
195+
err = CHIP_ERROR_INTERNAL;
196+
ExitNow(ChipLogError(DeviceLayer, "total operational hours Timer start failed"));
197+
}
198+
}
199+
170200
// Initialize the generic implementation base class.
171201
err = Internal::GenericConfigurationManagerImpl<ESP32Config>::Init();
172202
SuccessOrExit(err);
173203

174-
// TODO: Initialize the global GroupKeyStore object here (#1266)
175-
176204
err = CHIP_NO_ERROR;
177205

178206
exit:

‎src/platform/ESP32/ConfigurationManagerImpl.h

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
9696
// ===== Private members reserved for use by this class only.
9797

9898
static void DoFactoryReset(intptr_t arg);
99+
100+
static uint32_t mTotalOperationalHours;
101+
static void TotalOperationalHoursTimerCallback(TimerHandle_t timer);
99102
};
100103

101104
/**

0 commit comments

Comments
 (0)