Skip to content

Commit 2003216

Browse files
yunhanw-googlebzbarsky-apple
authored andcommitted
fix subscription id (#21609)
* fix subscription id ReadClient should return error when receiving report with subscription Id when current action is read. * Update src/app/ReadClient.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 7974e3c commit 2003216

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

src/app/ReadClient.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ ReadClient::ReadClient(InteractionModelEngine * apImEngine, Messaging::ExchangeM
5757

5858
void ReadClient::ClearActiveSubscriptionState()
5959
{
60-
mIsReporting = false;
61-
mIsPrimingReports = true;
62-
mPendingMoreChunks = false;
63-
mMinIntervalFloorSeconds = 0;
64-
mMaxInterval = 0;
65-
mSubscriptionId = 0;
60+
mIsReporting = false;
61+
mWaitingForFirstPrimingReport = true;
62+
mPendingMoreChunks = false;
63+
mMinIntervalFloorSeconds = 0;
64+
mMaxInterval = 0;
65+
mSubscriptionId = 0;
6666
MoveToState(ClientState::Idle);
6767
}
6868

@@ -504,7 +504,8 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
504504
err = report.GetSubscriptionId(&subscriptionId);
505505
if (CHIP_NO_ERROR == err)
506506
{
507-
if (mIsPrimingReports)
507+
VerifyOrExit(IsSubscriptionType(), err = CHIP_ERROR_INVALID_ARGUMENT);
508+
if (mWaitingForFirstPrimingReport)
508509
{
509510
mSubscriptionId = subscriptionId;
510511
}
@@ -592,7 +593,7 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
592593
err = StatusResponse::Send(Status::Success, mExchange.Get(), !noResponseExpected);
593594
}
594595

595-
mIsPrimingReports = false;
596+
mWaitingForFirstPrimingReport = false;
596597
return err;
597598
}
598599

src/app/ReadClient.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,15 @@ class ReadClient : public Messaging::ExchangeDelegate
496496
Messaging::ExchangeManager * mpExchangeMgr = nullptr;
497497
Messaging::ExchangeHolder mExchange;
498498
Callback & mpCallback;
499-
ClientState mState = ClientState::Idle;
500-
bool mIsReporting = false;
501-
bool mIsInitialReport = true;
502-
bool mIsPrimingReports = true;
503-
bool mPendingMoreChunks = false;
504-
uint16_t mMinIntervalFloorSeconds = 0;
505-
uint16_t mMaxInterval = 0;
506-
SubscriptionId mSubscriptionId = 0;
499+
ClientState mState = ClientState::Idle;
500+
bool mIsReporting = false;
501+
bool mIsInitialReport = true;
502+
// boolean to check if client is waiting for the first priming report
503+
bool mWaitingForFirstPrimingReport = true;
504+
bool mPendingMoreChunks = false;
505+
uint16_t mMinIntervalFloorSeconds = 0;
506+
uint16_t mMaxInterval = 0;
507+
SubscriptionId mSubscriptionId = 0;
507508
ScopedNodeId mPeer;
508509
InteractionType mInteractionType = InteractionType::Read;
509510
Timestamp mEventTimestamp;

src/app/tests/TestReadInteraction.cpp

+36-2
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ class TestReadInteraction
286286
{
287287
public:
288288
static void TestReadClient(nlTestSuite * apSuite, void * apContext);
289+
static void TestReadUnexpectedSubscriptionId(nlTestSuite * apSuite, void * apContext);
289290
static void TestReadHandler(nlTestSuite * apSuite, void * apContext);
290291
static void TestReadClientGenerateAttributePathList(nlTestSuite * apSuite, void * apContext);
291292
static void TestReadClientGenerateInvalidAttributePathList(nlTestSuite * apSuite, void * apContext);
@@ -331,11 +332,11 @@ class TestReadInteraction
331332

332333
private:
333334
static void GenerateReportData(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload,
334-
bool aNeedInvalidReport, bool aSuppressResponse);
335+
bool aNeedInvalidReport, bool aSuppressResponse, bool aHasSubscriptionId);
335336
};
336337

