A Discrete-Event Network Simulator
API
mac16-address.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
4  * Copyright (c) 2011 The Boeing Company
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "mac16-address.h"
22 #include "ns3/address.h"
23 #include "ns3/assert.h"
24 #include "ns3/log.h"
25 #include <iomanip>
26 #include <iostream>
27 #include <cstring>
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("Mac16Address");
32 
34 
35 #define ASCII_a (0x41)
36 #define ASCII_z (0x5a)
37 #define ASCII_A (0x61)
38 #define ASCII_Z (0x7a)
39 #define ASCII_COLON (0x3a)
40 #define ASCII_ZERO (0x30)
41 
47 static char
49 {
50  if (c >= ASCII_a && c <= ASCII_z) {
51  return c;
52  } else if (c >= ASCII_A && c <= ASCII_Z) {
53  return c + (ASCII_a - ASCII_A);
54  } else {
55  return c;
56  }
57 }
58 
59 
61 {
62  NS_LOG_FUNCTION (this);
63  memset (m_address, 0, 2);
64 }
65 
66 Mac16Address::Mac16Address (const char *str)
67 {
68  NS_LOG_FUNCTION (this << str);
69  int i = 0;
70  while (*str != 0 && i < 2)
71  {
72  uint8_t byte = 0;
73  while (*str != ASCII_COLON && *str != 0)
74  {
75  byte <<= 4;
76  char low = AsciiToLowCase (*str);
77  if (low >= ASCII_a)
78  {
79  byte |= low - ASCII_a + 10;
80  }
81  else
82  {
83  byte |= low - ASCII_ZERO;
84  }
85  str++;
86  }
87  m_address[i] = byte;
88  i++;
89  if (*str == 0)
90  {
91  break;
92  }
93  str++;
94  }
95  NS_ASSERT (i == 2);
96 }
97 
98 void
99 Mac16Address::CopyFrom (const uint8_t buffer[2])
100 {
101  NS_LOG_FUNCTION (this << &buffer);
102  memcpy (m_address, buffer, 2);
103 }
104 void
105 Mac16Address::CopyTo (uint8_t buffer[2]) const
106 {
107  NS_LOG_FUNCTION (this << &buffer);
108  memcpy (buffer, m_address, 2);
109 }
110 
111 bool
113 {
115  return address.CheckCompatible (GetType (), 2);
116 }
117 
118 Mac16Address::operator Address () const
119 {
120  return ConvertTo ();
121 }
122 
125 {
127  NS_ASSERT (address.CheckCompatible (GetType (), 2));
128  Mac16Address retval;
129  address.CopyTo (retval.m_address);
130  return retval;
131 }
132 Address
134 {
135  NS_LOG_FUNCTION (this);
136  return Address (GetType (), m_address, 2);
137 }
138 
141 {
143  static uint64_t id = 0;
144  id++;
146  address.m_address[0] = (id >> 8) & 0xff;
147  address.m_address[1] = (id >> 0) & 0xff;
148  return address;
149 }
150 
151 uint8_t
153 {
155  static uint8_t type = Address::Register ();
156  return type;
157 }
158 
159 std::ostream & operator<< (std::ostream& os, const Mac16Address & address)
160 {
161  uint8_t ad[2];
162  address.CopyTo (ad);
163 
164  os.setf (std::ios::hex, std::ios::basefield);
165  os.fill ('0');
166  for (uint8_t i = 0; i < 1; i++)
167  {
168  os << std::setw (2) << (uint32_t)ad[i] << ":";
169  }
170  // Final byte not suffixed by ":"
171  os << std::setw (2) << (uint32_t)ad[1];
172  os.setf (std::ios::dec, std::ios::basefield);
173  os.fill (' ');
174  return os;
175 }
176 
177 std::istream& operator>> (std::istream& is, Mac16Address & address)
178 {
179  std::string v;
180  is >> v;
181 
182  std::string::size_type col = 0;
183  for (uint8_t i = 0; i < 2; ++i)
184  {
185  std::string tmp;
186  std::string::size_type next;
187  next = v.find (":", col);
188  if (next == std::string::npos)
189  {
190  tmp = v.substr (col, v.size ()-col);
191  address.m_address[i] = strtoul (tmp.c_str(), 0, 16);
192  break;
193  }
194  else
195  {
196  tmp = v.substr (col, next-col);
197  address.m_address[i] = strtoul (tmp.c_str(), 0, 16);
198  col = next + 1;
199  }
200  }
201  return is;
202 }
203 
204 }
#define ASCII_z
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint8_t m_address[2]
address value
static Mac16Address ConvertFrom(const Address &address)
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
#define ASCII_Z
Address ConvertTo(void) const
void CopyFrom(const uint8_t buffer[2])
#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:202
void CopyTo(uint8_t buffer[2]) const
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define ASCII_COLON
a polymophic address class
Definition: address.h:90
static char AsciiToLowCase(char c)
Converts a char to lower case.
static uint8_t GetType(void)
Return the Type of address.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
#define ASCII_A
#define ASCII_ZERO
static bool IsMatchingType(const Address &address)
static Mac16Address Allocate(void)
Allocate a new Mac16Address.
#define ASCII_a
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138