Skip to content

Commit

Permalink
[chip-tool] Add busyWaitMs optional argument to chip-tool to lock the…
Browse files Browse the repository at this point in the history
… main thread and the reception of messages for a given duration once a message is sent
  • Loading branch information
vivien-apple committed Dec 2, 2022
1 parent 5529d74 commit 0c834ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
8 changes: 6 additions & 2 deletions examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds) override
{
return InteractionModelCommands::SendCommand(device, endpointIds.at(0), mClusterId, mCommandId, mPayload,
mTimedInteractionTimeoutMs, mSuppressResponse, mRepeatCount, mRepeatDelayInMs);
mTimedInteractionTimeoutMs, mBusyWaitForMs, mSuppressResponse, mRepeatCount,
mRepeatDelayInMs);
}

template <class T>
CHIP_ERROR SendCommand(chip::DeviceProxy * device, chip::EndpointId endpointId, chip::ClusterId clusterId,
chip::CommandId commandId, const T & value)
{
return InteractionModelCommands::SendCommand(device, endpointId, clusterId, commandId, value, mTimedInteractionTimeoutMs,
mSuppressResponse, mRepeatCount, mRepeatDelayInMs);
mBusyWaitForMs, mSuppressResponse, mRepeatCount, mRepeatDelayInMs);
}

CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex) override
Expand Down Expand Up @@ -142,6 +143,8 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs,
"If provided, do a timed invoke with the given timed interaction timeout. See \"7.6.10. Timed Interaction\" in "
"the Matter specification.");
AddArgument("busyWaitForMs", 0, UINT16_MAX, &mBusyWaitForMs,
"If provided, block the main thread processing for the given time right after sending a command.");
AddArgument("suppressResponse", 0, 1, &mSuppressResponse);
AddArgument("repeat-count", 1, UINT16_MAX, &mRepeatCount);
AddArgument("repeat-delay-ms", 0, UINT16_MAX, &mRepeatDelayInMs);
Expand All @@ -152,6 +155,7 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
chip::ClusterId mClusterId;
chip::CommandId mCommandId;
chip::Optional<uint16_t> mTimedInteractionTimeoutMs;
chip::Optional<uint16_t> mBusyWaitForMs;
chip::Optional<bool> mSuppressResponse;
chip::Optional<uint16_t> mRepeatCount;
chip::Optional<uint16_t> mRepeatDelayInMs;
Expand Down
7 changes: 5 additions & 2 deletions examples/chip-tool/commands/clusters/WriteAttributeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi
std::vector<chip::ClusterId> clusterIds, std::vector<chip::AttributeId> attributeIds, const T & values)
{
return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, values,
mTimedInteractionTimeoutMs, mSuppressResponse, mDataVersions, mRepeatCount,
mRepeatDelayInMs);
mTimedInteractionTimeoutMs, mBusyWaitForMs, mSuppressResponse, mDataVersions,
mRepeatCount, mRepeatDelayInMs);
}

CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, std::vector<chip::ClusterId> clusterIds,
Expand Down Expand Up @@ -202,6 +202,8 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi
AddArgument("timedInteractionTimeoutMs", 0, UINT16_MAX, &mTimedInteractionTimeoutMs,
"If provided, do a timed write with the given timed interaction timeout. See \"7.6.10. Timed Interaction\" in "
"the Matter specification.");
AddArgument("busyWaitForMs", 0, UINT16_MAX, &mBusyWaitForMs,
"If provided, block the main thread processing for the given time right after sending a command.");
AddArgument("data-version", 0, UINT32_MAX, &mDataVersions,
"Comma-separated list of data versions for the clusters being written.");
AddArgument("suppressResponse", 0, 1, &mSuppressResponse);
Expand All @@ -222,6 +224,7 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi

CHIP_ERROR mError = CHIP_NO_ERROR;
chip::Optional<uint16_t> mTimedInteractionTimeoutMs;
chip::Optional<uint16_t> mBusyWaitForMs;
chip::Optional<std::vector<chip::DataVersion>> mDataVersions;
chip::Optional<bool> mSuppressResponse;
chip::Optional<uint16_t> mRepeatCount;
Expand Down
36 changes: 32 additions & 4 deletions src/app/tests/suites/commands/interaction_model/InteractionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class InteractionModelCommands
CHIP_ERROR SendCommand(chip::DeviceProxy * device, chip::EndpointId endpointId, chip::ClusterId clusterId,
chip::CommandId commandId, const T & value,
const chip::Optional<uint16_t> & timedInteractionTimeoutMs = chip::NullOptional,
const chip::Optional<uint16_t> & busyWaitForMs = chip::NullOptional,
const chip::Optional<bool> & suppressResponse = chip::NullOptional,
const chip::Optional<uint16_t> & repeatCount = chip::NullOptional,
const chip::Optional<uint16_t> & repeatDelayInMs = chip::NullOptional)
Expand All @@ -174,6 +175,17 @@ class InteractionModelCommands
ReturnErrorOnFailure(commandSender->SendCommandRequest(device->GetSecureSession().Value()));
mCommandSender.push_back(std::move(commandSender));

