paper-lowrate-iot/ns-3_wifi_tests/wifi-test.cc

263 lines
9.1 KiB
C++
Raw Normal View History

2019-04-10 17:01:17 +02:00
// ns-3
2019-04-11 11:20:12 +02:00
2019-04-10 17:01:17 +02:00
#include "ns3/command-line.h"
2019-04-11 11:20:12 +02:00
#include "ns3/config.h"
#include "ns3/string.h"
#include "ns3/log.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/ssid.h"
#include "ns3/mobility-helper.h"
2019-04-10 17:01:17 +02:00
#include "ns3/on-off-helper.h"
2019-04-11 11:20:12 +02:00
#include "ns3/yans-wifi-channel.h"
#include "ns3/mobility-model.h"
#include "ns3/packet-sink.h"
2019-04-10 17:01:17 +02:00
#include "ns3/packet-sink-helper.h"
#include "ns3/udp-echo-helper.h"
2019-04-11 11:20:12 +02:00
#include "ns3/tcp-westwood.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
2019-04-10 17:01:17 +02:00
#include "ns3/constant-position-mobility-model.h"
2019-04-11 16:07:21 +02:00
#include "ns3/energy-module.h"
#include "ns3/wifi-radio-energy-model-helper.h"
2019-04-12 10:48:33 +02:00
#include "ns3/point-to-point-helper.h"
2019-04-10 17:01:17 +02:00
// C++ library
#include <iostream>
#include <utility> // To use std::pair
2019-04-12 10:48:33 +02:00
#include <iomanip>
2019-04-10 17:01:17 +02:00
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("wifi-tcp");
2019-04-12 10:48:33 +02:00
Ipv4Address cloudIP;
int cloudPort=50;
2019-04-11 11:20:12 +02:00
// ---------- Code ----------
// Cell tuple like ((APNode,SensorNodes),(APNetDev,SensorsNetDev))
typedef std::pair<NodeContainer,NodeContainer> CellNodes;
typedef std::pair<NetDeviceContainer,NetDeviceContainer> CellNetDevices;
typedef std::pair<CellNodes,CellNetDevices> Cell;
2019-04-12 10:48:33 +02:00
void CloudSwitchRx(Ptr< const Packet > packet, const Address &address){
NS_LOG_UNCOND(std::setw(7)<<Simulator::Now ().GetSeconds ()<< " Cloud switch receive a packet!");
}
void
TotalEnergy (std::string context,double oldValue, double newValue)
{
NS_LOG_UNCOND ("Energy Value of node " << context << " at time " <<Simulator::Now ().GetSeconds ()
<< "\t"<< newValue);
}
2019-04-11 11:20:12 +02:00
/**
2019-04-12 10:48:33 +02:00
* Create a sensors cell base on
* nbSensors Number of temperature sensors in the cell
* ap the Access Point (usually linked to the cloud)
2019-04-11 11:20:12 +02:00
*/
2019-04-12 10:48:33 +02:00
Cell createCell(uint32_t nbSensors, Ptr<ns3::Node> ap){
2019-04-10 17:01:17 +02:00
// Create sensors
NodeContainer sensors;
sensors.Create(nbSensors);
// Place nodes somehow, this is required by every wireless simulation
for (uint8_t i = 0; i < nbSensors; ++i)
{
sensors.Get (i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
}
2019-04-12 10:48:33 +02:00
ap->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
2019-04-11 11:20:12 +02:00
// To apply XXWifiPhy and WifiMac on sensors
WifiHelper wifiHelper;
wifiHelper.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ);
/* Set up Legacy Channel */
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel", "Frequency", DoubleValue (5e9));
/* Setup Physical Layer */
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
wifiPhy.Set ("TxPowerStart", DoubleValue (10.0));
wifiPhy.Set ("TxPowerEnd", DoubleValue (10.0));
wifiPhy.Set ("TxPowerLevels", UintegerValue (1));
wifiPhy.Set ("TxGain", DoubleValue (0));
wifiPhy.Set ("RxGain", DoubleValue (0));
wifiPhy.Set ("RxNoiseFigure", DoubleValue (10));
wifiPhy.Set ("CcaMode1Threshold", DoubleValue (-79));
wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (-79 + 3));
// wifiPhy.SetErrorRateModel ("ns3::YansErrorRateModel");
wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue ("HtMcs7"),
"ControlMode", StringValue ("HtMcs0"));
/* Configure AP */
Ssid ssid = Ssid ("network");
2019-04-10 17:01:17 +02:00
WifiMacHelper wifiMac;
2019-04-11 11:20:12 +02:00
wifiMac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid));
NetDeviceContainer apNetDevice;
apNetDevice = wifiHelper.Install (wifiPhy, wifiMac, ap);
/* Configure STA */
wifiMac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid));
NetDeviceContainer sensorsNetDevices;
sensorsNetDevices = wifiHelper.Install (wifiPhy, wifiMac, sensors);
return(std::make_pair(std::make_pair(ap,sensors),std::make_pair(apNetDevice,sensorsNetDevices)));
2019-04-10 17:01:17 +02:00
}
2019-04-12 10:48:33 +02:00
2019-04-11 11:20:12 +02:00
/**
* Install network stack and applications
*/
void applyScenarios(Cell cell,int sensorsPktSize, int sensorsSendInterval){
NodeContainer ap=cell.first.first;
NodeContainer sensors=cell.first.second;
NetDeviceContainer apNetDev= cell.second.first;
NetDeviceContainer sensorsNetDev= cell.second.second;
2019-04-10 17:01:17 +02:00
// 6. Install TCP/IP stack & assign IP addresses
InternetStackHelper internet;
2019-04-12 10:48:33 +02:00
// internet.Install (ap);
2019-04-10 17:01:17 +02:00
internet.Install (sensors);
Ipv4AddressHelper ipv4;
2019-04-12 10:48:33 +02:00
ipv4.SetBase ("10.0.0.0", "255.255.255.0");
2019-04-11 11:20:12 +02:00
Ipv4InterfaceContainer apInt,sensorsInt;
apInt=ipv4.Assign(apNetDev);
sensorsInt=ipv4.Assign(sensorsNetDev);
2019-04-12 10:48:33 +02:00
UdpEchoClientHelper echoClientHelper (InetSocketAddress (cloudIP, cloudPort));
2019-04-11 11:20:12 +02:00
// echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (10));
echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (sensorsSendInterval)));
echoClientHelper.SetAttribute ("PacketSize", UintegerValue (sensorsPktSize));
2019-04-10 17:01:17 +02:00
ApplicationContainer pingApps;
// again using different start times to workaround Bug 388 and Bug 912
2019-04-11 11:20:12 +02:00
echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (1))); // Start at 1 (WIFI seems to not work when t<1)
echoClientHelper.Install (sensors);
2019-04-10 17:01:17 +02:00
}
2019-04-12 10:48:33 +02:00
Ptr<ns3::Node> buildEdgeAndCloud(int nbOp){
NodeContainer OpNodes;
OpNodes.Create(nbOp+1);
InternetStackHelper stack;
stack.Install(OpNodes);
for(int i=0;i<nbOp;i++){ // Not nbOp-1 (We add a AP)
NodeContainer curNodes(OpNodes.Get(i),OpNodes.Get(i+1));
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install (curNodes);
Ipv4AddressHelper address;
address.SetBase (("10.1."+std::to_string(i)+".0").c_str(), "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);
// NS_LOG_UNCOND(i);
if(i==nbOp-1){ // If we are on the last Op
cloudIP=p2pInterfaces.GetAddress (1);
PacketSinkHelper apSink("ns3::UdpSocketFactory",InetSocketAddress (Ipv4Address::GetAny (), cloudPort));
ApplicationContainer sinkApp=apSink.Install(curNodes.Get(1));
sinkApp.Get(0)->TraceConnectWithoutContext("Rx",MakeCallback(&CloudSwitchRx));
sinkApp.Start (Seconds (0));
}
}
return(OpNodes.Get(0));
2019-04-11 16:07:21 +02:00
}
void setupEnergy(Cell cell){
NodeContainer nodes(cell.first.first,cell.first.second);
NetDeviceContainer nodesNetDev(cell.second.first,cell.second.second);
// Install energy source
BasicEnergySourceHelper edgeBasicSourceHelper;
edgeBasicSourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (2.9009));
edgeBasicSourceHelper.Set ("BasicEnergySupplyVoltageV", DoubleValue (3.3));
EnergySourceContainer apEdgeNodesSources = edgeBasicSourceHelper.Install (cell.first.first);
EnergySourceContainer wifiEdgeNodesSources = edgeBasicSourceHelper.Install (cell.first.second);
// Install device energy model
WifiRadioEnergyModelHelper radioEnergyHelper;
radioEnergyHelper.Set ("TxCurrentA", DoubleValue (0.38));
radioEnergyHelper.Set ("RxCurrentA", DoubleValue (0.313));
radioEnergyHelper.Set ("IdleCurrentA", DoubleValue (0.273));
DeviceEnergyModelContainer edgeApDeviceModels = radioEnergyHelper.Install (cell.second.first, apEdgeNodesSources);
DeviceEnergyModelContainer edgeDeviceModels = radioEnergyHelper.Install (cell.second.second, wifiEdgeNodesSources);
// Trace
DeviceEnergyModelContainer energyModels(edgeApDeviceModels, edgeDeviceModels);
DeviceEnergyModelContainer::Iterator it=energyModels.Begin();
int i=0;
while(it!=energyModels.End()){
(*it)->TraceConnect ("TotalEnergyConsumption", std::to_string(i),MakeCallback (&TotalEnergy));
it++;
i++;
}
// Ptr<BasicEnergySource> basicSourcePtr0 = DynamicCast<BasicEnergySource> (wifiEdgeNodesSources.Get (0));
// //basicSourcePtr0->TraceConnectWithoutContext ("RemainingEnergy", MakeCallback (&RemainingEnergy));
// //device energy model
// Ptr<DeviceEnergyModel> basicRadioModelPtr0 =
// basicSourcePtr0->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get (0);
// NS_ASSERT (basicRadioModelPtr0 != NULL);
// basicRadioModelPtr0->TraceConnectWithoutContext ("TotalEnergyConsumption", MakeCallback (&TotalEnergy));
}
2019-04-10 17:01:17 +02:00
int main(int argc, char* argv[]){
2019-04-12 10:48:33 +02:00
//LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
// LogComponentEnable("PacketSink", LOG_LEVEL_INFO);
2019-04-10 17:01:17 +02:00
2019-04-11 11:20:12 +02:00
uint32_t sensorsFrequency=1;
uint32_t sensorsPktSize=150;
2019-04-11 16:07:21 +02:00
uint32_t sensorsNumber=2;
2019-04-12 10:48:33 +02:00
uint32_t nbHop=5;
2019-04-10 17:01:17 +02:00
CommandLine cmd;
2019-04-12 10:48:33 +02:00
cmd.AddValue ("sensorsSendInterval", "Number of temperature measurement per second", sensorsFrequency);
cmd.AddValue ("sensorsPktSize", "Sensor measurements packet size (bytes)", sensorsPktSize);
cmd.AddValue ("sensorsNumber", "Number of sensors", sensorsNumber);
cmd.AddValue ("nbHop", "Number of hop between AP and Cloud sensors", sensorsNumber);
2019-04-10 17:01:17 +02:00
cmd.Parse (argc, argv);
2019-04-12 10:48:33 +02:00
Cell c=createCell(sensorsNumber,buildEdgeAndCloud(nbHop));
2019-04-11 11:20:12 +02:00
applyScenarios(c,sensorsPktSize,sensorsFrequency);
2019-04-12 10:48:33 +02:00
// setupEnergy(c);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
2019-04-10 17:01:17 +02:00
// Run simulators
2019-04-11 16:07:21 +02:00
Simulator::Stop (Seconds (20));
2019-04-10 17:01:17 +02:00
Simulator::Run ();
// Destroy
Simulator::Destroy ();
return(0);
}