Skip to content

Commit

Permalink
[chip-tool] Rely on the readclient/writeclient/commandsender onDone c…
Browse files Browse the repository at this point in the history
…allback to be called for returning any errors (#15571)
  • Loading branch information
vivien-apple authored and pull[bot] committed Nov 20, 2023
1 parent 6c371dd commit 2537000
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
9 changes: 5 additions & 4 deletions examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
if (CHIP_NO_ERROR != error)
{
ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
SetCommandExitStatus(error);
mError = error;
return;
}

Expand All @@ -75,7 +75,7 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
if (CHIP_NO_ERROR != error)
{
ChipLogError(chipTool, "Response Failure: Can not decode Data");
SetCommandExitStatus(error);
mError = error;
return;
}
}
Expand All @@ -84,13 +84,13 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
virtual void OnError(const chip::app::CommandSender * client, CHIP_ERROR error) override
{
ChipLogProgress(chipTool, "Error: %s", chip::ErrorStr(error));
SetCommandExitStatus(error);
mError = error;
}

virtual void OnDone(chip::app::CommandSender * client) override
{
mCommandSender.reset();
SetCommandExitStatus(CHIP_NO_ERROR);
SetCommandExitStatus(mError);
}

template <class T>
Expand All @@ -112,6 +112,7 @@ class ClusterCommand : public ModelCommand, public chip::app::CommandSender::Cal
chip::CommandId mCommandId;
chip::Optional<uint16_t> mTimedInteractionTimeoutMs;

CHIP_ERROR mError = CHIP_NO_ERROR;
CustomArgument mPayload;
std::unique_ptr<chip::app::CommandSender> mCommandSender;
};
18 changes: 10 additions & 8 deletions examples/chip-tool/commands/clusters/ReportCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
if (CHIP_NO_ERROR != error)
{
ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
SetCommandExitStatus(error);
mError = error;
return;
}

if (data == nullptr)
{
ChipLogError(chipTool, "Response Failure: No Data");
SetCommandExitStatus(CHIP_ERROR_INTERNAL);
mError = CHIP_ERROR_INTERNAL;
return;
}

error = DataModelLogger::LogAttribute(path, data);
if (CHIP_NO_ERROR != error)
{
ChipLogError(chipTool, "Response Failure: Can not decode Data");
SetCommandExitStatus(error);
mError = error;
return;
}
}
Expand All @@ -70,37 +70,37 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
if (CHIP_NO_ERROR != error)
{
ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
SetCommandExitStatus(error);
mError = error;
return;
}
}

if (data == nullptr)
{
ChipLogError(chipTool, "Response Failure: No Data");
SetCommandExitStatus(CHIP_ERROR_INTERNAL);
mError = CHIP_ERROR_INTERNAL;
return;
}

CHIP_ERROR error = DataModelLogger::LogEvent(eventHeader, data);
if (CHIP_NO_ERROR != error)
{
ChipLogError(chipTool, "Response Failure: Can not decode Data");
SetCommandExitStatus(error);
mError = error;
return;
}
}

void OnError(CHIP_ERROR error) override
{
ChipLogProgress(chipTool, "Error: %s", chip::ErrorStr(error));
SetCommandExitStatus(error);
mError = error;
}

void OnDone() override
{
mReadClient.reset();
SetCommandExitStatus(CHIP_NO_ERROR);
SetCommandExitStatus(mError);
}

void OnSubscriptionEstablished(uint64_t subscriptionId) override { OnAttributeSubscription(); }
Expand Down Expand Up @@ -177,6 +177,8 @@ class ReportCommand : public ModelCommand, public chip::app::ReadClient::Callbac
// mFabricFiltered is really only used by the attribute commands, but we end
// up needing it in our class's shared code.
chip::Optional<bool> mFabricFiltered;

CHIP_ERROR mError = CHIP_NO_ERROR;
};

class ReadAttribute : public ReportCommand
Expand Down
8 changes: 4 additions & 4 deletions examples/chip-tool/commands/clusters/WriteAttributeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,20 @@ class WriteAttribute : public ModelCommand, public chip::app::WriteClient::Callb
if (CHIP_NO_ERROR != error)
{
ChipLogError(chipTool, "Response Failure: %s", chip::ErrorStr(error));
SetCommandExitStatus(error);
return;
mError = error;
}
}

void OnError(const chip::app::WriteClient * client, CHIP_ERROR error) override
{
ChipLogProgress(chipTool, "Error: %s", chip::ErrorStr(error));
SetCommandExitStatus(error);
mError = error;
}

void OnDone(chip::app::WriteClient * client) override
{
mWriteClient.reset();
SetCommandExitStatus(CHIP_NO_ERROR);
SetCommandExitStatus(mError);
}

template <class T>
Expand All @@ -109,6 +108,7 @@ class WriteAttribute : public ModelCommand, public chip::app::WriteClient::Callb
private:
chip::ClusterId mClusterId;
chip::AttributeId mAttributeId;
CHIP_ERROR mError = CHIP_NO_ERROR;
chip::Optional<uint16_t> mTimedInteractionTimeoutMs;
chip::Optional<chip::DataVersion> mDataVersion = chip::NullOptional;
CustomArgument mAttributeValue;
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CHIP_ERROR CHIPCommand::Run()
ReturnLogErrorOnFailure(InitializeCommissioner(kIdentityGamma, kIdentityGammaFabricId, trustStore));

chip::DeviceLayer::PlatformMgr().ScheduleWork(RunQueuedCommand, reinterpret_cast<intptr_t>(this));
ReturnLogErrorOnFailure(StartWaiting(GetWaitDuration()));
CHIP_ERROR err = StartWaiting(GetWaitDuration());

Shutdown();

Expand All @@ -77,7 +77,7 @@ CHIP_ERROR CHIPCommand::Run()
ReturnLogErrorOnFailure(ShutdownCommissioner(kIdentityGamma));

StopTracing();
return CHIP_NO_ERROR;
return err;
}

void CHIPCommand::StartTracing()
Expand Down

0 comments on commit 2537000

Please sign in to comment.