Skip to content

Commit

Permalink
Fix EFR32 door lock example
Browse files Browse the repository at this point in the history
  • Loading branch information
Morozov-5F committed May 11, 2022
1 parent 6190e61 commit ef9f27f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/lock-app/efr32/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 4 additions & 2 deletions examples/lock-app/efr32/include/LockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class LockManager
kState_UnlockCompleted,
} State;

CHIP_ERROR Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state);
CHIP_ERROR Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state,
uint8_t maxNumberOfCredentialsPerUser);
bool NextState();
bool IsActionInProgress();
bool InitiateAction(int32_t aActor, Action_t aAction);
Expand Down Expand Up @@ -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<DlCredential> mCredentials;
uint8_t mMaxCredentialsPerUser;

static LockManager sLock;
};
Expand Down
12 changes: 11 additions & 1 deletion examples/lock-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
19 changes: 14 additions & 5 deletions examples/lock-app/efr32/src/LockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ TimerHandle_t sLockTimer;

using namespace ::chip::DeviceLayer::Internal;

CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> state)
CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> 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)
Expand Down Expand Up @@ -67,7 +76,7 @@ bool LockManager::ReadConfigValues()

EFR32Config::ReadConfigValueBin(EFR32Config::kConfigKey_CredentialData, mCredentialData, sizeof(mCredentialData), outLen);

EFR32Config::ReadConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast<uint8_t *>(&mCredentials),
EFR32Config::ReadConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast<uint8_t *>(mCredentials.Get()),
sizeof(DlCredential), outLen);

return true;
Expand Down Expand Up @@ -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);
Expand All @@ -278,13 +287,13 @@ bool LockManager::SetUser(uint16_t userIndex, chip::FabricIndex creator, chip::F
mCredentials[i].CredentialIndex = i + 1;
}

userInStorage.credentials = chip::Span<const DlCredential>(mCredentials, totalCredentials);
userInStorage.credentials = chip::Span<const DlCredential>(mCredentials.Get(), totalCredentials);

// Save user information in NVM flash
EFR32Config::WriteConfigValueBin(EFR32Config::kConfigKey_LockUser, reinterpret_cast<const uint8_t *>(&userInStorage),
sizeof(EmberAfPluginDoorLockUserInfo));

EFR32Config::WriteConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast<const uint8_t *>(&mCredentials),
EFR32Config::WriteConfigValueBin(EFR32Config::kConfigKey_UserCredentials, reinterpret_cast<const uint8_t *>(mCredentials.Get()),
sizeof(DlCredential));

EFR32Config::WriteConfigValueStr(EFR32Config::kConfigKey_LockUserName, mUserName, sizeof(userName.size()));
Expand Down
4 changes: 2 additions & 2 deletions examples/lock-app/linux/src/LockEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int>(totalCredentials),
userInStorage.credentials.capacity());
static_cast<unsigned int>(userInStorage.credentials.capacity()));
return false;
}

Expand Down

0 comments on commit ef9f27f

Please sign in to comment.