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  .SetGroupName ("Wifi")
38  .AddConstructor<WifiPhyStateHelper> ()
39  .AddTraceSource ("State",
40  "The state of the PHY layer",
42  "ns3::WifiPhyStateHelper::StateTracedCallback")
43  .AddTraceSource ("RxOk",
44  "A packet has been received successfully.",
46  "ns3::WifiPhyStateHelper::RxOkTracedCallback")
47  .AddTraceSource ("RxError",
48  "A packet has been received unsuccessfully.",
50  "ns3::WifiPhyStateHelper::RxErrorTracedCallback")
51  .AddTraceSource ("Tx", "Packet transmission is starting.",
53  "ns3::WifiPhyStateHelper::TxTracedCallback")
54  ;
55  return tid;
56 }
57 
59  : m_rxing (false),
60  m_sleeping (false),
61  m_endTx (Seconds (0)),
62  m_endRx (Seconds (0)),
63  m_endCcaBusy (Seconds (0)),
64  m_endSwitching (Seconds (0)),
65  m_startTx (Seconds (0)),
66  m_startRx (Seconds (0)),
67  m_startCcaBusy (Seconds (0)),
68  m_startSwitching (Seconds (0)),
69  m_startSleep (Seconds (0)),
70  m_previousStateChangeTime (Seconds (0))
71 {
72  NS_LOG_FUNCTION (this);
73 }
74 
75 void
77 {
78  m_rxOkCallback = callback;
79 }
80 void
82 {
83  m_rxErrorCallback = callback;
84 }
85 void
87 {
88  m_listeners.push_back (listener);
89 }
90 void
92 {
93  ListenersI i = find (m_listeners.begin(), m_listeners.end(), listener);
94  if (i != m_listeners.end())
95  {
96  m_listeners.erase(i);
97  }
98 }
99 
100 bool
102 {
103  return (GetState () == WifiPhy::IDLE);
104 }
105 bool
107 {
108  return (GetState () != WifiPhy::IDLE);
109 }
110 bool
112 {
113  return (GetState () == WifiPhy::CCA_BUSY);
114 }
115 bool
117 {
118  return (GetState () == WifiPhy::RX);
119 }
120 bool
122 {
123  return (GetState () == WifiPhy::TX);
124 }
125 bool
127 {
128  return (GetState () == WifiPhy::SWITCHING);
129 }
130 bool
132 {
133  return (GetState () == WifiPhy::SLEEP);
134 }
135 
136 
137 
138 Time
140 {
142 }
143 
144 Time
146 {
147  Time retval;
148 
149  switch (GetState ())
150  {
151  case WifiPhy::RX:
152  retval = m_endRx - Simulator::Now ();
153  break;
154  case WifiPhy::TX:
155  retval = m_endTx - Simulator::Now ();
156  break;
157  case WifiPhy::CCA_BUSY:
158  retval = m_endCcaBusy - Simulator::Now ();
159  break;
160  case WifiPhy::SWITCHING:
161  retval = m_endSwitching - Simulator::Now ();
162  break;
163  case WifiPhy::IDLE:
164  retval = Seconds (0);
165  break;
166  case WifiPhy::SLEEP:
167  NS_FATAL_ERROR ("Cannot determine when the device will wake up.");
168  retval = Seconds (0);
169  break;
170  default:
171  NS_FATAL_ERROR ("Invalid WifiPhy state.");
172  retval = Seconds (0);
173  break;
174  }
175  retval = Max (retval, Seconds (0));
176  return retval;
177 }
178 
179 Time
181 {
182  return m_startRx;
183 }
184 
185 enum WifiPhy::State
187 {
188  if (m_sleeping)
189  {
190  return WifiPhy::SLEEP;
191  }
192  else if (m_endTx > Simulator::Now ())
193  {
194  return WifiPhy::TX;
195  }
196  else if (m_rxing)
197  {
198  return WifiPhy::RX;
199  }
200  else if (m_endSwitching > Simulator::Now ())
201  {
202  return WifiPhy::SWITCHING;
203  }
204  else if (m_endCcaBusy > Simulator::Now ())
205  {
206  return WifiPhy::CCA_BUSY;
207  }
208  else
209  {
210  return WifiPhy::IDLE;
211  }
212 }
213 
214 
215 void
216 WifiPhyStateHelper::NotifyTxStart (Time duration, double txPowerDbm)
217 {
218  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
219  {
220  (*i)->NotifyTxStart (duration, txPowerDbm);
221  }
222 }
223 void
225 {
226  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
227  {
228  (*i)->NotifyRxStart (duration);
229  }
230 }
231 void
233 {
234  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
235  {
236  (*i)->NotifyRxEndOk ();
237  }
238 }
239 void
241 {
242  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
243  {
244  (*i)->NotifyRxEndError ();
245  }
246 }
247 void
249 {
250  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
251  {
252  (*i)->NotifyMaybeCcaBusyStart (duration);
253  }
254 }
255 void
257 {
258  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
259  {
260  (*i)->NotifySwitchingStart (duration);
261  }
262 }
263 void
265 {
266  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
267  {
268  (*i)->NotifySleep ();
269  }
270 }
271 void
273 {
274  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
275  {
276  (*i)->NotifyWakeup ();
277  }
278 }
279 
280 
281 void
283 {
284  Time now = Simulator::Now ();
285  Time idleStart = Max (m_endCcaBusy, m_endRx);
286  idleStart = Max (idleStart, m_endTx);
287  idleStart = Max (idleStart, m_endSwitching);
288  NS_ASSERT (idleStart <= now);
289  if (m_endCcaBusy > m_endRx
291  && m_endCcaBusy > m_endTx)
292  {
293  Time ccaBusyStart = Max (m_endTx, m_endRx);
294  ccaBusyStart = Max (ccaBusyStart, m_startCcaBusy);
295  ccaBusyStart = Max (ccaBusyStart, m_endSwitching);
296  m_stateLogger (ccaBusyStart, idleStart - ccaBusyStart, WifiPhy::CCA_BUSY);
297  }
298  m_stateLogger (idleStart, now - idleStart, WifiPhy::IDLE);
299 }
300 
301 void
302 WifiPhyStateHelper::SwitchToTx (Time txDuration, Ptr<const Packet> packet, double txPowerDbm,
303  WifiTxVector txVector, WifiPreamble preamble)
304 {
305  m_txTrace (packet, txVector.GetMode(), preamble, txVector.GetTxPowerLevel());
306  Time now = Simulator::Now ();
307  switch (GetState ())
308  {
309  case WifiPhy::RX:
310  /* The packet which is being received as well
311  * as its endRx event are cancelled by the caller.
312  */
313  m_rxing = false;
315  m_endRx = now;
316  break;
317  case WifiPhy::CCA_BUSY:
318  {
319  Time ccaStart = Max (m_endRx, m_endTx);
320  ccaStart = Max (ccaStart, m_startCcaBusy);
321  ccaStart = Max (ccaStart, m_endSwitching);
322  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
323  } break;
324  case WifiPhy::IDLE:
326  break;
327  case WifiPhy::SWITCHING:
328  case WifiPhy::SLEEP:
329  default:
330  NS_FATAL_ERROR ("Invalid WifiPhy state.");
331  break;
332  }
333  m_stateLogger (now, txDuration, WifiPhy::TX);
335  m_endTx = now + txDuration;
336  m_startTx = now;
337  NotifyTxStart (txDuration, txPowerDbm);
338 }
339 void
341 {
343  NS_ASSERT (!m_rxing);
344  Time now = Simulator::Now ();
345  switch (GetState ())
346  {
347  case WifiPhy::IDLE:
349  break;
350  case WifiPhy::CCA_BUSY:
351  {
352  Time ccaStart = Max (m_endRx, m_endTx);
353  ccaStart = Max (ccaStart, m_startCcaBusy);
354  ccaStart = Max (ccaStart, m_endSwitching);
355  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
356  } break;
357  case WifiPhy::SWITCHING:
358  case WifiPhy::RX:
359  case WifiPhy::TX:
360  case WifiPhy::SLEEP:
361  NS_FATAL_ERROR ("Invalid WifiPhy state.");
362  break;
363  }
365  m_rxing = true;
366  m_startRx = now;
367  m_endRx = now + rxDuration;
368  NotifyRxStart (rxDuration);
369  NS_ASSERT (IsStateRx ());
370 }
371 
372 void
374 {
375  Time now = Simulator::Now ();
376  switch (GetState ())
377  {
378  case WifiPhy::RX:
379  /* The packet which is being received as well
380  * as its endRx event are cancelled by the caller.
381  */
382  m_rxing = false;
384  m_endRx = now;
385  break;
386  case WifiPhy::CCA_BUSY:
387  {
388  Time ccaStart = Max (m_endRx, m_endTx);
389  ccaStart = Max (ccaStart, m_startCcaBusy);
390  ccaStart = Max (ccaStart, m_endSwitching);
391  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
392  } break;
393  case WifiPhy::IDLE:
395  break;
396  case WifiPhy::TX:
397  case WifiPhy::SWITCHING:
398  case WifiPhy::SLEEP:
399  default:
400  NS_FATAL_ERROR ("Invalid WifiPhy state.");
401  break;
402  }
403 
404  if (now < m_endCcaBusy)
405  {
406  m_endCcaBusy = now;
407  }
408 
409  m_stateLogger (now, switchingDuration, WifiPhy::SWITCHING);
411  m_startSwitching = now;
412  m_endSwitching = now + switchingDuration;
413  NotifySwitchingStart (switchingDuration);
415 }
416 
417 void
418 WifiPhyStateHelper::SwitchFromRxEndOk (Ptr<Packet> packet, double snr, WifiTxVector txVector, enum WifiPreamble preamble)
419 {
420  m_rxOkTrace (packet, snr, txVector.GetMode(), preamble);
421  NotifyRxEndOk ();
422  DoSwitchFromRx ();
423  if (!m_rxOkCallback.IsNull ())
424  {
425  m_rxOkCallback (packet, snr, txVector, preamble);
426  }
427 
428 }
429 void
431 {
432  m_rxErrorTrace (packet, snr);
433  NotifyRxEndError ();
434  DoSwitchFromRx ();
435  if (!m_rxErrorCallback.IsNull ())
436  {
437  m_rxErrorCallback (packet, snr);
438  }
439 }
440 
441 void
443 {
444  NS_ASSERT (IsStateRx ());
445  NS_ASSERT (m_rxing);
446 
447  Time now = Simulator::Now ();
450  m_rxing = false;
451 
453 }
454 void
456 {
457  NotifyMaybeCcaBusyStart (duration);
458  Time now = Simulator::Now ();
459  switch (GetState ())
460  {
461  case WifiPhy::SWITCHING:
462  break;
463  case WifiPhy::SLEEP:
464  break;
465  case WifiPhy::IDLE:
467  break;
468  case WifiPhy::CCA_BUSY:
469  break;
470  case WifiPhy::RX:
471  break;
472  case WifiPhy::TX:
473  break;
474  }
475  if (GetState () != WifiPhy::CCA_BUSY)
476  {
477  m_startCcaBusy = now;
478  }
479  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
480 }
481 void
483 {
484  Time now = Simulator::Now ();
485  switch (GetState ())
486  {
487  case WifiPhy::IDLE:
489  break;
490  case WifiPhy::CCA_BUSY:
491  {
492  Time ccaStart = Max (m_endRx, m_endTx);
493  ccaStart = Max (ccaStart, m_startCcaBusy);
494  ccaStart = Max (ccaStart, m_endSwitching);
495  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
496  } break;
497  case WifiPhy::RX:
498  case WifiPhy::SWITCHING:
499  case WifiPhy::TX:
500  case WifiPhy::SLEEP:
501  NS_FATAL_ERROR ("Invalid WifiPhy state.");
502  break;
503  }
505  m_sleeping = true;
506  m_startSleep = now;
507  NotifySleep ();
508  NS_ASSERT (IsStateSleep ());
509 }
510 void
512 {
513  NS_ASSERT (IsStateSleep ());
514  Time now = Simulator::Now ();
517  m_sleeping = false;
518  NotifyWakeup ();
519  // update m_endCcaBusy after the sleep period
520  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
521  if (m_endCcaBusy > now)
523 }
524 
525 } // 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
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:1078
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: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.
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:209
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.
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.
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:866
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
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