A Discrete-Event Network Simulator
API
single-model-spectrum-channel.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 CTTC
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: Nicola Baldo <nbaldo@cttc.es>
18  */
19 
21 
22 #include <ns3/angles.h>
23 #include <ns3/antenna-model.h>
24 #include <ns3/double.h>
25 #include <ns3/log.h>
26 #include <ns3/mobility-model.h>
27 #include <ns3/net-device.h>
28 #include <ns3/node.h>
29 #include <ns3/object.h>
30 #include <ns3/packet-burst.h>
31 #include <ns3/packet.h>
32 #include <ns3/propagation-delay-model.h>
33 #include <ns3/propagation-loss-model.h>
34 #include <ns3/simulator.h>
35 #include <ns3/spectrum-phy.h>
36 #include <ns3/spectrum-propagation-loss-model.h>
37 
38 #include <algorithm>
39 
40 namespace ns3
41 {
42 
43 NS_LOG_COMPONENT_DEFINE("SingleModelSpectrumChannel");
44 
45 NS_OBJECT_ENSURE_REGISTERED(SingleModelSpectrumChannel);
46 
48 {
49  NS_LOG_FUNCTION(this);
50 }
51 
52 void
54 {
55  NS_LOG_FUNCTION(this);
56  m_phyList.clear();
57  m_spectrumModel = nullptr;
59 }
60 
61 TypeId
63 {
65  static TypeId tid = TypeId("ns3::SingleModelSpectrumChannel")
67  .SetGroupName("Spectrum")
68  .AddConstructor<SingleModelSpectrumChannel>();
69  return tid;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION(this << phy);
76  auto it = std::find(begin(m_phyList), end(m_phyList), phy);
77  if (it != std::end(m_phyList))
78  {
79  m_phyList.erase(it);
80  }
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION(this << phy);
87  if (std::find(m_phyList.cbegin(), m_phyList.cend(), phy) == m_phyList.cend())
88  {
89  m_phyList.push_back(phy);
90  }
91 }
92 
93 void
95 {
96  NS_LOG_FUNCTION(this << txParams->psd << txParams->duration << txParams->txPhy);
97  NS_ASSERT_MSG(txParams->psd, "NULL txPsd");
98  NS_ASSERT_MSG(txParams->txPhy, "NULL txPhy");
99 
100  Ptr<SpectrumSignalParameters> txParamsTrace =
101  txParams->Copy(); // copy it since traced value cannot be const (because of potential
102  // underlying DynamicCasts)
103  m_txSigParamsTrace(txParamsTrace);
104 
105  // just a sanity check routine. We might want to remove it to save some computational load --
106  // one "if" statement ;-)
107  if (!m_spectrumModel)
108  {
109  // first pak, record SpectrumModel
110  m_spectrumModel = txParams->psd->GetSpectrumModel();
111  }
112  else
113  {
114  // all attached SpectrumPhy instances must use the same SpectrumModel
115  NS_ASSERT(*(txParams->psd->GetSpectrumModel()) == *m_spectrumModel);
116  }
117 
118  Ptr<MobilityModel> senderMobility = txParams->txPhy->GetMobility();
119 
120  for (PhyList::const_iterator rxPhyIterator = m_phyList.begin();
121  rxPhyIterator != m_phyList.end();
122  ++rxPhyIterator)
123  {
124  Ptr<NetDevice> rxNetDevice = (*rxPhyIterator)->GetDevice();
125  Ptr<NetDevice> txNetDevice = txParams->txPhy->GetDevice();
126 
127  if (rxNetDevice && txNetDevice)
128  {
129  // we assume that devices are attached to a node
130  if (rxNetDevice->GetNode()->GetId() == txNetDevice->GetNode()->GetId())
131  {
132  NS_LOG_DEBUG("Skipping the pathloss calculation among different antennas of the "
133  "same node, not supported yet by any pathloss model in ns-3.");
134  continue;
135  }
136  }
137 
138  if ((*rxPhyIterator) != txParams->txPhy)
139  {
140  Time delay = MicroSeconds(0);
141 
142  Ptr<MobilityModel> receiverMobility = (*rxPhyIterator)->GetMobility();
143  NS_LOG_LOGIC("copying signal parameters " << txParams);
144  Ptr<SpectrumSignalParameters> rxParams = txParams->Copy();
145 
146  if (senderMobility && receiverMobility)
147  {
148  double txAntennaGain = 0;
149  double rxAntennaGain = 0;
150  double propagationGainDb = 0;
151  double pathLossDb = 0;
152  if (rxParams->txAntenna)
153  {
154  Angles txAngles(receiverMobility->GetPosition(), senderMobility->GetPosition());
155  txAntennaGain = rxParams->txAntenna->GetGainDb(txAngles);
156  NS_LOG_LOGIC("txAntennaGain = " << txAntennaGain << " dB");
157  pathLossDb -= txAntennaGain;
158  }
159  Ptr<AntennaModel> rxAntenna =
160  DynamicCast<AntennaModel>((*rxPhyIterator)->GetAntenna());
161  if (rxAntenna)
162  {
163  Angles rxAngles(senderMobility->GetPosition(), receiverMobility->GetPosition());
164  rxAntennaGain = rxAntenna->GetGainDb(rxAngles);
165  NS_LOG_LOGIC("rxAntennaGain = " << rxAntennaGain << " dB");
166  pathLossDb -= rxAntennaGain;
167  }
168  if (m_propagationLoss)
169  {
170  propagationGainDb =
171  m_propagationLoss->CalcRxPower(0, senderMobility, receiverMobility);
172  NS_LOG_LOGIC("propagationGainDb = " << propagationGainDb << " dB");
173  pathLossDb -= propagationGainDb;
174  }
175  NS_LOG_LOGIC("total pathLoss = " << pathLossDb << " dB");
176  // Gain trace
177  m_gainTrace(senderMobility,
178  receiverMobility,
179  txAntennaGain,
180  rxAntennaGain,
181  propagationGainDb,
182  pathLossDb);
183  // Pathloss trace
184  m_pathLossTrace(txParams->txPhy, *rxPhyIterator, pathLossDb);
185  if (pathLossDb > m_maxLossDb)
186  {
187  // beyond range
188  continue;
189  }
190  double pathGainLinear = std::pow(10.0, (-pathLossDb) / 10.0);
191  *(rxParams->psd) *= pathGainLinear;
192 
193  if (m_propagationDelay)
194  {
195  delay = m_propagationDelay->GetDelay(senderMobility, receiverMobility);
196  }
197  }
198 
199  if (rxNetDevice)
200  {
201  // the receiver has a NetDevice, so we expect that it is attached to a Node
202  uint32_t dstNode = rxNetDevice->GetNode()->GetId();
204  delay,
206  this,
207  rxParams,
208  *rxPhyIterator);
209  }
210  else
211  {
212  // the receiver is not attached to a NetDevice, so we cannot assume that it is
213  // attached to a node
214  Simulator::Schedule(delay,
216  this,
217  rxParams,
218  *rxPhyIterator);
219  }
220  }
221  }
222 }
223 
224 void
226 {
227  NS_LOG_FUNCTION(this << params);
229  {
230  params->psd =
231  m_spectrumPropagationLoss->CalcRxPowerSpectralDensity(params,
232  params->txPhy->GetMobility(),
233  receiver->GetMobility());
234  }
235  receiver->StartRx(params);
236 }
237 
238 std::size_t
240 {
241  NS_LOG_FUNCTION(this);
242  return m_phyList.size();
243 }
244 
247 {
248  NS_LOG_FUNCTION(this << i);
249  return m_phyList.at(i)->GetDevice()->GetObject<NetDevice>();
250 }
251 
252 } // namespace ns3
Class holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:118
virtual double GetGainDb(Angles a)=0
this method is expected to be re-implemented by each antenna model
Vector GetPosition() const
Network layer to device interface.
Definition: net-device.h:98
double CalcRxPower(double txPowerDbm, Ptr< MobilityModel > a, Ptr< MobilityModel > b) const
Returns the Rx Power taking into account all the PropagationLossModel(s) chained to the current one.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:587
SpectrumChannel implementation which handles a single spectrum model.
Ptr< const SpectrumModel > m_spectrumModel
SpectrumModel that this channel instance is supporting.
PhyList m_phyList
List of SpectrumPhy instances attached to the channel.
void StartTx(Ptr< SpectrumSignalParameters > params) override
Used by attached PHY instances to transmit signals on the channel.
void DoDispose() override
Destructor implementation.
void RemoveRx(Ptr< SpectrumPhy > phy) override
Remove a SpectrumPhy from a channel.
void AddRx(Ptr< SpectrumPhy > phy) override
Add a SpectrumPhy to a channel, so it can receive packets.
Ptr< NetDevice > GetDevice(std::size_t i) const override
void StartRx(Ptr< SpectrumSignalParameters > params, Ptr< SpectrumPhy > receiver)
Used internally to reschedule transmission after the propagation delay.
static TypeId GetTypeId()
Get the type ID.
Defines the interface for spectrum-aware channel implementations.
TracedCallback< Ptr< SpectrumSignalParameters > > m_txSigParamsTrace
Traced callback for SpectrumSignalParameters in StartTx requests.
void DoDispose() override
Destructor implementation.
Ptr< PropagationDelayModel > m_propagationDelay
Propagation delay model to be used with this channel.
Ptr< SpectrumPropagationLossModel > m_spectrumPropagationLoss
Frequency-dependent propagation loss model to be used with this channel.
TracedCallback< Ptr< const SpectrumPhy >, Ptr< const SpectrumPhy >, double > m_pathLossTrace
The PathLoss trace source.
TracedCallback< Ptr< const MobilityModel >, Ptr< const MobilityModel >, double, double, double, double > m_gainTrace
The Gain trace source.
Ptr< PropagationLossModel > m_propagationLoss
Single-frequency propagation loss model to be used with this channel.
double m_maxLossDb
Maximum loss [dB].
virtual void StartRx(Ptr< SpectrumSignalParameters > params)=0
Notify the SpectrumPhy instance of an incoming signal.
virtual Ptr< MobilityModel > GetMobility() const =0
Get the associated MobilityModel instance.
virtual Ptr< NetDevice > GetDevice() const =0
Get the associated NetDevice instance.
Ptr< const SpectrumModel > GetSpectrumModel() const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#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_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:86
#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
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1360
Every class exported by the ns3 library is enclosed in the ns3 namespace.
phy
Definition: third.py:82
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
Ptr< AntennaModel > txAntenna
The AntennaModel instance that was used to transmit this signal.
Time duration
The duration of the packet transmission.
Ptr< SpectrumPhy > txPhy
The SpectrumPhy instance that is making the transmission.
virtual Ptr< SpectrumSignalParameters > Copy() const
make a "virtual" copy of this class, where "virtual" refers to the fact that if the actual object is ...
Ptr< SpectrumValue > psd
The Power Spectral Density of the waveform, in linear units.