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 #include "wifi-phy-state-helper.h"
21 #include "ns3/log.h"
22 #include "ns3/simulator.h"
23 #include "ns3/trace-source-accessor.h"
24 #include <algorithm>
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("WifiPhyStateHelper");
29 
30 NS_OBJECT_ENSURE_REGISTERED (WifiPhyStateHelper);
31 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::WifiPhyStateHelper")
36  .SetParent<Object> ()
37  .AddConstructor<WifiPhyStateHelper> ()
38  .AddTraceSource ("State",
39  "The state of the PHY layer",
41  "ns3::WifiPhyStateHelper::StateTracedCallback")
42  .AddTraceSource ("RxOk",
43  "A packet has been received successfully.",
45  "ns3::WifiPhyStateHelper::RxOkTracedCallback")
46  .AddTraceSource ("RxError",
47  "A packet has been received unsuccessfully.",
49  "ns3::WifiPhyStateHelper::RxErrorTracedCallback")
50  .AddTraceSource ("Tx", "Packet transmission is starting.",
52  "ns3::WifiPhyStateHelper::TxTracedCallback")
53  ;
54  return tid;
55 }
56 
58  : m_rxing (false),
59  m_sleeping (false),
60  m_endTx (Seconds (0)),
61  m_endRx (Seconds (0)),
62  m_endCcaBusy (Seconds (0)),
63  m_endSwitching (Seconds (0)),
64  m_startTx (Seconds (0)),
65  m_startRx (Seconds (0)),
66  m_startCcaBusy (Seconds (0)),
67  m_startSwitching (Seconds (0)),
68  m_startSleep (Seconds (0)),
69  m_previousStateChangeTime (Seconds (0))
70 {
71  NS_LOG_FUNCTION (this);
72 }
73 
74 void
76 {
77  m_rxOkCallback = callback;
78 }
79 void
81 {
82  m_rxErrorCallback = callback;
83 }
84 void
86 {
87  m_listeners.push_back (listener);
88 }
89 void
91 {
92  ListenersI i = find (m_listeners.begin(), m_listeners.end(), listener);
93  if (i != m_listeners.end())
94  {
95  m_listeners.erase(i);
96  }
97 }
98 
99 bool
101 {
102  return (GetState () == WifiPhy::IDLE);
103 }
104 bool
106 {
107  return (GetState () != WifiPhy::IDLE);
108 }
109 bool
111 {
112  return (GetState () == WifiPhy::CCA_BUSY);
113 }
114 bool
116 {
117  return (GetState () == WifiPhy::RX);
118 }
119 bool
121 {
122  return (GetState () == WifiPhy::TX);
123 }
124 bool
126 {
127  return (GetState () == WifiPhy::SWITCHING);
128 }
129 bool
131 {
132  return (GetState () == WifiPhy::SLEEP);
133 }
134 
135 
136 
137 Time
139 {
141 }
142 
143 Time
145 {
146  Time retval;
147 
148  switch (GetState ())
149  {
150  case WifiPhy::RX:
151  retval = m_endRx - Simulator::Now ();
152  break;
153  case WifiPhy::TX:
154  retval = m_endTx - Simulator::Now ();
155  break;
156  case WifiPhy::CCA_BUSY:
157  retval = m_endCcaBusy - Simulator::Now ();
158  break;
159  case WifiPhy::SWITCHING:
160  retval = m_endSwitching - Simulator::Now ();
161  break;
162  case WifiPhy::IDLE:
163  retval = Seconds (0);
164  break;
165  case WifiPhy::SLEEP:
166  NS_FATAL_ERROR ("Cannot determine when the device will wake up.");
167  retval = Seconds (0);
168  break;
169  default:
170  NS_FATAL_ERROR ("Invalid WifiPhy state.");
171  retval = Seconds (0);
172  break;
173  }
174  retval = Max (retval, Seconds (0));
175  return retval;
176 }
177 
178 Time
180 {
181  return m_startRx;
182 }
183 
184 enum WifiPhy::State
186 {
187  if (m_sleeping)
188  {
189  return WifiPhy::SLEEP;
190  }
191  else if (m_endTx > Simulator::Now ())
192  {
193  return WifiPhy::TX;
194  }
195  else if (m_rxing)
196  {
197  return WifiPhy::RX;
198  }
199  else if (m_endSwitching > Simulator::Now ())
200  {
201  return WifiPhy::SWITCHING;
202  }
203  else if (m_endCcaBusy > Simulator::Now ())
204  {
205  return WifiPhy::CCA_BUSY;
206  }
207  else
208  {
209  return WifiPhy::IDLE;
210  }
211 }
212 
213 
214 void
215 WifiPhyStateHelper::NotifyTxStart (Time duration, double txPowerDbm)
216 {
217  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
218  {
219  (*i)->NotifyTxStart (duration, txPowerDbm);
220  }
221 }
222 void
224 {
225  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
226  {
227  (*i)->NotifyRxStart (duration);
228  }
229 }
230 void
232 {
233  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
234  {
235  (*i)->NotifyRxEndOk ();
236  }
237 }
238 void
240 {
241  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
242  {
243  (*i)->NotifyRxEndError ();
244  }
245 }
246 void
248 {
249  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
250  {
251  (*i)->NotifyMaybeCcaBusyStart (duration);
252  }
253 }
254 void
256 {
257  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
258  {
259  (*i)->NotifySwitchingStart (duration);
260  }
261 }
262 void
264 {
265  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
266  {
267  (*i)->NotifySleep ();
268  }
269 }
270 void
272 {
273  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
274  {
275  (*i)->NotifyWakeup ();
276  }
277 }
278 
279 
280 void
282 {
283  Time now = Simulator::Now ();
284  Time idleStart = Max (m_endCcaBusy, m_endRx);
285  idleStart = Max (idleStart, m_endTx);
286  idleStart = Max (idleStart, m_endSwitching);
287  NS_ASSERT (idleStart <= now);
288  if (m_endCcaBusy > m_endRx
290  && m_endCcaBusy > m_endTx)
291  {
292  Time ccaBusyStart = Max (m_endTx, m_endRx);
293  ccaBusyStart = Max (ccaBusyStart, m_startCcaBusy);
294  ccaBusyStart = Max (ccaBusyStart, m_endSwitching);
295  m_stateLogger (ccaBusyStart, idleStart - ccaBusyStart, WifiPhy::CCA_BUSY);
296  }
297  m_stateLogger (idleStart, now - idleStart, WifiPhy::IDLE);
298 }
299 
300 void
301 WifiPhyStateHelper::SwitchToTx (Time txDuration, Ptr<const Packet> packet, double txPowerDbm,
302  WifiTxVector txVector, WifiPreamble preamble)
303 {
304  m_txTrace (packet, txVector.GetMode(), preamble, txVector.GetTxPowerLevel());
305  Time now = Simulator::Now ();
306  switch (GetState ())
307  {
308  case WifiPhy::RX:
309  /* The packet which is being received as well
310  * as its endRx event are cancelled by the caller.
311  */
312  m_rxing = false;
314  m_endRx = now;
315  break;
316  case WifiPhy::CCA_BUSY:
317  {
318  Time ccaStart = Max (m_endRx, m_endTx);
319  ccaStart = Max (ccaStart, m_startCcaBusy);
320  ccaStart = Max (ccaStart, m_endSwitching);
321  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
322  } break;
323  case WifiPhy::IDLE:
325  break;
326  case WifiPhy::SWITCHING:
327  case WifiPhy::SLEEP:
328  default:
329  NS_FATAL_ERROR ("Invalid WifiPhy state.");
330  break;
331  }
332  m_stateLogger (now, txDuration, WifiPhy::TX);
334  m_endTx = now + txDuration;
335  m_startTx = now;
336  NotifyTxStart (txDuration, txPowerDbm);
337 }
338 void
340 {
342  NS_ASSERT (!m_rxing);
343  Time now = Simulator::Now ();
344  switch (GetState ())
345  {
346  case WifiPhy::IDLE:
348  break;
349  case WifiPhy::CCA_BUSY:
350  {
351  Time ccaStart = Max (m_endRx, m_endTx);
352  ccaStart = Max (ccaStart, m_startCcaBusy);
353  ccaStart = Max (ccaStart, m_endSwitching);
354  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
355  } break;
356  case WifiPhy::SWITCHING:
357  case WifiPhy::RX:
358  case WifiPhy::TX:
359  case WifiPhy::SLEEP:
360  NS_FATAL_ERROR ("Invalid WifiPhy state.");
361  break;
362  }
364  m_rxing = true;
365  m_startRx = now;
366  m_endRx = now + rxDuration;
367  NotifyRxStart (rxDuration);
368  NS_ASSERT (IsStateRx ());
369 }
370 
371 void
373 {
374  Time now = Simulator::Now ();
375  switch (GetState ())
376  {
377  case WifiPhy::RX:
378  /* The packet which is being received as well
379  * as its endRx event are cancelled by the caller.
380  */
381  m_rxing = false;
383  m_endRx = now;
384  break;
385  case WifiPhy::CCA_BUSY:
386  {
387  Time ccaStart = Max (m_endRx, m_endTx);
388  ccaStart = Max (ccaStart, m_startCcaBusy);
389  ccaStart = Max (ccaStart, m_endSwitching);
390  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
391  } break;
392  case WifiPhy::IDLE:
394  break;
395  case WifiPhy::TX:
396  case WifiPhy::SWITCHING:
397  case WifiPhy::SLEEP:
398  default:
399  NS_FATAL_ERROR ("Invalid WifiPhy state.");
400  break;
401  }
402 
403  if (now < m_endCcaBusy)
404  {
405  m_endCcaBusy = now;
406  }
407 
408  m_stateLogger (now, switchingDuration, WifiPhy::SWITCHING);
410  m_startSwitching = now;
411  m_endSwitching = now + switchingDuration;
412  NotifySwitchingStart (switchingDuration);
414 }
415 
416 void
418 {
419  m_rxOkTrace (packet, snr, mode, preamble);
420  NotifyRxEndOk ();
421  DoSwitchFromRx ();
422  if (!m_rxOkCallback.IsNull ())
423  {
424  m_rxOkCallback (packet, snr, mode, preamble);
425  }
426 
427 }
428 void
430 {
431  m_rxErrorTrace (packet, snr);
432  NotifyRxEndError ();
433  DoSwitchFromRx ();
434  if (!m_rxErrorCallback.IsNull ())
435  {
436  m_rxErrorCallback (packet, snr);
437  }
438 }
439 
440 void
442 {
443  NS_ASSERT (IsStateRx ());
444  NS_ASSERT (m_rxing);
445 
446  Time now = Simulator::Now ();
449  m_rxing = false;
450 
452 }
453 void
455 {
456  NotifyMaybeCcaBusyStart (duration);
457  Time now = Simulator::Now ();
458  switch (GetState ())
459  {
460  case WifiPhy::SWITCHING:
461  break;
462  case WifiPhy::SLEEP:
463  break;
464  case WifiPhy::IDLE:
466  break;
467  case WifiPhy::CCA_BUSY:
468  break;
469  case WifiPhy::RX:
470  break;
471  case WifiPhy::TX:
472  break;
473  }
474  if (GetState () != WifiPhy::CCA_BUSY)
475  {
476  m_startCcaBusy = now;
477  }
478  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
479 }
480 void
482 {
483  Time now = Simulator::Now ();
484  switch (GetState ())
485  {
486  case WifiPhy::IDLE:
488  break;
489  case WifiPhy::CCA_BUSY:
490  {
491  Time ccaStart = Max (m_endRx, m_endTx);
492  ccaStart = Max (ccaStart, m_startCcaBusy);
493  ccaStart = Max (ccaStart, m_endSwitching);
494  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
495  } break;
496  case WifiPhy::RX:
497  case WifiPhy::SWITCHING:
498  case WifiPhy::TX:
499  case WifiPhy::SLEEP:
500  NS_FATAL_ERROR ("Invalid WifiPhy state.");
501  break;
502  }
504  m_sleeping = true;
505  m_startSleep = now;
506  NotifySleep ();
507  NS_ASSERT (IsStateSleep ());
508 }
509 void
511 {
512  NS_ASSERT (IsStateSleep ());
513  Time now = Simulator::Now ();
516  m_sleeping = false;
517  NotifyWakeup ();
518  // update m_endCcaBusy after the sleep period
519  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
520  if (m_endCcaBusy > now)
522 }
523 
524 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
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 "...
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
TracedCallback< Time, Time, enum WifiPhy::State > m_stateLogger
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:141
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1072
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:61
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:157
#define NS_FATAL_ERROR(msg)
Fatal error handling.
Definition: fatal-error.h:100
std::vector< WifiPhyListener * >::iterator ListenersI
void SetReceiveOkCallback(WifiPhy::RxOkCallback callback)
Set a callback for a successful reception.
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:93
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:29
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.
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:44
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:137
bool IsStateRx(void)
Check whether the current state is RX.
int64x64_t Max(const int64x64_t &a, const int64x64_t &b)
Maximum.
Definition: int64x64.h:203
void SetReceiveErrorCallback(WifiPhy::RxErrorCallback callback)
Set a callback for a failed reception.
The PHY layer is receiving a packet.
Definition: wifi-phy.h:149
bool IsStateSleep(void)
Check whether the current state is SLEEP.
The PHY layer is sending a packet.
Definition: wifi-phy.h:145
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:153
void SwitchToSleep(void)
Switch to sleep mode.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SwitchFromRxEndError(Ptr< const Packet > packet, double snr)
Switch from RX after the reception failed.
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:223
void NotifyRxEndOk(void)
Notify all WifiPhyListener that the reception was successful.
void SwitchFromRxEndOk(Ptr< Packet > packet, double snr, WifiMode mode, enum WifiPreamble preamble)
Switch from RX after the reception was successful.
static TypeId GetTypeId(void)
enum WifiPhy::State GetState(void)
Return the current state of WifiPhy.
bool IsStateBusy(void)
Check whether the current state is not IDLE.
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:859
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:132
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:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
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