Skip to content

Commit

Permalink
#26925 async execution of lock actions in example app
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarc committed Jul 20, 2023
1 parent a1850d5 commit 53e551b
Show file tree
Hide file tree
Showing 10 changed files with 1,390 additions and 689 deletions.
2 changes: 2 additions & 0 deletions examples/lock-app/lock-common/include/LockEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class LockEndpoint
bool weekDayScheduleInAction(uint16_t userIndex) const;
bool yearDayScheduleInAction(uint16_t userIndex) const;

static void OnLockActionCompleteCallback(chip::System::Layer *, void * callbackContext);

chip::EndpointId mEndpointId;
DlLockState mLockState;
DoorStateEnum mDoorState;
Expand Down
87 changes: 80 additions & 7 deletions examples/lock-app/lock-common/src/LockEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@
#include "LockEndpoint.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <cstring>
#include <platform/CHIPDeviceLayer.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>

using chip::to_underlying;
using chip::app::DataModel::MakeNullable;

struct LockActionData
{
chip::EndpointId endpointId;
DlLockState lockState;
OperationSourceEnum opSource;
Nullable<uint16_t> userIndex;
uint16_t credentialIndex;
Nullable<List<const LockOpCredentials>> credentials;
Nullable<chip::FabricIndex> fabricIdx;
Nullable<chip::NodeId> nodeId;
bool moving = false;
};

LockActionData currentAction;

bool LockEndpoint::Lock(const Nullable<chip::FabricIndex> & fabricIdx, const Nullable<chip::NodeId> & nodeId,
const Optional<chip::ByteSpan> & pin, OperationErrorEnum & err, OperationSourceEnum opSource)
{
Expand All @@ -34,7 +51,7 @@ bool LockEndpoint::Unlock(const Nullable<chip::FabricIndex> & fabricIdx, const N
if (DoorLockServer::Instance().SupportsUnbolt(mEndpointId))
{
// If Unbolt is supported Unlock is supposed to pull the latch
setLockState(fabricIdx, nodeId, DlLockState::kUnlatched, pin, err, opSource);
return setLockState(fabricIdx, nodeId, DlLockState::kUnlatched, pin, err, opSource);
}

return setLockState(fabricIdx, nodeId, DlLockState::kUnlocked, pin, err, opSource);
Expand Down Expand Up @@ -410,7 +427,21 @@ bool LockEndpoint::setLockState(const Nullable<chip::FabricIndex> & fabricIdx, c
ChipLogProgress(Zcl, "Door Lock App: setting door lock state to \"%s\" [endpointId=%d]", lockStateToString(lockState),
mEndpointId);

DoorLockServer::Instance().SetLockState(mEndpointId, lockState, opSource);
if (currentAction.moving == true)
{
return false;
}

currentAction.moving = true;
currentAction.endpointId = mEndpointId;
currentAction.lockState = lockState;
currentAction.opSource = opSource;
currentAction.userIndex = NullNullable;
currentAction.fabricIdx = fabricIdx;
currentAction.nodeId = nodeId;

// simulate 3s lock movement duration
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(3), OnLockActionCompleteCallback, nullptr);

return true;
}
Expand Down Expand Up @@ -475,15 +506,57 @@ bool LockEndpoint::setLockState(const Nullable<chip::FabricIndex> & fabricIdx, c
"Lock App: specified PIN code was found in the database, setting door lock state to \"%s\" [endpointId=%d,userIndex=%u]",
lockStateToString(lockState), mEndpointId, userIndex);

mLockState = lockState;
LockOpCredentials userCredential[] = { { CredentialTypeEnum::kPin, uint16_t(credentialIndex) } };
auto userCredentials = MakeNullable<List<const LockOpCredentials>>(userCredential);
DoorLockServer::Instance().SetLockState(mEndpointId, mLockState, opSource, MakeNullable(static_cast<uint16_t>(userIndex + 1)),
userCredentials, fabricIdx, nodeId);
if (currentAction.moving == true)
{
return false;
}

currentAction.moving = true;
currentAction.endpointId = mEndpointId;
currentAction.lockState = lockState;
currentAction.opSource = opSource;
currentAction.userIndex = MakeNullable(static_cast<uint16_t>(userIndex + 1));
currentAction.credentialIndex = static_cast<uint16_t>(credentialIndex);
currentAction.fabricIdx = fabricIdx;
currentAction.nodeId = nodeId;

// simulate 3s lock movement duration
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(3), OnLockActionCompleteCallback, nullptr);

