Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : */
16 :
17 : #include "EventReportIB.h"
18 : #include "EventDataIB.h"
19 : #include "MessageDefHelper.h"
20 : #include "StructBuilder.h"
21 : #include "StructParser.h"
22 :
23 : #include <inttypes.h>
24 : #include <stdarg.h>
25 : #include <stdio.h>
26 :
27 : #include <app/AppConfig.h>
28 :
29 : namespace chip {
30 : namespace app {
31 : #if CHIP_CONFIG_IM_PRETTY_PRINT
32 1585 : CHIP_ERROR EventReportIB::Parser::PrettyPrint() const
33 : {
34 1585 : CHIP_ERROR err = CHIP_NO_ERROR;
35 1585 : TLV::TLVReader reader;
36 :
37 1585 : PRETTY_PRINT("EventReportIB =");
38 1585 : PRETTY_PRINT("{");
39 :
40 : // make a copy of the reader
41 1585 : reader.Init(mReader);
42 :
43 3170 : while (CHIP_NO_ERROR == (err = reader.Next()))
44 : {
45 1585 : if (!TLV::IsContextTag(reader.GetTag()))
46 : {
47 0 : continue;
48 : }
49 1585 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
50 1585 : switch (tagNum)
51 : {
52 3 : case to_underlying(Tag::kEventStatus): {
53 3 : EventStatusIB::Parser eventStatus;
54 3 : ReturnErrorOnFailure(eventStatus.Init(reader));
55 :
56 3 : PRETTY_PRINT_INCDEPTH();
57 3 : ReturnErrorOnFailure(eventStatus.PrettyPrint());
58 3 : PRETTY_PRINT_DECDEPTH();
59 : }
60 3 : break;
61 1582 : case to_underlying(Tag::kEventData): {
62 1582 : EventDataIB::Parser eventData;
63 1582 : ReturnErrorOnFailure(eventData.Init(reader));
64 :
65 1582 : PRETTY_PRINT_INCDEPTH();
66 1582 : ReturnErrorOnFailure(eventData.PrettyPrint());
67 1582 : PRETTY_PRINT_DECDEPTH();
68 : }
69 1582 : break;
70 0 : default:
71 0 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
72 0 : break;
73 : }
74 : }
75 :
76 1585 : PRETTY_PRINT("},");
77 1585 : PRETTY_PRINT_BLANK_LINE();
78 :
79 1585 : if (CHIP_END_OF_TLV == err)
80 : {
81 1585 : err = CHIP_NO_ERROR;
82 : }
83 :
84 1585 : ReturnErrorOnFailure(err);
85 1585 : return reader.ExitContainer(mOuterContainerType);
86 : }
87 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
88 :
89 3 : CHIP_ERROR EventReportIB::Parser::GetEventStatus(EventStatusIB::Parser * const apEventStatus) const
90 : {
91 3 : TLV::TLVReader reader;
92 3 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kEventStatus), reader));
93 3 : return apEventStatus->Init(reader);
94 : }
95 :
96 1583 : CHIP_ERROR EventReportIB::Parser::GetEventData(EventDataIB::Parser * const apEventData) const
97 : {
98 1583 : TLV::TLVReader reader;
99 1583 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kEventData), reader));
100 1580 : return apEventData->Init(reader);
101 : }
102 :
103 3 : EventStatusIB::Builder & EventReportIB::Builder::CreateEventStatus()
104 : {
105 3 : if (mError == CHIP_NO_ERROR)
106 : {
107 3 : mError = mEventStatus.Init(mpWriter, to_underlying(Tag::kEventStatus));
108 : }
109 3 : return mEventStatus;
110 : }
111 :
112 1333 : EventDataIB::Builder & EventReportIB::Builder::CreateEventData()
113 : {
114 1333 : if (mError == CHIP_NO_ERROR)
115 : {
116 1333 : mError = mEventData.Init(mpWriter, to_underlying(Tag::kEventData));
117 : }
118 1333 : return mEventData;
119 : }
120 :
121 1336 : CHIP_ERROR EventReportIB::Builder::EndOfEventReportIB()
122 : {
123 1336 : EndOfContainer();
124 1336 : return GetError();
125 : }
126 :
127 3 : CHIP_ERROR EventReportIB::ConstructEventStatusIB(TLV::TLVWriter & aWriter, const ConcreteEventPath & aEvent, StatusIB aStatus)
128 : {
129 3 : Builder eventReportIBBuilder;
130 3 : ReturnErrorOnFailure(eventReportIBBuilder.Init(&aWriter));
131 3 : EventStatusIB::Builder & eventStatusIBBuilder = eventReportIBBuilder.CreateEventStatus();
132 3 : ReturnErrorOnFailure(eventReportIBBuilder.GetError());
133 3 : EventPathIB::Builder & eventPathIBBuilder = eventStatusIBBuilder.CreatePath();
134 3 : ReturnErrorOnFailure(eventStatusIBBuilder.GetError());
135 3 : ReturnErrorOnFailure(
136 : eventPathIBBuilder.Endpoint(aEvent.mEndpointId).Cluster(aEvent.mClusterId).Event(aEvent.mEventId).EndOfEventPathIB());
137 :
138 3 : ReturnErrorOnFailure(eventStatusIBBuilder.CreateErrorStatus().EncodeStatusIB(aStatus).GetError());
139 :
140 3 : ReturnErrorOnFailure(eventStatusIBBuilder.EndOfEventStatusIB());
141 3 : ReturnErrorOnFailure(eventReportIBBuilder.EndOfEventReportIB());
142 3 : ReturnErrorOnFailure(aWriter.Finalize());
143 3 : return CHIP_NO_ERROR;
144 : }
145 : } // namespace app
146 : } // namespace chip
|