00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "wifi-net-device.h"
00021 #include "wifi-mac.h"
00022 #include "wifi-phy.h"
00023 #include "wifi-remote-station-manager.h"
00024 #include "wifi-channel.h"
00025 #include "ns3/llc-snap-header.h"
00026 #include "ns3/packet.h"
00027 #include "ns3/uinteger.h"
00028 #include "ns3/pointer.h"
00029 #include "ns3/node.h"
00030 #include "ns3/trace-source-accessor.h"
00031
00032 namespace ns3 {
00033
00034 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
00035
00036 TypeId
00037 WifiNetDevice::GetTypeId (void)
00038 {
00039 static TypeId tid = TypeId ("ns3::WifiNetDevice")
00040 .SetParent<NetDevice> ()
00041 .AddAttribute ("Channel", "The channel attached to this device",
00042 PointerValue (),
00043 MakePointerAccessor (&WifiNetDevice::DoGetChannel),
00044 MakePointerChecker<WifiChannel> ())
00045 .AddAttribute ("Phy", "The PHY layer attached to this device.",
00046 PointerValue (),
00047 MakePointerAccessor (&WifiNetDevice::GetPhy,
00048 &WifiNetDevice::SetPhy),
00049 MakePointerChecker<WifiPhy> ())
00050 .AddAttribute ("Mac", "The MAC layer attached to this device.",
00051 PointerValue (),
00052 MakePointerAccessor (&WifiNetDevice::GetMac,
00053 &WifiNetDevice::SetMac),
00054 MakePointerChecker<WifiMac> ())
00055 .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
00056 PointerValue (),
00057 MakePointerAccessor (&WifiNetDevice::SetRemoteStationManager,
00058 &WifiNetDevice::GetRemoteStationManager),
00059 MakePointerChecker<WifiRemoteStationManager> ())
00060 .AddTraceSource ("Rx", "Received payload from the MAC layer.",
00061 MakeTraceSourceAccessor (&WifiNetDevice::m_rxLogger))
00062 .AddTraceSource ("Tx", "Send payload to the MAC layer.",
00063 MakeTraceSourceAccessor (&WifiNetDevice::m_txLogger))
00064 ;
00065 return tid;
00066 }
00067
00068 WifiNetDevice::WifiNetDevice ()
00069 : m_mtu (0),
00070 m_configComplete (false)
00071 {}
00072 WifiNetDevice::~WifiNetDevice ()
00073 {}
00074
00075 void
00076 WifiNetDevice::DoDispose (void)
00077 {
00078 m_node = 0;
00079 m_mac->Dispose ();
00080 m_phy->Dispose ();
00081 m_stationManager->Dispose ();
00082 m_mac = 0;
00083 m_phy = 0;
00084 m_stationManager = 0;
00085
00086 NetDevice::DoDispose ();
00087 }
00088
00089 void
00090 WifiNetDevice::CompleteConfig (void)
00091 {
00092 if (m_mac == 0 ||
00093 m_phy == 0 ||
00094 m_stationManager == 0 ||
00095 m_node == 0 ||
00096 m_configComplete)
00097 {
00098 return;
00099 }
00100 m_mac->SetWifiRemoteStationManager (m_stationManager);
00101 m_mac->SetWifiPhy (m_phy);
00102 m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
00103 m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
00104 m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
00105 m_stationManager->SetupPhy (m_phy);
00106 m_configComplete = true;
00107 }
00108
00109 void
00110 WifiNetDevice::SetMac (Ptr<WifiMac> mac)
00111 {
00112 m_mac = mac;
00113 CompleteConfig ();
00114 }
00115 void
00116 WifiNetDevice::SetPhy (Ptr<WifiPhy> phy)
00117 {
00118 m_phy = phy;
00119 CompleteConfig ();
00120 }
00121 void
00122 WifiNetDevice::SetRemoteStationManager (Ptr<WifiRemoteStationManager> manager)
00123 {
00124 m_stationManager = manager;
00125 CompleteConfig ();
00126 }
00127 Ptr<WifiMac>
00128 WifiNetDevice::GetMac (void) const
00129 {
00130 return m_mac;
00131 }
00132 Ptr<WifiPhy>
00133 WifiNetDevice::GetPhy (void) const
00134 {
00135 return m_phy;
00136 }
00137 Ptr<WifiRemoteStationManager>
00138 WifiNetDevice::GetRemoteStationManager (void) const
00139 {
00140 return m_stationManager;
00141 }
00142
00143 void
00144 WifiNetDevice::SetName(const std::string name)
00145 {
00146 m_name = name;
00147 }
00148 std::string
00149 WifiNetDevice::GetName(void) const
00150 {
00151 return m_name;
00152 }
00153 void
00154 WifiNetDevice::SetIfIndex(const uint32_t index)
00155 {
00156 m_ifIndex = index;
00157 }
00158 uint32_t
00159 WifiNetDevice::GetIfIndex(void) const
00160 {
00161 return m_ifIndex;
00162 }
00163 Ptr<Channel>
00164 WifiNetDevice::GetChannel (void) const
00165 {
00166 return m_phy->GetChannel ();
00167 }
00168 Ptr<WifiChannel>
00169 WifiNetDevice::DoGetChannel (void) const
00170 {
00171 return m_phy->GetChannel ();
00172 }
00173 Address
00174 WifiNetDevice::GetAddress (void) const
00175 {
00176 return m_mac->GetAddress ();
00177 }
00178 bool
00179 WifiNetDevice::SetMtu (const uint16_t mtu)
00180 {
00181 UintegerValue maxMsduSize;
00182 m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
00183 if (mtu > maxMsduSize.Get () || mtu == 0)
00184 {
00185 return false;
00186 }
00187 m_mtu = mtu;
00188 return true;
00189 }
00190 uint16_t
00191 WifiNetDevice::GetMtu (void) const
00192 {
00193 if (m_mtu == 0)
00194 {
00195 UintegerValue maxMsduSize;
00196 m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
00197 m_mtu = maxMsduSize.Get ();
00198 }
00199 return m_mtu;
00200 }
00201 bool
00202 WifiNetDevice::IsLinkUp (void) const
00203 {
00204 return m_phy != 0 && m_linkUp;
00205 }
00206 void
00207 WifiNetDevice::SetLinkChangeCallback (Callback<void> callback)
00208 {
00209 m_linkChange = callback;
00210 }
00211 bool
00212 WifiNetDevice::IsBroadcast (void) const
00213 {
00214 return true;
00215 }
00216 Address
00217 WifiNetDevice::GetBroadcast (void) const
00218 {
00219 return Mac48Address::GetBroadcast ();
00220 }
00221 bool
00222 WifiNetDevice::IsMulticast (void) const
00223 {
00224 return false;
00225 }
00226 Address
00227 WifiNetDevice::GetMulticast (Ipv4Address multicastGroup) const
00228 {
00229 return Mac48Address::GetMulticast (multicastGroup);
00230 }
00231 Address WifiNetDevice::GetMulticast (Ipv6Address addr) const
00232 {
00233 return Mac48Address::GetMulticast (addr);
00234 }
00235 bool
00236 WifiNetDevice::IsPointToPoint (void) const
00237 {
00238 return false;
00239 }
00240 bool
00241 WifiNetDevice::IsBridge (void) const
00242 {
00243 return false;
00244 }
00245 bool
00246 WifiNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
00247 {
00248 NS_ASSERT (Mac48Address::IsMatchingType (dest));
00249
00250 Mac48Address realTo = Mac48Address::ConvertFrom (dest);
00251
00252 LlcSnapHeader llc;
00253 llc.SetType (protocolNumber);
00254 packet->AddHeader (llc);
00255
00256 m_txLogger (packet, realTo);
00257
00258 m_mac->Enqueue (packet, realTo);
00259 return true;
00260 }
00261 Ptr<Node>
00262 WifiNetDevice::GetNode (void) const
00263 {
00264 return m_node;
00265 }
00266 void
00267 WifiNetDevice::SetNode (Ptr<Node> node)
00268 {
00269 m_node = node;
00270 CompleteConfig ();
00271 }
00272 bool
00273 WifiNetDevice::NeedsArp (void) const
00274 {
00275 return true;
00276 }
00277 void
00278 WifiNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
00279 {
00280 m_forwardUp = cb;
00281 }
00282
00283 void
00284 WifiNetDevice::ForwardUp (Ptr<Packet> packet, Mac48Address from, Mac48Address to)
00285 {
00286 m_rxLogger (packet, from);
00287 LlcSnapHeader llc;
00288 packet->RemoveHeader (llc);
00289 enum NetDevice::PacketType type;
00290 if (to.IsBroadcast ())
00291 {
00292 type = NetDevice::PACKET_BROADCAST;
00293 }
00294 else if (to.IsMulticast ())
00295 {
00296 type = NetDevice::PACKET_MULTICAST;
00297 }
00298 else if (to == m_mac->GetAddress ())
00299 {
00300 type = NetDevice::PACKET_HOST;
00301 }
00302 else
00303 {
00304 type = NetDevice::PACKET_OTHERHOST;
00305 }
00306 if (type != NetDevice::PACKET_OTHERHOST)
00307 {
00308 m_forwardUp (this, packet, llc.GetType (), from);
00309 }
00310 if (!m_promiscRx.IsNull ())
00311 {
00312 m_promiscRx (this, packet, llc.GetType (), from, to, type);
00313 }
00314 }
00315
00316 void
00317 WifiNetDevice::LinkUp (void)
00318 {
00319 m_linkUp = true;
00320 if (!m_linkChange.IsNull ())
00321 {
00322 m_linkChange ();
00323 }
00324 }
00325 void
00326 WifiNetDevice::LinkDown (void)
00327 {
00328 m_linkUp = false;
00329 if (!m_linkChange.IsNull ())
00330 {
00331 m_linkChange ();
00332 }
00333 }
00334
00335 bool
00336 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
00337 {
00338 NS_ASSERT (Mac48Address::IsMatchingType (dest));
00339 NS_ASSERT (Mac48Address::IsMatchingType (source));
00340
00341 Mac48Address realTo = Mac48Address::ConvertFrom (dest);
00342 Mac48Address realFrom = Mac48Address::ConvertFrom (source);
00343
00344 LlcSnapHeader llc;
00345 llc.SetType (protocolNumber);
00346 packet->AddHeader (llc);
00347
00348 m_txLogger (packet, realTo);
00349
00350 m_mac->Enqueue (packet, realTo, realFrom);
00351
00352 return true;
00353 }
00354
00355 void
00356 WifiNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
00357 {
00358 m_promiscRx = cb;
00359 }
00360
00361 bool
00362 WifiNetDevice::SupportsSendFrom (void) const
00363 {
00364 return m_mac->SupportsSendFrom ();
00365 }
00366
00367 }
00368