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 "spectrum-interference.h"
23 #include "spectrum-error-model.h"
24 
25 #include <ns3/simulator.h>
26 #include <ns3/log.h>
27 
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("SpectrumInterference");
32 
SpectrumInterference()33 SpectrumInterference::SpectrumInterference ()
34   : m_receiving (false),
35     m_rxSignal (0),
36     m_allSignals (0),
37     m_noise (0),
38     m_errorModel (0)
39 {
40   NS_LOG_FUNCTION (this);
41 }
42 
~SpectrumInterference()43 SpectrumInterference::~SpectrumInterference ()
44 {
45   NS_LOG_FUNCTION (this);
46 }
47 
48 TypeId
GetTypeId()49 SpectrumInterference::GetTypeId ()
50 {
51   static TypeId tid = TypeId ("ns3::SpectrumInterference")
52     .SetParent<Object> ()
53     .SetGroupName ("Spectrum")
54     .AddConstructor<SpectrumInterference> ()
55     ;
56   return tid;
57 }
58 
59 void
DoDispose()60 SpectrumInterference::DoDispose ()
61 {
62   NS_LOG_FUNCTION (this);
63   m_rxSignal = 0;
64   m_allSignals = 0;
65   m_noise = 0;
66   m_errorModel = 0;
67   Object::DoDispose ();
68 }
69 
70 void
StartRx(Ptr<const Packet> p,Ptr<const SpectrumValue> rxPsd)71 SpectrumInterference::StartRx (Ptr<const Packet> p, Ptr<const SpectrumValue> rxPsd)
72 {
73   NS_LOG_FUNCTION (this << p << *rxPsd);
74   m_rxSignal = rxPsd;
75   m_lastChangeTime = Now ();
76   m_receiving = true;
77   m_errorModel->StartRx (p);
78 }
79 
80 void
AbortRx()81 SpectrumInterference::AbortRx ()
82 {
83   m_receiving = false;
84 }
85 
86 bool
EndRx()87 SpectrumInterference::EndRx ()
88 {
89   NS_LOG_FUNCTION (this);
90   ConditionallyEvaluateChunk ();
91   m_receiving = false;
92   return m_errorModel->IsRxCorrect ();
93 }
94 
95 
96 void
AddSignal(Ptr<const SpectrumValue> spd,const Time duration)97 SpectrumInterference::AddSignal (Ptr<const SpectrumValue> spd, const Time duration)
98 {
99   NS_LOG_FUNCTION (this << *spd << duration);
100   DoAddSignal (spd);
101   Simulator::Schedule (duration, &SpectrumInterference::DoSubtractSignal, this, spd);
102 }
103 
104 
105 void
DoAddSignal(Ptr<const SpectrumValue> spd)106 SpectrumInterference::DoAddSignal  (Ptr<const SpectrumValue> spd)
107 {
108   NS_LOG_FUNCTION (this << *spd);
109   ConditionallyEvaluateChunk ();
110   (*m_allSignals) += (*spd);
111   m_lastChangeTime = Now ();
112 }
113 
114 void
DoSubtractSignal(Ptr<const SpectrumValue> spd)115 SpectrumInterference::DoSubtractSignal  (Ptr<const SpectrumValue> spd)
116 {
117   NS_LOG_FUNCTION (this << *spd);
118   ConditionallyEvaluateChunk ();
119   (*m_allSignals) -= (*spd);
120   m_lastChangeTime = Now ();
121 }
122 
123 
124 void
ConditionallyEvaluateChunk()125 SpectrumInterference::ConditionallyEvaluateChunk ()
126 {
127   NS_LOG_FUNCTION (this);
128   NS_LOG_LOGIC ("m_receiving: " << m_receiving );
129   NS_LOG_LOGIC ("m_lastChangeTime: " << m_lastChangeTime << " Now: " << Now ());
130   bool condition  = m_receiving && (Now () > m_lastChangeTime);
131   NS_LOG_LOGIC ("if condition: " << condition);
132   if (condition)
133     {
134       SpectrumValue sinr = (*m_rxSignal) / ((*m_allSignals) - (*m_rxSignal) + (*m_noise));
135       Time duration = Now () - m_lastChangeTime;
136       NS_LOG_LOGIC ("calling m_errorModel->EvaluateChunk (sinr, duration)");
137       m_errorModel->EvaluateChunk (sinr, duration);
138     }
139 }
140 
141 void
SetNoisePowerSpectralDensity(Ptr<const SpectrumValue> noisePsd)142 SpectrumInterference::SetNoisePowerSpectralDensity (Ptr<const SpectrumValue> noisePsd)
143 {
144   NS_LOG_FUNCTION (this << noisePsd);
145   m_noise = noisePsd;
146   // we can initialize m_allSignal only now, because earlier we
147   // didn't know what spectrum model was going to be used.
148   // we'll now create a zeroed SpectrumValue using the same
149   // SpectrumModel which is being specified for the noise.
150   m_allSignals = Create<SpectrumValue> (noisePsd->GetSpectrumModel ());
151 }
152 
153 void
SetErrorModel(Ptr<SpectrumErrorModel> e)154 SpectrumInterference::SetErrorModel (Ptr<SpectrumErrorModel> e)
155 {
156   NS_LOG_FUNCTION (this << e);
157   m_errorModel = e;
158 }
159 
160 
161 
162 
163 } // namespace ns3
164 
165 
166