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 rts;
203  return rts.GetSize () + 4;
204 }
205 
206 uint32_t
208 {
209  WifiMacHeader cts;
211  return cts.GetSize () + 4;
212 }
213 
214 bool
215 IsInWindow (uint16_t seq, uint16_t winstart, uint16_t winsize)
216 {
217  return ((seq - winstart + 4096) % 4096) < winsize;
218 }
219 
220 void
222 {
223  WifiMacTrailer fcs;
224  packet->AddTrailer (fcs);
225 }
226 
227 uint32_t
228 GetSize (Ptr<const Packet> packet, const WifiMacHeader *hdr, bool isAmpdu)
229 {
230  uint32_t size;
231  WifiMacTrailer fcs;
232  if (isAmpdu)
233  {
234  size = packet->GetSize ();
235  }
236  else
237  {
238  size = packet->GetSize () + hdr->GetSize () + fcs.GetSerializedSize ();
239  }
240  return size;
241 }
242 
243 Time
245 {
246  Time duration;
247 
248  switch (preamble)
249  {
250  case WIFI_PREAMBLE_HT_GF:
251  duration = MicroSeconds (10000);
252  break;
253  case WIFI_PREAMBLE_HT_MF:
256  case WIFI_PREAMBLE_HE_SU:
258  case WIFI_PREAMBLE_HE_MU:
259  case WIFI_PREAMBLE_HE_TB:
260  duration = MicroSeconds (5484);
261  break;
262  default:
263  duration = MicroSeconds (0);
264  break;
265  }
266  return duration;
267 }
268 
269 bool
271 {
273  //Rely on metadata if present
274  bool metadataEnabled = false;
275  PacketMetadata::ItemIterator metadataIterator = packet->BeginItem ();
276  while (metadataIterator.HasNext ())
277  {
278  metadataEnabled = true;
279  PacketMetadata::Item item = metadataIterator.Next ();
280  if (item.tid == hdr.GetTypeId ())
281  {
282  return true;
283  }
284  }
285  if (metadataEnabled)
286  {
287  //Didn't find header in metadata
288  return false;
289  }
290 
291  //No metadata so peek manually into buffer and check consistency of extracted data
292  uint32_t totalSize = packet->GetSize ();
293  uint32_t deserialized = packet->PeekHeader (hdr);
294  if (deserialized == hdr.GetSerializedSize ()
295  && hdr.IsSignatureValid ()
296  && hdr.GetLength () <= (totalSize - deserialized))
297  {
298  return true;
299  }
300  else
301  {
302  return false;
303  }
304 }
305 
306 } //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:221
uint32_t GetRtsSize(void)
Return the total RTS size (including FCS trailer).
Definition: wifi-utils.cc:199
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:41
Item Next(void)
Retrieve the next metadata item.
structure describing a packet metadata item
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:228
uint32_t GetSize(void) const
Return the size of the WifiMacHeader in octets.
TypeId tid
TypeId of Header or Trailer.
#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:743
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
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:30
bool GetShortGuardIntervalSupported(void) const
uint32_t GetSerializedSize(void) const
Iterator class for metadata items.
uint16_t ConvertGuardIntervalToNanoSeconds(WifiMode mode, const Ptr< WifiNetDevice > device)
Convert the guard interval to nanoseconds based on the wifimode.
Definition: wifi-utils.cc:71
Introspection did not find any typical Config paths.
HT PHY (Clause 20)
Definition: wifi-mode.h:58
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
Headers for Block ack response.
Definition: ctrl-headers.h:193
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:494
bool HasNext(void) const
Checks if there is another metadata item.
bool IsAmpdu(Ptr< const Packet > packet)
Definition: wifi-utils.cc:270
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:215
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
PacketMetadata::ItemIterator BeginItem(void) const
Returns an iterator which points to the first &#39;item&#39; stored in this buffer.
Definition: packet.cc:566
double DbToRatio(double dB)
Convert from dB to ratio.
Definition: wifi-utils.cc:35
uint32_t GetSerializedSize(void) const
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:207
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:244
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
uint16_t GetLength(void) const
Return the length field.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1078
bool IsSignatureValid(void) const
Return whether the pattern stored in the delimiter signature field is correct, i.e.
Ptr< HtConfiguration > GetHtConfiguration(void) const
static TypeId GetTypeId(void)
Get the type ID.
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.