A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
half-duplex-ideal-phy.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #include <ns3/object-factory.h>
22 #include <ns3/log.h>
23 #include <cmath>
24 #include <ns3/simulator.h>
25 #include <ns3/trace-source-accessor.h>
26 #include <ns3/packet-burst.h>
27 #include <ns3/callback.h>
28 #include <ns3/antenna-model.h>
29 
30 #include "half-duplex-ideal-phy.h"
32 #include "spectrum-error-model.h"
33 
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("HalfDuplexIdealPhy");
38 
39 NS_OBJECT_ENSURE_REGISTERED (HalfDuplexIdealPhy);
40 
42  : m_mobility (0),
43  m_netDevice (0),
44  m_channel (0),
45  m_txPsd (0),
46  m_state (IDLE)
47 {
48  m_interference.SetErrorModel (CreateObject<ShannonSpectrumErrorModel> ());
49 }
50 
51 
53 {
54 }
55 
56 void
58 {
59  NS_LOG_FUNCTION (this);
60  m_mobility = 0;
61  m_netDevice = 0;
62  m_channel = 0;
63  m_txPsd = 0;
64  m_rxPsd = 0;
65  m_txPacket = 0;
66  m_rxPacket = 0;
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 
80 std::ostream& operator<< (std::ostream& os, HalfDuplexIdealPhy::State s)
81 {
82  switch (s)
83  {
85  os << "IDLE";
86  break;
88  os << "RX";
89  break;
91  os << "TX";
92  break;
93  default:
94  os << "UNKNOWN";
95  break;
96  }
97  return os;
98 }
99 
100 
101 TypeId
103 {
104  static TypeId tid = TypeId ("ns3::HalfDuplexIdealPhy")
106  .AddConstructor<HalfDuplexIdealPhy> ()
107  .AddAttribute ("Rate",
108  "The PHY rate used by this device",
109  DataRateValue (DataRate ("1Mbps")),
110  MakeDataRateAccessor (&HalfDuplexIdealPhy::SetRate,
112  MakeDataRateChecker ())
113  .AddTraceSource ("TxStart",
114  "Trace fired when a new transmission is started",
116  "ns3::Packet::TraceCallback")
117  .AddTraceSource ("TxEnd",
118  "Trace fired when a previosuly started transmission is finished",
120  "ns3::Packet::TraceCallback")
121  .AddTraceSource ("RxStart",
122  "Trace fired when the start of a signal is detected",
124  "ns3::Packet::TraceCallback")
125  .AddTraceSource ("RxAbort",
126  "Trace fired when a previously started RX is aborted before time",
128  "ns3::Packet::TraceCallback")
129  .AddTraceSource ("RxEndOk",
130  "Trace fired when a previosuly started RX terminates successfully",
132  "ns3::Packet::TraceCallback")
133  .AddTraceSource ("RxEndError",
134  "Trace fired when a previosuly started RX terminates with an error (packet is corrupted)",
136  "ns3::Packet::TraceCallback")
137  ;
138  return tid;
139 }
140 
141 
142 
145 {
146  NS_LOG_FUNCTION (this);
147  return m_netDevice;
148 }
149 
150 
153 {
154  NS_LOG_FUNCTION (this);
155  return m_mobility;
156 }
157 
158 
159 void
161 {
162  NS_LOG_FUNCTION (this << d);
163  m_netDevice = d;
164 }
165 
166 
167 void
169 {
170  NS_LOG_FUNCTION (this << m);
171  m_mobility = m;
172 }
173 
174 
175 void
177 {
178  NS_LOG_FUNCTION (this << c);
179  m_channel = c;
180 }
181 
184 {
185  if (m_txPsd)
186  {
187  return m_txPsd->GetSpectrumModel ();
188  }
189  else
190  {
191  return 0;
192  }
193 }
194 
195 void
197 {
198  NS_LOG_FUNCTION (this << txPsd);
199  NS_ASSERT (txPsd);
200  m_txPsd = txPsd;
201  NS_LOG_INFO ( *txPsd << *m_txPsd);
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION (this << noisePsd);
208  NS_ASSERT (noisePsd);
210 }
211 
212 void
214 {
215  NS_LOG_FUNCTION (this << rate);
216  m_rate = rate;
217 }
218 
219 DataRate
221 {
222  NS_LOG_FUNCTION (this);
223  return m_rate;
224 }
225 
226 
227 void
229 {
230  NS_LOG_FUNCTION (this);
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION (this);
239 }
240 
241 
242 void
244 {
245  NS_LOG_FUNCTION (this);
247 }
248 
249 
250 void
252 {
253  NS_LOG_FUNCTION (this);
255 }
256 
259 {
260  NS_LOG_FUNCTION (this);
261  return m_antenna;
262 }
263 
264 void
266 {
267  NS_LOG_FUNCTION (this << a);
268  m_antenna = a;
269 }
270 
271 void
273 {
274  NS_LOG_LOGIC (this << " state: " << m_state << " -> " << newState);
275  m_state = newState;
276 }
277 
278 bool
280 {
281  NS_LOG_FUNCTION (this << p);
282  NS_LOG_LOGIC (this << "state: " << m_state);
283 
284  m_phyTxStartTrace (p);
285 
286  switch (m_state)
287  {
288  case RX:
289  AbortRx ();
290  // fall through
291 
292  case IDLE:
293  {
294  m_txPacket = p;
295  ChangeState (TX);
296  Ptr<HalfDuplexIdealPhySignalParameters> txParams = Create<HalfDuplexIdealPhySignalParameters> ();
297  Time txTimeSeconds = m_rate.CalculateBytesTxTime (p->GetSize ());
298  txParams->duration = txTimeSeconds;
299  txParams->txPhy = GetObject<SpectrumPhy> ();
300  txParams->txAntenna = m_antenna;
301  txParams->psd = m_txPsd;
302  txParams->data = m_txPacket;
303 
304  NS_LOG_LOGIC (this << " tx power: " << 10 * std::log10 (Integral (*(txParams->psd))) + 30 << " dBm");
305  m_channel->StartTx (txParams);
306  Simulator::Schedule (txTimeSeconds, &HalfDuplexIdealPhy::EndTx, this);
307  }
308  break;
309 
310  case TX:
311 
312  return true;
313  break;
314  }
315  return false;
316 }
317 
318 
319 void
321 {
322  NS_LOG_FUNCTION (this);
323  NS_LOG_LOGIC (this << " state: " << m_state);
324 
325  NS_ASSERT (m_state == TX);
326 
328 
330  {
332  }
333 
334  m_txPacket = 0;
335  ChangeState (IDLE);
336 }
337 
338 
339 void
341 {
342  NS_LOG_FUNCTION (this << spectrumParams);
343  NS_LOG_LOGIC (this << " state: " << m_state);
344  NS_LOG_LOGIC (this << " rx power: " << 10 * std::log10 (Integral (*(spectrumParams->psd))) + 30 << " dBm");
345 
346  // interference will happen regardless of the state of the receiver
347  m_interference.AddSignal (spectrumParams->psd, spectrumParams->duration);
348 
349  // the device might start RX only if the signal is of a type understood by this device
350  // this corresponds in real devices to preamble detection
351  Ptr<HalfDuplexIdealPhySignalParameters> rxParams = DynamicCast<HalfDuplexIdealPhySignalParameters> (spectrumParams);
352  if (rxParams != 0)
353  {
354  // signal is of known type
355  switch (m_state)
356  {
357  case TX:
358  // the PHY will not notice this incoming signal
359  break;
360 
361  case RX:
362  // we should check if we should re-sync on a new incoming signal and discard the old one
363  // (somebody calls this the "capture" effect)
364  // criteria considered to do might include the following:
365  // 1) signal strength (e.g., as returned by rxPsd.Norm ())
366  // 2) how much time has passed since previous RX attempt started
367  // if re-sync (capture) is done, then we should call AbortRx ()
368  break;
369 
370  case IDLE:
371  // preamble detection and synchronization is supposed to be always successful.
372 
373  Ptr<Packet> p = rxParams->data;
374  m_phyRxStartTrace (p);
375  m_rxPacket = p;
376  m_rxPsd = rxParams->psd;
377  ChangeState (RX);
379  {
380  NS_LOG_LOGIC (this << " calling m_phyMacRxStartCallback");
382  }
383  else
384  {
385  NS_LOG_LOGIC (this << " m_phyMacRxStartCallback is NULL");
386  }
387  m_interference.StartRx (p, rxParams->psd);
388  NS_LOG_LOGIC (this << " scheduling EndRx with delay " << rxParams->duration);
389  m_endRxEventId = Simulator::Schedule (rxParams->duration, &HalfDuplexIdealPhy::EndRx, this);
390 
391  break;
392 
393  }
394  }
395  else // rxParams == 0
396  {
397  NS_LOG_LOGIC (this << " signal of unknown type");
398  }
399 
400  NS_LOG_LOGIC (this << " state: " << m_state);
401 }
402 
403 
404 void
406 {
407  NS_LOG_FUNCTION (this);
408  NS_LOG_LOGIC (this << "state: " << m_state);
409 
410  NS_ASSERT (m_state == RX);
414  m_rxPacket = 0;
415  ChangeState (IDLE);
416 }
417 
418 
419 void
421 {
422  NS_LOG_FUNCTION (this);
423  NS_LOG_LOGIC (this << " state: " << m_state);
424 
425  NS_ASSERT (m_state == RX);
426 
427  bool rxOk = m_interference.EndRx ();
428 
429  if (rxOk)
430  {
433  {
434  NS_LOG_LOGIC (this << " calling m_phyMacRxEndOkCallback");
436  }
437  else
438  {
439  NS_LOG_LOGIC (this << " m_phyMacRxEndOkCallback is NULL");
440  }
441  }
442  else
443  {
446  {
447  NS_LOG_LOGIC (this << " calling m_phyMacRxEndErrorCallback");
449  }
450  else
451  {
452  NS_LOG_LOGIC (this << " m_phyMacRxEndErrorCallback is NULL");
453  }
454  }
455 
456  ChangeState (IDLE);
457  m_rxPacket = 0;
458  m_rxPsd = 0;
459 }
460 
461 
462 
463 } // namespace ns3
void SetRate(DataRate rate)
Set the PHY rate to be used by this PHY.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void SetGenericPhyTxEndCallback(GenericPhyTxEndCallback c)
Set the callback for the end of a TX, as part of the interconnections between the PHY and the MAC...
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Ptr< MobilityModel > m_mobility
Mobility model.
Abstract base class for Spectrum-aware PHY layers.
Definition: spectrum-phy.h:45
double Integral(const SpectrumValue &arg)
bool StartTx(Ptr< Packet > p)
Start a transmission.
Ptr< NetDevice > GetDevice() const
Get the associated NetDevice instance.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:235
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
EventId m_endRxEventId
End Rx event.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
void SetNoisePowerSpectralDensity(Ptr< const SpectrumValue > noisePsd)
Set the Noise Power Spectral Density.
void SetGenericPhyRxStartCallback(GenericPhyRxStartCallback c)
Set the callback for the start of RX, as part of the interconnections between the PHY and the MAC...
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
SpectrumInterference m_interference
Received interference.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
GenericPhyRxEndErrorCallback m_phyMacRxEndErrorCallback
Callback - Rx error.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:340
void AddSignal(Ptr< const SpectrumValue > spd, const Time duration)
Notify that a new signal is being perceived in the medium.
Ptr< MobilityModel > GetMobility()
Get the associated MobilityModel instance.
Ptr< const SpectrumValue > m_rxPsd
Rx power spectral density.
TracedCallback< Ptr< const Packet > > m_phyTxEndTrace
Trace - Tx end.
void AbortRx()
Notify that the PHY has aborted RX.
void StartRx(Ptr< SpectrumSignalParameters > params)
Notify the SpectrumPhy instance of an incoming signal.
void StartRx(Ptr< const Packet > p, Ptr< const SpectrumValue > rxPsd)
Notify that the PHY is starting a RX attempt.
TracedCallback< Ptr< const Packet > > m_phyRxStartTrace
Trace - Rx start.
TracedCallback< Ptr< const Packet > > m_phyRxAbortTrace
Trace - Rx abort.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Class for representing data rates.
Definition: data-rate.h:88
TracedCallback< Ptr< const Packet > > m_phyTxStartTrace
Trace - Tx start.
void SetDevice(Ptr< NetDevice > d)
Set the associated NetDevice instance.
void SetAntenna(Ptr< AntennaModel > a)
set the AntennaModel to be used
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1221
Ptr< SpectrumValue > m_txPsd
Tx power spectral density.
void SetGenericPhyRxEndOkCallback(GenericPhyRxEndOkCallback c)
set the callback for the successful end of a RX, as part of the interconnections between the PHY and ...
void AbortRx()
About current Rx.
GenericPhyRxEndOkCallback m_phyMacRxEndOkCallback
Callback - Rx end.
Ptr< const SpectrumModel > GetSpectrumModel() const
virtual void DoDispose(void)
Destructor implementation.
Ptr< AntennaModel > GetRxAntenna()
Get the AntennaModel used by the NetDevice for reception.
Ptr< SpectrumChannel > m_channel
Channel.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
void SetNoisePowerSpectralDensity(Ptr< const SpectrumValue > noisePsd)
Set the Noise Power Spectral Density in power units (Watt, Pascal...) per Hz.
void EndRx()
End current Rx.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
void ChangeState(State newState)
Change the PHY state.
bool EndRx()
Notify that the RX attempt has ended.
Ptr< NetDevice > m_netDevice
NetDevice connected to theis phy.
Ptr< Packet > m_rxPacket
Rx packet.
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...
Ptr< AntennaModel > m_antenna
Antenna model.
TracedCallback< Ptr< const Packet > > m_phyRxEndErrorTrace
Trace - Rx end (error)
AttributeValue implementation for DataRate.
GenericPhyRxStartCallback m_phyMacRxStartCallback
Callback - Rx start.
static TypeId GetTypeId(void)
Get the type ID.
Ptr< Packet > m_txPacket
Tx packet.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
void SetChannel(Ptr< SpectrumChannel > c)
Set the channel attached to this device.
void EndTx()
End the current Tx.
void SetErrorModel(Ptr< SpectrumErrorModel > e)
Set the SpectrumErrorModel to be used.
DataRate GetRate() const
Get the PHY rate to be used by this PHY.
TracedCallback< Ptr< const Packet > > m_phyRxEndOkTrace
Trace - Tx end (ok)
void SetMobility(Ptr< MobilityModel > m)
Set the mobility model associated with this device.
GenericPhyTxEndCallback m_phyMacTxEndCallback
Callback - Tx end.
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
Set the Power Spectral Density of outgoing signals in power units (Watt, Pascal...) per Hz.
a unique identifier for an interface.
Definition: type-id.h:58
Ptr< const SpectrumModel > GetRxSpectrumModel() const
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:827