summaryrefslogtreecommitdiffstats
path: root/emu-radio/wifi-emulator/src/wifi-emulator.cc
blob: 2f15e793693265a5d039e17843da137f2560496f (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "wifi-emulator.h"

namespace ns3
{
namespace emulator
{

std::map<uint64_t, double> WifiEmulator::McsRateMap = {{0, 15.0},
                                                       {1, 30.0},
                                                       {2, 45.0},
                                                       {3, 60.0},
                                                       {4, 90.0},
                                                       {5, 120.0},
                                                       {6, 135.0},
                                                       {7, 150.0}};

WifiEmulator::WifiEmulator (unsigned int accessPointNumber, unsigned int stationNumber)
    : m_accessPointNumber (accessPointNumber), m_stationNumber (stationNumber), m_channel (YansWifiChannelHelper::Default ()), m_phy (YansWifiPhyHelper::Default ()), m_wifi (WifiHelper::Default ()), m_mac (HtWifiMacHelper::Default ()), m_ssid (SSID), m_avgTransmissionRate (0), m_alpha (0.1)
{
  //
  // We are interacting with the outside, real, world.  This means we have to
  // interact in real-time and therefore means we have to use the real-time
  // simulator and take the time to calculate checksums.
  //
  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));

  m_wifiStaNodes.Create (stationNumber);
  m_wifiApNodes.Create (accessPointNumber);
}

WifiEmulator &WifiEmulator::setWifi (WifiPhyStandard standard)
{
  switch (standard)
    {
      case WIFI_PHY_STANDARD_80211n_5GHZ:
        installNWifi (standard);
      break;
      default:
        std::cerr << "The wifi standard requested is not available!" << std::endl;
      break;
    }

  return *this;
}

void WifiEmulator::installNWifi (WifiPhyStandard standard)
{
  // Default propagation loss model.
  m_channel.AddPropagationLoss ("ns3::NakagamiPropagationLossModel", "m0", DoubleValue (1.0), "m1", DoubleValue (1.0), "m2", DoubleValue (1.0));

  m_phy.SetChannel (m_channel.Create ());
  m_phy.Set ("ShortGuardEnabled", BooleanValue (1));

  m_wifi.SetStandard (standard);

  m_wifi.SetRemoteStationManager ("ns3::MinstrelHtWifiManager");

  m_mac.SetMpduAggregatorForAc (AC_BE, "ns3::MpduStandardAggregator"); // A-MPDU  of max length 65535 bytes
  m_mac.SetMsduAggregatorForAc (AC_BE, "ns3::MsduStandardAggregator"); // A-MSDU of max length 7935 bytes
  m_mac.SetBlockAckThresholdForAc (AC_BE, 2); // block acknowledgement of 5 MPDU
  m_mac.SetBlockAckInactivityTimeoutForAc (AC_BE, 400);

  m_mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (m_ssid), "ActiveProbing", BooleanValue (false));

  m_staDevices = m_wifi.Install (m_phy, m_mac, m_wifiStaNodes);

  m_mac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (m_ssid));

  m_apDevices = m_wifi.Install (m_phy, m_mac, m_wifiApNodes);

  // Set callbacks and values
  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (40));

  Config::Connect ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$ns3::MinstrelHtWifiManager/RateChange", MakeCallback (&WifiEmulator::logNewTransmissionRate, this));
}

WifiEmulator &WifiEmulator::setMobility (double bs_x, double bs_y, double initialDistance)
{
  // Access point mobility
  MobilityHelper apMobility;
  Ptr <ListPositionAllocator> apPositionAlloc = CreateObject<ListPositionAllocator> ();
  apPositionAlloc->Add (Vector (bs_x, bs_y, 0.0));
  apMobility.SetPositionAllocator (apPositionAlloc);
  apMobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  apMobility.Install (m_wifiApNodes);

  // Station mobility. By default the stations start from the same position of the access point
  MobilityHelper staMobility;
  Ptr <ListPositionAllocator> staPositionAlloc = CreateObject<ListPositionAllocator> ();
  staPositionAlloc->Add (Vector (bs_x, bs_y + initialDistance, 0.0));
  staMobility.SetPositionAllocator (staPositionAlloc);
  staMobility.SetMobilityModel ("ns3::WaypointMobilityModel", "InitialPositionIsWaypoint", BooleanValue (false));
  staMobility.Install (m_wifiStaNodes);

  return *this;
}

