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 "spectrum-phy.h"
25
26#include <ns3/angles.h>
27#include <ns3/antenna-model.h>
28#include <ns3/double.h>
29#include <ns3/log.h>
30#include <ns3/mobility-model.h>
31#include <ns3/net-device.h>
32#include <ns3/node.h>
33#include <ns3/object.h>
34#include <ns3/packet-burst.h>
35#include <ns3/packet.h>
36#include <ns3/propagation-delay-model.h>
37#include <ns3/propagation-loss-model.h>
38#include <ns3/simulator.h>
39
40#include <algorithm>
41
42namespace ns3
43{
44
45NS_LOG_COMPONENT_DEFINE("SingleModelSpectrumChannel");
46
47NS_OBJECT_ENSURE_REGISTERED(SingleModelSpectrumChannel);
48
50{
51 NS_LOG_FUNCTION(this);
52}
53
54void
56{
57 NS_LOG_FUNCTION(this);
58 m_phyList.clear();
59 m_spectrumModel = nullptr;
61}
62
65{
67 static TypeId tid = TypeId("ns3::SingleModelSpectrumChannel")
69 .SetGroupName("Spectrum")
70 .AddConstructor<SingleModelSpectrumChannel>();
71 return tid;
72}
73
74void
76{
77 NS_LOG_FUNCTION(this << phy);
78 auto it = std::find(begin(m_phyList), end(m_phyList), phy);
79 if (it != std::end(m_phyList))
80 {
81 m_phyList.erase(it);
82 }
83}
84
85void
87{
88 NS_LOG_FUNCTION(this << phy);
89 if (std::find(m_phyList.cbegin(), m_phyList.cend(), phy) == m_phyList.cend())
90 {
91 m_phyList.push_back(phy);
92 }
93 else
94 {
95 // PHY has switched its channel, reset m_spectrumModel
96 m_spectrumModel = nullptr;
97 }
98}
99
100void
102{
103 NS_LOG_FUNCTION(this << txParams->psd << txParams->duration << txParams->txPhy);
104 NS_ASSERT_MSG(txParams->psd, "NULL txPsd");
105 NS_ASSERT_MSG(txParams->txPhy, "NULL txPhy");
106
107 Ptr<SpectrumSignalParameters> txParamsTrace =
108 txParams->Copy(); // copy it since traced value cannot be const (because of potential
109 // underlying DynamicCasts)
110 m_txSigParamsTrace(txParamsTrace);
111
112 // just a sanity check routine. We might want to remove it to save some computational load --
113 // one "if" statement ;-)
114 if (!m_spectrumModel)
115 {
116 // first pak, record SpectrumModel
117 m_spectrumModel = txParams->psd->GetSpectrumModel();
118 }
119 else
120 {
121 // all attached SpectrumPhy instances must use the same SpectrumModel
122 NS_ASSERT(*(txParams->psd->GetSpectrumModel()) == *m_spectrumModel);
123 }
124
125 Ptr<MobilityModel> senderMobility = txParams->txPhy->GetMobility();
126
127 for (auto rxPhyIterator = m_phyList.begin(); rxPhyIterator != m_phyList.end(); ++rxPhyIterator)
128 {
129 Ptr<NetDevice> rxNetDevice = (*rxPhyIterator)->GetDevice();
130 Ptr<NetDevice> txNetDevice = txParams->txPhy->GetDevice();
131
132 if (rxNetDevice && txNetDevice)
133 {
134 // we assume that devices are attached to a node
135 if (rxNetDevice->GetNode()->GetId() == txNetDevice->GetNode()->GetId())
136 {
137 NS_LOG_DEBUG("Skipping the pathloss calculation among different antennas of the "
138 "same node, not supported yet by any pathloss model in ns-3.");
139 continue;
140 }
141 }
142
143 if (m_filter && m_filter->Filter(txParams, *rxPhyIterator))
144 {
145 continue;
146 }
147
148 if ((*rxPhyIterator) != txParams->txPhy)
149 {
150 Time delay = MicroSeconds(0);
151
152 Ptr<MobilityModel> receiverMobility = (*rxPhyIterator)->GetMobility();
153 NS_LOG_LOGIC("copying signal parameters " << txParams);
154 Ptr<SpectrumSignalParameters> rxParams = txParams->Copy();
155
156 if (senderMobility && receiverMobility)
157 {
158 double txAntennaGain = 0;
159 double rxAntennaGain = 0;
160 double propagationGainDb = 0;
161 double pathLossDb = 0;
162 if (rxParams->txAntenna)
163 {
164 Angles txAngles(receiverMobility->GetPosition(), senderMobility->GetPosition());
165 txAntennaGain = rxParams->txAntenna->GetGainDb(txAngles);
166 NS_LOG_LOGIC("txAntennaGain = " << txAntennaGain << " dB");
167 pathLossDb -= txAntennaGain;
168 }
169 Ptr<AntennaModel> rxAntenna =
170 DynamicCast<AntennaModel>((*rxPhyIterator)->GetAntenna());
171 if (rxAntenna)
172 {
173 Angles rxAngles(senderMobility->GetPosition(), receiverMobility->GetPosition());
174 rxAntennaGain = rxAntenna->GetGainDb(rxAngles);
175 NS_LOG_LOGIC("rxAntennaGain = " << rxAntennaGain << " dB");
176 pathLossDb -= rxAntennaGain;
177 }
179 {
180 propagationGainDb =
181 m_propagationLoss->CalcRxPower(0, senderMobility, receiverMobility);
182 NS_LOG_LOGIC("propagationGainDb = " << propagationGainDb << " dB");
183 pathLossDb -= propagationGainDb;
184 }
185 NS_LOG_LOGIC("total pathLoss = " << pathLossDb << " dB");
186 // Gain trace
187 m_gainTrace(senderMobility,
188 receiverMobility,
189 txAntennaGain,
190 rxAntennaGain,
191 propagationGainDb,
192 pathLossDb);
193 // Pathloss trace
194 m_pathLossTrace(txParams->txPhy, *rxPhyIterator, pathLossDb);
195 if (pathLossDb > m_maxLossDb)
196 {
197 // beyond range
198 continue;
199 }
200 double pathGainLinear = std::pow(10.0, (-pathLossDb) / 10.0);
201 *(rxParams->psd) *= pathGainLinear;
202
204 {
205 delay = m_propagationDelay->GetDelay(senderMobility, receiverMobility);
206 }
207 }
208
209 if (rxNetDevice)
210 {
211 // the receiver has a NetDevice, so we expect that it is attached to a Node
212 uint32_t dstNode = rxNetDevice->GetNode()->GetId();
214 delay,
216 this,
217 rxParams,
218 *rxPhyIterator);
219 }
220 else
221 {
222 // the receiver is not attached to a NetDevice, so we cannot assume that it is
223 // attached to a node
226 this,
227 rxParams,
228 *rxPhyIterator);
229 }
230 }
231 }
232}
233
234void
236{
237 NS_LOG_FUNCTION(this << params);
239 {
240 params->psd =
241 m_spectrumPropagationLoss->CalcRxPowerSpectralDensity(params,
242 params->txPhy->GetMobility(),
243 receiver->GetMobility());
244 }
245 receiver->StartRx(params);
246}
247
248std::size_t
250{
251 NS_LOG_FUNCTION(this);
252 return m_phyList.size();
253}
254
257{
258 NS_LOG_FUNCTION(this << i);
259 return m_phyList.at(i)->GetDevice()->GetObject<NetDevice>();
260}
261
262} // 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:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
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:932
#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:1343
Every class exported by the ns3 library is enclosed in the ns3 namespace.