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
35using namespace ns3;
36
38static bool g_verbose = true;
39
46void
47DevTxTrace (std::string context, Ptr<const Packet> p)
48{
49 if (g_verbose)
50 {
51 std::cout << " TX p: " << *p << std::endl;
52 }
53}
54
61void
62DevRxTrace (std::string context, Ptr<const Packet> p)
63{
64 if (g_verbose)
65 {
66 std::cout << " RX p: " << *p << std::endl;
67 }
68}
69
79void
80PhyRxOkTrace (std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, WifiPreamble preamble)
81{
82 if (g_verbose)
83 {
84 std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
85 }
86}
87
95void
96PhyRxErrorTrace (std::string context, Ptr<const Packet> packet, double snr)
97{
98 if (g_verbose)
99 {
100 std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
101 }
102}
103
113void
114PhyTxTrace (std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
115{
116 if (g_verbose)
117 {
118 std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
119 }
120}
121
130void
131PhyStateTrace (std::string context, Time start, Time duration, WifiPhyState state)
132{
133 if (g_verbose)
134 {
135 std::cout << " state=" << state << " start=" << start << " duration=" << duration << std::endl;
136 }
137}
138
144static void
146{
148 Vector pos = mobility->GetPosition ();
149 pos.x += 5.0;
150 if (pos.x >= 210.0)
151 {
152 return;
153 }
154 mobility->SetPosition (pos);
155
156 Simulator::Schedule (Seconds (1.0), &AdvancePosition, node);
157}
158
159int main (int argc, char *argv[])
160{
161 CommandLine cmd (__FILE__);
162 cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
163 cmd.Parse (argc, argv);
164
165 Packet::EnablePrinting ();
166
169 NodeContainer stas;
170 NodeContainer ap;
171 NetDeviceContainer staDevs;
172 PacketSocketHelper packetSocket;
173
174 stas.Create (2);
175 ap.Create (1);
176
177 // give packet socket powers to nodes.
178 packetSocket.Install (stas);
179 packetSocket.Install (ap);
180
181 WifiMacHelper wifiMac;
182 YansWifiPhyHelper wifiPhy;
183 YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
184 wifiPhy.SetChannel (wifiChannel.Create ());
185 Ssid ssid = Ssid ("wifi-default");
186 // setup stas.
187 wifiMac.SetType ("ns3::StaWifiMac",
188 "ActiveProbing", BooleanValue (true),
189 "Ssid", SsidValue (ssid));
190 staDevs = wifi.Install (wifiPhy, wifiMac, stas);
191 // setup ap.
192 wifiMac.SetType ("ns3::ApWifiMac",
193 "Ssid", SsidValue (ssid));
194 wifi.Install (wifiPhy, wifiMac, ap);
195
196 // mobility.
197 mobility.Install (stas);
198 mobility.Install (ap);
199
200 Simulator::Schedule (Seconds (1.0), &AdvancePosition, ap.Get (0));
201
202 PacketSocketAddress socket;
203 socket.SetSingleDevice (staDevs.Get (0)->GetIfIndex ());
204 socket.SetPhysicalAddress (staDevs.Get (1)->GetAddress ());
205 socket.SetProtocol (1);
206
207 OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
208 onoff.SetConstantRate (DataRate ("500kb/s"));
209
210 ApplicationContainer apps = onoff.Install (stas.Get (0));
211 apps.Start (Seconds (0.5));
212 apps.Stop (Seconds (43.0));
213
214 Simulator::Stop (Seconds (44.0));
215
216 Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback (&DevTxTrace));
217 Config::Connect ("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback (&DevRxTrace));
218 Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback (&PhyRxOkTrace));
219 Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback (&PhyRxErrorTrace));
220 Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback (&PhyTxTrace));
221 Config::Connect ("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback (&PhyStateTrace));
222
223 AthstatsHelper athstats;
224 athstats.EnableAthstats ("athstats-sta", stas);
225 athstats.EnableAthstats ("athstats-ap", ap);
226
228
229 Simulator::Destroy ();
230
231 return 0;
232}
void Run(ObjectFactory &factory, uint32_t pop, uint32_t total, uint32_t runs, Ptr< RandomVariableStream > eventStream, bool calRev)
Perform the runs for a single scheduler type.
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:229
Class for representing data rates.
Definition: data-rate.h:89
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(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
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.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create WifiNetDevice objects
Definition: wifi-helper.h:322
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
represent a single transmission mode
Definition: wifi-mode.h:48
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create(void) 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:920
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1245
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:661
cmd
Definition: second.py:43
ssid
Definition: third.py:88
wifi
Definition: third.py:90
mobility
Definition: third.py:98
def start()
Definition: core.py:1852
void PhyRxErrorTrace(std::string context, Ptr< const Packet > packet, double snr)
PHY-level RX error trace.
Definition: wifi-ap.cc:96
void PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
PHY state trace.
Definition: wifi-ap.cc:131
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
PHY-level RX OK trace.
Definition: wifi-ap.cc:80
void PhyTxTrace(std::string context, Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
PHY-level TX trace.
Definition: wifi-ap.cc:114
static bool g_verbose
True for verbose output.
Definition: wifi-ap.cc:38
void DevTxTrace(std::string context, Ptr< const Packet > p)
MAC-level TX trace.
Definition: wifi-ap.cc:47
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:145
void DevRxTrace(std::string context, Ptr< const Packet > p)
MAC-level RX trace.
Definition: wifi-ap.cc:62
WifiPhyState
The state of the PHY layer.