diff --git a/examples/lock-app/linux/include/LockEndpoint.h b/examples/lock-app/linux/include/LockEndpoint.h index 0e0b32fd3b775b..cc648146168f42 100644 --- a/examples/lock-app/linux/include/LockEndpoint.h +++ b/examples/lock-app/linux/include/LockEndpoint.h @@ -39,6 +39,7 @@ struct YearDayScheduleInfo; struct HolidayScheduleInfo; static constexpr size_t DOOR_LOCK_CREDENTIAL_INFO_MAX_DATA_SIZE = 20; +static constexpr size_t DOOR_LOCK_CREDENTIAL_INFO_MAX_TYPES = 6; class LockEndpoint { @@ -48,7 +49,7 @@ class LockEndpoint uint8_t numberOfHolidaySchedules) : mEndpointId{ endpointId }, mLockState{ DlLockState::kLocked }, mDoorState{ DoorStateEnum::kDoorClosed }, mLockUsers(numberOfLockUsersSupported), - mLockCredentials(numberOfCredentialsSupported + 1), + mLockCredentials(DOOR_LOCK_CREDENTIAL_INFO_MAX_TYPES, std::vector(numberOfCredentialsSupported + 1)), mWeekDaySchedules(numberOfLockUsersSupported, std::vector(weekDaySchedulesPerUser)), mYearDaySchedules(numberOfLockUsersSupported, std::vector(yearDaySchedulesPerUser)), mHolidaySchedules(numberOfHolidaySchedules) @@ -109,7 +110,7 @@ class LockEndpoint // This is very naive implementation of users/credentials/schedules database and by no means the best practice. Proper storage // of those items is out of scope of this example. std::vector mLockUsers; - std::vector mLockCredentials; + std::vector> mLockCredentials; std::vector> mWeekDaySchedules; std::vector> mYearDaySchedules; std::vector mHolidaySchedules; diff --git a/examples/lock-app/linux/src/LockEndpoint.cpp b/examples/lock-app/linux/src/LockEndpoint.cpp index 6ed9592df0c0f9..d0ee0ca454c768 100644 --- a/examples/lock-app/linux/src/LockEndpoint.cpp +++ b/examples/lock-app/linux/src/LockEndpoint.cpp @@ -164,14 +164,19 @@ bool LockEndpoint::GetCredential(uint16_t credentialIndex, CredentialTypeEnum cr ChipLogProgress(Zcl, "Lock App: LockEndpoint::GetCredential [endpoint=%d,credentialIndex=%u,credentialType=%u]", mEndpointId, credentialIndex, to_underlying(credentialType)); - if (credentialIndex >= mLockCredentials.size() || - (0 == credentialIndex && CredentialTypeEnum::kProgrammingPIN != credentialType)) + if (to_underlying(credentialType) >= mLockCredentials.size()) { ChipLogError(Zcl, "Cannot get the credential - index out of range [endpoint=%d,index=%d]", mEndpointId, credentialIndex); return false; } - const auto & credentialInStorage = mLockCredentials[credentialIndex]; + if (credentialIndex >= mLockCredentials.at(to_underlying(credentialType)).size() || (0 == credentialIndex && CredentialTypeEnum::kProgrammingPIN != credentialType)) + { + ChipLogError(Zcl, "Cannot get the credential - index out of range [endpoint=%d,index=%d]", mEndpointId, credentialIndex); + return false; + } + + const auto & credentialInStorage = mLockCredentials[to_underlying(credentialType)][credentialIndex]; credential.status = credentialInStorage.status; if (DlCredentialStatus::kAvailable == credential.status) @@ -206,14 +211,19 @@ bool LockEndpoint::SetCredential(uint16_t credentialIndex, chip::FabricIndex cre mEndpointId, credentialIndex, to_underlying(credentialStatus), to_underlying(credentialType), static_cast(credentialData.size()), creator, modifier); - if (credentialIndex >= mLockCredentials.size() || - (0 == credentialIndex && CredentialTypeEnum::kProgrammingPIN != credentialType)) + if (to_underlying(credentialType) >= mLockCredentials.capacity()) + { + ChipLogError(Zcl, "Cannot set the credential - type out of range [endpoint=%d,type=%d]", mEndpointId, to_underlying(credentialType)); + return false; + } + + if (credentialIndex >= mLockCredentials.at(to_underlying(credentialType)).size() || (0 == credentialIndex && CredentialTypeEnum::kProgrammingPIN != credentialType)) { ChipLogError(Zcl, "Cannot set the credential - index out of range [endpoint=%d,index=%d]", mEndpointId, credentialIndex); return false; } - auto & credentialInStorage = mLockCredentials[credentialIndex]; + auto & credentialInStorage = mLockCredentials[to_underlying(credentialType)][credentialIndex]; if (credentialData.size() > DOOR_LOCK_CREDENTIAL_INFO_MAX_DATA_SIZE) { ChipLogError(Zcl, @@ -391,14 +401,29 @@ bool LockEndpoint::setLockState(DlLockState lockState, const Optional(usercredentials - mLockCredentials.begin()); + auto credential = std::find_if(mLockCredentials[credentialType].begin(), mLockCredentials[credentialType].end(), [&pin](const LockCredentialInfo & c) { + return (c.status != DlCredentialStatus::kAvailable) && chip::ByteSpan{ c.credentialData, c.credentialDataSize }.data_equal(pin.Value()); }); - if (credential == mLockCredentials.end()) + if (credential == mLockCredentials[credentialType].end()) { ChipLogDetail(Zcl, - "Lock App: specified PIN code was not found in the database, ignoring command to set lock state to \"%s\" " + "Lock App: specified Credential was not found in the database, ignoring command to set lock state to \"%s\" " "[endpointId=%d]", lockStateToString(lockState), mEndpointId); @@ -407,7 +432,7 @@ bool LockEndpoint::setLockState(DlLockState lockState, const Optional(credential - mLockCredentials.begin()); + auto credentialIndex = static_cast(credential - usercredentials->begin()); auto user = std::find_if(mLockUsers.begin(), mLockUsers.end(), [credential, credentialIndex](const LockUserInfo & u) { return std::any_of(u.credentials.begin(), u.credentials.end(), [&credential, credentialIndex](const CredentialStruct & c) { return c.CredentialIndex == credentialIndex && c.CredentialType == to_underlying(credential->credentialType); @@ -416,7 +441,7 @@ bool LockEndpoint::setLockState(DlLockState lockState, const Optional