From ef9f27f49bfccc8deae541cf32c0960945537401 Mon Sep 17 00:00:00 2001 From: Morozov-5F Date: Wed, 11 May 2022 20:58:21 +0300 Subject: [PATCH] Fix EFR32 door lock example --- examples/lock-app/efr32/include/AppTask.h | 1 + examples/lock-app/efr32/include/LockManager.h | 6 ++++-- examples/lock-app/efr32/src/AppTask.cpp | 12 +++++++++++- examples/lock-app/efr32/src/LockManager.cpp | 19 ++++++++++++++----- examples/lock-app/linux/src/LockEndpoint.cpp | 4 ++-- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/examples/lock-app/efr32/include/AppTask.h b/examples/lock-app/efr32/include/AppTask.h index 67803623f2fbc8..7dab19d18c0728 100644 --- a/examples/lock-app/efr32/include/AppTask.h +++ b/examples/lock-app/efr32/include/AppTask.h @@ -38,6 +38,7 @@ #define APP_ERROR_CREATE_TIMER_FAILED CHIP_APPLICATION_ERROR(0x04) #define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05) #define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06) +#define APP_ERROR_ALLOCATION_FAILED CHIP_APPLICATION_ERROR(0x07) class AppTask { diff --git a/examples/lock-app/efr32/include/LockManager.h b/examples/lock-app/efr32/include/LockManager.h index dad3c119884b18..ba2deb7007ce04 100644 --- a/examples/lock-app/efr32/include/LockManager.h +++ b/examples/lock-app/efr32/include/LockManager.h @@ -54,7 +54,8 @@ class LockManager kState_UnlockCompleted, } State; - CHIP_ERROR Init(chip::app::DataModel::Nullable state); + CHIP_ERROR Init(chip::app::DataModel::Nullable state, + uint8_t maxNumberOfCredentialsPerUser); bool NextState(); bool IsActionInProgress(); bool InitiateAction(int32_t aActor, Action_t aAction); @@ -103,7 +104,8 @@ class LockManager char mUserName[DOOR_LOCK_MAX_USER_NAME_SIZE]; uint8_t mCredentialData[DOOR_LOCK_MAX_CREDENTIAL_SIZE]; - DlCredential mCredentials[DOOR_LOCK_MAX_CREDENTIALS_PER_USER]; + chip::Platform::ScopedMemoryBuffer mCredentials; + uint8_t mMaxCredentialsPerUser; static LockManager sLock; }; diff --git a/examples/lock-app/efr32/src/AppTask.cpp b/examples/lock-app/efr32/src/AppTask.cpp index 1f593f41322ab2..066d5704111bb1 100644 --- a/examples/lock-app/efr32/src/AppTask.cpp +++ b/examples/lock-app/efr32/src/AppTask.cpp @@ -235,7 +235,17 @@ CHIP_ERROR AppTask::Init() chip::app::Clusters::DoorLock::Attributes::LockState::Get(endpointId, state); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); - err = LockMgr().Init(state); + uint8_t maxCredentialsPerUser = 0; + if (!DoorLockServer::Instance().GetNumberOfCredentialsSupportedPerUser(endpointId, maxCredentialsPerUser)) + { + ChipLogError(Zcl, + "Unable to get number of credentials supported per user when initializing lock endpoint, defaulting to 5 " + "[endpointId=%d]", + endpointId); + maxCredentialsPerUser = 5; + } + + err = LockMgr().Init(state, maxCredentialsPerUser); if (err != CHIP_NO_ERROR) { EFR32_LOG("LockMgr().Init() failed"); diff --git a/examples/lock-app/efr32/src/LockManager.cpp b/examples/lock-app/efr32/src/LockManager.cpp index 39b507daa4e6d4..1d2d30559a24d6 100644 --- a/examples/lock-app/efr32/src/LockManager.cpp +++ b/examples/lock-app/efr32/src/LockManager.cpp @@ -31,8 +31,17 @@ TimerHandle_t sLockTimer; using namespace ::chip::DeviceLayer::Internal; -CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable state) +CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable state, + uint8_t maxNumberOfCredentialsPerUser) { + // Allocate buffer for credentials + if (!mCredentials.Alloc(maxNumberOfCredentialsPerUser)) + { + EFR32_LOG("Failed to allocate array for lock credentials"); + return APP_ERROR_ALLOCATION_FAILED; + } + mMaxCredentialsPerUser = maxNumberOfCredentialsPerUser; + // Create FreeRTOS sw timer for lock timer. sLockTimer = xTimerCreate("lockTmr", // Just a text name, not used by the RTOS kernel 1, // == default timer period (mS) @@ -67,7 +76,7 @@ bool LockManager::ReadConfigValues() EFR32Config::ReadConfigValueBin(EFR32Config::kConfigKey_CredentialData, mCredentialData, sizeof(mCredentialData), outLen); - EFR32Config::ReadConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast(&mCredentials), + EFR32Config::ReadConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast(mCredentials.Get()), sizeof(DlCredential), outLen); return true; @@ -254,7 +263,7 @@ bool LockManager::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::F return false; } - if (totalCredentials > sizeof(DOOR_LOCK_MAX_CREDENTIALS_PER_USER)) + if (totalCredentials > mMaxCredentialsPerUser) { ChipLogError(Zcl, "Cannot set user - total number of credentials is too big [endpoint=%d,index=%d,totalCredentials=%u]", mEndpointId, userIndex, totalCredentials); @@ -278,13 +287,13 @@ bool LockManager::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::F mCredentials[i].CredentialIndex = i + 1; } - userInStorage.credentials = chip::Span(mCredentials, totalCredentials); + userInStorage.credentials = chip::Span(mCredentials.Get(), totalCredentials); // Save user information in NVM flash EFR32Config::WriteConfigValueBin(EFR32Config::kConfigKey_LockUser, reinterpret_cast(&userInStorage), sizeof(EmberAfPluginDoorLockUserInfo)); - EFR32Config::WriteConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast(&mCredentials), + EFR32Config::WriteConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast(mCredentials.Get()), sizeof(DlCredential)); EFR32Config::WriteConfigValueStr(EFR32Config::kConfigKey_LockUserName, mUserName, sizeof(userName.size())); diff --git a/examples/lock-app/linux/src/LockEndpoint.cpp b/examples/lock-app/linux/src/LockEndpoint.cpp index c684db530ccb68..404fecebff0913 100644 --- a/examples/lock-app/linux/src/LockEndpoint.cpp +++ b/examples/lock-app/linux/src/LockEndpoint.cpp @@ -102,9 +102,9 @@ bool LockEndpoint::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip:: { ChipLogError(Zcl, "Cannot set user - total number of credentials is too big [endpoint=%d,index=%d,adjustedUserIndex=%u" - ",totalCredentials=%u,maxNumberOfCredentials=%zu]", + ",totalCredentials=%u,maxNumberOfCredentials=%u]", mEndpointId, userIndex, adjustedUserIndex, static_cast(totalCredentials), - userInStorage.credentials.capacity()); + static_cast(userInStorage.credentials.capacity())); return false; }