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