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