Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2020-2021 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 "AttributeDataIB.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 6603 : CHIP_ERROR AttributeDataIB::Parser::PrettyPrint() const
33 : {
34 6603 : CHIP_ERROR err = CHIP_NO_ERROR;
35 6603 : TLV::TLVReader reader;
36 :
37 6603 : PRETTY_PRINT("AttributeDataIB =");
38 6603 : PRETTY_PRINT("{");
39 :
40 : // make a copy of the Path reader
41 6603 : reader.Init(mReader);
42 :
43 23936 : while (CHIP_NO_ERROR == (err = reader.Next()))
44 : {
45 17333 : if (!TLV::IsContextTag(reader.GetTag()))
46 : {
47 0 : continue;
48 : }
49 17333 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
50 17333 : switch (tagNum)
51 : {
52 4127 : case to_underlying(Tag::kDataVersion):
53 4127 : VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
54 : #if CHIP_DETAIL_LOGGING
55 : {
56 : chip::DataVersion version;
57 4127 : ReturnErrorOnFailure(reader.Get(version));
58 4127 : PRETTY_PRINT("\tDataVersion = 0x%" PRIx32 ",", version);
59 : }
60 : #endif // CHIP_DETAIL_LOGGING
61 4127 : break;
62 6603 : case to_underlying(Tag::kPath): {
63 6603 : AttributePathIB::Parser path;
64 6603 : ReturnErrorOnFailure(path.Init(reader));
65 :
66 6603 : PRETTY_PRINT_INCDEPTH();
67 6603 : ReturnErrorOnFailure(path.PrettyPrint());
68 6603 : PRETTY_PRINT_DECDEPTH();
69 : }
70 6603 : break;
71 6603 : case to_underlying(Tag::kData):
72 6603 : PRETTY_PRINT_INCDEPTH();
73 6603 : ReturnErrorOnFailure(CheckIMPayload(reader, 0, "Data"));
74 6603 : PRETTY_PRINT_DECDEPTH();
75 6603 : break;
76 0 : default:
77 0 : PRETTY_PRINT("Unknown tag num %" PRIu32, tagNum);
78 0 : break;
79 : }
80 : }
81 6603 : PRETTY_PRINT("},");
82 6603 : PRETTY_PRINT_BLANK_LINE();
83 :
84 : // if we have exhausted this container
85 6603 : if (CHIP_END_OF_TLV == err)
86 : {
87 6603 : err = CHIP_NO_ERROR;
88 : }
89 6603 : ReturnErrorOnFailure(err);
90 6603 : return reader.ExitContainer(mOuterContainerType);
91 : }
92 : #endif // CHIP_CONFIG_IM_PRETTY_PRINT
93 :
94 6646 : CHIP_ERROR AttributeDataIB::Parser::GetPath(AttributePathIB::Parser * const apPath) const
95 : {
96 6646 : TLV::TLVReader reader;
97 6646 : ReturnErrorOnFailure(mReader.FindElementWithTag(TLV::ContextTag(Tag::kPath), reader));
98 6646 : return apPath->Init(reader);
99 : }
100 :
101 6634 : CHIP_ERROR AttributeDataIB::Parser::GetDataVersion(chip::DataVersion * const apVersion) const
102 : {
103 6634 : return GetUnsignedInteger(to_underlying(Tag::kDataVersion), apVersion);
104 : }
105 :
106 6642 : CHIP_ERROR AttributeDataIB::Parser::GetData(TLV::TLVReader * const apReader) const
107 : {
108 6642 : return mReader.FindElementWithTag(TLV::ContextTag(Tag::kData), *apReader);
109 : }
110 :
111 8349 : AttributePathIB::Builder & AttributeDataIB::Builder::CreatePath()
112 : {
113 8349 : if (mError == CHIP_NO_ERROR)
114 : {
115 8314 : mError = mPath.Init(mpWriter, to_underlying(Tag::kPath));
116 : }
117 8349 : return mPath;
118 : }
119 :
120 4404 : AttributeDataIB::Builder & AttributeDataIB::Builder::DataVersion(const chip::DataVersion aDataVersion)
121 : {
122 : // skip if error has already been set
123 4404 : if (mError == CHIP_NO_ERROR)
124 : {
125 4404 : mError = mpWriter->Put(TLV::ContextTag(Tag::kDataVersion), aDataVersion);
126 : }
127 4404 : return *this;
128 : }
129 :
130 6823 : CHIP_ERROR AttributeDataIB::Builder::EndOfAttributeDataIB()
131 : {
132 6823 : EndOfContainer();
133 6823 : return GetError();
134 : }
135 : } // namespace app
136 : } // namespace chip
|