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 
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 (),
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.",
114  .AddTraceSource ("Tx", "Send payload to the MAC layer.",
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  }
135  }
136 
137 }
138 
139 void
141 {
142  if (phy != 0)
143  {
144  m_phy = phy;
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  {
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");
175  NS_LOG_DEBUG ("Set Transducer channel");
176  }
177  if (m_phy != 0 )
178  {
179  m_phy->SetChannel (channel);
180  }
181  }
182 }
183 
186 {
187  return m_channel;
188 
189 }
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
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 
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  {
352  NS_LOG_DEBUG ("Attached Phy to transducer");
353  }
354 
355  if (m_channel != 0)
356  {
357  m_channel->AddDevice (this, m_trans);
359  NS_LOG_DEBUG ("Added self to channel device list");
360  }
361  }
362 
363 }
364 
365 void
367 {
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");
390 }
391 
392 void
394 {
395  m_phy->SetSleepMode (sleep);
396 }
397 
398 } // namespace ns3
399