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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
10
11#include "spectrum-phy.h"
14#include "wraparound-model.h"
15
16#include "ns3/angles.h"
17#include "ns3/antenna-model.h"
18#include "ns3/double.h"
19#include "ns3/log.h"
20#include "ns3/mobility-model.h"
21#include "ns3/net-device.h"
22#include "ns3/node.h"
23#include "ns3/object.h"
24#include "ns3/packet-burst.h"
25#include "ns3/packet.h"
26#include "ns3/propagation-delay-model.h"
27#include "ns3/propagation-loss-model.h"
28#include "ns3/simulator.h"
29
30#include <algorithm>
31
32namespace ns3
33{
34
35NS_LOG_COMPONENT_DEFINE("SingleModelSpectrumChannel");
36
38
43
44void
52
55{
57 static TypeId tid = TypeId("ns3::SingleModelSpectrumChannel")
59 .SetGroupName("Spectrum")
60 .AddConstructor<SingleModelSpectrumChannel>();
61 return tid;
62}
63
64void
66{
67 NS_LOG_FUNCTION(this << phy);
68 auto it = std::find(begin(m_phyList), end(m_phyList), phy);
69 if (it != std::end(m_phyList))
70 {
71 m_phyList.erase(it);
72 }
73}
74
75void
77{
78 NS_LOG_FUNCTION(this << phy);
79 if (std::find(m_phyList.cbegin(), m_phyList.cend(), phy) == m_phyList.cend())
80 {
81 m_phyList.push_back(phy);
82 }
83 else
84 {
85 // PHY has switched its channel, reset m_spectrumModel
86 m_spectrumModel = nullptr;
87 }
88}
89
90void
92{
93 NS_LOG_FUNCTION(this << txParams->psd << txParams->duration << txParams->txPhy);
94 NS_ASSERT_MSG(txParams->psd, "NULL txPsd");
95 NS_ASSERT_MSG(txParams->txPhy, "NULL txPhy");
96
97 Ptr<SpectrumSignalParameters> txParamsTrace =
98 txParams->Copy(); // copy it since traced value cannot be const (because of potential
99 // underlying DynamicCasts)
100 m_txSigParamsTrace(txParamsTrace);
101
102 // just a sanity check routine. We might want to remove it to save some computational load --
103 // one "if" statement ;-)
104 if (!m_spectrumModel)
105 {
106 // first pak, record SpectrumModel
107 m_spectrumModel = txParams->psd->GetSpectrumModel();
108 }
109 else
110 {
111 // all attached SpectrumPhy instances must use the same SpectrumModel
112 NS_ASSERT(*(txParams->psd->GetSpectrumModel()) == *m_spectrumModel);
113 }
114
115 auto wraparound = GetObject<WraparoundModel>();
116 Ptr<MobilityModel> refSenderMobility = txParams->txPhy->GetMobility();
117 Ptr<MobilityModel> senderMobility = refSenderMobility;
118
119 for (auto rxPhyIterator = m_phyList.begin(); rxPhyIterator != m_phyList.end(); ++rxPhyIterator)
120 {
121 Ptr<NetDevice> rxNetDevice = (*rxPhyIterator)->GetDevice();
122 Ptr<NetDevice> txNetDevice = txParams->txPhy->GetDevice();
123
124 if (rxNetDevice && txNetDevice)
125 {
126 // we assume that devices are attached to a node
127 if (rxNetDevice->GetNode()->GetId() == txNetDevice->GetNode()->GetId())
128 {
129 NS_LOG_DEBUG("Skipping the pathloss calculation among different antennas of the "
130 "same node, not supported yet by any pathloss model in ns-3.");
131 continue;
132 }
133 }
134
135 if (m_filter && m_filter->Filter(txParams, *rxPhyIterator))
136 {
137 continue;
138 }
139
140 if ((*rxPhyIterator) != txParams->txPhy)
141 {
142 Time delay;
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 if (wraparound)
151 {
152 // Use virtual mobility model instead
153 senderMobility =
154 wraparound->GetVirtualMobilityModel(refSenderMobility, receiverMobility);
155 }
156 rxParams->txMobility = senderMobility;
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->txMobility,
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:107
Network layer to device interface.
Definition net-device.h:87
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:578
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.
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:96
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:585