Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / MessageDef / CommandDataElement.h
1 /**
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *
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 CommandDataElement parser and builder in CHIP interaction model
21  *
22  */
23
24 #pragma once
25
26 #ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_DATA_ELEMENT_H
27 #define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_DATA_ELEMENT_H
28
29 #include "Builder.h"
30 #include "CommandPath.h"
31
32 #include "Parser.h"
33 #include "StatusElement.h"
34 #include <core/CHIPCore.h>
35 #include <core/CHIPTLV.h>
36 #include <support/CodeUtils.h>
37 #include <support/logging/CHIPLogging.h>
38 #include <util/basic-types.h>
39
40 namespace chip {
41 namespace app {
42 namespace CommandDataElement {
43 enum
44 {
45     kCsTag_CommandPath   = 0,
46     kCsTag_Data          = 1,
47     kCsTag_StatusElement = 2,
48 };
49
50 class Parser : public chip::app::Parser
51 {
52 public:
53     /**
54      *  @brief Initialize the parser object with TLVReader
55      *
56      *  @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this CommandDataElement
57      *
58      *  @return #CHIP_NO_ERROR on success
59      */
60     CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
61
62     /**
63      *  @brief Roughly verify the message is correctly formed
64      *   1) all mandatory tags are present
65      *   2) all elements have expected data type
66      *   3) any tag can only appear once
67      *   4) At the top level of the structure, unknown tags are ignored for forward compatibility
68      *  @note The main use of this function is to print out what we're
69      *    receiving during protocol development and debugging.
70      *    The encoding rule has changed in IM encoding spec so this
71      *    check is only "roughly" conformant now.
72      *
73      *  @return #CHIP_NO_ERROR on success
74      */
75     CHIP_ERROR CheckSchemaValidity() const;
76
77     /**
78      *  @brief Get a TLVReader for the CommandPath. Next() must be called before accessing them.
79      *
80      *  @param [in] apCommandPath    A pointer to apCommandPath
81      *
82      *  @return #CHIP_NO_ERROR on success
83      *          #CHIP_ERROR_WRONG_TLV_TYPE if there is such element but it's not a Path
84      *          #CHIP_END_OF_TLV if there is no such element
85      */
86     CHIP_ERROR GetCommandPath(CommandPath::Parser * const apCommandPath) const;
87
88     /**
89      *  @brief Get a TLVReader for the Data. Next() must be called before accessing them.
90      *
91      *  @param [in] apReader    A pointer to apReader
92      *
93      *  @return #CHIP_NO_ERROR on success
94      *          #CHIP_END_OF_TLV if there is no such element
95      */
96     CHIP_ERROR GetData(chip::TLV::TLVReader * const apReader) const;
97
98     /**
99      *  @brief Get a TLVReader for the StatusElement. Next() must be called before accessing them.
100      *
101      *  @param [in] apStatusElement    A pointer to apStatusElement
102      *
103      *  @return #CHIP_NO_ERROR on success
104      *          # CHIP_ERROR_WRONG_TLV_TYPE if there is such element but it's not a structure
105      *          #CHIP_END_OF_TLV if there is no such element
106      */
107     CHIP_ERROR GetStatusElement(StatusElement::Parser * const apStatusElement) const;
108
109 protected:
110     // A recursively callable function to parse a data element and pretty-print it.
111     CHIP_ERROR ParseData(chip::TLV::TLVReader & aReader, int aDepth) const;
112 };
113
114 class Builder : public chip::app::Builder
115 {
116 public:
117     /**
118      *  @brief Initialize a AttributeDataList::Builder for writing into a TLV stream
119      *
120      *  @param [in] apWriter    A pointer to TLVWriter
121      *
122      *  @return #CHIP_NO_ERROR on success
123      */
124     CHIP_ERROR Init(chip::TLV::TLVWriter * const apWriter);
125
126     /**
127      *  @brief Initialize a CommandPath::Builder for writing into the TLV stream
128      *
129      *  @return A reference to CommandPath::Builder
130      */
131     CommandPath::Builder & CreateCommandPathBuilder();
132
133     /**
134      *  @brief Initialize a StatusElement::Builder for writing into the TLV stream
135      *
136      *  @return A reference to StatusElement::Builder
137      */
138     StatusElement::Builder & CreateStatusElementBuilder();
139
140     /**
141      *  @brief Mark the end of this CommandDataElement
142      *
143      *  @return A reference to *this
144      */
145     CommandDataElement::Builder & EndOfCommandDataElement();
146
147 private:
148     CommandPath::Builder mCommandPathBuilder;
149     StatusElement::Builder mStatusElementBuilder;
150 };
151 }; // namespace CommandDataElement
152 }; // namespace app
153 }; // namespace chip
154
155 #endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_COMMAND_DATA_ELEMENT_H