From 1081118e29c47c47cb4c4819658a64ca4d13e543 Mon Sep 17 00:00:00 2001 From: Curtis Rahman <111998767+CuRahman@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:40:53 -0400 Subject: [PATCH] [Silabs] Moved nvm RebootCount and TotalOperationalHours keys to config (#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 --- .../silabs/SilabsDeviceAttestationCreds.cpp | 29 ++++------ .../silabs/DiagnosticDataProviderImpl.cpp | 15 +----- src/platform/silabs/MigrationManager.cpp | 25 ++++++++- src/platform/silabs/MigrationManager.h | 2 + src/platform/silabs/PlatformManagerImpl.h | 4 ++ .../silabs/SiWx917/PlatformManagerImpl.cpp | 28 +++++----- src/platform/silabs/SilabsConfig.h | 54 +++++++++---------- .../silabs/efr32/PlatformManagerImpl.cpp | 28 +++++----- 8 files changed, 95 insertions(+), 90 deletions(-) diff --git a/examples/platform/silabs/SilabsDeviceAttestationCreds.cpp b/examples/platform/silabs/SilabsDeviceAttestationCreds.cpp index bc69573c13286b..810529c26399ab 100644 --- a/examples/platform/silabs/SilabsDeviceAttestationCreds.cpp +++ b/examples/platform/silabs/SilabsDeviceAttestationCreds.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -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) { @@ -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 diff --git a/src/platform/silabs/DiagnosticDataProviderImpl.cpp b/src/platform/silabs/DiagnosticDataProviderImpl.cpp index 03afa5efe29487..02668206e26662 100644 --- a/src/platform/silabs/DiagnosticDataProviderImpl.cpp +++ b/src/platform/silabs/DiagnosticDataProviderImpl.cpp @@ -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(upTime / 3600); - return CHIP_NO_ERROR; - } - } - - return CHIP_ERROR_INVALID_TIME; + return ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours); } CHIP_ERROR DiagnosticDataProviderImpl::GetActiveHardwareFaults(GeneralFaults & hardwareFaults) diff --git a/src/platform/silabs/MigrationManager.cpp b/src/platform/silabs/MigrationManager.cpp index e07b730e67f048..f78facc938471b 100644 --- a/src/platform/silabs/MigrationManager.cpp +++ b/src/platform/silabs/MigrationManager.cpp @@ -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. }; @@ -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() { diff --git a/src/platform/silabs/MigrationManager.h b/src/platform/silabs/MigrationManager.h index 708fdb3368feef..69afbf47ac7562 100644 --- a/src/platform/silabs/MigrationManager.h +++ b/src/platform/silabs/MigrationManager.h @@ -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(){}; @@ -43,6 +44,7 @@ class MigrationManager */ void MigrateKvsMap(void); void MigrateDacProvider(void); +void MigrateCounterConfigs(void); } // namespace Silabs } // namespace DeviceLayer diff --git a/src/platform/silabs/PlatformManagerImpl.h b/src/platform/silabs/PlatformManagerImpl.h index 7d6eef7cd90ed3..4a0bfdb52c1e81 100644 --- a/src/platform/silabs/PlatformManagerImpl.h +++ b/src/platform/silabs/PlatformManagerImpl.h @@ -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); diff --git a/src/platform/silabs/SiWx917/PlatformManagerImpl.cpp b/src/platform/silabs/SiWx917/PlatformManagerImpl.cpp index d14947d65af3f3..c3db4afb2510f5 100644 --- a/src/platform/silabs/SiWx917/PlatformManagerImpl.cpp +++ b/src/platform/silabs/SiWx917/PlatformManagerImpl.cpp @@ -41,6 +41,8 @@ #include "AppConfig.h" #include "FreeRTOS.h" +using namespace chip::DeviceLayer::Internal; + namespace chip { namespace DeviceLayer { @@ -111,32 +113,30 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) err = Internal::GenericPlatformManagerImpl_FreeRTOS::_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(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::_Shutdown(); } #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION diff --git a/src/platform/silabs/SilabsConfig.h b/src/platform/silabs/SilabsConfig.h index 3bd5abfba1b8f3..92a879177530c7 100644 --- a/src/platform/silabs/SilabsConfig.h +++ b/src/platform/silabs/SilabsConfig.h @@ -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); @@ -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); diff --git a/src/platform/silabs/efr32/PlatformManagerImpl.cpp b/src/platform/silabs/efr32/PlatformManagerImpl.cpp index 25de6d51d164b2..2ebc64d77babc5 100644 --- a/src/platform/silabs/efr32/PlatformManagerImpl.cpp +++ b/src/platform/silabs/efr32/PlatformManagerImpl.cpp @@ -37,6 +37,8 @@ #include "AppConfig.h" #include "FreeRTOS.h" +using namespace chip::DeviceLayer::Internal; + namespace chip { namespace DeviceLayer { @@ -62,32 +64,30 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) err = Internal::GenericPlatformManagerImpl_FreeRTOS::_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(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::_Shutdown(); } #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION