Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2018 Google LLC.
5 : * Copyright (c) 2016-2017 Nest Labs, Inc.
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 : * @file
20 : * This file defines ReportDataMessage parser and builder in CHIP interaction model
21 : *
22 : */
23 :
24 : #include "ReportDataMessage.h"
25 :
26 : #include "MessageDefHelper.h"
27 :
28 : #include <inttypes.h>
29 : #include <stdarg.h>
30 : #include <stdio.h>
31 :
32 : #include <app/AppConfig.h>
33 :
34 : using namespace chip;
35 :
36 : namespace chip {
37 : namespace app {
38 : #if CHIP_CONFIG_IM_PRETTY_PRINT
39 1939 : CHIP_ERROR ReportDataMessage::Parser::PrettyPrint() const
40 : {
41 1939 : CHIP_ERROR err = CHIP_NO_ERROR;
42 1939 : TLV::TLVReader reader;
43 1939 : AttributeReportIBs::Parser attributeReportIBs;
44 1939 : EventReportIBs::Parser eventReportIBs;
45 :
46 1939 : PRETTY_PRINT("ReportDataMessage =");
47 1939 : PRETTY_PRINT("{");
48 :
49 : // make a copy of the reader
50 1939 : reader.Init(mReader);
51 :
52 15656 : while (CHIP_NO_ERROR == (err = reader.Next()))
53 : {
54 5889 : if (!TLV::IsContextTag(reader.GetTag()))
55 : {
56 0 : continue;
57 : }
58 5889 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
59 5889 : switch (tagNum)
60 : {
61 705 : case to_underlying(Tag::kSuppressResponse):
62 705 : VerifyOrReturnError(TLV::kTLVType_Boolean == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
63 : #if CHIP_DETAIL_LOGGING
64 : {
65 : bool SuppressResponse;
66 705 : ReturnErrorOnFailure(reader.Get(SuppressResponse));
67 705 : PRETTY_PRINT("\tSuppressResponse = %s, ", SuppressResponse ? "true" : "false");
68 : }
69 : #endif // CHIP_DETAIL_LOGGING
70 705 : break;
71 431 : case to_underlying(Tag::kSubscriptionId):
72 431 : VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
73 : #if CHIP_DETAIL_LOGGING
74 : {
75 : SubscriptionId subscriptionId;
76 431 : ReturnErrorOnFailure(reader.Get(subscriptionId));
77 431 : PRETTY_PRINT("\tSubscriptionId = 0x%" PRIx32 ",", subscriptionId);
78 : }
79 : #endif // CHIP_DETAIL_LOGGING
80 431 : break;
81 1301 : case to_underlying(Tag::kAttributeReportIBs):
82 1301 : VerifyOrReturnError(TLV::kTLVType_Array == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
83 : #if CHIP_DETAIL_LOGGING
84 : {
85 1301 : TEMPORARY_RETURN_IGNORED attributeReportIBs.Init(reader);
86 :
87 1301 : PRETTY_PRINT_INCDEPTH();
88 1301 : ReturnErrorOnFailure(attributeReportIBs.PrettyPrint());
89 1301 : PRETTY_PRINT_DECDEPTH();
90 : }
91 : #endif // CHIP_DETAIL_LOGGING
92 1301 : break;
93 642 : case to_underlying(Tag::kEventReports):
94 642 : VerifyOrReturnError(TLV::kTLVType_Array == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
95 : #if CHIP_DETAIL_LOGGING
96 : {
97 642 : TEMPORARY_RETURN_IGNORED eventReportIBs.Init(reader);
98 :
99 642 : PRETTY_PRINT_INCDEPTH();
100 642 : ReturnErrorOnFailure(eventReportIBs.PrettyPrint());
101 642 : PRETTY_PRINT_DECDEPTH();
102 : }
103 : #endif // CHIP_DETAIL_LOGGING
104 642 : break;
105 871 : case to_underlying(Tag::kMoreChunkedMessages):
106 871 : VerifyOrReturnError(TLV::kTLVType_Boolean == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
107 : #if CHIP_DETAIL_LOGGING
108 : {
109 : bool moreChunkedMessages;
110 871 : ReturnErrorOnFailure(reader.Get(moreChunkedMessages));
111 871 : PRETTY_PRINT("\tMoreChunkedMessages = %s, ", moreChunkedMessages ? "true" : "false");
112 : }
113 : #endif // CHIP_DETAIL_LOGGING
114 871 : break;
115 1939 : case Revision::kInteractionModelRevisionTag:
116 1939 : ReturnErrorOnFailure(MessageParser::CheckInteractionModelRevision(reader));
117 1939 : break;
118 0 : default:
119 0 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
120 0 : break;
121 : }
122 : }
123 :
124 1939 : PRETTY_PRINT("}");
125 1939 : PRETTY_PRINT_BLANK_LINE();
126 : // if we have exhausted this container
127 3878 : if (CHIP_END_OF_TLV == err)
128 : {
129 1939 : err = CHIP_NO_ERROR;
130 : }
131 1939 : ReturnErrorOnFailure(err);
132 1939 : return reader.ExitContainer(mOuterContainerType);
133 : }
134 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
135 :
136 1936 : CHIP_ERROR ReportDataMessage::Parser::GetSuppressResponse(bool * const apSuppressResponse) const
137 : {
138 1936 : CHIP_ERROR err = GetSimpleValue(to_underlying(Tag::kSuppressResponse), TLV::kTLVType_Boolean, apSuppressResponse);
139 3872 : if (CHIP_END_OF_TLV == err)
140 : {
141 : // If SuppressResponse is not present, treat it as false.
142 1232 : *apSuppressResponse = false;
143 1232 : return CHIP_NO_ERROR;
144 : }
145 704 : return err;
146 : }
147 :
148 2027 : CHIP_ERROR ReportDataMessage::Parser::GetSubscriptionId(SubscriptionId * const apSubscriptionId) const
149 : {
150 2027 : return GetUnsignedInteger(to_underlying(Tag::kSubscriptionId), apSubscriptionId);
151 : }
152 :
153 1932 : CHIP_ERROR ReportDataMessage::Parser::GetAttributeReportIBs(AttributeReportIBs::Parser * const apAttributeReportIBs) const
154 : {
155 1932 : TLV::TLVReader reader;
156 1932 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kAttributeReportIBs), reader));
157 1298 : return apAttributeReportIBs->Init(reader);
158 : }
159 :
160 1932 : CHIP_ERROR ReportDataMessage::Parser::GetEventReports(EventReportIBs::Parser * const apEventReports) const
161 : {
162 1932 : TLV::TLVReader reader;
163 1932 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kEventReports), reader));
164 642 : return apEventReports->Init(reader);
165 : }
166 :
167 1932 : CHIP_ERROR ReportDataMessage::Parser::GetMoreChunkedMessages(bool * const apMoreChunkedMessages) const
168 : {
169 1932 : return GetSimpleValue(to_underlying(Tag::kMoreChunkedMessages), TLV::kTLVType_Boolean, apMoreChunkedMessages);
170 : }
171 :
172 723 : ReportDataMessage::Builder & ReportDataMessage::Builder::SuppressResponse(const bool aSuppressResponse)
173 : {
174 : // skip if error has already been set
175 1446 : if (mError == CHIP_NO_ERROR)
176 : {
177 723 : mError = mpWriter->PutBoolean(TLV::ContextTag(Tag::kSuppressResponse), aSuppressResponse);
178 : }
179 723 : return *this;
180 : }
181 :
182 450 : ReportDataMessage::Builder & ReportDataMessage::Builder::SubscriptionId(const chip::SubscriptionId aSubscriptionId)
183 : {
184 : // skip if error has already been set
185 900 : if (mError == CHIP_NO_ERROR)
186 : {
187 450 : mError = mpWriter->Put(TLV::ContextTag(Tag::kSubscriptionId), aSubscriptionId);
188 : }
189 450 : return *this;
190 : }
191 :
192 2005 : AttributeReportIBs::Builder & ReportDataMessage::Builder::CreateAttributeReportIBs()
193 : {
194 : // skip if error has already been set
195 4010 : if (mError == CHIP_NO_ERROR)
196 : {
197 2005 : mError = mAttributeReportIBsBuilder.Init(mpWriter, to_underlying(Tag::kAttributeReportIBs));
198 : }
199 2005 : return mAttributeReportIBsBuilder;
200 : }
201 :
202 865 : EventReportIBs::Builder & ReportDataMessage::Builder::CreateEventReports()
203 : {
204 : // skip if error has already been set
205 1730 : if (mError == CHIP_NO_ERROR)
206 : {
207 865 : mError = mEventReportsBuilder.Init(mpWriter, to_underlying(Tag::kEventReports));
208 : }
209 865 : return mEventReportsBuilder;
210 : }
211 :
212 881 : ReportDataMessage::Builder & ReportDataMessage::Builder::MoreChunkedMessages(const bool aMoreChunkedMessages)
213 : {
214 : // skip if error has already been set
215 1762 : if (mError == CHIP_NO_ERROR)
216 : {
217 881 : mError = mpWriter->PutBoolean(TLV::ContextTag(Tag::kMoreChunkedMessages), aMoreChunkedMessages);
218 : }
219 881 : return *this;
220 : }
221 :
222 1978 : CHIP_ERROR ReportDataMessage::Builder::EndOfReportDataMessage()
223 : {
224 3956 : if (mError == CHIP_NO_ERROR)
225 : {
226 1978 : mError = MessageBuilder::EncodeInteractionModelRevision();
227 : }
228 3956 : if (mError == CHIP_NO_ERROR)
229 : {
230 1978 : EndOfContainer();
231 : }
232 1978 : return GetError();
233 : }
234 : } // namespace app
235 : } // namespace chip
|