Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021 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 <app/EventLoggingDelegate.h>
23 : #include <app/EventManagement.h>
24 : #include <app/data-model/Encode.h>
25 : #include <app/data-model/FabricScoped.h>
26 : #include <app/data-model/List.h> // So we can encode lists
27 : #include <lib/core/DataModelTypes.h>
28 :
29 : namespace chip {
30 : namespace app {
31 :
32 : template <typename T>
33 : class EventLogger : public EventLoggingDelegate
34 : {
35 : public:
36 42 : EventLogger(const T & aEventData) : mEventData(aEventData){};
37 62 : CHIP_ERROR WriteEvent(chip::TLV::TLVWriter & aWriter) final override
38 : {
39 62 : return DataModel::Encode(aWriter, TLV::ContextTag(EventDataIB::Tag::kData), mEventData);
40 : }
41 :
42 : private:
43 : const T & mEventData;
44 : };
45 :
46 : /**
47 : * @brief
48 : * Log an event via a EventLoggingDelegate, with options.
49 : *
50 : * The EventLoggingDelegate writes the event metadata and calls the `apDelegate`
51 : * with an TLV::TLVWriter reference so that the user code can emit
52 : * the event data directly into the event log. This form of event
53 : * logging minimizes memory consumption, as event data is serialized
54 : * directly into the target buffer. The event data MUST contain
55 : * context tags to be interpreted within the schema identified by
56 : * `ClusterID` and `EventId`.
57 : *
58 : * The consumer has to either lock the Matter stack lock or queue the event to
59 : * the Matter event queue when using LogEvent. This function is not safe to call
60 : * outside of the main Matter processing context.
61 : *
62 : * @param[in] aEventData The event cluster object
63 : * @param[in] aEndpoint The current cluster's Endpoint Id
64 : * @param[out] aEventNumber The event Number if the event was written to the
65 : * log, 0 otherwise. The Event number is expected to monotonically increase.
66 : *
67 : * @return CHIP_ERROR CHIP Error Code
68 : */
69 : template <typename T>
70 42 : CHIP_ERROR LogEvent(const T & aEventData, EndpointId aEndpoint, EventNumber & aEventNumber)
71 : {
72 42 : EventLogger<T> eventData(aEventData);
73 :
74 42 : EventOptions eventOptions;
75 42 : eventOptions.mPath = ConcreteEventPath(aEndpoint, aEventData.GetClusterId(), aEventData.GetEventId());
76 42 : eventOptions.mPriority = aEventData.GetPriorityLevel();
77 :
78 : if constexpr (DataModel::IsFabricScoped<T>::value)
79 : {
80 : eventOptions.mFabricIndex = aEventData.GetFabricIndex();
81 : // A fabric-sensitive event must be associated with a fabric to make sense.
82 : VerifyOrReturnError(eventOptions.mFabricIndex != kUndefinedFabricIndex, CHIP_ERROR_INVALID_FABRIC_INDEX);
83 : }
84 :
85 42 : return EventManagement::GetInstance().LogEvent(&eventData, eventOptions, aEventNumber);
86 42 : }
87 :
88 : } // namespace app
89 : } // namespace chip
|