A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
wifi-mode.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 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 #include "wifi-mode.h"
21 #include "ns3/simulator.h"
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 
25 namespace ns3 {
26 
35 bool operator == (const WifiMode &a, const WifiMode &b)
36 {
37  return a.GetUid () == b.GetUid ();
38 }
46 std::ostream & operator << (std::ostream & os, const WifiMode &mode)
47 {
48  os << mode.GetUniqueName ();
49  return os;
50 }
58 std::istream & operator >> (std::istream &is, WifiMode &mode)
59 {
60  std::string str;
61  is >> str;
62  mode = WifiModeFactory::GetFactory ()->Search (str);
63  return is;
64 }
65 
66 uint32_t
68 {
70  return item->bandwidth;
71 }
72 uint64_t
74 {
76  return item->phyRate;
77 }
78 uint64_t
80 {
82  return item->dataRate;
83 }
84 enum WifiCodeRate
86 {
88  return item->codingRate;
89 }
90 uint8_t
92 {
94  return item->constellationSize;
95 }
96 std::string
98 {
99  // needed for ostream printing of the invalid mode
101  return item->uniqueUid;
102 }
103 bool
105 {
107  return item->isMandatory;
108 }
109 uint32_t
110 WifiMode::GetUid (void) const
111 {
112  return m_uid;
113 }
116 {
118  return item->modClass;
119 }
121  : m_uid (0)
122 {
123 }
124 WifiMode::WifiMode (uint32_t uid)
125  : m_uid (uid)
126 {
127 }
128 WifiMode::WifiMode (std::string name)
129 {
130  *this = WifiModeFactory::GetFactory ()->Search (name);
131 }
132 
134 
136 {
137 }
138 
139 
140 WifiMode
141 WifiModeFactory::CreateWifiMode (std::string uniqueName,
142  enum WifiModulationClass modClass,
143  bool isMandatory,
144  uint32_t bandwidth,
145  uint32_t dataRate,
146  enum WifiCodeRate codingRate,
147  uint8_t constellationSize)
148 {
149  WifiModeFactory *factory = GetFactory ();
150  uint32_t uid = factory->AllocateUid (uniqueName);
151  WifiModeItem *item = factory->Get (uid);
152  item->uniqueUid = uniqueName;
153  item->modClass = modClass;
154  // The modulation class for this WifiMode must be valid.
155  NS_ASSERT (modClass != WIFI_MOD_CLASS_UNKNOWN);
156 
157  item->bandwidth = bandwidth;
158  item->dataRate = dataRate;
159 
160  item->codingRate = codingRate;
161 
162  switch (codingRate)
163  {
164  case WIFI_CODE_RATE_5_6:
165  item->phyRate = dataRate * 6 / 5;
166  break;
167  case WIFI_CODE_RATE_3_4:
168  item->phyRate = dataRate * 4 / 3;
169  break;
170  case WIFI_CODE_RATE_2_3:
171  item->phyRate = dataRate * 3 / 2;
172  break;
173  case WIFI_CODE_RATE_1_2:
174  item->phyRate = dataRate * 2 / 1;
175  break;
177  default:
178  item->phyRate = dataRate;
179  break;
180  }
181 
182  // Check for compatibility between modulation class and coding
183  // rate. If modulation class is DSSS then coding rate must be
184  // undefined, and vice versa. I could have done this with an
185  // assertion, but it seems better to always give the error (i.e.,
186  // not only in non-optimised builds) and the cycles that extra test
187  // here costs are only suffered at simulation setup.
188  if ((codingRate == WIFI_CODE_RATE_UNDEFINED) != (modClass == WIFI_MOD_CLASS_DSSS))
189  {
190  NS_FATAL_ERROR ("Error in creation of WifiMode named " << uniqueName << std::endl
191  << "Code rate must be WIFI_CODE_RATE_UNDEFINED iff Modulation Class is WIFI_MOD_CLASS_DSSS");
192  }
193 
194  item->constellationSize = constellationSize;
195  item->isMandatory = isMandatory;
196 
197  return WifiMode (uid);
198 }
199 
200 WifiMode
201 WifiModeFactory::Search (std::string name)
202 {
203  WifiModeItemList::const_iterator i;
204  uint32_t j = 0;
205  for (i = m_itemList.begin (); i != m_itemList.end (); i++)
206  {
207  if (i->uniqueUid == name)
208  {
209  return WifiMode (j);
210  }
211  j++;
212  }
213 
214  // If we get here then a matching WifiMode was not found above. This
215  // is a fatal problem, but we try to be helpful by displaying the
216  // list of WifiModes that are supported.
217  NS_LOG_UNCOND ("Could not find match for WifiMode named \""
218  << name << "\". Valid options are:");
219  for (i = m_itemList.begin (); i != m_itemList.end (); i++)
220  {
221  NS_LOG_UNCOND (" " << i->uniqueUid);
222  }
223  // Empty fatal error to die. We've already unconditionally logged
224  // the helpful information.
225  NS_FATAL_ERROR ("");
226 
227  // This next line is unreachable because of the fatal error
228  // immediately above, and that is fortunate, because we have no idea
229  // what is in WifiMode (0), but we do know it is not what our caller
230  // has requested by name. It's here only because it's the safest
231  // thing that'll give valid code.
232  return WifiMode (0);
233 }
234 
235 uint32_t
236 WifiModeFactory::AllocateUid (std::string uniqueUid)
237 {
238  uint32_t j = 0;
239  for (WifiModeItemList::const_iterator i = m_itemList.begin ();
240  i != m_itemList.end (); i++)
241  {
242  if (i->uniqueUid == uniqueUid)
243  {
244  return j;
245  }
246  j++;
247  }
248  uint32_t uid = m_itemList.size ();
249  m_itemList.push_back (WifiModeItem ());
250  return uid;
251 }
252 
254 WifiModeFactory::Get (uint32_t uid)
255 {
256  NS_ASSERT (uid < m_itemList.size ());
257  return &m_itemList[uid];
258 }
259 
262 {
263  static bool isFirstTime = true;
264  static WifiModeFactory factory;
265  if (isFirstTime)
266  {
267  uint32_t uid = factory.AllocateUid ("Invalid-WifiMode");
268  WifiModeItem *item = factory.Get (uid);
269  item->uniqueUid = "Invalid-WifiMode";
270  item->bandwidth = 0;
271  item->dataRate = 0;
272  item->phyRate = 0;
274  item->constellationSize = 0;
276  item->isMandatory = false;
277  isFirstTime = false;
278  }
279  return &factory;
280 }
281 
282 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:49
WifiMode()
Create an invalid WifiMode.
Definition: wifi-mode.cc:120
WifiCodeRate
This enumeration defines the various convolutional coding rates used for the OFDM transmission modes ...
Definition: wifi-mode.h:67
enum WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:115
#define NS_ASSERT(condition)
Definition: assert.h:64
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:91
bool IsMandatory(void) const
Definition: wifi-mode.cc:104
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
Modulation class unknown or unspecified.
Definition: wifi-mode.h:40
WifiModeItem * Get(uint32_t uid)
Return a WifiModeItem at the given uid index.
Definition: wifi-mode.cc:254
uint32_t m_uid
Definition: wifi-mode.h:168
enum WifiCodeRate GetCodeRate(void) const
Definition: wifi-mode.cc:85
std::string GetUniqueName(void) const
Definition: wifi-mode.cc:97
WifiModeItemList m_itemList
Definition: wifi-mode.h:293
static WifiMode CreateWifiMode(std::string uniqueName, enum WifiModulationClass modClass, bool isMandatory, uint32_t bandwidth, uint32_t dataRate, enum WifiCodeRate codingRate, uint8_t constellationSize)
Definition: wifi-mode.cc:141
uint32_t GetBandwidth(void) const
Definition: wifi-mode.cc:67
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
uint8_t GetConstellationSize(void) const
Definition: wifi-mode.cc:91
uint64_t GetPhyRate(void) const
Definition: wifi-mode.cc:73
#define NS_LOG_UNCOND(msg)
Definition: log.h:377
friend class WifiMode
Definition: wifi-mode.h:239
No explicit coding (e.g., DSSS rates)
Definition: wifi-mode.h:70
create WifiMode class instances and keep track of them.
Definition: wifi-mode.h:209
WifiModulationClass
This enumeration defines the modulation classes per IEEE 802.11-2007, Section 9.6.1, Table 9-2.
Definition: wifi-mode.h:36
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
static WifiModeFactory * GetFactory()
Return a WifiModeFactory.
Definition: wifi-mode.cc:261
uint32_t AllocateUid(std::string uniqueUid)
Allocate a WifiModeItem from a given uniqueUid.
Definition: wifi-mode.cc:236
enum WifiModulationClass modClass
Definition: wifi-mode.h:261
ATTRIBUTE_HELPER_CPP(ObjectFactory)
uint32_t GetUid(void) const
Definition: wifi-mode.cc:110
WifiMode Search(std::string name)
Search and return WifiMode from a given name.
Definition: wifi-mode.cc:201
uint64_t GetDataRate(void) const
Definition: wifi-mode.cc:79
This is the data associated to a unique WifiMode.
Definition: wifi-mode.h:255
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
Definition: wifi-mode.h:46