Files

255 lines
9.0 KiB
C++
Raw Permalink Normal View History

2014-09-05 16:33:57 -07:00
/*
* Copyright (c) 2009 The Boeing Company
* 2014 Universita' degli Studi di Napoli "Federico II"
*
2024-06-17 16:17:10 +02:00
* SPDX-License-Identifier: GPL-2.0-only
2014-09-05 16:33:57 -07:00
*
*/
// This script configures two nodes on an 802.11b physical layer, with
// 802.11b NICs in adhoc mode. One of the nodes generates on-off traffic
// destined to the other node.
//
2017-02-06 20:31:02 +01:00
// The purpose is to test the energy depletion on the nodes and the
2014-09-05 16:33:57 -07:00
// activation of the callback that puts a node in the sleep state when
// its energy is depleted. Furthermore, this script can be used to test
// the available policies for updating the transmit current based on
// the nominal tx power used to transmit each frame.
//
// There are a number of command-line options available to control
// the default behavior. The list of available command-line options
// can be listed with the following command:
2022-01-13 23:59:59 -03:00
// ./ns3 run "wifi-sleep --help"
2014-09-05 16:33:57 -07:00
//
// Note that all ns-3 attributes (not just the ones exposed in the below
// script) can be changed at command line; see the documentation.
//
// This script can also be helpful to put the Wifi layer into verbose
// logging mode; this command will turn on all wifi logging:
2017-02-06 20:31:02 +01:00
//
2022-01-13 23:59:59 -03:00
// ./ns3 run "wifi-sleep --verbose=1"
2014-09-05 16:33:57 -07:00
//
// When you are done, you will notice four trace files in your directory:
// two for the remaining energy on each node and two for the state transitions
// of each node.
2022-10-07 20:08:35 +00:00
#include "ns3/basic-energy-source-helper.h"
#include "ns3/command-line.h"
#include "ns3/config.h"
2022-10-07 20:08:35 +00:00
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/log.h"
#include "ns3/mobility-helper.h"
#include "ns3/mobility-model.h"
#include "ns3/on-off-helper.h"
#include "ns3/packet-sink-helper.h"
2022-10-07 20:08:35 +00:00
#include "ns3/string.h"
#include "ns3/wifi-net-device.h"
#include "ns3/wifi-radio-energy-model-helper.h"
#include "ns3/wifi-utils.h"
2022-10-07 20:08:35 +00:00
#include "ns3/yans-wifi-channel.h"
#include "ns3/yans-wifi-helper.h"
2014-09-05 16:33:57 -07:00
using namespace ns3;
2024-04-23 21:34:27 +02:00
using namespace ns3::energy;
2014-09-05 16:33:57 -07:00
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("WifiSleep");
2022-08-28 13:24:25 -05:00
/**
* Remaining energy trace sink
*
2024-11-08 18:05:46 +00:00
* @tparam node The node ID this trace belongs to.
* @param oldValue Old value.
* @param newValue New value.
2022-08-28 13:24:25 -05:00
*/
2014-09-05 16:33:57 -07:00
template <int node>
2022-10-07 20:08:35 +00:00
void
RemainingEnergyTrace(double oldValue, double newValue)
2014-09-05 16:33:57 -07:00
{
2022-10-07 20:08:35 +00:00
std::stringstream ss;
ss << "energy_" << node << ".log";
2014-09-05 16:33:57 -07:00
static std::fstream f(ss.str(), std::ios::out);
2014-09-05 16:33:57 -07:00
2022-10-07 20:08:35 +00:00
f << Simulator::Now().GetSeconds() << " remaining energy=" << newValue << std::endl;
2014-09-05 16:33:57 -07:00
}
2022-08-28 13:24:25 -05:00
/**
* PHY state trace sink
*
2024-11-08 18:05:46 +00:00
* @tparam node The node ID this trace belongs to.
* @param context The context
* @param start Start time for the current state
* @param duration Duratio of the current state
* @param state State
2022-08-28 13:24:25 -05:00
*/
2014-09-05 16:33:57 -07:00
template <int node>
2022-10-07 20:08:35 +00:00
void
PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
2014-09-05 16:33:57 -07:00
{
2022-10-07 20:08:35 +00:00
std::stringstream ss;
ss << "state_" << node << ".log";
2014-09-05 16:33:57 -07:00
static std::fstream f(ss.str(), std::ios::out);
2014-09-05 16:33:57 -07:00
2022-10-07 20:08:35 +00:00
f << Simulator::Now().GetSeconds() << " state=" << state << " start=" << start
<< " duration=" << duration << std::endl;
2014-09-05 16:33:57 -07:00
}
2022-10-07 20:08:35 +00:00
int
main(int argc, char* argv[])
2014-09-05 16:33:57 -07:00
{
DataRate dataRate{"1Mb/s"};
uint32_t packetSize{1000}; // bytes
Time duration{"10s"};
2024-06-28 12:00:31 +02:00
joule_u initialEnergy{7.5};
2024-06-13 20:58:50 +02:00
volt_u voltage{3.0};
dBm_u txPowerStart{0.0};
dBm_u txPowerEnd{15.0};
uint32_t nTxPowerLevels{16};
uint32_t txPowerLevel{0};
2024-06-13 20:56:55 +02:00
ampere_u idleCurrent{0.273};
ampere_u txCurrent{0.380};
bool verbose{false};
2022-10-07 20:08:35 +00:00
CommandLine cmd(__FILE__);
cmd.AddValue("dataRate", "Data rate", dataRate);
cmd.AddValue("packetSize", "size of application packet sent", packetSize);
cmd.AddValue("duration", "duration of the experiment", duration);
2022-10-07 20:08:35 +00:00
cmd.AddValue("initialEnergy", "Initial Energy (Joule) of each node", initialEnergy);
cmd.AddValue("voltage", "Supply voltage (Joule)", voltage);
cmd.AddValue("txPowerStart", "Minimum available transmission level (dbm)", txPowerStart);
cmd.AddValue("txPowerEnd", "Maximum available transmission level (dbm)", txPowerEnd);
cmd.AddValue("nTxPowerLevels",
"Number of transmission power levels available between txPowerStart and "
"txPowerEnd included",
nTxPowerLevels);
cmd.AddValue("txPowerLevel", "Transmission power level", txPowerLevel);
cmd.AddValue("idleCurrent", "The radio Idle current in Ampere", idleCurrent);
cmd.AddValue("txCurrent", "The radio Tx current in Ampere", txCurrent);
cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose);
cmd.Parse(argc, argv);
NodeContainer c;
c.Create(2);
// The below set of helpers will help us to put together the wifi NICs we want
WifiHelper wifi;
if (verbose)
2014-09-05 16:33:57 -07:00
{
WifiHelper::EnableLogComponents(); // Turn on all Wifi logging
2014-09-05 16:33:57 -07:00
}
2022-10-07 20:08:35 +00:00
wifi.SetStandard(WIFI_STANDARD_80211b);
YansWifiPhyHelper wifiPhy;
wifiPhy.Set("TxPowerStart", DoubleValue(txPowerStart));
wifiPhy.Set("TxPowerEnd", DoubleValue(txPowerEnd));
wifiPhy.Set("TxPowerLevels", UintegerValue(nTxPowerLevels));
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
wifiPhy.SetChannel(wifiChannel.Create());
// Add a mac and set the selected tx power level
WifiMacHelper wifiMac;
wifi.SetRemoteStationManager("ns3::ArfWifiManager",
"DefaultTxPowerLevel",
UintegerValue(txPowerLevel));
// Set it to adhoc mode
wifiMac.SetType("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, c);
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
positionAlloc->Add(Vector(0.0, 0.0, 0.0));
positionAlloc->Add(Vector(10.0, 0.0, 0.0));
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(c);
InternetStackHelper internet;
internet.Install(c);
Ipv4AddressHelper ipv4;
NS_LOG_INFO("Assign IP Addresses.");
ipv4.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign(devices);
ApplicationContainer apps;
std::string transportProto = std::string("ns3::UdpSocketFactory");
OnOffHelper onOff(transportProto, InetSocketAddress(Ipv4Address("10.1.1.2"), 9000));
onOff.SetAttribute("DataRate", DataRateValue(DataRate(dataRate)));
onOff.SetAttribute("PacketSize", UintegerValue(packetSize));
onOff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0.001]"));
apps = onOff.Install(c.Get(0));
apps.Start(Seconds(0.01));
apps.Stop(duration);
2022-10-07 20:08:35 +00:00
// Create a packet sink to receive these packets
PacketSinkHelper sink(transportProto, InetSocketAddress(Ipv4Address::GetAny(), 9001));
apps = sink.Install(c.Get(1));
apps.Start(Seconds(0.01));
apps.Stop(duration);
2022-10-07 20:08:35 +00:00
// Energy sources
EnergySourceContainer eSources;
BasicEnergySourceHelper basicSourceHelper;
WifiRadioEnergyModelHelper radioEnergyHelper;
basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(initialEnergy));
basicSourceHelper.Set("BasicEnergySupplyVoltageV", DoubleValue(voltage));
radioEnergyHelper.Set("IdleCurrentA", DoubleValue(idleCurrent));
radioEnergyHelper.Set("TxCurrentA", DoubleValue(txCurrent));
// compute the efficiency of the power amplifier (eta) assuming that the provided value for tx
// current corresponds to the minimum tx power level
double eta = DbmToW(txPowerStart) / ((txCurrent - idleCurrent) * voltage);
radioEnergyHelper.SetTxCurrentModel("ns3::LinearWifiTxCurrentModel",
"Voltage",
DoubleValue(voltage),
"IdleCurrent",
DoubleValue(idleCurrent),
"Eta",
DoubleValue(eta));
// install an energy source on each node
for (auto n = c.Begin(); n != c.End(); n++)
2014-09-05 16:33:57 -07:00
{
2022-10-07 20:08:35 +00:00
eSources.Add(basicSourceHelper.Install(*n));
2017-02-06 20:31:02 +01:00
2022-10-07 20:08:35 +00:00
Ptr<WifiNetDevice> wnd;
2017-02-06 20:31:02 +01:00
2022-10-07 20:08:35 +00:00
for (uint32_t i = 0; i < (*n)->GetNDevices(); ++i)
2017-02-06 20:31:02 +01:00
{
2022-10-07 20:08:35 +00:00
wnd = (*n)->GetDevice(i)->GetObject<WifiNetDevice>();
// if it is a WifiNetDevice
if (wnd)
2017-02-06 20:31:02 +01:00
{
2022-10-07 20:08:35 +00:00
// this device draws power from the last created energy source
radioEnergyHelper.Install(wnd, eSources.Get(eSources.GetN() - 1));
2017-02-06 20:31:02 +01:00
}
}
2014-09-05 16:33:57 -07:00
}
2022-10-07 20:08:35 +00:00
// Tracing
eSources.Get(0)->TraceConnectWithoutContext("RemainingEnergy",
MakeCallback(&RemainingEnergyTrace<0>));
eSources.Get(1)->TraceConnectWithoutContext("RemainingEnergy",
MakeCallback(&RemainingEnergyTrace<1>));
2014-09-05 16:33:57 -07:00
2022-10-07 20:08:35 +00:00
Config::Connect("/NodeList/0/DeviceList/*/Phy/State/State", MakeCallback(&PhyStateTrace<0>));
Config::Connect("/NodeList/1/DeviceList/*/Phy/State/State", MakeCallback(&PhyStateTrace<1>));
2014-09-05 16:33:57 -07:00
Simulator::Stop(duration + Seconds(1));
2014-09-05 16:33:57 -07:00
2022-10-07 20:08:35 +00:00
Simulator::Run();
Simulator::Destroy();
2014-09-05 16:33:57 -07:00
2022-10-07 20:08:35 +00:00
return 0;
2014-09-05 16:33:57 -07:00
}