Skip to content

Commit

Permalink
More clean up. Remove mPendingMsgHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple committed Aug 14, 2023
1 parent fbccbed commit 1189960
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
76 changes: 39 additions & 37 deletions src/protocols/bdx/BdxTransferSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ CHIP_ERROR TransferSession::StartTransfer(TransferRole role, const TransferInitD
initMsg.Metadata = initData.Metadata;
initMsg.MetadataLength = initData.MetadataLength;

ReturnErrorOnFailure(WriteToPacketBuffer(initMsg, mPendingMsgHandle));
System::PacketBufferHandle msg;
ReturnErrorOnFailure(WriteToPacketBuffer(initMsg, msg));

const MessageType msgType = (mRole == TransferRole::kSender) ? MessageType::SendInit : MessageType::ReceiveInit;

Expand All @@ -133,7 +134,7 @@ CHIP_ERROR TransferSession::StartTransfer(TransferRole role, const TransferInitD
mAwaitingResponse = true;

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

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -172,6 +173,7 @@ CHIP_ERROR TransferSession::AcceptTransfer(const TransferAcceptData & acceptData

mTransferMaxBlockSize = acceptData.MaxBlockSize;

System::PacketBufferHandle msg;
if (mRole == TransferRole::kSender)
{
mStartOffset = acceptData.StartOffset;
Expand All @@ -186,7 +188,7 @@ CHIP_ERROR TransferSession::AcceptTransfer(const TransferAcceptData & acceptData
acceptMsg.Metadata = acceptData.Metadata;
acceptMsg.MetadataLength = acceptData.MetadataLength;

ReturnErrorOnFailure(WriteToPacketBuffer(acceptMsg, mPendingMsgHandle));
ReturnErrorOnFailure(WriteToPacketBuffer(acceptMsg, msg));
msgType = MessageType::ReceiveAccept;

#if CHIP_AUTOMATION_LOGGING
Expand All @@ -203,7 +205,7 @@ CHIP_ERROR TransferSession::AcceptTransfer(const TransferAcceptData & acceptData
acceptMsg.Metadata = acceptData.Metadata;
acceptMsg.MetadataLength = acceptData.MetadataLength;

ReturnErrorOnFailure(WriteToPacketBuffer(acceptMsg, mPendingMsgHandle));
ReturnErrorOnFailure(WriteToPacketBuffer(acceptMsg, msg));
msgType = MessageType::SendAccept;

#if CHIP_AUTOMATION_LOGGING
Expand All @@ -221,7 +223,7 @@ CHIP_ERROR TransferSession::AcceptTransfer(const TransferAcceptData & acceptData
}

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

return CHIP_NO_ERROR;
Expand All @@ -238,7 +240,8 @@ CHIP_ERROR TransferSession::PrepareBlockQuery()
BlockQuery queryMsg;
queryMsg.BlockCounter = mNextQueryNum;

ReturnErrorOnFailure(WriteToPacketBuffer(queryMsg, mPendingMsgHandle));
System::PacketBufferHandle msg;
ReturnErrorOnFailure(WriteToPacketBuffer(queryMsg, msg));

#if CHIP_AUTOMATION_LOGGING
ChipLogAutomation("Sending BDX Message");
Expand All @@ -249,7 +252,7 @@ CHIP_ERROR TransferSession::PrepareBlockQuery()
mLastQueryNum = mNextQueryNum++;

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

return CHIP_NO_ERROR;
Expand All @@ -267,7 +270,8 @@ CHIP_ERROR TransferSession::PrepareBlockQueryWithSkip(const uint64_t & bytesToSk
queryMsg.BlockCounter = mNextQueryNum;
queryMsg.BytesToSkip = bytesToSkip;

ReturnErrorOnFailure(WriteToPacketBuffer(queryMsg, mPendingMsgHandle));
System::PacketBufferHandle msg;
ReturnErrorOnFailure(WriteToPacketBuffer(queryMsg, msg));

#if CHIP_AUTOMATION_LOGGING
ChipLogAutomation("Sending BDX Message");
Expand All @@ -278,7 +282,7 @@ CHIP_ERROR TransferSession::PrepareBlockQueryWithSkip(const uint64_t & bytesToSk
mLastQueryNum = mNextQueryNum++;

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

return CHIP_NO_ERROR;
Expand All @@ -298,7 +302,8 @@ CHIP_ERROR TransferSession::PrepareBlock(const BlockData & inData)
blockMsg.Data = inData.Data;
blockMsg.DataLength = inData.Length;

ReturnErrorOnFailure(WriteToPacketBuffer(blockMsg, mPendingMsgHandle));
System::PacketBufferHandle msg;
ReturnErrorOnFailure(WriteToPacketBuffer(blockMsg, msg));

const MessageType msgType = inData.IsEof ? MessageType::BlockEOF : MessageType::Block;

Expand All @@ -316,7 +321,7 @@ CHIP_ERROR TransferSession::PrepareBlock(const BlockData & inData)
mLastBlockNum = mNextBlockNum++;

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

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

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

#if CHIP_AUTOMATION_LOGGING
ChipLogAutomation("Sending BDX Message");
Expand All @@ -356,7 +362,7 @@ CHIP_ERROR TransferSession::PrepareBlockAck()
}

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

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -385,8 +391,6 @@ void TransferSession::Reset()
mTransferMaxBlockSize = 0;
mTransferRequestMaxBlockSize = 0;

mPendingMsgHandle = nullptr;
mFileDesignator = nullptr;
mFileDesLength = 0;

mNumBytesProcessed = 0;
Expand Down Expand Up @@ -458,14 +462,10 @@ CHIP_ERROR TransferSession::HandleBdxMessage(const PayloadHeader & header, Syste

case MessageType::Block: {
HandleBlock(std::move(msg));
OutputEvent event = OutputEvent::BlockDataEvent(mBlockEventData);
DispatchOutputEvent(event);
break;
}
case MessageType::BlockEOF: {
HandleBlockEOF(std::move(msg));
OutputEvent event = OutputEvent::BlockDataEvent(mBlockEventData);
DispatchOutputEvent(event);
break;
}

Expand Down Expand Up @@ -556,7 +556,6 @@ void TransferSession::HandleTransferInit(MessageType msgType, System::PacketBuff
transferRequestData.MetadataLength = transferInit.MetadataLength;

mState = TransferState::kNegotiateTransferParams;
mPendingMsgHandle = std::move(msgData);

#if CHIP_AUTOMATION_LOGGING
transferInit.LogMessage(msgType);
Expand Down Expand Up @@ -594,7 +593,6 @@ void TransferSession::HandleReceiveAccept(System::PacketBufferHandle msgData)

mAwaitingResponse = (mControlMode == TransferControlFlags::kSenderDrive);
mState = TransferState::kTransferInProgress;
//mPendingMsgHandle = std::move(msgData);

#if CHIP_AUTOMATION_LOGGING
rcvAcceptMsg.LogMessage(MessageType::ReceiveAccept);
Expand Down Expand Up @@ -629,7 +627,6 @@ void TransferSession::HandleSendAccept(System::PacketBufferHandle msgData)

mAwaitingResponse = (mControlMode == TransferControlFlags::kReceiverDrive);
mState = TransferState::kTransferInProgress;
//mPendingMsgHandle = std::move(msgData);

#if CHIP_AUTOMATION_LOGGING
sendAcceptMsg.LogMessage(MessageType::SendAccept);
Expand Down Expand Up @@ -706,20 +703,23 @@ void TransferSession::HandleBlock(System::PacketBufferHandle msgData)
VerifyOrReturn(mNumBytesProcessed + blockMsg.DataLength <= mTransferLength, SendStatusReport(StatusCode::kLengthMismatch));
}

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

mNumBytesProcessed += blockMsg.DataLength;
mLastBlockNum = blockMsg.BlockCounter;

mAwaitingResponse = false;
//mPendingMsgHandle = std::move(msgData);

#if CHIP_AUTOMATION_LOGGING
blockMsg.LogMessage(MessageType::Block);
#endif // CHIP_AUTOMATION_LOGGING

OutputEvent event = OutputEvent::BlockDataEvent(blockEventData);
DispatchOutputEvent(event);
}

void TransferSession::HandleBlockEOF(System::PacketBufferHandle msgData)
Expand All @@ -735,21 +735,24 @@ void TransferSession::HandleBlockEOF(System::PacketBufferHandle msgData)
VerifyOrReturn(blockEOFMsg.BlockCounter == mLastQueryNum, SendStatusReport(StatusCode::kBadBlockCounter));
VerifyOrReturn(blockEOFMsg.DataLength <= mTransferMaxBlockSize, SendStatusReport(StatusCode::kBadMessageContents));

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

mNumBytesProcessed += blockEOFMsg.DataLength;
mLastBlockNum = blockEOFMsg.BlockCounter;

mAwaitingResponse = false;
mState = TransferState::kReceivedEOF;
//mPendingMsgHandle = std::move(msgData);

#if CHIP_AUTOMATION_LOGGING
blockEOFMsg.LogMessage(MessageType::BlockEOF);
#endif // CHIP_AUTOMATION_LOGGING

OutputEvent event = OutputEvent::BlockDataEvent(blockEventData);
DispatchOutputEvent(event);
}

void TransferSession::HandleBlockAck(System::PacketBufferHandle msgData)
Expand Down Expand Up @@ -902,18 +905,17 @@ void TransferSession::SendStatusReport(StatusCode code)
}

report.WriteToBuffer(bbuf);
mPendingMsgHandle = bbuf.Finalize();
if (mPendingMsgHandle.IsNull())
System::PacketBufferHandle msg = bbuf.Finalize();
if (msg.IsNull())
{
ChipLogError(BDX, "%s: Error preparing status report: %" CHIP_ERROR_FORMAT, __FUNCTION__, CHIP_ERROR_NO_MEMORY.Format());
SendStatusReportWithError(statusReportData);
}
else
{
TransferSession::MessageTypeData outputMsgType =
PrepareOutgoingMessageEvent(Protocols::SecureChannel::MsgType::StatusReport);
ChipLogError(BDX, "Error preparing status report move mpendingmsghandle");
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(mPendingMsgHandle));
PrepareOutgoingMessageEvent(Protocols::SecureChannel::MsgType::StatusReport);
OutputEvent event = TransferSession::OutputEvent::MsgToSendEvent(outputMsgType, std::move(msg));
DispatchOutputEvent(event);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/protocols/bdx/BdxTransferSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ class DLL_EXPORT TransferSession
uint16_t mFileDesLength = 0;
uint16_t mTransferRequestMaxBlockSize = 0;
TransferControlFlags mTransferRequestControlFlags;

BlockData mBlockEventData;

size_t mNumBytesProcessed = 0;

Expand All @@ -426,7 +424,6 @@ class DLL_EXPORT TransferSession
uint32_t mNextQueryNum = 0;

bool mAwaitingResponse = false;
System::PacketBufferHandle mPendingMsgHandle;
};

} // namespace bdx
Expand Down

0 comments on commit 1189960

Please sign in to comment.