A Discrete-Event Network Simulator
API
wifi-phy-thresholds-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2018
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  * Author: Sébastien Deronne <sebastien.deronne@gmail.com>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/test.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/nist-error-rate-model.h"
27 #include "ns3/wifi-mac-header.h"
28 #include "ns3/wifi-spectrum-signal-parameters.h"
29 #include "ns3/wifi-utils.h"
30 #include "ns3/wifi-psdu.h"
31 #include "ns3/wifi-ppdu.h"
32 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE ("WifiPhyThresholdsTest");
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 
48 {
49 public:
55  WifiPhyThresholdsTest (std::string test_name);
59  virtual ~WifiPhyThresholdsTest ();
60 
61 protected:
67  virtual Ptr<SpectrumSignalParameters> MakeWifiSignal (double txPowerWatts);
73  virtual Ptr<SpectrumSignalParameters> MakeForeignSignal (double txPowerWatts);
79  virtual void SendSignal (double txPowerWatts, bool wifiSignal);
87  virtual void RxSuccess (Ptr<WifiPsdu> psdu, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu);
92  virtual void RxFailure (Ptr<WifiPsdu> psdu);
98  void RxDropped (Ptr<const Packet> p, WifiPhyRxfailureReason reason);
105  virtual void PhyStateChanged (Time start, Time duration, WifiPhyState newState);
106 
108  uint32_t m_rxSuccess;
109  uint32_t m_rxFailure;
110  uint32_t m_rxDropped;
111  uint32_t m_stateChanged;
112  uint32_t m_rxStateCount;
113  uint32_t m_idleStateCount;
115 
116 private:
117  virtual void DoSetup (void);
118 };
119 
121  : TestCase (test_name),
122  m_rxSuccess (0),
123  m_rxFailure (0),
124  m_rxDropped (0),
125  m_stateChanged (0),
126  m_rxStateCount (0),
127  m_idleStateCount (0),
128  m_ccabusyStateCount (0)
129 {
130 }
131 
133 {
134 }
135 
138 {
139  WifiTxVector txVector = WifiTxVector (WifiPhy::GetOfdmRate6Mbps (), 0, WIFI_PREAMBLE_LONG, 800, 1, 1, 0, 20, false, false);
140 
141  Ptr<Packet> pkt = Create<Packet> (1000);
142  WifiMacHeader hdr;
143 
145  hdr.SetQosTid (0);
146 
147  Ptr<WifiPsdu> psdu = Create<WifiPsdu> (pkt, hdr);
148  Time txDuration = m_phy->CalculateTxDuration (psdu->GetSize (), txVector, m_phy->GetPhyBand ());
149 
150  Ptr<WifiPpdu> ppdu = Create<WifiPpdu> (psdu, txVector, txDuration, WIFI_PHY_BAND_5GHZ);
151 
152  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts, CHANNEL_WIDTH);
153  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
154  txParams->psd = txPowerSpectrum;
155  txParams->txPhy = 0;
156  txParams->duration = txDuration;
157  txParams->ppdu = ppdu;
158  return txParams;
159 }
160 
163 {
164  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts, CHANNEL_WIDTH);
165  Ptr<SpectrumSignalParameters> txParams = Create<SpectrumSignalParameters> ();
166  txParams->psd = txPowerSpectrum;
167  txParams->txPhy = 0;
168  txParams->duration = Seconds (0.5);
169  return txParams;
170 }
171 
172 void
173 WifiPhyThresholdsTest::SendSignal (double txPowerWatts, bool wifiSignal)
174 {
175  if (wifiSignal)
176  {
177  m_phy->StartRx (MakeWifiSignal (txPowerWatts));
178  }
179  else
180  {
181  m_phy->StartRx (MakeForeignSignal (txPowerWatts));
182  }
183 }
184 
185 void
186 WifiPhyThresholdsTest::RxSuccess (Ptr<WifiPsdu> psdu, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu)
187 {
188  NS_LOG_FUNCTION (this << *psdu << snr << txVector);
189  m_rxSuccess++;
190 }
191 
192 void
194 {
195  NS_LOG_FUNCTION (this << *psdu);
196  m_rxFailure++;
197 }
198 
199 void
201 {
202  NS_LOG_FUNCTION (this << p << reason);
203  m_rxDropped++;
204 }
205 
206 void
208 {
209  NS_LOG_FUNCTION (this << start << duration << newState);
210  m_stateChanged++;
211  if (newState == WifiPhyState::IDLE)
212  {
214  }
215  else if (newState == WifiPhyState::RX)
216  {
217  m_rxStateCount++;
218  }
219  else if (newState == WifiPhyState::CCA_BUSY)
220  {
222  }
223 }
224 
225 void
227 {
228  m_phy = CreateObject<SpectrumWifiPhy> ();
230  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel> ();
231  m_phy->SetErrorRateModel (error);
237  m_phy->GetState ()->TraceConnectWithoutContext ("State", MakeCallback (&WifiPhyThresholdsTest::PhyStateChanged, this));
238 }
239 
250 {
251 public:
254  virtual void DoRun (void);
255 };
256 
258  : WifiPhyThresholdsTest ("WifiPhy reception thresholds: test weak wifi signal reception")
259 {
260 }
261 
263 {
264 }
265 
266 void
268 {
269  double txPowerWatts = DbmToW (-110);
270 
271  Simulator::Schedule (Seconds (1), &WifiPhyThresholdsWeakWifiSignalTest::SendSignal, this, txPowerWatts, true);
272 
273  Simulator::Run ();
274  Simulator::Destroy ();
275 
276  NS_TEST_ASSERT_MSG_EQ (m_rxDropped + m_rxSuccess + m_rxFailure, 0, "Reception should not have been triggered if packet is weaker than RxSensitivity threshold");
277  NS_TEST_ASSERT_MSG_EQ (m_stateChanged, 0, "State should stay idle if reception involves a signal weaker than RxSensitivity threshold");
278 }
279 
290 {
291 public:
294  virtual void DoRun (void);
295 };
296 
298  : WifiPhyThresholdsTest ("WifiPhy reception thresholds: test weak foreign signal reception")
299 {
300 }
301 
303 {
304 }
305 
306 void
308 {
309  double txPowerWatts = DbmToW (-90);
310 
311  Simulator::Schedule (Seconds (1), &WifiPhyThresholdsWeakForeignSignalTest::SendSignal, this, txPowerWatts, false);
312 
313  Simulator::Run ();
314  Simulator::Destroy ();
315 
316  NS_TEST_ASSERT_MSG_EQ (m_rxDropped + m_rxSuccess + m_rxFailure, 0, "Reception of non-wifi packet should not be triggered");
317  NS_TEST_ASSERT_MSG_EQ (m_stateChanged, 0, "State should stay idle if reception involves a signal weaker than RxSensitivity threshold");
318 }
319 
330 {
331 public:
334  virtual void DoRun (void);
335 };
336 
338  : WifiPhyThresholdsTest ("WifiPhy reception thresholds: test strong wifi signal reception")
339 {
340 }
341 
343 {
344 }
345 
346 void
348 {
349  double txPowerWatts = DbmToW (-60);
350 
351  Simulator::Schedule (Seconds (1), &WifiPhyThresholdsStrongWifiSignalTest::SendSignal, this, txPowerWatts, true);
352 
353  Simulator::Run ();
354  Simulator::Destroy ();
355 
356  NS_TEST_ASSERT_MSG_EQ (m_rxDropped + m_rxFailure, 0, "Packet reception should have been successfull");
357  NS_TEST_ASSERT_MSG_EQ (m_rxSuccess, 1, "Packet should have been successfully received");
358  NS_TEST_ASSERT_MSG_EQ (m_ccabusyStateCount, 2, "State should have moved to CCA_BUSY once");
359  NS_TEST_ASSERT_MSG_EQ (m_stateChanged, 4, "State should have moved to CCA_BUSY, then to RX and finally back to IDLE");
360  NS_TEST_ASSERT_MSG_EQ (m_rxStateCount, 1, "State should have moved to RX once");
361  NS_TEST_ASSERT_MSG_EQ (m_idleStateCount, 1, "State should have moved to IDLE once");
362 }
363 
374 {
375 public:
378  virtual void DoRun (void);
379 };
380 
382  : WifiPhyThresholdsTest ("WifiPhy reception thresholds: test weak foreign signal reception")
383 {
384 }
385 
387 {
388 }
389 
390 void
392 {
393  double txPowerWatts = DbmToW (-60);
394 
395  Simulator::Schedule (Seconds (1), &WifiPhyThresholdsStrongForeignSignalTest::SendSignal, this, txPowerWatts, false);
396 
397  Simulator::Run ();
398  Simulator::Destroy ();
399 
400  NS_TEST_ASSERT_MSG_EQ (m_rxDropped + m_rxSuccess + m_rxFailure, 0, "Reception of non-wifi packet should not be triggered");
401  NS_TEST_ASSERT_MSG_EQ (m_idleStateCount, 1, "State should have moved to CCA-BUSY then back to IDLE");
402 }
403 
411 {
412 public:
414 };
415 
417  : TestSuite ("wifi-phy-thresholds", UNIT)
418 {
419  AddTestCase (new WifiPhyThresholdsWeakWifiSignalTest, TestCase::QUICK);
421  AddTestCase (new WifiPhyThresholdsStrongWifiSignalTest, TestCase::QUICK);
423 }
424 
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
static const uint32_t FREQUENCY
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.
Ptr< WifiPhyStateHelper > GetState(void) const
Return the WifiPhyStateHelper of this PHY.
Definition: wifi-phy.cc:576
uint32_t m_ccabusyStateCount
count number of PHY state change to CCA_BUSY state
void StartRx(Ptr< SpectrumSignalParameters > rxParams)
Input method for delivering a signal from the spectrum channel and low-level PHY interface to this Sp...
uint32_t m_rxDropped
count number of dropped packets
WifiPhyBand GetPhyBand(void) const
Get the configured Wi-Fi band.
Definition: wifi-phy.cc:1425
uint32_t m_rxSuccess
count number of successfully received packets
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:41
A suite of tests to run.
Definition: test.h:1343
def start()
Definition: core.py:1855
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
uint32_t m_rxStateCount
count number of PHY state change to RX state
static const uint8_t CHANNEL_NUMBER
Wifi Phy Threshold Strong Foreign Signal Test.
Wifi Phy Threshold Weak Wifi Signal Test.
virtual void DoRun(void)
Implementation to actually run this TestCase.
The 5 GHz band.
Definition: wifi-phy-band.h:35
encapsulates test code
Definition: test.h:1153
virtual void PhyStateChanged(Time start, Time duration, WifiPhyState newState)
PHY state changed callback function.
virtual void SetChannelNumber(uint8_t id)
Set channel number.
virtual Ptr< SpectrumSignalParameters > MakeWifiSignal(double txPowerWatts)
Make wifi signal function.
Wifi Phy Threshold Strong Wifi Signal Test.
Wifi Phy Threshold Weak Foreign Signal Test.
uint32_t GetSize(void) const
Return the size of the PSDU in bytes.
Definition: wifi-psdu.cc:260
void RxDropped(Ptr< const Packet > p, WifiPhyRxfailureReason reason)
PHY dropped packet callback function.
uint32_t m_stateChanged
count number of PHY state change
static WifiPhyThresholdsTestSuite wifiPhyThresholdsTestSuite
the test suite
static Time CalculateTxDuration(uint32_t size, WifiTxVector txVector, WifiPhyBand band)
Definition: wifi-phy.cc:2539
virtual void DoRun(void)
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
#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 DoSetup(void)
Implementation to do any local setup required for this TestCase.
The PHY layer has sense the medium busy through the CCA mechanism.
Ptr< SpectrumPhy > txPhy
The SpectrumPhy instance that is making the transmission.
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
virtual void SendSignal(double txPowerWatts, bool wifiSignal)
Send signal function.
virtual void DoRun(void)
Implementation to actually run this TestCase.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:293
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint16_t CHANNEL_WIDTH
The PHY layer is IDLE.
WifiPhyState
The state of the PHY layer.
void SetType(WifiMacType type, bool resetToDsFromDs=true)
Set Type/Subtype values with the correct values depending on the given type.
uint32_t m_rxFailure
count number of unsuccessfully received packets
void SetReceiveOkCallback(RxOkCallback callback)
Definition: wifi-phy.cc:582
Ptr< SpectrumValue > psd
The Power Spectral Density of the waveform, in linear units.
Ptr< SpectrumWifiPhy > m_phy
PHY object.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
WifiPhyRxfailureReason
Enumeration of the possible reception failure reasons.
Definition: wifi-phy.h:52
The PHY layer is receiving a packet.
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t m_idleStateCount
count number of PHY state change to IDLE state
virtual Ptr< SpectrumSignalParameters > MakeForeignSignal(double txPowerWatts)
Make foreign signal function.
virtual void RxFailure(Ptr< WifiPsdu > psdu)
PHY receive failure callback function.
Time duration
The duration of the packet transmission.
virtual ~WifiPhyThresholdsTest()
Destructor.
WifiPhyThresholdsTest(std::string test_name)
Constructor.
virtual void RxSuccess(Ptr< WifiPsdu > psdu, double snr, WifiTxVector txVector, std::vector< bool > statusPerMpdu)
PHY receive success callback function.
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
Implements the IEEE 802.11 MAC header.
Wifi Phy Threshold Test base class.
Wifi Phy Thresholds Test Suite.