src/devices/wifi/wifi-net-device.cc
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
parent 3841 1e7abf5fca79
child 3914 18ac5bec5c49
child 3936 e525995ce5dc
permissions -rw-r--r--
Initial IPv6 capability
     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"
    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"
    29 #include "ns3/node.h"
    30 #include "ns3/trace-source-accessor.h"
    31 
    32 namespace ns3 {
    33 
    34 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
    35 
    36 TypeId 
    37 WifiNetDevice::GetTypeId (void)
    38 {
    39   static TypeId tid = TypeId ("ns3::WifiNetDevice")
    40     .SetParent<NetDevice> ()
    41     .AddAttribute ("Channel", "The channel attached to this device",
    42                    PointerValue (),
    43                    MakePointerAccessor (&WifiNetDevice::DoGetChannel,
    44                                         &WifiNetDevice::SetChannel),
    45                    MakePointerChecker<WifiChannel> ())
    46     .AddAttribute ("Phy", "The PHY layer attached to this device.",
    47                    PointerValue (),
    48                    MakePointerAccessor (&WifiNetDevice::GetPhy,
    49                                         &WifiNetDevice::SetPhy),
    50                    MakePointerChecker<WifiPhy> ())
    51     .AddAttribute ("Mac", "The MAC layer attached to this device.",
    52                    PointerValue (),
    53                    MakePointerAccessor (&WifiNetDevice::GetMac,
    54                                         &WifiNetDevice::SetMac),
    55                    MakePointerChecker<WifiMac> ())
    56     .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
    57                    PointerValue (),
    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))
    65     ;
    66   return tid;
    67 }
    68 
    69 WifiNetDevice::WifiNetDevice ()
    70   : m_mtu (0)
    71 {}
    72 WifiNetDevice::~WifiNetDevice ()
    73 {}
    74 
    75 void
    76 WifiNetDevice::DoDispose (void)
    77 {
    78   m_node = 0;
    79   m_mac->Dispose ();
    80   m_phy->Dispose ();
    81   m_stationManager->Dispose ();
    82   m_mac = 0;
    83   m_phy = 0;
    84   m_channel = 0;
    85   m_stationManager = 0;
    86   // chain up.
    87   NetDevice::DoDispose ();
    88 }
    89   
    90 void 
    91 WifiNetDevice::SetMac (Ptr<WifiMac> mac)
    92 {
    93   m_mac = mac;
    94   if (m_mac != 0)
    95     {
    96       if (m_stationManager != 0)
    97         {
    98           m_mac->SetWifiRemoteStationManager (m_stationManager);
    99         }
   100       if (m_phy != 0)
   101         {
   102           m_mac->SetWifiPhy (m_phy);
   103         }
   104       m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
   105       m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
   106       m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
   107     }
   108 }
   109 void 
   110 WifiNetDevice::SetPhy (Ptr<WifiPhy> phy)
   111 {
   112   m_phy = phy;
   113   if (m_phy != 0)
   114     {
   115       if (m_channel != 0)
   116         {
   117           m_channel->Add (this, m_phy);
   118           m_phy->SetChannel (m_channel);
   119         }
   120       if (m_stationManager != 0)
   121         {
   122           m_stationManager->SetupPhy (m_phy);
   123         }
   124       if (m_mac != 0)
   125         {
   126           m_mac->SetWifiPhy (m_phy);
   127         }
   128     }
   129 }
   130 void 
   131 WifiNetDevice::SetRemoteStationManager (Ptr<WifiRemoteStationManager> manager)
   132 {
   133   m_stationManager = manager;
   134   if (m_stationManager != 0)
   135     {
   136       if (m_phy != 0)
   137         {
   138           m_stationManager->SetupPhy (m_phy);
   139         }
   140       if (m_mac != 0)
   141         {
   142           m_mac->SetWifiRemoteStationManager (m_stationManager);
   143         }
   144     }
   145 }
   146 void 
   147 WifiNetDevice::SetChannel (Ptr<WifiChannel> channel)
   148 {
   149   m_channel = channel;
   150   if (m_channel != 0 && m_phy != 0)
   151     {
   152       m_channel->Add (this, m_phy);
   153       m_phy->SetChannel (m_channel);
   154     }
   155 }
   156 Ptr<WifiMac> 
   157 WifiNetDevice::GetMac (void) const
   158 {
   159   return m_mac;
   160 }
   161 Ptr<WifiPhy> 
   162 WifiNetDevice::GetPhy (void) const
   163 {
   164   return m_phy;
   165 }
   166 Ptr<WifiRemoteStationManager> 
   167 WifiNetDevice::GetRemoteStationManager (void) const
   168 {
   169   return m_stationManager;
   170 }
   171 
   172 void 
   173 WifiNetDevice::SetName(const std::string name)
   174 {
   175   m_name = name;
   176 }
   177 std::string 
   178 WifiNetDevice::GetName(void) const
   179 {
   180   return m_name;
   181 }
   182 void 
   183 WifiNetDevice::SetIfIndex(const uint32_t index)
   184 {
   185   m_ifIndex = index;
   186 }
   187 uint32_t 
   188 WifiNetDevice::GetIfIndex(void) const
   189 {
   190   return m_ifIndex;
   191 }
   192 Ptr<Channel> 
   193 WifiNetDevice::GetChannel (void) const
   194 {
   195   return m_channel;
   196 }
   197 Ptr<WifiChannel> 
   198 WifiNetDevice::DoGetChannel (void) const
   199 {
   200   return m_channel;
   201 }
   202 Address 
   203 WifiNetDevice::GetAddress (void) const
   204 {
   205   return m_mac->GetAddress ();
   206 }
   207 bool 
   208 WifiNetDevice::SetMtu (const uint16_t mtu)
   209 {
   210   UintegerValue maxMsduSize;
   211   m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
   212   if (mtu > maxMsduSize.Get () || mtu == 0)
   213     {
   214       return false;
   215     }
   216   m_mtu = mtu;
   217   return true;
   218 }
   219 uint16_t 
   220 WifiNetDevice::GetMtu (void) const
   221 {
   222   if (m_mtu == 0)
   223     {
   224       UintegerValue maxMsduSize;
   225       m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
   226       m_mtu = maxMsduSize.Get ();
   227     }
   228   return m_mtu;
   229 }
   230 bool 
   231 WifiNetDevice::IsLinkUp (void) const
   232 {
   233   return m_phy != 0 && m_linkUp;
   234 }
   235 void 
   236 WifiNetDevice::SetLinkChangeCallback (Callback<void> callback)
   237 {
   238   m_linkChange = callback;
   239 }
   240 bool 
   241 WifiNetDevice::IsBroadcast (void) const
   242 {
   243   return true;
   244 }
   245 Address 
   246 WifiNetDevice::GetBroadcast (void) const
   247 {
   248   return Mac48Address::GetBroadcast ();
   249 }
   250 bool 
   251 WifiNetDevice::IsMulticast (void) const
   252 {
   253   return false;
   254 }
   255 Address 
   256 WifiNetDevice::GetMulticast (Ipv4Address multicastGroup) const
   257 {
   258   return Mac48Address::GetMulticast (multicastGroup);
   259 }
   260 Address WifiNetDevice::GetMulticast (Ipv6Address addr) const
   261 {
   262   return Mac48Address::GetMulticast (addr);
   263 }
   264 bool 
   265 WifiNetDevice::IsPointToPoint (void) const
   266 {
   267   return false;
   268 }
   269 bool 
   270 WifiNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
   271 {
   272   NS_ASSERT (Mac48Address::IsMatchingType (dest));
   273 
   274   Mac48Address realTo = Mac48Address::ConvertFrom (dest);
   275 
   276   LlcSnapHeader llc;
   277   llc.SetType (protocolNumber);
   278   packet->AddHeader (llc);
   279 
   280   m_txLogger (packet, realTo);
   281 
   282   m_mac->Enqueue (packet, realTo);
   283   return true;
   284 }
   285 Ptr<Node> 
   286 WifiNetDevice::GetNode (void) const
   287 {
   288   return m_node;
   289 }
   290 void 
   291 WifiNetDevice::SetNode (Ptr<Node> node)
   292 {
   293   m_node = node;
   294 }
   295 bool 
   296 WifiNetDevice::NeedsArp (void) const
   297 {
   298   return true;
   299 }
   300 void 
   301 WifiNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
   302 {
   303   m_forwardUp = cb;
   304 }
   305 
   306 void
   307 WifiNetDevice::ForwardUp (Ptr<Packet> packet, Mac48Address from, Mac48Address to)
   308 {
   309   m_rxLogger (packet, from);
   310   LlcSnapHeader llc;
   311   packet->RemoveHeader (llc);
   312   enum NetDevice::PacketType type;
   313   if (to.IsBroadcast ())
   314     {
   315       type = NetDevice::PACKET_BROADCAST;
   316     }
   317   else if (to.IsMulticast ())
   318     {
   319       type = NetDevice::PACKET_MULTICAST;
   320     }
   321   else if (to == m_mac->GetAddress ())
   322     {
   323       type = NetDevice::PACKET_HOST;
   324     }
   325   else
   326     {
   327       type = NetDevice::PACKET_OTHERHOST;
   328     }
   329   if (type != NetDevice::PACKET_OTHERHOST)
   330     {
   331       m_forwardUp (this, packet, llc.GetType (), from);
   332     }
   333   if (!m_promiscRx.IsNull ())
   334     {
   335       m_promiscRx (this, packet, llc.GetType (), from, to, type);
   336     }
   337 }
   338 
   339 void
   340 WifiNetDevice::LinkUp (void)
   341 {
   342   m_linkUp = true;
   343   if (!m_linkChange.IsNull ())
   344     {
   345       m_linkChange ();
   346     }
   347 }
   348 void
   349 WifiNetDevice::LinkDown (void)
   350 {
   351   m_linkUp = false;
   352   if (!m_linkChange.IsNull ())
   353     {
   354       m_linkChange ();
   355     }
   356 }
   357 
   358 bool
   359 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
   360 {
   361   NS_ASSERT (Mac48Address::IsMatchingType (dest));
   362   NS_ASSERT (Mac48Address::IsMatchingType (source));
   363 
   364   Mac48Address realTo = Mac48Address::ConvertFrom (dest);
   365   Mac48Address realFrom = Mac48Address::ConvertFrom (source);
   366 
   367   LlcSnapHeader llc;
   368   llc.SetType (protocolNumber);
   369   packet->AddHeader (llc);
   370 
   371   m_txLogger (packet, realTo);
   372 
   373   m_mac->Enqueue (packet, realTo, realFrom);
   374 
   375   return true;
   376 }
   377 
   378 void
   379 WifiNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
   380 {
   381   m_promiscRx = cb;
   382 }
   383 
   384 bool
   385 WifiNetDevice::SupportsSendFrom (void) const
   386 {
   387   return m_mac->SupportsSendFrom ();
   388 }
   389 
   390 } // namespace ns3
   391