A Discrete-Event Network Simulator
API
spectrum-wifi-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
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  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  * Sébastien Deronne <sebastien.deronne@gmail.com>
19  */
20 
21 #include "spectrum-wifi-helper.h"
22 
23 #include "ns3/error-rate-model.h"
24 #include "ns3/frame-capture-model.h"
25 #include "ns3/interference-helper.h"
26 #include "ns3/log.h"
27 #include "ns3/mobility-model.h"
28 #include "ns3/names.h"
29 #include "ns3/preamble-detection-model.h"
30 #include "ns3/spectrum-channel.h"
31 #include "ns3/spectrum-transmit-filter.h"
32 #include "ns3/spectrum-wifi-phy.h"
33 #include "ns3/wifi-bandwidth-filter.h"
34 #include "ns3/wifi-net-device.h"
35 #include "ns3/wifi-spectrum-value-helper.h"
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE("SpectrumWifiHelper");
41 
43  : WifiPhyHelper(nLinks)
44 {
45  NS_ABORT_IF(m_phys.size() != nLinks);
46  for (auto& phy : m_phys)
47  {
48  phy.SetTypeId("ns3::SpectrumWifiPhy");
49  }
50  SetInterferenceHelper("ns3::InterferenceHelper");
51  SetErrorRateModel("ns3::TableBasedErrorRateModel");
52 }
53 
54 void
56 {
58 }
59 
60 void
61 SpectrumWifiPhyHelper::SetChannel(const std::string& channelName)
62 {
63  AddChannel(channelName);
64 }
65 
66 void
68  const FrequencyRange& freqRange)
69 {
70  m_channels[freqRange] = channel;
72 }
73 
74 void
75 SpectrumWifiPhyHelper::AddChannel(const std::string& channelName, const FrequencyRange& freqRange)
76 {
77  Ptr<SpectrumChannel> channel = Names::Find<SpectrumChannel>(channelName);
78  AddChannel(channel, freqRange);
79 }
80 
81 void
83 {
84  Ptr<const SpectrumTransmitFilter> p = channel->GetSpectrumTransmitFilter();
85  bool found = false;
86  while (p && !found)
87  {
88  if (DynamicCast<const WifiBandwidthFilter>(p))
89  {
90  NS_LOG_DEBUG("Found existing WifiBandwidthFilter for channel " << channel);
91  found = true;
92  }
93  else
94  {
95  NS_LOG_DEBUG("Found different SpectrumTransmitFilter for channel " << channel);
96  p = p->GetNext();
97  }
98  }
99  if (!found)
100  {
101  Ptr<WifiBandwidthFilter> pWifi = CreateObject<WifiBandwidthFilter>();
102  channel->AddSpectrumTransmitFilter(pWifi);
103  NS_LOG_DEBUG("Adding WifiBandwidthFilter to channel " << channel);
104  }
105 }
106 
107 void
109 {
110  if (auto it = m_interfacesMap.find(linkId); it == m_interfacesMap.end())
111  {
112  m_interfacesMap.insert({linkId, {freqRange}});
113  }
114  else
115  {
116  it->second.emplace(freqRange);
117  }
118 }
119 
120 void
122 {
123  m_interfacesMap.clear();
124 }
125 
126 std::vector<Ptr<WifiPhy>>
128 {
129  std::vector<Ptr<WifiPhy>> ret;
130 
131  for (std::size_t i = 0; i < m_phys.size(); i++)
132  {
133  auto phy = m_phys.at(i).Create<SpectrumWifiPhy>();
134  auto interference = m_interferenceHelper.Create<InterferenceHelper>();
135  phy->SetInterferenceHelper(interference);
136  auto error = m_errorRateModel.at(i).Create<ErrorRateModel>();
137  phy->SetErrorRateModel(error);
138  if (m_frameCaptureModel.at(i).IsTypeIdSet())
139  {
140  auto frameCapture = m_frameCaptureModel.at(i).Create<FrameCaptureModel>();
141  phy->SetFrameCaptureModel(frameCapture);
142  }
143  if (m_preambleDetectionModel.at(i).IsTypeIdSet())
144  {
145  auto preambleDetection =
147  phy->SetPreambleDetectionModel(preambleDetection);
148  }
150  phy->SetChannelSwitchedCallback(
152  phy->SetDevice(device);
153  phy->SetMobility(node->GetObject<MobilityModel>());
154  ret.emplace_back(phy);
155  }
156 
157  return ret;
158 }
159 
160 void
162 {
163  if (m_interfacesMap.count(linkId) == 0)
164  {
165  // default setup: set all interfaces to this link
166  for (const auto& [freqRange, channel] : m_channels)
167  {
168  phy->AddChannel(channel, freqRange);
169  }
170  }
171  else
172  {
173  for (const auto& freqRange : m_interfacesMap.at(linkId))
174  {
175  phy->AddChannel(m_channels.at(freqRange), freqRange);
176  }
177  }
178 }
179 
180 void
182 {
183  for (const auto& otherPhy : phy->GetDevice()->GetPhys())
184  {
185  auto spectrumPhy = DynamicCast<SpectrumWifiPhy>(otherPhy);
186  NS_ASSERT(spectrumPhy);
187  if (spectrumPhy == phy)
188  {
189  // this is the PHY that has switched
190  continue;
191  }
192  if (spectrumPhy->GetCurrentFrequencyRange() == phy->GetCurrentFrequencyRange())
193  {
194  // this is the active interface
195  continue;
196  }
197  if (const auto& interfaces = spectrumPhy->GetSpectrumPhyInterfaces();
198  interfaces.count(phy->GetCurrentFrequencyRange()) == 0)
199  {
200  // no interface attached to that channel
201  continue;
202  }
203  spectrumPhy->ConfigureInterface(phy->GetFrequency(), phy->GetChannelWidth());
204  }
205 }
206 
207 } // namespace ns3
the interface for Wifi's error models
the interface for Wifi's frame capture models
handles interference calculations
Keep track of the current position and velocity of an object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
the interface for Wifi's preamble detection models
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
std::vector< Ptr< WifiPhy > > Create(Ptr< Node > node, Ptr< WifiNetDevice > device) const override
void ResetPhyToFreqRangeMapping()
Reset mapping of the spectrum PHY interfaces added to the PHY instances.
SpectrumWifiPhyHelper(uint8_t nLinks=1)
Create a PHY helper.
std::map< uint8_t, std::set< FrequencyRange > > m_interfacesMap
map of the spectrum PHY interfaces to be added to the PHY instance corresponding to a given link ID
void AddChannel(const Ptr< SpectrumChannel > channel, const FrequencyRange &freqRange=WHOLE_WIFI_SPECTRUM)
void InstallPhyInterfaces(uint8_t linkId, Ptr< SpectrumWifiPhy > phy) const
Install PHY interfaces to the PHY instance of a given link based on the currently configured mapping ...
void SetChannel(const Ptr< SpectrumChannel > channel)
static void SpectrumChannelSwitched(Ptr< SpectrumWifiPhy > phy)
Function that is notified when a spectrum channel switched.
void AddPhyToFreqRangeMapping(uint8_t linkId, const FrequencyRange &freqRange)
Add a given spectrum PHY interface to the PHY instance corresponding of a given link.
void AddWifiBandwidthFilter(Ptr< SpectrumChannel > channel)
std::map< FrequencyRange, Ptr< SpectrumChannel > > m_channels
the spectrum channels
802.11 PHY layer model
create PHY objects
Definition: wifi-helper.h:49
void SetErrorRateModel(std::string type, Args &&... args)
Helper function used to set the error rate model.
Definition: wifi-helper.h:550
void SetInterferenceHelper(std::string type, Args &&... args)
Helper function used to set the interference helper.
Definition: wifi-helper.h:542
std::vector< ObjectFactory > m_frameCaptureModel
frame capture model
Definition: wifi-helper.h:241
std::vector< ObjectFactory > m_preambleDetectionModel
preamble detection model
Definition: wifi-helper.h:242
std::vector< ObjectFactory > m_phys
PHY objects.
Definition: wifi-helper.h:238
std::vector< ObjectFactory > m_errorRateModel
error rate model
Definition: wifi-helper.h:240
ObjectFactory m_interferenceHelper
interference helper
Definition: wifi-helper.h:239
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:76
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
interfaces
Definition: first.py:50
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
channel
Definition: third.py:88
phy
Definition: third.py:89
Struct defining a frequency range between minFrequency (MHz) and maxFrequency (MHz).