A Discrete-Event Network Simulator
API
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 
21 #include "wifi-net-device.h"
22 #include "wifi-mac.h"
23 #include "wifi-phy.h"
25 #include "wifi-channel.h"
26 #include "ns3/llc-snap-header.h"
27 #include "ns3/packet.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/pointer.h"
30 #include "ns3/node.h"
31 #include "ns3/trace-source-accessor.h"
32 #include "ns3/log.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
37 
38 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::WifiNetDevice")
44  .SetParent<NetDevice> ()
45  .AddConstructor<WifiNetDevice> ()
46  .SetGroupName ("Wifi")
47  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
51  MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH))
52  .AddAttribute ("Channel", "The channel attached to this device",
53  PointerValue (),
55  MakePointerChecker<WifiChannel> ())
56  .AddAttribute ("Phy", "The PHY layer attached to this device.",
57  PointerValue (),
60  MakePointerChecker<WifiPhy> ())
61  .AddAttribute ("Mac", "The MAC layer attached to this device.",
62  PointerValue (),
65  MakePointerChecker<WifiMac> ())
66  .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
67  PointerValue (),
70  MakePointerChecker<WifiRemoteStationManager> ())
71  ;
72  return tid;
73 }
74 
76  : m_configComplete (false)
77 {
79 }
80 
82 {
84 }
85 
86 void
88 {
90  m_node = 0;
91  m_mac->Dispose ();
92  m_phy->Dispose ();
94  m_mac = 0;
95  m_phy = 0;
96  m_stationManager = 0;
98 }
99 
100 void
102 {
103  m_phy->Initialize ();
104  m_mac->Initialize ();
107 }
108 
109 void
111 {
112  if (m_mac == 0
113  || m_phy == 0
114  || m_stationManager == 0
115  || m_node == 0
116  || m_configComplete)
117  {
118  return;
119  }
120  m_mac->SetWifiRemoteStationManager (m_stationManager);
121  m_mac->SetWifiPhy (m_phy);
122  m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
123  m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
124  m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
127  m_configComplete = true;
128 }
129 
130 void
132 {
133  m_mac = mac;
134  CompleteConfig ();
135 }
136 
137 void
139 {
140  m_phy = phy;
141  CompleteConfig ();
142 }
143 
144 void
146 {
147  m_stationManager = manager;
148  CompleteConfig ();
149 }
150 
153 {
154  return m_mac;
155 }
156 
159 {
160  return m_phy;
161 }
162 
165 {
166  return m_stationManager;
167 }
168 
169 void
170 WifiNetDevice::SetIfIndex (const uint32_t index)
171 {
172  m_ifIndex = index;
173 }
174 
175 uint32_t
177 {
178  return m_ifIndex;
179 }
180 
183 {
184  return m_phy->GetChannel ();
185 }
186 
189 {
190  return m_phy->GetChannel ();
191 }
192 
193 void
195 {
196  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
197 }
198 
199 Address
201 {
202  return m_mac->GetAddress ();
203 }
204 
205 bool
206 WifiNetDevice::SetMtu (const uint16_t mtu)
207 {
209  {
210  return false;
211  }
212  m_mtu = mtu;
213  return true;
214 }
215 
216 uint16_t
218 {
219  return m_mtu;
220 }
221 
222 bool
224 {
225  return m_phy != 0 && m_linkUp;
226 }
227 
228 void
230 {
232 }
233 
234 bool
236 {
237  return true;
238 }
239 
240 Address
242 {
243  return Mac48Address::GetBroadcast ();
244 }
245 
246 bool
248 {
249  return true;
250 }
251 
252 Address
254 {
255  return Mac48Address::GetMulticast (multicastGroup);
256 }
257 
259 {
260  return Mac48Address::GetMulticast (addr);
261 }
262 
263 bool
265 {
266  return false;
267 }
268 
269 bool
271 {
272  return false;
273 }
274 
275 bool
276 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
277 {
279 
280  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
281 
282  LlcSnapHeader llc;
283  llc.SetType (protocolNumber);
284  packet->AddHeader (llc);
285 
286  m_mac->NotifyTx (packet);
287  m_mac->Enqueue (packet, realTo);
288  return true;
289 }
290 
291 Ptr<Node>
293 {
294  return m_node;
295 }
296 
297 void
299 {
300  m_node = node;
301  CompleteConfig ();
302 }
303 
304 bool
306 {
307  return true;
308 }
309 
310 void
312 {
313  m_forwardUp = cb;
314 }
315 
316 void
318 {
319  LlcSnapHeader llc;
320  enum NetDevice::PacketType type;
321  if (to.IsBroadcast ())
322  {
324  }
325  else if (to.IsGroup ())
326  {
328  }
329  else if (to == m_mac->GetAddress ())
330  {
331  type = NetDevice::PACKET_HOST;
332  }
333  else
334  {
336  }
337 
338  if (type != NetDevice::PACKET_OTHERHOST)
339  {
340  m_mac->NotifyRx (packet);
341  packet->RemoveHeader (llc);
342  m_forwardUp (this, packet, llc.GetType (), from);
343  }
344  else
345  {
346  packet->RemoveHeader (llc);
347  }
348 
349  if (!m_promiscRx.IsNull ())
350  {
351  m_mac->NotifyPromiscRx (packet);
352  m_promiscRx (this, packet, llc.GetType (), from, to, type);
353  }
354 }
355 
356 void
358 {
359  m_linkUp = true;
360  m_linkChanges ();
361 }
362 
363 void
365 {
366  m_linkUp = false;
367  m_linkChanges ();
368 }
369 
370 bool
371 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
372 {
375 
376  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
377  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
378 
379  LlcSnapHeader llc;
380  llc.SetType (protocolNumber);
381  packet->AddHeader (llc);
382 
383  m_mac->NotifyTx (packet);
384  m_mac->Enqueue (packet, realTo, realFrom);
385 
386  return true;
387 }
388 
389 void
391 {
392  m_promiscRx = cb;
393  m_mac->SetPromisc ();
394 }
395 
396 bool
398 {
399  return m_mac->SupportsSendFrom ();
400 }
401 
402 } //namespace ns3
virtual void SetNode(Ptr< Node > node)
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
void Dispose(void)
Dispose of this Object.
Definition: object.cc:208
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 void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:346
virtual bool SetMtu(const uint16_t mtu)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void LinkUp(void)
Set that the link is up.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
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:1258
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:538
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool IsBroadcast(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Packet addressed to multicast group.
Definition: net-device.h:544
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
void SetType(uint16_t type)
Set the Ethertype.
Packet addressed oo us.
Definition: net-device.h:540
void ForwardUp(Ptr< Packet > packet, Mac48Address from, Mac48Address to)
Receive a packet from the lower layer and pass the packet up the stack.
virtual void SetupPhy(Ptr< WifiPhy > phy)
Set up PHY associated with this device since it is the object that knows the full set of transmit rat...
virtual Ptr< Channel > GetChannel(void) const
virtual bool IsBroadcast(void) const
a polymophic address class
Definition: address.h:90
void SetMac(Ptr< WifiMac > mac)
virtual bool IsMulticast(void) const
Ptr< WifiPhy > GetPhy(void) const
tuple phy
Definition: third.py:86
virtual void DoDispose(void)
Destructor implementation.
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
Ptr< WifiRemoteStationManager > GetRemoteStationManager(void) const
static Mac48Address GetMulticast(Ipv4Address address)
uint16_t GetType(void)
Return the Ethertype.
void SetPhy(Ptr< WifiPhy > phy)
Hold an unsigned integer type.
Definition: uinteger.h:44
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:1480
tuple mac
Definition: third.py:92
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedCallback m_linkChanges
Hold objects of type Ptr.
Definition: pointer.h:36
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
bool IsGroup(void) const
Packet addressed to someone else.
Definition: net-device.h:546
virtual uint32_t GetIfIndex(void) const
static const uint16_t MAX_MSDU_SIZE
an EUI-48 address
Definition: mac48-address.h:43
virtual void DoInitialize(void)
Initialize() implementation.
virtual bool NeedsArp(void) const
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual void SetupMac(Ptr< WifiMac > mac)
Set up MAC associated with this device since it is the object that knows the full set of timing param...
virtual Address GetBroadcast(void) const
Describes an IPv6 address.
Definition: ipv6-address.h:48
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
virtual void SetIfIndex(const uint32_t index)
NetDevice::ReceiveCallback m_forwardUp
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Network layer to device interface.
Definition: net-device.h:337
Packet addressed to all.
Definition: net-device.h:542
virtual bool IsLinkUp(void) const
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.
void LinkDown(void)
Set that the link is down (i.e.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
static TypeId GetTypeId(void)
NetDevice::PromiscReceiveCallback m_promiscRx
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
virtual void SetAddress(Address address)
Set the address of this interface.
Header for the LLC/SNAP encapsulation.