Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / MessageDef / Parser.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 parser in CHIP interaction model
21  *
22  */
23
24 #pragma once
25
26 #ifndef _CHIP_INTERACTION_MODEL_MESSAGE_DEF_PARSER_H
27 #define _CHIP_INTERACTION_MODEL_MESSAGE_DEF_PARSER_H
28
29 #include <core/CHIPCore.h>
30 #include <core/CHIPTLV.h>
31 #include <support/CodeUtils.h>
32 #include <support/logging/CHIPLogging.h>
33 #include <util/basic-types.h>
34
35 namespace chip {
36 namespace app {
37 class Parser
38 {
39 public:
40     /**
41      *  @brief Initialize the Builder object with TLVReader and ContainerType
42      *
43      *  @param [in] aReader TLVReader
44      *  @param [in] aOuterContainerType outer container type
45      *
46      */
47     void Init(const chip::TLV::TLVReader & aReader, chip::TLV::TLVType aOuterContainerType);
48
49     /**
50      *  @brief Initialize a TLVReader to point to the beginning of any tagged element in this request
51      *
52      *  @param [in]  aTagToFind Tag to find in the request
53      *  @param [out] apReader   A pointer to TLVReader, which will be initialized at the specified TLV element
54      *                          on success
55      *
56      *  @return #CHIP_NO_ERROR on success
57      */
58     CHIP_ERROR GetReaderOnTag(const uint64_t aTagToFind, chip::TLV::TLVReader * const apReader) const;
59
60     /**
61      *  @brief Get the TLV Reader
62      *
63      *  @param [in] aReader A pointer to a TLVReader
64      *
65      */
66     void GetReader(chip::TLV::TLVReader * const apReader);
67
68 protected:
69     chip::TLV::TLVReader mReader;
70     chip::TLV::TLVType mOuterContainerType;
71     Parser();
72
73     template <typename T>
74     CHIP_ERROR GetUnsignedInteger(const uint8_t aContextTag, T * const apLValue) const
75     {
76         return GetSimpleValue(aContextTag, chip::TLV::kTLVType_UnsignedInteger, apLValue);
77     };
78
79     template <typename T>
80     CHIP_ERROR GetSimpleValue(const uint8_t aContextTag, const chip::TLV::TLVType aTLVType, T * const apLValue) const
81     {
82         CHIP_ERROR err = CHIP_NO_ERROR;
83         chip::TLV::TLVReader reader;
84
85         *apLValue = 0;
86
87         err = mReader.FindElementWithTag(chip::TLV::ContextTag(aContextTag), reader);
88         SuccessOrExit(err);
89
90         VerifyOrExit(aTLVType == reader.GetType(), err = CHIP_ERROR_WRONG_TLV_TYPE);
91
92         err = reader.Get(*apLValue);
93         SuccessOrExit(err);
94
95     exit:
96         ChipLogIfFalse((CHIP_NO_ERROR == err) || (CHIP_END_OF_TLV == err));
97
98         return err;
99     };
100 };
101 }; // namespace app
102 }; // namespace chip
103
104 #endif // _CHIP_INTERACTION_MODEL_MESSAGE_DEF_PARSER_H