Skip to content

Commit

Permalink
[Silabs] Moved nvm RebootCount and TotalOperationalHours keys to conf…
Browse files Browse the repository at this point in the history
…ig (#32917)

* Moved nvm counter vals to config vals

* Removed getter function

* Updated include dir

* Restyled by whitespace

* Restyled by clang-format

* Upaded include directory

* Moved MigrateCounterConfigs() to MigrationManager

* Added MigrationManager

* Updated config key base max val

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Apr 17, 2024
1 parent b49547e commit 1081118
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 90 deletions.
29 changes: 9 additions & 20 deletions examples/platform/silabs/SilabsDeviceAttestationCreds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/Span.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/silabs/MigrationManager.h>
#include <platform/silabs/SilabsConfig.h>
#include <psa/crypto.h>

Expand Down Expand Up @@ -171,18 +172,6 @@ DeviceAttestationCredentialsProvider * GetSilabsDacProvider()

namespace DeviceLayer {
namespace Silabs {
namespace {

void MigrateUint32(uint32_t old_key, uint32_t new_key)
{
uint32_t value = 0;
if (SilabsConfig::ConfigValueExists(old_key) && (CHIP_NO_ERROR == SilabsConfig::ReadConfigValue(old_key, value)))
{
SilabsConfig::WriteConfigValue(new_key, value);
}
}

} // namespace

void MigrateDacProvider(void)
{
Expand All @@ -195,14 +184,14 @@ void MigrateDacProvider(void)
constexpr uint32_t kOldKey_Creds_CD_Offset = SilabsConfigKey(SilabsConfig::kMatterConfig_KeyBase, 0x27);
constexpr uint32_t kOldKey_Creds_CD_Size = SilabsConfigKey(SilabsConfig::kMatterConfig_KeyBase, 0x28);

MigrateUint32(kOldKey_Creds_KeyId, SilabsConfig::kConfigKey_Creds_KeyId);
MigrateUint32(kOldKey_Creds_Base_Addr, SilabsConfig::kConfigKey_Creds_Base_Addr);
MigrateUint32(kOldKey_Creds_DAC_Offset, SilabsConfig::kConfigKey_Creds_DAC_Offset);
MigrateUint32(kOldKey_Creds_DAC_Size, SilabsConfig::kConfigKey_Creds_DAC_Size);
MigrateUint32(kOldKey_Creds_PAI_Offset, SilabsConfig::kConfigKey_Creds_PAI_Offset);
MigrateUint32(kOldKey_Creds_PAI_Size, SilabsConfig::kConfigKey_Creds_PAI_Size);
MigrateUint32(kOldKey_Creds_CD_Offset, SilabsConfig::kConfigKey_Creds_CD_Offset);
MigrateUint32(kOldKey_Creds_CD_Size, SilabsConfig::kConfigKey_Creds_CD_Size);
MigrationManager::MigrateUint32(kOldKey_Creds_KeyId, SilabsConfig::kConfigKey_Creds_KeyId);
MigrationManager::MigrateUint32(kOldKey_Creds_Base_Addr, SilabsConfig::kConfigKey_Creds_Base_Addr);
MigrationManager::MigrateUint32(kOldKey_Creds_DAC_Offset, SilabsConfig::kConfigKey_Creds_DAC_Offset);
MigrationManager::MigrateUint32(kOldKey_Creds_DAC_Size, SilabsConfig::kConfigKey_Creds_DAC_Size);
MigrationManager::MigrateUint32(kOldKey_Creds_PAI_Offset, SilabsConfig::kConfigKey_Creds_PAI_Offset);
MigrationManager::MigrateUint32(kOldKey_Creds_PAI_Size, SilabsConfig::kConfigKey_Creds_PAI_Size);
MigrationManager::MigrateUint32(kOldKey_Creds_CD_Offset, SilabsConfig::kConfigKey_Creds_CD_Offset);
MigrationManager::MigrateUint32(kOldKey_Creds_CD_Size, SilabsConfig::kConfigKey_Creds_CD_Size);
}

} // namespace Silabs
Expand Down
15 changes: 1 addition & 14 deletions src/platform/silabs/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetUpTime(uint64_t & upTime)

CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours)
{
uint64_t upTime = 0;

if (GetUpTime(upTime) == CHIP_NO_ERROR)
{
uint32_t totalHours = 0;
if (ConfigurationMgr().GetTotalOperationalHours(totalHours) == CHIP_NO_ERROR)
{
VerifyOrReturnError(upTime / 3600 <= UINT32_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE);
totalOperationalHours = totalHours + static_cast<uint32_t>(upTime / 3600);
return CHIP_NO_ERROR;
}
}

return CHIP_ERROR_INVALID_TIME;
return ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours);
}

