A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
34using namespace ns3;
35
36/// True for verbose output.
37static bool g_verbose = true;
38
39/**
40 * MAC-level TX trace.
41 *
42 * \param context The context.
43 * \param p The packet.
44 */
45void
46DevTxTrace(std::string context, Ptr<const Packet> p)
47{
48 if (g_verbose)
49 {
50 std::cout << " TX p: " << *p << std::endl;
51 }
52}
53
54/**
55 * MAC-level RX trace.
56 *
57 * \param context The context.
58 * \param p The packet.
59 */
60void
61DevRxTrace(std::string context, Ptr<const Packet> p)
62{
63 if (g_verbose)
64 {
65 std::cout << " RX p: " << *p << std::endl;
66 }
67}
68
69/**
70 * PHY-level RX OK trace
71 *
72 * \param context The context.
73 * \param packet The packet.
74 * \param snr The SNR.
75 * \param mode The wifi mode.
76 * \param preamble The preamble.
77 */
78void
79PhyRxOkTrace(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
91/**
92 * PHY-level RX error trace
93 *
94 * \param context The context.
95 * \param packet The packet.
96 * \param snr The SNR.
97 */
98void
99PhyRxErrorTrace(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
107/**
108 * PHY-level TX trace.
109 *
110 * \param context The context.
111 * \param packet The packet.
112 * \param mode The wifi mode.
113 * \param preamble The preamble.
114 * \param txPower The TX power.
115 */
116void
117PhyTxTrace(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
129/**
130 * PHY state trace.
131 *
132 * \param context The context.
133 * \param start Start time of the state.
134 * \param duration Duration of the state.
135 * \param state The state.
136 */
137void
138PhyStateTrace(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
147/**
148 * Move a node position by 5m on the x axis every second, up to 210m.
149 *
150 * \param node The node.
151 */
152static void
154{
155 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
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
167int
168main(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
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
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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
AttributeValue implementation for Ssid.
Definition: ssid.h:96
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:978
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.
WifiPhyState
The state of the PHY layer.
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
ns cmd
Definition: second.py:40
ns wifi
Definition: third.py:95
ns ssid
Definition: third.py:93
ns 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