Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2018 Google LLC.
5 : * Copyright (c) 2016-2017 Nest Labs, Inc.
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : #include "CommandDataIB.h"
20 :
21 : #include "MessageDefHelper.h"
22 :
23 : #include <inttypes.h>
24 : #include <stdarg.h>
25 : #include <stdio.h>
26 :
27 : #include <app/AppConfig.h>
28 :
29 : namespace chip {
30 : namespace app {
31 : #if CHIP_CONFIG_IM_PRETTY_PRINT
32 89 : CHIP_ERROR CommandDataIB::Parser::PrettyPrint() const
33 : {
34 89 : CHIP_ERROR err = CHIP_NO_ERROR;
35 89 : TLV::TLVReader reader;
36 :
37 89 : PRETTY_PRINT("CommandDataIB =");
38 89 : PRETTY_PRINT("{");
39 :
40 : // make a copy of the reader
41 89 : reader.Init(mReader);
42 :
43 546 : while (CHIP_NO_ERROR == (err = reader.Next()))
44 : {
45 184 : if (!TLV::IsContextTag(reader.GetTag()))
46 : {
47 0 : continue;
48 : }
49 184 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
50 :
51 184 : switch (tagNum)
52 : {
53 89 : case to_underlying(Tag::kPath): {
54 89 : CommandPathIB::Parser path;
55 89 : ReturnErrorOnFailure(path.Init(reader));
56 89 : PRETTY_PRINT_INCDEPTH();
57 89 : ReturnErrorOnFailure(path.PrettyPrint());
58 89 : PRETTY_PRINT_DECDEPTH();
59 : }
60 :
61 89 : break;
62 84 : case to_underlying(Tag::kFields):
63 84 : PRETTY_PRINT_INCDEPTH();
64 84 : ReturnErrorOnFailure(CheckIMPayload(reader, 0, "CommandFields"));
65 84 : PRETTY_PRINT_DECDEPTH();
66 84 : break;
67 10 : case to_underlying(Tag::kRef):
68 10 : VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
69 : {
70 : uint16_t reference;
71 10 : ReturnErrorOnFailure(reader.Get(reference));
72 10 : PRETTY_PRINT("\tRef = 0x%x,", reference);
73 : }
74 10 : break;
75 1 : default:
76 1 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
77 1 : break;
78 : }
79 : }
80 :
81 89 : PRETTY_PRINT("},");
82 89 : PRETTY_PRINT_BLANK_LINE();
83 :
84 178 : if (CHIP_END_OF_TLV == err)
85 : {
86 89 : err = CHIP_NO_ERROR;
87 : }
88 :
89 89 : ReturnErrorOnFailure(err);
90 89 : return reader.ExitContainer(mOuterContainerType);
91 : }
92 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
93 :
94 131 : CHIP_ERROR CommandDataIB::Parser::GetPath(CommandPathIB::Parser * const apPath) const
95 : {
96 131 : TLV::TLVReader reader;
97 131 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kPath), reader));
98 131 : return apPath->Init(reader);
99 : }
100 :
101 61 : CHIP_ERROR CommandDataIB::Parser::GetFields(TLV::TLVReader * const apReader) const
102 : {
103 61 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kFields), *apReader));
104 59 : return CHIP_NO_ERROR;
105 : }
106 :
107 76 : CHIP_ERROR CommandDataIB::Parser::GetRef(uint16_t * const apRef) const
108 : {
109 76 : return GetUnsignedInteger(to_underlying(Tag::kRef), apRef);
110 : }
111 :
112 110 : CommandPathIB::Builder & CommandDataIB::Builder::CreatePath()
113 : {
114 110 : mError = mPath.Init(mpWriter, to_underlying(Tag::kPath));
115 110 : return mPath;
116 : }
117 :
118 23 : CHIP_ERROR CommandDataIB::Builder::Ref(const uint16_t aRef)
119 : {
120 23 : return mpWriter->Put(TLV::ContextTag(Tag::kRef), aRef);
121 : }
122 :
123 105 : CHIP_ERROR CommandDataIB::Builder::EndOfCommandDataIB()
124 : {
125 105 : EndOfContainer();
126 105 : return GetError();
127 : }
128 : } // namespace app
129 : } // namespace chip
|