Skip to content

Commit

Permalink
Reserve the last read handler for ReadInteraction (#12764)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Feb 16, 2022
1 parent 478c308 commit 32541eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
23 changes: 22 additions & 1 deletion src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ CHIP_ERROR InteractionModelEngine::NewReadClient(ReadClient ** const apReadClien
ReadClient::Callback * aCallback)
{
*apReadClient = nullptr;

for (auto & readClient : mReadClients)
{
if (readClient.IsFree())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 1 addition & 24 deletions src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 32541eb

Please sign in to comment.