A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-ack-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 Fraunhofer FKIE
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author:
18 * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
19 * Modifications:
20 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
21 */
22
23#include "ns3/rng-seed-manager.h"
24#include <ns3/constant-position-mobility-model.h>
25#include <ns3/core-module.h>
26#include <ns3/log.h>
27#include <ns3/lr-wpan-module.h>
28#include <ns3/packet.h>
29#include <ns3/propagation-delay-model.h>
30#include <ns3/propagation-loss-model.h>
31#include <ns3/simulator.h>
32#include <ns3/single-model-spectrum-channel.h>
33
34#include <fstream>
35#include <iostream>
36#include <streambuf>
37#include <string>
38
39using namespace ns3;
40
41NS_LOG_COMPONENT_DEFINE("lr-wpan-ack-test");
42
43/**
44 * \ingroup lr-wpan
45 * \defgroup lr-wpan-test LrWpan module tests
46 */
47
48/**
49 * \ingroup lr-wpan-test
50 * \ingroup tests
51 *
52 * \brief LrWpan ACK Test
53 */
55{
56 public:
57 /**
58 * Test modes
59 */
61 {
62 EXTENDED_ADDRESS_UNICAST, //!< extended addresses
63 SHORT_ADDRESS_UNICAST, //!< short addresses, unicast
64 SHORT_ADDRESS_MULTICAST, //!< short addresses, multicast
65 SHORT_ADDRESS_BROADCAST, //!< short addresses, broadcast
66 };
67
68 /**
69 * Create test case
70 *
71 * \param prefix Unique file names prefix
72 * \param mode Test mode
73 */
74 LrWpanAckTestCase(const char* const prefix, TestMode_e mode);
75
76 /**
77 * \brief Function called when DataIndication is hit on dev0.
78 * \param params The MCPS params.
79 * \param p the packet.
80 */
82 /**
83 * \brief Function called when DataIndication is hit on dev1.
84 * \param params The MCPS params.
85 * \param p the packet.
86 */
88 /**
89 * \brief Function called when DataConfirm is hit on dev0.
90 * \param params The MCPS params.
91 */
93 /**
94 * \brief Function called when DataConfirm is hit on dev1.
95 * \param params The MCPS params.
96 */
98
99 private:
100 void DoRun() override;
101
102 std::string m_prefix; //!< Filename prefix
103 Time m_requestTime; //!< Request time.
104 Time m_requestSentTime; //!< Request successfully sent time.
105 Time m_replyTime; //!< Reply time.
106 Time m_replySentTime; //!< Reply successfully sent time.
107 Time m_replyArrivalTime; //!< Reply arrival time.
108 TestMode_e m_mode; //!< Test mode.
109 Ptr<LrWpanNetDevice> m_dev0; //!< 1st LrWpanNetDevice.
110 Ptr<LrWpanNetDevice> m_dev1; //!< 2nd LrWpanNetDevice.
111};
112
114 : TestCase("Test the 802.15.4 ACK handling")
115{
116 m_prefix = prefix;
119 m_replyTime = Seconds(0);
122 m_mode = mode;
123}
124
125void
127{
129}
130
131void
133{
134 Ptr<Packet> pkt = Create<Packet>(10); // 10 bytes of dummy data
135 McpsDataRequestParams replyParams;
136 replyParams.m_dstPanId = 0;
137 replyParams.m_msduHandle = 0;
138 replyParams.m_txOptions = TX_OPTION_NONE;
139
141 {
142 replyParams.m_srcAddrMode = EXT_ADDR;
143 replyParams.m_dstAddrMode = EXT_ADDR;
144 replyParams.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
145 }
146 else
147 {
148 replyParams.m_srcAddrMode = SHORT_ADDR;
149 replyParams.m_dstAddrMode = SHORT_ADDR;
150 replyParams.m_dstAddr = Mac16Address("00:01");
151 }
153 m_dev1->GetMac()->McpsDataRequest(replyParams, pkt);
154}
155
156void
158{
160}
161
162void
164{
166}
167
168void
170{
171 // Test setup:
172 // Two nodes well in communication range.
173 // Node 1 sends a request packet to node 2 with ACK request bit set. Node 2
174 // immediately answers with a reply packet on reception of the request.
175 // We expect the ACK of the request packet to always arrive at node 1 before
176 // the reply packet sent by node 2.
177 // This, of course, unelss the packet is sent to a broadcast or multicast address
178 // in this case we don't expect any ACK.
179
180 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
181 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
182
183 // Set the random seed and run number for this test
186
188
189 // Helper - used to create traces
190 LrWpanHelper helper;
191 std::string asciiPrefix;
192
193 // Create 2 nodes, and a NetDevice for each one
194 Ptr<Node> n0 = CreateObject<Node>();
195 Ptr<Node> n1 = CreateObject<Node>();
196
197 m_dev0 = CreateObject<LrWpanNetDevice>();
198 m_dev1 = CreateObject<LrWpanNetDevice>();
199
200 // Make random variable stream assignment deterministic
203
204 // Add short addresses.
205 m_dev0->SetAddress(Mac16Address("00:01"));
206 m_dev1->SetAddress(Mac16Address("00:02"));
207 // Add extended addresses.
208 m_dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
209 m_dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
210
211 // Each device must be attached to the same channel
212 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
214 CreateObject<LogDistancePropagationLossModel>();
216 CreateObject<ConstantSpeedPropagationDelayModel>();
217 channel->AddPropagationLossModel(propModel);
218 channel->SetPropagationDelayModel(delayModel);
219
220 m_dev0->SetChannel(channel);
221 m_dev1->SetChannel(channel);
222
223 // To complete configuration, a LrWpanNetDevice must be added to a node
224 n0->AddDevice(m_dev0);
225 n1->AddDevice(m_dev1);
226
227 Ptr<ConstantPositionMobilityModel> sender0Mobility =
228 CreateObject<ConstantPositionMobilityModel>();
229 sender0Mobility->SetPosition(Vector(0, 0, 0));
230 m_dev0->GetPhy()->SetMobility(sender0Mobility);
231 Ptr<ConstantPositionMobilityModel> sender1Mobility =
232 CreateObject<ConstantPositionMobilityModel>();
233 // Configure position 10 m distance
234 sender1Mobility->SetPosition(Vector(0, 10, 0));
235 m_dev1->GetPhy()->SetMobility(sender1Mobility);
236
239 m_dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
240
243 m_dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
244
247 m_dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
248
251 m_dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
252
253 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
255 uint8_t expectedAckCount = 0;
256 switch (m_mode)
257 {
259 params.m_srcAddrMode = SHORT_ADDR;
260 params.m_dstAddrMode = SHORT_ADDR;
261 params.m_dstAddr = Mac16Address("00:02");
262 expectedAckCount = 1;
263 break;
265 params.m_srcAddrMode = SHORT_ADDR;
266 params.m_dstAddrMode = SHORT_ADDR;
268 expectedAckCount = 0;
269 break;
271 params.m_srcAddrMode = SHORT_ADDR;
272 params.m_dstAddrMode = SHORT_ADDR;
273 params.m_dstAddr = Mac16Address::GetBroadcast();
274 expectedAckCount = 0;
275 break;
277 params.m_srcAddrMode = EXT_ADDR;
278 params.m_dstAddrMode = EXT_ADDR;
279 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
280 expectedAckCount = 1;
281 break;
282 }
283 params.m_dstPanId = 0;
284 params.m_msduHandle = 0;
285 params.m_txOptions = TX_OPTION_ACK;
288
291
292 std::ifstream traceFile(CreateTempDirFilename(m_prefix) + "-0-0.tr");
293 uint8_t ackCounter = 0;
294 std::string sub("Frame Type = 2");
295 for (std::string line; getline(traceFile, line);)
296 {
297 if (line.find(sub, 0) != std::string::npos)
298 {
299 ackCounter++;
300 }
301 }
302 traceFile.close();
303
304 // Note: the packet being correctly sent includes receiving an ACK in case of for unicact
305 // packets.
308 "Sent the request before the reply (as expected)");
309 NS_TEST_EXPECT_MSG_GT(m_requestSentTime, Time(0), "The request was sent (as expected)");
312 "The request was sent before the reply arrived (as expected)");
315 "The reply was sent before the reply arrived (as expected)");
316 NS_TEST_EXPECT_MSG_EQ(ackCounter,
317 expectedAckCount,
318 "The right amount of ACKs have been seen on the channel (as expected)");
319
320 m_dev0 = nullptr;
321 m_dev1 = nullptr;
322
324}
325
326/**
327 * \ingroup lr-wpan-test
328 * \ingroup tests
329 *
330 * \brief LrWpan ACK TestSuite
331 */
333{
334 public:
336};
337
339 : TestSuite("lr-wpan-ack", Type::UNIT)
340{
342 TestCase::Duration::QUICK);
345 TestCase::Duration::QUICK);
348 TestCase::Duration::QUICK);
351 TestCase::Duration::QUICK);
352}
353
354static LrWpanAckTestSuite g_lrWpanAckTestSuite; //!< Static variable for test initialization
LrWpan ACK Test.
@ SHORT_ADDRESS_MULTICAST
short addresses, multicast
@ SHORT_ADDRESS_BROADCAST
short addresses, broadcast
@ EXTENDED_ADDRESS_UNICAST
extended addresses
@ SHORT_ADDRESS_UNICAST
short addresses, unicast
Time m_requestSentTime
Request successfully sent time.
void DataIndicationDev0(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit on dev0.
Time m_requestTime
Request time.
Time m_replyTime
Reply time.
void DataIndicationDev1(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit on dev1.
std::string m_prefix
Filename prefix.
Time m_replySentTime
Reply successfully sent time.
void DataConfirmDev0(McpsDataConfirmParams params)
Function called when DataConfirm is hit on dev0.
Ptr< LrWpanNetDevice > m_dev0
1st LrWpanNetDevice.
LrWpanAckTestCase(const char *const prefix, TestMode_e mode)
Create test case.
void DoRun() override
Implementation to actually run this TestCase.
TestMode_e m_mode
Test mode.
Ptr< LrWpanNetDevice > m_dev1
2nd LrWpanNetDevice.
Time m_replyArrivalTime
Reply arrival time.
void DataConfirmDev1(McpsDataConfirmParams params)
Function called when DataConfirm is hit on dev1.
LrWpan ACK TestSuite.
void EnableAscii(std::string prefix, Ptr< NetDevice > nd, bool explicitFilename=false)
Enable ascii trace output on the indicated net device.
static Ipv6Address GetAllNodesMulticast()
Get the "all nodes multicast" address.
helps to manage and create IEEE 802.15.4 NetDevice objects
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
Definition: lr-wpan-mac.cc:384
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
static Mac16Address GetBroadcast()
an EUI-64 address
Definition: mac64-address.h:46
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition: test.cc:438
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:61
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:60
@ SHORT_ADDR
#define NS_TEST_EXPECT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report if not.
Definition: test.h:791
#define NS_TEST_EXPECT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report if not.
Definition: test.h:957
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static LrWpanAckTestSuite g_lrWpanAckTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
MCPS-DATA.confirm params.
MCPS-DATA.indication params.
MCPS-DATA.request params.
LrWpanAddressMode m_srcAddrMode
Source address mode.
Mac64Address m_dstExtAddr
Destination extended address.
LrWpanAddressMode m_dstAddrMode
Destination address mode.
uint16_t m_dstPanId
Destination PAN identifier.
Mac16Address m_dstAddr
Destination address.
uint8_t m_msduHandle
MSDU handle.
uint8_t m_txOptions
Tx Options (bitfield)