337338
void TestReadInteraction::GenerateReportData(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload,
338-
bool aNeedInvalidReport, bool aSuppressResponse)
339+
bool aNeedInvalidReport, bool aSuppressResponse, bool aHasSubscriptionId = false)
339340
{
340341
CHIP_ERROR err = CHIP_NO_ERROR;
341342
System::PacketBufferTLVWriter writer;
@@ -346,6 +347,12 @@ void TestReadInteraction::GenerateReportData(nlTestSuite * apSuite, void * apCon
346347
err = reportDataMessageBuilder.Init(&writer);
347348
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
348349

350+
if (aHasSubscriptionId)
351+
{
352+
reportDataMessageBuilder.SubscriptionId(1);
353+
NL_TEST_ASSERT(apSuite, reportDataMessageBuilder.GetError() == CHIP_NO_ERROR);
354+
}
355+
349356
AttributeReportIBs::Builder & attributeReportIBsBuilder = reportDataMessageBuilder.CreateAttributeReportIBs();
350357
NL_TEST_ASSERT(apSuite, reportDataMessageBuilder.GetError() == CHIP_NO_ERROR);
351358

@@ -435,6 +442,32 @@ void TestReadInteraction::TestReadClient(nlTestSuite * apSuite, void * apContext
435442
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
436443
}
437444

445+
void TestReadInteraction::TestReadUnexpectedSubscriptionId(nlTestSuite * apSuite, void * apContext)
446+
{
447+
CHIP_ERROR err = CHIP_NO_ERROR;
448+
TestContext & ctx = *static_cast<TestContext *>(apContext);
449+
MockInteractionModelApp delegate;
450+
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &ctx.GetExchangeManager(), delegate,
451+
chip::app::ReadClient::InteractionType::Read);
452+
System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
453+
454+
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
455+
err = readClient.SendRequest(readPrepareParams);
456+
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
457+
458+
// We don't actually want to deliver that message, because we want to
459+
// synthesize the read response. But we don't want it hanging around
460+
// forever either.
461+
ctx.GetLoopback().mNumMessagesToDrop = 1;
462+
ctx.DrainAndServiceIO();
463+
464+
// For read, we don't expect there is subscription id in report data.
465+
GenerateReportData(apSuite, apContext, buf, false /*aNeedInvalidReport*/, true /* aSuppressResponse*/,
466+
true /*aHasSubscriptionId*/);
467+
err = readClient.ProcessReportData(std::move(buf));
468+
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
469+
}
470+
438471
void TestReadInteraction::TestReadHandler(nlTestSuite * apSuite, void * apContext)
439472
{
440473
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -3350,6 +3383,7 @@ const nlTest sTests[] =
33503383
NL_TEST_DEF("TestReadChunking", chip::app::TestReadInteraction::TestReadChunking),
33513384
NL_TEST_DEF("TestSetDirtyBetweenChunks", chip::app::TestReadInteraction::TestSetDirtyBetweenChunks),
33523385
NL_TEST_DEF("CheckReadClient", chip::app::TestReadInteraction::TestReadClient),
3386+
NL_TEST_DEF("TestReadUnexpectedSubscriptionId", chip::app::TestReadInteraction::TestReadUnexpectedSubscriptionId),
33533387
NL_TEST_DEF("CheckReadHandler", chip::app::TestReadInteraction::TestReadHandler),
33543388
NL_TEST_DEF("TestReadClientGenerateAttributePathList", chip::app::TestReadInteraction::TestReadClientGenerateAttributePathList),
33553389
NL_TEST_DEF("TestReadClientGenerateInvalidAttributePathList", chip::app::TestReadInteraction::TestReadClientGenerateInvalidAttributePathList),

0 commit comments

Comments
 (0)