2 * Copyright (c) 2017, The OpenThread Authors.
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.
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.
31 * This file includes definition for Thread Management Framework(TMF) Tlv.
34 #ifndef OTBR_COMMON_TLV_HPP_
35 #define OTBR_COMMON_TLV_HPP_
37 #include "openthread-br/config.h"
45 * This class implements TMF Tlv functionality.
52 kLengthEscape = 0xff, ///< This length value indicates the actual length is of two-bytes length.
57 * This method returns the Tlv type.
59 * @returns The Tlv type.
62 uint8_t GetType(void) const { return mType; }
65 * This method sets the Tlv type.
68 void SetType(uint8_t aType) { mType = aType; }
71 * This method returns the Tlv length.
73 * @returns The Tlv length.
76 uint16_t GetLength(void) const
78 return (mLength != kLengthEscape ? mLength : static_cast<uint16_t>((&mLength)[1] << 8 | (&mLength)[2]));
82 * This method sets the length.
84 void SetLength(uint16_t aLength, bool aForceExtended = false)
86 if (aLength >= kLengthEscape || aForceExtended)
88 mLength = kLengthEscape;
89 (&mLength)[1] = (aLength >> 8);
90 (&mLength)[2] = (aLength & 0xff);
94 mLength = static_cast<uint8_t>(aLength);
99 * This method returns a pointer to the value.
101 * @returns The Tlv value.
104 const void *GetValue(void) const
106 return reinterpret_cast<const uint8_t *>(this) + sizeof(mType) +
107 (mLength != kLengthEscape ? sizeof(mLength) : (sizeof(uint16_t) + sizeof(mLength)));
111 * This method returns the value as a uint16_t.
113 * @returns The uint16_t value.
116 uint16_t GetValueUInt16(void) const
118 const uint8_t *p = static_cast<const uint8_t *>(GetValue());
120 return static_cast<uint16_t>(p[0] << 8 | p[1]);
124 * This method returns the value as a uint8_t.
126 * @returns The uint8_t value.
129 uint8_t GetValueUInt8(void) const { return *static_cast<const uint8_t *>(GetValue()); }
132 * This method sets uint16_t as the value.
134 * @param[in] aValue uint16_t value
137 void SetValue(uint16_t aValue)
141 SetLength(sizeof(aValue), false);
142 value = static_cast<uint8_t *>(GetValue());
143 value[0] = (aValue >> 8);
144 value[1] = (aValue & 0xff);
148 * This method sets uint8_t as the value.
150 * @param[in] aValue uint8_t value
153 void SetValue(uint8_t aValue)
155 SetLength(sizeof(aValue), false);
156 *static_cast<uint8_t *>(GetValue()) = aValue;
160 * This method sets int8_t as the value.
162 * @param[in] aValue int8_t value
165 void SetValue(int8_t aValue)
167 SetLength(sizeof(aValue), false);
168 *static_cast<int8_t *>(GetValue()) = aValue;
172 * This method copies the value.
174 void SetValue(const void *aValue, uint16_t aLength, bool aForceExtended = false)
176 SetLength(aLength, aForceExtended);
177 memcpy(GetValue(), aValue, aLength);
181 * This method returns the pointer to the next Tlv.
183 * @returns A pointer to the next Tlv.
186 const Tlv *GetNext(void) const
188 return reinterpret_cast<const Tlv *>(static_cast<const uint8_t *>(GetValue()) + GetLength());
192 * This method returns the pointer to the next Tlv.
194 * @returns A pointer to the next Tlv.
197 Tlv *GetNext(void) { return reinterpret_cast<Tlv *>(static_cast<uint8_t *>(GetValue()) + GetLength()); }
202 return reinterpret_cast<uint8_t *>(this) + sizeof(mType) +
203 (mLength != kLengthEscape ? sizeof(mLength) : (sizeof(uint16_t) + sizeof(mLength)));
214 kCommissionerId = 10,
215 kCommissionerSessionId = 11,
216 kJoinerDtlsEncapsulation = 17,
220 kJoinerRouterLocator = 20,
221 kJoinerRouterKek = 21,
222 kUdpEncapsulation = 48,
233 } // namespace Meshcop
237 #endif // OTBR_COMMON_TLV_HPP_