Skip to content

Commit

Permalink
[chip-tool] Allow passing multiple data versions for attributes reads (
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored Mar 8, 2022
1 parent 535c6c8 commit 949bd86
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
48 changes: 30 additions & 18 deletions examples/chip-tool/commands/clusters/ReportCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,28 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
CHIP_ERROR ReportAttribute(ChipDevice * device, std::vector<chip::EndpointId> endpointIds,
std::vector<chip::ClusterId> clusterIds, std::vector<chip::AttributeId> attributeIds,
chip::app::ReadClient::InteractionType interactionType, uint16_t minInterval = 0,
uint16_t maxInterval = 0,
const chip::Optional<chip::DataVersion> & aDataVersion = chip::NullOptional)
uint16_t maxInterval = 0,
const chip::Optional<std::vector<chip::DataVersion>> & dataVersions = chip::NullOptional)
{
const size_t clusterCount = clusterIds.size();
const size_t attributeCount = attributeIds.size();
const size_t endpointCount = endpointIds.size();
const size_t clusterCount = clusterIds.size();
const size_t attributeCount = attributeIds.size();
const size_t endpointCount = endpointIds.size();
const size_t dataVersionsCount = dataVersions.HasValue() ? dataVersions.Value().size() : 0;

VerifyOrReturnError(clusterCount > 0 && clusterCount <= kMaxAllowedPaths, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(attributeCount > 0 && attributeCount <= kMaxAllowedPaths, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(endpointCount > 0 && endpointCount <= kMaxAllowedPaths, CHIP_ERROR_INVALID_ARGUMENT);

const bool hasSameIdsCount = (clusterCount == attributeCount) && (clusterCount == endpointCount);
const bool multipleClusters = clusterCount > 1 && attributeCount == 1 && endpointCount == 1;
const bool multipleAttributes = attributeCount > 1 && clusterCount == 1 && endpointCount == 1;
const bool multipleEndpoints = endpointCount > 1 && clusterCount == 1 && attributeCount == 1;
VerifyOrReturnError(dataVersionsCount <= kMaxAllowedPaths, CHIP_ERROR_INVALID_ARGUMENT);

const bool hasSameIdsCount = (clusterCount == attributeCount) && (clusterCount == endpointCount) &&
(dataVersionsCount == 0 || clusterCount == dataVersionsCount);
const bool multipleClusters =
clusterCount > 1 && attributeCount == 1 && endpointCount == 1 && (dataVersionsCount == 0 || dataVersionsCount == 1);
const bool multipleAttributes =
attributeCount > 1 && clusterCount == 1 && endpointCount == 1 && (dataVersionsCount == 0 || dataVersionsCount == 1);
const bool multipleEndpoints =
endpointCount > 1 && clusterCount == 1 && attributeCount == 1 && (dataVersionsCount == 0 || dataVersionsCount == 1);
const bool multipleDataVersions = dataVersionsCount > 1 && clusterCount == 1 && attributeCount == 1 && endpointCount == 1;

size_t pathsCount = 0;
if (hasSameIdsCount)
Expand All @@ -144,6 +151,10 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
{
pathsCount = endpointCount;
}
else if (multipleDataVersions)
{
pathsCount = dataVersionsCount;
}
else
{
ChipLogError(
Expand All @@ -157,11 +168,11 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
return CHIP_ERROR_INVALID_ARGUMENT;
}

chip::app::AttributePathParams attributePathParams[kMaxAllowedPaths];
chip::app::DataVersionFilter dataVersionFilter[kMaxAllowedPaths];

ChipLogProgress(chipTool, "Sending %sAttribute to:",
interactionType == chip::app::ReadClient::InteractionType::Subscribe ? "Subscribe" : "Read");

chip::app::AttributePathParams attributePathParams[kMaxAllowedPaths];
chip::app::DataVersionFilter dataVersionFilter[kMaxAllowedPaths];
for (size_t i = 0; i < pathsCount; i++)
{
chip::ClusterId clusterId = clusterIds.at((hasSameIdsCount || multipleClusters) ? i : 0);
Expand All @@ -174,11 +185,12 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
attributePathParams[i].mAttributeId = attributeId;
attributePathParams[i].mEndpointId = endpointId;

if (aDataVersion.HasValue())
if (dataVersions.HasValue())
{
chip::DataVersion dataVersion = dataVersions.Value().at((hasSameIdsCount || multipleDataVersions) ? i : 0);
dataVersionFilter[i].mEndpointId = endpointId;
dataVersionFilter[i].mClusterId = clusterId;
dataVersionFilter[i].mDataVersion.SetValue(aDataVersion.Value());
dataVersionFilter[i].mDataVersion.SetValue(dataVersion);
}
}

Expand All @@ -193,7 +205,7 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
params.mIsFabricFiltered = mFabricFiltered.Value();
}

if (aDataVersion.HasValue())
if (dataVersions.HasValue())
{
params.mpDataVersionFilterList = dataVersionFilter;
params.mDataVersionFilterListSize = pathsCount;
Expand Down Expand Up @@ -344,7 +356,7 @@ class ReadAttribute : public ReportCommand
private:
std::vector<chip::ClusterId> mClusterIds;
std::vector<chip::AttributeId> mAttributeIds;
chip::Optional<chip::DataVersion> mDataVersion;
chip::Optional<std::vector<chip::DataVersion>> mDataVersion;
};

class SubscribeAttribute : public ReportCommand
Expand Down Expand Up @@ -425,7 +437,7 @@ class SubscribeAttribute : public ReportCommand

uint16_t mMinInterval;
uint16_t mMaxInterval;
chip::Optional<chip::DataVersion> mDataVersion;
chip::Optional<std::vector<chip::DataVersion>> mDataVersion;
bool mWait;
};

Expand Down
30 changes: 29 additions & 1 deletion examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,29 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
vectorArgument->push_back(static_cast<uint16_t>(v));
}
}
else
else if (arg.type == ArgumentType::Vector32 && arg.flags != Argument::kOptional)
{
auto vectorArgument = static_cast<std::vector<uint32_t> *>(arg.value);
for (uint64_t v : values)
{
vectorArgument->push_back(static_cast<uint32_t>(v));
}
}
else if (arg.type == ArgumentType::Vector32 && arg.flags == Argument::kOptional)
{
std::vector<uint32_t> vectorArgument;
for (uint64_t v : values)
{
vectorArgument.push_back(static_cast<uint32_t>(v));
}

auto optionalArgument = static_cast<chip::Optional<std::vector<uint32_t>> *>(arg.value);
optionalArgument->SetValue(vectorArgument);
}
else
{
return false;
}

return true;
}
Expand Down Expand Up @@ -557,6 +572,19 @@ size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, std::v
return AddArgumentToList(std::move(arg));
}

size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, chip::Optional<std::vector<uint32_t>> * value)
{
Argument arg;
arg.type = ArgumentType::Vector32;
arg.name = name;
arg.value = static_cast<void *>(value);
arg.min = min;
arg.max = max;
arg.flags = Argument::kOptional;

return AddArgumentToList(std::move(arg));
}

size_t Command::AddArgument(const char * name, ComplexArgument * value)
{
Argument arg;
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/commands/common/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class Command

size_t AddArgument(const char * name, int64_t min, uint64_t max, std::vector<uint16_t> * value);
size_t AddArgument(const char * name, int64_t min, uint64_t max, std::vector<uint32_t> * value);
size_t AddArgument(const char * name, int64_t min, uint64_t max, chip::Optional<std::vector<uint32_t>> * value);

template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
size_t AddArgument(const char * name, int64_t min, uint64_t max, T * out, uint8_t flags = 0)
Expand Down

0 comments on commit 949bd86

Please sign in to comment.