From 32541ebffd7c32581246bc39037b0f5aa8b65730 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Thu, 9 Dec 2021 12:23:07 -0800 Subject: [PATCH] Reserve the last read handler for ReadInteraction (#12764) --- src/app/InteractionModelEngine.cpp | 23 ++++++++++++++++++++++- src/app/InteractionModelEngine.h | 2 ++ src/app/tests/TestReadInteraction.cpp | 25 +------------------------ 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/app/InteractionModelEngine.cpp b/src/app/InteractionModelEngine.cpp index e1a26d15b81468..1ebfbdbaf4bade 100644 --- a/src/app/InteractionModelEngine.cpp +++ b/src/app/InteractionModelEngine.cpp @@ -131,7 +131,6 @@ CHIP_ERROR InteractionModelEngine::NewReadClient(ReadClient ** const apReadClien ReadClient::Callback * aCallback) { *apReadClient = nullptr; - for (auto & readClient : mReadClients) { if (readClient.IsFree()) @@ -314,6 +313,15 @@ CHIP_ERROR InteractionModelEngine::OnReadInitialRequest(Messaging::ExchangeConte } } + // Reserve the last ReadHandler for ReadInteraction + if (aInteractionType == ReadHandler::InteractionType::Subscribe && + ((CHIP_IM_MAX_NUM_READ_HANDLER - GetNumActiveReadHandlers()) == 1) && !HasActiveRead()) + { + ChipLogProgress(InteractionModel, "Reserve the last ReadHandler for IM read Interaction"); + aStatus = Protocols::InteractionModel::Status::ResourceExhausted; + return CHIP_NO_ERROR; + } + for (auto & readHandler : mReadHandlers) { if (readHandler.IsFree()) @@ -728,5 +736,18 @@ void InteractionModelEngine::OnTimedWrite(TimedHandler * apTimedHandler, Messagi } } +bool InteractionModelEngine::HasActiveRead() +{ + for (auto & readHandler : mReadHandlers) + { + if (!readHandler.IsFree() && readHandler.IsReadType()) + { + return true; + } + } + + return false; +} + } // namespace app } // namespace chip diff --git a/src/app/InteractionModelEngine.h b/src/app/InteractionModelEngine.h index a9ed2dd46e848a..042eb5526a4df3 100644 --- a/src/app/InteractionModelEngine.h +++ b/src/app/InteractionModelEngine.h @@ -275,6 +275,8 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman TLV::TLVReader & apPayload) override; bool CommandExists(const ConcreteCommandPath & aCommandPath) override; + bool HasActiveRead(); + Messaging::ExchangeManager * mpExchangeMgr = nullptr; InteractionModelDelegate * mpDelegate = nullptr; diff --git a/src/app/tests/TestReadInteraction.cpp b/src/app/tests/TestReadInteraction.cpp index 74d7baeb751f1c..d0f872b145cff6 100644 --- a/src/app/tests/TestReadInteraction.cpp +++ b/src/app/tests/TestReadInteraction.cpp @@ -1193,36 +1193,13 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a readPrepareParams3.mMinIntervalFloorSeconds = 2; readPrepareParams3.mMaxIntervalCeilingSeconds = 5; - printf("\nSend 4th subscribe request message to Node: %" PRIu64 "\n", chip::kTestDeviceNodeId); + printf("\nSend 4th subscribe request message to Node: %" PRIu64 ", resource exhausted\n", chip::kTestDeviceNodeId); ReadClient readClient3; readClient3.Init(&ctx.GetExchangeManager(), &delegate, ReadClient::InteractionType::Subscribe); err = readClient3.SendRequest(readPrepareParams3); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); engine->GetReportingEngine().Run(); - NL_TEST_ASSERT(apSuite, delegate.mGotReport); - NL_TEST_ASSERT(apSuite, delegate.mNumAttributeResponse == 1); - NL_TEST_ASSERT(apSuite, !delegate.mReadError); - - delegate.mNumAttributeResponse = 0; - delegate.mGotReport = false; - ReadPrepareParams readPrepareParams4(ctx.GetSessionBobToAlice()); - chip::app::AttributePathParams attributePathParams4[1]; - readPrepareParams4.mpAttributePathParamsList = attributePathParams4; - readPrepareParams4.mpAttributePathParamsList[0].mEndpointId = kTestEndpointId; - readPrepareParams4.mpAttributePathParamsList[0].mClusterId = kTestClusterId; - readPrepareParams4.mpAttributePathParamsList[0].mAttributeId = 1; - readPrepareParams4.mAttributePathParamsListSize = 1; - readPrepareParams4.mMinIntervalFloorSeconds = 2; - readPrepareParams4.mMaxIntervalCeilingSeconds = 5; - - printf("\nSend 5th subscribe request message to Node: %" PRIu64 ", resource exhausted\n", chip::kTestDeviceNodeId); - - ReadClient readClient4; - readClient4.Init(&ctx.GetExchangeManager(), &delegate, ReadClient::InteractionType::Subscribe); - err = readClient4.SendRequest(readPrepareParams4); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - engine->GetReportingEngine().Run(); NL_TEST_ASSERT(apSuite, !delegate.mGotReport); NL_TEST_ASSERT(apSuite, delegate.mNumAttributeResponse == 0); NL_TEST_ASSERT(apSuite, delegate.mReadError);