A Discrete-Event Network Simulator
API
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 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("UanNetDevice");
36 
37 NS_OBJECT_ENSURE_REGISTERED (UanNetDevice);
38 
40  : NetDevice (),
41  m_mtu (64000),
42  m_cleared (false)
43 {
44 }
45 
47 {
48 }
49 
50 void
52 {
53  if (m_cleared)
54  {
55  return;
56  }
57  m_cleared = true;
58  m_node = 0;
59  if (m_channel)
60  {
61  m_channel->Clear ();
62  m_channel = 0;
63  }
64  if (m_mac)
65  {
66  m_mac->Clear ();
67  m_mac = 0;
68  }
69  if (m_phy)
70  {
71  m_phy->Clear ();
72  m_phy = 0;
73  }
74  if (m_trans)
75  {
76  m_trans->Clear ();
77  m_trans = 0;
78  }
79 }
80 
81 void
83 {
84  Clear ();
86 }
87 
88 TypeId
90 {
91 
92 
93  static TypeId tid = TypeId ("ns3::UanNetDevice")
94  .SetParent<NetDevice> ()
95  .AddAttribute ("Channel", "The channel attached to this device.",
96  PointerValue (),
98  MakePointerChecker<UanChannel> ())
99  .AddAttribute ("Phy", "The PHY layer attached to this device.",
100  PointerValue (),
102  MakePointerChecker<UanPhy> ())
103  .AddAttribute ("Mac", "The MAC layer attached to this device.",
104  PointerValue (),
106  MakePointerChecker<UanMac> ())
107  .AddAttribute ("Transducer", "The Transducer attached to this device.",
108  PointerValue (),
111  MakePointerChecker<UanTransducer> ())
112  .AddTraceSource ("Rx", "Received payload from the MAC layer.",
114  "ns3::UanNetDevice::RxTxTracedCallback")
115  .AddTraceSource ("Tx", "Send payload to the MAC layer.",
117  "ns3::UanNetDevice::RxTxTracedCallback")
118  ;
119  return tid;
120 }
121 
122 void
124 {
125  if (mac != 0)
126  {
127  m_mac = mac;
128  NS_LOG_DEBUG ("Set MAC");
129 
130  if (m_phy != 0)
131  {
132  m_phy->SetMac (mac);
133  m_mac->AttachPhy (m_phy);
134  NS_LOG_DEBUG ("Attached MAC to PHY");
135  }
136  m_mac->SetForwardUpCb (MakeCallback (&UanNetDevice::ForwardUp, this));
137  }
138 
139 }
140 
141 void
143 {
144  if (phy != 0)
145  {
146  m_phy = phy;
147  m_phy->SetDevice (Ptr<UanNetDevice> (this));
148  NS_LOG_DEBUG ("Set PHY");
149  if (m_mac != 0)
150  {
151  m_mac->AttachPhy (phy);
152  m_phy->SetMac (m_mac);
153  NS_LOG_DEBUG ("Attached PHY to MAC");
154  }
155  if (m_trans != 0)
156  {
157  m_phy->SetTransducer (m_trans);
158  NS_LOG_DEBUG ("Added PHY to trans");
159  }
160 
161  }
162 }
163 
164 void
166 {
167  if (channel != 0)
168  {
169  m_channel = channel;
170  NS_LOG_DEBUG ("Set CHANNEL");
171  if (m_trans != 0)
172  {
173 
174  m_channel->AddDevice (this, m_trans);
175  NS_LOG_DEBUG ("Added self to channel device list");
176  m_trans->SetChannel (m_channel);
177  NS_LOG_DEBUG ("Set Transducer channel");
178  }
179  if (m_phy != 0 )
180  {
181  m_phy->SetChannel (channel);
182  }
183  }
184 }
185 
188 {
189  return m_channel;
190 
191 }
194 {
195  return m_mac;
196 }
197 
200 {
201  return m_phy;
202 }
203 
204 void
205 UanNetDevice::SetIfIndex (uint32_t index)
206 {
207  m_ifIndex = index;
208 }
209 
210 uint32_t
212 {
213  return m_ifIndex;
214 }
215 
218 {
219  return m_channel;
220 }
221 
222 Address
224 {
225  return m_mac->GetAddress ();
226 }
227 
228 bool
229 UanNetDevice::SetMtu (uint16_t mtu)
230 {
232  NS_LOG_WARN ("UanNetDevice: MTU is not implemented");
233  m_mtu = mtu;
234  return true;
235 }
236 
237 uint16_t
239 {
240  return m_mtu;
241 }
242 
243 bool
245 {
246  return (m_linkup && (m_phy != 0));
247 }
248 
249 bool
251 {
252  return true;
253 }
254 
255 Address
257 {
258  return m_mac->GetBroadcast ();
259 }
260 
261 bool
263 {
264  return false;
265 }
266 
267 Address
269 {
270  NS_FATAL_ERROR ("UanNetDevice does not support multicast");
271  return m_mac->GetBroadcast ();
272 }
273 
274 Address
276 {
277  NS_FATAL_ERROR ("UanNetDevice does not support multicast");
278  return m_mac->GetBroadcast ();
279 }
280 
281 bool
283 {
284  return false;
285 }
286 bool
288 {
289  return false;
290 }
291 
292 bool
293 UanNetDevice::Send (Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber)
294 {
295  return m_mac->Enqueue (packet, dest, protocolNumber);
296 }
297 
298 bool
299 UanNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
300 {
301  // Not yet implemented
302  NS_ASSERT_MSG (0, "Not yet implemented");
303  return false;
304 }
305 Ptr<Node>
307 {
308  return m_node;
309 }
310 
311 void
313 {
314  m_node = node;
315 }
316 
317 bool
319 {
320  return false;
321 }
322 
323 void
325 {
326  m_forwardUp = cb;
327 }
328 
329 void
331 {
332  NS_LOG_DEBUG ("Forwarding packet up to application");
333  m_rxLogger (pkt, src);
334  m_forwardUp (this, pkt, 0, src);
335 
336 }
337 
340 {
341  return m_trans;
342 }
343 void
345 {
346 
347  if (trans != 0)
348  {
349  m_trans = trans;
350  NS_LOG_DEBUG ("Set Transducer");
351  if (m_phy != 0)
352  {
353  m_phy->SetTransducer (m_trans);
354  NS_LOG_DEBUG ("Attached Phy to transducer");
355  }
356 
357  if (m_channel != 0)
358  {
359  m_channel->AddDevice (this, m_trans);
360  m_trans->SetChannel (m_channel);
361  NS_LOG_DEBUG ("Added self to channel device list");
362  }
363  }
364 
365 }
366 
367 void
369 {
371 }
372 
373 
374 void
376 {
377  // Not implemented yet
378  NS_ASSERT_MSG (0, "Not yet implemented");
379 }
380 
381 bool
383 {
384  return false;
385 }
386 
387 void
389 {
390  NS_ASSERT_MSG (0, "Tried to set MAC address with no MAC");
391  m_mac->SetAddress (UanAddress::ConvertFrom (address));
392 }
393 
394 void
396 {
397  m_phy->SetSleepMode (sleep);
398 }
399 
400 } // namespace ns3
401 
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:73
void SetMac(Ptr< UanMac > mac)
Set the MAC layer for this device.
virtual void SetAddress(Address address)
Set the address of this interface.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
bool m_linkup
The link state, true if up.
virtual void DoDispose()
Destructor implementation.
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:338
#define NS_FATAL_ERROR(msg)
Fatal error handling.
Definition: fatal-error.h:100
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
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
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)
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:210
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:1290
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
virtual bool SupportsSendFrom(void) const
TracedCallback< Ptr< const Packet >, UanAddress > m_txLogger
Trace source triggered when sending to the MAC layer.
Ptr< UanPhy > GetPhy(void) const
Get the Phy used by this device.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
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:47
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
bool m_cleared
Flag when we've been cleared.
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
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:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void Clear(void)
Clear all pointer references.
virtual void SetIfIndex(const uint32_t index)