Skip to content

Commit 9d09096

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

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

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

+18-32
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ bool LockManager::IsValidCredentialIndex(uint16_t credentialIndex, CredentialTyp
126126
{
127127
return (0 == credentialIndex); // 0 is required index for Programming PIN
128128
}
129+
130+
credentialIndex--;
131+
129132
return (credentialIndex < kMaxCredentialsPerUser);
130133
}
131134

@@ -484,15 +487,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
484487

485488
VerifyOrReturnValue(IsValidCredentialType(credentialType), false);
486489

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-
}
490+
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType), false);
496491

497492
ChipLogProgress(Zcl, "Lock App: LockManager::GetCredential [credentialType=%u], credentialIndex=%d",
498493
to_underlying(credentialType), credentialIndex);
@@ -516,18 +511,18 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
516511
{
517512
credential.status = mCredentialInStorage.status;
518513
ChipLogDetail(Zcl, "CredentialStatus: %d, CredentialIndex: %d ", (int) credential.status, credentialIndex);
514+
if (DlCredentialStatus::kAvailable == credential.status)
515+
{
516+
ChipLogDetail(Zcl, "Found unoccupied credential ");
517+
return true;
518+
}
519519
}
520520
else
521521
{
522522
ChipLogError(Zcl, "Error reading KVS key");
523523
return false;
524524
}
525525

526-
if (DlCredentialStatus::kAvailable == credential.status)
527-
{
528-
ChipLogDetail(Zcl, "Found unoccupied credential ");
529-
return true;
530-
}
531526
credential.credentialType = mCredentialInStorage.credentialType;
532527
credential.credentialData = chip::ByteSpan{ mCredentialInStorage.credentialData, mCredentialInStorage.credentialDataSize };
533528
credential.createdBy = mCredentialInStorage.createdBy;
@@ -537,8 +532,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
537532
credential.creationSource = DlAssetSource::kMatterIM;
538533
credential.modificationSource = DlAssetSource::kMatterIM;
539534

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

543537
return true;
544538
}
@@ -553,15 +547,7 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
553547

554548
VerifyOrReturnValue(IsValidCredentialType(credentialType), false);
555549

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-
}
550+
VerifyOrReturnValue(IsValidCredentialIndex(credentialIndex, credentialType), false);
565551

566552
VerifyOrReturnValue(credentialData.size() <= kMaxCredentialSize, false);
567553

@@ -585,7 +571,7 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
585571
// Clear mCredentialInStorage.credentialData
586572
memset(mCredentialInStorage.credentialData, 0, kMaxCredentialSize);
587573

588-
if ((error != CHIP_NO_ERROR))
574+
if (error != CHIP_NO_ERROR)
589575
{
590576
ChipLogError(Zcl, "Error reading from KVS key");
591577
return false;
@@ -904,17 +890,17 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
904890

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

907-
// No user exists at this index
893+
// User exists at this index
908894
if (error == CHIP_NO_ERROR)
909895
{
910896
chip::StorageKeyName credentialKey = LockUserCredentialMap(userIndex);
911897

912-
uint16_t credentialSize = CredentialStructSize * mUserInStorage.currentCredentialCount;
898+
uint16_t credentialStructSize = CredentialStructSize * mUserInStorage.currentCredentialCount;
913899

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

917-
// No credential data associated with user
903+
// Credential data associated with user
918904
if (error == CHIP_NO_ERROR)
919905
{
920906
// Loop through each credential attached to the user
@@ -933,7 +919,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
933919

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

936-
if ((error == CHIP_NO_ERROR) || (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND))
922+
if (error == CHIP_NO_ERROR)
937923
{
938924
if (mCredentialInStorage.status == DlCredentialStatus::kOccupied)
939925
{
@@ -959,7 +945,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
959945
}
960946
}
961947
}
962-
else
948+
else if (error != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
963949
{
964950
ChipLogError(Zcl, "Error reading credential");
965951
return false;

0 commit comments

Comments
 (0)