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 27 : CHIP_ERROR CommandStatusIB::Parser::PrettyPrint() const
32 : {
33 27 : CHIP_ERROR err = CHIP_NO_ERROR;
34 27 : int tagPresenceMask = 0;
35 27 : TLV::TLVReader reader;
36 :
37 27 : PRETTY_PRINT("CommandStatusIB =");
38 27 : PRETTY_PRINT("{");
39 :
40 : // make a copy of the reader
41 27 : reader.Init(mReader);
42 :
43 82 : while (CHIP_NO_ERROR == (err = reader.Next()))
44 : {
45 55 : if (!TLV::IsContextTag(reader.GetTag()))
46 : {
47 0 : continue;
48 : }
49 55 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
50 55 : switch (tagNum)
51 : {
52 27 : case to_underlying(Tag::kPath):
53 : // check if this tag has appeared before
54 27 : VerifyOrReturnError(!(tagPresenceMask & (1 << to_underlying(Tag::kPath))), CHIP_ERROR_INVALID_TLV_TAG);
55 27 : tagPresenceMask |= (1 << to_underlying(Tag::kPath));
56 : {
57 27 : CommandPathIB::Parser path;
58 27 : ReturnErrorOnFailure(path.Init(reader));
59 :
60 27 : PRETTY_PRINT_INCDEPTH();
61 27 : ReturnErrorOnFailure(path.PrettyPrint());
62 27 : PRETTY_PRINT_DECDEPTH();
63 : }
64 27 : break;
65 27 : case to_underlying(Tag::kErrorStatus):
66 : // check if this tag has appeared before
67 27 : VerifyOrReturnError(!(tagPresenceMask & (1 << to_underlying(Tag::kErrorStatus))), CHIP_ERROR_INVALID_TLV_TAG);
68 27 : tagPresenceMask |= (1 << to_underlying(Tag::kErrorStatus));
69 : {
70 27 : StatusIB::Parser errorStatus;
71 27 : ReturnErrorOnFailure(errorStatus.Init(reader));
72 :
73 27 : PRETTY_PRINT_INCDEPTH();
74 27 : ReturnErrorOnFailure(errorStatus.PrettyPrint());
75 27 : PRETTY_PRINT_DECDEPTH();
76 : }
77 27 : 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 27 : PRETTY_PRINT("},");
93 27 : PRETTY_PRINT_BLANK_LINE();
94 :
95 27 : if (CHIP_END_OF_TLV == err)
96 : {
97 27 : err = CHIP_NO_ERROR;
98 : }
99 :
100 27 : ReturnErrorOnFailure(err);
101 27 : return reader.ExitContainer(mOuterContainerType);
102 : }
103 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
104 :
105 26 : CHIP_ERROR CommandStatusIB::Parser::GetPath(CommandPathIB::Parser * const apPath) const
106 : {
107 26 : TLV::TLVReader reader;
108 26 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kPath), reader));
109 26 : return apPath->Init(reader);
110 : }
111 :
112 26 : CHIP_ERROR CommandStatusIB::Parser::GetErrorStatus(StatusIB::Parser * const apErrorStatus) const
113 : {
114 26 : TLV::TLVReader reader;
115 26 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kErrorStatus), reader));
116 26 : return apErrorStatus->Init(reader);
117 : }
118 :
119 25 : CHIP_ERROR CommandStatusIB::Parser::GetRef(uint16_t * const apRef) const
120 : {
121 25 : return GetUnsignedInteger(to_underlying(Tag::kRef), apRef);
122 : }
123 :
124 40 : CommandPathIB::Builder & CommandStatusIB::Builder::CreatePath()
125 : {
126 40 : if (mError == CHIP_NO_ERROR)
127 : {
128 40 : mError = mPath.Init(mpWriter, to_underlying(Tag::kPath));
129 : }
130 40 : return mPath;
131 : }
132 :
133 40 : StatusIB::Builder & CommandStatusIB::Builder::CreateErrorStatus()
134 : {
135 40 : if (mError == CHIP_NO_ERROR)
136 : {
137 40 : mError = mErrorStatus.Init(mpWriter, to_underlying(Tag::kErrorStatus));
138 : }
139 40 : 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 40 : CHIP_ERROR CommandStatusIB::Builder::EndOfCommandStatusIB()
148 : {
149 40 : EndOfContainer();
150 40 : return GetError();
151 : }
152 : } // namespace app
153 : } // namespace chip
|