A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
yans-wifi-phy.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "yans-wifi-phy.h"
22 #include "yans-wifi-channel.h"
23 #include "wifi-mode.h"
24 #include "wifi-preamble.h"
25 #include "wifi-phy-state-helper.h"
26 #include "error-rate-model.h"
27 #include "ns3/simulator.h"
28 #include "ns3/packet.h"
29 #include "ns3/assert.h"
30 #include "ns3/log.h"
31 #include "ns3/double.h"
32 #include "ns3/uinteger.h"
33 #include "ns3/enum.h"
34 #include "ns3/pointer.h"
35 #include "ns3/net-device.h"
36 #include "ns3/trace-source-accessor.h"
37 #include <cmath>
38 
39 NS_LOG_COMPONENT_DEFINE ("YansWifiPhy");
40 
41 namespace ns3 {
42 
43 NS_OBJECT_ENSURE_REGISTERED (YansWifiPhy);
44 
45 TypeId
46 YansWifiPhy::GetTypeId (void)
47 {
48  static TypeId tid = TypeId ("ns3::YansWifiPhy")
49  .SetParent<WifiPhy> ()
50  .AddConstructor<YansWifiPhy> ()
51  .AddAttribute ("EnergyDetectionThreshold",
52  "The energy of a received signal should be higher than "
53  "this threshold (dbm) to allow the PHY layer to detect the signal.",
54  DoubleValue (-96.0),
55  MakeDoubleAccessor (&YansWifiPhy::SetEdThreshold,
56  &YansWifiPhy::GetEdThreshold),
57  MakeDoubleChecker<double> ())
58  .AddAttribute ("CcaMode1Threshold",
59  "The energy of a received signal should be higher than "
60  "this threshold (dbm) to allow the PHY layer to declare CCA BUSY state",
61  DoubleValue (-99.0),
62  MakeDoubleAccessor (&YansWifiPhy::SetCcaMode1Threshold,
63  &YansWifiPhy::GetCcaMode1Threshold),
64  MakeDoubleChecker<double> ())
65  .AddAttribute ("TxGain",
66  "Transmission gain (dB).",
67  DoubleValue (1.0),
68  MakeDoubleAccessor (&YansWifiPhy::SetTxGain,
69  &YansWifiPhy::GetTxGain),
70  MakeDoubleChecker<double> ())
71  .AddAttribute ("RxGain",
72  "Reception gain (dB).",
73  DoubleValue (1.0),
74  MakeDoubleAccessor (&YansWifiPhy::SetRxGain,
75  &YansWifiPhy::GetRxGain),
76  MakeDoubleChecker<double> ())
77  .AddAttribute ("TxPowerLevels",
78  "Number of transmission power levels available between "
79  "TxPowerStart and TxPowerEnd included.",
80  UintegerValue (1),
81  MakeUintegerAccessor (&YansWifiPhy::m_nTxPower),
82  MakeUintegerChecker<uint32_t> ())
83  .AddAttribute ("TxPowerEnd",
84  "Maximum available transmission level (dbm).",
85  DoubleValue (16.0206),
86  MakeDoubleAccessor (&YansWifiPhy::SetTxPowerEnd,
87  &YansWifiPhy::GetTxPowerEnd),
88  MakeDoubleChecker<double> ())
89  .AddAttribute ("TxPowerStart",
90  "Minimum available transmission level (dbm).",
91  DoubleValue (16.0206),
92  MakeDoubleAccessor (&YansWifiPhy::SetTxPowerStart,
93  &YansWifiPhy::GetTxPowerStart),
94  MakeDoubleChecker<double> ())
95  .AddAttribute ("RxNoiseFigure",
96  "Loss (dB) in the Signal-to-Noise-Ratio due to non-idealities in the receiver."
97  " According to Wikipedia (http://en.wikipedia.org/wiki/Noise_figure), this is "
98  "\"the difference in decibels (dB) between"
99  " the noise output of the actual receiver to the noise output of an "
100  " ideal receiver with the same overall gain and bandwidth when the receivers "
101  " are connected to sources at the standard noise temperature T0 (usually 290 K)\"."
102  " For",
103  DoubleValue (7),
104  MakeDoubleAccessor (&YansWifiPhy::SetRxNoiseFigure,
105  &YansWifiPhy::GetRxNoiseFigure),
106  MakeDoubleChecker<double> ())
107  .AddAttribute ("State", "The state of the PHY layer",
108  PointerValue (),
109  MakePointerAccessor (&YansWifiPhy::m_state),
110  MakePointerChecker<WifiPhyStateHelper> ())
111  .AddAttribute ("ChannelSwitchDelay",
112  "Delay between two short frames transmitted on different frequencies.",
113  TimeValue (MicroSeconds (250)),
114  MakeTimeAccessor (&YansWifiPhy::m_channelSwitchDelay),
115  MakeTimeChecker ())
116  .AddAttribute ("ChannelNumber",
117  "Channel center frequency = Channel starting frequency + 5 MHz * (nch - 1)",
118  UintegerValue (1),
119  MakeUintegerAccessor (&YansWifiPhy::SetChannelNumber,
121  MakeUintegerChecker<uint16_t> ())
122 
123  ;
124  return tid;
125 }
126 
127 YansWifiPhy::YansWifiPhy ()
128  : m_channelNumber (1),
129  m_endRxEvent (),
130  m_channelStartingFrequency (0)
131 {
132  NS_LOG_FUNCTION (this);
133  m_random = CreateObject<UniformRandomVariable> ();
134  m_state = CreateObject<WifiPhyStateHelper> ();
135 }
136 
137 YansWifiPhy::~YansWifiPhy ()
138 {
139  NS_LOG_FUNCTION (this);
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (this);
146  m_channel = 0;
147  m_deviceRateSet.clear ();
148  m_device = 0;
149  m_mobility = 0;
150  m_state = 0;
151 }
152 
153 void
154 YansWifiPhy::ConfigureStandard (enum WifiPhyStandard standard)
155 {
156  NS_LOG_FUNCTION (this << standard);
157  switch (standard)
158  {
160  Configure80211a ();
161  break;
163  Configure80211b ();
164  break;
166  Configure80211g ();
167  break;
169  Configure80211_10Mhz ();
170  break;
172  Configure80211_5Mhz ();
173  break;
175  ConfigureHolland ();
176  break;
178  Configure80211p_CCH ();
179  break;
181  Configure80211p_SCH ();
182  break;
183  default:
184  NS_ASSERT (false);
185  break;
186  }
187 }
188 
189 Time
190 YansWifiPhy::GetSwitchingDelay (void)
191 {
192  return m_channelSwitchDelay;
193 }
194 
195 void
196 YansWifiPhy::SetRxNoiseFigure (double noiseFigureDb)
197 {
198  NS_LOG_FUNCTION (this << noiseFigureDb);
199  m_interference.SetNoiseFigure (DbToRatio (noiseFigureDb));
200 }
201 void
202 YansWifiPhy::SetTxPowerStart (double start)
203 {
204  NS_LOG_FUNCTION (this << start);
205  m_txPowerBaseDbm = start;
206 }
207 void
208 YansWifiPhy::SetTxPowerEnd (double end)
209 {
210  NS_LOG_FUNCTION (this << end);
211  m_txPowerEndDbm = end;
212 }
213 void
214 YansWifiPhy::SetNTxPower (uint32_t n)
215 {
216  NS_LOG_FUNCTION (this << n);
217  m_nTxPower = n;
218 }
219 void
220 YansWifiPhy::SetTxGain (double gain)
221 {
222  NS_LOG_FUNCTION (this << gain);
223  m_txGainDb = gain;
224 }
225 void
226 YansWifiPhy::SetRxGain (double gain)
227 {
228  NS_LOG_FUNCTION (this << gain);
229  m_rxGainDb = gain;
230 }
231 void
232 YansWifiPhy::SetEdThreshold (double threshold)
233 {
234  NS_LOG_FUNCTION (this << threshold);
235  m_edThresholdW = DbmToW (threshold);
236 }
237 void
238 YansWifiPhy::SetCcaMode1Threshold (double threshold)
239 {
240  NS_LOG_FUNCTION (this << threshold);
241  m_ccaMode1ThresholdW = DbmToW (threshold);
242 }
243 void
244 YansWifiPhy::SetErrorRateModel (Ptr<ErrorRateModel> rate)
245 {
246  m_interference.SetErrorRateModel (rate);
247 }
248 void
249 YansWifiPhy::SetDevice (Ptr<Object> device)
250 {
251  m_device = device;
252 }
253 void
254 YansWifiPhy::SetMobility (Ptr<Object> mobility)
255 {
256  m_mobility = mobility;
257 }
258 
259 double
260 YansWifiPhy::GetRxNoiseFigure (void) const
261 {
262  return RatioToDb (m_interference.GetNoiseFigure ());
263 }
264 double
265 YansWifiPhy::GetTxPowerStart (void) const
266 {
267  return m_txPowerBaseDbm;
268 }
269 double
270 YansWifiPhy::GetTxPowerEnd (void) const
271 {
272  return m_txPowerEndDbm;
273 }
274 double
275 YansWifiPhy::GetTxGain (void) const
276 {
277  return m_txGainDb;
278 }
279 double
280 YansWifiPhy::GetRxGain (void) const
281 {
282  return m_rxGainDb;
283 }
284 
285 double
286 YansWifiPhy::GetEdThreshold (void) const
287 {
288  return WToDbm (m_edThresholdW);
289 }
290 
291 double
292 YansWifiPhy::GetCcaMode1Threshold (void) const
293 {
294  return WToDbm (m_ccaMode1ThresholdW);
295 }
296 
297 Ptr<ErrorRateModel>
298 YansWifiPhy::GetErrorRateModel (void) const
299 {
300  return m_interference.GetErrorRateModel ();
301 }
302 Ptr<Object>
303 YansWifiPhy::GetDevice (void) const
304 {
305  return m_device;
306 }
307 Ptr<Object>
308 YansWifiPhy::GetMobility (void)
309 {
310  return m_mobility;
311 }
312 
313 double
314 YansWifiPhy::CalculateSnr (WifiMode txMode, double ber) const
315 {
316  return m_interference.GetErrorRateModel ()->CalculateSnr (txMode, ber);
317 }
318 
320 YansWifiPhy::GetChannel (void) const
321 {
322  return m_channel;
323 }
324 void
325 YansWifiPhy::SetChannel (Ptr<YansWifiChannel> channel)
326 {
327  m_channel = channel;
328  m_channel->Add (this);
329 }
330 
331 void
333 {
334  if (Simulator::Now () == Seconds (0) || IsStateSwitching())
335  {
336  // this is not channel switch, this is initialization
337  NS_LOG_DEBUG ("start at channel " << nch);
338  m_channelNumber = nch;
339  return;
340  }
341 
343  switch (m_state->GetState ())
344  {
345  case YansWifiPhy::RX:
346  NS_LOG_DEBUG ("drop packet because of channel switching while reception");
347  m_endRxEvent.Cancel ();
348  goto switchChannel;
349  break;
350  case YansWifiPhy::TX:
351  NS_LOG_DEBUG ("channel switching postponed until end of current transmission");
353  break;
355  case YansWifiPhy::IDLE:
356  goto switchChannel;
357  break;
358  default:
359  NS_ASSERT (false);
360  break;
361  }
362 
363  return;
364 
365 switchChannel:
366 
367  NS_LOG_DEBUG ("switching channel " << m_channelNumber << " -> " << nch);
368  m_state->SwitchToChannelSwitching (m_channelSwitchDelay, nch);
369  m_interference.EraseEvents ();
370  /*
371  * Needed here to be able to correctly sensed the medium for the first
372  * time after the switching. The actual switching is not performed until
373  * after m_channelSwitchDelay. Packets received during the switching
374  * state are added to the event list and are employed later to figure
375  * out the state of the medium after the switching.
376  */
377  m_channelNumber = nch;
378  if (!m_handoffEndedCallback.IsNull())
379  Simulator::Schedule (m_channelSwitchDelay, &YansWifiPhy::m_handoffEndedCallback, this);
380 }
381 
382 void
384 {
385 
387  switch (m_state->GetState ())
388  {
389  case YansWifiPhy::RX:
390  NS_LOG_DEBUG ("channel sensing postponed until end of current reception");
392  break;
393  case YansWifiPhy::TX:
394  NS_LOG_DEBUG ("channel sensing postponed until end of current transmission");
396  break;
398  case YansWifiPhy::IDLE:
399  goto startSensing;
400  break;
401  default:
402  NS_ASSERT (false);
403  break;
404  }
405 
406  return;
407 
408 startSensing:
409 
410  NS_LOG_DEBUG ("sensing started for duration " << duration);
411  m_state->SwitchToChannelSensing (duration);
412  m_interference.EraseEvents ();
413  Simulator::Schedule (duration, &YansWifiPhy::m_senseEndedCallback, this);
414  //TODO 2: must see what happens to packets received during sensing
415  /*
416  * Needed here to be able to correctly sensed the medium for the first
417  * time after the switching. The actual switching is not performed until
418  * after m_channelSwitchDelay. Packets received during the switching
419  * state are added to the event list and are employed later to figure
420  * out the state of the medium after the switching.
421  */
422  //m_channelNumber = nch;
423 }
424 
425 uint16_t
427 {
428  return m_channelNumber;
429 }
430 
431 double
433 {
435 }
436 
437 void
439 {
440  m_state->SetReceiveOkCallback (callback);
441 }
442 void
444 {
445  m_state->SetReceiveErrorCallback (callback);
446 }
447 void
449 {
450  m_senseEndedCallback = callback;
451 }
452 void
454 {
455  m_handoffEndedCallback = callback;
456 }
457 void
458 YansWifiPhy::StartReceivePacket (Ptr<Packet> packet,
459  double rxPowerDbm,
460  WifiMode txMode,
461  enum WifiPreamble preamble)
462 {
463  NS_LOG_FUNCTION (this << packet << rxPowerDbm << txMode << preamble);
464  rxPowerDbm += m_rxGainDb;
465  double rxPowerW = DbmToW (rxPowerDbm);
466  Time rxDuration = CalculateTxDuration (packet->GetSize (), txMode, preamble);
467  Time endRx = Simulator::Now () + rxDuration;
468 
470  event = m_interference.Add (packet->GetSize (),
471  txMode,
472  preamble,
473  rxDuration,
474  rxPowerW);
475 
476  switch (m_state->GetState ())
477  {
479  NS_LOG_DEBUG ("drop packet because of channel switching");
480  NotifyRxDrop (packet);
481  /*
482  * Packets received on the upcoming channel are added to the event list
483  * during the switching state. This way the medium can be correctly sensed
484  * when the device listens to the channel for the first time after the
485  * switching e.g. after channel switching, the channel may be sensed as
486  * busy due to other devices' tramissions started before the end of
487  * the switching.
488  */
489  if (endRx > Simulator::Now () + m_state->GetDelayUntilIdle ())
490  {
491  // that packet will be noise _after_ the completion of the
492  // channel switching.
493  goto maybeCcaBusy;
494  }
495  break;
497  NS_LOG_DEBUG ("drop packet because of channel sensing on channel " << GetChannelNumber());
498  NotifyRxDrop(packet);
499  if (endRx > Simulator::Now() + m_state->GetDelayUntilIdle ())
500  {
501  goto maybeCcaBusy;
502  }
503  break;
504  case YansWifiPhy::RX:
505  NS_LOG_DEBUG ("drop packet because already in Rx (power=" <<
506  rxPowerW << "W)");
507  NotifyRxDrop (packet);
508  if (endRx > Simulator::Now () + m_state->GetDelayUntilIdle ())
509  {
510  // that packet will be noise _after_ the reception of the
511  // currently-received packet.
512  goto maybeCcaBusy;
513  }
514  break;
515  case YansWifiPhy::TX:
516  NS_LOG_DEBUG ("drop packet because already in Tx (power=" <<
517  rxPowerW << "W)");
518  NotifyRxDrop (packet);
519  if (endRx > Simulator::Now () + m_state->GetDelayUntilIdle ())
520  {
521  // that packet will be noise _after_ the transmission of the
522  // currently-transmitted packet.
523  goto maybeCcaBusy;
524  }
525  break;
527  case YansWifiPhy::IDLE:
528  if (rxPowerW > m_edThresholdW)
529  {
530  NS_LOG_DEBUG ("sync to signal (power=" << rxPowerW << "W)");
531  // sync to signal
532  m_state->SwitchToRx (rxDuration);
533  NS_ASSERT (m_endRxEvent.IsExpired ());
534  NotifyRxBegin (packet);
535  m_interference.NotifyRxStart ();
536  m_endRxEvent = Simulator::Schedule (rxDuration, &YansWifiPhy::EndReceive, this,
537  packet,
538  event);
539  }
540  else
541  {
542  NS_LOG_DEBUG ("drop packet because signal power too Small (" <<
543  rxPowerW << "<" << m_edThresholdW << ")");
544  NotifyRxDrop (packet);
545  goto maybeCcaBusy;
546  }
547  break;
548  }
549 
550  return;
551 
552 maybeCcaBusy:
553  // We are here because we have received the first bit of a packet and we are
554  // not going to be able to synchronize on it
555  // In this model, CCA becomes busy when the aggregation of all signals as
556  // tracked by the InterferenceHelper class is higher than the CcaBusyThreshold
557 
558  Time delayUntilCcaEnd = m_interference.GetEnergyDuration (m_ccaMode1ThresholdW);
559  if (!delayUntilCcaEnd.IsZero ())
560  {
561  m_state->SwitchMaybeToCcaBusy (delayUntilCcaEnd);
562  }
563 }
564 
565 void
566 YansWifiPhy::SendPacket (Ptr<const Packet> packet, WifiMode txMode, WifiPreamble preamble, uint8_t txPower)
567 {
568  NS_LOG_FUNCTION (this << packet << txMode << preamble << (uint32_t)txPower);
569  /* Transmission can happen if:
570  * - we are syncing on a packet. It is the responsability of the
571  * MAC layer to avoid doing this but the PHY does nothing to
572  * prevent it.
573  * - we are idle
574  */
575  NS_ASSERT (!m_state->IsStateTx () && !m_state->IsStateSwitching () &&
576  !m_state->IsStateSensing());
577 
578  Time txDuration = CalculateTxDuration (packet->GetSize (), txMode, preamble);
579  if (m_state->IsStateRx ())
580  {
581  m_endRxEvent.Cancel ();
582  m_interference.NotifyRxEnd ();
583  }
584  NotifyTxBegin (packet);
585  uint32_t dataRate500KbpsUnits = txMode.GetDataRate () / 500000;
586  bool isShortPreamble = (WIFI_PREAMBLE_SHORT == preamble);
587  NotifyMonitorSniffTx (packet, (uint16_t)GetChannelFrequencyMhz (), GetChannelNumber (), dataRate500KbpsUnits, isShortPreamble);
588  m_state->SwitchToTx (txDuration, packet, txMode, preamble, txPower);
589  m_channel->Send (this, packet, GetPowerDbm (txPower) + m_txGainDb, txMode, preamble);
590 }
591 
592 uint32_t
594 {
595  return m_deviceRateSet.size ();
596 }
597 WifiMode
598 YansWifiPhy::GetMode (uint32_t mode) const
599 {
600  return m_deviceRateSet[mode];
601 }
602 uint32_t
604 {
605  return m_nTxPower;
606 }
607 
608 void
609 YansWifiPhy::Configure80211a (void)
610 {
611  NS_LOG_FUNCTION (this);
612  m_channelStartingFrequency = 5e3; // 5.000 GHz
613 
615  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate9Mbps ());
616  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate12Mbps ());
617  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate18Mbps ());
618  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate24Mbps ());
619  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate36Mbps ());
620  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate48Mbps ());
621  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate54Mbps ());
622 }
623 
624 
625 void
626 YansWifiPhy::Configure80211b (void)
627 {
628  NS_LOG_FUNCTION (this);
629  m_channelStartingFrequency = 2407; // 2.407 GHz
630 
632  m_deviceRateSet.push_back (WifiPhy::GetDsssRate2Mbps ());
634  m_deviceRateSet.push_back (WifiPhy::GetDsssRate11Mbps ());
635 }
636 
637 void
638 YansWifiPhy::Configure80211g (void)
639 {
640  NS_LOG_FUNCTION (this);
641  m_channelStartingFrequency = 2407; // 2.407 GHz
642 
644  m_deviceRateSet.push_back (WifiPhy::GetDsssRate2Mbps ());
647  m_deviceRateSet.push_back (WifiPhy::GetErpOfdmRate9Mbps ());
648  m_deviceRateSet.push_back (WifiPhy::GetDsssRate11Mbps ());
649  m_deviceRateSet.push_back (WifiPhy::GetErpOfdmRate12Mbps ());
650  m_deviceRateSet.push_back (WifiPhy::GetErpOfdmRate18Mbps ());
651  m_deviceRateSet.push_back (WifiPhy::GetErpOfdmRate24Mbps ());
652  m_deviceRateSet.push_back (WifiPhy::GetErpOfdmRate36Mbps ());
653  m_deviceRateSet.push_back (WifiPhy::GetErpOfdmRate48Mbps ());
654  m_deviceRateSet.push_back (WifiPhy::GetErpOfdmRate54Mbps ());
655 }
656 
657 void
658 YansWifiPhy::Configure80211_10Mhz (void)
659 {
660  NS_LOG_FUNCTION (this);
661  m_channelStartingFrequency = 5e3; // 5.000 GHz, suppose 802.11a
662 
663  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate3MbpsBW10MHz ());
664  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate4_5MbpsBW10MHz ());
665  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate6MbpsBW10MHz ());
666  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate9MbpsBW10MHz ());
667  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate12MbpsBW10MHz ());
668  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate18MbpsBW10MHz ());
669  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate24MbpsBW10MHz ());
670  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate27MbpsBW10MHz ());
671 }
672 
673 void
674 YansWifiPhy::Configure80211_5Mhz (void)
675 {
676  NS_LOG_FUNCTION (this);
677  m_channelStartingFrequency = 5e3; // 5.000 GHz, suppose 802.11a
678 
679  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate1_5MbpsBW5MHz ());
680  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate2_25MbpsBW5MHz ());
681  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate3MbpsBW5MHz ());
682  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate4_5MbpsBW5MHz ());
683  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate6MbpsBW5MHz ());
684  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate9MbpsBW5MHz ());
685  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate12MbpsBW5MHz ());
686  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate13_5MbpsBW5MHz ());
687 }
688 
689 void
690 YansWifiPhy::ConfigureHolland (void)
691 {
692  NS_LOG_FUNCTION (this);
693  m_channelStartingFrequency = 5e3; // 5.000 GHz
695  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate12Mbps ());
696  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate18Mbps ());
697  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate36Mbps ());
698  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate54Mbps ());
699 }
700 
701 void
702 YansWifiPhy::Configure80211p_CCH (void)
703 {
704  NS_LOG_FUNCTION (this);
705  m_channelStartingFrequency = 5e3; // 802.11p works over the 5Ghz freq range
706 
707  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate3MbpsBW10MHz ());
708  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate4_5MbpsBW10MHz ());
709  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate6MbpsBW10MHz ());
710  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate9MbpsBW10MHz ());
711  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate12MbpsBW10MHz ());
712  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate18MbpsBW10MHz ());
713  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate24MbpsBW10MHz ());
714  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate27MbpsBW10MHz ());
715 }
716 
717 void
718 YansWifiPhy::Configure80211p_SCH (void)
719 {
720  NS_LOG_FUNCTION (this);
721  m_channelStartingFrequency = 5e3; // 802.11p works over the 5Ghz freq range
722 
723  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate3MbpsBW10MHz ());
724  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate4_5MbpsBW10MHz ());
725  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate6MbpsBW10MHz ());
726  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate9MbpsBW10MHz ());
727  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate12MbpsBW10MHz ());
728  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate18MbpsBW10MHz ());
729  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate24MbpsBW10MHz ());
730  m_deviceRateSet.push_back (WifiPhy::GetOfdmRate27MbpsBW10MHz ());
731 }
732 
733 void
735 {
736  m_state->RegisterListener (listener);
737 }
738 
739 bool
741 {
742  return m_state->IsStateCcaBusy ();
743 }
744 
745 bool
747 {
748  return m_state->IsStateIdle ();
749 }
750 bool
752 {
753  return m_state->IsStateBusy ();
754 }
755 bool
757 {
758  return m_state->IsStateRx ();
759 }
760 bool
762 {
763  return m_state->IsStateTx ();
764 }
765 bool
767 {
768  return m_state->IsStateSwitching ();
769 }
770 bool
772 {
773  return m_state->IsStateSensing ();
774 }
775 
776 Time
778 {
779  return m_state->GetStateDuration ();
780 }
781 Time
783 {
784  return m_state->GetDelayUntilIdle ();
785 }
786 
787 Time
788 YansWifiPhy::GetLastRxStartTime (void) const
789 {
790  return m_state->GetLastRxStartTime ();
791 }
792 
793 double
794 YansWifiPhy::DbToRatio (double dB) const
795 {
796  double ratio = std::pow (10.0, dB / 10.0);
797  return ratio;
798 }
799 
800 double
801 YansWifiPhy::DbmToW (double dBm) const
802 {
803  double mW = std::pow (10.0, dBm / 10.0);
804  return mW / 1000.0;
805 }
806 
807 double
808 YansWifiPhy::WToDbm (double w) const
809 {
810  return 10.0 * std::log10 (w * 1000.0);
811 }
812 
813 double
814 YansWifiPhy::RatioToDb (double ratio) const
815 {
816  return 10.0 * std::log10 (ratio);
817 }
818 
819 double
820 YansWifiPhy::GetEdThresholdW (void) const
821 {
822  return m_edThresholdW;
823 }
824 
825 double
826 YansWifiPhy::GetPowerDbm (uint8_t power) const
827 {
828  NS_ASSERT (m_txPowerBaseDbm <= m_txPowerEndDbm);
829  NS_ASSERT (m_nTxPower > 0);
830  double dbm;
831  if (m_nTxPower > 1)
832  {
833  dbm = m_txPowerBaseDbm + power * (m_txPowerEndDbm - m_txPowerBaseDbm) / (m_nTxPower - 1);
834  }
835  else
836  {
837  NS_ASSERT_MSG (m_txPowerBaseDbm == m_txPowerEndDbm, "cannot have TxPowerEnd != TxPowerStart with TxPowerLevels == 1");
838  dbm = m_txPowerBaseDbm;
839  }
840  return dbm;
841 }
842 
843 void
844 YansWifiPhy::EndReceive (Ptr<Packet> packet, Ptr<InterferenceHelper::Event> event)
845 {
846  NS_LOG_FUNCTION (this << packet << event);
847  NS_ASSERT (IsStateRx ());
848  NS_ASSERT (event->GetEndTime () == Simulator::Now ());
849 
850  struct InterferenceHelper::SnrPer snrPer;
851  snrPer = m_interference.CalculateSnrPer (event);
852  m_interference.NotifyRxEnd ();
853 
854  NS_LOG_DEBUG ("mode=" << (event->GetPayloadMode ().GetDataRate ()) <<
855  ", snr=" << snrPer.snr << ", per=" << snrPer.per << ", size=" << packet->GetSize ());
856  if (m_random->GetValue () > snrPer.per)
857  {
858  NotifyRxEnd (packet);
859  uint32_t dataRate500KbpsUnits = event->GetPayloadMode ().GetDataRate () / 500000;
860  bool isShortPreamble = (WIFI_PREAMBLE_SHORT == event->GetPreambleType ());
861  double signalDbm = RatioToDb (event->GetRxPowerW ()) + 30;
862  double noiseDbm = RatioToDb (event->GetRxPowerW () / snrPer.snr) - GetRxNoiseFigure () + 30;
863  NotifyMonitorSniffRx (packet, (uint16_t)GetChannelFrequencyMhz (), GetChannelNumber (), dataRate500KbpsUnits, isShortPreamble, signalDbm, noiseDbm);
864  m_state->SwitchFromRxEndOk (packet, snrPer.snr, event->GetPayloadMode (), event->GetPreambleType ());
865  }
866  else
867  {
868  /* failure. */
869  NotifyRxDrop (packet);
870  m_state->SwitchFromRxEndError (packet, snrPer.snr);
871  }
872 }
873 
874 int64_t
876 {
877  NS_LOG_FUNCTION (this << stream);
878  m_random->SetStream (stream);
879  return 1;
880 }
881 } // namespace ns3
void StartSensing(Time duration)
Start sensing on current channel.
virtual bool IsStateBusy(void)
keep track of time unit.
Definition: nstime.h:149
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
virtual bool IsStateSwitching(void)
virtual bool IsStateTx(void)
static WifiMode GetDsssRate1Mbps()
Definition: wifi-phy.cc:357
#define NS_ASSERT(condition)
Definition: assert.h:64
Time GetEnergyDuration(double energyW)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void RegisterListener(WifiPhyListener *listener)
uint32_t GetSize(void) const
Definition: packet.h:620
uint16_t GetChannelNumber() const
Return current channel number, see SetChannelNumber()
void SetChannelNumber(uint16_t id)
Set channel number.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
void NotifyTxBegin(Ptr< const Packet > packet)
Definition: wifi-phy.cc:304
virtual Time GetDelayUntilIdle(void)
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:88
virtual Time GetStateDuration(void)
virtual void SendPacket(Ptr< const Packet > packet, WifiMode mode, enum WifiPreamble preamble, uint8_t txPowerLevel)
virtual bool IsStateSensing(void)
virtual void SetHandoffEndedCallback(WifiPhy::HandoffEndedCallback callback)
virtual void SetReceiveErrorCallback(WifiPhy::RxErrorCallback callback)
void NotifyRxDrop(Ptr< const Packet > packet)
Definition: wifi-phy.cc:334
WifiPreamble
Definition: wifi-preamble.h:29
virtual uint32_t GetNModes(void) const
virtual bool IsStateRx(void)
virtual WifiMode GetMode(uint32_t mode) const
double m_channelStartingFrequency
Standard-dependent center frequency of 0-th channel, MHz.
receive notifications about phy events.
Definition: wifi-phy.h:44
WifiModeList m_deviceRateSet
virtual void DoDispose(void)
void NotifyRxBegin(Ptr< const Packet > packet)
Definition: wifi-phy.cc:322
double GetChannelFrequencyMhz() const
Return current center channel frequency in MHz, see SetChannelNumber()
static Time CalculateTxDuration(uint32_t size, WifiMode payloadMode, enum WifiPreamble preamble)
Definition: wifi-phy.cc:294
virtual bool IsStateCcaBusy(void)
void NotifyMonitorSniffRx(Ptr< const Packet > packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble, double signalDbm, double noiseDbm)
Definition: wifi-phy.cc:340
virtual void SetReceiveOkCallback(WifiPhy::RxOkCallback callback)
int64_t AssignStreams(int64_t stream)
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
virtual void SetSenseEndedCallback(WifiPhy::SnsEndedCallback callback)
static WifiMode GetDsssRate5_5Mbps()
Definition: wifi-phy.cc:387
static Time Now(void)
Definition: simulator.cc:179
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
virtual bool IsStateIdle(void)
virtual double CalculateSnr(WifiMode txMode, double ber) const
static WifiMode GetErpOfdmRate6Mbps()
Definition: wifi-phy.cc:417
Ptr< UniformRandomVariable > m_random
Provides uniform random variables.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
void NotifyRxEnd(Ptr< const Packet > packet)
Definition: wifi-phy.cc:328
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
void Cancel(void)
Definition: event-id.cc:47
bool IsExpired(void) const
Definition: event-id.cc:53
void NotifyMonitorSniffTx(Ptr< const Packet > packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble)
Definition: wifi-phy.cc:346
uint64_t GetDataRate(void) const
Definition: wifi-mode.cc:57
Time MicroSeconds(uint64_t us)
create ns3::Time instances in units of microseconds.
Definition: nstime.h:615
static WifiMode GetOfdmRate6Mbps()
Definition: wifi-phy.cc:525
virtual uint32_t GetNTxPower(void) const