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