A Discrete-Event Network Simulator
API
spectrum-wifi-phy.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
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  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  * Ghada Badawy <gbadawy@gmail.com>
20  * S├ębastien Deronne <sebastien.deronne@gmail.com>
21  *
22  * Ported from yans-wifi-phy.cc by several contributors starting
23  * with Nicola Baldo and Dean Armstrong
24  */
25 
26 #include "ns3/wifi-spectrum-value-helper.h"
27 #include "ns3/log.h"
28 #include "ns3/boolean.h"
29 #include "ns3/net-device.h"
30 #include "ns3/node.h"
31 #include "spectrum-wifi-phy.h"
34 #include "wifi-utils.h"
35 #include "wifi-ppdu.h"
36 #include "wifi-psdu.h"
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("SpectrumWifiPhy");
41 
42 NS_OBJECT_ENSURE_REGISTERED (SpectrumWifiPhy);
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::SpectrumWifiPhy")
48  .SetParent<WifiPhy> ()
49  .SetGroupName ("Wifi")
50  .AddConstructor<SpectrumWifiPhy> ()
51  .AddAttribute ("DisableWifiReception", "Prevent Wi-Fi frame sync from ever happening",
52  BooleanValue (false),
55  .AddTraceSource ("SignalArrival",
56  "Signal arrival",
58  "ns3::SpectrumWifiPhy::SignalArrivalCallback")
59  ;
60  return tid;
61 }
62 
64 {
65  NS_LOG_FUNCTION (this);
66 }
67 
69 {
70  NS_LOG_FUNCTION (this);
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION (this);
77  m_channel = 0;
80 }
81 
82 void
84 {
85  NS_LOG_FUNCTION (this);
87  // This connection is deferred until frequency and channel width are set
89  {
91  }
92  else
93  {
94  NS_FATAL_ERROR ("SpectrumWifiPhy misses channel and WifiSpectrumPhyInterface objects at initialization time");
95  }
96 }
97 
100 {
101  NS_LOG_FUNCTION (this);
102  if (m_rxSpectrumModel)
103  {
104  return m_rxSpectrumModel;
105  }
106  else
107  {
108  if (GetFrequency () == 0)
109  {
110  NS_LOG_DEBUG ("Frequency is not set; returning 0");
111  return 0;
112  }
113  else
114  {
115  uint16_t channelWidth = GetChannelWidth ();
116  NS_LOG_DEBUG ("Creating spectrum model from frequency/width pair of (" << GetFrequency () << ", " << channelWidth << ")");
118  }
119  }
120  return m_rxSpectrumModel;
121 }
122 
125 {
126  return m_channel;
127 }
128 
129 void
131 {
132  m_channel = channel;
133 }
134 
135 void
137 {
138  NS_LOG_FUNCTION (this);
139  NS_ASSERT_MSG (IsInitialized (), "Executing method before run-time");
140  uint16_t channelWidth = GetChannelWidth ();
141  NS_LOG_DEBUG ("Run-time change of spectrum model from frequency/width pair of (" << GetFrequency () << ", " << channelWidth << ")");
142  // Replace existing spectrum model with new one, and must call AddRx ()
143  // on the SpectrumChannel to provide this new spectrum model to it
146 }
147 
148 void
150 {
151  NS_LOG_FUNCTION (this << +nch);
153  if (IsInitialized ())
154  {
156  }
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION (this << freq);
163  WifiPhy::SetFrequency (freq);
164  if (IsInitialized ())
165  {
167  }
168 }
169 
170 void
171 SpectrumWifiPhy::SetChannelWidth (uint16_t channelwidth)
172 {
173  NS_LOG_FUNCTION (this << channelwidth);
174  WifiPhy::SetChannelWidth (channelwidth);
175  if (IsInitialized ())
176  {
178  }
179 }
180 
181 void
183 {
184  NS_LOG_FUNCTION (this << standard << band);
185  WifiPhy::ConfigureStandardAndBand (standard, band);
186  if (IsInitialized ())
187  {
189  }
190 }
191 
192 void
194 {
195  NS_LOG_FUNCTION (this << rxParams);
196  Time rxDuration = rxParams->duration;
197  Ptr<SpectrumValue> receivedSignalPsd = rxParams->psd;
198  NS_LOG_DEBUG ("Received signal with PSD " << *receivedSignalPsd << " and duration " << rxDuration.As (Time::NS));
199  uint32_t senderNodeId = 0;
200  if (rxParams->txPhy)
201  {
202  senderNodeId = rxParams->txPhy->GetDevice ()->GetNode ()->GetId ();
203  }
204  NS_LOG_DEBUG ("Received signal from " << senderNodeId << " with unfiltered power " << WToDbm (Integral (*receivedSignalPsd)) << " dBm");
205  // Integrate over our receive bandwidth (i.e., all that the receive
206  // spectral mask representing our filtering allows) to find the
207  // total energy apparent to the "demodulator".
208  uint16_t channelWidth = GetChannelWidth ();
210  SpectrumValue filteredSignal = (*filter) * (*receivedSignalPsd);
211  // Add receiver antenna gain
212  NS_LOG_DEBUG ("Signal power received (watts) before antenna gain: " << Integral (filteredSignal));
213  double rxPowerW = Integral (filteredSignal) * DbToRatio (GetRxGain ());
214  NS_LOG_DEBUG ("Signal power received after antenna gain: " << rxPowerW << " W (" << WToDbm (rxPowerW) << " dBm)");
215 
216  Ptr<WifiSpectrumSignalParameters> wifiRxParams = DynamicCast<WifiSpectrumSignalParameters> (rxParams);
217 
218  // Log the signal arrival to the trace source
219  m_signalCb (wifiRxParams ? true : false, senderNodeId, WToDbm (rxPowerW), rxDuration);
220 
221  // Do no further processing if signal is too weak
222  // Current implementation assumes constant RX power over the PPDU duration
223  if (WToDbm (rxPowerW) < GetRxSensitivity ())
224  {
225  NS_LOG_INFO ("Received signal too weak to process: " << WToDbm (rxPowerW) << " dBm");
226  return;
227  }
228  if (wifiRxParams == 0)
229  {
230  NS_LOG_INFO ("Received non Wi-Fi signal");
231  m_interference.AddForeignSignal (rxDuration, rxPowerW);
233  return;
234  }
235  if (wifiRxParams && m_disableWifiReception)
236  {
237  NS_LOG_INFO ("Received Wi-Fi signal but blocked from syncing");
238  m_interference.AddForeignSignal (rxDuration, rxPowerW);
240  return;
241  }
242 
243  NS_LOG_INFO ("Received Wi-Fi signal");
244  Ptr<WifiPpdu> ppdu = Copy (wifiRxParams->ppdu);
245  StartReceivePreamble (ppdu, rxPowerW);
246 }
247 
250 {
251  return m_antenna;
252 }
253 
254 void
256 {
257  NS_LOG_FUNCTION (this << a);
258  m_antenna = a;
259 }
260 
261 void
263 {
264  NS_LOG_FUNCTION (this << device);
265  m_wifiSpectrumPhyInterface = CreateObject<WifiSpectrumPhyInterface> ();
266  m_wifiSpectrumPhyInterface->SetSpectrumWifiPhy (this);
267  m_wifiSpectrumPhyInterface->SetDevice (device);
268 }
269 
271 SpectrumWifiPhy::GetTxPowerSpectralDensity (uint16_t centerFrequency, uint16_t channelWidth, double txPowerW, WifiModulationClass modulationClass) const
272 {
273  NS_LOG_FUNCTION (centerFrequency << channelWidth << txPowerW);
275  switch (modulationClass)
276  {
277  case WIFI_MOD_CLASS_OFDM:
279  v = WifiSpectrumValueHelper::CreateOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
280  break;
281  case WIFI_MOD_CLASS_DSSS:
283  NS_ABORT_MSG_IF (channelWidth != 22, "Invalid channel width for DSSS");
284  v = WifiSpectrumValueHelper::CreateDsssTxPowerSpectralDensity (centerFrequency, txPowerW, GetGuardBandwidth (channelWidth));
285  break;
286  case WIFI_MOD_CLASS_HT:
287  case WIFI_MOD_CLASS_VHT:
288  v = WifiSpectrumValueHelper::CreateHtOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
289  break;
290  case WIFI_MOD_CLASS_HE:
291  v = WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
292  break;
293  default:
294  NS_FATAL_ERROR ("modulation class unknown: " << modulationClass);
295  break;
296  }
297  return v;
298 }
299 
300 uint16_t
302 {
303  NS_LOG_FUNCTION (this << txVector);
304  uint16_t centerFrequencyForSupportedWidth = GetFrequency ();
305  uint16_t supportedWidth = GetChannelWidth ();
306  uint16_t currentWidth = txVector.GetChannelWidth ();
307  if (currentWidth != supportedWidth)
308  {
309  uint16_t startingFrequency = centerFrequencyForSupportedWidth - (supportedWidth / 2);
310  return startingFrequency + (currentWidth / 2); // primary channel is in the lower part (for the time being)
311  }
312  return centerFrequencyForSupportedWidth;
313 }
314 
315 void
317 {
318  NS_LOG_FUNCTION (this << ppdu);
319  WifiTxVector txVector = ppdu->GetTxVector ();
320  double txPowerDbm = GetTxPowerForTransmission (txVector) + GetTxGain ();
321  NS_LOG_DEBUG ("Start transmission: signal power before antenna gain=" << txPowerDbm << "dBm");
322  double txPowerWatts = DbmToW (txPowerDbm);
323  Ptr<SpectrumValue> txPowerSpectrum = GetTxPowerSpectralDensity (GetCenterFrequencyForChannelWidth (txVector), txVector.GetChannelWidth (), txPowerWatts, txVector.GetMode ().GetModulationClass ());
324  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
325  txParams->duration = ppdu->GetTxDuration ();
326  txParams->psd = txPowerSpectrum;
327  NS_ASSERT_MSG (m_wifiSpectrumPhyInterface, "SpectrumPhy() is not set; maybe forgot to call CreateWifiSpectrumPhyInterface?");
328  txParams->txPhy = m_wifiSpectrumPhyInterface->GetObject<SpectrumPhy> ();
329  txParams->txAntenna = m_antenna;
330  txParams->ppdu = ppdu;
331  NS_LOG_DEBUG ("Starting transmission with power " << WToDbm (txPowerWatts) << " dBm on channel " << +GetChannelNumber ());
332  NS_LOG_DEBUG ("Starting transmission with integrated spectrum power " << WToDbm (Integral (*txPowerSpectrum)) << " dBm; spectrum model Uid: " << txPowerSpectrum->GetSpectrumModel ()->GetUid ());
333  m_channel->StartTx (txParams);
334 }
335 
336 uint32_t
338 {
339  uint32_t bandBandwidth = 0;
340  switch (GetPhyStandard ())
341  {
348  // Use OFDM subcarrier width of 312.5 KHz as band granularity
349  bandBandwidth = 312500;
350  break;
352  if (GetChannelWidth () == 5)
353  {
354  // Use OFDM subcarrier width of 78.125 KHz as band granularity
355  bandBandwidth = 78125;
356  }
357  else
358  {
359  // Use OFDM subcarrier width of 156.25 KHz as band granularity
360  bandBandwidth = 156250;
361  }
362  break;
364  // Use OFDM subcarrier width of 78.125 KHz as band granularity
365  bandBandwidth = 78125;
366  break;
367  default:
368  NS_FATAL_ERROR ("Standard unknown: " << GetPhyStandard ());
369  break;
370  }
371  return bandBandwidth;
372 }
373 
374 uint16_t
375 SpectrumWifiPhy::GetGuardBandwidth (uint16_t currentChannelWidth) const
376 {
377  uint16_t guardBandwidth = 0;
378  if (currentChannelWidth == 22)
379  {
380  //handle case of DSSS transmission
381  guardBandwidth = 10;
382  }
383  else
384  {
385  //In order to properly model out of band transmissions for OFDM, the guard
386  //band has been configured so as to expand the modeled spectrum up to the
387  //outermost referenced point in "Transmit spectrum mask" sections' PSDs of
388  //each PHY specification of 802.11-2016 standard. It thus ultimately corresponds
389  //to the currently considered channel bandwidth (which can be different from
390  //supported channel width).
391  guardBandwidth = currentChannelWidth;
392  }
393  return guardBandwidth;
394 }
395 
396 } //namespace ns3
ERP-OFDM PHY (Clause 19, Section 19.5)
nanosecond
Definition: nstime.h:118
uint16_t GetGuardBandwidth(uint16_t currentChannelWidth) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
TracedCallback< bool, uint32_t, double, Time > m_signalCb
Signal callback.
void AddForeignSignal(Time duration, double rxPower)
Add a non-Wifi signal to interference helper.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Abstract base class for Spectrum-aware PHY layers.
Definition: spectrum-phy.h:45
ERP-OFDM PHY (19.5)
Definition: wifi-mode.h:56
AttributeValue implementation for Boolean.
Definition: boolean.h:36
double GetRxGain(void) const
Return the reception gain (dB).
Definition: wifi-phy.cc:732
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
double Integral(const SpectrumValue &arg)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
static Ptr< SpectrumValue > CreateHtOfdmTxPowerSpectralDensity(uint32_t centerFrequency, uint16_t channelWidth, double txPowerW, uint16_t guardBandwidth)
Create a transmit power spectral density corresponding to OFDM High Throughput (HT) (802...
void StartRx(Ptr< SpectrumSignalParameters > rxParams)
Input method for delivering a signal from the spectrum channel and low-level PHY interface to this Sp...
802.11 PHY layer model
Definition: wifi-phy.h:141
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:41
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:85
virtual void ConfigureStandardAndBand(WifiPhyStandard standard, WifiPhyBand band)
Configure the PHY-level parameters for different Wi-Fi standard.
Definition: wifi-phy.cc:1371
virtual void DoDispose(void)
Destructor implementation.
Definition: wifi-phy.cc:544
bool IsInitialized(void) const
Check if the object has been initialized.
Definition: object.cc:208
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
static TypeId GetTypeId(void)
Get the type ID.
bool m_disableWifiReception
forces this PHY to fail to sync on any signal
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
static Ptr< SpectrumValue > CreateRfFilter(uint32_t centerFrequency, uint16_t channelWidth, uint32_t bandBandwidth, uint16_t guardBandwidth)
Create a spectral density corresponding to the RF filter.
VHT PHY (Clause 22)
Definition: wifi-mode.h:62
TimeWithUnit As(const enum Unit unit) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:389
HR/DSSS PHY (Clause 18)
Definition: wifi-mode.h:50
static Ptr< SpectrumValue > CreateDsssTxPowerSpectralDensity(uint32_t centerFrequency, double txPowerW, uint16_t guardBandwidth)
Create a transmit power spectral density corresponding to DSSS.
void StartTx(Ptr< WifiPpdu > ppdu)
double GetTxPowerForTransmission(WifiTxVector txVector) const
Compute the transmit power (in dBm) for the next transmission.
Definition: wifi-phy.cc:4514
virtual void SetFrequency(uint16_t freq)
Definition: wifi-phy.cc:1437
void SetAntenna(const Ptr< AntennaModel > antenna)
virtual void SetChannelNumber(uint8_t id)
Set channel number.
SpectrumModelUid_t GetUid() const
Ptr< const SpectrumModel > GetRxSpectrumModel() const
void DoInitialize(void)
Initialize() implementation.
channel
Definition: third.py:92
double GetRxSensitivity(void) const
Return the receive sensitivity threshold (dBm).
Definition: wifi-phy.cc:646
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< SpectrumChannel > m_channel
SpectrumChannel that this SpectrumWifiPhy is connected to.
WifiMode GetMode(uint16_t staId=SU_STA_ID) const
If this TX vector is associated with an SU PPDU, return the selected payload transmission mode...
uint16_t GetChannelWidth(void) const
Definition: wifi-phy.cc:1514
Ptr< SpectrumValue > GetTxPowerSpectralDensity(uint16_t centerFrequency, uint16_t channelWidth, double txPowerW, WifiModulationClass modulationClass) const
WifiPhyStandard
Identifies the PHY specification that a Wifi device is configured to use.
HT PHY (Clause 20)
Definition: wifi-mode.h:60
OFDM PHY (Clause 17 - amendment for 10 MHz and 5 MHz channels)
void DoDispose(void)
Destructor implementation.
802.11 PHY layer modelThis PHY implements a spectrum-aware enhancement of the 802.11 SpectrumWifiPhy model.
Ptr< const SpectrumModel > m_rxSpectrumModel
receive spectrum model
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:480
static Ptr< SpectrumValue > CreateHeOfdmTxPowerSpectralDensity(uint32_t centerFrequency, uint16_t channelWidth, double txPowerW, uint16_t guardBandwidth)
Create a transmit power spectral density corresponding to OFDM High Efficiency (HE) (802...
virtual void SetFrequency(uint16_t freq)
WifiPhyStandard GetPhyStandard(void) const
Get the configured Wi-Fi standard.
Definition: wifi-phy.cc:1431
virtual void ConfigureStandardAndBand(WifiPhyStandard standard, WifiPhyBand band)
Configure the PHY-level parameters for different Wi-Fi standard.
This is intended to be the configuration used in this paper: Gavin Holland, Nitin Vaidya and Paramvir...
void StartReceivePreamble(Ptr< WifiPpdu > ppdu, double rxPowerW)
Start receiving the PHY preamble of a PPDU (i.e.
Definition: wifi-phy.cc:2843
static Ptr< SpectrumModel > GetSpectrumModel(uint32_t centerFrequency, uint16_t channelWidth, uint32_t bandBandwidth, uint16_t guardBandwidth)
Return a SpectrumModel instance corresponding to the center frequency and channel width...
OFDM PHY (Clause 17)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint16_t GetFrequency(void) const
Definition: wifi-phy.cc:1494
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
uint32_t GetBandBandwidth(void) const
Ptr< const SpectrumModel > GetSpectrumModel() const
virtual void SetChannelNumber(uint8_t id)
Set channel number.
Definition: wifi-phy.cc:1612
virtual void DoInitialize(void)
Initialize() implementation.
Definition: wifi-phy.cc:562
double WToDbm(double w)
Convert from Watts to dBm.
Definition: wifi-utils.cc:47
uint8_t GetChannelNumber(void) const
Return current channel number.
Definition: wifi-phy.cc:1665
double DbToRatio(double dB)
Convert from dB to ratio.
Definition: wifi-utils.cc:35
virtual void SetChannelWidth(uint16_t channelwidth)
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
WifiPhyBand
Identifies the PHY band.
Definition: wifi-phy-band.h:30
WifiModulationClass
This enumeration defines the modulation classes per (Table 9-4 "Modulation classes"; IEEE 802...
Definition: wifi-mode.h:38
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
Ptr< AntennaModel > GetRxAntenna(void) const
Get the antenna model used for reception.
InterferenceHelper m_interference
Pointer to InterferenceHelper.
Definition: wifi-phy.h:1756
virtual void SetChannelWidth(uint16_t channelWidth)
Definition: wifi-phy.cc:1500
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Ptr< AntennaModel > m_antenna
antenna model
OFDM PHY (Clause 17)
Definition: wifi-mode.h:58
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
uint16_t GetCenterFrequencyForChannelWidth(WifiTxVector txVector) const
Get the center frequency of the channel corresponding the current TxVector rather than that of the su...
Ptr< WifiSpectrumPhyInterface > m_wifiSpectrumPhyInterface
Spectrum PHY interface.
void ResetSpectrumModel(void)
Perform run-time spectrum model change.
double GetTxGain(void) const
Return the transmission gain (dB).
Definition: wifi-phy.cc:719
uint16_t GetChannelWidth(void) const
static Ptr< SpectrumValue > CreateOfdmTxPowerSpectralDensity(uint32_t centerFrequency, uint16_t channelWidth, double txPowerW, uint16_t guardBandwidth)
Create a transmit power spectral density corresponding to OFDM (802.11a/g).
Ptr< Channel > GetChannel(void) const
Return the Channel this WifiPhy is connected to.
void SetChannel(const Ptr< SpectrumChannel > channel)
Set the SpectrumChannel this SpectrumWifiPhy is to be connected to.
Set of values corresponding to a given SpectrumModel.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:536
void SwitchMaybeToCcaBusy(void)
Check if PHY state should move to CCA busy state based on current state of interference tracker...
Definition: wifi-phy.cc:4460
HE PHY (Clause 26)
Definition: wifi-mode.h:64
DSSS PHY (Clause 15)
Definition: wifi-mode.h:48
void CreateWifiSpectrumPhyInterface(Ptr< NetDevice > device)
Method to encapsulate the creation of the WifiSpectrumPhyInterface object (used to bind the WifiSpect...