Skip to content

Commit be9e449

Browse files
committed
Address review comments
1 parent bcd514b commit be9e449

6 files changed

+55
-42
lines changed

src/darwin/Framework/CHIP/MTROTAImageTransferHandler.mm

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#import "MTROTAUnsolicitedBDXMessageHandler.h"
2222
#import "NSStringSpanConversion.h"
2323

24-
#include <MTRError_Internal.h>
25-
2624
using namespace chip;
2725
using namespace chip::bdx;
2826
using namespace chip::app;
@@ -345,7 +343,7 @@
345343
VerifyOrReturnError(ec != nullptr, CHIP_ERROR_INCORRECT_STATE);
346344
CHIP_ERROR err;
347345

348-
// If we receive a ReceiveInit message, then we prepare for transfer
346+
// If we receive a ReceiveInit message, then we prepare for transfer.
349347
if (payloadHeader.HasMessageType(MessageType::ReceiveInit)) {
350348
NodeId nodeId = ec->GetSessionHandle()->GetPeer().GetNodeId();
351349
FabricIndex fabricIndex = ec->GetSessionHandle()->GetFabricIndex();

src/darwin/Framework/CHIP/MTROTAUnsolicitedBDXMessageHandler.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ NS_ASSUME_NONNULL_BEGIN
2828
* This class creates an unsolicited handler for listening to all unsolicited BDX messages
2929
* and when it receives a BDX ReceiveInit message from a node, it creates a new
3030
* MTROTAImageTransferHandler object as a delegate that will prepare for transfer and
31-
* handle all BDX messages for the BDX transfer session with that node.
31+
* handle all BDX messages for the BDX transfer session with that node. If it receives an out of order
32+
* BDX message or if the message is received on a non-valid session, the OnUnsolicitedMessageReceived
33+
* returns CHIP_ERROR_INCORRECT_STATE.
3234
*
3335
*/
3436
class MTROTAUnsolicitedBDXMessageHandler : public chip::Messaging::UnsolicitedMessageHandler
@@ -55,8 +57,6 @@ class MTROTAUnsolicitedBDXMessageHandler : public chip::Messaging::UnsolicitedMe
5557
private:
5658

5759
CHIP_ERROR OnUnsolicitedMessageReceived(const chip::PayloadHeader & payloadHeader, const chip::SessionHandle & session,
58-
chip::Messaging::ExchangeDelegate * _Nonnull & newDelegate) override;
59-
CHIP_ERROR OnUnsolicitedMessageReceived(const chip::PayloadHeader & payloadHeader,
6060
chip::Messaging::ExchangeDelegate * _Nonnull & newDelegate) override;
6161

6262
void OnExchangeCreationFailed(chip::Messaging::ExchangeDelegate * _Nonnull delegate) override;

src/darwin/Framework/CHIP/MTROTAUnsolicitedBDXMessageHandler.mm

+20-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#import "MTROTAUnsolicitedBDXMessageHandler.h"
1919
#import "MTROTAImageTransferHandler.h"
2020

21-
#import <protocols/bdx/BdxMessages.h>
21+
#include <protocols/bdx/BdxMessages.h>
2222

2323
using namespace chip;
2424
using namespace chip::Messaging;
@@ -56,13 +56,7 @@
5656
}
5757

