0.01.00
tlvs.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, 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 TLVS_HPP_
35 #define TLVS_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include "utils/wrap_string.h"
40 
41 #include <openthread/types.h>
42 
43 #include "common/encoding.hpp"
44 #include "common/message.hpp"
45 
46 using ot::Encoding::BigEndian::HostSwap16;
47 using ot::Encoding::BigEndian::HostSwap32;
48 
49 namespace ot {
50 
56 class Tlv
57 {
58 public:
63  Tlv(void):
64  mType(0),
65  mLength(0) {}
66 
73  uint8_t GetType(void) const { return mType; }
74 
81  void SetType(uint8_t aType) { mType = aType; }
82 
89  uint8_t GetLength(void) const { return mLength; }
90 
97  void SetLength(uint8_t aLength) { mLength = aLength; }
98 
105  uint8_t GetSize(void) const { return sizeof(Tlv) + mLength; }
106 
113  uint8_t *GetValue(void) { return reinterpret_cast<uint8_t *>(this) + sizeof(Tlv); }
114 
121  const uint8_t *GetValue(void) const { return reinterpret_cast<const uint8_t *>(this) + sizeof(Tlv); }
122 
129  Tlv *GetNext(void) {
130  return reinterpret_cast<Tlv *>(reinterpret_cast<uint8_t *>(this) + sizeof(*this) + mLength);
131  }
132 
139  const Tlv *GetNext(void) const {
140  return reinterpret_cast<const Tlv *>(reinterpret_cast<const uint8_t *>(this) + sizeof(*this) + mLength);
141  }
142 
155  static otError Get(const Message &aMessage, uint8_t aType, uint16_t aMaxLength, Tlv &aTlv);
156 
168  static otError GetOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset);
169 
182  static otError GetValueOffset(const Message &aMesasge, uint8_t aType, uint16_t &aOffset, uint16_t &aLength);
183 
184 protected:
189  enum
190  {
192  };
193 
194 private:
195  uint8_t mType;
196  uint8_t mLength;
198 
200 class ExtendedTlv: public Tlv
201 {
202 public:
207  uint16_t GetLength(void) const { return HostSwap16(mLength); }
208 
215  void SetLength(uint16_t aLength) { Tlv::SetLength(kExtendedLength); mLength = HostSwap16(aLength); }
216 
217 private:
218  uint16_t mLength;
220 
221 } // namespace ot
222 
223 #endif // TLVS_HPP_
Definition: cli.cpp:90
This file defines the types and structures used in the OpenThread library API.
Definition: tlvs.hpp:200
This file is a wrapper for the standard "string.h" file Some platforms provide all required functions...
const Tlv * GetNext(void) const
This method returns a pointer to the next TLV.
Definition: tlvs.hpp:139
Extended Length value.
Definition: tlvs.hpp:191
static otError Get(const Message &aMessage, uint8_t aType, uint16_t aMaxLength, Tlv &aTlv)
This static method reads the requested TLV out of aMessage.
Definition: tlvs.cpp:41
static otError GetValueOffset(const Message &aMesasge, uint8_t aType, uint16_t &aOffset, uint16_t &aLength)
This static method finds the offset and length of a given TLV type.
Definition: tlvs.cpp:100
void SetType(uint8_t aType)
This method sets the Type value.
Definition: tlvs.hpp:81
#define OT_TOOL_PACKED_BEGIN
Compiler-specific indication that a class or struct must be byte packed.
Definition: toolchain.h:170
uint16_t GetLength(void) const
This method returns the Length value.
Definition: tlvs.hpp:207
This class represents a message.
Definition: message.hpp:195
This file includes definitions for the message buffer pool and message buffers.
const uint8_t * GetValue(void) const
This method returns a pointer to the Value.
Definition: tlvs.hpp:121
uint8_t * GetValue(void)
This method returns a pointer to the Value.
Definition: tlvs.hpp:113
This file includes definitions for byte-ordering encoding.
Tlv * GetNext(void)
This method returns a pointer to the next TLV.
Definition: tlvs.hpp:129
void SetLength(uint16_t aLength)
This method sets the Length value.
Definition: tlvs.hpp:215
static otError GetOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset)
This static method obtains the offset of a TLV within aMessage.
Definition: tlvs.cpp:60
Tlv(void)
Default constructor.
Definition: tlvs.hpp:63
#define OT_TOOL_PACKED_END
Compiler-specific indication at the end of a byte packed class or struct.
Definition: toolchain.h:172
void SetLength(uint8_t aLength)
This method sets the Length value.
Definition: tlvs.hpp:97
uint8_t GetType(void) const
This method returns the Type value.
Definition: tlvs.hpp:73
This class implements TLV generation and parsing.
Definition: tlvs.hpp:56
uint8_t GetSize(void) const
This method returns the total size including Type, Length, and Value fields.
Definition: tlvs.hpp:105
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
uint8_t GetLength(void) const
This method returns the Length value.
Definition: tlvs.hpp:89
This file includes compile-time configuration constants for OpenThread.