Skip to content

Commit

Permalink
Chip-tool group command support
Browse files Browse the repository at this point in the history
  • Loading branch information
jepenven-silabs committed Mar 1, 2022
1 parent 5f40c45 commit 7903051
Show file tree
Hide file tree
Showing 8 changed files with 3,322 additions and 15 deletions.
35 changes: 35 additions & 0 deletions examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
return ClusterCommand::SendCommand(device, endpointId, mClusterId, mCommandId, mPayload);
}

CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::NodeId senderNodeId) override
{
return ClusterCommand::SendGroupCommand(groupId, fabricIndex, senderNodeId, mClusterId, mCommandId, mPayload);
}

/////////// CommandSender Callback Interface /////////
virtual void OnResponse(chip::app::CommandSender * client, const chip::app::ConcreteCommandPath & path,
const chip::app::StatusIB & status, chip::TLV::TLVReader * data) override
Expand Down Expand Up @@ -107,6 +112,36 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
return CHIP_NO_ERROR;
}

template <class T>
CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::NodeId senderNodeId,
chip::ClusterId clusterId, chip::CommandId commandId, const T & value)
{
chip::app::CommandPathParams commandPath = { 0 /* endpoint */, groupId, clusterId, commandId,
(chip::app::CommandPathFlags::kGroupIdValid) };

chip::Messaging::ExchangeManager * exchangeManager = chip::app::InteractionModelEngine::GetInstance()->GetExchangeManager();

ChipLogDetail(chipTool, "Sending command to Group %u, on Fabric %x, for cluster %u with commandId %u", groupId, fabricIndex,
clusterId, commandId);

auto commandSender = chip::Platform::MakeUnique<chip::app::CommandSender>(this, exchangeManager, false);
VerifyOrReturnError(commandSender != nullptr, CHIP_ERROR_NO_MEMORY);
ReturnErrorOnFailure(commandSender->AddRequestDataNoTimedCheck(commandPath, value, chip::NullOptional));

chip::Optional<chip::SessionHandle> session =
exchangeManager->GetSessionManager()->CreateGroupSession(groupId, fabricIndex, senderNodeId);
if (!session.HasValue())
{
return CHIP_ERROR_NO_MEMORY;
}
ReturnErrorOnFailure(commandSender->SendGroupCommandRequest(session.Value()));

commandSender.release();
exchangeManager->GetSessionManager()->RemoveGroupSession(session.Value()->AsGroupSession());

return CHIP_NO_ERROR;
}

private:
chip::ClusterId mClusterId;
chip::CommandId mCommandId;
Expand Down
10 changes: 10 additions & 0 deletions examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ using namespace ::chip;

CHIP_ERROR ModelCommand::RunCommand()
{

if (IsGroupId(mNodeId))
{
ChipLogProgress(chipTool, "Sending command to group 0x%" PRIx16 " on Fabric Index 0x%" PRIx16, GroupIdFromNodeId(mNodeId),
static_cast<FabricIndex>(mEndPointId));

return SendGroupCommand(GroupIdFromNodeId(mNodeId), static_cast<FabricIndex>(mEndPointId),
CurrentCommissioner().GetNodeId());
}

ChipLogProgress(chipTool, "Sending command to node 0x%" PRIx64, mNodeId);
return CurrentCommissioner().GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
}
Expand Down
9 changes: 7 additions & 2 deletions examples/chip-tool/commands/clusters/ModelCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class ModelCommand : public CHIPCommand

void AddArguments()
{
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);
AddArgument("endpoint-id", 0, UINT16_MAX, &mEndPointId);
AddArgument("node-id/group-id", 0, UINT64_MAX, &mNodeId);
AddArgument("endpoint-id/FabricIndex", 0, UINT16_MAX, &mEndPointId);
}

/////////// CHIPCommand Interface /////////
Expand All @@ -44,6 +44,11 @@ class ModelCommand : public CHIPCommand

virtual CHIP_ERROR SendCommand(ChipDevice * device, chip::EndpointId endPointId) = 0;

virtual CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::NodeId senderNodeId)
{
return CHIP_ERROR_BAD_REQUEST;
};

private:
chip::NodeId mNodeId;
chip::EndpointId mEndPointId;
Expand Down
37 changes: 37 additions & 0 deletions examples/chip-tool/commands/clusters/WriteAttributeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class WriteAttribute : public ModelCommand, public chip::app::WriteClient::Callb
return WriteAttribute::SendCommand(device, endpointId, mClusterId, mAttributeId, mAttributeValue);
}

CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::NodeId senderNodeId) override
{
return WriteAttribute::SendGroupCommand(groupId, fabricIndex, senderNodeId, mClusterId, mAttributeId, mAttributeValue);
}

