Skip to content

Commit

Permalink
[ChipTool] Update ReportingCommand.cpp to use the async GetConnectedD… (
Browse files Browse the repository at this point in the history
#8373)

* [ChipTool] Update ReportingCommand.cpp to use the async GetConnectedDevice method

* Update examples/chip-tool/commands/reporting/ReportingCommand.cpp

Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
  • Loading branch information
2 people authored and pull[bot] committed Aug 14, 2021
1 parent dd41bfb commit 4934851
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
43 changes: 33 additions & 10 deletions examples/chip-tool/commands/reporting/ReportingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,42 @@ using namespace ::chip;

CHIP_ERROR ReportingCommand::Run()
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::Controller::BasicCluster cluster;

auto * ctx = GetExecContext();
err = ctx->commissioner->GetDevice(ctx->remoteId, &mDevice);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Init failure! No pairing for device: %" PRIu64, ctx->localId));

AddReportCallbacks(mEndPointId);
cluster.Associate(mDevice, mEndPointId);

err = cluster.MfgSpecificPing(nullptr, nullptr);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init failure! Ping failure: %s", ErrorStr(err)));
CHIP_ERROR err =
ctx->commissioner->GetConnectedDevice(ctx->remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(chipTool, "Failed in initiating connection to the device: %" PRIu64 ", error %s", ctx->remoteId,
ErrorStr(err)));

exit:
return err;
}

void ReportingCommand::OnDeviceConnectedFn(void * context, chip::Controller::Device * device)
{
ReportingCommand * command = reinterpret_cast<ReportingCommand *>(context);
VerifyOrReturn(command != nullptr,
ChipLogError(chipTool, "Device connected, but cannot send the command, as the context is null"));

chip::Controller::BasicCluster cluster;
cluster.Associate(device, command->mEndPointId);

command->AddReportCallbacks(command->mEndPointId);

CHIP_ERROR err = cluster.MfgSpecificPing(nullptr, nullptr);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Init failure! Ping failure: %s", ErrorStr(err));
command->SetCommandExitStatus(err);
}
}

void ReportingCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR err)
{
ChipLogError(chipTool, "Failed in connecting to the device %" PRIu64 ". Error %s", deviceId, ErrorStr(err));

ReportingCommand * command = reinterpret_cast<ReportingCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "ReportingCommand context is null"));
command->SetCommandExitStatus(err);
}
11 changes: 9 additions & 2 deletions examples/chip-tool/commands/reporting/ReportingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
class ReportingCommand : public Command
{
public:
ReportingCommand(const char * commandName) : Command(commandName)
ReportingCommand(const char * commandName) :
Command(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this),
mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this)
{
AddArgument("endpoint-id", CHIP_ZCL_ENDPOINT_MIN, CHIP_ZCL_ENDPOINT_MAX, &mEndPointId);
}
Expand All @@ -43,5 +45,10 @@ class ReportingCommand : public Command

private:
uint8_t mEndPointId;
ChipDevice * mDevice;

static void OnDeviceConnectedFn(void * context, chip::Controller::Device * device);
static void OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error);

chip::Callback::Callback<chip::Controller::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::Controller::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
};

0 comments on commit 4934851

Please sign in to comment.