Skip to content

Commit 158d766

Browse files
committed
segement credentials by credential type
1 parent 29a9f25 commit 158d766

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

examples/lock-app/silabs/include/LockManager.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -271,26 +271,30 @@ class LockManager
271271

272272
EFR32DoorLock::LockInitParams::LockParam LockParams;
273273

274+
// Stores LockUserInfo corresponding to a user index
274275
static StorageKeyName LockUserEndpoint(uint16_t userIndex, chip::EndpointId endpoint)
275276
{
276277
return StorageKeyName::Formatted("g/lu/%x/e/%x", userIndex, endpoint);
277278
}
278-
static StorageKeyName LockCredentialEndpoint(uint16_t credentialIndex, chip::EndpointId endpoint)
279+
// Stores LockCredentialInfo corresponding to a credential index and type
280+
static StorageKeyName LockCredentialEndpoint(uint16_t credentialIndex, CredentialTypeEnum credentialType,
281+
chip::EndpointId endpoint)
279282
{
280-
return StorageKeyName::Formatted("g/lc/%x/e/%x", credentialIndex, endpoint);
283+
return StorageKeyName::Formatted("g/lc/%x/t/%x/e/%x", credentialIndex, static_cast<uint16_t>(credentialType), endpoint);
281284
}
282-
static StorageKeyName LockUserCredentialMap(uint16_t userIndex)
283-
{
284-
return StorageKeyName::Formatted("g/lu/%x/lc", userIndex);
285-
} // Stores all the credential indices that belong to a user
285+
// Stores all the credential indices that belong to a user
286+
static StorageKeyName LockUserCredentialMap(uint16_t userIndex) { return StorageKeyName::Formatted("g/lu/%x/lc", userIndex); }
287+
// Stores WeekDayScheduleInfo corresponding to a user and schedule index
286288
static StorageKeyName LockUserWeekDayScheduleEndpoint(uint16_t userIndex, uint16_t scheduleIndex, chip::EndpointId endpoint)
287289
{
288290
return StorageKeyName::Formatted("g/lu/%x/lw/%x/e/%x", userIndex, scheduleIndex, endpoint);
289291
}
292+
// Stores YearDayScheduleInfo corresponding to a user and schedule index
290293
static StorageKeyName LockUserYearDayScheduleEndpoint(uint16_t userIndex, uint16_t scheduleIndex, chip::EndpointId endpoint)
291294
{
292295
return StorageKeyName::Formatted("g/lu/%x/ly/%x/e/%x", userIndex, scheduleIndex, endpoint);
293296
}
297+
// Stores HolidayScheduleInfo corresponding to a schedule index
294298
static StorageKeyName LockHolidayScheduleEndpoint(uint16_t scheduleIndex, chip::EndpointId endpoint)
295299
{
296300
return StorageKeyName::Formatted("g/lh/%x/e/%x", scheduleIndex, endpoint);
@@ -301,7 +305,6 @@ class LockManager
301305
WeekDayScheduleInfo weekDayScheduleInStorage;
302306
YearDayScheduleInfo yearDayScheduleInStorage;
303307
HolidayScheduleInfo holidayScheduleInStorage;
304-
CredentialStruct mCredential;
305308
CredentialStruct mCredentials[kMaxCredentialsPerUser];
306309
};
307310

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
#include "LockManager.h"
21-
2221
#include "AppConfig.h"
2322
#include "AppTask.h"
2423
#include <app-common/zap-generated/attributes/Accessors.h>
@@ -377,7 +376,7 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
377376
uint16_t credentialSize = static_cast<uint16_t>(sizeof(CredentialStruct) * userInStorage.currentCredentialCount);
378377

379378
error =
380-
chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(credentialKey.KeyName(), &mCredential, credentialSize);
379+
chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(credentialKey.KeyName(), mCredentials, credentialSize);
381380

382381
// If no data is found at credential key
383382
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -387,7 +386,7 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
387386
// Else if KVS read was successful
388387
else if (error == CHIP_NO_ERROR)
389388
{
390-
user.credentials = chip::Span<const CredentialStruct>{ &mCredential, userInStorage.currentCredentialCount };
389+
user.credentials = chip::Span<const CredentialStruct>{ mCredentials, userInStorage.currentCredentialCount };
391390
}
392391
else
393392
{
@@ -503,7 +502,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
503502

504503
uint16_t size = static_cast<uint16_t>(sizeof(LockCredentialInfo));
505504

506-
chip::StorageKeyName key = LockCredentialEndpoint(credentialIndex, endpointId);
505+
chip::StorageKeyName key = LockCredentialEndpoint(credentialIndex, credentialType, endpointId);
507506

508507
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(key.KeyName(), &credentialInStorage, size);
509508

@@ -578,7 +577,7 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
578577

579578
memcpy(credentialInStorage.credentialData, credentialData.data(), credentialInStorage.credentialDataSize);
580579

581-
chip::StorageKeyName key = LockCredentialEndpoint(credentialIndex, endpointId);
580+
chip::StorageKeyName key = LockCredentialEndpoint(credentialIndex, credentialType, endpointId);
582581

583582
if ((error != CHIP_NO_ERROR))
584583
{
@@ -912,11 +911,11 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
912911

913912
chip::StorageKeyName credentialKey = LockUserCredentialMap(userIndex);
914913

915-
uint16_t credentialSize = static_cast<uint16_t>(sizeof(CredentialStruct) * userInStorage.currentCredentialCount);
914+
uint16_t credentialStructSize = static_cast<uint16_t>(sizeof(CredentialStruct) * userInStorage.currentCredentialCount);
916915

917916
// Get array of credential indices and types associated to user
918917
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(credentialKey.KeyName(), &mCredentials,
919-
credentialSize);
918+
credentialStructSize);
920919

921920
// No credential data associated with user
922921
if (error != CHIP_NO_ERROR)
@@ -930,14 +929,15 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
930929
if (mCredentials[j].credentialType == CredentialTypeEnum::kPin)
931930
{
932931
// Read the individual credential at credentialIndex j
933-
uint16_t size = static_cast<uint16_t>(sizeof(LockCredentialInfo));
932+
uint16_t credentialSize = static_cast<uint16_t>(sizeof(LockCredentialInfo));
934933

935-
chip::StorageKeyName key = LockCredentialEndpoint(mCredentials[j].credentialIndex, endpointId);
934+
chip::StorageKeyName key =
935+
LockCredentialEndpoint(mCredentials[j].credentialIndex, mCredentials[j].credentialType, endpointId);
936936

937-
error =
938-
chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(key.KeyName(), &credentialInStorage, size);
937+
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(key.KeyName(), &credentialInStorage,
938+
credentialSize);
939939

940-
if (error != CHIP_NO_ERROR)
940+
if ((error != CHIP_NO_ERROR) && (error != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND))
941941
{
942942
ChipLogError(Zcl, "Error reading credential");
943943
return false;

0 commit comments

Comments
 (0)