Skip to content

Commit

Permalink
[infineon PSoC6] Fix for Doorlock credentials update (#25558)
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenCY authored and pull[bot] committed Jul 6, 2023
1 parent 3502b24 commit 1211664
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
13 changes: 6 additions & 7 deletions examples/lock-app/infineon/psoc6/include/LockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ 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;

// Indices received for user/credential/schedules are 1-indexed
static constexpr uint8_t kStartIndexValue = 1;
static constexpr uint8_t kMaxCredentialSize = 20;
static constexpr uint8_t kNumCredentialTypes = 6;

static constexpr uint8_t kMaxCredentials = kMaxUsers * kMaxCredentialsPerUser;
} // namespace ResourceRanges
Expand Down Expand Up @@ -178,6 +176,7 @@ class LockManager
uint32_t localEndTime, OperatingModeEnum operatingMode);

bool IsValidUserIndex(uint16_t userIndex);
bool IsValidCredentialType(CredentialTypeEnum type);
bool IsValidCredentialIndex(uint16_t credentialIndex, CredentialTypeEnum type);
bool IsValidWeekdayScheduleIndex(uint8_t scheduleIndex);
bool IsValidYeardayScheduleIndex(uint8_t scheduleIndex);
Expand Down Expand Up @@ -205,14 +204,14 @@ class LockManager
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);

EmberAfPluginDoorLockUserInfo mLockUsers[kMaxUsers];
EmberAfPluginDoorLockCredentialInfo mLockCredentials[kMaxCredentials];
EmberAfPluginDoorLockCredentialInfo mLockCredentials[kNumCredentialTypes][kMaxCredentials];
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];
CredentialStruct mCredentials[kMaxUsers][kMaxCredentialsPerUser];
uint8_t mCredentialData[kNumCredentialTypes][kMaxCredentials][kMaxCredentialSize];
CredentialStruct mCredentials[kMaxUsers][kMaxCredentials];

static LockManager sLock;
P6DoorLock::LockInitParams::LockParam LockParams;
Expand Down
6 changes: 2 additions & 4 deletions examples/lock-app/infineon/psoc6/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ void AppTask::lockMgr_Init()
}

ConfigurationMgr().LogDeviceConfig();

// Users and credentials should be checked once from flash on boot
LockMgr().ReadConfigValues();
// Print setup info
PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE));
}
Expand Down Expand Up @@ -323,9 +324,6 @@ void AppTask::AppTaskMain(void * pvParameter)
sAppTask.Init();
P6_LOG("App Task started");

// Users and credentials should be checked once from flash on boot
LockMgr().ReadConfigValues();

while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, portMAX_DELAY);
Expand Down
76 changes: 38 additions & 38 deletions examples/lock-app/infineon/psoc6/src/LockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ bool LockManager::IsValidCredentialIndex(uint16_t credentialIndex, CredentialTyp
return (credentialIndex < kMaxCredentialsPerUser);
}

bool LockManager::IsValidCredentialType(CredentialTypeEnum type)
{
return (to_underlying(type) < kNumCredentialTypes);
}

bool LockManager::IsValidWeekdayScheduleIndex(uint8_t scheduleIndex)
{
return (scheduleIndex < kMaxWeekdaySchedulesPerUser);
Expand All @@ -140,7 +145,7 @@ bool LockManager::ReadConfigValues()
sizeof(EmberAfPluginDoorLockUserInfo) * ArraySize(mLockUsers), outLen);

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

P6Config::ReadConfigValueBin(P6Config::kConfigKey_LockUserName, reinterpret_cast<uint8_t *>(mUserNames), sizeof(mUserNames),
outLen);
Expand All @@ -149,8 +154,7 @@ bool LockManager::ReadConfigValues()
sizeof(mCredentialData), outLen);

P6Config::ReadConfigValueBin(P6Config::kConfigKey_UserCredentials, reinterpret_cast<uint8_t *>(mCredentials),
sizeof(CredentialStruct) * LockParams.numberOfUsers * LockParams.numberOfCredentialsPerUser,
outLen);
sizeof(CredentialStruct) * LockParams.numberOfUsers * kMaxCredentials, outLen);

P6Config::ReadConfigValueBin(P6Config::kConfigKey_WeekDaySchedules, reinterpret_cast<uint8_t *>(mWeekdaySchedule),
sizeof(EmberAfPluginDoorLockWeekDaySchedule) * LockParams.numberOfWeekdaySchedulesPerUser *
Expand Down Expand Up @@ -384,11 +388,6 @@ 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/25081
// For now, preserve pre-existing behavior, which set credentialType to 1.
mCredentials[userIndex][i].credentialType = CredentialTypeEnum::kPin;
mCredentials[userIndex][i].credentialIndex = i + 1;
}

