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  case WifiPhyState::SLEEP:
170  case WifiPhyState::OFF:
171  retval = Seconds (0);
172  break;
173  default:
174  NS_FATAL_ERROR ("Invalid WifiPhy state.");
175  retval = Seconds (0);
176  break;
177  }
178  retval = Max (retval, Seconds (0));
179  return retval;
180 }
181 
182 Time
184 {
185  return m_startRx;
186 }
187 
188 Time
190 {
191  return m_endRx;
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  Time ccaBusyDuration = idleStart - ccaBusyStart;
344  if (ccaBusyDuration.IsStrictlyPositive ())
345  {
346  m_stateLogger (ccaBusyStart, ccaBusyDuration, WifiPhyState::CCA_BUSY);
347  }
348  }
349  Time idleDuration = now - idleStart;
350  if (idleDuration.IsStrictlyPositive ())
351  {
352  m_stateLogger (idleStart, idleDuration, WifiPhyState::IDLE);
353  }
354 }
355 
356 void
357 WifiPhyStateHelper::SwitchToTx (Time txDuration, Ptr<const Packet> packet, double txPowerDbm,
358  WifiTxVector txVector)
359 {
360  NS_LOG_FUNCTION (this << txDuration << packet << txPowerDbm << txVector);
361  m_txTrace (packet, txVector.GetMode (), txVector.GetPreambleType (), txVector.GetTxPowerLevel ());
362  Time now = Simulator::Now ();
363  switch (GetState ())
364  {
365  case WifiPhyState::RX:
366  /* The packet which is being received as well
367  * as its endRx event are cancelled by the caller.
368  */
370  m_endRx = now;
371  break;
373  {
374  Time ccaStart = Max (m_endRx, m_endTx);
375  ccaStart = Max (ccaStart, m_startCcaBusy);
376  ccaStart = Max (ccaStart, m_endSwitching);
377  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
378  } break;
379  case WifiPhyState::IDLE:
381  break;
382  default:
383  NS_FATAL_ERROR ("Invalid WifiPhy state.");
384  break;
385  }
386  m_stateLogger (now, txDuration, WifiPhyState::TX);
388  m_endTx = now + txDuration;
389  m_startTx = now;
390  NotifyTxStart (txDuration, txPowerDbm);
391 }
392 
393 void
395 {
396  NS_LOG_FUNCTION (this << rxDuration);
398  Time now = Simulator::Now ();
399  switch (GetState ())
400  {
401  case WifiPhyState::IDLE:
403  break;
405  {
406  Time ccaStart = Max (m_endRx, m_endTx);
407  ccaStart = Max (ccaStart, m_startCcaBusy);
408  ccaStart = Max (ccaStart, m_endSwitching);
409  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
410  } break;
411  default:
412  NS_FATAL_ERROR ("Invalid WifiPhy state " << GetState ());
413  break;
414  }
416  m_startRx = now;
417  m_endRx = now + rxDuration;
418  NotifyRxStart (rxDuration);
419  NS_ASSERT (IsStateRx ());
420 }
421 
422 void
424 {
425  NS_LOG_FUNCTION (this << switchingDuration);
426  Time now = Simulator::Now ();
427  switch (GetState ())
428  {
429  case WifiPhyState::RX:
430  /* The packet which is being received as well
431  * as its endRx event are cancelled by the caller.
432  */
434  m_endRx = now;
435  break;
437  {
438  Time ccaStart = Max (m_endRx, m_endTx);
439  ccaStart = Max (ccaStart, m_startCcaBusy);
440  ccaStart = Max (ccaStart, m_endSwitching);
441  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
442  } break;
443  case WifiPhyState::IDLE:
445  break;
446  default:
447  NS_FATAL_ERROR ("Invalid WifiPhy state.");
448  break;
449  }
450 
451  if (now < m_endCcaBusy)
452  {
453  m_endCcaBusy = now;
454  }
455 
456  m_stateLogger (now, switchingDuration, WifiPhyState::SWITCHING);
458  m_startSwitching = now;
459  m_endSwitching = now + switchingDuration;
460  NotifySwitchingStart (switchingDuration);
462 }
463 
464 void
465 WifiPhyStateHelper::SwitchFromRxEndOk (Ptr<WifiPsdu> psdu, double snr, WifiTxVector txVector, std::vector<bool> statusPerMpdu)
466 {
467  NS_LOG_FUNCTION (this << *psdu << snr << txVector << statusPerMpdu.size () <<
468  std::all_of(statusPerMpdu.begin(), statusPerMpdu.end(), [](bool v) { return v; })); //returns true if all true
469  NS_ASSERT (statusPerMpdu.size () != 0);
471  m_rxOkTrace (psdu->GetPacket (), snr, txVector.GetMode (), txVector.GetPreambleType ());
472  NotifyRxEndOk ();
473  DoSwitchFromRx ();
474  if (!m_rxOkCallback.IsNull ())
475  {
476  m_rxOkCallback (psdu, snr, txVector, statusPerMpdu);
477  }
478 
479 }
480 
481 void
483 {
484  NS_LOG_FUNCTION (this << *psdu << snr);
486  m_rxErrorTrace (psdu->GetPacket (), snr);
487  NotifyRxEndError ();
488  DoSwitchFromRx ();
489  if (!m_rxErrorCallback.IsNull ())
490  {
491  m_rxErrorCallback (psdu);
492  }
493 }
494 
495 void
497 {
498  NS_LOG_FUNCTION (this);
499  Time now = Simulator::Now ();
502  m_endRx = Simulator::Now ();
504 }
505 
506 void
508 {
509  NS_LOG_FUNCTION (this << duration);
510  if (GetState () != WifiPhyState::RX)
511  {
512  NotifyMaybeCcaBusyStart (duration);
513  }
514  Time now = Simulator::Now ();
515  switch (GetState ())
516  {
517  case WifiPhyState::IDLE:
519  break;
520  case WifiPhyState::RX:
521  return;
522  default:
523  break;
524  }
526  {
527  m_startCcaBusy = now;
528  }
529  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
530 }
531 
532 void
534 {
535  NS_LOG_FUNCTION (this);
536  Time now = Simulator::Now ();
537  switch (GetState ())
538  {
539  case WifiPhyState::IDLE:
541  break;
543  {
544  Time ccaStart = Max (m_endRx, m_endTx);
545  ccaStart = Max (ccaStart, m_startCcaBusy);
546  ccaStart = Max (ccaStart, m_endSwitching);
547  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
548  } break;
549  default:
550  NS_FATAL_ERROR ("Invalid WifiPhy state.");
551  break;
552  }
554  m_sleeping = true;
555  m_startSleep = now;
556  NotifySleep ();
557  NS_ASSERT (IsStateSleep ());
558 }
559 
560 void
562 {
563  NS_LOG_FUNCTION (this << duration);
564  NS_ASSERT (IsStateSleep ());
565  Time now = Simulator::Now ();
568  m_sleeping = false;
569  NotifyWakeup ();
570  //update m_endCcaBusy after the sleep period
571  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
572  if (m_endCcaBusy > now)
573  {
575  }
576 }
577 
578 void
580 {
581  NS_LOG_FUNCTION (this);
582  NS_ASSERT (IsStateRx ());
583  NotifyRxEndOk ();
584  DoSwitchFromRx ();
587  NS_ASSERT (IsStateIdle ());
588 }
589 
590 void
592 {
593  NS_LOG_FUNCTION (this);
594  Time now = Simulator::Now ();
595  switch (GetState ())
596  {
597  case WifiPhyState::RX:
598  /* The packet which is being received as well
599  * as its endRx event are cancelled by the caller.
600  */
602  m_endRx = now;
603  break;
604  case WifiPhyState::TX:
605  /* The packet which is being transmitted as well
606  * as its endTx event are cancelled by the caller.
607  */
609  m_endTx = now;
610  break;
611  case WifiPhyState::IDLE:
613  break;
615  {
616  Time ccaStart = Max (m_endRx, m_endTx);
617  ccaStart = Max (ccaStart, m_startCcaBusy);
618  ccaStart = Max (ccaStart, m_endSwitching);
619  m_stateLogger (ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
620  } break;
621  default:
622  NS_FATAL_ERROR ("Invalid WifiPhy state.");
623  break;
624  }
626  m_isOff = true;
627  NotifyOff ();
628  NS_ASSERT (IsStateOff ());
629 }
630 
631 void
633 {
634  NS_LOG_FUNCTION (this << duration);
635  NS_ASSERT (IsStateOff ());
636  Time now = Simulator::Now ();
638  m_isOff = false;
639  NotifyOn ();
640  //update m_endCcaBusy after the off period
641  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
642  if (m_endCcaBusy > now)
643  {
645  }
646 }
647 
648 } //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.
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
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
bool IsStrictlyPositive(void) const
Definition: nstime.h:314
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:230
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.
Time GetLastRxEndTime(void) const
Return the time the last RX end.
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:195
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:1062
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:1386
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:923
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.