A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/config-store-module.h"
27 #include "ns3/wifi-module.h"
28 #include "ns3/athstats-helper.h"
29 
30 #include <iostream>
31 
32 using namespace ns3;
33 
34 static bool g_verbose = true;
35 
36 void
37 DevTxTrace (std::string context, Ptr<const Packet> p)
38 {
39  if (g_verbose)
40  {
41  std::cout << " TX p: " << *p << std::endl;
42  }
43 }
44 void
45 DevRxTrace (std::string context, Ptr<const Packet> p)
46 {
47  if (g_verbose)
48  {
49  std::cout << " RX p: " << *p << std::endl;
50  }
51 }
52 void
53 PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
54 {
55  if (g_verbose)
56  {
57  std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
58  }
59 }
60 void
61 PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
62 {
63  if (g_verbose)
64  {
65  std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
66  }
67 }
68 void
69 PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
70 {
71  if (g_verbose)
72  {
73  std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
74  }
75 }
76 void
77 PhyStateTrace (std::string context, Time start, Time duration, enum WifiPhy::State state)
78 {
79  if (g_verbose)
80  {
81  std::cout << " state=" << state << " start=" << start << " duration=" << duration << std::endl;
82  }
83 }
84 
85 static void
86 SetPosition (Ptr<Node> node, Vector position)
87 {
88  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
89  mobility->SetPosition (position);
90 }
91 
92 static Vector
94 {
95  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
96  return mobility->GetPosition ();
97 }
98 
99 static void
101 {
102  Vector pos = GetPosition (node);
103  pos.x += 5.0;
104  if (pos.x >= 210.0)
105  {
106  return;
107  }
108  SetPosition (node, pos);
109 
110  if (g_verbose)
111  {
112  //std::cout << "x="<<pos.x << std::endl;
113  }
114  Simulator::Schedule (Seconds (1.0), &AdvancePosition, node);
115 }
116 
117 int main (int argc, char *argv[])
118 {
119  CommandLine cmd;
120  cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
121 
122  cmd.Parse (argc, argv);
123 
125 
126  // enable rts cts all the time.
127  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("0"));
128  // disable fragmentation
129  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
130 
132  MobilityHelper mobility;
133  NodeContainer stas;
134  NodeContainer ap;
135  NetDeviceContainer staDevs;
136  PacketSocketHelper packetSocket;
137 
138  stas.Create (2);
139  ap.Create (1);
140 
141  // give packet socket powers to nodes.
142  packetSocket.Install (stas);
143  packetSocket.Install (ap);
144 
148  wifiPhy.SetChannel (wifiChannel.Create ());
149  Ssid ssid = Ssid ("wifi-default");
150  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
151  // setup stas.
152  wifiMac.SetType ("ns3::StaWifiMac",
153  "Ssid", SsidValue (ssid),
154  "ActiveProbing", BooleanValue (false));
155  staDevs = wifi.Install (wifiPhy, wifiMac, stas);
156  // setup ap.
157  wifiMac.SetType ("ns3::ApWifiMac",
158  "Ssid", SsidValue (ssid));
159  wifi.Install (wifiPhy, wifiMac, ap);
160 
161  // mobility.
162  mobility.Install (stas);
163  mobility.Install (ap);
164 
165  Simulator::Schedule (Seconds (1.0), &AdvancePosition, ap.Get (0));
166 
167  PacketSocketAddress socket;
168  socket.SetSingleDevice (staDevs.Get (0)->GetIfIndex ());
169  socket.SetPhysicalAddress (staDevs.Get (1)->GetAddress ());
170  socket.SetProtocol (1);
171 
172  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
173  onoff.SetConstantRate (DataRate ("500kb/s"));
174 
175  ApplicationContainer apps = onoff.Install (stas.Get (0));
176  apps.Start (Seconds (0.5));
177  apps.Stop (Seconds (43.0));
178 
179  Simulator::Stop (Seconds (44.0));
180 
181  Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback (&DevTxTrace));
182  Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback (&DevRxTrace));
183  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback (&PhyRxOkTrace));
184  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback (&PhyRxErrorTrace));
185  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback (&PhyTxTrace));
186  Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback (&PhyStateTrace));
187 
188  AthstatsHelper athstats;
189  athstats.EnableAthstats ("athstats-sta", stas);
190  athstats.EnableAthstats ("athstats-ap", ap);
191 
192  Simulator::Run ();
193 
195 
196  return 0;
197 }
holds a vector of ns3::Application pointers.
double x
x coordinate of vector
Definition: vector.h:49
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
Hold a bool native type.
Definition: boolean.h:38
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:100
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:73
hold variables of type string
Definition: string.h:18
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.
State
The state of the PHY layer.
Definition: wifi-phy.h:123
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())
void DevTxTrace(std::string context, Ptr< const Packet > p)
Definition: wifi-ap.cc:37
an address for a packet socket
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
Vector GetPosition(void) const
void PhyStateTrace(std::string context, Time start, Time duration, enum WifiPhy::State state)
Definition: wifi-ap.cc:77
void PhyRxErrorTrace(std::string context, Ptr< const Packet > packet, double snr)
Definition: wifi-ap.cc:61
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
helps to create WifiNetDevice objects
Definition: wifi-helper.h:88
represent a single transmission modeA WifiMode is implemented by a single integer which is used to lo...
Definition: wifi-mode.h:91
static Vector GetPosition(Ptr< Node > node)
Definition: wifi-ap.cc:93
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
a 3d vector
Definition: vector.h:31
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:86
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:102
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
Definition: wifi-preamble.h:29
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
int main(int argc, char *argv[])
Definition: wifi-ap.cc:117
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:552
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, enum WifiPreamble preamble)
Definition: wifi-ap.cc:53
static NqosWifiMacHelper Default(void)
Create a mac helper in a default working state.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void DevRxTrace(std::string context, Ptr< const Packet > p)
Definition: wifi-ap.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
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:69
create AthstatsWifiTraceSink instances and connect them to wifi devices
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.
void SetPosition(const Vector &position)
void EnableAthstats(std::string filename, uint32_t nodeid, uint32_t deviceid)
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:35
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:435
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
hold objects of type ns3::Ssid
static bool g_verbose
Definition: wifi-ap.cc:34
void Parse(int argc, char *argv[])
Parse the program arguments.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< T > GetObject(void) const
Definition: object.h:362
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
static WifiHelper Default(void)
Definition: wifi-helper.cc:65