Skip to content

Commit 91eed4b

Browse files
committedOct 8, 2024
Addresssed review comments
1 parent 3c0ca7c commit 91eed4b

File tree

4 files changed

+66
-53
lines changed

4 files changed

+66
-53
lines changed
 

‎src/darwin/Framework/CHIP/MTROTAImageTransferHandler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MTROTAImageTransferHandler : public chip::bdx::AsyncResponder
4646
chip::System::PacketBufferHandle && payload) override;
4747

4848
private:
49-
CHIP_ERROR PrepareForTransfer(chip::System::Layer * layer, chip::Messaging::ExchangeContext * exchangeCtx,
49+
CHIP_ERROR Init(chip::System::Layer * layer, chip::Messaging::ExchangeContext * exchangeCtx,
5050
chip::FabricIndex fabricIndex, chip::NodeId nodeId);
5151

5252
CHIP_ERROR ConfigureState(chip::FabricIndex fabricIndex, chip::NodeId nodeId);

‎src/darwin/Framework/CHIP/MTROTAImageTransferHandler.mm

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
MTROTAUnsolicitedBDXMessageHandler::IncrementNumberOfDelegates();
3939
}
4040

41-
CHIP_ERROR MTROTAImageTransferHandler::PrepareForTransfer(System::Layer * layer,
41+
CHIP_ERROR MTROTAImageTransferHandler::Init(System::Layer * layer,
4242
Messaging::ExchangeContext * exchangeCtx, FabricIndex fabricIndex, NodeId nodeId)
4343
{
4444
assertChipStackLockedByCurrentThread();
@@ -50,7 +50,7 @@
5050

5151
BitFlags<bdx::TransferControlFlags> flags(bdx::TransferControlFlags::kReceiverDrive);
5252

53-
return AsyncResponder::PrepareForTransfer(layer, exchangeCtx, kBdxRole, flags, kMaxBdxBlockSize, kBdxTimeout);
53+
return AsyncResponder::Init(mSystemLayer, exchangeCtx, kBdxRole, flags, kMaxBdxBlockSize, kBdxTimeout);
5454
}
5555

5656
MTROTAImageTransferHandler::~MTROTAImageTransferHandler()
@@ -350,7 +350,7 @@
350350
FabricIndex fabricIndex = ec->GetSessionHandle()->GetFabricIndex();
351351

352352
if (nodeId != kUndefinedNodeId && fabricIndex != kUndefinedFabricIndex) {
353-
err = PrepareForTransfer(&DeviceLayer::SystemLayer(), ec, fabricIndex, nodeId);
353+
err = Init(&DeviceLayer::SystemLayer(), ec, fabricIndex, nodeId);
354354
if (err != CHIP_NO_ERROR) {
355355
ChipLogError(Controller, "OnMessageReceived: Failed to prepare for transfer for BDX");
356356
return err;

‎src/protocols/bdx/AsyncTransferFacilitator.cpp

+29-24
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,34 @@ bdx::StatusCode AsyncTransferFacilitator::GetBdxStatusCodeFromChipError(CHIP_ERR
5454
return bdx::StatusCode::kUnknown;
5555
}
5656

57+
CHIP_ERROR AsyncTransferFacilitator::Init(System::Layer * layer, Messaging::ExchangeContext * exchangeCtx,
58+
System::Clock::Timeout timeout)
59+
{
60+
VerifyOrReturnError(layer != nullptr, CHIP_ERROR_INCORRECT_STATE);
61+
VerifyOrReturnError(exchangeCtx != nullptr, CHIP_ERROR_INCORRECT_STATE);
62+
VerifyOrReturnError(!mExchange, CHIP_ERROR_INCORRECT_STATE);
63+
64+
mSystemLayer = layer;
65+
mExchange.Grab(exchangeCtx);
66+
mTimeout = timeout;
67+
return CHIP_NO_ERROR;
68+
}
69+
5770
/**
5871
* Calls the GetNextAction on the TransferSession to get the next output events until it receives
5972
* TransferSession::OutputEventType::kNone If the output event is of type TransferSession::OutputEventType::kMsgToSend, it sends the
6073
* message over the exchange context, otherwise it calls the HandleTransferSessionOutput method implemented by the subclass to
6174
* handle the BDX message.
6275
*/
63-
void AsyncTransferFacilitator::HandleNextOutputEvents()
76+
void AsyncTransferFacilitator::ProcessOutputEvents()
6477
{
65-
if (mHandlingOutputEvents)
78+
if (mProcessingOutputEvents)
6679
{
67-
ChipLogDetail(BDX, "HandleNextOutputEvents: Still getting and processing output events from a previous call. Return.");
80+
ChipLogDetail(BDX, "ProcessOutputEvents: Still getting and processing output events from a previous call. Return.");
6881
return;
6982
}
7083

71-
mHandlingOutputEvents = true;
84+
mProcessingOutputEvents = true;
7285

7386
// Get the next output event and handle it based on the type of event.
7487
// If its of type kMsgToSend send it over the exchange, otherwise call the HandleTransferSessionOutput
@@ -88,7 +101,7 @@ void AsyncTransferFacilitator::HandleNextOutputEvents()
88101
}
89102
mTransfer.GetNextAction(outEvent);
90103
}
91-
mHandlingOutputEvents = false;
104+
mProcessingOutputEvents = false;
92105
}
93106

94107
CHIP_ERROR AsyncTransferFacilitator::SendMessage(const TransferSession::MessageTypeData msgTypeData,
@@ -136,7 +149,7 @@ CHIP_ERROR AsyncTransferFacilitator::OnMessageReceived(Messaging::ExchangeContex
136149
ChipLogError(BDX, "OnMessageReceived: Failed to handle message: %" CHIP_ERROR_FORMAT, err.Format());
137150

138151
// This should notify the tranfer object to abort transfer so it can send a status report across the exchange
139-
// when we call HandleNextOutputEvents below.
152+
// when we call ProcessOutputEvents below.
140153
mTransfer.AbortTransfer(AsyncResponder::GetBdxStatusCodeFromChipError(err));
141154
}
142155
else if (!payloadHeader.HasMessageType(MessageType::BlockAckEOF))
@@ -146,7 +159,7 @@ CHIP_ERROR AsyncTransferFacilitator::OnMessageReceived(Messaging::ExchangeContex
146159
ec->WillSendMessage();
147160
}
148161

149-
HandleNextOutputEvents();
162+
ProcessOutputEvents();
150163
return err;
151164
}
152165

@@ -162,26 +175,19 @@ void AsyncTransferFacilitator::OnResponseTimeout(Messaging::ExchangeContext * ec
162175
CleanUp();
163176
}
164177

165-
CHIP_ERROR AsyncResponder::PrepareForTransfer(System::Layer * layer, Messaging::ExchangeContext * exchangeCtx, TransferRole role,
178+
CHIP_ERROR AsyncResponder::Init(System::Layer * layer, Messaging::ExchangeContext * exchangeCtx, TransferRole role,
166179
BitFlags<TransferControlFlags> xferControlOpts, uint16_t maxBlockSize,
167180
System::Clock::Timeout timeout)
168181
{
169-
VerifyOrReturnError(exchangeCtx != nullptr, CHIP_ERROR_INCORRECT_STATE);
170-
VerifyOrReturnError(!mExchange, CHIP_ERROR_INCORRECT_STATE);
171-
172-
mSystemLayer = layer;
173-
mTimeout = timeout;
174-
175-
ReturnErrorOnFailure(mTransfer.WaitForTransfer(role, xferControlOpts, maxBlockSize, mTimeout));
176-
177-
mExchange.Grab(exchangeCtx);
182+
AsyncTransferFacilitator::Init(layer, exchangeCtx, timeout);
183+
ReturnErrorOnFailure(mTransfer.WaitForTransfer(role, xferControlOpts, maxBlockSize, timeout));
178184

179185
return CHIP_NO_ERROR;
180186
}
181187

182-
void AsyncResponder::NotifyEventHandled(TransferSession::OutputEvent & event, CHIP_ERROR error)
188+
void AsyncResponder::NotifyEventHandled(TransferSession::OutputEvent & event, CHIP_ERROR status)
183189
{
184-
ChipLogDetail(BDX, "NotifyEventHandled : Event %s Error %" CHIP_ERROR_FORMAT, event.ToString(event.EventType), error.Format());
190+
ChipLogDetail(BDX, "NotifyEventHandled : Event %s Error %" CHIP_ERROR_FORMAT, event.ToString(event.EventType), status.Format());
185191

186192
// If it's a message indicating either the end of the transfer or a timeout reported by the transfer session
187193
// or an error occured, we need to call CleanUp.
@@ -195,14 +201,13 @@ void AsyncResponder::NotifyEventHandled(TransferSession::OutputEvent & event, CH
195201
}
196202

197203
// If there was an error handling the output event, this should notify the tranfer object to abort transfer so it can send a
198-
// status report
199-
// across the exchange when we call HandleNextOutputEvents below.
200-
if (error != CHIP_NO_ERROR)
204+
// status report across the exchange when we call ProcessOutputEvents below.
205+
if (status != CHIP_NO_ERROR)
201206
{
202-
mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(error));
207+
mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(status));
203208
}
204209

205-
HandleNextOutputEvents();
210+
ProcessOutputEvents();
206211
}
207212

