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 #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 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
36 
37 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::WifiNetDevice")
43  .SetParent<NetDevice> ()
44  .AddConstructor<WifiNetDevice> ()
45  .SetGroupName ("Wifi")
46  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
50  MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH))
51  .AddAttribute ("Channel", "The channel attached to this device",
52  PointerValue (),
54  MakePointerChecker<WifiChannel> ())
55  .AddAttribute ("Phy", "The PHY layer attached to this device.",
56  PointerValue (),
59  MakePointerChecker<WifiPhy> ())
60  .AddAttribute ("Mac", "The MAC layer attached to this device.",
61  PointerValue (),
64  MakePointerChecker<WifiMac> ())
65  .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
66  PointerValue (),
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 ();
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 ();
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));
126  m_configComplete = true;
127 }
128 
129 void
131 {
132  m_mac = mac;
133  CompleteConfig ();
134 }
135 void
137 {
138  m_phy = phy;
139  CompleteConfig ();
140 }
141 void
143 {
144  m_stationManager = manager;
145  CompleteConfig ();
146 }
149 {
150  return m_mac;
151 }
154 {
155  return m_phy;
156 }
159 {
160  return m_stationManager;
161 }
162 
163 void
164 WifiNetDevice::SetIfIndex (const uint32_t index)
165 {
166  m_ifIndex = index;
167 }
168 uint32_t
170 {
171  return m_ifIndex;
172 }
175 {
176  return m_phy->GetChannel ();
177 }
180 {
181  return m_phy->GetChannel ();
182 }
183 void
185 {
186  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
187 }
188 Address
190 {
191  return m_mac->GetAddress ();
192 }
193 bool
194 WifiNetDevice::SetMtu (const uint16_t mtu)
195 {
197  {
198  return false;
199  }
200  m_mtu = mtu;
201  return true;
202 }
203 uint16_t
205 {
206  return m_mtu;
207 }
208 bool
210 {
211  return m_phy != 0 && m_linkUp;
212 }
213 void
215 {
217 }
218 bool
220 {
221  return true;
222 }
223 Address
225 {
226  return Mac48Address::GetBroadcast ();
227 }
228 bool
230 {
231  return true;
232 }
233 Address
235 {
236  return Mac48Address::GetMulticast (multicastGroup);
237 }
239 {
240  return Mac48Address::GetMulticast (addr);
241 }
242 bool
244 {
245  return false;
246 }
247 bool
249 {
250  return false;
251 }
252 bool
253 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
254 {
256 
257  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
258 
259  LlcSnapHeader llc;
260  llc.SetType (protocolNumber);
261  packet->AddHeader (llc);
262 
263  m_mac->NotifyTx (packet);
264  m_mac->Enqueue (packet, realTo);
265  return true;
266 }
267 Ptr<Node>
269 {
270  return m_node;
271 }
272 void
274 {
275  m_node = node;
276  CompleteConfig ();
277 }
278 bool
280 {
281  return true;
282 }
283 void
285 {
286  m_forwardUp = cb;
287 }
288 
289 void
291 {
292  LlcSnapHeader llc;
293  packet->RemoveHeader (llc);
294  enum NetDevice::PacketType type;
295  if (to.IsBroadcast ())
296  {
298  }
299  else if (to.IsGroup ())
300  {
302  }
303  else if (to == m_mac->GetAddress ())
304  {
305  type = NetDevice::PACKET_HOST;
306  }
307  else
308  {
310  }
311 
312  if (type != NetDevice::PACKET_OTHERHOST)
313  {
314  m_mac->NotifyRx (packet);
315  m_forwardUp (this, packet, llc.GetType (), from);
316  }
317 
318  if (!m_promiscRx.IsNull ())
319  {
320  m_mac->NotifyPromiscRx (packet);
321  m_promiscRx (this, packet, llc.GetType (), from, to, type);
322  }
323 }
324 
325 void
327 {
328  m_linkUp = true;
329  m_linkChanges ();
330 }
331 void
333 {
334  m_linkUp = false;
335  m_linkChanges ();
336 }
337 
338 bool
339 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
340 {
343 
344  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
345  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
346 
347  LlcSnapHeader llc;
348  llc.SetType (protocolNumber);
349  packet->AddHeader (llc);
350 
351  m_mac->NotifyTx (packet);
352  m_mac->Enqueue (packet, realTo, realFrom);
353 
354  return true;
355 }
356 
357 void
359 {
360  m_promiscRx = cb;
361  m_mac->SetPromisc();
362 }
363 
364 bool
366 {
367  return m_mac->SupportsSendFrom ();
368 }
369 
370 } // namespace ns3
371 
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: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:1078
#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:272
#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.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
Packet addressed to multicast group.
Definition: net-device.h:278
#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:274
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
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:210
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:1296
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:280
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:47
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:75
Packet addressed to all.
Definition: net-device.h:276
virtual bool IsLinkUp(void) const
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
void Dispose(void)
Dispose of this Object.
Definition: object.cc:208
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)
Initialize() implementation.
Definition: object.cc:346
virtual void SetAddress(Address address)
Set the address of this interface.
Header for the LLC/SNAP encapsulation.