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 : * @file
18 : * This file defines WriteRequestMessage parser and builder in CHIP interaction model
19 : *
20 : */
21 :
22 : #include "WriteRequestMessage.h"
23 : #include "MessageDefHelper.h"
24 :
25 : #include <inttypes.h>
26 : #include <stdarg.h>
27 : #include <stdio.h>
28 :
29 : #include <app/AppConfig.h>
30 :
31 : namespace chip {
32 : namespace app {
33 : #if CHIP_CONFIG_IM_PRETTY_PRINT
34 1738 : CHIP_ERROR WriteRequestMessage::Parser::PrettyPrint() const
35 : {
36 1738 : CHIP_ERROR err = CHIP_NO_ERROR;
37 1738 : TLV::TLVReader reader;
38 :
39 1738 : PRETTY_PRINT("WriteRequestMessage =");
40 1738 : PRETTY_PRINT("{");
41 :
42 : // make a copy of the reader
43 1738 : reader.Init(mReader);
44 :
45 10420 : while (CHIP_NO_ERROR == (err = reader.Next()))
46 : {
47 8682 : if (!TLV::IsContextTag(reader.GetTag()))
48 : {
49 0 : continue;
50 : }
51 8682 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
52 8682 : switch (tagNum)
53 : {
54 1734 : case to_underlying(Tag::kSuppressResponse):
55 : #if CHIP_DETAIL_LOGGING
56 : {
57 : bool suppressResponse;
58 1734 : ReturnErrorOnFailure(reader.Get(suppressResponse));
59 1734 : PRETTY_PRINT("\tsuppressResponse = %s, ", suppressResponse ? "true" : "false");
60 : }
61 : #endif // CHIP_DETAIL_LOGGING
62 1734 : break;
63 :
64 1738 : case to_underlying(Tag::kTimedRequest):
65 : #if CHIP_DETAIL_LOGGING
66 : {
67 : bool timedRequest;
68 1738 : ReturnErrorOnFailure(reader.Get(timedRequest));
69 1738 : PRETTY_PRINT("\ttimedRequest = %s, ", timedRequest ? "true" : "false");
70 : }
71 : #endif // CHIP_DETAIL_LOGGING
72 1738 : break;
73 1738 : case to_underlying(Tag::kWriteRequests): {
74 1738 : AttributeDataIBs::Parser writeRequests;
75 1738 : ReturnErrorOnFailure(writeRequests.Init(reader));
76 :
77 1738 : PRETTY_PRINT_INCDEPTH();
78 1738 : ReturnErrorOnFailure(writeRequests.PrettyPrint());
79 1738 : PRETTY_PRINT_DECDEPTH();
80 : }
81 1738 : break;
82 1734 : case to_underlying(Tag::kMoreChunkedMessages):
83 : #if CHIP_DETAIL_LOGGING
84 : {
85 : bool moreChunkedMessages;
86 1734 : ReturnErrorOnFailure(reader.Get(moreChunkedMessages));
87 1734 : PRETTY_PRINT("\tmoreChunkedMessages = %s, ", moreChunkedMessages ? "true" : "false");
88 : }
89 : #endif // CHIP_DETAIL_LOGGING
90 1734 : break;
91 1738 : case Revision::kInteractionModelRevisionTag:
92 1738 : ReturnErrorOnFailure(MessageParser::CheckInteractionModelRevision(reader));
93 1738 : break;
94 0 : default:
95 0 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
96 0 : break;
97 : }
98 : }
99 :
100 1738 : PRETTY_PRINT("},");
101 1738 : PRETTY_PRINT_BLANK_LINE();
102 :
103 1738 : if (CHIP_END_OF_TLV == err)
104 : {
105 1738 : err = CHIP_NO_ERROR;
106 : }
107 :
108 1738 : ReturnErrorOnFailure(err);
109 1738 : return reader.ExitContainer(mOuterContainerType);
110 : }
111 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
112 :
113 1738 : CHIP_ERROR WriteRequestMessage::Parser::GetSuppressResponse(bool * const apSuppressResponse) const
114 : {
115 1738 : return GetSimpleValue(to_underlying(Tag::kSuppressResponse), TLV::kTLVType_Boolean, apSuppressResponse);
116 : }
117 :
118 1738 : CHIP_ERROR WriteRequestMessage::Parser::GetTimedRequest(bool * const apTimedRequest) const
119 : {
120 1738 : return GetSimpleValue(to_underlying(Tag::kTimedRequest), TLV::kTLVType_Boolean, apTimedRequest);
121 : }
122 :
123 1738 : CHIP_ERROR WriteRequestMessage::Parser::GetWriteRequests(AttributeDataIBs::Parser * const apAttributeDataIBs) const
124 : {
125 1738 : TLV::TLVReader reader;
126 1738 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kWriteRequests), reader));
127 1738 : return apAttributeDataIBs->Init(reader);
128 : }
129 :
130 1738 : CHIP_ERROR WriteRequestMessage::Parser::GetMoreChunkedMessages(bool * const apMoreChunkedMessages) const
131 : {
132 1738 : return GetSimpleValue(to_underlying(Tag::kMoreChunkedMessages), TLV::kTLVType_Boolean, apMoreChunkedMessages);
133 : }
134 :
135 1844 : WriteRequestMessage::Builder & WriteRequestMessage::Builder::SuppressResponse(const bool aSuppressResponse)
136 : {
137 : // skip if error has already been set
138 1844 : if (mError == CHIP_NO_ERROR)
139 : {
140 1844 : mError = mpWriter->PutBoolean(TLV::ContextTag(Tag::kSuppressResponse), aSuppressResponse);
141 : }
142 1844 : return *this;
143 : }
144 :
145 1848 : WriteRequestMessage::Builder & WriteRequestMessage::Builder::TimedRequest(const bool aTimedRequest)
146 : {
147 : // skip if error has already been set
148 1848 : if (mError == CHIP_NO_ERROR)
149 : {
150 1848 : mError = mpWriter->PutBoolean(TLV::ContextTag(Tag::kTimedRequest), aTimedRequest);
151 : }
152 1848 : return *this;
153 : }
154 :
155 1848 : AttributeDataIBs::Builder & WriteRequestMessage::Builder::CreateWriteRequests()
156 : {
157 : // skip if error has already been set
158 1848 : if (mError == CHIP_NO_ERROR)
159 : {
160 1848 : mError = mWriteRequests.Init(mpWriter, to_underlying(Tag::kWriteRequests));
161 : }
162 1848 : return mWriteRequests;
163 : }
164 :
165 1791 : WriteRequestMessage::Builder & WriteRequestMessage::Builder::MoreChunkedMessages(const bool aMoreChunkedMessages)
166 : {
167 : // skip if error has already been set
168 1791 : if (mError == CHIP_NO_ERROR)
169 : {
170 1791 : mError = mpWriter->PutBoolean(TLV::ContextTag(Tag::kMoreChunkedMessages), aMoreChunkedMessages);
171 : }
172 1791 : return *this;
173 : }
174 :
175 1795 : CHIP_ERROR WriteRequestMessage::Builder::EndOfWriteRequestMessage()
176 : {
177 1795 : if (mError == CHIP_NO_ERROR)
178 : {
179 1795 : mError = MessageBuilder::EncodeInteractionModelRevision();
180 : }
181 1795 : if (mError == CHIP_NO_ERROR)
182 : {
183 1795 : EndOfContainer();
184 : }
185 1795 : return GetError();
186 : }
187 : } // namespace app
188 : } // namespace chip
|