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 #include <ns3/object.h>
22 #include <ns3/spectrum-interference.h>
23 #include <ns3/spectrum-error-model.h>
24 #include <ns3/log.h>
25 #include <ns3/test.h>
26 #include <ns3/simulator.h>
27 #include <ns3/packet.h>
28 #include <ns3/ptr.h>
29 #include <iostream>
30 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE ("SpectrumInterferenceTest");
34 
35 
36 
37 class SpectrumInterferenceTestCase : public TestCase
38 {
39 public:
40   SpectrumInterferenceTestCase (Ptr<SpectrumValue> s, uint32_t txBytes, bool rxCorrect, std::string name);
41   virtual ~SpectrumInterferenceTestCase ();
42   virtual void DoRun (void);
43   void RetrieveTestResult (SpectrumInterference* si);
44 
45 private:
46   Ptr<SpectrumValue> m_s;
47   uint32_t m_txBytes;
48   uint32_t m_rxCorrectKnownOutcome;
49   Ptr<const SpectrumModel> m_mySpectrumModel;
50 };
51 
52 
53 
SpectrumInterferenceTestCase(Ptr<SpectrumValue> s,uint32_t txBytes,bool rxCorrect,std::string name)54 SpectrumInterferenceTestCase::SpectrumInterferenceTestCase (Ptr<SpectrumValue> s, uint32_t txBytes, bool rxCorrect, std::string name)
55   : TestCase (name),
56     m_s (s),
57     m_txBytes (txBytes),
58     m_rxCorrectKnownOutcome (rxCorrect),
59     m_mySpectrumModel (s->GetSpectrumModel ())
60 {
61 }
62 
~SpectrumInterferenceTestCase()63 SpectrumInterferenceTestCase::~SpectrumInterferenceTestCase ()
64 {
65 }
66 
67 
68 
69 void
DoRun(void)70 SpectrumInterferenceTestCase::DoRun (void)
71 {
72   Ptr<SpectrumValue> n = Create<SpectrumValue> (m_mySpectrumModel);
73   Ptr<SpectrumValue> i1 = Create<SpectrumValue> (m_mySpectrumModel);
74   Ptr<SpectrumValue> i2 = Create<SpectrumValue> (m_mySpectrumModel);
75   Ptr<SpectrumValue> i3 = Create<SpectrumValue> (m_mySpectrumModel);
76   Ptr<SpectrumValue> i4 = Create<SpectrumValue> (m_mySpectrumModel);
77 
78 
79   (*n)[0] = 5.000000000000e-19;
80   (*n)[1] = 4.545454545455e-19;
81 
82   (*i1)[0] = 5.000000000000e-18;
83   (*i2)[0] = 5.000000000000e-16;
84   (*i3)[0] = 1.581138830084e-16;
85   (*i4)[0] = 7.924465962306e-17;
86   (*i1)[1] = 1.437398936440e-18;
87   (*i2)[1] = 5.722388235428e-16;
88   (*i3)[1] = 7.204059965732e-17;
89   (*i4)[1] = 5.722388235428e-17;
90 
91 
92   SpectrumInterference si;
93   si.SetErrorModel (CreateObject<ShannonSpectrumErrorModel> ());
94   si.SetNoisePowerSpectralDensity (n);
95 
96   Time ts  = Seconds (1);
97   Time ds  = Seconds (1);
98   Time ti1 = Seconds (0);
99   Time di1 = Seconds (3);
100   Time ti2 = Seconds (0.7);
101   Time di2 = Seconds (1);
102   Time ti3 = Seconds (1.2);
103   Time di3 = Seconds (1);
104   Time ti4 = Seconds (1.5);
105   Time di4 = Seconds (0.1);
106 
107   Simulator::Schedule (ts,  &SpectrumInterference::AddSignal, &si, m_s,  ds);
108   Simulator::Schedule (ti1, &SpectrumInterference::AddSignal, &si, i1, di1);
109   Simulator::Schedule (ti2, &SpectrumInterference::AddSignal, &si, i2, di2);
110   Simulator::Schedule (ti3, &SpectrumInterference::AddSignal, &si, i3, di3);
111   Simulator::Schedule (ti4, &SpectrumInterference::AddSignal, &si, i4, di4);
112 
113   Ptr<Packet> p = Create<Packet> (m_txBytes);
114   Simulator::Schedule (ts,  &SpectrumInterference::StartRx, &si, p, m_s);
115   Simulator::Schedule (ts + ds,  &SpectrumInterferenceTestCase::RetrieveTestResult, this, &si);
116 
117   Simulator::Run ();
118   // the above will return and after RetrieveTestResults have
119   // been called and after all signals have expired
120   Simulator::Destroy ();
121 }
122 
123 
124 
125 void
RetrieveTestResult(SpectrumInterference * si)126 SpectrumInterferenceTestCase::RetrieveTestResult (SpectrumInterference* si)
127 {
128   NS_TEST_ASSERT_MSG_EQ (si->EndRx (), m_rxCorrectKnownOutcome, "no message");
129 }
130 
131 
132 
133 class SpectrumInterferenceTestSuite : public TestSuite
134 {
135 public:
136   SpectrumInterferenceTestSuite ();
137 };
138 
SpectrumInterferenceTestSuite()139 SpectrumInterferenceTestSuite::SpectrumInterferenceTestSuite ()
140   : TestSuite ("spectrum-interference", UNIT)
141 {
142 
143   NS_LOG_INFO ("creating SpectrumInterferenceTestSuite");
144 
145   Ptr<const SpectrumModel> m;
146 
147   Bands bands;
148   struct BandInfo bi;
149 
150   bi.fl = 2.400e9;
151   bi.fc = 2.410e9;
152   bi.fh = 2.420e9;
153   bands.push_back (bi);
154 
155   bi.fl = 2.420e9;
156   bi.fc = 2.431e9;
157   bi.fh = 2.442e9;
158   bands.push_back (bi);
159 
160   m = Create<SpectrumModel> (bands);
161 
162 
163   double b; // max deliverable bytes
164 
165   const double e = 1e-5; // max tolerated relative error for
166                          // deliverable bytes
167 
168   // Power Spectral Density of the signal of interest  = [-46 -48] dBm;
169   Ptr<SpectrumValue> s1  = Create<SpectrumValue> (m);
170   (*s1)[0] = 1.255943215755e-15;
171   (*s1)[1] = 7.204059965732e-16;
172   b = 10067205.5632012;
173   AddTestCase (new SpectrumInterferenceTestCase (s1,   0, true,            "sdBm  = [-46 -48]  tx bytes: 1"), TestCase::QUICK);
174   AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * 0.5 + 0.5), true,   "sdBm  = [-46 -48]  tx bytes: b*0.5"), TestCase::QUICK);
175   AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * (1 - e) + 0.5), true, "sdBm  = [-46 -48]  tx bytes: b*(1-e)"), TestCase::QUICK);
176   AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * (1 + e) + 0.5), false, "sdBm  = [-46 -48]  tx bytes: b*(1+e)"), TestCase::QUICK);
177   AddTestCase (new SpectrumInterferenceTestCase (s1, static_cast<uint32_t> (b * 1.5 + 0.5), false,   "sdBm  = [-46 -48]  tx bytes: b*1.5"), TestCase::QUICK);
178   AddTestCase (new SpectrumInterferenceTestCase (s1, 0xffffffff, false,     "sdBm  = [-46 -48]  tx bytes: 2^32-1"), TestCase::QUICK);
179 
180   // Power Spectral Density of the signal of interest  =  [-63 -61] dBm;
181   Ptr<SpectrumValue> s2  = Create<SpectrumValue> (m);
182   (*s2)[0] = 2.505936168136e-17;
183   (*s2)[1] = 3.610582885110e-17;
184   b = 882401.591840728;
185   AddTestCase (new SpectrumInterferenceTestCase (s2,   1, true,            "sdBm  = [-63 -61]  tx bytes: 1"), TestCase::QUICK);
186   AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * 0.5 + 0.5), true,   "sdBm  = [-63 -61]  tx bytes: b*0.5"), TestCase::QUICK);
187   AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * (1 - e) + 0.5), true, "sdBm  = [-63 -61]  tx bytes: b*(1-e)"), TestCase::QUICK);
188   AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * (1 + e) + 0.5), false, "sdBm  = [-63 -61]  tx bytes: b*(1+e)"), TestCase::QUICK);
189   AddTestCase (new SpectrumInterferenceTestCase (s2, static_cast<uint32_t> (b * 1.5 + 0.5), false,   "sdBm  = [-63 -61]  tx bytes: b*1.5"), TestCase::QUICK);
190   AddTestCase (new SpectrumInterferenceTestCase (s2, 0xffffffff, false,     "sdBm  = [-63 -61]  tx bytes: 2^32-1"), TestCase::QUICK);
191 
192 }
193 
194 static SpectrumInterferenceTestSuite spectrumInterferenceTestSuite;
195