summaryrefslogtreecommitdiff
path: root/ns-3_wifi_tests/wifi-test.cc
blob: 52ccf88745a272bfea67d3c5ca81501a24ccebf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// ns-3

#include "ns3/command-line.h"
#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"
#include "ns3/on-off-helper.h"
#include "ns3/yans-wifi-channel.h"
#include "ns3/mobility-model.h"
#include "ns3/packet-sink.h"
#include "ns3/packet-sink-helper.h"
#include "ns3/udp-echo-helper.h"
#include "ns3/tcp-westwood.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/constant-position-mobility-model.h"
#include "ns3/energy-module.h"
#include "ns3/wifi-radio-energy-model-helper.h"

// C++ library
#include <iostream>
#include <utility> // To use std::pair

using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("wifi-tcp");




// ---------- 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;

/**
 * Create a sensors cell
 */
Cell createCell(uint32_t nbSensors){
  // Create sensors
  NodeContainer sensors;
  sensors.Create(nbSensors);
  NodeContainer ap;
  ap.Create(1);

  // Place nodes somehow, this is required by every wireless simulation
  for (uint8_t i = 0; i < nbSensors; ++i)
    {
      sensors.Get (i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
    }
  ap.Get (0)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());

  // 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");
  WifiMacHelper wifiMac;
  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)));
}

/**
 * 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;
  
  // 6. Install TCP/IP stack & assign IP addresses
  InternetStackHelper internet;
  internet.Install (ap);
  internet.Install (sensors);
  Ipv4AddressHelper ipv4;
  ipv4.SetBase ("10.0.0.0", "255.0.0.0");
  Ipv4InterfaceContainer apInt,sensorsInt;
  apInt=ipv4.Assign(apNetDev);
  sensorsInt=ipv4.Assign(sensorsNetDev);

  uint16_t  echoPort = 9;
  UdpEchoClientHelper echoClientHelper (InetSocketAddress (apInt.GetAddress (0), echoPort));
  //  echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (10));
  echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (sensorsSendInterval)));
  echoClientHelper.SetAttribute ("PacketSize", UintegerValue (sensorsPktSize));
  ApplicationContainer pingApps;

  // again using different start times to workaround Bug 388 and Bug 912
  echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (1))); // Start at 1 (WIFI seems to not work when t<1)
  echoClientHelper.Install (sensors);

  PacketSinkHelper apSink("ns3::UdpSocketFactory",InetSocketAddress (Ipv4Address::GetAny (), echoPort));
  ApplicationContainer sinkApp=apSink.Install(ap);
  sinkApp.Start (Seconds (0));
}

void
TotalEnergy (std::string context,double oldValue, double newValue)
{
  NS_LOG_UNCOND ("Energy Value of node " << context << " at time " <<Simulator::Now ().GetSeconds ()
                 << "\t"<< newValue);
}


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));

  
}


int main(int argc, char* argv[]){

  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable("PacketSink", LOG_LEVEL_INFO);

  uint32_t sensorsFrequency=1;
  uint32_t sensorsPktSize=150;
  uint32_t sensorsNumber=2;

  CommandLine cmd;
  cmd.AddValue ("sensorsSendInterval", "Number of temperature measurement per second (default 1)", sensorsFrequency);
  cmd.AddValue ("sensorsPktSize", "Sensor measurements packet size (default 64 bytes)", sensorsPktSize);
  cmd.AddValue ("sensorsNumber", "Number of sensors (default 10)", sensorsNumber);
  cmd.Parse (argc, argv);


  Cell c=createCell(sensorsNumber);
  applyScenarios(c,sensorsPktSize,sensorsFrequency);
  setupEnergy(c);
  
  // Run simulators
  Simulator::Stop (Seconds (20));
  Simulator::Run ();

  // Destroy
  Simulator::Destroy ();
  
  return(0);
}