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