A Discrete-Event Network Simulator
API
spectrum-wifi-phy-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/test.h"
20 #include "ns3/packet.h"
21 #include "ns3/tag.h"
22 #include "ns3/packet-burst.h"
23 #include "ns3/spectrum-wifi-helper.h"
24 #include "ns3/wifi-spectrum-value-helper.h"
25 #include "ns3/spectrum-wifi-phy.h"
26 #include "ns3/interference-helper.h"
27 #include "ns3/nist-error-rate-model.h"
28 #include "ns3/wifi-mac-header.h"
29 #include "ns3/wifi-mac-trailer.h"
30 #include "ns3/wifi-phy-tag.h"
31 #include "ns3/wifi-phy-standard.h"
32 #include "ns3/wifi-spectrum-signal-parameters.h"
33 
34 using namespace ns3;
35 
36 static const uint16_t CHANNEL_NUMBER = 36;
37 static const uint32_t FREQUENCY = 5180; // MHz
38 static const uint32_t CHANNEL_WIDTH = 20; // MHz
39 
41 {
42 public:
44  SpectrumWifiPhyBasicTest (std::string name);
45  virtual ~SpectrumWifiPhyBasicTest ();
46 protected:
47  virtual void DoSetup (void);
49  Ptr<SpectrumSignalParameters> MakeSignal (double txPowerWatts);
50  void SendSignal (double txPowerWatts);
51  void SpectrumWifiPhyReceiver (bool rxSucceeded);
52  uint32_t m_count;
53 private:
54  virtual void DoRun (void);
55 };
56 
58  : TestCase ("SpectrumWifiPhy test case receives one packet"),
59  m_count (0)
60 {
61 }
62 
64  : TestCase (name),
65  m_count (0)
66 {
67 }
68 
69 // Make a Wi-Fi signal to inject directly to the StartRx() method
72 {
73  WifiPreamble preamble;
74  preamble = WIFI_PREAMBLE_LONG;
75  WifiMode mode = WifiPhy::GetOfdmRate6Mbps ();
76  WifiTxVector txVector = WifiTxVector (mode, 0, 0, false, 1, 0, 20000000, false, false);
77  enum mpduType mpdutype = NORMAL_MPDU;
78 
79  Ptr<Packet> pkt = Create<Packet> (1000);
80  WifiMacHeader hdr;
81  WifiMacTrailer trailer;
82 
83  hdr.SetType (WIFI_MAC_QOSDATA);
84  hdr.SetQosTid (0);
85  uint32_t size = pkt->GetSize () + hdr.GetSize () + trailer.GetSerializedSize ();
86  Time txDuration = m_phy->CalculateTxDuration (size, txVector, preamble, m_phy->GetFrequency(), mpdutype, 0);
87  hdr.SetDuration (txDuration);
88 
89  pkt->AddHeader (hdr);
90  pkt->AddTrailer (trailer);
91  WifiPhyTag tag (txVector, preamble, mpdutype);
92  pkt->AddPacketTag (tag);
93  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts);
94  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
95  txParams->psd = txPowerSpectrum;
96  txParams->txPhy = 0;
97  txParams->duration = txDuration;
98  txParams->packet = pkt;
99  return txParams;
100 }
101 
102 // Make a Wi-Fi signal to inject directly to the StartRx() method
103 void
105 {
106  m_phy->StartRx (MakeSignal (txPowerWatts));
107 }
108 
109 void
111 {
112  m_count++;
113 }
114 
116 {
117 }
118 
119 // Create necessary objects, and inject signals. Test that the expected
120 // number of packet receptions occur.
121 void
123 {
124  m_phy = CreateObject<SpectrumWifiPhy> ();
126  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel> ();
127  m_phy->SetErrorRateModel (error);
131  //Bug 2460: CcaMode1Threshold default should be set to -62 dBm when using Spectrum
132  m_phy->SetCcaMode1Threshold (-62.0);
133 }
134 
135 // Test that the expected number of packet receptions occur.
136 void
138 {
139  double txPowerWatts = 0.010;
140  // Send packets spaced 1 second apart; all should be received
141  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
142  Simulator::Schedule (Seconds (2), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
143  Simulator::Schedule (Seconds (3), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
144  // Send packets spaced 1 microsecond second apart; only one should be received
145  Simulator::Schedule (MicroSeconds (4000000), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
146  Simulator::Schedule (MicroSeconds (4000001), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
147  Simulator::Run ();
148  Simulator::Destroy ();
149 
150  NS_TEST_ASSERT_MSG_EQ (m_count, 4, "Didn't receive right number of packets");
151 }
152 
154 {
155 public:
161  m_notifyRxStart (0),
162  m_notifyRxEndOk (0),
163  m_notifyRxEndError (0),
165  {
166  }
167  virtual ~TestPhyListener ()
168  {
169  }
170  virtual void NotifyRxStart (Time duration)
171  {
172  ++m_notifyRxStart;
173  }
174  virtual void NotifyRxEndOk (void)
175  {
176  ++m_notifyRxEndOk;
177  }
178  virtual void NotifyRxEndError (void)
179  {
181  }
182  virtual void NotifyTxStart (Time duration, double txPowerDbm)
183  {
184  }
185  virtual void NotifyMaybeCcaBusyStart (Time duration)
186  {
188  }
189  virtual void NotifySwitchingStart (Time duration)
190  {
191  }
192  virtual void NotifySleep (void)
193  {
194  }
195  virtual void NotifyWakeup (void)
196  {
197  }
198  uint32_t m_notifyRxStart;
199  uint32_t m_notifyRxEndOk;
202 private:
203 };
204 
205 
207 {
208 public:
210  virtual ~SpectrumWifiPhyListenerTest ();
211 private:
212  virtual void DoSetup (void);
213  virtual void DoRun (void);
215 };
216 
218  : SpectrumWifiPhyBasicTest ("SpectrumWifiPhy test operation of WifiPhyListener")
219 {
220 }
221 
223 {
224 }
225 
226 void
228 {
232 }
233 
234 void
236 {
237  double txPowerWatts = 0.010;
238  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyListenerTest::SendSignal, this, txPowerWatts);
239  Simulator::Run ();
240 
241  NS_TEST_ASSERT_MSG_EQ (m_count, 1, "Didn't receive right number of packets");
242  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxStart, 1, "Didn't receive NotifyRxStart");
243  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxEndOk, 1, "Didn't receive NotifyRxEnd");
244  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyMaybeCcaBusyStart, 0, "Received NotifyMaybeCcaBusyStart unexpectedly");
245 
246  Simulator::Destroy ();
247  delete m_listener;
248 }
249 
251 {
252 public:
254 };
255 
257  : TestSuite ("spectrum-wifi-phy", UNIT)
258 {
259  AddTestCase (new SpectrumWifiPhyBasicTest, TestCase::QUICK);
260  AddTestCase (new SpectrumWifiPhyListenerTest, TestCase::QUICK);
261 }
262 
static SpectrumWifiPhyTestSuite spectrumWifiPhyTestSuite
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
HT OFDM PHY for the 5 GHz band (clause 20)
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
void SpectrumWifiPhyReceiver(bool rxSucceeded)
void StartRx(Ptr< SpectrumSignalParameters > rxParams)
Input method for delivering a signal from the spectrum channel and low-level Phy interface to this Sp...
A suite of tests to run.
Definition: test.h:1333
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:824
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
virtual void NotifySleep(void)
Notify listeners that we went to sleep.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
static const uint32_t FREQUENCY
encapsulates test code
Definition: test.h:1147
virtual void NotifyMaybeCcaBusyStart(Time duration)
virtual void DoRun(void)
Implementation to actually run this TestCase.
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:99
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:30
The MPDU is not part of an A-MPDU.
Definition: wifi-phy.h:59
void SendSignal(double txPowerWatts)
virtual void NotifyWakeup(void)
Notify listeners that we woke up.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
receive notifications about phy events.
Definition: wifi-phy.h:81
Ptr< SpectrumSignalParameters > MakeSignal(double txPowerWatts)
virtual void NotifySwitchingStart(Time duration)
#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:161
virtual void NotifyRxStart(Time duration)
Time CalculateTxDuration(uint32_t size, WifiTxVector txVector, enum WifiPreamble preamble, double frequency)
Definition: wifi-phy.cc:1942
Ptr< SpectrumPhy > txPhy
The SpectrumPhy instance that is making the transmission.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual uint32_t GetFrequency(void) const
Definition: wifi-phy.cc:1143
void SetPacketReceivedCallback(RxCallback callback)
Set the packet received callback (invoked at the end of a frame reception), to notify whether the fra...
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
virtual void RegisterListener(WifiPhyListener *listener)
virtual void NotifyRxEndError(void)
We have received the last bit of a packet for which NotifyRxStart was invoked first and...
virtual void SetChannelNumber(uint16_t id)
Set channel number.
Definition: wifi-phy.cc:1275
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void NotifyRxEndOk(void)
We have received the last bit of a packet for which NotifyRxStart was invoked first and...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t CHANNEL_WIDTH
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:285
void SetCcaMode1Threshold(double threshold)
Sets the CCA threshold (dBm).
Definition: wifi-phy.cc:442
Ptr< Packet > packet
The packet being transmitted with this signal.
Ptr< SpectrumWifiPhy > m_phy
virtual void ConfigureStandard(enum WifiPhyStandard standard)
Configure the PHY-level parameters for different Wi-Fi standard.
Definition: wifi-phy.cc:1026
Ptr< SpectrumValue > psd
The Power Spectral Density of the waveform, in linear units.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
virtual void SetFrequency(uint32_t freq)
Definition: wifi-phy.cc:1086
TestPhyListener(void)
Create a test PhyListener.
Tag for WifiTxVector and WifiPreamble information to be embedded in outgoing transmissions as a Packe...
Definition: wifi-phy-tag.h:36
Time duration
The duration of the packet transmission.
static const uint16_t CHANNEL_NUMBER
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:911
virtual void NotifyTxStart(Time duration, double txPowerDbm)
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
Implements the IEEE 802.11 MAC header.
Implements the IEEE 802.11 MAC trailer.
virtual uint32_t GetSerializedSize(void) const
mpduType
This enumeration defines the type of an MPDU.
Definition: wifi-phy.h:56
void SetErrorRateModel(Ptr< ErrorRateModel > rate)
Sets the error rate model.
Definition: wifi-phy.cc:629