From 1fb3b7989813ab3a3ab9e1c9d679a9fa6777cfc4 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 15 Dec 2022 12:35:47 +0100 Subject: [PATCH] [chip-tool] Move common arguments from commands/clusters/WriteAttributeCommand.h to src/app/tests/suites/commands/interaction_model/InteractionModel.h and simplify the InteractionModel helper APIs --- .../commands/clusters/WriteAttributeCommand.h | 10 +- .../interaction_model/InteractionModel.h | 110 ++++++++++++++---- 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/examples/chip-tool/commands/clusters/WriteAttributeCommand.h b/examples/chip-tool/commands/clusters/WriteAttributeCommand.h index d220468d622260..56363cb481721d 100644 --- a/examples/chip-tool/commands/clusters/WriteAttributeCommand.h +++ b/examples/chip-tool/commands/clusters/WriteAttributeCommand.h @@ -129,9 +129,7 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, const T & values) { - return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, values, - mTimedInteractionTimeoutMs, mSuppressResponse, mDataVersions, mRepeatCount, - mRepeatDelayInMs); + return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, values); } CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, std::vector clusterIds, @@ -249,12 +247,6 @@ class WriteAttribute : public InteractionModelWriter, public ModelCommand, publi std::vector mAttributeIds; CHIP_ERROR mError = CHIP_NO_ERROR; - chip::Optional mTimedInteractionTimeoutMs; - chip::Optional> mDataVersions; - chip::Optional mSuppressResponse; - chip::Optional mRepeatCount; - chip::Optional mRepeatDelayInMs; - T mAttributeValues; }; diff --git a/src/app/tests/suites/commands/interaction_model/InteractionModel.h b/src/app/tests/suites/commands/interaction_model/InteractionModel.h index 5a73a582da4ee1..83a0d6fff769b6 100644 --- a/src/app/tests/suites/commands/interaction_model/InteractionModel.h +++ b/src/app/tests/suites/commands/interaction_model/InteractionModel.h @@ -344,25 +344,20 @@ class InteractionModelWriter template CHIP_ERROR WriteAttribute(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, - const std::vector & values, - const chip::Optional & timedInteractionTimeoutMs = 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) + const std::vector & values) { InteractionModelConfig::AttributePathsConfig pathsConfig; ReturnErrorOnFailure( - InteractionModelConfig::GetAttributePaths(endpointIds, clusterIds, attributeIds, dataVersions, pathsConfig)); + InteractionModelConfig::GetAttributePaths(endpointIds, clusterIds, attributeIds, mDataVersions, pathsConfig)); VerifyOrReturnError(pathsConfig.count == values.size() || values.size() == 1, CHIP_ERROR_INVALID_ARGUMENT); - uint16_t repeat = repeatCount.ValueOr(1); + uint16_t repeat = mRepeatCount.ValueOr(1); while (repeat--) { mWriteClient = std::make_unique(device->GetExchangeManager(), &mChunkedWriteCallback, - timedInteractionTimeoutMs, suppressResponse.ValueOr(false)); + mTimedInteractionTimeoutMs, mSuppressResponse.ValueOr(false)); VerifyOrReturnError(mWriteClient != nullptr, CHIP_ERROR_NO_MEMORY); for (uint8_t i = 0; i < pathsConfig.count; i++) @@ -375,9 +370,9 @@ class InteractionModelWriter ReturnErrorOnFailure(mWriteClient->SendWriteRequest(device->GetSecureSession().Value())); - if (repeatDelayInMs.HasValue()) + if (mRepeatDelayInMs.HasValue()) { - chip::test_utils::SleepMillis(repeatDelayInMs.Value()); + chip::test_utils::SleepMillis(mRepeatDelayInMs.Value()); } } @@ -386,16 +381,10 @@ class InteractionModelWriter template 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 & suppressResponse = chip::NullOptional, - const chip::Optional> & dataVersions = chip::NullOptional, - const chip::Optional & repeatCount = chip::NullOptional, - const chip::Optional & repeatDelayInMs = chip::NullOptional) + std::vector clusterIds, std::vector attributeIds, const T & value) { std::vector values = { value }; - return WriteAttribute(device, endpointIds, clusterIds, attributeIds, values, timedInteractionTimeoutMs, suppressResponse, - dataVersions, repeatCount, repeatDelayInMs); + return WriteAttribute(device, endpointIds, clusterIds, attributeIds, values); } template @@ -438,6 +427,81 @@ class InteractionModelWriter std::unique_ptr mWriteClient; chip::app::ChunkedWriteCallback mChunkedWriteCallback; + InteractionModelWriter & SetTimedInteractionTimeoutMs(uint16_t timedInteractionTimeoutMs) + { + mTimedInteractionTimeoutMs.SetValue(timedInteractionTimeoutMs); + return *this; + } + + InteractionModelWriter & SetTimedInteractionTimeoutMs(const chip::Optional & timedInteractionTimeoutMs) + { + mTimedInteractionTimeoutMs = timedInteractionTimeoutMs; + return *this; + } + + InteractionModelWriter & SetSuppressResponse(bool suppressResponse) + { + mSuppressResponse.SetValue(suppressResponse); + return *this; + } + + InteractionModelWriter & SetSuppressResponse(const chip::Optional & suppressResponse) + { + mSuppressResponse = suppressResponse; + return *this; + } + + InteractionModelWriter & SetDataVersions(const std::vector & dataVersions) + { + mDataVersions.SetValue(dataVersions); + return *this; + } + + InteractionModelWriter & SetDataVersions(const chip::Optional> & dataVersions) + { + mDataVersions = dataVersions; + return *this; + } + + InteractionModelWriter & SetRepeatCount(uint16_t repeatCount) + { + mRepeatCount.SetValue(repeatCount); + return *this; + } + + InteractionModelWriter & SetRepeatCount(const chip::Optional & repeatCount) + { + mRepeatCount = repeatCount; + return *this; + } + + InteractionModelWriter & SetRepeatDelayInMs(uint16_t repeatDelayInMs) + { + mRepeatDelayInMs.SetValue(repeatDelayInMs); + return *this; + } + + InteractionModelWriter & SetRepeatDelayInMs(const chip::Optional & repeatDelayInMs) + { + mRepeatDelayInMs = repeatDelayInMs; + return *this; + } + + void ResetOptions() + { + mTimedInteractionTimeoutMs = chip::NullOptional; + mSuppressResponse = chip::NullOptional; + mDataVersions = chip::NullOptional; + mRepeatCount = chip::NullOptional; + mRepeatDelayInMs = chip::NullOptional; + } + + chip::Optional mTimedInteractionTimeoutMs; + chip::Optional> mDataVersions; + chip::Optional mSuppressResponse; + chip::Optional mRepeatCount; + chip::Optional mRepeatDelayInMs; + private: template CHIP_ERROR EncodeAttribute(const chip::app::AttributePathParams & path, const chip::Optional & dataVersion, @@ -509,8 +573,12 @@ class InteractionModel : public InteractionModelReports, optionalDataVersions.SetValue(dataVersions); } - return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, value, - timedInteractionTimeoutMs, suppressResponse, optionalDataVersions); + InteractionModelWriter::ResetOptions(); + InteractionModelWriter::SetTimedInteractionTimeoutMs(timedInteractionTimeoutMs); + InteractionModelWriter::SetSuppressResponse(suppressResponse); + InteractionModelWriter::SetDataVersions(optionalDataVersions); + + return InteractionModelWriter::WriteAttribute(device, endpointIds, clusterIds, attributeIds, value); } template