src/devices/wifi/yans-wifi-phy.cc
author Pavel Boyko <boyko@iitp.ru>
Fri Nov 13 14:51:59 2009 +0300 (2009-11-13)
changeset 5511 d77201fa5ee2
parent 5495 7f6bb3ad07b4
child 5512 28bccc88b34c
permissions -rw-r--r--
ChannelNumber attribute added to YansWifiPhy. Now it is possible to setup wifi channel using WifiPhyHelper::Set() method.
     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/random-variable.h"
    30 #include "ns3/assert.h"
    31 #include "ns3/log.h"
    32 #include "ns3/double.h"
    33 #include "ns3/uinteger.h"
    34 #include "ns3/enum.h"
    35 #include "ns3/pointer.h"
    36 #include "ns3/net-device.h"
    37 #include "ns3/trace-source-accessor.h"
    38 #include <math.h>
    39 
    40 NS_LOG_COMPONENT_DEFINE ("YansWifiPhy");
    41 
    42 namespace ns3 {
    43 
    44 NS_OBJECT_ENSURE_REGISTERED (YansWifiPhy);
    45 
    46 TypeId 
    47 YansWifiPhy::GetTypeId (void)
    48 {
    49   static TypeId tid = TypeId ("ns3::YansWifiPhy")
    50     .SetParent<WifiPhy> ()
    51     .AddConstructor<YansWifiPhy> ()
    52     .AddAttribute ("EnergyDetectionThreshold",
    53                    "The energy of a received signal should be higher than "
    54                    "this threshold (dbm) to allow the PHY layer to detect the signal.",
    55                    DoubleValue (-96.0),
    56                    MakeDoubleAccessor (&YansWifiPhy::SetEdThreshold,
    57                                        &YansWifiPhy::GetEdThreshold),
    58                    MakeDoubleChecker<double> ())
    59     .AddAttribute ("CcaMode1Threshold",
    60                    "The energy of a received signal should be higher than "
    61                    "this threshold (dbm) to allow the PHY layer to declare CCA BUSY state",
    62                    DoubleValue (-99.0),
    63                    MakeDoubleAccessor (&YansWifiPhy::SetCcaMode1Threshold,
    64                                        &YansWifiPhy::GetCcaMode1Threshold),
    65                    MakeDoubleChecker<double> ())
    66     .AddAttribute ("TxGain",
    67                    "Transmission gain (dB).",
    68                    DoubleValue (1.0),
    69                    MakeDoubleAccessor (&YansWifiPhy::SetTxGain,
    70                                        &YansWifiPhy::GetTxGain),
    71                    MakeDoubleChecker<double> ())
    72     .AddAttribute ("RxGain",
    73                    "Reception gain (dB).",
    74                    DoubleValue (1.0),
    75                    MakeDoubleAccessor (&YansWifiPhy::SetRxGain,
    76                                        &YansWifiPhy::GetRxGain),
    77                    MakeDoubleChecker<double> ())
    78     .AddAttribute ("TxPowerLevels",
    79                    "Number of transmission power levels available between "
    80                    "TxPowerBase and TxPowerEnd included.",
    81                    UintegerValue (1),
    82                    MakeUintegerAccessor (&YansWifiPhy::m_nTxPower),
    83                    MakeUintegerChecker<uint32_t> ())
    84     .AddAttribute ("TxPowerEnd",
    85                    "Maximum available transmission level (dbm).",
    86                    DoubleValue (16.0206),
    87                    MakeDoubleAccessor (&YansWifiPhy::SetTxPowerEnd, 
    88                                        &YansWifiPhy::GetTxPowerEnd),
    89                    MakeDoubleChecker<double> ())
    90     .AddAttribute ("TxPowerStart",
    91                    "Minimum available transmission level (dbm).",
    92                    DoubleValue (16.0206),
    93                    MakeDoubleAccessor (&YansWifiPhy::SetTxPowerStart, 
    94                                        &YansWifiPhy::GetTxPowerStart),
    95                    MakeDoubleChecker<double> ())
    96     .AddAttribute ("RxNoiseFigure",
    97                    "Loss (dB) in the Signal-to-Noise-Ratio due to non-idealities in the receiver."
    98                    " According to Wikipedia (http://en.wikipedia.org/wiki/Noise_figure), this is "
    99                    "\"the difference in decibels (dB) between"
   100                    " the noise output of the actual receiver to the noise output of an "
   101                    " ideal receiver with the same overall gain and bandwidth when the receivers "
   102                    " are connected to sources at the standard noise temperature T0 (usually 290 K)\"."
   103                    " For",
   104                    DoubleValue (7),
   105                    MakeDoubleAccessor (&YansWifiPhy::SetRxNoiseFigure,
   106                                        &YansWifiPhy::GetRxNoiseFigure),
   107                    MakeDoubleChecker<double> ())
   108     .AddAttribute ("State", "The state of the PHY layer",
   109                    PointerValue (),
   110                    MakePointerAccessor (&YansWifiPhy::m_state),
   111                    MakePointerChecker<WifiPhyStateHelper> ())
   112     .AddAttribute ("ChannelSwitchDelay",
   113                    "Delay between two short frames transmitted on different frequencies. NOTE: Unused now.",
   114                    TimeValue (MicroSeconds (250)),
   115                    MakeTimeAccessor (&YansWifiPhy::m_channelSwitchDelay), 
   116                    MakeTimeChecker ())
   117     .AddAttribute ("ChannelNumber",
   118                    "Channel center frequency = Channel starting frequency + 5 MHz * (nch - 1)",
   119                    UintegerValue (1),
   120                    MakeUintegerAccessor (&YansWifiPhy::SetChannelNumber, 
   121                                          &YansWifiPhy::GetChannelNumber),
   122                    MakeUintegerChecker<uint16_t> ())
   123 
   124     ;
   125   return tid;
   126 }
   127 
   128 YansWifiPhy::YansWifiPhy ()
   129   :  m_channelNumber (1),
   130      m_endSyncEvent (),
   131      m_random (0.0, 1.0),
   132      m_channelStartingFrequency (0)
   133 {
   134   NS_LOG_FUNCTION (this);
   135   m_state = CreateObject<WifiPhyStateHelper> ();
   136 }
   137 
   138 YansWifiPhy::~YansWifiPhy ()
   139 {
   140   NS_LOG_FUNCTION (this);
   141 }
   142 
   143 void
   144 YansWifiPhy::DoDispose (void)
   145 {
   146   NS_LOG_FUNCTION (this);
   147   m_channel = 0;
   148   m_modes.clear ();
   149   m_device = 0;
   150   m_mobility = 0;
   151   m_state = 0;
   152 }
   153 
   154 void
   155 YansWifiPhy::ConfigureStandard (enum WifiPhyStandard standard)
   156 {
   157   NS_LOG_FUNCTION (this << standard);
   158   switch (standard) {
   159   case WIFI_PHY_STANDARD_80211a:
   160     Configure80211a ();
   161     break;
   162   case WIFI_PHY_STANDARD_80211b:
   163     Configure80211b ();
   164     break;
   165   case WIFI_PHY_STANDARD_80211_10Mhz: 
   166     Configure80211_10Mhz ();
   167     break;
   168   case WIFI_PHY_STANDARD_80211_5Mhz:
   169     Configure80211_5Mhz ();
   170     break; 
   171   case WIFI_PHY_STANDARD_holland:
   172     ConfigureHolland ();
   173     break;
   174   default:
   175     NS_ASSERT (false);
   176     break;
   177   }
   178 }
   179 
   180 
   181 void 
   182 YansWifiPhy::SetRxNoiseFigure (double noiseFigureDb)
   183 {
   184   NS_LOG_FUNCTION (this << noiseFigureDb);
   185   m_interference.SetNoiseFigure (DbToRatio (noiseFigureDb));
   186 }
   187 void 
   188 YansWifiPhy::SetTxPowerStart (double start)
   189 {
   190   NS_LOG_FUNCTION (this << start);
   191   m_txPowerBaseDbm = start;
   192 }
   193 void 
   194 YansWifiPhy::SetTxPowerEnd (double end)
   195 {
   196   NS_LOG_FUNCTION (this << end);
   197   m_txPowerEndDbm = end;
   198 }
   199 void 
   200 YansWifiPhy::SetNTxPower (uint32_t n)
   201 {
   202   NS_LOG_FUNCTION (this << n);
   203   m_nTxPower = n;
   204 }
   205 void 
   206 YansWifiPhy::SetTxGain (double gain)
   207 {
   208   NS_LOG_FUNCTION (this << gain);
   209   m_txGainDb = gain;
   210 }
   211 void 
   212 YansWifiPhy::SetRxGain (double gain)
   213 {
   214   NS_LOG_FUNCTION (this << gain);
   215   m_rxGainDb = gain;
   216 }
   217 void 
   218 YansWifiPhy::SetEdThreshold (double threshold)
   219 {
   220   NS_LOG_FUNCTION (this << threshold);
   221   m_edThresholdW = DbmToW (threshold);
   222 }
   223 void 
   224 YansWifiPhy::SetCcaMode1Threshold (double threshold)
   225 {
   226   NS_LOG_FUNCTION (this << threshold);
   227   m_ccaMode1ThresholdW = DbmToW (threshold);
   228 }
   229 void 
   230 YansWifiPhy::SetErrorRateModel (Ptr<ErrorRateModel> rate)
   231 {
   232   m_interference.SetErrorRateModel (rate);
   233 }
   234 void 
   235 YansWifiPhy::SetDevice (Ptr<Object> device)
   236 {
   237   m_device = device;
   238 }
   239 void 
   240 YansWifiPhy::SetMobility (Ptr<Object> mobility)
   241 {
   242   m_mobility = mobility;
   243 }
   244 
   245 double 
   246 YansWifiPhy::GetRxNoiseFigure (void) const
   247 {
   248   return RatioToDb (m_interference.GetNoiseFigure ());
   249 }
   250 double 
   251 YansWifiPhy::GetTxPowerStart (void) const
   252 {
   253   return m_txPowerBaseDbm;
   254 }
   255 double 
   256 YansWifiPhy::GetTxPowerEnd (void) const
   257 {
   258   return m_txPowerEndDbm;
   259 }
   260 double 
   261 YansWifiPhy::GetTxGain (void) const
   262 {
   263   return m_txGainDb;
   264 }
   265 double 
   266 YansWifiPhy::GetRxGain (void) const
   267 {
   268   return m_rxGainDb;
   269 }
   270 
   271 double 
   272 YansWifiPhy::GetEdThreshold (void) const
   273 {
   274   return WToDbm (m_edThresholdW);
   275 }
   276 
   277 double 
   278 YansWifiPhy::GetCcaMode1Threshold (void) const
   279 {
   280   return WToDbm (m_ccaMode1ThresholdW);
   281 }
   282 
   283 Ptr<ErrorRateModel> 
   284 YansWifiPhy::GetErrorRateModel (void) const
   285 {
   286   return m_interference.GetErrorRateModel ();
   287 }
   288 Ptr<Object> 
   289 YansWifiPhy::GetDevice (void) const
   290 {
   291   return m_device;
   292 }
   293 Ptr<Object> 
   294 YansWifiPhy::GetMobility (void)
   295 {
   296   return m_mobility;
   297 }
   298 
   299 double 
   300 YansWifiPhy::CalculateSnr (WifiMode txMode, double ber) const
   301 {
   302   return m_interference.GetErrorRateModel ()->CalculateSnr (txMode, ber);
   303 }
   304 
   305 Ptr<WifiChannel> 
   306 YansWifiPhy::GetChannel (void) const
   307 {
   308   return m_channel;
   309 }
   310 void 
   311 YansWifiPhy::SetChannel (Ptr<YansWifiChannel> channel)
   312 {
   313   m_channel = channel;
   314   m_channel->Add (this);
   315 }
   316 
   317 void 
   318 YansWifiPhy::SetChannelNumber (uint16_t nch)
   319 {
   320   NS_ASSERT(!IsStateSwitching()); 
   321   switch (m_state->GetState ()) {
   322   case YansWifiPhy::SYNC:
   323     NS_LOG_DEBUG ("drop packet because of channel switching while reception");
   324     m_endSyncEvent.Cancel();
   325     goto switchChannel;
   326     break;
   327   case YansWifiPhy::TX:
   328       NS_LOG_DEBUG ("channel switching postponed until end of current transmission");
   329       Simulator::Schedule (GetDelayUntilIdle(), &YansWifiPhy::SetChannelNumber, this, nch);
   330     break;
   331   case YansWifiPhy::CCA_BUSY:
   332   case YansWifiPhy::IDLE:
   333     goto switchChannel;
   334     break;
   335   default:
   336     NS_ASSERT (false);
   337     break;
   338   }
   339 
   340   return;
   341 
   342   switchChannel: 
   343 
   344   NS_LOG_DEBUG("switching channel " << m_channelNumber << " -> " << nch);
   345   m_state->SwitchToChannelSwitching(m_channelSwitchDelay); 
   346   m_interference.EraseEvents(); 
   347   /*
   348    * Needed here to be able to correctly sensed the medium for the first
   349    * time after the switching. The actual switching is not performed until
   350    * after m_channelSwitchDelay. Packets received during the switching
   351    * state are added to the event list and are employed later to figure
   352    * out the state of the medium after the switching.
   353    */
   354   m_channelNumber = nch;
   355 }
   356 
   357 uint16_t 
   358 YansWifiPhy::GetChannelNumber() const
   359 {
   360   return m_channelNumber;
   361 }
   362 
   363 double
   364 YansWifiPhy::GetChannelFrequencyMhz() const
   365 {
   366   return m_channelStartingFrequency + 5 * (GetChannelNumber() - 1);
   367 }
   368 
   369 void 
   370 YansWifiPhy::SetReceiveOkCallback (SyncOkCallback callback)
   371 {
   372   m_state->SetReceiveOkCallback (callback);
   373 }
   374 void 
   375 YansWifiPhy::SetReceiveErrorCallback (SyncErrorCallback callback)
   376 {
   377   m_state->SetReceiveErrorCallback (callback);
   378 }
   379 void 
   380 YansWifiPhy::StartReceivePacket (Ptr<Packet> packet, 
   381                                  double rxPowerDbm,
   382                                  WifiMode txMode,
   383                                  enum WifiPreamble preamble)
   384 {
   385   NS_LOG_FUNCTION (this << packet << rxPowerDbm << txMode << preamble);
   386   rxPowerDbm += m_rxGainDb;
   387   double rxPowerW = DbmToW (rxPowerDbm);
   388   Time rxDuration = CalculateTxDuration (packet->GetSize (), txMode, preamble);
   389   Time endRx = Simulator::Now () + rxDuration;
   390 
   391   Ptr<InterferenceHelper::Event> event;
   392   event = m_interference.Add (packet->GetSize (), 
   393                               txMode,
   394                               preamble,
   395                               rxDuration,
   396                               rxPowerW);
   397 
   398   switch (m_state->GetState ()) {
   399   case YansWifiPhy::SWITCHING: 
   400     NS_LOG_DEBUG ("drop packet because of channel switching");
   401     NotifyRxDrop (packet);
   402     /*
   403      * Packets received on the upcoming channel are added to the event list
   404      * during the switching state. This way the medium can be correctly sensed
   405      * when the device listens to the channel for the first time after the
   406      * switching e.g. after channel switching, the channel may be sensed as
   407      * busy due to other devices' tramissions started before the end of
   408      * the switching.
   409      */
   410     if (endRx > Simulator::Now () + m_state->GetDelayUntilIdle ()) 
   411       {
   412         // that packet will be noise _after_ the completion of the
   413         // channel switching.
   414         goto maybeCcaBusy;
   415       }
   416     break;
   417   case YansWifiPhy::SYNC:
   418     NS_LOG_DEBUG ("drop packet because already in Sync (power="<<
   419                   rxPowerW<<"W)");
   420     NotifyRxDrop (packet);
   421     if (endRx > Simulator::Now () + m_state->GetDelayUntilIdle ()) 
   422       {
   423         // that packet will be noise _after_ the reception of the
   424         // currently-received packet.
   425         goto maybeCcaBusy;
   426       }
   427     break;
   428   case YansWifiPhy::TX:
   429     NS_LOG_DEBUG ("drop packet because already in Tx (power="<<
   430                   rxPowerW<<"W)");
   431     NotifyRxDrop (packet);
   432     if (endRx > Simulator::Now () + m_state->GetDelayUntilIdle ()) 
   433       {
   434         // that packet will be noise _after_ the transmission of the
   435         // currently-transmitted packet.
   436         goto maybeCcaBusy;
   437       }
   438     break;
   439   case YansWifiPhy::CCA_BUSY:
   440   case YansWifiPhy::IDLE:
   441     if (rxPowerW > m_edThresholdW) 
   442       {
   443         NS_LOG_DEBUG ("sync (power="<<rxPowerW<<"W)");
   444         // sync to signal
   445         m_state->SwitchToSync (rxDuration);
   446         NS_ASSERT (m_endSyncEvent.IsExpired ());
   447         NotifyRxBegin (packet);
   448         m_endSyncEvent = Simulator::Schedule (rxDuration, &YansWifiPhy::EndSync, this, 
   449                                               packet,
   450                                               event);
   451       }
   452     else 
   453       {
   454         NS_LOG_DEBUG ("drop packet because signal power too Small ("<<
   455                       rxPowerW<<"<"<<m_edThresholdW<<")");
   456         NotifyRxDrop (packet);
   457         goto maybeCcaBusy;
   458       }
   459     break;
   460   }
   461 
   462   return;
   463 
   464  maybeCcaBusy:
   465   // We are here because we have received the first bit of a packet and we are
   466   // not going to be able to synchronize on it
   467   // In this model, CCA becomes busy when the aggregation of all signals as
   468   // tracked by the InterferenceHelper class is higher than the CcaBusyThreshold
   469 
   470   Time delayUntilCcaEnd = m_interference.GetEnergyDuration (m_ccaMode1ThresholdW);
   471   if (!delayUntilCcaEnd.IsZero ())
   472     {
   473       m_state->SwitchMaybeToCcaBusy (delayUntilCcaEnd);
   474     }
   475 }
   476 
   477 void 
   478 YansWifiPhy::SendPacket (Ptr<const Packet> packet, WifiMode txMode, WifiPreamble preamble, uint8_t txPower)
   479 {
   480   NS_LOG_FUNCTION (this << packet << txMode << preamble << (uint32_t)txPower);
   481   /* Transmission can happen if:
   482    *  - we are syncing on a packet. It is the responsability of the
   483    *    MAC layer to avoid doing this but the PHY does nothing to 
   484    *    prevent it.
   485    *  - we are idle
   486    */
   487   NS_ASSERT (!m_state->IsStateTx () && !m_state->IsStateSwitching ());
   488 
   489   Time txDuration = CalculateTxDuration (packet->GetSize (), txMode, preamble);
   490   if (m_state->IsStateSync ())
   491     {
   492       m_endSyncEvent.Cancel ();
   493     }
   494   NotifyTxBegin (packet);
   495   uint32_t dataRate500KbpsUnits = txMode.GetDataRate () / 500000;   
   496   bool isShortPreamble = (WIFI_PREAMBLE_SHORT == preamble);
   497   NotifyPromiscSniffTx (packet, (uint16_t)GetChannelFrequencyMhz (), GetChannelNumber (), dataRate500KbpsUnits, isShortPreamble);
   498   m_state->SwitchToTx (txDuration, packet, txMode, preamble, txPower);
   499   m_channel->Send (this, packet, GetPowerDbm (txPower) + m_txGainDb, txMode, preamble);
   500 }
   501 
   502 uint32_t 
   503 YansWifiPhy::GetNModes (void) const
   504 {
   505   return m_modes.size ();
   506 }
   507 WifiMode 
   508 YansWifiPhy::GetMode (uint32_t mode) const
   509 {
   510   return m_modes[mode];
   511 }
   512 uint32_t 
   513 YansWifiPhy::GetNTxPower (void) const
   514 {
   515   return m_nTxPower;
   516 }
   517 
   518 void
   519 YansWifiPhy::Configure80211a (void)
   520 {
   521   NS_LOG_FUNCTION (this);
   522   m_channelStartingFrequency = 5e3; // 5.000 GHz 
   523   m_modes.push_back (WifiPhy::Get6mba ());
   524   m_modes.push_back (WifiPhy::Get9mba ());
   525   m_modes.push_back (WifiPhy::Get12mba ());
   526   m_modes.push_back (WifiPhy::Get18mba ());
   527   m_modes.push_back (WifiPhy::Get24mba ());
   528   m_modes.push_back (WifiPhy::Get36mba ());
   529   m_modes.push_back (WifiPhy::Get48mba ());
   530   m_modes.push_back (WifiPhy::Get54mba ());
   531 }
   532 
   533 
   534 void
   535 YansWifiPhy::Configure80211b (void)
   536 {
   537   NS_LOG_FUNCTION (this);
   538   m_channelStartingFrequency = 2412; // 2.412 GHz 
   539   m_modes.push_back (WifiPhy::Get1mbb ());
   540   m_modes.push_back (WifiPhy::Get2mbb ());
   541   m_modes.push_back (WifiPhy::Get5_5mbb ());
   542   m_modes.push_back (WifiPhy::Get11mbb ());
   543 }
   544 
   545 void
   546 YansWifiPhy::Configure80211_10Mhz (void)
   547 {
   548   NS_LOG_FUNCTION (this);
   549   m_channelStartingFrequency = 5e3; // 5.000 GHz, suppose 802.11a 
   550   m_modes.push_back (WifiPhy::Get3mb10Mhz ());
   551   m_modes.push_back (WifiPhy::Get4_5mb10Mhz ());
   552   m_modes.push_back (WifiPhy::Get6mb10Mhz ());
   553   m_modes.push_back (WifiPhy::Get9mb10Mhz ());
   554   m_modes.push_back (WifiPhy::Get12mb10Mhz ());
   555   m_modes.push_back (WifiPhy::Get18mb10Mhz ());
   556   m_modes.push_back (WifiPhy::Get24mb10Mhz ());
   557   m_modes.push_back (WifiPhy::Get27mb10Mhz  ());
   558 }
   559 
   560 void
   561 YansWifiPhy::Configure80211_5Mhz (void)
   562 {
   563   NS_LOG_FUNCTION (this); 
   564   m_channelStartingFrequency = 5e3; // 5.000 GHz, suppose 802.11a
   565   m_modes.push_back (WifiPhy::Get1_5mb5Mhz ());
   566   m_modes.push_back (WifiPhy::Get2_25mb5Mhz ());
   567   m_modes.push_back (WifiPhy::Get3mb5Mhz ());
   568   m_modes.push_back (WifiPhy::Get4_5mb5Mhz ());
   569   m_modes.push_back (WifiPhy::Get6mb5Mhz ());
   570   m_modes.push_back (WifiPhy::Get9mb5Mhz ());
   571   m_modes.push_back (WifiPhy::Get12mb5Mhz ());
   572   m_modes.push_back (WifiPhy::Get13_5mb5Mhz  ());
   573 }
   574 
   575 void
   576 YansWifiPhy::ConfigureHolland (void)
   577 {
   578   NS_LOG_FUNCTION (this);
   579   m_channelStartingFrequency = 5e3; // 5.000 GHz 
   580   m_modes.push_back (WifiPhy::Get6mba ());
   581   m_modes.push_back (WifiPhy::Get12mba ());
   582   m_modes.push_back (WifiPhy::Get18mba ());
   583   m_modes.push_back (WifiPhy::Get36mba ());
   584   m_modes.push_back (WifiPhy::Get54mba ());
   585 }
   586 
   587 void 
   588 YansWifiPhy::RegisterListener (WifiPhyListener *listener)
   589 {
   590   m_state->RegisterListener (listener);
   591 }
   592 
   593 bool 
   594 YansWifiPhy::IsStateCcaBusy (void)
   595 {
   596   return m_state->IsStateCcaBusy ();
   597 }
   598 
   599 bool 
   600 YansWifiPhy::IsStateIdle (void)
   601 {
   602   return m_state->IsStateIdle ();
   603 }
   604 bool 
   605 YansWifiPhy::IsStateBusy (void)
   606 {
   607   return m_state->IsStateBusy ();
   608 }
   609 bool 
   610 YansWifiPhy::IsStateSync (void)
   611 {
   612   return m_state->IsStateSync ();
   613 }
   614 bool 
   615 YansWifiPhy::IsStateTx (void)
   616 {
   617   return m_state->IsStateTx ();
   618 }
   619 bool 
   620 YansWifiPhy::IsStateSwitching (void)
   621 {
   622   return m_state->IsStateSwitching ();
   623 }
   624 
   625 Time
   626 YansWifiPhy::GetStateDuration (void)
   627 {
   628   return m_state->GetStateDuration ();
   629 }
   630 Time
   631 YansWifiPhy::GetDelayUntilIdle (void)
   632 {
   633   return m_state->GetDelayUntilIdle ();
   634 }
   635 
   636 Time 
   637 YansWifiPhy::GetLastRxStartTime (void) const
   638 {
   639   return m_state->GetLastRxStartTime ();
   640 }
   641 
   642 Time 
   643 YansWifiPhy::CalculateTxDuration (uint32_t size, WifiMode payloadMode, enum WifiPreamble preamble) const
   644 {
   645   return m_interference.CalculateTxDuration (size, payloadMode, preamble);
   646 }
   647 
   648 double 
   649 YansWifiPhy::DbToRatio (double dB) const
   650 {
   651   double ratio = pow(10.0,dB/10.0);
   652   return ratio;
   653 }
   654 
   655 double 
   656 YansWifiPhy::DbmToW (double dBm) const
   657 {
   658   double mW = pow(10.0,dBm/10.0);
   659   return mW / 1000.0;
   660 }
   661 
   662 double
   663 YansWifiPhy::WToDbm (double w) const
   664 {
   665   return 10.0 * log10(w * 1000.0);
   666 }
   667 
   668 double
   669 YansWifiPhy::RatioToDb (double ratio) const
   670 {
   671   return 10.0 * log10(ratio);
   672 }
   673 
   674 double
   675 YansWifiPhy::GetEdThresholdW (void) const
   676 {
   677   return m_edThresholdW;
   678 }
   679 
   680 double 
   681 YansWifiPhy::GetPowerDbm (uint8_t power) const
   682 {
   683   NS_ASSERT (m_txPowerBaseDbm <= m_txPowerEndDbm);
   684   NS_ASSERT (m_nTxPower > 0);
   685   double dbm = m_txPowerBaseDbm + power * (m_txPowerEndDbm - m_txPowerBaseDbm) / m_nTxPower;
   686   return dbm;
   687 }
   688 
   689 void
   690 YansWifiPhy::EndSync (Ptr<Packet> packet, Ptr<InterferenceHelper::Event> event)
   691 {
   692   NS_LOG_FUNCTION (this << packet << event);
   693   NS_ASSERT (IsStateSync ());
   694   NS_ASSERT (event->GetEndTime () == Simulator::Now ());
   695 
   696   struct InterferenceHelper::SnrPer snrPer;
   697   snrPer = m_interference.CalculateSnrPer (event);
   698 
   699   NS_LOG_DEBUG ("mode="<<(event->GetPayloadMode ().GetDataRate ())<<
   700                 ", snr="<<snrPer.snr<<", per="<<snrPer.per<<", size="<<packet->GetSize ());
   701   if (m_random.GetValue () > snrPer.per) 
   702     {
   703       NotifyRxEnd (packet); 
   704       uint32_t dataRate500KbpsUnits = event->GetPayloadMode ().GetDataRate () / 500000;   
   705       bool isShortPreamble = (WIFI_PREAMBLE_SHORT == event->GetPreambleType ());  
   706       double signalDbm = RatioToDb (event->GetRxPowerW ()) + 30;
   707       double noiseDbm = RatioToDb(event->GetRxPowerW() / snrPer.snr) - GetRxNoiseFigure() + 30 ;
   708       NotifyPromiscSniffRx (packet, (uint16_t)GetChannelFrequencyMhz (), GetChannelNumber (), dataRate500KbpsUnits, isShortPreamble, signalDbm, noiseDbm);
   709       m_state->SwitchFromSyncEndOk (packet, snrPer.snr, event->GetPayloadMode (), event->GetPreambleType ());
   710     } 
   711   else 
   712     {
   713       /* failure. */
   714       NotifyRxDrop (packet);
   715       m_state->SwitchFromSyncEndError (packet, snrPer.snr);
   716     }
   717 }
   718 } // namespace ns3