208213
} // namespace bdx

‎src/protocols/bdx/AsyncTransferFacilitator.h

+33-25
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ namespace bdx {
2828

2929
/**
3030
* An abstract class with methods for handling BDX messages received from an ExchangeContext. Once a message is received, this
31-
* class passes the message to the TransferSession to process the received message and gets the resulting output events from the
32-
* TransferSession state machine and either sends a message accross the exchange or calls the HandleTransferSessionOutput virtual
33-
* method to notify the subclass of the event generated. It keeps getting the next output event until it recieves an output event
34-
* of type TransferSession::OutputEventType::kNone. For events that are handled via the HandleTransferSessionOutput method, the
35-
* subclass must call the NotifyEventHandled to notify the AsyncTransferFacilitator that the event has been handled and returns an
36-
* error code for error cases or success.
31+
* class passes the message to the TransferSession to process the received messages and calls ProcessOutputEvents to get the next
32+
* output events generated by the TransferSession state machine until it receives an output event of type TransferSession::OutputEventType::kNone.
33+
* For each output event it receives, it either sends a message accross the exchange if the TransferSession generated an event of
34+
* type TransferSession::OutputEventType::kMsgToSend or calls the HandleTransferSessionOutput method to notify the subclass of the output event
35+
* which has the business logic of handling the BDX message and calling the TransferSession API to proceed with the BDX transfer. For e.g.,
36+
* If this class received a ReceiveInit message and passed it to the HandleTransferSessionOutput method, the subclass would call the
37+
* AcceptTransfer on the TransferSession object to negotiate params and prepare a ReceiveAccept message.
3738
*
3839
* This class does not define any methods for beginning a transfer or initializing the underlying TransferSession object.
3940
* See AsyncResponder for a class that does.
@@ -47,6 +48,18 @@ class AsyncTransferFacilitator : public Messaging::ExchangeDelegate
4748
AsyncTransferFacilitator() : mExchange(*this) {}
4849
~AsyncTransferFacilitator() override;
4950

51+
protected:
52+
CHIP_ERROR Init(System::Layer * layer, Messaging::ExchangeContext * exchangeCtx,
53+
System::Clock::Timeout timeout);
54+
CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
55+
System::PacketBufferHandle && payload) override;
56+
void OnResponseTimeout(Messaging::ExchangeContext * ec) override;
57+
void OnExchangeClosing(Messaging::ExchangeContext * ec) override;
58+
59+
CHIP_ERROR SendMessage(const TransferSession::MessageTypeData msgTypeData, System::PacketBufferHandle & msgBuf);
60+
61+
static bdx::StatusCode GetBdxStatusCodeFromChipError(CHIP_ERROR err);
62+
5063
/**
5164
* This method should be implemented to contain business-logic handling of BDX messages
5265
* and other TransferSession events.
@@ -60,23 +73,16 @@ class AsyncTransferFacilitator : public Messaging::ExchangeDelegate
6073
*/
6174
virtual void DestroySelf() = 0;
6275

63-
protected:
64-
CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
65-
System::PacketBufferHandle && payload) override;
66-
void OnResponseTimeout(Messaging::ExchangeContext * ec) override;
67-
void OnExchangeClosing(Messaging::ExchangeContext * ec) override;
68-
69-
CHIP_ERROR SendMessage(const TransferSession::MessageTypeData msgTypeData, System::PacketBufferHandle & msgBuf);
70-
71-
static bdx::StatusCode GetBdxStatusCodeFromChipError(CHIP_ERROR err);
76+
void ProcessOutputEvents();
7277

