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/spectrum-wifi-helper.h"
21 #include "ns3/wifi-spectrum-value-helper.h"
22 #include "ns3/spectrum-wifi-phy.h"
23 #include "ns3/nist-error-rate-model.h"
24 #include "ns3/wifi-mac-header.h"
25 #include "ns3/wifi-mac-trailer.h"
26 #include "ns3/wifi-phy-tag.h"
27 #include "ns3/wifi-spectrum-signal-parameters.h"
28 #include "ns3/log.h"
29 
30 using namespace ns3;
31 
32 NS_LOG_COMPONENT_DEFINE ("SpectrumWifiPhyBasicTest");
33 
34 static const uint8_t CHANNEL_NUMBER = 36;
35 static const uint32_t FREQUENCY = 5180; // MHz
36 static const uint8_t CHANNEL_WIDTH = 20; // MHz
37 static const uint8_t GUARD_WIDTH = CHANNEL_WIDTH; // MHz (expanded to channel width to model spectrum mask)
38 
46 {
47 public:
54  SpectrumWifiPhyBasicTest (std::string name);
55  virtual ~SpectrumWifiPhyBasicTest ();
56 protected:
57  virtual void DoSetup (void);
59 
64  Ptr<SpectrumSignalParameters> MakeSignal (double txPowerWatts);
69  void SendSignal (double txPowerWatts);
76  void SpectrumWifiPhyRxSuccess (Ptr<Packet> p, double snr, WifiTxVector txVector);
82  void SpectrumWifiPhyRxFailure (Ptr<Packet> p, double snr);
83  uint32_t m_count;
84 private:
85  virtual void DoRun (void);
86 };
87 
89  : TestCase ("SpectrumWifiPhy test case receives one packet"),
90  m_count (0)
91 {
92 }
93 
95  : TestCase (name),
96  m_count (0)
97 {
98 }
99 
100 // Make a Wi-Fi signal to inject directly to the StartRx() method
103 {
104  WifiTxVector txVector = WifiTxVector (WifiPhy::GetOfdmRate6Mbps (), 0, WIFI_PREAMBLE_LONG, false, 1, 1, 0, 20, false, false);
105  MpduType mpdutype = NORMAL_MPDU;
106 
107  Ptr<Packet> pkt = Create<Packet> (1000);
108  WifiMacHeader hdr;
109  WifiMacTrailer trailer;
110 
111  hdr.SetType (WIFI_MAC_QOSDATA);
112  hdr.SetQosTid (0);
113  uint32_t size = pkt->GetSize () + hdr.GetSize () + trailer.GetSerializedSize ();
114  Time txDuration = m_phy->CalculateTxDuration (size, txVector, m_phy->GetFrequency (), mpdutype, 0);
115  hdr.SetDuration (txDuration);
116 
117  pkt->AddHeader (hdr);
118  pkt->AddTrailer (trailer);
119  WifiPhyTag tag (txVector, mpdutype, 1);
120  pkt->AddPacketTag (tag);
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->packet = pkt;
127  return txParams;
128 }
129 
130 // Make a Wi-Fi signal to inject directly to the StartRx() method
131 void
133 {
134  m_phy->StartRx (MakeSignal (txPowerWatts));
135 }
136 
137 void
139 {
140  NS_LOG_FUNCTION (this << p << snr << txVector);
141  m_count++;
142 }
143 
144 void
146 {
147  NS_LOG_FUNCTION (this << p << snr);
148  m_count++;
149 }
150 
152 {
153 }
154 
155 // Create necessary objects, and inject signals. Test that the expected
156 // number of packet receptions occur.
157 void
159 {
160  m_phy = CreateObject<SpectrumWifiPhy> ();
162  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel> ();
163  m_phy->SetErrorRateModel (error);
168  //Bug 2460: CcaMode1Threshold default should be set to -62 dBm when using Spectrum
169  m_phy->SetCcaMode1Threshold (-62.0);
170 }
171 
172 // Test that the expected number of packet receptions occur.
173 void
175 {
176  double txPowerWatts = 0.010;
177  // Send packets spaced 1 second apart; all should be received
178  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
179  Simulator::Schedule (Seconds (2), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
180  Simulator::Schedule (Seconds (3), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
181  // Send packets spaced 1 microsecond second apart; only one should be received
182  Simulator::Schedule (MicroSeconds (4000000), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
183  Simulator::Schedule (MicroSeconds (4000001), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
184  Simulator::Run ();
185  Simulator::Destroy ();
186 
187  NS_TEST_ASSERT_MSG_EQ (m_count, 4, "Didn't receive right number of packets");
188 }
189 
197 {
198 public:
204  : m_notifyRxStart (0),
205  m_notifyRxEndOk (0),
206  m_notifyRxEndError (0),
208  {
209  }
210  virtual ~TestPhyListener ()
211  {
212  }
213  virtual void NotifyRxStart (Time duration)
214  {
215  NS_LOG_FUNCTION (this << duration);
216  ++m_notifyRxStart;
217  }
218  virtual void NotifyRxEndOk (void)
219  {
220  NS_LOG_FUNCTION (this);
221  ++m_notifyRxEndOk;
222  }
223  virtual void NotifyRxEndError (void)
224  {
225  NS_LOG_FUNCTION (this);
227  }
228  virtual void NotifyTxStart (Time duration, double txPowerDbm)
229  {
230  NS_LOG_FUNCTION (this << duration << txPowerDbm);
231  }
232  virtual void NotifyMaybeCcaBusyStart (Time duration)
233  {
234  NS_LOG_FUNCTION (this);
236  }
237  virtual void NotifySwitchingStart (Time duration)
238  {
239  }
240  virtual void NotifySleep (void)
241  {
242  }
243  virtual void NotifyOff (void)
244  {
245  }
246  virtual void NotifyWakeup (void)
247  {
248  }
249  virtual void NotifyOn (void)
250  {
251  }
252  uint32_t m_notifyRxStart;
253  uint32_t m_notifyRxEndOk;
256 private:
257 };
258 
266 {
267 public:
269  virtual ~SpectrumWifiPhyListenerTest ();
270 private:
271  virtual void DoSetup (void);
272  virtual void DoRun (void);
274 };
275 
277  : SpectrumWifiPhyBasicTest ("SpectrumWifiPhy test operation of WifiPhyListener")
278 {
279 }
280 
282 {
283 }
284 
285 void
287 {
291 }
292 
293 void
295 {
296  double txPowerWatts = 0.010;
297  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyListenerTest::SendSignal, this, txPowerWatts);
298  Simulator::Run ();
299 
300  NS_TEST_ASSERT_MSG_EQ (m_count, 1, "Didn't receive right number of packets");
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  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyMaybeCcaBusyStart, 0, "Received NotifyMaybeCcaBusyStart unexpectedly");
304 
305  Simulator::Destroy ();
306  delete m_listener;
307 }
308 
316 {
317 public:
319 };
320 
322  : TestSuite ("spectrum-wifi-phy", UNIT)
323 {
324  AddTestCase (new SpectrumWifiPhyBasicTest, TestCase::QUICK);
325  AddTestCase (new SpectrumWifiPhyListenerTest, TestCase::QUICK);
326 }
327 
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
The MPDU is not part of an A-MPDU.
Definition: wifi-phy.h:64
#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...
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
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:852
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:412
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:821
static const uint32_t FREQUENCY
Test Phy Listener.
encapsulates test code
Definition: test.h:1155
virtual void NotifyMaybeCcaBusyStart(Time duration)
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SpectrumWifiPhyRxFailure(Ptr< Packet > p, double snr)
Spectrum wifi receive failure function.
uint16_t GetFrequency(void) const
Definition: wifi-phy.cc:1260
virtual void SetChannelNumber(uint8_t id)
Set channel number.
Spectrum Wifi Phy Basic Test.
Time CalculateTxDuration(uint32_t size, WifiTxVector txVector, uint16_t frequency)
Definition: wifi-phy.cc:2301
uint32_t m_notifyRxEndOk
notify receive end OK
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
void SpectrumWifiPhyRxSuccess(Ptr< Packet > p, double snr, WifiTxVector txVector)
Spectrum wifi receive success function.
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:168
virtual void NotifyRxStart(Time duration)
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...
virtual void DoRun(void)
Implementation to actually run this TestCase.
static const uint8_t CHANNEL_WIDTH
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:685
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
void SetCcaMode1Threshold(double threshold)
Sets the CCA threshold (dBm).
Definition: wifi-phy.cc:483
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:406
void RegisterListener(WifiPhyListener *listener)
Definition: wifi-phy.cc:418
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.
static const uint8_t GUARD_WIDTH
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1007
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:35
Time duration
The duration of the packet transmission.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1023
Spectrum Wifi Phy Listener Test.
virtual void NotifyTxStart(Time duration, double txPowerDbm)
MpduType
This enumeration defines the type of an MPDU.
Definition: wifi-phy.h:61
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.
uint32_t GetSerializedSize(void) const