Skip to content

Commit

Permalink
Fix invalid subscriptionId error (#21500)
Browse files Browse the repository at this point in the history
* Fix invalid subscriptionId error

When there is no valid subscription Id, we should generate invalid
subscription status.

* address comments
  • Loading branch information
yunhanw-google authored and pull[bot] committed Jan 16, 2024
1 parent 5a078f0 commit d4ae4ac
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 26 deletions.
4 changes: 1 addition & 3 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,7 @@ Status InteractionModelEngine::OnUnsolicitedReportData(Messaging::ExchangeContex
{
continue;
}

VerifyOrReturnError(readClient->OnUnsolicitedReportData(apExchangeContext, std::move(aPayload)) == CHIP_NO_ERROR,
Status::InvalidAction);
readClient->OnUnsolicitedReportData(apExchangeContext, std::move(aPayload));
return Status::Success;
}

Expand Down
35 changes: 22 additions & 13 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,18 @@ CHIP_ERROR ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchange
System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;

Status status = Status::InvalidAction;
VerifyOrExit(!IsIdle(), err = CHIP_ERROR_INCORRECT_STATE);

if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::ReportData))
{
err = ProcessReportData(std::move(aPayload));
SuccessOrExit(err);
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::SubscribeResponse))
{
ChipLogProgress(DataManagement, "SubscribeResponse is received");
VerifyOrExit(apExchangeContext == mExchange.Get(), err = CHIP_ERROR_INCORRECT_STATE);
err = ProcessSubscribeResponse(std::move(aPayload));
SuccessOrExit(err);
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
{
Expand All @@ -439,9 +438,11 @@ CHIP_ERROR ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchange
exit:
if (err != CHIP_NO_ERROR)
{
// TODO: if we get here with a ReportData that has an incorrect subscription id, we need to send status with
// InvalidSubscription
StatusResponse::Send(Status::InvalidAction, apExchangeContext, false /*aExpectResponse*/);
if (err == CHIP_ERROR_INVALID_SUBSCRIPTION)
{
status = Status::InvalidSubscription;
}
StatusResponse::Send(status, apExchangeContext, false /*aExpectResponse*/);
}

if ((!IsSubscriptionType() && !mPendingMoreChunks) || err != CHIP_NO_ERROR)
Expand All @@ -452,18 +453,26 @@ CHIP_ERROR ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchange
return err;
}

CHIP_ERROR ReadClient::OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext,
System::PacketBufferHandle && aPayload)
void ReadClient::OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload)
{
Status status = Status::Success;
mExchange.Grab(apExchangeContext);

CHIP_ERROR err = ProcessReportData(std::move(aPayload));
if (err != CHIP_NO_ERROR)
{
if (err == CHIP_ERROR_INVALID_SUBSCRIPTION)
{
status = Status::InvalidSubscription;
}
else
{
status = Status::InvalidAction;
}

StatusResponse::Send(status, mExchange.Get(), false /*aExpectResponse*/);
Close(err);
}

return err;
}

CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
Expand Down Expand Up @@ -501,7 +510,7 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
}
else if (!IsMatchingClient(subscriptionId))
{
err = CHIP_ERROR_INVALID_ARGUMENT;
err = CHIP_ERROR_INVALID_SUBSCRIPTION;
}
}
else if (CHIP_END_OF_TLV == err)
Expand Down Expand Up @@ -827,8 +836,8 @@ CHIP_ERROR ReadClient::ProcessSubscribeResponse(System::PacketBufferHandle && aP
#endif

SubscriptionId subscriptionId = 0;
ReturnErrorOnFailure(subscribeResponse.GetSubscriptionId(&subscriptionId));
VerifyOrReturnError(IsMatchingClient(subscriptionId), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(subscribeResponse.GetSubscriptionId(&subscriptionId) == CHIP_NO_ERROR, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(IsMatchingClient(subscriptionId), CHIP_ERROR_INVALID_SUBSCRIPTION);
ReturnErrorOnFailure(subscribeResponse.GetMaxInterval(&mMaxInterval));

ChipLogProgress(DataManagement,
Expand Down
2 changes: 1 addition & 1 deletion src/app/ReadClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class ReadClient : public Messaging::ExchangeDelegate
*/
CHIP_ERROR SendRequest(ReadPrepareParams & aReadPrepareParams);

CHIP_ERROR OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload);
void OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload);

auto GetSubscriptionId() const
{
Expand Down
Loading

0 comments on commit d4ae4ac

Please sign in to comment.