Skip to content

Commit c737f25

Browse files
committed
don't decrement credentialIndex for get/setCredential
1 parent 7c80b3d commit c737f25

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

examples/lock-app/silabs/src/LockManager.cpp

+18-33
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ bool LockManager::IsValidCredentialIndex(uint16_t credentialIndex, CredentialTyp
126126
{
127127
return (0 == credentialIndex); // 0 is required index for Programming PIN
128128
}
129-
return (credentialIndex < kMaxCredentialsPerUser);
129+
130+
// Other credential types are valid at all uint16_t indices, credential limit is based on available storage
131+
return (credentialIndex < UINT16_MAX);
130132
}
131133

132134
bool LockManager::IsValidCredentialType(CredentialTypeEnum type)
@@ -484,15 +486,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
484486

485487
VerifyOrReturnValue(IsValidCredentialType(credentialType), false);
486488

487-
if (CredentialTypeEnum::kProgrammingPIN == credentialType)
488-
{
489-
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType),
490-
false); // programming pin index is only index allowed to contain 0
491-
}
492-
else
493-
{
494-
VerifyOrReturnValue(IsValidCredentialIndex(--credentialIndex, credentialType), false); // otherwise, indices are one-indexed
495-
}
489+
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType), false);
496490

497491
ChipLogProgress(Zcl, "Lock App: LockManager::GetCredential [credentialType=%u], credentialIndex=%d",
498492
to_underlying(credentialType), credentialIndex);
@@ -516,18 +510,18 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
516510
{
517511
credential.status = mCredentialInStorage.status;
518512
ChipLogDetail(Zcl, "CredentialStatus: %d, CredentialIndex: %d ", (int) credential.status, credentialIndex);
513+
if (DlCredentialStatus::kAvailable == credential.status)
514+
{
515+
ChipLogDetail(Zcl, "Found unoccupied credential ");
516+
return true;
517+
}
519518
}
520519
else
521520
{
522521
ChipLogError(Zcl, "Error reading KVS key");
523522
return false;
524523
}
525524

526-
if (DlCredentialStatus::kAvailable == credential.status)
527-
{
528-
ChipLogDetail(Zcl, "Found unoccupied credential ");
529-
return true;
530-
}
531525
credential.credentialType = mCredentialInStorage.credentialType;
532526
credential.credentialData = chip::ByteSpan{ mCredentialInStorage.credentialData, mCredentialInStorage.credentialDataSize };
533527
credential.createdBy = mCredentialInStorage.createdBy;
@@ -537,8 +531,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
537531
credential.creationSource = DlAssetSource::kMatterIM;
538532
credential.modificationSource = DlAssetSource::kMatterIM;
539533

540-
ChipLogDetail(Zcl, "Found occupied credential [type=%u,dataSize=%u]", to_underlying(credential.credentialType),
541-
credential.credentialData.size());
534+
ChipLogDetail(Zcl, "Found occupied credential");
542535

543536
return true;
544537
}
@@ -553,15 +546,7 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
553546

554547
VerifyOrReturnValue(IsValidCredentialType(credentialType), false);
555548

556-
if (CredentialTypeEnum::kProgrammingPIN == credentialType)
557-
{
558-
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType),
559-
false); // programming pin index is only index allowed to contain 0
560-
}
561-
else
562-
{
563-
VerifyOrReturnValue(IsValidCredentialIndex(--credentialIndex, credentialType), false); // otherwise, indices are one-indexed
564-
}
549+
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType), false);
565550

566551
VerifyOrReturnValue(credentialData.size() <= kMaxCredentialSize, false);
567552

@@ -585,7 +570,7 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
585570
// Clear mCredentialInStorage.credentialData
586571
memset(mCredentialInStorage.credentialData, 0, kMaxCredentialSize);
587572

588-
if ((error != CHIP_NO_ERROR))
573+
if (error != CHIP_NO_ERROR)
589574
{
590575
ChipLogError(Zcl, "Error reading from KVS key");
591576
return false;
@@ -904,17 +889,17 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
904889

905890
error = mStorage->SyncGetKeyValue(userKey.KeyName(), &mUserInStorage, userSize);
906891

907-
// No user exists at this index
892+
// User exists at this index
908893
if (error == CHIP_NO_ERROR)
909894
{
910895
chip::StorageKeyName credentialKey = LockUserCredentialMap(userIndex);
911896

912-
uint16_t credentialSize = CredentialStructSize * mUserInStorage.currentCredentialCount;
897+
uint16_t credentialStructSize = CredentialStructSize * mUserInStorage.currentCredentialCount;
913898

914899
// Get array of credential indices and types associated to user
915-
error = mStorage->SyncGetKeyValue(credentialKey.KeyName(), &mCredentials, credentialSize);
900+
error = mStorage->SyncGetKeyValue(credentialKey.KeyName(), &mCredentials, credentialStructSize);
916901

917-
// No credential data associated with user
902+
// Credential data associated with user
918903
if (error == CHIP_NO_ERROR)
919904
{
920905
// Loop through each credential attached to the user
@@ -933,7 +918,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
933918

934919
error = mStorage->SyncGetKeyValue(key.KeyName(), &mCredentialInStorage, credentialSize);
935920

936-
if ((error == CHIP_NO_ERROR) || (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND))
921+
if (error == CHIP_NO_ERROR)
937922
{
938923
if (mCredentialInStorage.status == DlCredentialStatus::kOccupied)
939924
{
@@ -959,7 +944,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
959944
}
960945
}
961946
}
962-
else
947+
else if (error != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
963948
{
964949
ChipLogError(Zcl, "Error reading credential");
965950
return false;

0 commit comments

Comments
 (0)