A Discrete-Event Network Simulator
API
vendor-specific-action.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Dalian University of Technology
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Junling Bu <linlinjavaer@gmail.com>
17  */
18 #include "vendor-specific-action.h"
19 
20 #include "ns3/assert.h"
21 #include "ns3/log.h"
22 
23 #include <cstring>
24 #include <iomanip>
25 #include <iostream>
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("VendorSpecificAction");
31 
32 /*********** OrganizationIdentifier *******/
33 
35 
37  : m_type(Unknown)
38 {
39  NS_LOG_FUNCTION(this);
40  m_type = Unknown;
41  std::memset(m_oi, 0, 5);
42 }
43 
44 OrganizationIdentifier::OrganizationIdentifier(const uint8_t* str, uint32_t length)
45 {
46  NS_LOG_FUNCTION(this << str << length);
47  if (length == 3)
48  {
49  m_type = OUI24;
50  std::memcpy(m_oi, str, length);
51  }
52  else if (length == 5)
53  {
54  m_type = OUI36;
55  std::memcpy(m_oi, str, length);
56  }
57  else
58  {
59  m_type = Unknown;
60  NS_FATAL_ERROR("cannot support organization identifier with length=" << length);
61  }
62 }
63 
66 {
67  this->m_type = oi.m_type;
68  std::memcpy(this->m_oi, oi.m_oi, 5);
69  return (*this);
70 }
71 
73 {
74  NS_LOG_FUNCTION(this);
75 }
76 
77 uint8_t
79 {
80  NS_LOG_FUNCTION(this);
82  return (m_oi[4] & 0x0f);
83 }
84 
85 bool
87 {
88  NS_LOG_FUNCTION(this);
89  return m_type == Unknown;
90 }
91 
92 uint32_t
94 {
95  NS_LOG_FUNCTION(this);
96  switch (m_type)
97  {
98  case OUI24:
99  return 3;
100  case OUI36:
101  return 5;
102  case Unknown:
103  default:
105  return 0;
106  }
107 }
108 
109 void
111 {
112  NS_LOG_FUNCTION(this);
113  m_type = type;
114 }
115 
118 {
119  NS_LOG_FUNCTION(this);
120  return m_type;
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION(this << &start);
127  start.Write(m_oi, GetSerializedSize());
128 }
129 
130 /* because OrganizationIdentifier field is not standard
131  * and the length of OrganizationIdentifier is variable
132  * so data parse here is troublesome
133  */
134 uint32_t
136 {
137  NS_LOG_FUNCTION(this << &start);
138  // first try to parse OUI24 with 3 bytes
139  start.Read(m_oi, 3);
140  for (std::vector<OrganizationIdentifier>::iterator i = OrganizationIdentifiers.begin();
141  i != OrganizationIdentifiers.end();
142  ++i)
143  {
144  if ((i->m_type == OUI24) && (std::memcmp(i->m_oi, m_oi, 3) == 0))
145  {
146  m_type = OUI24;
147  return 3;
148  }
149  }
150 
151  // then try to parse OUI36 with 5 bytes
152  start.Read(m_oi + 3, 2);
153  for (std::vector<OrganizationIdentifier>::iterator i = OrganizationIdentifiers.begin();
154  i != OrganizationIdentifiers.end();
155  ++i)
156  {
157  if ((i->m_type == OUI36) && (std::memcmp(i->m_oi, m_oi, 4) == 0))
158  {
159  // OUI36 first check 4 bytes, then check half of the 5th byte
160  if ((i->m_oi[4] & 0xf0) == (m_oi[4] & 0xf0))
161  {
162  m_type = OUI36;
163  return 5;
164  }
165  }
166  }
167 
168  // if we cannot deserialize the organization identifier field,
169  // we will fail
170  NS_FATAL_ERROR("cannot deserialize the organization identifier field successfully");
171  return 0;
172 }
173 
180 bool
182 {
183  if (a.m_type != b.m_type)
184  {
185  return false;
186  }
187 
189  {
190  return memcmp(a.m_oi, b.m_oi, 3) == 0;
191  }
192 
194  {
195  return (memcmp(a.m_oi, b.m_oi, 4) == 0) && ((a.m_oi[4] & 0xf0) == (b.m_oi[4] & 0xf0));
196  }
197 
198  return false;
199 }
200 
207 bool
209 {
210  return !(a == b);
211 }
212 
219 bool
221 {
222  return memcmp(a.m_oi, b.m_oi, std::min(a.m_type, b.m_type)) < 0;
223 }
224 
231 std::ostream&
232 operator<<(std::ostream& os, const OrganizationIdentifier& oi)
233 {
234  for (int i = 0; i < oi.m_type; i++)
235  {
236  os << "0x" << std::hex << static_cast<int>(oi.m_oi[i]) << " ";
237  }
238  os << std::endl;
239  return os;
240 }
241 
248 std::istream&
249 operator>>(std::istream& is, const OrganizationIdentifier& oi)
250 {
251  return is;
252 }
253 
254 /*********** VendorSpecificActionHeader *******/
256 
258  : m_oi(),
259  m_category(CATEGORY_OF_VSA)
260 {
261  NS_LOG_FUNCTION(this);
262 }
263 
265 {
266  NS_LOG_FUNCTION(this);
267 }
268 
269 void
271 {
272  NS_LOG_FUNCTION(this << oi);
273  m_oi = oi;
274 }
275 
278 {
279  NS_LOG_FUNCTION(this);
280  return m_oi;
281 }
282 
283 TypeId
285 {
286  static TypeId tid = TypeId("ns3::VendorSpecificActionHeader")
287  .SetParent<Header>()
288  .SetGroupName("Wave")
289  .AddConstructor<VendorSpecificActionHeader>();
290 
291  return tid;
292 }
293 
294 uint8_t
296 {
297  NS_LOG_FUNCTION(this);
298  return m_category;
299 }
300 
301 TypeId
303 {
304  NS_LOG_FUNCTION(this);
305  return GetTypeId();
306 }
307 
308 void
309 VendorSpecificActionHeader::Print(std::ostream& os) const
310 {
311  NS_LOG_FUNCTION(this << &os);
312  os << "VendorSpecificActionHeader[ "
313  << "category = 0x" << std::hex << (int)m_category << "organization identifier = " << m_oi
314  << std::dec;
315 }
316 
317 uint32_t
319 {
320  NS_LOG_FUNCTION(this);
321  return sizeof(m_category) + m_oi.GetSerializedSize();
322 }
323 
324 void
326 {
327  NS_LOG_FUNCTION(this << &start);
328  start.WriteU8(m_category);
330 }
331 
332 uint32_t
334 {
335  NS_LOG_FUNCTION(this << &start);
336  m_category = start.ReadU8();
338  {
339  return 0;
340  }
342 
343  return GetSerializedSize();
344 }
345 
346 /********* VendorSpecificContentManager ***********/
348 {
349  NS_LOG_FUNCTION(this);
350 }
351 
353 {
354  NS_LOG_FUNCTION(this);
355 }
356 
357 void
359 {
360  NS_LOG_FUNCTION(this << oi << &cb);
361  if (IsVscCallbackRegistered(oi))
362  {
363  NS_LOG_WARN("there is already a VsaCallback registered for OrganizationIdentifier " << oi);
364  }
365  m_callbacks.insert(std::make_pair(oi, cb));
366 }
367 
368 void
370 {
371  NS_LOG_FUNCTION(this << oi);
372  m_callbacks.erase(oi);
373 }
374 
375 bool
377 {
378  NS_LOG_FUNCTION(this << oi);
379  if (m_callbacks.find(oi) == m_callbacks.end())
380  {
381  OrganizationIdentifiers.push_back(oi);
382  return false;
383  }
384  return true;
385 }
386 
389  Ptr<WifiMac>,
390  const OrganizationIdentifier&,
392  const Address&>();
393 
396 {
397  NS_LOG_FUNCTION(this << oi);
398  VscCallbacksI i;
399  i = m_callbacks.find(oi);
400  return (i == m_callbacks.end()) ? null_callback : i->second;
401 }
402 
403 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
a polymophic address class
Definition: address.h:100
iterator in a Buffer instance
Definition: buffer.h:100
Callback template class.
Definition: callback.h:443
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
the organization identifier is a public organizationally unique identifier assigned by the IEEE.
OrganizationIdentifierType
OrganizationIdentifierType enumeration.
OrganizationIdentifier & operator=(const OrganizationIdentifier &oi)
Assignment operator.
uint32_t GetSerializedSize() const
Get serialized size.
void Serialize(Buffer::Iterator start) const
Serialize to buffer.
uint32_t Deserialize(Buffer::Iterator start)
Deserialize from buffer.
OrganizationIdentifierType GetType() const
uint8_t m_oi[5]
organization identifier
OrganizationIdentifierType m_type
OI type.
void SetType(OrganizationIdentifierType type)
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
See IEEE 802.11-2007 chapter 7.3.1.11 and 7.4.5 also IEEE 802.11p-2010 chapter 7.4....
uint32_t GetSerializedSize() const override
void Serialize(Buffer::Iterator start) const override
void SetOrganizationIdentifier(OrganizationIdentifier oi)
uint8_t GetCategory() const
Get the category field.
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
OrganizationIdentifier GetOrganizationIdentifier() const
static TypeId GetTypeId()
Get the type ID.
void RegisterVscCallback(OrganizationIdentifier oi, VscCallback cb)
bool IsVscCallbackRegistered(OrganizationIdentifier &oi)
VscCallback FindVscCallback(OrganizationIdentifier &oi)
std::map< OrganizationIdentifier, VscCallback >::iterator VscCallbacksI
VSC callback iterator typedef.
void DeregisterVscCallback(OrganizationIdentifier &oi)
VscCallbacks m_callbacks
VSC callbacks.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:750
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition: fatal-error.h:142
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static VscCallback null_callback
VSC callback function.
static std::vector< OrganizationIdentifier > OrganizationIdentifiers
the OIs
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:681
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
ATTRIBUTE_HELPER_CPP(Length)
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
static const uint8_t CATEGORY_OF_VSA
see IEEE 802.11-2007 chapter 7.3.1.11 Table 7-24—Category values