Skip to content

Commit

Permalink
BDX: Added BlockCount field to BlockData
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp committed Dec 9, 2021
1 parent bba082b commit deeadee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/protocols/bdx/BdxTransferSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,10 @@ void TransferSession::HandleBlock(System::PacketBufferHandle msgData)
PrepareStatusReport(StatusCode::kLengthMismatch));
}

mBlockEventData.Data = blockMsg.Data;
mBlockEventData.Length = blockMsg.DataLength;
mBlockEventData.IsEof = false;
mBlockEventData.Data = blockMsg.Data;
mBlockEventData.Length = blockMsg.DataLength;
mBlockEventData.IsEof = false;
mBlockEventData.BlockCounter = blockMsg.BlockCounter;

mPendingMsgHandle = std::move(msgData);
mPendingOutput = OutputEventType::kBlockReceived;
Expand Down Expand Up @@ -678,9 +679,10 @@ void TransferSession::HandleBlockEOF(System::PacketBufferHandle msgData)
VerifyOrReturn(blockEOFMsg.BlockCounter == mLastQueryNum, PrepareStatusReport(StatusCode::kBadBlockCounter));
VerifyOrReturn(blockEOFMsg.DataLength <= mTransferMaxBlockSize, PrepareStatusReport(StatusCode::kBadMessageContents));

mBlockEventData.Data = blockEOFMsg.Data;
mBlockEventData.Length = blockEOFMsg.DataLength;
mBlockEventData.IsEof = true;
mBlockEventData.Data = blockEOFMsg.Data;
mBlockEventData.Length = blockEOFMsg.DataLength;
mBlockEventData.IsEof = true;
mBlockEventData.BlockCounter = blockEOFMsg.BlockCounter;

mPendingMsgHandle = std::move(msgData);
mPendingOutput = OutputEventType::kBlockReceived;
Expand Down
18 changes: 15 additions & 3 deletions src/protocols/bdx/BdxTransferSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ class DLL_EXPORT TransferSession

struct BlockData
{
const uint8_t * Data = nullptr;
size_t Length = 0;
bool IsEof = false;
const uint8_t * Data = nullptr;
size_t Length = 0;
bool IsEof = false;
uint32_t BlockCounter = 0;
};

struct MessageTypeData
Expand Down Expand Up @@ -220,6 +221,17 @@ class DLL_EXPORT TransferSession
*/
CHIP_ERROR PrepareBlockQuery();

/**
* @brief
* Prepare a BlockQueryWithSkip message. The Block counter will be populated automatically.
*
* @param bytesToSkip Number of bytes to seek skip
*
* @return CHIP_ERROR The result of the preparation of a BlockQueryWithSkip message. May also indicate if the TransferSession
* object is unable to handle this request.
*/
CHIP_ERROR PrepareBlockQueryWithSkip(const uint64_t & bytesToSkip);

/**
* @brief
* Prepare a Block message. The Block counter will be populated automatically.
Expand Down
13 changes: 8 additions & 5 deletions src/protocols/bdx/tests/TestBdxTransferSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void SendAndVerifyQuery(nlTestSuite * inSuite, void * inContext, TransferSession
// Helper method for preparing a sending a Block message between two TransferSession objects. The sender refers to the node that is
// sending Blocks. Uses a static counter incremented with each call. Also verifies that block data received matches what was sent.
void SendAndVerifyArbitraryBlock(nlTestSuite * inSuite, void * inContext, TransferSession & sender, TransferSession & receiver,
TransferSession::OutputEvent & outEvent, bool isEof)
TransferSession::OutputEvent & outEvent, bool isEof, uint32_t inBlockCounter)
{
CHIP_ERROR err = CHIP_NO_ERROR;
static uint8_t dataCount = 0;
Expand Down Expand Up @@ -319,6 +319,7 @@ void SendAndVerifyArbitraryBlock(nlTestSuite * inSuite, void * inContext, Transf
if (outEvent.EventType == TransferSession::OutputEventType::kBlockReceived && outEvent.blockdata.Data != nullptr)
{
NL_TEST_ASSERT(inSuite, !memcmp(fakeBlockData, outEvent.blockdata.Data, outEvent.blockdata.Length));
NL_TEST_ASSERT(inSuite, outEvent.blockdata.BlockCounter == inBlockCounter);
}
VerifyNoMoreOutput(inSuite, inContext, receiver);
}
Expand Down Expand Up @@ -414,7 +415,7 @@ void TestInitiatingReceiverReceiverDrive(nlTestSuite * inSuite, void * inContext

// Test BlockQuery -> Block -> BlockAck
SendAndVerifyQuery(inSuite, inContext, respondingSender, initiatingReceiver, outEvent);
SendAndVerifyArbitraryBlock(inSuite, inContext, respondingSender, initiatingReceiver, outEvent, false);
SendAndVerifyArbitraryBlock(inSuite, inContext, respondingSender, initiatingReceiver, outEvent, false, numBlocksSent);
numBlocksSent++;

// Test only one block can be prepared at a time, without receiving a response to the first
Expand All @@ -441,7 +442,7 @@ void TestInitiatingReceiverReceiverDrive(nlTestSuite * inSuite, void * inContext
bool isEof = (numBlocksSent == numBlockSends - 1);

SendAndVerifyQuery(inSuite, inContext, respondingSender, initiatingReceiver, outEvent);
SendAndVerifyArbitraryBlock(inSuite, inContext, respondingSender, initiatingReceiver, outEvent, isEof);
SendAndVerifyArbitraryBlock(inSuite, inContext, respondingSender, initiatingReceiver, outEvent, isEof, numBlocksSent);

numBlocksSent++;
}
Expand Down Expand Up @@ -509,14 +510,16 @@ void TestInitiatingSenderSenderDrive(nlTestSuite * inSuite, void * inContext)
SendAndVerifyAcceptMsg(inSuite, inContext, outEvent, respondingReceiver, TransferRole::kReceiver, acceptData, initiatingSender,
initOptions);

uint32_t numBlocksSent = 0;
// Test multiple Block -> BlockAck -> Block
for (int i = 0; i < 3; i++)
{
SendAndVerifyArbitraryBlock(inSuite, inContext, initiatingSender, respondingReceiver, outEvent, false);
SendAndVerifyArbitraryBlock(inSuite, inContext, initiatingSender, respondingReceiver, outEvent, false, numBlocksSent);
SendAndVerifyBlockAck(inSuite, inContext, initiatingSender, respondingReceiver, outEvent, false);
numBlocksSent++;
}

SendAndVerifyArbitraryBlock(inSuite, inContext, initiatingSender, respondingReceiver, outEvent, true);
SendAndVerifyArbitraryBlock(inSuite, inContext, initiatingSender, respondingReceiver, outEvent, true, numBlocksSent);
SendAndVerifyBlockAck(inSuite, inContext, initiatingSender, respondingReceiver, outEvent, true);
}

Expand Down

0 comments on commit deeadee

Please sign in to comment.