Skip to content

Commit 64e96bc

Browse files
committed
inject PersistentStorageDelegate * storage into LockManager
1 parent 9c0e2c1 commit 64e96bc

File tree

3 files changed

+71
-77
lines changed

3 files changed

+71
-77
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class LockManager
161161
} State;
162162

163163
CHIP_ERROR Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state,
164-
EFR32DoorLock::LockInitParams::LockParam lockParam);
164+
EFR32DoorLock::LockInitParams::LockParam lockParam, PersistentStorageDelegate * storage);
165165
bool NextState();
166166
bool IsActionInProgress();
167167
bool InitiateAction(int32_t aActor, Action_t aAction);
@@ -300,8 +300,11 @@ class LockManager
300300
return StorageKeyName::Formatted("g/lh/%x/e/%x", scheduleIndex, endpoint);
301301
}
302302

303-
LockUserInfo userInStorage;
304-
LockCredentialInfo credentialInStorage;
303+
// Pointer to the PeristentStorage
304+
PersistentStorageDelegate * mStorage = nullptr;
305+
306+
LockUserInfo mUserInStorage;
307+
LockCredentialInfo mCredentialInStorage;
305308
CredentialStruct mCredentials[kMaxCredentialsPerUser];
306309
};
307310

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ CHIP_ERROR AppTask::Init()
206206
.SetNumberOfWeekdaySchedulesPerUser(numberOfWeekdaySchedulesPerUser)
207207
.SetNumberOfYeardaySchedulesPerUser(numberOfYeardaySchedulesPerUser)
208208
.SetNumberOfHolidaySchedules(numberOfHolidaySchedules)
209-
.GetLockParam());
209+
.GetLockParam(),
210+
&Server::GetInstance().GetPersistentStorage());
210211

211212
if (err != CHIP_NO_ERROR)
212213
{

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

+63-73
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "AppConfig.h"
2222
#include "AppTask.h"
2323
#include <app-common/zap-generated/attributes/Accessors.h>
24-
#include <app/server/Server.h>
2524
#include <cstring>
2625
#include <lib/support/logging/CHIPLogging.h>
2726

@@ -38,7 +37,8 @@ LockManager & LockMgr()
3837
return sLock;
3938
}
4039

41-
CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state, LockParam lockParam)
40+
CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state, LockParam lockParam,
41+
PersistentStorageDelegate * storage)
4242
{
4343

4444
LockParams = lockParam;
@@ -95,6 +95,10 @@ CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters:
9595
return APP_ERROR_CREATE_TIMER_FAILED;
9696
}
9797

98+
VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
99+
100+
mStorage = storage;
101+
98102
if (state.Value() == DlLockState::kUnlocked)
99103
mState = kState_UnlockCompleted;
100104
else
@@ -335,7 +339,7 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
335339

336340
uint16_t size = static_cast<uint16_t>(sizeof(LockUserInfo));
337341

