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 "ht-configuration.h"
28 #include "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 bool
59 Is2_4Ghz (double frequency)
60 {
61  return frequency >= 2400 && frequency <= 2500;
62 }
63 
64 bool
65 Is5Ghz (double frequency)
66 {
67  return frequency >= 5000 && frequency <= 6000;
68 }
69 
70 uint16_t
72 {
73  uint16_t gi = 800;
75  {
76  Ptr<HeConfiguration> heConfiguration = device->GetHeConfiguration ();
77  NS_ASSERT (heConfiguration); //If HE modulation is used, we should have a HE configuration attached
78  gi = static_cast<uint16_t> (heConfiguration->GetGuardInterval ().GetNanoSeconds ());
79  }
81  {
82  Ptr<HtConfiguration> htConfiguration = device->GetHtConfiguration ();
83  NS_ASSERT (htConfiguration); //If HT/VHT modulation is used, we should have a HT configuration attached
84  gi = htConfiguration->GetShortGuardIntervalSupported () ? 400 : 800;
85  }
86  return gi;
87 }
88 
89 uint16_t
90 ConvertGuardIntervalToNanoSeconds (WifiMode mode, bool htShortGuardInterval, Time heGuardInterval)
91 {
92  uint16_t gi;
94  {
95  gi = static_cast<uint16_t> (heGuardInterval.GetNanoSeconds ());
96  }
98  {
99  gi = htShortGuardInterval ? 400 : 800;
100  }
101  else
102  {
103  gi = 800;
104  }
105  return gi;
106 }
107 
108 uint16_t
109 GetChannelWidthForTransmission (WifiMode mode, uint16_t maxSupportedChannelWidth)
110 {
111  WifiModulationClass modulationClass = mode.GetModulationClass ();
112  if (maxSupportedChannelWidth > 20
113  && (modulationClass == WifiModulationClass::WIFI_MOD_CLASS_OFDM // all non-HT OFDM control and management frames
114  || modulationClass == WifiModulationClass::WIFI_MOD_CLASS_ERP_OFDM)) // special case of beacons at 2.4 GHz
115  {
116  return 20;
117  }
118  //at 2.4 GHz basic rate can be non-ERP DSSS
119  if (modulationClass == WifiModulationClass::WIFI_MOD_CLASS_DSSS
120  || modulationClass == WifiModulationClass::WIFI_MOD_CLASS_HR_DSSS)
121  {
122  return 22;
123  }
124  return maxSupportedChannelWidth;
125 }
126 
128 GetPreambleForTransmission (WifiModulationClass modulation, bool useShortPreamble, bool useGreenfield)
129 {
130  if (modulation == WIFI_MOD_CLASS_HE)
131  {
132  return WIFI_PREAMBLE_HE_SU;
133  }
134  else if (modulation == WIFI_MOD_CLASS_VHT)
135  {
136  return WIFI_PREAMBLE_VHT_SU;
137  }
138  else if (modulation == WIFI_MOD_CLASS_HT && useGreenfield)
139  {
140  //If protection for greenfield is used we go for HT_MF preamble which is the default protection for GF format defined in the standard.
141  return WIFI_PREAMBLE_HT_GF;
142  }
143  else if (modulation == WIFI_MOD_CLASS_HT)
144  {
145  return WIFI_PREAMBLE_HT_MF;
146  }
147  else if (useShortPreamble)
148  {
149  return WIFI_PREAMBLE_SHORT;
150  }
151  else
152  {
153  return WIFI_PREAMBLE_LONG;
154  }
155 }
156 
157 bool
159 {
160  switch (modClassReq)
161  {
162  case WIFI_MOD_CLASS_DSSS:
163  return (modClassAnswer == WIFI_MOD_CLASS_DSSS);
165  return (modClassAnswer == WIFI_MOD_CLASS_DSSS || modClassAnswer == WIFI_MOD_CLASS_HR_DSSS);
167  return (modClassAnswer == WIFI_MOD_CLASS_DSSS || modClassAnswer == WIFI_MOD_CLASS_HR_DSSS || modClassAnswer == WIFI_MOD_CLASS_ERP_OFDM);
168  case WIFI_MOD_CLASS_OFDM:
169  return (modClassAnswer == WIFI_MOD_CLASS_OFDM);
170  case WIFI_MOD_CLASS_HT:
171  case WIFI_MOD_CLASS_VHT:
172  case WIFI_MOD_CLASS_HE:
173  return true;
174  default:
175  NS_FATAL_ERROR ("Modulation class not defined");
176  return false;
177  }
178 }
179 
180 uint32_t
182 {
183  WifiMacHeader ack;
185  return ack.GetSize () + 4;
186 }
187 
188 uint32_t
190 {
191  WifiMacHeader hdr;
193  CtrlBAckResponseHeader blockAck;
194  blockAck.SetType (type);
195  return hdr.GetSize () + blockAck.GetSerializedSize () + 4;
196 }
197 
198 uint32_t
200 {
201  WifiMacHeader hdr;
204  bar.SetType (type);
205  return hdr.GetSize () + bar.GetSerializedSize () + 4;
206 }
207 
208 uint32_t
210 {
211  WifiMacHeader rts;
213  return rts.GetSize () + 4;
214 }
215 
216 uint32_t
218 {
219  WifiMacHeader cts;
221  return cts.GetSize () + 4;
222 }
223 
224 bool
225 IsInWindow (uint16_t seq, uint16_t winstart, uint16_t winsize)
226 {
227  return ((seq - winstart + 4096) % 4096) < winsize;
228 }
229 
230 void
232 {
233  WifiMacTrailer fcs;
234  packet->AddTrailer (fcs);
235 }
236 
237 uint32_t
238 GetSize (Ptr<const Packet> packet, const WifiMacHeader *hdr, bool isAmpdu)
239 {
240  uint32_t size;
241  WifiMacTrailer fcs;
242  if (isAmpdu)
243  {
244  size = packet->GetSize ();
245  }
246  else
247  {
248  size = packet->GetSize () + hdr->GetSize () + fcs.GetSerializedSize ();
249  }
250  return size;
251 }
252 
253 Time
255 {
256  Time duration;
257 
258  switch (preamble)
259  {
260  case WIFI_PREAMBLE_HT_GF:
261  duration = MicroSeconds (10000);
262  break;
263  case WIFI_PREAMBLE_HT_MF:
266  case WIFI_PREAMBLE_HE_SU:
268  case WIFI_PREAMBLE_HE_MU:
269  case WIFI_PREAMBLE_HE_TB:
270  duration = MicroSeconds (5484);
271  break;
272  default:
273  duration = MicroSeconds (0);
274  break;
275  }
276  return duration;
277 }
278 
279 } //namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
ERP-OFDM PHY (19.5)
Definition: wifi-mode.h:54
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:231
uint32_t GetRtsSize(void)
Return the total RTS size (including FCS trailer).
Definition: wifi-utils.cc:209
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:41
uint32_t GetSerializedSize(void) const
bool Is2_4Ghz(double frequency)
Definition: wifi-utils.cc:59
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:238
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:162
uint32_t GetAckSize(void)
Return the total ACK size (including FCS trailer).
Definition: wifi-utils.cc:181
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:745
bool Is5Ghz(double frequency)
Definition: wifi-utils.cc:65
VHT PHY (Clause 22)
Definition: wifi-mode.h:60
HR/DSSS PHY (Clause 18)
Definition: wifi-mode.h:48
WifiPreamble GetPreambleForTransmission(WifiModulationClass modulation, bool useShortPreamble, bool useGreenfield)
Return the preamble to be used for the transmission.
Definition: wifi-utils.cc:128
Time GetGuardInterval(void) const
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:97
void SetType(BlockAckType type)
Set the block ACK type.
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:32
bool GetShortGuardIntervalSupported(void) const
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:71
HT PHY (Clause 20)
Definition: wifi-mode.h:58
Headers for Block ack response.
Definition: ctrl-headers.h:193
uint32_t GetSerializedSize(void) const
Definition: ctrl-headers.cc:66
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:494
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:367
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:109
bool IsInWindow(uint16_t seq, uint16_t winstart, uint16_t winsize)
Definition: wifi-utils.cc:225
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:158
double WToDbm(double w)
Convert from Watts to dBm.
Definition: wifi-utils.cc:47
uint32_t GetBlockAckRequestSize(BlockAckType type)
Return the total Block Ack Request size (including FCS trailer).
Definition: wifi-utils.cc:199
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:217
void SetType(WifiMacType type, bool resetToDsFromDs=true)
Set Type/Subtype values with the correct values depending on the given type.
WifiModulationClass
This enumeration defines the modulation classes per (Table 9-4 "Modulation classes"; IEEE 802...
Definition: wifi-mode.h:36
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:254
uint32_t GetBlockAckSize(BlockAckType type)
Return the total Block ACK size (including FCS trailer).
Definition: wifi-utils.cc:189
OFDM PHY (Clause 17)
Definition: wifi-mode.h:56
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1080
Headers for Block ack request.
Definition: ctrl-headers.h:41
Ptr< HtConfiguration > GetHtConfiguration(void) const
HE PHY (Clause 26)
Definition: wifi-mode.h:62
Implements the IEEE 802.11 MAC header.
DSSS PHY (Clause 15)
Definition: wifi-mode.h:46
Implements the IEEE 802.11 MAC trailer.
BlockAckType
The different block ACK policies.