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 
45 {
46  this->m_nRates = rates.m_nRates;
47  memcpy (this->m_rates, rates.m_rates, MAX_SUPPORTED_RATES);
48  //reset the back pointer to this object
49  this->extended.SetSupportedRates (this);
50  return (*this);
51 }
52 
53 void
55 {
57  if (IsSupportedRate (bs))
58  {
59  return;
60  }
61  m_rates[m_nRates] = bs / 500000;
62  m_nRates++;
63  NS_LOG_DEBUG ("add rate=" << bs << ", n rates=" << (uint32_t)m_nRates);
64 }
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 
87 bool
88 SupportedRates::IsBasicRate (uint32_t bs) const
89 {
90  uint8_t rate = (bs / 500000) | 0x80;
91  for (uint8_t i = 0; i < m_nRates; i++)
92  {
93  if (rate == m_rates[i])
94  {
95  return true;
96  }
97  }
98  return false;
99 }
100 
101 bool
103 {
104  uint8_t rate = bs / 500000;
105  for (uint8_t i = 0; i < m_nRates; i++)
106  {
107  if (rate == m_rates[i]
108  || (rate | 0x80) == m_rates[i])
109  {
110  return true;
111  }
112  }
113  return false;
114 }
115 
116 uint8_t
118 {
119  return m_nRates;
120 }
121 
122 uint32_t
123 SupportedRates::GetRate (uint8_t i) const
124 {
125  return (m_rates[i] & 0x7f) * 500000;
126 }
127 
130 {
131  return IE_SUPPORTED_RATES;
132 }
133 
134 uint8_t
136 {
137  //The Supported Rates Information Element contains only the first 8
138  //supported rates - the remainder appear in the Extended Supported
139  //Rates Information Element.
140  return m_nRates > 8 ? 8 : m_nRates;
141 }
142 
143 void
145 {
146  //The Supported Rates Information Element contains only the first 8
147  //supported rates - the remainder appear in the Extended Supported
148  //Rates Information Element.
149  start.Write (m_rates, m_nRates > 8 ? 8 : m_nRates);
150 }
151 
152 uint8_t
154  uint8_t length)
155 {
156  NS_ASSERT (length <= 8);
157  m_nRates = length;
158  start.Read (m_rates, m_nRates);
159  return m_nRates;
160 }
161 
163 {
164 }
165 
167 {
168  m_supportedRates = sr;
169 }
170 
173 {
175 }
176 
177 void
179 {
180  m_supportedRates = sr;
181 }
182 
183 uint8_t
185 {
186  //If there are 8 or fewer rates then we don't need an Extended
187  //Supported Rates IE and so could return zero here, but we're
188  //overriding the GetSerializedSize() method, so if this function is
189  //invoked in that case then it indicates a programming error. Hence
190  //we have an assertion on that condition.
192 
193  //The number of rates we have beyond the initial 8 is the size of
194  //the information field.
195  return (m_supportedRates->m_nRates - 8);
196 }
197 
198 void
200 {
201  //If there are 8 or fewer rates then there should be no Extended
202  //Supported Rates Information Element at all so being here would
203  //seemingly indicate a programming error.
204  //
205  //Our overridden version of the Serialize() method should ensure
206  //that this routine is never invoked in that case (by ensuring that
207  //WifiInformationElement::Serialize() is not invoked).
210 }
211 
214 {
215  //If there are 8 or fewer rates then we don't need an Extended
216  //Supported Rates IE, so we don't serialise anything.
217  if (m_supportedRates->m_nRates <= 8)
218  {
219  return start;
220  }
221 
222  //If there are more than 8 rates then we serialise as per normal.
223  return WifiInformationElement::Serialize (start);
224 }
225 
226 uint16_t
228 {
229  //If there are 8 or fewer rates then we don't need an Extended
230  //Supported Rates IE, so it's serialised length will be zero.
231  if (m_supportedRates->m_nRates <= 8)
232  {
233  return 0;
234  }
235 
236  //Otherwise, the size of it will be the number of supported rates
237  //beyond 8, plus 2 for the Element ID and Length.
239 }
240 
241 uint8_t
243  uint8_t length)
244 {
245  NS_ASSERT (length > 0);
248  m_supportedRates->m_nRates += length;
249  return length;
250 }
251 
260 std::ostream &operator << (std::ostream &os, const SupportedRates &rates)
261 {
262  os << "[";
263  for (uint8_t i = 0; i < rates.GetNRates (); i++)
264  {
265  uint32_t rate = rates.GetRate (i);
266  if (rates.IsBasicRate (rate))
267  {
268  os << "*";
269  }
270  os << rate / 1000000 << "mbs";
271  if (i < rates.GetNRates () - 1)
272  {
273  os << " ";
274  }
275  }
276  os << "]";
277  return os;
278 }
279 
280 } //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:67
#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:1123
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:953
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.