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 
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);
54 
58 };
59 
61  : TestCase ("Wifi")
62 {
63 }
64 
65 void
67 {
68  Ptr<Packet> p = Create<Packet> ();
69  dev->Send (p, dev->GetBroadcast (), 1);
70 }
71 
72 void
74 {
75  Ptr<Node> node = CreateObject<Node> ();
76  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
77 
78  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
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);
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
103 {
104  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
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 //-----------------------------------------------------------------------------
186 {
187 public:
189 
190  virtual void DoRun (void);
191 private:
194  void SwitchCh (Ptr<WifiNetDevice> dev);
195 
199 };
200 
202  : TestCase ("InterferenceHelperSequence")
203 {
204 }
205 
206 void
208 {
209  Ptr<Packet> p = Create<Packet> (9999);
210  dev->Send (p, dev->GetBroadcast (), 1);
211 }
212 
213 void
215 {
216  Ptr<WifiPhy> p = dev->GetPhy ();
217  p->SetChannelNumber (1);
218 }
219 
220 Ptr<Node>
222 {
223  Ptr<Node> node = CreateObject<Node> ();
224  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
225 
226  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
228  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
229  Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
230  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
231  phy->SetErrorRateModel (error);
232  phy->SetChannel (channel);
233  phy->SetDevice (dev);
234  phy->SetMobility (node);
235  phy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
237 
238  mobility->SetPosition (pos);
239  node->AggregateObject (mobility);
240  mac->SetAddress (Mac48Address::Allocate ());
241  dev->SetMac (mac);
242  dev->SetPhy (phy);
243  dev->SetRemoteStationManager (manager);
244  node->AddDevice (dev);
245 
246  return node;
247 }
248 
249 void
251 {
252  m_mac.SetTypeId ("ns3::AdhocWifiMac");
253  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
254  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
255 
256  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
258  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
259  channel->SetPropagationDelayModel (propDelay);
260  channel->SetPropagationLossModel (propLoss);
261 
262  Ptr<Node> rxOnly = CreateOne (Vector (0.0, 0.0, 0.0), channel);
263  Ptr<Node> senderA = CreateOne (Vector (5.0, 0.0, 0.0), channel);
264  Ptr<Node> senderB = CreateOne (Vector (-5.0, 0.0, 0.0), channel);
265 
266  propLoss->SetLoss (senderB->GetObject<MobilityModel> (), rxOnly->GetObject<MobilityModel> (), 0, true);
267  propLoss->SetDefaultLoss (999);
268 
269  Simulator::Schedule (Seconds (1.0),
271  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
272 
273  Simulator::Schedule (Seconds (1.0000001),
275  DynamicCast<WifiNetDevice> (rxOnly->GetDevice (0)));
276 
277  Simulator::Schedule (Seconds (5.0),
279  DynamicCast<WifiNetDevice> (senderA->GetDevice (0)));
280 
281  Simulator::Schedule (Seconds (7.0),
283  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
284 
285  Simulator::Stop (Seconds (100.0));
286  Simulator::Run ();
287 
289 }
290 
291 //-----------------------------------------------------------------------------
324 class Bug555TestCase : public TestCase
325 {
326 public:
327 
328  Bug555TestCase ();
329 
330  virtual void DoRun (void);
331 
332 private:
333 
335 
339 
342  unsigned int m_numSentPackets;
343 
345 };
346 
348  : TestCase ("Test case for Bug 555")
349 {
350 }
351 
352 void
354 {
355  if (m_numSentPackets == 0)
356  {
357  NS_ASSERT_MSG (Simulator::Now() == Time (Seconds (1)), "Packet 0 not transmitted at 1 second");
360  }
361  else if (m_numSentPackets == 1)
362  {
364  }
365 }
366 
367 void
369 {
370  Ptr<Packet> p = Create<Packet> (1000);
371  dev->Send (p, dev->GetBroadcast (), 1);
372 }
373 
374 void
376 {
377  m_mac.SetTypeId ("ns3::AdhocWifiMac");
378  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
379  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
380 
381  //The simulation with the following seed and run numbers expe
384 
385  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
387  Ptr<PropagationLossModel> propLoss = CreateObject<RandomPropagationLossModel> ();
388  channel->SetPropagationDelayModel (propDelay);
389  channel->SetPropagationLossModel (propLoss);
390 
391  Ptr<Node> txNode = CreateObject<Node> ();
392  Ptr<WifiNetDevice> txDev = CreateObject<WifiNetDevice> ();
393  Ptr<WifiMac> txMac = m_mac.Create<WifiMac> ();
395 
396  Ptr<ConstantPositionMobilityModel> txMobility = CreateObject<ConstantPositionMobilityModel> ();
397  Ptr<YansWifiPhy> txPhy = CreateObject<YansWifiPhy> ();
398  Ptr<ErrorRateModel> txError = CreateObject<YansErrorRateModel> ();
399  txPhy->SetErrorRateModel (txError);
400  txPhy->SetChannel (channel);
401  txPhy->SetDevice (txDev);
402  txPhy->SetMobility (txNode);
403  txPhy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
404 
406 
407  txMobility->SetPosition (Vector (0.0, 0.0, 0.0));
408  txNode->AggregateObject (txMobility);
409  txMac->SetAddress (Mac48Address::Allocate ());
410  txDev->SetMac (txMac);
411  txDev->SetPhy (txPhy);
412  txDev->SetRemoteStationManager (m_manager.Create<WifiRemoteStationManager> ());
413  txNode->AddDevice (txDev);
414 
415  m_firstTransmissionTime = Seconds (0.0);
416  m_secondTransmissionTime = Seconds (0.0);
417  m_numSentPackets = 0;
418 
419  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
420  Simulator::Schedule (Seconds (1.0), &Bug555TestCase::SendOnePacket, this, txDev);
421 
422  Simulator::Stop (Seconds (2.0));
423  Simulator::Run ();
425 
426  // First packet has 1408 us of transmit time. Slot time is 9 us.
427  // Backoff is 0 slots. SIFS is 16 us. AIFS is 2 slots = 18 us.
428  // Should send next packet at 1408 us + (0 * 9 us) + 16 us + 18 us
429  // 1442 us after the first one.
430  uint32_t expectedWait1 = 1408 + (0 * 9) + 16 + 18;
431  Time expectedSecondTransmissionTime = MicroSeconds (expectedWait1) + Seconds (1.0);
432 
433  NS_TEST_ASSERT_MSG_EQ (m_secondTransmissionTime, expectedSecondTransmissionTime, "The second transmission time not correct!");
434 }
435 
436 //-----------------------------------------------------------------------------
437 class WifiTestSuite : public TestSuite
438 {
439 public:
440  WifiTestSuite ();
441 };
442 
444  : TestSuite ("devices-wifi", UNIT)
445 {
449  AddTestCase (new Bug555TestCase, TestCase::QUICK); // Bug 555
450 }
451 
453 
454 } // namespace ns3