examples/spectrum/adhoc-aloha-ideal-phy.cc
changeset 6349 4bab6b10a034
equal deleted inserted replaced
6348:43e956769ceb 6349:4bab6b10a034
       
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2010 CTTC
       
     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: Nicola Baldo <nbaldo@cttc.es>
       
    19  */
       
    20 
       
    21 
       
    22 
       
    23 #include <iostream>
       
    24 
       
    25 #include <ns3/core-module.h>
       
    26 #include <ns3/common-module.h>
       
    27 #include <ns3/node-module.h>
       
    28 #include <ns3/simulator-module.h>
       
    29 #include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
       
    30 #include <ns3/spectrum-model-300kHz-300GHz-log.h>
       
    31 #include <ns3/wifi-spectrum-value-helper.h>
       
    32 #include <ns3/single-model-spectrum-channel.h>
       
    33 #include <ns3/waveform-generator.h>
       
    34 #include <ns3/spectrum-analyzer.h>
       
    35 #include <ns3/log.h>
       
    36 #include <string>
       
    37 #include <ns3/friis-spectrum-propagation-loss.h>
       
    38 #include <ns3/propagation-delay-model.h>
       
    39 #include <ns3/mobility-module.h>
       
    40 #include <ns3/spectrum-helper.h>
       
    41 #include <ns3/helper-module.h>
       
    42 #include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
       
    43 
       
    44 NS_LOG_COMPONENT_DEFINE ("TestAdhocOfdmAloha");
       
    45 
       
    46 using namespace ns3;
       
    47 
       
    48 static bool g_verbose = false;
       
    49 
       
    50 void
       
    51 PhyTxStartTrace (std::string context, Ptr<const Packet> p)
       
    52 {  
       
    53   if (g_verbose)
       
    54     {
       
    55       std::cout << context << " PHY TX START p: " << p << std::endl;
       
    56     }
       
    57 }
       
    58 
       
    59 
       
    60 void
       
    61 PhyTxEndTrace (std::string context, Ptr<const Packet> p)
       
    62 {  
       
    63   if (g_verbose)
       
    64     {
       
    65       std::cout << context << " PHY TX END p: " << p << std::endl;
       
    66     }
       
    67 }
       
    68 
       
    69 void
       
    70 PhyRxStartTrace (std::string context, Ptr<const Packet> p)
       
    71 {
       
    72   if (g_verbose)
       
    73     {
       
    74       std::cout << context << " PHY RX START p:" << p << std::endl; 
       
    75     }
       
    76 }
       
    77 
       
    78 void
       
    79 PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
       
    80 {
       
    81   if (g_verbose)
       
    82     {
       
    83       std::cout << context << " PHY RX END OK p:" << p << std::endl; 
       
    84     }
       
    85 }
       
    86 
       
    87 void
       
    88 PhyRxEndErrorTrace (std::string context, Ptr<const Packet> p)
       
    89 {
       
    90   if (g_verbose)
       
    91     {
       
    92       std::cout << context << " PHY RX END ERROR p:" << p << std::endl; 
       
    93     }
       
    94 }
       
    95 
       
    96 
       
    97 void
       
    98 ReceivePacket (Ptr<Socket> socket)
       
    99 {
       
   100   Ptr<Packet> packet;
       
   101   uint64_t bytes = 0;
       
   102   while (packet = socket->Recv ())
       
   103     {
       
   104       bytes += packet->GetSize ();
       
   105     }
       
   106   if (g_verbose)
       
   107     {
       
   108       std::cout << "SOCKET received " << bytes << " bytes" <<std::endl; 
       
   109     }
       
   110 }
       
   111 
       
   112 Ptr<Socket>
       
   113 SetupPacketReceive (Ptr<Node> node)
       
   114 {
       
   115   TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
       
   116   Ptr<Socket> sink = Socket::CreateSocket (node, tid);
       
   117   sink->Bind ();
       
   118   sink->SetRecvCallback (MakeCallback (&ReceivePacket));
       
   119   return sink;
       
   120 }
       
   121 
       
   122 int main (int argc, char** argv)
       
   123 {
       
   124   CommandLine cmd;
       
   125   cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
       
   126   cmd.Parse (argc, argv);
       
   127   
       
   128   NodeContainer c;
       
   129   c.Create (2);
       
   130 
       
   131   MobilityHelper mobility;
       
   132   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
       
   133   positionAlloc->Add (Vector (0.0, 0.0, 0.0));
       
   134   positionAlloc->Add (Vector (5.0, 0.0, 0.0));
       
   135   mobility.SetPositionAllocator (positionAlloc);
       
   136   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
       
   137 
       
   138   mobility.Install (c);
       
   139   
       
   140    
       
   141   SpectrumChannelHelper channelHelper = SpectrumChannelHelper::Default ();
       
   142   Ptr<SpectrumChannel> channel = channelHelper.Create ();
       
   143 
       
   144   WifiSpectrumValue5MhzFactory sf;
       
   145 
       
   146   double txPower = 0.1; // Watts
       
   147   uint32_t channelNumber = 1;
       
   148   Ptr<SpectrumValue> txPsd =  sf.CreateTxPowerSpectralDensity (txPower, channelNumber);  
       
   149   
       
   150   // for the noise, we use the Power Spectral Density of thermal noise
       
   151   // at room temperature. The value of the PSD will be constant over the band of interest.  
       
   152   const double k = 1.381e-23; //Boltzmann's constant
       
   153   const double T = 290; // temperature in Kelvin
       
   154   double noisePsdValue = k*T; // watts per hertz
       
   155   Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);   
       
   156 
       
   157   AdhocAlohaNoackIdealPhyHelper deviceHelper;
       
   158   deviceHelper.SetChannel(channel);
       
   159   deviceHelper.SetTxPowerSpectralDensity (txPsd);
       
   160   deviceHelper.SetNoisePowerSpectralDensity (noisePsd);
       
   161   deviceHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate ("1Mbps")));
       
   162   NetDeviceContainer devices = deviceHelper.Install (c);
       
   163 
       
   164   PacketSocketHelper packetSocket;
       
   165   packetSocket.Install (c);
       
   166 
       
   167   PacketSocketAddress socket;
       
   168   socket.SetSingleDevice(devices.Get (0)->GetIfIndex ());
       
   169   socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
       
   170   socket.SetProtocol (1);
       
   171 
       
   172   OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
       
   173   onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (250)));
       
   174   onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
       
   175   onoff.SetAttribute ("DataRate", DataRateValue (DataRate ("0.5Mbps")));
       
   176   onoff.SetAttribute ("PacketSize", UintegerValue (125));
       
   177 
       
   178   ApplicationContainer apps = onoff.Install (c.Get (0));
       
   179   apps.Start (Seconds (0.1));
       
   180   apps.Stop (Seconds (0.104));
       
   181   
       
   182   Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
       
   183 
       
   184   Simulator::Stop (Seconds (10.0));
       
   185 
       
   186   Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback (&PhyTxStartTrace));
       
   187   Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback (&PhyTxEndTrace));
       
   188   Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback (&PhyRxStartTrace));
       
   189   Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
       
   190   Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback (&PhyRxEndErrorTrace));
       
   191 
       
   192 
       
   193   Simulator::Run ();
       
   194 
       
   195   Simulator::Destroy ();
       
   196 
       
   197   return 0;
       
   198 }