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