A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
wifi-mode.cc
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 {
47  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
48  return item->bandwidth;
49 }
50 uint64_t
52 {
53  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
54  return item->phyRate;
55 }
56 uint64_t
58 {
59  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
60  return item->dataRate;
61 }
62 enum WifiCodeRate
64 {
65  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
66  return item->codingRate;
67 }
68 uint8_t
70 {
71  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
72  return item->constellationSize;
73 }
74 std::string
76 {
77  // needed for ostream printing of the invalid mode
78  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
79  return item->uniqueUid;
80 }
81 bool
83 {
84  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
85  return item->isMandatory;
86 }
87 uint32_t
88 WifiMode::GetUid (void) const
89 {
90  return m_uid;
91 }
94 {
95  struct WifiModeFactory::WifiModeItem *item = WifiModeFactory::GetFactory ()->Get (m_uid);
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 
111 ATTRIBUTE_HELPER_CPP (WifiMode);
112 
113 WifiModeFactory::WifiModeFactory ()
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_3_4:
143  item->phyRate = dataRate * 4 / 3;
144  break;
145  case WIFI_CODE_RATE_2_3:
146  item->phyRate = dataRate * 3 / 2;
147  break;
148  case WIFI_CODE_RATE_1_2:
149  item->phyRate = dataRate * 2 / 1;
150  break;
152  default:
153  item->phyRate = dataRate;
154  break;
155  }
156 
157  // Check for compatibility between modulation class and coding
158  // rate. If modulation class is DSSS then coding rate must be
159  // undefined, and vice versa. I could have done this with an
160  // assertion, but it seems better to always give the error (i.e.,
161  // not only in non-optimised builds) and the cycles that extra test
162  // here costs are only suffered at simulation setup.
163  if ((codingRate == WIFI_CODE_RATE_UNDEFINED) != (modClass == WIFI_MOD_CLASS_DSSS))
164  {
165  NS_FATAL_ERROR ("Error in creation of WifiMode named " << uniqueName << std::endl
166  << "Code rate must be WIFI_CODE_RATE_UNDEFINED iff Modulation Class is WIFI_MOD_CLASS_DSSS");
167  }
168 
169  item->constellationSize = constellationSize;
170  item->isMandatory = isMandatory;
171 
172  return WifiMode (uid);
173 }
174 
175 WifiMode
176 WifiModeFactory::Search (std::string name)
177 {
178  WifiModeItemList::const_iterator i;
179  uint32_t j = 0;
180  for (i = m_itemList.begin (); i != m_itemList.end (); i++)
181  {
182  if (i->uniqueUid == name)
183  {
184  return WifiMode (j);
185  }
186  j++;
187  }
188 
189  // If we get here then a matching WifiMode was not found above. This
190  // is a fatal problem, but we try to be helpful by displaying the
191  // list of WifiModes that are supported.
192  NS_LOG_UNCOND ("Could not find match for WifiMode named \""
193  << name << "\". Valid options are:");
194  for (i = m_itemList.begin (); i != m_itemList.end (); i++)
195  {
196  NS_LOG_UNCOND (" " << i->uniqueUid);
197  }
198  // Empty fatal error to die. We've already unconditionally logged
199  // the helpful information.
200  NS_FATAL_ERROR ("");
201 
202  // This next line is unreachable because of the fatal error
203  // immediately above, and that is fortunate, because we have no idea
204  // what is in WifiMode (0), but we do know it is not what our caller
205  // has requested by name. It's here only because it's the safest
206  // thing that'll give valid code.
207  return WifiMode (0);
208 }
209 
210 uint32_t
211 WifiModeFactory::AllocateUid (std::string uniqueUid)
212 {
213  uint32_t j = 0;
214  for (WifiModeItemList::const_iterator i = m_itemList.begin ();
215  i != m_itemList.end (); i++)
216  {
217  if (i->uniqueUid == uniqueUid)
218  {
219  return j;
220  }
221  j++;
222  }
223  uint32_t uid = m_itemList.size ();
224  m_itemList.push_back (WifiModeItem ());
225  return uid;
226 }
227 
228 struct WifiModeFactory::WifiModeItem *
229 WifiModeFactory::Get (uint32_t uid)
230 {
231  NS_ASSERT (uid < m_itemList.size ());
232  return &m_itemList[uid];
233 }
234 
235 WifiModeFactory *
236 WifiModeFactory::GetFactory (void)
237 {
238  static bool isFirstTime = true;
239  static WifiModeFactory factory;
240  if (isFirstTime)
241  {
242  uint32_t uid = factory.AllocateUid ("Invalid-WifiMode");
243  WifiModeItem *item = factory.Get (uid);
244  item->uniqueUid = "Invalid-WifiMode";
245  item->bandwidth = 0;
246  item->dataRate = 0;
247  item->phyRate = 0;
248  item->modClass = WIFI_MOD_CLASS_UNKNOWN;
249  item->constellationSize = 0;
250  item->codingRate = WIFI_CODE_RATE_UNDEFINED;
251  item->isMandatory = false;
252  isFirstTime = false;
253  }
254  return &factory;
255 }
256 
257 } // 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
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:88
bool IsMandatory(void) const
Definition: wifi-mode.cc:82
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
enum WifiCodeRate GetCodeRate(void) const
Definition: wifi-mode.cc:63
std::string GetUniqueName(void) const
Definition: wifi-mode.cc:75
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 ATTRIBUTE_HELPER_CPP(type)
#define NS_LOG_UNCOND(msg)
Definition: log.h:343
create WifiMode class instances and keep track of them.
Definition: wifi-mode.h:183
WifiModulationClass
Definition: wifi-mode.h:36
uint32_t GetUid(void) const
Definition: wifi-mode.cc:88
uint64_t GetDataRate(void) const
Definition: wifi-mode.cc:57