A Discrete-Event Network Simulator
API
wifi-phy-thresholds-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Sébastien Deronne <sebastien.deronne@gmail.com>
18  */
19 
20 #include "ns3/interference-helper.h"
21 #include "ns3/log.h"
22 #include "ns3/multi-model-spectrum-channel.h"
23 #include "ns3/nist-error-rate-model.h"
24 #include "ns3/ofdm-phy.h"
25 #include "ns3/ofdm-ppdu.h"
26 #include "ns3/spectrum-phy.h"
27 #include "ns3/spectrum-wifi-helper.h"
28 #include "ns3/spectrum-wifi-phy.h"
29 #include "ns3/test.h"
30 #include "ns3/wifi-mac-header.h"
31 #include "ns3/wifi-net-device.h"
32 #include "ns3/wifi-psdu.h"
33 #include "ns3/wifi-spectrum-phy-interface.h"
34 #include "ns3/wifi-spectrum-signal-parameters.h"
35 #include "ns3/wifi-spectrum-value-helper.h"
36 #include "ns3/wifi-utils.h"
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE("WifiPhyThresholdsTest");
41 
42 static const uint8_t CHANNEL_NUMBER = 36;
43 static const uint32_t FREQUENCY = 5180; // MHz
44 static const uint16_t CHANNEL_WIDTH = 20; // MHz
45 
53 {
54  public:
60  WifiPhyThresholdsTest(std::string test_name);
64  ~WifiPhyThresholdsTest() override;
65 
66  protected:
73  virtual Ptr<SpectrumSignalParameters> MakeWifiSignal(double txPowerWatts,
80  virtual Ptr<SpectrumSignalParameters> MakeForeignSignal(double txPowerWatts);
86  virtual void SendSignal(double txPowerWatts, bool wifiSignal);
94  virtual void RxSuccess(Ptr<const WifiPsdu> psdu,
95  RxSignalInfo rxSignalInfo,
96  WifiTxVector txVector,
97  std::vector<bool> statusPerMpdu);
102  virtual void RxFailure(Ptr<const WifiPsdu> psdu);
108  void RxDropped(Ptr<const Packet> p, WifiPhyRxfailureReason reason);
115  virtual void PhyStateChanged(Time start, Time duration, WifiPhyState newState);
116 
118  uint32_t m_rxSuccess;
119  uint32_t m_rxFailure;
120  uint32_t m_rxDropped;
121  uint32_t m_stateChanged;
122  uint32_t m_rxStateCount;
123  uint32_t m_idleStateCount;
125 
126  private:
127  void DoSetup() override;
128  void DoTeardown() override;
129 };
130 
132  : TestCase(test_name),
133  m_rxSuccess(0),
134  m_rxFailure(0),
135  m_rxDropped(0),
136  m_stateChanged(0),
137  m_rxStateCount(0),
138  m_idleStateCount(0),
139  m_ccabusyStateCount(0)
140 {
141 }
142 
144 {
145 }
146 
149 {
150  WifiTxVector txVector = WifiTxVector(OfdmPhy::GetOfdmRate6Mbps(),
151  0,
153  800,
154  1,
155  1,
156  0,
158  false);
159 
160  Ptr<Packet> pkt = Create<Packet>(1000);
161  WifiMacHeader hdr;
162 
164  hdr.SetQosTid(0);
165 
166  Ptr<WifiPsdu> psdu = Create<WifiPsdu>(pkt, hdr);
167  Time txDuration = m_phy->CalculateTxDuration(psdu->GetSize(), txVector, m_phy->GetPhyBand());
168 
169  Ptr<WifiPpdu> ppdu = Create<OfdmPpdu>(psdu, txVector, channel, 0);
170 
171  Ptr<SpectrumValue> txPowerSpectrum =
172  WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity(
173  channel.GetPrimaryChannelCenterFrequency(CHANNEL_WIDTH),
175  txPowerWatts,
176  CHANNEL_WIDTH);
177  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters>();
178  txParams->psd = txPowerSpectrum;
179  txParams->txPhy = nullptr;
180  txParams->duration = txDuration;
181  txParams->ppdu = ppdu;
182  return txParams;
183 }
184 
187 {
188  Ptr<SpectrumValue> txPowerSpectrum =
189  WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity(FREQUENCY,
191  txPowerWatts,
192  CHANNEL_WIDTH);
193  Ptr<SpectrumSignalParameters> txParams = Create<SpectrumSignalParameters>();
194  txParams->psd = txPowerSpectrum;
195  txParams->txPhy = nullptr;
196  txParams->duration = Seconds(0.5);
197  return txParams;
198 }
199 
200 void
201 WifiPhyThresholdsTest::SendSignal(double txPowerWatts, bool wifiSignal)
202 {
203  if (wifiSignal)
204  {
205  m_phy->StartRx(MakeWifiSignal(txPowerWatts, m_phy->GetOperatingChannel()), nullptr);
206  }
207  else
208  {
209  m_phy->StartRx(MakeForeignSignal(txPowerWatts), nullptr);
210  }
211 }
212 
213 void
215  RxSignalInfo rxSignalInfo,
216  WifiTxVector txVector,
217  std::vector<bool> statusPerMpdu)
218 {
219  NS_LOG_FUNCTION(this << *psdu << rxSignalInfo << txVector);
220  m_rxSuccess++;
221 }
222 
223 void
225 {
226  NS_LOG_FUNCTION(this << *psdu);
227  m_rxFailure++;
228 }
229 
230 void
232 {
233  NS_LOG_FUNCTION(this << p << reason);
234  m_rxDropped++;
235 }
236 
237 void
239 {
240  NS_LOG_FUNCTION(this << start << duration << newState);
241  m_stateChanged++;
242  if (newState == WifiPhyState::IDLE)
243  {
245  }
246  else if (newState == WifiPhyState::RX)
247  {
248  m_rxStateCount++;
249  }
250  else if (newState == WifiPhyState::CCA_BUSY)
251  {
253  }
254 }
255 
256 void
258 {
259  Ptr<MultiModelSpectrumChannel> spectrumChannel = CreateObject<MultiModelSpectrumChannel>();
260  Ptr<Node> node = CreateObject<Node>();
261  Ptr<WifiNetDevice> dev = CreateObject<WifiNetDevice>();
262  m_phy = CreateObject<SpectrumWifiPhy>();
263  Ptr<InterferenceHelper> interferenceHelper = CreateObject<InterferenceHelper>();
264  m_phy->SetInterferenceHelper(interferenceHelper);
265  Ptr<ErrorRateModel> error = CreateObject<NistErrorRateModel>();
266  m_phy->SetErrorRateModel(error);
267  m_phy->SetDevice(dev);
268  m_phy->AddChannel(spectrumChannel);
273  m_phy->TraceConnectWithoutContext("PhyRxDrop",
275  m_phy->GetState()->TraceConnectWithoutContext(
276  "State",
278  dev->SetPhy(m_phy);
279  node->AddDevice(dev);
280 }
281 
282 void
284 {
285  m_phy->Dispose();
286  m_phy = nullptr;
287 }
288 
299 {
300  public:
303  void DoRun() override;
304 };
305 
307  : WifiPhyThresholdsTest("WifiPhy reception thresholds: test weak wifi signal reception")
308 {
309 }
310 
312 {
313 }
314 
315 void
317 {
318  double txPowerWatts = DbmToW(-110);
319 
320  Simulator::Schedule(Seconds(1),
322  this,
323  txPowerWatts,
324  true);
325 
326  Simulator::Run();
327  Simulator::Destroy();
328 
330  0,
331  "Reception should not have been triggered if packet is weaker than "
332  "RxSensitivity threshold");
334  0,
335  "State should stay idle if reception involves a signal weaker than "
336  "RxSensitivity threshold");
337 }
338 
349 {
350  public:
353  void DoRun() override;
354 };
355 
357  : WifiPhyThresholdsTest("WifiPhy reception thresholds: test weak foreign signal reception")
358 {
359 }
360 
362 {
363 }
364 
365 void
367 {
368  double txPowerWatts = DbmToW(-90);
369 
370  Simulator::Schedule(Seconds(1),
372  this,
373  txPowerWatts,
374  false);
375 
376  Simulator::Run();
377  Simulator::Destroy();
378 
380  0,
381  "Reception of non-wifi packet should not be triggered");
383  0,
384  "State should stay idle if reception involves a signal weaker than "
385  "RxSensitivity threshold");
386 }
387 
398 {
399  public:
402  void DoRun() override;
403 };
404 
406  : WifiPhyThresholdsTest("WifiPhy reception thresholds: test strong wifi signal reception")
407 {
408 }
409 
411 {
412 }
413 
414 void
416 {
417  double txPowerWatts = DbmToW(-60);
418 
419  Simulator::Schedule(Seconds(1),
421  this,
422  txPowerWatts,
423  true);
424 
425  Simulator::Run();
426  Simulator::Destroy();
427 
429  0,
430  "Packet reception should have been successful");
431  NS_TEST_ASSERT_MSG_EQ(m_rxSuccess, 1, "Packet should have been successfully received");
432  NS_TEST_ASSERT_MSG_EQ(m_ccabusyStateCount, 2, "State should have moved to CCA_BUSY once");
435  4,
436  "State should have moved to CCA_BUSY, then to RX and finally back to IDLE");
437  NS_TEST_ASSERT_MSG_EQ(m_rxStateCount, 1, "State should have moved to RX once");
438  NS_TEST_ASSERT_MSG_EQ(m_idleStateCount, 1, "State should have moved to IDLE once");
439 }
440 
451 {
452  public:
455  void DoRun() override;
456 };
457 
459  : WifiPhyThresholdsTest("WifiPhy reception thresholds: test weak foreign signal reception")
460 {
461 }
462 
464 {
465 }
466 
467 void
469 {
470  double txPowerWatts = DbmToW(-60);
471 
472  Simulator::Schedule(Seconds(1),
474  this,
475  txPowerWatts,
476  false);
477 
478  Simulator::Run();
479  Simulator::Destroy();
480 
482  0,
483  "Reception of non-wifi packet should not be triggered");
485  1,
486  "State should have moved to CCA-BUSY then back to IDLE");
487 }
488 
496 {
497  public:
499 };
500 
502  : TestSuite("wifi-phy-thresholds", UNIT)
503 {
504  AddTestCase(new WifiPhyThresholdsWeakWifiSignalTest, TestCase::QUICK);
508 }
509 
Wifi Phy Threshold Strong Foreign Signal Test.
void DoRun() override
Implementation to actually run this TestCase.
Wifi Phy Threshold Strong Wifi Signal Test.
void DoRun() override
Implementation to actually run this TestCase.
Wifi Phy Threshold Test base class.
uint32_t m_rxDropped
count number of dropped packets
uint32_t m_ccabusyStateCount
count number of PHY state change to CCA_BUSY state
uint32_t m_idleStateCount
count number of PHY state change to IDLE state
uint32_t m_rxStateCount
count number of PHY state change to RX state
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
virtual void PhyStateChanged(Time start, Time duration, WifiPhyState newState)
PHY state changed callback function.
virtual Ptr< SpectrumSignalParameters > MakeWifiSignal(double txPowerWatts, const WifiPhyOperatingChannel &channel)
Make wifi signal function.
~WifiPhyThresholdsTest() override
Destructor.
uint32_t m_stateChanged
count number of PHY state change
uint32_t m_rxFailure
count number of unsuccessfuly received packets
virtual void RxFailure(Ptr< const WifiPsdu > psdu)
PHY receive failure callback function.
virtual Ptr< SpectrumSignalParameters > MakeForeignSignal(double txPowerWatts)
Make foreign signal function.
void RxDropped(Ptr< const Packet > p, WifiPhyRxfailureReason reason)
PHY dropped packet callback function.
Ptr< SpectrumWifiPhy > m_phy
PHY object.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
virtual void RxSuccess(Ptr< const WifiPsdu > psdu, RxSignalInfo rxSignalInfo, WifiTxVector txVector, std::vector< bool > statusPerMpdu)
PHY receive success callback function.
virtual void SendSignal(double txPowerWatts, bool wifiSignal)
Send signal function.
WifiPhyThresholdsTest(std::string test_name)
Constructor.
uint32_t m_rxSuccess
count number of successfully received packets
Wifi Phy Thresholds Test Suite.
Wifi Phy Threshold Weak Foreign Signal Test.
void DoRun() override
Implementation to actually run this TestCase.
Wifi Phy Threshold Weak Wifi Signal Test.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
void Dispose()
Dispose of this Object.
Definition: object.cc:219
void SetDevice(const Ptr< WifiNetDevice > device) override
Sets the device this PHY is associated with.
void StartRx(Ptr< SpectrumSignalParameters > rxParams, Ptr< const WifiSpectrumPhyInterface > interface)
Input method for delivering a signal from the spectrum channel and low-level PHY interface to this Sp...
void AddChannel(const Ptr< SpectrumChannel > channel, const FrequencyRange &freqRange=WHOLE_WIFI_SPECTRUM)
Attach a SpectrumChannel to use for a given frequency range.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Implements the IEEE 802.11 MAC header.
virtual void SetType(WifiMacType type, bool resetToDsFromDs=true)
Set Type/Subtype values with the correct values depending on the given type.
void SetQosTid(uint8_t tid)
Set the TID for the QoS header.
void SetPhy(const Ptr< WifiPhy > phy)
virtual void SetInterferenceHelper(const Ptr< InterferenceHelper > helper)
Sets the interference helper.
Definition: wifi-phy.cc:631
void SetErrorRateModel(const Ptr< ErrorRateModel > model)
Sets the error rate model.
Definition: wifi-phy.cc:639
void SetReceiveErrorCallback(RxErrorCallback callback)
Definition: wifi-phy.cc:455
virtual void ConfigureStandard(WifiStandard standard)
Configure the PHY-level parameters for different Wi-Fi standard.
Definition: wifi-phy.cc:961
static Time CalculateTxDuration(uint32_t size, const WifiTxVector &txVector, WifiPhyBand band, uint16_t staId=SU_STA_ID)
Definition: wifi-phy.cc:1507
Ptr< WifiPhyStateHelper > GetState() const
Return the WifiPhyStateHelper of this PHY.
Definition: wifi-phy.cc:443
void SetOperatingChannel(const ChannelTuple &channelTuple)
If the standard for this object has not been set yet, store the given channel settings.
Definition: wifi-phy.cc:1098
WifiPhyBand GetPhyBand() const
Get the configured Wi-Fi band.
Definition: wifi-phy.cc:1021
std::tuple< uint8_t, uint16_t, WifiPhyBand, uint8_t > ChannelTuple
Tuple identifying an operating channel.
Definition: wifi-phy.h:891
void SetReceiveOkCallback(RxOkCallback callback)
Definition: wifi-phy.cc:449
const WifiPhyOperatingChannel & GetOperatingChannel() const
Get a const reference to the operating channel.
Definition: wifi-phy.cc:1033
Class that keeps track of all information about the current PHY operating channel.
uint32_t GetSize() const
Return the size of the PSDU in bytes.
Definition: wifi-psdu.cc:273
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
WifiPhyRxfailureReason
Enumeration of the possible reception failure reasons.
@ WIFI_STANDARD_80211ax
@ WIFI_PREAMBLE_LONG
@ WIFI_PHY_BAND_5GHZ
The 5 GHz band.
Definition: wifi-phy-band.h:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:40
@ WIFI_MAC_QOSDATA
channel
Definition: third.py:88
RxSignalInfo structure containing info on the received signal.
Definition: phy-entity.h:69
Time duration
The duration of the packet transmission.
Ptr< SpectrumPhy > txPhy
The SpectrumPhy instance that is making the transmission.
Ptr< SpectrumValue > psd
The Power Spectral Density of the waveform, in linear units.
WifiPhyState
The state of the PHY layer.
@ CCA_BUSY
The PHY layer has sense the medium busy through the CCA mechanism.
@ RX
The PHY layer is receiving a packet.
@ IDLE
The PHY layer is IDLE.
static const uint8_t CHANNEL_NUMBER
static WifiPhyThresholdsTestSuite wifiPhyThresholdsTestSuite
the test suite
static const uint16_t CHANNEL_WIDTH
static const uint32_t FREQUENCY