A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
wifi-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) 2005,2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "wifi-net-device.h"
21 #include "wifi-mac.h"
22 #include "wifi-phy.h"
24 #include "wifi-channel.h"
25 #include "ns3/llc-snap-header.h"
26 #include "ns3/packet.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/pointer.h"
29 #include "ns3/node.h"
30 #include "ns3/trace-source-accessor.h"
31 #include "ns3/log.h"
32 
33 NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
34 
35 namespace ns3 {
36 
37 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice)
38  ;
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::WifiNetDevice")
44  .SetParent<NetDevice> ()
45  .AddConstructor<WifiNetDevice> ()
46  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
48  MakeUintegerAccessor (&WifiNetDevice::SetMtu,
50  MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH))
51  .AddAttribute ("Channel", "The channel attached to this device",
52  PointerValue (),
53  MakePointerAccessor (&WifiNetDevice::DoGetChannel),
54  MakePointerChecker<WifiChannel> ())
55  .AddAttribute ("Phy", "The PHY layer attached to this device.",
56  PointerValue (),
57  MakePointerAccessor (&WifiNetDevice::GetPhy,
59  MakePointerChecker<WifiPhy> ())
60  .AddAttribute ("Mac", "The MAC layer attached to this device.",
61  PointerValue (),
62  MakePointerAccessor (&WifiNetDevice::GetMac,
64  MakePointerChecker<WifiMac> ())
65  .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
66  PointerValue (),
67  MakePointerAccessor (&WifiNetDevice::SetRemoteStationManager,
69  MakePointerChecker<WifiRemoteStationManager> ())
70  ;
71  return tid;
72 }
73 
75  : m_configComplete (false)
76 {
78 }
80 {
82 }
83 
84 void
86 {
88  m_node = 0;
89  m_mac->Dispose ();
90  m_phy->Dispose ();
91  m_stationManager->Dispose ();
92  m_mac = 0;
93  m_phy = 0;
94  m_stationManager = 0;
95  // chain up.
97 }
98 
99 void
101 {
102  m_phy->Initialize ();
103  m_mac->Initialize ();
104  m_stationManager->Initialize ();
106 }
107 
108 void
110 {
111  if (m_mac == 0
112  || m_phy == 0
113  || m_stationManager == 0
114  || m_node == 0
115  || m_configComplete)
116  {
117  return;
118  }
119  m_mac->SetWifiRemoteStationManager (m_stationManager);
120  m_mac->SetWifiPhy (m_phy);
121  m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
122  m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
123  m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
124  m_stationManager->SetupPhy (m_phy);
125  m_configComplete = true;
126 }
127 
128 void
130 {
131  m_mac = mac;
132  CompleteConfig ();
133 }
134 void
136 {
137  m_phy = phy;
138  CompleteConfig ();
139 }
140 void
142 {
143  m_stationManager = manager;
144  CompleteConfig ();
145 }
148 {
149  return m_mac;
150 }
153 {
154  return m_phy;
155 }
158 {
159  return m_stationManager;
160 }
161 
162 void
163 WifiNetDevice::SetIfIndex (const uint32_t index)
164 {
165  m_ifIndex = index;
166 }
167 uint32_t
169 {
170  return m_ifIndex;
171 }
174 {
175  return m_phy->GetChannel ();
176 }
179 {
180  return m_phy->GetChannel ();
181 }
182 void
184 {
185  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
186 }
187 Address
189 {
190  return m_mac->GetAddress ();
191 }
192 bool
193 WifiNetDevice::SetMtu (const uint16_t mtu)
194 {
196  {
197  return false;
198  }
199  m_mtu = mtu;
200  return true;
201 }
202 uint16_t
204 {
205  return m_mtu;
206 }
207 bool
209 {
210  return m_phy != 0 && m_linkUp;
211 }
212 void
214 {
216 }
217 bool
219 {
220  return true;
221 }
222 Address
224 {
225  return Mac48Address::GetBroadcast ();
226 }
227 bool
229 {
230  return true;
231 }
232 Address
234 {
235  return Mac48Address::GetMulticast (multicastGroup);
236 }
238 {
239  return Mac48Address::GetMulticast (addr);
240 }
241 bool
243 {
244  return false;
245 }
246 bool
248 {
249  return false;
250 }
251 bool
252 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
253 {
255 
256  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
257 
258  LlcSnapHeader llc;
259  llc.SetType (protocolNumber);
260  packet->AddHeader (llc);
261 
262  m_mac->NotifyTx (packet);
263  m_mac->Enqueue (packet, realTo);
264  return true;
265 }
266 Ptr<Node>
268 {
269  return m_node;
270 }
271 void
273 {
274  m_node = node;
275  CompleteConfig ();
276 }
277 bool
279 {
280  return true;
281 }
282 void
284 {
285  m_forwardUp = cb;
286 }
287 
288 void
290 {
291  LlcSnapHeader llc;
292  packet->RemoveHeader (llc);
293  enum NetDevice::PacketType type;
294  if (to.IsBroadcast ())
295  {
297  }
298  else if (to.IsGroup ())
299  {
301  }
302  else if (to == m_mac->GetAddress ())
303  {
304  type = NetDevice::PACKET_HOST;
305  }
306  else
307  {
309  }
310 
311  if (type != NetDevice::PACKET_OTHERHOST)
312  {
313  m_mac->NotifyRx (packet);
314  m_forwardUp (this, packet, llc.GetType (), from);
315  }
316 
317  if (!m_promiscRx.IsNull ())
318  {
319  m_mac->NotifyPromiscRx (packet);
320  m_promiscRx (this, packet, llc.GetType (), from, to, type);
321  }
322 }
323 
324 void
326 {
327  m_linkUp = true;
328  m_linkChanges ();
329 }
330 void
332 {
333  m_linkUp = false;
334  m_linkChanges ();
335 }
336 
337 bool
338 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
339 {
342 
343  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
344  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
345 
346  LlcSnapHeader llc;
347  llc.SetType (protocolNumber);
348  packet->AddHeader (llc);
349 
350  m_mac->NotifyTx (packet);
351  m_mac->Enqueue (packet, realTo, realFrom);
352 
353  return true;
354 }
355 
356 void
358 {
359  m_promiscRx = cb;
360  m_mac->SetPromisc();
361 }
362 
363 bool
365 {
366  return m_mac->SupportsSendFrom ();
367 }
368 
369 } // namespace ns3
370 
virtual void SetNode(Ptr< Node > node)
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
static bool IsMatchingType(const Address &address)
virtual Ptr< WifiChannel > GetChannel(void) const =0
Return the WifiChannel this WifiPhy is connected to.
virtual bool SetMtu(const uint16_t mtu)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
void LinkUp(void)
Set that the link is up.
Packet addressed to someone else.
Definition: net-device.h:278
virtual uint16_t GetMtu(void) const
virtual Address GetAddress(void) const
Ptr< WifiRemoteStationManager > m_stationManager
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1014
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
bool IsBroadcast(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
void SetType(uint16_t type)
void ForwardUp(Ptr< Packet > packet, Mac48Address from, Mac48Address to)
Receive a packet from the lower layer and pass the packet up the stack.
virtual Ptr< Channel > GetChannel(void) const
virtual bool IsBroadcast(void) const
a polymophic address class
Definition: address.h:86
void SetMac(Ptr< WifiMac > mac)
virtual bool IsMulticast(void) const
Ptr< WifiPhy > GetPhy(void) const
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Ptr< WifiRemoteStationManager > GetRemoteStationManager(void) const
static Mac48Address GetMulticast(Ipv4Address address)
uint16_t GetType(void)
void SetPhy(Ptr< WifiPhy > phy)
Hold an unsigned integer type.
Definition: uinteger.h:46
Ptr< WifiChannel > DoGetChannel(void) const
Return the WifiChannel this device is connected to.
Ptr< WifiMac > m_mac
virtual bool SupportsSendFrom(void) const
Ptr< WifiPhy > m_phy
static Mac48Address GetBroadcast(void)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void SetRemoteStationManager(Ptr< WifiRemoteStationManager > manager)
static Mac48Address ConvertFrom(const Address &address)
static const uint16_t LLC_SNAP_HEADER_LENGTH
The length in octects of the LLC/SNAP header.
virtual Ptr< Node > GetNode(void) const
TracedCallback m_linkChanges
hold objects of type Ptr
Definition: pointer.h:33
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
bool IsGroup(void) const
virtual uint32_t GetIfIndex(void) const
static const uint16_t MAX_MSDU_SIZE
an EUI-48 address
Definition: mac48-address.h:41
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
virtual bool NeedsArp(void) const
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Address GetBroadcast(void) const
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
virtual void SetIfIndex(const uint32_t index)
NetDevice::ReceiveCallback m_forwardUp
void ConnectWithoutContext(const CallbackBase &callback)
Packet addressed oo us.
Definition: net-device.h:272
Network layer to device interface.
Definition: net-device.h:75
virtual bool IsLinkUp(void) const
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:180
virtual void AddLinkChangeCallback(Callback< void > callback)
void CompleteConfig(void)
Complete the configuration of this Wi-Fi device by connecting all lower components (e...
Ptr< WifiMac > GetMac(void) const
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
tuple address
Definition: first.py:37
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
void LinkDown(void)
Set that the link is down (i.e.
NS_LOG_COMPONENT_DEFINE("WifiNetDevice")
Packet addressed to multicast group.
Definition: net-device.h:276
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:205
Packet addressed to all.
Definition: net-device.h:274
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
static TypeId GetTypeId(void)
NetDevice::PromiscReceiveCallback m_promiscRx
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:343
virtual void SetAddress(Address address)
Set the address of this interface.
Header for the LLC/SNAP encapsulation.