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 Jan 4, 2023
1 parent 28cb6cc commit d59dbb4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/commands/clusters/WriteAttributeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> & dataVersion)
{
Expand Down
44 changes: 44 additions & 0 deletions src/app/tests/suites/commands/interaction_model/InteractionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -321,18 +332,32 @@ class InteractionModelCommands
return *this;
}

InteractionModelCommands & SetBusyWaitForMs(uint16_t busyWaitForMs)
{
mBusyWaitForMs.SetValue(busyWaitForMs);
return *this;
}

InteractionModelCommands & SetBusyWaitForMs(const chip::Optional<uint16_t> & busyWaitForMs)
{
mBusyWaitForMs = busyWaitForMs;
return *this;
}

void ResetOptions()
{
mTimedInteractionTimeoutMs = chip::NullOptional;
mSuppressResponse = chip::NullOptional;
mRepeatCount = chip::NullOptional;
mRepeatDelayInMs = chip::NullOptional;
mBusyWaitForMs = chip::NullOptional;
}

chip::Optional<uint16_t> mTimedInteractionTimeoutMs;
chip::Optional<bool> mSuppressResponse;
chip::Optional<uint16_t> mRepeatCount;
chip::Optional<uint16_t> mRepeatDelayInMs;
chip::Optional<uint16_t> mBusyWaitForMs;
};

class InteractionModelWriter
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -487,20 +517,34 @@ class InteractionModelWriter
return *this;
}

InteractionModelWriter & SetBusyWaitForMs(uint16_t busyWaitForMs)
{
mBusyWaitForMs.SetValue(busyWaitForMs);
return *this;
}

InteractionModelWriter & SetBusyWaitForMs(const chip::Optional<uint16_t> & busyWaitForMs)
{
mBusyWaitForMs = busyWaitForMs;
return *this;
}

void ResetOptions()
{
mTimedInteractionTimeoutMs = chip::NullOptional;
mSuppressResponse = chip::NullOptional;
mDataVersions = chip::NullOptional;
mRepeatCount = chip::NullOptional;
mRepeatDelayInMs = chip::NullOptional;
mBusyWaitForMs = chip::NullOptional;
}

chip::Optional<uint16_t> mTimedInteractionTimeoutMs;
chip::Optional<std::vector<chip::DataVersion>> mDataVersions;
chip::Optional<bool> mSuppressResponse;
chip::Optional<uint16_t> mRepeatCount;
chip::Optional<uint16_t> mRepeatDelayInMs;
chip::Optional<uint16_t> mBusyWaitForMs;

private:
template <typename T>
Expand Down

0 comments on commit d59dbb4

Please sign in to comment.