if (busyWaitForMs.HasValue())
{
auto & clock = chip::System::SystemClock();
auto start = clock.GetMonotonicTimestamp();
chip::System::Clock::Milliseconds32 durationInMs(busyWaitForMs.Value());
while (clock.GetMonotonicTimestamp() - start < durationInMs)
{
// nothing to do.
};
}

if (repeatDelayInMs.HasValue())
{
chip::test_utils::SleepMillis(repeatDelayInMs.Value());
Expand Down Expand Up @@ -222,6 +234,7 @@ class InteractionModelWriter
std::vector<chip::ClusterId> clusterIds, std::vector<chip::AttributeId> attributeIds,
const std::vector<T> & values,
const chip::Optional<uint16_t> & timedInteractionTimeoutMs = chip::NullOptional,
const chip::Optional<uint16_t> & busyWaitForMs = chip::NullOptional,
const chip::Optional<bool> & suppressResponse = chip::NullOptional,
const chip::Optional<std::vector<chip::DataVersion>> & dataVersions = chip::NullOptional,
const chip::Optional<uint16_t> & repeatCount = chip::NullOptional,
Expand Down Expand Up @@ -251,6 +264,17 @@ class InteractionModelWriter

ReturnErrorOnFailure(mWriteClient->SendWriteRequest(device->GetSecureSession().Value()));

if (busyWaitForMs.HasValue())
{
auto & clock = chip::System::SystemClock();
auto start = clock.GetMonotonicTimestamp();
chip::System::Clock::Milliseconds32 durationInMs(busyWaitForMs.Value());
while (clock.GetMonotonicTimestamp() - start < durationInMs)
{
// nothing to do.
};
}

if (repeatDelayInMs.HasValue())
{
chip::test_utils::SleepMillis(repeatDelayInMs.Value());
Expand All @@ -264,14 +288,15 @@ class InteractionModelWriter
CHIP_ERROR WriteAttribute(chip::DeviceProxy * device, std::vector<chip::EndpointId> endpointIds,
std::vector<chip::ClusterId> clusterIds, std::vector<chip::AttributeId> attributeIds, const T & value,
const chip::Optional<uint16_t> & timedInteractionTimeoutMs = chip::NullOptional,
const chip::Optional<uint16_t> & busyWaitForMs = chip::NullOptional,
const chip::Optional<bool> & suppressResponse = chip::NullOptional,
const chip::Optional<std::vector<chip::DataVersion>> & dataVersions = chip::NullOptional,
const chip::Optional<uint16_t> & repeatCount = chip::NullOptional,
const chip::Optional<uint16_t> & repeatDelayInMs = chip::NullOptional)
{
std::vector<T> values = { value };
return WriteAttribute(device, endpointIds, clusterIds, attributeIds, values, timedInteractionTimeoutMs, suppressResponse,
dataVersions, repeatCount, repeatDelayInMs);
return WriteAttribute(device, endpointIds, clusterIds, attributeIds, values, timedInteractionTimeoutMs, busyWaitForMs,
suppressResponse, dataVersions, repeatCount, repeatDelayInMs);
}

template <class T>
Expand Down Expand Up @@ -368,6 +393,7 @@ class InteractionModel : public InteractionModelReports,
CHIP_ERROR WriteAttribute(const char * identity, chip::EndpointId endpointId, chip::ClusterId clusterId,
chip::AttributeId attributeId, const T & value,
const chip::Optional<uint16_t> & timedInteractionTimeoutMs = chip::NullOptional,
const chip::Optional<uint16_t> & busyWaitForMs = chip::NullOptional,
const chip::Optional<bool> & suppressResponse = chip::NullOptional,
const chip::Optional<chip::DataVersion> & dataVersion = chip::NullOptional)
{
Expand All @@ -386,7 +412,8 @@ class InteractionModel : public InteractionModelReports,
}

return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, value,
timedInteractionTimeoutMs, suppressResponse, optionalDataVersions);
timedInteractionTimeoutMs, busyWaitForMs, suppressResponse,
optionalDataVersions);
}

template <class T>
Expand All @@ -404,13 +431,14 @@ class InteractionModel : public InteractionModelReports,
template <class T>
CHIP_ERROR SendCommand(const char * identity, chip::EndpointId endpointId, chip::ClusterId clusterId, chip::CommandId commandId,
const T & value, chip::Optional<uint16_t> timedInteractionTimeoutMs = chip::NullOptional,
chip::Optional<uint16_t> busyWaitMs = chip::NullOptional,
const chip::Optional<bool> & suppressResponse = chip::NullOptional)
{
chip::DeviceProxy * device = GetDevice(identity);
VerifyOrReturnError(device != nullptr, CHIP_ERROR_INCORRECT_STATE);

return InteractionModelCommands::SendCommand(device, endpointId, clusterId, commandId, value, timedInteractionTimeoutMs,
suppressResponse);
busyWaitMs, suppressResponse);
}

template <class T>
Expand Down

0 comments on commit 0c834ae

Please sign in to comment.