A Discrete-Event Network Simulator
API
wifi-phy-state-helper.cc
Go to the documentation of this file.
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 <algorithm>
22 #include "ns3/log.h"
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "wifi-phy-state-helper.h"
26 #include "wifi-tx-vector.h"
27 #include "wifi-phy-listener.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("WifiPhyStateHelper");
32 
33 NS_OBJECT_ENSURE_REGISTERED (WifiPhyStateHelper);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::WifiPhyStateHelper")
39  .SetParent<Object> ()
40  .SetGroupName ("Wifi")
41  .AddConstructor<WifiPhyStateHelper> ()
42  .AddTraceSource ("State",
43  "The state of the PHY layer",
45  "ns3::WifiPhyStateHelper::StateTracedCallback")
46  .AddTraceSource ("RxOk",
47  "A packet has been received successfully.",
49  "ns3::WifiPhyStateHelper::RxOkTracedCallback")
50  .AddTraceSource ("RxError",
51  "A packet has been received unsuccessfully.",
53  "ns3::WifiPhyStateHelper::RxEndErrorTracedCallback")
54  .AddTraceSource ("Tx", "Packet transmission is starting.",
56  "ns3::WifiPhyStateHelper::TxTracedCallback")
57  ;
58  return tid;
59 }
60 
62  : m_rxing (false),
63  m_sleeping (false),
64  m_isOff (false),
65  m_endTx (Seconds (0)),
66  m_endRx (Seconds (0)),
67  m_endCcaBusy (Seconds (0)),
68  m_endSwitching (Seconds (0)),
69  m_startTx (Seconds (0)),
70  m_startRx (Seconds (0)),
71  m_startCcaBusy (Seconds (0)),
72  m_startSwitching (Seconds (0)),
73  m_startSleep (Seconds (0)),
74  m_previousStateChangeTime (Seconds (0))
75 {
76  NS_LOG_FUNCTION (this);
77 }
78 
79 void
81 {
82  m_rxOkCallback = callback;
83 }
84 
85 void
87 {
88  m_rxErrorCallback = callback;
89 }
90 
91 void
93 {
94  m_listeners.push_back (listener);
95 }
96 
97 void
99 {
100  ListenersI i = find (m_listeners.begin (), m_listeners.end (), listener);
101  if (i != m_listeners.end ())
102  {
103  m_listeners.erase (i);
104  }
105 }
106 
107 bool
109 {
110  return (GetState () == WifiPhyState::IDLE);
111 }
112 
113 bool
115 {
116  return (GetState () == WifiPhyState::CCA_BUSY);
117 }
118 
119 bool
121 {
122  return (GetState () == WifiPhyState::RX);
123 }
124 
125 bool
127 {
128  return (GetState () == WifiPhyState::TX);
129 }
130 
131 bool
133 {
134  return (GetState () == WifiPhyState::SWITCHING);
135 }
136 
137 bool
139 {
140  return (GetState () == WifiPhyState::SLEEP);
141 }
142 
143 bool
145 {
146  return (GetState () == WifiPhyState::OFF);
147 }
148 
149 Time
151 {
152  Time retval;
153 
154  switch (GetState ())
155  {
156  case WifiPhyState::RX:
157  retval = m_endRx - Simulator::Now ();
158  break;
159  case WifiPhyState::TX:
160  retval = m_endTx - Simulator::Now ();
161  break;
163  retval = m_endCcaBusy - Simulator::Now ();
164  break;
166  retval = m_endSwitching - Simulator::Now ();
167  break;
168  case WifiPhyState::IDLE:
169  retval = Seconds (0);
170  break;
171  case WifiPhyState::SLEEP:
172  NS_FATAL_ERROR ("Cannot determine when the device will wake up.");
173  retval = Seconds (0);
174  break;
175  case WifiPhyState::OFF:
176  NS_FATAL_ERROR ("Cannot determine when the device will be switched on.");
177  retval = Seconds (0);
178  break;
179  default:
180  NS_FATAL_ERROR ("Invalid WifiPhy state.");
181  retval = Seconds (0);
182  break;
183  }
184  retval = Max (retval, Seconds (0));
185  return retval;
186 }
187 
188 Time
190 {
191  return m_startRx;
192 }
193 
196 {
197  if (m_isOff)
198  {
199  return WifiPhyState::OFF;
200  }
201  if (m_sleeping)
202  {
203  return WifiPhyState::SLEEP;
204  }
205  else if (m_endTx > Simulator::Now ())
206  {
207  return WifiPhyState::TX;
208  }
209  else if (m_rxing)
210  {
211  return WifiPhyState::RX;
212  }
213  else if (m_endSwitching > Simulator::Now ())
214  {
216  }
217  else if (m_endCcaBusy > Simulator::Now ())
218  {
219  return WifiPhyState::CCA_BUSY;
220  }
221  else
222  {
223  return WifiPhyState::IDLE;
224  }
225 }
226 
227 void
228 WifiPhyStateHelper::NotifyTxStart (Time duration, double txPowerDbm)
229 {
230  NS_LOG_FUNCTION (this);
231  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
232  {
233  (*i)->NotifyTxStart (duration, txPowerDbm);
234  }
235 }
236 
237 void
239 {
240  NS_LOG_FUNCTION (this);
241  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
242  {
243  (*i)->NotifyRxStart (duration);
244  }
245 }
246 
247 void
249 {
250  NS_LOG_FUNCTION (this);
251  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
252  {
253  (*i)->NotifyRxEndOk ();
254  }
255 }
256 
257 void
259 {
260  NS_LOG_FUNCTION (this);
261  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
262  {
263  (*i)->NotifyRxEndError ();
264  }
265 }
266 
267 void
269 {
270  NS_LOG_FUNCTION (this);
271  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
272  {
273  (*i)->NotifyMaybeCcaBusyStart (duration);
274  }
275 }
276 
277 void
279 {
280  NS_LOG_FUNCTION (this);
281  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
282  {
283  (*i)->NotifySwitchingStart (duration);
284  }
285 }
286 
287 void
289 {
290  NS_LOG_FUNCTION (this);
291  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
292  {
293  (*i)->NotifySleep ();
294  }
295 }
296 
297 void
299 {
300  NS_LOG_FUNCTION (this);
301  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
302  {
303  (*i)->NotifyOff ();
304  }
305 }
306 
307 void
309 {
310  NS_LOG_FUNCTION (this);
311  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
312  {
313  (*i)->NotifyWakeup ();
314  }
315 }
316 
317 void
319 {
320  NS_LOG_FUNCTION (this);
321  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
322  {
323  (*i)->NotifyOn ();
324  }
325 }
326 
327 void
329 {
330  NS_LOG_FUNCTION (this);
331  Time now = Simulator::Now ();
332  Time idleStart = Max (m_endCcaBusy, m_endRx);
333  idleStart = Max (idleStart, m_endTx);
334  idleStart = Max (idleStart, m_endSwitching);
335  NS_ASSERT (idleStart <= now);
336  if (m_endCcaBusy > m_endRx
338  && m_endCcaBusy > m_endTx)
339  {
340  Time ccaBusyStart = Max (m_endTx, m_endRx);
341  ccaBusyStart = Max (ccaBusyStart, m_startCcaBusy);
342  ccaBusyStart = Max (ccaBusyStart, m_endSwitching);
343  m_stateLogger (ccaBusyStart, idleStart - ccaBusyStart, WifiPhyState::CCA_BUSY);
344  }
345  m_stateLogger (idleStart, now - idleStart, WifiPhyState::IDLE);
346 }
347 
348 void
349 WifiPhyStateHelper::SwitchToTx (Time txDuration, Ptr<const Packet> packet, double txPowerDbm,
350  WifiTxVector txVector)
351 {
352  NS_LOG_FUNCTION (this << txDuration << packet << txPowerDbm << txVector);
353  m_txTrace (packet, txVector.GetMode (), txVector.GetPreambleType (), txVector.GetTxPowerLevel ());
354  Time now = Simulator::Now ();
355  switch (GetState ())
356  {
357  case WifiPhyState::RX:
358  /* The packet which is being received as well
359  * as its endRx event are cancelled by the caller.
360  */
361  m_rxing = false;
363  m_endRx = now;
364  break;
366  {
367  Time ccaStart = Max (m_endRx, m_endTx);
368  ccaStart = Max (ccaStart, m_startCcaBusy);
369  ccaStart = Max (ccaStart, m_endSwitching);
370  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
371  } break;
372  case WifiPhyState::IDLE:
374  break;
375  default:
376  NS_FATAL_ERROR ("Invalid WifiPhy state.");
377  break;
378  }
379  m_stateLogger (now, txDuration, WifiPhyState::TX);
381  m_endTx = now + txDuration;
382  m_startTx = now;
383  NotifyTxStart (txDuration, txPowerDbm);
384 }
385 
386 void
388 {
389  NS_LOG_FUNCTION (this << rxDuration);
391  NS_ASSERT (!m_rxing);
392  Time now = Simulator::Now ();
393  switch (GetState ())
394  {
395  case WifiPhyState::IDLE:
397  break;
399  {
400  Time ccaStart = Max (m_endRx, m_endTx);
401  ccaStart = Max (ccaStart, m_startCcaBusy);
402  ccaStart = Max (ccaStart, m_endSwitching);
403  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
404  } break;
405  default:
406  NS_FATAL_ERROR ("Invalid WifiPhy state.");
407  break;
408  }
410  m_rxing = true;
411  m_startRx = now;
412  m_endRx = now + rxDuration;
413  NotifyRxStart (rxDuration);
414  NS_ASSERT (IsStateRx ());
415 }
416 
417 void
419 {
420  NS_LOG_FUNCTION (this << switchingDuration);
421  Time now = Simulator::Now ();
422  switch (GetState ())
423  {
424  case WifiPhyState::RX:
425  /* The packet which is being received as well
426  * as its endRx event are cancelled by the caller.
427  */
428  m_rxing = false;
430  m_endRx = now;
431  break;
433  {
434  Time ccaStart = Max (m_endRx, m_endTx);
435  ccaStart = Max (ccaStart, m_startCcaBusy);
436  ccaStart = Max (ccaStart, m_endSwitching);
437  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
438  } break;
439  case WifiPhyState::IDLE:
441  break;
442  default:
443  NS_FATAL_ERROR ("Invalid WifiPhy state.");
444  break;
445  }
446 
447  if (now < m_endCcaBusy)
448  {
449  m_endCcaBusy = now;
450  }
451 
452  m_stateLogger (now, switchingDuration, WifiPhyState::SWITCHING);
454  m_startSwitching = now;
455  m_endSwitching = now + switchingDuration;
456  NotifySwitchingStart (switchingDuration);
458 }
459 
460 void
462 {
463  NS_LOG_FUNCTION (this << packet << snr << txVector);
464  m_rxOkTrace (packet, snr, txVector.GetMode (), txVector.GetPreambleType ());
465  NotifyRxEndOk ();
466  DoSwitchFromRx ();
467  if (!m_rxOkCallback.IsNull ())
468  {
469  m_rxOkCallback (packet, snr, txVector);
470  }
471 
472 }
473 
474 void
476 {
477  NS_LOG_FUNCTION (this << packet << snr);
478  m_rxErrorTrace (packet, snr);
479  NotifyRxEndError ();
480  DoSwitchFromRx ();
481  if (!m_rxErrorCallback.IsNull ())
482  {
483  m_rxErrorCallback (packet, snr);
484  }
485 }
486 
487 void
489 {
490  NS_LOG_FUNCTION (this);
491  NS_ASSERT (IsStateRx ());
492  NS_ASSERT (m_rxing);
493 
494  Time now = Simulator::Now ();
497  m_rxing = false;
498 
500 }
501 
502 void
504 {
505  NS_LOG_FUNCTION (this << duration);
506  NotifyMaybeCcaBusyStart (duration);
507  Time now = Simulator::Now ();
508  switch (GetState ())
509  {
510  case WifiPhyState::IDLE:
512  break;
513  default:
514  break;
515  }
517  {
518  m_startCcaBusy = now;
519  }
520  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
521 }
522 
523 void
525 {
526  NS_LOG_FUNCTION (this);
527  Time now = Simulator::Now ();
528  switch (GetState ())
529  {
530  case WifiPhyState::IDLE:
532  break;
534  {
535  Time ccaStart = Max (m_endRx, m_endTx);
536  ccaStart = Max (ccaStart, m_startCcaBusy);
537  ccaStart = Max (ccaStart, m_endSwitching);
538  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
539  } break;
540  default:
541  NS_FATAL_ERROR ("Invalid WifiPhy state.");
542  break;
543  }
545  m_sleeping = true;
546  m_startSleep = now;
547  NotifySleep ();
548  NS_ASSERT (IsStateSleep ());
549 }
550 
551 void
553 {
554  NS_LOG_FUNCTION (this << duration);
555  NS_ASSERT (IsStateSleep ());
556  Time now = Simulator::Now ();
559  m_sleeping = false;
560  NotifyWakeup ();
561  //update m_endCcaBusy after the sleep period
562  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
563  if (m_endCcaBusy > now)
564  {
566  }
567 }
568 
569 void
571 {
572  NS_LOG_FUNCTION (this);
573  NS_ASSERT (IsStateRx ());
574  NS_ASSERT (m_rxing);
575  m_endRx = Simulator::Now ();
576  DoSwitchFromRx ();
577  NS_ASSERT (!IsStateRx ());
578 }
579 
580 void
582 {
583  NS_LOG_FUNCTION (this);
584  Time now = Simulator::Now ();
585  switch (GetState ())
586  {
587  case WifiPhyState::RX:
588  /* The packet which is being received as well
589  * as its endRx event are cancelled by the caller.
590  */
591  m_rxing = false;
593  m_endRx = now;
594  break;
595  case WifiPhyState::TX:
596  /* The packet which is being transmitted as well
597  * as its endTx event are cancelled by the caller.
598  */
600  m_endTx = now;
601  break;
602  case WifiPhyState::IDLE:
604  break;
606  {
607  Time ccaStart = Max (m_endRx, m_endTx);
608  ccaStart = Max (ccaStart, m_startCcaBusy);
609  ccaStart = Max (ccaStart, m_endSwitching);
610  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
611  } break;
612  default:
613  NS_FATAL_ERROR ("Invalid WifiPhy state.");
614  break;
615  }
617  m_isOff = true;
618  NotifyOff ();
619  NS_ASSERT (IsStateOff ());
620 }
621 
622 void
624 {
625  NS_LOG_FUNCTION (this << duration);
626  NS_ASSERT (IsStateOff ());
627  Time now = Simulator::Now ();
629  m_isOff = false;
630  NotifyOn ();
631  //update m_endCcaBusy after the off period
632  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
633  if (m_endCcaBusy > now)
634  {
636  }
637 }
638 
639 } //namespace ns3
bool IsStateIdle(void) const
Check whether the current state is IDLE.
Time GetLastRxStartTime(void) const
Return the time the last RX start.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void DoSwitchFromRx(void)
Switch the state from RX.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
TracedCallback< Time, Time, WifiPhyState > m_stateLogger
The trace source fired when state is changed.
void NotifyWakeup(void)
Notify all WifiPhyListener that we woke up.
Time m_endCcaBusy
endn CCA busy
The PHY layer is sleeping.
Time m_startCcaBusy
start CCA busy
void SwitchToOff(void)
Switch to off mode.
void SwitchToChannelSwitching(Time switchingDuration)
Switch state to channel switching for the given duration.
void UnregisterListener(WifiPhyListener *listener)
Remove WifiPhyListener from this WifiPhyStateHelper.
#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 NotifyRxEndError(void)
Notify all WifiPhyListener that the reception was not successful.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void SwitchFromRxAbort(void)
Abort current reception.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
std::vector< WifiPhyListener * >::iterator ListenersI
typedef for a list of WifiPhyListeners iterator
bool IsStateRx(void) const
Check whether the current state is RX.
bool IsStateSwitching(void) const
Check whether the current state is SWITCHING.
void SwitchFromSleep(Time duration)
Switch from sleep mode.
Time GetDelayUntilIdle(void) const
Return the time before the state is back to IDLE.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
WifiPreamble GetPreambleType(void) const
void NotifySleep(void)
Notify all WifiPhyListener that we are going to sleep.
Time m_endSwitching
end switching
#define max(a, b)
Definition: 80211b.c:43
bool IsStateSleep(void) const
Check whether the current state is SLEEP.
void NotifyOn(void)
Notify all WifiPhyListener that we are going to switch on.
receive notifications about phy events.
void NotifySwitchingStart(Time duration)
Notify all WifiPhyListener that we are switching channel with the given channel switching delay...
void SwitchFromRxEndError(Ptr< Packet > packet, double snr)
Switch from RX after the reception failed.
int64x64_t Max(const int64x64_t &a, const int64x64_t &b)
Maximum.
Definition: int64x64.h:209
WifiMode GetMode(void) const
The PHY layer has sense the medium busy through the CCA mechanism.
Time m_previousStateChangeTime
previous state change time
TracedCallback< Ptr< const Packet >, double, WifiMode, WifiPreamble > m_rxOkTrace
receive OK trace callback
void NotifyMaybeCcaBusyStart(Time duration)
Notify all WifiPhyListener that the CCA has started for the given duration.
RxErrorCallback m_rxErrorCallback
receive error callback
bool IsStateTx(void) const
Check whether the current state is TX.
void SwitchToTx(Time txDuration, Ptr< const Packet > packet, double txPowerDbm, WifiTxVector txVector)
Switch state to TX for the given duration.
void SwitchToSleep(void)
Switch to sleep mode.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
The PHY layer is IDLE.
WifiPhyState
The state of the PHY layer.
Time m_startSwitching
start switching
void SetReceiveOkCallback(RxOkCallback callback)
Set a callback for a successful reception.
Time m_startTx
start transmit
void NotifyTxStart(Time duration, double txPowerDbm)
Notify all WifiPhyListener that the transmission has started for the given duration.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void NotifyRxEndOk(void)
Notify all WifiPhyListener that the reception was successful.
TracedCallback< Ptr< const Packet >, WifiMode, WifiPreamble, uint8_t > m_txTrace
transmit trace callback
static TypeId GetTypeId(void)
Get the type ID.
This objects implements the PHY state machine of the Wifi device.
WifiPhyState GetState(void) const
Return the current state of WifiPhy.
The PHY layer is sending a packet.
void SwitchFromRxEndOk(Ptr< Packet > packet, double snr, WifiTxVector txVector)
Switch from RX after the reception was successful.
void SwitchFromOff(Time duration)
Switch from off mode.
Listeners m_listeners
listeners
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
The PHY layer is receiving a packet.
bool IsStateOff(void) const
Check whether the current state is OFF.
void SetReceiveErrorCallback(RxErrorCallback callback)
Set a callback for a failed reception.
The PHY layer is switched off.
uint8_t GetTxPowerLevel(void) const
TracedCallback< Ptr< const Packet >, double > m_rxErrorTrace
receive error trace callback
The PHY layer is switching to other channel.
A base class which provides memory management and object aggregation.
Definition: object.h:87
void SwitchToRx(Time rxDuration)
Switch state to RX for the given duration.
void LogPreviousIdleAndCcaBusyStates(void)
Log the ideal and CCA states.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
void NotifyRxStart(Time duration)
Notify all WifiPhyListener that the reception has started for the given duration. ...
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void RegisterListener(WifiPhyListener *listener)
Register WifiPhyListener to this WifiPhyStateHelper.
void SwitchMaybeToCcaBusy(Time duration)
Switch to CCA busy.
RxOkCallback m_rxOkCallback
receive OK callback
void NotifyOff(void)
Notify all WifiPhyListener that we are going to switch off.
bool IsStateCcaBusy(void) const
Check whether the current state is CCA busy.