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-mac-trailer.h"
29 #include "ns3/wifi-phy-tag.h"
30 #include "ns3/wifi-spectrum-signal-parameters.h"
31 #include "ns3/wifi-utils.h"
32 #include "ns3/wifi-phy-header.h"
33 
34 using namespace ns3;
35 
36 NS_LOG_COMPONENT_DEFINE ("WifiPhyThresholdsTest");
37 
38 static const uint8_t CHANNEL_NUMBER = 36;
39 static const uint32_t FREQUENCY = 5180; //MHz
40 static const uint16_t CHANNEL_WIDTH = 20; //MHz
41 
49 {
50 public:
51  WifiPhyThresholdsTest (std::string test_name);
52  virtual ~WifiPhyThresholdsTest ();
53 
54 protected:
60  virtual Ptr<SpectrumSignalParameters> MakeWifiSignal (double txPowerWatts);
66  virtual Ptr<SpectrumSignalParameters> MakeForeignSignal (double txPowerWatts);
72  virtual void SendSignal (double txPowerWatts, bool wifiSignal);
80  virtual void RxSuccess (Ptr<Packet> p, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu);
85  virtual void RxFailure (Ptr<Packet> p);
91  void RxDropped (Ptr<const Packet> p, WifiPhyRxfailureReason reason);
98  virtual void PhyStateChanged (Time start, Time duration, WifiPhyState newState);
99 
101  uint32_t m_rxSuccess;
102  uint32_t m_rxFailure;
103  uint32_t m_rxDropped;
104  uint32_t m_stateChanged;
105  uint32_t m_rxStateCount;
106  uint32_t m_idleStateCount;
108 
109 private:
110  virtual void DoSetup (void);
111 };
112 
114  : TestCase (test_name),
115  m_rxSuccess (0),
116  m_rxFailure (0),
117  m_rxDropped (0),
118  m_stateChanged (0),
119  m_rxStateCount (0),
120  m_idleStateCount (0),
121  m_ccabusyStateCount (0)
122 {
123 }
124 
126 {
127 }
128 
131 {
132  WifiTxVector txVector = WifiTxVector (WifiPhy::GetOfdmRate6Mbps (), 0, WIFI_PREAMBLE_LONG, false, 1, 1, 0, 20, false, false);
133 
134  Ptr<Packet> pkt = Create<Packet> (1000);
135  WifiMacHeader hdr;
136  WifiMacTrailer trailer;
137 
138  hdr.SetType (WIFI_MAC_QOSDATA);
139  hdr.SetQosTid (0);
140  uint32_t size = pkt->GetSize () + hdr.GetSize () + trailer.GetSerializedSize ();
141  Time txDuration = m_phy->CalculateTxDuration (size, txVector, m_phy->GetFrequency ());
142  hdr.SetDuration (txDuration);
143 
144  pkt->AddHeader (hdr);
145  pkt->AddTrailer (trailer);
146 
147  LSigHeader sig;
148  pkt->AddHeader (sig);
149 
150  WifiPhyTag tag (txVector.GetPreambleType (), txVector.GetMode ().GetModulationClass (), 1);
151  pkt->AddPacketTag (tag);
152 
153  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts, CHANNEL_WIDTH);
154  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
155  txParams->psd = txPowerSpectrum;
156  txParams->txPhy = 0;
157  txParams->duration = txDuration;
158  txParams->packet = pkt;
159  return txParams;
160 }
161 
164 {
165  Ptr<SpectrumValue> txPowerSpectrum = WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity (FREQUENCY, CHANNEL_WIDTH, txPowerWatts, CHANNEL_WIDTH);
166  Ptr<SpectrumSignalParameters> txParams = Create<SpectrumSignalParameters> ();
167  txParams->psd = txPowerSpectrum;
168  txParams->txPhy = 0;
169  txParams->duration = Seconds (0.5);
170  return txParams;
171 }
172 
173 void
174 WifiPhyThresholdsTest::SendSignal (double txPowerWatts, bool wifiSignal)
175 {
176  if (wifiSignal)
177  {
178  m_phy->StartRx (MakeWifiSignal (txPowerWatts));
179  }
180  else
181  {
182  m_phy->StartRx (MakeForeignSignal (txPowerWatts));
183  }
184 }
185 
186 void
187 WifiPhyThresholdsTest::RxSuccess (Ptr<Packet> p, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu)
188 {
189  NS_LOG_FUNCTION (this << p << snr << txVector);
190  m_rxSuccess++;
191 }
192 
193 void
195 {
196  NS_LOG_FUNCTION (this << p);
197  m_rxFailure++;
198 }
199 
200 void
202 {
203  NS_LOG_FUNCTION (this << p << reason);
204  m_rxDropped++;
205 }
206 
207 void
209 {
210  NS_LOG_FUNCTION (this << start << duration << newState);
211  m_stateChanged++;
212  if (newState == WifiPhyState::IDLE)
213  {
215  }
216  else if (newState == WifiPhyState::RX)
217  {
218  m_rxStateCount++;
219  }
220  else if (newState == WifiPhyState::CCA_BUSY)
221  {
223  }
224 }
225 
226 void
228 {
229  m_phy = CreateObject<SpectrumWifiPhy> ();
231  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel> ();
232  m_phy->SetErrorRateModel (error);
238  m_phy->GetState ()->TraceConnectWithoutContext ("State", MakeCallback (&WifiPhyThresholdsTest::PhyStateChanged, this));
239 }
240 
251 {
252 public:
255  virtual void DoRun (void);
256 };
257 
259  : WifiPhyThresholdsTest ("WifiPhy reception thresholds: test weak wifi signal reception")
260 {
261 }
262 
264 {
265 }
266 
267 void
269 {
270  double txPowerWatts = DbmToW (-110);
271 
272  Simulator::Schedule (Seconds (1), &WifiPhyThresholdsWeakWifiSignalTest::SendSignal, this, txPowerWatts, true);
273 
274  Simulator::Run ();
275  Simulator::Destroy ();
276 
277  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");
278  NS_TEST_ASSERT_MSG_EQ (m_stateChanged, 0, "State should stay idle if reception involves a signal weaker than RxSensitivity threshold");
279 }
280 
291 {
292 public:
295  virtual void DoRun (void);
296 };
297 
299  : WifiPhyThresholdsTest ("WifiPhy reception thresholds: test weak foreign signal reception")
300 {
301 }
302 
304 {
305 }
306 
307 void
309 {
310  double txPowerWatts = DbmToW (-90);
311 
312  Simulator::Schedule (Seconds (1), &WifiPhyThresholdsWeakForeignSignalTest::SendSignal, this, txPowerWatts, false);
313 
314  Simulator::Run ();
315  Simulator::Destroy ();
316 
317  NS_TEST_ASSERT_MSG_EQ (m_rxDropped + m_rxSuccess + m_rxFailure, 0, "Reception of non-wifi packet should not be triggered");
318  NS_TEST_ASSERT_MSG_EQ (m_stateChanged, 0, "State should stay idle if reception involves a signal weaker than RxSensitivity threshold");
319 }
320 
331 {
332 public:
335  virtual void DoRun (void);
336 };
337 
339  : WifiPhyThresholdsTest ("WifiPhy reception thresholds: test strong wifi signal reception")
340 {
341 }
342 
344 {
345 }
346 
347 void
349 {
350  double txPowerWatts = DbmToW (-60);
351 
352  Simulator::Schedule (Seconds (1), &WifiPhyThresholdsStrongWifiSignalTest::SendSignal, this, txPowerWatts, true);
353 
354  Simulator::Run ();
355  Simulator::Destroy ();
356 
357  NS_TEST_ASSERT_MSG_EQ (m_rxDropped + m_rxFailure, 0, "Packet reception should have been successfull");
358  NS_TEST_ASSERT_MSG_EQ (m_rxSuccess, 1, "Packet should have been successfully received");
359  NS_TEST_ASSERT_MSG_EQ (m_stateChanged, 2, "State should have moved to RX then 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_ccabusyStateCount, 1, "State should have moved to CCA-BUSY");
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:102
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...
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
virtual void RxSuccess(Ptr< Packet > p, double snr, WifiTxVector txVector, std::vector< bool > statusPerMpdu)
PHY receive success callback function.
Ptr< WifiPhyStateHelper > GetState(void) const
Return the WifiPhyStateHelper of this PHY.
Definition: wifi-phy.cc:443
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
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:1342
def start()
Definition: core.py:1858
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
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.
encapsulates test code
Definition: test.h:1155
virtual void RxFailure(Ptr< Packet > p)
PHY receive failure callback function.
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.
void RxDropped(Ptr< const Packet > p, WifiPhyRxfailureReason reason)
PHY dropped packet callback function.
WifiPreamble GetPreambleType(void) const
Time CalculateTxDuration(uint32_t size, WifiTxVector txVector, uint16_t frequency)
Definition: wifi-phy.cc:2344
HE PHY for the 5 GHz band (clause 26)
uint32_t m_stateChanged
count number of PHY state change
static WifiPhyThresholdsTestSuite wifiPhyThresholdsTestSuite
the test suite
virtual void DoRun(void)
Implementation to actually run this TestCase.
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
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 DoSetup(void)
Implementation to do any local setup required for this TestCase.
WifiMode GetMode(void) const
The PHY layer has sense the medium busy through the CCA mechanism.
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
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:494
virtual void SetFrequency(uint16_t freq)
void SetErrorRateModel(const Ptr< ErrorRateModel > rate)
Sets the error rate model.
Definition: wifi-phy.cc:762
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
uint16_t GetFrequency(void) const
Definition: wifi-phy.cc:1337
The PHY layer is IDLE.
WifiPhyState
The state of the PHY layer.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
Ptr< Packet > packet
The packet being transmitted with this signal.
uint32_t m_rxFailure
count number of unsuccessfully received packets
void SetReceiveOkCallback(RxOkCallback callback)
Definition: wifi-phy.cc:449
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.
Ptr< SpectrumWifiPhy > m_phy
PHY object.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
WifiPhyRxfailureReason
Definition: wifi-phy.h:48
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.
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.
WifiPhyThresholdsTest(std::string test_name)
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.
Wifi Phy Threshold Test base class.
Wifi Phy Thresholds Test Suite.