Line data Source code
1 : /*
2 : * Copyright (c) 2025 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/data-model-provider/EventsGenerator.h>
22 : #include <app/data-model/Decode.h>
23 :
24 : namespace chip {
25 : namespace Test {
26 :
27 : /// Keeps the "last event" in-memory to allow tests to validate
28 : /// that event writing and encoding worked.
29 : ///
30 : /// Provides the ability to get last event information as well as
31 : /// to decode the last event contents for inspection.
32 : class LogOnlyEvents : public app::DataModel::EventsGenerator
33 : {
34 : public:
35 0 : CHIP_ERROR GenerateEvent(app::EventLoggingDelegate * eventContentWriter, const app::EventOptions & options,
36 : EventNumber & generatedEventNumber) override
37 : {
38 0 : TLV::TLVWriter writer;
39 : TLV::TLVType outerType;
40 0 : writer.Init(mLastEventEncodeBuffer);
41 :
42 0 : ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outerType));
43 0 : ReturnErrorOnFailure(eventContentWriter->WriteEvent(writer));
44 0 : ReturnErrorOnFailure(writer.EndContainer(outerType));
45 0 : ReturnErrorOnFailure(writer.Finalize());
46 0 : mLastEncodedSpan = ByteSpan(mLastEventEncodeBuffer, writer.GetLengthWritten());
47 :
48 0 : mLastOptions = options;
49 0 : generatedEventNumber = ++mCurrentEventNumber;
50 :
51 0 : return CHIP_NO_ERROR;
52 : }
53 :
54 : [[nodiscard]] EventNumber CurrentEventNumber() const { return mCurrentEventNumber; }
55 : [[nodiscard]] const app::EventOptions & LastOptions() const { return mLastOptions; }
56 : [[nodiscard]] ByteSpan LastWrittenEvent() const { return mLastEncodedSpan; }
57 :
58 : // This relies on the default encoding of events which uses
59 : // DataModel::Encode on a EventDataIB::Tag::kData
60 : template <typename T>
61 : CHIP_ERROR DecodeLastEvent(T & dest)
62 : {
63 : // attempt to decode the last encoded event
64 : TLV::TLVReader reader;
65 : TLV::TLVType outerType;
66 :
67 : reader.Init(LastWrittenEvent());
68 :
69 : ReturnErrorOnFailure(reader.Next());
70 : ReturnErrorOnFailure(reader.EnterContainer(outerType));
71 :
72 : ReturnErrorOnFailure(reader.Next()); // MUST be positioned on the first element
73 : ReturnErrorOnFailure(app::DataModel::Decode(reader, dest));
74 :
75 : ReturnErrorOnFailure(reader.ExitContainer(outerType));
76 :
77 : return CHIP_NO_ERROR;
78 : }
79 :
80 : private:
81 : EventNumber mCurrentEventNumber = 0;
82 : app::EventOptions mLastOptions;
83 : uint8_t mLastEventEncodeBuffer[128];
84 : ByteSpan mLastEncodedSpan;
85 : };
86 :
87 : } // namespace Test
88 : } // namespace chip
|