Skip to content

Commit

Permalink
Switch to having only one struct named LabelStruct. (#13380)
Browse files Browse the repository at this point in the history
This is shared across the fixed label and user label clusters.

The struct definition is in the "detail" namespace, with alias
namespaces in the two clusters that use it.  Consumers are generally
expected to use the per-cluster aliases.
  • Loading branch information
bzbarsky-apple authored Jan 10, 2022
1 parent 4a1f481 commit 7d2771f
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 295 deletions.
33 changes: 17 additions & 16 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@
// generated ones, so are placed here.
namespace {

{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
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}}
CHIP_ERROR LogValue(const char * label, size_t indent, {{zapTypeToDecodableClusterObjectType name ns=parent.name isArgument=true}} value);
{{/unless}}
{{/zcl_structs}}
{{/zcl_clusters}}

Expand Down Expand Up @@ -145,24 +153,17 @@ CHIP_ERROR LogValue(const char * label, size_t indent, const chip::Optional<T> &
// be logging.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> log_struct_value ns="detail"}}
{{/if}}
{{/zcl_structs}}

{{#zcl_clusters}}
{{#zcl_structs}}
CHIP_ERROR LogValue(const char * label, size_t indent, {{zapTypeToDecodableClusterObjectType name ns=parent.name isArgument=true}} value)
{
ChipLogProgress(chipTool, "%s%s: {", IndentStr(indent).c_str(), label);
{{#zcl_struct_items}}
{
CHIP_ERROR err = LogValue("{{asUpperCamelCase label}}", indent + 1, value.{{asLowerCamelCase label}});
if (err != CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "%sStruct truncated due to invalid value for '{{asUpperCamelCase label}}'", IndentStr(indent + 1).c_str());
return err;
}
}
{{/zcl_struct_items}}
ChipLogProgress(chipTool, "%s}", IndentStr(indent).c_str());
return CHIP_NO_ERROR;
}
{{#unless has_more_than_one_cluster}}
{{> log_struct_value ns=parent.name}}
{{/unless}}
{{/zcl_structs}}
{{/zcl_clusters}}
#pragma GCC diagnostic pop
Expand Down
16 changes: 16 additions & 0 deletions examples/chip-tool/templates/partials/log_struct_value.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CHIP_ERROR LogValue(const char * label, size_t indent, {{zapTypeToDecodableClusterObjectType name ns=ns isArgument=true}} value)
{
ChipLogProgress(chipTool, "%s%s: {", IndentStr(indent).c_str(), label);
{{#zcl_struct_items}}
{
CHIP_ERROR err = LogValue("{{asUpperCamelCase label}}", indent + 1, value.{{asLowerCamelCase label}});
if (err != CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "%sStruct truncated due to invalid value for '{{asUpperCamelCase label}}'", IndentStr(indent + 1).c_str());
return err;
}
}
{{/zcl_struct_items}}
ChipLogProgress(chipTool, "%s}", IndentStr(indent).c_str());
return CHIP_NO_ERROR;
}
4 changes: 4 additions & 0 deletions examples/chip-tool/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
{
"name": "valueEquals",
"path": "partials/test_cluster_value_equals.zapt"
},
{
"name": "log_struct_value",
"path": "partials/log_struct_value.zapt"
}
],
"templates": [
Expand Down
4 changes: 4 additions & 0 deletions src/app/common/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
{
"name": "header",
"path": "../../zap-templates/partials/header.zapt"
},
{
"name": "cluster_objects_struct",
"path": "../../zap-templates/partials/cluster-objects-struct.zapt"
}
],
"templates": [
Expand Down
77 changes: 77 additions & 0 deletions src/app/zap-templates/partials/cluster-objects-struct.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{{#if header}}
namespace {{asUpperCamelCase name}} {
enum class Fields {
{{#zcl_struct_items}}
k{{asUpperCamelCase label}} = {{fieldIdentifier}},
{{/zcl_struct_items}}
};

struct Type {
public:
{{#zcl_struct_items}}
{{zapTypeToEncodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_struct_items}}

CHIP_ERROR Encode(TLV::TLVWriter &writer, TLV::Tag tag) const;
{{#unless struct_contains_array}}
CHIP_ERROR Decode(TLV::TLVReader &reader);
{{/unless}}
{{#if struct_is_fabric_scoped}}
bool MatchesFabricIndex(FabricIndex fabricIndex_) const {
return {{ asLowerCamelCase struct_fabric_idx_field }} == fabricIndex_;
}
{{/if}}
};

{{#if struct_contains_array}}
struct DecodableType {
public:
{{#zcl_struct_items}}
{{zapTypeToDecodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_struct_items}}
CHIP_ERROR Decode(TLV::TLVReader &reader);
};
{{else}}
using DecodableType = Type;
{{/if}}

} // namespace {{asUpperCamelCase name}}
{{else}}
namespace {{asUpperCamelCase name}} {
CHIP_ERROR Type::Encode(TLV::TLVWriter &writer, TLV::Tag tag) const{
TLV::TLVType outer;
ReturnErrorOnFailure(writer.StartContainer(tag, TLV::kTLVType_Structure, outer));
{{#zcl_struct_items}}
ReturnErrorOnFailure(DataModel::Encode(writer, TLV::ContextTag(to_underlying(Fields::k{{asUpperCamelCase label}})), {{asLowerCamelCase label}}));
{{/zcl_struct_items}}
ReturnErrorOnFailure(writer.EndContainer(outer));
return CHIP_NO_ERROR;
}

CHIP_ERROR DecodableType::Decode(TLV::TLVReader &reader) {
CHIP_ERROR err = CHIP_NO_ERROR;
TLV::TLVType outer;
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
err = reader.EnterContainer(outer);
ReturnErrorOnFailure(err);
while ((err = reader.Next()) == CHIP_NO_ERROR) {
VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
switch (TLV::TagNumFromTag(reader.GetTag()))
{
{{#zcl_struct_items}}
case to_underlying(Fields::k{{asUpperCamelCase label}}):
ReturnErrorOnFailure(DataModel::Decode(reader, {{asLowerCamelCase label}}));
break;
{{/zcl_struct_items}}
default:
break;
}
}

VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
ReturnErrorOnFailure(reader.ExitContainer(outer));
return CHIP_NO_ERROR;
}

} // namespace {{asUpperCamelCase name}}
{{/if}}
52 changes: 15 additions & 37 deletions src/app/zap-templates/templates/app/cluster-objects-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,27 @@
namespace chip {
namespace app {
namespace Clusters {

namespace detail {
// Structs shared across multiple clusters.
namespace Structs {
{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> cluster_objects_struct header=false}}
{{/if}}
{{/zcl_structs}}
} // namespace Structs
} // namespace detail

{{#zcl_clusters}}
namespace {{asUpperCamelCase name}} {
{{#zcl_structs}}
{{#first}}
namespace Structs {
{{/first}}
namespace {{asType label}} {
CHIP_ERROR Type::Encode(TLV::TLVWriter &writer, TLV::Tag tag) const{
TLV::TLVType outer;
ReturnErrorOnFailure(writer.StartContainer(tag, TLV::kTLVType_Structure, outer));
{{#zcl_struct_items}}
ReturnErrorOnFailure(DataModel::Encode(writer, TLV::ContextTag(to_underlying(Fields::k{{asUpperCamelCase label}})), {{asLowerCamelCase label}}));
{{/zcl_struct_items}}
ReturnErrorOnFailure(writer.EndContainer(outer));
return CHIP_NO_ERROR;
}

CHIP_ERROR DecodableType::Decode(TLV::TLVReader &reader) {
CHIP_ERROR err = CHIP_NO_ERROR;
TLV::TLVType outer;
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
err = reader.EnterContainer(outer);
ReturnErrorOnFailure(err);
while ((err = reader.Next()) == CHIP_NO_ERROR) {
VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
switch (TLV::TagNumFromTag(reader.GetTag()))
{
{{#zcl_struct_items}}
case to_underlying(Fields::k{{asUpperCamelCase label}}):
ReturnErrorOnFailure(DataModel::Decode(reader, {{asLowerCamelCase label}}));
break;
{{/zcl_struct_items}}
default:
break;
}
}

VerifyOrReturnError(err == CHIP_END_OF_TLV, err);
ReturnErrorOnFailure(reader.ExitContainer(outer));
return CHIP_NO_ERROR;
}

} // namespace {{asType label}}
{{#unless has_more_than_one_cluster}}
{{> cluster_objects_struct header=false}}
{{/unless}}
{{#last}}
} // namespace Structs
{{/last}}
Expand Down
53 changes: 16 additions & 37 deletions src/app/zap-templates/templates/app/cluster-objects.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ namespace chip {
namespace app {
namespace Clusters {

namespace detail {
// Structs shared across multiple clusters.
namespace Structs {
{{#zcl_structs}}
{{#if has_more_than_one_cluster}}
{{> cluster_objects_struct header=true}}
{{/if}}
{{/zcl_structs}}
} // namespace Structs
} // namespace detail

{{#zcl_clusters}}
namespace {{asUpperCamelCase name}} {
{{#zcl_enums}}
Expand Down Expand Up @@ -55,43 +66,11 @@ k{{asUpperCamelCase label}} = {{asHex mask}},
{{#first}}
namespace Structs {
{{/first}}
namespace {{asUpperCamelCase name}} {
enum class Fields {
{{#zcl_struct_items}}
k{{asUpperCamelCase label}} = {{fieldIdentifier}},
{{/zcl_struct_items}}
};

struct Type {
public:
{{#zcl_struct_items}}
{{zapTypeToEncodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_struct_items}}

CHIP_ERROR Encode(TLV::TLVWriter &writer, TLV::Tag tag) const;
{{#unless struct_contains_array}}
CHIP_ERROR Decode(TLV::TLVReader &reader);
{{/unless}}
{{#if struct_is_fabric_scoped}}
bool MatchesFabricIndex(FabricIndex fabricIndex_) const {
return {{ asLowerCamelCase struct_fabric_idx_field }} == fabricIndex_;
}
{{/if}}
};

{{#if struct_contains_array}}
struct DecodableType {
public:
{{#zcl_struct_items}}
{{zapTypeToDecodableClusterObjectType type}} {{asLowerCamelCase label}};
{{/zcl_struct_items}}
CHIP_ERROR Decode(TLV::TLVReader &reader);
};
{{else}}
using DecodableType = Type;
{{/if}}

} // namespace {{asUpperCamelCase name}}
{{#if has_more_than_one_cluster}}
namespace {{asUpperCamelCase name}} = Clusters::detail::Structs::{{asUpperCamelCase name}};
{{else}}
{{> cluster_objects_struct header=true}}
{{/if}}
{{#last}}
} // namespace Structs
{{/last}}
Expand Down
12 changes: 11 additions & 1 deletion src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ function asMEI(prefix, suffix)
return cHelper.asHex((prefix << 16) + suffix, 8);
}

// Not to be exported.
function nsValueToNamespace(ns)
{
if (ns == "detail") {
return ns;
}

return asUpperCamelCase(ns);
}

/*
* @brief
*
Expand All @@ -372,7 +382,7 @@ 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) + '::') : '';
const ns = options.hash.ns ? ('chip::app::Clusters::' + nsValueToNamespace(options.hash.ns) + '::') : '';
const typeChecker = async (method) => zclHelper[method](this.global.db, type, pkgId).then(zclType => zclType != 'unknown');

if (await typeChecker('isEnum')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.

<struct name="LabelStruct">
<cluster code="0x0040"/>
<cluster code="0x0041"/>
<item name="label" type="CHAR_STRING" length="16"/>
<item name="value" type="CHAR_STRING" length="16"/>
</struct>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ limitations under the License.
<configurator>
<domain name="CHIP"/>

<struct name="LabelStruct">
<cluster code="0x0041"/>
<item name="label" type="CHAR_STRING" length="16"/>
<item name="value" type="CHAR_STRING" length="16"/>
</struct>

<cluster>
<domain>General</domain>
<name>User Label</name>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7d2771f

Please sign in to comment.