Skip to content

Commit

Permalink
[chip-tool] Add read-event support to chip-tool (#13502)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed May 20, 2022
1 parent c24fe23 commit 6032885
Show file tree
Hide file tree
Showing 12 changed files with 4,845 additions and 1,916 deletions.
15 changes: 15 additions & 0 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,21 @@ const char * Command::GetAttribute(void) const
return nullptr;
}

const char * Command::GetEvent(void) const
{
size_t argsCount = mArgs.size();
for (size_t i = 0; i < argsCount; i++)
{
Argument arg = mArgs.at(i);
if (arg.type == ArgumentType::Attribute)
{
return reinterpret_cast<const char *>(arg.value);
}
}

return nullptr;
}

size_t Command::AddArgumentToList(Argument && argument)
{
if (argument.isOptional() || mArgs.empty() || !mArgs.back().isOptional())
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/commands/common/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Command

const char * GetName(void) const { return mName; }
const char * GetAttribute(void) const;
const char * GetEvent(void) const;
const char * GetArgumentName(size_t index) const;
bool GetArgumentIsOptional(size_t index) const { return mArgs[index].isOptional(); }
size_t GetArgumentsCount(void) const { return mArgs.size(); }
Expand Down
59 changes: 55 additions & 4 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ CHIP_ERROR Commands::RunCommand(int argc, char ** argv)
return CHIP_ERROR_INVALID_ARGUMENT;
}
}
else if (IsEventCommand(argv[2]))
{
if (argc <= 3)
{
ChipLogError(chipTool, "Missing event name");
ShowClusterEvents(argv[0], argv[1], argv[2], cluster->second);
return CHIP_ERROR_INVALID_ARGUMENT;
}

command = GetGlobalCommand(cluster->second, argv[2], argv[3]);
if (command == nullptr)
{
ChipLogError(chipTool, "Unknown event: %s", argv[3]);
ShowClusterEvents(argv[0], argv[1], argv[2], cluster->second);
return CHIP_ERROR_INVALID_ARGUMENT;
}
}
else
{
if (argc <= 3)
Expand Down Expand Up @@ -158,11 +175,21 @@ Command * Commands::GetGlobalCommand(CommandsVector & commands, std::string comm
return nullptr;
}

bool Commands::IsGlobalCommand(std::string commandName) const
bool Commands::IsAttributeCommand(std::string commandName) const
{
return commandName.compare("read") == 0 || commandName.compare("write") == 0 || commandName.compare("report") == 0;
}

bool Commands::IsEventCommand(std::string commandName) const
{
return commandName.compare("read-event") == 0;
}

bool Commands::IsGlobalCommand(std::string commandName) const
{
return IsAttributeCommand(commandName) || IsEventCommand(commandName);
}

void Commands::ShowClusters(std::string executable)
{
fprintf(stderr, "Usage:\n");
Expand All @@ -189,9 +216,10 @@ void Commands::ShowCluster(std::string executable, std::string clusterName, Comm
fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
fprintf(stderr, " | Commands: |\n");
fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
bool readCommand = false;
bool writeCommand = false;
bool reportCommand = false;
bool readCommand = false;
bool writeCommand = false;
bool reportCommand = false;
bool readEventCommand = false;
for (auto & command : commands)
{
bool shouldPrint = true;
Expand All @@ -210,6 +238,10 @@ void Commands::ShowCluster(std::string executable, std::string clusterName, Comm
{
reportCommand = true;
}
else if (strcmp(command->GetName(), "read-event") == 0 && readEventCommand == false)
{
readEventCommand = true;
}
else
{
shouldPrint = false;
Expand Down Expand Up @@ -244,6 +276,25 @@ void Commands::ShowClusterAttributes(std::string executable, std::string cluster
fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
}

void Commands::ShowClusterEvents(std::string executable, std::string clusterName, std::string commandName,
CommandsVector & commands)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s %s %s event-name [param1 param2 ...]\n", executable.c_str(), clusterName.c_str(), commandName.c_str());
fprintf(stderr, "\n");
fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
fprintf(stderr, " | Events: |\n");
fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
for (auto & command : commands)
{
if (commandName.compare(command->GetName()) == 0)
{
fprintf(stderr, " | * %-82s|\n", command->GetEvent());
}
}
fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
}

void Commands::ShowCommand(std::string executable, std::string clusterName, Command * command)
{
fprintf(stderr, "Usage:\n");
Expand Down
3 changes: 3 additions & 0 deletions examples/chip-tool/commands/common/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ class Commands
std::map<std::string, CommandsVector>::iterator GetCluster(std::string clusterName);
Command * GetCommand(CommandsVector & commands, std::string commandName);
Command * GetGlobalCommand(CommandsVector & commands, std::string commandName, std::string attributeName);
bool IsAttributeCommand(std::string commandName) const;
bool IsEventCommand(std::string commandName) const;
bool IsGlobalCommand(std::string commandName) const;

void ShowClusters(std::string executable);
void ShowCluster(std::string executable, std::string clusterName, CommandsVector & commands);
void ShowClusterAttributes(std::string executable, std::string clusterName, std::string commandName, CommandsVector & commands);
void ShowClusterEvents(std::string executable, std::string clusterName, std::string commandName, CommandsVector & commands);
void ShowCommand(std::string executable, std::string clusterName, Command * command);

std::map<std::string, CommandsVector> mClusters;
Expand Down
68 changes: 63 additions & 5 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ CHIP_ERROR LogValue(const char * label, size_t indent, {{zapTypeToDecodableClust
{{/zcl_structs}}
{{/zcl_clusters}}

{{#zcl_clusters}}
{{#zcl_events}}
{{#unless has_more_than_one_cluster}}
CHIP_ERROR LogValue(const char * label, size_t indent, chip::app::Clusters::{{asUpperCamelCase parent.name}}::Events::{{asUpperCamelCase name}}::DecodableType value);
{{/unless}}
{{/zcl_events}}
{{/zcl_clusters}}

#if CHIP_PROGRESS_LOGGING
std::string IndentStr(size_t indent)
{
Expand Down Expand Up @@ -168,6 +176,18 @@ CHIP_ERROR LogValue(const char * label, size_t indent, const chip::Optional<T> &
{{/zcl_clusters}}
#pragma GCC diagnostic pop

// We output helpers for all events here, including ones we might not actually
// be logging.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
{{#zcl_clusters}}
{{#zcl_events}}
{{#unless has_more_than_one_cluster}}
{{> log_event_value ns=parent.name}}
{{/unless}}
{{/zcl_events}}
{{/zcl_clusters}}
#pragma GCC diagnostic pop
} // anonymous namespace

static void OnDefaultSuccessResponseWithoutExit(void * context)
Expand Down Expand Up @@ -197,7 +217,7 @@ static void OnDefaultSuccess(void * context, const chip::app::DataModel::NullObj
};

template <typename T>
static void OnGeneralAttributeResponse(void * context, const char * label, T value)
static void OnGeneralAttributeEventResponse(void * context, const char * label, T value)
{
CHIP_ERROR err = LogValue(label, 0, value);

Expand Down Expand Up @@ -278,6 +298,41 @@ private:

{{/chip_cluster_commands}}

{{#chip_server_cluster_events}}
/*
* Event {{asUpperCamelCase name}}
*/
class Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}: public ModelCommand
{
public:
Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}(): ModelCommand("read-event")
{
AddArgument("event-name", "{{asDelimitedCommand (asUpperCamelCase name)}}");
ModelCommand::AddArguments();
}

~Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}()
{
}

CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) ReadEvent on endpoint %" PRIu8, endpointId);

chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
return cluster.ReadEvent<chip::app::Clusters::{{asUpperCamelCase parent.name}}::Events::{{asUpperCamelCase name}}::DecodableType>(this,
OnEventResponse,
OnDefaultFailure);
}

static void OnEventResponse(void * context, chip::app::Clusters::{{asUpperCamelCase parent.name}}::Events::{{asUpperCamelCase name}}::DecodableType value)
{
OnGeneralAttributeEventResponse(context, "{{asUpperCamelCase parent.name}}.{{asUpperCamelCase name}} response", value);
}
};
{{/chip_server_cluster_events}}

{{#chip_server_cluster_attributes}}
{{! TODO: Various types (floats, structs) not supported here. }}
{{#unless (isStrEqual chipCallback.name "Unsupported")}}
Expand All @@ -299,7 +354,7 @@ public:

CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x00) on endpoint %" PRIu8, endpointId);
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) ReadAttribute on endpoint %" PRIu8, endpointId);

chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
Expand All @@ -310,7 +365,7 @@ public:

static void OnAttributeResponse(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} value)
{
OnGeneralAttributeResponse(context, "{{asUpperCamelCase parent.name}}.{{asUpperCamelCase name}} response", value);
OnGeneralAttributeEventResponse(context, "{{asUpperCamelCase parent.name}}.{{asUpperCamelCase name}} response", value);
}
};

Expand Down Expand Up @@ -342,7 +397,7 @@ public:

CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x01) on endpoint %" PRIu8, endpointId);
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) WriteAttribute on endpoint %" PRIu8, endpointId);

chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
Expand Down Expand Up @@ -374,7 +429,7 @@ public:

CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
{
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) command (0x06) on endpoint %" PRIu8, endpointId);
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 4}}) ReportAttribute on endpoint %" PRIu8, endpointId);

chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);
Expand Down Expand Up @@ -434,6 +489,9 @@ void registerCluster{{asUpperCamelCase name}}(Commands & commands)
{{/if}}
{{/unless}}
{{/chip_server_cluster_attributes}}
{{#chip_server_cluster_events}}
make_unique<Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}>(), //
{{/chip_server_cluster_events}}
};

commands.Register(clusterName, clusterCommands);
Expand Down
16 changes: 16 additions & 0 deletions examples/chip-tool/templates/partials/log_event_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, chip::app::Clusters::{{asUpperCamelCase parent.name}}::Events::{{asUpperCamelCase name}}::DecodableType value)
{
ChipLogProgress(chipTool, "%s%s: {", IndentStr(indent).c_str(), label);
{{#zcl_event_fields}}
{
CHIP_ERROR err = LogValue("{{asUpperCamelCase name}}", indent + 1, value.{{asLowerCamelCase name}});
if (err != CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "%sEvent truncated due to invalid value for '{{asUpperCamelCase name}}'", IndentStr(indent + 1).c_str());
return err;
}
}
{{/zcl_event_fields}}
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 @@ -39,6 +39,10 @@
{
"name": "log_struct_value",
"path": "partials/log_struct_value.zapt"
},
{
"name": "log_event_value",
"path": "partials/log_event_value.zapt"
}
],
"templates": [
Expand Down
Loading

0 comments on commit 6032885

Please sign in to comment.