21
21
#include " AppConfig.h"
22
22
#include " AppTask.h"
23
23
#include < app-common/zap-generated/attributes/Accessors.h>
24
- #include < app/server/Server.h>
25
24
#include < cstring>
26
25
#include < lib/support/logging/CHIPLogging.h>
27
26
@@ -38,7 +37,8 @@ LockManager & LockMgr()
38
37
return sLock ;
39
38
}
40
39
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)
42
42
{
43
43
44
44
LockParams = lockParam;
@@ -95,6 +95,10 @@ CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters:
95
95
return APP_ERROR_CREATE_TIMER_FAILED;
96
96
}
97
97
98
+ VerifyOrReturnError (storage != nullptr , CHIP_ERROR_INVALID_ARGUMENT);
99
+
100
+ mStorage = storage;
101
+
98
102
if (state.Value () == DlLockState::kUnlocked )
99
103
mState = kState_UnlockCompleted ;
100
104
else
@@ -335,7 +339,7 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
335
339
336
340
uint16_t size = static_cast <uint16_t >(sizeof (LockUserInfo));
337
341
338
- error = chip::Server::GetInstance (). GetPersistentStorage (). SyncGetKeyValue (userKey.KeyName (), &userInStorage , size);
342
+ error = mStorage -> SyncGetKeyValue (userKey.KeyName (), &mUserInStorage , size);
339
343
// If no data is found at user key
340
344
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
341
345
{
@@ -347,9 +351,9 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
347
351
// Else if KVS read was successful
348
352
else if (error == CHIP_NO_ERROR)
349
353
{
350
- user.userStatus = userInStorage .userStatus ;
354
+ user.userStatus = mUserInStorage .userStatus ;
351
355
352
- if (userInStorage .userStatus == UserStatusEnum::kAvailable )
356
+ if (mUserInStorage .userStatus == UserStatusEnum::kAvailable )
353
357
{
354
358
ChipLogDetail (Zcl, " Found unoccupied user [endpoint=%d]" , endpointId);
355
359
return true ;
@@ -361,27 +365,26 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
361
365
return false ;
362
366
}
363
367
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 ;
368
372
// So far there's no way to actually create the credential outside Matter, so here we always set the creation/modification
369
373
// source to Matter
370
374
user.creationSource = DlAssetSource::kMatterIM ;
371
- user.createdBy = userInStorage .createdBy ;
375
+ user.createdBy = mUserInStorage .createdBy ;
372
376
user.modificationSource = DlAssetSource::kMatterIM ;
373
- user.lastModifiedBy = userInStorage .lastModifiedBy ;
377
+ user.lastModifiedBy = mUserInStorage .lastModifiedBy ;
374
378
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 );
377
381
378
382
// Get credential struct from nvm3
379
383
chip::StorageKeyName credentialKey = LockUserCredentialMap (userIndex);
380
384
381
- uint16_t credentialSize = static_cast <uint16_t >(sizeof (CredentialStruct) * userInStorage .currentCredentialCount );
385
+ uint16_t credentialSize = static_cast <uint16_t >(sizeof (CredentialStruct) * mUserInStorage .currentCredentialCount );
382
386
383
- error =
384
- chip::Server::GetInstance ().GetPersistentStorage ().SyncGetKeyValue (credentialKey.KeyName (), mCredentials , credentialSize);
387
+ error = mStorage ->SyncGetKeyValue (credentialKey.KeyName (), mCredentials , credentialSize);
385
388
386
389
// If no data is found at credential key
387
390
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -391,7 +394,7 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
391
394
// Else if KVS read was successful
392
395
else if (error == CHIP_NO_ERROR)
393
396
{
394
- user.credentials = chip::Span<const CredentialStruct>{ mCredentials , userInStorage .currentCredentialCount };
397
+ user.credentials = chip::Span<const CredentialStruct>{ mCredentials , mUserInStorage .currentCredentialCount };
395
398
}
396
399
else
397
400
{
@@ -445,22 +448,22 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip:
445
448
return false ;
446
449
}
447
450
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;
456
459
457
- userInStorage .currentCredentialCount = totalCredentials;
460
+ mUserInStorage .currentCredentialCount = totalCredentials;
458
461
459
462
// Save credential struct in nvm3
460
463
chip::StorageKeyName credentialKey = LockUserCredentialMap (userIndex);
461
464
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));
464
467
465
468
if ((error != CHIP_NO_ERROR))
466
469
{
@@ -471,8 +474,7 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip:
471
474
// Save user in nvm3
472
475
chip::StorageKeyName userKey = LockUserEndpoint (userIndex, endpointId);
473
476
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)));
476
478
477
479
if ((error != CHIP_NO_ERROR))
478
480
{
@@ -511,7 +513,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
511
513
512
514
chip::StorageKeyName key = LockCredentialEndpoint (credentialIndex, credentialType, endpointId);
513
515
514
- error = chip::Server::GetInstance (). GetPersistentStorage (). SyncGetKeyValue (key.KeyName (), &credentialInStorage , size);
516
+ error = mStorage -> SyncGetKeyValue (key.KeyName (), &mCredentialInStorage , size);
515
517
516
518
// If no data is found at credential key
517
519
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -524,7 +526,7 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
524
526
// Else if KVS read was successful
525
527
else if (error == CHIP_NO_ERROR)
526
528
{
527
- credential.status = credentialInStorage .status ;
529
+ credential.status = mCredentialInStorage .status ;
528
530
ChipLogDetail (Zcl, " CredentialStatus: %d, CredentialIndex: %d " , (int ) credential.status , credentialIndex);
529
531
}
530
532
else
@@ -538,10 +540,10 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential
538
540
ChipLogDetail (Zcl, " Found unoccupied credential " );
539
541
return true ;
540
542
}
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 ;
545
547
// So far there's no way to actually create the credential outside Matter, so here we always set the creation/modification
546
548
// source to Matter
547
549
credential.creationSource = DlAssetSource::kMatterIM ;
@@ -578,27 +580,20 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential
578
580
" [credentialStatus=%u,credentialType=%u,credentialDataSize=%u,creator=%d,modifier=%d]" ,
579
581
to_underlying (credentialStatus), to_underlying (credentialType), credentialData.size (), creator, modifier);
580
582
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 ();
586
588
587
- memcpy (credentialInStorage .credentialData , credentialData.data (), credentialInStorage .credentialDataSize );
589
+ memcpy (mCredentialInStorage .credentialData , credentialData.data (), mCredentialInStorage .credentialDataSize );
588
590
589
591
chip::StorageKeyName key = LockCredentialEndpoint (credentialIndex, credentialType, endpointId);
590
592
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)));
599
594
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 );
602
597
603
598
if ((error != CHIP_NO_ERROR))
604
599
{
@@ -634,8 +629,7 @@ DlStatus LockManager::GetWeekdaySchedule(chip::EndpointId endpointId, uint8_t we
634
629
635
630
uint16_t scheduleSize = static_cast <uint16_t >(sizeof (WeekDayScheduleInfo));
636
631
637
- error = chip::Server::GetInstance ().GetPersistentStorage ().SyncGetKeyValue (scheduleDataKey.KeyName (), &weekDayScheduleInStorage,
638
- scheduleSize);
632
+ error = mStorage ->SyncGetKeyValue (scheduleDataKey.KeyName (), &weekDayScheduleInStorage, scheduleSize);
639
633
640
634
// If no data is found at scheduleUserMapKey key
641
635
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -692,8 +686,8 @@ DlStatus LockManager::SetWeekdaySchedule(chip::EndpointId endpointId, uint8_t we
692
686
// Save schedule data in nvm3
693
687
chip::StorageKeyName scheduleDataKey = LockUserWeekDayScheduleEndpoint (userIndex, weekdayIndex, endpointId);
694
688
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)));
697
691
698
692
if ((error != CHIP_NO_ERROR))
699
693
{
@@ -727,8 +721,7 @@ DlStatus LockManager::GetYeardaySchedule(chip::EndpointId endpointId, uint8_t ye
727
721
728
722
uint16_t scheduleSize = static_cast <uint16_t >(sizeof (YearDayScheduleInfo));
729
723
730
- error = chip::Server::GetInstance ().GetPersistentStorage ().SyncGetKeyValue (scheduleDataKey.KeyName (), &yearDayScheduleInStorage,
731
- scheduleSize);
724
+ error = mStorage ->SyncGetKeyValue (scheduleDataKey.KeyName (), &yearDayScheduleInStorage, scheduleSize);
732
725
733
726
// If no data is found at scheduleUserMapKey key
734
727
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -781,8 +774,8 @@ DlStatus LockManager::SetYeardaySchedule(chip::EndpointId endpointId, uint8_t ye
781
774
// Save schedule data in nvm3
782
775
chip::StorageKeyName scheduleDataKey = LockUserYearDayScheduleEndpoint (userIndex, yearDayIndex, endpointId);
783
776
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)));
786
779
787
780
if ((error != CHIP_NO_ERROR))
788
781
{
@@ -813,8 +806,7 @@ DlStatus LockManager::GetHolidaySchedule(chip::EndpointId endpointId, uint8_t ho
813
806
814
807
uint16_t scheduleSize = static_cast <uint16_t >(sizeof (HolidayScheduleInfo));
815
808
816
- error = chip::Server::GetInstance ().GetPersistentStorage ().SyncGetKeyValue (scheduleDataKey.KeyName (), &holidayScheduleInStorage,
817
- scheduleSize);
809
+ error = mStorage ->SyncGetKeyValue (scheduleDataKey.KeyName (), &holidayScheduleInStorage, scheduleSize);
818
810
819
811
// If no data is found at scheduleUserMapKey key
820
812
if (error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -865,8 +857,8 @@ DlStatus LockManager::SetHolidaySchedule(chip::EndpointId endpointId, uint8_t ho
865
857
// Save schedule data in nvm3
866
858
chip::StorageKeyName scheduleDataKey = LockHolidayScheduleEndpoint (holidayIndex, endpointId);
867
859
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)));
870
862
871
863
if ((error != CHIP_NO_ERROR))
872
864
{
@@ -939,7 +931,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
939
931
940
932
uint16_t size = static_cast <uint16_t >(sizeof (LockUserInfo));
941
933
942
- error = chip::Server::GetInstance (). GetPersistentStorage (). SyncGetKeyValue (userKey.KeyName (), &userInStorage , size);
934
+ error = mStorage -> SyncGetKeyValue (userKey.KeyName (), &mUserInStorage , size);
943
935
944
936
// No user exists at this index
945
937
if (error != CHIP_NO_ERROR)
@@ -949,19 +941,18 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
949
941
950
942
chip::StorageKeyName credentialKey = LockUserCredentialMap (userIndex);
951
943
952
- uint16_t credentialStructSize = static_cast <uint16_t >(sizeof (CredentialStruct) * userInStorage .currentCredentialCount );
944
+ uint16_t credentialStructSize = static_cast <uint16_t >(sizeof (CredentialStruct) * mUserInStorage .currentCredentialCount );
953
945
954
946
// 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);
957
948
958
949
// No credential data associated with user
959
950
if (error != CHIP_NO_ERROR)
960
951
{
961
952
continue ;
962
953
}
963
954
964
- for (int j = 0 ; j < userInStorage .currentCredentialCount ; j++)
955
+ for (int j = 0 ; j < mUserInStorage .currentCredentialCount ; j++)
965
956
{
966
957
// If the current credential is a pin type, then check it against pin input. Otherwise ignore
967
958
if (mCredentials [j].credentialType == CredentialTypeEnum::kPin )
@@ -972,8 +963,7 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
972
963
chip::StorageKeyName key =
973
964
LockCredentialEndpoint (mCredentials [j].credentialIndex , mCredentials [j].credentialType , endpointId);
974
965
975
- error = chip::Server::GetInstance ().GetPersistentStorage ().SyncGetKeyValue (key.KeyName (), &credentialInStorage,
976
- credentialSize);
966
+ error = mStorage ->SyncGetKeyValue (key.KeyName (), &mCredentialInStorage , credentialSize);
977
967
978
968
if ((error != CHIP_NO_ERROR) && (error != CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND))
979
969
{
@@ -982,13 +972,13 @@ bool LockManager::setLockState(chip::EndpointId endpointId, const Nullable<chip:
982
972
}
983
973
984
974
// See if it matches the provided PIN
985
- if (credentialInStorage .status == DlCredentialStatus::kAvailable )
975
+ if (mCredentialInStorage .status == DlCredentialStatus::kAvailable )
986
976
{
987
977
continue ;
988
978
}
989
979
990
980
chip::ByteSpan currentCredential =
991
- chip::ByteSpan{ credentialInStorage .credentialData , credentialInStorage .credentialDataSize };
981
+ chip::ByteSpan{ mCredentialInStorage .credentialData , mCredentialInStorage .credentialDataSize };
992
982
993
983
if (currentCredential.data_equal (pin.Value ()))
994
984
{
0 commit comments