From e516b005d7fd80e77b9780daf546943cd39bdfdf Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Mon, 12 Oct 2020 07:57:16 +0200 Subject: [PATCH] Check if the result of EncodeCommand is successful before trying to send the command (#3151) --- .../chip-tool/commands/common/EchoCommand.cpp | 8 ++++---- .../commands/common/ModelCommand.cpp | 20 ++++++++++++++----- .../chip-tool/commands/common/ModelCommand.h | 2 +- .../commands/common/NetworkCommand.h | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/examples/chip-tool/commands/common/EchoCommand.cpp b/examples/chip-tool/commands/common/EchoCommand.cpp index 98f4498ec779d2..e9d57e41426e65 100644 --- a/examples/chip-tool/commands/common/EchoCommand.cpp +++ b/examples/chip-tool/commands/common/EchoCommand.cpp @@ -43,11 +43,11 @@ void EchoCommand::SendEcho() const CHIP_ERROR err = mController->SendMessage(NULL, buffer); if (err == CHIP_NO_ERROR) { - ChipLogProgress(chipTool, "Echo (%s): Message sent to server", GetName()); + ChipLogProgress(chipTool, "Echo (%s): Message sent to server", GetNetworkName()); } else { - ChipLogError(chipTool, "Echo (%s): %s", GetName(), ErrorStr(err)); + ChipLogError(chipTool, "Echo (%s): %s", GetNetworkName(), ErrorStr(err)); } } @@ -62,11 +62,11 @@ void EchoCommand::ReceiveEcho(PacketBuffer * buffer) const bool isEchoIdenticalToMessage = strncmp(msg_buffer, PAYLOAD, data_len) == 0; if (isEchoIdenticalToMessage) { - ChipLogProgress(chipTool, "Echo (%s): Received expected message !", GetName()); + ChipLogProgress(chipTool, "Echo (%s): Received expected message !", GetNetworkName()); } else { - ChipLogProgress(chipTool, "Echo: (%s): Error \nSend: %s \nRecv: %s", GetName(), PAYLOAD, msg_buffer); + ChipLogProgress(chipTool, "Echo: (%s): Error \nSend: %s \nRecv: %s", GetNetworkName(), PAYLOAD, msg_buffer); } } diff --git a/examples/chip-tool/commands/common/ModelCommand.cpp b/examples/chip-tool/commands/common/ModelCommand.cpp index 98a90fa44307bb..5029d0731da732 100644 --- a/examples/chip-tool/commands/common/ModelCommand.cpp +++ b/examples/chip-tool/commands/common/ModelCommand.cpp @@ -50,9 +50,11 @@ bool isValidFrame(uint8_t frameControl) void ModelCommand::OnConnect(ChipDeviceController * dc) { - SendCommand(dc); - UpdateWaitForResponse(true); - WaitForResponse(); + if (SendCommand(dc)) + { + UpdateWaitForResponse(true); + WaitForResponse(); + } } void ModelCommand::OnError(ChipDeviceController * dc, CHIP_ERROR err) @@ -80,7 +82,7 @@ CHIP_ERROR ModelCommand::Run(ChipDeviceController * dc, NodeId remoteId) return err; } -void ModelCommand::SendCommand(ChipDeviceController * dc) +bool ModelCommand::SendCommand(ChipDeviceController * dc) { // Make sure our buffer is big enough, but this will need a better setup! static const size_t bufferSize = 1024; @@ -89,10 +91,17 @@ void ModelCommand::SendCommand(ChipDeviceController * dc) if (buffer == nullptr) { ChipLogError(chipTool, "Failed to allocate memory for packet data."); - return; + return false; } uint16_t dataLength = EncodeCommand(buffer, bufferSize, mEndPointId); + if (dataLength == 0) + { + PacketBuffer::Free(buffer); + ChipLogError(chipTool, "Error while encoding data for command: %s", GetName()); + return false; + } + buffer->SetDataLength(dataLength); ChipLogProgress(chipTool, "Encoded data of length %d", dataLength); @@ -101,6 +110,7 @@ void ModelCommand::SendCommand(ChipDeviceController * dc) #endif dc->SendMessage(NULL, buffer); + return true; } void ModelCommand::ReceiveCommandResponse(ChipDeviceController * dc, PacketBuffer * buffer) const diff --git a/examples/chip-tool/commands/common/ModelCommand.h b/examples/chip-tool/commands/common/ModelCommand.h index 38a7594bd5e104..ff7d0dba2f03c5 100644 --- a/examples/chip-tool/commands/common/ModelCommand.h +++ b/examples/chip-tool/commands/common/ModelCommand.h @@ -56,7 +56,7 @@ class ModelCommand : public NetworkCommand virtual bool HandleClusterResponse(uint8_t * message, uint16_t messageLen) const { return false; } private: - void SendCommand(ChipDeviceController * dc); + bool SendCommand(ChipDeviceController * dc); void ReceiveCommandResponse(ChipDeviceController * dc, chip::System::PacketBuffer * buffer) const; void ParseGlobalResponseCommand(uint8_t commandId, uint8_t * message, uint16_t messageLen) const; diff --git a/examples/chip-tool/commands/common/NetworkCommand.h b/examples/chip-tool/commands/common/NetworkCommand.h index bde44cca25ab7b..e4efd5ea3a0203 100644 --- a/examples/chip-tool/commands/common/NetworkCommand.h +++ b/examples/chip-tool/commands/common/NetworkCommand.h @@ -46,7 +46,7 @@ class NetworkCommand : public Command } } - const char * GetName(void) const { return mName; } + const char * GetNetworkName(void) const { return mName; } virtual void OnConnect(ChipDeviceController * dc) = 0; virtual void OnError(ChipDeviceController * dc, CHIP_ERROR err) = 0;