A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
yans-wifi-helper.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/trace-helper.h"
22 #include "yans-wifi-helper.h"
23 #include "ns3/error-rate-model.h"
24 #include "ns3/propagation-loss-model.h"
25 #include "ns3/propagation-delay-model.h"
26 #include "ns3/yans-wifi-channel.h"
27 #include "ns3/yans-wifi-phy.h"
28 #include "ns3/wifi-net-device.h"
29 #include "ns3/radiotap-header.h"
30 #include "ns3/pcap-file-wrapper.h"
31 #include "ns3/simulator.h"
32 #include "ns3/config.h"
33 #include "ns3/names.h"
34 #include "ns3/abort.h"
35 #include "ns3/log.h"
36 
37 NS_LOG_COMPONENT_DEFINE ("YansWifiHelper");
38 
39 namespace ns3 {
40 
41 static void
42 AsciiPhyTransmitSinkWithContext (
43  Ptr<OutputStreamWrapper> stream,
44  std::string context,
45  Ptr<const Packet> p,
46  WifiMode mode,
47  WifiPreamble preamble,
48  uint8_t txLevel)
49 {
50  NS_LOG_FUNCTION (stream << context << p << mode << preamble << txLevel);
51  *stream->GetStream () << "t " << Simulator::Now ().GetSeconds () << " " << 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 () << " " << *p << std::endl;
64 }
65 
66 static void
67 AsciiPhyReceiveSinkWithContext (
68  Ptr<OutputStreamWrapper> stream,
69  std::string context,
70  Ptr<const Packet> p,
71  double snr,
72  WifiMode mode,
73  enum WifiPreamble preamble)
74 {
75  NS_LOG_FUNCTION (stream << context << p << snr << mode << preamble);
76  *stream->GetStream () << "r " << Simulator::Now ().GetSeconds () << " " << context << " " << *p << std::endl;
77 }
78 
79 static void
80 AsciiPhyReceiveSinkWithoutContext (
81  Ptr<OutputStreamWrapper> stream,
82  Ptr<const Packet> p,
83  double snr,
84  WifiMode mode,
85  enum WifiPreamble preamble)
86 {
87  NS_LOG_FUNCTION (stream << p << snr << mode << preamble);
88  *stream->GetStream () << "r " << Simulator::Now ().GetSeconds () << " " << *p << std::endl;
89 }
90 
92 {
93 }
94 
97 {
98  YansWifiChannelHelper helper;
99  helper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
100  helper.AddPropagationLoss ("ns3::LogDistancePropagationLossModel");
101  return helper;
102 }
103 
104 void
106  std::string n0, const AttributeValue &v0,
107  std::string n1, const AttributeValue &v1,
108  std::string n2, const AttributeValue &v2,
109  std::string n3, const AttributeValue &v3,
110  std::string n4, const AttributeValue &v4,
111  std::string n5, const AttributeValue &v5,
112  std::string n6, const AttributeValue &v6,
113  std::string n7, const AttributeValue &v7)
114 {
115  ObjectFactory factory;
116  factory.SetTypeId (type);
117  factory.Set (n0, v0);
118  factory.Set (n1, v1);
119  factory.Set (n2, v2);
120  factory.Set (n3, v3);
121  factory.Set (n4, v4);
122  factory.Set (n5, v5);
123  factory.Set (n6, v6);
124  factory.Set (n7, v7);
125  m_propagationLoss.push_back (factory);
126 }
127 
128 void
130  std::string n0, const AttributeValue &v0,
131  std::string n1, const AttributeValue &v1,
132  std::string n2, const AttributeValue &v2,
133  std::string n3, const AttributeValue &v3,
134  std::string n4, const AttributeValue &v4,
135  std::string n5, const AttributeValue &v5,
136  std::string n6, const AttributeValue &v6,
137  std::string n7, const AttributeValue &v7)
138 {
139  ObjectFactory factory;
140  factory.SetTypeId (type);
141  factory.Set (n0, v0);
142  factory.Set (n1, v1);
143  factory.Set (n2, v2);
144  factory.Set (n3, v3);
145  factory.Set (n4, v4);
146  factory.Set (n5, v5);
147  factory.Set (n6, v6);
148  factory.Set (n7, v7);
149  m_propagationDelay = factory;
150 }
151 
154 {
155  Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
156  Ptr<PropagationLossModel> prev = 0;
157  for (std::vector<ObjectFactory>::const_iterator i = m_propagationLoss.begin (); i != m_propagationLoss.end (); ++i)
158  {
159  Ptr<PropagationLossModel> cur = (*i).Create<PropagationLossModel> ();
160  if (prev != 0)
161  {
162  prev->SetNext (cur);
163  }
164  if (m_propagationLoss.begin () == i)
165  {
166  channel->SetPropagationLossModel (cur);
167  }
168  prev = cur;
169  }
170  Ptr<PropagationDelayModel> delay = m_propagationDelay.Create<PropagationDelayModel> ();
171  channel->SetPropagationDelayModel (delay);
172  return channel;
173 }
174 
175 int64_t
177 {
178  return c->AssignStreams (stream);
179 }
180 
182  : m_channel (0),
183  m_pcapDlt (PcapHelper::DLT_IEEE802_11)
184 {
185  m_phy.SetTypeId ("ns3::YansWifiPhy");
186 }
187 
190 {
191  YansWifiPhyHelper helper;
192  helper.SetErrorRateModel ("ns3::NistErrorRateModel");
193  return helper;
194 }
195 
196 void
198 {
199  m_channel = channel;
200 }
201 void
202 YansWifiPhyHelper::SetChannel (std::string channelName)
203 {
204  Ptr<YansWifiChannel> channel = Names::Find<YansWifiChannel> (channelName);
205  m_channel = channel;
206 }
207 void
208 YansWifiPhyHelper::Set (std::string name, const AttributeValue &v)
209 {
210  m_phy.Set (name, v);
211 }
212 
213 void
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 
238 {
239  Ptr<YansWifiPhy> phy = m_phy.Create<YansWifiPhy> ();
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  return phy;
246 }
247 
248 static void
249 PcapSniffTxEvent (
251  Ptr<const Packet> packet,
252  uint16_t channelFreqMhz,
253  uint16_t channelNumber,
254  uint32_t rate,
255  bool isShortPreamble)
256 {
257  uint32_t dlt = file->GetDataLinkType ();
258 
259  switch (dlt)
260  {
261  case PcapHelper::DLT_IEEE802_11:
262  file->Write (Simulator::Now (), packet);
263  return;
264  case PcapHelper::DLT_PRISM_HEADER:
265  {
266  NS_FATAL_ERROR ("PcapSniffTxEvent(): DLT_PRISM_HEADER not implemented");
267  return;
268  }
269  case PcapHelper::DLT_IEEE802_11_RADIO:
270  {
271  Ptr<Packet> p = packet->Copy ();
272  RadiotapHeader header;
273  uint8_t frameFlags = RadiotapHeader::FRAME_FLAG_NONE;
274  header.SetTsft (Simulator::Now ().GetMicroSeconds ());
275 
276  // Our capture includes the FCS, so we set the flag to say so.
278 
279  if (isShortPreamble)
280  {
282  }
283 
284  header.SetFrameFlags (frameFlags);
285  header.SetRate (rate);
286 
287  uint16_t channelFlags = 0;
288  switch (rate)
289  {
290  case 2: // 1Mbps
291  case 4: // 2Mbps
292  case 10: // 5Mbps
293  case 22: // 11Mbps
294  channelFlags |= RadiotapHeader::CHANNEL_FLAG_CCK;
295  break;
296 
297  default:
298  channelFlags |= RadiotapHeader::CHANNEL_FLAG_OFDM;
299  break;
300  }
301 
302  if (channelFreqMhz < 2500)
303  {
305  }
306  else
307  {
309  }
310 
311  header.SetChannelFrequencyAndFlags (channelFreqMhz, channelFlags);
312 
313  p->AddHeader (header);
314  file->Write (Simulator::Now (), p);
315  return;
316  }
317  default:
318  NS_ABORT_MSG ("PcapSniffTxEvent(): Unexpected data link type " << dlt);
319  }
320 }
321 
322 static void
323 PcapSniffRxEvent (
324  Ptr<PcapFileWrapper> file,
325  Ptr<const Packet> packet,
326  uint16_t channelFreqMhz,
327  uint16_t channelNumber,
328  uint32_t rate,
329  bool isShortPreamble,
330  double signalDbm,
331  double noiseDbm)
332 {
333  uint32_t dlt = file->GetDataLinkType ();
334 
335  switch (dlt)
336  {
337  case PcapHelper::DLT_IEEE802_11:
338  file->Write (Simulator::Now (), packet);
339  return;
340  case PcapHelper::DLT_PRISM_HEADER:
341  {
342  NS_FATAL_ERROR ("PcapSniffRxEvent(): DLT_PRISM_HEADER not implemented");
343  return;
344  }
345  case PcapHelper::DLT_IEEE802_11_RADIO:
346  {
347  Ptr<Packet> p = packet->Copy ();
348  RadiotapHeader header;
349  uint8_t frameFlags = RadiotapHeader::FRAME_FLAG_NONE;
350  header.SetTsft (Simulator::Now ().GetMicroSeconds ());
351 
352  // Our capture includes the FCS, so we set the flag to say so.
354 
355  if (isShortPreamble)
356  {
358  }
359 
360  header.SetFrameFlags (frameFlags);
361  header.SetRate (rate);
362 
363  uint16_t channelFlags = 0;
364  switch (rate)
365  {
366  case 2: // 1Mbps
367  case 4: // 2Mbps
368  case 10: // 5Mbps
369  case 22: // 11Mbps
370  channelFlags |= RadiotapHeader::CHANNEL_FLAG_CCK;
371  break;
372 
373  default:
374  channelFlags |= RadiotapHeader::CHANNEL_FLAG_OFDM;
375  break;
376  }
377 
378  if (channelFreqMhz < 2500)
379  {
381  }
382  else
383  {
385  }
386 
387  header.SetChannelFrequencyAndFlags (channelFreqMhz, channelFlags);
388 
389  header.SetAntennaSignalPower (signalDbm);
390  header.SetAntennaNoisePower (noiseDbm);
391 
392  p->AddHeader (header);
393  file->Write (Simulator::Now (), p);
394  return;
395  }
396  default:
397  NS_ABORT_MSG ("PcapSniffRxEvent(): Unexpected data link type " << dlt);
398  }
399 }
400 
401 void
403 {
404  switch (dlt)
405  {
406  case DLT_IEEE802_11:
407  m_pcapDlt = PcapHelper::DLT_IEEE802_11;
408  return;
409  case DLT_PRISM_HEADER:
410  m_pcapDlt = PcapHelper::DLT_PRISM_HEADER;
411  return;
413  m_pcapDlt = PcapHelper::DLT_IEEE802_11_RADIO;
414  return;
415  default:
416  NS_ABORT_MSG ("YansWifiPhyHelper::SetPcapFormat(): Unexpected format");
417  }
418 }
419 
420 void
421 YansWifiPhyHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
422 {
423  //
424  // All of the Pcap enable functions vector through here including the ones
425  // that are wandering through all of devices on perhaps all of the nodes in
426  // the system. We can only deal with devices of type WifiNetDevice.
427  //
428  Ptr<WifiNetDevice> device = nd->GetObject<WifiNetDevice> ();
429  if (device == 0)
430  {
431  NS_LOG_INFO ("YansWifiHelper::EnablePcapInternal(): Device " << &device << " not of type ns3::WifiNetDevice");
432  return;
433  }
434 
435  Ptr<WifiPhy> phy = device->GetPhy ();
436  NS_ABORT_MSG_IF (phy == 0, "YansWifiPhyHelper::EnablePcapInternal(): Phy layer in WifiNetDevice must be set");
437 
438  PcapHelper pcapHelper;
439 
440  std::string filename;
441  if (explicitFilename)
442  {
443  filename = prefix;
444  }
445  else
446  {
447  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
448  }
449 
450  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out, m_pcapDlt);
451 
452  phy->TraceConnectWithoutContext ("MonitorSnifferTx", MakeBoundCallback (&PcapSniffTxEvent, file));
453  phy->TraceConnectWithoutContext ("MonitorSnifferRx", MakeBoundCallback (&PcapSniffRxEvent, file));
454 }
455 
456 void
459  std::string prefix,
460  Ptr<NetDevice> nd,
461  bool explicitFilename)
462 {
463  //
464  // All of the ascii enable functions vector through here including the ones
465  // that are wandering through all of devices on perhaps all of the nodes in
466  // the system. We can only deal with devices of type WifiNetDevice.
467  //
468  Ptr<WifiNetDevice> device = nd->GetObject<WifiNetDevice> ();
469  if (device == 0)
470  {
471  NS_LOG_INFO ("YansWifiHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::WifiNetDevice");
472  return;
473  }
474 
475  //
476  // Our trace sinks are going to use packet printing, so we have to make sure
477  // that is turned on.
478  //
480 
481  uint32_t nodeid = nd->GetNode ()->GetId ();
482  uint32_t deviceid = nd->GetIfIndex ();
483  std::ostringstream oss;
484 
485  //
486  // If we are not provided an OutputStreamWrapper, we are expected to create
487  // one using the usual trace filename conventions and write our traces
488  // without a context since there will be one file per context and therefore
489  // the context would be redundant.
490  //
491  if (stream == 0)
492  {
493  //
494  // Set up an output stream object to deal with private ofstream copy
495  // constructor and lifetime issues. Let the helper decide the actual
496  // name of the file given the prefix.
497  //
498  AsciiTraceHelper asciiTraceHelper;
499 
500  std::string filename;
501  if (explicitFilename)
502  {
503  filename = prefix;
504  }
505  else
506  {
507  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
508  }
509 
510  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
511  //
512  // We could go poking through the phy and the state looking for the
513  // correct trace source, but we can let Config deal with that with
514  // some search cost. Since this is presumably happening at topology
515  // creation time, it doesn't seem much of a price to pay.
516  //
517  oss.str ("");
518  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/RxOk";
519  Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&AsciiPhyReceiveSinkWithoutContext, theStream));
520 
521  oss.str ("");
522  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/Tx";
523  Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&AsciiPhyTransmitSinkWithoutContext, theStream));
524 
525  return;
526  }
527 
528  //
529  // If we are provided an OutputStreamWrapper, we are expected to use it, and
530  // to provide a context. We are free to come up with our own context if we
531  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
532  // compatibility and simplicity, we just use Config::Connect and let it deal
533  // with coming up with a context.
534  //
535  oss.str ("");
536  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/RxOk";
537  Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyReceiveSinkWithContext, stream));
538 
539  oss.str ("");
540  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/Tx";
541  Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyTransmitSinkWithContext, stream));
542 }
543 
544 } // namespace ns3
void AddPropagationLoss(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Manage ASCII trace files for device models.
Definition: trace-helper.h:109
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void SetErrorRateModel(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, uint32_t dataLinkType, uint32_t snapLen=65535, int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
void SetNext(Ptr< PropagationLossModel > next)
Enables a chain of loss models to act on the signal.
Ptr< YansWifiChannel > Create(void) const
Make it easy to create and manage PHY objects for the yans model.
static YansWifiChannelHelper Default(void)
Hold a value for an Attribute.
Definition: attribute.h:51
Manage pcap files for device models.
Definition: trace-helper.h:38
void Write(Time t, Ptr< const Packet > p)
Write the next packet to file.
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
void SetTypeId(TypeId tid)
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)
Enable ascii trace output on the indicated net device.
#define NS_LOG_INFO(msg)
Definition: log.h:264
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
void Set(std::string name, const AttributeValue &v)
static YansWifiPhyHelper Default(void)
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
the interface for Wifi's error models
WifiPreamble
Definition: wifi-preamble.h:29
void SetChannel(Ptr< YansWifiChannel > channel)
double GetSeconds(void) const
Definition: nstime.h:262
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for a pcap file associated with a device...
Definition: trace-helper.cc:80
static void EnablePrinting(void)
Definition: packet.cc:575
Ptr< Object > Create(void) const
calculate a propagation delay.
Hold together all Wifi-related objects.This class holds together ns3::WifiChannel, ns3::WifiPhy, ns3::WifiMac, and, ns3::WifiRemoteStationManager.
Ptr< Packet > Copy(void) const
Definition: packet.cc:131
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:268
802.11 PHY layer modelThis PHY implements a model of 802.11a. The model implemented here is based on ...
Definition: yans-wifi-phy.h:61
virtual Ptr< WifiPhy > Create(Ptr< Node > node, Ptr< WifiNetDevice > device) const
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)
Enable pcap output the indicated net device.
void Set(std::string name, const AttributeValue &value)
manage and create wifi channel objects for the yans model.
static Time Now(void)
Definition: simulator.cc:179
#define NS_ABORT_MSG(msg)
Abnormal program termination.
Definition: abort.h:43
instantiate subclasses of ns3::Object.
Modelize the propagation loss through a transmission medium.
void SetPropagationDelay(std::string name, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition: abort.h:98
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:717
int64_t AssignStreams(Ptr< YansWifiChannel > c, int64_t stream)