Skip to content

Commit 73d0fae

Browse files
Improve ProcessStatusResponse (project-chip#21331)
* Improve ProcessStatusResponse -- Improve ProcessStatusResponse signature so that we can tell if the error is caused by malformed status response or the error is from stored value inside status response. * address comments * Fix unexpected non-StatusResponse handling. Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent b281903 commit 73d0fae

10 files changed

+48
-62
lines changed

src/app/CommandSender.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ CHIP_ERROR CommandSender::SendInvokeRequest()
120120
CHIP_ERROR CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
121121
System::PacketBufferHandle && aPayload)
122122
{
123+
using namespace Protocols::InteractionModel;
124+
123125
if (mState == State::CommandSent)
124126
{
125127
MoveToState(State::ResponseReceived);
@@ -130,21 +132,27 @@ CHIP_ERROR CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExcha
130132

131133
if (mState == State::AwaitingTimedStatus)
132134
{
133-
err = HandleTimedStatus(aPayloadHeader, std::move(aPayload));
135+
VerifyOrExit(aPayloadHeader.HasMessageType(MsgType::StatusResponse), err = CHIP_ERROR_INVALID_MESSAGE_TYPE);
136+
CHIP_ERROR statusError = CHIP_NO_ERROR;
137+
SuccessOrExit(err = StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError));
138+
SuccessOrExit(err = statusError);
139+
err = SendInvokeRequest();
134140
// Skip all other processing here (which is for the response to the
135141
// invoke request), no matter whether err is success or not.
136142
goto exit;
137143
}
138144

139-
if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandResponse))
145+
if (aPayloadHeader.HasMessageType(MsgType::InvokeCommandResponse))
140146
{
141147
err = ProcessInvokeResponse(std::move(aPayload));
142148
SuccessOrExit(err);
143149
}
144-
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
150+
else if (aPayloadHeader.HasMessageType(MsgType::StatusResponse))
145151
{
146-
err = StatusResponse::ProcessStatusResponse(std::move(aPayload));
147-
SuccessOrExit(err);
152+
CHIP_ERROR statusError = CHIP_NO_ERROR;
153+
SuccessOrExit(err = StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError));
154+
SuccessOrExit(err = statusError);
155+
err = CHIP_ERROR_INVALID_MESSAGE_TYPE;
148156
}
149157
else
150158
{
@@ -369,13 +377,6 @@ TLV::TLVWriter * CommandSender::GetCommandDataIBTLVWriter()
369377
return mInvokeRequestBuilder.GetInvokeRequests().GetCommandData().GetWriter();
370378
}
371379

