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 1580 : CHIP_ERROR EventReportIB::Parser::PrettyPrint() const
33 : {
34 1580 : CHIP_ERROR err = CHIP_NO_ERROR;
35 1580 : TLV::TLVReader reader;
36 :
37 1580 : PRETTY_PRINT("EventReportIB =");
38 1580 : PRETTY_PRINT("{");
39 :
40 : // make a copy of the reader
41 1580 : reader.Init(mReader);
42 :
43 3160 : while (CHIP_NO_ERROR == (err = reader.Next()))
44 : {
45 1580 : if (!TLV::IsContextTag(reader.GetTag()))
46 : {
47 0 : continue;
48 : }
49 1580 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
50 1580 : switch (tagNum)
51 : {
52 2 : case to_underlying(Tag::kEventStatus): {
53 2 : EventStatusIB::Parser eventStatus;
54 2 : ReturnErrorOnFailure(eventStatus.Init(reader));
55 :
56 2 : PRETTY_PRINT_INCDEPTH();
57 2 : ReturnErrorOnFailure(eventStatus.PrettyPrint());
58 2 : PRETTY_PRINT_DECDEPTH();
59 : }
60 2 : break;
61 1578 : case to_underlying(Tag::kEventData): {
62 1578 : EventDataIB::Parser eventData;
63 1578 : ReturnErrorOnFailure(eventData.Init(reader));
64 :
65 1578 : PRETTY_PRINT_INCDEPTH();
66 1578 : ReturnErrorOnFailure(eventData.PrettyPrint());
67 1578 : PRETTY_PRINT_DECDEPTH();
68 : }
69 1578 : break;
70 0 : default:
71 0 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
72 0 : break;
73 : }
74 : }
75 :
76 1580 : PRETTY_PRINT("},");
77 1580 : PRETTY_PRINT_BLANK_LINE();
78 :
79 1580 : if (CHIP_END_OF_TLV == err)
80 : {
81 1580 : err = CHIP_NO_ERROR;
82 : }
83 :
84 1580 : ReturnErrorOnFailure(err);
85 1580 : return reader.ExitContainer(mOuterContainerType);
86 : }
87 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
88 :
89 2 : CHIP_ERROR EventReportIB::Parser::GetEventStatus(EventStatusIB::Parser * const apEventStatus) const
90 : {
91 2 : TLV::TLVReader reader;
92 2 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kEventStatus), reader));
93 2 : return apEventStatus->Init(reader);
94 : }
95 :
96 1578 : CHIP_ERROR EventReportIB::Parser::GetEventData(EventDataIB::Parser * const apEventData) const
97 : {
98 1578 : TLV::TLVReader reader;
99 1578 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kEventData), reader));
100 1576 : return apEventData->Init(reader);
101 : }
102 :
103 2 : EventStatusIB::Builder & EventReportIB::Builder::CreateEventStatus()
104 : {
105 2 : if (mError == CHIP_NO_ERROR)
106 : {
107 2 : mError = mEventStatus.Init(mpWriter, to_underlying(Tag::kEventStatus));
108 : }
109 2 : return mEventStatus;
110 : }
111 :
112 1327 : EventDataIB::Builder & EventReportIB::Builder::CreateEventData()
113 : {
114 1327 : if (mError == CHIP_NO_ERROR)
115 : {
116 1327 : mError = mEventData.Init(mpWriter, to_underlying(Tag::kEventData));
117 : }
118 1327 : return mEventData;
119 : }
120 :
121 1329 : CHIP_ERROR EventReportIB::Builder::EndOfEventReportIB()
122 : {
123 1329 : EndOfContainer();
124 1329 : return GetError();
125 : }
126 :
127 2 : CHIP_ERROR EventReportIB::ConstructEventStatusIB(TLV::TLVWriter & aWriter, const ConcreteEventPath & aEvent, StatusIB aStatus)
128 : {
129 2 : Builder eventReportIBBuilder;
130 2 : ReturnErrorOnFailure(eventReportIBBuilder.Init(&aWriter));
131 2 : EventStatusIB::Builder & eventStatusIBBuilder = eventReportIBBuilder.CreateEventStatus();
132 2 : ReturnErrorOnFailure(eventReportIBBuilder.GetError());
133 2 : EventPathIB::Builder & eventPathIBBuilder = eventStatusIBBuilder.CreatePath();
134 2 : ReturnErrorOnFailure(eventStatusIBBuilder.GetError());
135 2 : ReturnErrorOnFailure(
136 : eventPathIBBuilder.Endpoint(aEvent.mEndpointId).Cluster(aEvent.mClusterId).Event(aEvent.mEventId).EndOfEventPathIB());
137 :
138 2 : ReturnErrorOnFailure(eventStatusIBBuilder.CreateErrorStatus().EncodeStatusIB(aStatus).GetError());
139 :
140 2 : ReturnErrorOnFailure(eventStatusIBBuilder.EndOfEventStatusIB());
141 2 : ReturnErrorOnFailure(eventReportIBBuilder.EndOfEventReportIB());
142 2 : ReturnErrorOnFailure(aWriter.Finalize());
143 2 : return CHIP_NO_ERROR;
144 : }
145 : } // namespace app
146 : } // namespace chip
|