A Discrete-Event Network Simulator
API
wifi-phy-state-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005,2006 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #include "wifi-phy-state-helper.h"
21 
22 #include "wifi-phy-listener.h"
23 #include "wifi-phy.h"
24 #include "wifi-psdu.h"
25 #include "wifi-tx-vector.h"
26 
27 #include "ns3/log.h"
28 #include "ns3/packet.h"
29 #include "ns3/simulator.h"
30 
31 #include <algorithm>
32 #include <iterator>
33 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE("WifiPhyStateHelper");
38 
39 NS_OBJECT_ENSURE_REGISTERED(WifiPhyStateHelper);
40 
41 TypeId
43 {
44  static TypeId tid =
45  TypeId("ns3::WifiPhyStateHelper")
46  .SetParent<Object>()
47  .SetGroupName("Wifi")
48  .AddConstructor<WifiPhyStateHelper>()
49  .AddTraceSource("State",
50  "The state of the PHY layer",
52  "ns3::WifiPhyStateHelper::StateTracedCallback")
53  .AddTraceSource("RxOk",
54  "A packet has been received successfully.",
56  "ns3::WifiPhyStateHelper::RxOkTracedCallback")
57  .AddTraceSource("RxError",
58  "A packet has been received unsuccessfuly.",
60  "ns3::WifiPhyStateHelper::RxEndErrorTracedCallback")
61  .AddTraceSource("Tx",
62  "Packet transmission is starting.",
64  "ns3::WifiPhyStateHelper::TxTracedCallback");
65  return tid;
66 }
67 
69  : m_sleeping(false),
70  m_isOff(false),
71  m_endTx(Seconds(0)),
72  m_endRx(Seconds(0)),
73  m_endCcaBusy(Seconds(0)),
74  m_endSwitching(Seconds(0)),
75  m_startTx(Seconds(0)),
76  m_startRx(Seconds(0)),
77  m_startCcaBusy(Seconds(0)),
78  m_startSwitching(Seconds(0)),
79  m_startSleep(Seconds(0)),
80  m_previousStateChangeTime(Seconds(0))
81 {
82  NS_LOG_FUNCTION(this);
83 }
84 
85 void
87 {
88  m_rxOkCallback = callback;
89 }
90 
91 void
93 {
94  m_rxErrorCallback = callback;
95 }
96 
97 void
99 {
100  m_listeners.push_back(listener);
101 }
102 
103 void
105 {
106  auto it = find(m_listeners.begin(), m_listeners.end(), listener);
107  if (it != m_listeners.end())
108  {
109  m_listeners.erase(it);
110  }
111 }
112 
113 bool
115 {
116  return (GetState() == WifiPhyState::IDLE);
117 }
118 
119 bool
121 {
122  return (GetState() == WifiPhyState::CCA_BUSY);
123 }
124 
125 bool
127 {
128  return (GetState() == WifiPhyState::RX);
129 }
130 
131 bool
133 {
134  return (GetState() == WifiPhyState::TX);
135 }
136 
137 bool
139 {
140  return (GetState() == WifiPhyState::SWITCHING);
141 }
142 
143 bool
145 {
146  return (GetState() == WifiPhyState::SLEEP);
147 }
148 
149 bool
151 {
152  return (GetState() == WifiPhyState::OFF);
153 }
154 
155 Time
157 {
158  Time retval;
159 
160  switch (GetState())
161  {
162  case WifiPhyState::RX:
163  retval = m_endRx - Simulator::Now();
164  break;
165  case WifiPhyState::TX:
166  retval = m_endTx - Simulator::Now();
167  break;
169  retval = m_endCcaBusy - Simulator::Now();
170  break;
172  retval = m_endSwitching - Simulator::Now();
173  break;
174  case WifiPhyState::IDLE:
175  case WifiPhyState::SLEEP:
176  case WifiPhyState::OFF:
177  retval = Seconds(0);
178  break;
179  default:
180  NS_FATAL_ERROR("Invalid WifiPhy state.");
181  retval = Seconds(0);
182  break;
183  }
184  retval = Max(retval, Seconds(0));
185  return retval;
186 }
187 
188 Time
190 {
191  return m_startRx;
192 }
193 
194 Time
196 {
197  return m_endRx;
198 }
199 
202 {
203  if (m_isOff)
204  {
205  return WifiPhyState::OFF;
206  }
207  if (m_sleeping)
208  {
209  return WifiPhyState::SLEEP;
210  }
211  else if (m_endTx > Simulator::Now())
212  {
213  return WifiPhyState::TX;
214  }
215  else if (m_endRx > Simulator::Now())
216  {
217  return WifiPhyState::RX;
218  }
219  else if (m_endSwitching > Simulator::Now())
220  {
222  }
223  else if (m_endCcaBusy > Simulator::Now())
224  {
225  return WifiPhyState::CCA_BUSY;
226  }
227  else
228  {
229  return WifiPhyState::IDLE;
230  }
231 }
232 
233 void
234 WifiPhyStateHelper::NotifyTxStart(Time duration, double txPowerDbm)
235 {
236  NS_LOG_FUNCTION(this);
237  for (const auto& listener : m_listeners)
238  {
239  listener->NotifyTxStart(duration, txPowerDbm);
240  }
241 }
242 
243 void
245 {
246  NS_LOG_FUNCTION(this);
247  for (const auto& listener : m_listeners)
248  {
249  listener->NotifyRxStart(duration);
250  }
251 }
252 
253 void
255 {
256  NS_LOG_FUNCTION(this);
257  for (const auto& listener : m_listeners)
258  {
259  listener->NotifyRxEndOk();
260  }
261 }
262 
263 void
265 {
266  NS_LOG_FUNCTION(this);
267  for (const auto& listener : m_listeners)
268  {
269  listener->NotifyRxEndError();
270  }
271 }
272 
273 void
275  WifiChannelListType channelType,
276  const std::vector<Time>& per20MhzDurations)
277 {
278  NS_LOG_FUNCTION(this);
279  for (const auto& listener : m_listeners)
280  {
281  listener->NotifyCcaBusyStart(duration, channelType, per20MhzDurations);
282  }
283 }
284 
285 void
287 {
288  NS_LOG_FUNCTION(this);
289  for (const auto& listener : m_listeners)
290  {
291  listener->NotifySwitchingStart(duration);
292  }
293 }
294 
295 void
297 {
298  NS_LOG_FUNCTION(this);
299  for (const auto& listener : m_listeners)
300  {
301  listener->NotifySleep();
302  }
303 }
304 
305 void
307 {
308  NS_LOG_FUNCTION(this);
309  for (const auto& listener : m_listeners)
310  {
311  listener->NotifyOff();
312  }
313 }
314 
315 void
317 {
318  NS_LOG_FUNCTION(this);
319  for (const auto& listener : m_listeners)
320  {
321  listener->NotifyWakeup();
322  }
323 }
324 
325 void
327 {
328  NS_LOG_FUNCTION(this);
329  for (const auto& listener : m_listeners)
330  {
331  listener->NotifyOn();
332  }
333 }
334 
335 void
337 {
338  NS_LOG_FUNCTION(this);
339  Time now = Simulator::Now();
340  WifiPhyState state = GetState();
341  if (state == WifiPhyState::CCA_BUSY)
342  {
344  m_stateLogger(ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
345  }
346  else if (state == WifiPhyState::IDLE)
347  {
349  NS_ASSERT(idleStart <= now);
351  {
353  Time ccaBusyDuration = idleStart - ccaBusyStart;
354  if (ccaBusyDuration.IsStrictlyPositive())
355  {
356  m_stateLogger(ccaBusyStart, ccaBusyDuration, WifiPhyState::CCA_BUSY);
357  }
358  }
359  Time idleDuration = now - idleStart;
360  if (idleDuration.IsStrictlyPositive())
361  {
362  m_stateLogger(idleStart, idleDuration, WifiPhyState::IDLE);
363  }
364  }
365 }
366 
367 void
369  WifiConstPsduMap psdus,
370  double txPowerDbm,
371  const WifiTxVector& txVector)
372 {
373  NS_LOG_FUNCTION(this << txDuration << psdus << txPowerDbm << txVector);
374  if (!m_txTrace.IsEmpty())
375  {
376  for (const auto& psdu : psdus)
377  {
378  m_txTrace(psdu.second->GetPacket(),
379  txVector.GetMode(psdu.first),
380  txVector.GetPreambleType(),
381  txVector.GetTxPowerLevel());
382  }
383  }
384  Time now = Simulator::Now();
385  switch (GetState())
386  {
387  case WifiPhyState::RX:
388  /* The packet which is being received as well
389  * as its endRx event are cancelled by the caller.
390  */
392  m_endRx = now;
393  break;
395  [[fallthrough]];
396  case WifiPhyState::IDLE:
398  break;
399  default:
400  NS_FATAL_ERROR("Invalid WifiPhy state.");
401  break;
402  }
403  m_stateLogger(now, txDuration, WifiPhyState::TX);
405  m_endTx = now + txDuration;
406  m_startTx = now;
407  NotifyTxStart(txDuration, txPowerDbm);
408 }
409 
410 void
412 {
413  NS_LOG_FUNCTION(this << rxDuration);
415  Time now = Simulator::Now();
416  switch (GetState())
417  {
418  case WifiPhyState::IDLE:
419  [[fallthrough]];
422  break;
423  default:
424  NS_FATAL_ERROR("Invalid WifiPhy state " << GetState());
425  break;
426  }
428  m_startRx = now;
429  m_endRx = now + rxDuration;
430  NotifyRxStart(rxDuration);
431  NS_ASSERT(IsStateRx());
432 }
433 
434 void
436 {
437  NS_LOG_FUNCTION(this << switchingDuration);
438  Time now = Simulator::Now();
439  switch (GetState())
440  {
441  case WifiPhyState::RX:
442  /* The packet which is being received as well
443  * as its endRx event are cancelled by the caller.
444  */
446  m_endRx = now;
447  break;
449  [[fallthrough]];
450  case WifiPhyState::IDLE:
452  break;
453  default:
454  NS_FATAL_ERROR("Invalid WifiPhy state.");
455  break;
456  }
457 
459  m_stateLogger(now, switchingDuration, WifiPhyState::SWITCHING);
461  m_startSwitching = now;
462  m_endSwitching = now + switchingDuration;
463  NotifySwitchingStart(switchingDuration);
464  NS_ASSERT(switchingDuration.IsZero() || IsStateSwitching());
465 }
466 
467 void
469  RxSignalInfo rxSignalInfo,
470  const WifiTxVector& txVector)
471 {
472  NS_LOG_FUNCTION(this << *psdu << rxSignalInfo << txVector);
473  if (!m_rxOkCallback.IsNull())
474  {
475  m_rxOkCallback(psdu, rxSignalInfo, txVector, {});
476  }
477 }
478 
479 void
481  RxSignalInfo rxSignalInfo,
482  const WifiTxVector& txVector,
483  uint16_t staId,
484  const std::vector<bool>& statusPerMpdu)
485 {
486  NS_LOG_FUNCTION(this << *psdu << rxSignalInfo << txVector << staId << statusPerMpdu.size()
487  << std::all_of(statusPerMpdu.begin(), statusPerMpdu.end(), [](bool v) {
488  return v;
489  })); // returns true if all true
490  NS_ASSERT(!statusPerMpdu.empty());
491  if (!m_rxOkTrace.IsEmpty())
492  {
493  m_rxOkTrace(psdu->GetPacket(),
494  rxSignalInfo.snr,
495  txVector.GetMode(staId),
496  txVector.GetPreambleType());
497  }
498  if (!m_rxOkCallback.IsNull())
499  {
500  m_rxOkCallback(psdu, rxSignalInfo, txVector, statusPerMpdu);
501  }
502 }
503 
504 void
506 {
507  NS_LOG_FUNCTION(this << *psdu << snr);
508  if (!m_rxErrorTrace.IsEmpty())
509  {
510  m_rxErrorTrace(psdu->GetPacket(), snr);
511  }
512  if (!m_rxErrorCallback.IsNull())
513  {
514  m_rxErrorCallback(psdu);
515  }
516 }
517 
518 void
520 {
521  NS_LOG_FUNCTION(this);
523  NotifyRxEndOk();
524  DoSwitchFromRx();
525 }
526 
527 void
529 {
530  NS_LOG_FUNCTION(this);
533  DoSwitchFromRx();
534 }
535 
536 void
538 {
539  NS_LOG_FUNCTION(this);
540  Time now = Simulator::Now();
545 }
546 
547 void
549  WifiChannelListType channelType,
550  const std::vector<Time>& per20MhzDurations)
551 {
552  NS_LOG_FUNCTION(this << duration << channelType);
553  if (GetState() == WifiPhyState::RX)
554  {
555  return;
556  }
557  NotifyCcaBusyStart(duration, channelType, per20MhzDurations);
558  if (channelType != WIFI_CHANLIST_PRIMARY)
559  {
560  // WifiPhyStateHelper only updates CCA start and end durations for the primary channel
561  return;
562  }
563  Time now = Simulator::Now();
564  if (GetState() == WifiPhyState::IDLE)
565  {
567  }
569  {
570  m_startCcaBusy = now;
571  }
572  m_endCcaBusy = std::max(m_endCcaBusy, now + duration);
573 }
574 
575 void
577 {
578  NS_LOG_FUNCTION(this);
579  Time now = Simulator::Now();
580  switch (GetState())
581  {
582  case WifiPhyState::IDLE:
583  [[fallthrough]];
586  break;
587  default:
588  NS_FATAL_ERROR("Invalid WifiPhy state.");
589  break;
590  }
592  m_sleeping = true;
593  m_startSleep = now;
594  NotifySleep();
596 }
597 
598 void
600 {
601  NS_LOG_FUNCTION(this);
603  Time now = Simulator::Now();
606  m_sleeping = false;
607  NotifyWakeup();
608 }
609 
610 void
612 {
613  NS_LOG_FUNCTION(this << operatingWidth);
614  NS_ASSERT(IsStateCcaBusy()); // abort is called (with OBSS_PD_CCA_RESET reason) before RX is set
615  // by payload start
616  NotifyRxEndOk();
617  DoSwitchFromRx();
619  std::vector<Time> per20MhzDurations;
620  if (operatingWidth >= 40)
621  {
622  std::fill_n(std::back_inserter(per20MhzDurations), (operatingWidth / 20), Seconds(0));
623  }
624  NotifyCcaBusyStart(Seconds(0), WIFI_CHANLIST_PRIMARY, per20MhzDurations);
626 }
627 
628 void
630 {
631  NS_LOG_FUNCTION(this);
632  Time now = Simulator::Now();
633  switch (GetState())
634  {
635  case WifiPhyState::RX:
636  /* The packet which is being received as well
637  * as its endRx event are cancelled by the caller.
638  */
640  m_endRx = now;
641  break;
642  case WifiPhyState::TX:
643  /* The packet which is being transmitted as well
644  * as its endTx event are cancelled by the caller.
645  */
647  m_endTx = now;
648  break;
649  case WifiPhyState::IDLE:
650  [[fallthrough]];
653  break;
654  default:
655  NS_FATAL_ERROR("Invalid WifiPhy state.");
656  break;
657  }
659  m_isOff = true;
660  NotifyOff();
662 }
663 
664 void
666 {
667  NS_LOG_FUNCTION(this);
669  Time now = Simulator::Now();
671  m_isOff = false;
672  NotifyOn();
673 }
674 
675 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
bool IsNull() const
Check for null implementation.
Definition: callback.h:572
A base class which provides memory management and object aggregation.
Definition: object.h:89
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
bool IsStrictlyPositive() const
Exactly equivalent to t > 0.
Definition: nstime.h:350
bool IsZero() const
Exactly equivalent to t == 0.
Definition: nstime.h:314
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
receive notifications about PHY events.
This objects implements the PHY state machine of the Wifi device.
void NotifyRxEndOk()
Notify all WifiPhyListener that the reception was successful.
bool IsStateSwitching() const
Check whether the current state is SWITCHING.
void SwitchToRx(Time rxDuration)
Switch state to RX for the given duration.
void NotifyOn()
Notify all WifiPhyListener that we are going to switch on.
bool IsStateCcaBusy() const
Check whether the current state is CCA busy.
Time GetDelayUntilIdle() const
Return the time before the state is back to IDLE.
bool IsStateIdle() const
Check whether the current state is IDLE.
Time GetLastRxStartTime() const
Return the time the last RX start.
void DoSwitchFromRx()
Switch the state from RX.
void NotifyRxEndError()
Notify all WifiPhyListener that the reception was not successful.
void NotifySwitchingStart(Time duration)
Notify all WifiPhyListener that we are switching channel with the given channel switching delay.
void SwitchFromRxEndOk()
Switch from RX after the reception was successful.
Time m_previousStateChangeTime
previous state change time
void NotifyRxStart(Time duration)
Notify all WifiPhyListener that the reception has started for the given duration.
void SwitchToChannelSwitching(Time switchingDuration)
Switch state to channel switching for the given duration.
void SwitchToOff()
Switch to off mode.
void NotifyRxMpdu(Ptr< const WifiPsdu > psdu, RxSignalInfo rxSignalInfo, const WifiTxVector &txVector)
Notify the reception of an MPDU included in an A-MPDU.
TracedCallback< Ptr< const Packet >, WifiMode, WifiPreamble, uint8_t > m_txTrace
transmit trace callback
Time m_endSwitching
end switching
void SwitchToTx(Time txDuration, WifiConstPsduMap psdus, double txPowerDbm, const WifiTxVector &txVector)
Switch state to TX for the given duration.
Time m_startSwitching
start switching
TracedCallback< Time, Time, WifiPhyState > m_stateLogger
The trace source fired when state is changed.
void LogPreviousIdleAndCcaBusyStates()
Log the idle and CCA busy states.
void NotifySleep()
Notify all WifiPhyListener that we are going to sleep.
void RegisterListener(WifiPhyListener *listener)
Register WifiPhyListener to this WifiPhyStateHelper.
void NotifyCcaBusyStart(Time duration, WifiChannelListType channelType, const std::vector< Time > &per20MhzDurations)
Notify all WifiPhyListener that the CCA has started for the given duration.
static TypeId GetTypeId()
Get the type ID.
RxOkCallback m_rxOkCallback
receive OK callback
TracedCallback< Ptr< const Packet >, double, WifiMode, WifiPreamble > m_rxOkTrace
receive OK trace callback
void NotifyRxPsduFailed(Ptr< const WifiPsdu > psdu, double snr)
Handle the unsuccessful reception of a PSDU.
bool IsStateOff() const
Check whether the current state is OFF.
void SwitchFromRxAbort(uint16_t operatingWidth)
Abort current reception following a CCA reset request.
WifiPhyState GetState() const
Return the current state of WifiPhy.
RxErrorCallback m_rxErrorCallback
receive error callback
void SwitchToSleep()
Switch to sleep mode.
void SwitchMaybeToCcaBusy(Time duration, WifiChannelListType channelType, const std::vector< Time > &per20MhzDurations)
Switch to CCA busy.
TracedCallback< Ptr< const Packet >, double > m_rxErrorTrace
receive error trace callback
void NotifyOff()
Notify all WifiPhyListener that we are going to switch off.
bool IsStateTx() const
Check whether the current state is TX.
void SwitchFromOff()
Switch from off mode.
Time m_startCcaBusy
start CCA busy
void NotifyWakeup()
Notify all WifiPhyListener that we woke up.
void NotifyTxStart(Time duration, double txPowerDbm)
Notify all WifiPhyListener that the transmission has started for the given duration.
Listeners m_listeners
listeners
Time GetLastRxEndTime() const
Return the time the last RX end.
void SwitchFromRxEndError()
Switch from RX after the reception failed.
void SetReceiveOkCallback(RxOkCallback callback)
Set a callback for a successful reception.
void SwitchFromSleep()
Switch from sleep mode.
bool IsStateSleep() const
Check whether the current state is SLEEP.
bool IsStateRx() const
Check whether the current state is RX.
void SetReceiveErrorCallback(RxErrorCallback callback)
Set a callback for a failed reception.
void UnregisterListener(WifiPhyListener *listener)
Remove WifiPhyListener from this WifiPhyStateHelper.
void NotifyRxPsduSucceeded(Ptr< const WifiPsdu > psdu, RxSignalInfo rxSignalInfo, const WifiTxVector &txVector, uint16_t staId, const std::vector< bool > &statusPerMpdu)
Handle the successful reception of a PSDU.
Ptr< const Packet > GetPacket() const
Get the PSDU as a single packet.
Definition: wifi-psdu.cc:89
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
WifiMode GetMode(uint16_t staId=SU_STA_ID) const
If this TX vector is associated with an SU PPDU, return the selected payload transmission mode.
WifiPreamble GetPreambleType() const
uint8_t GetTxPowerLevel() const
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
int64x64_t Max(const int64x64_t &a, const int64x64_t &b)
Maximum.
Definition: int64x64.h:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
WifiChannelListType
Enumeration of the possible channel-list parameter elements defined in Table 8-5 of IEEE 802....
@ WIFI_CHANLIST_PRIMARY
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::unordered_map< uint16_t, Ptr< const WifiPsdu > > WifiConstPsduMap
Map of const PSDUs indexed by STA-ID.
RxSignalInfo structure containing info on the received signal.
Definition: phy-entity.h:70
double snr
SNR in linear scale.
Definition: phy-entity.h:71
WifiPhyState
The state of the PHY layer.
@ CCA_BUSY
The PHY layer has sense the medium busy through the CCA mechanism.
@ SWITCHING
The PHY layer is switching to other channel.
@ RX
The PHY layer is receiving a packet.
@ TX
The PHY layer is sending a packet.
@ OFF
The PHY layer is switched off.
@ SLEEP
The PHY layer is sleeping.
@ IDLE
The PHY layer is IDLE.