From f5cc5910c977bd54f8ffdb9cdf661d03af53b774 Mon Sep 17 00:00:00 2001 From: alvin hsiao Date: Mon, 27 Jun 2022 13:36:26 +0800 Subject: [PATCH] [Infineon] Lock-app updates, adding multiple users and credentials for CYW30739 * Fix mCredentials, fix NVM flash saving * restyle * cleanup * remove unused array * remove TotalCredentials key --- .../lock-app/cyw30739/include/LockManager.h | 32 +-- .../lock-app/cyw30739/include/chip_lock.h | 1 + .../lock-app/cyw30739/src/LockManager.cpp | 202 ++++++++++++------ .../lock-app/cyw30739/src/ZclCallbacks.cpp | 12 +- examples/lock-app/cyw30739/src/main.cpp | 56 +++-- 5 files changed, 193 insertions(+), 110 deletions(-) diff --git a/examples/lock-app/cyw30739/include/LockManager.h b/examples/lock-app/cyw30739/include/LockManager.h index 596e5e9f42d035..28cd413862e7c0 100644 --- a/examples/lock-app/cyw30739/include/LockManager.h +++ b/examples/lock-app/cyw30739/include/LockManager.h @@ -28,7 +28,13 @@ using namespace ::chip; +// Currently up to 10 users are support on the CYW30739 platform +#define DOOR_LOCK_MAX_USERS 10 #define DOOR_LOCK_MAX_CREDENTIAL_SIZE 8 +#define MININUM_USER_INDEX 1 +#define MINIMUM_CREDENTIAL_INDEX 1 +#define MAX_CREDENTIAL_PER_USER 10 +#define MAX_CREDENTIALS 50 static constexpr size_t DOOR_LOCK_CREDENTIAL_INFO_MAX_DATA_SIZE = 20; @@ -59,7 +65,7 @@ class LockManager } Actor; CHIP_ERROR Init(chip::app::DataModel::Nullable state, - uint8_t maxNumberOfCredentialsPerUser); + uint8_t maxNumberOfCredentialsPerUser, uint16_t numberOfSupportedUsers); bool NextState(); bool IsActionInProgress(); bool InitiateAction(int32_t aActor, Action_t aAction); @@ -71,16 +77,16 @@ class LockManager bool Lock(chip::EndpointId endpointId, const Optional & pin, DlOperationError & err); bool Unlock(chip::EndpointId endpointId, const Optional & pin, DlOperationError & err); - bool GetUser(uint16_t userIndex, EmberAfPluginDoorLockUserInfo & user) const; - bool SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::FabricIndex modifier, const chip::CharSpan & userName, - uint32_t uniqueId, DlUserStatus userStatus, DlUserType usertype, DlCredentialRule credentialRule, - const DlCredential * credentials, size_t totalCredentials); + bool GetUser(chip::EndpointId endpointId, uint16_t userIndex, EmberAfPluginDoorLockUserInfo & user) const; + bool SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip::FabricIndex creator, chip::FabricIndex modifier, + const chip::CharSpan & userName, uint32_t uniqueId, DlUserStatus userStatus, DlUserType usertype, + DlCredentialRule credentialRule, const DlCredential * credentials, size_t totalCredentials); bool GetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, DlCredentialType credentialType, EmberAfPluginDoorLockCredentialInfo & credential) const; - bool SetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, DlCredentialStatus credentialStatus, - DlCredentialType credentialType, const chip::ByteSpan & credentialData); + bool SetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, chip::FabricIndex creator, chip::FabricIndex modifier, + DlCredentialStatus credentialStatus, DlCredentialType credentialType, const chip::ByteSpan & credentialData); bool setLockState(chip::EndpointId endpointId, DlLockState lockState, const Optional & pin, DlOperationError & err); @@ -102,13 +108,15 @@ class LockManager static void TimerEventHandler(WICED_TIMER_PARAM_TYPE cb_params); static int ActuatorMovementTimerEventHandler(void * aEvent); - EmberAfPluginDoorLockUserInfo mLockUser; - EmberAfPluginDoorLockCredentialInfo mLockCredentials; + EmberAfPluginDoorLockUserInfo mLockUsers[DOOR_LOCK_MAX_USERS]; + EmberAfPluginDoorLockCredentialInfo mLockCredentials[MAX_CREDENTIALS]; - char mUserName[DOOR_LOCK_MAX_USER_NAME_SIZE]; - uint8_t mCredentialData[DOOR_LOCK_MAX_CREDENTIAL_SIZE]; - chip::Platform::ScopedMemoryBuffer mCredentials; uint8_t mMaxCredentialsPerUser; + uint16_t mMaxUsers; + + char mUserNames[ArraySize(mLockUsers)][DOOR_LOCK_MAX_USER_NAME_SIZE]; + uint8_t mCredentialData[MAX_CREDENTIALS][DOOR_LOCK_MAX_CREDENTIAL_SIZE]; + chip::Platform::ScopedMemoryBuffer mCredentials[MAX_CREDENTIAL_PER_USER]; static LockManager sLock; }; diff --git a/examples/lock-app/cyw30739/include/chip_lock.h b/examples/lock-app/cyw30739/include/chip_lock.h index b752e02e2fe067..85aaddb87057aa 100644 --- a/examples/lock-app/cyw30739/include/chip_lock.h +++ b/examples/lock-app/cyw30739/include/chip_lock.h @@ -23,6 +23,7 @@ // Application-defined error codes in the CHIP_ERROR space #define APP_ERROR_INIT_TIMER_FAILED CHIP_ERROR_NO_MEMORY // should use CHIP_APPLICATION_ERROR when it's ready +#define APP_ERROR_ALLOCATION_FAILED CHIP_APPLICATION_ERROR(0x07) // Application configurations #define ACTUATOR_MOVEMENT_PERIOS_MS 1000 diff --git a/examples/lock-app/cyw30739/src/LockManager.cpp b/examples/lock-app/cyw30739/src/LockManager.cpp index d5af8592543829..a064fbb03d513a 100644 --- a/examples/lock-app/cyw30739/src/LockManager.cpp +++ b/examples/lock-app/cyw30739/src/LockManager.cpp @@ -19,6 +19,7 @@ #include "LockManager.h" #include "wiced_bt_event.h" +#include #include #include @@ -29,23 +30,35 @@ LockManager LockManager::sLock; wiced_timer_t sLockTimer; CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable state, - uint8_t maxNumberOfCredentialsPerUser) + uint8_t maxNumberOfCredentialsPerUser, uint16_t numberOfSupportedUsers) { - // Allocate buffer for credentials - if (!mCredentials.Alloc(maxNumberOfCredentialsPerUser)) + for (uint8_t i = 0; i < ArraySize(mLockUsers); i++) { - printf("Failed to allocate array for lock credentials"); - return CHIP_ERROR_NO_MEMORY; + // Allocate buffer for credentials + if (!mCredentials[i].Alloc(maxNumberOfCredentialsPerUser)) + { + ChipLogError(Zcl, "Failed to allocate array for lock credentials"); + return APP_ERROR_ALLOCATION_FAILED; + } } + mMaxCredentialsPerUser = maxNumberOfCredentialsPerUser; + mMaxUsers = numberOfSupportedUsers; + if (mMaxUsers > ArraySize(mLockUsers)) + { + ChipLogError(Zcl, "Max number of users is greater than %d, the maximum amount of users currently supported on this platform", + ArraySize(mLockUsers)); + return APP_ERROR_ALLOCATION_FAILED; + } + wiced_result_t result; // Create wiced timer for lock timer. result = wiced_init_timer(&sLockTimer, TimerEventHandler, (WICED_TIMER_PARAM_TYPE) this, WICED_MILLI_SECONDS_TIMER); if (result != WICED_BT_SUCCESS) { - printf("sLockTimer timer create failed"); + ChipLogError(Zcl, "sLockTimer timer create failed"); return APP_ERROR_INIT_TIMER_FAILED; } @@ -60,17 +73,20 @@ CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable(&mLockUser), - sizeof(EmberAfPluginDoorLockUserInfo), outLen); + CYW30739Config::ReadConfigValueBin(CYW30739Config::kConfigKey_LockUser, reinterpret_cast(&mLockUsers), + sizeof(EmberAfPluginDoorLockUserInfo) * ArraySize(mLockUsers), outLen); + CYW30739Config::ReadConfigValueBin(CYW30739Config::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), - sizeof(EmberAfPluginDoorLockCredentialInfo), outLen); + sizeof(EmberAfPluginDoorLockCredentialInfo) * ArraySize(mLockCredentials), outLen); - CYW30739Config::ReadConfigValueStr(CYW30739Config::kConfigKey_LockUserName, mUserName, DOOR_LOCK_USER_NAME_BUFFER_SIZE, outLen); + CYW30739Config::ReadConfigValueBin(CYW30739Config::kConfigKey_LockUserName, reinterpret_cast(mUserNames), + sizeof(mUserNames), outLen); - CYW30739Config::ReadConfigValueBin(CYW30739Config::kConfigKey_CredentialData, mCredentialData, sizeof(mCredentialData), outLen); + CYW30739Config::ReadConfigValueBin(CYW30739Config::kConfigKey_CredentialData, reinterpret_cast(mCredentialData), + sizeof(mCredentialData), outLen); - CYW30739Config::ReadConfigValueBin(CYW30739Config::kConfigKey_UserCredentials, reinterpret_cast(mCredentials.Get()), - sizeof(DlCredential), outLen); + CYW30739Config::ReadConfigValueBin(CYW30739Config::kConfigKey_UserCredentials, reinterpret_cast(mCredentials[0].Get()), + sizeof(DlCredential) * mMaxUsers * mMaxCredentialsPerUser, outLen); return true; } @@ -131,13 +147,13 @@ void LockManager::StartTimer(uint32_t aTimeoutMs) { if (wiced_is_timer_in_use(&sLockTimer)) { - printf("app timer already started!\n"); + ChipLogError(Zcl, "app timer already started!"); CancelTimer(); } if (wiced_start_timer(&sLockTimer, aTimeoutMs) != WICED_BT_SUCCESS) { - printf("sLockTimer timer start() failed\n"); + ChipLogError(Zcl, "sLockTimer timer start() failed"); return; } } @@ -146,7 +162,7 @@ void LockManager::CancelTimer(void) { if (wiced_stop_timer(&sLockTimer) != WICED_BT_SUCCESS) { - printf("Lock timer timer stop() failed\n"); + ChipLogError(Zcl, "Lock timer timer stop() failed"); return; } } @@ -163,7 +179,7 @@ void LockManager::TimerEventHandler(WICED_TIMER_PARAM_TYPE cb_params) if (wiced_app_event_serialize(fn, (void *) lock) != WICED_TRUE) { - printf("wiced_app_event_serialize failed\n"); + ChipLogError(Zcl, "wiced_app_event_serialize failed "); } } @@ -205,16 +221,19 @@ bool LockManager::Unlock(chip::EndpointId endpointId, const Optional(user.userName.size()), user.userName.data(), user.credentials.size(), + endpointId, static_cast(user.userName.size()), user.userName.data(), user.credentials.size(), user.userUniqueId, to_underlying(user.userType), to_underlying(user.credentialRule), user.createdBy, user.lastModifiedBy); return true; } -bool LockManager::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::FabricIndex modifier, +bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip::FabricIndex creator, chip::FabricIndex modifier, const chip::CharSpan & userName, uint32_t uniqueId, DlUserStatus userStatus, DlUserType usertype, DlCredentialRule credentialRule, const DlCredential * credentials, size_t totalCredentials) { @@ -245,27 +268,29 @@ bool LockManager::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::F "Door Lock App: LockManager::SetUser " "[endpoint=%d,userIndex=%d,creator=%d,modifier=%d,userName=%s,uniqueId=%ld " "userStatus=%u,userType=%u,credentialRule=%u,credentials=%p,totalCredentials=%u]", - mEndpointId, userIndex, creator, modifier, userName.data(), uniqueId, to_underlying(userStatus), + endpointId, userIndex, creator, modifier, userName.data(), uniqueId, to_underlying(userStatus), to_underlying(usertype), to_underlying(credentialRule), credentials, totalCredentials); - auto & userInStorage = mLockUser; + uint16_t adjustedUserIndex = userIndex - 1; + + // door-lock-server checks for valid user index + auto & userInStorage = mLockUsers[adjustedUserIndex]; if (userName.size() > DOOR_LOCK_MAX_USER_NAME_SIZE) { - ChipLogError(Zcl, "Cannot set user - user name is too long [endpoint=%d,index=%d]", mEndpointId, userIndex); + ChipLogError(Zcl, "Cannot set user - user name is too long [endpoint=%d,index=%d]", endpointId, adjustedUserIndex); return false; } if (totalCredentials > mMaxCredentialsPerUser) { ChipLogError(Zcl, "Cannot set user - total number of credentials is too big [endpoint=%d,index=%d,totalCredentials=%u]", - mEndpointId, userIndex, totalCredentials); + endpointId, adjustedUserIndex, totalCredentials); return false; } - chip::Platform::CopyString(mUserName, userName); - mUserName[userName.size()] = 0; - userInStorage.userName = chip::CharSpan(mUserName, userName.size()); + chip::Platform::CopyString(mUserNames[adjustedUserIndex], userName); + userInStorage.userName = chip::CharSpan(mUserNames[adjustedUserIndex], userName.size()); userInStorage.userUniqueId = uniqueId; userInStorage.userStatus = userStatus; userInStorage.userType = usertype; @@ -275,23 +300,23 @@ bool LockManager::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::F for (size_t i = 0; i < totalCredentials; ++i) { - mCredentials[i] = credentials[i]; - mCredentials[i].CredentialType = 1; - mCredentials[i].CredentialIndex = i + 1; + mCredentials[adjustedUserIndex][i] = credentials[i]; + mCredentials[adjustedUserIndex][i].CredentialType = 1; + mCredentials[adjustedUserIndex][i].CredentialIndex = i + 1; } - userInStorage.credentials = chip::Span(mCredentials.Get(), totalCredentials); + userInStorage.credentials = chip::Span(mCredentials[adjustedUserIndex].Get(), totalCredentials); // Save user information in NVM flash - CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_LockUser, reinterpret_cast(&userInStorage), - sizeof(EmberAfPluginDoorLockUserInfo)); + CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_LockUser, reinterpret_cast(&mLockUsers), + sizeof(EmberAfPluginDoorLockUserInfo) * mMaxUsers); - CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_UserCredentials, - reinterpret_cast(mCredentials.Get()), sizeof(DlCredential)); + CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_UserCredentials, reinterpret_cast(mCredentials[0].Get()), sizeof(DlCredential) * mMaxUsers * mMaxCredentialsPerUser); - CYW30739Config::WriteConfigValueStr(CYW30739Config::kConfigKey_LockUserName, mUserName, sizeof(userName.size())); + CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_LockUserName, reinterpret_cast(mUserNames), + sizeof(mUserNames)); - ChipLogProgress(Zcl, "Successfully set the user [mEndpointId=%d,index=%d]", mEndpointId, userIndex); + ChipLogProgress(Zcl, "Successfully set the user [mEndpointId=%d,index=%d]", endpointId, adjustedUserIndex); return true; } @@ -299,11 +324,17 @@ bool LockManager::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::F bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, DlCredentialType credentialType, EmberAfPluginDoorLockCredentialInfo & credential) const { - ChipLogProgress(Zcl, "Lock App: LockManager::GetCredential [credentialType=%u]", to_underlying(credentialType)); + uint16_t adjustedCredentialIndex = credentialIndex - 1; + + ChipLogProgress(Zcl, "Lock App: LockManager::GetCredential [credentialType=%u], credentialIndex=%d", + to_underlying(credentialType), adjustedCredentialIndex); - const auto & credentialInStorage = mLockCredentials; + // door-lock-server checks for valid credential index + const auto & credentialInStorage = mLockCredentials[adjustedCredentialIndex]; credential.status = credentialInStorage.status; + ChipLogDetail(Zcl, "CredentialStatus: %d, CredentialIndex: %d ", (int) credential.status, adjustedCredentialIndex); + if (DlCredentialStatus::kAvailable == credential.status) { ChipLogDetail(Zcl, "Found unoccupied credential "); @@ -311,6 +342,12 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential } credential.credentialType = credentialInStorage.credentialType; credential.credentialData = credentialInStorage.credentialData; + credential.createdBy = credentialInStorage.createdBy; + credential.lastModifiedBy = credentialInStorage.lastModifiedBy; + // So far there's no way to actually create the credential outside Matter, so here we always set the creation/modification + // source to Matter + credential.creationSource = DlAssetSource::kMatterIM; + credential.modificationSource = DlAssetSource::kMatterIM; ChipLogDetail(Zcl, "Found occupied credential [type=%u,dataSize=%u]", to_underlying(credential.credentialType), credential.credentialData.size()); @@ -318,15 +355,20 @@ bool LockManager::GetCredential(chip::EndpointId endpointId, uint16_t credential return true; } -bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, DlCredentialStatus credentialStatus, - DlCredentialType credentialType, const chip::ByteSpan & credentialData) +bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, chip::FabricIndex creator, + chip::FabricIndex modifier, DlCredentialStatus credentialStatus, DlCredentialType credentialType, + const chip::ByteSpan & credentialData) { ChipLogProgress(Zcl, "Door Lock App: LockManager::SetCredential " - "[credentialStatus=%u,credentialType=%u,credentialDataSize=%u]", - to_underlying(credentialStatus), to_underlying(credentialType), credentialData.size()); + "[credentialStatus=%u,credentialType=%u,credentialDataSize=%u,creator=%d,modifier=%d]", + to_underlying(credentialStatus), to_underlying(credentialType), credentialData.size(), creator, modifier); + + uint16_t adjustedCredentialIndex = credentialIndex - 1; + + // door-lock-server checks for valid credential index + auto & credentialInStorage = mLockCredentials[adjustedCredentialIndex]; - auto & credentialInStorage = mLockCredentials; if (credentialData.size() > DOOR_LOCK_CREDENTIAL_INFO_MAX_DATA_SIZE) { ChipLogError(Zcl, @@ -337,19 +379,18 @@ bool LockManager::SetCredential(chip::EndpointId endpointId, uint16_t credential } credentialInStorage.status = credentialStatus; credentialInStorage.credentialType = credentialType; + credentialInStorage.createdBy = creator; + credentialInStorage.lastModifiedBy = modifier; - memcpy(mCredentialData, credentialData.data(), credentialData.size()); - mCredentialData[credentialData.size()] = 0; + memcpy(mCredentialData[adjustedCredentialIndex], credentialData.data(), credentialData.size()); + credentialInStorage.credentialData = chip::ByteSpan{ mCredentialData[adjustedCredentialIndex], credentialData.size() }; - credentialInStorage.credentialData = chip::ByteSpan{ mCredentialData, credentialData.size() }; + // Save credential information in NVM flash + CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_Credential, reinterpret_cast(&mLockCredentials), + sizeof(EmberAfPluginDoorLockCredentialInfo) * mMaxCredentialsPerUser); - // Save user information in NVM flash - CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_Credential, - reinterpret_cast(&credentialInStorage), - sizeof(EmberAfPluginDoorLockCredentialInfo)); - - CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_CredentialData, - reinterpret_cast(&mCredentialData), credentialData.size()); + CYW30739Config::WriteConfigValueBin(CYW30739Config::kConfigKey_CredentialData, reinterpret_cast(&mCredentialData), + sizeof(mCredentialData)); ChipLogProgress(Zcl, "Successfully set the credential [credentialType=%u]", to_underlying(credentialType)); @@ -378,30 +419,51 @@ bool LockManager::setLockState(chip::EndpointId endpointId, DlLockState lockStat if (mState == kState_UnlockCompleted) curState = DlLockState::kUnlocked; - if (curState == lockState) + if ((curState == lockState) && (curState == DlLockState::kLocked)) { - ChipLogDetail(Zcl, "Door Lock App: door is already locked, set lock state to \"%s\" [endpointId=%d]", - lockStateToString(lockState), mEndpointId); + ChipLogDetail(Zcl, "Door Lock App: door is already locked, ignoring command to set lock state to \"%s\" [endpointId=%d]", + lockStateToString(lockState), endpointId); } + else if ((curState == lockState) && (curState == DlLockState::kUnlocked)) + { + ChipLogDetail(Zcl, + "Door Lock App: door is already unlocked, ignoring command to set unlock state to \"%s\" [endpointId=%d]", + lockStateToString(lockState), endpointId); + return true; + } + + // Check the RequirePINforRemoteOperation attribute + bool requirePin = false; + // chip::app::Clusters::DoorLock::Attributes::RequirePINforRemoteOperation::Get(endpointId, &requirePin); + // If a pin code is not given if (!pin.HasValue()) { - ChipLogDetail(Zcl, "Door Lock App: PIN code is not specified, setting door lock state to \"%s\" [endpointId=%d]", - lockStateToString(lockState), mEndpointId); + ChipLogDetail(Zcl, "Door Lock App: PIN code is not specified, but it is required [endpointId=%d]", mEndpointId); curState = lockState; - return true; + // If a pin code is not required + if (!requirePin) + { + ChipLogDetail(Zcl, "Door Lock App: setting door lock state to \"%s\" [endpointId=%d]", lockStateToString(lockState), + endpointId); + curState = lockState; + return true; + } + + return false; } // Check the PIN code - for (uint8_t i; i < 10; i++) + for (uint8_t i = 0; i < mMaxCredentialsPerUser; i++) { - if (mLockCredentials.credentialType != DlCredentialType::kPin || mLockCredentials.status == DlCredentialStatus::kAvailable) + if (mLockCredentials[i].credentialType != DlCredentialType::kPin || + mLockCredentials[i].status == DlCredentialStatus::kAvailable) { continue; } - if (mLockCredentials.credentialData.data_equal(pin.Value())) + if (mLockCredentials[i].credentialData.data_equal(pin.Value())) { ChipLogDetail(Zcl, "Lock App: specified PIN code was found in the database, setting lock state to \"%s\" [endpointId=%d]", diff --git a/examples/lock-app/cyw30739/src/ZclCallbacks.cpp b/examples/lock-app/cyw30739/src/ZclCallbacks.cpp index ecbc2f78804b24..2b06dd4e1f3d1f 100644 --- a/examples/lock-app/cyw30739/src/ZclCallbacks.cpp +++ b/examples/lock-app/cyw30739/src/ZclCallbacks.cpp @@ -94,15 +94,17 @@ bool emberAfPluginDoorLockGetCredential(chip::EndpointId endpointId, uint16_t cr return LockMgr().GetCredential(endpointId, credentialIndex, credentialType, credential); } -bool emberAfPluginDoorLockSetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, DlCredentialStatus credentialStatus, +bool emberAfPluginDoorLockSetCredential(chip::EndpointId endpointId, uint16_t credentialIndex, chip::FabricIndex creator, + chip::FabricIndex modifier, DlCredentialStatus credentialStatus, DlCredentialType credentialType, const chip::ByteSpan & credentialData) { - return LockMgr().SetCredential(endpointId, credentialIndex, credentialStatus, credentialType, credentialData); + return LockMgr().SetCredential(endpointId, credentialIndex, creator, modifier, credentialStatus, credentialType, + credentialData); } bool emberAfPluginDoorLockGetUser(chip::EndpointId endpointId, uint16_t userIndex, EmberAfPluginDoorLockUserInfo & user) { - return LockMgr().GetUser(userIndex, user); + return LockMgr().GetUser(endpointId, userIndex, user); } bool emberAfPluginDoorLockSetUser(chip::EndpointId endpointId, uint16_t userIndex, chip::FabricIndex creator, @@ -111,8 +113,8 @@ bool emberAfPluginDoorLockSetUser(chip::EndpointId endpointId, uint16_t userInde const DlCredential * credentials, size_t totalCredentials) { - return LockMgr().SetUser(userIndex, creator, modifier, userName, uniqueId, userStatus, usertype, credentialRule, credentials, - totalCredentials); + return LockMgr().SetUser(endpointId, userIndex, creator, modifier, userName, uniqueId, userStatus, usertype, credentialRule, + credentials, totalCredentials); } // TODO: These functions will be supported by door-lock-server in the future. These are set to return failure until implemented. diff --git a/examples/lock-app/cyw30739/src/main.cpp b/examples/lock-app/cyw30739/src/main.cpp index c214e63796e07e..3e690ebd27f8ed 100644 --- a/examples/lock-app/cyw30739/src/main.cpp +++ b/examples/lock-app/cyw30739/src/main.cpp @@ -84,20 +84,20 @@ APPLICATION_START() wiced_result_t result; uint32_t i; - printf("\nChipLock App starting\n"); + ChipLogProgress(Zcl, "ChipLock App starting"); mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree); err = chip::Platform::MemoryInit(); if (err != CHIP_NO_ERROR) { - printf("ERROR MemoryInit %ld\n", err.AsInteger()); + ChipLogError(Zcl, "ERROR MemoryInit %ld", err.AsInteger()); } result = app_button_init(); if (result != WICED_SUCCESS) { - printf("ERROR app_button_init %d\n", result); + ChipLogError(Zcl, "ERROR app_button_init %d", result); } /* Init. LED Manager. */ @@ -105,44 +105,44 @@ APPLICATION_START() { result = wiced_led_manager_init(&chip_lighting_led_config[i]); if (result != WICED_SUCCESS) - printf("wiced_led_manager_init fail i=%ld, (%d)\n", i, result); + ChipLogError(Zcl, "wiced_led_manager_init fail i=%ld, (%d)", i, result); } - printf("Initializing CHIP\n"); + ChipLogProgress(Zcl, "Initializing CHIP"); err = PlatformMgr().InitChipStack(); if (err != CHIP_NO_ERROR) { - printf("ERROR InitChipStack %ld\n", err.AsInteger()); + ChipLogError(Zcl, "ERROR InitChipStack %ld", err.AsInteger()); } #if CHIP_DEVICE_CONFIG_ENABLE_THREAD - printf("Initializing OpenThread stack\n"); + ChipLogProgress(Zcl, "Initializing OpenThread stack"); err = ThreadStackMgr().InitThreadStack(); if (err != CHIP_NO_ERROR) { - printf("ERROR InitThreadStack %ld\n", err.AsInteger()); + ChipLogError(Zcl, "ERROR InitThreadStack %ld", err.AsInteger()); } #endif err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router); if (err != CHIP_NO_ERROR) { - printf("ERROR SetThreadDeviceType %ld\n", err.AsInteger()); + ChipLogError(Zcl, "ERROR SetThreadDeviceType %ld", err.AsInteger()); } - printf("Starting event loop task\n"); + ChipLogProgress(Zcl, "Starting event loop task"); err = PlatformMgr().StartEventLoopTask(); if (err != CHIP_NO_ERROR) { - printf("ERROR StartEventLoopTask %ld\n", err.AsInteger()); + ChipLogError(Zcl, "ERROR StartEventLoopTask %ld", err.AsInteger()); } #if CHIP_DEVICE_CONFIG_ENABLE_THREAD - printf("Starting thread task\n"); + ChipLogProgress(Zcl, "Starting thread task"); err = ThreadStackMgr().StartThreadTask(); if (err != CHIP_NO_ERROR) { - printf("ERROR StartThreadTask %ld\n", err.AsInteger()); + ChipLogError(Zcl, "ERROR StartThreadTask %ld", err.AsInteger()); } #endif @@ -151,7 +151,7 @@ APPLICATION_START() const int ret = Engine::Root().Init(); if (!chip::ChipError::IsSuccess(ret)) { - printf("ERROR Shell Init %d\n", ret); + ChipLogError(Zcl, "ERROR Shell Init %d", ret); } RegisterAppShellCommands(); Engine::Root().RunMainLoop(); @@ -194,12 +194,22 @@ void InitApp(intptr_t args) endpointId); maxCredentialsPerUser = 5; } + + uint16_t numberOfSupportedUsers = 0; + if (!DoorLockServer::Instance().GetNumberOfUserSupported(endpointId, numberOfSupportedUsers)) + { + ChipLogError(Zcl, + "Unable to get number of supported users when initializing lock endpoint, defaulting to 10 [endpointId=%d]", + endpointId); + numberOfSupportedUsers = 10; + } + chip::DeviceLayer::PlatformMgr().UnlockChipStack(); - CHIP_ERROR err = LockMgr().Init(state, maxCredentialsPerUser); + CHIP_ERROR err = LockMgr().Init(state, maxCredentialsPerUser, numberOfSupportedUsers); if (err != CHIP_NO_ERROR) { - printf("LockMgr().Init() failed\n"); + ChipLogError(Zcl, "LockMgr().Init() failed"); } LockMgr().SetCallbacks(ActionInitiated, ActionCompleted); @@ -229,11 +239,11 @@ void ActionInitiated(LockManager::Action_t aAction, int32_t aActor) // and start flashing the LEDs rapidly to indicate action initiation. if (aAction == LockManager::LOCK_ACTION) { - printf("Lock Action has been initiated\n"); + ChipLogDetail(Zcl, "Lock Action has been initiated"); } else if (aAction == LockManager::UNLOCK_ACTION) { - printf("Unlock Action has been initiated\n"); + ChipLogDetail(Zcl, "Unlock Action has been initiated"); } if (aActor == LockManager::ACTOR_BUTTON) @@ -244,12 +254,12 @@ void ActionInitiated(LockManager::Action_t aAction, int32_t aActor) // Action initiated, update the light led if (aAction == LockManager::LOCK_ACTION) { - printf("Lock Action has been initiated"); + ChipLogDetail(Zcl, "Lock Action has been initiated"); wiced_led_manager_disable_led(PLATFORM_LED_2); } else if (aAction == LockManager::UNLOCK_ACTION) { - printf("Unlock Action has been initiated"); + ChipLogDetail(Zcl, "Unlock Action has been initiated"); wiced_led_manager_enable_led(PLATFORM_LED_2); } } @@ -267,7 +277,7 @@ void UpdateClusterState(intptr_t context) if (status != EMBER_ZCL_STATUS_SUCCESS) { - printf("ERR: updating lock state %x", status); + ChipLogError(Zcl, "ERR: updating lock state %x", status); } } @@ -278,11 +288,11 @@ void ActionCompleted(LockManager::Action_t aAction) // Turn off the lock LED if in an UNLOCKED state. if (aAction == LockManager::LOCK_ACTION) { - printf("Lock Action has been completed\n"); + ChipLogDetail(Zcl, "Lock Action has been completed"); } else if (aAction == LockManager::UNLOCK_ACTION) { - printf("Unlock Action has been completed\n"); + ChipLogDetail(Zcl, "Unlock Action has been completed"); } if (syncClusterToButtonAction)