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 
39 
40 
41 namespace ns3 {
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 = 0;
62 }
63 
64 TypeId
66 {
68  static TypeId tid = TypeId ("ns3::SingleModelSpectrumChannel")
70  .AddConstructor<SingleModelSpectrumChannel> ()
71  .AddAttribute ("MaxLossDb",
72  "If a single-frequency PropagationLossModel is used, "
73  "this value represents the maximum loss in dB for which "
74  "transmissions will be passed to the receiving PHY. "
75  "Signals for which the PropagationLossModel returns "
76  "a loss bigger than this value will not be propagated "
77  "to the receiver. This parameter is to be used to reduce "
78  "the computational load by not propagating signals "
79  "that are far beyond the interference range. Note that "
80  "the default value corresponds to considering all signals "
81  "for reception. Tune this value with care. ",
82  DoubleValue (1.0e9),
84  MakeDoubleChecker<double> ())
85  .AddTraceSource ("PathLoss",
86  "This trace is fired whenever a new path loss value "
87  "is calculated. The first and second parameters "
88  "to the trace are pointers respectively to the TX and "
89  "RX SpectrumPhy instances, whereas the third parameters "
90  "is the loss value in dB. Note that the loss value "
91  "reported by this trace is the single-frequency loss "
92  "value obtained by evaluating only the TX and RX "
93  "AntennaModels and the PropagationLossModel. "
94  "In particular, note that SpectrumPropagationLossModel "
95  "(even if present) is never used to evaluate the "
96  "loss value reported in this trace. ",
98  "ns3::SpectrumChannel::LossTracedCallback")
99  ;
100  return tid;
101 }
102 
103 
104 void
106 {
107  NS_LOG_FUNCTION (this << phy);
108  m_phyList.push_back (phy);
109 }
110 
111 
112 void
114 {
115  NS_LOG_FUNCTION (this << txParams->psd << txParams->duration << txParams->txPhy);
116  NS_ASSERT_MSG (txParams->psd, "NULL txPsd");
117  NS_ASSERT_MSG (txParams->txPhy, "NULL txPhy");
118 
119  // just a sanity check routine. We might want to remove it to save some computational load -- one "if" statement ;-)
120  if (m_spectrumModel == 0)
121  {
122  // first pak, record SpectrumModel
123  m_spectrumModel = txParams->psd->GetSpectrumModel ();
124  }
125  else
126  {
127  // all attached SpectrumPhy instances must use the same SpectrumModel
128  NS_ASSERT (*(txParams->psd->GetSpectrumModel ()) == *m_spectrumModel);
129  }
130 
131 
132 
133 
134  Ptr<MobilityModel> senderMobility = txParams->txPhy->GetMobility ();
135 
136  for (PhyList::const_iterator rxPhyIterator = m_phyList.begin ();
137  rxPhyIterator != m_phyList.end ();
138  ++rxPhyIterator)
139  {
140  if ((*rxPhyIterator) != txParams->txPhy)
141  {
142  Time delay = MicroSeconds (0);
143 
144  Ptr<MobilityModel> receiverMobility = (*rxPhyIterator)->GetMobility ();
145  NS_LOG_LOGIC ("copying signal parameters " << txParams);
146  Ptr<SpectrumSignalParameters> rxParams = txParams->Copy ();
147 
148  if (senderMobility && receiverMobility)
149  {
150  double pathLossDb = 0;
151  if (rxParams->txAntenna != 0)
152  {
153  Angles txAngles (receiverMobility->GetPosition (), senderMobility->GetPosition ());
154  double txAntennaGain = rxParams->txAntenna->GetGainDb (txAngles);
155  NS_LOG_LOGIC ("txAntennaGain = " << txAntennaGain << " dB");
156  pathLossDb -= txAntennaGain;
157  }
158  Ptr<AntennaModel> rxAntenna = (*rxPhyIterator)->GetRxAntenna ();
159  if (rxAntenna != 0)
160  {
161  Angles rxAngles (senderMobility->GetPosition (), receiverMobility->GetPosition ());
162  double rxAntennaGain = rxAntenna->GetGainDb (rxAngles);
163  NS_LOG_LOGIC ("rxAntennaGain = " << rxAntennaGain << " dB");
164  pathLossDb -= rxAntennaGain;
165  }
166  if (m_propagationLoss)
167  {
168  double propagationGainDb = m_propagationLoss->CalcRxPower (0, senderMobility, receiverMobility);
169  NS_LOG_LOGIC ("propagationGainDb = " << propagationGainDb << " dB");
170  pathLossDb -= propagationGainDb;
171  }
172  NS_LOG_LOGIC ("total pathLoss = " << pathLossDb << " dB");
173  m_pathLossTrace (txParams->txPhy, *rxPhyIterator, pathLossDb);
174  if ( pathLossDb > m_maxLossDb)
175  {
176  // beyond range
177  continue;
178  }
179  double pathGainLinear = std::pow (10.0, (-pathLossDb) / 10.0);
180  *(rxParams->psd) *= pathGainLinear;
181 
183  {
184  rxParams->psd = m_spectrumPropagationLoss->CalcRxPowerSpectralDensity (rxParams->psd, senderMobility, receiverMobility);
185  }
186 
187  if (m_propagationDelay)
188  {
189  delay = m_propagationDelay->GetDelay (senderMobility, receiverMobility);
190  }
191  }
192 
193 
194  Ptr<NetDevice> netDev = (*rxPhyIterator)->GetDevice ();
195  if (netDev)
196  {
197  // the receiver has a NetDevice, so we expect that it is attached to a Node
198  uint32_t dstNode = netDev->GetNode ()->GetId ();
199  Simulator::ScheduleWithContext (dstNode, delay, &SingleModelSpectrumChannel::StartRx, this, rxParams, *rxPhyIterator);
200  }
201  else
202  {
203  // the receiver is not attached to a NetDevice, so we cannot assume that it is attached to a node
205  rxParams, *rxPhyIterator);
206  }
207  }
208  }
209 
210 }
211 
212 void
214 {
215  NS_LOG_FUNCTION (this << params);
216  receiver->StartRx (params);
217 }
218 
219 
220 
221 uint32_t
223 {
224  NS_LOG_FUNCTION (this);
225  return m_phyList.size ();
226 }
227 
228 
231 {
232  NS_LOG_FUNCTION (this << i);
233  return m_phyList.at (i)->GetDevice ()->GetObject<NetDevice> ();
234 }
235 
236 
237 void
239 {
240  NS_LOG_FUNCTION (this << loss);
242  m_propagationLoss = loss;
243 }
244 
245 
246 void
248 {
249  NS_LOG_FUNCTION (this << loss);
252 }
253 
254 void
256 {
257  NS_LOG_FUNCTION (this << delay);
259  m_propagationDelay = delay;
260 }
261 
262 
265 {
266  NS_LOG_FUNCTION (this);
268 }
269 
270 
271 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void StartRx(Ptr< SpectrumSignalParameters > params)=0
Notify the SpectrumPhy instance of an incoming signal.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual Ptr< SpectrumPropagationLossModel > GetSpectrumPropagationLossModel(void)
Ptr< PropagationDelayModel > m_propagationDelay
propagation delay model to be used with this channel
virtual void AddRx(Ptr< SpectrumPhy > phy)
add a SpectrumPhy to a channel, so it can receive packets
virtual void AddSpectrumPropagationLossModel(Ptr< SpectrumPropagationLossModel > loss)
set the frequency-dependent propagation loss model to be used
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Vector GetPosition(void) const
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:338
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
set the single-frequency propagation loss model to be used
virtual Ptr< NetDevice > GetDevice(uint32_t i) const
Ptr< PropagationLossModel > m_propagationLoss
single-frequency propagation loss model to be used with this channel
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
TracedCallback< Ptr< SpectrumPhy >, Ptr< SpectrumPhy >, double > m_pathLossTrace
void StartRx(Ptr< SpectrumSignalParameters > params, Ptr< SpectrumPhy > receiver)
used internally to reschedule transmission after the propagation delay
virtual void DoDispose()
Destructor implementation.
double CalcRxPower(double txPowerDbm, Ptr< MobilityModel > a, Ptr< MobilityModel > b) const
Returns the Rx Power taking into account all the PropagatinLossModel(s) chained to the current one...
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:899
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void StartTx(Ptr< SpectrumSignalParameters > params)
Used by attached PHY instances to transmit signals on the channel.
Ptr< SpectrumPropagationLossModel > m_spectrumPropagationLoss
frequency-dependent propagation loss model to be used with this channel
#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:84
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: double.h:42
Network layer to device interface.
Definition: net-device.h:75
virtual void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
set the propagation delay model to be used
Defines the interface for spectrum-aware channel implementations.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:875
struct holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:71
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
PhyList m_phyList
list of SpectrumPhy instances attached to the channel
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
Ptr< const SpectrumModel > m_spectrumModel
SpectrumModel that this channel instance is supporting.