Skip to content

Commit 48e8a0e

Browse files
Inject event management into report engine (#36831)
* Inject event management into report engine * Restyled by whitespace * Restyled by clang-format * add event scheduler file * add missing includes * Rename it to EventReporter * Restyled by clang-format * Restyled by gn * Modify comments and fix typo * Fix some comments * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 27ca6ec commit 48e8a0e

8 files changed

+104
-27
lines changed

src/app/BUILD.gn

+6
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ source_set("test-event-trigger") {
150150
sources = [ "TestEventTriggerDelegate.h" ]
151151
}
152152

153+
source_set("event-reporter") {
154+
sources = [ "EventReporter.h" ]
155+
}
156+
153157
# interaction-model is a static-library because it currently requires global functions (app/util/...) that are stubbed in different test files that depend on the app static_library
154158
# which in tern depens on the interaction-model.
155159
# Using source_set prevents the unit test to build correctly.
@@ -211,6 +215,7 @@ static_library("interaction-model") {
211215
":app_config",
212216
":command-handler-impl",
213217
":constants",
218+
":event-reporter",
214219
":paths",
215220
":subscription-info-provider",
216221
"${chip_root}/src/app/MessageDef",
@@ -456,6 +461,7 @@ static_library("app") {
456461
":app_config",
457462
":attribute-access",
458463
":constants",
464+
":event-reporter",
459465
":global-attributes",
460466
":interaction-model",
461467
"${chip_root}/src/app/data-model",

src/app/EventManagement.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct CopyAndAdjustDeltaTimeContext
8383
void EventManagement::Init(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers,
8484
CircularEventBuffer * apCircularEventBuffer, const LogStorageResources * const apLogStorageResources,
8585
MonotonicallyIncreasingCounter<EventNumber> * apEventNumberCounter,
86-
System::Clock::Milliseconds64 aMonotonicStartupTime)
86+
System::Clock::Milliseconds64 aMonotonicStartupTime, EventReporter * apEventReporter)
8787
{
8888
CircularEventBuffer * current = nullptr;
8989
CircularEventBuffer * prev = nullptr;
@@ -124,6 +124,16 @@ void EventManagement::Init(Messaging::ExchangeManager * apExchangeManager, uint3
124124
mBytesWritten = 0;
125125

126126
mMonotonicStartupTime = aMonotonicStartupTime;
127+
128+
// TODO(#36890): Should remove using the global instance and rely only on passed in variable.
129+
if (apEventReporter == nullptr)
130+
{
131+
mpEventReporter = &InteractionModelEngine::GetInstance()->GetReportingEngine();
132+
}
133+
else
134+
{
135+
mpEventReporter = apEventReporter;
136+
}
127137
}
128138

129139
CHIP_ERROR EventManagement::CopyToNextBuffer(CircularEventBuffer * apEventBuffer)
@@ -490,7 +500,7 @@ CHIP_ERROR EventManagement::LogEventPrivate(EventLoggingDelegate * apDelegate, c
490500
opts.mTimestamp.mType == Timestamp::Type::kSystem ? "Sys" : "Epoch", ChipLogValueX64(opts.mTimestamp.mValue));
491501
#endif // CHIP_CONFIG_EVENT_LOGGING_VERBOSE_DEBUG_LOGS
492502

493-
err = InteractionModelEngine::GetInstance()->GetReportingEngine().ScheduleEventDelivery(opts.mPath, mBytesWritten);
503+
err = mpEventReporter->NewEventGenerated(opts.mPath, mBytesWritten);
494504
}
495505

496506
return err;

src/app/EventManagement.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "EventLoggingDelegate.h"
3030
#include <access/SubjectDescriptor.h>
3131
#include <app/EventLoggingTypes.h>
32+
#include <app/EventReporter.h>
3233
#include <app/MessageDef/EventDataIB.h>
3334
#include <app/MessageDef/StatusIB.h>
3435
#include <app/data-model-provider/EventsGenerator.h>
@@ -225,11 +226,13 @@ class EventManagement : public DataModel::EventsGenerator
225226
* time 0" for cases when we use
226227
* system-time event timestamps.
227228
*
229+
* @param[in] apEventReporter Event reporter to be notified when events are generated.
230+
*
228231
*/
229232
void Init(Messaging::ExchangeManager * apExchangeManager, uint32_t aNumBuffers, CircularEventBuffer * apCircularEventBuffer,
230233
const LogStorageResources * const apLogStorageResources,
231234
MonotonicallyIncreasingCounter<EventNumber> * apEventNumberCounter,
232-
System::Clock::Milliseconds64 aMonotonicStartupTime);
235+
System::Clock::Milliseconds64 aMonotonicStartupTime, EventReporter * apEventReporter = nullptr);
233236

234237
static EventManagement & GetInstance();
235238

@@ -563,6 +566,8 @@ class EventManagement : public DataModel::EventsGenerator
563566
Timestamp mLastEventTimestamp; ///< The timestamp of the last event in this buffer
564567

565568
System::Clock::Milliseconds64 mMonotonicStartupTime;
569+
570+
EventReporter * mpEventReporter = nullptr;
566571
};
567572

