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-mac-trailer.h"
27 #include "ns3/wifi-phy-tag.h"
28 #include "ns3/wifi-spectrum-signal-parameters.h"
29 #include "ns3/wifi-phy-listener.h"
30 #include "ns3/log.h"
31 #include "ns3/wifi-phy-header.h"
32 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE ("SpectrumWifiPhyBasicTest");
36 
37 static const uint8_t CHANNEL_NUMBER = 36;
38 static const uint32_t FREQUENCY = 5180; // MHz
39 static const uint16_t CHANNEL_WIDTH = 20; // MHz
40 static const uint16_t GUARD_WIDTH = CHANNEL_WIDTH; // MHz (expanded to channel width to model spectrum mask)
41 
49 {
50 public:
57  SpectrumWifiPhyBasicTest (std::string name);
58  virtual ~SpectrumWifiPhyBasicTest ();
59 
60 protected:
61  virtual void DoSetup (void);
63 
68  Ptr<SpectrumSignalParameters> MakeSignal (double txPowerWatts);
73  void SendSignal (double txPowerWatts);
81  void SpectrumWifiPhyRxSuccess (Ptr<Packet> p, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu);
86  void SpectrumWifiPhyRxFailure (Ptr<Packet> p);
87  uint32_t m_count;
88 
89 private:
90  virtual void DoRun (void);
91 };
92 
94  : TestCase ("SpectrumWifiPhy test case receives one packet"),
95  m_count (0)
96 {
97 }
98 
100  : TestCase (name),
101  m_count (0)
102 {
103 }
104 
105 // Make a Wi-Fi signal to inject directly to the StartRx() method
108 {
109  WifiTxVector txVector = WifiTxVector (WifiPhy::GetOfdmRate6Mbps (), 0, WIFI_PREAMBLE_LONG, 800, 1, 1, 0, 20, false, false);
110 
111  Ptr<Packet> pkt = Create<Packet> (1000);
112  WifiMacHeader hdr;
113  WifiMacTrailer trailer;
114 
115  hdr.SetType (WIFI_MAC_QOSDATA);
116  hdr.SetQosTid (0);
117  uint32_t size = pkt->GetSize () + hdr.GetSize () + trailer.GetSerializedSize ();
118  Time txDuration = m_phy->CalculateTxDuration (size, txVector, m_phy->GetFrequency ());
119  hdr.SetDuration (txDuration);
120 
121  pkt->AddHeader (hdr);
122  pkt->AddTrailer (trailer);
123 
124  LSigHeader sig;
125  pkt->AddHeader (sig);
126 
127  WifiPhyTag tag (txVector.GetPreambleType (), txVector.GetMode ().GetModulationClass (), 1);
128  pkt->AddPacketTag (tag);
129 
130  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts, GUARD_WIDTH);
131  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
132  txParams->psd = txPowerSpectrum;
133  txParams->txPhy = 0;
134  txParams->duration = txDuration;
135  txParams->packet = pkt;
136  return txParams;
137 }
138 
139 // Make a Wi-Fi signal to inject directly to the StartRx() method
140 void
142 {
143  m_phy->StartRx (MakeSignal (txPowerWatts));
144 }
145 
146 void
147 SpectrumWifiPhyBasicTest::SpectrumWifiPhyRxSuccess (Ptr<Packet> p, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu)
148 {
149  NS_LOG_FUNCTION (this << p << snr << txVector);
150  m_count++;
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this << p);
157  m_count++;
158 }
159 
161 {
162 }
163 
164 // Create necessary objects, and inject signals. Test that the expected
165 // number of packet receptions occur.
166 void
168 {
169  m_phy = CreateObject<SpectrumWifiPhy> ();
171  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel> ();
172  m_phy->SetErrorRateModel (error);
177 }
178 
179 // Test that the expected number of packet receptions occur.
180 void
182 {
183  double txPowerWatts = 0.010;
184  // Send packets spaced 1 second apart; all should be received
185  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
186  Simulator::Schedule (Seconds (2), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
187  Simulator::Schedule (Seconds (3), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
188  // Send packets spaced 1 microsecond second apart; none should be received (PHY header reception failure)
189  Simulator::Schedule (MicroSeconds (4000000), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
190  Simulator::Schedule (MicroSeconds (4000001), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
191  Simulator::Run ();
192  Simulator::Destroy ();
193 
194  NS_TEST_ASSERT_MSG_EQ (m_count, 3, "Didn't receive right number of packets");
195 }
196 
204 {
205 public:
211  : m_notifyRxStart (0),
212  m_notifyRxEndOk (0),
213  m_notifyRxEndError (0),
215  {
216  }
217  virtual ~TestPhyListener ()
218  {
219  }
220  virtual void NotifyRxStart (Time duration)
221  {
222  NS_LOG_FUNCTION (this << duration);
223  ++m_notifyRxStart;
224  }
225  virtual void NotifyRxEndOk (void)
226  {
227  NS_LOG_FUNCTION (this);
228  ++m_notifyRxEndOk;
229  }
230  virtual void NotifyRxEndError (void)
231  {
232  NS_LOG_FUNCTION (this);
234  }
235  virtual void NotifyTxStart (Time duration, double txPowerDbm)
236  {
237  NS_LOG_FUNCTION (this << duration << txPowerDbm);
238  }
239  virtual void NotifyMaybeCcaBusyStart (Time duration)
240  {
241  NS_LOG_FUNCTION (this);
243  }
244  virtual void NotifySwitchingStart (Time duration)
245  {
246  }
247  virtual void NotifySleep (void)
248  {
249  }
250  virtual void NotifyOff (void)
251  {
252  }
253  virtual void NotifyWakeup (void)
254  {
255  }
256  virtual void NotifyOn (void)
257  {
258  }
259  uint32_t m_notifyRxStart;
260  uint32_t m_notifyRxEndOk;
263 private:
264 };
265 
273 {
274 public:
276  virtual ~SpectrumWifiPhyListenerTest ();
277 private:
278  virtual void DoSetup (void);
279  virtual void DoRun (void);
281 };
282 
284  : SpectrumWifiPhyBasicTest ("SpectrumWifiPhy test operation of WifiPhyListener")
285 {
286 }
287 
289 {
290 }
291 
292 void
294 {
298 }
299 
300 void
302 {
303  double txPowerWatts = 0.010;
304  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyListenerTest::SendSignal, this, txPowerWatts);
305  Simulator::Run ();
306 
307  NS_TEST_ASSERT_MSG_EQ (m_count, 1, "Didn't receive right number of packets");
308  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxStart, 1, "Didn't receive NotifyRxStart");
309  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxEndOk, 1, "Didn't receive NotifyRxEnd");
310  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyMaybeCcaBusyStart, 0, "Received NotifyMaybeCcaBusyStart unexpectedly");
311 
312  Simulator::Destroy ();
313  delete m_listener;
314 }
315 
323 {
324 public:
326 };
327 
329  : TestSuite ("wifi-spectrum-wifi-phy", UNIT)
330 {
331  AddTestCase (new SpectrumWifiPhyBasicTest, TestCase::QUICK);
332  AddTestCase (new SpectrumWifiPhyListenerTest, TestCase::QUICK);
333 }
334 
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:102
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
HT 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...
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
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:1342
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:455
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
static const uint32_t FREQUENCY
Test Phy Listener.
static const uint16_t CHANNEL_WIDTH
encapsulates test code
Definition: test.h:1155
virtual void NotifyMaybeCcaBusyStart(Time duration)
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void SetChannelNumber(uint8_t id)
Set channel number.
Spectrum Wifi Phy Basic Test.
WifiPreamble GetPreambleType(void) const
Time CalculateTxDuration(uint32_t size, WifiTxVector txVector, uint16_t frequency)
Definition: wifi-phy.cc:2344
uint32_t m_notifyRxEndOk
notify receive end OK
void SpectrumWifiPhyRxSuccess(Ptr< Packet > p, double snr, WifiTxVector txVector, std::vector< bool > statusPerMpdu)
Spectrum wifi receive success function.
void SendSignal(double txPowerWatts)
Send signal function.
virtual void NotifyWakeup(void)
Notify listeners that we woke up.
virtual void ConfigureStandard(WifiPhyStandard standard)
Configure the PHY-level parameters for different Wi-Fi standard.
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)
uint32_t GetSerializedSize(void) const
#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:168
virtual void NotifyRxStart(Time duration)
WifiMode GetMode(void) const
Implements the IEEE 802.11 OFDM and ERP OFDM L-SIG PHY header.
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 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...
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:494
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)
void SetErrorRateModel(const Ptr< ErrorRateModel > rate)
Sets the error rate model.
Definition: wifi-phy.cc:762
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint16_t GetFrequency(void) const
Definition: wifi-phy.cc:1337
void SpectrumWifiPhyRxFailure(Ptr< Packet > p)
Spectrum wifi receive failure function
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
Ptr< Packet > packet
The packet being transmitted with this signal.
Ptr< SpectrumWifiPhy > m_phy
Phy.
virtual void NotifyOn(void)
Notify listeners that we went to switch on.
Spectrum Wifi Phy Test Suite.
void SetReceiveOkCallback(RxOkCallback callback)
Definition: wifi-phy.cc:449
void RegisterListener(WifiPhyListener *listener)
Definition: wifi-phy.cc:461
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:863
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:1062
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.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1078
Spectrum Wifi Phy Listener Test.
static const uint16_t GUARD_WIDTH
virtual void NotifyTxStart(Time duration, double txPowerDbm)
uint32_t m_notifyRxStart
notify receive start
static const uint8_t CHANNEL_NUMBER
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Implements the IEEE 802.11 MAC header.
Implements the IEEE 802.11 MAC trailer.