A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-phy-state-helper.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
10
11#include "wifi-phy-listener.h"
12#include "wifi-phy.h"
13#include "wifi-psdu.h"
14#include "wifi-tx-vector.h"
15
16#include "ns3/log.h"
17#include "ns3/packet.h"
18#include "ns3/simulator.h"
19
20#include <algorithm>
21#include <functional>
22#include <iterator>
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("WifiPhyStateHelper");
28
29NS_OBJECT_ENSURE_REGISTERED(WifiPhyStateHelper);
30
31TypeId
33{
34 static TypeId tid =
35 TypeId("ns3::WifiPhyStateHelper")
37 .SetGroupName("Wifi")
38 .AddConstructor<WifiPhyStateHelper>()
39 .AddTraceSource("State",
40 "The state of the PHY layer",
42 "ns3::WifiPhyStateHelper::StateTracedCallback")
43 .AddTraceSource("RxOk",
44 "A packet has been received successfully.",
46 "ns3::WifiPhyStateHelper::RxOkTracedCallback")
47 .AddTraceSource(
48 "RxOutcome",
49 "The outcome of the decoding of the PPDU, including MPDU decoding status",
51 "ns3::WifiPhyStateHelper::RxOutcomeTracedCallback")
52 .AddTraceSource("RxError",
53 "A packet has been received unsuccessfuly.",
55 "ns3::WifiPhyStateHelper::RxEndErrorTracedCallback")
56 .AddTraceSource("Tx",
57 "Packet transmission is starting.",
59 "ns3::WifiPhyStateHelper::TxTracedCallback");
60 return tid;
61}
62
64 : NS_LOG_TEMPLATE_DEFINE("WifiPhyStateHelper"),
65 m_sleeping(false),
66 m_isOff(false),
67 m_endTx(Time{0}),
68 m_endRx(Time{0}),
69 m_endCcaBusy(Time{0}),
70 m_endSwitching(Time{0}),
71 m_endSleep(Time{0}),
72 m_endOff(Time{0}),
73 m_endIdle(Time{0}),
74 m_startTx(Time{0}),
75 m_startRx(Time{0}),
76 m_startCcaBusy(Time{0}),
77 m_startSwitching(Time{0}),
78 m_startSleep(Time{0}),
79 m_startOff(Time{0}),
80 m_previousStateChangeTime(Time{0})
81{
82 NS_LOG_FUNCTION(this);
83}
84
85void
90
91void
96
97void
98WifiPhyStateHelper::RegisterListener(const std::shared_ptr<WifiPhyListener>& listener)
99{
100 m_listeners.emplace_back(listener);
101}
102
103void
104WifiPhyStateHelper::UnregisterListener(const std::shared_ptr<WifiPhyListener>& listener)
105{
106 m_listeners.remove_if([&listener](auto&& weakPtr) { return weakPtr.lock() == listener; });
107}
108
109bool
114
115bool
120
121bool
123{
124 return (GetState() == WifiPhyState::RX);
125}
126
127bool
129{
130 return (GetState() == WifiPhyState::TX);
131}
132
133bool
138
139bool
144
145bool
147{
148 return (GetState() == WifiPhyState::OFF);
149}
150
151Time
153{
154 Time retval;
155
156 switch (GetState())
157 {
158 case WifiPhyState::RX:
159 retval = m_endRx - Simulator::Now();
160 break;
161 case WifiPhyState::TX:
162 retval = m_endTx - Simulator::Now();
163 break;
165 retval = m_endCcaBusy - Simulator::Now();
166 break;
168 retval = m_endSwitching - Simulator::Now();
169 break;
173 retval = Seconds(0);
174 break;
175 default:
176 NS_FATAL_ERROR("Invalid WifiPhy state.");
177 retval = Seconds(0);
178 break;
179 }
180 retval = Max(retval, Seconds(0));
181 return retval;
182}
183
184Time
189
190Time
192{
193 return m_endRx;
194}
195
196Time
197WifiPhyStateHelper::GetLastTime(std::initializer_list<WifiPhyState> states) const
198{
199 Time last{0};
200 auto currentState = GetState();
201
202 for (auto state : states)
203 {
204 if (state == currentState)
205 {
206 return Simulator::Now();
207 }
208
209 switch (state)
210 {
211 case WifiPhyState::RX:
212 last = std::max(last, m_endRx);
213 break;
214 case WifiPhyState::TX:
215 last = std::max(last, m_endTx);
216 break;
218 last = std::max(last, m_endCcaBusy);
219 break;
221 last = std::max(last, m_endSwitching);
222 break;
224 last = std::max(last, m_endSleep);
225 break;
227 last = std::max(last, m_endOff);
228 break;
230 last = std::max(last, m_endIdle);
231 break;
232 default:
233 NS_FATAL_ERROR("Invalid WifiPhy state " << state);
234 }
235 }
236 NS_ASSERT(last <= Simulator::Now());
237 return last;
238}
239
242{
243 const auto now = Simulator::Now();
244 if (m_isOff)
245 {
246 return WifiPhyState::OFF;
247 }
248 if (m_sleeping)
249 {
250 return WifiPhyState::SLEEP;
251 }
252 else if (m_endTx > now)
253 {
254 return WifiPhyState::TX;
255 }
256 else if (m_endRx > now)
257 {
258 return WifiPhyState::RX;
259 }
260 else if (m_endSwitching > now)
261 {
263 }
264 else if (m_endCcaBusy > now)
265 {
267 }
268 else
269 {
270 return WifiPhyState::IDLE;
271 }
272}
273
274void
276{
277 NS_LOG_FUNCTION(this);
278 const auto now = Simulator::Now();
279 WifiPhyState state = GetState();
280 if (state == WifiPhyState::CCA_BUSY)
281 {
282 m_endCcaBusy = now;
283 const auto ccaStart =
285 m_stateLogger(ccaStart, now - ccaStart, WifiPhyState::CCA_BUSY);
286 }
287 else if (state == WifiPhyState::IDLE)
288 {
289 m_endIdle = now;
290 const auto endAllButCcaBusy =
292 const auto idleStart = std::max(m_endCcaBusy, endAllButCcaBusy);
293 NS_ASSERT(idleStart <= now);
294 if (m_endCcaBusy > endAllButCcaBusy)
295 {
296 const auto ccaBusyStart = std::max(m_startCcaBusy, endAllButCcaBusy);
297 if (const auto ccaBusyDuration = idleStart - ccaBusyStart;
298 ccaBusyDuration.IsStrictlyPositive())
299 {
300 m_stateLogger(ccaBusyStart, ccaBusyDuration, WifiPhyState::CCA_BUSY);
301 }
302 }
303 if (const auto idleDuration = now - idleStart; idleDuration.IsStrictlyPositive())
304 {
305 m_stateLogger(idleStart, idleDuration, WifiPhyState::IDLE);
306 }
307 }
308}
309
310void
312 const WifiConstPsduMap& psdus,
313 dBm_u txPower,
314 const WifiTxVector& txVector)
315{
316 NS_LOG_FUNCTION(this << txDuration << psdus << txPower << txVector);
317 if (!m_txTrace.IsEmpty())
318 {
319 for (const auto& psdu : psdus)
320 {
321 m_txTrace(psdu.second->GetPacket(),
322 txVector.GetMode(psdu.first),
323 txVector.GetPreambleType(),
324 txVector.GetTxPowerLevel());
325 }
326 }
327 const auto now = Simulator::Now();
328 switch (GetState())
329 {
330 case WifiPhyState::RX:
331 /* The packet which is being received as well
332 * as its endRx event are cancelled by the caller.
333 */
335 m_endRx = now;
336 break;
338 [[fallthrough]];
341 break;
342 default:
343 NS_FATAL_ERROR("Invalid WifiPhy state.");
344 break;
345 }
346 m_stateLogger(now, txDuration, WifiPhyState::TX);
348 m_endTx = now + txDuration;
349 m_startTx = now;
350 NotifyListeners(&WifiPhyListener::NotifyTxStart, txDuration, txPower);
351}
352
353void
355{
356 NS_LOG_FUNCTION(this << rxDuration);
358 Time now = Simulator::Now();
359 switch (GetState())
360 {
362 [[fallthrough]];
365 break;
366 default:
367 NS_FATAL_ERROR("Invalid WifiPhy state " << GetState());
368 break;
369 }
371 m_startRx = now;
372 m_endRx = now + rxDuration;
375}
376
377void
379{
380 NS_LOG_FUNCTION(this << switchingDuration);
381 Time now = Simulator::Now();
382 switch (GetState())
383 {
384 case WifiPhyState::RX:
385 /* The packet which is being received as well
386 * as its endRx event are cancelled by the caller.
387 */
389 m_endRx = now;
390 break;
392 [[fallthrough]];
395 break;
397 // do nothing
398 break;
399 default:
400 NS_FATAL_ERROR("Invalid WifiPhy state.");
401 break;
402 }
403
404 m_endCcaBusy = std::min(now, m_endCcaBusy);
405 m_stateLogger(now, switchingDuration, WifiPhyState::SWITCHING);
407 m_startSwitching = now;
408 m_endSwitching = now + switchingDuration;
410 NS_ASSERT(switchingDuration.IsZero() || IsStateSwitching());
411}
412
413void
415 RxSignalInfo rxSignalInfo,
416 const WifiTxVector& txVector)
417{
418 NS_LOG_FUNCTION(this << *psdu << rxSignalInfo << txVector);
419 if (!m_rxOkCallback.IsNull())
420 {
421 m_rxOkCallback(psdu, rxSignalInfo, txVector, {});
422 }
423}
424
425void
427 RxSignalInfo rxSignalInfo,
428 const WifiTxVector& txVector,
429 uint16_t staId,
430 const std::vector<bool>& statusPerMpdu)
431{
432 NS_LOG_FUNCTION(this << *psdu << rxSignalInfo << txVector << staId << statusPerMpdu.size()
433 << std::all_of(statusPerMpdu.begin(), statusPerMpdu.end(), [](bool v) {
434 return v;
435 })); // returns true if all true
436 NS_ASSERT(!statusPerMpdu.empty());
437 if (!m_rxOkTrace.IsEmpty())
438 {
439 m_rxOkTrace(psdu->GetPacket(),
440 rxSignalInfo.snr,
441 txVector.GetMode(staId),
442 txVector.GetPreambleType());
443 }
444 if (!m_rxOkCallback.IsNull())
445 {
446 m_rxOkCallback(psdu, rxSignalInfo, txVector, statusPerMpdu);
447 }
448}
449
450void
452{
453 NS_LOG_FUNCTION(this << *psdu << snr);
454 if (!m_rxErrorTrace.IsEmpty())
455 {
456 m_rxErrorTrace(psdu->GetPacket(), snr);
457 }
459 {
460 m_rxErrorCallback(psdu);
461 }
462}
463
464void
466 RxSignalInfo rxSignalInfo,
467 const WifiTxVector& txVector,
468 uint16_t staId,
469 const std::vector<bool>& statusPerMpdu)
470{
471 m_rxOutcomeTrace(ppdu, rxSignalInfo, txVector, statusPerMpdu);
472}
473
474void
482
483void
491
492void
502
503void
505 WifiChannelListType channelType,
506 const std::vector<Time>& per20MhzDurations)
507{
508 NS_LOG_FUNCTION(this << duration << channelType);
509 if (GetState() == WifiPhyState::RX)
510 {
511 return;
512 }
513 NotifyListeners(&WifiPhyListener::NotifyCcaBusyStart, duration, channelType, per20MhzDurations);
514 if (channelType != WIFI_CHANLIST_PRIMARY)
515 {
516 // WifiPhyStateHelper only updates CCA start and end durations for the primary channel
517 return;
518 }
519 Time now = Simulator::Now();
521 {
523 }
525 {
526 m_startCcaBusy = now;
527 }
528 m_endCcaBusy = std::max(m_endCcaBusy, now + duration);
529}
530
531void
533{
534 NS_LOG_FUNCTION(this);
535 Time now = Simulator::Now();
536 switch (GetState())
537 {
539 [[fallthrough]];
542 break;
543 default:
544 NS_FATAL_ERROR("Invalid WifiPhy state.");
545 break;
546 }
548 m_sleeping = true;
549 m_startSleep = now;
552}
553
554void
566
567void
569{
570 NS_LOG_FUNCTION(this << operatingWidth);
571 NS_ASSERT(IsStateCcaBusy()); // abort is called (with OBSS_PD_CCA_RESET reason) before RX is set
572 // by payload start
576 std::vector<Time> per20MhzDurations;
577 if (operatingWidth >= 40)
578 {
579 std::fill_n(std::back_inserter(per20MhzDurations), (operatingWidth / 20), Seconds(0));
580 }
582 Seconds(0),
584 per20MhzDurations);
586}
587
588void
590{
591 NS_LOG_FUNCTION(this);
592 Time now = Simulator::Now();
593 switch (GetState())
594 {
595 case WifiPhyState::RX:
596 /* The packet which is being received as well
597 * as its endRx event are cancelled by the caller.
598 */
600 m_endRx = now;
601 break;
602 case WifiPhyState::TX:
603 /* The packet which is being transmitted as well
604 * as its endTx event are cancelled by the caller.
605 */
607 m_endTx = now;
608 break;
610 [[fallthrough]];
613 break;
614 default:
615 NS_FATAL_ERROR("Invalid WifiPhy state.");
616 break;
617 }
619 m_isOff = true;
620 m_startOff = now;
623}
624
625void
636
637} // namespace ns3
#define Max(a, b)
bool IsNull() const
Check for null implementation.
Definition callback.h:555
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
bool IsZero() const
Exactly equivalent to t == 0.
Definition nstime.h:304
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
virtual void NotifyRxEndError()=0
We have received the last bit of a packet for which NotifyRxStart was invoked first and,...
virtual void NotifySwitchingStart(Time duration)=0
virtual void NotifyCcaBusyStart(Time duration, WifiChannelListType channelType, const std::vector< Time > &per20MhzDurations)=0
virtual void NotifyOff()=0
Notify listeners that we went to switch off.
virtual void NotifyRxEndOk()=0
We have received the last bit of a packet for which NotifyRxStart was invoked first and,...
virtual void NotifyTxStart(Time duration, dBm_u txPower)=0
virtual void NotifySleep()=0
Notify listeners that we went to sleep.
virtual void NotifyOn()=0
Notify listeners that we went to switch on.
virtual void NotifyRxStart(Time duration)=0
virtual void NotifyWakeup()=0
Notify listeners that we woke up.
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 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 DoSwitchFromRx()
Switch the state from RX.
void SwitchToTx(Time txDuration, const WifiConstPsduMap &psdus, dBm_u txPower, const WifiTxVector &txVector)
Switch state to TX for the given duration.
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
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.
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
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
void SwitchFromRxAbort(MHz_u operatingWidth)
Abort current reception following a CCA reset request.
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 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...
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:55
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition log.h:225
#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:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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.
WifiPhyState
The state of the PHY layer.
@ SWITCHING
The PHY layer is switching to other channel.
@ TX
The PHY layer is sending a packet.
@ OFF
The PHY layer is switched off.
@ IDLE
The PHY layer is IDLE.
@ CCA_BUSY
The PHY layer has sense the medium busy through the CCA mechanism.
@ SLEEP
The PHY layer is sleeping.
@ RX
The PHY layer is receiving a packet.
RxSignalInfo structure containing info on the received signal.
Definition wifi-types.h:72
double snr
SNR in linear scale.
Definition wifi-types.h:73