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 29 #ifndef TEST_LOWPAN_HPP 30 #define TEST_LOWPAN_HPP 31 32 #include <stdint.h> 33 34 #include "common/code_utils.hpp" 35 #include "instance/instance.hpp" 36 #include "mac/mac.hpp" 37 #include "net/ip6_headers.hpp" 38 #include "thread/lowpan.hpp" 39 #include "thread/thread_netif.hpp" 40 41 namespace ot { 42 43 class TestIphcVector 44 { 45 public: 46 enum 47 { 48 kContextUnused = 255, 49 kPayloadMaxLength = 512 50 }; 51 52 struct Payload 53 { 54 uint8_t mData[kPayloadMaxLength]; 55 uint16_t mLength; 56 }; 57 58 /** 59 * Default constructor for the object. 60 */ TestIphcVector(const char * aTestName)61 explicit TestIphcVector(const char *aTestName) 62 { 63 memset(reinterpret_cast<void *>(this), 0, sizeof(TestIphcVector)); 64 mTestName = aTestName; 65 mSrcContext.mContextId = kContextUnused; 66 mDstContext.mContextId = kContextUnused; 67 } 68 69 /** 70 * Sets long MAC source address. 71 * 72 * @param aAddress Pointer to the long MAC address. 73 */ SetMacSource(const uint8_t * aAddress)74 void SetMacSource(const uint8_t *aAddress) { mMacAddrs.mSource.SetExtended(aAddress); } 75 76 /** 77 * Sets short MAC source address. 78 * 79 * @param aAddress Short MAC address. 80 */ SetMacSource(uint16_t aAddress)81 void SetMacSource(uint16_t aAddress) { mMacAddrs.mSource.SetShort(aAddress); } 82 83 /** 84 * Sets long MAC destination address. 85 * 86 * @param aAddress Pointer to the long MAC address. 87 */ SetMacDestination(const uint8_t * aAddress)88 void SetMacDestination(const uint8_t *aAddress) { mMacAddrs.mDestination.SetExtended(aAddress); } 89 90 /** 91 * Sets short MAC destination address. 92 * 93 * @param aAddress Short MAC address. 94 */ SetMacDestination(uint16_t aAddress)95 void SetMacDestination(uint16_t aAddress) { mMacAddrs.mDestination.SetShort(aAddress); } 96 97 /** 98 * Gets the IPv6 header 99 * 100 * @returns the IPv6 header. 101 */ GetIpHeader(void) const102 const Ip6::Header &GetIpHeader(void) const { return mIpHeader; } 103 104 /** 105 * Initializes IPv6 Header. 106 * 107 * @param aVersionClassFlow Value of the Version, Traffic class and Flow control fields. 108 * @param aPayloadLength Value of the payload length field. 109 * @param aNextHeader Value of the next header field. 110 * @param aHopLimit Value of the hop limit field. 111 * @param aSource String represents IPv6 source address. 112 * @param aDestination String represents IPv6 destination address. 113 */ SetIpHeader(uint32_t aVersionClassFlow,uint16_t aPayloadLength,uint8_t aNextHeader,uint8_t aHopLimit,const char * aSource,const char * aDestination)114 void SetIpHeader(uint32_t aVersionClassFlow, 115 uint16_t aPayloadLength, 116 uint8_t aNextHeader, 117 uint8_t aHopLimit, 118 const char *aSource, 119 const char *aDestination) 120 { 121 mIpHeader.SetVerionTrafficClassFlow(aVersionClassFlow); 122 mIpHeader.SetPayloadLength(aPayloadLength); 123 mIpHeader.SetNextHeader(aNextHeader); 124 mIpHeader.SetHopLimit(aHopLimit); 125 IgnoreError(mIpHeader.GetSource().FromString(aSource)); 126 IgnoreError(mIpHeader.GetDestination().FromString(aDestination)); 127 } 128 129 /** 130 * Initializes IPv6 Encapsulated Header. 131 * 132 * @param aVersionClassFlow Value of the Version, Traffic class and Flow control fields. 133 * @param aPayloadLength Value of the payload length field. 134 * @param aNextHeader Value of the next header field. 135 * @param aHopLimit Value of the hop limit field. 136 * @param aSource String represents IPv6 source address. 137 * @param aDestination String represents IPv6 destination address. 138 */ SetIpTunneledHeader(uint32_t aVersionClassFlow,uint16_t aPayloadLength,uint8_t aNextHeader,uint8_t aHopLimit,const char * aSource,const char * aDestination)139 void SetIpTunneledHeader(uint32_t aVersionClassFlow, 140 uint16_t aPayloadLength, 141 uint8_t aNextHeader, 142 uint8_t aHopLimit, 143 const char *aSource, 144 const char *aDestination) 145 { 146 mIpTunneledHeader.SetVerionTrafficClassFlow(aVersionClassFlow); 147 mIpTunneledHeader.SetPayloadLength(aPayloadLength); 148 mIpTunneledHeader.SetNextHeader(aNextHeader); 149 mIpTunneledHeader.SetHopLimit(aHopLimit); 150 IgnoreError(mIpTunneledHeader.GetSource().FromString(aSource)); 151 IgnoreError(mIpTunneledHeader.GetDestination().FromString(aDestination)); 152 } 153 154 /** 155 * Initializes IPv6 Extension Header. 156 * 157 * @param aExtHeader Pointer to the extension header data. 158 * @param aExtHeaderLength Length of the extension header data. 159 */ SetExtHeader(const uint8_t * aExtHeader,uint16_t aExtHeaderLength)160 void SetExtHeader(const uint8_t *aExtHeader, uint16_t aExtHeaderLength) 161 { 162 memcpy(mExtHeader.mData, aExtHeader, aExtHeaderLength); 163 mExtHeader.mLength = aExtHeaderLength; 164 } 165 166 /** 167 * Initializes UDP Header. 168 * 169 * @param aSource Value of the source port. 170 * @param aDestination Value of the destination port. 171 * @param aLength Value of the length field. 172 * @param aChecksum Value of the checksum field. 173 */ SetUDPHeader(uint16_t aSource,uint16_t aDestination,uint16_t aLength,uint16_t aChecksum)174 void SetUDPHeader(uint16_t aSource, uint16_t aDestination, uint16_t aLength, uint16_t aChecksum) 175 { 176 mUdpHeader.SetSourcePort(aSource); 177 mUdpHeader.SetDestinationPort(aDestination); 178 mUdpHeader.SetLength(aLength); 179 mUdpHeader.SetChecksum(aChecksum); 180 } 181 182 /** 183 * Initializes LOWPAN_IPHC Header. 184 * 185 * @param aIphc Pointer to the LOWPAN_IPHC header. 186 * @param aIphcLength Length of the LOWPAN_IPHC header. 187 */ SetIphcHeader(const uint8_t * aIphc,uint16_t aIphcLength)188 void SetIphcHeader(const uint8_t *aIphc, uint16_t aIphcLength) 189 { 190 memcpy(mIphcHeader.mData, aIphc, aIphcLength); 191 mIphcHeader.mLength = aIphcLength; 192 } 193 194 /** 195 * Sets the expect result of the compression / decompression procedure. 196 * 197 * @param aError Expected result. 198 */ SetError(Error aError)199 void SetError(Error aError) { mError = aError; } 200 201 /** 202 * Initializes IPv6 Payload (uncompressed data). 203 * 204 * @param aPayload Pointer to the payload data. 205 * @param aLength Length of the payload data. 206 */ SetPayload(const uint8_t * aPayload,uint16_t aLength)207 void SetPayload(const uint8_t *aPayload, uint16_t aLength) 208 { 209 memcpy(mPayload.mData, aPayload, aLength); 210 mPayload.mLength = aLength; 211 } 212 213 /** 214 * Sets the offset from the beginning of the IPv6 header to the uncompressed 215 * payload. 216 * 217 * @param aPayloadOffset The offset from the beginning of the IPv6 header to the uncompressed 218 * payload. 219 */ SetPayloadOffset(uint16_t aPayloadOffset)220 void SetPayloadOffset(uint16_t aPayloadOffset) { mPayloadOffset = aPayloadOffset; } 221 222 /** 223 * Returns compressed LOWPAN_IPHC frame. 224 * 225 * @returns The compressed stream. 226 */ 227 void GetCompressedStream(uint8_t *aIphc, uint16_t &aIphcLength); 228 229 /** 230 * Returns message object with the uncompressed IPv6 packet. 231 * 232 * @returns The message object with the uncompressed IPv6 packet. 233 */ 234 void GetUncompressedStream(Message &aMessage); 235 236 /** 237 * Returns data with the uncompressed IPv6 packet. 238 * 239 * @returns The data with the uncompressed IPv6 packet. 240 */ 241 void GetUncompressedStream(uint8_t *aIp6, uint16_t &aIp6Length); 242 243 /** 244 * This fields represent uncompressed IPv6 packet. 245 */ 246 Mac::Addresses mMacAddrs; 247 Ip6::Header mIpHeader; 248 Payload mExtHeader; 249 Ip6::Header mIpTunneledHeader; 250 Ip6::Udp::Header mUdpHeader; 251 252 /** 253 * This fields represent compressed IPv6 packet. 254 */ 255 Payload mIphcHeader; 256 uint16_t mPayloadOffset; 257 Lowpan::Context mSrcContext; 258 Lowpan::Context mDstContext; 259 260 /** 261 * General purpose fields. 262 */ 263 Payload mPayload; 264 Error mError; 265 const char *mTestName; 266 }; 267 268 } // namespace ot 269 270 #endif // TEST_LOWPAN_HPP 271