Skip to content

Commit

Permalink
Update chip-tool to use the InvokeCommand API
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Oct 18, 2021
1 parent 3fa9d49 commit 9007875
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 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,39 @@ 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}}
{{#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}}", &mRequest.{{asLowerCamelCase label}});
{{else}}
AddArgument("{{asUpperCamelCase label}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, &m{{asUpperCamelCase label}});
AddArgument("{{asUpperCamelCase label}}", {{asTypeMinValue type}}, {{asTypeMaxValue type}}, {{#if_is_enum type}}reinterpret_cast<std::underlying_type_t<decltype(mRequest.{{asLowerCamelCase label}})> *>({{/if_is_enum}}&mRequest.{{asLowerCamelCase label}}{{#if_is_enum type}}){{/if_is_enum}});
{{/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

0 comments on commit 9007875

Please sign in to comment.