avoid crashing AP wifi sample
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sun, 02 Mar 2008 01:53:26 +0100
changeset 2528 a527ec47b756
parent 2527 75df305d02d9
child 2529 d5f8bee5fcbd
avoid crashing AP wifi sample
samples/main-ap-wifi.cc
samples/wscript
src/devices/wifi/adhoc-wifi-mac.cc
src/devices/wifi/nqap-wifi-mac.cc
src/devices/wifi/nqap-wifi-mac.h
src/devices/wifi/nqsta-wifi-mac.cc
src/devices/wifi/nqsta-wifi-mac.h
src/devices/wifi/wifi-mac.cc
src/devices/wifi/wifi-net-device.cc
--- a/samples/main-ap-wifi.cc	Sun Mar 02 01:53:06 2008 +0100
+++ b/samples/main-ap-wifi.cc	Sun Mar 02 01:53:26 2008 +0100
@@ -18,20 +18,26 @@
  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
  */
 
-#include "ns3/wifi-net-device.h"
-#include "ns3/wifi-channel.h"
-#include "ns3/wifi-phy.h"
 
 #include "ns3/simulator.h"
 #include "ns3/callback.h"
 #include "ns3/ptr.h"
 #include "ns3/node.h"
 #include "ns3/onoff-application.h"
-#include "ns3/static-mobility-model.h"
+#include "ns3/mobility-helper.h"
+#include "ns3/wifi-helper.h"
+#include "ns3/node-container.h"
 #include "ns3/random-variable.h"
 #include "ns3/packet-socket-address.h"
 #include "ns3/packet.h"
 #include "ns3/node-list.h"
+#include "ns3/ssid.h"
+#include "ns3/wifi-phy.h"
+#include "ns3/mobility-model.h"
+#include "ns3/config.h"
+#include "ns3/string.h"
+#include "ns3/wifi-channel.h"
+#include "ns3/boolean.h"
 
 
 #include <iostream>
@@ -39,14 +45,14 @@
 using namespace ns3;
 
 void
-WifiNetDeviceTrace (const TraceContext &context, Ptr<const Packet> p, Mac48Address address)
+WifiNetDeviceTrace (Ptr<const Packet> p, Mac48Address address)
 {
-  std::cout << context << " ad=" << address << " p: " << p << std::endl;
+  std::cout << " ad=" << address << " p: " << p << std::endl;
 }
 void
