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 "wifi-phy-state-helper.h"
22 #include "ns3/log.h"
23 #include "ns3/simulator.h"
24 #include "ns3/trace-source-accessor.h"
25 #include <algorithm>
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("WifiPhyStateHelper");
30 
31 NS_OBJECT_ENSURE_REGISTERED (WifiPhyStateHelper);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::WifiPhyStateHelper")
37  .SetParent<Object> ()
38  .SetGroupName ("Wifi")
39  .AddConstructor<WifiPhyStateHelper> ()
40  .AddTraceSource ("State",
41  "The state of the PHY layer",
43  "ns3::WifiPhyStateHelper::StateTracedCallback")
44  .AddTraceSource ("RxOk",
45  "A packet has been received successfully.",
47  "ns3::WifiPhyStateHelper::RxOkTracedCallback")
48  .AddTraceSource ("RxError",
49  "A packet has been received unsuccessfully.",
51  "ns3::WifiPhyStateHelper::RxEndErrorTracedCallback")
52  .AddTraceSource ("Tx", "Packet transmission is starting.",
54  "ns3::WifiPhyStateHelper::TxTracedCallback")
55  ;
56  return tid;
57 }
58 
60  : m_rxing (false),
61  m_sleeping (false),
62  m_endTx (Seconds (0)),
63  m_endRx (Seconds (0)),
64  m_endCcaBusy (Seconds (0)),
65  m_endSwitching (Seconds (0)),
66  m_startTx (Seconds (0)),
67  m_startRx (Seconds (0)),
68  m_startCcaBusy (Seconds (0)),
69  m_startSwitching (Seconds (0)),
70  m_startSleep (Seconds (0)),
71  m_previousStateChangeTime (Seconds (0))
72 {
73  NS_LOG_FUNCTION (this);
74 }
75 
76 void
78 {
79  m_rxOkCallback = callback;
80 }
81 
82 void
84 {
85  m_rxErrorCallback = callback;
86 }
87 
88 void
90 {
91  m_listeners.push_back (listener);
92 }
93 
94 void
96 {
97  ListenersI i = find (m_listeners.begin (), m_listeners.end (), listener);
98  if (i != m_listeners.end ())
99  {
100  m_listeners.erase (i);
101  }
102 }
103 
104 bool
106 {
107  return (GetState () == WifiPhy::IDLE);
108 }
109 
110 bool
112 {
113  return (GetState () != WifiPhy::IDLE);
114 }
115 
116 bool
118 {
119  return (GetState () == WifiPhy::CCA_BUSY);
120 }
121 
122 bool
124 {
125  return (GetState () == WifiPhy::RX);
126 }
127 
128 bool
130 {
131  return (GetState () == WifiPhy::TX);
132 }
133 
134 bool
136 {
137  return (GetState () == WifiPhy::SWITCHING);
138 }
139 
140 bool
142 {
143  return (GetState () == WifiPhy::SLEEP);
144 }
145 
146 Time
148 {
150 }
151 
152 Time
154 {
155  Time retval;
156 
157  switch (GetState ())
158  {
159  case WifiPhy::RX:
160  retval = m_endRx - Simulator::Now ();
161  break;
162  case WifiPhy::TX:
163  retval = m_endTx - Simulator::Now ();
164  break;
165  case WifiPhy::CCA_BUSY:
166  retval = m_endCcaBusy - Simulator::Now ();
167  break;
168  case WifiPhy::SWITCHING:
169  retval = m_endSwitching - Simulator::Now ();
170  break;
171  case WifiPhy::IDLE:
172  retval = Seconds (0);
173  break;
174  case WifiPhy::SLEEP:
175  NS_FATAL_ERROR ("Cannot determine when the device will wake up.");
176  retval = Seconds (0);
177  break;
178  default:
179  NS_FATAL_ERROR ("Invalid WifiPhy state.");
180  retval = Seconds (0);
181  break;
182  }
183  retval = Max (retval, Seconds (0));
184  return retval;
185 }
186 
187 Time
189 {
190  return m_startRx;
191 }
192 
193 enum WifiPhy::State
195 {
196  if (m_sleeping)
197  {
198  return WifiPhy::SLEEP;
199  }
200  else if (m_endTx > Simulator::Now ())
201  {
202  return WifiPhy::TX;
203  }
204  else if (m_rxing)
205  {
206  return WifiPhy::RX;
207  }
208  else if (m_endSwitching > Simulator::Now ())
209  {
210  return WifiPhy::SWITCHING;
211  }
212  else if (m_endCcaBusy > Simulator::Now ())
213  {
214  return WifiPhy::CCA_BUSY;
215  }
216  else
217  {
218  return WifiPhy::IDLE;
219  }
220 }
221 
222 void
223 WifiPhyStateHelper::NotifyTxStart (Time duration, double txPowerDbm)
224 {
225  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
226  {
227  (*i)->NotifyTxStart (duration, txPowerDbm);
228  }
229 }
230 
231 void
233 {
234  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
235  {
236  (*i)->NotifyRxStart (duration);
237  }
238 }
239 
240 void
242 {
243  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
244  {
245  (*i)->NotifyRxEndOk ();
246  }
247 }
248 
249 void
251 {
252  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
253  {
254  (*i)->NotifyRxEndError ();
255  }
256 }
257 
258 void
260 {
261  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
262  {
263  (*i)->NotifyMaybeCcaBusyStart (duration);
264  }
265 }
266 
267 void
269 {
270  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
271  {
272  (*i)->NotifySwitchingStart (duration);
273  }
274 }
275 
276 void
278 {
279  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
280  {
281  (*i)->NotifySleep ();
282  }
283 }
284 
285 void
287 {
288  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
289  {
290  (*i)->NotifyWakeup ();
291  }
292 }
293 
294 void
296 {
297  Time now = Simulator::Now ();
298  Time idleStart = Max (m_endCcaBusy, m_endRx);
299  idleStart = Max (idleStart, m_endTx);
300  idleStart = Max (idleStart, m_endSwitching);
301  NS_ASSERT (idleStart <= now);
302  if (m_endCcaBusy > m_endRx
304  && m_endCcaBusy > m_endTx)
305  {
306  Time ccaBusyStart = Max (m_endTx, m_endRx);
307  ccaBusyStart = Max (ccaBusyStart, m_startCcaBusy);
308  ccaBusyStart = Max (ccaBusyStart, m_endSwitching);
309  m_stateLogger (ccaBusyStart, idleStart - ccaBusyStart, WifiPhy::CCA_BUSY);
310  }
311  m_stateLogger (idleStart, now - idleStart, WifiPhy::IDLE);
312 }
313 
314 void
315 WifiPhyStateHelper::SwitchToTx (Time txDuration, Ptr<const Packet> packet, double txPowerDbm,
316  WifiTxVector txVector, WifiPreamble preamble)
317 {
318  m_txTrace (packet, txVector.GetMode (), preamble, txVector.GetTxPowerLevel ());
319  Time now = Simulator::Now ();
320  switch (GetState ())
321  {
322  case WifiPhy::RX:
323  /* The packet which is being received as well
324  * as its endRx event are cancelled by the caller.
325  */
326  m_rxing = false;
328  m_endRx = now;
329  break;
330  case WifiPhy::CCA_BUSY:
331  {
332  Time ccaStart = Max (m_endRx, m_endTx);
333  ccaStart = Max (ccaStart, m_startCcaBusy);
334  ccaStart = Max (ccaStart, m_endSwitching);
335  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
336  } break;
337  case WifiPhy::IDLE:
339  break;
340  case WifiPhy::SWITCHING:
341  case WifiPhy::SLEEP:
342  default:
343  NS_FATAL_ERROR ("Invalid WifiPhy state.");
344  break;
345  }
346  m_stateLogger (now, txDuration, WifiPhy::TX);
348  m_endTx = now + txDuration;
349  m_startTx = now;
350  NotifyTxStart (txDuration, txPowerDbm);
351 }
352 
353 void
355 {
357  NS_ASSERT (!m_rxing);
358  Time now = Simulator::Now ();
359  switch (GetState ())
360  {
361  case WifiPhy::IDLE:
363  break;
364  case WifiPhy::CCA_BUSY:
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, WifiPhy::CCA_BUSY);
370  } break;
371  case WifiPhy::SWITCHING:
372  case WifiPhy::RX:
373  case WifiPhy::TX:
374  case WifiPhy::SLEEP:
375  NS_FATAL_ERROR ("Invalid WifiPhy state.");
376  break;
377  }
379  m_rxing = true;
380  m_startRx = now;
381  m_endRx = now + rxDuration;
382  NotifyRxStart (rxDuration);
383  NS_ASSERT (IsStateRx ());
384 }
385 
386 void
388 {
389  Time now = Simulator::Now ();
390  switch (GetState ())
391  {
392  case WifiPhy::RX:
393  /* The packet which is being received as well
394  * as its endRx event are cancelled by the caller.
395  */
396  m_rxing = false;
398  m_endRx = now;
399  break;
400  case WifiPhy::CCA_BUSY:
401  {
402  Time ccaStart = Max (m_endRx, m_endTx);
403  ccaStart = Max (ccaStart, m_startCcaBusy);
404  ccaStart = Max (ccaStart, m_endSwitching);
405  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
406  } break;
407  case WifiPhy::IDLE:
409  break;
410  case WifiPhy::TX:
411  case WifiPhy::SWITCHING:
412  case WifiPhy::SLEEP:
413  default:
414  NS_FATAL_ERROR ("Invalid WifiPhy state.");
415  break;
416  }
417 
418  if (now < m_endCcaBusy)
419  {
420  m_endCcaBusy = now;
421  }
422 
423  m_stateLogger (now, switchingDuration, WifiPhy::SWITCHING);
425  m_startSwitching = now;
426  m_endSwitching = now + switchingDuration;
427  NotifySwitchingStart (switchingDuration);
429 }
430 
431 void
432 WifiPhyStateHelper::SwitchFromRxEndOk (Ptr<Packet> packet, double snr, WifiTxVector txVector, enum WifiPreamble preamble)
433 {
434  m_rxOkTrace (packet, snr, txVector.GetMode (), preamble);
435  NotifyRxEndOk ();
436  DoSwitchFromRx ();
437  if (!m_rxOkCallback.IsNull ())
438  {
439  m_rxOkCallback (packet, snr, txVector, preamble);
440  }
441 
442 }
443 
444 void
446 {
447  m_rxErrorTrace (packet, snr);
448  NotifyRxEndError ();
449  DoSwitchFromRx ();
450  if (!m_rxErrorCallback.IsNull ())
451  {
452  m_rxErrorCallback (packet, snr);
453  }
454 }
455 
456 void
458 {
459  NS_ASSERT (IsStateRx ());
460  NS_ASSERT (m_rxing);
461 
462  Time now = Simulator::Now ();
465  m_rxing = false;
466 
468 }
469 
470 void
472 {
473  NotifyMaybeCcaBusyStart (duration);
474  Time now = Simulator::Now ();
475  switch (GetState ())
476  {
477  case WifiPhy::SWITCHING:
478  break;
479  case WifiPhy::SLEEP:
480  break;
481  case WifiPhy::IDLE:
483  break;
484  case WifiPhy::CCA_BUSY:
485  break;
486  case WifiPhy::RX:
487  break;
488  case WifiPhy::TX:
489  break;
490  }
491  if (GetState () != WifiPhy::CCA_BUSY)
492  {
493  m_startCcaBusy = now;
494  }
495  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
496 }
497 
498 void
500 {
501  Time now = Simulator::Now ();
502  switch (GetState ())
503  {
504  case WifiPhy::IDLE:
506  break;
507  case WifiPhy::CCA_BUSY:
508  {
509  Time ccaStart = Max (m_endRx, m_endTx);
510  ccaStart = Max (ccaStart, m_startCcaBusy);
511  ccaStart = Max (ccaStart, m_endSwitching);
512  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
513  } break;
514  case WifiPhy::RX:
515  case WifiPhy::SWITCHING:
516  case WifiPhy::TX:
517  case WifiPhy::SLEEP:
518  NS_FATAL_ERROR ("Invalid WifiPhy state.");
519  break;
520  }
522  m_sleeping = true;
523  m_startSleep = now;
524  NotifySleep ();
525  NS_ASSERT (IsStateSleep ());
526 }
527 
528 void
530 {
531  NS_ASSERT (IsStateSleep ());
532  Time now = Simulator::Now ();
535  m_sleeping = false;
536  NotifyWakeup ();
537  //update m_endCcaBusy after the sleep period
538  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
539  if (m_endCcaBusy > now)
540  {
542  }
543 }
544 
545 } //namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void DoSwitchFromRx(void)
Switch the state from RX.
Time GetStateDuration(void)
Return the elapsed time of the current state.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SwitchFromRxEndOk(Ptr< Packet > packet, double snr, WifiTxVector txVector, enum WifiPreamble preamble)
Switch from RX after the reception was successful.
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:44
void NotifyWakeup(void)
Notify all WifiPhyListener that we woke up.
The PHY layer has sense the medium busy through the CCA mechanism.
Definition: wifi-phy.h:177
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
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:201
The PHY layer is sleeping.
Definition: wifi-phy.h:193
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
std::vector< WifiPhyListener * >::iterator ListenersI
void SetReceiveOkCallback(WifiPhy::RxOkCallback callback)
Set a callback for a successful reception.
void SwitchFromSleep(Time duration)
Switch from sleep mode.
uint8_t GetTxPowerLevel(void) const
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:30
bool IsStateIdle(void)
Check whether the current state is IDLE.
Time GetDelayUntilIdle(void)
Return the time before the state is back to IDLE.
void NotifySleep(void)
Notify all WifiPhyListener that we are going to sleep.
#define max(a, b)
Definition: 80211b.c:45
TracedCallback< Ptr< const Packet >, double, WifiMode, enum WifiPreamble > m_rxOkTrace
bool IsStateSwitching(void)
Check whether the current state is SWITCHING.
receive notifications about phy events.
Definition: wifi-phy.h:81
void NotifySwitchingStart(Time duration)
Notify all WifiPhyListener that we are switching channel with the given channel switching delay...
The PHY layer is IDLE.
Definition: wifi-phy.h:173
bool IsStateRx(void)
Check whether the current state is RX.
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
void SetReceiveErrorCallback(WifiPhy::RxErrorCallback callback)
Set a callback for a failed reception.
The PHY layer is receiving a packet.
Definition: wifi-phy.h:185
bool IsStateSleep(void)
Check whether the current state is SLEEP.
The PHY layer is sending a packet.
Definition: wifi-phy.h:181
void NotifyMaybeCcaBusyStart(Time duration)
Notify all WifiPhyListener that the CCA has started for the given duration.
WifiPhy::RxErrorCallback m_rxErrorCallback
The PHY layer is switching to other channel.
Definition: wifi-phy.h:189
void SwitchToSleep(void)
Switch to sleep mode.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:224
void NotifyRxEndOk(void)
Notify all WifiPhyListener that the reception was successful.
static TypeId GetTypeId(void)
TracedCallback< Time, Time, enum WifiPhy::State > m_stateLogger
enum WifiPhy::State GetState(void)
Return the current state of WifiPhy.
bool IsStateBusy(void)
Check whether the current state is not IDLE.
This objects implements the PHY state machine of the Wifi device.
bool IsStateCcaBusy(void)
Check whether the current state is CCA busy.
TracedCallback< Ptr< const Packet >, WifiMode, WifiPreamble, uint8_t > m_txTrace
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
TracedCallback< Ptr< const Packet >, double > m_rxErrorTrace
bool IsStateTx(void)
Check whether the current state is TX.
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.
Time GetLastRxStartTime(void) const
Return the time the last RX start.
WifiMode GetMode(void) const
State
The state of the PHY layer.
Definition: wifi-phy.h:168
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:904
void RegisterListener(WifiPhyListener *listener)
Register WifiPhyListener to this WifiPhyStateHelper.
void SwitchMaybeToCcaBusy(Time duration)
Switch to CCA busy.
void SwitchToTx(Time txDuration, Ptr< const Packet > packet, double txPowerDbm, WifiTxVector txVector, WifiPreamble preamble)
Switch state to TX for the given duration.
WifiPhy::RxOkCallback m_rxOkCallback