A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
wifi-phy.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 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 "wifi-phy.h"
22 #include "wifi-mode.h"
23 #include "wifi-channel.h"
24 #include "wifi-preamble.h"
25 #include "ns3/simulator.h"
26 #include "ns3/packet.h"
27 #include "ns3/assert.h"
28 #include "ns3/log.h"
29 #include "ns3/double.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/enum.h"
32 #include "ns3/trace-source-accessor.h"
33 #include <math.h>
34 
35 NS_LOG_COMPONENT_DEFINE ("WifiPhy");
36 
37 namespace ns3 {
38 
39 /****************************************************************
40  * This destructor is needed.
41  ****************************************************************/
42 
44 {
45 }
46 
47 /****************************************************************
48  * The actual WifiPhy class
49  ****************************************************************/
50 
52 
53 TypeId
55 {
56  static TypeId tid = TypeId ("ns3::WifiPhy")
57  .SetParent<Object> ()
58  .AddTraceSource ("PhyTxBegin",
59  "Trace source indicating a packet has begun transmitting over the channel medium",
61  .AddTraceSource ("PhyTxEnd",
62  "Trace source indicating a packet has been completely transmitted over the channel. NOTE: the only official WifiPhy implementation available to this date (YansWifiPhy) never fires this trace source.",
64  .AddTraceSource ("PhyTxDrop",
65  "Trace source indicating a packet has been dropped by the device during transmission",
67  .AddTraceSource ("PhyRxBegin",
68  "Trace source indicating a packet has begun being received from the channel medium by the device",
70  .AddTraceSource ("PhyRxEnd",
71  "Trace source indicating a packet has been completely received from the channel medium by the device",
73  .AddTraceSource ("PhyRxDrop",
74  "Trace source indicating a packet has been dropped by the device during reception",
76  .AddTraceSource ("MonitorSnifferRx",
77  "Trace source simulating a wifi device in monitor mode sniffing all received frames",
79  .AddTraceSource ("MonitorSnifferTx",
80  "Trace source simulating the capability of a wifi device in monitor mode to sniff all frames being transmitted",
82  ;
83  return tid;
84 }
85 
87 {
88  NS_LOG_FUNCTION (this);
89 }
90 
92 {
93  NS_LOG_FUNCTION (this);
94 }
95 
96 
99 {
100  switch (payloadMode.GetModulationClass ())
101  {
102  case WIFI_MOD_CLASS_OFDM:
103  {
104  switch (payloadMode.GetBandwidth ())
105  {
106  case 5000000:
108  case 10000000:
110  default:
111  // IEEE Std 802.11-2007, 17.3.2
112  // actually this is only the first part of the PlcpHeader,
113  // because the last 16 bits of the PlcpHeader are using the
114  // same mode of the payload
115  return WifiPhy::GetOfdmRate6Mbps ();
116  }
117  }
118 
121 
122  case WIFI_MOD_CLASS_DSSS:
123  if (preamble == WIFI_PREAMBLE_LONG)
124  {
125  // IEEE Std 802.11-2007, sections 15.2.3 and 18.2.2.1
126  return WifiPhy::GetDsssRate1Mbps ();
127  }
128  else // WIFI_PREAMBLE_SHORT
129  {
130  // IEEE Std 802.11-2007, section 18.2.2.2
131  return WifiPhy::GetDsssRate2Mbps ();
132  }
133 
134  default:
135  NS_FATAL_ERROR ("unsupported modulation class");
136  return WifiMode ();
137  }
138 }
139 
140 uint32_t
142 {
143  switch (payloadMode.GetModulationClass ())
144  {
145  case WIFI_MOD_CLASS_OFDM:
146  {
147  switch (payloadMode.GetBandwidth ())
148  {
149  case 20000000:
150  default:
151  // IEEE Std 802.11-2007, section 17.3.3 and figure 17-4
152  // also section 17.3.2.3, table 17-4
153  // We return the duration of the SIGNAL field only, since the
154  // SERVICE field (which strictly speaking belongs to the PLCP
155  // header, see section 17.3.2 and figure 17-1) is sent using the
156  // payload mode.
157  return 4;
158  case 10000000:
159  // IEEE Std 802.11-2007, section 17.3.2.3, table 17-4
160  return 8;
161  case 5000000:
162  // IEEE Std 802.11-2007, section 17.3.2.3, table 17-4
163  return 16;
164  }
165  }
166 
168  return 16;
169 
170  case WIFI_MOD_CLASS_DSSS:
171  if (preamble == WIFI_PREAMBLE_SHORT)
172  {
173  // IEEE Std 802.11-2007, section 18.2.2.2 and figure 18-2
174  return 24;
175  }
176  else // WIFI_PREAMBLE_LONG
177  {
178  // IEEE Std 802.11-2007, sections 18.2.2.1 and figure 18-1
179  return 48;
180  }
181 
182  default:
183  NS_FATAL_ERROR ("unsupported modulation class");
184  return 0;
185  }
186 }
187 
188 uint32_t
190 {
191  switch (payloadMode.GetModulationClass ())
192  {
193  case WIFI_MOD_CLASS_OFDM:
194  {
195  switch (payloadMode.GetBandwidth ())
196  {
197  case 20000000:
198  default:
199  // IEEE Std 802.11-2007, section 17.3.3, figure 17-4
200  // also section 17.3.2.3, table 17-4
201  return 16;
202  case 10000000:
203  // IEEE Std 802.11-2007, section 17.3.3, table 17-4
204  // also section 17.3.2.3, table 17-4
205  return 32;
206  case 5000000:
207  // IEEE Std 802.11-2007, section 17.3.3
208  // also section 17.3.2.3, table 17-4
209  return 64;
210  }
211  }
212 
214  return 4;
215 
216  case WIFI_MOD_CLASS_DSSS:
217  if (preamble == WIFI_PREAMBLE_SHORT)
218  {
219  // IEEE Std 802.11-2007, section 18.2.2.2 and figure 18-2
220  return 72;
221  }
222  else // WIFI_PREAMBLE_LONG
223  {
224  // IEEE Std 802.11-2007, sections 18.2.2.1 and figure 18-1
225  return 144;
226  }
227 
228  default:
229  NS_FATAL_ERROR ("unsupported modulation class");
230  return 0;
231  }
232 }
233 
234 uint32_t
236 {
237  NS_LOG_FUNCTION (size << payloadMode);
238 
239  switch (payloadMode.GetModulationClass ())
240  {
241  case WIFI_MOD_CLASS_OFDM:
243  {
244  // IEEE Std 802.11-2007, section 17.3.2.3, table 17-4
245  // corresponds to T_{SYM} in the table
246  uint32_t symbolDurationUs;
247 
248  switch (payloadMode.GetBandwidth ())
249  {
250  case 20000000:
251  default:
252  symbolDurationUs = 4;
253  break;
254  case 10000000:
255  symbolDurationUs = 8;
256  break;
257  case 5000000:
258  symbolDurationUs = 16;
259  break;
260  }
261 
262  // IEEE Std 802.11-2007, section 17.3.2.2, table 17-3
263  // corresponds to N_{DBPS} in the table
264  double numDataBitsPerSymbol = payloadMode.GetDataRate () * symbolDurationUs / 1e6;
265 
266  // IEEE Std 802.11-2007, section 17.3.5.3, equation (17-11)
267  uint32_t numSymbols = lrint (ceil ((16 + size * 8.0 + 6.0) / numDataBitsPerSymbol));
268 
269  // Add signal extension for ERP PHY
270  if (payloadMode.GetModulationClass () == WIFI_MOD_CLASS_ERP_OFDM)
271  {
272  return numSymbols * symbolDurationUs + 6;
273  }
274  else
275  {
276  return numSymbols * symbolDurationUs;
277  }
278  }
279 
280  case WIFI_MOD_CLASS_DSSS:
281  // IEEE Std 802.11-2007, section 18.2.3.5
282  NS_LOG_LOGIC (" size=" << size
283  << " mode=" << payloadMode
284  << " rate=" << payloadMode.GetDataRate () );
285  return lrint (ceil ((size * 8.0) / (payloadMode.GetDataRate () / 1.0e6)));
286 
287  default:
288  NS_FATAL_ERROR ("unsupported modulation class");
289  return 0;
290  }
291 }
292 
293 Time
294 WifiPhy::CalculateTxDuration (uint32_t size, WifiMode payloadMode, WifiPreamble preamble)
295 {
296  uint32_t duration = GetPlcpPreambleDurationMicroSeconds (payloadMode, preamble)
297  + GetPlcpHeaderDurationMicroSeconds (payloadMode, preamble)
298  + GetPayloadDurationMicroSeconds (size, payloadMode);
299  return MicroSeconds (duration);
300 }
301 
302 
303 void
305 {
306  m_phyTxBeginTrace (packet);
307 }
308 
309 void
311 {
312  m_phyTxEndTrace (packet);
313 }
314 
315 void
317 {
318  m_phyTxDropTrace (packet);
319 }
320 
321 void
323 {
324  m_phyRxBeginTrace (packet);
325 }
326 
327 void
329 {
330  m_phyRxEndTrace (packet);
331 }
332 
333 void
335 {
336  m_phyRxDropTrace (packet);
337 }
338 
339 void
340 WifiPhy::NotifyMonitorSniffRx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble, double signalDbm, double noiseDbm)
341 {
342  m_phyMonitorSniffRxTrace (packet, channelFreqMhz, channelNumber, rate, isShortPreamble, signalDbm, noiseDbm);
343 }
344 
345 void
346 WifiPhy::NotifyMonitorSniffTx (Ptr<const Packet> packet, uint16_t channelFreqMhz, uint16_t channelNumber, uint32_t rate, bool isShortPreamble)
347 {
348  m_phyMonitorSniffTxTrace (packet, channelFreqMhz, channelNumber, rate, isShortPreamble);
349 }
350 
351 
356 WifiMode
358 {
359  static WifiMode mode =
360  WifiModeFactory::CreateWifiMode ("DsssRate1Mbps",
362  true,
363  22000000, 1000000,
365  2);
366  return mode;
367 }
368 
369 WifiMode
371 {
372  static WifiMode mode =
373  WifiModeFactory::CreateWifiMode ("DsssRate2Mbps",
375  true,
376  22000000, 2000000,
378  4);
379  return mode;
380 }
381 
382 
386 WifiMode
388 {
389  static WifiMode mode =
390  WifiModeFactory::CreateWifiMode ("DsssRate5_5Mbps",
392  true,
393  22000000, 5500000,
395  4);
396  return mode;
397 }
398 
399 WifiMode
401 {
402  static WifiMode mode =
403  WifiModeFactory::CreateWifiMode ("DsssRate11Mbps",
405  true,
406  22000000, 11000000,
408  4);
409  return mode;
410 }
411 
412 
416 WifiMode
418 {
419  static WifiMode mode =
420  WifiModeFactory::CreateWifiMode ("ErpOfdmRate6Mbps",
422  true,
423  20000000, 6000000,
425  2);
426  return mode;
427 }
428 
429 WifiMode
431 {
432  static WifiMode mode =
433  WifiModeFactory::CreateWifiMode ("ErpOfdmRate9Mbps",
435  false,
436  20000000, 9000000,
438  2);
439  return mode;
440 }
441 
442 WifiMode
444 {
445  static WifiMode mode =
446  WifiModeFactory::CreateWifiMode ("ErpOfdmRate12Mbps",
448  true,
449  20000000, 12000000,
451  4);
452  return mode;
453 }
454 
455 WifiMode
457 {
458  static WifiMode mode =
459  WifiModeFactory::CreateWifiMode ("ErpOfdmRate18Mbps",
461  false,
462  20000000, 18000000,
464  4);
465  return mode;
466 }
467 
468 WifiMode
470 {
471  static WifiMode mode =
472  WifiModeFactory::CreateWifiMode ("ErpOfdmRate24Mbps",
474  true,
475  20000000, 24000000,
477  16);
478  return mode;
479 }
480 
481 WifiMode
483 {
484  static WifiMode mode =
485  WifiModeFactory::CreateWifiMode ("ErpOfdmRate36Mbps",
487  false,
488  20000000, 36000000,
490  16);
491  return mode;
492 }
493 
494 WifiMode
496 {
497  static WifiMode mode =
498  WifiModeFactory::CreateWifiMode ("ErpOfdmRate48Mbps",
500  false,
501  20000000, 48000000,
503  64);
504  return mode;
505 }
506 
507 WifiMode
509 {
510  static WifiMode mode =
511  WifiModeFactory::CreateWifiMode ("ErpOfdmRate54Mbps",
513  false,
514  20000000, 54000000,
516  64);
517  return mode;
518 }
519 
520 
524 WifiMode
526 {
527  static WifiMode mode =
528  WifiModeFactory::CreateWifiMode ("OfdmRate6Mbps",
530  true,
531  20000000, 6000000,
533  2);
534  return mode;
535 }
536 
537 WifiMode
539 {
540  static WifiMode mode =
541  WifiModeFactory::CreateWifiMode ("OfdmRate9Mbps",
543  false,
544  20000000, 9000000,
546  2);
547  return mode;
548 }
549 
550 WifiMode
552 {
553  static WifiMode mode =
554  WifiModeFactory::CreateWifiMode ("OfdmRate12Mbps",
556  true,
557  20000000, 12000000,
559  4);
560  return mode;
561 }
562 
563 WifiMode
565 {
566  static WifiMode mode =
567  WifiModeFactory::CreateWifiMode ("OfdmRate18Mbps",
569  false,
570  20000000, 18000000,
572  4);
573  return mode;
574 }
575 
576 WifiMode
578 {
579  static WifiMode mode =
580  WifiModeFactory::CreateWifiMode ("OfdmRate24Mbps",
582  true,
583  20000000, 24000000,
585  16);
586  return mode;
587 }
588 
589 WifiMode
591 {
592  static WifiMode mode =
593  WifiModeFactory::CreateWifiMode ("OfdmRate36Mbps",
595  false,
596  20000000, 36000000,
598  16);
599  return mode;
600 }
601 
602 WifiMode
604 {
605  static WifiMode mode =
606  WifiModeFactory::CreateWifiMode ("OfdmRate48Mbps",
608  false,
609  20000000, 48000000,
611  64);
612  return mode;
613 }
614 
615 WifiMode
617 {
618  static WifiMode mode =
619  WifiModeFactory::CreateWifiMode ("OfdmRate54Mbps",
621  false,
622  20000000, 54000000,
624  64);
625  return mode;
626 }
627 
628 /* 10 MHz channel rates */
629 WifiMode
631 {
632  static WifiMode mode =
633  WifiModeFactory::CreateWifiMode ("OfdmRate3MbpsBW10MHz",
635  true,
636  10000000, 3000000,
638  2);
639  return mode;
640 }
641 
642 WifiMode
644 {
645  static WifiMode mode =
646  WifiModeFactory::CreateWifiMode ("OfdmRate4_5MbpsBW10MHz",
648  false,
649  10000000, 4500000,
651  2);
652  return mode;
653 }
654 
655 WifiMode
657 {
658  static WifiMode mode =
659  WifiModeFactory::CreateWifiMode ("OfdmRate6MbpsBW10MHz",
661  true,
662  10000000, 6000000,
664  4);
665  return mode;
666 }
667 
668 WifiMode
670 {
671  static WifiMode mode =
672  WifiModeFactory::CreateWifiMode ("OfdmRate9MbpsBW10MHz",
674  false,
675  10000000, 9000000,
677  4);
678  return mode;
679 }
680 
681 WifiMode
683 {
684  static WifiMode mode =
685  WifiModeFactory::CreateWifiMode ("OfdmRate12MbpsBW10MHz",
687  true,
688  10000000, 12000000,
690  16);
691  return mode;
692 }
693 
694 WifiMode
696 {
697  static WifiMode mode =
698  WifiModeFactory::CreateWifiMode ("OfdmRate18MbpsBW10MHz",
700  false,
701  10000000, 18000000,
703  16);
704  return mode;
705 }
706 
707 WifiMode
709 {
710  static WifiMode mode =
711  WifiModeFactory::CreateWifiMode ("OfdmRate24MbpsBW10MHz",
713  false,
714  10000000, 24000000,
716  64);
717  return mode;
718 }
719 
720 WifiMode
722 {
723  static WifiMode mode =
724  WifiModeFactory::CreateWifiMode ("OfdmRate27MbpsBW10MHz",
726  false,
727  10000000, 27000000,
729  64);
730  return mode;
731 }
732 
733 /* 5 MHz channel rates */
734 WifiMode
736 {
737  static WifiMode mode =
738  WifiModeFactory::CreateWifiMode ("OfdmRate1_5MbpsBW5MHz",
740  true,
741  5000000, 1500000,
743  2);
744  return mode;
745 }
746 
747 WifiMode
749 {
750  static WifiMode mode =
751  WifiModeFactory::CreateWifiMode ("OfdmRate2_25MbpsBW5MHz",
753  false,
754  5000000, 2250000,
756  2);
757  return mode;
758 }
759 
760 WifiMode
762 {
763  static WifiMode mode =
764  WifiModeFactory::CreateWifiMode ("OfdmRate3MbpsBW5MHz",
766  true,
767  5000000, 3000000,
769  4);
770  return mode;
771 }
772 
773 WifiMode
775 {
776  static WifiMode mode =
777  WifiModeFactory::CreateWifiMode ("OfdmRate4_5MbpsBW5MHz",
779  false,
780  5000000, 4500000,
782  4);
783  return mode;
784 }
785 
786 WifiMode
788 {
789  static WifiMode mode =
790  WifiModeFactory::CreateWifiMode ("OfdmRate6MbpsBW5MHz",
792  true,
793  5000000, 6000000,
795  16);
796  return mode;
797 }
798 
799 WifiMode
801 {
802  static WifiMode mode =
803  WifiModeFactory::CreateWifiMode ("OfdmRate9MbpsBW5MHz",
805  false,
806  5000000, 9000000,
808  16);
809  return mode;
810 }
811 
812 WifiMode
814 {
815  static WifiMode mode =
816  WifiModeFactory::CreateWifiMode ("OfdmRate12MbpsBW5MHz",
818  false,
819  5000000, 12000000,
821  64);
822  return mode;
823 }
824 
825 WifiMode
827 {
828  static WifiMode mode =
829  WifiModeFactory::CreateWifiMode ("OfdmRate13_5MbpsBW5MHz",
831  false,
832  5000000, 13500000,
834  64);
835  return mode;
836 }
837 
838 std::ostream& operator<< (std::ostream& os, enum WifiPhy::State state)
839 {
840  switch (state)
841  {
842  case WifiPhy::IDLE:
843  return (os << "IDLE");
844  case WifiPhy::CCA_BUSY:
845  return (os << "CCA_BUSY");
846  case WifiPhy::TX:
847  return (os << "TX");
848  case WifiPhy::RX:
849  return (os << "RX");
850  case WifiPhy::SWITCHING:
851  return (os << "SWITCHING");
852  default:
853  NS_FATAL_ERROR ("Invalid WifiPhy state");
854  return (os << "INVALID");
855  }
856 }
857 
858 } // namespace ns3
859 
860 namespace {
861 
862 static class Constructor
863 {
864 public:
866  {
903  }
904 } g_constructor;
905 }