A Discrete-Event Network Simulator
API
wifi-ap.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 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 
21 #include "ns3/command-line.h"
22 #include "ns3/config.h"
23 #include "ns3/boolean.h"
24 #include "ns3/string.h"
25 #include "ns3/yans-wifi-helper.h"
26 #include "ns3/ssid.h"
27 #include "ns3/mobility-helper.h"
28 #include "ns3/on-off-helper.h"
29 #include "ns3/yans-wifi-channel.h"
30 #include "ns3/mobility-model.h"
31 #include "ns3/packet-socket-helper.h"
32 #include "ns3/packet-socket-address.h"
33 #include "ns3/athstats-helper.h"
34 
35 using namespace ns3;
36 
37 static bool g_verbose = true;
38 
39 void
40 DevTxTrace (std::string context, Ptr<const Packet> p)
41 {
42  if (g_verbose)
43  {
44  std::cout << " TX p: " << *p << std::endl;
45  }
46 }
47 void
48 DevRxTrace (std::string context, Ptr<const Packet> p)
49 {
50  if (g_verbose)
51  {
52  std::cout << " RX p: " << *p << std::endl;
53  }
54 }
55 void
56 PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, WifiPreamble preamble)
57 {
58  if (g_verbose)
59  {
60  std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
61  }
62 }
63 void
64 PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
65 {
66  if (g_verbose)
67  {
68  std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
69  }
70 }
71 void
72 PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
73 {
74  if (g_verbose)
75  {
76  std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
77  }
78 }
79 void
80 PhyStateTrace (std::string context, Time start, Time duration, WifiPhyState state)
81 {
82  if (g_verbose)
83  {
84  std::cout << " state=" << state << " start=" << start << " duration=" << duration << std::endl;
85  }
86 }
87 
88 static void
89 SetPosition (Ptr<Node> node, Vector position)
90 {
92  mobility->SetPosition (position);
93 }
94 
95 static Vector
97 {
99  return mobility->GetPosition ();
100 }
101 
102 static void
104 {
105  Vector pos = GetPosition (node);
106  pos.x += 5.0;
107  if (pos.x >= 210.0)
108  {
109  return;
110  }
111  SetPosition (node, pos);
112 
114 }
115 
116 int main (int argc, char *argv[])
117 {
118  CommandLine cmd (__FILE__);
119  cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
120  cmd.Parse (argc, argv);
121 
123 
126  NodeContainer stas;
127  NodeContainer ap;
128  NetDeviceContainer staDevs;
129  PacketSocketHelper packetSocket;
130 
131  stas.Create (2);
132  ap.Create (1);
133 
134  // give packet socket powers to nodes.
135  packetSocket.Install (stas);
136  packetSocket.Install (ap);
137 
138  WifiMacHelper wifiMac;
139  YansWifiPhyHelper wifiPhy;
141  wifiPhy.SetChannel (wifiChannel.Create ());
142  Ssid ssid = Ssid ("wifi-default");
143  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
144  // setup stas.
145  wifiMac.SetType ("ns3::StaWifiMac",
146  "ActiveProbing", BooleanValue (true),
147  "Ssid", SsidValue (ssid));
148  staDevs = wifi.Install (wifiPhy, wifiMac, stas);
149  // setup ap.
150  wifiMac.SetType ("ns3::ApWifiMac",
151  "Ssid", SsidValue (ssid));
152  wifi.Install (wifiPhy, wifiMac, ap);
153 
154  // mobility.
155  mobility.Install (stas);
156  mobility.Install (ap);
157 
159 
160  PacketSocketAddress socket;
161  socket.SetSingleDevice (staDevs.Get (0)->GetIfIndex ());
162  socket.SetPhysicalAddress (staDevs.Get (1)->GetAddress ());
163  socket.SetProtocol (1);
164 
165  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
166  onoff.SetConstantRate (DataRate ("500kb/s"));
167 
168  ApplicationContainer apps = onoff.Install (stas.Get (0));
169  apps.Start (Seconds (0.5));
170  apps.Stop (Seconds (43.0));
171 
172  Simulator::Stop (Seconds (44.0));
173 
174  Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback (&DevTxTrace));
175  Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback (&DevRxTrace));
176  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback (&PhyRxOkTrace));
177  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback (&PhyRxErrorTrace));
178  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback (&PhyTxTrace));
179  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback (&PhyStateTrace));
180 
181  AthstatsHelper athstats;
182  athstats.EnableAthstats ("athstats-sta", stas);
183  athstats.EnableAthstats ("athstats-ap", ap);
184 
185  Simulator::Run ();
186 
188 
189  return 0;
190 }
holds a vector of ns3::Application pointers.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Boolean.
Definition: boolean.h:36
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:103
Make it easy to create and manage PHY objects for the YANS model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
def start()
Definition: core.py:1855
void DevTxTrace(std::string context, Ptr< const Packet > p)
Definition: wifi-ap.cc:40
an address for a packet socket
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
cmd
Definition: second.py:35
void PhyRxErrorTrace(std::string context, Ptr< const Packet > packet, double snr)
Definition: wifi-ap.cc:64
helps to create WifiNetDevice objects
Definition: wifi-helper.h:326
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:99
static Vector GetPosition(Ptr< Node > node)
Definition: wifi-ap.cc:96
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:89
a polymophic address class
Definition: address.h:90
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:108
void PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
Definition: wifi-ap.cc:80
Class for representing data rates.
Definition: data-rate.h:88
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:32
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
ssid
Definition: third.py:100
holds a vector of ns3::NetDevice pointers
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
Definition: wifi-ap.cc:56
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:226
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:918
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void DevRxTrace(std::string context, Ptr< const Packet > p)
Definition: wifi-ap.cc:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void PhyTxTrace(std::string context, Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
Definition: wifi-ap.cc:72
create AthstatsWifiTraceSink instances and connect them to wifi devices
WifiPhyState
The state of the PHY layer.
manage and create wifi channel objects for the YANS model.
create MAC layers for a ns3::WifiNetDevice.
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
Enable athstats.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:35
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
wifi
Definition: third.py:96
Helper class used to assign positions and mobility models to nodes.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
AttributeValue implementation for Ssid.
Definition: ssid.h:105
static bool g_verbose
Definition: wifi-ap.cc:37
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642