From d59dbb42db4de90c7ae7235a4c06a64c5c23c394 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 4 Jan 2023 11:25:53 +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 | 2 + .../commands/clusters/WriteAttributeCommand.h | 2 + .../interaction_model/InteractionModel.cpp | 15 +++++++ .../interaction_model/InteractionModel.h | 44 +++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/examples/chip-tool/commands/clusters/ClusterCommand.h b/examples/chip-tool/commands/clusters/ClusterCommand.h index f6cb307021ac5d..c2562843e024a8 100644 --- a/examples/chip-tool/commands/clusters/ClusterCommand.h +++ b/examples/chip-tool/commands/clusters/ClusterCommand.h @@ -173,6 +173,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); diff --git a/examples/chip-tool/commands/clusters/WriteAttributeCommand.h b/examples/chip-tool/commands/clusters/WriteAttributeCommand.h index 56363cb481721d..9b5e8f3ef70728 100644 --- a/examples/chip-tool/commands/clusters/WriteAttributeCommand.h +++ b/examples/chip-tool/commands/clusters/WriteAttributeCommand.h @@ -228,6 +228,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); diff --git a/src/app/tests/suites/commands/interaction_model/InteractionModel.cpp b/src/app/tests/suites/commands/interaction_model/InteractionModel.cpp index ad4bf1603a258d..542f4ffd29368f 100644 --- a/src/app/tests/suites/commands/interaction_model/InteractionModel.cpp +++ b/src/app/tests/suites/commands/interaction_model/InteractionModel.cpp @@ -21,6 +21,21 @@ using namespace chip; using namespace chip::app; +namespace chip { +namespace test_utils { +void BusyWaitMillis(uint16_t busyWaitForMs) +{ + auto & clock = chip::System::SystemClock(); + auto start = clock.GetMonotonicTimestamp(); + chip::System::Clock::Milliseconds32 durationInMs(busyWaitForMs); + while (clock.GetMonotonicTimestamp() - start < durationInMs) + { + // nothing to do. + }; +} +} // namespace test_utils +} // namespace chip + CHIP_ERROR InteractionModel::ReadAttribute(const char * identity, EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, bool fabricFiltered, const Optional & dataVersion) { diff --git a/src/app/tests/suites/commands/interaction_model/InteractionModel.h b/src/app/tests/suites/commands/interaction_model/InteractionModel.h index 83a0d6fff769b6..1d3e2eb735e116 100644 --- a/src/app/tests/suites/commands/interaction_model/InteractionModel.h +++ b/src/app/tests/suites/commands/interaction_model/InteractionModel.h @@ -30,6 +30,12 @@ constexpr uint8_t kMaxAllowedPaths = 10; +namespace chip { +namespace test_utils { +void BusyWaitMillis(uint16_t busyWaitForMs); +} // namespace test_utils +} // namespace chip + class InteractionModelConfig { public: @@ -237,6 +243,11 @@ class InteractionModelCommands ReturnErrorOnFailure(commandSender->SendCommandRequest(device->GetSecureSession().Value())); mCommandSender.push_back(std::move(commandSender)); + if (mBusyWaitForMs.HasValue()) + { + chip::test_utils::BusyWaitMillis(mBusyWaitForMs.Value()); + } + if (mRepeatDelayInMs.HasValue()) { chip::test_utils::SleepMillis(mRepeatDelayInMs.Value()); @@ -321,18 +332,32 @@ class InteractionModelCommands return *this; } + InteractionModelCommands & SetBusyWaitForMs(uint16_t busyWaitForMs) + { + mBusyWaitForMs.SetValue(busyWaitForMs); + return *this; + } + + InteractionModelCommands & SetBusyWaitForMs(const chip::Optional & busyWaitForMs) + { + mBusyWaitForMs = busyWaitForMs; + return *this; + } + void ResetOptions() { mTimedInteractionTimeoutMs = chip::NullOptional; mSuppressResponse = chip::NullOptional; mRepeatCount = chip::NullOptional; mRepeatDelayInMs = chip::NullOptional; + mBusyWaitForMs = chip::NullOptional; } chip::Optional mTimedInteractionTimeoutMs; chip::Optional mSuppressResponse; chip::Optional mRepeatCount; chip::Optional mRepeatDelayInMs; + chip::Optional mBusyWaitForMs; }; class InteractionModelWriter @@ -370,6 +395,11 @@ class InteractionModelWriter ReturnErrorOnFailure(mWriteClient->SendWriteRequest(device->GetSecureSession().Value())); + if (mBusyWaitForMs.HasValue()) + { + chip::test_utils::BusyWaitMillis(mBusyWaitForMs.Value()); + } + if (mRepeatDelayInMs.HasValue()) { chip::test_utils::SleepMillis(mRepeatDelayInMs.Value()); @@ -487,6 +517,18 @@ class InteractionModelWriter return *this; } + InteractionModelWriter & SetBusyWaitForMs(uint16_t busyWaitForMs) + { + mBusyWaitForMs.SetValue(busyWaitForMs); + return *this; + } + + InteractionModelWriter & SetBusyWaitForMs(const chip::Optional & busyWaitForMs) + { + mBusyWaitForMs = busyWaitForMs; + return *this; + } + void ResetOptions() { mTimedInteractionTimeoutMs = chip::NullOptional; @@ -494,6 +536,7 @@ class InteractionModelWriter mDataVersions = chip::NullOptional; mRepeatCount = chip::NullOptional; mRepeatDelayInMs = chip::NullOptional; + mBusyWaitForMs = chip::NullOptional; } chip::Optional mTimedInteractionTimeoutMs; @@ -501,6 +544,7 @@ class InteractionModelWriter chip::Optional mSuppressResponse; chip::Optional mRepeatCount; chip::Optional mRepeatDelayInMs; + chip::Optional mBusyWaitForMs; private: template