Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / common / tlv.hpp
1 /*
2  *    Copyright (c) 2017, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /**
30  * @file
31  *   This file includes definition for Thread Management Framework(TMF) Tlv.
32  */
33
34 #ifndef OTBR_COMMON_TLV_HPP_
35 #define OTBR_COMMON_TLV_HPP_
36
37 #include "openthread-br/config.h"
38
39 #include <stdint.h>
40 #include <string.h>
41
42 namespace otbr {
43
44 /**
45  * This class implements TMF Tlv functionality.
46  *
47  */
48 class Tlv
49 {
50     enum
51     {
52         kLengthEscape = 0xff, ///< This length value indicates the actual length is of two-bytes length.
53     };
54
55 public:
56     /**
57      * This method returns the Tlv type.
58      *
59      * @returns The Tlv type.
60      *
61      */
62     uint8_t GetType(void) const { return mType; }
63
64     /**
65      * This method sets the Tlv type.
66      *
67      */
68     void SetType(uint8_t aType) { mType = aType; }
69
70     /**
71      * This method returns the Tlv length.
72      *
73      * @returns The Tlv length.
74      *
75      */
76     uint16_t GetLength(void) const
77     {
78         return (mLength != kLengthEscape ? mLength : static_cast<uint16_t>((&mLength)[1] << 8 | (&mLength)[2]));
79     }
80
81     /**
82      * This method sets the length.
83      */
84     void SetLength(uint16_t aLength, bool aForceExtended = false)
85     {
86         if (aLength >= kLengthEscape || aForceExtended)
87         {
88             mLength       = kLengthEscape;
89             (&mLength)[1] = (aLength >> 8);
90             (&mLength)[2] = (aLength & 0xff);
91         }
92         else
93         {
94             mLength = static_cast<uint8_t>(aLength);
95         }
96     }
97
98     /**
99      * This method returns a pointer to the value.
100      *
101      * @returns The Tlv value.
102      *
103      */
104     const void *GetValue(void) const
105     {
106         return reinterpret_cast<const uint8_t *>(this) + sizeof(mType) +
107                (mLength != kLengthEscape ? sizeof(mLength) : (sizeof(uint16_t) + sizeof(mLength)));
108     }
109
110     /**
111      * This method returns the value as a uint16_t.
112      *
113      * @returns The uint16_t value.
114      *
115      */
116     uint16_t GetValueUInt16(void) const
117     {
118         const uint8_t *p = static_cast<const uint8_t *>(GetValue());
119
120         return static_cast<uint16_t>(p[0] << 8 | p[1]);
121     }
122
123     /**
124      * This method returns the value as a uint8_t.
125      *
126      * @returns The uint8_t value.
127      *
128      */
129     uint8_t GetValueUInt8(void) const { return *static_cast<const uint8_t *>(GetValue()); }
130
131     /**
132      * This method sets uint16_t as the value.
133      *
134      * @param[in]    aValue         uint16_t value
135      *
136      */
137     void SetValue(uint16_t aValue)
138     {
139         uint8_t *value;
140
141         SetLength(sizeof(aValue), false);
142         value    = static_cast<uint8_t *>(GetValue());
143         value[0] = (aValue >> 8);
144         value[1] = (aValue & 0xff);
145     }
146
147     /**
148      * This method sets uint8_t as the value.
149      *
150      * @param[in]    aValue         uint8_t value
151      *
152      */
153     void SetValue(uint8_t aValue)
154     {
155         SetLength(sizeof(aValue), false);
156         *static_cast<uint8_t *>(GetValue()) = aValue;
157     }
158
159     /**
160      * This method sets int8_t as the value.
161      *
162      * @param[in]    aValue         int8_t value
163      *
164      */
165     void SetValue(int8_t aValue)
166     {
167         SetLength(sizeof(aValue), false);
168         *static_cast<int8_t *>(GetValue()) = aValue;
169     }
170
171     /**
172      * This method copies the value.
173      */
174     void SetValue(const void *aValue, uint16_t aLength, bool aForceExtended = false)
175     {
176         SetLength(aLength, aForceExtended);
177         memcpy(GetValue(), aValue, aLength);
178     }
179
180     /**
181      * This method returns the pointer to the next Tlv.
182      *
183      * @returns A pointer to the next Tlv.
184      *
185      */
186     const Tlv *GetNext(void) const
187     {
188         return reinterpret_cast<const Tlv *>(static_cast<const uint8_t *>(GetValue()) + GetLength());
189     }
190
191     /**
192      * This method returns the pointer to the next Tlv.
193      *
194      * @returns A pointer to the next Tlv.
195      *
196      */
197     Tlv *GetNext(void) { return reinterpret_cast<Tlv *>(static_cast<uint8_t *>(GetValue()) + GetLength()); }
198
199 private:
200     void *GetValue(void)
201     {
202         return reinterpret_cast<uint8_t *>(this) + sizeof(mType) +
203                (mLength != kLengthEscape ? sizeof(mLength) : (sizeof(uint16_t) + sizeof(mLength)));
204     }
205     uint8_t mType;
206     uint8_t mLength;
207 };
208
209 namespace Meshcop {
210
211 enum
212 {
213     kState                   = 16,
214     kCommissionerId          = 10,
215     kCommissionerSessionId   = 11,
216     kJoinerDtlsEncapsulation = 17,
217     kSteeringData            = 8,
218     kJoinerUdpPort           = 18,
219     kJoinerIid               = 19,
220     kJoinerRouterLocator     = 20,
221     kJoinerRouterKek         = 21,
222     kUdpEncapsulation        = 48,
223     kIPv6Address             = 49,
224 };
225
226 enum
227 {
228     kStateAccepted = 1,
229     kStatePending  = 0,
230     kStateRejected = -1,
231 };
232
233 } // namespace Meshcop
234
235 } // namespace otbr
236
237 #endif // OTBR_COMMON_TLV_HPP_