openthread-br  0.3.0-72c0388
tlv.hpp
Go to the documentation of this file.
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 
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 
48 class Tlv
49 {
50  enum
51  {
52  kLengthEscape = 0xff,
53  };
54 
55 public:
62  uint8_t GetType(void) const { return mType; }
63 
68  void SetType(uint8_t aType) { mType = aType; }
69 
76  uint16_t GetLength(void) const
77  {
78  return (mLength != kLengthEscape ? mLength : static_cast<uint16_t>((&mLength)[1] << 8 | (&mLength)[2]));
79  }
80 
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 
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 
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 
129  uint8_t GetValueUInt8(void) const { return *static_cast<const uint8_t *>(GetValue()); }
130 
137  void SetValue(uint64_t aValue)
138  {
139  uint8_t *value;
140 
141  SetLength(sizeof(aValue), false);
142  value = static_cast<uint8_t *>(GetValue());
143  for (int i = 0; i < int{sizeof(aValue)}; ++i)
144  {
145  value[i] = (aValue >> (8 * (sizeof(aValue) - i - 1))) & 0xff;
146  }
147  }
148 
155  void SetValue(uint32_t aValue)
156  {
157  uint8_t *value;
158 
159  SetLength(sizeof(aValue), false);
160  value = static_cast<uint8_t *>(GetValue());
161  for (int i = 0; i < int{sizeof(aValue)}; ++i)
162  {
163  value[i] = (aValue >> (8 * (sizeof(aValue) - i - 1))) & 0xff;
164  }
165  }
166 
173  void SetValue(uint16_t aValue)
174  {
175  uint8_t *value;
176 
177  SetLength(sizeof(aValue), false);
178  value = static_cast<uint8_t *>(GetValue());
179  value[0] = (aValue >> 8);
180  value[1] = (aValue & 0xff);
181  }
182 
189  void SetValue(uint8_t aValue)
190  {
191  SetLength(sizeof(aValue), false);
192  *static_cast<uint8_t *>(GetValue()) = aValue;
193  }
194 
201  void SetValue(int8_t aValue)
202  {
203  SetLength(sizeof(aValue), false);
204  *static_cast<int8_t *>(GetValue()) = aValue;
205  }
206 
210  void SetValue(const void *aValue, uint16_t aLength, bool aForceExtended = false)
211  {
212  SetLength(aLength, aForceExtended);
213  memcpy(GetValue(), aValue, aLength);
214  }
215 
222  const Tlv *GetNext(void) const
223  {
224  return reinterpret_cast<const Tlv *>(static_cast<const uint8_t *>(GetValue()) + GetLength());
225  }
226 
233  Tlv *GetNext(void) { return reinterpret_cast<Tlv *>(static_cast<uint8_t *>(GetValue()) + GetLength()); }
234 
235 private:
236  void *GetValue(void)
237  {
238  return reinterpret_cast<uint8_t *>(this) + sizeof(mType) +
239  (mLength != kLengthEscape ? sizeof(mLength) : (sizeof(uint16_t) + sizeof(mLength)));
240  }
241  uint8_t mType;
242  uint8_t mLength;
243 };
244 
245 namespace Meshcop {
246 
247 enum
248 {
249  kState = 16,
250  kCommissionerId = 10,
251  kCommissionerSessionId = 11,
252  kJoinerDtlsEncapsulation = 17,
253  kSteeringData = 8,
254  kJoinerUdpPort = 18,
255  kJoinerIid = 19,
256  kJoinerRouterLocator = 20,
257  kJoinerRouterKek = 21,
258  kUdpEncapsulation = 48,
259  kIPv6Address = 49,
260 };
261 
262 enum
263 {
264  kStateAccepted = 1,
265  kStatePending = 0,
266  kStateRejected = -1,
267 };
268 
269 } // namespace Meshcop
270 
271 } // namespace otbr
272 
273 #endif // OTBR_COMMON_TLV_HPP_
otbr::Tlv::SetValue
void SetValue(const void *aValue, uint16_t aLength, bool aForceExtended=false)
Definition: tlv.hpp:210
otbr::Tlv::SetLength
void SetLength(uint16_t aLength, bool aForceExtended=false)
Definition: tlv.hpp:84
otbr::Tlv
Definition: tlv.hpp:48
otbr::Tlv::GetNext
const Tlv * GetNext(void) const
Definition: tlv.hpp:222
otbr::Tlv::GetValueUInt8
uint8_t GetValueUInt8(void) const
Definition: tlv.hpp:129
otbr::Tlv::GetValueUInt16
uint16_t GetValueUInt16(void) const
Definition: tlv.hpp:116
otbr::Tlv::GetNext
Tlv * GetNext(void)
Definition: tlv.hpp:233
otbr::Tlv::SetValue
void SetValue(uint8_t aValue)
Definition: tlv.hpp:189
otbr::Tlv::SetValue
void SetValue(uint32_t aValue)
Definition: tlv.hpp:155
otbr::Tlv::SetValue
void SetValue(uint64_t aValue)
Definition: tlv.hpp:137
otbr::Tlv::GetType
uint8_t GetType(void) const
Definition: tlv.hpp:62
otbr::Tlv::GetLength
uint16_t GetLength(void) const
Definition: tlv.hpp:76
otbr::Tlv::SetValue
void SetValue(uint16_t aValue)
Definition: tlv.hpp:173
config.h
otbr::Tlv::GetValue
const void * GetValue(void) const
Definition: tlv.hpp:104
otbr::Tlv::SetValue
void SetValue(int8_t aValue)
Definition: tlv.hpp:201
otbr::Tlv::SetType
void SetType(uint8_t aType)
Definition: tlv.hpp:68