A Discrete-Event Network Simulator
API
wifi-utils.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: S├ębastien Deronne <sebastien.deronne@gmail.com>
19  */
20 
21 #include "ns3/nstime.h"
22 #include "wifi-utils.h"
23 #include "ctrl-headers.h"
24 #include "wifi-mac-header.h"
25 #include "wifi-mac-trailer.h"
26 #include "wifi-net-device.h"
27 #include "ns3/ht-configuration.h"
28 #include "ns3/he-configuration.h"
29 #include "wifi-mode.h"
30 #include "ampdu-subframe-header.h"
31 
32 namespace ns3 {
33 
34 double
35 DbToRatio (double dB)
36 {
37  return std::pow (10.0, 0.1 * dB);
38 }
39 
40 double
41 DbmToW (double dBm)
42 {
43  return std::pow (10.0, 0.1 * (dBm - 30.0));
44 }
45 
46 double
47 WToDbm (double w)
48 {
49  return 10.0 * std::log10 (w) + 30.0;
50 }
51 
52 double
53 RatioToDb (double ratio)
54 {
55  return 10.0 * std::log10 (ratio);
56 }
57 
58 uint16_t
60 {
61  uint16_t gi = 800;
63  {
64  Ptr<HeConfiguration> heConfiguration = device->GetHeConfiguration ();
65  NS_ASSERT (heConfiguration); //If HE modulation is used, we should have a HE configuration attached
66  gi = static_cast<uint16_t> (heConfiguration->GetGuardInterval ().GetNanoSeconds ());
67  }
69  {
70  Ptr<HtConfiguration> htConfiguration = device->GetHtConfiguration ();
71  NS_ASSERT (htConfiguration); //If HT/VHT modulation is used, we should have a HT configuration attached
72  gi = htConfiguration->GetShortGuardIntervalSupported () ? 400 : 800;
73  }
74  return gi;
75 }
76 
77 uint16_t
78 ConvertGuardIntervalToNanoSeconds (WifiMode mode, bool htShortGuardInterval, Time heGuardInterval)
79 {
80  uint16_t gi;
82  {
83  gi = static_cast<uint16_t> (heGuardInterval.GetNanoSeconds ());
84  }
86  {
87  gi = htShortGuardInterval ? 400 : 800;
88  }
89  else
90  {
91  gi = 800;
92  }
93  return gi;
94 }
95 
96 uint16_t
97 GetChannelWidthForTransmission (WifiMode mode, uint16_t maxSupportedChannelWidth)
98 {
99  WifiModulationClass modulationClass = mode.GetModulationClass ();
100  if (maxSupportedChannelWidth > 20
101  && (modulationClass == WifiModulationClass::WIFI_MOD_CLASS_OFDM // all non-HT OFDM control and management frames
102  || modulationClass == WifiModulationClass::WIFI_MOD_CLASS_ERP_OFDM)) // special case of beacons at 2.4 GHz
103  {
104  return 20;
105  }
106  //at 2.4 GHz basic rate can be non-ERP DSSS
107  if (modulationClass == WifiModulationClass::WIFI_MOD_CLASS_DSSS
108  || modulationClass == WifiModulationClass::WIFI_MOD_CLASS_HR_DSSS)
109  {
110  return 22;
111  }
112  return maxSupportedChannelWidth;
113 }
114 
116 GetPreambleForTransmission (WifiModulationClass modulation, bool useShortPreamble)
117 {
118  if (modulation == WIFI_MOD_CLASS_HE)
119  {
120  return WIFI_PREAMBLE_HE_SU;
121  }
122  else if (modulation == WIFI_MOD_CLASS_VHT)
123  {
124  return WIFI_PREAMBLE_VHT_SU;
125  }
126  else if (modulation == WIFI_MOD_CLASS_HT)
127  {
128  return WIFI_PREAMBLE_HT_MF; // HT_GF has been removed
129  }
130  else if (modulation == WIFI_MOD_CLASS_HR_DSSS && useShortPreamble) //ERP_DSSS is modeled through HR_DSSS (since same preamble and modulation)
131  {
132  return WIFI_PREAMBLE_SHORT;
133  }
134  else
135  {
136  return WIFI_PREAMBLE_LONG;
137  }
138 }
139 
140 bool
142 {
143  switch (modClassReq)
144  {
145  case WIFI_MOD_CLASS_DSSS:
146  return (modClassAnswer == WIFI_MOD_CLASS_DSSS);
148  return (modClassAnswer == WIFI_MOD_CLASS_DSSS || modClassAnswer == WIFI_MOD_CLASS_HR_DSSS);
150  return (modClassAnswer == WIFI_MOD_CLASS_DSSS || modClassAnswer == WIFI_MOD_CLASS_HR_DSSS || modClassAnswer == WIFI_MOD_CLASS_ERP_OFDM);
151  case WIFI_MOD_CLASS_OFDM:
152  return (modClassAnswer == WIFI_MOD_CLASS_OFDM);
153  case WIFI_MOD_CLASS_HT:
154  case WIFI_MOD_CLASS_VHT:
155  case WIFI_MOD_CLASS_HE:
156  return true;
157  default:
158  NS_FATAL_ERROR ("Modulation class not defined");
159  return false;
160  }
161 }
162 
163 uint32_t
165 {
166  WifiMacHeader ack;
168  return ack.GetSize () + 4;
169 }
170 
171 uint32_t
173 {
174  WifiMacHeader hdr;
176  CtrlBAckResponseHeader blockAck;
177  blockAck.SetType (type);
178  return hdr.GetSize () + blockAck.GetSerializedSize () + 4;
179 }
180 
181 uint32_t
183 {
184  WifiMacHeader hdr;
187  bar.SetType (type);
188  return hdr.GetSize () + bar.GetSerializedSize () + 4;
189 }
190 
191 uint32_t
193 {
194  WifiMacHeader rts;
196  return rts.GetSize () + 4;
197 }
198 
199 uint32_t
201 {
202  WifiMacHeader cts;
204  return cts.GetSize () + 4;
205 }
206 
207 bool
208 IsInWindow (uint16_t seq, uint16_t winstart, uint16_t winsize)
209 {
210  return ((seq - winstart + 4096) % 4096) < winsize;
211 }
212 
213 void
215 {
216  WifiMacTrailer fcs;
217  packet->AddTrailer (fcs);
218 }
219 
220 uint32_t
221 GetSize (Ptr<const Packet> packet, const WifiMacHeader *hdr, bool isAmpdu)
222 {
223  uint32_t size;
224  WifiMacTrailer fcs;
225  if (isAmpdu)
226  {
227  size = packet->GetSize ();
228  }
229  else
230  {
231  size = packet->GetSize () + hdr->GetSize () + fcs.GetSerializedSize ();
232  }
233  return size;
234 }
235 
236 Time
238 {
239  Time duration;
240 
241  switch (preamble)
242  {
243  case WIFI_PREAMBLE_HT_MF:
246  case WIFI_PREAMBLE_HE_SU:
248  case WIFI_PREAMBLE_HE_MU:
249  case WIFI_PREAMBLE_HE_TB:
250  duration = MicroSeconds (5484);
251  break;
252  default:
253  duration = MicroSeconds (0);
254  break;
255  }
256  return duration;
257 }
258 
259 bool
260 IsHt (WifiPreamble preamble)
261 {
262  return (preamble == WIFI_PREAMBLE_HT_MF);
263 }
264 
265 bool
266 IsVht (WifiPreamble preamble)
267 {
268  return (preamble == WIFI_PREAMBLE_VHT_SU || preamble == WIFI_PREAMBLE_VHT_MU);
269 }
270 
271 bool
272 IsHe (WifiPreamble preamble)
273 {
274  return (preamble == WIFI_PREAMBLE_HE_SU || preamble == WIFI_PREAMBLE_HE_MU || preamble == WIFI_PREAMBLE_HE_TB || preamble == WIFI_PREAMBLE_HE_ER_SU);
275 }
276 
277 } //namespace ns3
WifiModulationClass
This enumeration defines the modulation classes per (Table 10-6 "Modulation classes"; IEEE 802...
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Ptr< HeConfiguration > GetHeConfiguration(void) const
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
void AddWifiMacTrailer(Ptr< Packet > packet)
Add FCS trailer to a packet.
Definition: wifi-utils.cc:214
uint32_t GetRtsSize(void)
Return the total RTS size (including FCS trailer).
Definition: wifi-utils.cc:192
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:41
uint32_t GetSerializedSize(void) const
uint32_t GetSize(Ptr< const Packet > packet, const WifiMacHeader *hdr, bool isAmpdu)
Return the total size of the packet after WifiMacHeader and FCS trailer have been added...
Definition: wifi-utils.cc:221
uint32_t GetSize(void) const
Return the size of the WifiMacHeader in octets.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
uint32_t GetAckSize(void)
Return the total Ack size (including FCS trailer).
Definition: wifi-utils.cc:164
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:813
The different BlockAckRequest variants.
bool IsVht(WifiPreamble preamble)
Return whether the preamble is a VHT format preamble.
Definition: wifi-utils.cc:266
WifiPreamble GetPreambleForTransmission(WifiModulationClass modulation, bool useShortPreamble)
Return the preamble to be used for the transmission.
Definition: wifi-utils.cc:116
Time GetGuardInterval(void) const
bool IsHe(WifiPreamble preamble)
Return whether the preamble is a HE format preamble.
Definition: wifi-utils.cc:272
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:68
HR/DSSS (Clause 16)
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
bool GetShortGuardIntervalSupported(void) const
void SetType(BlockAckReqType type)
Set the BlockAckRequest type.
uint32_t GetSerializedSize(void) const
uint16_t ConvertGuardIntervalToNanoSeconds(WifiMode mode, const Ptr< WifiNetDevice > device)
Convert the guard interval to nanoseconds based on the WifiMode.
Definition: wifi-utils.cc:59
Headers for BlockAck response.
Definition: ctrl-headers.h:194
uint32_t GetSerializedSize(void) const
Definition: ctrl-headers.cc:67
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:147
uint32_t GetBlockAckRequestSize(BlockAckReqType type)
Return the total BlockAckRequest size (including FCS trailer).
Definition: wifi-utils.cc:182
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:392
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
void SetType(BlockAckType type)
Set the block ack type.
uint16_t GetChannelWidthForTransmission(WifiMode mode, uint16_t maxSupportedChannelWidth)
Return the channel width that corresponds to the selected mode (instead of letting the PHY&#39;s default ...
Definition: wifi-utils.cc:97
bool IsInWindow(uint16_t seq, uint16_t winstart, uint16_t winsize)
Definition: wifi-utils.cc:208
bool IsAllowedControlAnswerModulationClass(WifiModulationClass modClassReq, WifiModulationClass modClassAnswer)
Return whether the modulation class of the selected mode for the control answer frame is allowed...
Definition: wifi-utils.cc:141
double WToDbm(double w)
Convert from Watts to dBm.
Definition: wifi-utils.cc:47
double DbToRatio(double dB)
Convert from dB to ratio.
Definition: wifi-utils.cc:35
double RatioToDb(double ratio)
Convert from ratio to dB.
Definition: wifi-utils.cc:53
uint32_t GetCtsSize(void)
Return the total CTS size (including FCS trailer).
Definition: wifi-utils.cc:200
void SetType(WifiMacType type, bool resetToDsFromDs=true)
Set Type/Subtype values with the correct values depending on the given type.
The different BlockAck variants.
Time GetPpduMaxTime(WifiPreamble preamble)
Get the maximum PPDU duration (see Section 10.14 of 802.11-2016) for the PHY layers defining the aPPD...
Definition: wifi-utils.cc:237
bool IsHt(WifiPreamble preamble)
Return whether the preamble is a HT format preamble.
Definition: wifi-utils.cc:260
uint32_t GetBlockAckSize(BlockAckType type)
Return the total BlockAck size (including FCS trailer).
Definition: wifi-utils.cc:172
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1305
Headers for BlockAckRequest.
Definition: ctrl-headers.h:42
Ptr< HtConfiguration > GetHtConfiguration(void) const
Implements the IEEE 802.11 MAC header.
Implements the IEEE 802.11 MAC trailer.