-WifiPhyStateTrace (const TraceContext &context, Time start, Time duration, enum WifiPhy::State state)
+WifiPhyStateTrace (Time start, Time duration, enum WifiPhy::State state)
 {
-  std::cout << context << " state=";
+  std::cout << " state=";
   switch (state) {
   case WifiPhy::TX:
     std::cout << "tx      ";
@@ -64,43 +70,6 @@
   std::cout << " start="<<start<<" duration="<<duration<<std::endl;
 }
 
-static Ptr<Node>
-CreateApNode (Ptr<WifiChannel> channel,
-              Vector position, 
-              const char *macAddress,
-              Ssid ssid, 
-              Time at)
-{
-  Ptr<Node> node = CreateObject<Node> ();  
-  Ptr<NqapWifiNetDevice> device = CreateObject<NqapWifiNetDevice> (node, Mac48Address (macAddress));
-  node->AddDevice (device);
-  device->SetSsid (ssid);
-  Simulator::Schedule (at, &NqapWifiNetDevice::StartBeaconing, device);
-  device->Attach (channel);
-  Ptr<MobilityModel> mobility = CreateObject<StaticMobilityModel> ();
-  mobility->SetPosition (position);
-  node->AggregateObject (mobility);
-  return node;
-}
-
-static Ptr<Node>
-CreateStaNode (Ptr<WifiChannel> channel,
-               Vector position, 
-               const char *macAddress,
-               Ssid ssid)
-{
-  Ptr<Node> node = CreateObject<Node> ();  
-  Ptr<NqstaWifiNetDevice> device = CreateObject<NqstaWifiNetDevice> (node, Mac48Address (macAddress));
-  node->AddDevice (device);
-  Simulator::ScheduleNow (&NqstaWifiNetDevice::StartActiveAssociation, device, 
-                          ssid);
-  device->Attach (channel);
-  Ptr<MobilityModel> mobility = CreateObject<StaticMobilityModel> ();
-  mobility->SetPosition (position);
-  node->AggregateObject (mobility);
-  return node;
-}
-
 static void
 SetPosition (Ptr<Node> node, Vector position)
 {
@@ -138,47 +107,51 @@
 
   Packet::EnableMetadata ();
 
-  //Simulator::EnableLogTo ("80211.log");
-
+  // enable rts cts all the time.
+  Config::SetDefault ("WifiRemoteStationManager::RtsCtsThreshold", String ("0"));
+  // disable fragmentation
+  Config::SetDefault ("WifiRemoteStationManager::FragmentationThreshold", String ("2200"));
 
-  // enable rts cts all the time.
-  DefaultValue::Bind ("WifiRtsCtsThreshold", "0");
-  // disable fragmentation
-  DefaultValue::Bind ("WifiFragmentationThreshold", "2200");
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "Aarf");
-  //DefaultValue::Bind ("WifiRateControlAlgorithm", "Arf");
+  WifiHelper wifi;
+  MobilityHelper mobility;
+  NodeContainer stas;
+  NodeContainer ap;
+  NetDeviceContainer staDevs;
+
+  stas.Create (2);
+  ap.Create (1);
 
   Ptr<WifiChannel> channel = CreateObject<WifiChannel> ();
-  Ssid ssid = Ssid ("mathieu");
-
-  Ptr<Node> a = CreateApNode (channel, 
-                              Vector (5.0,0.0,0.0),
-                              "00:00:00:00:00:01",
-                              ssid, 
-                              Seconds (0.1));
-  Simulator::Schedule (Seconds (1.0), &AdvancePosition, a);
+  Ssid ssid = Ssid ("wifi-default");
+  wifi.SetPhy ("WifiPhy");
+  wifi.SetRemoteStationManager ("ArfWifiManager");
+  // setup ap.
+  wifi.SetMac ("NqstaWifiMac", "Ssid", ssid,
+               "ActiveProbing", Boolean (false));
+  staDevs = wifi.Build (stas, channel);
+  // setup stas.
+  wifi.SetMac ("NqapWifiMac", "Ssid", ssid,
+               "BeaconGeneration", Boolean (true),
+               "BeaconInterval", Seconds (2.5));
+  wifi.Build (ap, channel);
 
-  Ptr<Node> b = CreateStaNode (channel,
-                               Vector (0.0, 0.0, 0.0),
-                               "00:00:00:00:00:02",
-                               ssid);
+  // mobility.
+  mobility.Layout (stas.Begin (), stas.End ());
+  mobility.Layout (ap.Begin (), ap.End ());
 
-  Ptr<Node> c = CreateStaNode (channel,
-                               Vector (0.0, 0.0, 0.0),
-                               "00:00:00:00:00:03",
-                               ssid);
+  Simulator::Schedule (Seconds (1.0), &AdvancePosition, ap.Get (0));
 
   PacketSocketAddress destination = PacketSocketAddress ();
   destination.SetProtocol (1);
   destination.SetSingleDevice (0);
-  destination.SetPhysicalAddress (Mac48Address ("00:00:00:00:00:03"));
+  destination.SetPhysicalAddress (staDevs.Get(1)->GetAddress ());
   Ptr<Application> app = 
-    CreateObjectWith<OnOffApplication> ("Node", b, 
+    CreateObjectWith<OnOffApplication> ("Node", stas.Get (0), 
                                         "Remote", Address (destination), 
                                         "Protocol", TypeId::LookupByName ("Packet"),
                                         "OnTime", ConstantVariable (42),
                                         "OffTime", ConstantVariable (0));
-  b->AddApplication (app);
+  stas.Get (0)->AddApplication (app);
   app->Start (Seconds (0.5));
   app->Stop (Seconds (43.0));
 
--- a/samples/wscript	Sun Mar 02 01:53:06 2008 +0100
+++ b/samples/wscript	Sun Mar 02 01:53:26 2008 +0100
@@ -39,9 +39,9 @@
                                  ['core', 'simulator', 'mobility', 'wifi'])
     obj.source = 'main-adhoc-wifi.cc'
 
-#    obj = bld.create_ns3_program('main-ap-wifi',
-#                                 ['core', 'simulator', 'mobility', 'wifi'])
-#    obj.source = 'main-ap-wifi.cc'
+    obj = bld.create_ns3_program('main-ap-wifi',
+                                 ['core', 'simulator', 'mobility', 'wifi'])
+    obj.source = 'main-ap-wifi.cc'
     
     obj = bld.create_ns3_program('main-random-walk',
                                  ['core', 'simulator', 'mobility'])
--- a/src/devices/wifi/adhoc-wifi-mac.cc	Sun Mar 02 01:53:06 2008 +0100
+++ b/src/devices/wifi/adhoc-wifi-mac.cc	Sun Mar 02 01:53:26 2008 +0100
@@ -67,8 +67,10 @@
 {
   delete m_rxMiddle;
   delete m_low;
+  delete m_dcfManager;
   m_rxMiddle = 0;
   m_low = 0;
+  m_dcfManager = 0;
   m_phy = 0;
   WifiMac::DoDispose ();
 }
--- a/src/devices/wifi/nqap-wifi-mac.cc	Sun Mar 02 01:53:06 2008 +0100
+++ b/src/devices/wifi/nqap-wifi-mac.cc	Sun Mar 02 01:53:26 2008 +0100
@@ -50,19 +50,26 @@
                    Seconds (1.0),
                    MakeTimeAccessor (&NqapWifiMac::m_beaconInterval),
                    MakeTimeChecker ())
