A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-simple-adhoc.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 adhoc mode, and by default, sends one packet of 1000
10// (application) bytes to the other node. The physical layer is configured
11// to receive at a fixed RSS (regardless of the distance and transmit
12// power); therefore, changing position of the nodes has no effect.
13//
14// There are a number of command-line options available to control
15// the default behavior. The list of available command-line options
16// can be listed with the following command:
17// ./ns3 run "wifi-simple-adhoc --help"
18//
19// For instance, for this configuration, the physical layer will
20// stop successfully receiving packets when rss drops below -97 dBm.
21// To see this effect, try running:
22//
23// ./ns3 run "wifi-simple-adhoc --rss=-97 --numPackets=20"
24// ./ns3 run "wifi-simple-adhoc --rss=-98 --numPackets=20"
25// ./ns3 run "wifi-simple-adhoc --rss=-99 --numPackets=20"
26//
27// Note that all ns-3 attributes (not just the ones exposed in the below
28// script) can be changed at command line; see the documentation.
29//
30// This script can also be helpful to put the Wifi layer into verbose
31// logging mode; this command will turn on all wifi logging:
32//
33// ./ns3 run "wifi-simple-adhoc --verbose=1"
34//
35// When you are done, you will notice two pcap trace files in your directory.
36// If you have tcpdump installed, you can try this:
37//
38// tcpdump -r wifi-simple-adhoc-0-0.pcap -nn -tt
39//
40
41#include "ns3/command-line.h"
42#include "ns3/config.h"
43#include "ns3/double.h"
44#include "ns3/internet-stack-helper.h"
45#include "ns3/ipv4-address-helper.h"
46#include "ns3/log.h"
47#include "ns3/mobility-helper.h"
48#include "ns3/mobility-model.h"
49#include "ns3/string.h"
50#include "ns3/yans-wifi-channel.h"
51#include "ns3/yans-wifi-helper.h"
52
53using namespace ns3;
54
55NS_LOG_COMPONENT_DEFINE("WifiSimpleAdhoc");
56
57/**
58 * Function called when a packet is received.
59 *
60 * \param socket The receiving socket.
61 */
62void
64{
65 while (socket->Recv())
66 {
67 NS_LOG_UNCOND("Received one packet!");
68 }
69}
70
71/**
72 * Generate traffic.
73 *
74 * \param socket The sending socket.
75 * \param pktSize The packet size.
76 * \param pktCount The packet count.
77 * \param pktInterval The interval between two packets.
78 */
79static void
81{
82 if (pktCount > 0)
83 {
84 socket->Send(Create<Packet>(pktSize));
85 Simulator::Schedule(pktInterval,
87 socket,
88 pktSize,
89 pktCount - 1,
90 pktInterval);
91 }
92 else
93 {
94 socket->Close();
95 }
96}
97
98int
99main(int argc, char* argv[])
100{
101 std::string phyMode("DsssRate1Mbps");
102 dBm_u rss{-80};
103 uint32_t packetSize{1000}; // bytes
104 uint32_t numPackets{1};
105 Time interPacketInterval{"1s"};
106 bool verbose{false};
107
108 CommandLine cmd(__FILE__);
109 cmd.AddValue("phyMode", "Wifi Phy mode", phyMode);
110 cmd.AddValue("rss", "received signal strength", rss);
111 cmd.AddValue("packetSize", "size of application packet sent", packetSize);
112 cmd.AddValue("numPackets", "number of packets generated", numPackets);
113 cmd.AddValue("interval", "interval between packets", interPacketInterval);
114 cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
115 cmd.Parse(argc, argv);
116
117 // Fix non-unicast data rate to be the same as that of unicast
118 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue(phyMode));
119
121 c.Create(2);
122
123 // The below set of helpers will help us to put together the wifi NICs we want
125 if (verbose)
126 {
127 WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
128 }
129 wifi.SetStandard(WIFI_STANDARD_80211b);
130
131 YansWifiPhyHelper wifiPhy;
132 // This is one parameter that matters when using FixedRssLossModel
133 // set it to zero; otherwise, gain will be added
134 wifiPhy.Set("RxGain", DoubleValue(0));
135 // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
137
138 YansWifiChannelHelper wifiChannel;
139 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
140 // The below FixedRssLossModel will cause the rss to be fixed regardless
141 // of the distance between the two stations, and the transmit power
142 wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(rss));
143 wifiPhy.SetChannel(wifiChannel.Create());
144
145 // Add a mac and disable rate control
146 WifiMacHelper wifiMac;
147 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
148 "DataMode",
149 StringValue(phyMode),
150 "ControlMode",
151 StringValue(phyMode));
152 // Set it to adhoc mode
153 wifiMac.SetType("ns3::AdhocWifiMac");
154 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c);
155
156 // Note that with FixedRssLossModel, the positions below are not
157 // used for received signal strength.
160 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
161 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
162 mobility.SetPositionAllocator(positionAlloc);
163 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
164 mobility.Install(c);
165
167 internet.Install(c);
168
170 NS_LOG_INFO("Assign IP Addresses.");
171 ipv4.SetBase("10.1.1.0", "255.255.255.0");
172 Ipv4InterfaceContainer i = ipv4.Assign(devices);
173
174 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
175 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(0), tid);
177 recvSink->Bind(local);
178 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
179
180 Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
181 InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
182 source->SetAllowBroadcast(true);
183 source->Connect(remote);
184
185 // Tracing
186 wifiPhy.EnablePcap("wifi-simple-adhoc", devices);
187
188 // Output what we are doing
189 NS_LOG_UNCOND("Testing " << numPackets << " packets sent with receiver rss " << rss);
190
191 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
192 Seconds(1.0),
194 source,
196 numPackets,
197 interPacketInterval);
198
201
202 return 0;
203}
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 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
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_UNCOND(msg)
Output the requested message unconditionally.
#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
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.