372-
CHIP_ERROR CommandSender::HandleTimedStatus(const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
373-
{
374-
ReturnErrorOnFailure(TimedRequest::HandleResponse(aPayloadHeader, std::move(aPayload)));
375-
376-
return SendInvokeRequest();
377-
}
378-
379380
CHIP_ERROR CommandSender::FinishCommand(const Optional<uint16_t> & aTimedInvokeTimeoutMs)
380381
{
381382
ReturnErrorOnFailure(FinishCommand(/* aEndDataStruct = */ false));

src/app/CommandSender.h

-8
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,6 @@ class CommandSender final : public Messaging::ExchangeDelegate
259259
CHIP_ERROR ProcessInvokeResponse(System::PacketBufferHandle && payload);
260260
CHIP_ERROR ProcessInvokeResponseIB(InvokeResponseIB::Parser & aInvokeResponse);
261261

262-
// Handle a message received when we are expecting a status response to a
263-
// Timed Request. The caller is assumed to have already checked that our
264-
// exchange context member is the one the message came in on.
265-
//
266-
// If the server returned an error status, that will be returned as an error
267-
// value of CHIP_ERROR.
268-
CHIP_ERROR HandleTimedStatus(const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload);
269-
270262
// Send our queued-up Invoke Request message. Assumes the exchange is ready
271263
// and mPendingInvokeData is populated.
272264
CHIP_ERROR SendInvokeRequest();

src/app/ReadClient.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,10 @@ CHIP_ERROR ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchange
424424
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
425425
{
426426
VerifyOrExit(apExchangeContext == mExchange.Get(), err = CHIP_ERROR_INCORRECT_STATE);
427-
err = StatusResponse::ProcessStatusResponse(std::move(aPayload));
428-
SuccessOrExit(err);
427+
CHIP_ERROR statusError = CHIP_NO_ERROR;
428+
SuccessOrExit(err = StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError));
429+
SuccessOrExit(err = statusError);
430+
err = CHIP_ERROR_INVALID_MESSAGE_TYPE;
429431
}
430432
else
431433
{

src/app/ReadHandler.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ CHIP_ERROR ReadHandler::OnInitialRequest(System::PacketBufferHandle && aPayload)
116116

117117
CHIP_ERROR ReadHandler::OnStatusResponse(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload)
118118
{
119-
CHIP_ERROR err = CHIP_NO_ERROR;
120-
err = StatusResponse::ProcessStatusResponse(std::move(aPayload));
121-
SuccessOrExit(err);
119+
CHIP_ERROR err = CHIP_NO_ERROR;
120+
CHIP_ERROR statusError = CHIP_NO_ERROR;
121+
SuccessOrExit(err = StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError));
122+
SuccessOrExit(err = statusError);
122123
switch (mState)
123124
{
124125
case HandlerState::AwaitingReportResponse:

src/app/StatusResponse.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ CHIP_ERROR StatusResponse::Send(Protocols::InteractionModel::Status aStatus, Mes
4444
return CHIP_NO_ERROR;
4545
}
4646

47-
CHIP_ERROR StatusResponse::ProcessStatusResponse(System::PacketBufferHandle && aPayload)
47+
CHIP_ERROR StatusResponse::ProcessStatusResponse(System::PacketBufferHandle && aPayload, CHIP_ERROR & aStatusError)
4848
{
4949
StatusResponseMessage::Parser response;
5050
System::PacketBufferTLVReader reader;
@@ -59,12 +59,8 @@ CHIP_ERROR StatusResponse::ProcessStatusResponse(System::PacketBufferHandle && a
5959
ChipLogValueIMStatus(status.mStatus));
6060
ReturnErrorOnFailure(response.ExitContainer());
6161

62-
if (status.mStatus == Protocols::InteractionModel::Status::Success)
63-
{
64-
return CHIP_NO_ERROR;
65-
}
66-
67-
return status.ToChipError();
62+
aStatusError = status.ToChipError();
63+
return CHIP_NO_ERROR;
6864
}
6965
} // namespace app
7066
} // namespace chip

src/app/StatusResponse.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ class StatusResponse
3232
public:
3333
static CHIP_ERROR Send(Protocols::InteractionModel::Status aStatus, Messaging::ExchangeContext * apExchangeContext,
3434
bool aExpectResponse);
35-
static CHIP_ERROR ProcessStatusResponse(System::PacketBufferHandle && aPayload);
35+
36+
// The return value indicates whether the StatusResponse was parsed properly, and if it is CHIP_NO_ERROR
37+
// then aStatus has been set to the actual status, which might be success or failure.
38+
static CHIP_ERROR ProcessStatusResponse(System::PacketBufferHandle && aPayload, CHIP_ERROR & aStatus);
3639
};
3740
} // namespace app
3841
} // namespace chip

src/app/TimedRequest.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,5 @@ CHIP_ERROR TimedRequest::Send(ExchangeContext * aExchangeContext, uint16_t aTime
5353
return aExchangeContext->SendMessage(MsgType::TimedRequest, std::move(payload), SendMessageFlags::kExpectResponse);
5454
}
5555

56-
CHIP_ERROR TimedRequest::HandleResponse(const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
57-
{
58-
VerifyOrReturnError(aPayloadHeader.HasMessageType(MsgType::StatusResponse), CHIP_ERROR_INVALID_MESSAGE_TYPE);
59-
60-
return StatusResponse::ProcessStatusResponse(std::move(aPayload));
61-
}
62-
6356
} // namespace app
6457
} // namespace chip

src/app/WriteClient.cpp

