A Discrete-Event Network Simulator
API
wifi-phy-state-helper.h
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 #ifndef WIFI_PHY_STATE_HELPER_H
21 #define WIFI_PHY_STATE_HELPER_H
22 
23 #include "wifi-phy-common.h"
24 #include "wifi-phy-state.h"
25 #include "wifi-ppdu.h"
26 
27 #include "ns3/callback.h"
28 #include "ns3/nstime.h"
29 #include "ns3/object.h"
30 #include "ns3/traced-callback.h"
31 
32 #include <list>
33 #include <memory>
34 #include <vector>
35 
36 namespace ns3
37 {
38 
39 class WifiPhyListener;
40 class WifiTxVector;
41 class WifiMode;
42 class Packet;
43 class WifiPsdu;
44 struct RxSignalInfo;
45 
56 typedef Callback<void, Ptr<const WifiPsdu>, RxSignalInfo, WifiTxVector, std::vector<bool>>
64 
70 class WifiPhyStateHelper : public Object
71 {
72  public:
77  static TypeId GetTypeId();
78 
80 
86  void SetReceiveOkCallback(RxOkCallback callback);
98  void RegisterListener(const std::shared_ptr<WifiPhyListener>& listener);
104  void UnregisterListener(const std::shared_ptr<WifiPhyListener>& listener);
110  WifiPhyState GetState() const;
116  bool IsStateCcaBusy() const;
122  bool IsStateIdle() const;
128  bool IsStateRx() const;
134  bool IsStateTx() const;
140  bool IsStateSwitching() const;
146  bool IsStateSleep() const;
152  bool IsStateOff() const;
158  Time GetDelayUntilIdle() const;
164  Time GetLastRxStartTime() const;
170  Time GetLastRxEndTime() const;
171 
180  void SwitchToTx(Time txDuration,
181  WifiConstPsduMap psdus,
182  double txPowerDbm,
183  const WifiTxVector& txVector);
189  void SwitchToRx(Time rxDuration);
195  void SwitchToChannelSwitching(Time switchingDuration);
204  RxSignalInfo rxSignalInfo,
205  const WifiTxVector& txVector);
216  RxSignalInfo rxSignalInfo,
217  const WifiTxVector& txVector,
218  uint16_t staId,
219  const std::vector<bool>& statusPerMpdu);
226  void NotifyRxPsduFailed(Ptr<const WifiPsdu> psdu, double snr);
230  void SwitchFromRxEndOk();
234  void SwitchFromRxEndError();
239  void SwitchFromRxAbort(uint16_t operatingWidth);
250  void SwitchMaybeToCcaBusy(Time duration,
251  WifiChannelListType channelType,
252  const std::vector<Time>& per20MhzDurations);
256  void SwitchToSleep();
260  void SwitchFromSleep();
264  void SwitchToOff();
268  void SwitchFromOff();
269 
278  typedef void (*StateTracedCallback)(Time start, Time duration, WifiPhyState state);
279 
288  typedef void (*RxOkTracedCallback)(Ptr<const Packet> packet,
289  double snr,
290  WifiMode mode,
291  WifiPreamble preamble);
292 
299  typedef void (*RxEndErrorTracedCallback)(Ptr<const Packet> packet, double snr);
300 
309  typedef void (*TxTracedCallback)(Ptr<const Packet> packet,
310  WifiMode mode,
311  WifiPreamble preamble,
312  uint8_t power);
313 
322  template <typename FUNC, typename... Ts>
323  void NotifyListeners(FUNC f, Ts&&... args);
324 
325  private:
331  typedef std::list<std::weak_ptr<WifiPhyListener>> Listeners;
332 
337 
341  void DoSwitchFromRx();
342 
347 
349  bool m_sleeping;
350  bool m_isOff;
361 
370 };
371 
372 } // namespace ns3
373 
374 /***************************************************************
375  * Implementation of the templates declared above.
376  ***************************************************************/
377 
378 namespace ns3
379 {
380 
381 template <typename FUNC, typename... Ts>
382 void
384 {
385  NS_LOG_FUNCTION(this);
386  // In some cases (e.g., when notifying an EMLSR client of a link switch), a notification
387  // to a PHY listener involves the addition and/or removal of a PHY listener, thus modifying
388  // the list we are iterating over. This is dangerous, so ensure that we iterate over a copy
389  // of the list of PHY listeners. The copied list contains shared pointers to the PHY listeners
390  // to prevent them from being deleted.
391  std::list<std::shared_ptr<WifiPhyListener>> listeners;
392  std::transform(m_listeners.cbegin(),
393  m_listeners.cend(),
394  std::back_inserter(listeners),
395  [](auto&& listener) { return listener.lock(); });
396 
397  for (const auto& listener : listeners)
398  {
399  if (listener)
400  {
401  std::invoke(f, listener, std::forward<Ts>(args)...);
402  }
403  }
404 }
405 
406 } // namespace ns3
407 
408 #endif /* WIFI_PHY_STATE_HELPER_H */
double f(double x, void *params)
Definition: 80211b.c:70
Callback template class.
Definition: callback.h:438
A base class which provides memory management and object aggregation.
Definition: object.h:89
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
represent a single transmission mode
Definition: wifi-mode.h:51
This objects implements the PHY state machine of the Wifi device.
bool IsStateSwitching() const
Check whether the current state is SWITCHING.
void(* TxTracedCallback)(Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t power)
TracedCallback signature for transmit event.
void SwitchToRx(Time rxDuration)
Switch state to RX for the given duration.
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(* StateTracedCallback)(Time start, Time duration, WifiPhyState state)
TracedCallback signature for state changes.
void DoSwitchFromRx()
Switch the state from RX.
void SwitchFromRxEndOk()
Switch from RX after the reception was successful.
Time m_previousStateChangeTime
previous state change time
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
void UnregisterListener(const std::shared_ptr< WifiPhyListener > &listener)
Remove WifiPhyListener from this WifiPhyStateHelper.
TracedCallback< Time, Time, WifiPhyState > m_stateLogger
The trace source fired when state is changed.
void NotifyListeners(FUNC f, Ts &&... args)
Notify all WifiPhyListener objects of the given PHY event.
void LogPreviousIdleAndCcaBusyStates()
Log the idle and CCA busy states.
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(* RxEndErrorTracedCallback)(Ptr< const Packet > packet, double snr)
TracedCallback signature for receive end error event.
void SwitchFromRxAbort(uint16_t operatingWidth)
Abort current reception following a CCA reset request.
void(* RxOkTracedCallback)(Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
TracedCallback signature for receive end OK event.
WifiPhyState GetState() const
Return the current state of WifiPhy.
NS_LOG_TEMPLATE_DECLARE
the log component
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
bool IsStateTx() const
Check whether the current state is TX.
void SwitchFromOff()
Switch from off mode.
Time m_startCcaBusy
start CCA busy
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.
std::list< std::weak_ptr< WifiPhyListener > > Listeners
typedef for a list of WifiPhyListeners.
void SetReceiveErrorCallback(RxErrorCallback callback)
Set a callback for a failed reception.
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.
void RegisterListener(const std::shared_ptr< WifiPhyListener > &listener)
Register WifiPhyListener to this WifiPhyStateHelper.
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
WifiChannelListType
Enumeration of the possible channel-list parameter elements defined in Table 8-5 of IEEE 802....
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< void, Ptr< const WifiPsdu > > RxErrorCallback
Callback if PSDU unsuccessfuly received.
std::unordered_map< uint16_t, Ptr< const WifiPsdu > > WifiConstPsduMap
Map of const PSDUs indexed by STA-ID.
Callback< void, Ptr< const WifiPsdu >, RxSignalInfo, WifiTxVector, std::vector< bool > > RxOkCallback
Callback if PSDU successfully received (i.e.
RxSignalInfo structure containing info on the received signal.
Definition: phy-entity.h:69
Declaration of the following enums:
WifiPhyState
The state of the PHY layer.
Declaration of ns3::WifiPpdu class and ns3::WifiConstPsduMap.