Skip to content

Commit

Permalink
[v1.3][ESP32] Support total operational hours (#35425) (#35501)
Browse files Browse the repository at this point in the history
* [ESP32] Support total operational hours (#35425)

* [ESP32] support for total operational hour

* remove stale TODOs

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>

* fix ci failure

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
shubhamdp and restyled-commits authored Sep 12, 2024
1 parent 6f112ce commit 82748b9
Show file tree
Hide file tree
Showing 3 changed files with 41 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
6 changes: 6 additions & 0 deletions src/platform/ESP32/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

#include <platform/ESP32/ESP32Config.h>

#include <freertos/FreeRTOS.h>
#include <freertos/timers.h>

namespace chip {
namespace DeviceLayer {

Expand Down Expand Up @@ -96,6 +99,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
1 change: 1 addition & 0 deletions src/test_driver/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=32768

#enable BT
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y

#enable HKDF in mbedtls
CONFIG_MBEDTLS_HKDF_C=y
Expand Down

0 comments on commit 82748b9

Please sign in to comment.