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 
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::Run ();
116 
117  Simulator::Stop (Seconds (10.0));
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 ();
151 }
152 
153 //-----------------------------------------------------------------------------
155 {
156 public:
157  QosUtilsIsOldPacketTest () : TestCase ("QosUtilsIsOldPacket")
158  {
159  }
160  virtual void DoRun (void)
161  {
162  // startingSeq=0, seqNum=2047
163  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 2047), false, "2047 is new in comparison to 0");
164  // startingSeq=0, seqNum=2048
165  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 2048), true, "2048 is old in comparison to 0");
166  // startingSeq=2048, seqNum=0
167  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2048, 0), true, "0 is old in comparison to 2048");
168  // startingSeq=4095, seqNum=0
169  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (4095, 0), false, "0 is new in comparison to 4095");
170  // startingSeq=0, seqNum=4095
171  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (0, 4095), true, "4095 is old in comparison to 0");
172  // startingSeq=4095 seqNum=2047
173  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (4095, 2047), true, "2047 is old in comparison to 4095");
174  // startingSeq=2048 seqNum=4095
175  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2048, 4095), false, "4095 is new in comparison to 2048");
176  // startingSeq=2049 seqNum=0
177  NS_TEST_EXPECT_MSG_EQ (QosUtilsIsOldPacket (2049, 0), false, "0 is new in comparison to 2049");
178  }
179 };
180 
181 //-----------------------------------------------------------------------------
183 {
184 public:
186 
187  virtual void DoRun (void);
188 private:
191  void SwitchCh (Ptr<WifiNetDevice> dev);
192 
196 };
197 
199  : TestCase ("InterferenceHelperSequence")
200 {
201 }
202 
203 void
205 {
206  Ptr<Packet> p = Create<Packet> (9999);
207  dev->Send (p, dev->GetBroadcast (), 1);
208 }
209 
210 void
212 {
213  Ptr<WifiPhy> p = dev->GetPhy ();
214  p->SetChannelNumber (1);
215 }
216 
217 Ptr<Node>
219 {
220  Ptr<Node> node = CreateObject<Node> ();
221  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
222 
223  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
225  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
226  Ptr<YansWifiPhy> phy = CreateObject<YansWifiPhy> ();
227  Ptr<ErrorRateModel> error = CreateObject<YansErrorRateModel> ();
228  phy->SetErrorRateModel (error);
229  phy->SetChannel (channel);
230  phy->SetDevice (dev);
231  phy->SetMobility (node);
232  phy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
234 
235  mobility->SetPosition (pos);
236  node->AggregateObject (mobility);
237  mac->SetAddress (Mac48Address::Allocate ());
238  dev->SetMac (mac);
239  dev->SetPhy (phy);
240  dev->SetRemoteStationManager (manager);
241  node->AddDevice (dev);
242 
243  return node;
244 }
245 
246 void
248 {
249  m_mac.SetTypeId ("ns3::AdhocWifiMac");
250  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
251  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
252 
253  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
255  Ptr<MatrixPropagationLossModel> propLoss = CreateObject<MatrixPropagationLossModel> ();
256  channel->SetPropagationDelayModel (propDelay);
257  channel->SetPropagationLossModel (propLoss);
258 
259  Ptr<Node> rxOnly = CreateOne (Vector (0.0, 0.0, 0.0), channel);
260  Ptr<Node> senderA = CreateOne (Vector (5.0, 0.0, 0.0), channel);
261  Ptr<Node> senderB = CreateOne (Vector (-5.0, 0.0, 0.0), channel);
262 
263  propLoss->SetLoss (senderB->GetObject<MobilityModel> (), rxOnly->GetObject<MobilityModel> (), 0, true);
264  propLoss->SetDefaultLoss (999);
265 
268  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
269 
270  Simulator::Schedule (Seconds (1.0000001),
272  DynamicCast<WifiNetDevice> (rxOnly->GetDevice (0)));
273 
276  DynamicCast<WifiNetDevice> (senderA->GetDevice (0)));
277 
280  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
281 
282  Simulator::Stop (Seconds (100.0));
283  Simulator::Run ();
284 
286 }
287 
288 //-----------------------------------------------------------------------------
318 class Bug555TestCase : public TestCase
319 {
320 public:
321 
322  Bug555TestCase ();
323 
324  virtual void DoRun (void);
325 
326 private:
327 
329 
333 
336  unsigned int m_numSentPackets;
337 
339 };
340 
342  : TestCase ("Test case for Bug 555")
343 {
344 }
345 
346 void
348 {
349  if (m_numSentPackets == 0)
350  {
351  NS_ASSERT_MSG (Simulator::Now() == Time (Seconds (1)), "Packet 0 not transmitted at 1 second");
354  }
355  else if (m_numSentPackets == 1)
356  {
358  }
359 }
360 
361 void
363 {
364  Ptr<Packet> p = Create<Packet> (1000);
365  dev->Send (p, dev->GetBroadcast (), 1);
366 }
367 
368 void
370 {
371  m_mac.SetTypeId ("ns3::AdhocWifiMac");
372  m_propDelay.SetTypeId ("ns3::ConstantSpeedPropagationDelayModel");
373  m_manager.SetTypeId ("ns3::ConstantRateWifiManager");
374 
375  //The simulation with the following seed and run numbers expe
378 
379  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
381  Ptr<PropagationLossModel> propLoss = CreateObject<RandomPropagationLossModel> ();
382  channel->SetPropagationDelayModel (propDelay);
383  channel->SetPropagationLossModel (propLoss);
384 
385  Ptr<Node> txNode = CreateObject<Node> ();
386  Ptr<WifiNetDevice> txDev = CreateObject<WifiNetDevice> ();
387  Ptr<WifiMac> txMac = m_mac.Create<WifiMac> ();
389 
390  Ptr<ConstantPositionMobilityModel> txMobility = CreateObject<ConstantPositionMobilityModel> ();
391  Ptr<YansWifiPhy> txPhy = CreateObject<YansWifiPhy> ();
392  Ptr<ErrorRateModel> txError = CreateObject<YansErrorRateModel> ();
393  txPhy->SetErrorRateModel (txError);
394  txPhy->SetChannel (channel);
395  txPhy->SetDevice (txDev);
396  txPhy->SetMobility (txNode);
397  txPhy->ConfigureStandard (WIFI_PHY_STANDARD_80211a);
398 
400 
401  txMobility->SetPosition (Vector (0.0, 0.0, 0.0));
402  txNode->AggregateObject (txMobility);
403  txMac->SetAddress (Mac48Address::Allocate ());
404  txDev->SetMac (txMac);
405  txDev->SetPhy (txPhy);
406  txDev->SetRemoteStationManager (m_manager.Create<WifiRemoteStationManager> ());
407  txNode->AddDevice (txDev);
408 
411  m_numSentPackets = 0;
412 
415 
416  Simulator::Stop (Seconds (2.0));
417  Simulator::Run ();
419 
420  // First packet has 1408 us of transmit time. Slot time is 9 us.
421  // Backoff is 0 slots. SIFS is 16 us. AIFS is 2 slots = 18 us.
422  // Should send next packet at 1408 us + (0 * 9 us) + 16 us + 18 us
423  // 1442 us after the first one.
424  uint32_t expectedWait1 = 1408 + (0 * 9) + 16 + 18;
425  Time expectedSecondTransmissionTime = MicroSeconds (expectedWait1) + Seconds (1.0);
426 
427  NS_TEST_ASSERT_MSG_EQ (m_secondTransmissionTime, expectedSecondTransmissionTime, "The second transmission time not correct!");
428 }
429 
430 //-----------------------------------------------------------------------------
431 
432 class WifiTestSuite : public TestSuite
433 {
434 public:
435  WifiTestSuite ();
436 };
437 
439  : TestSuite ("devices-wifi", UNIT)
440 {
441  AddTestCase (new WifiTest);
444  AddTestCase (new Bug555TestCase); // Bug 555
445 }
446 
448 
449 } // namespace ns3