A Discrete-Event Network Simulator
API
supported-rates.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "supported-rates.h"
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("SupportedRates");
28 
30  : extended (this),
31  m_nRates (0)
32 {
33 }
34 
36 {
37  m_nRates = rates.m_nRates;
38  memcpy (m_rates, rates.m_rates, MAX_SUPPORTED_RATES);
39  // reset the back pointer to this object
41 
42 }
43 
46 {
47  this->m_nRates = rates.m_nRates;
48  memcpy (this->m_rates, rates.m_rates, MAX_SUPPORTED_RATES);
49  // reset the back pointer to this object
50  this->extended.SetSupportedRates (this);
51  return (*this);
52 }
53 
54 void
56 {
58  if (IsSupportedRate (bs))
59  {
60  return;
61  }
62  m_rates[m_nRates] = bs / 500000;
63  m_nRates++;
64  NS_LOG_DEBUG ("add rate=" << bs << ", n rates=" << (uint32_t)m_nRates);
65 }
66 void
68 {
69  uint8_t rate = bs / 500000;
70  for (uint8_t i = 0; i < m_nRates; i++)
71  {
72  if ((rate | 0x80) == m_rates[i])
73  {
74  return;
75  }
76  if (rate == m_rates[i])
77  {
78  NS_LOG_DEBUG ("set basic rate=" << bs << ", n rates=" << (uint32_t)m_nRates);
79  m_rates[i] |= 0x80;
80  return;
81  }
82  }
83  AddSupportedRate (bs);
84  SetBasicRate (bs);
85 }
86 bool
87 SupportedRates::IsBasicRate (uint32_t bs) const
88 {
89  uint8_t rate = (bs / 500000) | 0x80;
90  for (uint8_t i = 0; i < m_nRates; i++)
91  {
92  if (rate == m_rates[i])
93  {
94  return true;
95  }
96  }
97  return false;
98 }
99 bool
101 {
102  uint8_t rate = bs / 500000;
103  for (uint8_t i = 0; i < m_nRates; i++)
104  {
105  if (rate == m_rates[i]
106  || (rate | 0x80) == m_rates[i])
107  {
108  return true;
109  }
110  }
111  return false;
112 }
113 uint8_t
115 {
116  return m_nRates;
117 }
118 uint32_t
119 SupportedRates::GetRate (uint8_t i) const
120 {
121  return (m_rates[i] & 0x7f) * 500000;
122 }
123 
126 {
127  return IE_SUPPORTED_RATES;
128 }
129 uint8_t
131 {
132  // The Supported Rates Information Element contains only the first 8
133  // supported rates - the remainder appear in the Extended Supported
134  // Rates Information Element.
135  return m_nRates > 8 ? 8 : m_nRates;
136 }
137 void
139 {
140  // The Supported Rates Information Element contains only the first 8
141  // supported rates - the remainder appear in the Extended Supported
142  // Rates Information Element.
143  start.Write (m_rates, m_nRates > 8 ? 8 : m_nRates);
144 }
145 uint8_t
147  uint8_t length)
148 {
149  NS_ASSERT (length <= 8);
150  m_nRates = length;
151  start.Read (m_rates, m_nRates);
152  return m_nRates;
153 }
154 
156 {
157 }
158 
160 {
161  m_supportedRates = sr;
162 }
163 
166 {
168 }
169 
170 void
172 {
173  m_supportedRates = sr;
174 }
175 
176 uint8_t
178 {
179  // If there are 8 or fewer rates then we don't need an Extended
180  // Supported Rates IE and so could return zero here, but we're
181  // overriding the GetSerializedSize() method, so if this function is
182  // invoked in that case then it indicates a programming error. Hence
183  // we have an assertion on that condition.
185 
186  // The number of rates we have beyond the initial 8 is the size of
187  // the information field.
188  return (m_supportedRates->m_nRates - 8);
189 }
190 
191 void
193 {
194  // If there are 8 or fewer rates then there should be no Extended
195  // Supported Rates Information Element at all so being here would
196  // seemingly indicate a programming error.
197  //
198  // Our overridden version of the Serialize() method should ensure
199  // that this routine is never invoked in that case (by ensuring that
200  // WifiInformationElement::Serialize() is not invoked).
203 }
204 
207 {
208  // If there are 8 or fewer rates then we don't need an Extended
209  // Supported Rates IE, so we don't serialise anything.
210  if (m_supportedRates->m_nRates <= 8)
211  {
212  return start;
213  }
214 
215  // If there are more than 8 rates then we serialise as per normal.
216  return WifiInformationElement::Serialize (start);
217 }
218 
219 uint16_t
221 {
222  // If there are 8 or fewer rates then we don't need an Extended
223  // Supported Rates IE, so it's serialised length will be zero.
224  if (m_supportedRates->m_nRates <= 8)
225  {
226  return 0;
227  }
228 
229  // Otherwise, the size of it will be the number of supported rates
230  // beyond 8, plus 2 for the Element ID and Length.
232 }
233 
234 uint8_t
236  uint8_t length)
237 {
238  NS_ASSERT (length > 0);
241  m_supportedRates->m_nRates += length;
242  return length;
243 }
244 
252 std::ostream &operator << (std::ostream &os, const SupportedRates &rates)
253 {
254  os << "[";
255  for (uint8_t i = 0; i < rates.GetNRates (); i++)
256  {
257  uint32_t rate = rates.GetRate (i);
258  if (rates.IsBasicRate (rate))
259  {
260  os << "*";
261  }
262  os << rate / 1000000 << "mbs";
263  if (i < rates.GetNRates () - 1)
264  {
265  os << " ";
266  }
267  }
268  os << "]";
269  return os;
270 }
271 
272 } // namespace ns3
uint32_t GetRate(uint8_t i) const
Return the rate at the given index.
void AddSupportedRate(uint32_t bs)
Add the given rate to the supported rates.
uint8_t DeserializeInformationField(Buffer::Iterator start, uint8_t length)
Deserialize information (i.e., the body of the IE, not including the Element ID and length octets) ...
def start()
Definition: core.py:1482
uint16_t GetSerializedSize() const
Return the serialized size of this supported rates information element.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint8_t GetNRates(void) const
Return the number of supported rates.
void SerializeInformationField(Buffer::Iterator start) const
Serialize information (i.e., the body of the IE, not including the Element ID and length octets) ...
The Supported Rates Information ElementThis class knows how to serialise and deserialise the Supporte...
uint16_t GetSerializedSize() const
Get the size of the serialized IE including Element ID and length fields.
iterator in a Buffer instance
Definition: buffer.h:98
ExtendedSupportedRatesIE extended
Buffer::Iterator Serialize(Buffer::Iterator start) const
This information element is a bit special in that it is only included if there are more than 8 rates...
WifiInformationElementId ElementId() const
Own unique Element ID.
uint8_t GetInformationFieldSize() const
Length of serialized information (i.e., the length of the body of the IE, not including the Element I...
#define IE_SUPPORTED_RATES
void SetSupportedRates(SupportedRates *rates)
void SetBasicRate(uint32_t bs)
Set the given rate to basic rates.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
bool IsBasicRate(uint32_t bs) const
Check if the given rate is a basic rate.
uint8_t m_nRates
Number of supported rates.
#define IE_EXTENDED_SUPPORTED_RATES
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t m_rates[MAX_SUPPORTED_RATES]
List of supported bitrate (divided by 500000)
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1152
uint8_t DeserializeInformationField(Buffer::Iterator start, uint8_t length)
Deserialize information (i.e., the body of the IE, not including the Element ID and length octets) ...
SupportedRates * m_supportedRates
This member points to the SupportedRates object that contains the actual rate details.
WifiInformationElementId ElementId() const
Own unique Element ID.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:982
bool IsSupportedRate(uint32_t bs) const
Check if the given rate is supported.
uint8_t WifiInformationElementId
This type is used to represent an Information Element ID.
uint8_t GetInformationFieldSize() const
Length of serialized information (i.e., the length of the body of the IE, not including the Element I...
SupportedRates & operator=(const SupportedRates &)
void SerializeInformationField(Buffer::Iterator start) const
Serialize information (i.e., the body of the IE, not including the Element ID and length octets) ...
static const uint8_t MAX_SUPPORTED_RATES
This defines the maximum number of supported rates that a STA is allowed to have. ...
Buffer::Iterator Serialize(Buffer::Iterator i) const
Serialize entire IE including Element ID and length fields.