A Discrete-Event Network Simulator
API
single-model-spectrum-channel.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 CTTC
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  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #include <ns3/object.h>
22 #include <ns3/simulator.h>
23 #include <ns3/log.h>
24 #include <ns3/packet.h>
25 #include <ns3/packet-burst.h>
26 #include <ns3/net-device.h>
27 #include <ns3/node.h>
28 #include <ns3/double.h>
29 #include <ns3/mobility-model.h>
30 #include <ns3/spectrum-phy.h>
31 #include <ns3/spectrum-propagation-loss-model.h>
32 #include <ns3/propagation-loss-model.h>
33 #include <ns3/propagation-delay-model.h>
34 #include <ns3/antenna-model.h>
35 #include <ns3/angles.h>
36 
37 #include <algorithm>
38 
40 
41 
42 namespace ns3 {
43 
44 NS_LOG_COMPONENT_DEFINE ("SingleModelSpectrumChannel");
45 
46 NS_OBJECT_ENSURE_REGISTERED (SingleModelSpectrumChannel);
47 
49 {
50  NS_LOG_FUNCTION (this);
51 }
52 
53 void
55 {
56  NS_LOG_FUNCTION (this);
57  m_phyList.clear ();
58  m_spectrumModel = 0;
60 }
61 
62 TypeId
64 {
66  static TypeId tid = TypeId ("ns3::SingleModelSpectrumChannel")
68  .SetGroupName ("Spectrum")
69  .AddConstructor<SingleModelSpectrumChannel> ()
70  ;
71  return tid;
72 }
73 
74 
75 void
77 {
78  NS_LOG_FUNCTION (this << phy);
79  auto it = std::find (begin (m_phyList), end (m_phyList), phy);
80  if (it != std::end (m_phyList))
81  {
82  m_phyList.erase (it);
83  }
84 }
85 
86 void
88 {
89  NS_LOG_FUNCTION (this << phy);
90  m_phyList.push_back (phy);
91 }
92 
93 
94 void
96 {
97  NS_LOG_FUNCTION (this << txParams->psd << txParams->duration << txParams->txPhy);
98  NS_ASSERT_MSG (txParams->psd, "NULL txPsd");
99  NS_ASSERT_MSG (txParams->txPhy, "NULL txPhy");
100 
101  Ptr<SpectrumSignalParameters> txParamsTrace = txParams->Copy (); // copy it since traced value cannot be const (because of potential underlying DynamicCasts)
102  m_txSigParamsTrace (txParamsTrace);
103 
104  // just a sanity check routine. We might want to remove it to save some computational load -- one "if" statement ;-)
105  if (m_spectrumModel == 0)
106  {
107  // first pak, record SpectrumModel
108  m_spectrumModel = txParams->psd->GetSpectrumModel ();
109  }
110  else
111  {
112  // all attached SpectrumPhy instances must use the same SpectrumModel
113  NS_ASSERT (*(txParams->psd->GetSpectrumModel ()) == *m_spectrumModel);
114  }
115 
116 
117 
118 
119  Ptr<MobilityModel> senderMobility = txParams->txPhy->GetMobility ();
120 
121  for (PhyList::const_iterator rxPhyIterator = m_phyList.begin ();
122  rxPhyIterator != m_phyList.end ();
123  ++rxPhyIterator)
124  {
125  Ptr<NetDevice> rxNetDevice = (*rxPhyIterator)->GetDevice ();
126  Ptr<NetDevice> txNetDevice = txParams->txPhy->GetDevice ();
127 
128  if (rxNetDevice && txNetDevice)
129  {
130  // we assume that devices are attached to a node
131  if (rxNetDevice->GetNode()->GetId() == txNetDevice->GetNode()->GetId())
132  {
133  NS_LOG_DEBUG ("Skipping the pathloss calculation among different antennas of the 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 != 0)
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 = DynamicCast<AntennaModel>((*rxPhyIterator)->GetAntenna ());
160  if (rxAntenna != 0)
161  {
162  Angles rxAngles (senderMobility->GetPosition (), receiverMobility->GetPosition ());
163  rxAntennaGain = rxAntenna->GetGainDb (rxAngles);
164  NS_LOG_LOGIC ("rxAntennaGain = " << rxAntennaGain << " dB");
165  pathLossDb -= rxAntennaGain;
166  }
167  if (m_propagationLoss)
168  {
169  propagationGainDb = m_propagationLoss->CalcRxPower (0, senderMobility, receiverMobility);
170  NS_LOG_LOGIC ("propagationGainDb = " << propagationGainDb << " dB");
171  pathLossDb -= propagationGainDb;
172  }
173  NS_LOG_LOGIC ("total pathLoss = " << pathLossDb << " dB");
174  // Gain trace
175  m_gainTrace (senderMobility, receiverMobility, txAntennaGain, rxAntennaGain, propagationGainDb, pathLossDb);
176  // Pathloss trace
177  m_pathLossTrace (txParams->txPhy, *rxPhyIterator, pathLossDb);
178  if ( pathLossDb > m_maxLossDb)
179  {
180  // beyond range
181  continue;
182  }
183  double pathGainLinear = std::pow (10.0, (-pathLossDb) / 10.0);
184  *(rxParams->psd) *= pathGainLinear;
185 
187  {
188  rxParams->psd = m_spectrumPropagationLoss->CalcRxPowerSpectralDensity (rxParams->psd, senderMobility, receiverMobility);
189  }
190 
191  if (m_propagationDelay)
192  {
193  delay = m_propagationDelay->GetDelay (senderMobility, receiverMobility);
194  }
195  }
196 
197 
198  if (rxNetDevice)
199  {
200  // the receiver has a NetDevice, so we expect that it is attached to a Node
201  uint32_t dstNode = rxNetDevice->GetNode ()->GetId ();
202  Simulator::ScheduleWithContext (dstNode, delay, &SingleModelSpectrumChannel::StartRx, this, rxParams, *rxPhyIterator);
203  }
204  else
205  {
206  // the receiver is not attached to a NetDevice, so we cannot assume that it is attached to a node
208  rxParams, *rxPhyIterator);
209  }
210  }
211  }
212 }
213 
214 void
216 {
217  NS_LOG_FUNCTION (this << params);
218  receiver->StartRx (params);
219 }
220 
221 std::size_t
223 {
224  NS_LOG_FUNCTION (this);
225  return m_phyList.size ();
226 }
227 
230 {
231  NS_LOG_FUNCTION (this << i);
232  return m_phyList.at (i)->GetDevice ()->GetObject<NetDevice> ();
233 }
234 
235 
236 } // namespace ns3
Class holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:119
virtual double GetGainDb(Angles a)=0
this method is expected to be re-implemented by each antenna model
Vector GetPosition(void) const
Network layer to device interface.
Definition: net-device.h:96
virtual Ptr< Node > GetNode(void) const =0
uint32_t GetId(void) const
Definition: node.cc:109
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(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
SpectrumChannel implementation which handles a single spectrum model.
Ptr< const SpectrumModel > m_spectrumModel
SpectrumModel that this channel instance is supporting.
virtual void RemoveRx(Ptr< SpectrumPhy > phy)
Remove a SpectrumPhy from a channel.
PhyList m_phyList
List of SpectrumPhy instances attached to the channel.
virtual void DoDispose()
Destructor implementation.
virtual void StartTx(Ptr< SpectrumSignalParameters > params)
Used by attached PHY instances to transmit signals on the channel.
virtual std::size_t GetNDevices(void) const
static TypeId GetTypeId(void)
Get the type ID.
virtual void AddRx(Ptr< SpectrumPhy > phy)
Add a SpectrumPhy to a channel, so it can receive packets.
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
void StartRx(Ptr< SpectrumSignalParameters > params, Ptr< SpectrumPhy > receiver)
Used internally to reschedule transmission after the propagation delay.
Defines the interface for spectrum-aware channel implementations.
TracedCallback< Ptr< const SpectrumPhy >, Ptr< const SpectrumPhy >, double > m_pathLossTrace
The PathLoss trace source.
TracedCallback< Ptr< SpectrumSignalParameters > > m_txSigParamsTrace
Traced callback for SpectrumSignalParameters in StartTx requests.
Ptr< PropagationDelayModel > m_propagationDelay
Propagation delay model to be used with this channel.
virtual void DoDispose(void)
Destructor implementation.
Ptr< SpectrumPropagationLossModel > m_spectrumPropagationLoss
Frequency-dependent propagation loss model to be used with this channel.
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.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:45
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1260
Every class exported by the ns3 library is enclosed in the ns3 namespace.
phy
Definition: third.py:93