Skip to content

Commit

Permalink
[chip-tool] Add structs_with_cluster_name zap helper instead of relyi…
Browse files Browse the repository at this point in the history
…ng on a partial and multiple zcl_structs calls (project-chip#13726)

* [chip-tool] Add structs_with_cluster_name zap helper instead of relying on a partial and multiple zcl_structs calls

* Update generated code
  • Loading branch information
vivien-apple authored Jan 26, 2022
1 parent e45f0a1 commit 799d195
Show file tree
Hide file tree
Showing 7 changed files with 845 additions and 845 deletions.
39 changes: 33 additions & 6 deletions examples/chip-tool/templates/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

// Import helpers from zap core
const zapPath = '../../../third_party/zap/repo/dist/src-electron/';
const templateUtil = require(zapPath + 'generator/template-util.js')
const zclHelper = require(zapPath + 'generator/helper-zcl.js')
const templateUtil = require(zapPath + 'generator/template-util.js');
const zclHelper = require(zapPath + 'generator/helper-zcl.js');
const zclQuery = require(zapPath + 'db/query-zcl.js');

const ChipTypesHelper = require('../../../src/app/zap-templates/common/ChipTypesHelper.js');

Expand Down Expand Up @@ -108,10 +109,36 @@ function utf8StringLength(str)
return new TextEncoder().encode(str).length
}

async function structs_with_cluster_name(options)
{
const packageId = await templateUtil.ensureZclPackageId(this);

const structs = await zclQuery.selectAllStructsWithItems(this.global.db, packageId);

let blocks = [];
for (const s of structs) {
if (s.struct_cluster_count == 0) {
continue;
}

if (s.struct_cluster_count == 1) {
const clusters = await zclQuery.selectStructClusters(this.global.db, s.id);
blocks.push({ id : s.id, name : s.name, clusterName : clusters[0].name });
}

if (s.struct_cluster_count > 1) {
blocks.push({ id : s.id, name : s.name, clusterName : "detail" });
}
}

return templateUtil.collectBlocks(blocks, options, this);
}

//
// Module exports
//
exports.asDelimitedCommand = asDelimitedCommand;
exports.asTypeMinValue = asTypeMinValue;
exports.asTypeMaxValue = asTypeMaxValue;
exports.utf8StringLength = utf8StringLength;
exports.asDelimitedCommand = asDelimitedCommand;
exports.asTypeMinValue = asTypeMinValue;
exports.asTypeMaxValue = asTypeMaxValue;
exports.utf8StringLength = utf8StringLength;
exports.structs_with_cluster_name = structs_with_cluster_name;
30 changes: 18 additions & 12 deletions examples/chip-tool/templates/logging/DataModelLogger-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

#include <commands/clusters/DataModelLogger.h>

{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> log_struct_value ns="detail"}}
{{/if}}
{{/zcl_structs}}
{{#structs_with_cluster_name}}
CHIP_ERROR DataModelLogger::LogValue(const char * label, size_t indent, {{zapTypeToDecodableClusterObjectType name ns=clusterName isArgument=true}} value)
{
DataModelLogger::LogString(label, indent, "{");
{{#zcl_struct_items}}
{
CHIP_ERROR err = LogValue("{{asUpperCamelCase label}}", indent + 1, value.{{asLowerCamelCase label}});
if (err != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "Struct truncated due to invalid value for '{{asUpperCamelCase label}}'");
return err;
}
}
{{/zcl_struct_items}}
DataModelLogger::LogString(indent, "}");

{{#zcl_clusters}}
{{#zcl_structs}}
{{#unless has_more_than_one_cluster}}
{{> log_struct_value ns=parent.name}}
{{/unless}}
{{/zcl_structs}}
{{/zcl_clusters}}
return CHIP_NO_ERROR;
}
{{/structs_with_cluster_name}}

{{#zcl_clusters}}
{{#zcl_events}}
Expand Down
16 changes: 3 additions & 13 deletions examples/chip-tool/templates/logging/DataModelLogger.zapt
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
{{> header}}

{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
static CHIP_ERROR LogValue(const char * label, size_t indent, {{zapTypeToDecodableClusterObjectType name ns="detail" isArgument=true}} value);
{{/if}}
{{/zcl_structs}}

{{#zcl_clusters}}
{{#zcl_structs}}
{{#unless has_more_than_one_cluster}}
static CHIP_ERROR LogValue(const char * label, size_t indent, {{zapTypeToDecodableClusterObjectType name ns=parent.name isArgument=true}} value);
{{/unless}}
{{/zcl_structs}}
{{/zcl_clusters}}
{{#structs_with_cluster_name}}
static CHIP_ERROR LogValue(const char * label, size_t indent, {{zapTypeToDecodableClusterObjectType name ns=clusterName isArgument=true}} value);
{{/structs_with_cluster_name}}

{{#zcl_clusters}}
{{#zcl_events}}
Expand Down
17 changes: 0 additions & 17 deletions examples/chip-tool/templates/partials/log_struct_value.zapt

This file was deleted.

4 changes: 0 additions & 4 deletions examples/chip-tool/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
{
"name": "valueEquals",
"path": "partials/test_cluster_value_equals.zapt"
},
{
"name": "log_struct_value",
"path": "partials/log_struct_value.zapt"
}
],
"templates": [
Expand Down
Loading

0 comments on commit 799d195

Please sign in to comment.