00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "ns3/assert.h"
00021 #include "ns3/log.h"
00022 #include "ns3/simulator.h"
00023 #include "ns3/node.h"
00024
00025 #include "nqap-wifi-mac.h"
00026 #include "dca-txop.h"
00027 #include "wifi-mac-header.h"
00028 #include "mgt-headers.h"
00029 #include "wifi-phy.h"
00030 #include "dcf-manager.h"
00031 #include "mac-rx-middle.h"
00032 #include "mac-low.h"
00033
00034 NS_LOG_COMPONENT_DEFINE ("NqapWifiMac");
00035
00036 namespace ns3 {
00037
00038 NS_OBJECT_ENSURE_REGISTERED (NqapWifiMac);
00039
00040 #undef NS_LOG_APPEND_CONTEXT
00041 #define NS_LOG_APPEND_CONTEXT if (m_low != 0) {std::clog << "[mac=" << m_low->GetAddress () << "] ";}
00042
00043 TypeId
00044 NqapWifiMac::GetTypeId (void)
00045 {
00046 static TypeId tid = TypeId ("ns3::NqapWifiMac")
00047 .SetParent<WifiMac> ()
00048 .AddConstructor<NqapWifiMac> ()
00049 .AddAttribute ("BeaconInterval", "Delay between two beacons",
00050 TimeValue (Seconds (0.1)),
00051 MakeTimeAccessor (&NqapWifiMac::GetBeaconInterval,
00052 &NqapWifiMac::SetBeaconInterval),
00053 MakeTimeChecker ())
00054 .AddAttribute ("BeaconGeneration", "Whether or not beacons are generated.",
00055 BooleanValue (true),
00056 MakeBooleanAccessor (&NqapWifiMac::SetBeaconGeneration,
00057 &NqapWifiMac::GetBeaconGeneration),
00058 MakeBooleanChecker ())
00059 ;
00060 return tid;
00061 }
00062
00063 NqapWifiMac::NqapWifiMac ()
00064 {
00065 NS_LOG_FUNCTION (this);
00066 m_rxMiddle = new MacRxMiddle ();
00067 m_rxMiddle->SetForwardCallback (MakeCallback (&NqapWifiMac::Receive, this));
00068
00069 m_low = CreateObject<MacLow> ();
00070 m_low->SetRxCallback (MakeCallback (&MacRxMiddle::Receive, m_rxMiddle));
00071
00072 m_dcfManager = new DcfManager ();
00073 m_dcfManager->SetupLowListener (m_low);
00074
00075 m_dca = CreateObject<DcaTxop> ();
00076 m_dca->SetLow (m_low);
00077 m_dca->SetManager (m_dcfManager);
00078 m_dca->SetTxOkCallback (MakeCallback (&NqapWifiMac::TxOk, this));
00079 m_dca->SetTxFailedCallback (MakeCallback (&NqapWifiMac::TxFailed, this));
00080
00081 m_beaconDca = CreateObject<DcaTxop> ();
00082 m_beaconDca->SetAifsn(1);
00083 m_beaconDca->SetMinCw(0);
00084 m_beaconDca->SetMaxCw(0);
00085 m_beaconDca->SetLow (m_low);
00086 m_beaconDca->SetManager (m_dcfManager);
00087 }
00088 NqapWifiMac::~NqapWifiMac ()
00089 {
00090 NS_LOG_FUNCTION (this);
00091 }
00092
00093 void
00094 NqapWifiMac::DoDispose (void)
00095 {
00096 NS_LOG_FUNCTION (this);
00097 delete m_rxMiddle;
00098 delete m_dcfManager;
00099 m_rxMiddle = 0;
00100 m_low = 0;
00101 m_dcfManager = 0;
00102 m_phy = 0;
00103 m_dca = 0;
00104 m_beaconDca = 0;
00105 m_beaconEvent.Cancel ();
00106 WifiMac::DoDispose ();
00107 }
00108
00109 void
00110 NqapWifiMac::SetBeaconGeneration (bool enable)
00111 {
00112 NS_LOG_FUNCTION (this << enable);
00113 if (enable)
00114 {
00115 m_beaconEvent = Simulator::ScheduleNow (&NqapWifiMac::SendOneBeacon, this);
00116 }
00117 else
00118 {
00119 m_beaconEvent.Cancel ();
00120 }
00121 }
00122
00123 bool
00124 NqapWifiMac::GetBeaconGeneration (void) const
00125 {
00126 return m_beaconEvent.IsRunning ();
00127 }
00128 Time
00129 NqapWifiMac::GetBeaconInterval (void) const
00130 {
00131 return m_beaconInterval;
00132 }
00133
00134 void
00135 NqapWifiMac::SetSlot (Time slotTime)
00136 {
00137 NS_LOG_FUNCTION (this << slotTime);
00138 m_dcfManager->SetSlot (slotTime);
00139 m_low->SetSlotTime (slotTime);
00140 }
00141 void
00142 NqapWifiMac::SetSifs (Time sifs)
00143 {
00144 NS_LOG_FUNCTION (this << sifs);
00145 m_dcfManager->SetSifs (sifs);
00146 m_low->SetSifs (sifs);
00147 }
00148 void
00149 NqapWifiMac::SetEifsNoDifs (Time eifsNoDifs)
00150 {
00151 NS_LOG_FUNCTION (this << eifsNoDifs);
00152 m_dcfManager->SetEifsNoDifs (eifsNoDifs);
00153 m_eifsNoDifs = eifsNoDifs;
00154 }
00155 void
00156 NqapWifiMac::SetAckTimeout (Time ackTimeout)
00157 {
00158 m_low->SetAckTimeout (ackTimeout);
00159 }
00160 void
00161 NqapWifiMac::SetCtsTimeout (Time ctsTimeout)
00162 {
00163 m_low->SetCtsTimeout (ctsTimeout);
00164 }
00165 void
00166 NqapWifiMac::SetPifs (Time pifs)
00167 {
00168 m_low->SetPifs (pifs);
00169 }
00170 Time
00171 NqapWifiMac::GetSlot (void) const
00172 {
00173 return m_low->GetSlotTime ();
00174 }
00175 Time
00176 NqapWifiMac::GetSifs (void) const
00177 {
00178 return m_low->GetSifs ();
00179 }
00180 Time
00181 NqapWifiMac::GetEifsNoDifs (void) const
00182 {
00183 return m_eifsNoDifs;
00184 }
00185 Time
00186 NqapWifiMac::GetAckTimeout (void) const
00187 {
00188 return m_low->GetAckTimeout ();
00189 }
00190 Time
00191 NqapWifiMac::GetCtsTimeout (void) const
00192 {
00193 return m_low->GetCtsTimeout ();
00194 }
00195 Time
00196 NqapWifiMac::GetPifs (void) const
00197 {
00198 return m_low->GetPifs ();
00199 }
00200 void
00201 NqapWifiMac::SetWifiPhy (Ptr<WifiPhy> phy)
00202 {
00203 NS_LOG_FUNCTION (this << phy);
00204 m_phy = phy;
00205 m_dcfManager->SetupPhyListener (phy);
00206 m_low->SetPhy (phy);
00207 }
00208 void
00209 NqapWifiMac::SetWifiRemoteStationManager (Ptr<WifiRemoteStationManager> stationManager)
00210 {
00211 NS_LOG_FUNCTION (this << stationManager);
00212 m_stationManager = stationManager;
00213 m_dca->SetWifiRemoteStationManager (stationManager);
00214 m_beaconDca->SetWifiRemoteStationManager (stationManager);
00215 m_low->SetWifiRemoteStationManager (stationManager);
00216 }
00217 void
00218 NqapWifiMac::SetForwardUpCallback (Callback<void,Ptr<Packet>, Mac48Address, Mac48Address> upCallback)
00219 {
00220 NS_LOG_FUNCTION (this);
00221 m_upCallback = upCallback;
00222 }
00223 void
00224 NqapWifiMac::SetLinkUpCallback (Callback<void> linkUp)
00225 {
00226 NS_LOG_FUNCTION (this);
00227 if (!linkUp.IsNull ())
00228 {
00229 linkUp ();
00230 }
00231 }
00232 void
00233 NqapWifiMac::SetLinkDownCallback (Callback<void> linkDown)
00234 {
00235 NS_LOG_FUNCTION (this);
00236 }
00237 Mac48Address
00238 NqapWifiMac::GetAddress (void) const
00239 {
00240 return m_low->GetAddress ();
00241 }
00242 Ssid
00243 NqapWifiMac::GetSsid (void) const
00244 {
00245 return m_ssid;
00246 }
00247 void
00248 NqapWifiMac::SetAddress (Mac48Address address)
00249 {
00250 NS_LOG_FUNCTION (address);
00251 m_low->SetAddress (address);
00252 m_low->SetBssid (address);
00253 }
00254 void
00255 NqapWifiMac::SetSsid (Ssid ssid)
00256 {
00257 NS_LOG_FUNCTION (ssid);
00258 m_ssid = ssid;
00259 }
00260 Mac48Address
00261 NqapWifiMac::GetBssid (void) const
00262 {
00263 return m_low->GetBssid ();
00264 }
00265
00266
00267 void
00268 NqapWifiMac::SetBeaconInterval (Time interval)
00269 {
00270 NS_LOG_FUNCTION (this << interval);
00271 m_beaconInterval = interval;
00272 }
00273 void
00274 NqapWifiMac::StartBeaconing (void)
00275 {
00276 NS_LOG_FUNCTION (this);
00277 SendOneBeacon ();
00278 }
00279 void
00280 NqapWifiMac::ForwardUp (Ptr<Packet> packet, Mac48Address from, Mac48Address to)
00281 {
00282 NS_LOG_FUNCTION (this << packet << from);
00283 m_upCallback (packet, from, to);
00284 }
00285 void
00286 NqapWifiMac::ForwardDown (Ptr<const Packet> packet, Mac48Address from, Mac48Address to)
00287 {
00288 NS_LOG_FUNCTION (this << packet << from << to);
00289 WifiMacHeader hdr;
00290 hdr.SetTypeData ();
00291 hdr.SetAddr1 (to);
00292 hdr.SetAddr2 (GetAddress ());
00293 hdr.SetAddr3 (from);
00294 hdr.SetDsFrom ();
00295 hdr.SetDsNotTo ();
00296 m_dca->Queue (packet, hdr);
00297 }
00298 void
00299 NqapWifiMac::Enqueue (Ptr<const Packet> packet, Mac48Address to, Mac48Address from)
00300 {
00301 NS_LOG_FUNCTION (this << packet << to << from);
00302 ForwardDown (packet, from, to);
00303 }
00304 void
00305 NqapWifiMac::Enqueue (Ptr<const Packet> packet, Mac48Address to)
00306 {
00307 NS_LOG_FUNCTION (this << packet << to);
00308 ForwardDown (packet, m_low->GetAddress (), to);
00309 }
00310 bool
00311 NqapWifiMac::SupportsSendFrom (void) const
00312 {
00313 return true;
00314 }
00315 SupportedRates
00316 NqapWifiMac::GetSupportedRates (void) const
00317 {
00318
00319
00320 SupportedRates rates;
00321 for (uint32_t i = 0; i < m_phy->GetNModes (); i++)
00322 {
00323 WifiMode mode = m_phy->GetMode (i);
00324 rates.AddSupportedRate (mode.GetDataRate ());
00325 }
00326
00327 for (uint32_t j = 0; j < m_stationManager->GetNBasicModes (); j++)
00328 {
00329 WifiMode mode = m_stationManager->GetBasicMode (j);
00330 rates.SetBasicRate (mode.GetDataRate ());
00331 }
00332 return rates;
00333 }
00334 void
00335 NqapWifiMac::SendProbeResp (Mac48Address to)
00336 {
00337 NS_LOG_FUNCTION (this << to);
00338 WifiMacHeader hdr;
00339 hdr.SetProbeResp ();
00340 hdr.SetAddr1 (to);
00341 hdr.SetAddr2 (GetAddress ());
00342 hdr.SetAddr3 (GetAddress ());
00343 hdr.SetDsNotFrom ();
00344 hdr.SetDsNotTo ();
00345 Ptr<Packet> packet = Create<Packet> ();
00346 MgtProbeResponseHeader probe;
00347 probe.SetSsid (GetSsid ());
00348 probe.SetSupportedRates (GetSupportedRates ());
00349 probe.SetBeaconIntervalUs (m_beaconInterval.GetMicroSeconds ());
00350 packet->AddHeader (probe);
00351
00352 m_dca->Queue (packet, hdr);
00353 }
00354 void
00355 NqapWifiMac::SendAssocResp (Mac48Address to, bool success)
00356 {
00357 NS_LOG_FUNCTION (this << to << success);
00358 WifiMacHeader hdr;
00359 hdr.SetAssocResp ();
00360 hdr.SetAddr1 (to);
00361 hdr.SetAddr2 (GetAddress ());
00362 hdr.SetAddr3 (GetAddress ());
00363 hdr.SetDsNotFrom ();
00364 hdr.SetDsNotTo ();
00365 Ptr<Packet> packet = Create<Packet> ();
00366 MgtAssocResponseHeader assoc;
00367 StatusCode code;
00368 if (success)
00369 {
00370 code.SetSuccess ();
00371 }
00372 else
00373 {
00374 code.SetFailure ();
00375 }
00376 assoc.SetSupportedRates (GetSupportedRates ());
00377 assoc.SetStatusCode (code);
00378 packet->AddHeader (assoc);
00379
00380 m_dca->Queue (packet, hdr);
00381 }
00382 void
00383 NqapWifiMac::SendOneBeacon (void)
00384 {
00385 NS_LOG_FUNCTION (this);
00386 WifiMacHeader hdr;
00387 hdr.SetBeacon ();
00388 hdr.SetAddr1 (Mac48Address::GetBroadcast ());
00389 hdr.SetAddr2 (GetAddress ());
00390 hdr.SetAddr3 (GetAddress ());
00391 hdr.SetDsNotFrom ();
00392 hdr.SetDsNotTo ();
00393 Ptr<Packet> packet = Create<Packet> ();
00394 MgtBeaconHeader beacon;
00395 beacon.SetSsid (GetSsid ());
00396 beacon.SetSupportedRates (GetSupportedRates ());
00397 beacon.SetBeaconIntervalUs (m_beaconInterval.GetMicroSeconds ());
00398 packet->AddHeader (beacon);
00399
00400 m_beaconDca->Queue (packet, hdr);
00401 m_beaconEvent = Simulator::Schedule (m_beaconInterval, &NqapWifiMac::SendOneBeacon, this);
00402 }
00403 void
00404 NqapWifiMac::TxOk (WifiMacHeader const &hdr)
00405 {
00406 NS_LOG_FUNCTION (this);
00407 WifiRemoteStation *station = m_stationManager->Lookup (hdr.GetAddr1 ());
00408 if (hdr.IsAssocResp () &&
00409 station->IsWaitAssocTxOk ())
00410 {
00411 NS_LOG_DEBUG ("associated with sta="<<hdr.GetAddr1 ());
00412 station->RecordGotAssocTxOk ();
00413 }
00414 }
00415 void
00416 NqapWifiMac::TxFailed (WifiMacHeader const &hdr)
00417 {
00418 NS_LOG_FUNCTION (this);
00419 WifiRemoteStation *station = m_stationManager->Lookup (hdr.GetAddr1 ());
00420 if (hdr.IsAssocResp () &&
00421 station->IsWaitAssocTxOk ())
00422 {
00423 NS_LOG_DEBUG ("assoc failed with sta="<<hdr.GetAddr1 ());
00424 station->RecordGotAssocTxFailed ();
00425 }
00426 }
00427 void
00428 NqapWifiMac::Receive (Ptr<Packet> packet, WifiMacHeader const *hdr)
00429 {
00430 NS_LOG_FUNCTION (this << packet << hdr);
00431
00432 Mac48Address from = hdr->GetAddr2 ();
00433 WifiRemoteStation *fromStation = m_stationManager->Lookup (from);
00434
00435 if (hdr->IsData ())
00436 {
00437 Mac48Address bssid = hdr->GetAddr1 ();
00438 if (!hdr->IsFromDs () &&
00439 hdr->IsToDs () &&
00440 bssid == GetAddress () &&
00441 fromStation->IsAssociated ())
00442 {
00443 Mac48Address to = hdr->GetAddr3 ();
00444 WifiRemoteStation *toStation = m_stationManager->Lookup (to);
00445 if (to == GetAddress ())
00446 {
00447 NS_LOG_DEBUG ("frame for me from="<<from);
00448 ForwardUp (packet, from, bssid);
00449 }
00450 else if (to.IsBroadcast () ||
00451 to.IsMulticast () ||
00452 toStation->IsAssociated ())
00453 {
00454 NS_LOG_DEBUG ("forwarding frame from="<<from<<", to="<<to);
00455 Ptr<Packet> copy = packet->Copy ();
00456 ForwardDown (packet,
00457 from,
00458 to);
00459 ForwardUp (copy, from, to);
00460 }
00461 else
00462 {
00463 ForwardUp (packet, from, to);
00464 }
00465 }
00466 else if (hdr->IsFromDs () &&
00467 hdr->IsToDs ())
00468 {
00469
00470
00471 }
00472 else
00473 {
00474
00475
00476 }
00477 }
00478 else if (hdr->IsMgt ())
00479 {
00480 if (hdr->IsProbeReq ())
00481 {
00482 NS_ASSERT (hdr->GetAddr1 ().IsBroadcast ());
00483 SendProbeResp (hdr->GetAddr2 ());
00484 }
00485 else if (hdr->GetAddr1 () == GetAddress ())
00486 {
00487 if (hdr->IsAssocReq ())
00488 {
00489
00490
00491 MgtAssocRequestHeader assocReq;
00492 packet->RemoveHeader (assocReq);
00493 SupportedRates rates = assocReq.GetSupportedRates ();
00494 bool problem = false;
00495 for (uint32_t i = 0; i < m_stationManager->GetNBasicModes (); i++)
00496 {
00497 WifiMode mode = m_stationManager->GetBasicMode (i);
00498 if (!rates.IsSupportedRate (mode.GetDataRate ()))
00499 {
00500 problem = true;
00501 break;
00502 }
00503 }
00504 if (problem)
00505 {
00506
00507
00508
00509 SendAssocResp (hdr->GetAddr2 (), false);
00510 }
00511 else
00512 {
00513
00514
00515 for (uint32_t j = 0; j < m_phy->GetNModes (); j++)
00516 {
00517 WifiMode mode = m_phy->GetMode (j);
00518 if (rates.IsSupportedRate (mode.GetDataRate ()))
00519 {
00520 fromStation->AddSupportedMode (mode);
00521 }
00522 }
00523 fromStation->RecordWaitAssocTxOk ();
00524
00525 SendAssocResp (hdr->GetAddr2 (), true);
00526 }
00527 }
00528 else if (hdr->IsDisassociation ())
00529 {
00530 fromStation->RecordDisassociated ();
00531 }
00532 else if (hdr->IsReassocReq ())
00533 {
00534
00535 }
00536 else if (hdr->IsAuthentication () ||
00537 hdr->IsDeauthentication ())
00538 {
00539
00540
00541 }
00542 else
00543 {
00544
00545
00546 }
00547 }
00548 }
00549 else
00550 {
00551
00552
00553
00554
00555 NS_ASSERT (false);
00556 }
00557 }
00558
00559 }