Skip to content

Commit

Permalink
[ESP32] Support total operational hours (#35425)
Browse files Browse the repository at this point in the history
* [ESP32] support for total operational hour

* remove stale TODOs

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
shubhamdp and restyled-commits committed Sep 10, 2024
1 parent 38ede42 commit 45de786
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ESP32Config>::Init();
SuccessOrExit(err);

// TODO: Initialize the global GroupKeyStore object here (#1266)

err = CHIP_NO_ERROR;

exit:
Expand Down
3 changes: 3 additions & 0 deletions src/platform/ESP32/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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);
};

/**
Expand Down

0 comments on commit 45de786

Please sign in to comment.