Skip to content

Commit

Permalink
TC-SWTCH tests (project-chip#34559)
Browse files Browse the repository at this point in the history
* Add non-AS switch endpoint to all-clusters

* Add has_feature partial for decorator

* TC-SWTCH-2.5: Add

Also adds a non-AS implementation in the button simulator.

* Restyled by autopep8

* Restyled by isort

* linter

* Fix test errors

- Need tag list on descriptors for ep3 and ep4 because they're now
  sibling endpoints
- Fix desc test to expect the other endpoint

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and j-ororke committed Aug 14, 2024
1 parent e5188ac commit ade4608
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 226 deletions.
35 changes: 30 additions & 5 deletions examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,27 @@ bool HasNumericField(Json::Value & jsonValue, const std::string & field)
}

/**
* Named pipe handler for simulated long press
* Named pipe handler for simulated long press
*
* Usage example:
* echo '{"Name": "SimulateLongPress", "EndpointId": 3, "ButtonId": 1, "LongPressDelayMillis": 800,
* echo '{"Name": "SimulateLongPress", "EndpointId": 3, "ButtonId": 1, "LongPressDelayMillis": 800,
* "LongPressDurationMillis": 1000}' > /tmp/chip_all_clusters_fifo_1146610
*
* JSON Arguments:
* - "Name": Must be "SimulateLongPress"
* - "EndpointId": ID of endpoint having a switch cluster
* - "EndpointId": number of endpoint having a switch cluster
* - "ButtonId": switch position in the switch cluster for "down" button (not idle)
* - "LongPressDelayMillis": Time in milliseconds before the LongPress
* - "LongPressDurationMillis": Total duration in milliseconds from start of the press to LongRelease
* - "FeatureMap": The feature map to simulate
* - "FeatureMap": The feature map to simulate
*
* @param jsonValue - JSON payload from named pipe
*/
void HandleSimulateLongPress(Json::Value & jsonValue)
void HandleSimulateLongPress(Json::Value & jsonValue)
{
if (sButtonSimulatorInstance != nullptr)
{
Expand All @@ -86,8 +90,14 @@ void HandleSimulateLongPress(Json::Value & jsonValue)
bool hasLongPressDurationMillis = HasNumericField(jsonValue, "LongPressDurationMillis");
bool hasFeatureMap = HasNumericField(jsonValue, "FeatureMap");
if (!hasEndpointId || !hasButtonId || !hasLongPressDelayMillis || !hasLongPressDurationMillis || !hasFeatureMap)
bool hasFeatureMap = HasNumericField(jsonValue, "FeatureMap");
if (!hasEndpointId || !hasButtonId || !hasLongPressDelayMillis || !hasLongPressDurationMillis || !hasFeatureMap)
{
std::string inputJson = jsonValue.toStyledString();
ChipLogError(NotSpecified,
"Missing or invalid value for one of EndpointId, ButtonId, LongPressDelayMillis, LongPressDurationMillis or "
"FeatureMap in %s",
inputJson.c_str());
ChipLogError(NotSpecified,
"Missing or invalid value for one of EndpointId, ButtonId, LongPressDelayMillis, LongPressDurationMillis or "
"FeatureMap in %s",
Expand All @@ -100,6 +110,7 @@ void HandleSimulateLongPress(Json::Value & jsonValue)
System::Clock::Milliseconds32 longPressDelayMillis{ static_cast<unsigned>(jsonValue["LongPressDelayMillis"].asUInt()) };
System::Clock::Milliseconds32 longPressDurationMillis{ static_cast<unsigned>(jsonValue["LongPressDurationMillis"].asUInt()) };
uint32_t featureMap = static_cast<uint32_t>(jsonValue["FeatureMap"].asUInt());
uint32_t featureMap = static_cast<uint32_t>(jsonValue["FeatureMap"].asUInt());
auto buttonSimulator = std::make_unique<ButtonEventsSimulator>();

bool success = buttonSimulator->SetMode(ButtonEventsSimulator::Mode::kModeLongPress)
Expand All @@ -109,6 +120,7 @@ void HandleSimulateLongPress(Json::Value & jsonValue)
.SetPressedButtonId(buttonId)
.SetEndpointId(endpointId)
.SetFeatureMap(featureMap)
.SetFeatureMap(featureMap)
.Execute([]() { sButtonSimulatorInstance.reset(); });

if (!success)
Expand All @@ -121,11 +133,14 @@ void HandleSimulateLongPress(Json::Value & jsonValue)
}

/**
* Named pipe handler for simulated multi-press.
* Named pipe handler for simulated multi-press.
*
* Usage example:
* echo '{"Name": "SimulateMultiPress", "EndpointId": 3, "ButtonId": 1, "MultiPressPressedTimeMillis": 100,
* "MultiPressReleasedTimeMillis": 350, "MultiPressNumPresses": 2, "FeatureMap": 58}' > /tmp/chip_all_clusters_fifo_1146610
* echo '{"Name": "SimulateMultiPress", "EndpointId": 3, "ButtonId": 1, "MultiPressPressedTimeMillis": 100,
* "MultiPressReleasedTimeMillis": 350, "MultiPressNumPresses": 2, "FeatureMap": 58}' > /tmp/chip_all_clusters_fifo_1146610
*
* JSON Arguments:
* - "Name": Must be "SimulateActionSwitchMultiPress"
Expand All @@ -136,10 +151,13 @@ void HandleSimulateLongPress(Json::Value & jsonValue)
* - "MultiPressNumPresses": Number of presses to simulate
* - "FeatureMap": The feature map to simulate
* - "MultiPressMax": max number of presses (from attribute).
* - "FeatureMap": The feature map to simulate
* - "MultiPressMax": max number of presses (from attribute).
*
* @param jsonValue - JSON payload from named pipe
*/
void HandleSimulateMultiPress(Json::Value & jsonValue)
void HandleSimulateMultiPress(Json::Value & jsonValue)
{
if (sButtonSimulatorInstance != nullptr)
{
Expand All @@ -154,13 +172,17 @@ void HandleSimulateMultiPress(Json::Value & jsonValue)
bool hasMultiPressNumPresses = HasNumericField(jsonValue, "MultiPressNumPresses");
bool hasFeatureMap = HasNumericField(jsonValue, "FeatureMap");
bool hasMultiPressMax = HasNumericField(jsonValue, "MultiPressMax");
bool hasFeatureMap = HasNumericField(jsonValue, "FeatureMap");
bool hasMultiPressMax = HasNumericField(jsonValue, "MultiPressMax");
if (!hasEndpointId || !hasButtonId || !hasMultiPressPressedTimeMillis || !hasMultiPressReleasedTimeMillis ||
!hasMultiPressNumPresses || !hasFeatureMap || !hasMultiPressMax)
!hasMultiPressNumPresses || !hasFeatureMap || !hasMultiPressMax)
{
std::string inputJson = jsonValue.toStyledString();
ChipLogError(NotSpecified,
"Missing or invalid value for one of EndpointId, ButtonId, MultiPressPressedTimeMillis, "
"MultiPressReleasedTimeMillis, MultiPressNumPresses, FeatureMap or MultiPressMax in %s",
"MultiPressReleasedTimeMillis, MultiPressNumPresses, FeatureMap or MultiPressMax in %s",
inputJson.c_str());
return;
}
Expand All @@ -174,6 +196,8 @@ void HandleSimulateMultiPress(Json::Value & jsonValue)
uint8_t multiPressNumPresses = static_cast<uint8_t>(jsonValue["MultiPressNumPresses"].asUInt());
uint32_t featureMap = static_cast<uint32_t>(jsonValue["FeatureMap"].asUInt());
uint8_t multiPressMax = static_cast<uint8_t>(jsonValue["MultiPressMax"].asUInt());
uint32_t featureMap = static_cast<uint32_t>(jsonValue["FeatureMap"].asUInt());
uint8_t multiPressMax = static_cast<uint8_t>(jsonValue["MultiPressMax"].asUInt());
auto buttonSimulator = std::make_unique<ButtonEventsSimulator>();

bool success = buttonSimulator->SetMode(ButtonEventsSimulator::Mode::kModeMultiPress)
Expand All @@ -185,6 +209,8 @@ void HandleSimulateMultiPress(Json::Value & jsonValue)
.SetEndpointId(endpointId)
.SetFeatureMap(featureMap)
.SetMultiPressMax(multiPressMax)
.SetFeatureMap(featureMap)
.SetMultiPressMax(multiPressMax)
.Execute([]() { sButtonSimulatorInstance.reset(); });

if (!success)
Expand Down Expand Up @@ -396,17 +422,16 @@ void AllClustersAppCommandHandler::HandleCommand(intptr_t context)
self->OnOperationalStateChange(device, operation, self->mJsonValue["Param"]);
}
else if (name == "SimulateLongPress")
else if (name == "SimulateLongPress")
{
HandleSimulateLongPress(self->mJsonValue);
HandleSimulateLongPress(self->mJsonValue);
}
else if (name == "SimulateMultiPress")
else if (name == "SimulateMultiPress")
{
HandleSimulateMultiPress(self->mJsonValue);
}
else if (name == "SimulateLatchPosition")
{
HandleSimulateLatchPosition(self->mJsonValue);
}
else
{
ChipLogError(NotSpecified, "Unhandled command '%s': this hould never happen", name.c_str());
Expand Down
Loading

0 comments on commit ade4608

Please sign in to comment.