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 "CommandStatusIB.h"
18 : #include "MessageDefHelper.h"
19 : #include "StructBuilder.h"
20 : #include "StructParser.h"
21 :
22 : #include <inttypes.h>
23 : #include <stdarg.h>
24 : #include <stdio.h>
25 :
26 : #include <app/AppConfig.h>
27 :
28 : namespace chip {
29 : namespace app {
30 : #if CHIP_CONFIG_IM_PRETTY_PRINT
31 45 : CHIP_ERROR CommandStatusIB::Parser::PrettyPrint() const
32 : {
33 45 : CHIP_ERROR err = CHIP_NO_ERROR;
34 45 : int tagPresenceMask = 0;
35 45 : TLV::TLVReader reader;
36 :
37 45 : PRETTY_PRINT("CommandStatusIB =");
38 45 : PRETTY_PRINT("{");
39 :
40 : // make a copy of the reader
41 45 : reader.Init(mReader);
42 :
43 272 : while (CHIP_NO_ERROR == (err = reader.Next()))
44 : {
45 91 : if (!TLV::IsContextTag(reader.GetTag()))
46 : {
47 0 : continue;
48 : }
49 91 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
50 91 : switch (tagNum)
51 : {
52 45 : case to_underlying(Tag::kPath):
53 : // check if this tag has appeared before
54 45 : VerifyOrReturnError(!(tagPresenceMask & (1 << to_underlying(Tag::kPath))), CHIP_ERROR_INVALID_TLV_TAG);
55 45 : tagPresenceMask |= (1 << to_underlying(Tag::kPath));
56 : {
57 45 : CommandPathIB::Parser path;
58 45 : ReturnErrorOnFailure(path.Init(reader));
59 :
60 45 : PRETTY_PRINT_INCDEPTH();
61 45 : ReturnErrorOnFailure(path.PrettyPrint());
62 45 : PRETTY_PRINT_DECDEPTH();
63 : }
64 45 : break;
65 45 : case to_underlying(Tag::kErrorStatus):
66 : // check if this tag has appeared before
67 45 : VerifyOrReturnError(!(tagPresenceMask & (1 << to_underlying(Tag::kErrorStatus))), CHIP_ERROR_INVALID_TLV_TAG);
68 45 : tagPresenceMask |= (1 << to_underlying(Tag::kErrorStatus));
69 : {
70 45 : StatusIB::Parser errorStatus;
71 45 : ReturnErrorOnFailure(errorStatus.Init(reader));
72 :
73 45 : PRETTY_PRINT_INCDEPTH();
74 45 : ReturnErrorOnFailure(errorStatus.PrettyPrint());
75 45 : PRETTY_PRINT_DECDEPTH();
76 : }
77 45 : break;
78 1 : case to_underlying(Tag::kRef):
79 1 : VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
80 : {
81 : uint16_t reference;
82 1 : ReturnErrorOnFailure(reader.Get(reference));
83 1 : PRETTY_PRINT("\tRef = 0x%x,", reference);
84 : }
85 1 : break;
86 0 : default:
87 0 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
88 0 : break;
89 : }
90 : }
91 :
92 45 : PRETTY_PRINT("},");
93 45 : PRETTY_PRINT_BLANK_LINE();
94 :
95 90 : if (CHIP_END_OF_TLV == err)
96 : {
97 45 : err = CHIP_NO_ERROR;
98 : }
99 :
100 45 : ReturnErrorOnFailure(err);
101 45 : return reader.ExitContainer(mOuterContainerType);
102 : }
103 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
104 :
105 44 : CHIP_ERROR CommandStatusIB::Parser::GetPath(CommandPathIB::Parser * const apPath) const
106 : {
107 44 : TLV::TLVReader reader;
108 44 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kPath), reader));
109 44 : return apPath->Init(reader);
110 : }
111 :
112 44 : CHIP_ERROR CommandStatusIB::Parser::GetErrorStatus(StatusIB::Parser * const apErrorStatus) const
113 : {
114 44 : TLV::TLVReader reader;
115 44 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kErrorStatus), reader));
116 44 : return apErrorStatus->Init(reader);
117 : }
118 :
119 43 : CHIP_ERROR CommandStatusIB::Parser::GetRef(uint16_t * const apRef) const
120 : {
121 43 : return GetUnsignedInteger(to_underlying(Tag::kRef), apRef);
122 : }
123 :
124 58 : CommandPathIB::Builder & CommandStatusIB::Builder::CreatePath()
125 : {
126 116 : if (mError == CHIP_NO_ERROR)
127 : {
128 58 : mError = mPath.Init(mpWriter, to_underlying(Tag::kPath));
129 : }
130 58 : return mPath;
131 : }
132 :
133 58 : StatusIB::Builder & CommandStatusIB::Builder::CreateErrorStatus()
134 : {
135 116 : if (mError == CHIP_NO_ERROR)
136 : {
137 58 : mError = mErrorStatus.Init(mpWriter, to_underlying(Tag::kErrorStatus));
138 : }
139 58 : return mErrorStatus;
140 : }
141 :
142 3 : CHIP_ERROR CommandStatusIB::Builder::Ref(const uint16_t aRef)
143 : {
144 3 : return mpWriter->Put(TLV::ContextTag(Tag::kRef), aRef);
145 : }
146 :
147 58 : CHIP_ERROR CommandStatusIB::Builder::EndOfCommandStatusIB()
148 : {
149 58 : EndOfContainer();
150 58 : return GetError();
151 : }
152 : } // namespace app
153 : } // namespace chip
|