A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
uan-net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
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: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #include "ns3/trace-source-accessor.h"
22 #include "ns3/traced-callback.h"
23 #include "ns3/pointer.h"
24 #include "ns3/node.h"
25 #include "ns3/assert.h"
26 #include "uan-net-device.h"
27 #include "uan-phy.h"
28 #include "uan-mac.h"
29 #include "uan-channel.h"
30 #include "uan-transducer.h"
31 #include "ns3/log.h"
32 
33 NS_LOG_COMPONENT_DEFINE ("UanNetDevice");
34 
35 namespace ns3 {
36 
37 NS_OBJECT_ENSURE_REGISTERED (UanNetDevice)
38  ;
39 
41  : NetDevice (),
42  m_mtu (64000),
43  m_cleared (false)
44 {
45 }
46 
48 {
49 }
50 
51 void
53 {
54  if (m_cleared)
55  {
56  return;
57  }
58  m_cleared = true;
59  m_node = 0;
60  if (m_channel)
61  {
62  m_channel->Clear ();
63  m_channel = 0;
64  }
65  if (m_mac)
66  {
67  m_mac->Clear ();
68  m_mac = 0;
69  }
70  if (m_phy)
71  {
72  m_phy->Clear ();
73  m_phy = 0;
74  }
75  if (m_trans)
76  {
77  m_trans->Clear ();
78  m_trans = 0;
79  }
80 }
81 
82 void
84 {
85  Clear ();
87 }
88 
89 TypeId
91 {
92 
93 
94  static TypeId tid = TypeId ("ns3::UanNetDevice")
95  .SetParent<NetDevice> ()
96  .AddAttribute ("Channel", "The channel attached to this device.",
97  PointerValue (),
99  MakePointerChecker<UanChannel> ())
100  .AddAttribute ("Phy", "The PHY layer attached to this device.",
101  PointerValue (),
102  MakePointerAccessor (&UanNetDevice::GetPhy, &UanNetDevice::SetPhy),
103  MakePointerChecker<UanPhy> ())
104  .AddAttribute ("Mac", "The MAC layer attached to this device.",
105  PointerValue (),
106  MakePointerAccessor (&UanNetDevice::GetMac, &UanNetDevice::SetMac),
107  MakePointerChecker<UanMac> ())
108  .AddAttribute ("Transducer", "The Transducer attached to this device.",
109  PointerValue (),
110  MakePointerAccessor (&UanNetDevice::GetTransducer,
112  MakePointerChecker<UanTransducer> ())
113  .AddTraceSource ("Rx", "Received payload from the MAC layer.",
115  .AddTraceSource ("Tx", "Send payload to the MAC layer.",
117  ;
118  return tid;
119 }
120 
121 void
123 {
124  if (mac != 0)
125  {
126  m_mac = mac;
127  NS_LOG_DEBUG ("Set MAC");
128 
129  if (m_phy != 0)
130  {
131  m_phy->SetMac (mac);
132  m_mac->AttachPhy (m_phy);
133  NS_LOG_DEBUG ("Attached MAC to PHY");
134  }
135  m_mac->SetForwardUpCb (MakeCallback (&UanNetDevice::ForwardUp, this));
136  }
137 
138 }
139 
140 void
142 {
143  if (phy != 0)
144  {
145  m_phy = phy;
146  m_phy->SetDevice (Ptr<UanNetDevice> (this));
147  NS_LOG_DEBUG ("Set PHY");
148  if (m_mac != 0)
149  {
150  m_mac->AttachPhy (phy);
151  m_phy->SetMac (m_mac);
152  NS_LOG_DEBUG ("Attached PHY to MAC");
153  }
154  if (m_trans != 0)
155  {
156  m_phy->SetTransducer (m_trans);
157  NS_LOG_DEBUG ("Added PHY to trans");
158  }
159 
160  }
161 }
162 
163 void
165 {
166  if (channel != 0)
167  {
168  m_channel = channel;
169  NS_LOG_DEBUG ("Set CHANNEL");
170  if (m_trans != 0)
171  {
172 
173  m_channel->AddDevice (this, m_trans);
174  NS_LOG_DEBUG ("Added self to channel device list");
175  m_trans->SetChannel (m_channel);
176  NS_LOG_DEBUG ("Set Transducer channel");
177  }
178  if (m_phy != 0 )
179  {
180  m_phy->SetChannel (channel);
181  }
182  }
183 }
184 
187 {
188  return m_channel;
189 
190 }
193 {
194  return m_mac;
195 }
196 
199 {
200  return m_phy;
201 }
202 
203 void
204 UanNetDevice::SetIfIndex (uint32_t index)
205 {
206  m_ifIndex = index;
207 }
208 
209 uint32_t
211 {
212  return m_ifIndex;
213 }
214 
217 {
218  return m_channel;
219 }
220 
221 Address
223 {
224  return m_mac->GetAddress ();
225 }
226 
227 bool
228 UanNetDevice::SetMtu (uint16_t mtu)
229 {
231  NS_LOG_WARN ("UanNetDevice: MTU is not implemented");
232  m_mtu = mtu;
233  return true;
234 }
235 
236 uint16_t
238 {
239  return m_mtu;
240 }
241 
242 bool
244 {
245  return (m_linkup && (m_phy != 0));
246 }
247 
248 bool
250 {
251  return true;
252 }
253 
254 Address
256 {
257  return m_mac->GetBroadcast ();
258 }
259 
260 bool
262 {
263  return false;
264 }
265 
266 Address
268 {
269  NS_FATAL_ERROR ("UanNetDevice does not support multicast");
270  return m_mac->GetBroadcast ();
271 }
272 
273 Address
275 {
276  NS_FATAL_ERROR ("UanNetDevice does not support multicast");
277  return m_mac->GetBroadcast ();
278 }
279 
280 bool
282 {
283  return false;
284 }
285 bool
287 {
288  return false;
289 }
290 
291 bool
292 UanNetDevice::Send (Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber)
293 {
294  return m_mac->Enqueue (packet, dest, protocolNumber);
295 }
296 
297 bool
298 UanNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
299 {
300  // Not yet implemented
301  NS_ASSERT_MSG (0, "Not yet implemented");
302  return false;
303 }
304 Ptr<Node>
306 {
307  return m_node;
308 }
309 
310 void
312 {
313  m_node = node;
314 }
315 
316 bool
318 {
319  return false;
320 }
321 
322 void
324 {
325  m_forwardUp = cb;
326 }
327 
328 void
330 {
331  NS_LOG_DEBUG ("Forwarding packet up to application");
332  m_rxLogger (pkt, src);
333  m_forwardUp (this, pkt, 0, src);
334 
335 }
336 
339 {
340  return m_trans;
341 }
342 void
344 {
345 
346  if (trans != 0)
347  {
348  m_trans = trans;
349  NS_LOG_DEBUG ("Set Transducer");
350  if (m_phy != 0)
351  {
352  m_phy->SetTransducer (m_trans);
353  NS_LOG_DEBUG ("Attached Phy to transducer");
354  }
355 
356  if (m_channel != 0)
357  {
358  m_channel->AddDevice (this, m_trans);
359  m_trans->SetChannel (m_channel);
360  NS_LOG_DEBUG ("Added self to channel device list");
361  }
362  }
363 
364 }
365 
366 void
368 {
370 }
371 
372 
373 void
375 {
376  // Not implemented yet
377  NS_ASSERT_MSG (0, "Not yet implemented");
378 }
379 
380 bool
382 {
383  return false;
384 }
385 
386 void
388 {
389  NS_ASSERT_MSG (0, "Tried to set MAC address with no MAC");
390  m_mac->SetAddress (UanAddress::ConvertFrom (address));
391 }
392 
393 void
395 {
396  m_phy->SetSleepMode (sleep);
397 }
398 
399 } // namespace ns3
400 
virtual bool SetMtu(const uint16_t mtu)
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
void SetSleepMode(bool sleep)
Set the Phy SLEEP mode.
Ptr< UanMac > GetMac(void) const
Get the MAC used by this device.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
void SetMac(Ptr< UanMac > mac)
Set the MAC layer for this device.
virtual void SetAddress(Address address)
Set the address of this interface.
bool m_linkup
The link state, true if up.
virtual void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void SetChannel(Ptr< UanChannel > channel)
Attach a channel.
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual bool IsMulticast(void) const
virtual bool IsLinkUp(void) const
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual uint16_t GetMtu(void) const
virtual Ptr< Channel > GetChannel(void) const
Ptr< Node > m_node
The node hosting this device.
Ptr< UanTransducer > m_trans
The Transducer attached to this device.
virtual Address GetBroadcast(void) const
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a polymophic address class
Definition: address.h:86
Ptr< UanTransducer > GetTransducer(void) const
Get the transducer associated with this device.
ReceiveCallback m_forwardUp
The receive callback.
uint16_t m_mtu
The device MTU value, in bytes.
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual uint32_t GetIfIndex(void) const
A class used for addressing UAN MAC's.
Definition: uan-address.h:40
static UanAddress ConvertFrom(const Address &address)
Convert a generic address to a UanAddress.
Definition: uan-address.cc:54
virtual void ForwardUp(Ptr< Packet > pkt, const UanAddress &src)
Forward the packet to a higher level, set with SetReceiveCallback.
TracedCallback m_linkChanges
Callback to invoke when the link state changes to UP.
Ptr< UanPhy > m_phy
The PHY layer attached to this device.
virtual Ptr< Node > GetNode(void) const
TracedCallback< Ptr< const Packet >, UanAddress > m_rxLogger
Trace source triggered when forwarding up received payload from the MAC layer.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
UanNetDevice()
Default constructor.
virtual bool IsBroadcast(void) const
Ptr< UanMac > m_mac
The MAC layer attached to this device.
static TypeId GetTypeId(void)
Register this type.
hold objects of type Ptr
Definition: pointer.h:33
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual bool SupportsSendFrom(void) const
TracedCallback< Ptr< const Packet >, UanAddress > m_txLogger
Trace source triggered when sending to the MAC layer.
NS_LOG_COMPONENT_DEFINE("UanNetDevice")
Ptr< UanPhy > GetPhy(void) const
Get the Phy used by this device.
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
virtual Address GetAddress(void) const
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
void SetPhy(Ptr< UanPhy > phy)
Set the Phy layer for this device.
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ConnectWithoutContext(const CallbackBase &callback)
bool m_cleared
Flag when we've been cleared.
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_WARN(msg)
Definition: log.h:280
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
Ptr< UanChannel > m_channel
The channel attached to this device.
virtual void SetNode(Ptr< Node > node)
void SetTransducer(Ptr< UanTransducer > trans)
Set the transdcuer used by this device.
virtual ~UanNetDevice()
Dummy destructor, DoDispose.
virtual bool NeedsArp(void) const
Ptr< UanChannel > DoGetChannel(void) const
tuple address
Definition: first.py:37
uint32_t m_ifIndex
The interface index of this device.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
void Clear(void)
Clear all pointer references.
virtual void SetIfIndex(const uint32_t index)