Skip to content

Commit

Permalink
Fix status report in ReadClient (#21368) (#21458)
Browse files Browse the repository at this point in the history
* Fix status report in ReadClient

* address comments

* comments

Co-authored-by: yunhanw-google <yunhanw@google.com>
  • Loading branch information
woody-apple and yunhanw-google authored Aug 2, 2022
1 parent c5e7ab6 commit 94e6b1b
Show file tree
Hide file tree
Showing 4 changed files with 460 additions and 19 deletions.
24 changes: 14 additions & 10 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ extern bool emberAfContainsAttribute(chip::EndpointId endpoint, chip::ClusterId

namespace chip {
namespace app {

using Protocols::InteractionModel::Status;

InteractionModelEngine sInteractionModelEngine;

InteractionModelEngine::InteractionModelEngine() {}
Expand Down Expand Up @@ -504,19 +507,18 @@ CHIP_ERROR InteractionModelEngine::OnTimedRequest(Messaging::ExchangeContext * a
return handler->OnMessageReceived(apExchangeContext, aPayloadHeader, std::move(aPayload));
}

CHIP_ERROR InteractionModelEngine::OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload)
Status InteractionModelEngine::OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
System::PacketBufferTLVReader reader;
reader.Init(aPayload.Retain());

ReportDataMessage::Parser report;
ReturnErrorOnFailure(report.Init(reader));
VerifyOrReturnError(report.Init(reader) == CHIP_NO_ERROR, Status::InvalidAction);

SubscriptionId subscriptionId = 0;
ReturnErrorOnFailure(report.GetSubscriptionId(&subscriptionId));
ReturnErrorOnFailure(report.ExitContainer());
VerifyOrReturnError(report.GetSubscriptionId(&subscriptionId) == CHIP_NO_ERROR, Status::InvalidAction);
VerifyOrReturnError(report.ExitContainer() == CHIP_NO_ERROR, Status::InvalidAction);

for (auto * readClient = mpActiveReadClientList; readClient != nullptr; readClient = readClient->GetNextClient())
{
Expand All @@ -530,10 +532,12 @@ CHIP_ERROR InteractionModelEngine::OnUnsolicitedReportData(Messaging::ExchangeCo
continue;
}

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

return CHIP_NO_ERROR;
return Status::InvalidSubscription;
}

CHIP_ERROR InteractionModelEngine::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader,
Expand Down Expand Up @@ -580,8 +584,7 @@ CHIP_ERROR InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::ReportData))
{
ReturnErrorOnFailure(OnUnsolicitedReportData(apExchangeContext, aPayloadHeader, std::move(aPayload)));
status = Status::Success;
status = OnUnsolicitedReportData(apExchangeContext, aPayloadHeader, std::move(aPayload));
}
else if (aPayloadHeader.HasMessageType(MsgType::TimedRequest))
{
Expand All @@ -590,6 +593,7 @@ CHIP_ERROR InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext
else
{
ChipLogProgress(InteractionModel, "Msg type %d not supported", aPayloadHeader.GetMessageType());
status = Status::InvalidAction;
}

if (status != Status::Success && !apExchangeContext->IsGroupExchangeContext())
Expand Down
4 changes: 2 additions & 2 deletions src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
/**This function handles processing of un-solicited ReportData messages on the client, which can
* only occur post subscription establishment
*/
CHIP_ERROR OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload);
Status OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload);

void DispatchCommand(CommandHandler & apCommandObj, const ConcreteCommandPath & aCommandPath,
TLV::TLVReader & apPayload) override;
Expand Down
17 changes: 11 additions & 6 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
namespace chip {
namespace app {

using Status = Protocols::InteractionModel::Status;

ReadClient::ReadClient(InteractionModelEngine * apImEngine, Messaging::ExchangeManager * apExchangeMgr, Callback & apCallback,
InteractionType aInteractionType) :
mExchange(*this),
Expand Down Expand Up @@ -435,6 +437,13 @@ 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 ((!IsSubscriptionType() && !mPendingMoreChunks) || err != CHIP_NO_ERROR)
{
Close(err);
Expand All @@ -461,13 +470,11 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
ReportDataMessage::Parser report;

bool suppressResponse = true;
SubscriptionId subscriptionId = 0;
EventReportIBs::Parser eventReportIBs;
AttributeReportIBs::Parser attributeReportIBs;
System::PacketBufferTLVReader reader;

reader.Init(std::move(aPayload));
err = report.Init(reader);
SuccessOrExit(err);
Expand Down Expand Up @@ -570,12 +577,10 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
}
}

if (!suppressResponse)
if (!suppressResponse && err == CHIP_NO_ERROR)
{
bool noResponseExpected = IsSubscriptionActive() && !mPendingMoreChunks;
err = StatusResponse::Send(err == CHIP_NO_ERROR ? Protocols::InteractionModel::Status::Success
: Protocols::InteractionModel::Status::InvalidSubscription,
mExchange.Get(), !noResponseExpected);
err = StatusResponse::Send(Status::Success, mExchange.Get(), !noResponseExpected);
}

mIsPrimingReports = false;
Expand Down
Loading

0 comments on commit 94e6b1b

Please sign in to comment.