+    .AddAttribute ("BeaconGeneration", "Whether or not beacons are generated.",
+                   Boolean (false),
+                   MakeBooleanAccessor (&NqapWifiMac::SetBeaconGeneration,
+                                        &NqapWifiMac::GetBeaconGeneration),
+                   MakeBooleanChecker ())
     ;
   return tid;
 }
 
 NqapWifiMac::NqapWifiMac ()
 {
-  m_dcfManager = new DcfManager ();
-
   m_rxMiddle = new MacRxMiddle ();
   m_rxMiddle->SetForwardCallback (MakeCallback (&NqapWifiMac::Receive, this));
 
   m_low = new MacLow ();
   m_low->SetRxCallback (MakeCallback (&MacRxMiddle::Receive, m_rxMiddle));
+  m_low->SetMac (this);
+
+  m_dcfManager = new DcfManager ();
+  m_dcfManager->SetupLowListener (m_low);
 
   m_dca = CreateObject<DcaTxop> ();
   m_dca->SetLow (m_low);
@@ -82,24 +89,69 @@
 {
   delete m_rxMiddle;
   delete m_low;
+  delete m_dcfManager;
   m_rxMiddle = 0;
   m_low = 0;
+  m_dcfManager = 0;
   m_phy = 0;
   m_dca = 0;
   m_beaconDca = 0;
+  m_beaconEvent.Cancel ();
   WifiMac::DoDispose ();
 }
 
+void
+NqapWifiMac::SetBeaconGeneration (bool enable)
+{
+  if (enable)
+    {
+      m_beaconEvent = Simulator::ScheduleNow (&NqapWifiMac::SendOneBeacon, this);
+    }
+  else
+    {
+      m_beaconEvent.Cancel ();
+    }
+}
+
+bool
+NqapWifiMac::GetBeaconGeneration (void) const
+{
+  return m_beaconEvent.IsRunning ();
+}
+
+void 
+NqapWifiMac::SetSlot (Time slotTime)
+{
+  m_dcfManager->SetSlot (slotTime);
+  WifiMac::SetSlot (slotTime);
+}
+void 
+NqapWifiMac::SetSifs (Time sifs)
+{
+  m_dcfManager->SetSifs (sifs);
+  WifiMac::SetSifs (sifs);
+}
+void 
+NqapWifiMac::SetEifsNoDifs (Time eifsNoDifs)
+{
+  m_dcfManager->SetEifsNoDifs (eifsNoDifs);
+  WifiMac::SetEifsNoDifs (eifsNoDifs);
+}
 
 void 
 NqapWifiMac::SetWifiPhy (Ptr<WifiPhy> phy)
 {
   m_phy = phy;
+  m_dcfManager->SetupPhyListener (phy);
+  m_low->SetPhy (phy);
 }
 void 
 NqapWifiMac::SetWifiRemoteStationManager (Ptr<WifiRemoteStationManager> stationManager)
 {
   m_stationManager = stationManager;
+  m_dca->SetWifiRemoteStationManager (stationManager);
+  m_beaconDca->SetWifiRemoteStationManager (stationManager);
+  m_low->SetWifiRemoteStationManager (stationManager);
 }
 void 
 NqapWifiMac::SetForwardUpCallback (Callback<void,Ptr<Packet>, const Mac48Address &> upCallback)
@@ -109,13 +161,14 @@
 void 
 NqapWifiMac::SetLinkUpCallback (Callback<void> linkUp)
 {
-
+  if (!linkUp.IsNull ())
+    {
+      linkUp ();
+    }
 }
 void 
 NqapWifiMac::SetLinkDownCallback (Callback<void> linkDown)
-{
-
-}
+{}
 Mac48Address 
 NqapWifiMac::GetAddress (void) const
 {
@@ -261,7 +314,7 @@
   packet->AddHeader (beacon);
 
   m_beaconDca->Queue (packet, hdr);
-  Simulator::Schedule (m_beaconInterval, &NqapWifiMac::SendOneBeacon, this);
+  m_beaconEvent = Simulator::Schedule (m_beaconInterval, &NqapWifiMac::SendOneBeacon, this);
 }
 void 
 NqapWifiMac::TxOk (WifiMacHeader const &hdr)
--- a/src/devices/wifi/nqap-wifi-mac.h	Sun Mar 02 01:53:06 2008 +0100
+++ b/src/devices/wifi/nqap-wifi-mac.h	Sun Mar 02 01:53:26 2008 +0100
@@ -58,6 +58,9 @@
   NqapWifiMac ();
   ~NqapWifiMac ();
 
+  virtual void SetSlot (Time slotTime);
+  virtual void SetSifs (Time sifs);
+  virtual void SetEifsNoDifs (Time eifsNoDifs);
   virtual void SetWifiPhy (Ptr<WifiPhy> phy);
   virtual void SetWifiRemoteStationManager (Ptr<WifiRemoteStationManager> stationManager);
   virtual void Enqueue (Ptr<const Packet> packet, Mac48Address to);
@@ -88,6 +91,8 @@
   void SendAssocResp (Mac48Address to, bool success);
   void SendOneBeacon (void);
   SupportedRates GetSupportedRates (void) const;
+  void SetBeaconGeneration (bool enable);
+  bool GetBeaconGeneration (void) const;
   virtual void DoDispose (void);
 
   Ptr<DcaTxop> m_dca;
@@ -102,6 +107,7 @@
   MacLow *m_low;
   Mac48Address m_address;
   Ssid m_ssid;
+  EventId m_beaconEvent;
 };
 
 } // namespace ns3
