A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
half-duplex-ideal-phy.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
24
25#include <ns3/antenna-model.h>
26#include <ns3/callback.h>
27#include <ns3/log.h>
28#include <ns3/object-factory.h>
29#include <ns3/packet-burst.h>
30#include <ns3/simulator.h>
31#include <ns3/trace-source-accessor.h>
32
33#include <cmath>
34
35namespace ns3
36{
37
38NS_LOG_COMPONENT_DEFINE("HalfDuplexIdealPhy");
39
40NS_OBJECT_ENSURE_REGISTERED(HalfDuplexIdealPhy);
41
43 : m_mobility(nullptr),
44 m_netDevice(nullptr),
45 m_channel(nullptr),
46 m_txPsd(nullptr),
47 m_state(IDLE)
48{
49 m_interference.SetErrorModel(CreateObject<ShannonSpectrumErrorModel>());
50}
51
53{
54}
55
56void
58{
59 NS_LOG_FUNCTION(this);
60 m_mobility = nullptr;
61 m_netDevice = nullptr;
62 m_channel = nullptr;
63 m_txPsd = nullptr;
64 m_rxPsd = nullptr;
65 m_txPacket = nullptr;
66 m_rxPacket = nullptr;
67 m_phyMacTxEndCallback = MakeNullCallback<void, Ptr<const Packet>>();
68 m_phyMacRxStartCallback = MakeNullCallback<void>();
69 m_phyMacRxEndErrorCallback = MakeNullCallback<void>();
70 m_phyMacRxEndOkCallback = MakeNullCallback<void, Ptr<Packet>>();
72}
73
74/**
75 * \brief Output stream operator
76 * \param os output stream
77 * \param s the state to print
78 * \return an output stream
79 */
80std::ostream&
81operator<<(std::ostream& os, HalfDuplexIdealPhy::State s)
82{
83 switch (s)
84 {
86 os << "IDLE";
87 break;
89 os << "RX";
90 break;
92 os << "TX";
93 break;
94 default:
95 os << "UNKNOWN";
96 break;
97 }
98 return os;
99}
100
101TypeId
103{
104 static TypeId tid =
105 TypeId("ns3::HalfDuplexIdealPhy")
107 .SetGroupName("Spectrum")
108 .AddConstructor<HalfDuplexIdealPhy>()
109 .AddAttribute(
110 "Rate",
111 "The PHY rate used by this device",
112 DataRateValue(DataRate("1Mbps")),
115 .AddTraceSource("TxStart",
116 "Trace fired when a new transmission is started",
118 "ns3::Packet::TracedCallback")
119 .AddTraceSource("TxEnd",
120 "Trace fired when a previously started transmission is finished",
122 "ns3::Packet::TracedCallback")
123 .AddTraceSource("RxStart",
124 "Trace fired when the start of a signal is detected",
126 "ns3::Packet::TracedCallback")
127 .AddTraceSource("RxAbort",
128 "Trace fired when a previously started RX is aborted before time",
130 "ns3::Packet::TracedCallback")
131 .AddTraceSource("RxEndOk",
132 "Trace fired when a previously started RX terminates successfully",
134 "ns3::Packet::TracedCallback")
135 .AddTraceSource("RxEndError",
136 "Trace fired when a previously started RX terminates with an error "
137 "(packet is corrupted)",
139 "ns3::Packet::TracedCallback");
140 return tid;
141}
142
145{
146 NS_LOG_FUNCTION(this);
147 return m_netDevice;
148}
149
152{
153 NS_LOG_FUNCTION(this);
154 return m_mobility;
155}
156
157void
159{
160 NS_LOG_FUNCTION(this << d);
161 m_netDevice = d;
162}
163
164void
166{
167 NS_LOG_FUNCTION(this << m);
168 m_mobility = m;
169}
170
171void
173{
174 NS_LOG_FUNCTION(this << c);
175 m_channel = c;
176}
177
180{
181 if (m_txPsd)
182 {
183 return m_txPsd->GetSpectrumModel();
184 }
185 else
186 {
187 return nullptr;
188 }
189}
190
191void
193{
194 NS_LOG_FUNCTION(this << txPsd);
195 NS_ASSERT(txPsd);
196 m_txPsd = txPsd;
197 NS_LOG_INFO(*txPsd << *m_txPsd);
198}
199
200void
202{
203 NS_LOG_FUNCTION(this << noisePsd);
204 NS_ASSERT(noisePsd);
206}
207
208void
210{
211 NS_LOG_FUNCTION(this << rate);
212 m_rate = rate;
213}
214
217{
218 NS_LOG_FUNCTION(this);
219 return m_rate;
220}
221
222void
224{
225 NS_LOG_FUNCTION(this);
227}
228
229void
231{
232 NS_LOG_FUNCTION(this);
234}
235
236void
238{
239 NS_LOG_FUNCTION(this);
241}
242
243void
245{
246 NS_LOG_FUNCTION(this);
248}
249
252{
253 NS_LOG_FUNCTION(this);
254 return m_antenna;
255}
256
257void
259{
260 NS_LOG_FUNCTION(this << a);
261 m_antenna = a;
262}
263
264void
266{
267 NS_LOG_LOGIC(this << " state: " << m_state << " -> " << newState);
268 m_state = newState;
269}
270
271bool
273{
274 NS_LOG_FUNCTION(this << p);
275 NS_LOG_LOGIC(this << "state: " << m_state);
276
278
279 switch (m_state)
280 {
281 case RX:
282 AbortRx();
283 // fall through
284
285 case IDLE: {
286 m_txPacket = p;
289 Create<HalfDuplexIdealPhySignalParameters>();
290 Time txTimeSeconds = m_rate.CalculateBytesTxTime(p->GetSize());
291 txParams->duration = txTimeSeconds;
292 txParams->txPhy = GetObject<SpectrumPhy>();
293 txParams->txAntenna = m_antenna;
294 txParams->psd = m_txPsd;
295 txParams->data = m_txPacket;
296
297 NS_LOG_LOGIC(this << " tx power: " << 10 * std::log10(Integral(*(txParams->psd))) + 30
298 << " dBm");
299 m_channel->StartTx(txParams);
300 Simulator::Schedule(txTimeSeconds, &HalfDuplexIdealPhy::EndTx, this);
301 }
302 break;
303
304 case TX:
305 return true;
306 }
307 return false;
308}
309
310void
312{
313 NS_LOG_FUNCTION(this);
314 NS_LOG_LOGIC(this << " state: " << m_state);
315
316 NS_ASSERT(m_state == TX);
317
319
321 {
323 }
324
325 m_txPacket = nullptr;
327}
328
329void
331{
332 NS_LOG_FUNCTION(this << spectrumParams);
333 NS_LOG_LOGIC(this << " state: " << m_state);
334 NS_LOG_LOGIC(this << " rx power: " << 10 * std::log10(Integral(*(spectrumParams->psd))) + 30
335 << " dBm");
336
337 // interference will happen regardless of the state of the receiver
338 m_interference.AddSignal(spectrumParams->psd, spectrumParams->duration);
339
340 // the device might start RX only if the signal is of a type understood by this device
341 // this corresponds in real devices to preamble detection
343 DynamicCast<HalfDuplexIdealPhySignalParameters>(spectrumParams);
344 if (rxParams)
345 {
346 // signal is of known type
347 switch (m_state)
348 {
349 case TX:
350 // the PHY will not notice this incoming signal
351 break;
352
353 case RX:
354 // we should check if we should re-sync on a new incoming signal and discard the old one
355 // (somebody calls this the "capture" effect)
356 // criteria considered to do might include the following:
357 // 1) signal strength (e.g., as returned by rxPsd.Norm ())
358 // 2) how much time has passed since previous RX attempt started
359 // if re-sync (capture) is done, then we should call AbortRx ()
360 break;
361
362 case IDLE:
363 // preamble detection and synchronization is supposed to be always successful.
364
365 Ptr<Packet> p = rxParams->data;
367 m_rxPacket = p;
368 m_rxPsd = rxParams->psd;
371 {
372 NS_LOG_LOGIC(this << " calling m_phyMacRxStartCallback");
374 }
375 else
376 {
377 NS_LOG_LOGIC(this << " m_phyMacRxStartCallback is NULL");
378 }
379 m_interference.StartRx(p, rxParams->psd);
380 NS_LOG_LOGIC(this << " scheduling EndRx with delay " << rxParams->duration);
382 Simulator::Schedule(rxParams->duration, &HalfDuplexIdealPhy::EndRx, this);
383
384 break;
385 }
386 }
387 else // rxParams == 0
388 {
389 NS_LOG_LOGIC(this << " signal of unknown type");
390 }
391
392 NS_LOG_LOGIC(this << " state: " << m_state);
393}
394
395void
397{
398 NS_LOG_FUNCTION(this);
399 NS_LOG_LOGIC(this << "state: " << m_state);
400
401 NS_ASSERT(m_state == RX);
405 m_rxPacket = nullptr;
407}
408
409void
411{
412 NS_LOG_FUNCTION(this);
413 NS_LOG_LOGIC(this << " state: " << m_state);
414
415 NS_ASSERT(m_state == RX);
416
417 bool rxOk = m_interference.EndRx();
418
419 if (rxOk)
420 {
423 {
424 NS_LOG_LOGIC(this << " calling m_phyMacRxEndOkCallback");
426 }
427 else
428 {
429 NS_LOG_LOGIC(this << " m_phyMacRxEndOkCallback is NULL");
430 }
431 }
432 else
433 {
436 {
437 NS_LOG_LOGIC(this << " calling m_phyMacRxEndErrorCallback");
439 }
440 else
441 {
442 NS_LOG_LOGIC(this << " m_phyMacRxEndErrorCallback is NULL");
443 }
444 }
445
447 m_rxPacket = nullptr;
448 m_rxPsd = nullptr;
449}
450
451} // namespace ns3
bool IsNull() const
Check for null implementation.
Definition: callback.h:571
Class for representing data rates.
Definition: data-rate.h:89
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:291
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
This PHY layer implementation realizes an ideal OFDM PHY which transmits half-duplex (i....
TracedCallback< Ptr< const Packet > > m_phyRxEndOkTrace
Trace - Tx end (ok)
void SetGenericPhyTxEndCallback(GenericPhyTxEndCallback c)
Set the callback for the end of a TX, as part of the interconnections between the PHY and the MAC.
Ptr< const SpectrumValue > m_rxPsd
Rx power spectral density.
GenericPhyTxEndCallback m_phyMacTxEndCallback
Callback - Tx end.
void SetGenericPhyRxEndErrorCallback(GenericPhyRxEndErrorCallback c)
set the callback for the end of a RX in error, as part of the interconnections between the PHY and th...
void SetRate(DataRate rate)
Set the PHY rate to be used by this PHY.
SpectrumInterference m_interference
Received interference.
DataRate GetRate() const
Get the PHY rate to be used by this PHY.
Ptr< MobilityModel > m_mobility
Mobility model.
void SetChannel(Ptr< SpectrumChannel > c) override
Set the channel attached to this device.
TracedCallback< Ptr< const Packet > > m_phyRxStartTrace
Trace - Rx start.
Ptr< AntennaModel > m_antenna
Antenna model.
TracedCallback< Ptr< const Packet > > m_phyRxAbortTrace
Trace - Rx abort.
Ptr< const SpectrumModel > GetRxSpectrumModel() const override
void SetAntenna(Ptr< AntennaModel > a)
set the AntennaModel to be used
TracedCallback< Ptr< const Packet > > m_phyTxStartTrace
Trace - Tx start.
Ptr< Object > GetAntenna() const override
Get the AntennaModel used by this SpectrumPhy instance for transmission and/or reception.
void SetGenericPhyRxEndOkCallback(GenericPhyRxEndOkCallback c)
set the callback for the successful end of a RX, as part of the interconnections between the PHY and ...
GenericPhyRxStartCallback m_phyMacRxStartCallback
Callback - Rx start.
void StartRx(Ptr< SpectrumSignalParameters > params) override
Notify the SpectrumPhy instance of an incoming signal.
void DoDispose() override
Destructor implementation.
static TypeId GetTypeId()
Get the type ID.
Ptr< SpectrumChannel > m_channel
Channel.
EventId m_endRxEventId
End Rx event.
void ChangeState(State newState)
Change the PHY state.
void EndRx()
End current Rx.
Ptr< Packet > m_rxPacket
Rx packet.
void SetNoisePowerSpectralDensity(Ptr< const SpectrumValue > noisePsd)
Set the Noise Power Spectral Density in power units (Watt, Pascal...) per Hz.
GenericPhyRxEndErrorCallback m_phyMacRxEndErrorCallback
Callback - Rx error.
Ptr< NetDevice > m_netDevice
NetDevice connected to this phy.
Ptr< NetDevice > GetDevice() const override
Get the associated NetDevice instance.
Ptr< Packet > m_txPacket
Tx packet.
void EndTx()
End the current Tx.
TracedCallback< Ptr< const Packet > > m_phyRxEndErrorTrace
Trace - Rx end (error)
void SetDevice(Ptr< NetDevice > d) override
Set the associated NetDevice instance.
Ptr< MobilityModel > GetMobility() const override
Get the associated MobilityModel instance.
void AbortRx()
About current Rx.
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
Set the Power Spectral Density of outgoing signals in power units (Watt, Pascal......
bool StartTx(Ptr< Packet > p)
Start a transmission.
void SetMobility(Ptr< MobilityModel > m) override
Set the mobility model associated with this device.
Ptr< SpectrumValue > m_txPsd
Tx power spectral density.
TracedCallback< Ptr< const Packet > > m_phyTxEndTrace
Trace - Tx end.
void SetGenericPhyRxStartCallback(GenericPhyRxStartCallback c)
Set the callback for the start of RX, as part of the interconnections between the PHY and the MAC.
GenericPhyRxEndOkCallback m_phyMacRxEndOkCallback
Callback - Rx end.
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
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
void AbortRx()
Notify that the PHY has aborted RX.
void SetNoisePowerSpectralDensity(Ptr< const SpectrumValue > noisePsd)
Set the Noise Power Spectral Density.
bool EndRx()
Notify that the RX attempt has ended.
void SetErrorModel(Ptr< SpectrumErrorModel > e)
Set the SpectrumErrorModel to be used.
void StartRx(Ptr< const Packet > p, Ptr< const SpectrumValue > rxPsd)
Notify that the PHY is starting a RX attempt.
void AddSignal(Ptr< const SpectrumValue > spd, const Time duration)
Notify that a new signal is being perceived in the medium.
Abstract base class for Spectrum-aware PHY layers.
Definition: spectrum-phy.h:46
Ptr< const SpectrumModel > GetSpectrumModel() const
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
Ptr< const AttributeAccessor > MakeDataRateAccessor(T1 a1)
Definition: data-rate.h:296
Ptr< const AttributeChecker > MakeDataRateChecker()
Definition: data-rate.cc:31
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
double Integral(const SpectrumValue &arg)
@ IDLE
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:76