A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-simple-infra.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8// This script configures two nodes on an 802.11b physical layer, with
9// 802.11b NICs in infrastructure mode, and by default, the station sends
10// one packet of 1000 (application) bytes to the access point. Unlike
11// the default physical layer configuration in which the path loss increases
12// (and the received signal strength decreases) as the distance between the
13// nodes increases, this example uses an artificial path loss model that
14// allows the configuration of the received signal strength (RSS) regardless
15// of other transmitter parameters (such as transmit power) or distance.
16// Therefore, changing position of the nodes has no effect.
17//
18// There are a number of command-line options available to control
19// the default behavior. The list of available command-line options
20// can be listed with the following command:
21// ./ns3 run "wifi-simple-infra --help"
22// Additional command-line options are available via the generic attribute
23// configuration system.
24//
25// For instance, for the default configuration, the physical layer will
26// stop successfully receiving packets when rss drops to -82 dBm or below.
27// To see this effect, try running:
28//
29// ./ns3 run "wifi-simple-infra --rss=-80 --numPackets=20"
30// ./ns3 run "wifi-simple-infra --rss=-81 --numPackets=20"
31// ./ns3 run "wifi-simple-infra --rss=-82 --numPackets=20"
32//
33// The last command (and any RSS value lower than this) results in no
34// packets received. This is due to the preamble detection model that
35// dominates the reception performance. By default, the
36// ThresholdPreambleDetectionModel is added to all WifiPhy objects, and this
37// model prevents reception unless the incoming signal has a RSS above its
38// 'MinimumRssi' value (default of -82 dBm) and has a SNR above the
39// 'Threshold' value (default of 4).
40//
41// If we relax these values, we can instead observe that signal reception
42// due to the 802.11b error model alone is much lower. For instance,
43// setting the MinimumRssi to -101 (around the thermal noise floor).
44// and the SNR Threshold to -10 dB, shows that the DsssErrorRateModel can
45// successfully decode at RSS values of -97 or -98 dBm.
46//
47// ./ns3 run "wifi-simple-infra --rss=-97 --numPackets=20
48// --ns3::ThresholdPreambleDetectionModel::Threshold=-10
49// --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
50// ./ns3 run "wifi-simple-infra --rss=-98 --numPackets=20
51// --ns3::ThresholdPreambleDetectionModel::Threshold=-10
52// --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
53// ./ns3 run "wifi-simple-infra --rss=-99 --numPackets=20
54// --ns3::ThresholdPreambleDetectionModel::Threshold=-10
55// --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
56
57//
58// Note that all ns-3 attributes (not just the ones exposed in the below
59// script) can be changed at command line; see the documentation.
60//
61// This script can also be helpful to put the Wifi layer into verbose
62// logging mode; this command will turn on all wifi logging:
63//
64// ./ns3 run "wifi-simple-infra --verbose=1"
65//
66// When you are done, you will notice two pcap trace files in your directory.
67// If you have tcpdump installed, you can try this:
68//
69// tcpdump -r wifi-simple-infra-0-0.pcap -nn -tt
70//
71
72#include "ns3/command-line.h"
73#include "ns3/config.h"
74#include "ns3/double.h"
75#include "ns3/internet-stack-helper.h"
76#include "ns3/ipv4-address-helper.h"
77#include "ns3/log.h"
78#include "ns3/mobility-helper.h"
79#include "ns3/mobility-model.h"
80#include "ns3/ssid.h"
81#include "ns3/string.h"
82#include "ns3/yans-wifi-channel.h"
83#include "ns3/yans-wifi-helper.h"
84
85using namespace ns3;
86
87NS_LOG_COMPONENT_DEFINE("WifiSimpleInfra");
88
89/**
90 * Function called when a packet is received.
91 *
92 * \param socket The receiving socket.
93 */
94void
96{
97 while (socket->Recv())
98 {
99 std::cout << "Received one packet!" << std::endl;
100 }
101}
102
103/**
104 * Generate traffic.
105 *
106 * \param socket The sending socket.
107 * \param pktSize The packet size.
108 * \param pktCount The packet count.
109 * \param pktInterval The interval between two packets.
110 */
111static void
113{
114 if (pktCount > 0)
115 {
116 NS_LOG_INFO("Generating one packet of size " << pktSize);
117 socket->Send(Create<Packet>(pktSize));
118 Simulator::Schedule(pktInterval,
120 socket,
121 pktSize,
122 pktCount - 1,
123 pktInterval);
124 }
125 else
126 {
127 socket->Close();
128 }
129}
130
131int
132main(int argc, char* argv[])
133{
134 std::string phyMode{"DsssRate1Mbps"};
135 dBm_u rss{-80};
136 uint32_t packetSize{1000}; // bytes
137 uint32_t numPackets{1};
138 Time interval{"1s"};
139 bool verbose{false};
140
141 CommandLine cmd(__FILE__);
142 cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
143 cmd.AddValue("rss", "received signal strength", rss);
144 cmd.AddValue("packetSize", "size of application packet sent", packetSize);
145 cmd.AddValue("numPackets", "number of packets generated", numPackets);
146 cmd.AddValue("interval", "interval between packets", interval);
147 cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
148 cmd.Parse(argc, argv);
149
150 // Fix non-unicast data rate to be the same as that of unicast
151 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
152
154 c.Create(2);
155
156 // The below set of helpers will help us to put together the wifi NICs we want
158 if (verbose)
159 {
160 WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
161 }
162 wifi.SetStandard(WIFI_STANDARD_80211b);
163
164 YansWifiPhyHelper wifiPhy;
165 // This is one parameter that matters when using FixedRssLossModel
166 // set it to zero; otherwise, gain will be added
167 wifiPhy.Set("RxGain", DoubleValue(0));
168 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
170
171 YansWifiChannelHelper wifiChannel;
172 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
173 // The below FixedRssLossModel will cause the rss to be fixed regardless
174 // of the distance between the two stations, and the transmit power
175 wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(rss));
176 wifiPhy.SetChannel(wifiChannel.Create());
177
178 // Add a mac and disable rate control
179 WifiMacHelper wifiMac;
180 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
181 "DataMode",
182 StringValue(phyMode),
183 "ControlMode",
184 StringValue(phyMode));
185
186 // Setup the rest of the MAC
187 Ssid ssid = Ssid("wifi-default");
188 // setup STA
189 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
190 NetDeviceContainer staDevice = wifi.Install(wifiPhy, wifiMac, c.Get(0));
191 NetDeviceContainer devices = staDevice;
192 // setup AP
193 wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
194 NetDeviceContainer apDevice = wifi.Install(wifiPhy, wifiMac, c.Get(1));
195 devices.Add(apDevice);
196
197 // Note that with FixedRssLossModel, the positions below are not
198 // used for received signal strength.
201 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
202 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
203 mobility.SetPositionAllocator(positionAlloc);
204 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
205 mobility.Install(c);
206
208 internet.Install(c);
209
211 ipv4.SetBase("10.1.1.0", "255.255.255.0");
212 Ipv4InterfaceContainer i = ipv4.Assign(devices);
213
214 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
215 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(0), tid);
217 recvSink->Bind(local);
218 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
219
220 Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
221 InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
222 source->SetAllowBroadcast(true);
223 source->Connect(remote);
224
225 // Tracing
226 wifiPhy.EnablePcap("wifi-simple-infra", devices);
227
228 // Output what we are doing
229 std::cout << "Testing " << numPackets << " packets sent with receiver rss " << rss << std::endl;
230
231 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
232 Seconds(1.0),
234 source,
236 numPackets,
237 interval);
238
242
243 return 0;
244}
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
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.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
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 ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
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
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
helps to create WifiNetDevice objects
static void EnableLogComponents(LogLevel logLevel=LOG_LEVEL_ALL)
Helper to enable all WifiNetDevice log components with one statement.
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
void Set(std::string name, const AttributeValue &v)
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
@ WIFI_STANDARD_80211b
devices
Definition first.py:31
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:684
ssid
Definition third.py:82
wifi
Definition third.py:84
mobility
Definition third.py:92
bool verbose
uint32_t pktSize
packet size used for the simulation (in bytes)
static const uint32_t packetSize
Packet size generated at the AP.
void ReceivePacket(Ptr< Socket > socket)
Function called when a packet is received.
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Generate traffic.