--- a/src/devices/wifi/nqsta-wifi-mac.cc	Sun Mar 02 01:53:06 2008 +0100
+++ b/src/devices/wifi/nqsta-wifi-mac.cc	Sun Mar 02 01:53:26 2008 +0100
@@ -79,6 +79,10 @@
                    Uinteger (10),
                    MakeUintegerAccessor (&NqstaWifiMac::m_maxMissedBeacons),
                    MakeUintegerChecker<uint32_t> ())
+    .AddAttribute ("ActiveProbing", "XXX",
+                   Boolean (false),
+                   MakeBooleanAccessor (&NqstaWifiMac::SetActiveProbing),
+                   MakeBooleanChecker ())
     ;
   return tid;
 }
@@ -97,6 +101,7 @@
 
   m_low = new MacLow ();
   m_low->SetRxCallback (MakeCallback (&MacRxMiddle::Receive, m_rxMiddle));
+  m_low->SetMac (this);
 
   m_dca = CreateObject<DcaTxop> ();
   m_dca->SetLow (m_low);
@@ -111,22 +116,47 @@
 {
   delete m_rxMiddle;
   delete m_low;
+  delete m_dcfManager;
   m_rxMiddle = 0;
   m_low = 0;
+  m_dcfManager = 0;
   m_phy = 0;
   m_dca = 0;
   WifiMac::DoDispose ();
 }
 
 void 
