6349
|
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/multi-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 |
#include <ns3/waveform-generator-helper.h>
|
|
44 |
#include <ns3/spectrum-analyzer-helper.h>
|
|
45 |
#include <ns3/non-communicating-net-device.h>
|
|
46 |
#include <ns3/microwave-oven-spectrum-value-helper.h>
|
|
47 |
|
|
48 |
NS_LOG_COMPONENT_DEFINE ("OfdmWithWaveformGenerator");
|
|
49 |
|
|
50 |
using namespace ns3;
|
|
51 |
|
|
52 |
static bool g_verbose = false;
|
|
53 |
|
|
54 |
|
|
55 |
void
|
|
56 |
PhyTxStartTrace (std::string context, Ptr<const Packet> p)
|
|
57 |
{
|
|
58 |
if (g_verbose)
|
|
59 |
{
|
|
60 |
std::cout << context << " PHY TX START p: " << p << std::endl;
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
void
|
|
66 |
PhyTxEndTrace (std::string context, Ptr<const Packet> p)
|
|
67 |
{
|
|
68 |
if (g_verbose)
|
|
69 |
{
|
|
70 |
std::cout << context << " PHY TX END p: " << p << std::endl;
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
|
74 |
void
|
|
75 |
PhyRxStartTrace (std::string context, Ptr<const Packet> p)
|
|
76 |
{
|
|
77 |
if (g_verbose)
|
|
78 |
{
|
|
79 |
std::cout << context << " PHY RX START p:" << p << std::endl;
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
void
|
|
84 |
PhyRxEndOkTrace (std::string context, Ptr<const Packet> p)
|
|
85 |
{
|
|
86 |
if (g_verbose)
|
|
87 |
{
|
|
88 |
std::cout << context << " PHY RX END OK p:" << p << std::endl;
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
void
|
|
93 |
PhyRxEndErrorTrace (std::string context, Ptr<const Packet> p)
|
|
94 |
{
|
|
95 |
if (g_verbose)
|
|
96 |
{
|
|
97 |
std::cout << context << " PHY RX END ERROR p:" << p << std::endl;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
|
|
102 |
void
|
|
103 |
ReceivePacket (Ptr<Socket> socket)
|
|
104 |
{
|
|
105 |
Ptr<Packet> packet;
|
|
106 |
uint64_t bytes = 0;
|
|
107 |
while (packet = socket->Recv ())
|
|
108 |
{
|
|
109 |
bytes += packet->GetSize ();
|
|
110 |
}
|
|
111 |
if (g_verbose)
|
|
112 |
{
|
|
113 |
std::cout << "SOCKET received " << bytes << " bytes" <<std::endl;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
Ptr<Socket>
|
|
118 |
SetupPacketReceive (Ptr<Node> node)
|
|
119 |
{
|
|
120 |
TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
|
|
121 |
Ptr<Socket> sink = Socket::CreateSocket (node, tid);
|
|
122 |
sink->Bind ();
|
|
123 |
sink->SetRecvCallback (MakeCallback (&ReceivePacket));
|
|
124 |
return sink;
|
|
125 |
}
|
|
126 |
|
|
127 |
int main (int argc, char** argv)
|
|
128 |
{
|
|
129 |
CommandLine cmd;
|
|
130 |
cmd.AddValue ("verbose", "Print trace information if true", g_verbose);
|
|
131 |
cmd.Parse (argc, argv);
|
|
132 |
|
|
133 |
NodeContainer ofdmNodes;
|
|
134 |
NodeContainer waveformGeneratorNodes;
|
|
135 |
NodeContainer spectrumAnalyzerNodes;
|
|
136 |
NodeContainer allNodes;
|
|
137 |
|
|
138 |
ofdmNodes.Create (2);
|
|
139 |
waveformGeneratorNodes.Create (1);
|
|
140 |
spectrumAnalyzerNodes.Create (1);
|
|
141 |
allNodes.Add (ofdmNodes);
|
|
142 |
allNodes.Add (waveformGeneratorNodes);
|
|
143 |
allNodes.Add (spectrumAnalyzerNodes);
|
|
144 |
|
|
145 |
MobilityHelper mobility;
|
|
146 |
Ptr<ListPositionAllocator> nodePositionList = CreateObject<ListPositionAllocator> ();
|
|
147 |
nodePositionList->Add (Vector (5.0, 0.0, 0.0)); // TX node
|
|
148 |
nodePositionList->Add (Vector (0.0, 0.0, 0.0)); // RX node
|
|
149 |
nodePositionList->Add (Vector (10.0, 0.0, 0.0)); // Microwave Oven
|
|
150 |
nodePositionList->Add (Vector (0.0, 0.0, 0.0)); // Spectrum Analyzer
|
|
151 |
mobility.SetPositionAllocator (nodePositionList);
|
|
152 |
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
|
|
153 |
|
|
154 |
mobility.Install (allNodes);
|
|
155 |
|
|
156 |
|
|
157 |
SpectrumChannelHelper channelHelper = SpectrumChannelHelper::Default ();
|
|
158 |
channelHelper.SetChannel ("ns3::MultiModelSpectrumChannel");
|
|
159 |
Ptr<SpectrumChannel> channel = channelHelper.Create ();
|
|
160 |
|
|
161 |
|
|
162 |
/////////////////////////
|
|
163 |
// Configure ofdm nodes
|
|
164 |
////////////////////////
|
|
165 |
|
|
166 |
WifiSpectrumValue5MhzFactory sf;
|
|
167 |
|
|
168 |
double txPower = 0.1; // Watts
|
|
169 |
uint32_t channelNumber = 2;
|
|
170 |
Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity (txPower, channelNumber);
|
|
171 |
|
|
172 |
// for the noise, we use the Power Spectral Density of thermal noise
|
|
173 |
// at room temperature. The value of the PSD will be constant over the band of interest.
|
|
174 |
const double k = 1.381e-23; //Boltzmann's constant
|
|
175 |
const double T = 290; // temperature in Kelvin
|
|
176 |
double noisePsdValue = k*T; // watts per hertz
|
|
177 |
Ptr<SpectrumValue> noisePsd = sf.CreateConstant (noisePsdValue);
|
|
178 |
|
|
179 |
|
|
180 |
AdhocAlohaNoackIdealPhyHelper adhocAlohaOfdmHelper;
|
|
181 |
adhocAlohaOfdmHelper.SetChannel(channel);
|
|
182 |
adhocAlohaOfdmHelper.SetTxPowerSpectralDensity (txPsd);
|
|
183 |
adhocAlohaOfdmHelper.SetNoisePowerSpectralDensity (noisePsd);
|
|
184 |
adhocAlohaOfdmHelper.SetPhyAttribute ("Rate", DataRateValue (DataRate ("1Mbps")));
|
|
185 |
NetDeviceContainer ofdmDevices = adhocAlohaOfdmHelper.Install (ofdmNodes);
|
|
186 |
|
|
187 |
PacketSocketHelper packetSocket;
|
|
188 |
packetSocket.Install (ofdmNodes);
|
|
189 |
|
|
190 |
PacketSocketAddress socket;
|
|
191 |
socket.SetSingleDevice(ofdmDevices.Get (0)->GetIfIndex ());
|
|
192 |
socket.SetPhysicalAddress (ofdmDevices.Get (1)->GetAddress ());
|
|
193 |
socket.SetProtocol (1);
|
|
194 |
|
|
195 |
OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
|
|
196 |
onoff.SetAttribute ("OnTime", RandomVariableValue (ExponentialVariable (0.4)));
|
|
197 |
onoff.SetAttribute ("OffTime", RandomVariableValue (ExponentialVariable (0.1)));
|
|
198 |
onoff.SetAttribute ("DataRate", DataRateValue (DataRate ("0.4Mbps")));
|
|
199 |
onoff.SetAttribute ("PacketSize", UintegerValue (1500));
|
|
200 |
|
|
201 |
ApplicationContainer apps = onoff.Install (ofdmNodes.Get (0));
|
|
202 |
apps.Start (Seconds (0.0));
|
|
203 |
apps.Stop (Seconds (2));
|
|
204 |
|
|
205 |
Ptr<Socket> recvSink = SetupPacketReceive (ofdmNodes.Get (1));
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
/////////////////////////////////
|
|
212 |
// Configure waveform generator
|
|
213 |
/////////////////////////////////
|
|
214 |
|
|
215 |
Ptr<SpectrumValue> mwoPsd = MicrowaveOvenSpectrumValueHelper::CreatePowerSpectralDensityMwo1 ();
|
|
216 |
NS_LOG_INFO ("mwoPsd : " << *mwoPsd);
|
|
217 |
|
|
218 |
WaveformGeneratorHelper waveformGeneratorHelper;
|
|
219 |
waveformGeneratorHelper.SetChannel(channel);
|
|
220 |
waveformGeneratorHelper.SetTxPowerSpectralDensity (mwoPsd);
|
|
221 |
|
|
222 |
waveformGeneratorHelper.SetPhyAttribute ("Period", TimeValue(Seconds(1.0/60))); // corresponds to 60 Hz
|
|
223 |
waveformGeneratorHelper.SetPhyAttribute ("DutyCycle", DoubleValue(0.5));
|
|
224 |
NetDeviceContainer waveformGeneratorDevices = waveformGeneratorHelper.Install (waveformGeneratorNodes);
|
|
225 |
|
|
226 |
Simulator::Schedule(Seconds(0.1), &WaveformGenerator::Start,
|
|
227 |
waveformGeneratorDevices.Get (0)->GetObject<NonCommunicatingNetDevice> ()->GetPhy ()->GetObject<WaveformGenerator> ());
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
/////////////////////////////////
|
|
232 |
// Configure spectrum analyzer
|
|
233 |
/////////////////////////////////
|
|
234 |
|
|
235 |
|
|
236 |
SpectrumAnalyzerHelper spectrumAnalyzerHelper;
|
|
237 |
spectrumAnalyzerHelper.SetChannel(channel);
|
|
238 |
spectrumAnalyzerHelper.SetRxSpectrumModel (SpectrumModelIsm2400MhzRes1Mhz);
|
|
239 |
spectrumAnalyzerHelper.SetPhyAttribute ("Resolution", TimeValue(MilliSeconds (2)));
|
|
240 |
spectrumAnalyzerHelper.SetPhyAttribute ("NoisePowerSpectralDensity", DoubleValue (1e-15)); // -120 dBm/Hz
|
|
241 |
spectrumAnalyzerHelper.EnableAsciiAll ("spectrum-analyzer-output");
|
|
242 |
NetDeviceContainer spectrumAnalyzerDevices = spectrumAnalyzerHelper.Install (spectrumAnalyzerNodes);
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback (&PhyTxStartTrace));
|
|
250 |
Config::Connect ("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback (&PhyTxEndTrace));
|
|
251 |
Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback (&PhyRxStartTrace));
|
|
252 |
Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback (&PhyRxEndOkTrace));
|
|
253 |
Config::Connect ("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback (&PhyRxEndErrorTrace));
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
Simulator::Stop (Seconds (0.3));
|
|
259 |
|
|
260 |
Simulator::Run ();
|
|
261 |
|
|
262 |
Simulator::Destroy ();
|
|
263 |
|
|
264 |
}
|
|
265 |
|
|
266 |
|