Skip to content

Commit

Permalink
Move the isArray logic into the decodable/encodable type helper. (#10822
Browse files Browse the repository at this point in the history
)

* Move the isArray logic into the decodable/encodable type helper.

This way instead of ugly templating to wrap in List/DecodableList it
just works automatically.

* Move treatment of args as const ref into the helper as well, so we
don't convert a bunch of integer args to const refs in ways that the
rest of the system does not expect.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Jan 18, 2022
1 parent 2b060cc commit 24a1be0
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 130 deletions.
5 changes: 2 additions & 3 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void OnCharStringAttributeResponse(void * context, const chip::CharSpan v
{{#chip_client_clusters}}
{{#chip_server_cluster_attributes}}
{{#if isList}}
static void On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeResponse(void * context, const chip::app::DataModel::DecodableList<{{zapTypeToDecodableClusterObjectType type ns=parent.name}}> & list)
static void On{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeResponse(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} list)
{
ModelCommand * command = static_cast<ModelCommand *>(context);

Expand Down Expand Up @@ -254,8 +254,7 @@ public:
{{#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);
mRequest.{{asLowerCamelCase label}} = {{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}();
{{/if}}
{{/chip_cluster_command_non_expanded_arguments}}

Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class {{filename}}: public TestCommand
{{#*inline "successCallback"}}mOnSuccessCallback_{{index}}{{/inline}}
{{#*inline "failureResponse"}}OnFailureCallback_{{index}}{{/inline}}
{{#*inline "successResponse"}}OnSuccessCallback_{{index}}{{/inline}}
{{#*inline "successArguments"}}void * context{{#chip_tests_item_response_parameters}}, {{#if isList}}const chip::app::DataModel::DecodableList<{{/if}}{{zapTypeToDecodableClusterObjectType type ns=parent.cluster}}{{#if isList}}> &{{/if}} {{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}}{{/inline}}
{{#*inline "successArguments"}}void * context{{#chip_tests_item_response_parameters}}, {{zapTypeToDecodableClusterObjectType type ns=parent.cluster isArgument=true}} {{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}}{{/inline}}
{{#*inline "failureArguments"}}void * context, uint8_t status{{/inline}}

{{#chip_tests_items}}
Expand Down Expand Up @@ -102,7 +102,7 @@ class {{filename}}: public TestCommand
{{#*inline "failureResponse"}}OnFailureResponse_{{index}}{{/inline}}
{{#*inline "successResponse"}}OnSuccessResponse_{{index}}{{/inline}}
{{#*inline "failureArguments"}}uint8_t status{{/inline}}
{{#*inline "successArguments"}}{{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}{{#if isList}}const chip::app::DataModel::DecodableList<{{/if}}{{#if isArray}}const chip::app::DataModel::DecodableList<{{/if}}{{zapTypeToDecodableClusterObjectType type ns=parent.cluster}}{{#if isList}}>& {{/if}}{{#if isArray}}>& {{/if}} {{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}}{{/inline}}
{{#*inline "successArguments"}}{{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}{{zapTypeToDecodableClusterObjectType type ns=parent.cluster isArgument=true}} {{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}}{{/inline}}

CHIP_ERROR {{>testCommand}}()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{#if isArray}}

{{zapTypeToEncodableClusterObjectType type ns=ns}} {{asLowerCamelCase label}}List[{{definedValue.length}}];
{{! forceNotList=true because we really want the type of a single item here }}
{{zapTypeToEncodableClusterObjectType type ns=ns forceNotList=true}} {{asLowerCamelCase label}}List[{{definedValue.length}}];
{{#each definedValue}}
{{>commandValue ns=../ns container=(concat (asLowerCamelCase ../label) "List[" @index "]") definedValue=. type=../type ignore=true}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ app::CHIPDeviceCallbacksMgr & gCallbacks = app::CHIPDeviceCallbacksMgr::GetInsta
{{#if isList}}
void {{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}ListAttributeFilter(TLV::TLVReader * tlvData, Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback)
{
DecodableList<{{zapTypeToDecodableClusterObjectType type ns=parent.name}}> list;
{{zapTypeToDecodableClusterObjectType type ns=parent.name}} list;
CHIP_ERROR err = Decode(*tlvData, list);
if (err != CHIP_NO_ERROR)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef void (*{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}C
{{#chip_server_cluster_attributes}}
{{#if isList}}
void {{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}ListAttributeFilter(chip::TLV::TLVReader * data, chip::Callback::Cancelable * onSuccessCallback, chip::Callback::Cancelable * onFailureCallback);
typedef void (*{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeCallback)(void * context, const chip::app::DataModel::DecodableList<{{zapTypeToDecodableClusterObjectType type ns=parent.name}}> & data);
typedef void (*{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeCallback)(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} data);
{{/if}}
{{/chip_server_cluster_attributes}}
{{/chip_client_clusters}}
16 changes: 8 additions & 8 deletions src/app/zap-templates/templates/app/cluster-objects.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace {{asUpperCamelCase name}} {
struct Type {
public:
{{#zcl_struct_items}}
{{#if isArray}}DataModel::List<{{/if}}{{zapTypeToEncodableClusterObjectType type}}{{#if isArray}}>{{/if}} {{asLowerCamelCase label}};
{{zapTypeToEncodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_struct_items}}

CHIP_ERROR Encode(TLV::TLVWriter &writer, TLV::Tag tag) const;
Expand All @@ -73,7 +73,7 @@ namespace {{asUpperCamelCase name}} {
struct DecodableType {
public:
{{#zcl_struct_items}}
{{#if isArray}}DataModel::DecodableList<{{/if}}{{zapTypeToDecodableClusterObjectType type}}{{#if isArray}}>{{/if}} {{asLowerCamelCase label}};
{{zapTypeToDecodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_struct_items}}
CHIP_ERROR Decode(TLV::TLVReader &reader);
};
Expand Down Expand Up @@ -106,7 +106,7 @@ public:
static constexpr ClusterId GetClusterId() { return {{asUpperCamelCase parent.name}}::Id; }

{{#zcl_command_arguments}}
{{#if isArray}}DataModel::List<{{/if}}{{zapTypeToEncodableClusterObjectType type}}{{#if isArray}}>{{/if}} {{asLowerCamelCase label}};
{{zapTypeToEncodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_command_arguments}}

CHIP_ERROR Encode(TLV::TLVWriter &writer, TLV::Tag tag) const;
Expand All @@ -118,7 +118,7 @@ public:
static constexpr ClusterId GetClusterId() { return {{asUpperCamelCase parent.name}}::Id; }

{{#zcl_command_arguments}}
{{#if isArray}}DataModel::DecodableList<{{/if}}{{zapTypeToDecodableClusterObjectType type}}{{#if isArray}}>{{/if}} {{asLowerCamelCase label}};
{{zapTypeToDecodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_command_arguments}}
CHIP_ERROR Decode(TLV::TLVReader &reader);
};
Expand All @@ -137,8 +137,8 @@ namespace Attributes {
namespace {{asUpperCamelCase label}} {
struct TypeInfo {
{{#if entryType}}
using Type = DataModel::List<{{zapTypeToEncodableClusterObjectType entryType}}>;
using DecodableType = DataModel::DecodableList<{{zapTypeToDecodableClusterObjectType entryType}}>;
using Type = {{zapTypeToEncodableClusterObjectType entryType}};
using DecodableType = {{zapTypeToDecodableClusterObjectType entryType}};
{{else}}
using Type = {{zapTypeToEncodableClusterObjectType type}};
using DecodableType = {{zapTypeToDecodableClusterObjectType type}};
Expand Down Expand Up @@ -176,7 +176,7 @@ public:
static constexpr ClusterId GetClusterId() { return {{asUpperCamelCase parent.name}}::Id; }

{{#zcl_event_fields}}
{{#if isArray}}DataModel::List<{{/if}}{{zapTypeToEncodableClusterObjectType type}}{{#if isArray}}>{{/if}} {{asLowerCamelCase name}};
{{zapTypeToEncodableClusterObjectType type}} {{asLowerCamelCase name}};
{{/zcl_event_fields}}

CHIP_ERROR Encode(TLV::TLVWriter &writer, TLV::Tag tag) const;
Expand All @@ -189,7 +189,7 @@ public:
static constexpr ClusterId GetClusterId() { return {{asUpperCamelCase parent.name}}::Id; }

{{#zcl_event_fields}}
{{#if isArray}}DataModel::DecodableList<{{/if}}{{zapTypeToDecodableClusterObjectType type}}{{#if isArray}}>{{/if}} {{asLowerCamelCase name}};
{{zapTypeToDecodableClusterObjectType type}} {{asLowerCamelCase name}};
{{/zcl_event_fields}}

CHIP_ERROR Decode(TLV::TLVReader &reader);
Expand Down
15 changes: 14 additions & 1 deletion src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ function asMEI(prefix, suffix)
*/
async function zapTypeToClusterObjectType(type, isDecodable, options)
{
let passByReference = false;
async function fn(pkgId)
{
const ns = options.hash.ns ? ('chip::app::Clusters::' + asUpperCamelCase(options.hash.ns) + '::') : '';
Expand All @@ -364,13 +365,25 @@ async function zapTypeToClusterObjectType(type, isDecodable, options)
}

if (await typeChecker('isStruct')) {
passByReference = true;
return ns + 'Structs::' + type + '::' + (isDecodable ? 'DecodableType' : 'Type');
}

return zclHelper.asUnderlyingZclType.call({ global : this.global }, type, options);
}

const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this));
let promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this));
if ((this.isList || this.isArray || this.entryType) && !options.hash.forceNotList) {
passByReference = true;
let listType = isDecodable ? "DecodableList" : "List";
// If we did not have a namespace provided, we can assume we're inside
// chip::app.
let listNamespace = options.hash.ns ? "chip::app::" : ""
promise = promise.then(typeStr => `${listNamespace}DataModel::${listType}<${typeStr}>`);
}
if (options.hash.isArgument && passByReference) {
promise = promise.then(typeStr => `const ${typeStr} &`);
}
return templateUtil.templatePromise(this.global, promise)
}

Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/templates/CHIPClusters-JNI.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCall
}
}

static void CallbackFn(void * context, const app::DataModel::DecodableList<{{zapTypeToDecodableClusterObjectType type ns=parent.name}}> & list)
static void CallbackFn(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} list)
{
chip::DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down
Loading

0 comments on commit 24a1be0

Please sign in to comment.