Skip to content

Commit

Permalink
Clean up BdxTransferSession code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple committed Jun 21, 2023
1 parent 6919158 commit 27032fa
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 161 deletions.
62 changes: 24 additions & 38 deletions src/protocols/bdx/BdxTransferSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ void TransferSession::DispatchOutputEvent(TransferSession::OutputEvent & outputE
}

template <typename MessageType>
TransferSession::MessageTypeData TransferSession::PrepareOutgoingMessageEvent(MessageType messageType)
void TransferSession::PrepareAndSendOutgoingMessageEvent(MessageType messageType, System::PacketBufferHandle msg)
{

static_assert(std::is_same<std::underlying_type_t<decltype(messageType)>, uint8_t>::value, "Cast is not safe");

TransferSession::MessageTypeData outputMsgType;
outputMsgType.ProtocolId = chip::Protocols::MessageTypeTraits<MessageType>::ProtocolId();
outputMsgType.MessageType = static_cast<uint8_t>(messageType);

return outputMsgType;
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
}

void TransferSession::RegisterOutputEventHandler(OutputEventHandler callback, void * context)
Expand Down Expand Up @@ -132,10 +134,8 @@ CHIP_ERROR TransferSession::StartTransfer(TransferRole role, const TransferInitD

mState = TransferState::kAwaitingAccept;
mAwaitingResponse = true;

TransferSession::MessageTypeData outputMsgType = PrepareOutgoingMessageEvent(msgType);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);

PrepareAndSendOutgoingMessageEvent(msgType, std::move(msg));

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -222,9 +222,7 @@ CHIP_ERROR TransferSession::AcceptTransfer(const TransferAcceptData & acceptData
mAwaitingResponse = true;
}

TransferSession::MessageTypeData outputMsgType = PrepareOutgoingMessageEvent(msgType);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
PrepareAndSendOutgoingMessageEvent(msgType, std::move(msg));

return CHIP_NO_ERROR;
}
Expand All @@ -251,9 +249,7 @@ CHIP_ERROR TransferSession::PrepareBlockQuery()
mAwaitingResponse = true;
mLastQueryNum = mNextQueryNum++;

TransferSession::MessageTypeData outputMsgType = PrepareOutgoingMessageEvent(msgType);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
PrepareAndSendOutgoingMessageEvent(msgType, std::move(msg));

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -281,9 +277,7 @@ CHIP_ERROR TransferSession::PrepareBlockQueryWithSkip(const uint64_t & bytesToSk
mAwaitingResponse = true;
mLastQueryNum = mNextQueryNum++;

TransferSession::MessageTypeData outputMsgType = PrepareOutgoingMessageEvent(msgType);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
PrepareAndSendOutgoingMessageEvent(msgType, std::move(msg));

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -320,9 +314,7 @@ CHIP_ERROR TransferSession::PrepareBlock(const BlockData & inData)
mAwaitingResponse = true;
mLastBlockNum = mNextBlockNum++;

TransferSession::MessageTypeData outputMsgType = PrepareOutgoingMessageEvent(msgType);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
PrepareAndSendOutgoingMessageEvent(msgType, std::move(msg));

return CHIP_NO_ERROR;
}
Expand All @@ -337,7 +329,7 @@ CHIP_ERROR TransferSession::PrepareBlockAck()
ackMsg.BlockCounter = mLastBlockNum;
const MessageType msgType = (mState == TransferState::kReceivedEOF) ? MessageType::BlockAckEOF : MessageType::BlockAck;

System::PacketBufferHandle msg;
System::PacketBufferHandle msg;
ReturnErrorOnFailure(WriteToPacketBuffer(ackMsg, msg));

#if CHIP_AUTOMATION_LOGGING
Expand All @@ -361,9 +353,7 @@ CHIP_ERROR TransferSession::PrepareBlockAck()
mAwaitingResponse = false;
}

TransferSession::MessageTypeData outputMsgType = PrepareOutgoingMessageEvent(msgType);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
PrepareAndSendOutgoingMessageEvent(msgType, std::move(msg));

