Skip to content

Commit

Permalink
Log BDX messages
Browse files Browse the repository at this point in the history
  • Loading branch information
carol-apple committed Nov 8, 2021
1 parent 6b422ee commit 5354f12
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 26 deletions.
105 changes: 92 additions & 13 deletions src/protocols/bdx/BdxMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#include <utility>

namespace {
constexpr uint8_t kVersionMask = 0x0F;
constexpr uint8_t kVersionMask = 0x0F;
constexpr uint8_t kMaxFileDesignatorLen = 32;
} // namespace

using namespace chip;
Expand Down Expand Up @@ -99,17 +100,16 @@ CHIP_ERROR TransferInit::Parse(System::PacketBufferHandle aBuffer)
uint32_t tmpUint32Value = 0; // Used for reading non-wide length and offset fields
uint8_t * bufStart = aBuffer->Start();
Reader bufReader(bufStart, aBuffer->DataLength());
BitFlags<RangeControlFlags> rangeCtlFlags;

SuccessOrExit(bufReader.Read8(&proposedTransferCtl).Read8(rangeCtlFlags.RawStorage()).Read16(&MaxBlockSize).StatusCode());
SuccessOrExit(bufReader.Read8(&proposedTransferCtl).Read8(RangeCtlFlags.RawStorage()).Read16(&MaxBlockSize).StatusCode());

Version = proposedTransferCtl & kVersionMask;
TransferCtlOptions.SetRaw(static_cast<uint8_t>(proposedTransferCtl & ~kVersionMask));

StartOffset = 0;
if (rangeCtlFlags.Has(RangeControlFlags::kStartOffset))
if (RangeCtlFlags.Has(RangeControlFlags::kStartOffset))
{
if (rangeCtlFlags.Has(RangeControlFlags::kWiderange))
if (RangeCtlFlags.Has(RangeControlFlags::kWiderange))
{
SuccessOrExit(bufReader.Read64(&StartOffset).StatusCode());
}
Expand All @@ -121,9 +121,9 @@ CHIP_ERROR TransferInit::Parse(System::PacketBufferHandle aBuffer)
}

MaxLength = 0;
if (rangeCtlFlags.Has(RangeControlFlags::kDefLen))
if (RangeCtlFlags.Has(RangeControlFlags::kDefLen))
{
if (rangeCtlFlags.Has(RangeControlFlags::kWiderange))
if (RangeCtlFlags.Has(RangeControlFlags::kWiderange))
{
SuccessOrExit(bufReader.Read64(&MaxLength).StatusCode());
}
Expand Down Expand Up @@ -166,6 +166,32 @@ size_t TransferInit::MessageSize() const
return WriteToBuffer(emptyBuf).Needed();
}

void TransferInit::LogMessage() const
{
char fd[kMaxFileDesignatorLen];
snprintf(fd, FileDesLength + 1, "%s", FileDesignator);

switch (MessageType)
{
case MessageType::SendInit:
ChipLogProgress(BDX, "SendInit");
break;
case MessageType::ReceiveInit:
ChipLogProgress(BDX, "ReceiveInit");
break;
default:
break;
}

ChipLogDetail(BDX, " Proposed Transfer Control: 0x%X", TransferCtlOptions.Raw() | Version);
ChipLogDetail(BDX, " Range Control: 0x%X", RangeCtlFlags.Raw());
ChipLogDetail(BDX, " Proposed Max Block Size: %" PRIu16, MaxBlockSize);
ChipLogDetail(BDX, " Start Offset: 0x" ChipLogFormatX64, ChipLogValueX64(StartOffset));
ChipLogDetail(BDX, " Proposed Max Length: 0x" ChipLogFormatX64, ChipLogValueX64(MaxLength));
ChipLogDetail(BDX, " File Designator Length: %" PRIu16, FileDesLength);
ChipLogDetail(BDX, " File Designator: %s", fd);
}

bool TransferInit::operator==(const TransferInit & another) const
{
if ((MetadataLength != another.MetadataLength) || (FileDesLength != another.FileDesLength))
Expand Down Expand Up @@ -246,6 +272,13 @@ size_t SendAccept::MessageSize() const
return WriteToBuffer(emptyBuf).Needed();
}

