From 23555612d1ec1e84769c2867e6e8fb8c381a73e4 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Thu, 2 Dec 2021 13:40:06 -0800 Subject: [PATCH] add urgent IM event support (#12459) --- src/app/EventManagement.cpp | 12 ++++------ src/app/EventManagement.h | 33 --------------------------- src/app/InteractionModelEngine.cpp | 5 ++++ src/app/InteractionModelEngine.h | 1 + src/app/ReadHandler.h | 6 +++++ src/app/reporting/Engine.cpp | 17 ++++++++++++++ src/app/reporting/Engine.h | 7 ++++++ src/app/tests/TestReadInteraction.cpp | 15 ++++++++---- 8 files changed, 51 insertions(+), 45 deletions(-) diff --git a/src/app/EventManagement.cpp b/src/app/EventManagement.cpp index d14e5a3b247b90..ee72348620c060 100644 --- a/src/app/EventManagement.cpp +++ b/src/app/EventManagement.cpp @@ -606,7 +606,11 @@ CHIP_ERROR EventManagement::LogEventPrivate(EventLoggingDelegate * apDelegate, E opts.mTimestamp.mType == Timestamp::Type::kSystem ? "Sys" : "Epoch", ChipLogValueX64(opts.mTimestamp.mValue)); #endif // CHIP_CONFIG_EVENT_LOGGING_VERBOSE_DEBUG_LOGS - ScheduleFlushIfNeeded(opts.mUrgent); + if (opts.mUrgent == EventOptions::Type::kUrgent) + { + ConcreteEventPath path(opts.mpEventSchema->mEndpointId, opts.mpEventSchema->mClusterId, opts.mpEventSchema->mEventId); + err = InteractionModelEngine::GetInstance()->GetReportingEngine().ScheduleUrgentEventDelivery(path); + } } return err; @@ -851,12 +855,6 @@ CHIP_ERROR EventManagement::EvictEvent(CHIPCircularTLVBuffer & apBuffer, void * return CHIP_END_OF_TLV; } -CHIP_ERROR EventManagement::ScheduleFlushIfNeeded(EventOptions::Type aUrgent) -{ - // TODO: Implement ScheduleFlushIfNeeded - return CHIP_NO_ERROR; -} - void EventManagement::SetScheduledEventEndpoint(EventNumber * apEventEndpoints) { CircularEventBuffer * eventBuffer = mpEventBuffer; diff --git a/src/app/EventManagement.h b/src/app/EventManagement.h index 604084f8e7c665..f8637a1609d23a 100644 --- a/src/app/EventManagement.h +++ b/src/app/EventManagement.h @@ -398,39 +398,6 @@ class EventManagement CHIP_ERROR FetchEventsSince(chip::TLV::TLVWriter & aWriter, ClusterInfo * apClusterInfolist, PriorityLevel aPriority, EventNumber & aEventNumber, size_t & aEventCount); - /** - * @brief - * Schedule a log offload task. - * - * The function decides whether to schedule a task offload process, - * and if so, it schedules the `LoggingFlushHandler` to be run - * asynchronously on the Chip thread. - * - * The decision to schedule a flush is dependent on three factors: - * - * -- an explicit request to flush the buffer - * - * -- the state of the event buffer and the amount of data not yet - * synchronized with the event consumers - * - * -- whether there is an already pending request flush request event. - * - * The explicit request to schedule a flush is passed via an input - * parameter. - * - * The automatic flush is typically scheduled when the event buffers - * contain enough data to merit starting a new offload. Additional - * triggers -- such as minimum and maximum time between offloads -- - * may also be taken into account depending on the offload strategy. - * - * - * @param aUrgent indiate whether the flush should be scheduled if it is urgent - * - * @retval #CHIP_ERROR_INCORRECT_STATE EventManagement module was not initialized fully. - * @retval #CHIP_NO_ERROR On success. - */ - CHIP_ERROR ScheduleFlushIfNeeded(EventOptions::Type aUrgent); - /** * @brief * Fetch the most recently vended Number for a particular priority level diff --git a/src/app/InteractionModelEngine.cpp b/src/app/InteractionModelEngine.cpp index 6272e7235ad1ee..5c92dd4fb1bbc5 100644 --- a/src/app/InteractionModelEngine.cpp +++ b/src/app/InteractionModelEngine.cpp @@ -502,6 +502,11 @@ uint16_t InteractionModelEngine::GetWriteClientArrayIndex(const WriteClient * co return static_cast(apWriteClient - mWriteClients); } +uint16_t InteractionModelEngine::GetReadHandlerArrayIndex(const ReadHandler * const apReadHandler) const +{ + return static_cast(apReadHandler - mReadHandlers); +} + void InteractionModelEngine::ReleaseClusterInfoList(ClusterInfo *& aClusterInfo) { ClusterInfo * lastClusterInfo = aClusterInfo; diff --git a/src/app/InteractionModelEngine.h b/src/app/InteractionModelEngine.h index c235c7d55c213d..85a2d5eaebf7c1 100644 --- a/src/app/InteractionModelEngine.h +++ b/src/app/InteractionModelEngine.h @@ -176,6 +176,7 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman uint16_t GetWriteClientArrayIndex(const WriteClient * const apWriteClient) const; + uint16_t GetReadHandlerArrayIndex(const ReadHandler * const apReadHandler) const; /** * The Magic number of this InteractionModelEngine, the magic number is set during Init() */ diff --git a/src/app/ReadHandler.h b/src/app/ReadHandler.h index be6451af897027..c020f73cb52089 100644 --- a/src/app/ReadHandler.h +++ b/src/app/ReadHandler.h @@ -146,6 +146,12 @@ class ReadHandler : public Messaging::ExchangeDelegate NodeId GetInitiatorNodeId() const { return mInitiatorNodeId; } FabricIndex GetAccessingFabricIndex() const { return mFabricIndex; } + void UnblockUrgentEventDelivery() + { + mHoldReport = false; + mDirty = true; + } + private: friend class TestReadInteraction; enum class HandlerState diff --git a/src/app/reporting/Engine.cpp b/src/app/reporting/Engine.cpp index 0d865c138b81c5..9a5e5aed607a57 100644 --- a/src/app/reporting/Engine.cpp +++ b/src/app/reporting/Engine.cpp @@ -571,6 +571,23 @@ void Engine::OnReportConfirm() ChipLogDetail(DataManagement, " OnReportConfirm: NumReports = %" PRIu32, mNumReportsInFlight); } +CHIP_ERROR Engine::ScheduleUrgentEventDelivery(ConcreteEventPath & aPath) +{ + for (auto & handler : InteractionModelEngine::GetInstance()->mReadHandlers) + { + for (auto clusterInfo = handler.GetEventClusterInfolist(); clusterInfo != nullptr; clusterInfo = clusterInfo->mpNext) + { + if (clusterInfo->IsEventPathSupersetOf(aPath)) + { + ChipLogProgress(DataManagement, " Unblock Urgent Event Delivery for readHandler[%d]", + InteractionModelEngine::GetInstance()->GetReadHandlerArrayIndex(&handler)); + handler.UnblockUrgentEventDelivery(); + } + } + } + return ScheduleRun(); +} + }; // namespace reporting } // namespace app } // namespace chip diff --git a/src/app/reporting/Engine.h b/src/app/reporting/Engine.h index 71890bd5900a26..a10d09190e7538 100644 --- a/src/app/reporting/Engine.h +++ b/src/app/reporting/Engine.h @@ -88,6 +88,13 @@ class Engine */ CHIP_ERROR SetDirty(ClusterInfo & aClusterInfo); + /** + * @brief + * Schedule the urgent event delivery + * + */ + CHIP_ERROR ScheduleUrgentEventDelivery(ConcreteEventPath & aPath); + private: friend class TestReportingEngine; /** diff --git a/src/app/tests/TestReadInteraction.cpp b/src/app/tests/TestReadInteraction.cpp index 40d0340048af34..fe6b10ee57660f 100644 --- a/src/app/tests/TestReadInteraction.cpp +++ b/src/app/tests/TestReadInteraction.cpp @@ -105,7 +105,7 @@ class TestEventGenerator : public chip::app::EventLoggingDelegate int32_t mStatus; }; -void GenerateEvents(nlTestSuite * apSuite, void * apContext) +void GenerateEvents(nlTestSuite * apSuite, void * apContext, bool aIsUrgent = false) { CHIP_ERROR err = CHIP_NO_ERROR; chip::EventNumber eid1, eid2; @@ -117,8 +117,12 @@ void GenerateEvents(nlTestSuite * apSuite, void * apContext) chip::app::EventOptions options2; TestEventGenerator testEventGenerator; - options1.mpEventSchema = &schema1; - options2.mpEventSchema = &schema2; + options1.mpEventSchema = &schema1; + options2.mpEventSchema = &schema2; + if (aIsUrgent) + { + options2.mUrgent = chip::app::EventOptions::Type::kUrgent; + } chip::app::EventManagement & logMgmt = chip::app::EventManagement::GetInstance(); testEventGenerator.SetStatus(0); err = logMgmt.LogEvent(&testEventGenerator, options1, eid1); @@ -974,8 +978,6 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a // Shouldn't have anything in the retransmit table when starting the test. NL_TEST_ASSERT(apSuite, rm->TestGetCountRetransTable() == 0); - GenerateEvents(apSuite, apContext); - MockInteractionModelApp delegate; auto * engine = chip::app::InteractionModelEngine::GetInstance(); err = engine->Init(&ctx.GetExchangeManager(), &delegate); @@ -1024,6 +1026,9 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a NL_TEST_ASSERT(apSuite, delegate.mNumAttributeResponse == 2); NL_TEST_ASSERT(apSuite, delegate.mNumSubscriptions == 1); + GenerateEvents(apSuite, apContext, true /*aIsUrgent*/); + NL_TEST_ASSERT(apSuite, delegate.mpReadHandler->mHoldReport == false); + NL_TEST_ASSERT(apSuite, delegate.mpReadHandler->mDirty == true); chip::app::ClusterInfo dirtyPath1; dirtyPath1.mClusterId = kTestClusterId; dirtyPath1.mEndpointId = kTestEndpointId;