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;
59 }
60 
61 TypeId
63 {
65  static TypeId tid = TypeId ("ns3::SingleModelSpectrumChannel")
67  .SetGroupName ("Spectrum")
68  .AddConstructor<SingleModelSpectrumChannel> ()
69  ;
70  return tid;
71 }
72 
73 
74 void
76 {
77  NS_LOG_FUNCTION (this << phy);
78  m_phyList.push_back (phy);
79 }
80 
81 
82 void
84 {
85  NS_LOG_FUNCTION (this << txParams->psd << txParams->duration << txParams->txPhy);
86  NS_ASSERT_MSG (txParams->psd, "NULL txPsd");
87  NS_ASSERT_MSG (txParams->txPhy, "NULL txPhy");
88 
89  Ptr<SpectrumSignalParameters> txParamsTrace = txParams->Copy (); // copy it since traced value cannot be const (because of potential underlying DynamicCasts)
90  m_txSigParamsTrace (txParamsTrace);
91 
92  // just a sanity check routine. We might want to remove it to save some computational load -- one "if" statement ;-)
93  if (m_spectrumModel == 0)
94  {
95  // first pak, record SpectrumModel
96  m_spectrumModel = txParams->psd->GetSpectrumModel ();
97  }
98  else
99  {
100  // all attached SpectrumPhy instances must use the same SpectrumModel
101  NS_ASSERT (*(txParams->psd->GetSpectrumModel ()) == *m_spectrumModel);
102  }
103 
104 
105 
106 
107  Ptr<MobilityModel> senderMobility = txParams->txPhy->GetMobility ();
108 
109  for (PhyList::const_iterator rxPhyIterator = m_phyList.begin ();
110  rxPhyIterator != m_phyList.end ();
111  ++rxPhyIterator)
112  {
113  if ((*rxPhyIterator) != txParams->txPhy)
114  {
115  Time delay = MicroSeconds (0);
116 
117  Ptr<MobilityModel> receiverMobility = (*rxPhyIterator)->GetMobility ();
118  NS_LOG_LOGIC ("copying signal parameters " << txParams);
119  Ptr<SpectrumSignalParameters> rxParams = txParams->Copy ();
120 
121  if (senderMobility && receiverMobility)
122  {
123  double txAntennaGain = 0;
124  double rxAntennaGain = 0;
125  double propagationGainDb = 0;
126  double pathLossDb = 0;
127  if (rxParams->txAntenna != 0)
128  {
129  Angles txAngles (receiverMobility->GetPosition (), senderMobility->GetPosition ());
130  txAntennaGain = rxParams->txAntenna->GetGainDb (txAngles);
131  NS_LOG_LOGIC ("txAntennaGain = " << txAntennaGain << " dB");
132  pathLossDb -= txAntennaGain;
133  }
134  Ptr<AntennaModel> rxAntenna = (*rxPhyIterator)->GetRxAntenna ();
135  if (rxAntenna != 0)
136  {
137  Angles rxAngles (senderMobility->GetPosition (), receiverMobility->GetPosition ());
138  rxAntennaGain = rxAntenna->GetGainDb (rxAngles);
139  NS_LOG_LOGIC ("rxAntennaGain = " << rxAntennaGain << " dB");
140  pathLossDb -= rxAntennaGain;
141  }
142  if (m_propagationLoss)
143  {
144  propagationGainDb = m_propagationLoss->CalcRxPower (0, senderMobility, receiverMobility);
145  NS_LOG_LOGIC ("propagationGainDb = " << propagationGainDb << " dB");
146  pathLossDb -= propagationGainDb;
147  }
148  NS_LOG_LOGIC ("total pathLoss = " << pathLossDb << " dB");
149  // Gain trace
150  m_gainTrace (senderMobility, receiverMobility, txAntennaGain, rxAntennaGain, propagationGainDb, pathLossDb);
151  // Pathloss trace
152  m_pathLossTrace (txParams->txPhy, *rxPhyIterator, pathLossDb);
153  if ( pathLossDb > m_maxLossDb)
154  {
155  // beyond range
156  continue;
157  }
158  double pathGainLinear = std::pow (10.0, (-pathLossDb) / 10.0);
159  *(rxParams->psd) *= pathGainLinear;
160 
162  {
163  rxParams->psd = m_spectrumPropagationLoss->CalcRxPowerSpectralDensity (rxParams->psd, senderMobility, receiverMobility);
164  }
165 
166  if (m_propagationDelay)
167  {
168  delay = m_propagationDelay->GetDelay (senderMobility, receiverMobility);
169  }
170  }
171 
172 
173  Ptr<NetDevice> netDev = (*rxPhyIterator)->GetDevice ();
174  if (netDev)
175  {
176  // the receiver has a NetDevice, so we expect that it is attached to a Node
177  uint32_t dstNode = netDev->GetNode ()->GetId ();
178  Simulator::ScheduleWithContext (dstNode, delay, &SingleModelSpectrumChannel::StartRx, this, rxParams, *rxPhyIterator);
179  }
180  else
181  {
182  // the receiver is not attached to a NetDevice, so we cannot assume that it is attached to a node
184  rxParams, *rxPhyIterator);
185  }
186  }
187  }
188 }
189 
190 void
192 {
193  NS_LOG_FUNCTION (this << params);
194  receiver->StartRx (params);
195 }
196 
197 std::size_t
199 {
200  NS_LOG_FUNCTION (this);
201  return m_phyList.size ();
202 }
203 
206 {
207  NS_LOG_FUNCTION (this << i);
208  return m_phyList.at (i)->GetDevice ()->GetObject<NetDevice> ();
209 }
210 
211 
212 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
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...
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:45
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
TracedCallback< Ptr< const SpectrumPhy >, Ptr< const SpectrumPhy >, double > m_pathLossTrace
The PathLoss trace source.
virtual void AddRx(Ptr< SpectrumPhy > phy)
Add a SpectrumPhy to a channel, so it can receive packets.
#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
double m_maxLossDb
Maximum loss [dB].
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
phy
Definition: third.py:93
void StartRx(Ptr< SpectrumSignalParameters > params, Ptr< SpectrumPhy > receiver)
Used internally to reschedule transmission after the propagation delay.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1390
virtual void DoDispose()
Destructor implementation.
Ptr< SpectrumPropagationLossModel > m_spectrumPropagationLoss
Frequency-dependent propagation loss model to be used with this channel.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TypeId GetTypeId(void)
Get the type ID.
virtual void StartTx(Ptr< SpectrumSignalParameters > params)
Used by attached PHY instances to transmit signals on the channel.
virtual std::size_t GetNDevices(void) const
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Vector GetPosition(void) const
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1484
Ptr< PropagationLossModel > m_propagationLoss
Single-frequency propagation loss model to be used with this channel.
Network layer to device interface.
Definition: net-device.h:95
TracedCallback< Ptr< SpectrumSignalParameters > > m_txSigParamsTrace
Traced callback for SpectrumSignalParameters in StartTx requests.
SpectrumChannel implementation which handles a single spectrum model.
Defines the interface for spectrum-aware channel implementations.
Ptr< PropagationDelayModel > m_propagationDelay
Propagation delay model to be used with this channel.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1086
struct holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:71
PhyList m_phyList
List of SpectrumPhy instances attached to the channel.
a unique identifier for an interface.
Definition: type-id.h:58
TracedCallback< Ptr< const MobilityModel >, Ptr< const MobilityModel >, double, double, double, double > m_gainTrace
The Gain trace source.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
virtual void DoDispose(void)
Destructor implementation.
Ptr< const SpectrumModel > m_spectrumModel
SpectrumModel that this channel instance is supporting.
virtual Ptr< NetDevice > GetDevice(std::size_t i) const