Skip to content

Commit

Permalink
Enhance chip-tool's formatting for some IDs (#35088)
Browse files Browse the repository at this point in the history
* Added logging functions

* Modify ZAP template file generation

* Updated files for CommandId support

* Updated manual tests for the new formatting

* Fixed variable name shadowing

* Fixed typo

* Added logging functions to fabric admin

* Restyled by whitespace

* Restyled by clang-format

* Fixed linking

* Fixed typo

* Fixed CommandId logging

* Updated ZAP generated files

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
XToripuru and restyled-commits authored Aug 22, 2024
1 parent 4aac314 commit 72ee6eb
Show file tree
Hide file tree
Showing 15 changed files with 7,765 additions and 713 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static_library("chip-tool-utils") {
"${chip_root}/src/controller/ExamplePersistentStorage.h",
"${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/ComplexArgumentParser.cpp",
"${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/logging/DataModelLogger.cpp",
"${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/logging/EntryToText.cpp",
"commands/clusters/ModelCommand.cpp",
"commands/clusters/ModelCommand.h",
"commands/common/BDXDiagnosticLogsServerDelegate.cpp",
Expand Down
95 changes: 95 additions & 0 deletions examples/chip-tool/commands/clusters/DataModelLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <app/data-model/DecodableList.h>
#include <commands/common/RemoteDataModelLogger.h>
#include <lib/support/BytesToHex.h>
#include <zap-generated/cluster/logging/EntryToText.h>

class DataModelLogger
{
Expand Down Expand Up @@ -157,6 +158,100 @@ class DataModelLogger
return CHIP_NO_ERROR;
}

static CHIP_ERROR LogClusterId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::ClusterId> & value)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + ClusterIdToText(iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

static CHIP_ERROR LogAttributeId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::AttributeId> & value, chip::ClusterId cluster)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + AttributeIdToText(cluster, iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

static CHIP_ERROR LogAcceptedCommandId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::CommandId> & value,
chip::ClusterId cluster)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + AcceptedCommandIdToText(cluster, iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

static CHIP_ERROR LogGeneratedCommandId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::CommandId> & value,
chip::ClusterId cluster)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + GeneratedCommandIdToText(cluster, iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

#include <zap-generated/cluster/logging/DataModelLogger.h>

static void LogString(size_t indent, const std::string string) { LogString("", indent, string); }
Expand Down
12 changes: 12 additions & 0 deletions examples/chip-tool/templates/logging/DataModelLogger-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ CHIP_ERROR DataModelLogger::LogAttribute(const chip::app::ConcreteDataAttributeP
{
{{zapTypeToDecodableClusterObjectType type ns=parent.name forceNotOptional=true}} value;
ReturnErrorOnFailure(chip::app::DataModel::Decode(*data, value));
{{#if (isEqual name "ServerList")}}
return DataModelLogger::LogClusterId("{{name}}", 1, value);
{{else if (isEqual name "ClientList")}}
return DataModelLogger::LogClusterId("{{name}}", 1, value);
{{else if (isEqual name "AttributeList")}}
return DataModelLogger::LogAttributeId("{{name}}", 1, value, {{asUpperCamelCase parent.name}}::Id);
{{else if (isEqual name "AcceptedCommandList")}}
return DataModelLogger::LogAcceptedCommandId("{{name}}", 1, value, {{asUpperCamelCase parent.name}}::Id);
{{else if (isEqual name "GeneratedCommandList")}}
return DataModelLogger::LogGeneratedCommandId("{{name}}", 1, value, {{asUpperCamelCase parent.name}}::Id);
{{else}}
return DataModelLogger::LogValue("{{name}}", 1, value);
{{/if}}
}
{{#last}}
}
Expand Down
84 changes: 84 additions & 0 deletions examples/chip-tool/templates/logging/EntryToText-src.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{{> header}}

#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app-common/zap-generated/ids/Commands.h>

char const * ClusterIdToText(chip::ClusterId id) {
switch(id)
{
{{#zcl_clusters}}
case chip::app::Clusters::{{asUpperCamelCase name}}::Id: return "{{asUpperCamelCase name}}";
{{/zcl_clusters}}
default: return "Unknown";
}
}

char const * AttributeIdToText(chip::ClusterId cluster, chip::AttributeId id) {
switch(cluster)
{
{{#zcl_clusters}}
{{#zcl_attributes_server}}
{{#first}}
case chip::app::Clusters::{{asUpperCamelCase parent.name}}::Id:
{
switch(id)
{
{{/first}}
case chip::app::Clusters::{{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::Id: return "{{asUpperCamelCase name}}";
{{#last}}
default: return "Unknown";
}
}
{{/last}}
{{/zcl_attributes_server}}
{{/zcl_clusters}}
default: return "Unknown";
}
}

char const * AcceptedCommandIdToText(chip::ClusterId cluster, chip::CommandId id) {
switch(cluster)
{
{{#zcl_clusters}}
{{#zcl_commands_source_client}}
{{#first}}
case chip::app::Clusters::{{asUpperCamelCase parent.name}}::Id:
{
switch(id)
{
{{/first}}
case chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Id: return "{{asUpperCamelCase name}}";
{{#last}}
default: return "Unknown";
}
}
{{/last}}
{{/zcl_commands_source_client}}
{{/zcl_clusters}}
default: return "Unknown";
}
}

char const * GeneratedCommandIdToText(chip::ClusterId cluster, chip::CommandId id) {
switch(cluster)
{
{{#zcl_clusters}}
{{#zcl_commands_source_server}}
{{#first}}
case chip::app::Clusters::{{asUpperCamelCase parent.name}}::Id:
{
switch(id)
{
{{/first}}
case chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Id: return "{{asUpperCamelCase name}}";
{{#last}}
default: return "Unknown";
}
}
{{/last}}
{{/zcl_commands_source_server}}
{{/zcl_clusters}}
default: return "Unknown";
}
}
13 changes: 13 additions & 0 deletions examples/chip-tool/templates/logging/EntryToText.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{> header}}

#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app-common/zap-generated/ids/Commands.h>

char const * ClusterIdToText(chip::ClusterId id);

char const * AttributeIdToText(chip::ClusterId cluster, chip::AttributeId id);

char const * AcceptedCommandIdToText(chip::ClusterId cluster, chip::CommandId id);

char const * GeneratedCommandIdToText(chip::ClusterId cluster, chip::CommandId id);
10 changes: 10 additions & 0 deletions examples/chip-tool/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
"path": "ComplexArgumentParser-src.zapt",
"name": "Complex Argument Parser",
"output": "cluster/ComplexArgumentParser.cpp"
},
{
"path": "logging/EntryToText.zapt",
"name": "Entry To Text header",
"output": "cluster/logging/EntryToText.h"
},
{
"path": "logging/EntryToText-src.zapt",
"name": "Entry To Text",
"output": "cluster/logging/EntryToText.cpp"
}
]
}
95 changes: 95 additions & 0 deletions examples/fabric-admin/commands/clusters/DataModelLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <app/data-model/DecodableList.h>
#include <commands/common/RemoteDataModelLogger.h>
#include <lib/support/BytesToHex.h>
#include <zap-generated/cluster/logging/EntryToText.h>

class DataModelLogger
{
Expand Down Expand Up @@ -157,6 +158,100 @@ class DataModelLogger
return CHIP_NO_ERROR;
}

static CHIP_ERROR LogClusterId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::ClusterId> & value)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + ClusterIdToText(iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

static CHIP_ERROR LogAttributeId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::AttributeId> & value, chip::ClusterId cluster)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + AttributeIdToText(cluster, iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

static CHIP_ERROR LogAcceptedCommandId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::CommandId> & value,
chip::ClusterId cluster)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + AcceptedCommandIdToText(cluster, iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

static CHIP_ERROR LogGeneratedCommandId(const char * label, size_t indent,
const chip::app::DataModel::DecodableList<chip::CommandId> & value,
chip::ClusterId cluster)
{
size_t count = 0;
ReturnErrorOnFailure(value.ComputeSize(&count));
DataModelLogger::LogString(label, indent, std::to_string(count) + " entries");

auto iter = value.begin();
size_t i = 0;
while (iter.Next())
{
++i;
std::string index = std::string("[") + std::to_string(i) + "]";
std::string item = std::to_string(iter.GetValue()) + " (" + GeneratedCommandIdToText(cluster, iter.GetValue()) + ")";
DataModelLogger::LogString(index, indent + 1, item);
}
if (iter.GetStatus() != CHIP_NO_ERROR)
{
DataModelLogger::LogString(indent + 1, "List truncated due to invalid value");
}
return iter.GetStatus();
}

#include <zap-generated/cluster/logging/DataModelLogger.h>

static void LogString(size_t indent, const std::string string) { LogString("", indent, string); }
Expand Down
1 change: 1 addition & 0 deletions examples/tv-casting-app/tv-casting-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ chip_data_model("tv-casting-common") {
"${chip_root}/src/controller/ExamplePersistentStorage.h",
"${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/ComplexArgumentParser.cpp",
"${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/logging/DataModelLogger.cpp",
"${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/logging/EntryToText.cpp",
"clusters/content-app-observer/ContentAppObserver.cpp",
"clusters/content-app-observer/ContentAppObserver.h",
"commands/clusters/ModelCommand.cpp",
Expand Down
Loading

0 comments on commit 72ee6eb

Please sign in to comment.