Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / MessageDef / AttributeDataVersionList.cpp
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  *    @file
20  *      This file defines AttributeDataVersionList parser and builder in CHIP interaction model
21  *
22  */
23
24 #include "AttributeDataVersionList.h"
25
26 #include "MessageDefHelper.h"
27
28 #include <inttypes.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31
32 using namespace chip;
33 using namespace chip::TLV;
34
35 namespace chip {
36 namespace app {
37 #if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
38 CHIP_ERROR AttributeDataVersionList::Parser::CheckSchemaValidity() const
39 {
40     CHIP_ERROR err = CHIP_NO_ERROR;
41     chip::TLV::TLVReader reader;
42     chip::DataVersion version;
43     size_t index = 0;
44
45     PRETTY_PRINT("AttributeDataVersionList = ");
46     PRETTY_PRINT("[");
47
48     reader.Init(mReader);
49
50     while (CHIP_NO_ERROR == (err = reader.Next()))
51     {
52         VerifyOrExit(chip::TLV::AnonymousTag == reader.GetTag(), err = CHIP_ERROR_INVALID_TLV_TAG);
53
54         switch (reader.GetType())
55         {
56         case chip::TLV::kTLVType_Null:
57             PRETTY_PRINT("\tNull,");
58             break;
59
60         case chip::TLV::kTLVType_UnsignedInteger:
61             err = reader.Get(version);
62             SuccessOrExit(err);
63
64             PRETTY_PRINT("\t0x%" PRIx64 ",", version);
65             break;
66
67         default:
68             ExitNow(err = CHIP_ERROR_WRONG_TLV_TYPE);
69             break;
70         }
71
72         ++index;
73     }
74
75     PRETTY_PRINT("],");
76
77     if (CHIP_END_OF_TLV == err)
78     {
79         err = CHIP_NO_ERROR;
80     }
81
82 exit:
83     ChipLogFunctError(err);
84
85     return err;
86 }
87 #endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
88
89 // 1) current element is anonymous
90 // 2) current element is either unsigned integer or NULL
91 bool AttributeDataVersionList::Parser::IsElementValid(void)
92 {
93     CHIP_ERROR err = CHIP_NO_ERROR;
94     bool result    = false;
95
96     VerifyOrExit(chip::TLV::AnonymousTag == mReader.GetTag(), err = CHIP_ERROR_INVALID_TLV_TAG);
97
98     switch (mReader.GetType())
99     {
100     case chip::TLV::kTLVType_Null:
101     case chip::TLV::kTLVType_UnsignedInteger:
102         result = true;
103         break;
104     default:
105         ExitNow();
106         break;
107     }
108
109 exit:
110     ChipLogFunctError(err);
111
112     return result;
113 }
114
115 bool AttributeDataVersionList::Parser::IsNull(void)
116 {
117     return (chip::TLV::kTLVType_Null == mReader.GetType());
118 }
119
120 CHIP_ERROR AttributeDataVersionList::Parser::GetVersion(chip::DataVersion * const apVersion)
121 {
122     CHIP_ERROR err = CHIP_NO_ERROR;
123     if (mReader.GetType() == kTLVType_Null)
124     {
125         *apVersion = 0;
126         ChipLogDetail(DataManagement, "Version is null in GetVersion");
127     }
128     else
129     {
130         err = mReader.Get(*apVersion);
131     }
132     return err;
133 }
134
135 AttributeDataVersionList::Builder & AttributeDataVersionList::Builder::AddVersion(const uint64_t aVersion)
136 {
137     // skip if error has already been set
138     SuccessOrExit(mError);
139
140     mError = mpWriter->Put(chip::TLV::AnonymousTag, aVersion);
141     ChipLogFunctError(mError);
142
143 exit:
144     return *this;
145 }
146
147 AttributeDataVersionList::Builder & AttributeDataVersionList::Builder::AddNull(void)
148 {
149     // skip if error has already been set
150     SuccessOrExit(mError);
151
152     mError = mpWriter->PutNull(chip::TLV::AnonymousTag);
153     ChipLogFunctError(mError);
154
155 exit:
156     return *this;
157 }
158
159 // Mark the end of this array and recover the type for outer container
160 AttributeDataVersionList::Builder & AttributeDataVersionList::Builder::EndOfAttributeDataVersionList(void)
161 {
162     EndOfContainer();
163     return *this;
164 }
165 }; // namespace app
166 }; // namespace chip