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 #include "wifi-psdu.h"
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("WifiPhyStateHelper");
33 
34 NS_OBJECT_ENSURE_REGISTERED (WifiPhyStateHelper);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::WifiPhyStateHelper")
40  .SetParent<Object> ()
41  .SetGroupName ("Wifi")
42  .AddConstructor<WifiPhyStateHelper> ()
43  .AddTraceSource ("State",
44  "The state of the PHY layer",
46  "ns3::WifiPhyStateHelper::StateTracedCallback")
47  .AddTraceSource ("RxOk",
48  "A packet has been received successfully.",
50  "ns3::WifiPhyStateHelper::RxOkTracedCallback")
51  .AddTraceSource ("RxError",
52  "A packet has been received unsuccessfully.",
54  "ns3::WifiPhyStateHelper::RxEndErrorTracedCallback")
55  .AddTraceSource ("Tx", "Packet transmission is starting.",
57  "ns3::WifiPhyStateHelper::TxTracedCallback")
58  ;
59  return tid;
60 }
61 
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_endRx > Simulator::Now ())
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  */
362  m_endRx = now;
363  break;
365  {
366  Time ccaStart = Max (m_endRx, m_endTx);
367  ccaStart = Max (ccaStart, m_startCcaBusy);
368  ccaStart = Max (ccaStart, m_endSwitching);
369  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
370  } break;
371  case WifiPhyState::IDLE:
373  break;
374  default:
375  NS_FATAL_ERROR ("Invalid WifiPhy state.");
376  break;
377  }
378  m_stateLogger (now, txDuration, WifiPhyState::TX);
380  m_endTx = now + txDuration;
381  m_startTx = now;
382  NotifyTxStart (txDuration, txPowerDbm);
383 }
384 
385 void
387 {
388  NS_LOG_FUNCTION (this << rxDuration);
390  Time now = Simulator::Now ();
391  switch (GetState ())
392  {
393  case WifiPhyState::IDLE:
395  break;
397  {
398  Time ccaStart = Max (m_endRx, m_endTx);
399  ccaStart = Max (ccaStart, m_startCcaBusy);
400  ccaStart = Max (ccaStart, m_endSwitching);
401  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
402  } break;
403  default:
404  NS_FATAL_ERROR ("Invalid WifiPhy state " << GetState ());
405  break;
406  }
408  m_startRx = now;
409  m_endRx = now + rxDuration;
410  NotifyRxStart (rxDuration);
411  NS_ASSERT (IsStateRx ());
412 }
413 
414 void
416 {
417  NS_LOG_FUNCTION (this << switchingDuration);
418  Time now = Simulator::Now ();
419  switch (GetState ())
420  {
421  case WifiPhyState::RX:
422  /* The packet which is being received as well
423  * as its endRx event are cancelled by the caller.
424  */
426  m_endRx = now;
427  break;
429  {
430  Time ccaStart = Max (m_endRx, m_endTx);
431  ccaStart = Max (ccaStart, m_startCcaBusy);
432  ccaStart = Max (ccaStart, m_endSwitching);
433  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
434  } break;
435  case WifiPhyState::IDLE:
437  break;
438  default:
439  NS_FATAL_ERROR ("Invalid WifiPhy state.");
440  break;
441  }
442 
443  if (now < m_endCcaBusy)
444  {
445  m_endCcaBusy = now;
446  }
447 
448  m_stateLogger (now, switchingDuration, WifiPhyState::SWITCHING);
450  m_startSwitching = now;
451  m_endSwitching = now + switchingDuration;
452  NotifySwitchingStart (switchingDuration);
454 }
455 
456 void
457 WifiPhyStateHelper::SwitchFromRxEndOk (Ptr<WifiPsdu> psdu, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu)
458 {
459  NS_LOG_FUNCTION (this << *psdu << snr << txVector << statusPerMpdu.size () <<
460  std::all_of(statusPerMpdu.begin(), statusPerMpdu.end(), [](bool v) { return v; })); //returns true if all true
461  NS_ASSERT (statusPerMpdu.size () != 0);
463  m_rxOkTrace (psdu->GetPacket (), snr, txVector.GetMode (), txVector.GetPreambleType ());
464  NotifyRxEndOk ();
465  DoSwitchFromRx ();
466  if (!m_rxOkCallback.IsNull ())
467  {
468  m_rxOkCallback (psdu, snr, txVector, statusPerMpdu);
469  }
470 
471 }
472 
473 void
475 {
476  NS_LOG_FUNCTION (this << *psdu << snr);
478  m_rxErrorTrace (psdu->GetPacket (), snr);
479  NotifyRxEndError ();
480  DoSwitchFromRx ();
481  if (!m_rxErrorCallback.IsNull ())
482  {
483  m_rxErrorCallback (psdu);
484  }
485 }
486 
487 void
489 {
490  NS_LOG_FUNCTION (this);
491  Time now = Simulator::Now ();
494  m_endRx = Simulator::Now ();
496 }
497 
498 void
500 {
501  NS_LOG_FUNCTION (this << duration);
502  if (GetState () != WifiPhyState::RX)
503  {
504  NotifyMaybeCcaBusyStart (duration);
505  }
506  Time now = Simulator::Now ();
507  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
508  switch (GetState ())
509  {
510  case WifiPhyState::IDLE:
512  break;
513  case WifiPhyState::RX:
514  return;
515  default:
516  break;
517  }
519  {
520  m_startCcaBusy = now;
521  }
522  m_stateLogger (now, duration, WifiPhyState::CCA_BUSY);
523 }
524 
525 void
527 {
528  NS_LOG_FUNCTION (this);
529  Time now = Simulator::Now ();
530  switch (GetState ())
531  {
532  case WifiPhyState::IDLE:
534  break;
536  {
537  Time ccaStart = Max (m_endRx, m_endTx);
538  ccaStart = Max (ccaStart, m_startCcaBusy);
539  ccaStart = Max (ccaStart, m_endSwitching);
540  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
541  } break;
542  default:
543  NS_FATAL_ERROR ("Invalid WifiPhy state.");
544  break;
545  }
547  m_sleeping = true;
548  m_startSleep = now;
549  NotifySleep ();
550  NS_ASSERT (IsStateSleep ());
551 }
552 
553 void
555 {
556  NS_LOG_FUNCTION (this << duration);
557  NS_ASSERT (IsStateSleep ());
558  Time now = Simulator::Now ();
561  m_sleeping = false;
562  NotifyWakeup ();
563  //update m_endCcaBusy after the sleep period
564  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
565  if (m_endCcaBusy > now)
566  {
568  }
569 }
570 
571 void
573 {
574  NS_LOG_FUNCTION (this);
575  NS_ASSERT (IsStateRx ());
576  if (failure)
577  {
578  NotifyRxEndError ();
579  }
580  else
581  {
582  NotifyRxEndOk ();
583  }
584  DoSwitchFromRx ();
585  NS_ASSERT (!IsStateRx ());
586 }
587 
588 void
590 {
591  NS_LOG_FUNCTION (this);
592  Time now = Simulator::Now ();
593  switch (GetState ())
594  {
595  case WifiPhyState::RX:
596  /* The packet which is being received as well
597  * as its endRx event are cancelled by the caller.
598  */
600  m_endRx = now;
601  break;
602  case WifiPhyState::TX:
603  /* The packet which is being transmitted as well
604  * as its endTx event are cancelled by the caller.
605  */
607  m_endTx = now;
608  break;
609  case WifiPhyState::IDLE:
611  break;
613  {
614  Time ccaStart = Max (m_endRx, m_endTx);
615  ccaStart = Max (ccaStart, m_startCcaBusy);
616  ccaStart = Max (ccaStart, m_endSwitching);
617  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
618  } break;
619  default:
620  NS_FATAL_ERROR ("Invalid WifiPhy state.");
621  break;
622  }
624  m_isOff = true;
625  NotifyOff ();
626  NS_ASSERT (IsStateOff ());
627 }
628 
629 void
631 {
632  NS_LOG_FUNCTION (this << duration);
633  NS_ASSERT (IsStateOff ());
634  Time now = Simulator::Now ();
636  m_isOff = false;
637  NotifyOn ();
638  //update m_endCcaBusy after the off period
639  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
640  if (m_endCcaBusy > now)
641  {
643  }
644 }
645 
646 } //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:205
#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...
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.
void SwitchFromRxAbort(bool failure)
Abort current 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:193
void NotifyRxEndOk(void)
Notify all WifiPhyListener that the reception was successful.
void SwitchFromRxEndError(Ptr< WifiPsdu > psdu, double snr)
Switch from RX after the reception failed.
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 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:1064
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
void SwitchFromRxEndOk(Ptr< WifiPsdu > psdu, double snr, WifiTxVector txVector, std::vector< bool > statusPerMpdu)
Switch from RX after the reception was successful.
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:1372
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:922
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.