CHIP_ERROR DiagnosticDataProviderImpl::GetActiveHardwareFaults(GeneralFaults<kMaxHardwareFaults> & hardwareFaults)
Expand Down
25 changes: 24 additions & 1 deletion src/platform/silabs/MigrationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ typedef struct

#define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
static migrationData_t migrationTable[] = {
{ .migrationGroup = 1, .migrationFunc = MigrateKvsMap }, { .migrationGroup = 2, .migrationFunc = MigrateDacProvider },
{ .migrationGroup = 1, .migrationFunc = MigrateKvsMap },
{ .migrationGroup = 2, .migrationFunc = MigrateDacProvider },
{ .migrationGroup = 3, .migrationFunc = MigrateCounterConfigs },
// add any additional migration neccesary. migrationGroup should stay equal if done in the same commit or increment by 1 for
// each new entry.
};
Expand All @@ -60,6 +62,27 @@ void MigrationManager::applyMigrations()
}
SilabsConfig::WriteConfigValue(SilabsConfig::kConfigKey_MigrationCounter, completedMigrationGroup);
}
void MigrationManager::MigrateUint32(uint32_t old_key, uint32_t new_key)
{
uint32_t value = 0;
if (SilabsConfig::ConfigValueExists(old_key) && (CHIP_NO_ERROR == SilabsConfig::ReadConfigValue(old_key, value)))
{
if (CHIP_NO_ERROR == SilabsConfig::WriteConfigValue(new_key, value))
{
// Free memory of old key location
SilabsConfig::ClearConfigValue(old_key);
}
}
}

void MigrateCounterConfigs(void)
{
constexpr uint32_t kOldConfigKey_BootCount = SilabsConfigKey(SilabsConfig::kMatterCounter_KeyBase, 0x00);
constexpr uint32_t kOldConfigKey_TotalOperationalHours = SilabsConfigKey(SilabsConfig::kMatterCounter_KeyBase, 0x01);

MigrationManager::MigrateUint32(kOldConfigKey_BootCount, SilabsConfig::kConfigKey_BootCount);
MigrationManager::MigrateUint32(kOldConfigKey_TotalOperationalHours, SilabsConfig::kConfigKey_TotalOperationalHours);
}

MigrationManager & MigrationManager::GetMigrationInstance()
{
Expand Down
2 changes: 2 additions & 0 deletions src/platform/silabs/MigrationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MigrationManager
*/
static MigrationManager & GetMigrationInstance();
static void applyMigrations();
static void MigrateUint32(uint32_t old_key, uint32_t new_key);

private:
MigrationManager(){};
Expand All @@ -43,6 +44,7 @@ class MigrationManager
*/
void MigrateKvsMap(void);
void MigrateDacProvider(void);
void MigrateCounterConfigs(void);

} // namespace Silabs
} // namespace DeviceLayer
Expand Down
4 changes: 4 additions & 0 deletions src/platform/silabs/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
System::Clock::Timestamp GetStartTime() { return mStartTime; }

private:
// ===== Members for internal use

