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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
10
13
14#include <ns3/antenna-model.h>
15#include <ns3/callback.h>
16#include <ns3/log.h>
17#include <ns3/object-factory.h>
18#include <ns3/packet-burst.h>
19#include <ns3/simulator.h>
20#include <ns3/trace-source-accessor.h>
21
22#include <cmath>
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("HalfDuplexIdealPhy");
28
29NS_OBJECT_ENSURE_REGISTERED(HalfDuplexIdealPhy);
30
32 : m_mobility(nullptr),
33 m_netDevice(nullptr),
34 m_channel(nullptr),
35 m_txPsd(nullptr),
36 m_state(IDLE)
37{
39}
40
44
45void
62
63/**
64 * \brief Output stream operator
65 * \param os output stream
66 * \param s the state to print
67 * \return an output stream
68 */
69std::ostream&
70operator<<(std::ostream& os, HalfDuplexIdealPhy::State s)
71{
72 switch (s)
73 {
75 os << "IDLE";
76 break;
78 os << "RX";
79 break;
81 os << "TX";
82 break;
83 default:
84 os << "UNKNOWN";
85 break;
86 }
87 return os;
88}
89
90TypeId
92{
93 static TypeId tid =
94 TypeId("ns3::HalfDuplexIdealPhy")
96 .SetGroupName("Spectrum")
97 .AddConstructor<HalfDuplexIdealPhy>()
98 .AddAttribute(
99 "Rate",
100 "The PHY rate used by this device",
101 DataRateValue(DataRate("1Mbps")),
104 .AddTraceSource("TxStart",
105 "Trace fired when a new transmission is started",
107 "ns3::Packet::TracedCallback")
108 .AddTraceSource("TxEnd",
109 "Trace fired when a previously started transmission is finished",
111 "ns3::Packet::TracedCallback")
112 .AddTraceSource("RxStart",
113 "Trace fired when the start of a signal is detected",
115 "ns3::Packet::TracedCallback")
116 .AddTraceSource("RxAbort",
117 "Trace fired when a previously started RX is aborted before time",
119 "ns3::Packet::TracedCallback")
120 .AddTraceSource("RxEndOk",
121 "Trace fired when a previously started RX terminates successfully",
123 "ns3::Packet::TracedCallback")
124 .AddTraceSource("RxEndError",
125 "Trace fired when a previously started RX terminates with an error "
126 "(packet is corrupted)",
128 "ns3::Packet::TracedCallback");
129 return tid;
130}
131
134{
135 NS_LOG_FUNCTION(this);
136 return m_netDevice;
137}
138
141{
142 NS_LOG_FUNCTION(this);
143 return m_mobility;
144}
145
146void
152
153void
159
160void
166
169{
170 if (m_txPsd)
171 {
172 return m_txPsd->GetSpectrumModel();
173 }
174 else
175 {
176 return nullptr;
177 }
178}
179
180void
182{
183 NS_LOG_FUNCTION(this << txPsd);
184 NS_ASSERT(txPsd);
185 m_txPsd = txPsd;
186 NS_LOG_INFO(*txPsd << *m_txPsd);
187}
188
189void
196
197void
199{
200 NS_LOG_FUNCTION(this << rate);
201 m_rate = rate;
202}
203
206{
207 NS_LOG_FUNCTION(this);
208 return m_rate;
209}
210
211void
217
218void
224
225void
231
232void
238
241{
242 NS_LOG_FUNCTION(this);
243 return m_antenna;
244}
245
246void
252
253void
255{
256 NS_LOG_LOGIC(this << " state: " << m_state << " -> " << newState);
257 m_state = newState;
258}
259
260bool
262{
263 NS_LOG_FUNCTION(this << p);
264 NS_LOG_LOGIC(this << "state: " << m_state);
265
267
268 switch (m_state)
269 {
270 case RX:
271 AbortRx();
272 // fall through
273
274 case IDLE: {
275 m_txPacket = p;
279 Time txTimeSeconds = m_rate.CalculateBytesTxTime(p->GetSize());
280 txParams->duration = txTimeSeconds;
281 txParams->txPhy = GetObject<SpectrumPhy>();
282 txParams->txAntenna = m_antenna;
283 txParams->psd = m_txPsd;
284 txParams->data = m_txPacket;
285
286 NS_LOG_LOGIC(this << " tx power: " << 10 * std::log10(Integral(*(txParams->psd))) + 30
287 << " dBm");
288 m_channel->StartTx(txParams);
289 Simulator::Schedule(txTimeSeconds, &HalfDuplexIdealPhy::EndTx, this);
290 }
291 break;
292
293 case TX:
294 return true;
295 }
296 return false;
297}
298
299void
301{
302 NS_LOG_FUNCTION(this);
303 NS_LOG_LOGIC(this << " state: " << m_state);
304
305 NS_ASSERT(m_state == TX);
306
308
310 {
312 }
313
314 m_txPacket = nullptr;
316}
317
318void
320{
321 NS_LOG_FUNCTION(this << spectrumParams);
322 NS_LOG_LOGIC(this << " state: " << m_state);
323 NS_LOG_LOGIC(this << " rx power: " << 10 * std::log10(Integral(*(spectrumParams->psd))) + 30
324 << " dBm");
325
326 // interference will happen regardless of the state of the receiver
327 m_interference.AddSignal(spectrumParams->psd, spectrumParams->duration);
328
329 // the device might start RX only if the signal is of a type understood by this device
330 // this corresponds in real devices to preamble detection
333 if (rxParams)
334 {
335 // signal is of known type
336 switch (m_state)
337 {
338 case TX:
339 // the PHY will not notice this incoming signal
340
341 case RX:
342 // we should check if we should re-sync on a new incoming signal and discard the old one
343 // (somebody calls this the "capture" effect)
344 // criteria considered to do might include the following:
345 // 1) signal strength (e.g., as returned by rxPsd.Norm ())
346 // 2) how much time has passed since previous RX attempt started
347 // if re-sync (capture) is done, then we should call AbortRx ()
348 break;
349
350 case IDLE:
351 // preamble detection and synchronization is supposed to be always successful.
352
353 Ptr<Packet> p = rxParams->data;
355 m_rxPacket = p;
356 m_rxPsd = rxParams->psd;
359 {
360 NS_LOG_LOGIC(this << " calling m_phyMacRxStartCallback");
362 }
363 else
364 {
365 NS_LOG_LOGIC(this << " m_phyMacRxStartCallback is NULL");
366 }
367 m_interference.StartRx(p, rxParams->psd);
368 NS_LOG_LOGIC(this << " scheduling EndRx with delay " << rxParams->duration);
370 Simulator::Schedule(rxParams->duration, &HalfDuplexIdealPhy::EndRx, this);
371
372 break;
373 }
374 }
375 else // rxParams == 0
376 {
377 NS_LOG_LOGIC(this << " signal of unknown type");
378 }
379
380 NS_LOG_LOGIC(this << " state: " << m_state);
381}
382
383void
385{
386 NS_LOG_FUNCTION(this);
387 NS_LOG_LOGIC(this << "state: " << m_state);
388
389 NS_ASSERT(m_state == RX);
393 m_rxPacket = nullptr;
395}
396
397void
399{
400 NS_LOG_FUNCTION(this);
401 NS_LOG_LOGIC(this << " state: " << m_state);
402
403 NS_ASSERT(m_state == RX);
404
405 bool rxOk = m_interference.EndRx();
406
407 if (rxOk)
408 {
411 {
412 NS_LOG_LOGIC(this << " calling m_phyMacRxEndOkCallback");
414 }
415 else
416 {
417 NS_LOG_LOGIC(this << " m_phyMacRxEndOkCallback is NULL");
418 }
419 }
420 else
421 {
424 {
425 NS_LOG_LOGIC(this << " calling m_phyMacRxEndErrorCallback");
427 }
428 else
429 {
430 NS_LOG_LOGIC(this << " m_phyMacRxEndErrorCallback is NULL");
431 }
432 }
433
435 m_rxPacket = nullptr;
436 m_rxPsd = nullptr;
437}
438
439} // namespace ns3
bool IsNull() const
Check for null implementation.
Definition callback.h:555
Class for representing data rates.
Definition data-rate.h:78
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition data-rate.cc:220
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
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.
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
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
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.
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
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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.
Ptr< const AttributeAccessor > MakeDataRateAccessor(T1 a1)
Definition data-rate.h:285
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
double Integral(const SpectrumValue &arg)
Ptr< const AttributeChecker > MakeDataRateChecker()
Definition data-rate.cc:20
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
@ IDLE
Channel is IDLE, no packet is being transmitted.