From 9010083fbe885d108b2b8203b935c8a5d563d681 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 3 Jan 2023 18:23:46 +0100 Subject: [PATCH] [chip-tool] Simplify the interaction model APIs to make it easier to maintain/review (#24091) * [chip-tool] Move common arguments from commands/clusters/ReportCommand.h to src/app/tests/suites/commands/interaction_model/InteractionModel.h and simplify the InteractionModel helper APIs * [chip-tool] Move common arguments from commands/clusters/ClusterCommand.h to src/app/tests/suites/commands/interaction_model/InteractionModel.h and simplify the InteractionModel helper APIs * [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/ClusterCommand.h | 10 +- .../commands/clusters/ReportCommand.h | 49 +-- .../commands/clusters/WriteAttributeCommand.h | 10 +- .../interaction_model/InteractionModel.cpp | 114 +++--- .../interaction_model/InteractionModel.h | 363 ++++++++++++++---- 5 files changed, 351 insertions(+), 195 deletions(-) diff --git a/examples/chip-tool/commands/clusters/ClusterCommand.h b/examples/chip-tool/commands/clusters/ClusterCommand.h index 33320760dd9d9a..f6cb307021ac5d 100644 --- a/examples/chip-tool/commands/clusters/ClusterCommand.h +++ b/examples/chip-tool/commands/clusters/ClusterCommand.h @@ -45,16 +45,14 @@ 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); + return InteractionModelCommands::SendCommand(device, endpointIds.at(0), mClusterId, mCommandId, mPayload); } template 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); + return InteractionModelCommands::SendCommand(device, endpointId, clusterId, commandId, value); } CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex) override @@ -184,10 +182,6 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub private: chip::ClusterId mClusterId; chip::CommandId mCommandId; - chip::Optional mTimedInteractionTimeoutMs; - chip::Optional mSuppressResponse; - chip::Optional mRepeatCount; - chip::Optional mRepeatDelayInMs; CHIP_ERROR mError = CHIP_NO_ERROR; CustomArgument mPayload; diff --git a/examples/chip-tool/commands/clusters/ReportCommand.h b/examples/chip-tool/commands/clusters/ReportCommand.h index b79ec5eb28ec19..eae5fa8ee51054 100644 --- a/examples/chip-tool/commands/clusters/ReportCommand.h +++ b/examples/chip-tool/commands/clusters/ReportCommand.h @@ -209,7 +209,7 @@ class ReadAttribute : public ReadCommand CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds) override { - return ReadCommand::ReadAttribute(device, endpointIds, mClusterIds, mAttributeIds, mFabricFiltered, mDataVersion); + return ReadCommand::ReadAttribute(device, endpointIds, mClusterIds, mAttributeIds); } private: @@ -224,14 +224,12 @@ class ReadAttribute : public ReadCommand { AddArgument("fabric-filtered", 0, 1, &mFabricFiltered, "Boolean indicating whether to do a fabric-filtered read. Defaults to true."); - AddArgument("data-version", 0, UINT32_MAX, &mDataVersion, + AddArgument("data-version", 0, UINT32_MAX, &mDataVersions, "Comma-separated list of data versions for the clusters being read."); } std::vector mClusterIds; std::vector mAttributeIds; - chip::Optional mFabricFiltered; - chip::Optional> mDataVersion; }; class SubscribeAttribute : public SubscribeCommand @@ -269,8 +267,7 @@ class SubscribeAttribute : public SubscribeCommand CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds) override { - return SubscribeCommand::SubscribeAttribute(device, endpointIds, mClusterIds, mAttributeIds, mMinInterval, mMaxInterval, - mFabricFiltered, mDataVersion, mKeepSubscriptions, mAutoResubscribe); + return SubscribeCommand::SubscribeAttribute(device, endpointIds, mClusterIds, mAttributeIds); } private: @@ -289,7 +286,7 @@ class SubscribeAttribute : public SubscribeCommand "Server must send a report if this number of seconds has elapsed since the last report."); AddArgument("fabric-filtered", 0, 1, &mFabricFiltered, "Boolean indicating whether to do a fabric-filtered subscription. Defaults to true."); - AddArgument("data-version", 0, UINT32_MAX, &mDataVersion, + AddArgument("data-version", 0, UINT32_MAX, &mDataVersions, "Comma-separated list of data versions for the clusters being subscribed to."); AddArgument("keepSubscriptions", 0, 1, &mKeepSubscriptions, "Boolean indicating whether to keep existing subscriptions when creating the new one. Defaults to false."); @@ -299,13 +296,6 @@ class SubscribeAttribute : public SubscribeCommand std::vector mClusterIds; std::vector mAttributeIds; - - uint16_t mMinInterval; - uint16_t mMaxInterval; - chip::Optional mFabricFiltered; - chip::Optional> mDataVersion; - chip::Optional mKeepSubscriptions; - chip::Optional mAutoResubscribe; }; class ReadEvent : public ReadCommand @@ -344,14 +334,12 @@ class ReadEvent : public ReadCommand CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds) override { - return ReadCommand::ReadEvent(device, endpointIds, mClusterIds, mEventIds, mFabricFiltered, mEventNumber); + return ReadCommand::ReadEvent(device, endpointIds, mClusterIds, mEventIds); } private: std::vector mClusterIds; std::vector mEventIds; - chip::Optional mFabricFiltered; - chip::Optional mEventNumber; }; class SubscribeEvent : public SubscribeCommand @@ -409,21 +397,12 @@ class SubscribeEvent : public SubscribeCommand CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds) override { - return SubscribeCommand::SubscribeEvent(device, endpointIds, mClusterIds, mEventIds, mMinInterval, mMaxInterval, - mFabricFiltered, mEventNumber, mKeepSubscriptions, mIsUrgents, mAutoResubscribe); + return SubscribeCommand::SubscribeEvent(device, endpointIds, mClusterIds, mEventIds); } private: std::vector mClusterIds; std::vector mEventIds; - - uint16_t mMinInterval; - uint16_t mMaxInterval; - chip::Optional mFabricFiltered; - chip::Optional mEventNumber; - chip::Optional mKeepSubscriptions; - chip::Optional> mIsUrgents; - chip::Optional mAutoResubscribe; }; class ReadAll : public ReadCommand @@ -458,18 +437,13 @@ class ReadAll : public ReadCommand CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds) override { - return ReadCommand::ReadAll(device, endpointIds, mClusterIds, mAttributeIds, mEventIds, mFabricFiltered, mDataVersions, - mEventNumber); + return ReadCommand::ReadAll(device, endpointIds, mClusterIds, mAttributeIds, mEventIds); } private: std::vector mClusterIds; std::vector mAttributeIds; std::vector mEventIds; - - chip::Optional mFabricFiltered; - chip::Optional> mDataVersions; - chip::Optional mEventNumber; }; class SubscribeAll : public SubscribeCommand @@ -502,18 +476,11 @@ class SubscribeAll : public SubscribeCommand CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector endpointIds) override { - return SubscribeCommand::SubscribeAll(device, endpointIds, mClusterIds, mAttributeIds, mEventIds, mMinInterval, - mMaxInterval, mFabricFiltered, mEventNumber, mKeepSubscriptions); + return SubscribeCommand::SubscribeAll(device, endpointIds, mClusterIds, mAttributeIds, mEventIds); } private: std::vector mClusterIds; std::vector mAttributeIds; std::vector mEventIds; - - uint16_t mMinInterval; - uint16_t mMaxInterval; - chip::Optional mFabricFiltered; - chip::Optional mEventNumber; - chip::Optional mKeepSubscriptions; }; 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.cpp b/src/app/tests/suites/commands/interaction_model/InteractionModel.cpp index fdd6dc6e9ed536..ad4bf1603a258d 100644 --- a/src/app/tests/suites/commands/interaction_model/InteractionModel.cpp +++ b/src/app/tests/suites/commands/interaction_model/InteractionModel.cpp @@ -37,8 +37,11 @@ CHIP_ERROR InteractionModel::ReadAttribute(const char * identity, EndpointId end dataVersions.Value().push_back(dataVersion.Value()); } - return InteractionModelReports::ReadAttribute(device, endpointIds, clusterIds, attributeIds, Optional(fabricFiltered), - dataVersions); + InteractionModelReports::ResetOptions(); + InteractionModelReports::SetFabricFiltered(fabricFiltered); + InteractionModelReports::SetDataVersions(dataVersions); + + return InteractionModelReports::ReadAttribute(device, endpointIds, clusterIds, attributeIds); } CHIP_ERROR InteractionModel::ReadEvent(const char * identity, EndpointId endpointId, ClusterId clusterId, EventId eventId, @@ -50,8 +53,12 @@ CHIP_ERROR InteractionModel::ReadEvent(const char * identity, EndpointId endpoin std::vector endpointIds = { endpointId }; std::vector clusterIds = { clusterId }; std::vector eventIds = { eventId }; - return InteractionModelReports::ReadEvent(device, endpointIds, clusterIds, eventIds, Optional(fabricFiltered), - eventNumber); + + InteractionModelReports::ResetOptions(); + InteractionModelReports::SetFabricFiltered(fabricFiltered); + InteractionModelReports::SetEventNumber(eventNumber); + + return InteractionModelReports::ReadEvent(device, endpointIds, clusterIds, eventIds); } CHIP_ERROR InteractionModel::SubscribeAttribute(const char * identity, EndpointId endpointId, ClusterId clusterId, @@ -72,9 +79,15 @@ CHIP_ERROR InteractionModel::SubscribeAttribute(const char * identity, EndpointI dataVersions.Value().push_back(dataVersion.Value()); } - return InteractionModelReports::SubscribeAttribute(device, endpointIds, clusterIds, attributeIds, minInterval, maxInterval, - Optional(fabricFiltered), dataVersions, keepSubscriptions, - autoResubscribe); + InteractionModelReports::ResetOptions(); + InteractionModelReports::SetMinInterval(minInterval); + InteractionModelReports::SetMaxInterval(maxInterval); + InteractionModelReports::SetFabricFiltered(fabricFiltered); + InteractionModelReports::SetDataVersions(dataVersions); + InteractionModelReports::SetKeepSubscriptions(keepSubscriptions); + InteractionModelReports::SetAutoResubscribe(autoResubscribe); + + return InteractionModelReports::SubscribeAttribute(device, endpointIds, clusterIds, attributeIds); } CHIP_ERROR InteractionModel::SubscribeEvent(const char * identity, EndpointId endpointId, ClusterId clusterId, EventId eventId, @@ -88,9 +101,16 @@ CHIP_ERROR InteractionModel::SubscribeEvent(const char * identity, EndpointId en std::vector endpointIds = { endpointId }; std::vector clusterIds = { clusterId }; std::vector eventIds = { eventId }; - return InteractionModelReports::SubscribeEvent(device, endpointIds, clusterIds, eventIds, minInterval, maxInterval, - Optional(fabricFiltered), eventNumber, keepSubscriptions, NullOptional, - autoResubscribe); + + InteractionModelReports::ResetOptions(); + SetMinInterval(minInterval); + SetMaxInterval(maxInterval); + SetFabricFiltered(fabricFiltered); + SetEventNumber(eventNumber); + SetKeepSubscriptions(keepSubscriptions); + SetAutoResubscribe(autoResubscribe); + + return InteractionModelReports::SubscribeEvent(device, endpointIds, clusterIds, eventIds); } void InteractionModel::Shutdown() @@ -281,15 +301,11 @@ CHIP_ERROR InteractionModelConfig::GetAttributePaths(std::vector end CHIP_ERROR InteractionModelReports::ReportAttribute(DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, - ReadClient::InteractionType interactionType, uint16_t minInterval, - uint16_t maxInterval, const Optional & fabricFiltered, - const Optional> & dataVersions, - const Optional & keepSubscriptions, - const Optional & autoResubscribe) + ReadClient::InteractionType interactionType) { InteractionModelConfig::AttributePathsConfig pathsConfig; ReturnErrorOnFailure( - InteractionModelConfig::GetAttributePaths(endpointIds, clusterIds, attributeIds, dataVersions, pathsConfig)); + InteractionModelConfig::GetAttributePaths(endpointIds, clusterIds, attributeIds, mDataVersions, pathsConfig)); ChipLogProgress(chipTool, "Sending %sAttribute to:", interactionType == ReadClient::InteractionType::Subscribe ? "Subscribe" : "Read"); @@ -300,12 +316,12 @@ CHIP_ERROR InteractionModelReports::ReportAttribute(DeviceProxy * device, std::v params.mpAttributePathParamsList = pathsConfig.attributePathParams.get(); params.mAttributePathParamsListSize = pathsConfig.count; - if (fabricFiltered.HasValue()) + if (mFabricFiltered.HasValue()) { - params.mIsFabricFiltered = fabricFiltered.Value(); + params.mIsFabricFiltered = mFabricFiltered.Value(); } - if (dataVersions.HasValue()) + if (mDataVersions.HasValue()) { params.mpDataVersionFilterList = pathsConfig.dataVersionFilter.get(); params.mDataVersionFilterListSize = pathsConfig.count; @@ -313,11 +329,11 @@ CHIP_ERROR InteractionModelReports::ReportAttribute(DeviceProxy * device, std::v if (interactionType == ReadClient::InteractionType::Subscribe) { - params.mMinIntervalFloorSeconds = minInterval; - params.mMaxIntervalCeilingSeconds = maxInterval; - if (keepSubscriptions.HasValue()) + params.mMinIntervalFloorSeconds = mMinInterval; + params.mMaxIntervalCeilingSeconds = mMaxInterval; + if (mKeepSubscriptions.HasValue()) { - params.mKeepSubscriptions = keepSubscriptions.Value(); + params.mKeepSubscriptions = mKeepSubscriptions.Value(); } } @@ -327,10 +343,10 @@ CHIP_ERROR InteractionModelReports::ReportAttribute(DeviceProxy * device, std::v { ReturnErrorOnFailure(client->SendRequest(params)); } - else if (autoResubscribe.ValueOr(false)) + else if (mAutoResubscribe.ValueOr(false)) { pathsConfig.attributePathParams.release(); - if (dataVersions.HasValue()) + if (mDataVersions.HasValue()) { pathsConfig.dataVersionFilter.release(); } @@ -348,16 +364,12 @@ CHIP_ERROR InteractionModelReports::ReportAttribute(DeviceProxy * device, std::v CHIP_ERROR InteractionModelReports::ReportEvent(DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector eventIds, - ReadClient::InteractionType interactionType, uint16_t minInterval, - uint16_t maxInterval, const Optional & fabricFiltered, - const Optional & eventNumber, const Optional & keepSubscriptions, - const Optional> & isUrgents, - const Optional & autoResubscribe) + chip::app::ReadClient::InteractionType interactionType) { const size_t clusterCount = clusterIds.size(); const size_t eventCount = eventIds.size(); const size_t endpointCount = endpointIds.size(); - const size_t isUrgentCount = isUrgents.HasValue() ? isUrgents.Value().size() : 0; + const size_t isUrgentCount = mIsUrgents.HasValue() ? mIsUrgents.Value().size() : 0; VerifyOrReturnError(clusterCount > 0 && clusterCount <= kMaxAllowedPaths, CHIP_ERROR_INVALID_ARGUMENT); VerifyOrReturnError(eventCount > 0 && eventCount <= kMaxAllowedPaths, CHIP_ERROR_INVALID_ARGUMENT); @@ -426,37 +438,37 @@ CHIP_ERROR InteractionModelReports::ReportEvent(DeviceProxy * device, std::vecto eventPathParams[i].mEndpointId = endpointId; } - if (isUrgents.HasValue() && isUrgents.Value().size() > i) + if (mIsUrgents.HasValue() && mIsUrgents.Value().size() > i) { - eventPathParams[i].mIsUrgentEvent = isUrgents.Value().at(i); + eventPathParams[i].mIsUrgentEvent = mIsUrgents.Value().at(i); } } ReadPrepareParams params(device->GetSecureSession().Value()); params.mpEventPathParamsList = eventPathParams.get(); params.mEventPathParamsListSize = pathsCount; - params.mEventNumber = eventNumber; + params.mEventNumber = mEventNumber; params.mpAttributePathParamsList = nullptr; params.mAttributePathParamsListSize = 0; - if (fabricFiltered.HasValue()) + if (mFabricFiltered.HasValue()) { - params.mIsFabricFiltered = fabricFiltered.Value(); + params.mIsFabricFiltered = mFabricFiltered.Value(); } if (interactionType == ReadClient::InteractionType::Subscribe) { - params.mMinIntervalFloorSeconds = minInterval; - params.mMaxIntervalCeilingSeconds = maxInterval; - if (keepSubscriptions.HasValue()) + params.mMinIntervalFloorSeconds = mMinInterval; + params.mMaxIntervalCeilingSeconds = mMaxInterval; + if (mKeepSubscriptions.HasValue()) { - params.mKeepSubscriptions = keepSubscriptions.Value(); + params.mKeepSubscriptions = mKeepSubscriptions.Value(); } } auto client = std::make_unique(InteractionModelEngine::GetInstance(), device->GetExchangeManager(), mBufferedReadAdapter, interactionType); - if (autoResubscribe.ValueOr(false)) + if (mAutoResubscribe.ValueOr(false)) { eventPathParams.release(); ReturnErrorOnFailure(client->SendAutoResubscribeRequest(std::move(params))); @@ -479,11 +491,7 @@ void InteractionModelReports::CleanupReadClient(ReadClient * aReadClient) CHIP_ERROR InteractionModelReports::ReportAll(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, std::vector eventIds, - chip::app::ReadClient::InteractionType interactionType, uint16_t minInterval, - uint16_t maxInterval, const chip::Optional & fabricFiltered, - const chip::Optional> & dataVersions, - const chip::Optional & eventNumber, - const chip::Optional & keepSubscriptions) + chip::app::ReadClient::InteractionType interactionType) { const size_t endpointCount = endpointIds.size(); const size_t clusterCount = clusterIds.size(); @@ -578,22 +586,22 @@ CHIP_ERROR InteractionModelReports::ReportAll(chip::DeviceProxy * device, std::v ReadPrepareParams params(device->GetSecureSession().Value()); params.mpEventPathParamsList = eventPathParams; params.mEventPathParamsListSize = eventCount; - params.mEventNumber = eventNumber; + params.mEventNumber = mEventNumber; params.mpAttributePathParamsList = attributePathParams; params.mAttributePathParamsListSize = attributeCount; - if (fabricFiltered.HasValue()) + if (mFabricFiltered.HasValue()) { - params.mIsFabricFiltered = fabricFiltered.Value(); + params.mIsFabricFiltered = mFabricFiltered.Value(); } if (interactionType == ReadClient::InteractionType::Subscribe) { - params.mMinIntervalFloorSeconds = minInterval; - params.mMaxIntervalCeilingSeconds = maxInterval; - if (keepSubscriptions.HasValue()) + params.mMinIntervalFloorSeconds = mMinInterval; + params.mMaxIntervalCeilingSeconds = mMaxInterval; + if (mKeepSubscriptions.HasValue()) { - params.mKeepSubscriptions = keepSubscriptions.Value(); + params.mKeepSubscriptions = mKeepSubscriptions.Value(); } } diff --git a/src/app/tests/suites/commands/interaction_model/InteractionModel.h b/src/app/tests/suites/commands/interaction_model/InteractionModel.h index c16318070a68ae..83a0d6fff769b6 100644 --- a/src/app/tests/suites/commands/interaction_model/InteractionModel.h +++ b/src/app/tests/suites/commands/interaction_model/InteractionModel.h @@ -49,91 +49,59 @@ class InteractionModelConfig class InteractionModelReports { public: - InteractionModelReports(chip::app::ReadClient::Callback * callback) : mBufferedReadAdapter(*callback) {} + InteractionModelReports(chip::app::ReadClient::Callback * callback) : mBufferedReadAdapter(*callback) { ResetOptions(); } protected: CHIP_ERROR ReadAttribute(chip::DeviceProxy * device, std::vector endpointIds, - std::vector clusterIds, std::vector attributeIds, - const chip::Optional & fabricFiltered = chip::Optional(true), - const chip::Optional> & dataVersions = chip::NullOptional) + std::vector clusterIds, std::vector attributeIds) { - return ReportAttribute(device, endpointIds, clusterIds, attributeIds, chip::app::ReadClient::InteractionType::Read, 0, 0, - fabricFiltered, dataVersions, chip::NullOptional, chip::NullOptional); + return ReportAttribute(device, endpointIds, clusterIds, attributeIds, chip::app::ReadClient::InteractionType::Read); } CHIP_ERROR SubscribeAttribute(chip::DeviceProxy * device, std::vector endpointIds, - std::vector clusterIds, std::vector attributeIds, - uint16_t minInterval, uint16_t maxInterval, const chip::Optional & fabricFiltered, - const chip::Optional> & dataVersions, - const chip::Optional & keepSubscriptions, const chip::Optional & autoResubscribe) + std::vector clusterIds, std::vector attributeIds) { - return ReportAttribute(device, endpointIds, clusterIds, attributeIds, chip::app::ReadClient::InteractionType::Subscribe, - minInterval, maxInterval, fabricFiltered, dataVersions, keepSubscriptions, autoResubscribe); + return ReportAttribute(device, endpointIds, clusterIds, attributeIds, chip::app::ReadClient::InteractionType::Subscribe); } CHIP_ERROR ReportAttribute(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, - chip::app::ReadClient::InteractionType interactionType, uint16_t minInterval, uint16_t maxInterval, - const chip::Optional & fabricFiltered, - const chip::Optional> & dataVersions, - const chip::Optional & keepSubscriptions, const chip::Optional & autoResubscribe); + chip::app::ReadClient::InteractionType interactionType); CHIP_ERROR ReadEvent(chip::DeviceProxy * device, std::vector endpointIds, - std::vector clusterIds, std::vector eventIds, - const chip::Optional & fabricFiltered = chip::Optional(true), - const chip::Optional & eventNumber = chip::NullOptional) + std::vector clusterIds, std::vector eventIds) { - return ReportEvent(device, endpointIds, clusterIds, eventIds, chip::app::ReadClient::InteractionType::Read, 0, 0, - fabricFiltered, eventNumber, chip::NullOptional, chip::NullOptional, chip::NullOptional); + return ReportEvent(device, endpointIds, clusterIds, eventIds, chip::app::ReadClient::InteractionType::Read); } CHIP_ERROR SubscribeEvent(chip::DeviceProxy * device, std::vector endpointIds, - std::vector clusterIds, std::vector eventIds, uint16_t minInterval, - uint16_t maxInterval, const chip::Optional & fabricFiltered, - const chip::Optional & eventNumber, const chip::Optional & keepSubscriptions, - const chip::Optional> & isUrgents, const chip::Optional & autoResubscribe) + std::vector clusterIds, std::vector eventIds) { - return ReportEvent(device, endpointIds, clusterIds, eventIds, chip::app::ReadClient::InteractionType::Subscribe, - minInterval, maxInterval, fabricFiltered, eventNumber, keepSubscriptions, isUrgents, autoResubscribe); + return ReportEvent(device, endpointIds, clusterIds, eventIds, chip::app::ReadClient::InteractionType::Subscribe); } CHIP_ERROR ReportEvent(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector eventIds, - chip::app::ReadClient::InteractionType interactionType, uint16_t minInterval, uint16_t maxInterval, - const chip::Optional & fabricFiltered, const chip::Optional & eventNumber, - const chip::Optional & keepSubscriptions, const chip::Optional> & isUrgents, - const chip::Optional & autoResubscribe); + chip::app::ReadClient::InteractionType interactionType); CHIP_ERROR ReadAll(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, - std::vector eventIds, - const chip::Optional & fabricFiltered = chip::Optional(true), - const chip::Optional> & dataVersions = chip::NullOptional, - const chip::Optional & eventNumber = chip::NullOptional) + std::vector eventIds) { - return ReportAll(device, endpointIds, clusterIds, attributeIds, eventIds, chip::app::ReadClient::InteractionType::Read, 0, - 0, fabricFiltered, dataVersions, eventNumber); + return ReportAll(device, endpointIds, clusterIds, attributeIds, eventIds, chip::app::ReadClient::InteractionType::Read); } CHIP_ERROR SubscribeAll(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, - std::vector eventIds, uint16_t minInterval = 0, uint16_t maxInterval = 0, - const chip::Optional & fabricFiltered = chip::Optional(true), - const chip::Optional & eventNumber = chip::NullOptional, - const chip::Optional & keepSubscriptions = chip::NullOptional) + std::vector eventIds) { - return ReportAll(device, endpointIds, clusterIds, attributeIds, eventIds, chip::app::ReadClient::InteractionType::Subscribe, - minInterval, maxInterval, fabricFiltered, chip::NullOptional, eventNumber, keepSubscriptions); + return ReportAll(device, endpointIds, clusterIds, attributeIds, eventIds, + chip::app::ReadClient::InteractionType::Subscribe); } CHIP_ERROR ReportAll(chip::DeviceProxy * device, std::vector endpointIds, std::vector clusterIds, std::vector attributeIds, - std::vector eventIds, chip::app::ReadClient::InteractionType interactionType, - uint16_t minInterval = 0, uint16_t maxInterval = 0, - const chip::Optional & fabricFiltered = chip::Optional(true), - const chip::Optional> & dataVersions = chip::NullOptional, - const chip::Optional & eventNumber = chip::NullOptional, - const chip::Optional & keepSubscriptions = chip::NullOptional); + std::vector eventIds, chip::app::ReadClient::InteractionType interactionType); void OnDeallocatePaths(chip::app::ReadPrepareParams && aReadPrepareParams); @@ -143,40 +111,135 @@ class InteractionModelReports std::vector> mReadClients; chip::app::BufferedReadCallback mBufferedReadAdapter; + + InteractionModelReports & SetDataVersions(const std::vector & dataVersions) + { + mDataVersions.SetValue(dataVersions); + return *this; + } + + InteractionModelReports & SetDataVersions(const chip::Optional> & dataVersions) + { + mDataVersions = dataVersions; + return *this; + } + + InteractionModelReports & SetIsUrgents(const std::vector isUrgents) + { + mIsUrgents.SetValue(isUrgents); + return *this; + } + + InteractionModelReports & SetIsUrgents(const chip::Optional> & isUrgents) + { + mIsUrgents = isUrgents; + return *this; + } + + InteractionModelReports & SetEventNumber(const chip::Optional & eventNumber) + { + mEventNumber = eventNumber; + return *this; + } + + InteractionModelReports & SetEventNumber(chip::EventNumber eventNumber) + { + mEventNumber.SetValue(eventNumber); + return *this; + } + + InteractionModelReports & SetFabricFiltered(bool fabricFiltered) + { + mFabricFiltered.SetValue(fabricFiltered); + return *this; + } + + InteractionModelReports & SetKeepSubscriptions(bool keepSubscriptions) + { + mKeepSubscriptions.SetValue(keepSubscriptions); + return *this; + } + + InteractionModelReports & SetKeepSubscriptions(const chip::Optional & keepSubscriptions) + { + mKeepSubscriptions = keepSubscriptions; + return *this; + } + + InteractionModelReports & SetAutoResubscribe(bool autoResubscribe) + { + mAutoResubscribe.SetValue(autoResubscribe); + return *this; + } + + InteractionModelReports & SetAutoResubscribe(const chip::Optional & autoResubscribe) + { + mAutoResubscribe = autoResubscribe; + return *this; + } + + InteractionModelReports & SetMinInterval(uint16_t minInterval) + { + mMinInterval = minInterval; + return *this; + } + + InteractionModelReports & SetMaxInterval(uint16_t maxInterval) + { + mMaxInterval = maxInterval; + return *this; + } + + void ResetOptions() + { + mDataVersions = chip::NullOptional; + mIsUrgents = chip::NullOptional; + mEventNumber = chip::NullOptional; + mFabricFiltered = chip::Optional(true); + mKeepSubscriptions = chip::NullOptional; + mAutoResubscribe = chip::NullOptional; + mMinInterval = 0; + mMaxInterval = 0; + } + + chip::Optional> mDataVersions; + chip::Optional> mIsUrgents; + chip::Optional mEventNumber; + chip::Optional mFabricFiltered; + chip::Optional mKeepSubscriptions; + chip::Optional mAutoResubscribe; + uint16_t mMinInterval; + uint16_t mMaxInterval; }; class InteractionModelCommands { public: - InteractionModelCommands(chip::app::CommandSender::Callback * callback) : mCallback(callback) {} + InteractionModelCommands(chip::app::CommandSender::Callback * callback) : mCallback(callback) { ResetOptions(); } protected: template 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 & suppressResponse = chip::NullOptional, - const chip::Optional & repeatCount = chip::NullOptional, - const chip::Optional & repeatDelayInMs = chip::NullOptional) + chip::CommandId commandId, const T & value) { - uint16_t repeat = repeatCount.ValueOr(1); + uint16_t repeat = mRepeatCount.ValueOr(1); while (repeat--) { chip::app::CommandPathParams commandPath = { endpointId, clusterId, commandId, (chip::app::CommandPathFlags::kEndpointIdValid) }; auto commandSender = std::make_unique(mCallback, device->GetExchangeManager(), - timedInteractionTimeoutMs.HasValue()); + mTimedInteractionTimeoutMs.HasValue()); VerifyOrReturnError(commandSender != nullptr, CHIP_ERROR_NO_MEMORY); - ReturnErrorOnFailure(commandSender->AddRequestDataNoTimedCheck(commandPath, value, timedInteractionTimeoutMs, - suppressResponse.ValueOr(false))); + ReturnErrorOnFailure(commandSender->AddRequestDataNoTimedCheck(commandPath, value, mTimedInteractionTimeoutMs, + mSuppressResponse.ValueOr(false))); ReturnErrorOnFailure(commandSender->SendCommandRequest(device->GetSecureSession().Value())); mCommandSender.push_back(std::move(commandSender)); - if (repeatDelayInMs.HasValue()) + if (mRepeatDelayInMs.HasValue()) { - chip::test_utils::SleepMillis(repeatDelayInMs.Value()); + chip::test_utils::SleepMillis(mRepeatDelayInMs.Value()); } } return CHIP_NO_ERROR; @@ -209,6 +272,67 @@ class InteractionModelCommands std::vector> mCommandSender; chip::app::CommandSender::Callback * mCallback; + + InteractionModelCommands & SetTimedInteractionTimeoutMs(uint16_t timedInteractionTimeoutMs) + { + mTimedInteractionTimeoutMs.SetValue(timedInteractionTimeoutMs); + return *this; + } + + InteractionModelCommands & SetTimedInteractionTimeoutMs(const chip::Optional & timedInteractionTimeoutMs) + { + mTimedInteractionTimeoutMs = timedInteractionTimeoutMs; + return *this; + } + + InteractionModelCommands & SetSuppressResponse(bool suppressResponse) + { + mSuppressResponse.SetValue(suppressResponse); + return *this; + } + + InteractionModelCommands & SetSuppressResponse(const chip::Optional & suppressResponse) + { + mSuppressResponse = suppressResponse; + return *this; + } + + InteractionModelCommands & SetRepeatCount(uint16_t repeatCount) + { + mRepeatCount.SetValue(repeatCount); + return *this; + } + + InteractionModelCommands & SetRepeatCount(const chip::Optional & repeatCount) + { + mRepeatCount = repeatCount; + return *this; + } + + InteractionModelCommands & SetRepeatDelayInMs(uint16_t repeatDelayInMs) + { + mRepeatDelayInMs.SetValue(repeatDelayInMs); + return *this; + } + + InteractionModelCommands & SetRepeatDelayInMs(const chip::Optional & repeatDelayInMs) + { + mRepeatDelayInMs = repeatDelayInMs; + return *this; + } + + void ResetOptions() + { + mTimedInteractionTimeoutMs = chip::NullOptional; + mSuppressResponse = chip::NullOptional; + mRepeatCount = chip::NullOptional; + mRepeatDelayInMs = chip::NullOptional; + } + + chip::Optional mTimedInteractionTimeoutMs; + chip::Optional mSuppressResponse; + chip::Optional mRepeatCount; + chip::Optional mRepeatDelayInMs; }; class InteractionModelWriter @@ -220,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++) @@ -251,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()); } } @@ -262,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 @@ -314,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, @@ -385,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 @@ -409,8 +601,11 @@ class InteractionModel : public InteractionModelReports, chip::DeviceProxy * device = GetDevice(identity); VerifyOrReturnError(device != nullptr, CHIP_ERROR_INCORRECT_STATE); - return InteractionModelCommands::SendCommand(device, endpointId, clusterId, commandId, value, timedInteractionTimeoutMs, - suppressResponse); + InteractionModelCommands::ResetOptions(); + InteractionModelCommands::SetTimedInteractionTimeoutMs(timedInteractionTimeoutMs); + InteractionModelCommands::SetSuppressResponse(suppressResponse); + + return InteractionModelCommands::SendCommand(device, endpointId, clusterId, commandId, value); } template