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
192 GetMuBarSize (std::list<BlockAckReqType> types)
193 {
194  WifiMacHeader hdr;
196  CtrlTriggerHeader trigger;
197  trigger.SetType (MU_BAR_TRIGGER);
198  for (auto& t : types)
199  {
200  auto userInfo = trigger.AddUserInfoField ();
202  bar.SetType (t);
203  userInfo.SetMuBarTriggerDepUserInfo (bar);
204  }
205  return hdr.GetSize () + trigger.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_MF:
263  case WIFI_PREAMBLE_HE_SU:
265  case WIFI_PREAMBLE_HE_MU:
266  case WIFI_PREAMBLE_HE_TB:
267  duration = MicroSeconds (5484);
268  break;
269  default:
270  duration = MicroSeconds (0);
271  break;
272  }
273  return duration;
274 }
275 
276 bool
277 IsHt (WifiPreamble preamble)
278 {
279  return (preamble == WIFI_PREAMBLE_HT_MF);
280 }
281 
282 bool
283 IsVht (WifiPreamble preamble)
284 {
285  return (preamble == WIFI_PREAMBLE_VHT_SU || preamble == WIFI_PREAMBLE_VHT_MU);
286 }
287 
288 bool
289 IsHe (WifiPreamble preamble)
290 {
291  return (preamble == WIFI_PREAMBLE_HE_SU || preamble == WIFI_PREAMBLE_HE_MU || preamble == WIFI_PREAMBLE_HE_TB || preamble == WIFI_PREAMBLE_HE_ER_SU);
292 }
293 
294 } //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
void SetType(TriggerFrameType type)
Set the Trigger frame type.
Ptr< HeConfiguration > GetHeConfiguration(void) const
Headers for Trigger frames.
Definition: ctrl-headers.h:885
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 GetSerializedSize(void) const override
Definition: ctrl-headers.cc:71
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
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: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:283
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:289
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:47
HR/DSSS (Clause 16)
uint32_t GetSerializedSize(void) const
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:201
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:159
uint32_t GetMuBarSize(std::list< BlockAckReqType > types)
Return the total MU-BAR size (including FCS trailer).
Definition: wifi-utils.cc:192
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: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: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:217
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:254
bool IsHt(WifiPreamble preamble)
Return whether the preamble is a HT format preamble.
Definition: wifi-utils.cc:277
CtrlTriggerUserInfoField & AddUserInfoField(void)
Append a new User Info field to this Trigger frame and return a non-const reference to it...
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:48
Ptr< HtConfiguration > GetHtConfiguration(void) const
Implements the IEEE 802.11 MAC header.
Implements the IEEE 802.11 MAC trailer.