568573
} // namespace app

src/app/EventReporter.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <app/ConcreteEventPath.h>
22+
#include <lib/core/CHIPError.h>
23+
24+
namespace chip {
25+
namespace app {
26+
27+
/**
28+
* Interface that EventManagement can use to notify when events are generated and may need reporting.
29+
*
30+
*/
31+
class EventReporter
32+
{
33+
public:
34+
virtual ~EventReporter() = default;
35+
36+
/**
37+
* Notify that an event was generated.
38+
*
39+
* @param[in] aPath The path that identifies the kind of event that was generated.
40+
* @param[in] aBytesConsumed The number of bytes needed to store the event in EventManagement.
41+
*/
42+
CHIP_ERROR virtual NewEventGenerated(ConcreteEventPath & aPath, uint32_t aBytesConsumed) = 0;
43+
};
44+
45+
} // namespace app
46+
} // namespace chip

src/app/InteractionModelEngine.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ InteractionModelEngine * InteractionModelEngine::GetInstance()
149149

150150
CHIP_ERROR InteractionModelEngine::Init(Messaging::ExchangeManager * apExchangeMgr, FabricTable * apFabricTable,
151151
reporting::ReportScheduler * reportScheduler, CASESessionManager * apCASESessionMgr,
152-
SubscriptionResumptionStorage * subscriptionResumptionStorage)
152+
SubscriptionResumptionStorage * subscriptionResumptionStorage,
153+
EventManagement * eventManagement)
153154
{
154155
VerifyOrReturnError(apFabricTable != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
155156
VerifyOrReturnError(apExchangeMgr != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
@@ -165,7 +166,7 @@ CHIP_ERROR InteractionModelEngine::Init(Messaging::ExchangeManager * apExchangeM
165166
ReturnErrorOnFailure(mpFabricTable->AddFabricDelegate(this));
166167
ReturnErrorOnFailure(mpExchangeMgr->RegisterUnsolicitedMessageHandlerForProtocol(Protocols::InteractionModel::Id, this));
167168

168-
mReportingEngine.Init();
169+
mReportingEngine.Init((eventManagement != nullptr) ? eventManagement : &EventManagement::GetInstance());
169170

170171
StatusIB::RegisterErrorFormatter();
171172

src/app/InteractionModelEngine.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
126126
* @param[in] apExchangeMgr A pointer to the ExchangeManager object.
127127
* @param[in] apFabricTable A pointer to the FabricTable object.
128128
* @param[in] apCASESessionMgr An optional pointer to a CASESessionManager (used for re-subscriptions).
129+
* @parma[in] eventManagement An optional pointer to a EventManagement. If null, the global instance will be used.
129130
*
130131
*/
131132
CHIP_ERROR Init(Messaging::ExchangeManager * apExchangeMgr, FabricTable * apFabricTable,
132133
reporting::ReportScheduler * reportScheduler, CASESessionManager * apCASESessionMgr = nullptr,
133-
SubscriptionResumptionStorage * subscriptionResumptionStorage = nullptr);
134+
SubscriptionResumptionStorage * subscriptionResumptionStorage = nullptr,
135+
EventManagement * eventManagement = nullptr);
134136

135137
void Shutdown();
136138

src/app/reporting/Engine.cpp

+15-12
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,13 @@ bool IsClusterDataVersionEqualTo(DataModel::Provider * dataModel, const Concrete
221221

222222
Engine::Engine(InteractionModelEngine * apImEngine) : mpImEngine(apImEngine) {}
223223

224-
CHIP_ERROR Engine::Init()
224+
CHIP_ERROR Engine::Init(EventManagement * apEventManagement)
225225
{
226+
VerifyOrReturnError(apEventManagement != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
226227
mNumReportsInFlight = 0;
227228
mCurReadHandlerIdx = 0;
229+
mpEventManagement = apEventManagement;
230+
228231
return CHIP_NO_ERROR;
229232
}
230233

@@ -560,20 +563,20 @@ CHIP_ERROR Engine::BuildSingleReportDataEventReports(ReportDataMessage::Builder
560563
size_t eventCount = 0;
561564
bool hasEncodedStatus = false;
562565
TLV::TLVWriter backup;
563-
bool eventClean = true;
564-
auto & eventMin = apReadHandler->GetEventMin();
565-
EventManagement & eventManager = EventManagement::GetInstance();
566-
bool hasMoreChunks = false;
566+
bool eventClean = true;
567+
auto & eventMin = apReadHandler->GetEventMin();
568+
bool hasMoreChunks = false;
567569

568570
aReportDataBuilder.Checkpoint(backup);
569571

570572
VerifyOrExit(apReadHandler->GetEventPathList() != nullptr, );
571573

572-
// If the eventManager is not valid or has not been initialized,
574+
// If the mpEventManagement is not valid or has not been initialized,
573575
// skip the rest of processing
574-
VerifyOrExit(eventManager.IsValid(), ChipLogError(DataManagement, "EventManagement has not yet initialized"));
576+
VerifyOrExit(mpEventManagement != nullptr && mpEventManagement->IsValid(),
577+
ChipLogError(DataManagement, "EventManagement has not yet initialized"));
575578

576-
eventClean = apReadHandler->CheckEventClean(eventManager);
579+
eventClean = apReadHandler->CheckEventClean(*mpEventManagement);
577580

578581
// proceed only if there are new events.
579582
if (eventClean)
@@ -593,8 +596,8 @@ CHIP_ERROR Engine::BuildSingleReportDataEventReports(ReportDataMessage::Builder
593596
err = CheckAccessDeniedEventPaths(*(eventReportIBs.GetWriter()), hasEncodedStatus, apReadHandler);
594597
SuccessOrExit(err);
595598

596-
err = eventManager.FetchEventsSince(*(eventReportIBs.GetWriter()), apReadHandler->GetEventPathList(), eventMin, eventCount,
597-
apReadHandler->GetSubjectDescriptor());
599+
err = mpEventManagement->FetchEventsSince(*(eventReportIBs.GetWriter()), apReadHandler->GetEventPathList(), eventMin,
600+
eventCount, apReadHandler->GetSubjectDescriptor());
598601

599602
if ((err == CHIP_END_OF_TLV) || (err == CHIP_ERROR_TLV_UNDERRUN) || (err == CHIP_NO_ERROR))
600603
{
@@ -1128,7 +1131,7 @@ CHIP_ERROR Engine::ScheduleBufferPressureEventDelivery(uint32_t aBytesWritten)
11281131
return CHIP_NO_ERROR;
11291132
}
11301133

1131-
CHIP_ERROR Engine::ScheduleEventDelivery(ConcreteEventPath & aPath, uint32_t aBytesWritten)
1134+
CHIP_ERROR Engine::NewEventGenerated(ConcreteEventPath & aPath, uint32_t aBytesConsumed)
11321135
{
11331136
// If we literally have no read handlers right now that care about any events,
11341137
// we don't need to call schedule run for event.
@@ -1166,7 +1169,7 @@ CHIP_ERROR Engine::ScheduleEventDelivery(ConcreteEventPath & aPath, uint32_t aBy
11661169
return CHIP_NO_ERROR;
11671170
}
11681171

1169-
return ScheduleBufferPressureEventDelivery(aBytesWritten);
1172+
return ScheduleBufferPressureEventDelivery(aBytesConsumed);
11701173
}
11711174

11721175
void Engine::ScheduleUrgentEventDeliverySync(Optional<FabricIndex> fabricIndex)

src/app/reporting/Engine.h

+13-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#pragma once
2626

2727
#include <access/AccessControl.h>
28+
#include <app/EventReporter.h>
2829
#include <app/MessageDef/ReportDataMessage.h>
2930
#include <app/ReadHandler.h>
3031
#include <app/data-model-provider/ProviderChangeListener.h>
@@ -55,7 +56,7 @@ namespace reporting {
5556
* At its core, it tries to gather and pack as much relevant attributes changes and/or events as possible into a report
5657
* message before sending that to the reader. It continues to do so until it has no more work to do.
5758
*/
58-
class Engine : public DataModel::ProviderChangeListener
59+
class Engine : public DataModel::ProviderChangeListener, public EventReporter
5960
{
6061
public:
6162
/**
@@ -66,10 +67,12 @@ class Engine : public DataModel::ProviderChangeListener
6667
/**
6768
* Initializes the reporting engine. Should only be called once.
6869
*
70+
* @param[in] A pointer to EventManagement, should not be a nullptr.
71+
*
6972
* @retval #CHIP_NO_ERROR On success.
7073
* @retval other Was unable to retrieve data and write it into the writer.
7174
*/
72-
CHIP_ERROR Init();
75+
CHIP_ERROR Init(EventManagement * apEventManagement);
7376

7477
void Shutdown();
7578

@@ -96,13 +99,6 @@ class Engine : public DataModel::ProviderChangeListener
9699
*/
97100
CHIP_ERROR SetDirty(const AttributePathParams & aAttributePathParams);
98101

99-
/**
100-
* @brief
101-
* Schedule the event delivery
102-
*
103-
*/
104-
CHIP_ERROR ScheduleEventDelivery(ConcreteEventPath & aPath, uint32_t aBytesWritten);
105-
106102
/*
107103
* Resets the tracker that tracks the currently serviced read handler.
108104
* apReadHandler can be non-null to indicate that the reset is due to a
@@ -182,6 +178,12 @@ class Engine : public DataModel::ProviderChangeListener
182178
bool IsClusterDataVersionMatch(const SingleLinkedListNode<DataVersionFilter> * aDataVersionFilterList,
183179
const ConcreteReadAttributePath & aPath);
184180

181+
/**
182+
* EventReporter implementation.
183+
*
184+
*/
185+
CHIP_ERROR NewEventGenerated(ConcreteEventPath & aPath, uint32_t aBytesConsumed) override;
186+
185187
/**
186188
* Send Report via ReadHandler
187189
*
@@ -287,6 +289,8 @@ class Engine : public DataModel::ProviderChangeListener
287289
#endif
288290

289291
InteractionModelEngine * mpImEngine = nullptr;
292+
293+
EventManagement * mpEventManagement = nullptr;
290294
};
291295

292296
}; // namespace reporting

0 commit comments

Comments
 (0)