return CHIP_NO_ERROR;
}
Expand All @@ -384,11 +374,11 @@ void TransferSession::Reset()
mState = TransferState::kUnitialized;
UnregisterOutputEventHandler();
mSuppportedXferOpts.ClearAll();
mTransferVersion = 0;
mMaxSupportedBlockSize = 0;
mStartOffset = 0;
mTransferLength = 0;
mTransferMaxBlockSize = 0;
mTransferVersion = 0;
mMaxSupportedBlockSize = 0;
mStartOffset = 0;
mTransferLength = 0;
mTransferMaxBlockSize = 0;
mTransferRequestMaxBlockSize = 0;

mFileDesLength = 0;
Expand Down Expand Up @@ -536,14 +526,13 @@ void TransferSession::HandleTransferInit(MessageType msgType, System::PacketBuff
mTransferMaxBlockSize = ::chip::min(mMaxSupportedBlockSize, transferInit.MaxBlockSize);

// Accept for now, they may be changed or rejected by the peer if this is a ReceiveInit
mStartOffset = transferInit.StartOffset;
mTransferLength = transferInit.MaxLength;
mFileDesignator = transferInit.FileDesignator;
mFileDesLength = transferInit.FileDesLength;
mStartOffset = transferInit.StartOffset;
mTransferLength = transferInit.MaxLength;
mFileDesignator = transferInit.FileDesignator;
mFileDesLength = transferInit.FileDesLength;
mTransferRequestMaxBlockSize = transferInit.MaxBlockSize;
mTransferRequestControlFlags = transferInit.TransferCtlOptions;


TransferInitData transferRequestData;
// Store the Request data to share with the caller for verification
transferRequestData.TransferCtlFlags = transferInit.TransferCtlOptions;
Expand Down Expand Up @@ -680,7 +669,7 @@ void TransferSession::HandleBlockQueryWithSkip(System::PacketBufferHandle msgDat

TransferSkipData bytesToSkip;
bytesToSkip.BytesToSkip = query.BytesToSkip;
OutputEvent event = OutputEvent::QueryWithSkipEvent(bytesToSkip);
OutputEvent event = OutputEvent::QueryWithSkipEvent(bytesToSkip);
DispatchOutputEvent(event);
}

Expand Down Expand Up @@ -796,7 +785,7 @@ void TransferSession::HandleBlockAckEOF(System::PacketBufferHandle msgData)
#if CHIP_AUTOMATION_LOGGING
ackMsg.LogMessage(MessageType::BlockAckEOF);
#endif // CHIP_AUTOMATION_LOGGING

OutputEvent event = OutputEvent(OutputEventType::kAckEOFReceived);
DispatchOutputEvent(event);
}
Expand Down Expand Up @@ -913,10 +902,7 @@ void TransferSession::SendStatusReport(StatusCode code)
}
else
{
TransferSession::MessageTypeData outputMsgType =
PrepareOutgoingMessageEvent(Protocols::SecureChannel::MsgType::StatusReport);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
PrepareAndSendOutgoingMessageEvent(Protocols::SecureChannel::MsgType::StatusReport, std::move(msg));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/protocols/bdx/BdxTransferSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class DLL_EXPORT TransferSession
* Helper method to prepare and send an outgoing message of type OutputEventType::kMsgToSend
*/
template <typename MessageType>
TransferSession::MessageTypeData PrepareOutgoingMessageEvent(MessageType messageType);
void PrepareAndSendOutgoingMessageEvent(MessageType messageType, System::PacketBufferHandle msg);

/**
* @brief
Expand Down Expand Up @@ -410,9 +410,9 @@ class DLL_EXPORT TransferSession
uint64_t mStartOffset = 0; ///< 0 represents no offset
uint64_t mTransferLength = 0; ///< 0 represents indefinite length
uint16_t mTransferMaxBlockSize = 0;
const uint8_t * mFileDesignator = nullptr;
uint16_t mFileDesLength = 0;

const uint8_t * mFileDesignator = nullptr;
uint16_t mFileDesLength = 0;
uint16_t mTransferRequestMaxBlockSize = 0;
TransferControlFlags mTransferRequestControlFlags;

Expand Down
Loading

0 comments on commit 27032fa

Please sign in to comment.