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 90 : CHIP_ERROR CommandDataIB::Parser::PrettyPrint() const
33 : {
34 90 : CHIP_ERROR err = CHIP_NO_ERROR;
35 90 : TLV::TLVReader reader;
36 :
37 90 : PRETTY_PRINT("CommandDataIB =");
38 90 : PRETTY_PRINT("{");
39 :
40 : // make a copy of the reader
41 90 : reader.Init(mReader);
42 :
43 552 : while (CHIP_NO_ERROR == (err = reader.Next()))
44 : {
45 186 : if (!TLV::IsContextTag(reader.GetTag()))
46 : {
47 0 : continue;
48 : }
49 186 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
50 :
51 186 : switch (tagNum)
52 : {
53 90 : case to_underlying(Tag::kPath): {
54 90 : CommandPathIB::Parser path;
55 90 : ReturnErrorOnFailure(path.Init(reader));
56 90 : PRETTY_PRINT_INCDEPTH();
57 90 : ReturnErrorOnFailure(path.PrettyPrint());
58 90 : PRETTY_PRINT_DECDEPTH();
59 : }
60 :
61 90 : break;
62 85 : case to_underlying(Tag::kFields):
63 85 : PRETTY_PRINT_INCDEPTH();
64 85 : ReturnErrorOnFailure(CheckIMPayload(reader, 0, "CommandFields"));
65 85 : PRETTY_PRINT_DECDEPTH();
66 85 : 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 90 : PRETTY_PRINT("},");
82 90 : PRETTY_PRINT_BLANK_LINE();
83 :
84 180 : if (CHIP_END_OF_TLV == err)
85 : {
86 90 : err = CHIP_NO_ERROR;
87 : }
88 :
89 90 : ReturnErrorOnFailure(err);
90 90 : return reader.ExitContainer(mOuterContainerType);
91 : }
92 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
93 :
94 133 : CHIP_ERROR CommandDataIB::Parser::GetPath(CommandPathIB::Parser * const apPath) const
95 : {
96 133 : TLV::TLVReader reader;
97 133 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kPath), reader));
98 133 : return apPath->Init(reader);
99 : }
100 :
101 62 : CHIP_ERROR CommandDataIB::Parser::GetFields(TLV::TLVReader * const apReader) const
102 : {
103 62 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kFields), *apReader));
104 60 : return CHIP_NO_ERROR;
105 : }
106 :
107 77 : CHIP_ERROR CommandDataIB::Parser::GetRef(uint16_t * const apRef) const
108 : {
109 77 : return GetUnsignedInteger(to_underlying(Tag::kRef), apRef);
110 : }
111 :
112 111 : CommandPathIB::Builder & CommandDataIB::Builder::CreatePath()
113 : {
114 111 : mError = mPath.Init(mpWriter, to_underlying(Tag::kPath));
115 111 : 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 106 : CHIP_ERROR CommandDataIB::Builder::EndOfCommandDataIB()
124 : {
125 106 : EndOfContainer();
126 106 : return GetError();
127 : }
128 : } // namespace app
129 : } // namespace chip
|