Skip to content

Commit

Permalink
[chip-tool] Add report-event support to chip-tool (project-chip#13528)
Browse files Browse the repository at this point in the history
* [chip-tool] Add report-event support to chip-tool

* Update generated content
  • Loading branch information
vivien-apple authored and step0035 committed Feb 8, 2022
1 parent 5c6a928 commit bc578d0
Show file tree
Hide file tree
Showing 4 changed files with 2,327 additions and 97 deletions.
15 changes: 10 additions & 5 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ bool Commands::IsAttributeCommand(std::string commandName) const

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

bool Commands::IsGlobalCommand(std::string commandName) const
Expand Down Expand Up @@ -216,10 +216,11 @@ 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 readEventCommand = false;
bool readCommand = false;
bool writeCommand = false;
bool reportCommand = false;
bool readEventCommand = false;
bool reportEventCommand = false;
for (auto & command : commands)
{
bool shouldPrint = true;
Expand All @@ -242,6 +243,10 @@ void Commands::ShowCluster(std::string executable, std::string clusterName, Comm
{
readEventCommand = true;
}
else if (strcmp(command->GetName(), "report-event") == 0 && reportEventCommand == false)
{
reportEventCommand = true;
}
else
{
shouldPrint = false;
Expand Down
49 changes: 49 additions & 0 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,54 @@ public:
OnGeneralAttributeEventResponse(context, "{{asUpperCamelCase parent.name}}.{{asUpperCamelCase name}} response", value);
}
};

class Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}: public ModelCommand
{
public:
Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}(): ModelCommand("report-event")
{
AddArgument("event-name", "{{asDelimitedCommand (asUpperCamelCase name)}}");
AddArgument("min-interval", 0, UINT16_MAX, &mMinInterval);
AddArgument("max-interval", 0, UINT16_MAX, &mMaxInterval);
AddArgument("wait", 0, 1, &mWait);
ModelCommand::AddArguments();
}

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

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

chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);

auto subscriptionEstablishedCallback = mWait ? OnDefaultSuccessResponseWithoutExit : OnDefaultSuccessResponse;
return cluster.SubscribeEvent<chip::app::Clusters::{{asUpperCamelCase parent.name}}::Events::{{asUpperCamelCase name}}::DecodableType>(this,
OnValueReport,
OnDefaultFailure,
mMinInterval,
mMaxInterval,
subscriptionEstablishedCallback);
}

chip::System::Clock::Timeout GetWaitDuration() const override
{
return chip::System::Clock::Seconds16(mWait ? UINT16_MAX : 10);
}

static void OnValueReport(void * context, chip::app::Clusters::{{asUpperCamelCase parent.name}}::Events::{{asUpperCamelCase name}}::DecodableType value)
{
LogValue("{{asUpperCamelCase parent.name}}.{{asUpperCamelCase name}} report", 0, value);
}

private:
uint16_t mMinInterval;
uint16_t mMaxInterval;
bool mWait;
};
{{/chip_server_cluster_events}}

{{#chip_server_cluster_attributes}}
Expand Down Expand Up @@ -491,6 +539,7 @@ void registerCluster{{asUpperCamelCase name}}(Commands & commands)
{{/chip_server_cluster_attributes}}
{{#chip_server_cluster_events}}
make_unique<Read{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}>(), //
make_unique<Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}>(), //
{{/chip_server_cluster_events}}
};

Expand Down
35 changes: 35 additions & 0 deletions src/controller/CHIPCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,41 @@ class DLL_EXPORT ClusterBase
onSuccessCb, onFailureCb);
}

template <typename DecodableType>
CHIP_ERROR SubscribeEvent(void * context, ReadResponseSuccessCallback<DecodableType> reportCb,
ReadResponseFailureCallback failureCb, uint16_t minIntervalFloorSeconds,
uint16_t maxIntervalCeilingSeconds,
SubscriptionEstablishedCallback subscriptionEstablishedCb = nullptr)
{
VerifyOrReturnError(mDevice != nullptr, CHIP_ERROR_INCORRECT_STATE);

auto onReportCb = [context, reportCb](const app::EventHeader & eventHeader, const DecodableType & aData) {
if (reportCb != nullptr)
{
reportCb(context, aData);
}
};

auto onFailureCb = [context, failureCb](const app::EventHeader * eventHeader, Protocols::InteractionModel::Status status,
CHIP_ERROR aError) {
if (failureCb != nullptr)
{
failureCb(context, app::ToEmberAfStatus(status));
}
};

auto onSubscriptionEstablishedCb = [context, subscriptionEstablishedCb]() {
if (subscriptionEstablishedCb != nullptr)
{
subscriptionEstablishedCb(context);
}
};

return Controller::SubscribeEvent<DecodableType>(mDevice->GetExchangeManager(), mDevice->GetSecureSession().Value(),
mEndpoint, onReportCb, onFailureCb, minIntervalFloorSeconds,
maxIntervalCeilingSeconds, onSubscriptionEstablishedCb);
}

protected:
ClusterBase(uint16_t cluster) : mClusterId(cluster) {}

Expand Down
Loading

0 comments on commit bc578d0

Please sign in to comment.