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 
27 bool operator == (const WifiMode &a, const WifiMode &b)
28 {
29  return a.GetUid () == b.GetUid ();
30 }
31 std::ostream & operator << (std::ostream & os, const WifiMode &mode)
32 {
33  os << mode.GetUniqueName ();
34  return os;
35 }
36 std::istream & operator >> (std::istream &is, WifiMode &mode)
37 {
38  std::string str;
39  is >> str;
40  mode = WifiModeFactory::GetFactory ()->Search (str);
41  return is;
42 }
43 
44 uint32_t
46 {
48  return item->bandwidth;
49 }
50 uint64_t
52 {
54  return item->phyRate;
55 }
56 uint64_t
58 {
60  return item->dataRate;
61 }
62 enum WifiCodeRate
64 {
66  return item->codingRate;
67 }
68 uint8_t
70 {
72  return item->constellationSize;
73 }
74 std::string
76 {
77  // needed for ostream printing of the invalid mode
79  return item->uniqueUid;
80 }
81 bool
83 {
85  return item->isMandatory;
86 }
87 uint32_t
88 WifiMode::GetUid (void) const
89 {
90  return m_uid;
91 }
94 {
96  return item->modClass;
97 }
99  : m_uid (0)
100 {
101 }
102 WifiMode::WifiMode (uint32_t uid)
103  : m_uid (uid)
104 {
105 }
106 WifiMode::WifiMode (std::string name)
107 {
108  *this = WifiModeFactory::GetFactory ()->Search (name);
109 }
110 
112 
114 {
115 }
116 
117 
118 WifiMode
119 WifiModeFactory::CreateWifiMode (std::string uniqueName,
120  enum WifiModulationClass modClass,
121  bool isMandatory,
122  uint32_t bandwidth,
123  uint32_t dataRate,
124  enum WifiCodeRate codingRate,
125  uint8_t constellationSize)
126 {
127  WifiModeFactory *factory = GetFactory ();
128  uint32_t uid = factory->AllocateUid (uniqueName);
129  WifiModeItem *item = factory->Get (uid);
130  item->uniqueUid = uniqueName;
131  item->modClass = modClass;
132  // The modulation class for this WifiMode must be valid.
133  NS_ASSERT (modClass != WIFI_MOD_CLASS_UNKNOWN);
134 
135  item->bandwidth = bandwidth;
136  item->dataRate = dataRate;
137 
138  item->codingRate = codingRate;
139 
140  switch (codingRate)
141  {
142  case WIFI_CODE_RATE_5_6:
143  item->phyRate = dataRate * 6 / 5;
144  break;
145  case WIFI_CODE_RATE_3_4:
146  item->phyRate = dataRate * 4 / 3;
147  break;
148  case WIFI_CODE_RATE_2_3:
149  item->phyRate = dataRate * 3 / 2;
150  break;
151  case WIFI_CODE_RATE_1_2:
152  item->phyRate = dataRate * 2 / 1;
153  break;
155  default:
156  item->phyRate = dataRate;
157  break;
158  }
159 
160  // Check for compatibility between modulation class and coding
161  // rate. If modulation class is DSSS then coding rate must be
162  // undefined, and vice versa. I could have done this with an
163  // assertion, but it seems better to always give the error (i.e.,
164  // not only in non-optimised builds) and the cycles that extra test
165  // here costs are only suffered at simulation setup.
166  if ((codingRate == WIFI_CODE_RATE_UNDEFINED) != (modClass == WIFI_MOD_CLASS_DSSS))
167  {
168  NS_FATAL_ERROR ("Error in creation of WifiMode named " << uniqueName << std::endl
169  << "Code rate must be WIFI_CODE_RATE_UNDEFINED iff Modulation Class is WIFI_MOD_CLASS_DSSS");
170  }
171 
172  item->constellationSize = constellationSize;
173  item->isMandatory = isMandatory;
174 
175  return WifiMode (uid);
176 }
177 
178 WifiMode
179 WifiModeFactory::Search (std::string name)
180 {
181  WifiModeItemList::const_iterator i;
182  uint32_t j = 0;
183  for (i = m_itemList.begin (); i != m_itemList.end (); i++)
184  {
185  if (i->uniqueUid == name)
186  {
187  return WifiMode (j);
188  }
189  j++;
190  }
191 
192  // If we get here then a matching WifiMode was not found above. This
193  // is a fatal problem, but we try to be helpful by displaying the
194  // list of WifiModes that are supported.
195  NS_LOG_UNCOND ("Could not find match for WifiMode named \""
196  << name << "\". Valid options are:");
197  for (i = m_itemList.begin (); i != m_itemList.end (); i++)
198  {
199  NS_LOG_UNCOND (" " << i->uniqueUid);
200  }
201  // Empty fatal error to die. We've already unconditionally logged
202  // the helpful information.
203  NS_FATAL_ERROR ("");
204 
205  // This next line is unreachable because of the fatal error
206  // immediately above, and that is fortunate, because we have no idea
207  // what is in WifiMode (0), but we do know it is not what our caller
208  // has requested by name. It's here only because it's the safest
209  // thing that'll give valid code.
210  return WifiMode (0);
211 }
212 
213 uint32_t
214 WifiModeFactory::AllocateUid (std::string uniqueUid)
215 {
216  uint32_t j = 0;
217  for (WifiModeItemList::const_iterator i = m_itemList.begin ();
218  i != m_itemList.end (); i++)
219  {
220  if (i->uniqueUid == uniqueUid)
221  {
222  return j;
223  }
224  j++;
225  }
226  uint32_t uid = m_itemList.size ();
227  m_itemList.push_back (WifiModeItem ());
228  return uid;
229 }
230 
232 WifiModeFactory::Get (uint32_t uid)
233 {
234  NS_ASSERT (uid < m_itemList.size ());
235  return &m_itemList[uid];
236 }
237 
240 {
241  static bool isFirstTime = true;
242  static WifiModeFactory factory;
243  if (isFirstTime)
244  {
245  uint32_t uid = factory.AllocateUid ("Invalid-WifiMode");
246  WifiModeItem *item = factory.Get (uid);
247  item->uniqueUid = "Invalid-WifiMode";
248  item->bandwidth = 0;
249  item->dataRate = 0;
250  item->phyRate = 0;
252  item->constellationSize = 0;
254  item->isMandatory = false;
255  isFirstTime = false;
256  }
257  return &factory;
258 }
259 
260 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:49
WifiCodeRate
Definition: wifi-mode.h:67
enum WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:93
#define NS_ASSERT(condition)
Definition: assert.h:64
uint32_t AllocateUid(std::string uniqueName)
Definition: wifi-mode.cc:214
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:82
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
WifiModeItem * Get(uint32_t uid)
Definition: wifi-mode.cc:232
uint32_t m_uid
Definition: wifi-mode.h:157
enum WifiCodeRate GetCodeRate(void) const
Definition: wifi-mode.cc:63
std::string GetUniqueName(void) const
Definition: wifi-mode.cc:75
WifiModeItemList m_itemList
Definition: wifi-mode.h:245
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:119
uint32_t GetBandwidth(void) const
Definition: wifi-mode.cc:45
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
uint8_t GetConstellationSize(void) const
Definition: wifi-mode.cc:69
uint64_t GetPhyRate(void) const
Definition: wifi-mode.cc:51
#define NS_LOG_UNCOND(msg)
Definition: log.h:343
friend class WifiMode
Definition: wifi-mode.h:218
create WifiMode class instances and keep track of them.
Definition: wifi-mode.h:189
WifiModulationClass
Definition: wifi-mode.h:36
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
static WifiModeFactory * GetFactory()
Definition: wifi-mode.cc:239
enum WifiModulationClass modClass
Definition: wifi-mode.h:234
ATTRIBUTE_HELPER_CPP(ObjectFactory)
uint32_t GetUid(void) const
Definition: wifi-mode.cc:88
WifiMode Search(std::string name)
Definition: wifi-mode.cc:179
uint64_t GetDataRate(void) const
Definition: wifi-mode.cc:57