userInStorage.credentials = chip::Span<const CredentialStruct>(mCredentials[userIndex], totalCredentials);
Expand All @@ -398,7 +397,7 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip:
sizeof(EmberAfPluginDoorLockUserInfo) * LockParams.numberOfUsers);

P6Config::WriteConfigValueBin(P6Config::kConfigKey_UserCredentials, reinterpret_cast<const uint8_t *>(mCredentials),
sizeof(CredentialStruct) * LockParams.numberOfUsers * LockParams.numberOfCredentialsPerUser);
sizeof(CredentialStruct) * LockParams.numberOfUsers * kMaxCredentials);

P6Config::WriteConfigValueBin(P6Config::kConfigKey_LockUserName, reinterpret_cast<const uint8_t *>(mUserNames),
sizeof(mUserNames));
Expand All @@ -411,25 +410,21 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip:
bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, CredentialTypeEnum credentialType,
EmberAfPluginDoorLockCredentialInfo & credential)
{

VerifyOrReturnValue(credentialIndex > 0, false); // indices are one-indexed

credentialIndex--;

VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType), false);
VerifyOrReturnValue(IsValidCredentialType(credentialType), false);
if (CredentialTypeEnum::kProgrammingPIN == credentialType)
{
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType),
false); // programming pin index is only index allowed to contain 0
}
else
{
VerifyOrReturnValue(IsValidCredentialIndex(--credentialIndex, credentialType), false); // otherwise, indices are one-indexed
}

ChipLogProgress(Zcl, "Lock App: LockManager::GetCredential [credentialType=%u], credentialIndex=%d",
to_underlying(credentialType), credentialIndex);

if (credentialType == CredentialTypeEnum::kProgrammingPIN)
{
ChipLogError(Zcl, "Programming user not supported [credentialType=%u], credentialIndex=%d", to_underlying(credentialType),
credentialIndex);

return true;
}

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);
Expand Down Expand Up @@ -458,31 +453,36 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
chip::FabricIndex modifier, DlCredentialStatus credentialStatus, CredentialTypeEnum credentialType,
const chip::ByteSpan & credentialData)
{

VerifyOrReturnValue(credentialIndex > 0, false); // indices are one-indexed

credentialIndex--;

VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType), false);
VerifyOrReturnValue(IsValidCredentialType(credentialType), false);
if (CredentialTypeEnum::kProgrammingPIN == credentialType)
{
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType),
false); // programming pin index is only index allowed to contain 0
}
else
{
VerifyOrReturnValue(IsValidCredentialIndex(--credentialIndex, credentialType), false); // otherwise, indices are one-indexed
}

ChipLogProgress(Zcl,
"Door Lock App: LockManager::SetCredential "
"[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
P6Config::WriteConfigValueBin(P6Config::kConfigKey_Credential, reinterpret_cast<const uint8_t *>(&mLockCredentials),
sizeof(EmberAfPluginDoorLockCredentialInfo) * LockParams.numberOfCredentialsPerUser);
sizeof(EmberAfPluginDoorLockCredentialInfo) * kMaxCredentials * kNumCredentialTypes);

P6Config::WriteConfigValueBin(P6Config::kConfigKey_CredentialData, reinterpret_cast<const uint8_t *>(&mCredentialData),
sizeof(mCredentialData));
Expand Down Expand Up @@ -686,15 +686,15 @@ bool LockManager::setLockState(chip::EndpointId endpointId, DlLockState lockStat
}

// Check the PIN code
for (uint8_t i = 0; i < kMaxCredentials; i++)
for (const auto & currentCredential : mLockCredentials[to_underlying(CredentialTypeEnum::kPin)])
{
if (mLockCredentials[i].credentialType != CredentialTypeEnum::kPin ||
mLockCredentials[i].status == DlCredentialStatus::kAvailable)

if (currentCredential.status == DlCredentialStatus::kAvailable)
{
continue;
}

if (mLockCredentials[i].credentialData.data_equal(pin.Value()))
if (currentCredential.credentialData.data_equal(pin.Value()))
{
ChipLogDetail(Zcl,
"Lock App: specified PIN code was found in the database, setting lock state to \"%s\" [endpointId=%d]",
Expand Down

0 comments on commit 1211664

Please sign in to comment.