A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
wifi-test.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  * 2010 NICTA
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * Quincy Tse <quincy.tse@nicta.com.au> (Case for Bug 991)
21  */
22 
23 #include "ns3/wifi-net-device.h"
24 #include "ns3/yans-wifi-channel.h"
25 #include "ns3/adhoc-wifi-mac.h"
26 #include "ns3/yans-wifi-phy.h"
27 #include "ns3/arf-wifi-manager.h"
28 #include "ns3/propagation-delay-model.h"
29 #include "ns3/propagation-loss-model.h"
30 #include "ns3/error-rate-model.h"
31 #include "ns3/yans-error-rate-model.h"
32 #include "ns3/constant-position-mobility-model.h"
33 #include "ns3/node.h"
34 #include "ns3/simulator.h"
35 #include "ns3/test.h"
36 #include "ns3/object-factory.h"
37 #include "ns3/dca-txop.h"
38 #include "ns3/mac-rx-middle.h"
39 #include "ns3/pointer.h"
40 #include "ns3/rng-seed-manager.h"
41 
42 namespace ns3 {
43 
44 class WifiTest : public TestCase
45 {
46 public:
47  WifiTest ();
48 
49  virtual void DoRun (void);
50 private:
51  void RunOne (void);
52  void CreateOne (Vector pos, Ptr<YansWifiChannel> channel);
53  void SendOnePacket (Ptr<WifiNetDevice> dev);
54 
55  ObjectFactory m_manager;
56  ObjectFactory m_mac;
57  ObjectFactory m_propDelay;
58 };
59 
60 WifiTest::WifiTest ()
61  : TestCase ("Wifi")
62 {
63 }
64 
65 void
66 WifiTest::SendOnePacket (Ptr<WifiNetDevice> dev)
67 {
68  Ptr<Packet> p = Create<Packet> ();
69  dev->Send (p, dev->GetBroadcast (), 1);
70 }
71 
72 void
73 WifiTest::CreateOne (Vector pos, Ptr<YansWifiChannel> channel)
74 {
75  Ptr<Node> node = CreateObject<Node> ();
76  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
77 
78  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
79  mac->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
80  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
81  Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
82  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
83  phy->SetErrorRateModel (error);
84  phy->SetChannel (channel);
85  phy->SetDevice (dev);
86  phy->SetMobility (node);
87  phy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
88  Ptr<WifiRemoteStationManager> manager = m_manager.Create<WifiRemoteStationManager> ();
89 
90  mobility->SetPosition (pos);
91  node->AggregateObject (mobility);
92  mac->SetAddress (Mac48Address::Allocate ());
93  dev->SetMac (mac);
94  dev->SetPhy (phy);
95  dev->SetRemoteStationManager (manager);
96  node->AddDevice (dev);
97 
98  Simulator::Schedule (Seconds (1.0), &WifiTest::SendOnePacket, this, dev);
99 }
100 
101 void
102 WifiTest::RunOne (void)
103 {
104  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
105  Ptr<PropagationDelayModel> propDelay = m_propDelay.Create<PropagationDelayModel> ();
106  Ptr<PropagationLossModel> propLoss = CreateObject<RandomPropagationLossModel> ();
107  channel->SetPropagationDelayModel (propDelay);
108  channel->SetPropagationLossModel (propLoss);
109 
110  CreateOne (Vector (0.0, 0.0, 0.0), channel);
111  CreateOne (Vector (5.0, 0.0, 0.0), channel);
112  CreateOne (Vector (5.0, 0.0, 0.0), channel);
113 
114  Simulator::Stop (Seconds (10.0));
115 
116  Simulator::Run ();
118 }
119 
120 void
122 {
123  m_mac.SetTypeId ("ns3::AdhocWifiMac");
124  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
125 
126  m_manager.SetTypeId ("ns3::ArfWifiManager");
127  RunOne ();
128  m_manager.SetTypeId ("ns3::AarfWifiManager");
129  RunOne ();
130  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
131  RunOne ();
132  m_manager.SetTypeId ("ns3::OnoeWifiManager");
133  RunOne ();
134  m_manager.SetTypeId ("ns3::AmrrWifiManager");
135  RunOne ();
136  m_manager.SetTypeId ("ns3::IdealWifiManager");
137  RunOne ();
138 
139  m_mac.SetTypeId ("ns3::AdhocWifiMac");
140  RunOne ();
141  m_mac.SetTypeId ("ns3::ApWifiMac");
142  RunOne ();
143  m_mac.SetTypeId ("ns3::StaWifiMac");
144  RunOne ();
145 
146 
147  m_propDelay.SetTypeId ("ns3::RandomPropagationDelayModel");
148  m_mac.SetTypeId ("ns3::AdhocWifiMac");
149  RunOne ();
150 }
151 
152 //-----------------------------------------------------------------------------
154 {
155 public:
156  QosUtilsIsOldPacketTest () : TestCase ("QosUtilsIsOldPacket")
157  {
158  }
159  virtual void DoRun (void)
160  {
161  // startingSeq=0, seqNum=2047
162  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 2047), false, "2047 is new in comparison to 0");
163  // startingSeq=0, seqNum=2048
164  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 2048), true, "2048 is old in comparison to 0");
165  // startingSeq=2048, seqNum=0
166  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2048, 0), true, "0 is old in comparison to 2048");
167  // startingSeq=4095, seqNum=0
168  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (4095, 0), false, "0 is new in comparison to 4095");
169  // startingSeq=0, seqNum=4095
170  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 4095), true, "4095 is old in comparison to 0");
171  // startingSeq=4095 seqNum=2047
172  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (4095, 2047), true, "2047 is old in comparison to 4095");
173  // startingSeq=2048 seqNum=4095
174  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2048, 4095), false, "4095 is new in comparison to 2048");
175  // startingSeq=2049 seqNum=0
176  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2049, 0), false, "0 is new in comparison to 2049");
177  }
178 };
179 
180 //-----------------------------------------------------------------------------
182 {
183 public:
185 
186  virtual void DoRun (void);
187 private:
188  Ptr<Node> CreateOne (Vector pos, Ptr<YansWifiChannel> channel);
189  void SendOnePacket (Ptr<WifiNetDevice> dev);
190  void SwitchCh (Ptr<WifiNetDevice> dev);
191 
192  ObjectFactory m_manager;
193  ObjectFactory m_mac;
194  ObjectFactory m_propDelay;
195 };
196 
197 InterferenceHelperSequenceTest::InterferenceHelperSequenceTest ()
198  : TestCase ("InterferenceHelperSequence")
199 {
200 }
201 
202 void
203 InterferenceHelperSequenceTest::SendOnePacket (Ptr<WifiNetDevice> dev)
204 {
205  Ptr<Packet> p = Create<Packet> (9999);
206  dev->Send (p, dev->GetBroadcast (), 1);
207 }
208 
209 void
210 InterferenceHelperSequenceTest::SwitchCh (Ptr<WifiNetDevice> dev)
211 {
212  Ptr<WifiPhy> p = dev->GetPhy ();
213  p->SetChannelNumber (1);
214 }
215 
216 Ptr<Node>
217 InterferenceHelperSequenceTest::CreateOne (Vector pos, Ptr<YansWifiChannel> channel)
218 {
219  Ptr<Node> node = CreateObject<Node> ();
220  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
221 
222  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
223  mac->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
224  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
225  Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
226  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
227  phy->SetErrorRateModel (error);
228  phy->SetChannel (channel);
229  phy->SetDevice (dev);
230  phy->SetMobility (node);
231  phy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
232  Ptr<WifiRemoteStationManager> manager = m_manager.Create<WifiRemoteStationManager> ();
233 
234  mobility->SetPosition (pos);
235  node->AggregateObject (mobility);
236  mac->SetAddress (Mac48Address::Allocate ());
237  dev->SetMac (mac);
238  dev->SetPhy (phy);
239  dev->SetRemoteStationManager (manager);
240  node->AddDevice (dev);
241 
242  return node;
243 }
244 
245 void
247 {
248  m_mac.SetTypeId ("ns3::AdhocWifiMac");
249  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
250  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
251 
252  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
253  Ptr<PropagationDelayModel> propDelay = m_propDelay.Create<PropagationDelayModel> ();
254  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
255  channel->SetPropagationDelayModel (propDelay);
256  channel->SetPropagationLossModel (propLoss);
257 
258  Ptr<Node> rxOnly = CreateOne (Vector (0.0, 0.0, 0.0), channel);
259  Ptr<Node> senderA = CreateOne (Vector (5.0, 0.0, 0.0), channel);
260  Ptr<Node> senderB = CreateOne (Vector (-5.0, 0.0, 0.0), channel);
261 
262  propLoss->SetLoss (senderB->GetObject<MobilityModel> (), rxOnly->GetObject<MobilityModel> (), 0, true);
263  propLoss->SetDefaultLoss (999);
264 
266  &InterferenceHelperSequenceTest::SendOnePacket, this,
267  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
268 
269  Simulator::Schedule (Seconds (1.0000001),
270  &InterferenceHelperSequenceTest::SwitchCh, this,
271  DynamicCast<WifiNetDevice> (rxOnly->GetDevice (0)));
272 
274  &InterferenceHelperSequenceTest::SendOnePacket, this,
275  DynamicCast<WifiNetDevice> (senderA->GetDevice (0)));
276 
278  &InterferenceHelperSequenceTest::SendOnePacket, this,
279  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
280 
281  Simulator::Stop (Seconds (100.0));
282  Simulator::Run ();
283 
285 }
286 
287 //-----------------------------------------------------------------------------
317 class Bug555TestCase : public TestCase
318 {
319 public:
320 
321  Bug555TestCase ();
322 
323  virtual void DoRun (void);
324 
325 private:
326 
327  void SendOnePacket (Ptr<WifiNetDevice> dev);
328 
329  ObjectFactory m_manager;
330  ObjectFactory m_mac;
331  ObjectFactory m_propDelay;
332 
333  Time m_firstTransmissionTime;
334  Time m_secondTransmissionTime;
335  unsigned int m_numSentPackets;
336 
337  void NotifyPhyTxBegin (Ptr<const Packet> p);
338 };
339 
340 Bug555TestCase::Bug555TestCase ()
341  : TestCase ("Test case for Bug 555")
342 {
343 }
344 
345 void
346 Bug555TestCase::NotifyPhyTxBegin (Ptr<const Packet> p)
347 {
348  if (m_numSentPackets == 0)
349  {
350  NS_ASSERT_MSG (Simulator::Now() == Time (Seconds (1)), "Packet 0 not transmitted at 1 second");
351  m_numSentPackets++;
352  m_firstTransmissionTime = Simulator::Now ();
353  }
354  else if (m_numSentPackets == 1)
355  {
356  m_secondTransmissionTime = Simulator::Now ();
357  }
358 }
359 
360 void
361 Bug555TestCase::SendOnePacket (Ptr<WifiNetDevice> dev)
362 {
363  Ptr<Packet> p = Create<Packet> (1000);
364  dev->Send (p, dev->GetBroadcast (), 1);
365 }
366 
367 void
369 {
370  m_mac.SetTypeId ("ns3::AdhocWifiMac");
371  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
372  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
373 
374  //The simulation with the following seed and run numbers expe
377 
378  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
379  Ptr<PropagationDelayModel> propDelay = m_propDelay.Create<PropagationDelayModel> ();
380  Ptr<PropagationLossModel> propLoss = CreateObject<RandomPropagationLossModel> ();
381  channel->SetPropagationDelayModel (propDelay);
382  channel->SetPropagationLossModel (propLoss);
383 
384  Ptr<Node> txNode = CreateObject<Node> ();
385  Ptr<WifiNetDevice> txDev = CreateObject<WifiNetDevice> ();
386  Ptr<WifiMac> txMac = m_mac.Create<WifiMac> ();
388 
389  Ptr<ConstantPositionMobilityModel> txMobility = CreateObject<ConstantPositionMobilityModel> ();
390  Ptr<YansWifiPhy> txPhy = CreateObject<YansWifiPhy> ();
391  Ptr<ErrorRateModel> txError = CreateObject<YansErrorRateModel> ();
392  txPhy->SetErrorRateModel (txError);
393  txPhy->SetChannel (channel);
394  txPhy->SetDevice (txDev);
395  txPhy->SetMobility (txNode);
396  txPhy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
397 
398  txPhy->TraceConnectWithoutContext ("PhyTxBegin", MakeCallback (&Bug555TestCase::NotifyPhyTxBegin, this));
399 
400  txMobility->SetPosition (Vector (0.0, 0.0, 0.0));
401  txNode->AggregateObject (txMobility);
402  txMac->SetAddress (Mac48Address::Allocate ());
403  txDev->SetMac (txMac);
404  txDev->SetPhy (txPhy);
405  txDev->SetRemoteStationManager (m_manager.Create<WifiRemoteStationManager> ());
406  txNode->AddDevice (txDev);
407 
408  m_firstTransmissionTime = Seconds (0.0);
409  m_secondTransmissionTime = Seconds (0.0);
410  m_numSentPackets = 0;
411 
412  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
413  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
414 
415  Simulator::Stop (Seconds (2.0));
416  Simulator::Run ();
418 
419  // First packet has 1408 us of transmit time. Slot time is 9 us.
420  // Backoff is 0 slots. SIFS is 16 us. AIFS is 2 slots = 18 us.
421  // Should send next packet at 1408 us + (0 * 9 us) + 16 us + 18 us
422  // 1442 us after the first one.
423  uint32_t expectedWait1 = 1408 + (0 * 9) + 16 + 18;
424  Time expectedSecondTransmissionTime = MicroSeconds (expectedWait1) + Seconds (1.0);
425 
426  NS_TEST_ASSERT_MSG_EQ (m_secondTransmissionTime, expectedSecondTransmissionTime, "The second transmission time not correct!");
427 }
428 
429 //-----------------------------------------------------------------------------
430 
431 class WifiTestSuite : public TestSuite
432 {
433 public:
434  WifiTestSuite ();
435 };
436 
437 WifiTestSuite::WifiTestSuite ()
438  : TestSuite ("devices-wifi", UNIT)
439 {
440  AddTestCase (new WifiTest, TestCase::QUICK);
441  AddTestCase (new QosUtilsIsOldPacketTest, TestCase::QUICK);
442  AddTestCase (new InterferenceHelperSequenceTest, TestCase::QUICK); // Bug 991
443  AddTestCase (new Bug555TestCase, TestCase::QUICK); // Bug 555
444 }
445 
446 static WifiTestSuite g_wifiTestSuite;
447 
448 } // namespace ns3
keep track of time unit.
Definition: nstime.h:149
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
A suite of tests to run.
Definition: test.h:962
static void Run(void)
Definition: simulator.cc:157
void ConfigureStandard(enum WifiPhyStandard standard)
Definition: wifi-mac.cc:261
void SetTypeId(TypeId tid)
bool QosUtilsIsOldPacket(uint16_t startingSeq, uint16_t seqNumber)
Definition: qos-utils.cc:86
virtual void DoRun(void)
Implementation to actually run this test case.
Definition: wifi-test.cc:368
encapsulates test code
Definition: test.h:834
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
a 3d vector
Definition: vector.h:31
static void SetRun(uint64_t run)
Set the run number of simulation.
Keep track of the current position and velocity of an object.
static Mac48Address Allocate(void)
Ptr< Object > Create(void) const
base class for all MAC-level wifi objects.This class encapsulates all the low-level MAC functionality...
Definition: wifi-mac.h:47
virtual void DoRun(void)
Implementation to actually run this test case.
Definition: wifi-test.cc:159
calculate a propagation delay.
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:133
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
TestCase(std::string name)
Definition: test.cc:147
hold a list of per-remote-station state.
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
static void Destroy(void)
Definition: simulator.cc:121
virtual void DoRun(void)
Implementation to actually run this test case.
Definition: wifi-test.cc:121
virtual void DoRun(void)
Implementation to actually run this test case.
Definition: wifi-test.cc:246
static Time Now(void)
Definition: simulator.cc:179
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
static void SetSeed(uint32_t seed)
set the seed it will duplicate the seed value 6 times
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
instantiate subclasses of ns3::Object.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:119
static void Stop(void)
Definition: simulator.cc:164
Ptr< T > GetObject(void) const
Definition: object.h:332
Time MicroSeconds(uint64_t us)
create ns3::Time instances in units of microseconds.
Definition: nstime.h:615