static void UpdateOperationalHours(System::Layer * systemLayer, void * appState);

// ===== Methods that implement the PlatformManager abstract interface.

CHIP_ERROR _InitChipStack(void);
Expand Down
28 changes: 14 additions & 14 deletions src/platform/silabs/SiWx917/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "AppConfig.h"
#include "FreeRTOS.h"

using namespace chip::DeviceLayer::Internal;

namespace chip {
namespace DeviceLayer {

Expand Down Expand Up @@ -111,32 +113,30 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
err = Internal::GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>::_InitChipStack();
SuccessOrExit(err);

// Start timer to increment TotalOperationalHours every hour
SystemLayer().StartTimer(System::Clock::Seconds32(kSecondsPerHour), UpdateOperationalHours, NULL);

exit:
return err;
}

void PlatformManagerImpl::_Shutdown()
void PlatformManagerImpl::UpdateOperationalHours(System::Layer * systemLayer, void * appState)
{
uint64_t upTime = 0;
uint32_t totalOperationalHours = 0;

if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR)
if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR)
{
uint32_t totalOperationalHours = 0;

if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR)
{
ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + static_cast<uint32_t>(upTime / 3600));
}
else
{
ChipLogError(DeviceLayer, "Failed to get total operational hours of the Node");
}
ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + 1);
}
else
{
ChipLogError(DeviceLayer, "Failed to get current uptime since the Node’s last reboot");
ChipLogError(DeviceLayer, "Failed to get total operational hours of the Node");
}

