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