A Discrete-Event Network Simulator
API
lr-wpan-ack-test.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Fraunhofer FKIE
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author:
19  * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
20  */
21 
22 #include <ns3/log.h>
23 #include <ns3/core-module.h>
24 #include <ns3/lr-wpan-module.h>
25 #include <ns3/propagation-loss-model.h>
26 #include <ns3/propagation-delay-model.h>
27 #include <ns3/simulator.h>
28 #include <ns3/single-model-spectrum-channel.h>
29 #include <ns3/constant-position-mobility-model.h>
30 #include <ns3/packet.h>
31 #include "ns3/rng-seed-manager.h"
32 
33 #include <iostream>
34 
35 using namespace ns3;
36 
37 NS_LOG_COMPONENT_DEFINE ("lr-wpan-ack-test");
38 
51 {
52 public:
54 
69  static void DataConfirm (LrWpanAckTestCase *testCase, Ptr<LrWpanNetDevice> dev, McpsDataConfirmParams params);
77  static void ExtendedAddressingDataIndication (LrWpanAckTestCase *testCase, Ptr<LrWpanNetDevice> dev, McpsDataIndicationParams params, Ptr<Packet> p);
84  static void ExtendedAddressingDataConfirm (LrWpanAckTestCase *testCase, Ptr<LrWpanNetDevice> dev, McpsDataConfirmParams params);
85 
86 private:
87  virtual void DoRun (void);
88 
94 };
95 
97  : TestCase ("Test the 802.15.4 ACK handling")
98 {
99  m_requestTime = Seconds (0);
101  m_replyTime = Seconds (0);
102  m_replyAckTime = Seconds (0);
104 }
105 
106 void
108 {
109  if (dev->GetMac ()->GetShortAddress () == Mac16Address ("00:02"))
110  {
111  Ptr<Packet> p = Create<Packet> (10); // 10 bytes of dummy data
112  McpsDataRequestParams params;
113  params.m_srcAddrMode = SHORT_ADDR;
114  params.m_dstAddrMode = SHORT_ADDR;
115  params.m_dstPanId = 0;
116  params.m_dstAddr = Mac16Address ("00:01");
117  params.m_msduHandle = 0;
118  params.m_txOptions = TX_OPTION_NONE;
119 
120  testCase->m_replyTime = Simulator::Now ();
121  dev->GetMac ()->McpsDataRequest (params, p);
122  }
123  else
124  {
125  testCase->m_replyArrivalTime = Simulator::Now ();
126  }
127 }
128 
129 void
131 {
132  if (dev->GetMac ()->GetShortAddress () == Mac16Address ("00:01"))
133  {
134  testCase->m_requestAckTime = Simulator::Now ();
135  }
136  else
137  {
138  testCase->m_replyAckTime = Simulator::Now ();
139  }
140 }
141 
142 void
144 {
145  if (dev->GetMac ()->GetExtendedAddress () == Mac64Address ("00:00:00:00:00:00:00:02"))
146  {
147  Ptr<Packet> p = Create<Packet> (10); // 10 bytes of dummy data
148  McpsDataRequestParams params;
149  params.m_srcAddrMode = EXT_ADDR;
150  params.m_dstAddrMode = EXT_ADDR;
151  params.m_dstPanId = 0;
152  params.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:01");
153  params.m_msduHandle = 0;
154  params.m_txOptions = TX_OPTION_NONE;
155 
156  testCase->m_replyTime = Simulator::Now ();
157  dev->GetMac ()->McpsDataRequest (params, p);
158  }
159  else
160  {
161  testCase->m_replyArrivalTime = Simulator::Now ();
162  }
163 }
164 
165 void
167 {
168  if (dev->GetMac ()->GetExtendedAddress () == Mac64Address ("00:00:00:00:00:00:00:01"))
169  {
170  testCase->m_requestAckTime = Simulator::Now ();
171  }
172  else
173  {
174  testCase->m_replyAckTime = Simulator::Now ();
175  }
176 }
177 
178 void
180 {
181  // Test setup:
182  // Two nodes well in communication range.
183  // Node 1 sends a request packet to node 2 with ACK request bit set. Node 2
184  // immediately answers with a reply packet on reception of the request.
185  // We expect the ACK of the request packet to always arrive at node 1 before
186  // the reply packet sent by node 2.
187  // The same is repeated for extended addressing mode.
188 
189  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
190  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
191 
192  // Set the random seed and run number for this test
193  RngSeedManager::SetSeed (1);
194  RngSeedManager::SetRun (6);
195 
196  // Create 2 nodes, and a NetDevice for each one
197  Ptr<Node> n0 = CreateObject <Node> ();
198  Ptr<Node> n1 = CreateObject <Node> ();
199 
200  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
201  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
202 
203  // Make random variable stream assignment deterministic
204  dev0->AssignStreams (0);
205  dev1->AssignStreams (10);
206 
207  dev0->SetAddress (Mac16Address ("00:01"));
208  dev1->SetAddress (Mac16Address ("00:02"));
209 
210  // Each device must be attached to the same channel
211  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
212  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
213  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
214  channel->AddPropagationLossModel (propModel);
215  channel->SetPropagationDelayModel (delayModel);
216 
217  dev0->SetChannel (channel);
218  dev1->SetChannel (channel);
219 
220  // To complete configuration, a LrWpanNetDevice must be added to a node
221  n0->AddDevice (dev0);
222  n1->AddDevice (dev1);
223 
224  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
225  sender0Mobility->SetPosition (Vector (0,0,0));
226  dev0->GetPhy ()->SetMobility (sender0Mobility);
227  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
228  // Configure position 10 m distance
229  sender1Mobility->SetPosition (Vector (0,10,0));
230  dev1->GetPhy ()->SetMobility (sender1Mobility);
231 
234  dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
235 
238  dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1);
239 
242  dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2);
243 
246  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3);
247 
248  Ptr<Packet> p0 = Create<Packet> (50); // 50 bytes of dummy data
249  McpsDataRequestParams params;
250  params.m_srcAddrMode = SHORT_ADDR;
251  params.m_dstAddrMode = SHORT_ADDR;
252  params.m_dstPanId = 0;
253  params.m_dstAddr = Mac16Address ("00:02");
254  params.m_msduHandle = 0;
255  params.m_txOptions = TX_OPTION_ACK;
257  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p0);
258 
259 
260  Simulator::Run ();
261 
262  NS_TEST_EXPECT_MSG_LT (m_requestTime, m_replyTime, "Sent the request before the reply (as expected)");
263  NS_TEST_EXPECT_MSG_LT (m_requestAckTime, m_replyArrivalTime, "The request was ACKed before the reply arrived (as expected)");
264  NS_TEST_EXPECT_MSG_LT (m_replyAckTime, m_replyArrivalTime, "The reply was ACKed before the reply arrived (as expected)");
265 
266  // Test extended addressing.
267 
268  // Resetting the timers.
269  m_requestTime = Seconds (0);
271  m_replyTime = Seconds (0);
272  m_replyAckTime = Seconds (0);
274 
275  // Adding exteneded addresses.
276  dev0->GetMac()->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:01"));
277  dev1->GetMac()->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:02"));
278 
279  // Changing callbacks for those with exteneded addressing.
281  dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
282 
284  dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1);
285 
287  dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2);
288 
290  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3);
291 
292  Ptr<Packet> p1 = Create<Packet> (50); // 50 bytes of dummy data
293  params.m_srcAddrMode = EXT_ADDR;
294  params.m_dstAddrMode = EXT_ADDR;
295  params.m_dstPanId = 0;
296  params.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:02");
297  params.m_msduHandle = 0;
298  params.m_txOptions = TX_OPTION_ACK;
300  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p0);
301 
302 
303  Simulator::Run ();
304 
305  NS_TEST_EXPECT_MSG_LT (m_requestTime, m_replyTime, "ExtendedAddressing: Sent the request before the reply (as expected)");
306  NS_TEST_EXPECT_MSG_LT (m_requestAckTime, m_replyArrivalTime, "ExtendedAddressing: The request was ACKed before the reply arrived (as expected)");
307  NS_TEST_EXPECT_MSG_LT (m_replyAckTime, m_replyArrivalTime, "ExtendedAddressing: The reply was ACKed before the reply arrived (as expected)");
308 
309  Simulator::Destroy ();
310 }
311 
319 {
320 public:
322 };
323 
325  : TestSuite ("lr-wpan-ack", UNIT)
326 {
327  AddTestCase (new LrWpanAckTestCase, TestCase::QUICK);
328 }
329 
static LrWpanAckTestSuite g_lrWpanAckTestSuite
Static variable for test initialization.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
static void ExtendedAddressingDataIndication(LrWpanAckTestCase *testCase, Ptr< LrWpanNetDevice > dev, McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit in extended addressing test.
A suite of tests to run.
Definition: test.h:1342
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1686
Time m_replyArrivalTime
Reply arrival time.
Time m_requestAckTime
Request ack time.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
encapsulates test code
Definition: test.h:1155
static void DataIndication(LrWpanAckTestCase *testCase, Ptr< LrWpanNetDevice > dev, McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit.
channel
Definition: third.py:92
an EUI-64 address
Definition: mac64-address.h:43
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-data.cc:42
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:57
static void DataConfirm(LrWpanAckTestCase *testCase, Ptr< LrWpanNetDevice > dev, McpsDataConfirmParams params)
Function called when DataConfirm is hit.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
LrWpan ACK Test.
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:171
LrWpan ACK TestSuite.
uint8_t m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:184
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:157
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoRun(void)
Implementation to actually run this TestCase.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
void SetPosition(const Vector &position)
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
Time m_replyTime
Reply time.
#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:903
Time m_replyAckTime
Reply ack time.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:309
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
static void ExtendedAddressingDataConfirm(LrWpanAckTestCase *testCase, Ptr< LrWpanNetDevice > dev, McpsDataConfirmParams params)
Function called when DataConfirm is hit in extended addressing test.
Time m_requestTime
Request time.
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:56
static void DataConfirm(McpsDataConfirmParams params)
Definition: lr-wpan-data.cc:47
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:182