A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
25 NS_LOG_COMPONENT_DEFINE ("WifiPhyStateHelper");
26 
27 namespace ns3 {
28 
29 NS_OBJECT_ENSURE_REGISTERED (WifiPhyStateHelper);
30 
31 TypeId
33 {
34  static TypeId tid = TypeId ("ns3::WifiPhyStateHelper")
35  .SetParent<Object> ()
36  .AddConstructor<WifiPhyStateHelper> ()
37  .AddTraceSource ("State",
38  "The state of the PHY layer",
40  .AddTraceSource ("RxOk",
41  "A packet has been received successfully.",
43  .AddTraceSource ("RxError",
44  "A packet has been received unsuccessfully.",
46  .AddTraceSource ("Tx", "Packet transmission is starting.",
48  ;
49  return tid;
50 }
51 
53  : m_rxing (false),
54  m_sleeping (false),
55  m_endTx (Seconds (0)),
56  m_endRx (Seconds (0)),
57  m_endCcaBusy (Seconds (0)),
58  m_endSwitching (Seconds (0)),
59  m_startTx (Seconds (0)),
60  m_startRx (Seconds (0)),
61  m_startCcaBusy (Seconds (0)),
62  m_startSwitching (Seconds (0)),
63  m_startSleep (Seconds (0)),
64  m_previousStateChangeTime (Seconds (0))
65 {
66  NS_LOG_FUNCTION (this);
67 }
68 
69 void
71 {
72  m_rxOkCallback = callback;
73 }
74 void
76 {
77  m_rxErrorCallback = callback;
78 }
79 void
81 {
82  m_listeners.push_back (listener);
83 }
84 
85 bool
87 {
88  return (GetState () == WifiPhy::IDLE);
89 }
90 bool
92 {
93  return (GetState () != WifiPhy::IDLE);
94 }
95 bool
97 {
98  return (GetState () == WifiPhy::CCA_BUSY);
99 }
100 bool
102 {
103  return (GetState () == WifiPhy::RX);
104 }
105 bool
107 {
108  return (GetState () == WifiPhy::TX);
109 }
110 bool
112 {
113  return (GetState () == WifiPhy::SWITCHING);
114 }
115 bool
117 {
118  return (GetState () == WifiPhy::SLEEP);
119 }
120 
121 
122 
123 Time
125 {
127 }
128 
129 Time
131 {
132  Time retval;
133 
134  switch (GetState ())
135  {
136  case WifiPhy::RX:
137  retval = m_endRx - Simulator::Now ();
138  break;
139  case WifiPhy::TX:
140  retval = m_endTx - Simulator::Now ();
141  break;
142  case WifiPhy::CCA_BUSY:
143  retval = m_endCcaBusy - Simulator::Now ();
144  break;
145  case WifiPhy::SWITCHING:
146  retval = m_endSwitching - Simulator::Now ();
147  break;
148  case WifiPhy::IDLE:
149  retval = Seconds (0);
150  break;
151  case WifiPhy::SLEEP:
152  NS_FATAL_ERROR ("Cannot determine when the device will wake up.");
153  retval = Seconds (0);
154  break;
155  default:
156  NS_FATAL_ERROR ("Invalid WifiPhy state.");
157  retval = Seconds (0);
158  break;
159  }
160  retval = Max (retval, Seconds (0));
161  return retval;
162 }
163 
164 Time
166 {
167  return m_startRx;
168 }
169 
170 enum WifiPhy::State
172 {
173  if (m_sleeping)
174  {
175  return WifiPhy::SLEEP;
176  }
177  else if (m_endTx > Simulator::Now ())
178  {
179  return WifiPhy::TX;
180  }
181  else if (m_rxing)
182  {
183  return WifiPhy::RX;
184  }
185  else if (m_endSwitching > Simulator::Now ())
186  {
187  return WifiPhy::SWITCHING;
188  }
189  else if (m_endCcaBusy > Simulator::Now ())
190  {
191  return WifiPhy::CCA_BUSY;
192  }
193  else
194  {
195  return WifiPhy::IDLE;
196  }
197 }
198 
199 
200 void
201 WifiPhyStateHelper::NotifyTxStart (Time duration, double txPowerDbm)
202 {
203  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
204  {
205  (*i)->NotifyTxStart (duration, txPowerDbm);
206  }
207 }
208 void
210 {
211  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
212  {
213  (*i)->NotifyRxStart (duration);
214  }
215 }
216 void
218 {
219  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
220  {
221  (*i)->NotifyRxEndOk ();
222  }
223 }
224 void
226 {
227  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
228  {
229  (*i)->NotifyRxEndError ();
230  }
231 }
232 void
234 {
235  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
236  {
237  (*i)->NotifyMaybeCcaBusyStart (duration);
238  }
239 }
240 void
242 {
243  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
244  {
245  (*i)->NotifySwitchingStart (duration);
246  }
247 }
248 void
250 {
251  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
252  {
253  (*i)->NotifySleep ();
254  }
255 }
256 void
258 {
259  for (Listeners::const_iterator i = m_listeners.begin (); i != m_listeners.end (); i++)
260  {
261  (*i)->NotifyWakeup ();
262  }
263 }
264 
265 
266 void
268 {
269  Time now = Simulator::Now ();
270  Time idleStart = Max (m_endCcaBusy, m_endRx);
271  idleStart = Max (idleStart, m_endTx);
272  idleStart = Max (idleStart, m_endSwitching);
273  NS_ASSERT (idleStart <= now);
274  if (m_endCcaBusy > m_endRx
276  && m_endCcaBusy > m_endTx)
277  {
278  Time ccaBusyStart = Max (m_endTx, m_endRx);
279  ccaBusyStart = Max (ccaBusyStart, m_startCcaBusy);
280  ccaBusyStart = Max (ccaBusyStart, m_endSwitching);
281  m_stateLogger (ccaBusyStart, idleStart - ccaBusyStart, WifiPhy::CCA_BUSY);
282  }
283  m_stateLogger (idleStart, now - idleStart, WifiPhy::IDLE);
284 }
285 
286 void
287 WifiPhyStateHelper::SwitchToTx (Time txDuration, Ptr<const Packet> packet, double txPowerDbm,
288  WifiTxVector txVector, WifiPreamble preamble)
289 {
290  m_txTrace (packet, txVector.GetMode(), preamble, txVector.GetTxPowerLevel());
291  Time now = Simulator::Now ();
292  switch (GetState ())
293  {
294  case WifiPhy::RX:
295  /* The packet which is being received as well
296  * as its endRx event are cancelled by the caller.
297  */
298  m_rxing = false;
300  m_endRx = now;
301  break;
302  case WifiPhy::CCA_BUSY:
303  {
304  Time ccaStart = Max (m_endRx, m_endTx);
305  ccaStart = Max (ccaStart, m_startCcaBusy);
306  ccaStart = Max (ccaStart, m_endSwitching);
307  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
308  } break;
309  case WifiPhy::IDLE:
311  break;
312  case WifiPhy::SWITCHING:
313  case WifiPhy::SLEEP:
314  default:
315  NS_FATAL_ERROR ("Invalid WifiPhy state.");
316  break;
317  }
318  m_stateLogger (now, txDuration, WifiPhy::TX);
320  m_endTx = now + txDuration;
321  m_startTx = now;
322  NotifyTxStart (txDuration, txPowerDbm);
323 }
324 void
326 {
328  NS_ASSERT (!m_rxing);
329  Time now = Simulator::Now ();
330  switch (GetState ())
331  {
332  case WifiPhy::IDLE:
334  break;
335  case WifiPhy::CCA_BUSY:
336  {
337  Time ccaStart = Max (m_endRx, m_endTx);
338  ccaStart = Max (ccaStart, m_startCcaBusy);
339  ccaStart = Max (ccaStart, m_endSwitching);
340  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
341  } break;
342  case WifiPhy::SWITCHING:
343  case WifiPhy::RX:
344  case WifiPhy::TX:
345  case WifiPhy::SLEEP:
346  NS_FATAL_ERROR ("Invalid WifiPhy state.");
347  break;
348  }
350  m_rxing = true;
351  m_startRx = now;
352  m_endRx = now + rxDuration;
353  NotifyRxStart (rxDuration);
354  NS_ASSERT (IsStateRx ());
355 }
356 
357 void
359 {
360  Time now = Simulator::Now ();
361  switch (GetState ())
362  {
363  case WifiPhy::RX:
364  /* The packet which is being received as well
365  * as its endRx event are cancelled by the caller.
366  */
367  m_rxing = false;
369  m_endRx = now;
370  break;
371  case WifiPhy::CCA_BUSY:
372  {
373  Time ccaStart = Max (m_endRx, m_endTx);
374  ccaStart = Max (ccaStart, m_startCcaBusy);
375  ccaStart = Max (ccaStart, m_endSwitching);
376  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
377  } break;
378  case WifiPhy::IDLE:
380  break;
381  case WifiPhy::TX:
382  case WifiPhy::SWITCHING:
383  case WifiPhy::SLEEP:
384  default:
385  NS_FATAL_ERROR ("Invalid WifiPhy state.");
386  break;
387  }
388 
389  if (now < m_endCcaBusy)
390  {
391  m_endCcaBusy = now;
392  }
393 
394  m_stateLogger (now, switchingDuration, WifiPhy::SWITCHING);
396  m_startSwitching = now;
397  m_endSwitching = now + switchingDuration;
398  NotifySwitchingStart (switchingDuration);
400 }
401 
402 void
404 {
405  m_rxOkTrace (packet, snr, mode, preamble);
406  NotifyRxEndOk ();
407  DoSwitchFromRx ();
408  if (!m_rxOkCallback.IsNull ())
409  {
410  m_rxOkCallback (packet, snr, mode, preamble);
411  }
412 
413 }
414 void
416 {
417  m_rxErrorTrace (packet, snr);
418  NotifyRxEndError ();
419  DoSwitchFromRx ();
420  if (!m_rxErrorCallback.IsNull ())
421  {
422  m_rxErrorCallback (packet, snr);
423  }
424 }
425 
426 void
428 {
429  NS_ASSERT (IsStateRx ());
430  NS_ASSERT (m_rxing);
431 
432  Time now = Simulator::Now ();
435  m_rxing = false;
436 
438 }
439 void
441 {
442  NotifyMaybeCcaBusyStart (duration);
443  Time now = Simulator::Now ();
444  switch (GetState ())
445  {
446  case WifiPhy::SWITCHING:
447  break;
448  case WifiPhy::SLEEP:
449  break;
450  case WifiPhy::IDLE:
452  break;
453  case WifiPhy::CCA_BUSY:
454  break;
455  case WifiPhy::RX:
456  break;
457  case WifiPhy::TX:
458  break;
459  }
460  if (GetState () != WifiPhy::CCA_BUSY)
461  {
462  m_startCcaBusy = now;
463  }
464  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
465 }
466 void
468 {
469  Time now = Simulator::Now ();
470  switch (GetState ())
471  {
472  case WifiPhy::IDLE:
474  break;
475  case WifiPhy::CCA_BUSY:
476  {
477  Time ccaStart = Max (m_endRx, m_endTx);
478  ccaStart = Max (ccaStart, m_startCcaBusy);
479  ccaStart = Max (ccaStart, m_endSwitching);
480  m_stateLogger (ccaStart, now - ccaStart, WifiPhy::CCA_BUSY);
481  } break;
482  case WifiPhy::RX:
483  case WifiPhy::SWITCHING:
484  case WifiPhy::TX:
485  case WifiPhy::SLEEP:
486  NS_FATAL_ERROR ("Invalid WifiPhy state.");
487  break;
488  }
490  m_sleeping = true;
491  m_startSleep = now;
492  NotifySleep ();
493  NS_ASSERT (IsStateSleep ());
494 }
495 void
497 {
498  NS_ASSERT (IsStateSleep ());
499  Time now = Simulator::Now ();
502  m_sleeping = false;
503  NotifyWakeup ();
504  // update m_endCcaBusy after the sleep period
505  m_endCcaBusy = std::max (m_endCcaBusy, now + duration);
506  if (m_endCcaBusy > now)
508 }
509 
510 } // 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 "...
The PHY layer is sleeping.
Definition: wifi-phy.h:157
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 the class in the ns-3 factory.
Definition: object-base.h:38
TracedCallback< Time, Time, enum WifiPhy::State > m_stateLogger
void NotifyWakeup(void)
Notify all WifiPhyListener that we woke up.
State
The state of the PHY layer.
Definition: wifi-phy.h:132
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1066
void SwitchToChannelSwitching(Time switchingDuration)
Switch state to channel switching for the given duration.
#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:170
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
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:91
void SwitchFromSleep(Time duration)
Switch from sleep mode.
uint8_t GetTxPowerLevel(void) const
The PHY layer is switching to other channel.
Definition: wifi-phy.h:153
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...
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:204
void SetReceiveErrorCallback(WifiPhy::RxErrorCallback callback)
Set a callback for a failed reception.
bool IsStateSleep(void)
Check whether the current state is SLEEP.
void NotifyMaybeCcaBusyStart(Time duration)
Notify all WifiPhyListener that the CCA has started for the given duration.
WifiPhy::RxErrorCallback m_rxErrorCallback
The PHY layer is IDLE.
Definition: wifi-phy.h:137
void SwitchToSleep(void)
Switch to sleep mode.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
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 time".
Definition: simulator.cc:180
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:845
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:64
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
The PHY layer is receiving a packet.
Definition: wifi-phy.h:149
void NotifyRxStart(Time duration)
Notify all WifiPhyListener that the reception has started for the given duration. ...
The PHY layer has sense the medium busy through the CCA mechanism.
Definition: wifi-phy.h:141
a unique identifier for an interface.
Definition: type-id.h:49
The PHY layer is sending a packet.
Definition: wifi-phy.h:145
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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