@@ -34,21 +34,21 @@ CHIP_ERROR ReadClient::Init(Messaging::ExchangeManager * apExchangeMgr, Interact
34
34
// Error if already initialized.
35
35
VerifyOrExit (apExchangeMgr != nullptr , err = CHIP_ERROR_INCORRECT_STATE);
36
36
VerifyOrExit (mpExchangeMgr == nullptr , err = CHIP_ERROR_INCORRECT_STATE);
37
- VerifyOrExit (mpExchangeCtx == nullptr , err = CHIP_ERROR_INCORRECT_STATE);
38
37
39
38
mpExchangeMgr = apExchangeMgr;
40
- mpExchangeCtx = nullptr ;
41
39
mpDelegate = apDelegate;
42
40
mState = ClientState::Initialized;
43
41
42
+ AbortExistingExchangeContext ();
43
+
44
44
exit :
45
45
ChipLogFunctError (err);
46
46
return err;
47
47
}
48
48
49
49
void ReadClient::Shutdown ()
50
50
{
51
- ClearExistingExchangeContext ();
51
+ AbortExistingExchangeContext ();
52
52
mpExchangeMgr = nullptr ;
53
53
mpDelegate = nullptr ;
54
54
MoveToState (ClientState::Uninitialized);
@@ -88,7 +88,10 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin
88
88
InteractionModelEngine::GetInstance ()->GetReadClientArrayIndex (this ), GetStateStr ());
89
89
VerifyOrExit (ClientState::Initialized == mState , err = CHIP_ERROR_INCORRECT_STATE);
90
90
VerifyOrExit (mpDelegate != nullptr , err = CHIP_ERROR_INCORRECT_STATE);
91
- VerifyOrExit (mpExchangeCtx == nullptr , err = CHIP_ERROR_INCORRECT_STATE);
91
+
92
+ // Discard any existing exchange context. Effectively we can only have one exchange per ReadClient
93
+ // at any one time.
94
+ AbortExistingExchangeContext ();
92
95
93
96
{
94
97
System::PacketBufferTLVWriter writer;
@@ -175,23 +178,33 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin
175
178
176
179
exit :
177
180
ChipLogFunctError (err);
181
+
182
+ if (err != CHIP_NO_ERROR)
183
+ {
184
+ AbortExistingExchangeContext ();
185
+ }
186
+
178
187
return err;
179
188
}
180
189
181
190
void ReadClient::OnMessageReceived (Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
182
191
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload)
183
192
{
184
193
CHIP_ERROR err = CHIP_NO_ERROR;
194
+
195
+ VerifyOrExit (apExchangeContext == mpExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
185
196
VerifyOrExit (aPayloadHeader.HasMessageType (Protocols::InteractionModel::MsgType::ReportData),
186
197
err = CHIP_ERROR_INVALID_MESSAGE_TYPE);
187
- VerifyOrExit (apExchangeContext == mpExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
188
198
err = ProcessReportData (std::move (aPayload));
189
199
190
200
exit :
191
201
ChipLogFunctError (err);
192
202
193
- ClearExistingExchangeContext ();
203
+ // Close the exchange cleanly so that the ExchangeManager will send an ack for the message we just received.
204
+ mpExchangeCtx->Close ();
205
+ mpExchangeCtx = nullptr ;
194
206
MoveToState (ClientState::Initialized);
207
+
195
208
if (mpDelegate != nullptr )
196
209
{
197
210
if (err != CHIP_NO_ERROR)
@@ -207,7 +220,7 @@ void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContex
207
220
return ;
208
221
}
209
222
210
- CHIP_ERROR ReadClient::ClearExistingExchangeContext ()
223
+ CHIP_ERROR ReadClient::AbortExistingExchangeContext ()
211
224
{
212
225
if (mpExchangeCtx != nullptr )
213
226
{
@@ -302,7 +315,7 @@ void ReadClient::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContex
302
315
{
303
316
ChipLogProgress (DataManagement, " Time out! failed to receive report data from Exchange: %d" ,
304
317
apExchangeContext->GetExchangeId ());
305
- ClearExistingExchangeContext ();
318
+ AbortExistingExchangeContext ();
306
319
MoveToState (ClientState::Initialized);
307
320
if (nullptr != mpDelegate)
308
321
{
0 commit comments