WifiEmulator &
WifiEmulator::setTapDevices (std::list <std::string> &ap_list, std::list <std::string> &station_list, std::list <std::string> &sta_taps_list, std::list <std::string> &ap_taps_list, std::list <std::string> &sta_macs_list, std::list <std::string> &ap_macs_list)
{
  TapBridgeHelper tapBridge;
  tapBridge.SetAttribute ("Mode", StringValue ("UseLocal"));

  std::list<std::string>::const_iterator station;
  std::list<std::string>::const_iterator ap;
  std::list<std::string>::const_iterator sta_tap;
  std::list<std::string>::const_iterator sta_mac;
  std::list<std::string>::const_iterator ap_tap;
  std::list<std::string>::const_iterator ap_mac;

  uint32_t i;

  assert (station_list.size () == sta_taps_list.size () && sta_taps_list.size () == sta_macs_list.size ());
  assert (ap_list.size () == ap_taps_list.size () && ap_taps_list.size () == ap_macs_list.size ());

  for (sta_tap = sta_taps_list.begin (), sta_mac = sta_macs_list.begin (), station = station_list.begin (), i = 0;
       sta_tap != sta_taps_list.end () && sta_mac != sta_macs_list.end ()
       && station != station_list.end (); sta_tap++, sta_mac++, station++, i++)
    {
      m_mapNameNs3node[*station] = m_wifiStaNodes.Get (i);

      if (sta_mac->compare (""))
        {
          m_staDevices.Get (i)->SetAddress (Mac48Address (sta_mac->c_str ()));
        }

      tapBridge.SetAttribute ("DeviceName", StringValue (sta_tap->c_str ()));
      tapBridge.Install (m_wifiStaNodes.Get (i), m_staDevices.Get (i));
    }

  for (ap_tap = ap_taps_list.begin (), ap_mac = ap_macs_list.begin (), ap = ap_list.begin (), i = 0;
       ap_tap != ap_taps_list.end () && ap_mac != ap_macs_list.end ()
       && ap != ap_list.end (); ap_tap++, ap_mac++, ap++, i++)
    {
      m_mapNameNs3node[*ap] = m_wifiApNodes.Get (i);

      if (ap_mac->compare (""))
        {
          m_apDevices.Get (i)->SetAddress (Mac48Address (ap_mac->c_str ()));
        }

      tapBridge.SetAttribute ("DeviceName", StringValue (ap_tap->c_str ()));
      tapBridge.Install (m_wifiApNodes.Get (i), m_apDevices.Get (i));
    }

  return *this;
}

void WifiEmulator::logNewTransmissionRate (std::string context, const uint64_t rate, const Mac48Address remoteAddress)
{
  // TODO Linear search! Improve it with an hash table when there will be more stations!

  for (auto station : m_mapNameNs3node)
    {
      if (station.second->GetDevice (0)->GetAddress () == remoteAddress)
        {
          const std::string &sta = station.first;

          // Compute EWMA of the physical rate
          m_avgTransmissionRate[sta] = m_alpha * McsRateMap[rate] + (1.0 - m_alpha) * m_avgTransmissionRate[sta];
        }
    }
}

bool WifiEmulator::getTransmissionRate (const std::string &station, double *transmissionRate)
{
  if (m_avgTransmissionRate.find (station) != m_avgTransmissionRate.end ())
    {
      *transmissionRate = m_avgTransmissionRate[station];
      return true;
    }

  return false;
}

WifiEmulator &WifiEmulator::runEmulation (bool async)
{
  m_simulationHandle = std::async (std::launch::async, [] ()
  {
    Simulator::Stop ();
    Simulator::Run ();
    Simulator::Destroy ();
  });

  if (!async)
    {
      m_simulationHandle.get ();
    }

  return *this;
}

