From 19293142074c4e288163a921bcbf954ec1e2f0f4 Mon Sep 17 00:00:00 2001 From: Michael Rupp <95718139+mykrupp@users.noreply.github.com> Date: Mon, 6 Mar 2023 14:42:59 -0500 Subject: [PATCH] Allow the efr32 lock app to store credentials by type (#25465) * Allow the efr32 lock app to store credentials by type * Restyled by clang-format * update 917 platform * Restyled by whitespace * fix typo --------- Co-authored-by: Restyled.io --- .../silabs/SiWx917/include/LockManager.h | 8 ++--- .../silabs/SiWx917/src/LockManager.cpp | 34 +++++++++---------- .../silabs/efr32/include/LockManager.h | 8 ++--- .../lock-app/silabs/efr32/src/LockManager.cpp | 34 +++++++++---------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/examples/lock-app/silabs/SiWx917/include/LockManager.h b/examples/lock-app/silabs/SiWx917/include/LockManager.h index 70a4ceba2438e9..1158a333a0c54f 100644 --- a/examples/lock-app/silabs/SiWx917/include/LockManager.h +++ b/examples/lock-app/silabs/SiWx917/include/LockManager.h @@ -55,9 +55,9 @@ static constexpr uint8_t kMaxCredentialsPerUser = 10; static constexpr uint8_t kMaxWeekdaySchedulesPerUser = 10; static constexpr uint8_t kMaxYeardaySchedulesPerUser = 10; static constexpr uint8_t kMaxHolidaySchedules = 10; -static constexpr uint8_t kMaxCredentialSize = 8; +static constexpr uint8_t kMaxCredentialSize = 20; +static constexpr uint8_t kNumCredentialTypes = 6; -static constexpr uint8_t kMaxCredentials = kMaxUsers * kMaxCredentialsPerUser; } // namespace ResourceRanges namespace LockInitParams { @@ -200,13 +200,13 @@ class LockManager static void ActuatorMovementTimerEventHandler(AppEvent * aEvent); EmberAfPluginDoorLockUserInfo mLockUsers[kMaxUsers]; - EmberAfPluginDoorLockCredentialInfo mLockCredentials[kMaxCredentials]; + EmberAfPluginDoorLockCredentialInfo mLockCredentials[kNumCredentialTypes][kMaxCredentialsPerUser]; WeekDaysScheduleInfo mWeekdaySchedule[kMaxUsers][kMaxWeekdaySchedulesPerUser]; YearDayScheduleInfo mYeardaySchedule[kMaxUsers][kMaxYeardaySchedulesPerUser]; HolidayScheduleInfo mHolidaySchedule[kMaxHolidaySchedules]; char mUserNames[ArraySize(mLockUsers)][DOOR_LOCK_MAX_USER_NAME_SIZE]; - uint8_t mCredentialData[kMaxCredentials][kMaxCredentialSize]; + uint8_t mCredentialData[kNumCredentialTypes][kMaxCredentialsPerUser][kMaxCredentialSize]; CredentialStruct mCredentials[kMaxUsers][kMaxCredentialsPerUser]; static LockManager sLock; diff --git a/examples/lock-app/silabs/SiWx917/src/LockManager.cpp b/examples/lock-app/silabs/SiWx917/src/LockManager.cpp index 9cb53498289629..1d227128f72258 100644 --- a/examples/lock-app/silabs/SiWx917/src/LockManager.cpp +++ b/examples/lock-app/silabs/SiWx917/src/LockManager.cpp @@ -134,8 +134,9 @@ bool LockManager::ReadConfigValues() SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_LockUser, reinterpret_cast(&mLockUsers), sizeof(EmberAfPluginDoorLockUserInfo) * ArraySize(mLockUsers), outLen); - SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo) * ArraySize(mLockCredentials), outLen); + SilabsConfig::ReadConfigValueBin( + SilabsConfig::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), + sizeof(EmberAfPluginDoorLockCredentialInfo) * LockParams.numberOfCredentialsPerUser * kNumCredentialTypes, outLen); SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_LockUserName, reinterpret_cast(mUserNames), sizeof(mUserNames), outLen); @@ -378,12 +379,9 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip: for (size_t i = 0; i < totalCredentials; ++i) { - mCredentials[userIndex][i] = credentials[i]; - // TODO: Why are we modifying the passed-in credentials? - // https://github.com/project-chip/connectedhomeip/issues/25082 - // For now, preserve pre-existing behavior, which set credentialType to 1. - mCredentials[userIndex][i].credentialType = CredentialTypeEnum::kPin; - mCredentials[userIndex][i].credentialIndex = i + 1; + mCredentials[userIndex][i] = credentials[i]; + mCredentials[userIndex][i].credentialType = credentials[i].credentialType; + mCredentials[userIndex][i].credentialIndex = credentials[i].credentialIndex; } userInStorage.credentials = chip::Span(mCredentials[userIndex], totalCredentials); @@ -420,7 +418,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential ChipLogProgress(Zcl, "Lock App: LockManager::GetCredential [credentialType=%u], credentialIndex=%d", to_underlying(credentialType), credentialIndex); - const auto & credentialInStorage = mLockCredentials[credentialIndex]; + const auto & credentialInStorage = mLockCredentials[to_underlying(credentialType)][credentialIndex]; credential.status = credentialInStorage.status; ChipLogDetail(Zcl, "CredentialStatus: %d, CredentialIndex: %d ", (int) credential.status, credentialIndex); @@ -465,19 +463,21 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential "[credentialStatus=%u,credentialType=%u,credentialDataSize=%u,creator=%d,modifier=%d]", to_underlying(credentialStatus), to_underlying(credentialType), credentialData.size(), creator, modifier); - auto & credentialInStorage = mLockCredentials[credentialIndex]; + auto & credentialInStorage = mLockCredentials[to_underlying(credentialType)][credentialIndex]; credentialInStorage.status = credentialStatus; credentialInStorage.credentialType = credentialType; credentialInStorage.createdBy = creator; credentialInStorage.lastModifiedBy = modifier; - memcpy(mCredentialData[credentialIndex], credentialData.data(), credentialData.size()); - credentialInStorage.credentialData = chip::ByteSpan{ mCredentialData[credentialIndex], credentialData.size() }; + memcpy(mCredentialData[to_underlying(credentialType)][credentialIndex], credentialData.data(), credentialData.size()); + credentialInStorage.credentialData = + chip::ByteSpan{ mCredentialData[to_underlying(credentialType)][credentialIndex], credentialData.size() }; // Save credential information in NVM flash SilabsConfig::WriteConfigValueBin(SilabsConfig::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo) * LockParams.numberOfCredentialsPerUser); + sizeof(EmberAfPluginDoorLockCredentialInfo) * LockParams.numberOfCredentialsPerUser * + kNumCredentialTypes); SilabsConfig::WriteConfigValueBin(SilabsConfig::kConfigKey_CredentialData, reinterpret_cast(&mCredentialData), sizeof(mCredentialData)); @@ -682,15 +682,15 @@ bool LockManager::setLockState(chip::EndpointId endpointId, DlLockState lockStat } // Check the PIN code - for (uint8_t i = 0; i < kMaxCredentials; i++) + for (uint8_t i = 0; i < kMaxCredentialsPerUser; i++) { - if (mLockCredentials[i].credentialType != CredentialTypeEnum::kPin || - mLockCredentials[i].status == DlCredentialStatus::kAvailable) + + if (mLockCredentials[to_underlying(CredentialTypeEnum::kPin)][i].status == DlCredentialStatus::kAvailable) { continue; } - if (mLockCredentials[i].credentialData.data_equal(pin.Value())) + if (mLockCredentials[to_underlying(CredentialTypeEnum::kPin)][i].credentialData.data_equal(pin.Value())) { ChipLogDetail(Zcl, "Lock App: specified PIN code was found in the database, setting lock state to \"%s\" [endpointId=%d]", diff --git a/examples/lock-app/silabs/efr32/include/LockManager.h b/examples/lock-app/silabs/efr32/include/LockManager.h index 018a9ff72c767f..a6177b7100ca62 100644 --- a/examples/lock-app/silabs/efr32/include/LockManager.h +++ b/examples/lock-app/silabs/efr32/include/LockManager.h @@ -55,9 +55,9 @@ static constexpr uint8_t kMaxCredentialsPerUser = 10; static constexpr uint8_t kMaxWeekdaySchedulesPerUser = 10; static constexpr uint8_t kMaxYeardaySchedulesPerUser = 10; static constexpr uint8_t kMaxHolidaySchedules = 10; -static constexpr uint8_t kMaxCredentialSize = 8; +static constexpr uint8_t kMaxCredentialSize = 20; +static constexpr uint8_t kNumCredentialTypes = 6; -static constexpr uint8_t kMaxCredentials = kMaxUsers * kMaxCredentialsPerUser; } // namespace ResourceRanges namespace LockInitParams { @@ -200,13 +200,13 @@ class LockManager static void ActuatorMovementTimerEventHandler(AppEvent * aEvent); EmberAfPluginDoorLockUserInfo mLockUsers[kMaxUsers]; - EmberAfPluginDoorLockCredentialInfo mLockCredentials[kMaxCredentials]; + EmberAfPluginDoorLockCredentialInfo mLockCredentials[kNumCredentialTypes][kMaxCredentialsPerUser]; WeekDaysScheduleInfo mWeekdaySchedule[kMaxUsers][kMaxWeekdaySchedulesPerUser]; YearDayScheduleInfo mYeardaySchedule[kMaxUsers][kMaxYeardaySchedulesPerUser]; HolidayScheduleInfo mHolidaySchedule[kMaxHolidaySchedules]; char mUserNames[ArraySize(mLockUsers)][DOOR_LOCK_MAX_USER_NAME_SIZE]; - uint8_t mCredentialData[kMaxCredentials][kMaxCredentialSize]; + uint8_t mCredentialData[kNumCredentialTypes][kMaxCredentialsPerUser][kMaxCredentialSize]; CredentialStruct mCredentials[kMaxUsers][kMaxCredentialsPerUser]; static LockManager sLock; diff --git a/examples/lock-app/silabs/efr32/src/LockManager.cpp b/examples/lock-app/silabs/efr32/src/LockManager.cpp index 65108746efd27d..51b7f034a27b0d 100644 --- a/examples/lock-app/silabs/efr32/src/LockManager.cpp +++ b/examples/lock-app/silabs/efr32/src/LockManager.cpp @@ -134,8 +134,9 @@ bool LockManager::ReadConfigValues() SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_LockUser, reinterpret_cast(&mLockUsers), sizeof(EmberAfPluginDoorLockUserInfo) * ArraySize(mLockUsers), outLen); - SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo) * ArraySize(mLockCredentials), outLen); + SilabsConfig::ReadConfigValueBin( + SilabsConfig::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), + sizeof(EmberAfPluginDoorLockCredentialInfo) * LockParams.numberOfCredentialsPerUser * kNumCredentialTypes, outLen); SilabsConfig::ReadConfigValueBin(SilabsConfig::kConfigKey_LockUserName, reinterpret_cast(mUserNames), sizeof(mUserNames), outLen); @@ -378,12 +379,9 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip: for (size_t i = 0; i < totalCredentials; ++i) { - mCredentials[userIndex][i] = credentials[i]; - // TODO: Why are we modifying the passed-in credentials? - // https://github.com/project-chip/connectedhomeip/issues/25082 - // For now, preserve pre-existing behavior, which set credentialType to 1. - mCredentials[userIndex][i].credentialType = CredentialTypeEnum::kPin; - mCredentials[userIndex][i].credentialIndex = i + 1; + mCredentials[userIndex][i] = credentials[i]; + mCredentials[userIndex][i].credentialType = credentials[i].credentialType; + mCredentials[userIndex][i].credentialIndex = credentials[i].credentialIndex; } userInStorage.credentials = chip::Span(mCredentials[userIndex], totalCredentials); @@ -420,7 +418,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential ChipLogProgress(Zcl, "Lock App: LockManager::GetCredential [credentialType=%u], credentialIndex=%d", to_underlying(credentialType), credentialIndex); - const auto & credentialInStorage = mLockCredentials[credentialIndex]; + const auto & credentialInStorage = mLockCredentials[to_underlying(credentialType)][credentialIndex]; credential.status = credentialInStorage.status; ChipLogDetail(Zcl, "CredentialStatus: %d, CredentialIndex: %d ", (int) credential.status, credentialIndex); @@ -465,19 +463,21 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential "[credentialStatus=%u,credentialType=%u,credentialDataSize=%u,creator=%d,modifier=%d]", to_underlying(credentialStatus), to_underlying(credentialType), credentialData.size(), creator, modifier); - auto & credentialInStorage = mLockCredentials[credentialIndex]; + auto & credentialInStorage = mLockCredentials[to_underlying(credentialType)][credentialIndex]; credentialInStorage.status = credentialStatus; credentialInStorage.credentialType = credentialType; credentialInStorage.createdBy = creator; credentialInStorage.lastModifiedBy = modifier; - memcpy(mCredentialData[credentialIndex], credentialData.data(), credentialData.size()); - credentialInStorage.credentialData = chip::ByteSpan{ mCredentialData[credentialIndex], credentialData.size() }; + memcpy(mCredentialData[to_underlying(credentialType)][credentialIndex], credentialData.data(), credentialData.size()); + credentialInStorage.credentialData = + chip::ByteSpan{ mCredentialData[to_underlying(credentialType)][credentialIndex], credentialData.size() }; // Save credential information in NVM flash SilabsConfig::WriteConfigValueBin(SilabsConfig::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo) * LockParams.numberOfCredentialsPerUser); + sizeof(EmberAfPluginDoorLockCredentialInfo) * LockParams.numberOfCredentialsPerUser * + kNumCredentialTypes); SilabsConfig::WriteConfigValueBin(SilabsConfig::kConfigKey_CredentialData, reinterpret_cast(&mCredentialData), sizeof(mCredentialData)); @@ -682,15 +682,15 @@ bool LockManager::setLockState(chip::EndpointId endpointId, DlLockState lockStat } // Check the PIN code - for (uint8_t i = 0; i < kMaxCredentials; i++) + for (uint8_t i = 0; i < kMaxCredentialsPerUser; i++) { - if (mLockCredentials[i].credentialType != CredentialTypeEnum::kPin || - mLockCredentials[i].status == DlCredentialStatus::kAvailable) + + if (mLockCredentials[to_underlying(CredentialTypeEnum::kPin)][i].status == DlCredentialStatus::kAvailable) { continue; } - if (mLockCredentials[i].credentialData.data_equal(pin.Value())) + if (mLockCredentials[to_underlying(CredentialTypeEnum::kPin)][i].credentialData.data_equal(pin.Value())) { ChipLogDetail(Zcl, "Lock App: specified PIN code was found in the database, setting lock state to \"%s\" [endpointId=%d]",