338-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(userKey.KeyName(), &userInStorage, size);
342+
error = mStorage->SyncGetKeyValue(userKey.KeyName(), &mUserInStorage, size);
339343
// If no data is found at user key
340344
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
341345
{
@@ -347,9 +351,9 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
347351
// Else if KVS read was successful
348352
else if (error == CHIP_NO_ERROR)
349353
{
350-
user.userStatus = userInStorage.userStatus;
354+
user.userStatus = mUserInStorage.userStatus;
351355

352-
if (userInStorage.userStatus == UserStatusEnum::kAvailable)
356+
if (mUserInStorage.userStatus == UserStatusEnum::kAvailable)
353357
{
354358
ChipLogDetail(Zcl, "Found unoccupied user [endpoint=%d]", endpointId);
355359
return true;
@@ -361,27 +365,26 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
361365
return false;
362366
}
363367

364-
user.userName = chip::CharSpan(userInStorage.userName, userInStorage.userNameSize);
365-
user.userUniqueId = userInStorage.userUniqueId;
366-
user.userType = userInStorage.userType;
367-
user.credentialRule = userInStorage.credentialRule;
368+
user.userName = chip::CharSpan(mUserInStorage.userName, mUserInStorage.userNameSize);
369+
user.userUniqueId = mUserInStorage.userUniqueId;
370+
user.userType = mUserInStorage.userType;
371+
user.credentialRule = mUserInStorage.credentialRule;
368372
// So far there's no way to actually create the credential outside Matter, so here we always set the creation/modification
369373
// source to Matter
370374
user.creationSource = DlAssetSource::kMatterIM;
371-
user.createdBy = userInStorage.createdBy;
375+
user.createdBy = mUserInStorage.createdBy;
372376
user.modificationSource = DlAssetSource::kMatterIM;
373-
user.lastModifiedBy = userInStorage.lastModifiedBy;
377+
user.lastModifiedBy = mUserInStorage.lastModifiedBy;
374378

375-
// Ensure userInStorage.currentCredentialCount <= kMaxCredentialsPerUser to avoid buffer overflow
376-
VerifyOrReturnValue(userInStorage.currentCredentialCount <= kMaxCredentialsPerUser, false);
379+
// Ensure mUserInStorage.currentCredentialCount <= kMaxCredentialsPerUser to avoid buffer overflow
380+
VerifyOrReturnValue(mUserInStorage.currentCredentialCount <= kMaxCredentialsPerUser, false);
377381

378382
// Get credential struct from nvm3
379383
chip::StorageKeyName credentialKey = LockUserCredentialMap(userIndex);
380384

381-
uint16_t credentialSize = static_cast<uint16_t>(sizeof(CredentialStruct) * userInStorage.currentCredentialCount);
385+
uint16_t credentialSize = static_cast<uint16_t>(sizeof(CredentialStruct) * mUserInStorage.currentCredentialCount);
382386

383-
error =
384-
chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(credentialKey.KeyName(), mCredentials, credentialSize);
387+
error = mStorage->SyncGetKeyValue(credentialKey.KeyName(), mCredentials, credentialSize);
385388

386389
// If no data is found at credential key
387390
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -391,7 +394,7 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
391394
// Else if KVS read was successful
392395
else if (error == CHIP_NO_ERROR)
393396
{
394-
user.credentials = chip::Span<const CredentialStruct>{ mCredentials, userInStorage.currentCredentialCount };
397+
user.credentials = chip::Span<const CredentialStruct>{ mCredentials, mUserInStorage.currentCredentialCount };
395398
}
396399
else
397400
{
@@ -445,22 +448,22 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip:
445448
return false;
446449
}
447450

448-
memcpy(userInStorage.userName, userName.data(), userName.size());
449-
userInStorage.userNameSize = userName.size();
450-
userInStorage.userUniqueId = uniqueId;
451-
userInStorage.userStatus = userStatus;
452-
userInStorage.userType = usertype;
453-
userInStorage.credentialRule = credentialRule;
454-
userInStorage.lastModifiedBy = modifier;
455-
userInStorage.createdBy = creator;
451+
memcpy(mUserInStorage.userName, userName.data(), userName.size());
452+
mUserInStorage.userNameSize = userName.size();
453+
mUserInStorage.userUniqueId = uniqueId;
454+
mUserInStorage.userStatus = userStatus;
455+
mUserInStorage.userType = usertype;
456+
mUserInStorage.credentialRule = credentialRule;
457+
mUserInStorage.lastModifiedBy = modifier;
458+
mUserInStorage.createdBy = creator;
456459

457-
userInStorage.currentCredentialCount = totalCredentials;
460+
mUserInStorage.currentCredentialCount = totalCredentials;
458461

459462
// Save credential struct in nvm3
460463
chip::StorageKeyName credentialKey = LockUserCredentialMap(userIndex);
461464

462-
error = chip::Server::GetInstance().GetPersistentStorage().SyncSetKeyValue(
463-
credentialKey.KeyName(), credentials, static_cast<uint16_t>(sizeof(CredentialStruct) * totalCredentials));
465+
error = mStorage->SyncSetKeyValue(credentialKey.KeyName(), credentials,
466+
static_cast<uint16_t>(sizeof(CredentialStruct) * totalCredentials));
464467

465468
if ((error != CHIP_NO_ERROR))
466469
{
@@ -471,8 +474,7 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip:
471474
// Save user in nvm3
472475
chip::StorageKeyName userKey = LockUserEndpoint(userIndex, endpointId);
473476

474-
error = chip::Server::GetInstance().GetPersistentStorage().SyncSetKeyValue(userKey.KeyName(), &userInStorage,
475-
static_cast<uint16_t>(sizeof(LockUserInfo)));
477+
error = mStorage->SyncSetKeyValue(userKey.KeyName(), &mUserInStorage, static_cast<uint16_t>(sizeof(LockUserInfo)));
476478

477479
if ((error != CHIP_NO_ERROR))
478480
{
@@ -511,7 +513,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
511513

512514
chip::StorageKeyName key = LockCredentialEndpoint(credentialIndex, credentialType, endpointId);
513515

514-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(key.KeyName(), &credentialInStorage, size);
516+
error = mStorage->SyncGetKeyValue(key.KeyName(), &mCredentialInStorage, size);
515517

516518
// If no data is found at credential key
517519
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -524,7 +526,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
524526
// Else if KVS read was successful
525527
else if (error == CHIP_NO_ERROR)
526528
{
527-
credential.status = credentialInStorage.status;
529+
credential.status = mCredentialInStorage.status;
528530
ChipLogDetail(Zcl, "CredentialStatus: %d, CredentialIndex: %d ", (int) credential.status, credentialIndex);
529531
}
530532
else
@@ -538,10 +540,10 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
538540
ChipLogDetail(Zcl, "Found unoccupied credential ");
539541
return true;
540542
}
541-
credential.credentialType = credentialInStorage.credentialType;
542-
credential.credentialData = chip::ByteSpan{ credentialInStorage.credentialData, credentialInStorage.credentialDataSize };
543-
credential.createdBy = credentialInStorage.createdBy;
544-
credential.lastModifiedBy = credentialInStorage.lastModifiedBy;
543+
credential.credentialType = mCredentialInStorage.credentialType;
544+
credential.credentialData = chip::ByteSpan{ mCredentialInStorage.credentialData, mCredentialInStorage.credentialDataSize };
545+
credential.createdBy = mCredentialInStorage.createdBy;
546+
credential.lastModifiedBy = mCredentialInStorage.lastModifiedBy;
545547
// So far there's no way to actually create the credential outside Matter, so here we always set the creation/modification
546548
// source to Matter
547549
credential.creationSource = DlAssetSource::kMatterIM;
@@ -578,27 +580,20 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
578580
"[credentialStatus=%u,credentialType=%u,credentialDataSize=%u,creator=%d,modifier=%d]",
579581
to_underlying(credentialStatus), to_underlying(credentialType), credentialData.size(), creator, modifier);
580582

581-
credentialInStorage.status = credentialStatus;
582-
credentialInStorage.credentialType = credentialType;
583-
credentialInStorage.createdBy = creator;
584-
credentialInStorage.lastModifiedBy = modifier;
585-
credentialInStorage.credentialDataSize = credentialData.size();
583+
mCredentialInStorage.status = credentialStatus;
584+
mCredentialInStorage.credentialType = credentialType;
585+
mCredentialInStorage.createdBy = creator;
586+
mCredentialInStorage.lastModifiedBy = modifier;
587+
mCredentialInStorage.credentialDataSize = credentialData.size();
586588

587-
memcpy(credentialInStorage.credentialData, credentialData.data(), credentialInStorage.credentialDataSize);
589+
memcpy(mCredentialInStorage.credentialData, credentialData.data(), mCredentialInStorage.credentialDataSize);
588590

589591
chip::StorageKeyName key = LockCredentialEndpoint(credentialIndex, credentialType, endpointId);
590592

591-
if ((error != CHIP_NO_ERROR))
592-
{
593-
ChipLogError(Zcl, "Error reading from KVS key");
594-
return false;
595-
}
596-
597-
chip::Server::GetInstance().GetPersistentStorage().SyncSetKeyValue(key.KeyName(), &credentialInStorage,
598-
static_cast<uint16_t>(sizeof(LockCredentialInfo)));
593+
error = mStorage->SyncSetKeyValue(key.KeyName(), &mCredentialInStorage, static_cast<uint16_t>(sizeof(LockCredentialInfo)));
599594

600-
// Clear credentialInStorage.credentialData
601-
memset(credentialInStorage.credentialData, 0, sizeof(uint8_t) * kMaxCredentialSize);
595+
// Clear mCredentialInStorage.credentialData
596+
memset(mCredentialInStorage.credentialData, 0, sizeof(uint8_t) * kMaxCredentialSize);
602597

603598
if ((error != CHIP_NO_ERROR))
604599
{
@@ -634,8 +629,7 @@ DlStatus LockManager::GetWeekdaySchedule(chip::EndpointId endpointId, uint8_t we
634629

635630
uint16_t scheduleSize = static_cast<uint16_t>(sizeof(WeekDayScheduleInfo));
636631

637-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(scheduleDataKey.KeyName(), &weekDayScheduleInStorage,
638-
scheduleSize);
632+
error = mStorage->SyncGetKeyValue(scheduleDataKey.KeyName(), &weekDayScheduleInStorage, scheduleSize);
639633

640634
// If no data is found at scheduleUserMapKey key
641635
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -692,8 +686,8 @@ DlStatus LockManager::SetWeekdaySchedule(chip::EndpointId endpointId, uint8_t we
692686
// Save schedule data in nvm3
693687
chip::StorageKeyName scheduleDataKey = LockUserWeekDayScheduleEndpoint(userIndex, weekdayIndex, endpointId);
694688

695-
error = chip::Server::GetInstance().GetPersistentStorage().SyncSetKeyValue(scheduleDataKey.KeyName(), &weekDayScheduleInStorage,
696-
static_cast<uint16_t>(sizeof(WeekDayScheduleInfo)));
689+
error = mStorage->SyncSetKeyValue(scheduleDataKey.KeyName(), &weekDayScheduleInStorage,
690+
static_cast<uint16_t>(sizeof(WeekDayScheduleInfo)));
697691

698692
if ((error != CHIP_NO_ERROR))
699693
{
@@ -727,8 +721,7 @@ DlStatus LockManager::GetYeardaySchedule(chip::EndpointId endpointId, uint8_t ye
727721

728722
uint16_t scheduleSize = static_cast<uint16_t>(sizeof(YearDayScheduleInfo));
729723

730-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(scheduleDataKey.KeyName(), &yearDayScheduleInStorage,
731-
scheduleSize);
724+
error = mStorage->SyncGetKeyValue(scheduleDataKey.KeyName(), &yearDayScheduleInStorage, scheduleSize);
732725

733726
// If no data is found at scheduleUserMapKey key
734727
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -781,8 +774,8 @@ DlStatus LockManager::SetYeardaySchedule(chip::EndpointId endpointId, uint8_t ye
781774
// Save schedule data in nvm3
782775
chip::StorageKeyName scheduleDataKey = LockUserYearDayScheduleEndpoint(userIndex, yearDayIndex, endpointId);
783776

784-
error = chip::Server::GetInstance().GetPersistentStorage().SyncSetKeyValue(scheduleDataKey.KeyName(), &yearDayScheduleInStorage,
785-
static_cast<uint16_t>(sizeof(YearDayScheduleInfo)));
777+
error = mStorage->SyncSetKeyValue(scheduleDataKey.KeyName(), &yearDayScheduleInStorage,
778+
static_cast<uint16_t>(sizeof(YearDayScheduleInfo)));
786779

787780
if ((error != CHIP_NO_ERROR))
788781
{
@@ -813,8 +806,7 @@ DlStatus LockManager::GetHolidaySchedule(chip::EndpointId endpointId, uint8_t ho
813806

814807
uint16_t scheduleSize = static_cast<uint16_t>(sizeof(HolidayScheduleInfo));
815808

816-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(scheduleDataKey.KeyName(), &holidayScheduleInStorage,
817-
scheduleSize);
809+
error = mStorage->SyncGetKeyValue(scheduleDataKey.KeyName(), &holidayScheduleInStorage, scheduleSize);
818810

819811
// If no data is found at scheduleUserMapKey key
820812
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -865,8 +857,8 @@ DlStatus LockManager::SetHolidaySchedule(chip::EndpointId endpointId, uint8_t ho
865857
// Save schedule data in nvm3
866858
chip::StorageKeyName scheduleDataKey = LockHolidayScheduleEndpoint(holidayIndex, endpointId);
867859

868-
error = chip::Server::GetInstance().GetPersistentStorage().SyncSetKeyValue(scheduleDataKey.KeyName(), &holidayScheduleInStorage,
869-
static_cast<uint16_t>(sizeof(HolidayScheduleInfo)));
860+
error = mStorage->SyncSetKeyValue(scheduleDataKey.KeyName(), &holidayScheduleInStorage,
861+
static_cast<uint16_t>(sizeof(HolidayScheduleInfo)));
870862

871863
if ((error != CHIP_NO_ERROR))
872864
{
@@ -939,7 +931,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
939931

940932
uint16_t size = static_cast<uint16_t>(sizeof(LockUserInfo));
941933

942-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(userKey.KeyName(), &userInStorage, size);
934+
error = mStorage->SyncGetKeyValue(userKey.KeyName(), &mUserInStorage, size);
943935

944936
// No user exists at this index
945937
if (error != CHIP_NO_ERROR)
@@ -949,19 +941,18 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
949941

950942
chip::StorageKeyName credentialKey = LockUserCredentialMap(userIndex);
951943

952-
uint16_t credentialStructSize = static_cast<uint16_t>(sizeof(CredentialStruct) * userInStorage.currentCredentialCount);
944+
uint16_t credentialStructSize = static_cast<uint16_t>(sizeof(CredentialStruct) * mUserInStorage.currentCredentialCount);
953945

954946
// Get array of credential indices and types associated to user
955-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(credentialKey.KeyName(), &mCredentials,
956-
credentialStructSize);
947+
error = mStorage->SyncGetKeyValue(credentialKey.KeyName(), &mCredentials, credentialStructSize);
957948

958949
// No credential data associated with user
959950
if (error != CHIP_NO_ERROR)
960951
{
961952
continue;
962953
}
963954

964-
for (int j = 0; j < userInStorage.currentCredentialCount; j++)
955+
for (int j = 0; j < mUserInStorage.currentCredentialCount; j++)
965956
{
966957
// If the current credential is a pin type, then check it against pin input. Otherwise ignore
967958
if (mCredentials[j].credentialType == CredentialTypeEnum::kPin)
@@ -972,8 +963,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
972963
chip::StorageKeyName key =
973964
LockCredentialEndpoint(mCredentials[j].credentialIndex, mCredentials[j].credentialType, endpointId);
974965

975-
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(key.KeyName(), &credentialInStorage,
976-
credentialSize);
966+
error = mStorage->SyncGetKeyValue(key.KeyName(), &mCredentialInStorage, credentialSize);
977967

978968
if ((error != CHIP_NO_ERROR) && (error != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND))
979969
{
@@ -982,13 +972,13 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
982972
}
983973

984974
// See if it matches the provided PIN
985-
if (credentialInStorage.status == DlCredentialStatus::kAvailable)
975+
if (mCredentialInStorage.status == DlCredentialStatus::kAvailable)
986976
{
987977
continue;
988978
}
989979

990980
chip::ByteSpan currentCredential =
991-
chip::ByteSpan{ credentialInStorage.credentialData, credentialInStorage.credentialDataSize };
981+
chip::ByteSpan{ mCredentialInStorage.credentialData, mCredentialInStorage.credentialDataSize };
992982

993983
if (currentCredential.data_equal(pin.Value()))
994984
{

0 commit comments

Comments
 (0)