void SendAccept::LogMessage() const
{
ChipLogProgress(BDX, "SendAccept");
ChipLogDetail(BDX, " Transfer Control: 0x%X", TransferCtlFlags.Raw() | Version);
ChipLogDetail(BDX, " Max Block Size: %" PRIu16, MaxBlockSize);
}

bool SendAccept::operator==(const SendAccept & another) const
{
if (MetadataLength != another.MetadataLength)
Expand Down Expand Up @@ -317,19 +350,18 @@ CHIP_ERROR ReceiveAccept::Parse(System::PacketBufferHandle aBuffer)
uint32_t tmpUint32Value = 0; // Used for reading non-wide length and offset fields
uint8_t * bufStart = aBuffer->Start();
Reader bufReader(bufStart, aBuffer->DataLength());
BitFlags<RangeControlFlags> rangeCtlFlags;

SuccessOrExit(bufReader.Read8(&transferCtl).Read8(rangeCtlFlags.RawStorage()).Read16(&MaxBlockSize).StatusCode());
SuccessOrExit(bufReader.Read8(&transferCtl).Read8(RangeCtlFlags.RawStorage()).Read16(&MaxBlockSize).StatusCode());

Version = transferCtl & kVersionMask;

// Only one of these values should be set. It is up to the caller to verify this.
TransferCtlFlags.SetRaw(static_cast<uint8_t>(transferCtl & ~kVersionMask));

StartOffset = 0;
if (rangeCtlFlags.Has(RangeControlFlags::kStartOffset))
if (RangeCtlFlags.Has(RangeControlFlags::kStartOffset))
{
if (rangeCtlFlags.Has(RangeControlFlags::kWiderange))
if (RangeCtlFlags.Has(RangeControlFlags::kWiderange))
{
SuccessOrExit(bufReader.Read64(&StartOffset).StatusCode());
}
Expand All @@ -341,9 +373,9 @@ CHIP_ERROR ReceiveAccept::Parse(System::PacketBufferHandle aBuffer)
}

Length = 0;
if (rangeCtlFlags.Has(RangeControlFlags::kDefLen))
if (RangeCtlFlags.Has(RangeControlFlags::kDefLen))
{
if (rangeCtlFlags.Has(RangeControlFlags::kWiderange))
if (RangeCtlFlags.Has(RangeControlFlags::kWiderange))
{
SuccessOrExit(bufReader.Read64(&Length).StatusCode());
}
Expand Down Expand Up @@ -380,6 +412,15 @@ size_t ReceiveAccept::MessageSize() const
return WriteToBuffer(emptyBuf).Needed();
}

void ReceiveAccept::LogMessage() const
{
ChipLogProgress(BDX, "ReceiveAccept");
ChipLogDetail(BDX, " Transfer Control: 0x%X", TransferCtlFlags.Raw() | Version);
ChipLogDetail(BDX, " Range Control: 0x%X", RangeCtlFlags.Raw());
ChipLogDetail(BDX, " Max Block Size: %" PRIu16, MaxBlockSize);
ChipLogDetail(BDX, " Length: 0x" ChipLogFormatX64, ChipLogValueX64(Length));
}

bool ReceiveAccept::operator==(const ReceiveAccept & another) const
{
if (MetadataLength != another.MetadataLength)
Expand Down Expand Up @@ -423,6 +464,26 @@ bool CounterMessage::operator==(const CounterMessage & another) const
return (BlockCounter == another.BlockCounter);
}

void CounterMessage::LogMessage() const
{
switch (MessageType)
{
case MessageType::BlockQuery:
ChipLogProgress(BDX, "BlockQuery");
break;
case MessageType::BlockAck:
ChipLogProgress(BDX, "BlockAck");
break;
case MessageType::BlockAckEOF:
ChipLogProgress(BDX, "BlockAckEOF");
break;
default:
break;
}

ChipLogDetail(BDX, " Block Counter: %" PRIu32, BlockCounter);
}

// WARNING: this function should never return early, since MessageSize() relies on it to calculate
// the size of the message (even if the message is incomplete or filled out incorrectly).
Encoding::LittleEndian::BufferWriter & DataBlock::WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const
Expand Down Expand Up @@ -469,6 +530,24 @@ size_t DataBlock::MessageSize() const
return WriteToBuffer(emptyBuf).Needed();
}

