A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
uan-net-device.cc
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 UanNetDevice::UanNetDevice ()
40  : NetDevice (),
41  m_mtu (64000),
42  m_cleared (false)
43 {
44 }
45 
46 UanNetDevice::~UanNetDevice ()
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
89 UanNetDevice::GetTypeId ()
90 {
91 
92 
93  static TypeId tid = TypeId ("ns3::UanNetDevice")
94  .SetParent<NetDevice> ()
95  .AddAttribute ("Channel", "The channel attached to this device",
96  PointerValue (),
97  MakePointerAccessor (&UanNetDevice::DoGetChannel, &UanNetDevice::SetChannel),
98  MakePointerChecker<UanChannel> ())
99  .AddAttribute ("Phy", "The PHY layer attached to this device.",
100  PointerValue (),
101  MakePointerAccessor (&UanNetDevice::GetPhy, &UanNetDevice::SetPhy),
102  MakePointerChecker<UanPhy> ())
103  .AddAttribute ("Mac", "The MAC layer attached to this device.",
104  PointerValue (),
105  MakePointerAccessor (&UanNetDevice::GetMac, &UanNetDevice::SetMac),
106  MakePointerChecker<UanMac> ())
107  .AddAttribute ("Transducer", "The Transducer attached to this device.",
108  PointerValue (),
109  MakePointerAccessor (&UanNetDevice::GetTransducer,
111  MakePointerChecker<UanTransducer> ())
112  .AddTraceSource ("Rx", "Received payload from the MAC layer.",
113  MakeTraceSourceAccessor (&UanNetDevice::m_rxLogger))
114  .AddTraceSource ("Tx", "Send payload to the MAC layer.",
115  MakeTraceSourceAccessor (&UanNetDevice::m_txLogger))
116  ;
117  return tid;
118 }
119 
120 void
122 {
123  if (mac != 0)
124  {
125  m_mac = mac;
126  NS_LOG_DEBUG ("Set MAC");
127 
128  if (m_phy != 0)
129  {
130  m_phy->SetMac (mac);
131  m_mac->AttachPhy (m_phy);
132  NS_LOG_DEBUG ("Attached MAC to PHY");
133  }
134  m_mac->SetForwardUpCb (MakeCallback (&UanNetDevice::ForwardUp, this));
135  }
136 
137 }
138 
139 void
141 {
142  if (phy != 0)
143  {
144  m_phy = phy;
145  m_phy->SetDevice (Ptr<UanNetDevice> (this));
146  NS_LOG_DEBUG ("Set PHY");
147  if (m_mac != 0)
148  {
149  m_mac->AttachPhy (phy);
150  m_phy->SetMac (m_mac);
151  NS_LOG_DEBUG ("Attached PHY to MAC");
152  }
153  if (m_trans != 0)
154  {
155  m_phy->SetTransducer (m_trans);
156  NS_LOG_DEBUG ("Added PHY to trans");
157  }
158 
159  }
160 }
161 
162 void
164 {
165  if (channel != 0)
166  {
167  m_channel = channel;
168  NS_LOG_DEBUG ("Set CHANNEL");
169  if (m_trans != 0)
170  {
171 
172  m_channel->AddDevice (this, m_trans);
173  NS_LOG_DEBUG ("Added self to channel device list");
174  m_trans->SetChannel (m_channel);
175  NS_LOG_DEBUG ("Set Transducer channel");
176  }
177  if (m_phy != 0 )
178  {
179  m_phy->SetChannel (channel);
180  }
181  }
182 }
183 
185 UanNetDevice::DoGetChannel (void) const
186 {
187  return m_channel;
188 
189 }
190 Ptr<UanMac>
192 {
193  return m_mac;
194 }
195 
198 {
199  return m_phy;
200 }
201 
202 void
203 UanNetDevice::SetIfIndex (uint32_t index)
204 {
205  m_ifIndex = index;
206 }
207 
208 uint32_t
210 {
211  return m_ifIndex;
212 }
213 
216 {
217  return m_channel;
218 }
219 
220 Address
222 {
223  return m_mac->GetAddress ();
224 }
225 
226 bool
227 UanNetDevice::SetMtu (uint16_t mtu)
228 {
229  // TODO: Check this in MAC
230  NS_LOG_WARN ("UanNetDevice: MTU is not implemented");
231  m_mtu = mtu;
232  return true;
233 }
234 
235 uint16_t
237 {
238  return m_mtu;
239 }
240 
241 bool
243 {
244  return (m_linkup && (m_phy != 0));
245 }
246 
247 bool
249 {
250  return true;
251 }
252 
253 Address
255 {
256  return m_mac->GetBroadcast ();
257 }
258 
259 bool
261 {
262  return false;
263 }
264 
265 Address
267 {
268  NS_FATAL_ERROR ("UanNetDevice does not support multicast");
269  return m_mac->GetBroadcast ();
270 }
271 
272 Address
274 {
275  NS_FATAL_ERROR ("UanNetDevice does not support multicast");
276  return m_mac->GetBroadcast ();
277 }
278 
279 bool
281 {
282  return false;
283 }
284 bool
286 {
287  return false;
288 }
289 
290 bool
291 UanNetDevice::Send (Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber)
292 {
293  return m_mac->Enqueue (packet, dest, protocolNumber);
294 }
295 
296 bool
297 UanNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
298 {
299  // Not yet implemented
300  NS_ASSERT_MSG (0, "Not yet implemented");
301  return false;
302 }
303 Ptr<Node>
305 {
306  return m_node;
307 }
308 
309 void
311 {
312  m_node = node;
313 }
314 
315 bool
317 {
318  return false;
319 }
320 
321 void
323 {
324  m_forwardUp = cb;
325 }
326 
327 void
328 UanNetDevice::ForwardUp (Ptr<Packet> pkt, const UanAddress &src)
329 {
330  NS_LOG_DEBUG ("Forwarding packet up to application");
331  m_rxLogger (pkt, src);
332  m_forwardUp (this, pkt, 0, src);
333 
334 }
335 
336 Ptr<UanTransducer>
338 {
339  return m_trans;
340 }
341 void
343 {
344 
345  if (trans != 0)
346  {
347  m_trans = trans;
348  NS_LOG_DEBUG ("Set Transducer");
349  if (m_phy != 0)
350  {
351  m_phy->SetTransducer (m_trans);
352  NS_LOG_DEBUG ("Attached Phy to transducer");
353  }
354 
355  if (m_channel != 0)
356  {
357  m_channel->AddDevice (this, m_trans);
358  m_trans->SetChannel (m_channel);
359  NS_LOG_DEBUG ("Added self to channel device list");
360  }
361  }
362 
363 }
364 
365 void
367 {
368  m_linkChanges.ConnectWithoutContext (callback);
369 }
370 
371 
372 void
374 {
375  // Not implemented yet
376  NS_ASSERT_MSG (0, "Not yet implemented");
377 }
378 
379 bool
381 {
382  return false;
383 }
384 
385 void
387 {
388  NS_ASSERT_MSG (0, "Tried to set MAC address with no MAC");
389  m_mac->SetAddress (UanAddress::ConvertFrom (address));
390 }
391 
392 void
393 UanNetDevice::SetSleepMode (bool sleep)
394 {
395  m_phy->SetSleepMode (sleep);
396 }
397 
398 } // namespace ns3
399 
virtual bool SetMtu(const uint16_t mtu)
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Ptr< UanMac > GetMac(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
void SetMac(Ptr< UanMac > mac)
virtual void SetAddress(Address address)
virtual void DoDispose()
void SetChannel(Ptr< UanChannel > 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)
Definition: log.h:122
virtual void DoDispose(void)
Definition: object.cc:335
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
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
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual uint32_t GetIfIndex(void) const
static UanAddress ConvertFrom(const Address &address)
Definition: uan-address.cc:54
virtual Ptr< Node > GetNode(void) const
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
virtual bool IsBroadcast(void) const
hold objects of type Ptr<T>
Definition: pointer.h:33
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual bool SupportsSendFrom(void) const
Ptr< UanPhy > GetPhy(void) const
#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)
Describes an IPv6 address.
Definition: ipv6-address.h:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ConnectWithoutContext(const CallbackBase &callback)
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_WARN(msg)
Definition: log.h:246
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
virtual void SetNode(Ptr< Node > node)
void SetTransducer(Ptr< UanTransducer > trans)
virtual bool NeedsArp(void) const
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
virtual void SetIfIndex(const uint32_t index)