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