blob: 619c17988dba9c2083169c098e566b73f4b2b8f6 [file] [log] [blame]
yunhanw-google36e61e32021-11-02 08:31:34 -07001/**
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 <inttypes.h>
18#include <stdarg.h>
19#include <stdio.h>
20
21#include "InvokeResponseMessage.h"
22#include "MessageDefHelper.h"
23
Daniel Nicoara641e3ad2022-07-25 15:32:03 -040024#include <app/AppConfig.h>
yunhanw-google36e61e32021-11-02 08:31:34 -070025
26namespace chip {
27namespace app {
yunhanw-google751d0dd2022-11-16 19:09:58 -080028#if CHIP_CONFIG_IM_PRETTY_PRINT
29CHIP_ERROR InvokeResponseMessage::Parser::PrettyPrint() const
yunhanw-google36e61e32021-11-02 08:31:34 -070030{
yunhanw-google751d0dd2022-11-16 19:09:58 -080031 CHIP_ERROR err = CHIP_NO_ERROR;
yunhanw-google36e61e32021-11-02 08:31:34 -070032 TLV::TLVReader reader;
33
34 PRETTY_PRINT("InvokeResponseMessage =");
35 PRETTY_PRINT("{");
36
37 // make a copy of the reader
38 reader.Init(mReader);
39
40 while (CHIP_NO_ERROR == (err = reader.Next()))
41 {
Boris Zbarsky4432d082022-04-27 13:42:10 -040042 if (!TLV::IsContextTag(reader.GetTag()))
43 {
44 continue;
45 }
yunhanw-google36e61e32021-11-02 08:31:34 -070046 uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
47 switch (tagNum)
48 {
49 case to_underlying(Tag::kSuppressResponse):
yunhanw-google36e61e32021-11-02 08:31:34 -070050#if CHIP_DETAIL_LOGGING
yunhanw-google751d0dd2022-11-16 19:09:58 -080051 {
52 bool suppressResponse;
53 ReturnErrorOnFailure(reader.Get(suppressResponse));
54 PRETTY_PRINT("\tsuppressResponse = %s, ", suppressResponse ? "true" : "false");
55 }
yunhanw-google36e61e32021-11-02 08:31:34 -070056#endif // CHIP_DETAIL_LOGGING
yunhanw-google751d0dd2022-11-16 19:09:58 -080057 break;
58 case to_underlying(Tag::kInvokeResponses): {
59 InvokeResponseIBs::Parser invokeResponses;
60 ReturnErrorOnFailure(invokeResponses.Init(reader));
yunhanw-google36e61e32021-11-02 08:31:34 -070061
yunhanw-google751d0dd2022-11-16 19:09:58 -080062 PRETTY_PRINT_INCDEPTH();
63 ReturnErrorOnFailure(invokeResponses.PrettyPrint());
64 PRETTY_PRINT_DECDEPTH();
65 }
66 break;
yunhanw-google86b7b7f2022-01-28 16:53:51 -080067 case kInteractionModelRevisionTag:
68 ReturnErrorOnFailure(MessageParser::CheckInteractionModelRevision(reader));
69 break;
yunhanw-google36e61e32021-11-02 08:31:34 -070070 default:
71 PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
72 break;
73 }
74 }
75
76 PRETTY_PRINT("},");
Boris Zbarsky6c4b54e2022-06-02 13:28:19 -040077 PRETTY_PRINT_BLANK_LINE();
yunhanw-google36e61e32021-11-02 08:31:34 -070078
79 if (CHIP_END_OF_TLV == err)
80 {
yunhanw-google751d0dd2022-11-16 19:09:58 -080081 err = CHIP_NO_ERROR;
yunhanw-google36e61e32021-11-02 08:31:34 -070082 }
83
84 ReturnErrorOnFailure(err);
yunhanw-google731ae5a2022-06-01 23:02:05 -070085 return reader.ExitContainer(mOuterContainerType);
yunhanw-google36e61e32021-11-02 08:31:34 -070086}
yunhanw-google751d0dd2022-11-16 19:09:58 -080087#endif // CHIP_CONFIG_IM_PRETTY_PRINT
yunhanw-google36e61e32021-11-02 08:31:34 -070088
89CHIP_ERROR InvokeResponseMessage::Parser::GetSuppressResponse(bool * const apSuppressResponse) const
90{
91 return GetSimpleValue(to_underlying(Tag::kSuppressResponse), TLV::kTLVType_Boolean, apSuppressResponse);
92}
93
yunhanw-googlef1117272021-12-02 19:59:44 -080094CHIP_ERROR InvokeResponseMessage::Parser::GetInvokeResponses(InvokeResponseIBs::Parser * const apStatus) const
yunhanw-google36e61e32021-11-02 08:31:34 -070095{
96 TLV::TLVReader reader;
97 ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(to_underlying(Tag::kInvokeResponses)), reader));
yunhanw-google731ae5a2022-06-01 23:02:05 -070098 return apStatus->Init(reader);
yunhanw-google36e61e32021-11-02 08:31:34 -070099}
100
101InvokeResponseMessage::Builder & InvokeResponseMessage::Builder::SuppressResponse(const bool aSuppressResponse)
102{
103 if (mError == CHIP_NO_ERROR)
104 {
105 mError = mpWriter->PutBoolean(TLV::ContextTag(to_underlying(Tag::kSuppressResponse)), aSuppressResponse);
106 }
107 return *this;
108}
109
yunhanw-googlef1117272021-12-02 19:59:44 -0800110InvokeResponseIBs::Builder & InvokeResponseMessage::Builder::CreateInvokeResponses()
yunhanw-google36e61e32021-11-02 08:31:34 -0700111{
112 if (mError == CHIP_NO_ERROR)
113 {
114 mError = mInvokeResponses.Init(mpWriter, to_underlying(Tag::kInvokeResponses));
115 }
116 return mInvokeResponses;
117}
118
119InvokeResponseMessage::Builder & InvokeResponseMessage::Builder::EndOfInvokeResponseMessage()
120{
yunhanw-google86b7b7f2022-01-28 16:53:51 -0800121 if (mError == CHIP_NO_ERROR)
122 {
123 mError = MessageBuilder::EncodeInteractionModelRevision();
124 }
125 if (mError == CHIP_NO_ERROR)
126 {
127 EndOfContainer();
128 }
yunhanw-google36e61e32021-11-02 08:31:34 -0700129 return *this;
130}
131} // namespace app
132} // namespace chip