A Discrete-Event Network Simulator
API
spectrum-analyzer-helper.cc
Go to the documentation of this file.
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 #include "ns3/propagation-delay-model.h"
21 #include "ns3/spectrum-propagation-loss-model.h"
22 #include "ns3/mobility-model.h"
23 #include "ns3/log.h"
24 #include "ns3/config.h"
25 #include "ns3/simulator.h"
26 #include "ns3/names.h"
27 #include "ns3/antenna-model.h"
28 #include "ns3/spectrum-channel.h"
29 #include "ns3/spectrum-analyzer.h"
30 #include "ns3/non-communicating-net-device.h"
31 #include "ns3/output-stream-wrapper.h"
32 #include "ns3/trace-helper.h"
34 
35 
36 
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("SpectrumAnalyzerHelper");
41 
42 static void
44  Ptr<const SpectrumValue> avgPowerSpectralDensity)
45 {
46  NS_LOG_FUNCTION (streamWrapper << avgPowerSpectralDensity);
47  std::ostream* ostream = streamWrapper->GetStream ();
48  if (ostream->good ())
49  {
50  Bands::const_iterator fi = avgPowerSpectralDensity->ConstBandsBegin ();
51  Values::const_iterator vi = avgPowerSpectralDensity->ConstValuesBegin ();
52  while (fi != avgPowerSpectralDensity->ConstBandsEnd ())
53  {
54  NS_ASSERT (vi != avgPowerSpectralDensity->ConstValuesEnd ());
55  *ostream << Now ().GetSeconds () << " "
56  << fi->fc << " "
57  << *vi << std::endl;
58  ++fi;
59  ++vi;
60  }
61  // An additional line separates different spectrums sweeps
62  *ostream << std::endl;
63  }
64 }
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);
85  m_channel = channel;
86 }
87 
88 void
89 SpectrumAnalyzerHelper::SetChannel (std::string channelName)
90 {
91  NS_LOG_FUNCTION (this);
92  Ptr<SpectrumChannel> channel = Names::Find<SpectrumChannel> (channelName);
93  m_channel = channel;
94 }
95 
96 
97 void
99 {
100  NS_LOG_FUNCTION (this);
101  m_phy.Set (name, v);
102 }
103 
104 
105 void
107 {
108  NS_LOG_FUNCTION (this);
109  m_device.Set (name, v);
110 }
111 
112 void
114  std::string n0, const AttributeValue &v0,
115  std::string n1, const AttributeValue &v1,
116  std::string n2, const AttributeValue &v2,
117  std::string n3, const AttributeValue &v3,
118  std::string n4, const AttributeValue &v4,
119  std::string n5, const AttributeValue &v5,
120  std::string n6, const AttributeValue &v6,
121  std::string n7, const AttributeValue &v7)
122 {
123  ObjectFactory factory;
124  factory.SetTypeId (type);
125  factory.Set (n0, v0);
126  factory.Set (n1, v1);
127  factory.Set (n2, v2);
128  factory.Set (n3, v3);
129  factory.Set (n4, v4);
130  factory.Set (n5, v5);
131  factory.Set (n6, v6);
132  factory.Set (n7, v7);
133  m_antenna = factory;
134 }
135 
136 
137 void
139 {
140  NS_LOG_FUNCTION (this);
141  m_rxSpectrumModel = m;
142 }
143 
144 void
146 {
147  NS_LOG_FUNCTION (this);
148  m_prefix = prefix;
149 }
150 
151 
152 
155 {
156  NS_LOG_FUNCTION (this);
158  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
159  {
160  Ptr<Node> node = *i;
161 
163 
165  NS_ASSERT (phy);
166 
167  dev->SetPhy (phy);
168 
169  NS_ASSERT (node);
170  phy->SetMobility (node->GetObject<MobilityModel> ());
171 
172  NS_ASSERT (dev);
173  phy->SetDevice (dev);
174 
175  NS_ASSERT_MSG (m_rxSpectrumModel, "you forgot to call SpectrumAnalyzerHelper::SetRxSpectrumModel ()");
176  phy->SetRxSpectrumModel (m_rxSpectrumModel);
177 
178  NS_ASSERT_MSG (m_channel, "you forgot to call SpectrumAnalyzerHelper::SetChannel ()");
179  m_channel->AddRx (phy);
180 
181  dev->SetChannel (m_channel);
182 
183  Ptr<AntennaModel> antenna = (m_antenna.Create ())->GetObject<AntennaModel> ();
184  NS_ASSERT_MSG (antenna, "error in creating the AntennaModel object");
185  phy->SetAntenna (antenna);
186 
187  uint32_t devId = node->AddDevice (dev);
188  devices.Add (dev);
189 
190  if (!m_prefix.empty ())
191  {
192  NS_LOG_LOGIC ("creating new output stream and binding it to the callback");
193  AsciiTraceHelper asciiTraceHelper;
194  std::string filename;
195  filename = asciiTraceHelper.GetFilenameFromDevice (m_prefix, dev);
196  Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream (filename);
197 
198  // note that we don't use AsciiTraceHelper to connect the trace sink, since we use a custom trace sink
199 
200  // the following is inspired from YansWifiPhyHelper::EnableAsciiInternal
201  std::ostringstream oss;
202  oss.str ("");
203  oss << "/NodeList/" << node->GetId () << "/DeviceList/" << devId << "/$ns3::NonCommunicatingNetDevice/Phy/AveragePowerSpectralDensityReport";
205 
206  phy->Start ();
207 
208  }
209  }
210  return devices;
211 }
212 
215 {
216  NS_LOG_FUNCTION (this);
217  return Install (NodeContainer (node));
218 }
219 
221 SpectrumAnalyzerHelper::Install (std::string nodeName) const
222 {
223  NS_LOG_FUNCTION (this);
224  Ptr<Node> node = Names::Find<Node> (nodeName);
225  return Install (node);
226 }
227 
228 
229 } // namespace ns3
Values::const_iterator ConstValuesEnd() const
Manage ASCII trace files for device models.
Definition: trace-helper.h:141
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
tuple devices
Definition: first.py:32
void SetAntenna(std::string type, 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())
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
NetDeviceContainer Install(NodeContainer c) const
Hold a value for an Attribute.
Definition: attribute.h:68
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1487
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
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. ...
This class implements a device which does not communicate, in the sense that it does not interact wit...
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...
Keep track of the current position and velocity of an object.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:327
Bands::const_iterator ConstBandsEnd() const
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
static void WriteAveragePowerSpectralDensityReport(Ptr< OutputStreamWrapper > streamWrapper, Ptr< const SpectrumValue > avgPowerSpectralDensity)
holds a vector of ns3::NetDevice pointers
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
void SetPhyAttribute(std::string name, const AttributeValue &v)
Bands::const_iterator ConstBandsBegin() const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Simple SpectrumPhy implemetation that averages the spectrum power density of incoming transmissions t...
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
#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:84
Instantiate subclasses of ns3::Object.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:120
uint32_t GetId(void) const
Definition: node.cc:106
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper ...
void SetRxSpectrumModel(Ptr< SpectrumModel > m)
Set the spectrum model used by the created SpectrumAnalyzer instances to represent incoming signals...
void EnableAsciiAll(std::string prefix)
Enable ASCII output.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:330
Values::const_iterator ConstValuesBegin() const
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:727
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.