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  m_operationalChannelList.push_back (channelNumber);
194 }
195 
196 std::vector<uint8_t>
198 {
199  std::vector<uint8_t> channelList;
200  channelList.push_back (GetChannelNumber ()); // first channel of list
201  for (std::vector<uint8_t>::size_type i = 0; i != m_operationalChannelList.size (); i++)
202  {
204  {
205  channelList.push_back (m_operationalChannelList[i]);
206  }
207  }
208  return channelList;
209 }
210 
211 void
213 {
214  m_operationalChannelList.clear ();
215 }
216 
217 void
219 {
220  NS_LOG_FUNCTION (this << rxParams);
221  Time rxDuration = rxParams->duration;
222  Ptr<SpectrumValue> receivedSignalPsd = rxParams->psd;
223  NS_LOG_DEBUG ("Received signal with PSD " << *receivedSignalPsd << " and duration " << rxDuration.As (Time::NS));
224  uint32_t senderNodeId = 0;
225  if (rxParams->txPhy)
226  {
227  senderNodeId = rxParams->txPhy->GetDevice ()->GetNode ()->GetId ();
228  }
229  NS_LOG_DEBUG ("Received signal from " << senderNodeId << " with unfiltered power " << WToDbm (Integral (*receivedSignalPsd)) << " dBm");
230  // Integrate over our receive bandwidth (i.e., all that the receive
231  // spectral mask representing our filtering allows) to find the
232  // total energy apparent to the "demodulator".
233  uint16_t channelWidth = GetChannelWidth ();
235  SpectrumValue filteredSignal = (*filter) * (*receivedSignalPsd);
236  // Add receiver antenna gain
237  NS_LOG_DEBUG ("Signal power received (watts) before antenna gain: " << Integral (filteredSignal));
238  double rxPowerW = Integral (filteredSignal) * DbToRatio (GetRxGain ());
239  NS_LOG_DEBUG ("Signal power received after antenna gain: " << rxPowerW << " W (" << WToDbm (rxPowerW) << " dBm)");
240 
241  Ptr<WifiSpectrumSignalParameters> wifiRxParams = DynamicCast<WifiSpectrumSignalParameters> (rxParams);
242 
243  // Log the signal arrival to the trace source
244  m_signalCb (wifiRxParams ? true : false, senderNodeId, WToDbm (rxPowerW), rxDuration);
245  if (wifiRxParams == 0)
246  {
247  NS_LOG_INFO ("Received non Wi-Fi signal");
248  m_interference.AddForeignSignal (rxDuration, rxPowerW);
250  return;
251  }
252  if (wifiRxParams && m_disableWifiReception)
253  {
254  NS_LOG_INFO ("Received Wi-Fi signal but blocked from syncing");
255  m_interference.AddForeignSignal (rxDuration, rxPowerW);
257  return;
258  }
259 
260  NS_LOG_INFO ("Received Wi-Fi signal");
261  Ptr<Packet> packet = wifiRxParams->packet->Copy ();
262  StartReceivePreambleAndHeader (packet, rxPowerW, rxDuration);
263 }
264 
267 {
268  return m_antenna;
269 }
270 
271 void
273 {
274  NS_LOG_FUNCTION (this << a);
275  m_antenna = a;
276 }
277 
278 void
280 {
281  NS_LOG_FUNCTION (this << device);
282  m_wifiSpectrumPhyInterface = CreateObject<WifiSpectrumPhyInterface> ();
283  m_wifiSpectrumPhyInterface->SetSpectrumWifiPhy (this);
284  m_wifiSpectrumPhyInterface->SetDevice (device);
285 }
286 
288 SpectrumWifiPhy::GetTxPowerSpectralDensity (uint16_t centerFrequency, uint16_t channelWidth, double txPowerW, WifiModulationClass modulationClass) const
289 {
290  NS_LOG_FUNCTION (centerFrequency << channelWidth << txPowerW);
292  switch (modulationClass)
293  {
294  case WIFI_MOD_CLASS_OFDM:
296  v = WifiSpectrumValueHelper::CreateOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
297  break;
298  case WIFI_MOD_CLASS_DSSS:
300  NS_ABORT_MSG_IF (channelWidth != 22, "Invalid channel width for DSSS");
301  v = WifiSpectrumValueHelper::CreateDsssTxPowerSpectralDensity (centerFrequency, txPowerW, GetGuardBandwidth (channelWidth));
302  break;
303  case WIFI_MOD_CLASS_HT:
304  case WIFI_MOD_CLASS_VHT:
305  v = WifiSpectrumValueHelper::CreateHtOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
306  break;
307  case WIFI_MOD_CLASS_HE:
308  v = WifiSpectrumValueHelper::CreateHeOfdmTxPowerSpectralDensity (centerFrequency, channelWidth, txPowerW, GetGuardBandwidth (channelWidth));
309  break;
310  default:
311  NS_FATAL_ERROR ("modulation class unknown: " << modulationClass);
312  break;
313  }
314  return v;
315 }
316 
317 uint16_t
319 {
320  NS_LOG_FUNCTION (this << txVector);
321  uint16_t centerFrequencyForSupportedWidth = GetFrequency ();
322  uint16_t supportedWidth = GetChannelWidth ();
323  uint16_t currentWidth = txVector.GetChannelWidth ();
324  if (currentWidth != supportedWidth)
325  {
326  uint16_t startingFrequency = centerFrequencyForSupportedWidth - (supportedWidth / 2);
327  return startingFrequency + (currentWidth / 2); // primary channel is in the lower part (for the time being)
328  }
329  return centerFrequencyForSupportedWidth;
330 }
331 
332 void
334 {
335  NS_LOG_DEBUG ("Start transmission: signal power before antenna gain=" << GetPowerDbm (txVector.GetTxPowerLevel ()) << "dBm");
336  double txPowerWatts = DbmToW (GetPowerDbm (txVector.GetTxPowerLevel ()) + GetTxGain ());
337  Ptr<SpectrumValue> txPowerSpectrum = GetTxPowerSpectralDensity (GetCenterFrequencyForChannelWidth (txVector), txVector.GetChannelWidth (), txPowerWatts, txVector.GetMode ().GetModulationClass ());
338  Ptr<WifiSpectrumSignalParameters> txParams = Create<WifiSpectrumSignalParameters> ();
339  txParams->duration = txDuration;
340  txParams->psd = txPowerSpectrum;
341  NS_ASSERT_MSG (m_wifiSpectrumPhyInterface, "SpectrumPhy() is not set; maybe forgot to call CreateWifiSpectrumPhyInterface?");
342  txParams->txPhy = m_wifiSpectrumPhyInterface->GetObject<SpectrumPhy> ();
343  txParams->txAntenna = m_antenna;
344  txParams->packet = packet;
345  NS_LOG_DEBUG ("Starting transmission with power " << WToDbm (txPowerWatts) << " dBm on channel " << +GetChannelNumber ());
346  NS_LOG_DEBUG ("Starting transmission with integrated spectrum power " << WToDbm (Integral (*txPowerSpectrum)) << " dBm; spectrum model Uid: " << txPowerSpectrum->GetSpectrumModel ()->GetUid ());
347  m_channel->StartTx (txParams);
348 }
349 
350 double
352 {
353  double bandBandwidth = 0;
354  switch (GetStandard ())
355  {
363  // Use OFDM subcarrier width of 312.5 KHz as band granularity
364  bandBandwidth = 312500;
365  break;
367  // Use OFDM subcarrier width of 156.25 KHz as band granularity
368  bandBandwidth = 156250;
369  break;
371  // Use OFDM subcarrier width of 78.125 KHz as band granularity
372  bandBandwidth = 78125;
373  break;
376  // Use OFDM subcarrier width of 78.125 KHz as band granularity
377  bandBandwidth = 78125;
378  break;
379  default:
380  NS_FATAL_ERROR ("Standard unknown: " << GetStandard ());
381  break;
382  }
383  return bandBandwidth;
384 }
385 
386 uint16_t
387 SpectrumWifiPhy::GetGuardBandwidth (uint16_t currentChannelWidth) const
388 {
389  uint16_t guardBandwidth = 0;
390  if (currentChannelWidth == 22)
391  {
392  //handle case of use of legacy DSSS transmission
393  guardBandwidth = 10;
394  }
395  else
396  {
397  //In order to properly model out of band transmissions for OFDM, the guard
398  //band has been configured so as to expand the modeled spectrum up to the
399  //outermost referenced point in "Transmit spectrum mask" sections' PSDs of
400  //each PHY specification of 802.11-2016 standard. It thus ultimately corresponds
401  //to the currently considered channel bandwidth (which can be different from
402  //supported channel width).
403  guardBandwidth = currentChannelWidth;
404  }
405  return guardBandwidth;
406 }
407 
408 } //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:565
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)
std::vector< uint8_t > GetOperationalChannelList(void) const
Return a list of channels to which it may be possible to roam By default, this method will return the...
#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:1180
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:64
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:38
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:389
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:202
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...
void StartReceivePreambleAndHeader(Ptr< Packet > packet, double rxPowerW, Time rxDuration)
Starting receiving the plcp of a packet (i.e.
Definition: wifi-phy.cc:2358
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:278
#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.
virtual void SetFrequency(uint16_t freq)
Definition: wifi-phy.cc:1186
void SetAntenna(const Ptr< AntennaModel > antenna)
virtual void SetChannelNumber(uint8_t id)
Set channel number.
Ptr< const SpectrumModel > GetRxSpectrumModel() const
void DoInitialize(void)
Initialize() implementation.
channel
Definition: third.py:85
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:1263
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:500
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)
double GetPowerDbm(uint8_t power) const
Get the power of the given power level in dBm.
Definition: wifi-phy.cc:700
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:1243
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual void SetChannelWidth(uint16_t channelwidth)
Definition: wifi-phy.cc:1249
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
static Ptr< SpectrumValue > CreateRfFilter(uint32_t centerFrequency, uint16_t channelWidth, double bandBandwidth, uint16_t guardBandwidth)
Create a spectral density corresponding to the RF filter.
virtual void SetChannelNumber(uint8_t id)
Set channel number.
Definition: wifi-phy.cc:1361
virtual void DoInitialize(void)
Initialize() implementation.
Definition: wifi-phy.cc:401
double WToDbm(double w)
Convert from Watts to dBm.
Definition: wifi-utils.cc:44
uint8_t GetChannelNumber(void) const
Return current channel number.
Definition: wifi-phy.cc:1415
double DbToRatio(double dB)
Convert from dB to ratio.
Definition: wifi-utils.cc:32
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:1498
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Ptr< AntennaModel > m_antenna
antenna model
OFDM PHY (Clause 17)
Definition: wifi-mode.h:56
std::vector< uint8_t > m_operationalChannelList
List of possible channels.
uint16_t GetCenterFrequencyForChannelWidth(WifiTxVector txVector) const
Get the center frequency of the channel corresponding the current TxVector rather than that of the su...
void ClearOperationalChannelList(void)
Clear the list of operational channels.
Ptr< WifiSpectrumPhyInterface > m_wifiSpectrumPhyInterface
Spectrum phy interface.
void ResetSpectrumModel(void)
Perform run-time spectrum model change.
uint8_t GetTxPowerLevel(void) const
double GetTxGain(void) const
Return the transmission gain (dB).
Definition: wifi-phy.cc:552
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).
void AddOperationalChannel(uint8_t channelNumber)
Add a channel number to the list of operational channels.
virtual void ConfigureStandard(WifiPhyStandard standard)
Configure the PHY-level parameters for different Wi-Fi standard.
Definition: wifi-phy.cc:1119
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:3583
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...