A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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#include <ns3/spectrum-transmit-filter.h>
38
39#include <algorithm>
40
41namespace ns3
42{
43
44NS_LOG_COMPONENT_DEFINE("SingleModelSpectrumChannel");
45
46NS_OBJECT_ENSURE_REGISTERED(SingleModelSpectrumChannel);
47
49{
50 NS_LOG_FUNCTION(this);
51}
52
53void
55{
56 NS_LOG_FUNCTION(this);
57 m_phyList.clear();
58 m_spectrumModel = nullptr;
60}
61
64{
66 static TypeId tid = TypeId("ns3::SingleModelSpectrumChannel")
68 .SetGroupName("Spectrum")
69 .AddConstructor<SingleModelSpectrumChannel>();
70 return tid;
71}
72
73void
75{
76 NS_LOG_FUNCTION(this << phy);
77 auto it = std::find(begin(m_phyList), end(m_phyList), phy);
78 if (it != std::end(m_phyList))
79 {
80 m_phyList.erase(it);
81 }
82}
83
84void
86{
87 NS_LOG_FUNCTION(this << phy);
88 if (std::find(m_phyList.cbegin(), m_phyList.cend(), phy) == m_phyList.cend())
89 {
90 m_phyList.push_back(phy);
91 }
92}
93
94void
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 =
102 txParams->Copy(); // copy it since traced value cannot be const (because of potential
103 // underlying DynamicCasts)
104 m_txSigParamsTrace(txParamsTrace);
105
106 // just a sanity check routine. We might want to remove it to save some computational load --
107 // one "if" statement ;-)
108 if (!m_spectrumModel)
109 {
110 // first pak, record SpectrumModel
111 m_spectrumModel = txParams->psd->GetSpectrumModel();
112 }
113 else
114 {
115 // all attached SpectrumPhy instances must use the same SpectrumModel
116 NS_ASSERT(*(txParams->psd->GetSpectrumModel()) == *m_spectrumModel);
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 "
134 "same node, not supported yet by any pathloss model in ns-3.");
135 continue;
136 }
137 }
138
139 if (m_filter && m_filter->Filter(txParams, *rxPhyIterator))
140 {
141 continue;
142 }
143
144 if ((*rxPhyIterator) != txParams->txPhy)
145 {
146 Time delay = MicroSeconds(0);
147
148 Ptr<MobilityModel> receiverMobility = (*rxPhyIterator)->GetMobility();
149 NS_LOG_LOGIC("copying signal parameters " << txParams);
150 Ptr<SpectrumSignalParameters> rxParams = txParams->Copy();
151
152 if (senderMobility && receiverMobility)
153 {
154 double txAntennaGain = 0;
155 double rxAntennaGain = 0;
156 double propagationGainDb = 0;
157 double pathLossDb = 0;
158 if (rxParams->txAntenna)
159 {
160 Angles txAngles(receiverMobility->GetPosition(), senderMobility->GetPosition());
161 txAntennaGain = rxParams->txAntenna->GetGainDb(txAngles);
162 NS_LOG_LOGIC("txAntennaGain = " << txAntennaGain << " dB");
163 pathLossDb -= txAntennaGain;
164 }
165 Ptr<AntennaModel> rxAntenna =
166 DynamicCast<AntennaModel>((*rxPhyIterator)->GetAntenna());
167 if (rxAntenna)
168 {
169 Angles rxAngles(senderMobility->GetPosition(), receiverMobility->GetPosition());
170 rxAntennaGain = rxAntenna->GetGainDb(rxAngles);
171 NS_LOG_LOGIC("rxAntennaGain = " << rxAntennaGain << " dB");
172 pathLossDb -= rxAntennaGain;
173 }
175 {
176 propagationGainDb =
177 m_propagationLoss->CalcRxPower(0, senderMobility, receiverMobility);
178 NS_LOG_LOGIC("propagationGainDb = " << propagationGainDb << " dB");
179 pathLossDb -= propagationGainDb;
180 }
181 NS_LOG_LOGIC("total pathLoss = " << pathLossDb << " dB");
182 // Gain trace
183 m_gainTrace(senderMobility,
184 receiverMobility,
185 txAntennaGain,
186 rxAntennaGain,
187 propagationGainDb,
188 pathLossDb);
189 // Pathloss trace
190 m_pathLossTrace(txParams->txPhy, *rxPhyIterator, pathLossDb);
191 if (pathLossDb > m_maxLossDb)
192 {
193 // beyond range
194 continue;
195 }
196 double pathGainLinear = std::pow(10.0, (-pathLossDb) / 10.0);
197 *(rxParams->psd) *= pathGainLinear;
198
200 {
201 delay = m_propagationDelay->GetDelay(senderMobility, receiverMobility);
202 }
203 }
204
205 if (rxNetDevice)
206 {
207 // the receiver has a NetDevice, so we expect that it is attached to a Node
208 uint32_t dstNode = rxNetDevice->GetNode()->GetId();
210 delay,
212 this,
213 rxParams,
214 *rxPhyIterator);
215 }
216 else
217 {
218 // the receiver is not attached to a NetDevice, so we cannot assume that it is
219 // attached to a node
222 this,
223 rxParams,
224 *rxPhyIterator);
225 }
226 }
227 }
228}
229
230void
232{
233 NS_LOG_FUNCTION(this << params);
235 {
236 params->psd =
237 m_spectrumPropagationLoss->CalcRxPowerSpectralDensity(params,
238 params->txPhy->GetMobility(),
239 receiver->GetMobility());
240 }
241 receiver->StartRx(params);
242}
243
244std::size_t
246{
247 NS_LOG_FUNCTION(this);
248 return m_phyList.size();
249}
250
253{
254 NS_LOG_FUNCTION(this << i);
255 return m_phyList.at(i)->GetDevice()->GetObject<NetDevice>();
256}
257
258} // namespace ns3
Class holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:118
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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< SpectrumTransmitFilter > m_filter
Transmit filter to be used with this channel.
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].
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
#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.