author | Vedran Miletić <rivanvx@gmail.com> |
Tue, 02 Aug 2011 17:42:33 -0400 | |
changeset 7385 | 10beb0e53130 |
parent 7142 | 89a701fec3a1 |
child 7553 | 2b93d333dea6 |
permissions | -rw-r--r-- |
7385
10beb0e53130
standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents:
7142
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
6349 | 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 |
#include <ns3/waveform-generator.h> |
|
22 |
#include <ns3/object-factory.h> |
|
23 |
#include <ns3/log.h> |
|
24 |
#include <math.h> |
|
25 |
#include <ns3/simulator.h> |
|
26 |
#include <ns3/trace-source-accessor.h> |
|
27 |
#include <ns3/packet-burst.h> |
|
28 |
#include <ns3/callback.h> |
|
29 |
#include "half-duplex-ideal-phy.h" |
|
30 |
#include "spectrum-error-model.h" |
|
31 |
||
32 |
NS_LOG_COMPONENT_DEFINE ("HalfDuplexIdealPhy"); |
|
33 |
||
34 |
namespace ns3 { |
|
35 |
||
36 |
||
37 |
NS_OBJECT_ENSURE_REGISTERED (HalfDuplexIdealPhy); |
|
38 |
||
39 |
HalfDuplexIdealPhy::HalfDuplexIdealPhy () |
|
40 |
: m_mobility (0), |
|
41 |
m_netDevice (0), |
|
42 |
m_channel (0), |
|
43 |
m_txPsd (0), |
|
44 |
m_state (IDLE) |
|
45 |
{ |
|
46 |
m_interference.SetErrorModel (CreateObject<ShannonSpectrumErrorModel> ()); |
|
47 |
} |
|
48 |
||
49 |
||
50 |
HalfDuplexIdealPhy::~HalfDuplexIdealPhy () |
|
51 |
{ |
|
52 |
} |
|
53 |
||
54 |
void |
|
55 |
HalfDuplexIdealPhy::DoDispose () |
|
56 |
{ |
|
57 |
NS_LOG_FUNCTION (this); |
|
58 |
m_mobility = 0; |
|
59 |
m_netDevice = 0; |
|
60 |
m_channel = 0; |
|
61 |
m_txPsd = 0; |
|
62 |
m_rxPsd = 0; |
|
63 |
m_txPacket = 0; |
|
64 |
m_rxPacket = 0; |
|
65 |
m_phyMacTxEndCallback = MakeNullCallback< void, Ptr<const Packet> > (); |
|
66 |
m_phyMacRxStartCallback = MakeNullCallback< void > (); |
|
67 |
m_phyMacRxEndErrorCallback = MakeNullCallback< void > (); |
|
68 |
m_phyMacRxEndOkCallback = MakeNullCallback< void, Ptr<Packet> > (); |
|
69 |
SpectrumPhy::DoDispose (); |
|
70 |
} |
|
71 |
||
72 |
std::ostream& operator<< (std::ostream& os, HalfDuplexIdealPhy::State s) |
|
73 |
{ |
|
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6969
diff
changeset
|
74 |
switch (s) |
6349 | 75 |
{ |
76 |
case HalfDuplexIdealPhy::IDLE: |
|
77 |
os << "IDLE"; |
|
78 |
break; |
|
79 |
case HalfDuplexIdealPhy::RX: |
|
80 |
os << "RX"; |
|
81 |
break; |
|
82 |
case HalfDuplexIdealPhy::TX: |
|
83 |
os << "TX"; |
|
84 |
break; |
|
85 |
default: |
|
86 |
os << "UNKNOWN"; |
|
87 |
break; |
|
88 |
} |
|
89 |
return os; |
|
90 |
} |
|
91 |
||
92 |
||
93 |
TypeId |
|
94 |
HalfDuplexIdealPhy::GetTypeId (void) |
|
95 |
{ |
|
96 |
static TypeId tid = TypeId ("ns3::HalfDuplexIdealPhy") |
|
97 |
.SetParent<SpectrumPhy> () |
|
98 |
.AddConstructor<HalfDuplexIdealPhy> () |
|
99 |
.AddAttribute ("Rate", |
|
100 |
"The PHY rate used by this device", |
|
101 |
DataRateValue (DataRate ("1Mbps")), |
|
102 |
MakeDataRateAccessor (&HalfDuplexIdealPhy::SetRate, |
|
103 |
&HalfDuplexIdealPhy::GetRate), |
|
104 |
MakeDataRateChecker ()) |
|
105 |
.AddTraceSource ("TxStart", |
|
106 |
"Trace fired when a new transmission is started", |
|
107 |
MakeTraceSourceAccessor (&HalfDuplexIdealPhy::m_phyTxStartTrace)) |
|
108 |
.AddTraceSource ("TxEnd", |
|
109 |
"Trace fired when a previosuly started transmission is finished", |
|
110 |
MakeTraceSourceAccessor (&HalfDuplexIdealPhy::m_phyTxEndTrace)) |
|
111 |
.AddTraceSource ("RxStart", |
|
112 |
"Trace fired when the start of a signal is detected", |
|
113 |
MakeTraceSourceAccessor (&HalfDuplexIdealPhy::m_phyRxStartTrace)) |
|
114 |
.AddTraceSource ("RxAbort", |
|
115 |
"Trace fired when a previously started RX is aborted before time", |
|
116 |
MakeTraceSourceAccessor (&HalfDuplexIdealPhy::m_phyRxAbortTrace)) |
|
117 |
.AddTraceSource ("RxEndOk", |
|
118 |
"Trace fired when a previosuly started RX terminates successfully", |
|
119 |
MakeTraceSourceAccessor (&HalfDuplexIdealPhy::m_phyRxEndOkTrace)) |
|
120 |
.AddTraceSource ("RxEndError", |
|
121 |
"Trace fired when a previosuly started RX terminates with an error (packet is corrupted)", |
|
122 |
MakeTraceSourceAccessor (&HalfDuplexIdealPhy::m_phyRxEndErrorTrace)) |
|
123 |
; |
|
124 |
return tid; |
|
125 |
} |
|
126 |
||
127 |
||
128 |
||
129 |
Ptr<Object> |
|
130 |
HalfDuplexIdealPhy::GetDevice () |
|
131 |
{ |
|
132 |
NS_LOG_FUNCTION (this); |
|
133 |
return m_netDevice; |
|
134 |
} |
|
135 |
||
136 |
||
137 |
Ptr<Object> |
|
138 |
HalfDuplexIdealPhy::GetMobility () |
|
139 |
{ |
|
140 |
NS_LOG_FUNCTION (this); |
|
141 |
return m_mobility; |
|
142 |
} |
|
143 |
||
144 |
||
145 |
void |
|
146 |
HalfDuplexIdealPhy::SetDevice (Ptr<Object> d) |
|
147 |
{ |
|
148 |
NS_LOG_FUNCTION (this << d); |
|
149 |
m_netDevice = d; |
|
150 |
} |
|
151 |
||
152 |
||
153 |
void |
|
154 |
HalfDuplexIdealPhy::SetMobility (Ptr<Object> m) |
|
155 |
{ |
|
156 |
NS_LOG_FUNCTION (this << m); |
|
157 |
m_mobility = m; |
|
158 |
} |
|
159 |
||
160 |
||
161 |
void |
|
162 |
HalfDuplexIdealPhy::SetChannel (Ptr<SpectrumChannel> c) |
|
163 |
{ |
|
164 |
NS_LOG_FUNCTION (this << c); |
|
165 |
m_channel = c; |
|
166 |
} |
|
167 |
||
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6969
diff
changeset
|
168 |
Ptr<const SpectrumModel> |
6349 | 169 |
HalfDuplexIdealPhy::GetRxSpectrumModel () const |
170 |
{ |
|
171 |
if (m_txPsd) |
|
172 |
{ |
|
173 |
return m_txPsd->GetSpectrumModel (); |
|
174 |
} |
|
175 |
else |
|
176 |
{ |
|
177 |
return 0; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
||
182 |
SpectrumType |
|
183 |
HalfDuplexIdealPhy::GetSpectrumType () |
|
184 |
{ |
|
185 |
NS_LOG_FUNCTION (this); |
|
186 |
static SpectrumType st = SpectrumTypeFactory::Create ("IdealOfdm"); |
|
187 |
return st; |
|
188 |
} |
|
189 |
||
190 |
void |
|
191 |
HalfDuplexIdealPhy::SetTxPowerSpectralDensity (Ptr<SpectrumValue> txPsd) |
|
192 |
{ |
|
193 |
NS_LOG_FUNCTION (this << txPsd); |
|
194 |
NS_ASSERT (txPsd); |
|
195 |
m_txPsd = txPsd; |
|
196 |
NS_LOG_INFO ( *txPsd << *m_txPsd); |
|
197 |
} |
|
198 |
||
199 |
void |
|
200 |
HalfDuplexIdealPhy::SetNoisePowerSpectralDensity (Ptr<const SpectrumValue> noisePsd) |
|
201 |
{ |
|
202 |
NS_LOG_FUNCTION (this << noisePsd); |
|
203 |
NS_ASSERT (noisePsd); |
|
204 |
m_interference.SetNoisePowerSpectralDensity (noisePsd); |
|
205 |
} |
|
206 |
||
207 |
void |
|
208 |
HalfDuplexIdealPhy::SetRate (DataRate rate) |
|
209 |
{ |
|
210 |
NS_LOG_FUNCTION (this << rate); |
|
211 |
m_rate = rate; |
|
212 |
} |
|
213 |
||
214 |
DataRate |
|
215 |
HalfDuplexIdealPhy::GetRate () const |
|
216 |
{ |
|
217 |
NS_LOG_FUNCTION (this); |
|
218 |
return m_rate; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
void |
|
6969 | 223 |
HalfDuplexIdealPhy::SetGenericPhyTxEndCallback (GenericPhyTxEndCallback c) |
6349 | 224 |
{ |
225 |
NS_LOG_FUNCTION (this); |
|
226 |
m_phyMacTxEndCallback = c; |
|
227 |
} |
|
228 |
||
229 |
void |
|
6969 | 230 |
HalfDuplexIdealPhy::SetGenericPhyRxStartCallback (GenericPhyRxStartCallback c) |
6349 | 231 |
{ |
232 |
NS_LOG_FUNCTION (this); |
|
233 |
m_phyMacRxStartCallback = c; |
|
234 |
} |
|
235 |
||
236 |
||
237 |
void |
|
6969 | 238 |
HalfDuplexIdealPhy::SetGenericPhyRxEndErrorCallback (GenericPhyRxEndErrorCallback c) |
6349 | 239 |
{ |
240 |
NS_LOG_FUNCTION (this); |
|
241 |
m_phyMacRxEndErrorCallback = c; |
|
242 |
} |
|
243 |
||
244 |
||
245 |
void |
|
6969 | 246 |
HalfDuplexIdealPhy::SetGenericPhyRxEndOkCallback (GenericPhyRxEndOkCallback c) |
6349 | 247 |
{ |
248 |
NS_LOG_FUNCTION (this); |
|
249 |
m_phyMacRxEndOkCallback = c; |
|
250 |
} |
|
251 |
||
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6969
diff
changeset
|
252 |
void |
6349 | 253 |
HalfDuplexIdealPhy::ChangeState (State newState) |
254 |
{ |
|
255 |
NS_LOG_LOGIC (this << " state: " << m_state << " -> " << newState); |
|
256 |
m_state = newState; |
|
257 |
} |
|
258 |
||
259 |
bool |
|
260 |
HalfDuplexIdealPhy::StartTx (Ptr<Packet> p) |
|
261 |
{ |
|
262 |
NS_LOG_FUNCTION (this << p); |
|
263 |
NS_LOG_LOGIC (this << "state: " << m_state); |
|
264 |
||
265 |
m_phyTxStartTrace (p); |
|
266 |
||
267 |
switch (m_state) |
|
268 |
{ |
|
269 |
case RX: |
|
270 |
AbortRx (); |
|
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6969
diff
changeset
|
271 |
// fall through |
6349 | 272 |
|
273 |
case IDLE: |
|
274 |
{ |
|
275 |
m_txPacket = p; |
|
276 |
ChangeState (TX); |
|
277 |
double txTimeSeconds = m_rate.CalculateTxTime (p->GetSize ()); |
|
278 |
Ptr<PacketBurst> pb = Create<PacketBurst> (); |
|
279 |
pb->AddPacket (p); |
|
280 |
m_channel->StartTx (pb, m_txPsd, GetSpectrumType (), Seconds (txTimeSeconds), GetObject<SpectrumPhy> ()); |
|
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6969
diff
changeset
|
281 |
Simulator::Schedule (Seconds (txTimeSeconds), &HalfDuplexIdealPhy::EndTx, this); |
6349 | 282 |
} |
283 |
break; |
|
284 |
||
285 |
case TX: |
|
286 |
||
287 |
return true; |
|
288 |
break; |
|
289 |
} |
|
290 |
return false; |
|
291 |
} |
|
292 |
||
293 |
||
294 |
void |
|
295 |
HalfDuplexIdealPhy::EndTx () |
|
296 |
{ |
|
297 |
NS_LOG_FUNCTION (this); |
|
298 |
NS_LOG_LOGIC (this << "state: " << m_state); |
|
299 |
||
300 |
NS_ASSERT (m_state == TX); |
|
301 |
||
302 |
m_phyTxEndTrace (m_txPacket); |
|
303 |
||
304 |
if (!m_phyMacTxEndCallback.IsNull ()) |
|
305 |
{ |
|
306 |
m_phyMacTxEndCallback (m_txPacket); |
|
307 |
} |
|
308 |
||
309 |
m_txPacket = 0; |
|
310 |
ChangeState (IDLE); |
|
311 |
} |
|
312 |
||
313 |
||
314 |
void |
|
315 |
HalfDuplexIdealPhy::StartRx (Ptr<PacketBurst> pb, Ptr <const SpectrumValue> rxPsd, SpectrumType st, Time duration) |
|
316 |
{ |
|
317 |
NS_LOG_FUNCTION (this << pb << rxPsd << st << duration); |
|
318 |
NS_LOG_LOGIC (this << "state: " << m_state); |
|
319 |
||
320 |
// interference will happen regardless of the state of the receiver |
|
321 |
m_interference.AddSignal (rxPsd, duration); |
|
322 |
||
323 |
// the device might start RX only if the signal is of a type understood by this device |
|
324 |
// this corresponds in real device to preamble detection |
|
325 |
if (st == GetSpectrumType ()) |
|
326 |
{ |
|
327 |
switch (m_state) |
|
328 |
{ |
|
329 |
case TX: |
|
330 |
// the PHY will not notice this incoming signal |
|
331 |
break; |
|
332 |
||
333 |
case RX: |
|
334 |
// we should check if we should re-sync on a new incoming signal and discard the old one |
|
335 |
// (somebody calls this the "capture" effect) |
|
336 |
// criteria considered to do might include the following: |
|
337 |
// 1) signal strength (e.g., as returned by rxPsd.Norm ()) |
|
338 |
// 2) how much time has passed since previous RX attempt started |
|
339 |
// if re-sync (capture) is done, then we should call AbortRx () |
|
340 |
break; |
|
341 |
||
342 |
case IDLE: |
|
343 |
// preamble detection and synchronization is supposed to be always successful. |
|
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6969
diff
changeset
|
344 |
NS_LOG_LOGIC (this << " receiving " << pb->GetNPackets () << " packet(s)" ); |
6349 | 345 |
NS_ASSERT (pb->GetNPackets () == 1); // this PHY only supports a single packet per waveform |
346 |
Ptr<Packet> p = pb->GetPackets ().front (); |
|
347 |
m_phyRxStartTrace (p); |
|
348 |
m_rxPacket = p; |
|
349 |
m_rxPsd = rxPsd; |
|
350 |
ChangeState (RX); |
|
351 |
if (!m_phyMacRxStartCallback.IsNull ()) |
|
352 |
{ |
|
353 |
NS_LOG_LOGIC (this << " calling m_phyMacRxStartCallback"); |
|
354 |
m_phyMacRxStartCallback (); |
|
355 |
} |
|
356 |
else |
|
357 |
{ |
|
358 |
NS_LOG_LOGIC (this << " m_phyMacRxStartCallback is NULL"); |
|
359 |
} |
|
360 |
m_interference.StartRx (p, rxPsd); |
|
361 |
NS_LOG_LOGIC (this << " scheduling EndRx with delay " << duration); |
|
362 |
m_endRxEventId = Simulator::Schedule (duration, &HalfDuplexIdealPhy::EndRx, this); |
|
363 |
||
364 |
break; |
|
365 |
||
366 |
} |
|
367 |
} |
|
7142
89a701fec3a1
run check-style.py on src/spectrum
Nicola Baldo <nicola@baldo.biz>
parents:
6969
diff
changeset
|
368 |
|
6349 | 369 |
NS_LOG_LOGIC (this << "state: " << m_state); |
370 |
} |
|
371 |
||
372 |
||
373 |
void |
|
374 |
HalfDuplexIdealPhy::AbortRx () |
|
375 |
{ |
|
376 |
NS_LOG_FUNCTION (this); |
|
377 |
NS_LOG_LOGIC (this << "state: " << m_state); |
|
378 |
||
379 |
NS_ASSERT (m_state == RX); |
|
380 |
m_phyRxAbortTrace (m_rxPacket); |
|
381 |
m_endRxEventId.Cancel (); |
|
382 |
m_rxPacket = 0; |
|
383 |
ChangeState (IDLE); |
|
384 |
} |
|
385 |
||
386 |
||
387 |
void |
|
388 |
HalfDuplexIdealPhy::EndRx () |
|
389 |
{ |
|
390 |
NS_LOG_FUNCTION (this); |
|
391 |
NS_LOG_LOGIC (this << "state: " << m_state); |
|
392 |
||
393 |
NS_ASSERT (m_state == RX); |
|
394 |
||
395 |
bool rxOk = m_interference.EndRx (); |
|
396 |
||
397 |
if (rxOk) |
|
398 |
{ |
|
399 |
m_phyRxEndOkTrace (m_rxPacket); |
|
400 |
if (!m_phyMacRxEndOkCallback.IsNull ()) |
|
401 |
{ |
|
402 |
NS_LOG_LOGIC (this << " calling m_phyMacRxEndOkCallback"); |
|
403 |
m_phyMacRxEndOkCallback (m_rxPacket); |
|
404 |
} |
|
405 |
else |
|
406 |
{ |
|
407 |
NS_LOG_LOGIC (this << " m_phyMacRxEndOkCallback is NULL"); |
|
408 |
} |
|
409 |
} |
|
410 |
else |
|
411 |
{ |
|
412 |
m_phyRxEndErrorTrace (m_rxPacket); |
|
413 |
if (!m_phyMacRxEndErrorCallback.IsNull ()) |
|
414 |
{ |
|
415 |
NS_LOG_LOGIC (this << " calling m_phyMacRxEndErrorCallback"); |
|
416 |
m_phyMacRxEndErrorCallback (); |
|
417 |
} |
|
418 |
else |
|
419 |
{ |
|
420 |
NS_LOG_LOGIC (this << " m_phyMacRxEndErrorCallback is NULL"); |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
ChangeState (IDLE); |
|
425 |
m_rxPacket = 0; |
|
426 |
m_rxPsd = 0; |
|
427 |
} |
|
428 |
||
429 |
||
430 |
||
431 |
} // namespace ns3 |