A Discrete-Event Network Simulator
API
uan-net-device.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Leonard Tracy <lentracy@gmail.com>
18  */
19 
20 #include "uan-net-device.h"
21 
22 #include "uan-channel.h"
23 #include "uan-mac.h"
24 #include "uan-phy.h"
25 #include "uan-transducer.h"
26 
27 #include "ns3/assert.h"
28 #include "ns3/log.h"
29 #include "ns3/node.h"
30 #include "ns3/pointer.h"
31 #include "ns3/trace-source-accessor.h"
32 #include "ns3/traced-callback.h"
33 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE("UanNetDevice");
38 
39 NS_OBJECT_ENSURE_REGISTERED(UanNetDevice);
40 
42  : NetDevice(),
43  m_mtu(64000),
44  m_cleared(false)
45 {
46 }
47 
49 {
50 }
51 
52 void
54 {
55  if (m_cleared)
56  {
57  return;
58  }
59  m_cleared = true;
60  m_node = nullptr;
61  if (m_channel)
62  {
63  m_channel->Clear();
64  m_channel = nullptr;
65  }
66  if (m_mac)
67  {
68  m_mac->Clear();
69  m_mac = nullptr;
70  }
71  if (m_phy)
72  {
73  m_phy->Clear();
74  m_phy = nullptr;
75  }
76  if (m_trans)
77  {
78  m_trans->Clear();
79  m_trans = nullptr;
80  }
81 }
82 
83 void
85 {
86  m_phy->Initialize();
87  m_mac->Initialize();
88  m_channel->Initialize();
89  m_trans->Initialize();
90 
92 }
93 
94 void
96 {
97  Clear();
99 }
100 
101 TypeId
103 {
104  static TypeId tid =
105  TypeId("ns3::UanNetDevice")
106  .SetParent<NetDevice>()
107  .SetGroupName("Uan")
108  .AddAttribute(
109  "Channel",
110  "The channel attached to this device.",
111  PointerValue(),
113  MakePointerChecker<UanChannel>())
114  .AddAttribute("Phy",
115  "The PHY layer attached to this device.",
116  PointerValue(),
118  MakePointerChecker<UanPhy>())
119  .AddAttribute("Mac",
120  "The MAC layer attached to this device.",
121  PointerValue(),
123  MakePointerChecker<UanMac>())
124  .AddAttribute(
125  "Transducer",
126  "The Transducer attached to this device.",
127  PointerValue(),
129  MakePointerChecker<UanTransducer>())
130  .AddTraceSource("Rx",
131  "Received payload from the MAC layer.",
133  "ns3::UanNetDevice::RxTxTracedCallback")
134  .AddTraceSource("Tx",
135  "Send payload to the MAC layer.",
137  "ns3::UanNetDevice::RxTxTracedCallback");
138  return tid;
139 }
140 
141 void
143 {
144  if (mac)
145  {
146  m_mac = mac;
147  NS_LOG_DEBUG("Set MAC");
148 
149  if (m_phy)
150  {
151  m_phy->SetMac(mac);
152  m_mac->AttachPhy(m_phy);
153  NS_LOG_DEBUG("Attached MAC to PHY");
154  }
155  m_mac->SetForwardUpCb(MakeCallback(&UanNetDevice::ForwardUp, this));
156  }
157 }
158 
159 void
161 {
162  if (phy)
163  {
164  m_phy = phy;
165  m_phy->SetDevice(Ptr<UanNetDevice>(this));
166  NS_LOG_DEBUG("Set PHY");
167  if (m_mac)
168  {
169  m_mac->AttachPhy(phy);
170  m_phy->SetMac(m_mac);
171  NS_LOG_DEBUG("Attached PHY to MAC");
172  }
173  if (m_trans)
174  {
175  m_phy->SetTransducer(m_trans);
176  NS_LOG_DEBUG("Added PHY to trans");
177  }
178  }
179 }
180 
181 void
183 {
184  if (channel)
185  {
186  m_channel = channel;
187  NS_LOG_DEBUG("Set CHANNEL");
188  if (m_trans)
189  {
190  m_channel->AddDevice(this, m_trans);
191  NS_LOG_DEBUG("Added self to channel device list");
192  m_trans->SetChannel(m_channel);
193  NS_LOG_DEBUG("Set Transducer channel");
194  }
195  if (m_phy)
196  {
197  m_phy->SetChannel(channel);
198  }
199  }
200 }
201 
204 {
205  return m_channel;
206 }
207 
210 {
211  return m_mac;
212 }
213 
216 {
217  return m_phy;
218 }
219 
220 void
222 {
223  m_ifIndex = index;
224 }
225 
226 uint32_t
228 {
229  return m_ifIndex;
230 }
231 
234 {
235  return m_channel;
236 }
237 
238 Address
240 {
241  return m_mac->GetAddress();
242 }
243 
244 bool
245 UanNetDevice::SetMtu(uint16_t mtu)
246 {
248  NS_LOG_WARN("UanNetDevice: MTU is not implemented");
249  m_mtu = mtu;
250  return true;
251 }
252 
253 uint16_t
255 {
256  return m_mtu;
257 }
258 
259 bool
261 {
262  return (m_linkup && m_phy);
263 }
264 
265 bool
267 {
268  return true;
269 }
270 
271 Address
273 {
274  return m_mac->GetBroadcast();
275 }
276 
277 bool
279 {
280  return true;
281 }
282 
283 Address
284 UanNetDevice::GetMulticast(Ipv4Address /* multicastGroup */) const
285 {
286  return m_mac->GetBroadcast();
287 }
288 
289 Address
291 {
292  return m_mac->GetBroadcast();
293 }
294 
295 bool
297 {
298  return false;
299 }
300 
301 bool
303 {
304  return false;
305 }
306 
307 bool
308 UanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
309 {
310  uint8_t tmp[6];
311  dest.CopyTo(tmp);
312  Mac8Address udest(tmp[0]);
313 
314  return m_mac->Enqueue(packet, protocolNumber, udest);
315 }
316 
317 bool
319  const Address& /* source */,
320  const Address& /* dest */,
321  uint16_t /* protocolNumber */)
322 {
323  // Not yet implemented
324  NS_ASSERT_MSG(false, "Not yet implemented");
325  return false;
326 }
327 
328 Ptr<Node>
330 {
331  return m_node;
332 }
333 
334 void
336 {
337  m_node = node;
338 }
339 
340 bool
342 {
343  return true;
344 }
345 
346 void
348 {
349  m_forwardUp = cb;
350 }
351 
352 void
353 UanNetDevice::ForwardUp(Ptr<Packet> pkt, uint16_t protocolNumber, const Mac8Address& src)
354 {
355  NS_LOG_DEBUG("Forwarding packet up to application");
356  m_rxLogger(pkt, src);
357  m_forwardUp(this, pkt, protocolNumber, src);
358 }
359 
362 {
363  return m_trans;
364 }
365 
366 void
368 {
369  if (trans)
370  {
371  m_trans = trans;
372  NS_LOG_DEBUG("Set Transducer");
373  if (m_phy)
374  {
375  m_phy->SetTransducer(m_trans);
376  NS_LOG_DEBUG("Attached Phy to transducer");
377  }
378 
379  if (m_channel)
380  {
381  m_channel->AddDevice(this, m_trans);
382  m_trans->SetChannel(m_channel);
383  NS_LOG_DEBUG("Added self to channel device list");
384  }
385  }
386 }
387 
388 void
390 {
392 }
393 
394 void
396 {
397  // Not implemented yet
398  NS_ASSERT_MSG(0, "Not yet implemented");
399 }
400 
401 bool
403 {
404  return false;
405 }
406 
407 void
409 {
410  NS_ASSERT_MSG(m_mac, "Tried to set MAC address with no MAC");
411  m_mac->SetAddress(Mac8Address::ConvertFrom(address));
412 }
413 
414 void
416 {
417  m_phy->SetSleepMode(sleep);
418 }
419 
420 void
421 UanNetDevice::SetTxModeIndex(uint32_t txModeIndex)
422 {
423  m_mac->SetTxModeIndex(txModeIndex);
424 }
425 
426 uint32_t
428 {
429  return m_mac->GetTxModeIndex();
430 }
431 
432 } // namespace ns3
a polymophic address class
Definition: address.h:100
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:86
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Describes an IPv6 address.
Definition: ipv6-address.h:50
A class used for addressing MAC8 MAC's.
Definition: mac8-address.h:44
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:61
Network layer to device interface.
Definition: net-device.h:98
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:360
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
bool SupportsSendFrom() const override
bool SetMtu(const uint16_t mtu) override
Ptr< UanTransducer > GetTransducer() const
Get the transducer associated with this device.
void SetTxModeIndex(uint32_t txModeIndex)
Set the Tx mode index (Modulation type).
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
Address GetAddress() const override
UanNetDevice()
Default constructor.
uint32_t m_ifIndex
The interface index of this device.
void AddLinkChangeCallback(Callback< void > callback) override
uint32_t GetTxModeIndex()
Get the Tx mode index (Modulation type).
bool IsLinkUp() const override
void DoDispose() override
Destructor implementation.
void DoInitialize() override
Initialize() implementation.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
void Clear()
Clear all pointer references.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
~UanNetDevice() override
Dummy destructor, DoDispose.
TracedCallback< Ptr< const Packet >, Mac8Address > m_txLogger
Trace source triggered when sending to the MAC layer.
void SetMac(Ptr< UanMac > mac)
Set the MAC layer for this device.
Ptr< Node > m_node
The node hosting this device.
bool IsBroadcast() const override
uint32_t GetIfIndex() const override
Ptr< UanPhy > m_phy
The PHY layer attached to this device.
bool IsMulticast() const override
Ptr< Node > GetNode() const override
void SetAddress(Address address) override
Set the address of this interface.
void SetSleepMode(bool sleep)
Set the Phy SLEEP mode.
bool m_cleared
Flag when we've been cleared.
virtual void ForwardUp(Ptr< Packet > pkt, uint16_t protocolNumber, const Mac8Address &src)
Forward the packet to a higher level, set with SetReceiveCallback.
Ptr< UanTransducer > m_trans
The Transducer attached to this device.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
bool m_linkup
The link state, true if up.
void SetChannel(Ptr< UanChannel > channel)
Attach a channel.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
void SetNode(Ptr< Node > node) override
Ptr< UanMac > GetMac() const
Get the MAC used by this device.
void SetIfIndex(const uint32_t index) override
TracedCallback m_linkChanges
Callback to invoke when the link state changes to UP.
Ptr< UanChannel > m_channel
The channel attached to this device.
Ptr< Channel > GetChannel() const override
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Ptr< UanChannel > DoGetChannel() const
TracedCallback< Ptr< const Packet >, Mac8Address > m_rxLogger
Trace source triggered when forwarding up received payload from the MAC layer.
uint16_t GetMtu() const override
Address GetBroadcast() const override
void SetTransducer(Ptr< UanTransducer > trans)
Set the transdcuer used by this device.
uint16_t m_mtu
The device MTU value, in bytes.
bool NeedsArp() const override
Ptr< UanPhy > GetPhy() const
Get the Phy used by this device.
ReceiveCallback m_forwardUp
The receive callback.
Ptr< UanMac > m_mac
The MAC layer attached to this device.
static TypeId GetTypeId()
Register this type.
void SetPhy(Ptr< UanPhy > phy)
Set the Phy layer for 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:86
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:231
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
address
Definition: first.py:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:707
channel
Definition: third.py:81
mac
Definition: third.py:85
phy
Definition: third.py:82