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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#include "ns3/athstats-helper.h"
10#include "ns3/boolean.h"
11#include "ns3/command-line.h"
12#include "ns3/config.h"
13#include "ns3/mobility-helper.h"
14#include "ns3/mobility-model.h"
15#include "ns3/on-off-helper.h"
16#include "ns3/packet-socket-address.h"
17#include "ns3/packet-socket-helper.h"
18#include "ns3/ssid.h"
19#include "ns3/string.h"
20#include "ns3/yans-wifi-channel.h"
21#include "ns3/yans-wifi-helper.h"
22
23using namespace ns3;
24
25/// True for verbose output.
26static bool g_verbose = true;
27
28/**
29 * MAC-level TX trace.
30 *
31 * \param context The context.
32 * \param p The packet.
33 */
34void
35DevTxTrace(std::string context, Ptr<const Packet> p)
36{
37 if (g_verbose)
38 {
39 std::cout << " TX p: " << *p << std::endl;
40 }
41}
42
43/**
44 * MAC-level RX trace.
45 *
46 * \param context The context.
47 * \param p The packet.
48 */
49void
50DevRxTrace(std::string context, Ptr<const Packet> p)
51{
52 if (g_verbose)
53 {
54 std::cout << " RX p: " << *p << std::endl;
55 }
56}
57
58/**
59 * PHY-level RX OK trace
60 *
61 * \param context The context.
62 * \param packet The packet.
63 * \param snr The SNR.
64 * \param mode The wifi mode.
65 * \param preamble The preamble.
66 */
67void
68PhyRxOkTrace(std::string context,
69 Ptr<const Packet> packet,
70 double snr,
71 WifiMode mode,
72 WifiPreamble preamble)
73{
74 if (g_verbose)
75 {
76 std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
77 }
78}
79
80/**
81 * PHY-level RX error trace
82 *
83 * \param context The context.
84 * \param packet The packet.
85 * \param snr The SNR.
86 */
87void
88PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
89{
90 if (g_verbose)
91 {
92 std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
93 }
94}
95
96/**
97 * PHY-level TX trace.
98 *
99 * \param context The context.
100 * \param packet The packet.
101 * \param mode The wifi mode.
102 * \param preamble The preamble.
103 * \param txPower The TX power.
104 */
105void
106PhyTxTrace(std::string context,
107 Ptr<const Packet> packet,
108 WifiMode mode,
109 WifiPreamble preamble,
110 uint8_t txPower)
111{
112 if (g_verbose)
113 {
114 std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
115 }
116}
117
118/**
119 * PHY state trace.
120 *
121 * \param context The context.
122 * \param start Start time of the state.
123 * \param duration Duration of the state.
124 * \param state The state.
125 */
126void
127PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
128{
129 if (g_verbose)
130 {
131 std::cout << " state=" << state << " start=" << start << " duration=" << duration
132 << std::endl;
133 }
134}
135
136/**
137 * Move a node position by 5m on the x axis every second, up to 210m.
138 *
139 * \param node The node.
140 */
141static void
143{
144 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
145 Vector pos = mobility->GetPosition();
146 pos.x += 5.0;
147 if (pos.x >= 210.0)
148 {
149 return;
150 }
151 mobility->SetPosition(pos);
152
154}
155
156int
157main(int argc, char* argv[])
158{
159 CommandLine cmd(__FILE__);
160 cmd.AddValue("verbose", "Print trace information if true", g_verbose);
161 cmd.Parse(argc, argv);
162
164
167 NodeContainer stas;
168 NodeContainer ap;
169 NetDeviceContainer staDevs;
170 PacketSocketHelper packetSocket;
171
172 stas.Create(2);
173 ap.Create(1);
174
175 // give packet socket powers to nodes.
176 packetSocket.Install(stas);
177 packetSocket.Install(ap);
178
179 WifiMacHelper wifiMac;
180 YansWifiPhyHelper wifiPhy;
182 wifiPhy.SetChannel(wifiChannel.Create());
183 Ssid ssid = Ssid("wifi-default");
184 // setup stas.
185 wifiMac.SetType("ns3::StaWifiMac",
186 "ActiveProbing",
187 BooleanValue(true),
188 "Ssid",
189 SsidValue(ssid));
190 staDevs = wifi.Install(wifiPhy, wifiMac, stas);
191 // setup ap.
192 wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
193 wifi.Install(wifiPhy, wifiMac, ap);
194
195 // mobility.
196 mobility.Install(stas);
197 mobility.Install(ap);
198
200
201 PacketSocketAddress socket;
202 socket.SetSingleDevice(staDevs.Get(0)->GetIfIndex());
203 socket.SetPhysicalAddress(staDevs.Get(1)->GetAddress());
204 socket.SetProtocol(1);
205
206 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
207 onoff.SetConstantRate(DataRate("500kb/s"));
208
209 ApplicationContainer apps = onoff.Install(stas.Get(0));
210 apps.Start(Seconds(0.5));
211 apps.Stop(Seconds(43.0));
212
214
215 Config::Connect("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback(&DevTxTrace));
216 Config::Connect("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback(&DevRxTrace));
217 Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback(&PhyRxOkTrace));
218 Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback(&PhyRxErrorTrace));
219 Config::Connect("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback(&PhyTxTrace));
220 Config::Connect("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback(&PhyStateTrace));
221
222 AthstatsHelper athstats;
223 athstats.EnableAthstats("athstats-sta", stas);
224 athstats.EnableAthstats("athstats-ap", ap);
225
227
229
230 return 0;
231}
a polymophic address class
Definition address.h:90
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.
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
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.
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
represent a single transmission mode
Definition wifi-mode.h:40
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:967
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684
ssid
Definition third.py:82
wifi
Definition third.py:84
mobility
Definition third.py:92
void PhyRxErrorTrace(std::string context, Ptr< const Packet > packet, double snr)
PHY-level RX error trace.
Definition wifi-ap.cc:88
void PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
PHY state trace.
Definition wifi-ap.cc:127
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
PHY-level RX OK trace.
Definition wifi-ap.cc:68
void PhyTxTrace(std::string context, Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
PHY-level TX trace.
Definition wifi-ap.cc:106
static bool g_verbose
True for verbose output.
Definition wifi-ap.cc:26
void DevTxTrace(std::string context, Ptr< const Packet > p)
MAC-level TX trace.
Definition wifi-ap.cc:35
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:142
void DevRxTrace(std::string context, Ptr< const Packet > p)
MAC-level RX trace.
Definition wifi-ap.cc:50