5858
CHIP_ERROR MTROTAUnsolicitedBDXMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, const SessionHandle & session,
59-
ExchangeDelegate * _Nonnull & newDelegate)
60-
{
61-
return OnUnsolicitedMessageReceived(payloadHeader, newDelegate);
62-
}
63-
64-
CHIP_ERROR MTROTAUnsolicitedBDXMessageHandler::OnUnsolicitedMessageReceived(
65-
const PayloadHeader & payloadHeader, ExchangeDelegate * _Nonnull & newDelegate)
59+
ExchangeDelegate * _Nonnull & newDelegate)
6660
{
6761
assertChipStackLockedByCurrentThread();
6862

@@ -71,19 +65,29 @@
7165

7266
VerifyOrReturnError(mExchangeMgr != nullptr, CHIP_ERROR_INCORRECT_STATE);
7367

74-
// If we receive a ReceiveInit BDX message, create a new MTROTAImageTransferHandler and register it
75-
// as the handler for all BDX messages that will come over this exchange and increment the number of delegates.
76-
if (payloadHeader.HasMessageType(MessageType::ReceiveInit)) {
77-
MTROTAImageTransferHandler * otaImageTransferHandler = new MTROTAImageTransferHandler();
78-
newDelegate = otaImageTransferHandler;
68+
if (GetNumberOfDelegates() >= 1)
69+
{
70+
return CHIP_ERROR_BUSY;
71+
}
72+
73+
// Only proceed if there is a valid fabric index for the SessionHandle.
74+
if (session->IsSecureSession() && session->AsSecureSession() != nullptr && session->AsSecureSession()->GetFabricIndex() != kUndefinedFabricIndex)
75+
{
76+
// If we receive a ReceiveInit BDX message, create a new MTROTAImageTransferHandler and register it
77+
// as the handler for all BDX messages that will come over this exchange and increment the number of delegates.
78+
if (payloadHeader.HasMessageType(MessageType::ReceiveInit)) {
79+
MTROTAImageTransferHandler * otaImageTransferHandler = new MTROTAImageTransferHandler();
80+
newDelegate = otaImageTransferHandler;
81+
return CHIP_NO_ERROR;
82+
}
7983
}
80-
return CHIP_NO_ERROR;
84+
return CHIP_ERROR_INCORRECT_STATE;
8185
}
8286

8387
void MTROTAUnsolicitedBDXMessageHandler::OnExchangeCreationFailed(ExchangeDelegate * delegate)
8488
{
85-
auto * transferHandler = static_cast<MTROTAImageTransferHandler *>(delegate);
86-
transferHandler->DestroySelf();
89+
auto * otaTransferHandler = static_cast<MTROTAImageTransferHandler *>(delegate);
90+
otaTransferHandler->DestroySelf();
8791
}
8892

8993
uint8_t MTROTAUnsolicitedBDXMessageHandler::GetNumberOfDelegates()

src/darwin/Framework/CHIPTests/MTROTAProviderTests.m

-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,6 @@ - (XCTestExpectation *)announceProviderToDevice:(MTRDevice *)device
736736

737737
- (void)test001_ReceiveQueryImageRequest_RespondUpdateNotAvailable
738738
{
739-
fprintf(stderr, "\n\n\n\n\n\n\n\n\nATTACH HERE: %d\n\n\n\n\n\n", getpid()); sleep(10);
740739
// Test that if we advertise ourselves as a provider we end up getting a
741740
// QueryImage callbacks that we can respond to.
742741
__auto_type * runner = [[MTROTARequestorAppRunner alloc] initWithPayload:kOnboardingPayload1 testcase:self];

src/protocols/bdx/AsyncTransferFacilitator.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
namespace chip {
2222
namespace bdx {
2323

24+
/**
25+
* Releases the exchange, resets the transfer session and schedules calling the DestroySelf
26+
* virtual method implemented by the subclass to delete the subclass.
27+
*/
2428
void AsyncTransferFacilitator::CleanUp()
2529
{
2630
mExchange.Release();
@@ -52,6 +56,11 @@ bdx::StatusCode AsyncTransferFacilitator::GetBdxStatusCodeFromChipError(CHIP_ERR
5256
return bdx::StatusCode::kUnknown;
5357
}
5458

59+
/**
60+
* Calls the GetNextAction on the TransferSession to get the next output events until it receives TransferSession::OutputEventType::kNone
61+
* If the output event is of type TransferSession::OutputEventType::kMsgToSend, it sends the message over the exchange context, otherwise it
62+
* calls the HandleTransferSessionOutput method implemented by the subclass to handle the BDX message.
63+
*/
5564
void AsyncTransferFacilitator::HandleNextOutputEvents()
5665
{
5766
if (mHandlingOutputEvents)
@@ -64,7 +73,7 @@ void AsyncTransferFacilitator::HandleNextOutputEvents()
6473

6574
// Get the next output event and handle it based on the type of event.
6675
// If its of type kMsgToSend send it over the exchange, otherwise call the HandleTransferSessionOutput
67-
// virtual method that must be implemeted by the subclass to handle the output events.
76+
// virtual method that must be implemeted by the subclass of this class to handle the BDX message.
6877
TransferSession::OutputEvent outEvent;
6978

7079
mTransfer.GetNextAction(outEvent);
@@ -103,9 +112,9 @@ CHIP_ERROR AsyncTransferFacilitator::SendMessage(const TransferSession::MessageT
103112

104113
CHIP_ERROR err = ec->SendMessage(msgTypeData.ProtocolId, msgTypeData.MessageType, std::move(msgBuf), sendFlags);
105114

106-
// If we failed to send the message across the exchange or if we just sent a status report, there is no way to let the other side know there was
107-
// and error sending the message so call CleanUp to release the exchange and clean up so the other side can get notified the exchange is closing
108-
// and clean up as needed.
115+
// If we failed to send the message across the exchange, there is no way to let the other side know there was an error sending
116+
// the message so call CleanUp to release the exchange so the other side can get notified the exchange is closing
117+
// and clean up as needed. Also the CleanUp API resets the transfer session and destroys the subclass.
109118
if (err != CHIP_NO_ERROR)
110119
{
111120
CleanUp();
@@ -127,7 +136,7 @@ CHIP_ERROR AsyncTransferFacilitator::OnMessageReceived(Messaging::ExchangeContex
127136
ChipLogError(BDX, "OnMessageReceived: Failed to handle message: %" CHIP_ERROR_FORMAT, err.Format());
128137

129138
// This should notify the tranfer object to abort transfer so it can send a status report across the exchange
130-
// and clean up when we call HandleNextOutputEvents below.
139+
// when we call HandleNextOutputEvents below.
131140
mTransfer.AbortTransfer(AsyncResponder::GetBdxStatusCodeFromChipError(err));
132141

133142
}
@@ -176,8 +185,7 @@ void AsyncResponder::NotifyEventHandled(TransferSession::OutputEvent & event, CH
176185
ChipLogDetail(BDX, "NotifyEventHandled : Event %s Error %" CHIP_ERROR_FORMAT, event.ToString(event.EventType), error.Format());
177186

178187
// If it's a message indicating either the end of the transfer or a timeout reported by the transfer session
179-
// or an error occured, we need to call the CleanUp method that calls the DestroySelf virtual method implemented
180-
// by the subclass to delete both the sublcass and base class objects.
188+
// or an error occured, we need to call CleanUp.
181189
if (event.EventType == TransferSession::OutputEventType::kAckEOFReceived ||
182190
event.EventType == TransferSession::OutputEventType::kInternalError ||
183191
event.EventType == TransferSession::OutputEventType::kTransferTimeout ||
@@ -187,8 +195,8 @@ void AsyncResponder::NotifyEventHandled(TransferSession::OutputEvent & event, CH
187195
return;
188196
}
189197

190-
// If there was an error, this should notify the tranfer object to abort transfer so it can send a status report
191-
// across the exchange and clean up when we call HandleNextOutputEvents below.
198+
// If there was an error handling the output event, this should notify the tranfer object to abort transfer so it can send a status report
199+
// across the exchange when we call HandleNextOutputEvents below.
192200
if (error != CHIP_NO_ERROR)
193201
{
194202
mTransfer.AbortTransfer(GetBdxStatusCodeFromChipError(error));

src/protocols/bdx/AsyncTransferFacilitator.h

+14-10
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ namespace chip {
2727
namespace bdx {
2828

2929
/**
30-
* An abstract class with methods for handling BDX messages from an ExchangeContext. Once a message is receievd, this
31-
* class passes the message to the TransferSession and gets the next output event from the TransferSession state machine
32-
* and processes it i.e either sends a message across the exchange or calls the HandleTransferSessionOutput virtual method
33-
* to notify the subclass of the event generated based on the type of event generated.
30+
* An abstract class with methods for handling BDX messages received from an ExchangeContext. Once a message is receievd, this
31+
* class passes the message to the TransferSession to process the received message and gets the next output events from the
32+
* TransferSession state machine and either sends a message accross the exchange or calls the HandleTransferSessionOutput virtual method
33+
* to notify the subclass of the event generated. It keeps getting the next output event until it receieves an output event of type
34+
* TransferSession::OutputEventType::kNone. For messages that are sent to the HandleTransferSessionOutput method, the subclass must call the
35+
* NotifyEventHandled to notify the AsyncTransferFacilitator that the event has been handled and returns an error code for error cases or success.
3436
*
3537
* This class does not define any methods for beginning a transfer or initializing the underlying TransferSession object.
3638
* See AsyncResponder for a class that does.
@@ -48,7 +50,7 @@ class AsyncTransferFacilitator : public Messaging::ExchangeDelegate
4850
* This method should be implemented to contain business-logic handling of BDX messages
4951
* and other TransferSession events.
5052
*
51-
* @param[in] event An OutputEvent that contains output from the TransferSession object.
53+
* @param[in] event An OutputEvent that contains the output from the TransferSession object.
5254
*/
5355
virtual void HandleTransferSessionOutput(TransferSession::OutputEvent & event) = 0;
5456

@@ -93,7 +95,7 @@ class AsyncTransferFacilitator : public Messaging::ExchangeDelegate
9395
* Provides a method for initializing the TransferSession members but still needs to be extended to implement
9496
* HandleTransferSessionOutput.
9597
*
96-
* An instance of some subclass of this class, should be used as the exchange delegate for a BDX transfer.
98+
* An instance of some subclass of this class should be used as the exchange delegate for a BDX transfer.
9799
*/
98100
class AsyncResponder : public AsyncTransferFacilitator
99101
{
@@ -112,12 +114,14 @@ class AsyncResponder : public AsyncTransferFacilitator
112114
System::Clock::Timeout timeout);
113115

114116
/**
115-
* This is called by the subclass implementing HandleTransferSessionOutput to notify the transfer facilitator
116-
* that an error has ocurred while handling the last generated output event from this class so that the
117-
* facilitator can call DestroySelf to dlet
117+
* This is called by the subclass implementing HandleTransferSessionOutput to notify the AsyncTransferFacilitator
118+
* that it has handled the OutputEvent specified in event and returns an error code (if any) or success in the error paramter.
119+
* Once this is called the AsyncTransferFacilitator either aborts the transfer if an error has ocurred or drives the TransferSession
120+
* state machine to generate the next output events to establish and continue the BDX session further.
118121
*
119122
*
120-
* @param[in] error The error that occured when handling the event.
123+
* @param[in] event The OutputEvent that was handled by the subclass.
124+
* @param[in] error The error code that occured when handling the event if an error occurs. Otherwise has CHIP_NO_ERROR.
121125
*/
122126
void NotifyEventHandled(TransferSession::OutputEvent & event, CHIP_ERROR error);
123127
};

0 commit comments

Comments
 (0)