Skip to content

Commit

Permalink
[chip-tool] Add repeat-count and repeat-duration-ms optional paramete…
Browse files Browse the repository at this point in the history
…rs for cluster commands (#16713)
  • Loading branch information
vivien-apple authored and pull[bot] committed Sep 1, 2023
1 parent d1f758e commit 1051348
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include <app/CommandSender.h>
#include <lib/support/UnitTestUtils.h>

#include "DataModelLogger.h"
#include "ModelCommand.h"
Expand All @@ -33,6 +34,8 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
AddArgument("payload", &mPayload);
AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs);
AddArgument("suppressResponse", 0, 1, &mSuppressResponse);
AddArgument("repeat-count", 1, UINT16_MAX, &mRepeatCount);
AddArgument("repeat-delay-ms", 0, UINT16_MAX, &mRepeatDelayInMs);
ModelCommand::AddArguments();
}

Expand All @@ -43,6 +46,8 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
AddArgument("payload", &mPayload);
AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs);
AddArgument("suppressResponse", 0, 1, &mSuppressResponse);
AddArgument("repeat-count", 1, UINT16_MAX, &mRepeatCount);
AddArgument("repeat-delay-ms", 0, UINT16_MAX, &mRepeatDelayInMs);
ModelCommand::AddArguments();
}

Expand All @@ -51,6 +56,8 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
{
AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs);
AddArgument("suppressResponse", 0, 1, &mSuppressResponse);
AddArgument("repeat-count", 1, UINT16_MAX, &mRepeatCount);
AddArgument("repeat-delay-ms", 0, UINT16_MAX, &mRepeatDelayInMs);
}

~ClusterCommand() {}
Expand Down Expand Up @@ -97,22 +104,48 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal

virtual void OnDone(chip::app::CommandSender * client) override
{
mCommandSender.reset();
SetCommandExitStatus(mError);
mCommandSender.front().reset();
mCommandSender.erase(mCommandSender.begin());

// If the command is repeated N times, wait for all the responses to comes in
// before exiting.
bool shouldStop = true;
if (mRepeatCount.HasValue())
{
mRepeatCount.SetValue(static_cast<uint16_t>(mRepeatCount.Value() - 1));
shouldStop = mRepeatCount.Value() == 0;
}

if (shouldStop)
{
SetCommandExitStatus(mError);
}
}

template <class T>
CHIP_ERROR SendCommand(ChipDevice * device, chip::EndpointId endpointId, chip::ClusterId clusterId, chip::CommandId commandId,
const T & value)
{
chip::app::CommandPathParams commandPath = { endpointId, 0 /* groupId */, clusterId, commandId,
(chip::app::CommandPathFlags::kEndpointIdValid) };
mCommandSender =
std::make_unique<chip::app::CommandSender>(this, device->GetExchangeManager(), mTimedInteractionTimeoutMs.HasValue());
VerifyOrReturnError(mCommandSender != nullptr, CHIP_ERROR_NO_MEMORY);
ReturnErrorOnFailure(mCommandSender->AddRequestDataNoTimedCheck(commandPath, value, mTimedInteractionTimeoutMs,
mSuppressResponse.ValueOr(false)));
ReturnErrorOnFailure(mCommandSender->SendCommandRequest(device->GetSecureSession().Value()));
uint16_t repeatCount = mRepeatCount.ValueOr(1);
while (repeatCount--)
{
chip::app::CommandPathParams commandPath = { endpointId, 0 /* groupId */, clusterId, commandId,
(chip::app::CommandPathFlags::kEndpointIdValid) };

auto commandSender = std::make_unique<chip::app::CommandSender>(this, device->GetExchangeManager(),
mTimedInteractionTimeoutMs.HasValue());
VerifyOrReturnError(commandSender != nullptr, CHIP_ERROR_NO_MEMORY);
ReturnErrorOnFailure(commandSender->AddRequestDataNoTimedCheck(commandPath, value, mTimedInteractionTimeoutMs,
mSuppressResponse.ValueOr(false)));

ReturnErrorOnFailure(commandSender->SendCommandRequest(device->GetSecureSession().Value()));
mCommandSender.push_back(std::move(commandSender));

if (mRepeatDelayInMs.HasValue())
{
chip::test_utils::SleepMillis(mRepeatDelayInMs.Value());
}
}
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -142,8 +175,10 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
chip::CommandId mCommandId;
chip::Optional<uint16_t> mTimedInteractionTimeoutMs;
chip::Optional<bool> mSuppressResponse;
chip::Optional<uint16_t> mRepeatCount;
chip::Optional<uint16_t> mRepeatDelayInMs;

CHIP_ERROR mError = CHIP_NO_ERROR;
CustomArgument mPayload;
std::unique_ptr<chip::app::CommandSender> mCommandSender;
std::vector<std::unique_ptr<chip::app::CommandSender>> mCommandSender;
};

0 comments on commit 1051348

Please sign in to comment.