SystemLayer().StartTimer(System::Clock::Seconds32(kSecondsPerHour), UpdateOperationalHours, NULL);
}
void PlatformManagerImpl::_Shutdown()
{
Internal::GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>::_Shutdown();
}
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
Expand Down
54 changes: 27 additions & 27 deletions src/platform/silabs/SilabsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,37 @@ class SilabsConfig
static constexpr Key kConfigKey_Creds_CD_Size = SilabsConfigKey(kMatterFactory_KeyBase, 0x27);
static constexpr Key kConfigKey_Test_Event_Trigger_Key = SilabsConfigKey(kMatterFactory_KeyBase, 0x28);
// Matter Config Keys
static constexpr Key kConfigKey_ServiceConfig = SilabsConfigKey(kMatterConfig_KeyBase, 0x01);
static constexpr Key kConfigKey_PairedAccountId = SilabsConfigKey(kMatterConfig_KeyBase, 0x02);
static constexpr Key kConfigKey_ServiceId = SilabsConfigKey(kMatterConfig_KeyBase, 0x03);
static constexpr Key kConfigKey_LastUsedEpochKeyId = SilabsConfigKey(kMatterConfig_KeyBase, 0x05);
static constexpr Key kConfigKey_FailSafeArmed = SilabsConfigKey(kMatterConfig_KeyBase, 0x06);
static constexpr Key kConfigKey_GroupKey = SilabsConfigKey(kMatterConfig_KeyBase, 0x07);
static constexpr Key kConfigKey_HardwareVersion = SilabsConfigKey(kMatterConfig_KeyBase, 0x08);
static constexpr Key kConfigKey_RegulatoryLocation = SilabsConfigKey(kMatterConfig_KeyBase, 0x09);
static constexpr Key kConfigKey_CountryCode = SilabsConfigKey(kMatterConfig_KeyBase, 0x0A);
static constexpr Key kConfigKey_WiFiSSID = SilabsConfigKey(kMatterConfig_KeyBase, 0x0C);
static constexpr Key kConfigKey_WiFiPSK = SilabsConfigKey(kMatterConfig_KeyBase, 0x0D);
static constexpr Key kConfigKey_WiFiSEC = SilabsConfigKey(kMatterConfig_KeyBase, 0x0E);
static constexpr Key kConfigKey_GroupKeyBase = SilabsConfigKey(kMatterConfig_KeyBase, 0x0F);
static constexpr Key kConfigKey_LockUser = SilabsConfigKey(kMatterConfig_KeyBase, 0x10);
static constexpr Key kConfigKey_Credential = SilabsConfigKey(kMatterConfig_KeyBase, 0x11);
static constexpr Key kConfigKey_LockUserName = SilabsConfigKey(kMatterConfig_KeyBase, 0x12);
static constexpr Key kConfigKey_CredentialData = SilabsConfigKey(kMatterConfig_KeyBase, 0x13);
static constexpr Key kConfigKey_UserCredentials = SilabsConfigKey(kMatterConfig_KeyBase, 0x14);
static constexpr Key kConfigKey_WeekDaySchedules = SilabsConfigKey(kMatterConfig_KeyBase, 0x15);
static constexpr Key kConfigKey_YearDaySchedules = SilabsConfigKey(kMatterConfig_KeyBase, 0x16);
static constexpr Key kConfigKey_HolidaySchedules = SilabsConfigKey(kMatterConfig_KeyBase, 0x17);
static constexpr Key kConfigKey_OpKeyMap = SilabsConfigKey(kMatterConfig_KeyBase, 0x20);
static constexpr Key kConfigKey_ServiceConfig = SilabsConfigKey(kMatterConfig_KeyBase, 0x01);
static constexpr Key kConfigKey_PairedAccountId = SilabsConfigKey(kMatterConfig_KeyBase, 0x02);
static constexpr Key kConfigKey_ServiceId = SilabsConfigKey(kMatterConfig_KeyBase, 0x03);
static constexpr Key kConfigKey_LastUsedEpochKeyId = SilabsConfigKey(kMatterConfig_KeyBase, 0x05);
static constexpr Key kConfigKey_FailSafeArmed = SilabsConfigKey(kMatterConfig_KeyBase, 0x06);
static constexpr Key kConfigKey_GroupKey = SilabsConfigKey(kMatterConfig_KeyBase, 0x07);
static constexpr Key kConfigKey_HardwareVersion = SilabsConfigKey(kMatterConfig_KeyBase, 0x08);
static constexpr Key kConfigKey_RegulatoryLocation = SilabsConfigKey(kMatterConfig_KeyBase, 0x09);
static constexpr Key kConfigKey_CountryCode = SilabsConfigKey(kMatterConfig_KeyBase, 0x0A);
static constexpr Key kConfigKey_WiFiSSID = SilabsConfigKey(kMatterConfig_KeyBase, 0x0C);
static constexpr Key kConfigKey_WiFiPSK = SilabsConfigKey(kMatterConfig_KeyBase, 0x0D);
static constexpr Key kConfigKey_WiFiSEC = SilabsConfigKey(kMatterConfig_KeyBase, 0x0E);
static constexpr Key kConfigKey_GroupKeyBase = SilabsConfigKey(kMatterConfig_KeyBase, 0x0F);
static constexpr Key kConfigKey_LockUser = SilabsConfigKey(kMatterConfig_KeyBase, 0x10);
static constexpr Key kConfigKey_Credential = SilabsConfigKey(kMatterConfig_KeyBase, 0x11);
static constexpr Key kConfigKey_LockUserName = SilabsConfigKey(kMatterConfig_KeyBase, 0x12);
static constexpr Key kConfigKey_CredentialData = SilabsConfigKey(kMatterConfig_KeyBase, 0x13);
static constexpr Key kConfigKey_UserCredentials = SilabsConfigKey(kMatterConfig_KeyBase, 0x14);
static constexpr Key kConfigKey_WeekDaySchedules = SilabsConfigKey(kMatterConfig_KeyBase, 0x15);
static constexpr Key kConfigKey_YearDaySchedules = SilabsConfigKey(kMatterConfig_KeyBase, 0x16);
static constexpr Key kConfigKey_HolidaySchedules = SilabsConfigKey(kMatterConfig_KeyBase, 0x17);
static constexpr Key kConfigKey_OpKeyMap = SilabsConfigKey(kMatterConfig_KeyBase, 0x20);
static constexpr Key kConfigKey_BootCount = SilabsConfigKey(kMatterConfig_KeyBase, 0x21);
static constexpr Key kConfigKey_TotalOperationalHours = SilabsConfigKey(kMatterConfig_KeyBase, 0x22);

