|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <app/EventLoggingDelegate.h> |
| 20 | +#include <app/EventLoggingTypes.h> |
| 21 | +#include <app/MessageDef/EventDataIB.h> |
| 22 | +#include <app/data-model/Encode.h> |
| 23 | +#include <app/data-model/FabricScoped.h> |
| 24 | +#include <lib/core/CHIPError.h> |
| 25 | +#include <lib/support/logging/CHIPLogging.h> |
| 26 | + |
| 27 | +#include <type_traits> |
| 28 | + |
| 29 | +namespace chip { |
| 30 | +namespace app { |
| 31 | +namespace InteractionModel { |
| 32 | + |
| 33 | +namespace internal { |
| 34 | +template <typename T> |
| 35 | +class SimpleEventLoggingDelegate : public EventLoggingDelegate |
| 36 | +{ |
| 37 | +public: |
| 38 | + SimpleEventLoggingDelegate(const T & aEventData) : mEventData(aEventData){}; |
| 39 | + CHIP_ERROR WriteEvent(chip::TLV::TLVWriter & aWriter) final override |
| 40 | + { |
| 41 | + return DataModel::Encode(aWriter, TLV::ContextTag(EventDataIB::Tag::kData), mEventData); |
| 42 | + } |
| 43 | + |
| 44 | +private: |
| 45 | + const T & mEventData; |
| 46 | +}; |
| 47 | + |
| 48 | +template <typename E, typename T, std::enable_if_t<DataModel::IsFabricScoped<T>::value, bool> = true> |
| 49 | +EventNumber GenerateEvent(E & emittor, const T & aEventData, EndpointId aEndpoint) |
| 50 | +{ |
| 51 | + internal::SimpleEventLoggingDelegate<T> eventData(aEventData); |
| 52 | + ConcreteEventPath path(aEndpoint, aEventData.GetClusterId(), aEventData.GetEventId()); |
| 53 | + EventOptions eventOptions; |
| 54 | + eventOptions.mPath = path; |
| 55 | + eventOptions.mPriority = aEventData.GetPriorityLevel(); |
| 56 | + eventOptions.mFabricIndex = aEventData.GetFabricIndex(); |
| 57 | + |
| 58 | + // this skips logging the event if it's fabric-scoped but no fabric association exists yet. |
| 59 | + |
| 60 | + if (eventOptions.mFabricIndex == kUndefinedFabricIndex) |
| 61 | + { |
| 62 | + ChipLogError(EventLogging, "Event encode failure: no fabric index for fabric scoped event"); |
| 63 | + return kInvalidEventId; |
| 64 | + } |
| 65 | + |
| 66 | + // |
| 67 | + // Unlike attributes which have a different 'EncodeForRead' for fabric-scoped structs, |
| 68 | + // fabric-sensitive events don't require that since the actual omission of the event in its entirety |
| 69 | + // happens within the event management framework itself at the time of access. |
| 70 | + // |
| 71 | + // The 'mFabricIndex' field in the event options above is encoded out-of-band alongside the event payload |
| 72 | + // and used to match against the accessing fabric. |
| 73 | + // |
| 74 | + EventNumber eventNumber; |
| 75 | + CHIP_ERROR err = emittor.GenerateEvent(&eventData, eventOptions, eventNumber); |
| 76 | + if (err != CHIP_NO_ERROR) |
| 77 | + { |
| 78 | + ChipLogError(EventLogging, "Failed to log event: %" CHIP_ERROR_FORMAT, err.Format()); |
| 79 | + return kInvalidEventId; |
| 80 | + } |
| 81 | + |
| 82 | + return eventNumber; |
| 83 | +} |
| 84 | + |
| 85 | +template <typename E, typename T, std::enable_if_t<!DataModel::IsFabricScoped<T>::value, bool> = true> |
| 86 | +EventNumber GenerateEvent(E & emittor, const T & aEventData, EndpointId endpointId) |
| 87 | +{ |
| 88 | + internal::SimpleEventLoggingDelegate<T> eventData(aEventData); |
| 89 | + ConcreteEventPath path(endpointId, aEventData.GetClusterId(), aEventData.GetEventId()); |
| 90 | + EventOptions eventOptions; |
| 91 | + eventOptions.mPath = path; |
| 92 | + eventOptions.mPriority = aEventData.GetPriorityLevel(); |
| 93 | + EventNumber eventNumber; |
| 94 | + CHIP_ERROR err = emittor.GenerateEvent(&eventData, eventOptions, eventNumber); |
| 95 | + if (err != CHIP_NO_ERROR) |
| 96 | + { |
| 97 | + ChipLogError(EventLogging, "Failed to log event: %" CHIP_ERROR_FORMAT, err.Format()); |
| 98 | + return kInvalidEventId; |
| 99 | + } |
| 100 | + |
| 101 | + return eventNumber; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace internal |
| 105 | + |
| 106 | +class Events |
| 107 | +{ |
| 108 | +public: |
| 109 | + virtual ~Events() = default; |
| 110 | + |
| 111 | + /// Generates the given event. |
| 112 | + /// |
| 113 | + /// Events are generally expected to be sent to subscribed clients and also |
| 114 | + /// be available for read later until they get overwritten by new events |
| 115 | + /// that are being generated. |
| 116 | + virtual CHIP_ERROR GenerateEvent(EventLoggingDelegate * eventContentWriter, const EventOptions & options, |
| 117 | + EventNumber & generatedEventNumber) = 0; |
| 118 | + |
| 119 | + // Convenience methods for event logging using cluster-object structures |
| 120 | + // On error, these log and return kInvalidEventId |
| 121 | + template <typename T> |
| 122 | + EventNumber GenerateEvent(const T & eventData, EndpointId endpointId) |
| 123 | + { |
| 124 | + return internal::GenerateEvent(*this, eventData, endpointId); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +} // namespace InteractionModel |
| 129 | +} // namespace app |
| 130 | +} // namespace chip |
0 commit comments