A Discrete-Event Network Simulator
API
wifi-sleep.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  * 2014 Universita' degli Studi di Napoli "Federico II"
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 // This script configures two nodes on an 802.11b physical layer, with
22 // 802.11b NICs in adhoc mode. One of the nodes generates on-off traffic
23 // destined to the other node.
24 //
25 // The purpose is to test the energy depletion on the nodes and the
26 // activation of the callback that puts a node in the sleep state when
27 // its energy is depleted. Furthermore, this script can be used to test
28 // the available policies for updating the transmit current based on
29 // the nominal tx power used to transmit each frame.
30 //
31 // There are a number of command-line options available to control
32 // the default behavior. The list of available command-line options
33 // can be listed with the following command:
34 // ./waf --run "wifi-sleep --help"
35 //
36 // Note that all ns-3 attributes (not just the ones exposed in the below
37 // script) can be changed at command line; see the documentation.
38 //
39 // This script can also be helpful to put the Wifi layer into verbose
40 // logging mode; this command will turn on all wifi logging:
41 //
42 // ./waf --run "wifi-sleep --verbose=1"
43 //
44 // When you are done, you will notice four trace files in your directory:
45 // two for the remaining energy on each node and two for the state transitions
46 // of each node.
47 
48 #include "ns3/command-line.h"
49 #include "ns3/config.h"
50 #include "ns3/string.h"
51 #include "ns3/log.h"
52 #include "ns3/yans-wifi-helper.h"
53 #include "ns3/mobility-helper.h"
54 #include "ns3/ipv4-address-helper.h"
55 #include "ns3/yans-wifi-channel.h"
56 #include "ns3/mobility-model.h"
57 #include "ns3/internet-stack-helper.h"
58 #include "ns3/on-off-helper.h"
59 #include "ns3/packet-sink-helper.h"
60 #include "ns3/basic-energy-source-helper.h"
61 #include "ns3/wifi-radio-energy-model-helper.h"
62 #include "ns3/wifi-utils.h"
63 #include "ns3/wifi-net-device.h"
64 
65 using namespace ns3;
66 
67 NS_LOG_COMPONENT_DEFINE ("WifiSleep");
68 
69 template <int node>
70 void RemainingEnergyTrace (double oldValue, double newValue)
71 {
72  std::stringstream ss;
73  ss << "energy_" << node << ".log";
74 
75  static std::fstream f (ss.str ().c_str (), std::ios::out);
76 
77  f << Simulator::Now ().GetSeconds () << " remaining energy=" << newValue << std::endl;
78 }
79 
80 template <int node>
81 void PhyStateTrace (std::string context, Time start, Time duration, WifiPhyState state)
82 {
83  std::stringstream ss;
84  ss << "state_" << node << ".log";
85 
86  static std::fstream f (ss.str ().c_str (), std::ios::out);
87 
88  f << Simulator::Now ().GetSeconds () << " state=" << state << " start=" << start << " duration=" << duration << std::endl;
89 }
90 
91 int main (int argc, char *argv[])
92 {
93  std::string dataRate = "1Mbps";
94  uint32_t packetSize = 1000; // bytes
95  double duration = 10.0; // seconds
96  double initialEnergy = 7.5; // joule
97  double voltage = 3.0; // volts
98  double txPowerStart = 0.0; // dbm
99  double txPowerEnd = 15.0; // dbm
100  uint32_t nTxPowerLevels = 16;
101  uint32_t txPowerLevel = 0;
102  double idleCurrent = 0.273; // Ampere
103  double txCurrent = 0.380; // Ampere
104  bool verbose = false;
105 
106  CommandLine cmd (__FILE__);
107  cmd.AddValue ("dataRate", "Data rate", dataRate);
108  cmd.AddValue ("packetSize", "size of application packet sent", packetSize);
109  cmd.AddValue ("duration", "duration (seconds) of the experiment", duration);
110  cmd.AddValue ("initialEnergy", "Initial Energy (Joule) of each node", initialEnergy);
111  cmd.AddValue ("voltage", "Supply voltage (Joule)", voltage);
112  cmd.AddValue ("txPowerStart", "Minimum available transmission level (dbm)", txPowerStart);
113  cmd.AddValue ("txPowerEnd", "Maximum available transmission level (dbm)", txPowerEnd);
114  cmd.AddValue ("nTxPowerLevels", "Number of transmission power levels available between txPowerStart and txPowerEnd included", nTxPowerLevels);
115  cmd.AddValue ("txPowerLevel", "Transmission power level", txPowerLevel);
116  cmd.AddValue ("idleCurrent", "The radio Idle current in Ampere", idleCurrent);
117  cmd.AddValue ("txCurrent", "The radio Tx current in Ampere", txCurrent);
118  cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);
119  cmd.Parse (argc, argv);
120 
121  NodeContainer c;
122  c.Create (2);
123 
124  // The below set of helpers will help us to put together the wifi NICs we want
126  if (verbose)
127  {
128  wifi.EnableLogComponents (); // Turn on all Wifi logging
129  }
130  wifi.SetStandard (WIFI_STANDARD_80211b);
131 
132  YansWifiPhyHelper wifiPhy;
133  wifiPhy.Set ("TxPowerStart", DoubleValue (txPowerStart));
134  wifiPhy.Set ("TxPowerEnd", DoubleValue (txPowerEnd));
135  wifiPhy.Set ("TxPowerLevels", UintegerValue (nTxPowerLevels));
136 
138  wifiPhy.SetChannel (wifiChannel.Create ());
139 
140  // Add a mac and set the selected tx power level
141  WifiMacHelper wifiMac;
142  wifi.SetRemoteStationManager ("ns3::ArfWifiManager", "DefaultTxPowerLevel", UintegerValue (txPowerLevel));
143  // Set it to adhoc mode
144  wifiMac.SetType ("ns3::AdhocWifiMac");
145  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);
146 
148  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
149  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
150  positionAlloc->Add (Vector (10.0, 0.0, 0.0));
151  mobility.SetPositionAllocator (positionAlloc);
152  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
153  mobility.Install (c);
154 
155  InternetStackHelper internet;
156  internet.Install (c);
157 
158  Ipv4AddressHelper ipv4;
159  NS_LOG_INFO ("Assign IP Addresses.");
160  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
162 
164 
165  std::string transportProto = std::string ("ns3::UdpSocketFactory");
166  OnOffHelper onOff (transportProto, InetSocketAddress (Ipv4Address ("10.1.1.2"), 9000));
167 
168  onOff.SetAttribute ("DataRate", DataRateValue (DataRate (dataRate)));
169  onOff.SetAttribute ("PacketSize", UintegerValue (packetSize));
170  onOff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.001]"));
171 
172  apps = onOff.Install (c.Get (0));
173 
174  apps.Start (Seconds (0.01));
175  apps.Stop (Seconds (duration));
176 
177  // Create a packet sink to receive these packets
178  PacketSinkHelper sink (transportProto, InetSocketAddress (Ipv4Address::GetAny (), 9001));
179  apps = sink.Install (c.Get (1));
180  apps.Start (Seconds (0.01));
181  apps.Stop (Seconds (duration));
182 
183  // Energy sources
184  EnergySourceContainer eSources;
185  BasicEnergySourceHelper basicSourceHelper;
186  WifiRadioEnergyModelHelper radioEnergyHelper;
187 
188  basicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (initialEnergy));
189  basicSourceHelper.Set ("BasicEnergySupplyVoltageV", DoubleValue (voltage));
190 
191  radioEnergyHelper.Set ("IdleCurrentA", DoubleValue (idleCurrent));
192  radioEnergyHelper.Set ("TxCurrentA", DoubleValue (txCurrent));
193 
194  // compute the efficiency of the power amplifier (eta) assuming that the provided value for tx current
195  // corresponds to the minimum tx power level
196  double eta = DbmToW (txPowerStart) / ((txCurrent - idleCurrent) * voltage);
197 
198  radioEnergyHelper.SetTxCurrentModel ("ns3::LinearWifiTxCurrentModel",
199  "Voltage", DoubleValue (voltage),
200  "IdleCurrent", DoubleValue (idleCurrent),
201  "Eta", DoubleValue (eta));
202 
203  // install an energy source on each node
204  for (NodeContainer::Iterator n = c.Begin (); n != c.End (); n++)
205  {
206  eSources.Add (basicSourceHelper.Install (*n));
207 
208  Ptr<WifiNetDevice> wnd;
209 
210  for (uint32_t i = 0; i < (*n)->GetNDevices (); ++i)
211  {
212  wnd = (*n)->GetDevice (i)->GetObject<WifiNetDevice> ();
213  // if it is a WifiNetDevice
214  if (wnd != 0)
215  {
216  // this device draws power from the last created energy source
217  radioEnergyHelper.Install (wnd, eSources.Get (eSources.GetN () - 1));
218  }
219  }
220  }
221 
222  // Tracing
223  eSources.Get (0)->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergyTrace<0>));
224  eSources.Get (1)->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergyTrace<1>));
225 
226  Config::Connect ("/NodeList/0/DeviceList/*/Phy/State/State", MakeCallback (&PhyStateTrace<0>));
227  Config::Connect ("/NodeList/1/DeviceList/*/Phy/State/State", MakeCallback (&PhyStateTrace<1>));
228 
229  Simulator::Stop (Seconds (duration + 1));
230 
231  Simulator::Run ();
233 
234  return 0;
235 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::DeviceEnergyModelHelper::Install
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
Definition: energy-model-helper.cc:91
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::YansWifiChannelHelper::Default
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
Definition: yans-wifi-helper.cc:42
ns3::YansWifiPhyHelper
Make it easy to create and manage PHY objects for the YANS model.
Definition: yans-wifi-helper.h:161
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::ListPositionAllocator::Add
void Add(Vector v)
Add a position to the list of positions.
Definition: position-allocator.cc:70
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::WifiHelper
helps to create WifiNetDevice objects
Definition: wifi-helper.h:323
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::WifiRadioEnergyModelHelper::SetTxCurrentModel
void SetTxCurrentModel(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())
Definition: wifi-radio-energy-model-helper.cc:60
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ns3::YansWifiPhyHelper::SetChannel
void SetChannel(Ptr< YansWifiChannel > channel)
Definition: yans-wifi-helper.cc:135
first.devices
devices
Definition: first.py:39
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::WifiNetDevice
Hold together all Wifi-related objects.
Definition: wifi-net-device.h:54
ns3::BasicEnergySourceHelper::Set
void Set(std::string name, const AttributeValue &v)
Definition: basic-energy-source-helper.cc:36
ns3::EnergySourceContainer
Holds a vector of ns3::EnergySource pointers.
Definition: energy-source-container.h:45
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::EnergySourceHelper::Install
EnergySourceContainer Install(Ptr< Node > node) const
Definition: energy-model-helper.cc:35
third.wifi
wifi
Definition: third.py:96
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::DataRate
Class for representing data rates.
Definition: data-rate.h:89
visualizer.core.start
def start()
Definition: core.py:1855
ns3::EnergySourceContainer::Get
Ptr< EnergySource > Get(uint32_t i) const
Get the i-th Ptr<EnergySource> stored in this container.
Definition: energy-source-container.cc:88
ns3::YansWifiChannelHelper::Create
Ptr< YansWifiChannel > Create(void) const
Definition: yans-wifi-helper.cc:99
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::NodeContainer::Begin
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Definition: node-container.cc:77
ns3::BasicEnergySourceHelper
Creates a BasicEnergySource object.
Definition: basic-energy-source-helper.h:35
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::WifiRadioEnergyModelHelper
Assign WifiRadioEnergyModel to wifi devices.
Definition: wifi-radio-energy-model-helper.h:37
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::WIFI_STANDARD_80211b
@ WIFI_STANDARD_80211b
Definition: wifi-standards.h:128
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
verbose
bool verbose
Definition: openflow-switch.cc:50
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
second.cmd
cmd
Definition: second.py:35
RemainingEnergyTrace
void RemainingEnergyTrace(double oldValue, double newValue)
Definition: wifi-sleep.cc:70
sink
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
ns3::MakeCallback
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:1642
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition: packet-sink-helper.h:36
WifiPhyState
WifiPhyState
The state of the PHY layer.
Definition: wifi-phy-state.h:32
ns3::NodeContainer::Iterator
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Definition: node-container.h:42
ns3::Config::Connect
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition: ipv4-address.cc:395
ns3::EnergySourceContainer::GetN
uint32_t GetN(void) const
Get the number of Ptr<EnergySource> stored in this container.
Definition: energy-source-container.cc:82
packetSize
static const uint32_t packetSize
Definition: wifi-power-adaptation-distance.cc:113
ns3::DbmToW
double DbmToW(double dBm)
Convert from dBm to Watts.
Definition: wifi-utils.cc:37
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
f
double f(double x, void *params)
Definition: 80211b.c:70
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::WifiMacHelper::SetType
void SetType(std::string type, Args &&... args)
Definition: wifi-mac-helper.h:130
ns3::NodeContainer::End
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
Definition: node-container.cc:82
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::YansWifiChannelHelper
manage and create wifi channel objects for the YANS model.
Definition: yans-wifi-helper.h:37
ns3::WifiMacHelper
create MAC layers for a ns3::WifiNetDevice.
Definition: wifi-mac-helper.h:48
PhyStateTrace
void PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
Definition: wifi-sleep.cc:81
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::EnergySourceContainer::Add
void Add(EnergySourceContainer container)
Definition: energy-source-container.cc:94
ns3::WifiPhyHelper::Set
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:140
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
third.mobility
mobility
Definition: third.py:108
ns3::WifiRadioEnergyModelHelper::Set
void Set(std::string name, const AttributeValue &v) override
Definition: wifi-radio-energy-model-helper.cc:40