Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update chip-tool to use the InvokeCommand API #10611

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 62 additions & 34 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cstdint>

#include <app-common/zap-generated/cluster-objects.h>
#include <commands/clusters/ModelCommand.h>
#include <lib/core/CHIPSafeCasts.h>
#include <lib/support/BytesToHex.h>
Expand All @@ -26,6 +27,16 @@ static void OnDefaultFailureResponse(void * context, uint8_t status)
command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
}

static void OnDefaultSuccess(void * context, const chip::app::DataModel::NullObjectType & data)
{
OnDefaultSuccessResponse(context);
};

static void OnDefaultFailure(void * context, EmberAfStatus status)
{
OnDefaultFailureResponse(context, status);
};

static void OnBooleanAttributeResponse(void * context, bool value)
{
ChipLogProgress(chipTool, "Boolean attribute Response: %d", value);
Expand Down Expand Up @@ -119,19 +130,6 @@ static void OnCharStringAttributeResponse(void * context, const chip::ByteSpan v
command->SetCommandExitStatus(CHIP_NO_ERROR);
}

{{#chip_client_clusters}}
{{#chip_cluster_responses}}
static void On{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}(void * context{{#chip_cluster_response_arguments}}, {{asUnderlyingZclType type}} {{asSymbol label}}{{/chip_cluster_response_arguments}})
{
ChipLogProgress(chipTool, "{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}");

ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}

{{/chip_cluster_responses}}
{{/chip_client_clusters}}

{{#chip_client_clusters}}
{{#chip_server_cluster_attributes}}
{{#if isList}}
Expand Down Expand Up @@ -172,6 +170,31 @@ static void On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttri
{{/chip_server_cluster_attributes}}
{{/chip_client_clusters}}

{{#chip_client_clusters}}
{{#chip_cluster_responses}}
static void On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}Success(void * context, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & data)
{
ChipLogProgress(Zcl, "Received {{asUpperCamelCase name}}:");
{{#chip_cluster_response_arguments}}
{{~#*inline "field"}}data.{{asLowerCamelCase label}}{{/inline~}}
{{#if isArray}}
ChipLogProgress(Zcl, " {{label}}: Array printing is not implemented yet.");
{{else if (isOctetString type)}}
ChipLogProgress(Zcl, " {{label}}: %zu", {{>field}}.size());
{{else if (isCharString type)}}
ChipLogProgress(Zcl, " {{label}}: %.*s", static_cast<int>({{>field}}.size()), {{>field}}.data());
{{else}}
ChipLogProgress(Zcl, " {{label}}: {{asPrintFormat type}}", {{>field}});
{{/if}}
{{/chip_cluster_response_arguments}}

ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
};

{{/chip_cluster_responses}}
{{/chip_client_clusters}}

{{> clusters_header}}

{{#chip_client_clusters}}
Expand All @@ -186,40 +209,45 @@ class {{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}: public ModelCom
public:
{{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}(): ModelCommand("{{asDelimitedCommand name}}")
{
{{#chip_cluster_command_arguments_with_structs_expanded}}
{{#if (isString type)}}
AddArgument("{{asUpperCamelCase label}}", &m{{asUpperCamelCase label}});
{{#chip_cluster_command_arguments}}
{{~#*inline "field"}}mRequest.{{asLowerCamelCase label}}{{/inline~}}
{{#if isArray}}
// {{label}} Array parsing is not supported yet
{{else if isStruct}}
// {{label}} Struct parsing is not supported yet
{{else if (isString type)}}
AddArgument("{{asUpperCamelCase label}}", &{{>field}});
{{else}}
AddArgument("{{asUpperCamelCase label}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &m{{asUpperCamelCase label}});
AddArgument("{{asUpperCamelCase label}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}},
{{~#if ./isEnum}}reinterpret_cast<std::underlying_type_t<decltype({{>field}})> *>(&{{>field}})
{{else ./isBitmap}}reinterpret_cast<std::underlying_type_t<chip::app::Clusters::{{asUpperCamelCase parent.clusterName}}::{{asUpperCamelCase type}}> *>(&{{>field}})
{{else}}&{{>field}}
{{/if~}}
);
{{/if}}
{{/chip_cluster_command_arguments_with_structs_expanded}}
{{/chip_cluster_command_arguments}}
ModelCommand::AddArguments();
}
~{{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}()
{
delete onSuccessCallback;
delete onFailureCallback;
}

CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command ({{asHex code 2}}) on endpoint %" PRIu8, endpointId);
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 8}}) command ({{asHex code 8}}) on endpoint %" PRIu8, endpointId);

{{#chip_cluster_command_non_expanded_arguments}}
{{#if isArray}}
{{! TODO Implement complex types parsing in order to properly set the request parameters }}
{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}} {{asLowerCamelCase label}};
mRequest.{{asLowerCamelCase label}} = chip::Span<{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}>(&{{asLowerCamelCase label}}, 1);
{{/if}}
{{/chip_cluster_command_non_expanded_arguments}}

chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
return cluster.{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(){{#chip_cluster_command_arguments_with_structs_expanded}}, m{{asUpperCamelCase label}}{{/chip_cluster_command_arguments_with_structs_expanded}});
return cluster.InvokeCommand(mRequest, this, {{#if hasSpecificResponse}}On{{asUpperCamelCase parent.name}}{{asUpperCamelCase response.name}}Success{{else}}OnDefaultSuccess{{/if}}, OnDefaultFailure);
}

private:
{{#if hasSpecificResponse}}
chip::Callback::Callback<{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}Callback> * onSuccessCallback = new chip::Callback::Callback<{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}Callback>(On{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase responseName}}, this);
{{else}}
chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
{{/if}}
chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
{{#chip_cluster_command_arguments_with_structs_expanded}}
{{#if (isCharString type)}}chip::ByteSpan{{else}}{{chipType}}{{/if}} m{{asUpperCamelCase label}};
{{/chip_cluster_command_arguments_with_structs_expanded}}
chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type mRequest;
};

{{/chip_cluster_commands}}
Expand Down
6 changes: 4 additions & 2 deletions src/app/zap-templates/common/ClustersHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,14 @@ function handleBasic(item, [ atomics, enums, bitmaps, structs ])

const enumItem = getEnum(enums, itemType);
if (enumItem) {
itemType = enumItem.type;
item.isEnum = true;
itemType = enumItem.type;
}

const bitmap = getBitmap(bitmaps, itemType);
if (bitmap) {
itemType = bitmap.type;
item.isBitmap = true;
itemType = bitmap.type;
}

const atomic = getAtomic(atomics, itemType);
Expand Down
Loading