A Discrete-Event Network Simulator
API
wifi-simple-infra.cc
Go to the documentation of this file.
1/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2009 The Boeing Company
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 */
19
20// This script configures two nodes on an 802.11b physical layer, with
21// 802.11b NICs in infrastructure mode, and by default, the station sends
22// one packet of 1000 (application) bytes to the access point. Unlike
23// the default physical layer configuration in which the path loss increases
24// (and the received signal strength decreases) as the distance between the
25// nodes increases, this example uses an artificial path loss model that
26// allows the configuration of the received signal strength (RSS) regardless
27// of other transmitter parameters (such as transmit power) or distance.
28// Therefore, changing position of the nodes has no effect.
29//
30// There are a number of command-line options available to control
31// the default behavior. The list of available command-line options
32// can be listed with the following command:
33// ./ns3 run "wifi-simple-infra --help"
34// Additional command-line options are available via the generic attribute
35// configuration system.
36//
37// For instance, for the default configuration, the physical layer will
38// stop successfully receiving packets when rss drops to -82 dBm or below.
39// To see this effect, try running:
40//
41// ./ns3 run "wifi-simple-infra --rss=-80 --numPackets=20"
42// ./ns3 run "wifi-simple-infra --rss=-81 --numPackets=20"
43// ./ns3 run "wifi-simple-infra --rss=-82 --numPackets=20"
44//
45// The last command (and any RSS value lower than this) results in no
46// packets received. This is due to the preamble detection model that
47// dominates the reception performance. By default, the
48// ThresholdPreambleDetectionModel is added to all WifiPhy objects, and this
49// model prevents reception unless the incoming signal has a RSS above its
50// 'MinimumRssi' value (default of -82 dBm) and has a SNR above the
51// 'Threshold' value (default of 4).
52//
53// If we relax these values, we can instead observe that signal reception
54// due to the 802.11b error model alone is much lower. For instance,
55// setting the MinimumRssi to -101 (around the thermal noise floor).
56// and the SNR Threshold to -10 dB, shows that the DsssErrorRateModel can
57// successfully decode at RSS values of -97 or -98 dBm.
58//
59// ./ns3 run "wifi-simple-infra --rss=-97 --numPackets=20 --ns3::ThresholdPreambleDetectionModel::Threshold=-10 --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
60// ./ns3 run "wifi-simple-infra --rss=-98 --numPackets=20 --ns3::ThresholdPreambleDetectionModel::Threshold=-10 --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
61// ./ns3 run "wifi-simple-infra --rss=-99 --numPackets=20 --ns3::ThresholdPreambleDetectionModel::Threshold=-10 --ns3::ThresholdPreambleDetectionModel::MinimumRssi=-101"
62
63//
64// Note that all ns-3 attributes (not just the ones exposed in the below
65// script) can be changed at command line; see the documentation.
66//
67// This script can also be helpful to put the Wifi layer into verbose
68// logging mode; this command will turn on all wifi logging:
69//
70// ./ns3 run "wifi-simple-infra --verbose=1"
71//
72// When you are done, you will notice two pcap trace files in your directory.
73// If you have tcpdump installed, you can try this:
74//
75// tcpdump -r wifi-simple-infra-0-0.pcap -nn -tt
76//
77
78#include "ns3/command-line.h"
79#include "ns3/config.h"
80#include "ns3/double.h"
81#include "ns3/string.h"
82#include "ns3/log.h"
83#include "ns3/yans-wifi-helper.h"
84#include "ns3/ssid.h"
85#include "ns3/mobility-helper.h"
86#include "ns3/ipv4-address-helper.h"
87#include "ns3/yans-wifi-channel.h"
88#include "ns3/mobility-model.h"
89#include "ns3/internet-stack-helper.h"
90
91using namespace ns3;
92
93NS_LOG_COMPONENT_DEFINE ("WifiSimpleInfra");
94
96{
97 while (socket->Recv ())
98 {
99 std::cout << "Received one packet!" << std::endl;
100 }
101}
102
104 uint32_t pktCount, Time pktInterval )
105{
106 if (pktCount > 0)
107 {
108 NS_LOG_INFO ("Generating one packet of size " << pktSize);
109 socket->Send (Create<Packet> (pktSize));
110 Simulator::Schedule (pktInterval, &GenerateTraffic,
111 socket, pktSize, pktCount - 1, pktInterval);
112 }
113 else
114 {
115 socket->Close ();
116 }
117}
118
119int main (int argc, char *argv[])
120{
121 std::string phyMode ("DsssRate1Mbps");
122 double rss = -80; // -dBm
123 uint32_t packetSize = 1000; // bytes
124 uint32_t numPackets = 1;
125 Time interval = Seconds (1.0);
126 bool verbose = false;
127
128 CommandLine cmd (__FILE__);
129 cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
130 cmd.AddValue ("rss", "received signal strength", rss);
131 cmd.AddValue ("packetSize", "size of application packet sent", packetSize);
132 cmd.AddValue ("numPackets", "number of packets generated", numPackets);
133 cmd.AddValue ("interval", "interval between packets", interval);
134 cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);
135 cmd.Parse (argc, argv);
136
137 // Fix non-unicast data rate to be the same as that of unicast
138 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
139 StringValue (phyMode));
140
142 c.Create (2);
143
144 // The below set of helpers will help us to put together the wifi NICs we want
146 if (verbose)
147 {
148 wifi.EnableLogComponents (); // Turn on all Wifi logging
149 }
150 wifi.SetStandard (WIFI_STANDARD_80211b);
151
152 YansWifiPhyHelper wifiPhy;
153 // This is one parameter that matters when using FixedRssLossModel
154 // set it to zero; otherwise, gain will be added
155 wifiPhy.Set ("RxGain", DoubleValue (0) );
156 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
157 wifiPhy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
158
159 YansWifiChannelHelper wifiChannel;
160 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
161 // The below FixedRssLossModel will cause the rss to be fixed regardless
162 // of the distance between the two stations, and the transmit power
163 wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rss));
164 wifiPhy.SetChannel (wifiChannel.Create ());
165
166 // Add a mac and disable rate control
167 WifiMacHelper wifiMac;
168 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
169 "DataMode", StringValue (phyMode),
170 "ControlMode", StringValue (phyMode));
171
172 // Setup the rest of the MAC
173 Ssid ssid = Ssid ("wifi-default");
174 // setup STA
175 wifiMac.SetType ("ns3::StaWifiMac",
176 "Ssid", SsidValue (ssid));
177 NetDeviceContainer staDevice = wifi.Install (wifiPhy, wifiMac, c.Get (0));
178 NetDeviceContainer devices = staDevice;
179 // setup AP
180 wifiMac.SetType ("ns3::ApWifiMac",
181 "Ssid", SsidValue (ssid));
182 NetDeviceContainer apDevice = wifi.Install (wifiPhy, wifiMac, c.Get (1));
183 devices.Add (apDevice);
184
185 // Note that with FixedRssLossModel, the positions below are not
186 // used for received signal strength.
188 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
189 positionAlloc->Add (Vector (0.0, 0.0, 0.0));
190 positionAlloc->Add (Vector (5.0, 0.0, 0.0));
191 mobility.SetPositionAllocator (positionAlloc);
192 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
193 mobility.Install (c);
194
195 InternetStackHelper internet;
196 internet.Install (c);
197
199 ipv4.SetBase ("10.1.1.0", "255.255.255.0");
201
202 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
203 Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (0), tid);
204 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
205 recvSink->Bind (local);
207
208 Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
209 InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
210 source->SetAllowBroadcast (true);
211 source->Connect (remote);
212
213 // Tracing
214 wifiPhy.EnablePcap ("wifi-simple-infra", devices);
215
216 // Output what we are doing
217 std::cout << "Testing " << numPackets << " packets sent with receiver rss " << rss << std::endl;
218
219 Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
220 Seconds (1.0), &GenerateTraffic,
221 source, packetSize, numPackets, interval);
222
223 Simulator::Stop (Seconds (30.0));
224 Simulator::Run ();
225 Simulator::Destroy ();
226
227 return 0;
228}
Parse command-line arguments.
Definition: command-line.h:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
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.
uint32_t GetId(void) const
Definition: node.cc:109
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Close(void)=0
Close a socket.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Hold variables of type string.
Definition: string.h:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create WifiNetDevice objects
Definition: wifi-helper.h:323
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.
Definition: wifi-helper.cc:574
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:141
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, 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())
Ptr< YansWifiChannel > Create(void) const
void AddPropagationLoss(std::string name, 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())
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:849
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
@ WIFI_STANDARD_80211b
devices
Definition: first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
ssid
Definition: third.py:97
wifi
Definition: third.py:99
mobility
Definition: third.py:107
bool verbose
uint32_t pktSize
packet size used for the simulation (in bytes)
Definition: wifi-bianchi.cc:89
static const uint32_t packetSize
Pcket size generated at the AP.
void ReceivePacket(Ptr< Socket > socket)
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)