A Discrete-Event Network Simulator
API
spectrum-analyzer-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 CTTC
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Nicola Baldo <nbaldo@cttc.es>
18  */
20 
21 #include "ns3/antenna-model.h"
22 #include "ns3/config.h"
23 #include "ns3/log.h"
24 #include "ns3/mobility-model.h"
25 #include "ns3/names.h"
26 #include "ns3/non-communicating-net-device.h"
27 #include "ns3/output-stream-wrapper.h"
28 #include "ns3/propagation-delay-model.h"
29 #include "ns3/simulator.h"
30 #include "ns3/spectrum-analyzer.h"
31 #include "ns3/spectrum-channel.h"
32 #include "ns3/spectrum-propagation-loss-model.h"
33 #include "ns3/trace-helper.h"
34 
35 namespace ns3
36 {
37 
38 NS_LOG_COMPONENT_DEFINE("SpectrumAnalyzerHelper");
39 
46 static void
48  Ptr<const SpectrumValue> avgPowerSpectralDensity)
49 {
50  NS_LOG_FUNCTION(streamWrapper << avgPowerSpectralDensity);
51  std::ostream* ostream = streamWrapper->GetStream();
52  if (ostream->good())
53  {
54  auto fi = avgPowerSpectralDensity->ConstBandsBegin();
55  auto vi = avgPowerSpectralDensity->ConstValuesBegin();
56  while (fi != avgPowerSpectralDensity->ConstBandsEnd())
57  {
58  NS_ASSERT(vi != avgPowerSpectralDensity->ConstValuesEnd());
59  *ostream << Now().GetSeconds() << " " << fi->fc << " " << *vi << std::endl;
60  ++fi;
61  ++vi;
62  }
63  // An additional line separates different spectrums sweeps
64  *ostream << std::endl;
65  }
66 }
67 
69 {
70  NS_LOG_FUNCTION(this);
71  m_phy.SetTypeId("ns3::SpectrumAnalyzer");
72  m_device.SetTypeId("ns3::NonCommunicatingNetDevice");
73  m_antenna.SetTypeId("ns3::IsotropicAntennaModel");
74 }
75 
77 {
78  NS_LOG_FUNCTION(this);
79 }
80 
81 void
83 {
84  NS_LOG_FUNCTION(this);
86 }
87 
88 void
89 SpectrumAnalyzerHelper::SetChannel(std::string channelName)
90 {
91  NS_LOG_FUNCTION(this);
92  Ptr<SpectrumChannel> channel = Names::Find<SpectrumChannel>(channelName);
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION(this);
100  m_phy.Set(name, v);
101 }
102 
103 void
105 {
106  NS_LOG_FUNCTION(this);
107  m_device.Set(name, v);
108 }
109 
110 void
112 {
113  NS_LOG_FUNCTION(this);
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION(this);
121  m_prefix = prefix;
122 }
123 
126 {
127  NS_LOG_FUNCTION(this);
129  for (auto i = c.Begin(); i != c.End(); ++i)
130  {
131  Ptr<Node> node = *i;
132 
135 
137  NS_ASSERT(phy);
138 
139  dev->SetPhy(phy);
140 
141  NS_ASSERT(node);
142  phy->SetMobility(node->GetObject<MobilityModel>());
143 
144  NS_ASSERT(dev);
145  phy->SetDevice(dev);
146 
148  "you forgot to call SpectrumAnalyzerHelper::SetRxSpectrumModel ()");
149  phy->SetRxSpectrumModel(m_rxSpectrumModel);
150 
151  NS_ASSERT_MSG(m_channel, "you forgot to call SpectrumAnalyzerHelper::SetChannel ()");
152  m_channel->AddRx(phy);
153 
154  dev->SetChannel(m_channel);
155 
156  Ptr<AntennaModel> antenna = (m_antenna.Create())->GetObject<AntennaModel>();
157  NS_ASSERT_MSG(antenna, "error in creating the AntennaModel object");
158  phy->SetAntenna(antenna);
159 
160  uint32_t devId = node->AddDevice(dev);
161  devices.Add(dev);
162 
163  if (!m_prefix.empty())
164  {
165  NS_LOG_LOGIC("creating new output stream and binding it to the callback");
166  AsciiTraceHelper asciiTraceHelper;
167  std::string filename;
168  filename = asciiTraceHelper.GetFilenameFromDevice(m_prefix, dev);
169  Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream(filename);
170 
171  // note that we don't use AsciiTraceHelper to connect the trace sink, since we use a
172  // custom trace sink
173 
174  // the following is inspired from YansWifiPhyHelper::EnableAsciiInternal
175  std::ostringstream oss;
176  oss.str("");
177  oss << "/NodeList/" << node->GetId() << "/DeviceList/" << devId
178  << "/$ns3::NonCommunicatingNetDevice/Phy/AveragePowerSpectralDensityReport";
180  oss.str(),
182 
183  phy->Start();
184  }
185  }
186  return devices;
187 }
188 
191 {
192  NS_LOG_FUNCTION(this);
193  return Install(NodeContainer(node));
194 }
195 
197 SpectrumAnalyzerHelper::Install(std::string nodeName) const
198 {
199  NS_LOG_FUNCTION(this);
200  Ptr<Node> node = Names::Find<Node>(nodeName);
201  return Install(node);
202 }
203 
204 } // namespace ns3
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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...
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.
Hold a value for an Attribute.
Definition: attribute.h:70
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t GetId() const
Definition: node.cc:117
This class implements a device which does not communicate, in the sense that it does not interact wit...
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
std::ostream * GetStream()
Return a pointer to an ostream previously set in the wrapper.
ObjectFactory m_phy
Object factory for the phy objects.
NetDeviceContainer Install(NodeContainer c) const
Ptr< SpectrumModel > m_rxSpectrumModel
Spectrum model.
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
void SetChannel(Ptr< SpectrumChannel > channel)
Set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper.
ObjectFactory m_antenna
Object factory for the Antenna objects.
std::string m_prefix
Prefix for the output files.
void EnableAsciiAll(std::string prefix)
Enable ASCII output.
Ptr< SpectrumChannel > m_channel
Channel.
ObjectFactory m_device
Object factory for the NetDevice objects.
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the created SpectrumAnalyzer instances to represent incoming signals.
Simple SpectrumPhy implementation that averages the spectrum power density of incoming transmissions ...
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:950
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:765
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
static void WriteAveragePowerSpectralDensityReport(Ptr< OutputStreamWrapper > streamWrapper, Ptr< const SpectrumValue > avgPowerSpectralDensity)
Writes a report of the Average Power Spectral Density.
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:88
phy
Definition: third.py:89