A Discrete-Event Network Simulator
API
wifi-ap.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005,2006,2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #include "ns3/athstats-helper.h"
21 #include "ns3/boolean.h"
22 #include "ns3/command-line.h"
23 #include "ns3/config.h"
24 #include "ns3/mobility-helper.h"
25 #include "ns3/mobility-model.h"
26 #include "ns3/on-off-helper.h"
27 #include "ns3/packet-socket-address.h"
28 #include "ns3/packet-socket-helper.h"
29 #include "ns3/ssid.h"
30 #include "ns3/string.h"
31 #include "ns3/yans-wifi-channel.h"
32 #include "ns3/yans-wifi-helper.h"
33 
34 using namespace ns3;
35 
37 static bool g_verbose = true;
38 
45 void
46 DevTxTrace(std::string context, Ptr<const Packet> p)
47 {
48  if (g_verbose)
49  {
50  std::cout << " TX p: " << *p << std::endl;
51  }
52 }
53 
60 void
61 DevRxTrace(std::string context, Ptr<const Packet> p)
62 {
63  if (g_verbose)
64  {
65  std::cout << " RX p: " << *p << std::endl;
66  }
67 }
68 
78 void
79 PhyRxOkTrace(std::string context,
80  Ptr<const Packet> packet,
81  double snr,
82  WifiMode mode,
83  WifiPreamble preamble)
84 {
85  if (g_verbose)
86  {
87  std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
88  }
89 }
90 
98 void
99 PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
100 {
101  if (g_verbose)
102  {
103  std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
104  }
105 }
106 
116 void
117 PhyTxTrace(std::string context,
118  Ptr<const Packet> packet,
119  WifiMode mode,
120  WifiPreamble preamble,
121  uint8_t txPower)
122 {
123  if (g_verbose)
124  {
125  std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
126  }
127 }
128 
137 void
138 PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
139 {
140  if (g_verbose)
141  {
142  std::cout << " state=" << state << " start=" << start << " duration=" << duration
143  << std::endl;
144  }
145 }
146 
152 static void
154 {
156  Vector pos = mobility->GetPosition();
157  pos.x += 5.0;
158  if (pos.x >= 210.0)
159  {
160  return;
161  }
162  mobility->SetPosition(pos);
163 
165 }
166 
167 int
168 main(int argc, char* argv[])
169 {
170  CommandLine cmd(__FILE__);
171  cmd.AddValue("verbose", "Print trace information if true", g_verbose);
172  cmd.Parse(argc, argv);
173 
175 
178  NodeContainer stas;
179  NodeContainer ap;
180  NetDeviceContainer staDevs;
181  PacketSocketHelper packetSocket;
182 
183  stas.Create(2);
184  ap.Create(1);
185 
186  // give packet socket powers to nodes.
187  packetSocket.Install(stas);
188  packetSocket.Install(ap);
189 
190  WifiMacHelper wifiMac;
191  YansWifiPhyHelper wifiPhy;
193  wifiPhy.SetChannel(wifiChannel.Create());
194  Ssid ssid = Ssid("wifi-default");
195  // setup stas.
196  wifiMac.SetType("ns3::StaWifiMac",
197  "ActiveProbing",
198  BooleanValue(true),
199  "Ssid",
200  SsidValue(ssid));
201  staDevs = wifi.Install(wifiPhy, wifiMac, stas);
202  // setup ap.
203  wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
204  wifi.Install(wifiPhy, wifiMac, ap);
205 
206  // mobility.
207  mobility.Install(stas);
208  mobility.Install(ap);
209 
211 
212  PacketSocketAddress socket;
213  socket.SetSingleDevice(staDevs.Get(0)->GetIfIndex());
214  socket.SetPhysicalAddress(staDevs.Get(1)->GetAddress());
215  socket.SetProtocol(1);
216 
217  OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
218  onoff.SetConstantRate(DataRate("500kb/s"));
219 
220  ApplicationContainer apps = onoff.Install(stas.Get(0));
221  apps.Start(Seconds(0.5));
222  apps.Stop(Seconds(43.0));
223 
224  Simulator::Stop(Seconds(44.0));
225 
226  Config::Connect("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback(&DevTxTrace));
227  Config::Connect("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback(&DevRxTrace));
228  Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback(&PhyRxOkTrace));
229  Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback(&PhyRxErrorTrace));
230  Config::Connect("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback(&PhyTxTrace));
231  Config::Connect("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback(&PhyStateTrace));
232 
233  AthstatsHelper athstats;
234  athstats.EnableAthstats("athstats-sta", stas);
235  athstats.EnableAthstats("athstats-ap", ap);
236 
237  Simulator::Run();
238 
240 
241  return 0;
242 }
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
create AthstatsWifiTraceSink instances and connect them to wifi devices
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
Enable athstats.
Parse command-line arguments.
Definition: command-line.h:232
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
represent a single transmission mode
Definition: wifi-mode.h:51
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:974
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
cmd
Definition: second.py:40
ssid
Definition: third.py:93
wifi
Definition: third.py:95
mobility
Definition: third.py:105
void PhyRxErrorTrace(std::string context, Ptr< const Packet > packet, double snr)
PHY-level RX error trace.
Definition: wifi-ap.cc:99
void PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
PHY state trace.
Definition: wifi-ap.cc:138
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
PHY-level RX OK trace.
Definition: wifi-ap.cc:79
void PhyTxTrace(std::string context, Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
PHY-level TX trace.
Definition: wifi-ap.cc:117
static bool g_verbose
True for verbose output.
Definition: wifi-ap.cc:37
void DevTxTrace(std::string context, Ptr< const Packet > p)
MAC-level TX trace.
Definition: wifi-ap.cc:46
static void AdvancePosition(Ptr< Node > node)
Move a node position by 5m on the x axis every second, up to 210m.
Definition: wifi-ap.cc:153
void DevRxTrace(std::string context, Ptr< const Packet > p)
MAC-level RX trace.
Definition: wifi-ap.cc:61
WifiPhyState
The state of the PHY layer.