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 <inttypes.h>
18 : #include <stdarg.h>
19 : #include <stdio.h>
20 :
21 : #include "InvokeResponseIB.h"
22 : #include "MessageDefHelper.h"
23 :
24 : #include <app/AppConfig.h>
25 :
26 : namespace chip {
27 : namespace app {
28 : #if CHIP_CONFIG_IM_PRETTY_PRINT
29 40 : CHIP_ERROR InvokeResponseIB::Parser::PrettyPrint() const
30 : {
31 40 : CHIP_ERROR err = CHIP_NO_ERROR;
32 40 : TLV::TLVReader reader;
33 :
34 40 : PRETTY_PRINT("InvokeResponseIB =");
35 40 : PRETTY_PRINT("{");
36 :
37 : // make a copy of the reader
38 40 : reader.Init(mReader);
39 :
40 80 : while (CHIP_NO_ERROR == (err = reader.Next()))
41 : {
42 40 : if (!TLV::IsContextTag(reader.GetTag()))
43 : {
44 0 : continue;
45 : }
46 40 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
47 40 : switch (tagNum)
48 : {
49 14 : case to_underlying(Tag::kCommand): {
50 14 : CommandDataIB::Parser command;
51 14 : ReturnErrorOnFailure(command.Init(reader));
52 :
53 14 : PRETTY_PRINT_INCDEPTH();
54 14 : ReturnErrorOnFailure(command.PrettyPrint());
55 14 : PRETTY_PRINT_DECDEPTH();
56 : }
57 14 : break;
58 26 : case to_underlying(Tag::kStatus): {
59 26 : CommandStatusIB::Parser status;
60 26 : ReturnErrorOnFailure(status.Init(reader));
61 :
62 26 : PRETTY_PRINT_INCDEPTH();
63 26 : ReturnErrorOnFailure(status.PrettyPrint());
64 26 : PRETTY_PRINT_DECDEPTH();
65 : }
66 26 : break;
67 0 : default:
68 0 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
69 0 : break;
70 : }
71 : }
72 :
73 40 : PRETTY_PRINT("},");
74 40 : PRETTY_PRINT_BLANK_LINE();
75 :
76 : // if we have exhausted this container
77 40 : if (CHIP_END_OF_TLV == err)
78 : {
79 40 : err = CHIP_NO_ERROR;
80 : }
81 40 : ReturnErrorOnFailure(err);
82 40 : return reader.ExitContainer(mOuterContainerType);
83 : }
84 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
85 :
86 12 : CHIP_ERROR InvokeResponseIB::Parser::GetCommand(CommandDataIB::Parser * const apCommand) const
87 : {
88 12 : TLV::TLVReader reader;
89 12 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kCommand), reader));
90 12 : return apCommand->Init(reader);
91 : }
92 :
93 37 : CHIP_ERROR InvokeResponseIB::Parser::GetStatus(CommandStatusIB::Parser * const apStatus) const
94 : {
95 37 : TLV::TLVReader reader;
96 37 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kStatus), reader));
97 26 : return apStatus->Init(reader);
98 : }
99 :
100 30 : CommandDataIB::Builder & InvokeResponseIB::Builder::CreateCommand()
101 : {
102 30 : if (mError == CHIP_NO_ERROR)
103 : {
104 30 : mError = mCommand.Init(mpWriter, to_underlying(Tag::kCommand));
105 : }
106 30 : return mCommand;
107 : }
108 :
109 39 : CommandStatusIB::Builder & InvokeResponseIB::Builder::CreateStatus()
110 : {
111 39 : if (mError == CHIP_NO_ERROR)
112 : {
113 39 : mError = mStatus.Init(mpWriter, to_underlying(Tag::kStatus));
114 : }
115 39 : return mStatus;
116 : }
117 :
118 65 : CHIP_ERROR InvokeResponseIB::Builder::EndOfInvokeResponseIB()
119 : {
120 65 : EndOfContainer();
121 65 : return GetError();
122 : }
123 : } // namespace app
124 : } // namespace chip
|