WifiEmulator &WifiEmulator::stopEmulation ()
{
  Simulator::Stop ();
//  Simulator::Destroy();
}

bool WifiEmulator::checkIfExist (const std::string &station)
{
  if (m_mapNameNs3node.find (station) == m_mapNameNs3node.end ())
    {
      std::cerr << "The station [" << station << "] does not exist in this simulation!" << std::endl;
      return false;
    }

  return true;
}

Ptr <MobilityModel> WifiEmulator::getMobilityModel (const std::string &station)
{
  if (checkIfExist (station))
    {
      Ptr <Node> sta_ptr = m_mapNameNs3node[station];
      Ptr <MobilityModel> staMobilityModel = sta_ptr->GetObject<MobilityModel> ();
      return staMobilityModel;
    }
  else
    {
      return nullptr;
    }
}

bool WifiEmulator::setStationCoordinates (const std::string &station, double x, double y)
{
  if (checkIfExist (station))
    {
      Ptr <MobilityModel> staMobilityModel = getMobilityModel (station);
      staMobilityModel->SetPosition (Vector (x, y, 0.0));
      return true;
    }

  return false;
}

bool WifiEmulator::setStationXCoordinate (const std::string &station, double x)
{
  if (checkIfExist (station))
    {
      Ptr <MobilityModel> staMobilityModel = getMobilityModel (station);
      staMobilityModel->SetPosition (Vector (x, staMobilityModel->GetPosition ().y, 0.0));
      return true;
    }

  return false;
}

bool WifiEmulator::setStationYCoordinate (const std::string &station, double y)
{
  if (checkIfExist (station))
    {
      Ptr <MobilityModel> staMobilityModel = getMobilityModel (station);
      staMobilityModel->SetPosition (Vector (staMobilityModel->GetPosition ().x, y, 0.0));
      return true;
    }

  return false;
}

std::pair<double, double> WifiEmulator::getStationCoordinates (const std::string &station)
{
  if (checkIfExist (station))
    {
      Ptr <MobilityModel> staMobilityModel = getMobilityModel (station);
      return {staMobilityModel->GetPosition ().x, staMobilityModel->GetPosition ().y};
    }

  return {};
}

bool WifiEmulator::getStationXCoordinate (const std::string &station, double *x)
{
  if (checkIfExist (station))
    {
      Ptr <MobilityModel> staMobilityModel = getMobilityModel (station);
      *x = staMobilityModel->GetPosition ().x;
      return true;
    }

  return false;
}

bool WifiEmulator::getStationYCoordinate (const std::string &station, double *y)
{
  if (checkIfExist (station))
    {
      Ptr <MobilityModel> staMobilityModel = getMobilityModel (station);
      *y = staMobilityModel->GetPosition ().y;
      return true;
    }

  return false;
}

WifiEmulator &
WifiEmulator::moveStationAlongSegment (const std::string &station, double start_x, double start_y, double end_x, double end_y, double duration)
{
  if (checkIfExist (station))
    {

      Ptr <Node> sta_ptr = m_mapNameNs3node[station];
      Ptr <WaypointMobilityModel> staMobilityModel = sta_ptr->GetObject<WaypointMobilityModel> ();

      if (staMobilityModel)
        {

          staMobilityModel->SetPosition (Vector (start_x, start_y, 0.0));
          staMobilityModel->AddWaypoint (Waypoint (Seconds (Simulator::Now ().GetSeconds ()), Vector3D (start_x, start_y, 0.0)));
          staMobilityModel->AddWaypoint (Waypoint (
              Seconds (Simulator::Now ().GetSeconds ()) + Seconds (duration), Vector3D (end_x, end_y, 0.0)));
        }
      else
        {
          std::cerr << "Access point has a constant position mobility model. Impossible to move it" << std::endl;
        }
    }

  return *this;
}

} // End namespace ns3
} // End namespace emulator