From 81eaec132b488d96c82714e3d73e9abb4f1f55a8 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 23 Nov 2023 19:47:35 +0200 Subject: [PATCH] [Telink] Update the set credentials part in LockManager --- .../telink/include/LockSettingsStorage.h | 5 +- examples/lock-app/telink/src/LockManager.cpp | 52 ++++++++++++------- .../telink/src/LockSettingsStorage.cpp | 22 +++++++- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/examples/lock-app/telink/include/LockSettingsStorage.h b/examples/lock-app/telink/include/LockSettingsStorage.h index 5cb1773eea4040..fd2c7939139327 100644 --- a/examples/lock-app/telink/include/LockSettingsStorage.h +++ b/examples/lock-app/telink/include/LockSettingsStorage.h @@ -18,6 +18,7 @@ #include <../Zephyr/ZephyrConfig.h> #include +#include #if LOCK_MANAGER_CONFIG_USE_NVM_CREDENTIAL_STORAGE namespace chip { @@ -28,13 +29,13 @@ class LockSettingsStorage : ZephyrConfig { public: static const ZephyrConfig::Key kConfigKey_LockUser; - static const ZephyrConfig::Key kConfigKey_Credential; static const ZephyrConfig::Key kConfigKey_LockUserName; - static const ZephyrConfig::Key kConfigKey_CredentialData; static const ZephyrConfig::Key kConfigKey_UserCredentials; static const ZephyrConfig::Key kConfigKey_WeekDaySchedules; static const ZephyrConfig::Key kConfigKey_YearDaySchedules; static const ZephyrConfig::Key kConfigKey_HolidaySchedules; + static const char * kConfigKey_Credential[kNumCredentialTypes]; + static const char * kConfigKey_CredentialData[kNumCredentialTypes]; }; } // namespace Internal } // namespace DeviceLayer diff --git a/examples/lock-app/telink/src/LockManager.cpp b/examples/lock-app/telink/src/LockManager.cpp index 4a7ee03c1a6bb6..5acc4b400801e3 100644 --- a/examples/lock-app/telink/src/LockManager.cpp +++ b/examples/lock-app/telink/src/LockManager.cpp @@ -426,15 +426,9 @@ bool LockManager::ReadConfigValues() ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_LockUser, reinterpret_cast(&mLockUsers), sizeof(EmberAfPluginDoorLockUserInfo) * ArraySize(mLockUsers), outLen); - ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials * kNumCredentialTypes, outLen); - ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_LockUserName, reinterpret_cast(mUserNames), sizeof(mUserNames), outLen); - ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_CredentialData, reinterpret_cast(mCredentialData), - sizeof(mCredentialData), outLen); - ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_UserCredentials, reinterpret_cast(mCredentials), sizeof(CredentialStruct) * LockParams.numberOfUsers * LockParams.numberOfCredentialsPerUser, outLen); @@ -453,6 +447,15 @@ bool LockManager::ReadConfigValues() reinterpret_cast(&(mHolidaySchedule)), sizeof(EmberAfPluginDoorLockHolidaySchedule) * LockParams.numberOfHolidaySchedules, outLen); + for (uint8_t i = 0; i < kNumCredentialTypes; i++) + { + ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_Credential[i], reinterpret_cast(&mLockCredentials[i]), + sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials, outLen); + + ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_CredentialData[i], reinterpret_cast(mCredentialData[i]), + kMaxCredentials * kMaxCredentialSize, outLen); + } + return true; } #endif @@ -654,20 +657,31 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential chip::ByteSpan{ mCredentialData[to_underlying(credentialType)][credentialIndex], credentialData.size() }; #if LOCK_MANAGER_CONFIG_USE_NVM_CREDENTIAL_STORAGE - // Save credential information in NVM flash - CHIP_ERROR err = ZephyrConfig::WriteConfigValueBin( - LockSettingsStorage::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials * kNumCredentialTypes); - if (err != CHIP_NO_ERROR) - ChipLogError( - Zcl, "Failed to write kConfigKey_Credential. User data will be resetted during reboot. Not enough storage space \n"); + CHIP_ERROR err; - err = ZephyrConfig::WriteConfigValueBin(LockSettingsStorage::kConfigKey_CredentialData, - reinterpret_cast(&mCredentialData), sizeof(mCredentialData)); - if (err != CHIP_NO_ERROR) - ChipLogError( - Zcl, - "Failed to write kConfigKey_CredentialData. User data will be resetted during reboot. Not enough storage space \n"); + for (uint8_t i = 0; i < kNumCredentialTypes; i++) + { + // Save credential information in NVM flash + err = ZephyrConfig::WriteConfigValueBin( + LockSettingsStorage::kConfigKey_Credential[i], reinterpret_cast(&mLockCredentials[i]), + sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Zcl, + "Failed to write kConfigKey_Credential(%d). User data will be resetted during reboot. Not enough storage space \n", i); + break; + } + + err = ZephyrConfig::WriteConfigValueBin( + LockSettingsStorage::kConfigKey_CredentialData[i],reinterpret_cast(&mCredentialData[i]), + kMaxCredentials * kMaxCredentialSize); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Zcl, + "Failed to write kConfigKey_CredentialData(%d). User data will be resetted during reboot. Not enough storage space \n", i); + break; + } + } #endif ChipLogProgress(Zcl, "Successfully set the credential [credentialType=%u]", to_underlying(credentialType)); diff --git a/examples/lock-app/telink/src/LockSettingsStorage.cpp b/examples/lock-app/telink/src/LockSettingsStorage.cpp index abe83ced65357e..17c8af936c9d93 100644 --- a/examples/lock-app/telink/src/LockSettingsStorage.cpp +++ b/examples/lock-app/telink/src/LockSettingsStorage.cpp @@ -33,16 +33,34 @@ namespace Internal { (key); \ static_assert(sizeof(key) <= SETTINGS_MAX_NAME_LEN, "Config key too long: " key) +#define CONFIG_KEY_CREDENTIAL(type) (NAMESPACE_CONFIG "credential-" type) +#define CONFIG_KEY_CREDENTIAL_DATA(type) (NAMESPACE_CONFIG "credential-" type "-data") + #define NAMESPACE_CONFIG CHIP_DEVICE_CONFIG_SETTINGS_KEY "/cfg/" const ZephyrConfig::Key LockSettingsStorage::kConfigKey_LockUser = CONFIG_KEY(NAMESPACE_CONFIG "lock-user"); -const ZephyrConfig::Key LockSettingsStorage::kConfigKey_Credential = CONFIG_KEY(NAMESPACE_CONFIG "credential"); const ZephyrConfig::Key LockSettingsStorage::kConfigKey_LockUserName = CONFIG_KEY(NAMESPACE_CONFIG "lock-user-name"); -const ZephyrConfig::Key LockSettingsStorage::kConfigKey_CredentialData = CONFIG_KEY(NAMESPACE_CONFIG "credential-data"); const ZephyrConfig::Key LockSettingsStorage::kConfigKey_UserCredentials = CONFIG_KEY(NAMESPACE_CONFIG "user-credentials"); const ZephyrConfig::Key LockSettingsStorage::kConfigKey_WeekDaySchedules = CONFIG_KEY(NAMESPACE_CONFIG "week-day-schedules"); const ZephyrConfig::Key LockSettingsStorage::kConfigKey_YearDaySchedules = CONFIG_KEY(NAMESPACE_CONFIG "year-day-schedules"); const ZephyrConfig::Key LockSettingsStorage::kConfigKey_HolidaySchedules = CONFIG_KEY(NAMESPACE_CONFIG "holiday-schedules"); +const char * LockSettingsStorage::kConfigKey_Credential[kNumCredentialTypes] = { + CONFIG_KEY_CREDENTIAL("programming-pin"), + CONFIG_KEY_CREDENTIAL("pin"), + CONFIG_KEY_CREDENTIAL("rfid"), + CONFIG_KEY_CREDENTIAL("finger-print"), + CONFIG_KEY_CREDENTIAL("finger-vein"), + CONFIG_KEY_CREDENTIAL("face") +}; +const char * LockSettingsStorage::kConfigKey_CredentialData[kNumCredentialTypes] = { + CONFIG_KEY_CREDENTIAL_DATA("programming-pin"), + CONFIG_KEY_CREDENTIAL_DATA("pin"), + CONFIG_KEY_CREDENTIAL_DATA("rfid"), + CONFIG_KEY_CREDENTIAL_DATA("finger-print"), + CONFIG_KEY_CREDENTIAL_DATA("finger-vein"), + CONFIG_KEY_CREDENTIAL_DATA("face") +}; + } // namespace Internal } // namespace DeviceLayer } // namespace chip