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 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, bool useGreenfield)
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 && useGreenfield)
127  {
128  //If protection for Greenfield is used we go for HT_MF preamble which is the default protection for GF format defined in the standard.
129  return WIFI_PREAMBLE_HT_GF;
130  }
131  else if (modulation == WIFI_MOD_CLASS_HT)
132  {
133  return WIFI_PREAMBLE_HT_MF;
134  }
135  else if (useShortPreamble)
136  {
137  return WIFI_PREAMBLE_SHORT;
138  }
139  else
140  {
141  return WIFI_PREAMBLE_LONG;
142  }
143 }
144 
145 bool
147 {
148  switch (modClassReq)
149  {
150  case WIFI_MOD_CLASS_DSSS:
151  return (modClassAnswer == WIFI_MOD_CLASS_DSSS);
153  return (modClassAnswer == WIFI_MOD_CLASS_DSSS || modClassAnswer == WIFI_MOD_CLASS_HR_DSSS);
155  return (modClassAnswer == WIFI_MOD_CLASS_DSSS || modClassAnswer == WIFI_MOD_CLASS_HR_DSSS || modClassAnswer == WIFI_MOD_CLASS_ERP_OFDM);
156  case WIFI_MOD_CLASS_OFDM:
157  return (modClassAnswer == WIFI_MOD_CLASS_OFDM);
158  case WIFI_MOD_CLASS_HT:
159  case WIFI_MOD_CLASS_VHT:
160  case WIFI_MOD_CLASS_HE:
161  return true;
162  default:
163  NS_FATAL_ERROR ("Modulation class not defined");
164  return false;
165  }
166 }
167 
168 uint32_t
170 {
171  WifiMacHeader ack;
173  return ack.GetSize () + 4;
174 }
175 
176 uint32_t
178 {
179  WifiMacHeader hdr;
181  CtrlBAckResponseHeader blockAck;
182  blockAck.SetType (type);
183  return hdr.GetSize () + blockAck.GetSerializedSize () + 4;
184 }
185 
186 uint32_t
188 {
189  WifiMacHeader hdr;
192  bar.SetType (type);
193  return hdr.GetSize () + bar.GetSerializedSize () + 4;
194 }
195 
196 uint32_t
198 {
199  WifiMacHeader rts;
201  return rts.GetSize () + 4;
202 }
203 
204 uint32_t
206 {
207  WifiMacHeader cts;
209  return cts.GetSize () + 4;
210 }
211 
212 bool
213 IsInWindow (uint16_t seq, uint16_t winstart, uint16_t winsize)
214 {
215  return ((seq - winstart + 4096) % 4096) < winsize;
216 }
217 
218 void
220 {
221  WifiMacTrailer fcs;
222  packet->AddTrailer (fcs);
223 }
224 
225 uint32_t
226 GetSize (Ptr<const Packet> packet, const WifiMacHeader *hdr, bool isAmpdu)
227 {
228  uint32_t size;
229  WifiMacTrailer fcs;
230  if (isAmpdu)
231  {
232  size = packet->GetSize ();
233  }
234  else
235  {
236  size = packet->GetSize () + hdr->GetSize () + fcs.GetSerializedSize ();
237  }
238  return size;
239 }
240 
241 Time
243 {
244  Time duration;
245 
246  switch (preamble)
247  {
248  case WIFI_PREAMBLE_HT_GF:
249  duration = MicroSeconds (10000);
250  break;
251  case WIFI_PREAMBLE_HT_MF:
254  case WIFI_PREAMBLE_HE_SU:
256  case WIFI_PREAMBLE_HE_MU:
257  case WIFI_PREAMBLE_HE_TB:
258  duration = MicroSeconds (5484);
259  break;
260  default:
261  duration = MicroSeconds (0);
262  break;
263  }
264  return duration;
265 }
266 
267 } //namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
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:219
uint32_t GetRtsSize(void)
Return the total RTS size (including FCS trailer).
Definition: wifi-utils.cc:197
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:226
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:169
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:802
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:116
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:59
HT PHY (Clause 20)
Definition: wifi-mode.h:58
Headers for BlockAck response.
Definition: ctrl-headers.h:193
uint32_t GetSerializedSize(void) const
Definition: ctrl-headers.cc:66
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:463
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:391
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:213
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:146
double WToDbm(double w)
Convert from Watts to dBm.
Definition: wifi-utils.cc:47
uint32_t GetBlockAckRequestSize(BlockAckType type)
Return the total BlockAckRequest size (including FCS trailer).
Definition: wifi-utils.cc:187
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:205
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:242
uint32_t GetBlockAckSize(BlockAckType type)
Return the total BlockAck size (including FCS trailer).
Definition: wifi-utils.cc:177
OFDM PHY (Clause 17)
Definition: wifi-mode.h:56
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1294
Headers for BlockAckRequest.
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.