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 
22 #include "ns3/core-module.h"
23 #include "ns3/network-module.h"
24 #include "ns3/applications-module.h"
25 #include "ns3/mobility-module.h"
26 #include "ns3/wifi-module.h"
27 
28 using namespace ns3;
29 
30 static bool g_verbose = true;
31 
32 void
33 DevTxTrace (std::string context, Ptr<const Packet> p)
34 {
35  if (g_verbose)
36  {
37  std::cout << " TX p: " << *p << std::endl;
38  }
39 }
40 void
41 DevRxTrace (std::string context, Ptr<const Packet> p)
42 {
43  if (g_verbose)
44  {
45  std::cout << " RX p: " << *p << std::endl;
46  }
47 }
48 void
49 PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
50 {
51  if (g_verbose)
52  {
53  std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
54  }
55 }
56 void
57 PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
58 {
59  if (g_verbose)
60  {
61  std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
62  }
63 }
64 void
65 PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
66 {
67  if (g_verbose)
68  {
69  std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
70  }
71 }
72 void
73 PhyStateTrace (std::string context, Time start, Time duration, enum WifiPhy::State state)
74 {
75  if (g_verbose)
76  {
77  std::cout << " state=" << state << " start=" << start << " duration=" << duration << std::endl;
78  }
79 }
80 
81 static void
82 SetPosition (Ptr<Node> node, Vector position)
83 {
85  mobility->SetPosition (position);
86 }
87 
88 static Vector
90 {
92  return mobility->GetPosition ();
93 }
94 
95 static void
97 {
98  Vector pos = GetPosition (node);
99  pos.x += 5.0;
100  if (pos.x >= 210.0)
101  {
102  return;
103  }
104  SetPosition (node, pos);
105 
106  if (g_verbose)
107  {
108  //std::cout << "x="<<pos.x << std::endl;
109  }
111 }
112 
113 int main (int argc, char *argv[])
114 {
116  cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
117 
118  cmd.Parse (argc, argv);
119 
121 
122  // enable rts cts all the time.
123  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("0"));
124  // disable fragmentation
125  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
126 
129  NodeContainer stas;
130  NodeContainer ap;
131  NetDeviceContainer staDevs;
132  PacketSocketHelper packetSocket;
133 
134  stas.Create (2);
135  ap.Create (1);
136 
137  // give packet socket powers to nodes.
138  packetSocket.Install (stas);
139  packetSocket.Install (ap);
140 
141  WifiMacHelper wifiMac;
144  wifiPhy.SetChannel (wifiChannel.Create ());
145  Ssid ssid = Ssid ("wifi-default");
146  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
147  // setup stas.
148  wifiMac.SetType ("ns3::StaWifiMac",
149  "Ssid", SsidValue (ssid));
150  staDevs = wifi.Install (wifiPhy, wifiMac, stas);
151  // setup ap.
152  wifiMac.SetType ("ns3::ApWifiMac",
153  "Ssid", SsidValue (ssid));
154  wifi.Install (wifiPhy, wifiMac, ap);
155 
156  // mobility.
157  mobility.Install (stas);
158  mobility.Install (ap);
159 
161 
162  PacketSocketAddress socket;
163  socket.SetSingleDevice (staDevs.Get (0)->GetIfIndex ());
164  socket.SetPhysicalAddress (staDevs.Get (1)->GetAddress ());
165  socket.SetProtocol (1);
166 
167  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
168  onoff.SetConstantRate (DataRate ("500kb/s"));
169 
170  ApplicationContainer apps = onoff.Install (stas.Get (0));
171  apps.Start (Seconds (0.5));
172  apps.Stop (Seconds (43.0));
173 
174  Simulator::Stop (Seconds (44.0));
175 
176  Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback (&DevTxTrace));
177  Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback (&DevRxTrace));
178  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback (&PhyRxOkTrace));
179  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback (&PhyRxErrorTrace));
180  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback (&PhyTxTrace));
181  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback (&PhyStateTrace));
182 
183  AthstatsHelper athstats;
184  athstats.EnableAthstats ("athstats-sta", stas);
185  athstats.EnableAthstats ("athstats-ap", ap);
186 
187  Simulator::Run ();
188 
190 
191  return 0;
192 }
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:96
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(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())
Definition: wifi-helper.cc:719
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
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:1790
void DevTxTrace(std::string context, Ptr< const Packet > p)
Definition: wifi-ap.cc:33
an address for a packet socket
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
Vector GetPosition(void) const
void PhyStateTrace(std::string context, Time start, Time duration, enum WifiPhy::State state)
Definition: wifi-ap.cc:73
void PhyRxErrorTrace(std::string context, Ptr< const Packet > packet, double snr)
Definition: wifi-ap.cc:57
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:213
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:97
static Vector GetPosition(Ptr< Node > node)
Definition: wifi-ap.cc:89
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.
tuple cmd
Definition: second.py:35
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:82
a polymophic address class
Definition: address.h:90
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:30
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:534
holds a vector of ns3::NetDevice pointers
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:748
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, enum WifiPreamble preamble)
Definition: wifi-ap.cc:49
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:205
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
void DevRxTrace(std::string context, Ptr< const Packet > p)
Definition: wifi-ap.cc:41
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:65
create AthstatsWifiTraceSink instances and connect them to wifi devices
tuple ssid
Definition: third.py:93
manage and create wifi channel objects for the yans model.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
create MAC layers for a ns3::WifiNetDevice.
void SetPosition(const Vector &position)
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())
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...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
AttributeValue implementation for Ssid.
Definition: ssid.h:117
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
static bool g_verbose
Definition: wifi-ap.cc:30
void Parse(int argc, char *argv[])
Parse the program arguments.
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
State
The state of the PHY layer.
Definition: wifi-phy.h:171