return true;
}

void LockEndpoint::OnLockActionCompleteCallback(chip::System::Layer *, void * callbackContext)
{
if (currentAction.userIndex == NullNullable)
{
DoorLockServer::Instance().SetLockState(currentAction.endpointId, currentAction.lockState, currentAction.opSource,
NullNullable, NullNullable, currentAction.fabricIdx, currentAction.nodeId);
}
else
{
LockOpCredentials userCredential[] = { { CredentialTypeEnum::kPin, currentAction.credentialIndex } };
auto userCredentials = MakeNullable<List<const LockOpCredentials>>(userCredential);

DoorLockServer::Instance().SetLockState(currentAction.endpointId, currentAction.lockState, currentAction.opSource,
currentAction.userIndex, userCredentials, currentAction.fabricIdx,
currentAction.nodeId);
}

// move back to Unlocked after Unlatch
if (currentAction.lockState == DlLockState::kUnlatched)
{
currentAction.lockState = DlLockState::kUnlocked;

// simulate 1s lock movement duration
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(1), OnLockActionCompleteCallback, nullptr);
}
else
{
currentAction.moving = false;
}
}

bool LockEndpoint::weekDayScheduleInAction(uint16_t userIndex) const
{
const auto & user = mLockUsers[userIndex];
Expand Down
114 changes: 113 additions & 1 deletion src/app/tests/suites/DL_LockUnlock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ config:
nodeId: 0x12344321
cluster: "Door Lock"
endpoint: 1
timeout: 25
timeout: 100

tests:
- label: "Wait for the commissioned device to be retrieved"
Expand All @@ -33,6 +33,14 @@ tests:
command: "UnlockDoor"
timedInteractionTimeoutMs: 10000

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Unlocked"
command: "readAttribute"
attribute: "LockState"
Expand All @@ -43,6 +51,14 @@ tests:
command: "LockDoor"
timedInteractionTimeoutMs: 10000

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Locked"
command: "readAttribute"
attribute: "LockState"
Expand All @@ -53,6 +69,14 @@ tests:
command: "UnboltDoor"
timedInteractionTimeoutMs: 10000

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Unlocked"
command: "readAttribute"
attribute: "LockState"
Expand All @@ -63,6 +87,14 @@ tests:
command: "LockDoor"
timedInteractionTimeoutMs: 10000

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Locked"
command: "readAttribute"
attribute: "LockState"
Expand Down Expand Up @@ -149,6 +181,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Read the LockOperation event list"
command: "readEvent"
event: "LockOperation"
Expand Down Expand Up @@ -267,6 +307,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Locked"
command: "readAttribute"
attribute: "LockState"
Expand Down Expand Up @@ -331,6 +379,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Unlocked"
command: "readAttribute"
attribute: "LockState"
Expand All @@ -345,6 +401,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Locked"
command: "readAttribute"
attribute: "LockState"
Expand Down Expand Up @@ -379,6 +443,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Unlocked"
command: "readAttribute"
attribute: "LockState"
Expand All @@ -393,6 +465,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Locked"
command: "readAttribute"
attribute: "LockState"
Expand Down Expand Up @@ -473,6 +553,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Unlocked"
command: "readAttribute"
attribute: "LockState"
Expand All @@ -487,6 +575,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Create a disabled user and credential"
command: "SetCredential"
timedInteractionTimeoutMs: 10000
Expand Down Expand Up @@ -537,6 +633,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Unlocked"
command: "readAttribute"
attribute: "LockState"
Expand Down Expand Up @@ -567,6 +671,14 @@ tests:
- name: "PINCode"
value: "123456"

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "Verify that lock state attribute value is set to Locked"
command: "readAttribute"
attribute: "LockState"
Expand Down
16 changes: 16 additions & 0 deletions src/app/tests/suites/certification/Test_TC_DRLK_2_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ tests:
command: "LockDoor"
timedInteractionTimeoutMs: 1000

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "TH reads LockState attribute from DUT"
PICS: DRLK.S.A0000
command: "readAttribute"
Expand All @@ -75,6 +83,14 @@ tests:
command: "UnlockDoor"
timedInteractionTimeoutMs: 1000

- label: "Wait for lock action to complete"
cluster: "DelayCommands"
command: "WaitForMs"
arguments:
values:
- name: "ms"
value: 5000

- label: "TH reads LockState attribute from DUT"
PICS: DRLK.S.A0000
command: "readAttribute"
Expand Down
Loading

0 comments on commit 53e551b

Please sign in to comment.