7378
void CleanUp();
7479

75-
void HandleNextOutputEvents();
76-
7780
// The transfer session coresponding to this AsyncTransferFacilitator object.
7881
TransferSession mTransfer;
7982

83+
private:
84+
bool mProcessingOutputEvents = false;
85+
8086
// The Exchange holder that holds the exchange context used for sending and receiving BDX messages.
8187
Messaging::ExchangeHolder mExchange;
8288

@@ -85,12 +91,11 @@ class AsyncTransferFacilitator : public Messaging::ExchangeDelegate
8591

8692
System::Layer * mSystemLayer;
8793

88-
private:
89-
bool mHandlingOutputEvents;
9094
};
9195

9296
/**
9397
* An AsyncTransferFacilitator that is initialized to respond to an incoming BDX transfer request.
98+
* An AsyncResponder object is associated with an exchange and handles all BDX messages sent over that exchange.
9499
*
95100
* Provides a method for initializing the TransferSession members but still needs to be extended to implement
96101
* HandleTransferSessionOutput.
@@ -109,21 +114,24 @@ class AsyncResponder : public AsyncTransferFacilitator
109114
* @param[in] maxBlockSize The maximum supported size of BDX Block data
110115
* @param[in] timeout The chosen timeout delay for the BDX transfer
111116
*/
112-
CHIP_ERROR PrepareForTransfer(System::Layer * layer, Messaging::ExchangeContext * exchangeCtx, TransferRole role,
117+
CHIP_ERROR Init(System::Layer * layer, Messaging::ExchangeContext * exchangeCtx, TransferRole role,
113118
BitFlags<TransferControlFlags> xferControlOpts, uint16_t maxBlockSize,
114119
System::Clock::Timeout timeout);
115120

116121
/**
117-
* This is called by the subclass implementing HandleTransferSessionOutput to notify the AsyncTransferFacilitator
122+
* This is called by the subclass implementing HandleTransferSessionOutput to notify the AsyncResponder
118123
* that it has handled the OutputEvent specified in "event" and "status" is the result of handling the event.
119-
* Once this is called the AsyncTransferFacilitator either aborts the transfer if an error has ocurred or drives the
120-
* TransferSession state machine to generate the next output events to establish and continue the BDX session further.
121-
*
124+
* If the OutputEvent was handled successfully by the subclass that means the TransferSession API call to handle the output event
125+
* was successful and the TransferSession has the next message prepared. In that case, the "status" is set to CHIP_NO_ERROR and the
126+
* AsyncResponder should call ProcessOutputEvents to get and process the next output events from the TransferSession to proceed with
127+
* the BDX transfer. Otherwise if the output event indicates the end of BDX transfer (like EOF receieved or transfer timeout), it will
128+
* clean up and destroy itself and the subclass. If the "status" is set to an error code, the AsyncResponder should abort the transfer
129+
* and clean up once the status report is sent over the exchange.
122130
*
123131
* @param[in] event The OutputEvent that was handled by the subclass.
124132
* @param[in] status The error code that occured when handling the event if an error occurs. Otherwise CHIP_NO_ERROR.
125133
*/
126-
void NotifyEventHandled(TransferSession::OutputEvent & event, CHIP_ERROR error);
134+
void NotifyEventHandled(TransferSession::OutputEvent & event, CHIP_ERROR status);
127135
};
128136

129137
} // namespace bdx

0 commit comments

Comments
 (0)