1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2005,2006 INRIA
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;
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.
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
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20 #include "wifi-net-device.h"
23 #include "wifi-remote-station-manager.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"
30 #include "ns3/trace-source-accessor.h"
34 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
37 WifiNetDevice::GetTypeId (void)
39 static TypeId tid = TypeId ("ns3::WifiNetDevice")
40 .SetParent<NetDevice> ()
41 .AddAttribute ("Channel", "The channel attached to this device",
43 MakePointerAccessor (&WifiNetDevice::DoGetChannel,
44 &WifiNetDevice::SetChannel),
45 MakePointerChecker<WifiChannel> ())
46 .AddAttribute ("Phy", "The PHY layer attached to this device.",
48 MakePointerAccessor (&WifiNetDevice::GetPhy,
49 &WifiNetDevice::SetPhy),
50 MakePointerChecker<WifiPhy> ())
51 .AddAttribute ("Mac", "The MAC layer attached to this device.",
53 MakePointerAccessor (&WifiNetDevice::GetMac,
54 &WifiNetDevice::SetMac),
55 MakePointerChecker<WifiMac> ())
56 .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
58 MakePointerAccessor (&WifiNetDevice::SetRemoteStationManager,
59 &WifiNetDevice::GetRemoteStationManager),
60 MakePointerChecker<WifiRemoteStationManager> ())
61 .AddTraceSource ("Rx", "Received payload from the MAC layer.",
62 MakeTraceSourceAccessor (&WifiNetDevice::m_rxLogger))
63 .AddTraceSource ("Tx", "Send payload to the MAC layer.",
64 MakeTraceSourceAccessor (&WifiNetDevice::m_txLogger))
69 WifiNetDevice::WifiNetDevice ()
72 WifiNetDevice::~WifiNetDevice ()
76 WifiNetDevice::DoDispose (void)
81 m_stationManager->Dispose ();
87 NetDevice::DoDispose ();
91 WifiNetDevice::SetMac (Ptr<WifiMac> mac)
96 if (m_stationManager != 0)
98 m_mac->SetWifiRemoteStationManager (m_stationManager);
102 m_mac->SetWifiPhy (m_phy);
104 m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
105 m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
106 m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
110 WifiNetDevice::SetPhy (Ptr<WifiPhy> phy)
117 m_channel->Add (this, m_phy);
118 m_phy->SetChannel (m_channel);
120 if (m_stationManager != 0)
122 m_stationManager->SetupPhy (m_phy);
126 m_mac->SetWifiPhy (m_phy);
131 WifiNetDevice::SetRemoteStationManager (Ptr<WifiRemoteStationManager> manager)
133 m_stationManager = manager;
134 if (m_stationManager != 0)
138 m_stationManager->SetupPhy (m_phy);
142 m_mac->SetWifiRemoteStationManager (m_stationManager);
147 WifiNetDevice::SetChannel (Ptr<WifiChannel> channel)
150 if (m_channel != 0 && m_phy != 0)
152 m_channel->Add (this, m_phy);
153 m_phy->SetChannel (m_channel);
157 WifiNetDevice::GetMac (void) const
162 WifiNetDevice::GetPhy (void) const
166 Ptr<WifiRemoteStationManager>
167 WifiNetDevice::GetRemoteStationManager (void) const
169 return m_stationManager;
173 WifiNetDevice::SetName(const std::string name)
178 WifiNetDevice::GetName(void) const
183 WifiNetDevice::SetIfIndex(const uint32_t index)
188 WifiNetDevice::GetIfIndex(void) const
193 WifiNetDevice::GetChannel (void) const
198 WifiNetDevice::DoGetChannel (void) const
203 WifiNetDevice::GetAddress (void) const
205 return m_mac->GetAddress ();
208 WifiNetDevice::SetMtu (const uint16_t mtu)
210 UintegerValue maxMsduSize;
211 m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
212 if (mtu > maxMsduSize.Get () || mtu == 0)
220 WifiNetDevice::GetMtu (void) const
224 UintegerValue maxMsduSize;
225 m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
226 m_mtu = maxMsduSize.Get ();
231 WifiNetDevice::IsLinkUp (void) const
233 return m_phy != 0 && m_linkUp;
236 WifiNetDevice::SetLinkChangeCallback (Callback<void> callback)
238 m_linkChange = callback;
241 WifiNetDevice::IsBroadcast (void) const
246 WifiNetDevice::GetBroadcast (void) const
248 return Mac48Address::GetBroadcast ();
251 WifiNetDevice::IsMulticast (void) const
256 WifiNetDevice::GetMulticast (Ipv4Address multicastGroup) const
258 return Mac48Address::GetMulticast (multicastGroup);
260 Address WifiNetDevice::GetMulticast (Ipv6Address addr) const
262 return Mac48Address::GetMulticast (addr);
265 WifiNetDevice::IsPointToPoint (void) const
270 WifiNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
272 NS_ASSERT (Mac48Address::IsMatchingType (dest));
274 Mac48Address realTo = Mac48Address::ConvertFrom (dest);
277 llc.SetType (protocolNumber);
278 packet->AddHeader (llc);
280 m_txLogger (packet, realTo);
282 m_mac->Enqueue (packet, realTo);
286 WifiNetDevice::GetNode (void) const
291 WifiNetDevice::SetNode (Ptr<Node> node)
296 WifiNetDevice::NeedsArp (void) const
301 WifiNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
307 WifiNetDevice::ForwardUp (Ptr<Packet> packet, Mac48Address from, Mac48Address to)
309 m_rxLogger (packet, from);
311 packet->RemoveHeader (llc);
312 enum NetDevice::PacketType type;
313 if (to.IsBroadcast ())
315 type = NetDevice::PACKET_BROADCAST;
317 else if (to.IsMulticast ())
319 type = NetDevice::PACKET_MULTICAST;
321 else if (to == m_mac->GetAddress ())
323 type = NetDevice::PACKET_HOST;
327 type = NetDevice::PACKET_OTHERHOST;
329 if (type != NetDevice::PACKET_OTHERHOST)
331 m_forwardUp (this, packet, llc.GetType (), from);
333 if (!m_promiscRx.IsNull ())
335 m_promiscRx (this, packet, llc.GetType (), from, to, type);
340 WifiNetDevice::LinkUp (void)
343 if (!m_linkChange.IsNull ())
349 WifiNetDevice::LinkDown (void)
352 if (!m_linkChange.IsNull ())
359 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
361 NS_ASSERT (Mac48Address::IsMatchingType (dest));
362 NS_ASSERT (Mac48Address::IsMatchingType (source));
364 Mac48Address realTo = Mac48Address::ConvertFrom (dest);
365 Mac48Address realFrom = Mac48Address::ConvertFrom (source);
368 llc.SetType (protocolNumber);
369 packet->AddHeader (llc);
371 m_txLogger (packet, realTo);
373 m_mac->Enqueue (packet, realTo, realFrom);
379 WifiNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
385 WifiNetDevice::SupportsSendFrom (void) const
387 return m_mac->SupportsSendFrom ();