A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
waveform-generator.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 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 #include <ns3/object-factory.h>
23 #include <ns3/log.h>
24 #include <ns3/simulator.h>
25 #include <ns3/double.h>
26 #include <ns3/packet-burst.h>
27 #include <ns3/antenna-model.h>
28 
29 #include "waveform-generator.h"
30 
31 NS_LOG_COMPONENT_DEFINE ("WaveformGenerator");
32 
33 namespace ns3 {
34 
35 NS_OBJECT_ENSURE_REGISTERED (WaveformGenerator);
36 
37 WaveformGenerator::WaveformGenerator ()
38  : m_mobility (0),
39  m_netDevice (0),
40  m_channel (0),
41  m_txPowerSpectralDensity (0),
42  m_startTime (Seconds (0)),
43  m_active (false)
44 {
45 
46 }
47 
48 
49 WaveformGenerator::~WaveformGenerator ()
50 {
51 
52 }
53 
54 
55 void
57 {
58  NS_LOG_FUNCTION (this);
59  m_channel = 0;
60  m_netDevice = 0;
61  m_mobility = 0;
62 }
63 
64 TypeId
65 WaveformGenerator::GetTypeId (void)
66 {
67  static TypeId tid = TypeId ("ns3::WaveformGenerator")
69  .AddConstructor<WaveformGenerator> ()
70  .AddAttribute ("Period",
71  "the period (=1/frequency)",
72  TimeValue (Seconds (1.0)),
73  MakeTimeAccessor (&WaveformGenerator::SetPeriod,
75  MakeTimeChecker ())
76  .AddAttribute ("DutyCycle",
77  "the duty cycle of the generator, i.e., the fraction of the period that is occupied by a signal",
78  DoubleValue (0.5),
79  MakeDoubleAccessor (&WaveformGenerator::SetDutyCycle,
81  MakeDoubleChecker<double> ())
82  .AddTraceSource ("TxStart",
83  "Trace fired when a new transmission is started",
84  MakeTraceSourceAccessor (&WaveformGenerator::m_phyTxStartTrace))
85  .AddTraceSource ("TxEnd",
86  "Trace fired when a previosuly started transmission is finished",
87  MakeTraceSourceAccessor (&WaveformGenerator::m_phyTxEndTrace))
88  ;
89  return tid;
90 }
91 
92 
93 
94 Ptr<NetDevice>
96 {
97  return m_netDevice;
98 }
99 
100 
103 {
104  return m_mobility;
105 }
106 
107 
110 {
111  // this device is not interested in RX
112  return 0;
113 }
114 
115 void
117 {
118  m_netDevice = d;
119 }
120 
121 
122 void
124 {
125  m_mobility = m;
126 }
127 
128 
129 void
131 {
133  m_channel = c;
134 }
135 
136 
137 
138 void
140 {
141  NS_LOG_FUNCTION (this << params);
142 }
143 
144 void
146 {
147  NS_LOG_FUNCTION (this << *txPsd);
148  m_txPowerSpectralDensity = txPsd;
149 }
150 
153 {
154  return m_antenna;
155 }
156 
157 void
159 {
160  NS_LOG_FUNCTION (this << a);
161  m_antenna = a;
162 }
163 
164 void
166 {
167  m_period = period;
168 }
169 
170 Time
172 {
173  return m_period;
174 }
175 
176 
177 
178 void
180 {
181  m_dutyCycle = dutyCycle;
182 }
183 
185 {
186  return m_dutyCycle;
187 }
188 
189 
190 
191 void
192 WaveformGenerator::GenerateWaveform ()
193 {
194  NS_LOG_FUNCTION (this);
195 
196  Ptr<SpectrumSignalParameters> txParams = Create<SpectrumSignalParameters> ();
197  txParams->duration = Time (m_period * m_dutyCycle);
198  txParams->psd = m_txPowerSpectralDensity;
199  txParams->txPhy = GetObject<SpectrumPhy> ();
200  txParams->txAntenna = m_antenna;
201 
202  NS_LOG_LOGIC ("generating waveform : " << *m_txPowerSpectralDensity);
203  m_phyTxStartTrace (0);
204  m_channel->StartTx (txParams);
205 
206  if (m_active)
207  {
208  NS_LOG_LOGIC ("scheduling next waveform");
209  Simulator::Schedule (m_period, &WaveformGenerator::GenerateWaveform, this);
210  }
211 }
212 
213 
214 void
216 {
217  NS_LOG_FUNCTION (this);
218  if (!m_active)
219  {
220  NS_LOG_LOGIC ("generator was not active, now starting");
221  m_active = true;
222  m_startTime = Now ();
223  Simulator::ScheduleNow (&WaveformGenerator::GenerateWaveform, this);
224  }
225 }
226 
227 
228 void
230 {
231  NS_LOG_FUNCTION (this);
232  m_active = false;
233 }
234 
235 
236 } // namespace ns3
virtual void DoDispose(void)
keep track of time unit.
Definition: nstime.h:149
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
void SetDevice(Ptr< NetDevice > d)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void StartRx(Ptr< SpectrumSignalParameters > params)
void SetChannel(Ptr< SpectrumChannel > c)
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txs)
void SetDutyCycle(double value)
Ptr< AntennaModel > GetRxAntenna()
hold objects of type ns3::Time
Definition: nstime.h:700
void SetPeriod(Time period)
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Definition: simulator.h:981
void SetAntenna(Ptr< AntennaModel > a)
Ptr< MobilityModel > GetMobility()
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:286
void SetMobility(Ptr< MobilityModel > m)
Ptr< NetDevice > GetDevice()
Hold an floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
Ptr< const SpectrumModel > GetRxSpectrumModel() const