A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 //-----------------------------------------------------------------------------
221 {
222 public:
224 
225  virtual void DoRun (void);
226 private:
229  void SwitchCh (Ptr<WifiNetDevice> dev);
230 
234 };
235 
237  : TestCase ("InterferenceHelperSequence")
238 {
239 }
240 
241 void
243 {
244  Ptr<Packet> p = Create<Packet> (9999);
245  dev->Send (p, dev->GetBroadcast (), 1);
246 }
247 
248 void
250 {
251  Ptr<WifiPhy> p = dev->GetPhy ();
252  p->SetChannelNumber (1);
253 }
254 
255 Ptr<Node>
257 {
258  Ptr<Node> node = CreateObject<Node> ();
259  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
260 
261  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
263  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
264  Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
265  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
266  phy->SetErrorRateModel (error);
267  phy->SetChannel (channel);
268  phy->SetDevice (dev);
269  phy->SetMobility (node);
270  phy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
272 
273  mobility->SetPosition (pos);
274  node->AggregateObject (mobility);
275  mac->SetAddress (Mac48Address::Allocate ());
276  dev->SetMac (mac);
277  dev->SetPhy (phy);
278  dev->SetRemoteStationManager (manager);
279  node->AddDevice (dev);
280 
281  return node;
282 }
283 
284 void
286 {
287  m_mac.SetTypeId ("ns3::AdhocWifiMac");
288  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
289  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
290 
291  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
293  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
294  channel->SetPropagationDelayModel (propDelay);
295  channel->SetPropagationLossModel (propLoss);
296 
297  Ptr<Node> rxOnly = CreateOne (Vector (0.0, 0.0, 0.0), channel);
298  Ptr<Node> senderA = CreateOne (Vector (5.0, 0.0, 0.0), channel);
299  Ptr<Node> senderB = CreateOne (Vector (-5.0, 0.0, 0.0), channel);
300 
301  propLoss->SetLoss (senderB->GetObject<MobilityModel> (), rxOnly->GetObject<MobilityModel> (), 0, true);
302  propLoss->SetDefaultLoss (999);
303 
304  Simulator::Schedule (Seconds (1.0),
306  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
307 
308  Simulator::Schedule (Seconds (1.0000001),
310  DynamicCast<WifiNetDevice> (rxOnly->GetDevice (0)));
311 
312  Simulator::Schedule (Seconds (5.0),
314  DynamicCast<WifiNetDevice> (senderA->GetDevice (0)));
315 
316  Simulator::Schedule (Seconds (7.0),
318  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
319 
320  Simulator::Stop (Seconds (100.0));
321  Simulator::Run ();
322 
323  Simulator::Destroy ();
324 }
325 
326 //-----------------------------------------------------------------------------
359 class Bug555TestCase : public TestCase
360 {
361 public:
362 
363  Bug555TestCase ();
364 
365  virtual void DoRun (void);
366 
367 private:
368 
370 
374 
377  unsigned int m_numSentPackets;
378 
380 };
381 
383  : TestCase ("Test case for Bug 555")
384 {
385 }
386 
387 void
389 {
390  if (m_numSentPackets == 0)
391  {
392  NS_ASSERT_MSG (Simulator::Now() == Time (Seconds (1)), "Packet 0 not transmitted at 1 second");
395  }
396  else if (m_numSentPackets == 1)
397  {
399  }
400 }
401 
402 void
404 {
405  Ptr<Packet> p = Create<Packet> (1000);
406  dev->Send (p, dev->GetBroadcast (), 1);
407 }
408 
409 void
411 {
412  m_mac.SetTypeId ("ns3::AdhocWifiMac");
413  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
414  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
415 
416  // Assign a seed and run number, and later fix the assignment of streams to
417  // WiFi random variables, so that the first backoff used is zero slots
418  RngSeedManager::SetSeed (1);
419  RngSeedManager::SetRun (17);
420 
421  // Disable the initial jitter of AP beacons (test case was written before
422  // beacon jitter was added)
423  Config::SetDefault ("ns3::ApWifiMac::EnableBeaconJitter", BooleanValue (false));
424 
425  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
427  Ptr<PropagationLossModel> propLoss = CreateObject<RandomPropagationLossModel> ();
428  channel->SetPropagationDelayModel (propDelay);
429  channel->SetPropagationLossModel (propLoss);
430 
431  Ptr<Node> txNode = CreateObject<Node> ();
432  Ptr<WifiNetDevice> txDev = CreateObject<WifiNetDevice> ();
433  Ptr<WifiMac> txMac = m_mac.Create<WifiMac> ();
435  // Fix the stream assignment to the Dcf Txop objects (backoffs)
436  // The below stream assignment will result in the DcaTxop object
437  // using a backoff value of zero for this test when the
438  // DcaTxop::EndTxNoAck() calls to StartBackoffNow()
439  AssignWifiRandomStreams (txMac, 23);
440 
441  Ptr<ConstantPositionMobilityModel> txMobility = CreateObject<ConstantPositionMobilityModel> ();
442  Ptr<YansWifiPhy> txPhy = CreateObject<YansWifiPhy> ();
443  Ptr<ErrorRateModel> txError = CreateObject<YansErrorRateModel> ();
444  txPhy->SetErrorRateModel (txError);
445  txPhy->SetChannel (channel);
446  txPhy->SetDevice (txDev);
447  txPhy->SetMobility (txNode);
448  txPhy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
449 
450  txPhy->TraceConnectWithoutContext ("PhyTxBegin", MakeCallback (&Bug555TestCase::NotifyPhyTxBegin, this));
451 
452  txMobility->SetPosition (Vector (0.0, 0.0, 0.0));
453  txNode->AggregateObject (txMobility);
454  txMac->SetAddress (Mac48Address::Allocate ());
455  txDev->SetMac (txMac);
456  txDev->SetPhy (txPhy);
457  txDev->SetRemoteStationManager (m_manager.Create<WifiRemoteStationManager> ());
458  txNode->AddDevice (txDev);
459 
460  m_firstTransmissionTime = Seconds (0.0);
461  m_secondTransmissionTime = Seconds (0.0);
462  m_numSentPackets = 0;
463 
464  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
465  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
466 
467  Simulator::Stop (Seconds (2.0));
468  Simulator::Run ();
469  Simulator::Destroy ();
470 
471  // First packet has 1408 us of transmit time. Slot time is 9 us.
472  // Backoff is 0 slots. SIFS is 16 us. AIFS is 2 slots = 18 us.
473  // Should send next packet at 1408 us + (0 * 9 us) + 16 us + 18 us
474  // 1442 us after the first one.
475  uint32_t expectedWait1 = 1408 + (0 * 9) + 16 + 18;
476  Time expectedSecondTransmissionTime = MicroSeconds (expectedWait1) + Seconds (1.0);
477 
478  NS_TEST_ASSERT_MSG_EQ (m_secondTransmissionTime, expectedSecondTransmissionTime, "The second transmission time not correct!");
479 }
480 
481 //-----------------------------------------------------------------------------
482 class WifiTestSuite : public TestSuite
483 {
484 public:
485  WifiTestSuite ();
486 };
487 
489  : TestSuite ("devices-wifi", UNIT)
490 {
491  AddTestCase (new WifiTest, TestCase::QUICK);
492  AddTestCase (new QosUtilsIsOldPacketTest, TestCase::QUICK);
493  AddTestCase (new InterferenceHelperSequenceTest, TestCase::QUICK); // Bug 991
494  AddTestCase (new Bug555TestCase, TestCase::QUICK); // Bug 555
495 }
496 
void RunOne(void)
Definition: wifi-test.cc:137
void NotifyPhyTxBegin(Ptr< const Packet > p)
Definition: wifi-test.cc:388
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
Ptr< T > Get(void) const
Definition: pointer.h:148
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
Hold a bool native type.
Definition: boolean.h:38
A suite of tests to run.
Definition: test.h:1105
void SetPropagationLossModel(Ptr< PropagationLossModel > loss)
ObjectFactory m_manager
Definition: wifi-test.cc:371
void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
void ConfigureStandard(enum WifiPhyStandard standard)
Definition: wifi-mac.cc:275
ObjectFactory m_mac
Definition: wifi-test.cc:372
void SetTypeId(TypeId tid)
#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:265
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:929
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:266
a 3d vector
Definition: vector.h:31
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:256
This queue contains packets for a particular access class.
Definition: edca-txop-n.h:84
void SendOnePacket(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:403
Time m_secondTransmissionTime
Definition: wifi-test.cc:376
Ptr< Object > Create(void) const
base class for all MAC-level wifi objects.
Definition: wifi-mac.h:44
Time m_firstTransmissionTime
Definition: wifi-test.cc:375
#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:148
calculate a propagation delay.
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:134
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
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)
Definition: object.cc:242
void SwitchCh(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:249
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: wifi-test.cc:156
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
OFDM PHY for the 5 GHz band (Clause 17)
hold objects of type Ptr
Definition: pointer.h:33
static WifiTestSuite g_wifiTestSuite
Definition: wifi-test.cc:497
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
Definition: object-base.cc:214
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
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:84
virtual Address GetBroadcast(void) const
unsigned int m_numSentPackets
Definition: wifi-test.cc:377
ObjectFactory m_manager
Definition: wifi-test.cc:90
instantiate subclasses of ns3::Object.
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:120
static void AssignWifiRandomStreams(Ptr< WifiMac > mac, int64_t stream)
Definition: wifi-test.cc:50
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
ObjectFactory m_propDelay
Definition: wifi-test.cc:373
void SendOnePacket(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:242
void SendOnePacket(Ptr< WifiNetDevice > dev)
Definition: wifi-test.cc:101
handle packet fragmentation and retransmissions.
Definition: dca-txop.h:67
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: wifi-test.cc:410
Ptr< T > GetObject(void) const
Definition: object.h:362
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:359
WifiTest()
Definition: wifi-test.cc:95
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: wifi-test.cc:285