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/spectrum-phy.h"
20 #include "ns3/test.h"
21 #include "ns3/spectrum-wifi-helper.h"
22 #include "ns3/wifi-spectrum-value-helper.h"
23 #include "ns3/spectrum-wifi-phy.h"
24 #include "ns3/nist-error-rate-model.h"
25 #include "ns3/wifi-mac-header.h"
26 #include "ns3/wifi-spectrum-signal-parameters.h"
27 #include "ns3/wifi-phy-listener.h"
28 #include "ns3/log.h"
29 #include "ns3/wifi-psdu.h"
30 #include "ns3/wifi-ppdu.h"
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("SpectrumWifiPhyBasicTest");
35 
36 static const uint8_t CHANNEL_NUMBER = 36;
37 static const uint32_t FREQUENCY = 5180; // MHz
38 static const uint16_t CHANNEL_WIDTH = 20; // MHz
39 static const uint16_t GUARD_WIDTH = CHANNEL_WIDTH; // MHz (expanded to channel width to model spectrum mask)
40 
48 {
49 public:
56  SpectrumWifiPhyBasicTest (std::string name);
57  virtual ~SpectrumWifiPhyBasicTest ();
58 
59 protected:
60  virtual void DoSetup (void);
62 
67  Ptr<SpectrumSignalParameters> MakeSignal (double txPowerWatts);
72  void SendSignal (double txPowerWatts);
80  void SpectrumWifiPhyRxSuccess (Ptr<WifiPsdu> psdu, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu);
85  void SpectrumWifiPhyRxFailure (Ptr<WifiPsdu> psdu);
86  uint32_t m_count;
87 
88 private:
89  virtual void DoRun (void);
90 };
91 
93  : TestCase ("SpectrumWifiPhy test case receives one packet"),
94  m_count (0)
95 {
96 }
97 
99  : TestCase (name),
100  m_count (0)
101 {
102 }
103 
104 // Make a Wi-Fi signal to inject directly to the StartRx() method
107 {
108  WifiTxVector txVector = WifiTxVector (WifiPhy::GetOfdmRate6Mbps (), 0, WIFI_PREAMBLE_LONG, 800, 1, 1, 0, 20, false, false);
109 
110  Ptr<Packet> pkt = Create<Packet> (1000);
111  WifiMacHeader hdr;
112 
114  hdr.SetQosTid (0);
115 
116  Ptr<WifiPsdu> psdu = Create<WifiPsdu> (pkt, hdr);
117  Time txDuration = m_phy->CalculateTxDuration (psdu->GetSize (), txVector, m_phy->GetPhyBand ());
118 
119  Ptr<WifiPpdu> ppdu = Create<WifiPpdu> (psdu, txVector, txDuration, WIFI_PHY_BAND_5GHZ);
120 
121  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts, GUARD_WIDTH);
122  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
123  txParams->psd = txPowerSpectrum;
124  txParams->txPhy = 0;
125  txParams->duration = txDuration;
126  txParams->ppdu = ppdu;
127 
128  return txParams;
129 }
130 
131 // Make a Wi-Fi signal to inject directly to the StartRx() method
132 void
134 {
135  m_phy->StartRx (MakeSignal (txPowerWatts));
136 }
137 
138 void
139 SpectrumWifiPhyBasicTest::SpectrumWifiPhyRxSuccess (Ptr<WifiPsdu> psdu, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu)
140 {
141  NS_LOG_FUNCTION (this << *psdu << snr << txVector);
142  m_count++;
143 }
144 
145 void
147 {
148  NS_LOG_FUNCTION (this << *psdu);
149  m_count++;
150 }
151 
153 {
154 }
155 
156 // Create necessary objects, and inject signals. Test that the expected
157 // number of packet receptions occur.
158 void
160 {
161  m_phy = CreateObject<SpectrumWifiPhy> ();
163  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel> ();
164  m_phy->SetErrorRateModel (error);
169 }
170 
171 // Test that the expected number of packet receptions occur.
172 void
174 {
175  double txPowerWatts = 0.010;
176  // Send packets spaced 1 second apart; all should be received
177  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
178  Simulator::Schedule (Seconds (2), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
179  Simulator::Schedule (Seconds (3), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
180  // Send packets spaced 1 microsecond second apart; none should be received (PHY header reception failure)
181  Simulator::Schedule (MicroSeconds (4000000), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
182  Simulator::Schedule (MicroSeconds (4000001), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
183  Simulator::Run ();
184  Simulator::Destroy ();
185 
186  NS_TEST_ASSERT_MSG_EQ (m_count, 3, "Didn't receive right number of packets");
187 }
188 
196 {
197 public:
203  : m_notifyRxStart (0),
204  m_notifyRxEndOk (0),
205  m_notifyRxEndError (0),
207  {
208  }
209  virtual ~TestPhyListener ()
210  {
211  }
212  virtual void NotifyRxStart (Time duration)
213  {
214  NS_LOG_FUNCTION (this << duration);
215  ++m_notifyRxStart;
216  }
217  virtual void NotifyRxEndOk (void)
218  {
219  NS_LOG_FUNCTION (this);
220  ++m_notifyRxEndOk;
221  }
222  virtual void NotifyRxEndError (void)
223  {
224  NS_LOG_FUNCTION (this);
226  }
227  virtual void NotifyTxStart (Time duration, double txPowerDbm)
228  {
229  NS_LOG_FUNCTION (this << duration << txPowerDbm);
230  }
231  virtual void NotifyMaybeCcaBusyStart (Time duration)
232  {
233  NS_LOG_FUNCTION (this);
235  }
236  virtual void NotifySwitchingStart (Time duration)
237  {
238  }
239  virtual void NotifySleep (void)
240  {
241  }
242  virtual void NotifyOff (void)
243  {
244  }
245  virtual void NotifyWakeup (void)
246  {
247  }
248  virtual void NotifyOn (void)
249  {
250  }
251  uint32_t m_notifyRxStart;
252  uint32_t m_notifyRxEndOk;
255 private:
256 };
257 
265 {
266 public:
268  virtual ~SpectrumWifiPhyListenerTest ();
269 private:
270  virtual void DoSetup (void);
271  virtual void DoRun (void);
273 };
274 
276  : SpectrumWifiPhyBasicTest ("SpectrumWifiPhy test operation of WifiPhyListener")
277 {
278 }
279 
281 {
282 }
283 
284 void
286 {
290 }
291 
292 void
294 {
295  double txPowerWatts = 0.010;
296  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyListenerTest::SendSignal, this, txPowerWatts);
297  Simulator::Run ();
298 
299  NS_TEST_ASSERT_MSG_EQ (m_count, 1, "Didn't receive right number of packets");
300  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyMaybeCcaBusyStart, 2, "Didn't receive NotifyMaybeCcaBusyStart (preamble deteted + L-SIG received)");
301  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxStart, 1, "Didn't receive NotifyRxStart");
302  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxEndOk, 1, "Didn't receive NotifyRxEnd");
303 
304  Simulator::Destroy ();
305  delete m_listener;
306 }
307 
315 {
316 public:
318 };
319 
321  : TestSuite ("wifi-spectrum-wifi-phy", UNIT)
322 {
323  AddTestCase (new SpectrumWifiPhyBasicTest, TestCase::QUICK);
324  AddTestCase (new SpectrumWifiPhyListenerTest, TestCase::QUICK);
325 }
326 
static SpectrumWifiPhyTestSuite spectrumWifiPhyTestSuite
the test suite
uint32_t m_notifyMaybeCcaBusyStart
notify maybe CCA busy start
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
Ptr< WifiPpdu > ppdu
The PPDU being transmitted.
void StartRx(Ptr< SpectrumSignalParameters > rxParams)
Input method for delivering a signal from the spectrum channel and low-level PHY interface to this Sp...
WifiPhyBand GetPhyBand(void) const
Get the configured Wi-Fi band.
Definition: wifi-phy.cc:1425
A suite of tests to run.
Definition: test.h:1343
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 m_notifyRxEndError
notify receive end error
void SetReceiveErrorCallback(RxErrorCallback callback)
Definition: wifi-phy.cc:588
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
static const uint32_t FREQUENCY
Test Phy Listener.
The 5 GHz band.
Definition: wifi-phy-band.h:35
static const uint16_t CHANNEL_WIDTH
encapsulates test code
Definition: test.h:1153
virtual void NotifyMaybeCcaBusyStart(Time duration)
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void SetChannelNumber(uint8_t id)
Set channel number.
void SpectrumWifiPhyRxSuccess(Ptr< WifiPsdu > psdu, double snr, WifiTxVector txVector, std::vector< bool > statusPerMpdu)
Spectrum wifi receive success function.
uint32_t GetSize(void) const
Return the size of the PSDU in bytes.
Definition: wifi-psdu.cc:260
Spectrum Wifi Phy Basic Test.
uint32_t m_notifyRxEndOk
notify receive end OK
void SendSignal(double txPowerWatts)
Send signal function.
static Time CalculateTxDuration(uint32_t size, WifiTxVector txVector, WifiPhyBand band)
Definition: wifi-phy.cc:2539
virtual void NotifyWakeup(void)
Notify listeners that we woke up.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
receive notifications about PHY events.
Ptr< SpectrumSignalParameters > MakeSignal(double txPowerWatts)
Make signal function.
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:166
virtual void NotifyRxStart(Time duration)
Ptr< SpectrumPhy > txPhy
The SpectrumPhy instance that is making the transmission.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
TestPhyListener * m_listener
listener
virtual void NotifyRxEndError(void)
We have received the last bit of a packet for which NotifyRxStart was invoked first and...
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...
virtual void SetFrequency(uint16_t freq)
virtual void ConfigureStandardAndBand(WifiPhyStandard standard, WifiPhyBand band)
Configure the PHY-level parameters for different Wi-Fi standard.
void SetErrorRateModel(const Ptr< ErrorRateModel > rate)
Sets the error rate model.
Definition: wifi-phy.cc:783
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< SpectrumWifiPhy > m_phy
Phy.
virtual void NotifyOn(void)
Notify listeners that we went to switch on.
void SetType(WifiMacType type, bool resetToDsFromDs=true)
Set Type/Subtype values with the correct values depending on the given type.
Spectrum Wifi Phy Test Suite.
void SetReceiveOkCallback(RxOkCallback callback)
Definition: wifi-phy.cc:582
void RegisterListener(WifiPhyListener *listener)
Definition: wifi-phy.cc:594
Ptr< SpectrumValue > psd
The Power Spectral Density of the waveform, in linear units.
virtual void NotifyOff(void)
Notify listeners that we went to switch off.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
TestPhyListener(void)
Create a test PhyListener.
Time duration
The duration of the packet transmission.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1294
Spectrum Wifi Phy Listener Test.
void SpectrumWifiPhyRxFailure(Ptr< WifiPsdu > psdu)
Spectrum wifi receive failure function
static const uint16_t GUARD_WIDTH
virtual void NotifyTxStart(Time duration, double txPowerDbm)
uint32_t m_notifyRxStart
notify receive start
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
static const uint8_t CHANNEL_NUMBER
Implements the IEEE 802.11 MAC header.