Skip to content

Commit

Permalink
[Telink] Update the set credentials part in LockManager
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiiSalamakha committed Nov 29, 2023
1 parent b379158 commit e27d906
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
5 changes: 3 additions & 2 deletions examples/lock-app/telink/include/LockSettingsStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <../Zephyr/ZephyrConfig.h>
#include <AppConfig.h>
#include <LockManager.h>

#if LOCK_MANAGER_CONFIG_USE_NVM_CREDENTIAL_STORAGE
namespace chip {
Expand All @@ -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
Expand Down
58 changes: 39 additions & 19 deletions examples/lock-app/telink/src/LockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -426,15 +432,9 @@ bool LockManager::ReadConfigValues()
ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_LockUser, reinterpret_cast<uint8_t *>(&mLockUsers),
sizeof(EmberAfPluginDoorLockUserInfo) * ArraySize(mLockUsers), outLen);

ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_Credential, reinterpret_cast<uint8_t *>(&mLockCredentials),
sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials * kNumCredentialTypes, outLen);

ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_LockUserName, reinterpret_cast<uint8_t *>(mUserNames),
sizeof(mUserNames), outLen);

ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_CredentialData, reinterpret_cast<uint8_t *>(mCredentialData),
sizeof(mCredentialData), outLen);

ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_UserCredentials, reinterpret_cast<uint8_t *>(mCredentials),
sizeof(CredentialStruct) * LockParams.numberOfUsers * LockParams.numberOfCredentialsPerUser,
outLen);
Expand All @@ -453,6 +453,15 @@ bool LockManager::ReadConfigValues()
reinterpret_cast<uint8_t *>(&(mHolidaySchedule)),
sizeof(EmberAfPluginDoorLockHolidaySchedule) * LockParams.numberOfHolidaySchedules, outLen);

for (uint8_t i = 0; i < kNumCredentialTypes; i++)
{
ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_Credential[i], reinterpret_cast<uint8_t *>(&mLockCredentials[i]),
sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials, outLen);

ZephyrConfig::ReadConfigValueBin(LockSettingsStorage::kConfigKey_CredentialData[i], reinterpret_cast<uint8_t *>(mCredentialData[i]),
kMaxCredentials * kMaxCredentialSize, outLen);
}

return true;
}
#endif
Expand Down Expand Up @@ -654,20 +663,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<const uint8_t *>(&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<const uint8_t *>(&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<const uint8_t *>(&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<const uint8_t *>(&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));
Expand Down
22 changes: 20 additions & 2 deletions examples/lock-app/telink/src/LockSettingsStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e27d906

Please sign in to comment.