diff --git a/examples/chip-tool/templates/commands.zapt b/examples/chip-tool/templates/commands.zapt index 448edab4023120..2aa635a1cadf68 100644 --- a/examples/chip-tool/templates/commands.zapt +++ b/examples/chip-tool/templates/commands.zapt @@ -4,6 +4,7 @@ #include +#include #include #include #include @@ -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); @@ -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(context); - command->SetCommandExitStatus(CHIP_NO_ERROR); -} - -{{/chip_cluster_responses}} -{{/chip_client_clusters}} - {{#chip_client_clusters}} {{#chip_server_cluster_attributes}} {{#if isList}} @@ -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({{>field}}.size()), {{>field}}.data()); + {{else}} + ChipLogProgress(Zcl, " {{label}}: {{asPrintFormat type}}", {{>field}}); + {{/if}} + {{/chip_cluster_response_arguments}} + + ModelCommand * command = static_cast(context); + command->SetCommandExitStatus(CHIP_NO_ERROR); +}; + +{{/chip_cluster_responses}} +{{/chip_client_clusters}} + {{> clusters_header}} {{#chip_client_clusters}} @@ -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 *>({{/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 * onSuccessCallback = new chip::Callback::Callback(OnDefaultSuccessResponse, this); - {{/if}} - chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(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}}