+14-12
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ CHIP_ERROR WriteClient::SendWriteRequest()
414414
CHIP_ERROR WriteClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
415415
System::PacketBufferHandle && aPayload)
416416
{
417+
using namespace Protocols::InteractionModel;
418+
417419
if (mState == State::AwaitingResponse &&
418420
// We had sent the last chunk of data, and received all responses
419421
mChunks.IsNull())
@@ -431,13 +433,18 @@ CHIP_ERROR WriteClient::OnMessageReceived(Messaging::ExchangeContext * apExchang
431433

432434
if (mState == State::AwaitingTimedStatus)
433435
{
434-
err = HandleTimedStatus(aPayloadHeader, std::move(aPayload));
436+
VerifyOrExit(aPayloadHeader.HasMessageType(MsgType::StatusResponse), err = CHIP_ERROR_INVALID_MESSAGE_TYPE);
437+
CHIP_ERROR statusError = CHIP_NO_ERROR;
438+
SuccessOrExit(err = StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError));
439+
SuccessOrExit(err = statusError);
440+
err = SendWriteRequest();
441+
435442
// Skip all other processing here (which is for the response to the
436443
// write request), no matter whether err is success or not.
437444
goto exit;
438445
}
439446

440-
if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::WriteResponse))
447+
if (aPayloadHeader.HasMessageType(MsgType::WriteResponse))
441448
{
442449
err = ProcessWriteResponseMessage(std::move(aPayload));
443450
SuccessOrExit(err);
@@ -447,10 +454,12 @@ CHIP_ERROR WriteClient::OnMessageReceived(Messaging::ExchangeContext * apExchang
447454
SuccessOrExit(SendWriteRequest());
448455
}
449456
}
450-
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
457+
else if (aPayloadHeader.HasMessageType(MsgType::StatusResponse))
451458
{
452-
err = StatusResponse::ProcessStatusResponse(std::move(aPayload));
453-
SuccessOrExit(err);
459+
CHIP_ERROR statusError = CHIP_NO_ERROR;
460+
SuccessOrExit(err = StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError));
461+
SuccessOrExit(err = statusError);
462+
err = CHIP_ERROR_INVALID_MESSAGE_TYPE;
454463
}
455464
else
456465
{
@@ -522,12 +531,5 @@ CHIP_ERROR WriteClient::ProcessAttributeStatusIB(AttributeStatusIB::Parser & aAt
522531
return err;
523532
}
524533

525-
CHIP_ERROR WriteClient::HandleTimedStatus(const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
526-
{
527-
ReturnErrorOnFailure(TimedRequest::HandleResponse(aPayloadHeader, std::move(aPayload)));
528-
529-
return SendWriteRequest();
530-
}
531-
532534
} // namespace app
533535
} // namespace chip

src/app/WriteClient.h

-9
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,6 @@ class WriteClient : public Messaging::ExchangeDelegate
340340
*/
341341
void Abort();
342342

343-
// Handle a message received when we are expecting a status response to a
344-
// Timed Request. The caller is assumed to have already checked that our
345-
// exchange context member is the one the message came in on.
346-
//
347-
// If the server returned an error status response its status will be
348-
// encapsulated in the CHIP_ERROR this returns. In that case,
349-
// StatusIB::InitFromChipError can be used to extract the status.
350-
CHIP_ERROR HandleTimedStatus(const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload);
351-
352343
// Send our queued-up Write Request message. Assumes the exchange is ready
353344
// and mPendingWriteData is populated.
354345
CHIP_ERROR SendWriteRequest();

src/app/tests/TestTimedHandler.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ class TestExchangeDelegate : public Messaging::ExchangeDelegate
6868
mLastMessageWasStatus = aPayloadHeader.HasMessageType(MsgType::StatusResponse);
6969
if (mLastMessageWasStatus)
7070
{
71-
mError = StatusResponse::ProcessStatusResponse(std::move(aPayload));
71+
CHIP_ERROR statusError = CHIP_NO_ERROR;
72+
mError = StatusResponse::ProcessStatusResponse(std::move(aPayload), statusError);
73+
if (mError == CHIP_NO_ERROR)
74+
{
75+
mError = statusError;
76+
}
7277
}
7378
if (mKeepExchangeOpen)
7479
{

0 commit comments

Comments
 (0)