static constexpr Key kConfigKey_GroupKeyMax =
SilabsConfigKey(kMatterConfig_KeyBase, 0x1E); // Allows 16 Group Keys to be created.

// Matter Counter Keys
static constexpr Key kConfigKey_BootCount = SilabsConfigKey(kMatterCounter_KeyBase, 0x00);
static constexpr Key kConfigKey_TotalOperationalHours = SilabsConfigKey(kMatterCounter_KeyBase, 0x01);
static constexpr Key kConfigKey_LifeTimeCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x02);
static constexpr Key kConfigKey_MigrationCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x03);
static constexpr Key kConfigKey_LifeTimeCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x02);
static constexpr Key kConfigKey_MigrationCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x03);

// Matter KVS storage Keys
static constexpr Key kConfigKey_KvsStringKeyMap = SilabsConfigKey(kMatterKvs_KeyBase, 0x00);
Expand All @@ -164,7 +164,7 @@ class SilabsConfig
static constexpr Key kMinConfigKey_MatterFactory = SilabsConfigKey(kMatterFactory_KeyBase, 0x00);
static constexpr Key kMaxConfigKey_MatterFactory = SilabsConfigKey(kMatterFactory_KeyBase, 0x2F);
static constexpr Key kMinConfigKey_MatterConfig = SilabsConfigKey(kMatterConfig_KeyBase, 0x00);
static constexpr Key kMaxConfigKey_MatterConfig = SilabsConfigKey(kMatterConfig_KeyBase, 0x20);
static constexpr Key kMaxConfigKey_MatterConfig = SilabsConfigKey(kMatterConfig_KeyBase, 0x22);

// Allows 32 Counters to be created.
static constexpr Key kMinConfigKey_MatterCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x00);
Expand Down
28 changes: 14 additions & 14 deletions src/platform/silabs/efr32/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "AppConfig.h"
#include "FreeRTOS.h"

using namespace chip::DeviceLayer::Internal;

namespace chip {
namespace DeviceLayer {

Expand All @@ -62,32 +64,30 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
err = Internal::GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>::_InitChipStack();
SuccessOrExit(err);

// Start timer to increment TotalOperationalHours every hour
SystemLayer().StartTimer(System::Clock::Seconds32(kSecondsPerHour), UpdateOperationalHours, NULL);

exit:
return err;
}

void PlatformManagerImpl::_Shutdown()
void PlatformManagerImpl::UpdateOperationalHours(System::Layer * systemLayer, void * appState)
{
uint64_t upTime = 0;
uint32_t totalOperationalHours = 0;

if (GetDiagnosticDataProvider().GetUpTime(upTime) == CHIP_NO_ERROR)
if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR)
{
uint32_t totalOperationalHours = 0;

if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR)
{
ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + static_cast<uint32_t>(upTime / 3600));
}
else
{
ChipLogError(DeviceLayer, "Failed to get total operational hours of the Node");
}
ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours + 1);
}
else
{
ChipLogError(DeviceLayer, "Failed to get current uptime since the Node’s last reboot");
ChipLogError(DeviceLayer, "Failed to get total operational hours of the Node");
}

SystemLayer().StartTimer(System::Clock::Seconds32(kSecondsPerHour), UpdateOperationalHours, NULL);
}
void PlatformManagerImpl::_Shutdown()
{
Internal::GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>::_Shutdown();
}
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
Expand Down

0 comments on commit 1081118

Please sign in to comment.