+NqstaWifiMac::SetSlot (Time slotTime)
+{
+  m_dcfManager->SetSlot (slotTime);
+  WifiMac::SetSlot (slotTime);
+}
+void 
+NqstaWifiMac::SetSifs (Time sifs)
+{
+  m_dcfManager->SetSifs (sifs);
+  WifiMac::SetSifs (sifs);
+}
+void 
+NqstaWifiMac::SetEifsNoDifs (Time eifsNoDifs)
+{
+  m_dcfManager->SetEifsNoDifs (eifsNoDifs);
+  WifiMac::SetEifsNoDifs (eifsNoDifs);
+}
+
+void 
 NqstaWifiMac::SetWifiPhy (Ptr<WifiPhy> phy)
 {
   m_phy = phy;
+  m_dcfManager->SetupPhyListener (phy);
+  m_low->SetPhy (phy);
 }
 void 
 NqstaWifiMac::SetWifiRemoteStationManager (Ptr<WifiRemoteStationManager> stationManager)
 {
   m_stationManager = stationManager;
+  m_dca->SetWifiRemoteStationManager (stationManager);
+  m_low->SetWifiRemoteStationManager (stationManager);
 }
 void 
 NqstaWifiMac::SetForwardUpCallback (Callback<void,Ptr<Packet>, const Mac48Address &> upCallback)
@@ -203,6 +233,18 @@
   m_bssid = bssid;
 }
 void 
+NqstaWifiMac::SetActiveProbing (bool enable)
+{
+  if (enable)
+    {
+      TryToEnsureAssociated ();
+    }
+  else
+    {
+      m_probeRequestEvent.Cancel ();
+    }
+}
+void 
 NqstaWifiMac::ForwardUp (Ptr<Packet> packet, const Mac48Address &address)
 {
   m_forwardUp (packet, address);
@@ -445,7 +487,10 @@
                         }
                     }
                 }
-              m_linkUp ();
+              if (!m_linkUp.IsNull ())
+                {
+                  m_linkUp ();
+                }
             } 
           else 
             {
--- a/src/devices/wifi/nqsta-wifi-mac.h	Sun Mar 02 01:53:06 2008 +0100
+++ b/src/devices/wifi/nqsta-wifi-mac.h	Sun Mar 02 01:53:26 2008 +0100
@@ -58,6 +58,9 @@
   NqstaWifiMac ();
   ~NqstaWifiMac ();
 
+  virtual void SetSlot (Time slotTime);
+  virtual void SetSifs (Time sifs);
+  virtual void SetEifsNoDifs (Time eifsNoDifs);
   virtual void SetWifiPhy (Ptr<WifiPhy> phy);
   virtual void SetWifiRemoteStationManager (Ptr<WifiRemoteStationManager> stationManager);
   virtual void Enqueue (Ptr<const Packet> packet, Mac48Address to);
@@ -98,6 +101,8 @@
 
 private:
   void SetBssid (Mac48Address bssid);
+  void SetActiveProbing (bool enable);
+  bool GetActiveProbing (void) const;
   void ForwardUp (Ptr<Packet> packet, const Mac48Address &address);
   void Receive (Ptr<Packet> packet, WifiMacHeader const *hdr);
   Mac48Address GetBroadcastBssid (void);
--- a/src/devices/wifi/wifi-mac.cc	Sun Mar 02 01:53:06 2008 +0100
+++ b/src/devices/wifi/wifi-mac.cc	Sun Mar 02 01:53:26 2008 +0100
@@ -89,6 +89,11 @@
 		   Uinteger (2304),
 		   MakeUintegerAccessor (&WifiMac::GetMaxMsduSize),
 		   MakeUintegerChecker (1,2304))
+    .AddAttribute ("Ssid", "XXX",
+		   Ssid ("default"),
+		   MakeSsidAccessor (&WifiMac::GetSsid,
+				     &WifiMac::SetSsid),
+		   MakeSsidChecker ())
     ;
   return tid;
 }
--- a/src/devices/wifi/wifi-net-device.cc	Sun Mar 02 01:53:06 2008 +0100
+++ b/src/devices/wifi/wifi-net-device.cc	Sun Mar 02 01:53:26 2008 +0100
@@ -58,9 +58,9 @@
   m_phy = phy;
   m_stationManager = manager;
 
-  manager->SetupPhy (phy);
+  m_stationManager->SetupPhy (m_phy);
 
-  m_mac->SetWifiRemoteStationManager (manager);
+  m_mac->SetWifiRemoteStationManager (m_stationManager);
   m_mac->SetWifiPhy (m_phy);
   m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
   m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
@@ -68,8 +68,6 @@
   channel->Add (this, m_phy);
 
   m_phy->SetChannel (channel);
-  //XXX
-  //m_stationManager->
 }
 Ptr<WifiMac> 
 WifiNetDevice::GetMac (void) const