From 0c834aed3f9d0f62ddf8914ff0a02e0ac14f56b0 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 25 Nov 2022 13:35:09 +0100 Subject: [PATCH] [chip-tool] Add busyWaitMs optional argument to chip-tool to lock the main thread and the reception of messages for a given duration once a message is sent --- .../commands/clusters/ClusterCommand.h | 8 +++-- .../commands/clusters/WriteAttributeCommand.h | 7 ++-- .../interaction_model/InteractionModel.h | 36 ++++++++++++++++--- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/examples/chip-tool/commands/clusters/ClusterCommand.h b/examples/chip-tool/commands/clusters/ClusterCommand.h index 4b8325074a8716..97e1b9c5644ae4 100644 --- a/examples/chip-tool/commands/clusters/ClusterCommand.h +++ b/examples/chip-tool/commands/clusters/ClusterCommand.h @@ -48,7 +48,8 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds) override { return InteractionModelCommands::SendCommand(device, endpointIds.at(0), mClusterId, mCommandId, mPayload, - mTimedInteractionTimeoutMs, mSuppressResponse, mRepeatCount, mRepeatDelayInMs); + mTimedInteractionTimeoutMs, mBusyWaitForMs, mSuppressResponse, mRepeatCount, + mRepeatDelayInMs); } template @@ -56,7 +57,7 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub 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 @@ -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); @@ -152,6 +155,7 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub chip::ClusterId mClusterId; chip::CommandId mCommandId; chip::Optional mTimedInteractionTimeoutMs; + chip::Optional mBusyWaitForMs; chip::Optional mSuppressResponse; chip::Optional mRepeatCount; chip::Optional mRepeatDelayInMs; diff --git a/examples/chip-tool/commands/clusters/WriteAttributeCommand.h b/examples/chip-tool/commands/clusters/WriteAttributeCommand.h index ac9f6233a65ff4..c9bbc59c3505e9 100644 --- a/examples/chip-tool/commands/clusters/WriteAttributeCommand.h +++ b/examples/chip-tool/commands/clusters/WriteAttributeCommand.h @@ -130,8 +130,8 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi std::vector clusterIds, std::vector 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 clusterIds, @@ -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); @@ -222,6 +224,7 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi CHIP_ERROR mError = CHIP_NO_ERROR; chip::Optional mTimedInteractionTimeoutMs; + chip::Optional mBusyWaitForMs; chip::Optional> mDataVersions; chip::Optional mSuppressResponse; chip::Optional mRepeatCount; diff --git a/src/app/tests/suites/commands/interaction_model/InteractionModel.h b/src/app/tests/suites/commands/interaction_model/InteractionModel.h index c16318070a68ae..e8d96c672fbf6a 100644 --- a/src/app/tests/suites/commands/interaction_model/InteractionModel.h +++ b/src/app/tests/suites/commands/interaction_model/InteractionModel.h @@ -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 & timedInteractionTimeoutMs = chip::NullOptional, + const chip::Optional & busyWaitForMs = chip::NullOptional, const chip::Optional & suppressResponse = chip::NullOptional, const chip::Optional & repeatCount = chip::NullOptional, const chip::Optional & repeatDelayInMs = chip::NullOptional) @@ -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()); @@ -222,6 +234,7 @@ class InteractionModelWriter std::vector clusterIds, std::vector attributeIds, const std::vector & values, const chip::Optional & timedInteractionTimeoutMs = chip::NullOptional, + const chip::Optional & busyWaitForMs = chip::NullOptional, const chip::Optional & suppressResponse = chip::NullOptional, const chip::Optional> & dataVersions = chip::NullOptional, const chip::Optional & repeatCount = chip::NullOptional, @@ -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()); @@ -264,14 +288,15 @@ class InteractionModelWriter CHIP_ERROR WriteAttribute(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, const T & value, const chip::Optional & timedInteractionTimeoutMs = chip::NullOptional, + const chip::Optional & busyWaitForMs = chip::NullOptional, const chip::Optional & suppressResponse = chip::NullOptional, const chip::Optional> & dataVersions = chip::NullOptional, const chip::Optional & repeatCount = chip::NullOptional, const chip::Optional & repeatDelayInMs = chip::NullOptional) { std::vector 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 @@ -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 & timedInteractionTimeoutMs = chip::NullOptional, + const chip::Optional & busyWaitForMs = chip::NullOptional, const chip::Optional & suppressResponse = chip::NullOptional, const chip::Optional & dataVersion = chip::NullOptional) { @@ -386,7 +412,8 @@ class InteractionModel : public InteractionModelReports, } return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, value, - timedInteractionTimeoutMs, suppressResponse, optionalDataVersions); + timedInteractionTimeoutMs, busyWaitForMs, suppressResponse, + optionalDataVersions); } template @@ -404,13 +431,14 @@ class InteractionModel : public InteractionModelReports, template CHIP_ERROR SendCommand(const char * identity, chip::EndpointId endpointId, chip::ClusterId clusterId, chip::CommandId commandId, const T & value, chip::Optional timedInteractionTimeoutMs = chip::NullOptional, + chip::Optional busyWaitMs = chip::NullOptional, const chip::Optional & 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