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  m_phy->Initialize ();
85  m_mac->Initialize ();
86  m_channel->Initialize ();
87  m_trans->Initialize ();
88 
90 }
91 
92 void
94 {
95  Clear ();
97 }
98 
99 TypeId
101 {
102  static TypeId tid = TypeId ("ns3::UanNetDevice")
103  .SetParent<NetDevice> ()
104  .SetGroupName ("Uan")
105  .AddAttribute ("Channel", "The channel attached to this device.",
106  PointerValue (),
108  MakePointerChecker<UanChannel> ())
109  .AddAttribute ("Phy", "The PHY layer attached to this device.",
110  PointerValue (),
112  MakePointerChecker<UanPhy> ())
113  .AddAttribute ("Mac", "The MAC layer attached to this device.",
114  PointerValue (),
116  MakePointerChecker<UanMac> ())
117  .AddAttribute ("Transducer", "The Transducer attached to this device.",
118  PointerValue (),
121  MakePointerChecker<UanTransducer> ())
122  .AddTraceSource ("Rx", "Received payload from the MAC layer.",
124  "ns3::UanNetDevice::RxTxTracedCallback")
125  .AddTraceSource ("Tx", "Send payload to the MAC layer.",
127  "ns3::UanNetDevice::RxTxTracedCallback")
128  ;
129  return tid;
130 }
131 
132 void
134 {
135  if (mac != 0)
136  {
137  m_mac = mac;
138  NS_LOG_DEBUG ("Set MAC");
139 
140  if (m_phy != 0)
141  {
142  m_phy->SetMac (mac);
143  m_mac->AttachPhy (m_phy);
144  NS_LOG_DEBUG ("Attached MAC to PHY");
145  }
146  m_mac->SetForwardUpCb (MakeCallback (&UanNetDevice::ForwardUp, this));
147  }
148 
149 }
150 
151 void
153 {
154  if (phy != 0)
155  {
156  m_phy = phy;
157  m_phy->SetDevice (Ptr<UanNetDevice> (this));
158  NS_LOG_DEBUG ("Set PHY");
159  if (m_mac != 0)
160  {
161  m_mac->AttachPhy (phy);
162  m_phy->SetMac (m_mac);
163  NS_LOG_DEBUG ("Attached PHY to MAC");
164  }
165  if (m_trans != 0)
166  {
167  m_phy->SetTransducer (m_trans);
168  NS_LOG_DEBUG ("Added PHY to trans");
169  }
170 
171  }
172 }
173 
174 void
176 {
177  if (channel != 0)
178  {
179  m_channel = channel;
180  NS_LOG_DEBUG ("Set CHANNEL");
181  if (m_trans != 0)
182  {
183 
184  m_channel->AddDevice (this, m_trans);
185  NS_LOG_DEBUG ("Added self to channel device list");
186  m_trans->SetChannel (m_channel);
187  NS_LOG_DEBUG ("Set Transducer channel");
188  }
189  if (m_phy != 0 )
190  {
191  m_phy->SetChannel (channel);
192  }
193  }
194 }
195 
198 {
199  return m_channel;
200 
201 }
204 {
205  return m_mac;
206 }
207 
210 {
211  return m_phy;
212 }
213 
214 void
215 UanNetDevice::SetIfIndex (uint32_t index)
216 {
217  m_ifIndex = index;
218 }
219 
220 uint32_t
222 {
223  return m_ifIndex;
224 }
225 
228 {
229  return m_channel;
230 }
231 
232 Address
234 {
235  return m_mac->GetAddress ();
236 }
237 
238 bool
239 UanNetDevice::SetMtu (uint16_t mtu)
240 {
242  NS_LOG_WARN ("UanNetDevice: MTU is not implemented");
243  m_mtu = mtu;
244  return true;
245 }
246 
247 uint16_t
249 {
250  return m_mtu;
251 }
252 
253 bool
255 {
256  return (m_linkup && (m_phy != 0));
257 }
258 
259 bool
261 {
262  return true;
263 }
264 
265 Address
267 {
268  return m_mac->GetBroadcast ();
269 }
270 
271 bool
273 {
274  return true;
275 }
276 
277 Address
279 {
280  NS_UNUSED (multicastGroup);
281  return m_mac->GetBroadcast ();
282 }
283 
284 Address
286 {
287  return m_mac->GetBroadcast ();
288 }
289 
290 bool
292 {
293  return false;
294 }
295 bool
297 {
298  return false;
299 }
300 
301 bool
302 UanNetDevice::Send (Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber)
303 {
304  uint8_t tmp [6];
305  dest.CopyTo (tmp);
306  Mac8Address udest (tmp[0]);
307 
308  return m_mac->Enqueue (packet, protocolNumber, udest);
309 }
310 
311 bool
312 UanNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
313 {
314  NS_UNUSED (source);
315  NS_UNUSED (dest);
316  NS_UNUSED (protocolNumber);
317  // Not yet implemented
318  NS_ASSERT_MSG (0, "Not yet implemented");
319  return false;
320 }
321 Ptr<Node>
323 {
324  return m_node;
325 }
326 
327 void
329 {
330  m_node = node;
331 }
332 
333 bool
335 {
336  return true;
337 }
338 
339 void
341 {
342  m_forwardUp = cb;
343 }
344 
345 void
346 UanNetDevice::ForwardUp (Ptr<Packet> pkt, uint16_t protocolNumber, const Mac8Address &src)
347 {
348  NS_LOG_DEBUG ("Forwarding packet up to application");
349  m_rxLogger (pkt, src);
350  m_forwardUp (this, pkt, protocolNumber, src);
351 
352 }
353 
356 {
357  return m_trans;
358 }
359 void
361 {
362 
363  if (trans != 0)
364  {
365  m_trans = trans;
366  NS_LOG_DEBUG ("Set Transducer");
367  if (m_phy != 0)
368  {
369  m_phy->SetTransducer (m_trans);
370  NS_LOG_DEBUG ("Attached Phy to transducer");
371  }
372 
373  if (m_channel != 0)
374  {
375  m_channel->AddDevice (this, m_trans);
376  m_trans->SetChannel (m_channel);
377  NS_LOG_DEBUG ("Added self to channel device list");
378  }
379  }
380 
381 }
382 
383 void
385 {
387 }
388 
389 
390 void
392 {
393  // Not implemented yet
394  NS_ASSERT_MSG (0, "Not yet implemented");
395 }
396 
397 bool
399 {
400  return false;
401 }
402 
403 void
405 {
406  NS_ASSERT_MSG (0, "Tried to set MAC address with no MAC");
407  m_mac->SetAddress (Mac8Address::ConvertFrom (address));
408 }
409 
410 void
412 {
413  m_phy->SetSleepMode (sleep);
414 }
415 
416 void
417 UanNetDevice::SetTxModeIndex (uint32_t txModeIndex)
418 {
419  m_mac->SetTxModeIndex (txModeIndex);
420 }
421 
422 uint32_t
424 {
425  return m_mac->GetTxModeIndex ();
426 }
427 
428 } // namespace ns3
429 
virtual bool SetMtu(const uint16_t mtu)
virtual Ptr< Node > GetNode(void) const
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual bool NeedsArp(void) const
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
void SetSleepMode(bool sleep)
Set the Phy SLEEP mode.
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.
Callback template class.
Definition: callback.h:1176
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:45
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
bool m_linkup
The link state, true if up.
virtual bool IsMulticast(void) const
void SetChannel(Ptr< UanChannel > channel)
Attach a channel.
void SetTxModeIndex(uint32_t txModeIndex)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_UNUSED(x)
Mark a local variable as unused.
Definition: unused.h:36
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Ptr< Node > m_node
The node hosting this device.
Ptr< UanTransducer > m_trans
The Transducer attached to this device.
TracedCallback< Ptr< const Packet >, Mac8Address > m_txLogger
Trace source triggered when sending to the MAC layer.
Ptr< UanChannel > DoGetChannel(void) const
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:85
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
ReceiveCallback m_forwardUp
The receive callback.
phy
Definition: third.py:86
virtual bool IsBroadcast(void) const
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:220
virtual void DoInitialize(void)
Initialize() implementation.
TracedCallback m_linkChanges
Callback to invoke when the link state changes to UP.
Ptr< UanPhy > m_phy
The PHY layer attached to this device.
mac
Definition: third.py:92
virtual Ptr< Channel > GetChannel(void) const
virtual uint32_t GetIfIndex(void) const
A class used for addressing MAC8 MAC&#39;s.
Definition: mac8-address.h:42
virtual Address GetBroadcast(void) const
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
virtual void DoDispose(void)
Destructor implementation.
UanNetDevice()
Default constructor.
virtual bool IsLinkUp(void) const
Ptr< UanMac > m_mac
The MAC layer attached to this device.
static TypeId GetTypeId(void)
Register this type.
virtual uint16_t GetMtu(void) const
virtual void ForwardUp(Ptr< Packet > pkt, uint16_t protocolNumber, const Mac8Address &src)
Forward the packet to a higher level, set with SetReceiveCallback.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
address
Definition: first.py:37
virtual bool SupportsSendFrom(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Ptr< UanMac > GetMac(void) const
Get the MAC used by this device.
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:49
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint32_t GetTxModeIndex()
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
bool m_cleared
Flag when we&#39;ve been cleared.
virtual Address GetAddress(void) const
Network layer to device interface.
Definition: net-device.h:95
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:262
TracedCallback< Ptr< const Packet >, Mac8Address > m_rxLogger
Trace source triggered when forwarding up received payload from the MAC layer.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Ptr< UanTransducer > GetTransducer(void) const
Get the transducer associated with this device.
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.
Ptr< UanPhy > GetPhy(void) const
Get the Phy used by this device.
uint32_t m_ifIndex
The interface index of this device.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:54
void Clear(void)
Clear all pointer references.
virtual void SetIfIndex(const uint32_t index)
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:82