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 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("SpectrumWifiPhy");
39 
40 NS_OBJECT_ENSURE_REGISTERED (SpectrumWifiPhy);
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::SpectrumWifiPhy")
46  .SetParent<WifiPhy> ()
47  .SetGroupName ("Wifi")
48  .AddConstructor<SpectrumWifiPhy> ()
49  .AddAttribute ("DisableWifiReception", "Prevent Wi-Fi frame sync from ever happening",
50  BooleanValue (false),
53  .AddTraceSource ("SignalArrival",
54  "Signal arrival",
56  "ns3::SpectrumWifiPhy::SignalArrivalCallback")
57  ;
58  return tid;
59 }
60 
62 {
63  NS_LOG_FUNCTION (this);
64 }
65 
67 {
68  NS_LOG_FUNCTION (this);
69 }
70 
71 void
73 {
74  NS_LOG_FUNCTION (this);
75  m_channel = 0;
78 }
79 
80 void
82 {
83  NS_LOG_FUNCTION (this);
85  // This connection is deferred until frequency and channel width are set
87  {
89  }
90  else
91  {
92  NS_FATAL_ERROR ("SpectrumWifiPhy misses channel and WifiSpectrumPhyInterface objects at initialization time");
93  }
94 }
95 
98 {
99  NS_LOG_FUNCTION (this);
100  if (m_rxSpectrumModel)
101  {
102  return m_rxSpectrumModel;
103  }
104  else
105  {
106  if (GetFrequency () == 0)
107  {
108  NS_LOG_DEBUG ("Frequency is not set; returning 0");
109  return 0;
110  }
111  else
112  {
113  uint16_t channelWidth = GetChannelWidth ();
114  NS_LOG_DEBUG ("Creating spectrum model from frequency/width pair of (" << GetFrequency () << ", " << channelWidth << ")");
116  }
117  }
118  return m_rxSpectrumModel;
119 }
120 
123 {
124  return m_channel;
125 }
126 
127 void
129 {
130  m_channel = channel;
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION (this);
137  NS_ASSERT_MSG (IsInitialized (), "Executing method before run-time");
138  uint16_t channelWidth = GetChannelWidth ();
139  NS_LOG_DEBUG ("Run-time change of spectrum model from frequency/width pair of (" << GetFrequency () << ", " << channelWidth << ")");
140  // Replace existing spectrum model with new one, and must call AddRx ()
141  // on the SpectrumChannel to provide this new spectrum model to it
144 }
145 
146 void
148 {
149  NS_LOG_FUNCTION (this << +nch);
151  if (IsInitialized ())
152  {
154  }
155 }
156 
157 void
159 {
160  NS_LOG_FUNCTION (this << freq);
161  WifiPhy::SetFrequency (freq);
162  if (IsInitialized ())
163  {
165  }
166 }
167 
168 void
169 SpectrumWifiPhy::SetChannelWidth (uint16_t channelwidth)
170 {
171  NS_LOG_FUNCTION (this << channelwidth);
172  WifiPhy::SetChannelWidth (channelwidth);
173  if (IsInitialized ())
174  {
176  }
177 }
178 
179 void
181 {
182  NS_LOG_FUNCTION (this << standard);
183  WifiPhy::ConfigureStandard (standard);
184  if (IsInitialized ())
185  {
187  }
188 }
189 
190 void
192 {
193  NS_LOG_FUNCTION (this << rxParams);
194  Time rxDuration = rxParams->duration;
195  Ptr<SpectrumValue> receivedSignalPsd = rxParams->psd;
196  NS_LOG_DEBUG ("Received signal with PSD " << *receivedSignalPsd << " and duration " << rxDuration.As (Time::NS));
197  uint32_t senderNodeId = 0;
198  if (rxParams->txPhy)
199  {
200  senderNodeId = rxParams->txPhy->GetDevice ()->GetNode ()->GetId ();
201  }
202  NS_LOG_DEBUG ("Received signal from " << senderNodeId << " with unfiltered power " << WToDbm (Integral (*receivedSignalPsd)) << " dBm");
203  // Integrate over our receive bandwidth (i.e., all that the receive
204  // spectral mask representing our filtering allows) to find the
205  // total energy apparent to the "demodulator".
206  uint16_t channelWidth = GetChannelWidth ();
208  SpectrumValue filteredSignal = (*filter) * (*receivedSignalPsd);
209  // Add receiver antenna gain
210  NS_LOG_DEBUG ("Signal power received (watts) before antenna gain: " << Integral (filteredSignal));
211  double rxPowerW = Integral (filteredSignal) * DbToRatio (GetRxGain ());
212  NS_LOG_DEBUG ("Signal power received after antenna gain: " << rxPowerW << " W (" << WToDbm (rxPowerW) << " dBm)");
213 
214  Ptr<WifiSpectrumSignalParameters> wifiRxParams = DynamicCast<WifiSpectrumSignalParameters> (rxParams);
215 
216  // Log the signal arrival to the trace source
217  m_signalCb (wifiRxParams ? true : false, senderNodeId, WToDbm (rxPowerW), rxDuration);
218 
219  // Do no further processing if signal is too weak
220  // Current implementation assumes constant rx power over the packet duration
221  if (WToDbm (rxPowerW) < GetRxSensitivity ())
222  {
223  NS_LOG_INFO ("Received signal too weak to process: " << WToDbm (rxPowerW) << " dBm");
224  return;
225  }
226  if (wifiRxParams == 0)
227  {
228  NS_LOG_INFO ("Received non Wi-Fi signal");
229  m_interference.AddForeignSignal (rxDuration, rxPowerW);
231  return;
232  }
233  if (wifiRxParams && m_disableWifiReception)
234  {
235  NS_LOG_INFO ("Received Wi-Fi signal but blocked from syncing");
236  m_interference.AddForeignSignal (rxDuration, rxPowerW);
238  return;
239  }
240 
241  NS_LOG_INFO ("Received Wi-Fi signal");
242  Ptr<Packet> packet = wifiRxParams->packet->Copy ();
243  StartReceivePreamble (packet, rxPowerW, rxDuration);
244 }
245 
248 {
249  return m_antenna;
250 }
251 
252 void
254 {
255  NS_LOG_FUNCTION (this << a);
256  m_antenna = a;
257 }
258 
259 void
261 {
262  NS_LOG_FUNCTION (this << device);
263  m_wifiSpectrumPhyInterface = CreateObject<WifiSpectrumPhyInterface> ();
264  m_wifiSpectrumPhyInterface->SetSpectrumWifiPhy (this);
265  m_wifiSpectrumPhyInterface->SetDevice (device);
266 }
267 
269 SpectrumWifiPhy::GetTxPowerSpectralDensity (uint16_t centerFrequency, uint16_t channelWidth, double txPowerW, WifiModulationClass modulationClass) const
270 {
271  NS_LOG_FUNCTION (centerFrequency << channelWidth << txPowerW);
273  switch (modulationClass)
274  {
275  case WIFI_MOD_CLASS_OFDM:
277  v = WifiSpectrumValueHelper::CreateOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
278  break;
279  case WIFI_MOD_CLASS_DSSS:
281  NS_ABORT_MSG_IF (channelWidth != 22, "Invalid channel width for DSSS");
282  v = WifiSpectrumValueHelper::CreateDsssTxPowerSpectralDensity (centerFrequency, txPowerW, GetGuardBandwidth (channelWidth));
283  break;
284  case WIFI_MOD_CLASS_HT:
285  case WIFI_MOD_CLASS_VHT:
286  v = WifiSpectrumValueHelper::CreateHtOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
287  break;
288  case WIFI_MOD_CLASS_HE:
289  v = WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
290  break;
291  default:
292  NS_FATAL_ERROR ("modulation class unknown: " << modulationClass);
293  break;
294  }
295  return v;
296 }
297 
298 uint16_t
300 {
301  NS_LOG_FUNCTION (this << txVector);
302  uint16_t centerFrequencyForSupportedWidth = GetFrequency ();
303  uint16_t supportedWidth = GetChannelWidth ();
304  uint16_t currentWidth = txVector.GetChannelWidth ();
305  if (currentWidth != supportedWidth)
306  {
307  uint16_t startingFrequency = centerFrequencyForSupportedWidth - (supportedWidth / 2);
308  return startingFrequency + (currentWidth / 2); // primary channel is in the lower part (for the time being)
309  }
310  return centerFrequencyForSupportedWidth;
311 }
312 
313 void
315 {
316  double txPowerDbm = GetTxPowerForTransmission (txVector) + GetTxGain ();
317  NS_LOG_DEBUG ("Start transmission: signal power before antenna gain=" << txPowerDbm << "dBm");
318  double txPowerWatts = DbmToW (txPowerDbm);
319  Ptr<SpectrumValue> txPowerSpectrum = GetTxPowerSpectralDensity (GetCenterFrequencyForChannelWidth (txVector), txVector.GetChannelWidth (), txPowerWatts, txVector.GetMode ().GetModulationClass ());
320  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
321  txParams->duration = txDuration;
322  txParams->psd = txPowerSpectrum;
323  NS_ASSERT_MSG (m_wifiSpectrumPhyInterface, "SpectrumPhy() is not set; maybe forgot to call CreateWifiSpectrumPhyInterface?");
324  txParams->txPhy = m_wifiSpectrumPhyInterface->GetObject<SpectrumPhy> ();
325  txParams->txAntenna = m_antenna;
326  txParams->packet = packet;
327  NS_LOG_DEBUG ("Starting transmission with power " << WToDbm (txPowerWatts) << " dBm on channel " << +GetChannelNumber ());
328  NS_LOG_DEBUG ("Starting transmission with integrated spectrum power " << WToDbm (Integral (*txPowerSpectrum)) << " dBm; spectrum model Uid: " << txPowerSpectrum->GetSpectrumModel ()->GetUid ());
329  m_channel->StartTx (txParams);
330 }
331 
332 double
334 {
335  double bandBandwidth = 0;
336  switch (GetStandard ())
337  {
345  // Use OFDM subcarrier width of 312.5 KHz as band granularity
346  bandBandwidth = 312500;
347  break;
349  // Use OFDM subcarrier width of 156.25 KHz as band granularity
350  bandBandwidth = 156250;
351  break;
353  // Use OFDM subcarrier width of 78.125 KHz as band granularity
354  bandBandwidth = 78125;
355  break;
358  // Use OFDM subcarrier width of 78.125 KHz as band granularity
359  bandBandwidth = 78125;
360  break;
361  default:
362  NS_FATAL_ERROR ("Standard unknown: " << GetStandard ());
363  break;
364  }
365  return bandBandwidth;
366 }
367 
368 uint16_t
369 SpectrumWifiPhy::GetGuardBandwidth (uint16_t currentChannelWidth) const
370 {
371  uint16_t guardBandwidth = 0;
372  if (currentChannelWidth == 22)
373  {
374  //handle case of use of legacy DSSS transmission
375  guardBandwidth = 10;
376  }
377  else
378  {
379  //In order to properly model out of band transmissions for OFDM, the guard
380  //band has been configured so as to expand the modeled spectrum up to the
381  //outermost referenced point in "Transmit spectrum mask" sections' PSDs of
382  //each PHY specification of 802.11-2016 standard. It thus ultimately corresponds
383  //to the currently considered channel bandwidth (which can be different from
384  //supported channel width).
385  guardBandwidth = currentChannelWidth;
386  }
387  return guardBandwidth;
388 }
389 
390 } //namespace ns3
ERP-OFDM PHY (Clause 19, Section 19.5)
nanosecond
Definition: nstime.h:117
uint16_t GetGuardBandwidth(uint16_t currentChannelWidth) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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 "...
OFDM PHY for the 5 GHz band (Clause 17 with 5 MHz channel bandwidth)
Abstract base class for Spectrum-aware PHY layers.
Definition: spectrum-phy.h:45
ERP-OFDM PHY (19.5)
Definition: wifi-mode.h:54
AttributeValue implementation for Boolean.
Definition: boolean.h:36
double GetRxGain(void) const
Return the reception gain (dB).
Definition: wifi-phy.cc:606
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...
double Integral(const SpectrumValue &arg)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
WifiPhyStandard GetStandard(void) const
Get the configured Wi-Fi standard.
Definition: wifi-phy.cc:1274
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
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:89
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:84
virtual void DoDispose(void)
Destructor implementation.
Definition: wifi-phy.cc:414
void StartTx(Ptr< Packet > packet, WifiTxVector txVector, Time txDuration)
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:204
HE PHY for the 2.4 GHz band (clause 26)
static Ptr< SpectrumModel > GetSpectrumModel(uint32_t centerFrequency, uint16_t channelWidth, double bandBandwidth, uint16_t guardBandwidth)
Return a SpectrumModel instance corresponding to the center frequency and channel width...
static TypeId GetTypeId(void)
Get the type ID.
OFDM PHY for the 5 GHz band (Clause 17 with 10 MHz channel bandwidth)
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:280
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
HT PHY for the 2.4 GHz band (clause 20)
VHT PHY (Clause 22)
Definition: wifi-mode.h:60
TimeWithUnit As(const enum Unit unit) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:388
HR/DSSS PHY (Clause 18)
Definition: wifi-mode.h:48
static Ptr< SpectrumValue > CreateDsssTxPowerSpectralDensity(uint32_t centerFrequency, double txPowerW, uint16_t guardBandwidth)
Create a transmit power spectral density corresponding to DSSS.
double GetTxPowerForTransmission(WifiTxVector txVector) const
Compute the transmit power (in dBm) for the next transmission.
Definition: wifi-phy.cc:4202
virtual void SetFrequency(uint16_t freq)
Definition: wifi-phy.cc:1280
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:85
double GetRxSensitivity(void) const
Return the receive sensitivity threshold (dBm).
Definition: wifi-phy.cc:520
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
double GetBandBandwidth(void) const
Ptr< SpectrumChannel > m_channel
SpectrumChannel that this SpectrumWifiPhy is connected to.
uint16_t GetChannelWidth(void) const
Definition: wifi-phy.cc:1357
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.
HE PHY for the 5 GHz band (clause 26)
virtual void ConfigureStandard(WifiPhyStandard standard)
Configure the PHY-level parameters for different Wi-Fi standard.
WifiMode GetMode(void) const
HT PHY (Clause 20)
Definition: wifi-mode.h:58
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:494
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)
This is intended to be the configuration used in this paper: Gavin Holland, Nitin Vaidya and Paramvir...
OFDM PHY for the 5 GHz band (Clause 17)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint16_t GetFrequency(void) const
Definition: wifi-phy.cc:1337
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual void SetChannelWidth(uint16_t channelwidth)
Definition: wifi-phy.cc:1343
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
void StartReceivePreamble(Ptr< Packet > packet, double rxPowerW, Time rxDuration)
Start receiving the PHY preamble of a packet (i.e.
Definition: wifi-phy.cc:2744
static Ptr< SpectrumValue > CreateRfFilter(uint32_t centerFrequency, uint16_t channelWidth, double bandBandwidth, uint16_t guardBandwidth)
Create a spectral density corresponding to the RF filter.
Ptr< const SpectrumModel > GetSpectrumModel() const
virtual void SetChannelNumber(uint8_t id)
Set channel number.
Definition: wifi-phy.cc:1455
virtual void DoInitialize(void)
Initialize() implementation.
Definition: wifi-phy.cc:430
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:1509
double DbToRatio(double dB)
Convert from dB to ratio.
Definition: wifi-utils.cc:35
virtual void SetChannelWidth(uint16_t channelwidth)
WifiModulationClass
This enumeration defines the modulation classes per (Table 9-4 "Modulation classes"; IEEE 802...
Definition: wifi-mode.h:36
#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:1610
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
Ptr< AntennaModel > m_antenna
antenna model
OFDM PHY (Clause 17)
Definition: wifi-mode.h:56
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:593
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).
virtual void ConfigureStandard(WifiPhyStandard standard)
Configure the PHY-level parameters for different Wi-Fi standard.
Definition: wifi-phy.cc:1217
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:915
void SwitchMaybeToCcaBusy(void)
Check if Phy state should move to CCA busy state based on current state of interference tracker...
Definition: wifi-phy.cc:4150
HE PHY (Clause 26)
Definition: wifi-mode.h:62
DSSS PHY (Clause 15)
Definition: wifi-mode.h:46
void CreateWifiSpectrumPhyInterface(Ptr< NetDevice > device)
Method to encapsulate the creation of the WifiSpectrumPhyInterface object (used to bind the WifiSpect...