A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
36namespace ns3
37{
38
39class WifiPhyListener;
40class WifiTxVector;
41class WifiMode;
42class Packet;
43class WifiPsdu;
44struct RxSignalInfo;
45
46/**
47 * Callback if PSDU successfully received (i.e. if aggregate,
48 * it means that at least one MPDU of the A-MPDU was received,
49 * considering that the per-MPDU reception status is also provided).
50 *
51 * arg1: PSDU received successfully
52 * arg2: info on the received signal (\see RxSignalInfo)
53 * arg3: TXVECTOR of PSDU
54 * arg4: vector of per-MPDU status of reception.
55 */
56typedef Callback<void, Ptr<const WifiPsdu>, RxSignalInfo, WifiTxVector, std::vector<bool>>
58/**
59 * Callback if PSDU unsuccessfuly received
60 *
61 * arg1: PSDU received unsuccessfuly
62 */
64
65/**
66 * \ingroup wifi
67 *
68 * This objects implements the PHY state machine of the Wifi device.
69 */
71{
72 public:
73 /**
74 * \brief Get the type ID.
75 * \return the object TypeId
76 */
77 static TypeId GetTypeId();
78
80
81 /**
82 * Set a callback for a successful reception.
83 *
84 * \param callback the RxOkCallback to set
85 */
87 /**
88 * Set a callback for a failed reception.
89 *
90 * \param callback the RxErrorCallback to set
91 */
93 /**
94 * Register WifiPhyListener to this WifiPhyStateHelper.
95 *
96 * \param listener the WifiPhyListener to register
97 */
98 void RegisterListener(const std::shared_ptr<WifiPhyListener>& listener);
99 /**
100 * Remove WifiPhyListener from this WifiPhyStateHelper.
101 *
102 * \param listener the WifiPhyListener to unregister
103 */
104 void UnregisterListener(const std::shared_ptr<WifiPhyListener>& listener);
105 /**
106 * Return the current state of WifiPhy.
107 *
108 * \return the current state of WifiPhy
109 */
110 WifiPhyState GetState() const;
111 /**
112 * Check whether the current state is CCA busy.
113 *
114 * \return true if the current state is CCA busy, false otherwise
115 */
116 bool IsStateCcaBusy() const;
117 /**
118 * Check whether the current state is IDLE.
119 *
120 * \return true if the current state is IDLE, false otherwise
121 */
122 bool IsStateIdle() const;
123 /**
124 * Check whether the current state is RX.
125 *
126 * \return true if the current state is RX, false otherwise
127 */
128 bool IsStateRx() const;
129 /**
130 * Check whether the current state is TX.
131 *
132 * \return true if the current state is TX, false otherwise
133 */
134 bool IsStateTx() const;
135 /**
136 * Check whether the current state is SWITCHING.
137 *
138 * \return true if the current state is SWITCHING, false otherwise
139 */
140 bool IsStateSwitching() const;
141 /**
142 * Check whether the current state is SLEEP.
143 *
144 * \return true if the current state is SLEEP, false otherwise
145 */
146 bool IsStateSleep() const;
147 /**
148 * Check whether the current state is OFF.
149 *
150 * \return true if the current state is OFF, false otherwise
151 */
152 bool IsStateOff() const;
153 /**
154 * Return the time before the state is back to IDLE.
155 *
156 * \return the delay before the state is back to IDLE
157 */
158 Time GetDelayUntilIdle() const;
159 /**
160 * Return the time the last RX start.
161 *
162 * \return the time the last RX start.
163 */
164 Time GetLastRxStartTime() const;
165 /**
166 * Return the time the last RX end.
167 *
168 * \return the time the last RX end.
169 */
170 Time GetLastRxEndTime() const;
171
172 /**
173 * \param states a set of PHY states
174 * \return the last time the PHY has been in any of the given states
175 */
176 Time GetLastTime(std::initializer_list<WifiPhyState> states) const;
177
178 /**
179 * Switch state to TX for the given duration.
180 *
181 * \param txDuration the duration of the PPDU to transmit
182 * \param psdus the PSDUs in the transmitted PPDU (only one unless it is a MU PPDU)
183 * \param txPowerDbm the nominal TX power in dBm
184 * \param txVector the TX vector for the transmission
185 */
186 void SwitchToTx(Time txDuration,
187 WifiConstPsduMap psdus,
188 double txPowerDbm,
189 const WifiTxVector& txVector);
190 /**
191 * Switch state to RX for the given duration.
192 *
193 * \param rxDuration the duration of the RX
194 */
195 void SwitchToRx(Time rxDuration);
196 /**
197 * Switch state to channel switching for the given duration.
198 *
199 * \param switchingDuration the duration of required to switch the channel
200 */
201 void SwitchToChannelSwitching(Time switchingDuration);
202 /**
203 * Notify the reception of an MPDU included in an A-MPDU.
204 *
205 * \param psdu the successfully received PSDU
206 * \param rxSignalInfo the info on the received signal (\see RxSignalInfo)
207 * \param txVector TXVECTOR of the PSDU
208 */
210 RxSignalInfo rxSignalInfo,
211 const WifiTxVector& txVector);
212 /**
213 * Handle the successful reception of a PSDU.
214 *
215 * \param psdu the successfully received PSDU
216 * \param rxSignalInfo the info on the received signal (\see RxSignalInfo)
217 * \param txVector TXVECTOR of the PSDU
218 * \param staId the station ID of the PSDU (only used for MU)
219 * \param statusPerMpdu reception status per MPDU
220 */
222 RxSignalInfo rxSignalInfo,
223 const WifiTxVector& txVector,
224 uint16_t staId,
225 const std::vector<bool>& statusPerMpdu);
226 /**
227 * Handle the unsuccessful reception of a PSDU.
228 *
229 * \param psdu the PSDU that we failed to received
230 * \param snr the SNR of the received PSDU in linear scale
231 */
232 void NotifyRxPsduFailed(Ptr<const WifiPsdu> psdu, double snr);
233 /**
234 * Switch from RX after the reception was successful.
235 */
236 void SwitchFromRxEndOk();
237 /**
238 * Switch from RX after the reception failed.
239 */
241 /**
242 * Abort current reception following a CCA reset request.
243 * \param operatingWidth the channel width the PHY is operating on (in MHz)
244 */
245 void SwitchFromRxAbort(uint16_t operatingWidth);
246 /**
247 * Switch to CCA busy.
248 *
249 * \param duration the duration of the CCA state
250 * \param channelType the channel type for which the CCA busy state is reported.
251 * \param per20MhzDurations vector that indicates for how long each 20 MHz subchannel
252 * (corresponding to the index of the element in the vector) is busy and where a zero
253 * duration indicates that the subchannel is idle. The vector is non-empty if the PHY supports
254 * 802.11ax or later and if the operational channel width is larger than 20 MHz.
255 */
256 void SwitchMaybeToCcaBusy(Time duration,
257 WifiChannelListType channelType,
258 const std::vector<Time>& per20MhzDurations);
259 /**
260 * Switch to sleep mode.
261 */
262 void SwitchToSleep();
263 /**
264 * Switch from sleep mode.
265 */
266 void SwitchFromSleep();
267 /**
268 * Switch to off mode.
269 */
270 void SwitchToOff();
271 /**
272 * Switch from off mode.
273 */
274 void SwitchFromOff();
275
276 /**
277 * TracedCallback signature for state changes.
278 *
279 * \param [in] start Time when the \pname{state} started.
280 * \param [in] duration Amount of time we've been in (or will be in)
281 * the \pname{state}.
282 * \param [in] state The state.
283 */
284 typedef void (*StateTracedCallback)(Time start, Time duration, WifiPhyState state);
285
286 /**
287 * TracedCallback signature for receive end OK event.
288 *
289 * \param [in] packet The received packet.
290 * \param [in] snr The SNR of the received packet in linear scale.
291 * \param [in] mode The transmission mode of the packet.
292 * \param [in] preamble The preamble of the packet.
293 */
294 typedef void (*RxOkTracedCallback)(Ptr<const Packet> packet,
295 double snr,
296 WifiMode mode,
297 WifiPreamble preamble);
298
299 /**
300 * TracedCallback signature for receive end error event.
301 *
302 * \param [in] packet The received packet.
303 * \param [in] snr The SNR of the received packet in linear scale.
304 */
305 typedef void (*RxEndErrorTracedCallback)(Ptr<const Packet> packet, double snr);
306
307 /**
308 * TracedCallback signature for transmit event.
309 *
310 * \param [in] packet The received packet.
311 * \param [in] mode The transmission mode of the packet.
312 * \param [in] preamble The preamble of the packet.
313 * \param [in] power The transmit power level.
314 */
315 typedef void (*TxTracedCallback)(Ptr<const Packet> packet,
316 WifiMode mode,
317 WifiPreamble preamble,
318 uint8_t power);
319
320 /**
321 * Notify all WifiPhyListener objects of the given PHY event.
322 *
323 * \tparam FUNC \deduced Member function type
324 * \tparam Ts \deduced Function argument types
325 * \param f the member function to invoke
326 * \param args arguments to pass to the member function
327 */
328 template <typename FUNC, typename... Ts>
329 void NotifyListeners(FUNC f, Ts&&... args);
330
331 private:
332 /**
333 * typedef for a list of WifiPhyListeners. We use weak pointers so that unregistering a
334 * listener is not necessary to delete a listener (reference count is not incremented by
335 * weak pointers).
336 */
337 typedef std::list<std::weak_ptr<WifiPhyListener>> Listeners;
338
339 /**
340 * Log the idle and CCA busy states.
341 */
343
344 /**
345 * Switch the state from RX.
346 */
347 void DoSwitchFromRx();
348
349 /**
350 * The trace source fired when state is changed.
351 */
353
354 NS_LOG_TEMPLATE_DECLARE; //!< the log component
355 bool m_sleeping; ///< sleeping
356 bool m_isOff; ///< switched off
357 Time m_endTx; ///< end transmit
358 Time m_endRx; ///< end receive
359 Time m_endCcaBusy; ///< end CCA busy
360 Time m_endSwitching; ///< end switching
361 Time m_endSleep; ///< end sleep
362 Time m_endOff; ///< end off
363 Time m_endIdle; ///< end idle
364 Time m_startTx; ///< start transmit
365 Time m_startRx; ///< start receive
366 Time m_startCcaBusy; ///< start CCA busy
367 Time m_startSwitching; ///< start switching
368 Time m_startSleep; ///< start sleep
369 Time m_startOff; ///< start off
370 Time m_previousStateChangeTime; ///< previous state change time
371
372 Listeners m_listeners; ///< listeners
374 m_rxOkTrace; ///< receive OK trace callback
375 TracedCallback<Ptr<const Packet>, double> m_rxErrorTrace; ///< receive error trace callback
377 m_txTrace; ///< transmit trace callback
378 RxOkCallback m_rxOkCallback; ///< receive OK callback
379 RxErrorCallback m_rxErrorCallback; ///< receive error callback
380};
381
382} // namespace ns3
383
384/***************************************************************
385 * Implementation of the templates declared above.
386 ***************************************************************/
387
388namespace ns3
389{
390
391template <typename FUNC, typename... Ts>
392void
394{
395 NS_LOG_FUNCTION(this);
396 // In some cases (e.g., when notifying an EMLSR client of a link switch), a notification
397 // to a PHY listener involves the addition and/or removal of a PHY listener, thus modifying
398 // the list we are iterating over. This is dangerous, so ensure that we iterate over a copy
399 // of the list of PHY listeners. The copied list contains shared pointers to the PHY listeners
400 // to prevent them from being deleted.
401 std::list<std::shared_ptr<WifiPhyListener>> listeners;
402 std::transform(m_listeners.cbegin(),
403 m_listeners.cend(),
404 std::back_inserter(listeners),
405 [](auto&& listener) { return listener.lock(); });
406
407 for (const auto& listener : listeners)
408 {
409 if (listener)
410 {
411 std::invoke(f, listener, std::forward<Ts>(args)...);
412 }
413 }
414}
415
416} // namespace ns3
417
418#endif /* WIFI_PHY_STATE_HELPER_H */
Callback template class.
Definition: callback.h:438
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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
Time GetLastTime(std::initializer_list< WifiPhyState > states) const
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.
WifiPhyState
The state of the PHY layer.
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:
Declaration of ns3::WifiPpdu class and ns3::WifiConstPsduMap.