void DataBlock::LogMessage() const
{
switch (MessageType)
{
case MessageType::Block:
ChipLogProgress(BDX, "Block");
break;
case MessageType::BlockEOF:
ChipLogProgress(BDX, "BlockEOF");
break;
default:
break;
}

ChipLogDetail(BDX, " Block Counter: %" PRIu32, BlockCounter);
ChipLogDetail(BDX, " Data Length: %zu", DataLength);
}

bool DataBlock::operator==(const DataBlock & another) const
{
if (DataLength != another.DataLength)
Expand Down
37 changes: 37 additions & 0 deletions src/protocols/bdx/BdxMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ struct BdxMessage
*/
virtual size_t MessageSize() const = 0;

/**
* @brief
* Log all parameters for this message.
*/
virtual void LogMessage() const = 0;

virtual ~BdxMessage() = default;
};

Expand All @@ -129,6 +135,9 @@ struct BdxMessage
*/
struct TransferInit : public BdxMessage
{
TransferInit() {}
TransferInit(bdx::MessageType messageType) { MessageType = messageType; }

/**
* @brief
* Equality check method.
Expand Down Expand Up @@ -158,6 +167,12 @@ struct TransferInit : public BdxMessage
CHIP_ERROR Parse(System::PacketBufferHandle aBuffer) override;
Encoding::LittleEndian::BufferWriter & WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const override;
size_t MessageSize() const override;
void LogMessage() const override;

private:
// These are only needed for logging purpose
bdx::MessageType MessageType;
BitFlags<RangeControlFlags> RangeCtlFlags;
};

using SendInit = TransferInit;
Expand Down Expand Up @@ -192,6 +207,7 @@ struct SendAccept : public BdxMessage
CHIP_ERROR Parse(System::PacketBufferHandle aBuffer) override;
Encoding::LittleEndian::BufferWriter & WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const override;
size_t MessageSize() const override;
void LogMessage() const override;
};

/**
Expand Down Expand Up @@ -226,6 +242,11 @@ struct ReceiveAccept : public BdxMessage
CHIP_ERROR Parse(System::PacketBufferHandle aBuffer) override;
Encoding::LittleEndian::BufferWriter & WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const override;
size_t MessageSize() const override;
void LogMessage() const override;

private:
// This is only needed for logging purpose
BitFlags<RangeControlFlags> RangeCtlFlags;
};

/**
Expand All @@ -234,6 +255,9 @@ struct ReceiveAccept : public BdxMessage
*/
struct CounterMessage : public BdxMessage
{
CounterMessage() {}
CounterMessage(bdx::MessageType messageType) { MessageType = messageType; }

/**
* @brief
* Equality check method.
Expand All @@ -245,6 +269,11 @@ struct CounterMessage : public BdxMessage
CHIP_ERROR Parse(System::PacketBufferHandle aBuffer) override;
Encoding::LittleEndian::BufferWriter & WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const override;
size_t MessageSize() const override;
void LogMessage() const override;

private:
// This is only needed for logging purpose
bdx::MessageType MessageType;
};

using BlockQuery = CounterMessage;
Expand All @@ -256,6 +285,9 @@ using BlockAckEOF = CounterMessage;
*/
struct DataBlock : public BdxMessage
{
DataBlock() {}
DataBlock(bdx::MessageType messageType) { MessageType = messageType; }

/**
* @brief
* Equality check method.
Expand All @@ -275,6 +307,11 @@ struct DataBlock : public BdxMessage
CHIP_ERROR Parse(System::PacketBufferHandle aBuffer) override;
Encoding::LittleEndian::BufferWriter & WriteToBuffer(Encoding::LittleEndian::BufferWriter & aBuffer) const override;
size_t MessageSize() const override;
void LogMessage() const override;

private:
// This is only needed for logging purpose
bdx::MessageType MessageType;
};

using Block = DataBlock;
Expand Down
Loading

0 comments on commit 5354f12

Please sign in to comment.