3 * Copyright (c) 2020 Project CHIP Authors
4 * 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * This file defines AttributeDataElement parser and builder in CHIP interaction model
26 #ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_ELEMENT_H
27 #define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_ELEMENT_H
29 #include "AttributePath.h"
32 #include <core/CHIPCore.h>
33 #include <core/CHIPTLV.h>
34 #include <support/CodeUtils.h>
35 #include <support/logging/CHIPLogging.h>
36 #include <util/basic-types.h>
40 namespace AttributeDataElement {
43 kCsTag_AttributePath = 0,
44 kCsTag_DataVersion = 1,
46 kCsTag_MoreClusterDataFlag = 3,
49 class Parser : public chip::app::Parser
53 * @brief Initialize the parser object with TLVReader
55 * @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this AttributeDataElement
57 * @return #CHIP_NO_ERROR on success
59 CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
62 * @brief Roughly verify the message is correctly formed
63 * 1) all mandatory tags are present
64 * 2) all elements have expected data type
65 * 3) any tag can only appear once
66 * 4) At the top level of the structure, unknown tags are ignored for forward compatibility
67 * @note The main use of this function is to print out what we're
68 * receiving during protocol development and debugging.
69 * The encoding rule has changed in IM encoding spec so this
70 * check is only "roughly" conformant now.
72 * @return #CHIP_NO_ERROR on success
74 CHIP_ERROR CheckSchemaValidity() const;
77 * @brief Get a TLVReader for the AttributePath. Next() must be called before accessing them.
79 * @param [in] apAttributePath A pointer to apAttributePath
81 * @return #CHIP_NO_ERROR on success
82 * #CHIP_ERROR_WRONG_TLV_TYPE if there is such element but it's not a Path
83 * #CHIP_END_OF_TLV if there is no such element
85 CHIP_ERROR GetAttributePath(AttributePath::Parser * const apAttributePath) const;
88 * @brief Get a TLVReader for the DataVersion. Next() must be called before accessing them.
90 * @param [in] apVersion A pointer to apVersion
92 * @return #CHIP_NO_ERROR on success
93 * #CHIP_ERROR_WRONG_TLV_TYPE if there is such element but it's not any of the defined unsigned integer types
94 * #CHIP_END_OF_TLV if there is no such element
96 CHIP_ERROR GetDataVersion(chip::DataVersion * const apVersion) const;
99 * @brief Get a TLVReader for the Data. Next() must be called before accessing them.
101 * @param [in] apReader A pointer to apReader
103 * @return #CHIP_NO_ERROR on success
104 * #CHIP_END_OF_TLV if there is no such element
106 CHIP_ERROR GetData(chip::TLV::TLVReader * const apReader) const;
109 * @brief Check whether it need more cluster data Next() must be called before accessing them.
111 * @param [in] apMoreClusterDataFlag A pointer to apMoreClusterDataFlag
113 * @return #CHIP_NO_ERROR on success
114 * #CHIP_END_OF_TLV if there is no such element
116 CHIP_ERROR GetMoreClusterDataFlag(bool * const apMoreClusterDataFlag) const;
119 // A recursively callable function to parse a data element and pretty-print it.
120 CHIP_ERROR ParseData(chip::TLV::TLVReader & aReader, int aDepth) const;
123 class Builder : public chip::app::Builder
127 * @brief Initialize a AttributeDataElement::Builder for writing into a TLV stream
129 * @param [in] apWriter A pointer to TLVWriter
131 * @return #CHIP_NO_ERROR on success
133 CHIP_ERROR Init(chip::TLV::TLVWriter * const apWriter);
136 * @brief Initialize a AttributePath::Builder for writing into the TLV stream
138 * @return A reference to AttributePath::Builder
140 AttributePath::Builder & CreateAttributePathBuilder();
143 * @brief Inject DataVersion into the TLV stream to indicate the numerical data version associated with
144 * the cluster that is referenced by the path.
146 * @param [in] aDataVersion The boolean variable to indicate if AttributeDataElement has version
148 * @return A reference to *this
150 AttributeDataElement::Builder & DataVersion(const chip::DataVersion aDataVersion);
153 * @brief Inject aMoreClusterData into the TLV stream to indicate whether there is more cluster data.
154 * This is present when there is more than one AttributeDataElement as part of a logical Changeset,
155 * and the entire set needs to be applied ‘atomically’ on the receiver.
157 * @param [in] aMoreClusterData The boolean variable to indicate if more cluster data is needed.
159 * @return A reference to *this
161 AttributeDataElement::Builder & MoreClusterData(const bool aMoreClusterData);
164 * @brief Mark the end of this AttributeDataElement
166 * @return A reference to *this
168 AttributeDataElement::Builder & EndOfAttributeDataElement();
171 AttributePath::Builder mAttributePathBuilder;
173 }; // namespace AttributeDataElement
178 #endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_ATTRIBUTE_DATA_ELEMENT_H