/////////// WriteClient Callback Interface /////////
void OnResponse(const chip::app::WriteClient * client, const chip::app::ConcreteDataAttributePath & path,
chip::app::StatusIB status) override
Expand Down Expand Up @@ -105,6 +110,38 @@ class WriteAttribute : public ModelCommand, public chip::app::WriteClient::Callb
return mWriteClient->SendWriteRequest(device->GetSecureSession().Value());
}

template <class T>
CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::NodeId senderNodeId,
chip::ClusterId clusterId, chip::AttributeId attributeId, const T & value)
{

chip::app::AttributePathParams attributePathParams;
attributePathParams.mClusterId = clusterId;
attributePathParams.mAttributeId = attributeId;

chip::Messaging::ExchangeManager * exchangeManager = chip::app::InteractionModelEngine::GetInstance()->GetExchangeManager();

ChipLogDetail(chipTool, "Sending Write Attribute to Group %u, on Fabric %x, for cluster %u with attributeId %u", groupId,
fabricIndex, clusterId, attributeId);

auto writeClient = chip::Platform::MakeUnique<chip::app::WriteClient>(exchangeManager, this, mTimedInteractionTimeoutMs);
VerifyOrReturnError(writeClient != nullptr, CHIP_ERROR_NO_MEMORY);
ReturnErrorOnFailure(writeClient->EncodeAttribute(attributePathParams, value, mDataVersion));

chip::Optional<chip::SessionHandle> session =
exchangeManager->GetSessionManager()->CreateGroupSession(groupId, fabricIndex, senderNodeId);
if (!session.HasValue())
{
return CHIP_ERROR_NO_MEMORY;
}
ReturnErrorOnFailure(writeClient->SendWriteRequest(session.Value()));

writeClient.release();
exchangeManager->GetSessionManager()->RemoveGroupSession(session.Value()->AsGroupSession());

return CHIP_NO_ERROR;
}

private:
chip::ClusterId mClusterId;
chip::AttributeId mAttributeId;
Expand Down
12 changes: 12 additions & 0 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public:
return ClusterCommand::SendCommand(device, endpointId, {{asHex parent.code 8}}, {{asHex code 8}}, mRequest);
}

CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::NodeId senderNodeId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 8}}) command ({{asHex code 8}}) on Group %" PRIu16, groupId);

return ClusterCommand::SendGroupCommand(groupId, fabricIndex, senderNodeId, {{asHex parent.code 8}}, {{asHex code 8}}, mRequest);
}

private:
chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type mRequest;
{{#zcl_command_arguments}}
Expand Down Expand Up @@ -81,6 +88,11 @@ public:
return WriteAttribute::SendCommand(device, endpointId, {{asHex parent.code 8}}, {{asHex code 8}}, mValue);
}

CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex, chip::NodeId senderNodeId) override
{
return WriteAttribute::SendGroupCommand(groupId, fabricIndex, senderNodeId, {{asHex parent.code 8}}, {{asHex code 8}}, mValue);
}

private:
{{zapTypeToEncodableClusterObjectType type ns=parent.name forceNotOptional=true}} mValue;
{{#if_chip_complex}}
Expand Down
4 changes: 2 additions & 2 deletions src/transport/GroupPeerMessageCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ CHIP_ERROR GroupOutgoingCounters::Init(chip::PersistentStorageDelegate * storage
uint32_t temp;
CHIP_ERROR err;
err = mStorage->SyncGetKeyValue(key.GroupControlCounter(), &temp, size);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND || err == CHIP_ERROR_KEY_NOT_FOUND)
{
// might be the first time we retrieve the value
// TODO handle this case
Expand All @@ -259,7 +259,7 @@ CHIP_ERROR GroupOutgoingCounters::Init(chip::PersistentStorageDelegate * storage
}

err = mStorage->SyncGetKeyValue(key.GroupDataCounter(), &temp, size);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND || err == CHIP_ERROR_KEY_NOT_FOUND)
{
// might be the first time we retrieve the value
// TODO handle this case
Expand Down
2 changes: 1 addition & 1 deletion src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ CHIP_ERROR SessionManager::Init(System::Layer * systemLayer, TransportMgrBase *
(void) mGlobalEncryptedMessageCounter.Init();
mGlobalUnencryptedMessageCounter.Init();

mGroupClientCounter.Init(storageDelegate);
ReturnErrorOnFailure(mGroupClientCounter.Init(storageDelegate));

ScheduleExpiryTimer();

Expand Down
Loading

0 comments on commit 7903051

Please sign in to comment.