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::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:
190  void SwitchCh (Ptr<WifiNetDevice> dev);
191 
195 };
196 
198  : TestCase ("InterferenceHelperSequence")
199 {
200 }
201 
202 void
204 {
205  Ptr<Packet> p = Create<Packet> (9999);
206  dev->Send (p, dev->GetBroadcast (), 1);
207 }
208 
209 void
211 {
212  Ptr<WifiPhy> p = dev->GetPhy ();
213  p->SetChannelNumber (1);
214 }
215 
216 Ptr<Node>
218 {
219  Ptr<Node> node = CreateObject<Node> ();
220  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice> ();
221 
222  Ptr<WifiMac> mac = m_mac.Create<WifiMac> ();
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);
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> ();
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 
267  DynamicCast<WifiNetDevice> (senderB->GetDevice (0)));
268 
269  Simulator::Schedule (Seconds (1.0000001),
271  DynamicCast<WifiNetDevice> (rxOnly->GetDevice (0)));
272 
275  DynamicCast<WifiNetDevice> (senderA->GetDevice (0)));
276 
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 
328 
332 
335  unsigned int m_numSentPackets;
336 
338 };
339 
341  : TestCase ("Test case for Bug 555")
342 {
343 }
344 
345 void
347 {
348  if (m_numSentPackets == 0)
349  {
350  NS_ASSERT_MSG (Simulator::Now() == Time (Seconds (1)), "Packet 0 not transmitted at 1 second");
353  }
354  else if (m_numSentPackets == 1)
355  {
357  }
358 }
359 
360 void
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> ();
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 
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 
410  m_numSentPackets = 0;
411 
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 
438  : TestSuite ("devices-wifi", UNIT)
439 {
443  AddTestCase (new Bug555TestCase, TestCase::QUICK); // Bug 555
444 }
445 
447 
448 } // namespace ns3