Skip to content

Commit 82748b9

Browse files
[v1.3][ESP32] Support total operational hours (#35425) (#35501)
* [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> * fix ci failure --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 6f112ce commit 82748b9

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-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

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
#include <platform/ESP32/ESP32Config.h>
3737

38+
#include <freertos/FreeRTOS.h>
39+
#include <freertos/timers.h>
40+
3841
namespace chip {
3942
namespace DeviceLayer {
4043

@@ -96,6 +99,9 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
9699
// ===== Private members reserved for use by this class only.
97100

98101
static void DoFactoryReset(intptr_t arg);
102+
103+
static uint32_t mTotalOperationalHours;
104+
static void TotalOperationalHoursTimerCallback(TimerHandle_t timer);
99105
};
100106

101107
/**

src/test_driver/esp32/sdkconfig.defaults

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=32768
3131

3232
#enable BT
3333
CONFIG_BT_ENABLED=y
34+
CONFIG_BT_NIMBLE_ENABLED=y
3435

3536
#enable HKDF in mbedtls
3637
CONFIG_MBEDTLS_HKDF_C=y

0 commit comments

Comments
 (0)