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"
31 #include "ns3/double.h"
32 #include "ns3/uinteger.h"
34 #include "ns3/pointer.h"
35 #include "ns3/net-device.h"
36 #include "ns3/trace-source-accessor.h"
43 NS_OBJECT_ENSURE_REGISTERED (YansWifiPhy);
46 YansWifiPhy::GetTypeId (
void)
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.",
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",
62 MakeDoubleAccessor (&YansWifiPhy::SetCcaMode1Threshold,
63 &YansWifiPhy::GetCcaMode1Threshold),
64 MakeDoubleChecker<double> ())
65 .AddAttribute (
"TxGain",
66 "Transmission gain (dB).",
68 MakeDoubleAccessor (&YansWifiPhy::SetTxGain,
69 &YansWifiPhy::GetTxGain),
70 MakeDoubleChecker<double> ())
71 .AddAttribute (
"RxGain",
72 "Reception gain (dB).",
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.",
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)\"."
104 MakeDoubleAccessor (&YansWifiPhy::SetRxNoiseFigure,
105 &YansWifiPhy::GetRxNoiseFigure),
106 MakeDoubleChecker<double> ())
107 .AddAttribute (
"State",
"The state of the PHY layer",
109 MakePointerAccessor (&YansWifiPhy::m_state),
110 MakePointerChecker<WifiPhyStateHelper> ())
111 .AddAttribute (
"ChannelSwitchDelay",
112 "Delay between two short frames transmitted on different frequencies.",
114 MakeTimeAccessor (&YansWifiPhy::m_channelSwitchDelay),
116 .AddAttribute (
"ChannelNumber",
117 "Channel center frequency = Channel starting frequency + 5 MHz * (nch - 1)",
121 MakeUintegerChecker<uint16_t> ())
127 YansWifiPhy::YansWifiPhy ()
128 : m_channelNumber (1),
130 m_channelStartingFrequency (0)
133 m_random = CreateObject<UniformRandomVariable> ();
134 m_state = CreateObject<WifiPhyStateHelper> ();
137 YansWifiPhy::~YansWifiPhy ()
169 Configure80211_10Mhz ();
172 Configure80211_5Mhz ();
178 Configure80211p_CCH ();
181 Configure80211p_SCH ();
190 YansWifiPhy::GetSwitchingDelay (
void)
192 return m_channelSwitchDelay;
196 YansWifiPhy::SetRxNoiseFigure (
double noiseFigureDb)
199 m_interference.SetNoiseFigure (DbToRatio (noiseFigureDb));
202 YansWifiPhy::SetTxPowerStart (
double start)
205 m_txPowerBaseDbm = start;
208 YansWifiPhy::SetTxPowerEnd (
double end)
211 m_txPowerEndDbm = end;
214 YansWifiPhy::SetNTxPower (uint32_t n)
220 YansWifiPhy::SetTxGain (
double gain)
226 YansWifiPhy::SetRxGain (
double gain)
232 YansWifiPhy::SetEdThreshold (
double threshold)
235 m_edThresholdW = DbmToW (threshold);
238 YansWifiPhy::SetCcaMode1Threshold (
double threshold)
241 m_ccaMode1ThresholdW = DbmToW (threshold);
244 YansWifiPhy::SetErrorRateModel (Ptr<ErrorRateModel> rate)
246 m_interference.SetErrorRateModel (rate);
249 YansWifiPhy::SetDevice (Ptr<Object> device)
254 YansWifiPhy::SetMobility (Ptr<Object> mobility)
256 m_mobility = mobility;
260 YansWifiPhy::GetRxNoiseFigure (
void)
const
262 return RatioToDb (m_interference.GetNoiseFigure ());
265 YansWifiPhy::GetTxPowerStart (
void)
const
267 return m_txPowerBaseDbm;
270 YansWifiPhy::GetTxPowerEnd (
void)
const
272 return m_txPowerEndDbm;
275 YansWifiPhy::GetTxGain (
void)
const
280 YansWifiPhy::GetRxGain (
void)
const
286 YansWifiPhy::GetEdThreshold (
void)
const
288 return WToDbm (m_edThresholdW);
292 YansWifiPhy::GetCcaMode1Threshold (
void)
const
294 return WToDbm (m_ccaMode1ThresholdW);
298 YansWifiPhy::GetErrorRateModel (
void)
const
300 return m_interference.GetErrorRateModel ();
303 YansWifiPhy::GetDevice (
void)
const
308 YansWifiPhy::GetMobility (
void)
316 return m_interference.GetErrorRateModel ()->CalculateSnr (txMode, ber);
320 YansWifiPhy::GetChannel (
void)
const
325 YansWifiPhy::SetChannel (Ptr<YansWifiChannel> channel)
328 m_channel->Add (
this);
338 m_channelNumber = nch;
343 switch (m_state->GetState ())
346 NS_LOG_DEBUG (
"drop packet because of channel switching while reception");
351 NS_LOG_DEBUG (
"channel switching postponed until end of current transmission");
367 NS_LOG_DEBUG (
"switching channel " << m_channelNumber <<
" -> " << nch);
368 m_state->SwitchToChannelSwitching (m_channelSwitchDelay, nch);
369 m_interference.EraseEvents ();
377 m_channelNumber = nch;
378 if (!m_handoffEndedCallback.IsNull())
387 switch (m_state->GetState ())
390 NS_LOG_DEBUG (
"channel sensing postponed until end of current reception");
394 NS_LOG_DEBUG (
"channel sensing postponed until end of current transmission");
410 NS_LOG_DEBUG (
"sensing started for duration " << duration);
411 m_state->SwitchToChannelSensing (duration);
412 m_interference.EraseEvents ();
428 return m_channelNumber;
440 m_state->SetReceiveOkCallback (callback);
445 m_state->SetReceiveErrorCallback (callback);
450 m_senseEndedCallback = callback;
455 m_handoffEndedCallback = callback;
458 YansWifiPhy::StartReceivePacket (
Ptr<Packet> packet,
464 rxPowerDbm += m_rxGainDb;
465 double rxPowerW = DbmToW (rxPowerDbm);
470 event = m_interference.Add (packet->
GetSize (),
476 switch (m_state->GetState ())
479 NS_LOG_DEBUG (
"drop packet because of channel switching");
505 NS_LOG_DEBUG (
"drop packet because already in Rx (power=" <<
516 NS_LOG_DEBUG (
"drop packet because already in Tx (power=" <<
528 if (rxPowerW > m_edThresholdW)
530 NS_LOG_DEBUG (
"sync to signal (power=" << rxPowerW <<
"W)");
532 m_state->SwitchToRx (rxDuration);
535 m_interference.NotifyRxStart ();
542 NS_LOG_DEBUG (
"drop packet because signal power too Small (" <<
543 rxPowerW <<
"<" << m_edThresholdW <<
")");
559 if (!delayUntilCcaEnd.IsZero ())
561 m_state->SwitchMaybeToCcaBusy (delayUntilCcaEnd);
568 NS_LOG_FUNCTION (
this << packet << txMode << preamble << (uint32_t)txPower);
575 NS_ASSERT (!m_state->IsStateTx () && !m_state->IsStateSwitching () &&
576 !m_state->IsStateSensing());
579 if (m_state->IsStateRx ())
582 m_interference.NotifyRxEnd ();
585 uint32_t dataRate500KbpsUnits = txMode.
GetDataRate () / 500000;
586 bool isShortPreamble = (WIFI_PREAMBLE_SHORT == preamble);
588 m_state->SwitchToTx (txDuration, packet, txMode, preamble, txPower);
589 m_channel->Send (
this, packet, GetPowerDbm (txPower) + m_txGainDb, txMode, preamble);
609 YansWifiPhy::Configure80211a (
void)
626 YansWifiPhy::Configure80211b (
void)
638 YansWifiPhy::Configure80211g (
void)
658 YansWifiPhy::Configure80211_10Mhz (
void)
674 YansWifiPhy::Configure80211_5Mhz (
void)
690 YansWifiPhy::ConfigureHolland (
void)
702 YansWifiPhy::Configure80211p_CCH (
void)
718 YansWifiPhy::Configure80211p_SCH (
void)
736 m_state->RegisterListener (listener);
742 return m_state->IsStateCcaBusy ();
748 return m_state->IsStateIdle ();
753 return m_state->IsStateBusy ();
758 return m_state->IsStateRx ();
763 return m_state->IsStateTx ();
768 return m_state->IsStateSwitching ();
773 return m_state->IsStateSensing ();
779 return m_state->GetStateDuration ();
784 return m_state->GetDelayUntilIdle ();
788 YansWifiPhy::GetLastRxStartTime (
void)
const
790 return m_state->GetLastRxStartTime ();
794 YansWifiPhy::DbToRatio (
double dB)
const
796 double ratio = std::pow (10.0, dB / 10.0);
801 YansWifiPhy::DbmToW (
double dBm)
const
803 double mW = std::pow (10.0, dBm / 10.0);
808 YansWifiPhy::WToDbm (
double w)
const
810 return 10.0 * std::log10 (w * 1000.0);
814 YansWifiPhy::RatioToDb (
double ratio)
const
816 return 10.0 * std::log10 (ratio);
820 YansWifiPhy::GetEdThresholdW (
void)
const
822 return m_edThresholdW;
826 YansWifiPhy::GetPowerDbm (uint8_t power)
const
828 NS_ASSERT (m_txPowerBaseDbm <= m_txPowerEndDbm);
833 dbm = m_txPowerBaseDbm + power * (m_txPowerEndDbm - m_txPowerBaseDbm) / (m_nTxPower - 1);
837 NS_ASSERT_MSG (m_txPowerBaseDbm == m_txPowerEndDbm,
"cannot have TxPowerEnd != TxPowerStart with TxPowerLevels == 1");
838 dbm = m_txPowerBaseDbm;
844 YansWifiPhy::EndReceive (Ptr<Packet> packet, Ptr<InterferenceHelper::Event> event)
850 struct InterferenceHelper::SnrPer snrPer;
851 snrPer = m_interference.CalculateSnrPer (event);
852 m_interference.NotifyRxEnd ();
854 NS_LOG_DEBUG (
"mode=" << (event->GetPayloadMode ().GetDataRate ()) <<
855 ", snr=" << snrPer.snr <<
", per=" << snrPer.per <<
", size=" << packet->GetSize ());
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;
864 m_state->SwitchFromRxEndOk (packet, snrPer.snr, event->GetPayloadMode (),
event->GetPreambleType ());
870 m_state->SwitchFromRxEndError (packet, snrPer.snr);
void StartSensing(Time duration)
Start sensing on current channel.
virtual bool IsStateBusy(void)
smart pointer class similar to boost::intrusive_ptr
#define NS_LOG_FUNCTION(parameters)
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
virtual bool IsStateSwitching(void)
virtual bool IsStateTx(void)
static WifiMode GetDsssRate1Mbps()
#define NS_ASSERT(condition)
Time GetEnergyDuration(double energyW)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void RegisterListener(WifiPhyListener *listener)
uint32_t GetSize(void) const
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)
void NotifyTxBegin(Ptr< const Packet > packet)
virtual Time GetDelayUntilIdle(void)
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
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)
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.
WifiModeList m_deviceRateSet
virtual void DoDispose(void)
void NotifyRxBegin(Ptr< const Packet > packet)
double GetChannelFrequencyMhz() const
Return current center channel frequency in MHz, see SetChannelNumber()
static Time CalculateTxDuration(uint32_t size, WifiMode payloadMode, enum WifiPreamble preamble)
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)
virtual void SetReceiveOkCallback(WifiPhy::RxOkCallback callback)
int64_t AssignStreams(int64_t stream)
virtual void SetSenseEndedCallback(WifiPhy::SnsEndedCallback callback)
static WifiMode GetDsssRate5_5Mbps()
#define NS_ASSERT_MSG(condition, message)
virtual bool IsStateIdle(void)
virtual double CalculateSnr(WifiMode txMode, double ber) const
static WifiMode GetErpOfdmRate6Mbps()
Ptr< UniformRandomVariable > m_random
Provides uniform random variables.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
void NotifyRxEnd(Ptr< const Packet > packet)
#define NS_LOG_DEBUG(msg)
bool IsExpired(void) const
void NotifyMonitorSniffTx(Ptr< const Packet > packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble)
uint64_t GetDataRate(void) const
Time MicroSeconds(uint64_t us)
create ns3::Time instances in units of microseconds.
static WifiMode GetOfdmRate6Mbps()
virtual uint32_t GetNTxPower(void) const