LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> |
| 19 */ |
| 20 |
| 21 #include "nsl-wifi-helper.h" |
| 22 #include "ns3/nsl-wifi-phy.h" |
| 23 #include "ns3/nsl-wifi-channel.h" |
| 24 #include "ns3/error-rate-model.h" |
| 25 #include "ns3/propagation-loss-model.h" |
| 26 #include "ns3/propagation-delay-model.h" |
| 27 #include "ns3/wifi-net-device.h" |
| 28 #include "ns3/radiotap-header.h" |
| 29 #include "ns3/pcap-file-wrapper.h" |
| 30 #include "ns3/simulator.h" |
| 31 #include "ns3/config.h" |
| 32 #include "ns3/names.h" |
| 33 #include "ns3/abort.h" |
| 34 #include "ns3/log.h" |
| 35 |
| 36 NS_LOG_COMPONENT_DEFINE ("NslWifiHelper"); |
| 37 |
| 38 namespace ns3 { |
| 39 |
| 40 static void |
| 41 AsciiPhyTransmitSinkWithContext ( |
| 42 Ptr<OutputStreamWrapper> stream, |
| 43 std::string context, |
| 44 Ptr<const Packet> p, |
| 45 WifiMode mode, |
| 46 WifiPreamble preamble, |
| 47 uint8_t txLevel) |
| 48 { |
| 49 NS_LOG_FUNCTION (stream << context << p << mode << preamble << txLevel); |
| 50 *stream->GetStream () << "t " << Simulator::Now ().GetSeconds () << " " |
| 51 << context << " " << *p << std::endl; |
| 52 } |
| 53 |
| 54 static void |
| 55 AsciiPhyTransmitSinkWithoutContext ( |
| 56 Ptr<OutputStreamWrapper> stream, |
| 57 Ptr<const Packet> p, |
| 58 WifiMode mode, |
| 59 WifiPreamble preamble, |
| 60 uint8_t txLevel) |
| 61 { |
| 62 NS_LOG_FUNCTION (stream << p << mode << preamble << txLevel); |
| 63 *stream->GetStream () << "t " << Simulator::Now ().GetSeconds () << " " |
| 64 << *p << std::endl; |
| 65 } |
| 66 |
| 67 static void |
| 68 AsciiPhyReceiveSinkWithContext ( |
| 69 Ptr<OutputStreamWrapper> stream, |
| 70 std::string context, |
| 71 Ptr<const Packet> p, |
| 72 double snr, |
| 73 WifiMode mode, |
| 74 enum WifiPreamble preamble) |
| 75 { |
| 76 NS_LOG_FUNCTION (stream << context << p << snr << mode << preamble); |
| 77 *stream->GetStream () << "r " << Simulator::Now ().GetSeconds () << " " |
| 78 << context << " " << *p << std::endl; |
| 79 } |
| 80 |
| 81 static void |
| 82 AsciiPhyReceiveSinkWithoutContext ( |
| 83 Ptr<OutputStreamWrapper> stream, |
| 84 Ptr<const Packet> p, |
| 85 double snr, |
| 86 WifiMode mode, |
| 87 enum WifiPreamble preamble) |
| 88 { |
| 89 NS_LOG_FUNCTION (stream << p << snr << mode << preamble); |
| 90 *stream->GetStream () << "r " << Simulator::Now ().GetSeconds () << " " |
| 91 << *p << std::endl; |
| 92 } |
| 93 |
| 94 //----------------------------------------------------------------------------// |
| 95 |
| 96 NslWifiChannelHelper::NslWifiChannelHelper () |
| 97 {} |
| 98 |
| 99 NslWifiChannelHelper |
| 100 NslWifiChannelHelper::Default (void) |
| 101 { |
| 102 NslWifiChannelHelper helper; |
| 103 helper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 104 helper.AddPropagationLoss ("ns3::LogDistancePropagationLossModel"); |
| 105 return helper; |
| 106 } |
| 107 |
| 108 void |
| 109 NslWifiChannelHelper::AddPropagationLoss (std::string type, |
| 110 std::string n0, const AttributeValue &v0, |
| 111 std::string n1, const AttributeValue &v1, |
| 112 std::string n2, const AttributeValue &v2, |
| 113 std::string n3, const AttributeValue &v3, |
| 114 std::string n4, const AttributeValue &v4, |
| 115 std::string n5, const AttributeValue &v5, |
| 116 std::string n6, const AttributeValue &v6, |
| 117 std::string n7, const AttributeValue &v7) |
| 118 { |
| 119 ObjectFactory factory; |
| 120 factory.SetTypeId (type); |
| 121 factory.Set (n0, v0); |
| 122 factory.Set (n1, v1); |
| 123 factory.Set (n2, v2); |
| 124 factory.Set (n3, v3); |
| 125 factory.Set (n4, v4); |
| 126 factory.Set (n5, v5); |
| 127 factory.Set (n6, v6); |
| 128 factory.Set (n7, v7); |
| 129 m_propagationLoss.push_back (factory); |
| 130 } |
| 131 |
| 132 void |
| 133 NslWifiChannelHelper::SetPropagationDelay (std::string type, |
| 134 std::string n0, const AttributeValue &v0, |
| 135 std::string n1, const AttributeValue &v1, |
| 136 std::string n2, const AttributeValue &v2, |
| 137 std::string n3, const AttributeValue &v3, |
| 138 std::string n4, const AttributeValue &v4, |
| 139 std::string n5, const AttributeValue &v5, |
| 140 std::string n6, const AttributeValue &v6, |
| 141 std::string n7, const AttributeValue &v7) |
| 142 { |
| 143 ObjectFactory factory; |
| 144 factory.SetTypeId (type); |
| 145 factory.Set (n0, v0); |
| 146 factory.Set (n1, v1); |
| 147 factory.Set (n2, v2); |
| 148 factory.Set (n3, v3); |
| 149 factory.Set (n4, v4); |
| 150 factory.Set (n5, v5); |
| 151 factory.Set (n6, v6); |
| 152 factory.Set (n7, v7); |
| 153 m_propagationDelay = factory; |
| 154 } |
| 155 |
| 156 Ptr<NslWifiChannel> |
| 157 NslWifiChannelHelper::Create (void) const |
| 158 { |
| 159 Ptr<NslWifiChannel> channel = CreateObject<NslWifiChannel> (); |
| 160 Ptr<PropagationLossModel> prev = 0; |
| 161 for (std::vector<ObjectFactory>::const_iterator i = m_propagationLoss.begin ()
; i != m_propagationLoss.end (); ++i) |
| 162 { |
| 163 Ptr<PropagationLossModel> cur = (*i).Create<PropagationLossModel> (); |
| 164 if (prev != 0) |
| 165 { |
| 166 prev->SetNext (cur); |
| 167 } |
| 168 if (m_propagationLoss.begin () == i) |
| 169 { |
| 170 channel->SetPropagationLossModel (cur); |
| 171 } |
| 172 prev = cur; |
| 173 } |
| 174 Ptr<PropagationDelayModel> delay = m_propagationDelay.Create<PropagationDelayM
odel> (); |
| 175 channel->SetPropagationDelayModel (delay); |
| 176 return channel; |
| 177 } |
| 178 |
| 179 //----------------------------------------------------------------------------// |
| 180 |
| 181 NslWifiPhyHelper::NslWifiPhyHelper () |
| 182 : m_channel (0), |
| 183 m_pcapDlt(PcapHelper::DLT_IEEE802_11) |
| 184 { |
| 185 m_phy.SetTypeId ("ns3::NslWifiPhy"); |
| 186 } |
| 187 |
| 188 NslWifiPhyHelper |
| 189 NslWifiPhyHelper::Default (void) |
| 190 { |
| 191 NslWifiPhyHelper helper; |
| 192 helper.SetErrorRateModel ("ns3::YansErrorRateModel"); |
| 193 return helper; |
| 194 } |
| 195 |
| 196 void |
| 197 NslWifiPhyHelper::SetChannel (Ptr<NslWifiChannel> channel) |
| 198 { |
| 199 m_channel = channel; |
| 200 } |
| 201 void |
| 202 NslWifiPhyHelper::SetChannel (std::string channelName) |
| 203 { |
| 204 Ptr<NslWifiChannel> channel = Names::Find<NslWifiChannel> (channelName); |
| 205 m_channel = channel; |
| 206 } |
| 207 void |
| 208 NslWifiPhyHelper::Set (std::string name, const AttributeValue &v) |
| 209 { |
| 210 m_phy.Set (name, v); |
| 211 } |
| 212 |
| 213 void |
| 214 NslWifiPhyHelper::SetErrorRateModel (std::string name, |
| 215 std::string n0, const AttributeValue &v0, |
| 216 std::string n1, const AttributeValue &v1, |
| 217 std::string n2, const AttributeValue &v2, |
| 218 std::string n3, const AttributeValue &v3, |
| 219 std::string n4, const AttributeValue &v4, |
| 220 std::string n5, const AttributeValue &v5, |
| 221 std::string n6, const AttributeValue &v6, |
| 222 std::string n7, const AttributeValue &v7) |
| 223 { |
| 224 m_errorRateModel = ObjectFactory (); |
| 225 m_errorRateModel.SetTypeId (name); |
| 226 m_errorRateModel.Set (n0, v0); |
| 227 m_errorRateModel.Set (n1, v1); |
| 228 m_errorRateModel.Set (n2, v2); |
| 229 m_errorRateModel.Set (n3, v3); |
| 230 m_errorRateModel.Set (n4, v4); |
| 231 m_errorRateModel.Set (n5, v5); |
| 232 m_errorRateModel.Set (n6, v6); |
| 233 m_errorRateModel.Set (n7, v7); |
| 234 } |
| 235 |
| 236 Ptr<WifiPhy> |
| 237 NslWifiPhyHelper::Create (Ptr<Node> node, Ptr<WifiNetDevice> device) const |
| 238 { |
| 239 Ptr<NslWifiPhy> phy = m_phy.Create<NslWifiPhy> (); |
| 240 Ptr<ErrorRateModel> error = m_errorRateModel.Create<ErrorRateModel> (); |
| 241 phy->SetErrorRateModel (error); |
| 242 phy->SetChannel (m_channel); |
| 243 phy->SetMobility (node); |
| 244 phy->SetDevice (device); |
| 245 phy->SetNode (node); |
| 246 return phy; |
| 247 } |
| 248 |
| 249 static void |
| 250 PcapSniffTxEvent ( |
| 251 Ptr<PcapFileWrapper> file, |
| 252 Ptr<const Packet> packet, |
| 253 uint16_t channelFreqMhz, |
| 254 uint16_t channelNumber, |
| 255 uint32_t rate, |
| 256 bool isShortPreamble) |
| 257 { |
| 258 uint32_t dlt = file->GetDataLinkType (); |
| 259 |
| 260 switch (dlt) |
| 261 { |
| 262 case PcapHelper::DLT_IEEE802_11: |
| 263 file->Write (Simulator::Now (), packet); |
| 264 return; |
| 265 case PcapHelper::DLT_PRISM_HEADER: |
| 266 { |
| 267 NS_FATAL_ERROR ("PcapSniffTxEvent(): DLT_PRISM_HEADER not implemented"); |
| 268 return; |
| 269 } |
| 270 case PcapHelper::DLT_IEEE802_11_RADIO: |
| 271 { |
| 272 Ptr<Packet> p = packet->Copy (); |
| 273 RadiotapHeader header; |
| 274 uint8_t frameFlags = RadiotapHeader::FRAME_FLAG_NONE; |
| 275 header.SetTsft (Simulator::Now ().GetMicroSeconds ()); |
| 276 |
| 277 // Our capture includes the FCS, so we set the flag to say so. |
| 278 frameFlags |= RadiotapHeader::FRAME_FLAG_FCS_INCLUDED; |
| 279 |
| 280 if (isShortPreamble) |
| 281 { |
| 282 frameFlags |= RadiotapHeader::FRAME_FLAG_SHORT_PREAMBLE; |
| 283 } |
| 284 |
| 285 header.SetFrameFlags (frameFlags); |
| 286 header.SetRate (rate); |
| 287 |
| 288 if (channelFreqMhz < 2500) |
| 289 { |
| 290 header.SetChannelFrequencyAndFlags (channelFreqMhz, |
| 291 RadiotapHeader::CHANNEL_FLAG_SPECTRUM_2GHZ | RadiotapHeader::CHANN
EL_FLAG_CCK); |
| 292 } |
| 293 else |
| 294 { |
| 295 header.SetChannelFrequencyAndFlags (channelFreqMhz, |
| 296 RadiotapHeader::CHANNEL_FLAG_SPECTRUM_5GHZ | RadiotapHeader::CHANN
EL_FLAG_OFDM); |
| 297 } |
| 298 |
| 299 p->AddHeader (header); |
| 300 file->Write (Simulator::Now (), p); |
| 301 return; |
| 302 } |
| 303 default: |
| 304 NS_ABORT_MSG ("PcapSniffTxEvent(): Unexpected data link type " << dlt); |
| 305 } |
| 306 } |
| 307 |
| 308 static void |
| 309 PcapSniffRxEvent ( |
| 310 Ptr<PcapFileWrapper> file, |
| 311 Ptr<const Packet> packet, |
| 312 uint16_t channelFreqMhz, |
| 313 uint16_t channelNumber, |
| 314 uint32_t rate, |
| 315 bool isShortPreamble, |
| 316 double signalDbm, |
| 317 double noiseDbm) |
| 318 { |
| 319 uint32_t dlt = file->GetDataLinkType (); |
| 320 |
| 321 switch (dlt) |
| 322 { |
| 323 case PcapHelper::DLT_IEEE802_11: |
| 324 file->Write (Simulator::Now (), packet); |
| 325 return; |
| 326 case PcapHelper::DLT_PRISM_HEADER: |
| 327 { |
| 328 NS_FATAL_ERROR ("PcapSniffRxEvent(): DLT_PRISM_HEADER not implemented"); |
| 329 return; |
| 330 } |
| 331 case PcapHelper::DLT_IEEE802_11_RADIO: |
| 332 { |
| 333 Ptr<Packet> p = packet->Copy (); |
| 334 RadiotapHeader header; |
| 335 uint8_t frameFlags = RadiotapHeader::FRAME_FLAG_NONE; |
| 336 header.SetTsft (Simulator::Now ().GetMicroSeconds ()); |
| 337 |
| 338 // Our capture includes the FCS, so we set the flag to say so. |
| 339 frameFlags |= RadiotapHeader::FRAME_FLAG_FCS_INCLUDED; |
| 340 |
| 341 if (isShortPreamble) |
| 342 { |
| 343 frameFlags |= RadiotapHeader::FRAME_FLAG_SHORT_PREAMBLE; |
| 344 } |
| 345 |
| 346 header.SetFrameFlags (frameFlags); |
| 347 header.SetRate (rate); |
| 348 |
| 349 if (channelFreqMhz < 2500) |
| 350 { |
| 351 header.SetChannelFrequencyAndFlags (channelFreqMhz, |
| 352 RadiotapHeader::CHANNEL_FLAG_SPECTRUM_2GHZ | RadiotapHeader::CHANN
EL_FLAG_CCK); |
| 353 } |
| 354 else |
| 355 { |
| 356 header.SetChannelFrequencyAndFlags (channelFreqMhz, |
| 357 RadiotapHeader::CHANNEL_FLAG_SPECTRUM_5GHZ | RadiotapHeader::CHANN
EL_FLAG_OFDM); |
| 358 } |
| 359 |
| 360 header.SetAntennaSignalPower (signalDbm); |
| 361 header.SetAntennaNoisePower (noiseDbm); |
| 362 |
| 363 p->AddHeader (header); |
| 364 file->Write (Simulator::Now (), p); |
| 365 return; |
| 366 } |
| 367 default: |
| 368 NS_ABORT_MSG ("PcapSniffRxEvent(): Unexpected data link type " << dlt); |
| 369 } |
| 370 } |
| 371 |
| 372 void |
| 373 NslWifiPhyHelper::SetPcapDataLinkType (enum SupportedPcapDataLinkTypes dlt) |
| 374 { |
| 375 switch (dlt) |
| 376 { |
| 377 case DLT_IEEE802_11: |
| 378 m_pcapDlt = PcapHelper::DLT_IEEE802_11; |
| 379 return; |
| 380 case DLT_PRISM_HEADER: |
| 381 m_pcapDlt = PcapHelper::DLT_PRISM_HEADER; |
| 382 return; |
| 383 case DLT_IEEE802_11_RADIO: |
| 384 m_pcapDlt = PcapHelper::DLT_IEEE802_11_RADIO; |
| 385 return; |
| 386 default: |
| 387 NS_ABORT_MSG ("NslWifiPhyHelper::SetPcapFormat(): Unexpected format"); |
| 388 } |
| 389 } |
| 390 |
| 391 void |
| 392 NslWifiPhyHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, boo
l promiscuous, bool explicitFilename) |
| 393 { |
| 394 // |
| 395 // All of the Pcap enable functions vector through here including the ones |
| 396 // that are wandering through all of devices on perhaps all of the nodes in |
| 397 // the system. We can only deal with devices of type WifiNetDevice. |
| 398 // |
| 399 Ptr<WifiNetDevice> device = nd->GetObject<WifiNetDevice> (); |
| 400 if (device == 0) |
| 401 { |
| 402 NS_LOG_INFO ("NslWifiHelper::EnablePcapInternal(): Device " << &device <<
" not of type ns3::WifiNetDevice"); |
| 403 return; |
| 404 } |
| 405 |
| 406 Ptr<WifiPhy> phy = device->GetPhy (); |
| 407 NS_ABORT_MSG_IF (phy == 0, "NslWifiPhyHelper::EnablePcapInternal(): Phy layer
in WifiNetDevice must be set"); |
| 408 |
| 409 PcapHelper pcapHelper; |
| 410 |
| 411 std::string filename; |
| 412 if (explicitFilename) |
| 413 { |
| 414 filename = prefix; |
| 415 } |
| 416 else |
| 417 { |
| 418 filename = pcapHelper.GetFilenameFromDevice (prefix, device); |
| 419 } |
| 420 |
| 421 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, m_
pcapDlt); |
| 422 |
| 423 phy->TraceConnectWithoutContext ("PromiscSnifferTx", MakeBoundCallback (&PcapS
niffTxEvent, file)); |
| 424 phy->TraceConnectWithoutContext ("PromiscSnifferRx", MakeBoundCallback (&PcapS
niffRxEvent, file)); |
| 425 } |
| 426 |
| 427 void |
| 428 NslWifiPhyHelper::EnableAsciiInternal ( |
| 429 Ptr<OutputStreamWrapper> stream, |
| 430 std::string prefix, |
| 431 Ptr<NetDevice> nd, |
| 432 bool explicitFilename) |
| 433 { |
| 434 // |
| 435 // All of the ascii enable functions vector through here including the ones |
| 436 // that are wandering through all of devices on perhaps all of the nodes in |
| 437 // the system. We can only deal with devices of type CsmaNetDevice. |
| 438 // |
| 439 Ptr<WifiNetDevice> device = nd->GetObject<WifiNetDevice> (); |
| 440 if (device == 0) |
| 441 { |
| 442 NS_LOG_INFO ("NslWifiHelper::EnableAsciiInternal(): Device " << device <<
" not of type ns3::WifiNetDevice"); |
| 443 return; |
| 444 } |
| 445 |
| 446 // |
| 447 // Our trace sinks are going to use packet printing, so we have to make sure |
| 448 // that is turned on. |
| 449 // |
| 450 Packet::EnablePrinting (); |
| 451 |
| 452 uint32_t nodeid = nd->GetNode ()->GetId (); |
| 453 uint32_t deviceid = nd->GetIfIndex (); |
| 454 std::ostringstream oss; |
| 455 |
| 456 // |
| 457 // If we are not provided an OutputStreamWrapper, we are expected to create |
| 458 // one using the usual trace filename conventions and write our traces |
| 459 // without a context since there will be one file per context and therefore |
| 460 // the context would be redundant. |
| 461 // |
| 462 if (stream == 0) |
| 463 { |
| 464 // |
| 465 // Set up an output stream object to deal with private ofstream copy |
| 466 // constructor and lifetime issues. Let the helper decide the actual |
| 467 // name of the file given the prefix. |
| 468 // |
| 469 AsciiTraceHelper asciiTraceHelper; |
| 470 |
| 471 std::string filename; |
| 472 if (explicitFilename) |
| 473 { |
| 474 filename = prefix; |
| 475 } |
| 476 else |
| 477 { |
| 478 filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device); |
| 479 } |
| 480 |
| 481 // |
| 482 // We could go poking through the phy and the state looking for the |
| 483 // correct trace source, but we can let Config deal with that with |
| 484 // some search cost. Since this is presumably happening at topology |
| 485 // creation time, it doesn't seem much of a price to pay. |
| 486 // |
| 487 oss.str (""); |
| 488 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::Wif
iNetDevice/Phy/State/RxOk"; |
| 489 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&AsciiPhyRec
eiveSinkWithoutContext, stream)); |
| 490 |
| 491 oss.str (""); |
| 492 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::Wif
iNetDevice/Phy/State/Tx"; |
| 493 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&AsciiPhyTra
nsmitSinkWithoutContext, stream)); |
| 494 |
| 495 return; |
| 496 } |
| 497 |
| 498 // |
| 499 // If we are provided an OutputStreamWrapper, we are expected to use it, and |
| 500 // to provide a context. We are free to come up with our own context if we |
| 501 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for |
| 502 // compatibility and simplicity, we just use Config::Connect and let it deal |
| 503 // with coming up with a context. |
| 504 // |
| 505 oss.str (""); |
| 506 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNet
Device/Phy/State/RxOk"; |
| 507 Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyReceiveSinkWithContex
t, stream)); |
| 508 |
| 509 oss.str (""); |
| 510 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNet
Device/Phy/State/Tx"; |
| 511 Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyTransmitSinkWithConte
xt, stream)); |
| 512 } |
| 513 |
| 514 } // namespace ns3 |
LEFT | RIGHT |