A Discrete-Event Network Simulator
API
wifi-test.cc
Go to the documentation of this file.
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 #include "ns3/edca-txop-n.h"
42 #include "ns3/config.h"
43 #include "ns3/boolean.h"
44 
45 using namespace ns3;
46 
47 // helper function to assign streams to random variables, to control
48 // randomness in the tests
49 static void
51 {
52  int64_t currentStream = stream;
53  Ptr<RegularWifiMac> rmac = DynamicCast<RegularWifiMac> (mac);
54  if (rmac)
55  {
56  PointerValue ptr;
57  rmac->GetAttribute ("DcaTxop", ptr);
58  Ptr<DcaTxop> dcaTxop = ptr.Get<DcaTxop> ();
59  currentStream += dcaTxop->AssignStreams (currentStream);
60 
61  rmac->GetAttribute ("VO_EdcaTxopN", ptr);
62  Ptr<EdcaTxopN> vo_edcaTxopN = ptr.Get<EdcaTxopN> ();
63  currentStream += vo_edcaTxopN->AssignStreams (currentStream);
64 
65  rmac->GetAttribute ("VI_EdcaTxopN", ptr);
66  Ptr<EdcaTxopN> vi_edcaTxopN = ptr.Get<EdcaTxopN> ();
67  currentStream += vi_edcaTxopN->AssignStreams (currentStream);
68 
69  rmac->GetAttribute ("BE_EdcaTxopN", ptr);
70  Ptr<EdcaTxopN> be_edcaTxopN = ptr.Get<EdcaTxopN> ();
71  currentStream += be_edcaTxopN->AssignStreams (currentStream);
72 
73  rmac->GetAttribute ("BK_EdcaTxopN", ptr);
74  Ptr<EdcaTxopN> bk_edcaTxopN = ptr.Get<EdcaTxopN> ();
75  currentStream += bk_edcaTxopN->AssignStreams (currentStream);
76  }
77 }
78 
79 class WifiTest : public TestCase
80 {
81 public:
82  WifiTest ();
83 
84  virtual void DoRun (void);
85 private:
86  void RunOne (void);
87  void CreateOne (Vector pos, Ptr<YansWifiChannel> channel);
89 
93 };
94 
96  : TestCase ("Wifi")
97 {
98 }
99 
100 void
102 {
103  Ptr<Packet> p = Create<Packet> ();
104  dev->Send (p, dev->GetBroadcast (), 1);
105 }
106 
107 void
109 {
110  Ptr<Node> node = CreateObject<Node> ();
111  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
112 
113  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
115  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
116  Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
117  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
118  phy->SetErrorRateModel (error);
119  phy->SetChannel (channel);
120  phy->SetDevice (dev);
121  phy->SetMobility (node);
122  phy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
124 
125  mobility->SetPosition (pos);
126  node->AggregateObject (mobility);
127  mac->SetAddress (Mac48Address::Allocate ());
128  dev->SetMac (mac);
129  dev->SetPhy (phy);
130  dev->SetRemoteStationManager (manager);
131  node->AddDevice (dev);
132 
133  Simulator::Schedule (Seconds (1.0), &WifiTest::SendOnePacket, this, dev);
134 }
135 
136 void
138 {
139  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
141  Ptr<PropagationLossModel> propLoss = CreateObject<RandomPropagationLossModel> ();
142  channel->SetPropagationDelayModel (propDelay);
143  channel->SetPropagationLossModel (propLoss);
144 
145  CreateOne (Vector (0.0, 0.0, 0.0), channel);
146  CreateOne (Vector (5.0, 0.0, 0.0), channel);
147  CreateOne (Vector (5.0, 0.0, 0.0), channel);
148 
149  Simulator::Stop (Seconds (10.0));
150 
151  Simulator::Run ();
152  Simulator::Destroy ();
153 }
154 
155 void
157 {
158  m_mac.SetTypeId ("ns3::AdhocWifiMac");
159  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
160 
161  m_manager.SetTypeId ("ns3::ArfWifiManager");
162  RunOne ();
163  m_manager.SetTypeId ("ns3::AarfWifiManager");
164  RunOne ();
165  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
166  RunOne ();
167  m_manager.SetTypeId ("ns3::OnoeWifiManager");
168  RunOne ();
169  m_manager.SetTypeId ("ns3::AmrrWifiManager");
170  RunOne ();
171  m_manager.SetTypeId ("ns3::IdealWifiManager");
172  RunOne ();
173 
174  m_mac.SetTypeId ("ns3::AdhocWifiMac");
175  RunOne ();
176  m_mac.SetTypeId ("ns3::ApWifiMac");
177  RunOne ();
178  m_mac.SetTypeId ("ns3::StaWifiMac");
179  RunOne ();
180 
181 
182  m_propDelay.SetTypeId ("ns3::RandomPropagationDelayModel");
183  m_mac.SetTypeId ("ns3::AdhocWifiMac");
184  RunOne ();
185 }
186 
187 //-----------------------------------------------------------------------------
189 {
190 public:
191  QosUtilsIsOldPacketTest () : TestCase ("QosUtilsIsOldPacket")
192  {
193  }
194  virtual void DoRun (void)
195  {
196  // startingSeq=0, seqNum=2047
197  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 2047), false, "2047 is new in comparison to 0");
198  // startingSeq=0, seqNum=2048
199  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 2048), true, "2048 is old in comparison to 0");
200  // startingSeq=2048, seqNum=0
201  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2048, 0), true, "0 is old in comparison to 2048");
202  // startingSeq=4095, seqNum=0
203  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (4095, 0), false, "0 is new in comparison to 4095");
204  // startingSeq=0, seqNum=4095
205  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 4095), true, "4095 is old in comparison to 0");
206  // startingSeq=4095 seqNum=2047
207  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (4095, 2047), true, "2047 is old in comparison to 4095");
208  // startingSeq=2048 seqNum=4095
209  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2048, 4095), false, "4095 is new in comparison to 2048");
210  // startingSeq=2049 seqNum=0
211  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2049, 0), false, "0 is new in comparison to 2049");
212  }
213 };
214 
215 //-----------------------------------------------------------------------------
220 {
221 public:
223 
224  virtual void DoRun (void);
225 private:
226  Ptr<Node> CreateOne (Vector pos, Ptr<YansWifiChannel> channel);
228  void SwitchCh (Ptr<WifiNetDevice> dev);
229 
233 };
234 
236  : TestCase ("InterferenceHelperSequence")
237 {
238 }
239 
240 void
242 {
243  Ptr<Packet> p = Create<Packet> (9999);
244  dev->Send (p, dev->GetBroadcast (), 1);
245 }
246 
247 void
249 {
250  Ptr<WifiPhy> p = dev->GetPhy ();
251  p->SetChannelNumber (1);
252 }
253 
254 Ptr<Node>
256 {
257  Ptr<Node> node = CreateObject<Node> ();
258  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
259 
260  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
262  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
263  Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
264  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
265  phy->SetErrorRateModel (error);
266  phy->SetChannel (channel);
267  phy->SetDevice (dev);
268  phy->SetMobility (node);
269  phy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
271 
272  mobility->SetPosition (pos);
273  node->AggregateObject (mobility);
274  mac->SetAddress (Mac48Address::Allocate ());
275  dev->SetMac (mac);
276  dev->SetPhy (phy);
277  dev->SetRemoteStationManager (manager);
278  node->AddDevice (dev);
279 
280  return node;
281 }
282 
283 void
285 {
286  m_mac.SetTypeId ("ns3::AdhocWifiMac");
287  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
288  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
289 
290  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
292  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
293  channel->SetPropagationDelayModel (propDelay);
294  channel->SetPropagationLossModel (propLoss);
295 
296  Ptr<Node> rxOnly = CreateOne (Vector (0.0, 0.0, 0.0), channel);
297  Ptr<Node> senderA = CreateOne (Vector (5.0, 0.0, 0.0), channel);
298  Ptr<Node> senderB = CreateOne (Vector (-5.0, 0.0, 0.0), channel);
299 
300  propLoss->SetLoss (senderB->GetObject<MobilityModel> (), rxOnly->GetObject<MobilityModel> (), 0, true);
301  propLoss->SetDefaultLoss (999);
302 
303  Simulator::Schedule (Seconds (1.0),
305  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
306 
307  Simulator::Schedule (Seconds (1.0000001),
309  DynamicCast<WifiNetDevice> (rxOnly->GetDevice (0)));
310 
311  Simulator::Schedule (Seconds (5.0),
313  DynamicCast<WifiNetDevice> (senderA->GetDevice (0)));
314 
315  Simulator::Schedule (Seconds (7.0),
317  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
318 
319  Simulator::Stop (Seconds (100.0));
320  Simulator::Run ();
321 
322  Simulator::Destroy ();
323 }
324 
325 //-----------------------------------------------------------------------------
357 class Bug555TestCase : public TestCase
358 {
359 public:
360 
361  Bug555TestCase ();
362 
363  virtual void DoRun (void);
364 
365 private:
366 
368 
372 
375  unsigned int m_numSentPackets;
376 
378 };
379 
381  : TestCase ("Test case for Bug 555")
382 {
383 }
384 
385 void
387 {
388  if (m_numSentPackets == 0)
389  {
390  NS_ASSERT_MSG (Simulator::Now() == Time (Seconds (1)), "Packet 0 not transmitted at 1 second");
393  }
394  else if (m_numSentPackets == 1)
395  {
397  }
398 }
399 
400 void
402 {
403  Ptr<Packet> p = Create<Packet> (1000);
404  dev->Send (p, dev->GetBroadcast (), 1);
405 }
406 
407 void
409 {
410  m_mac.SetTypeId ("ns3::AdhocWifiMac");
411  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
412  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
413 
414  // Assign a seed and run number, and later fix the assignment of streams to
415  // WiFi random variables, so that the first backoff used is zero slots
416  RngSeedManager::SetSeed (1);
417  RngSeedManager::SetRun (17);
418 
419  // Disable the initial jitter of AP beacons (test case was written before
420  // beacon jitter was added)
421  Config::SetDefault ("ns3::ApWifiMac::EnableBeaconJitter", BooleanValue (false));
422 
423  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
425  Ptr<PropagationLossModel> propLoss = CreateObject<RandomPropagationLossModel> ();
426  channel->SetPropagationDelayModel (propDelay);
427  channel->SetPropagationLossModel (propLoss);
428 
429  Ptr<Node> txNode = CreateObject<Node> ();
430  Ptr<WifiNetDevice> txDev = CreateObject<WifiNetDevice> ();
431  Ptr<WifiMac> txMac = m_mac.Create<WifiMac> ();
433  // Fix the stream assignment to the Dcf Txop objects (backoffs)
434  // The below stream assignment will result in the DcaTxop object
435  // using a backoff value of zero for this test when the
436  // DcaTxop::EndTxNoAck() calls to StartBackoffNow()
437  AssignWifiRandomStreams (txMac, 23);
438 
439  Ptr<ConstantPositionMobilityModel> txMobility = CreateObject<ConstantPositionMobilityModel> ();
440  Ptr<YansWifiPhy> txPhy = CreateObject<YansWifiPhy> ();
441  Ptr<ErrorRateModel> txError = CreateObject<YansErrorRateModel> ();
442  txPhy->SetErrorRateModel (txError);
443  txPhy->SetChannel (channel);
444  txPhy->SetDevice (txDev);
445  txPhy->SetMobility (txNode);
446  txPhy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
447 
448  txPhy->TraceConnectWithoutContext ("PhyTxBegin", MakeCallback (&Bug555TestCase::NotifyPhyTxBegin, this));
449 
450  txMobility->SetPosition (Vector (0.0, 0.0, 0.0));
451  txNode->AggregateObject (txMobility);
452  txMac->SetAddress (Mac48Address::Allocate ());
453  txDev->SetMac (txMac);
454  txDev->SetPhy (txPhy);
455  txDev->SetRemoteStationManager (m_manager.Create<WifiRemoteStationManager> ());
456  txNode->AddDevice (txDev);
457 
460  m_numSentPackets = 0;
461 
462  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
463  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
464 
465  Simulator::Stop (Seconds (2.0));
466  Simulator::Run ();
467  Simulator::Destroy ();
468 
469  // First packet has 1408 us of transmit time. Slot time is 9 us.
470  // Backoff is 0 slots. SIFS is 16 us. AIFS is 2 slots = 18 us.
471  // Should send next packet at 1408 us + (0 * 9 us) + 16 us + 18 us
472  // 1442 us after the first one.
473  uint32_t expectedWait1 = 1408 + (0 * 9) + 16 + 18;
474  Time expectedSecondTransmissionTime = MicroSeconds (expectedWait1) + Seconds (1.0);
475 
476  NS_TEST_ASSERT_MSG_EQ (m_secondTransmissionTime, expectedSecondTransmissionTime, "The second transmission time not correct!");
477 }
478 
479 //-----------------------------------------------------------------------------
480 class WifiTestSuite : public TestSuite
481 {
482 public:
483  WifiTestSuite ();
484 };
485 
487  : TestSuite ("devices-wifi", UNIT)
488 {
489  AddTestCase (new WifiTest, TestCase::QUICK);
490  AddTestCase (new QosUtilsIsOldPacketTest, TestCase::QUICK);
491  AddTestCase (new InterferenceHelperSequenceTest, TestCase::QUICK); // Bug 991
492  AddTestCase (new Bug555TestCase, TestCase::QUICK); // Bug 555
493 }
494 
void RunOne(void)
Definition: wifi-test.cc:137
void NotifyPhyTxBegin(Ptr< const Packet > p)
Definition: wifi-test.cc:386
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Ptr< T > Get(void) const
Definition: pointer.h:184
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:34
A suite of tests to run.
Definition: test.h:1276
void SetPropagationLossModel(Ptr< PropagationLossModel > loss)
ObjectFactory m_manager
Definition: wifi-test.cc:369
void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
void ConfigureStandard(enum WifiPhyStandard standard)
Definition: wifi-mac.cc:281
ObjectFactory m_mac
Definition: wifi-test.cc:370
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
#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:273
bool QosUtilsIsOldPacket(uint16_t startingSeq, uint16_t seqNumber)
This function checks if packet with sequence number seqNumber is an "old" packet. ...
Definition: qos-utils.cc:86
encapsulates test code
Definition: test.h:1108
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: dca-txop.cc:275
Keep track of the current position and velocity of an object.
Ptr< WifiPhy > GetPhy(void) const
Ptr< Node > CreateOne(Vector pos, Ptr< YansWifiChannel > channel)
Definition: wifi-test.cc:255
This queue contains packets for a particular access class.
Definition: edca-txop-n.h:84
void SendOnePacket(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:401
Time m_secondTransmissionTime
Definition: wifi-test.cc:374
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:190
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
base class for all MAC-level wifi objects.
Definition: wifi-mac.h:44
Time m_firstTransmissionTime
Definition: wifi-test.cc:373
Vector3D Vector
Definition: vector.h:166
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:158
calculate a propagation delay.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:135
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
hold a list of per-remote-station state.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:246
void SwitchCh(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:248
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: wifi-test.cc:156
OFDM PHY for the 5 GHz band (Clause 17)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
static WifiTestSuite g_wifiTestSuite
Definition: wifi-test.cc:495
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
ObjectFactory m_mac
Definition: wifi-test.cc:91
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: wifi-test.cc:194
ObjectFactory m_propDelay
Definition: wifi-test.cc:92
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:228
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
virtual void SetChannelNumber(uint16_t id)=0
Set channel number.
void SetPosition(const Vector &position)
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
virtual Address GetBroadcast(void) const
unsigned int m_numSentPackets
Definition: wifi-test.cc:375
ObjectFactory m_manager
Definition: wifi-test.cc:90
Instantiate subclasses of ns3::Object.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:121
static void AssignWifiRandomStreams(Ptr< WifiMac > mac, int64_t stream)
Definition: wifi-test.cc:50
ObjectFactory m_propDelay
Definition: wifi-test.cc:371
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:866
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:683
void SendOnePacket(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:241
void SendOnePacket(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:101
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:882
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:330
handle packet fragmentation and retransmissions.
Definition: dca-txop.h:67
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: wifi-test.cc:408
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
void CreateOne(Vector pos, Ptr< YansWifiChannel > channel)
Definition: wifi-test.cc:108
Make sure that when multiple broadcast packets are queued on the same device in a short succession no...
Definition: wifi-test.cc:357
WifiTest()
Definition: wifi-test.cc:95
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: wifi-test.cc:284