From 4934851fc6cbd601352c3c1c8ddb42de39788050 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 14 Jul 2021 17:27:10 +0200 Subject: [PATCH] =?UTF-8?q?[ChipTool]=20Update=20ReportingCommand.cpp=20to?= =?UTF-8?q?=20use=20the=20async=20GetConnectedD=E2=80=A6=20(#8373)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [ChipTool] Update ReportingCommand.cpp to use the async GetConnectedDevice method * Update examples/chip-tool/commands/reporting/ReportingCommand.cpp Co-authored-by: Boris Zbarsky --- .../commands/reporting/ReportingCommand.cpp | 43 ++++++++++++++----- .../commands/reporting/ReportingCommand.h | 11 ++++- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/examples/chip-tool/commands/reporting/ReportingCommand.cpp b/examples/chip-tool/commands/reporting/ReportingCommand.cpp index 4e7fb3f148f487..e6691565996700 100644 --- a/examples/chip-tool/commands/reporting/ReportingCommand.cpp +++ b/examples/chip-tool/commands/reporting/ReportingCommand.cpp @@ -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(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(context); + VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "ReportingCommand context is null")); + command->SetCommandExitStatus(err); +} diff --git a/examples/chip-tool/commands/reporting/ReportingCommand.h b/examples/chip-tool/commands/reporting/ReportingCommand.h index 0f2bbfc0c6038b..d868e5390001cf 100644 --- a/examples/chip-tool/commands/reporting/ReportingCommand.h +++ b/examples/chip-tool/commands/reporting/ReportingCommand.h @@ -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); } @@ -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 mOnDeviceConnectedCallback; + chip::Callback::Callback mOnDeviceConnectionFailureCallback; };