A Discrete-Event Network Simulator
API
mac8-address.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Leonard Tracy <lentracy@gmail.com>
18  */
19 
20 #include "mac8-address.h"
21 
22 #include "ns3/address.h"
23 #include "ns3/log.h"
24 #include "ns3/simulator.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("Mac8Address");
30 
32 
34  : m_address(addr)
35 {
36 }
37 
39 {
40 }
41 
42 uint8_t
44 {
45  static uint8_t type = Address::Register();
46  return type;
47 }
48 
49 Address
51 {
52  return Address(GetType(), &m_address, 1);
53 }
54 
57 {
59  Mac8Address uAddr;
60  address.CopyTo(&uAddr.m_address);
61  return uAddr;
62 }
63 
64 bool
66 {
67  return address.CheckCompatible(GetType(), 1);
68 }
69 
70 Mac8Address::operator Address() const
71 {
72  return ConvertTo();
73 }
74 
75 void
76 Mac8Address::CopyFrom(const uint8_t* pBuffer)
77 {
78  m_address = *pBuffer;
79 }
80 
81 void
82 Mac8Address::CopyTo(uint8_t* pBuffer) const
83 {
84  *pBuffer = m_address;
85 }
86 
89 {
90  return Mac8Address(255);
91 }
92 
95 {
97 
98  if (m_allocationIndex == 0)
99  {
101  }
102 
103  uint8_t address = m_allocationIndex++;
104  if (m_allocationIndex == 255)
105  {
106  m_allocationIndex = 0;
107  }
108 
109  return Mac8Address(address);
110 }
111 
112 void
114 {
116 
117  m_allocationIndex = 0;
118 }
119 
120 bool
121 operator<(const Mac8Address& a, const Mac8Address& b)
122 {
123  return a.m_address < b.m_address;
124 }
125 
126 bool
127 operator==(const Mac8Address& a, const Mac8Address& b)
128 {
129  return a.m_address == b.m_address;
130 }
131 
132 bool
133 operator!=(const Mac8Address& a, const Mac8Address& b)
134 {
135  return !(a == b);
136 }
137 
138 std::ostream&
139 operator<<(std::ostream& os, const Mac8Address& address)
140 {
141  os << (int)address.m_address;
142  return os;
143 }
144 
145 std::istream&
146 operator>>(std::istream& is, Mac8Address& address)
147 {
148  uint8_t x;
149  is >> x;
150  NS_ASSERT(0 <= x);
151  NS_ASSERT(x <= 255);
152  address.m_address = x;
153  return is;
154 }
155 
156 } // namespace ns3
a polymophic address class
Definition: address.h:101
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
A class used for addressing MAC8 MAC's.
Definition: mac8-address.h:44
static Mac8Address GetBroadcast()
Get the broadcast address (255).
Definition: mac8-address.cc:88
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:56
static void ResetAllocationIndex()
Reset the Mac8Address allocation index.
Address ConvertTo() const
Convert to a generic Address.
Definition: mac8-address.cc:50
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:65
static uint8_t GetType()
Get the Mac8Address type.
Definition: mac8-address.cc:43
uint8_t m_address
The address.
Definition: mac8-address.h:134
Mac8Address()=default
static uint8_t m_allocationIndex
Address allocation index.
Definition: mac8-address.h:133
void CopyTo(uint8_t *pBuffer) const
Writes address to buffer parameter.
Definition: mac8-address.cc:82
static Mac8Address Allocate()
Allocates Mac8Address from 0-254.
Definition: mac8-address.cc:94
void CopyFrom(const uint8_t *pBuffer)
Sets address to address stored in parameter.
Definition: mac8-address.cc:76
virtual ~Mac8Address()
Destructor.
Definition: mac8-address.cc:38
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition: simulator.h:622
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:678
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159