Skip to content

Commit

Permalink
[SimulatedDevice] Add a test step to wait for commissioning complete (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Feb 4, 2022
1 parent ae579cb commit 1421650
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 46 deletions.
47 changes: 24 additions & 23 deletions examples/placeholder/linux/include/MatterCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,62 @@

#pragma once

#include "Options.h"

#include <app/ConcreteAttributePath.h>
#include <app/ConcreteCommandPath.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>

#include <zap-generated/test/Commands.h>

TestCommand * gTestCommand = nullptr;

void OnPlatformEvent(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
TestCommand * GetTargetTest()
{
switch (event->Type)
const char * command = LinuxDeviceOptions::GetInstance().command;
if (command == nullptr)
{
return nullptr;
}

static auto test = GetTestCommand(command);
if (test.get() == nullptr)
{
case chip::DeviceLayer::DeviceEventType::kCommissioningComplete:
ChipLogError(Zcl, "Commissioning complete");

TestCommand * command = reinterpret_cast<TestCommand *>(arg);
if (command == nullptr)
{
ChipLogError(Zcl, "No tests.");
return;
}

gTestCommand = command;
gTestCommand->NextTest();
break;
ChipLogError(chipTool, "Specified test command does not exist: %s", command);
return nullptr;
}

return test.get();
}

void MatterPostCommandReceivedCallback(const chip::app::ConcreteCommandPath & commandPath)
{
VerifyOrReturn(gTestCommand != nullptr);
auto test = GetTargetTest();
VerifyOrReturn(test != nullptr && test->isRunning);

ChipLogError(Zcl, "Receive command: Endpoint: %u, Cluster: " ChipLogFormatMEI ", Command: " ChipLogFormatMEI,
commandPath.mEndpointId, ChipLogValueMEI(commandPath.mClusterId), ChipLogValueMEI(commandPath.mCommandId));

gTestCommand->CheckCommandPath(commandPath);
test->CheckCommandPath(commandPath);
}

void MatterPostAttributeReadCallback(const chip::app::ConcreteAttributePath & attributePath)
{
VerifyOrReturn(gTestCommand != nullptr);
auto test = GetTargetTest();
VerifyOrReturn(test != nullptr && test->isRunning);

ChipLogError(Zcl, "Receive READ attribute command: Endpoint: %u, Cluster: " ChipLogFormatMEI ", Attribute: " ChipLogFormatMEI,
attributePath.mEndpointId, ChipLogValueMEI(attributePath.mClusterId), ChipLogValueMEI(attributePath.mAttributeId));

gTestCommand->CheckAttributePath(attributePath);
test->CheckAttributePath(attributePath);
}

void MatterPostAttributeWriteCallback(const chip::app::ConcreteAttributePath & attributePath)
{
VerifyOrReturn(gTestCommand != nullptr);
auto test = GetTargetTest();
VerifyOrReturn(test != nullptr && test->isRunning);

ChipLogError(Zcl, "Receive WRITE attribute command: Endpoint: %u, Cluster: " ChipLogFormatMEI ", Attribute: " ChipLogFormatMEI,
attributePath.mEndpointId, ChipLogValueMEI(attributePath.mClusterId), ChipLogValueMEI(attributePath.mAttributeId));

gTestCommand->CheckAttributePath(attributePath);
test->CheckAttributePath(attributePath);
}
25 changes: 25 additions & 0 deletions examples/placeholder/linux/include/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#pragma once

#include <atomic>

#include <app/ConcreteAttributePath.h>
#include <app/ConcreteCommandPath.h>

Expand Down Expand Up @@ -46,6 +48,27 @@ class TestCommand
return CHIP_NO_ERROR;
}

CHIP_ERROR WaitForCommissioning()
{
isRunning = false;
return chip::DeviceLayer::PlatformMgr().AddEventHandler(OnPlatformEvent, reinterpret_cast<intptr_t>(this));
}

static void OnPlatformEvent(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
{
switch (event->Type)
{
case chip::DeviceLayer::DeviceEventType::kCommissioningComplete:
ChipLogProgress(chipTool, "Commissioning complete");
chip::DeviceLayer::PlatformMgr().RemoveEventHandler(OnPlatformEvent, arg);

TestCommand * command = reinterpret_cast<TestCommand *>(arg);
command->isRunning = true;
command->NextTest();
break;
}
}

void CheckCommandPath(const chip::app::ConcreteCommandPath & commandPath)
{
if (commandPath == mCommandPath)
Expand Down Expand Up @@ -76,6 +99,8 @@ class TestCommand
mAttributePath = chip::app::ConcreteAttributePath(0, 0, 0);
}

std::atomic_bool isRunning{ true };

protected:
chip::app::ConcreteCommandPath mCommandPath;
chip::app::ConcreteAttributePath mAttributePath;
Expand Down
27 changes: 5 additions & 22 deletions examples/placeholder/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,18 @@
*/

#include "AppMain.h"
#include "Options.h"

#include <lib/support/CodeUtils.h>

#include "MatterCallbacks.h"

std::unique_ptr<TestCommand> RunTestCommand()
int main(int argc, char * argv[])
{
const char * command = LinuxDeviceOptions::GetInstance().command;
if (command == nullptr)
{
return nullptr;
}
VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0);

auto test = GetTestCommand(command);
if (test.get() == nullptr)
auto test = GetTargetTest();
if (test != nullptr)
{
ChipLogError(chipTool, "Specified test command does not exists: %s", command);
return nullptr;
test->NextTest();
}

chip::DeviceLayer::PlatformMgr().AddEventHandler(OnPlatformEvent, reinterpret_cast<intptr_t>(test.get()));
return test;
}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0);
auto test = RunTestCommand();
ChipLinuxAppMainLoop();
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ config:
endpoint: 0

tests:
- label: "Wait for the device to be commissioned"
cluster: "DelayCommands"
command: "WaitForCommissioning"

- label: "Log OnOff Test Startup"
cluster: "LogCommands"
command: "Log"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ const WaitForMs = {
response : { arguments : [] }
};

const WaitForCommissioning = {
name : 'WaitForCommissioning',
arguments : [],
response : { arguments : [] }
};

const DelayCommands = {
name : 'DelayCommands',
commands : [ WaitForMs ],
commands : [ WaitForMs, WaitForCommissioning ],
};

//
Expand Down

0 comments on commit 1421650

Please sign in to comment.