33#include "ns3/boolean.h"
34#include "ns3/double.h"
35#include "ns3/he-phy.h"
38#include "ns3/simulator.h"
39#include "ns3/spectrum-channel.h"
40#include "ns3/wifi-net-device.h"
55 TypeId(
"ns3::SpectrumWifiPhy")
59 .AddAttribute(
"DisableWifiReception",
60 "Prevent Wi-Fi frame sync from ever happening",
65 "TrackSignalsFromInactiveInterfaces",
66 "Enable or disable tracking signals coming from inactive spectrum PHY interfaces",
71 "TxMaskInnerBandMinimumRejection",
72 "Minimum rejection (dBr) for the inner band of the transmit spectrum mask",
75 MakeDoubleChecker<double>())
77 "TxMaskOuterBandMinimumRejection",
78 "Minimum rejection (dBr) for the outer band of the transmit spectrum mask",
81 MakeDoubleChecker<double>())
83 "TxMaskOuterBandMaximumRejection",
84 "Maximum rejection (dBr) for the outer band of the transmit spectrum mask",
87 MakeDoubleChecker<double>())
88 .AddTraceSource(
"SignalArrival",
91 "ns3::SpectrumWifiPhy::SignalArrivalCallback");
96 : m_spectrumPhyInterfaces{},
97 m_currentSpectrumPhyInterface{nullptr}
131 if (channelWidth < 20)
133 bands.push_back(
GetBand(channelWidth));
137 for (uint16_t bw = 160; bw >= 20; bw = bw / 2)
139 for (
uint32_t i = 0; i < (channelWidth / bw); ++i)
141 bands.push_back(
GetBand(bw, i));
149 for (
const auto& bandRuPair : ruBands)
151 allBands.push_back(bandRuPair.first);
164 for (
const auto& band : allBands)
183 const auto foundOverlappingChannel =
186 [freqRange, channel](
const auto& item) {
187 const auto spectrumRange = item.first;
188 const auto noOverlap =
189 ((freqRange.minFrequency >= spectrumRange.maxFrequency) ||
190 (freqRange.maxFrequency <= spectrumRange.minFrequency));
194 "Added a wifi spectrum channel that overlaps with another existing wifi "
197 auto wifiSpectrumPhyInterface = CreateObject<WifiSpectrumPhyInterface>(freqRange);
198 wifiSpectrumPhyInterface->SetSpectrumWifiPhy(
this);
199 wifiSpectrumPhyInterface->SetChannel(channel);
202 wifiSpectrumPhyInterface->SetDevice(
GetDevice());
256 if ((frequencyBefore == frequencyAfter) && (widthBefore == widthAfter))
270 "No spectrum channel covers frequency range ["
271 << frequencyAfter - (widthAfter / 2) <<
" MHz - "
272 << frequencyAfter + (widthAfter / 2) <<
" MHz]");
274 if (interfaceChanged)
276 NS_LOG_DEBUG(
"Switch to existing RF interface with frequency/width pair of ("
277 << frequencyAfter <<
", " << widthAfter <<
")");
304 Time rxDuration = rxParams->duration;
306 NS_LOG_DEBUG(
"Received signal with PSD " << *receivedSignalPsd <<
" and duration "
311 senderNodeId = rxParams->txPhy->GetDevice()->GetNode()->GetId();
313 NS_LOG_DEBUG(
"Received signal from " << senderNodeId <<
" with unfiltered power "
326 std::size_t index = 0;
328 for (const auto& band : bands)
330 uint16_t bw = (band.frequencies.second - band.frequencies.first) / 1e6;
332 index = ((bw != prevBw) ? 0 : (index + 1));
333 double rxPowerPerBandW =
334 WifiSpectrumValueHelper::GetBandPowerW(receivedSignalPsd, band.indices);
335 NS_LOG_DEBUG(
"Signal power received (watts) before antenna gain for "
336 << bw <<
" MHz channel band " << index <<
": " << band);
337 rxPowerPerBandW *=
DbToRatio(GetRxGain());
338 rxPowerW.insert({band, rxPowerPerBandW});
339 NS_LOG_DEBUG(
"Signal power received after antenna gain for "
340 << bw <<
" MHz channel band " << index <<
": " << rxPowerPerBandW <<
" W ("
341 <<
WToDbm(rxPowerPerBandW) <<
" dBm)");
344 totalRxPowerW += rxPowerPerBandW;
351 const auto& ruBands =
354 for (const auto& [band, ru] : ruBands)
356 double rxPowerPerBandW =
357 WifiSpectrumValueHelper::GetBandPowerW(receivedSignalPsd, band.indices);
358 NS_LOG_DEBUG(
"Signal power received (watts) before antenna gain for RU with type "
359 << ru.GetRuType() <<
" and index " << ru.GetIndex() <<
" -> ("
360 << band.indices.first <<
"; " << band.indices.second
361 <<
"): " << rxPowerPerBandW);
362 rxPowerPerBandW *=
DbToRatio(GetRxGain());
363 NS_LOG_DEBUG(
"Signal power received after antenna gain for RU with type "
364 << ru.GetRuType() <<
" and index " << ru.GetIndex() <<
" -> ("
365 << band.indices.first <<
"; " << band.indices.second <<
"): "
366 << rxPowerPerBandW <<
" W (" <<
WToDbm(rxPowerPerBandW) <<
" dBm)");
367 rxPowerW.insert({band, rxPowerPerBandW});
371 NS_LOG_DEBUG(
"Total signal power received after antenna gain: "
372 << totalRxPowerW <<
" W (" <<
WToDbm(totalRxPowerW) <<
" dBm)");
375 DynamicCast<WifiSpectrumSignalParameters>(rxParams);
378 m_signalCb(
bool(wifiRxParams), senderNodeId,
WToDbm(totalRxPowerW), rxDuration);
390 NS_LOG_INFO(
"Received Wi-Fi signal but blocked from syncing");
399 NS_LOG_INFO(
"Received Wi-Fi signal from a non-active PHY interface");
408 const auto txWidth = wifiRxParams->txWidth;
410 const auto& txVector = ppdu->GetTxVector();
413 NS_LOG_INFO(
"Received signal too weak to process: " <<
WToDbm(totalRxPowerW) <<
" dBm");
419 if (wifiRxParams->txPhy)
423 NS_LOG_INFO(
"Cannot start reception of the PPDU, consider it as interference");
460 spectrumPhyInterface.second->SetDevice(device);
482 uint16_t guardBandwidth = 0;
483 if (currentChannelWidth == 22)
496 guardBandwidth = currentChannelWidth;
498 return guardBandwidth;
506 auto numBandsInChannel =
static_cast<size_t>(channelWidth * 1e6 / subcarrierSpacing);
507 auto numBandsInBand =
static_cast<size_t>(bandWidth * 1e6 / subcarrierSpacing);
508 if (numBandsInBand % 2 == 0)
510 numBandsInChannel += 1;
514 size_t totalNumBands = rxSpectrumModel->GetNumBands();
515 NS_ASSERT_MSG((numBandsInChannel % 2 == 1) && (totalNumBands % 2 == 1),
516 "Should have odd number of bands");
517 NS_ASSERT_MSG((bandIndex * bandWidth) < channelWidth,
"Band index is out of bound");
518 NS_ASSERT(totalNumBands >= numBandsInChannel);
519 auto startIndex = ((totalNumBands - numBandsInChannel) / 2) + (bandIndex * numBandsInBand);
520 auto stopIndex = startIndex + numBandsInBand - 1;
523 NS_ASSERT(frequencies.first >= (freqRange.minFrequency * 1e6));
524 NS_ASSERT(frequencies.second <= (freqRange.maxFrequency * 1e6));
525 NS_ASSERT((frequencies.second - frequencies.first) == (bandWidth * 1e6));
526 if (startIndex >= totalNumBands / 2)
531 return {{startIndex, stopIndex}, frequencies};
539 auto startGuardBand = rxSpectrumModel->Begin();
540 auto startChannel = std::next(startGuardBand, indices.first);
541 auto endChannel = std::next(startGuardBand, indices.second + 1);
542 auto lowFreq =
static_cast<uint64_t
>(startChannel->fc);
543 auto highFreq =
static_cast<uint64_t
>(endChannel->fc);
544 return {lowFreq, highFreq};
547std::tuple<double, double, double>
565 const auto lowFreq = frequency - (width / 2);
566 const auto highFreq = frequency + (width / 2);
569 [lowFreq, highFreq](
const auto& item) {
570 return ((lowFreq >= item.first.minFrequency) &&
571 (highFreq <= item.first.maxFrequency));
AttributeValue implementation for Boolean.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
static RuBands GetRuBands(Ptr< const WifiPhy > phy, uint16_t channelWidth, uint16_t guardBandwidth)
Static function to compute the RU bands that belong to a given channel width.
bool IsInitialized() const
Check if the object has been initialized.
Smart pointer class similar to boost::intrusive_ptr.
~SpectrumWifiPhy() override
TracedCallback< bool, uint32_t, double, Time > m_signalCb
Signal callback.
std::tuple< double, double, double > GetTxMaskRejectionParams() const override
bool CanStartRx(Ptr< const WifiPpdu > ppdu, uint16_t txChannelWidth) const
Determine whether the PHY shall issue a PHY-RXSTART.indication primitive in response to a given PPDU.
void SetDevice(const Ptr< WifiNetDevice > device) override
Sets the device this PHY is associated with.
void Transmit(Ptr< WifiSpectrumSignalParameters > txParams)
This function is sending the signal to the Spectrum channel after finishing the configuration of the ...
void StartRx(Ptr< SpectrumSignalParameters > rxParams, Ptr< const WifiSpectrumPhyInterface > interface)
Input method for delivering a signal from the spectrum channel and low-level PHY interface to this Sp...
void DoInitialize() override
Initialize() implementation.
std::map< FrequencyRange, Ptr< WifiSpectrumPhyInterface > > m_spectrumPhyInterfaces
Spectrum PHY interfaces.
Ptr< AntennaModel > m_antenna
antenna model
FrequencyRange GetCurrentFrequencyRange() const override
Get the frequency range of the current RF interface.
Ptr< const WifiPpdu > GetRxPpduFromTxPpdu(Ptr< const WifiPpdu > ppdu)
Determine the WifiPpdu to be used by the RX PHY based on the WifiPpdu sent by the TX PHY.
Ptr< WifiSpectrumPhyInterface > GetInterfaceCoveringChannelBand(uint16_t frequency, uint16_t width) const
Get the spectrum PHY interface that covers a band portion of the RF channel.
Ptr< AntennaModel > GetAntenna() const
Get the antenna model used for reception.
Ptr< Channel > GetChannel() const override
Return the Channel this WifiPhy is connected to.
void AddChannel(const Ptr< SpectrumChannel > channel, const FrequencyRange &freqRange=WHOLE_WIFI_SPECTRUM)
Attach a SpectrumChannel to use for a given frequency range.
Ptr< WifiSpectrumPhyInterface > m_currentSpectrumPhyInterface
The current Spectrum PHY interface (held for performance reasons)
Ptr< WifiSpectrumPhyInterface > GetCurrentInterface() const
Get the currently active spectrum PHY interface.
double m_txMaskInnerBandMinimumRejection
The minimum rejection (in dBr) for the inner band of the transmit spectrum mask.
bool m_trackSignalsInactiveInterfaces
flag whether signals coming from inactive spectrum PHY interfaces are tracked
double m_txMaskOuterBandMinimumRejection
The minimum rejection (in dBr) for the outer band of the transmit spectrum mask.
void DoDispose() override
Destructor implementation.
uint16_t GetGuardBandwidth(uint16_t currentChannelWidth) const override
void ResetSpectrumModelIfNeeded()
Perform run-time spectrum model change if the one used by the current Spectrum PHY interface has chan...
static TypeId GetTypeId()
Get the type ID.
void SetAntenna(const Ptr< AntennaModel > antenna)
double m_txMaskOuterBandMaximumRejection
The maximum rejection (in dBr) for the outer band of the transmit spectrum mask.
WifiSpectrumBandInfo GetBand(uint16_t bandWidth, uint8_t bandIndex=0) override
Get the info of a given band.
bool m_disableWifiReception
forces this PHY to fail to sync on any signal
void UpdateInterferenceHelperBands()
This function is called to update the bands handled by the InterferenceHelper.
void StartTx(Ptr< const WifiPpdu > ppdu) override
void DoChannelSwitch() override
Actually switch channel based on the stored channel settings.
WifiSpectrumBandFrequencies ConvertIndicesToFrequencies(const WifiSpectrumBandIndices &indices) const override
This is a helper function to convert start and stop indices to start and stop frequencies.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
uint32_t GetSubcarrierSpacing() const
uint16_t GetChannelWidth() const
double GetRxSensitivity() const
Return the receive sensitivity threshold (dBm).
uint16_t GetFrequency() const
Ptr< PhyEntity > GetPhyEntity(WifiModulationClass modulation) const
Get the supported PHY entity corresponding to the modulation class.
virtual void SetDevice(const Ptr< WifiNetDevice > device)
Sets the device this PHY is associated with.
void StartReceivePreamble(Ptr< const WifiPpdu > ppdu, RxPowerWattPerChannelBand &rxPowersW, Time rxDuration)
Start receiving the PHY preamble of a PPDU (i.e.
Ptr< WifiNetDevice > GetDevice() const
Return the device this PHY is associated with.
void DoDispose() override
Destructor implementation.
virtual void DoChannelSwitch()
Actually switch channel based on the stored channel settings.
Ptr< InterferenceHelper > m_interference
Pointer to a helper responsible for interference computations.
void DoInitialize() override
Initialize() implementation.
WifiStandard GetStandard() const
Get the configured Wi-Fi standard.
Ptr< PhyEntity > GetPhyEntityForPpdu(const Ptr< const WifiPpdu > ppdu) const
Get the supported PHY entity to use for a received PPDU.
void SwitchMaybeToCcaBusy(const Ptr< const WifiPpdu > ppdu)
Check if PHY state should move to CCA busy state based on current state of interference tracker.
const WifiPhyOperatingChannel & GetOperatingChannel() const
Get a const reference to the operating channel.
Ptr< PhyEntity > GetLatestPhyEntity() const
Get the latest PHY entity supported by this PHY instance.
bool IsSet() const
Return true if a valid channel has been set, false otherwise.
std::vector< WifiSpectrumBandInfo > WifiSpectrumBands
vector of spectrum bands handled by this interface
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Ptr< const AttributeChecker > MakeBooleanChecker()
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double WToDbm(double w)
Convert from Watts to dBm.
std::pair< uint64_t, uint64_t > WifiSpectrumBandFrequencies
typedef for a pair of start and stop frequencies in Hz to represent a band
double Integral(const SpectrumValue &arg)
double DbmToW(double dBm)
Convert from dBm to Watts.
std::pair< uint32_t, uint32_t > WifiSpectrumBandIndices
typedef for a pair of start and stop sub-band indices
std::map< WifiSpectrumBandInfo, double > RxPowerWattPerChannelBand
A map of the received power (Watts) for each band.
double DbToRatio(double dB)
Convert from dB to ratio.
Struct defining a frequency range between minFrequency (MHz) and maxFrequency (MHz).
WifiSpectrumBandInfo structure containing info about a spectrum band.