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 "wifi-utils.h"
22 #include "wifi-mac-header.h"
23 #include <cmath>
24 
25 namespace ns3 {
26 
27 double
28 Log2 (double val)
29 {
30  return std::log (val) / std::log (2.0);
31 }
32 
33 
34 double
35 DbToRatio (double dB)
36 {
37  double ratio = std::pow (10.0, dB / 10.0);
38  return ratio;
39 }
40 
41 double
42 DbmToW (double dBm)
43 {
44  double mW = std::pow (10.0, dBm / 10.0);
45  return mW / 1000.0;
46 }
47 
48 double
49 WToDbm (double w)
50 {
51  return 10.0 * std::log10 (w * 1000.0);
52 }
53 
54 double
55 RatioToDb (double ratio)
56 {
57  return 10.0 * std::log10 (ratio);
58 }
59 
60 bool
61 Is2_4Ghz (double frequency)
62 {
63  if (frequency >= 2400 && frequency <= 2500)
64  {
65  return true;
66  }
67  return false;
68 }
69 
70 bool
71 Is5Ghz (double frequency)
72 {
73  if (frequency >= 5000 && frequency <= 6000)
74  {
75  return true;
76  }
77  return false;
78 }
79 
80 uint16_t
81 ConvertGuardIntervalToNanoSeconds (WifiMode mode, bool htShortGuardInterval, Time heGuardInterval)
82 {
83  uint16_t gi;
85  {
86  gi = heGuardInterval.GetNanoSeconds ();
87  }
89  {
90  gi = htShortGuardInterval ? 400 : 800;
91  }
92  else
93  {
94  gi = 800;
95  }
96  return gi;
97 }
98 
99 uint32_t
101 {
102  WifiMacHeader ack;
104  return ack.GetSize () + 4;
105 }
106 
107 uint32_t
109 {
110  WifiMacHeader hdr;
112  CtrlBAckResponseHeader blockAck;
113  if (type == BASIC_BLOCK_ACK)
114  {
115  blockAck.SetType (BASIC_BLOCK_ACK);
116  }
117  else if (type == COMPRESSED_BLOCK_ACK)
118  {
119  blockAck.SetType (COMPRESSED_BLOCK_ACK);
120  }
121  else if (type == MULTI_TID_BLOCK_ACK)
122  {
123  //Not implemented
124  NS_ASSERT (false);
125  }
126  return hdr.GetSize () + blockAck.GetSerializedSize () + 4;
127 }
128 
129 uint32_t
131 {
132  WifiMacHeader rts;
134  return rts.GetSize () + 4;
135 }
136 
137 uint32_t
139 {
140  WifiMacHeader cts;
142  return cts.GetSize () + 4;
143 }
144 
145 bool
146 IsInWindow (uint16_t seq, uint16_t winstart, uint16_t winsize)
147 {
148  return ((seq - winstart + 4096) % 4096) < winsize;
149 }
150 
151 void
153 {
154  WifiMacTrailer fcs;
155  packet->AddTrailer (fcs);
156 }
157 
158 uint32_t
159 GetSize (Ptr<const Packet> packet, const WifiMacHeader *hdr, bool isAmpdu)
160 {
161  uint32_t size;
162  WifiMacTrailer fcs;
163  if (isAmpdu)
164  {
165  size = packet->GetSize ();
166  }
167  else
168  {
169  size = packet->GetSize () + hdr->GetSize () + fcs.GetSerializedSize ();
170  }
171  return size;
172 }
173 
174 } //namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
uint32_t GetSize(void) const
Return the size of the WifiMacHeader in octets.
void SetType(WifiMacType type)
Set Type/Subtype values with the correct values depending on the given type.
void AddWifiMacTrailer(Ptr< Packet > packet)
Add FCS trailer to a packet.
Definition: wifi-utils.cc:152
uint32_t GetRtsSize(void)
Return the total RTS size (including FCS trailer).
Definition: wifi-utils.cc:130
uint16_t ConvertGuardIntervalToNanoSeconds(WifiMode mode, bool htShortGuardInterval, Time heGuardInterval)
Convert the guard interval to nanoseconds based on the wifimode.
Definition: wifi-utils.cc:81
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:42
bool Is2_4Ghz(double frequency)
Definition: wifi-utils.cc:61
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:159
#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
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:821
uint32_t GetAckSize(void)
Return the total ACK size (including FCS trailer).
Definition: wifi-utils.cc:100
bool Is5Ghz(double frequency)
Definition: wifi-utils.cc:71
VHT PHY (Clause 22)
Definition: wifi-mode.h:60
BlockAckType
Enumeration for different block ACK policies.
Definition: ctrl-headers.h:31
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:97
HT PHY (Clause 20)
Definition: wifi-mode.h:58
Headers for Block ack response.
Definition: ctrl-headers.h:190
uint32_t GetSerializedSize(void) const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
void SetType(BlockAckType type)
Set the block ACK type.
bool IsInWindow(uint16_t seq, uint16_t winstart, uint16_t winsize)
Definition: wifi-utils.cc:146
double WToDbm(double w)
Convert from Watts to dBm.
Definition: wifi-utils.cc:49
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:55
uint32_t GetCtsSize(void)
Return the total CTS size (including FCS trailer).
Definition: wifi-utils.cc:138
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:367
uint32_t GetBlockAckSize(BlockAckType type)
Return the total Block ACK size (including FCS trailer).
Definition: wifi-utils.cc:108
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:487
SpectrumValue Log2(const SpectrumValue &arg)
HE PHY (Clause 26)
Definition: wifi-mode.h:62
Implements the IEEE 802.11 MAC header.
Implements the IEEE 802.11 MAC trailer.
uint32_t GetSerializedSize(void) const