From dc096db59a3498ce2fb8c8d5315749c924eed5c3 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Fri, 21 Jan 2022 23:34:41 -0800 Subject: [PATCH] apply keepSubscription logic for subscription only (#13807) --- src/app/InteractionModelEngine.cpp | 56 ++++++++++++++++++++---------- src/app/InteractionModelEngine.h | 3 ++ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/app/InteractionModelEngine.cpp b/src/app/InteractionModelEngine.cpp index 4b8ad8c596eedf..8cf9da4edd015c 100644 --- a/src/app/InteractionModelEngine.cpp +++ b/src/app/InteractionModelEngine.cpp @@ -205,14 +205,29 @@ CHIP_ERROR InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeCon return CHIP_NO_ERROR; } -CHIP_ERROR InteractionModelEngine::OnReadInitialRequest(Messaging::ExchangeContext * apExchangeContext, - const PayloadHeader & aPayloadHeader, - System::PacketBufferHandle && aPayload, - ReadHandler::InteractionType aInteractionType, - Protocols::InteractionModel::Status & aStatus) +CHIP_ERROR InteractionModelEngine::ShutdownExistingSubscriptionsIfNeeded(Messaging::ExchangeContext * apExchangeContext, + System::PacketBufferHandle && aPayload) { - ChipLogDetail(InteractionModel, "Received %s request", - aInteractionType == ReadHandler::InteractionType::Subscribe ? "Subscribe" : "Read"); + bool keepSubscriptions = true; + System::PacketBufferTLVReader reader; + reader.Init(std::move(aPayload)); + ReturnErrorOnFailure(reader.Next()); + SubscribeRequestMessage::Parser subscribeRequestParser; + ReturnErrorOnFailure(subscribeRequestParser.Init(reader)); + CHIP_ERROR err = subscribeRequestParser.GetKeepSubscriptions(&keepSubscriptions); + if (CHIP_END_OF_TLV == err) + { + return CHIP_NO_ERROR; + } + else if (CHIP_NO_ERROR != err) + { + return err; + } + + if (keepSubscriptions) + { + return CHIP_NO_ERROR; + } for (auto & readHandler : mReadHandlers) { @@ -220,19 +235,20 @@ CHIP_ERROR InteractionModelEngine::OnReadInitialRequest(Messaging::ExchangeConte readHandler.GetInitiatorNodeId() == apExchangeContext->GetSessionHandle()->AsSecureSession()->GetPeerNodeId() && readHandler.GetAccessingFabricIndex() == apExchangeContext->GetSessionHandle()->AsSecureSession()->GetFabricIndex()) { - bool keepSubscriptions = true; - System::PacketBufferTLVReader reader; - reader.Init(aPayload.Retain()); - ReturnErrorOnFailure(reader.Next()); - SubscribeRequestMessage::Parser subscribeRequestParser; - ReturnErrorOnFailure(subscribeRequestParser.Init(reader)); - CHIP_ERROR err = subscribeRequestParser.GetKeepSubscriptions(&keepSubscriptions); - if (err == CHIP_NO_ERROR && !keepSubscriptions) - { - readHandler.Shutdown(ReadHandler::ShutdownOptions::AbortCurrentExchange); - } + readHandler.Shutdown(ReadHandler::ShutdownOptions::AbortCurrentExchange); } } + return CHIP_NO_ERROR; +} + +CHIP_ERROR InteractionModelEngine::OnReadInitialRequest(Messaging::ExchangeContext * apExchangeContext, + const PayloadHeader & aPayloadHeader, + System::PacketBufferHandle && aPayload, + ReadHandler::InteractionType aInteractionType, + Protocols::InteractionModel::Status & aStatus) +{ + ChipLogDetail(InteractionModel, "Received %s request", + aInteractionType == ReadHandler::InteractionType::Subscribe ? "Subscribe" : "Read"); // Reserve the last ReadHandler for ReadInteraction if (aInteractionType == ReadHandler::InteractionType::Subscribe && @@ -355,7 +371,9 @@ CHIP_ERROR InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext } else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::SubscribeRequest)) { - SuccessOrExit(OnReadInitialRequest(apExchangeContext, aPayloadHeader, std::move(aPayload), + System::PacketBufferHandle payload = aPayload.Retain(); + SuccessOrExit(ShutdownExistingSubscriptionsIfNeeded(apExchangeContext, std::move(aPayload))); + SuccessOrExit(OnReadInitialRequest(apExchangeContext, aPayloadHeader, std::move(payload), ReadHandler::InteractionType::Subscribe, status)); } else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::ReportData)) diff --git a/src/app/InteractionModelEngine.h b/src/app/InteractionModelEngine.h index 001263eb686740..4e23604e22f2ad 100644 --- a/src/app/InteractionModelEngine.h +++ b/src/app/InteractionModelEngine.h @@ -232,6 +232,9 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman bool HasActiveRead(); + CHIP_ERROR ShutdownExistingSubscriptionsIfNeeded(Messaging::ExchangeContext * apExchangeContext, + System::PacketBufferHandle && aPayload); + Messaging::ExchangeManager * mpExchangeMgr = nullptr; InteractionModelDelegate * mpDelegate = nullptr;