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