Skip to content

Commit

Permalink
Check if the result of EncodeCommand is successful before trying to s…
Browse files Browse the repository at this point in the history
…end the command (project-chip#3151)
  • Loading branch information
vivien-apple authored and Rob Walker committed Oct 12, 2020
1 parent 08889a0 commit e516b00
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 4 additions & 4 deletions examples/chip-tool/commands/common/EchoCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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);
}
}

Expand Down
20 changes: 15 additions & 5 deletions examples/chip-tool/commands/common/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -101,6 +110,7 @@ void ModelCommand::SendCommand(ChipDeviceController * dc)
#endif

dc->SendMessage(NULL, buffer);
return true;
}

void ModelCommand::ReceiveCommandResponse(ChipDeviceController * dc, PacketBuffer * buffer) const
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/common/ModelCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/common/NetworkCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e516b00

Please sign in to comment.