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);
70 
71 private:
72  virtual void DoRun (void);
73 
79 };
80 
82  : TestCase ("Test the 802.15.4 ACK handling")
83 {
84  m_requestTime = Seconds (0);
86  m_replyTime = Seconds (0);
87  m_replyAckTime = Seconds (0);
89 }
90 
91 void
93 {
94  if (dev->GetAddress () == Mac16Address ("00:02"))
95  {
96  Ptr<Packet> p = Create<Packet> (10); // 10 bytes of dummy data
97  McpsDataRequestParams params;
98  params.m_srcAddrMode = SHORT_ADDR;
99  params.m_dstAddrMode = SHORT_ADDR;
100  params.m_dstPanId = 0;
101  params.m_dstAddr = Mac16Address ("00:01");
102  params.m_msduHandle = 0;
103  params.m_txOptions = TX_OPTION_NONE;
104 
105  testCase->m_replyTime = Simulator::Now ();
106  dev->GetMac ()->McpsDataRequest (params, p);
107  }
108  else
109  {
110  testCase->m_replyArrivalTime = Simulator::Now ();
111  }
112 }
113 
114 void
116 {
117  if (dev->GetAddress () == Mac16Address ("00:01"))
118  {
119  testCase->m_requestAckTime = Simulator::Now ();
120  }
121  else
122  {
123  testCase->m_replyAckTime = Simulator::Now ();
124  }
125 }
126 
127 void
129 {
130  // Test setup:
131  // Two nodes well in communication range.
132  // Node 1 sends a request packet to node 2 with ACK request bit set. Node 2
133  // immediately answers with a reply packet on reception of the request.
134  // We expect the ACK of the request packet to always arrive at node 1 before
135  // the reply packet sent by node 2.
136 
137  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
138  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
139 
140  // Set the random seed and run number for this test
141  RngSeedManager::SetSeed (1);
142  RngSeedManager::SetRun (6);
143 
144  // Create 2 nodes, and a NetDevice for each one
145  Ptr<Node> n0 = CreateObject <Node> ();
146  Ptr<Node> n1 = CreateObject <Node> ();
147 
148  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
149  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
150 
151  // Make random variable stream assignment deterministic
152  dev0->AssignStreams (0);
153  dev1->AssignStreams (10);
154 
155  dev0->SetAddress (Mac16Address ("00:01"));
156  dev1->SetAddress (Mac16Address ("00:02"));
157 
158  // Each device must be attached to the same channel
159  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
160  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
161  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
162  channel->AddPropagationLossModel (propModel);
163  channel->SetPropagationDelayModel (delayModel);
164 
165  dev0->SetChannel (channel);
166  dev1->SetChannel (channel);
167 
168  // To complete configuration, a LrWpanNetDevice must be added to a node
169  n0->AddDevice (dev0);
170  n1->AddDevice (dev1);
171 
172  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
173  sender0Mobility->SetPosition (Vector (0,0,0));
174  dev0->GetPhy ()->SetMobility (sender0Mobility);
175  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
176  // Configure position 10 m distance
177  sender1Mobility->SetPosition (Vector (0,10,0));
178  dev1->GetPhy ()->SetMobility (sender1Mobility);
179 
182  dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
183 
186  dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1);
187 
190  dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2);
191 
194  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3);
195 
196  Ptr<Packet> p0 = Create<Packet> (50); // 50 bytes of dummy data
197  McpsDataRequestParams params;
198  params.m_srcAddrMode = SHORT_ADDR;
199  params.m_dstAddrMode = SHORT_ADDR;
200  params.m_dstPanId = 0;
201  params.m_dstAddr = Mac16Address ("00:02");
202  params.m_msduHandle = 0;
203  params.m_txOptions = TX_OPTION_ACK;
205  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p0);
206 
207 
208  Simulator::Run ();
209 
210  NS_TEST_EXPECT_MSG_LT (m_requestTime, m_replyTime, "Sent the request before the reply (as expected)");
211  NS_TEST_EXPECT_MSG_LT (m_requestAckTime, m_replyArrivalTime, "The request was ACKed before the reply arrived (as expected)");
212  NS_TEST_EXPECT_MSG_LT (m_replyAckTime, m_replyArrivalTime, "The reply was ACKed before the reply arrived (as expected)");
213 
214  Simulator::Destroy ();
215 }
216 
224 {
225 public:
227 };
228 
230  : TestSuite ("lr-wpan-ack", UNIT)
231 {
232  AddTestCase (new LrWpanAckTestCase, TestCase::QUICK);
233 }
234 
tuple channel
Definition: third.py:85
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
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:201
encapsulates test code
Definition: test.h:1155
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Add the single-frequency propagation loss model to be used.
static void DataIndication(LrWpanAckTestCase *testCase, Ptr< LrWpanNetDevice > dev, McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit.
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:170
LrWpan ACK TestSuite.
uint8_t m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:183
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)
virtual Address GetAddress(void) const
This method indirects to LrWpanMac::SetShortAddress ()
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:993
Time m_replyTime
Reply time.
virtual void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
Set the propagation delay model to be used.
#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:365
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
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:181
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.