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 
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 protected:
59  virtual void DoSetup (void);
61 
66  Ptr<SpectrumSignalParameters> MakeSignal (double txPowerWatts);
71  void SendSignal (double txPowerWatts);
78  void SpectrumWifiPhyRxSuccess (Ptr<Packet> p, double snr, WifiTxVector txVector);
84  void SpectrumWifiPhyRxFailure (Ptr<Packet> p, double snr);
85  uint32_t m_count;
86 private:
87  virtual void DoRun (void);
88 };
89 
91  : TestCase ("SpectrumWifiPhy test case receives one packet"),
92  m_count (0)
93 {
94 }
95 
97  : TestCase (name),
98  m_count (0)
99 {
100 }
101 
102 // Make a Wi-Fi signal to inject directly to the StartRx() method
105 {
106  WifiTxVector txVector = WifiTxVector (WifiPhy::GetOfdmRate6Mbps (), 0, WIFI_PREAMBLE_LONG, false, 1, 1, 0, 20, false, false);
107  MpduType mpdutype = NORMAL_MPDU;
108 
109  Ptr<Packet> pkt = Create<Packet> (1000);
110  WifiMacHeader hdr;
111  WifiMacTrailer trailer;
112 
113  hdr.SetType (WIFI_MAC_QOSDATA);
114  hdr.SetQosTid (0);
115  uint32_t size = pkt->GetSize () + hdr.GetSize () + trailer.GetSerializedSize ();
116  Time txDuration = m_phy->CalculateTxDuration (size, txVector, m_phy->GetFrequency (), mpdutype, 0);
117  hdr.SetDuration (txDuration);
118 
119  pkt->AddHeader (hdr);
120  pkt->AddTrailer (trailer);
121  WifiPhyTag tag (txVector, mpdutype, 1);
122  pkt->AddPacketTag (tag);
123  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts, GUARD_WIDTH);
124  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
125  txParams->psd = txPowerSpectrum;
126  txParams->txPhy = 0;
127  txParams->duration = txDuration;
128  txParams->packet = pkt;
129  return txParams;
130 }
131 
132 // Make a Wi-Fi signal to inject directly to the StartRx() method
133 void
135 {
136  m_phy->StartRx (MakeSignal (txPowerWatts));
137 }
138 
139 void
141 {
142  NS_LOG_FUNCTION (this << p << snr << txVector);
143  m_count++;
144 }
145 
146 void
148 {
149  NS_LOG_FUNCTION (this << p << snr);
150  m_count++;
151 }
152 
154 {
155 }
156 
157 // Create necessary objects, and inject signals. Test that the expected
158 // number of packet receptions occur.
159 void
161 {
162  m_phy = CreateObject<SpectrumWifiPhy> ();
164  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel> ();
165  m_phy->SetErrorRateModel (error);
170  //Bug 2460: CcaMode1Threshold default should be set to -62 dBm when using Spectrum
171  m_phy->SetCcaMode1Threshold (-62.0);
172 }
173 
174 // Test that the expected number of packet receptions occur.
175 void
177 {
178  double txPowerWatts = 0.010;
179  // Send packets spaced 1 second apart; all should be received
180  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
181  Simulator::Schedule (Seconds (2), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
182  Simulator::Schedule (Seconds (3), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
183  // Send packets spaced 1 microsecond second apart; only one should be received
184  Simulator::Schedule (MicroSeconds (4000000), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
185  Simulator::Schedule (MicroSeconds (4000001), &SpectrumWifiPhyBasicTest::SendSignal, this, txPowerWatts);
186  Simulator::Run ();
187  Simulator::Destroy ();
188 
189  NS_TEST_ASSERT_MSG_EQ (m_count, 4, "Didn't receive right number of packets");
190 }
191 
199 {
200 public:
206  : m_notifyRxStart (0),
207  m_notifyRxEndOk (0),
208  m_notifyRxEndError (0),
209  m_notifyMaybeCcaBusyStart (0)
210  {
211  }
212  virtual ~TestPhyListener ()
213  {
214  }
215  virtual void NotifyRxStart (Time duration)
216  {
217  NS_LOG_FUNCTION (this << duration);
218  ++m_notifyRxStart;
219  }
220  virtual void NotifyRxEndOk (void)
221  {
222  NS_LOG_FUNCTION (this);
223  ++m_notifyRxEndOk;
224  }
225  virtual void NotifyRxEndError (void)
226  {
227  NS_LOG_FUNCTION (this);
228  ++m_notifyRxEndError;
229  }
230  virtual void NotifyTxStart (Time duration, double txPowerDbm)
231  {
232  NS_LOG_FUNCTION (this << duration << txPowerDbm);
233  }
234  virtual void NotifyMaybeCcaBusyStart (Time duration)
235  {
236  NS_LOG_FUNCTION (this);
237  ++m_notifyMaybeCcaBusyStart;
238  }
239  virtual void NotifySwitchingStart (Time duration)
240  {
241  }
242  virtual void NotifySleep (void)
243  {
244  }
245  virtual void NotifyOff (void)
246  {
247  }
248  virtual void NotifyWakeup (void)
249  {
250  }
251  virtual void NotifyOn (void)
252  {
253  }
254  uint32_t m_notifyRxStart;
255  uint32_t m_notifyRxEndOk;
258 private:
259 };
260 
268 {
269 public:
271  virtual ~SpectrumWifiPhyListenerTest ();
272 private:
273  virtual void DoSetup (void);
274  virtual void DoRun (void);
276 };
277 
279  : SpectrumWifiPhyBasicTest ("SpectrumWifiPhy test operation of WifiPhyListener")
280 {
281 }
282 
284 {
285 }
286 
287 void
289 {
293 }
294 
295 void
297 {
298  double txPowerWatts = 0.010;
299  Simulator::Schedule (Seconds (1), &SpectrumWifiPhyListenerTest::SendSignal, this, txPowerWatts);
300  Simulator::Run ();
301 
302  NS_TEST_ASSERT_MSG_EQ (m_count, 1, "Didn't receive right number of packets");
303  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxStart, 1, "Didn't receive NotifyRxStart");
304  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyRxEndOk, 1, "Didn't receive NotifyRxEnd");
305  NS_TEST_ASSERT_MSG_EQ (m_listener->m_notifyMaybeCcaBusyStart, 0, "Received NotifyMaybeCcaBusyStart unexpectedly");
306 
307  Simulator::Destroy ();
308  delete m_listener;
309 }
310 
318 {
319 public:
321 };
322 
324  : TestSuite ("spectrum-wifi-phy", UNIT)
325 {
326  AddTestCase (new SpectrumWifiPhyBasicTest, TestCase::QUICK);
327  AddTestCase (new SpectrumWifiPhyListenerTest, TestCase::QUICK);
328 }
329 
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...
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:202
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
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.
void SpectrumWifiPhyRxFailure(Ptr< Packet > p, double snr)
Spectrum wifi receive failure function.
uint16_t GetFrequency(void) const
Definition: wifi-phy.cc:1235
virtual void SetChannelNumber(uint8_t id)
Set channel number.
MpduType
The type of an MPDU.
Spectrum Wifi Phy Basic Test.
Time CalculateTxDuration(uint32_t size, WifiTxVector txVector, uint16_t frequency)
Definition: wifi-phy.cc:2226
uint32_t m_notifyRxEndOk
notify receive end OK
The MPDU is not part of an A-MPDU.
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.
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:673
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:477
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.
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:36
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.
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.
uint32_t GetSerializedSize(void) const