From e818d35751bb570aa52a70ffd0807afc2f18c20b Mon Sep 17 00:00:00 2001 From: Serhii Salamakha Date: Thu, 14 Dec 2023 00:52:41 +0200 Subject: [PATCH] [Telink] Update Lock App and flash overlay (#30745) * [Telink] Update the set credentials part in LockManager * [Telink] Update storage partition size * Restyled by clang-format * [Telink] Move SETTINGS_NVS_SECTOR_COUNT to common config --------- Co-authored-by: Restyled.io Co-authored-by: Alex Tsitsiura --- config/telink/chip-module/Kconfig.defaults | 4 +++ .../telink/include/LockSettingsStorage.h | 5 +-- examples/lock-app/telink/src/LockManager.cpp | 36 +++++++++++++------ .../telink/src/LockSettingsStorage.cpp | 26 +++++++++----- src/platform/telink/1m_flash.overlay | 10 +++--- src/platform/telink/2m_flash.overlay | 14 ++++---- src/platform/telink/4m_flash.overlay | 14 ++++---- 7 files changed, 69 insertions(+), 40 deletions(-) diff --git a/config/telink/chip-module/Kconfig.defaults b/config/telink/chip-module/Kconfig.defaults index 54137bc281562b..c4ccd625b9cda7 100644 --- a/config/telink/chip-module/Kconfig.defaults +++ b/config/telink/chip-module/Kconfig.defaults @@ -209,6 +209,10 @@ config NVS_LOOKUP_CACHE_SIZE config SETTINGS_NVS_SECTOR_SIZE_MULT default 1 +# Set sector counter of NVS +config SETTINGS_NVS_SECTOR_COUNT + default 10 + # Enable OpenThread config NET_L2_OPENTHREAD 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..529e1aa557fa7c 100644 --- a/examples/lock-app/telink/src/LockManager.cpp +++ b/examples/lock-app/telink/src/LockManager.cpp @@ -120,6 +120,7 @@ bool LockManager::LockAction(int32_t appSource, Action_t aAction, OperationSourc } else { + status = true; LOG_INF("Lock Action: Lock is already locked. No action performed"); } break; @@ -181,6 +182,7 @@ bool LockManager::LockAction(int32_t appSource, Action_t aAction, OperationSourc } else { + status = true; LOG_INF("Unlock Action: Lock is already unlocked. no action performed"); } break; @@ -205,6 +207,7 @@ bool LockManager::LockAction(int32_t appSource, Action_t aAction, OperationSourc } else { + status = true; LOG_INF("Unbolt Action: Lock is already in unbolt state. no action performed"); } break; @@ -244,6 +247,7 @@ bool LockManager::LockAction(int32_t appSource, Action_t aAction, OperationSourc } else { + status = true; LOG_INF("Lock Action: Lock is already locked. No action performed"); } break; @@ -302,6 +306,7 @@ bool LockManager::LockAction(int32_t appSource, Action_t aAction, OperationSourc } else { + status = true; LOG_INF("Unlock Action: Lock is already unlocked. no action performed"); } break; @@ -325,6 +330,7 @@ bool LockManager::LockAction(int32_t appSource, Action_t aAction, OperationSourc } else { + status = true; LOG_INF("Unbolt Action: Lock is already in unbolt state. no action performed"); } break; @@ -426,15 +432,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 +453,17 @@ 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,16 +665,19 @@ 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 + CHIP_ERROR err; + // Save credential information in NVM flash - CHIP_ERROR err = ZephyrConfig::WriteConfigValueBin( - LockSettingsStorage::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials * kNumCredentialTypes); + err = ZephyrConfig::WriteConfigValueBin(LockSettingsStorage::kConfigKey_Credential[to_underlying(credentialType)], + reinterpret_cast(&mLockCredentials[to_underlying(credentialType)]), + sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials); if (err != CHIP_NO_ERROR) ChipLogError( Zcl, "Failed to write kConfigKey_Credential. User data will be resetted during reboot. Not enough storage space \n"); - err = ZephyrConfig::WriteConfigValueBin(LockSettingsStorage::kConfigKey_CredentialData, - reinterpret_cast(&mCredentialData), sizeof(mCredentialData)); + err = ZephyrConfig::WriteConfigValueBin(LockSettingsStorage::kConfigKey_CredentialData[to_underlying(credentialType)], + reinterpret_cast(&mCredentialData[to_underlying(credentialType)]), + kMaxCredentials * kMaxCredentialSize); if (err != CHIP_NO_ERROR) ChipLogError( Zcl, diff --git a/examples/lock-app/telink/src/LockSettingsStorage.cpp b/examples/lock-app/telink/src/LockSettingsStorage.cpp index abe83ced65357e..73b88e2c593dea 100644 --- a/examples/lock-app/telink/src/LockSettingsStorage.cpp +++ b/examples/lock-app/telink/src/LockSettingsStorage.cpp @@ -33,16 +33,26 @@ 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 ZephyrConfig::Key LockSettingsStorage::kConfigKey_LockUser = CONFIG_KEY(NAMESPACE_CONFIG "lock-user"); +const ZephyrConfig::Key LockSettingsStorage::kConfigKey_LockUserName = CONFIG_KEY(NAMESPACE_CONFIG "lock-user-name"); +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 diff --git a/src/platform/telink/1m_flash.overlay b/src/platform/telink/1m_flash.overlay index 698d7721d5012c..0b1fd1b2213c23 100644 --- a/src/platform/telink/1m_flash.overlay +++ b/src/platform/telink/1m_flash.overlay @@ -21,15 +21,15 @@ }; slot0_partition: partition@19000 { label = "image-0"; - reg = <0x19000 0xDC000>; + reg = <0x19000 0xda000>; }; - factory_partition: partition@F5000 { + factory_partition: partition@f3000 { label = "factory-data"; - reg = <0xF5000 0x1000>; + reg = <0xf3000 0x1000>; }; - storage_partition: partition@F6000 { + storage_partition: partition@f4000 { label = "storage"; - reg = <0xF6000 0x8000>; + reg = <0xf4000 0xa000>; }; vendor_partition: partition@fe000 { label = "vendor-data"; diff --git a/src/platform/telink/2m_flash.overlay b/src/platform/telink/2m_flash.overlay index 8a12e9c02aad02..95f1a749057436 100644 --- a/src/platform/telink/2m_flash.overlay +++ b/src/platform/telink/2m_flash.overlay @@ -21,19 +21,19 @@ }; slot0_partition: partition@19000 { label = "image-0"; - reg = <0x19000 0xee000>; + reg = <0x19000 0xed000>; }; - factory_partition: partition@107000 { + factory_partition: partition@106000{ label = "factory-data"; - reg = <0x107000 0x1000>; + reg = <0x106000 0x1000>; }; - storage_partition: partition@108000 { + storage_partition: partition@107000 { label = "storage"; - reg = <0x108000 0x8000>; + reg = <0x107000 0xa000>; }; - slot1_partition: partition@110000 { + slot1_partition: partition@111000 { label = "image-1"; - reg = <0x110000 0xee000>; + reg = <0x111000 0xed000>; }; vendor_partition: partition@1fe000 { label = "vendor-data"; diff --git a/src/platform/telink/4m_flash.overlay b/src/platform/telink/4m_flash.overlay index 52a3f1c9a6f2fc..e22c007e0bc235 100644 --- a/src/platform/telink/4m_flash.overlay +++ b/src/platform/telink/4m_flash.overlay @@ -21,19 +21,19 @@ }; slot0_partition: partition@19000 { label = "image-0"; - reg = <0x19000 0x1ee000>; + reg = <0x19000 0x1ed000>; }; - factory_partition: partition@207000 { + factory_partition: partition@206000 { label = "factory-data"; - reg = <0x207000 0x1000>; + reg = <0x206000 0x1000>; }; - storage_partition: partition@208000 { + storage_partition: partition@207000 { label = "storage"; - reg = <0x208000 0x8000>; + reg = <0x207000 0xa000>; }; - slot1_partition: partition@210000 { + slot1_partition: partition@211000 { label = "image-1"; - reg = <0x210000 0x1ee000>; + reg = <0x211000 0x1ed000>; }; vendor_partition: partition@3fe000 { label = "vendor-data";