A Discrete-Event Network Simulator
API
uan-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Leonard Tracy <lentracy@gmail.com>
18  */
19 
20 #include "ns3/callback.h"
21 #include "ns3/constant-position-mobility-model.h"
22 #include "ns3/node.h"
23 #include "ns3/object-factory.h"
24 #include "ns3/pointer.h"
25 #include "ns3/simulator.h"
26 #include "ns3/test.h"
27 #include "ns3/uan-channel.h"
28 #include "ns3/uan-mac-aloha.h"
29 #include "ns3/uan-net-device.h"
30 #include "ns3/uan-phy-gen.h"
31 #include "ns3/uan-prop-model-ideal.h"
32 #include "ns3/uan-transducer-hd.h"
33 
34 using namespace ns3;
35 
42 class UanTest : public TestCase
43 {
44  public:
45  UanTest();
46 
47  void DoRun() override;
48 
49  private:
56  Ptr<UanNetDevice> CreateNode(Vector pos, Ptr<UanChannel> chan);
61  bool DoPhyTests();
73  uint32_t DoOnePhyTest(Time t1,
74  Time t2,
75  uint32_t r1,
76  uint32_t r2,
77  Ptr<UanPropModel> prop,
78  uint16_t mode1 = 0,
79  uint16_t mode2 = 0);
88  bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
94  void SendOnePacket(Ptr<UanNetDevice> dev, uint16_t mode);
96  uint32_t m_bytesRx;
97 };
98 
100  : TestCase("UAN")
101 {
102 }
103 
104 bool
106  Ptr<const Packet> pkt,
107  uint16_t /* mode */,
108  const Address& /* sender */)
109 {
110  m_bytesRx += pkt->GetSize();
111  return true;
112 }
113 
114 void
116 {
117  Ptr<Packet> pkt = Create<Packet>(17);
118  dev->SetTxModeIndex(mode);
119  dev->Send(pkt, dev->GetBroadcast(), 0);
120 }
121 
124 {
126  Ptr<Node> node = CreateObject<Node>();
127  Ptr<UanNetDevice> dev = CreateObject<UanNetDevice>();
128  Ptr<UanMacAloha> mac = CreateObject<UanMacAloha>();
129  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel>();
130 
131  Ptr<UanTransducerHd> trans = CreateObject<UanTransducerHd>();
132 
133  mobility->SetPosition(pos);
134  node->AggregateObject(mobility);
135  mac->SetAddress(Mac8Address::Allocate());
136 
137  dev->SetPhy(phy);
138  dev->SetMac(mac);
139  dev->SetChannel(chan);
140  dev->SetTransducer(trans);
141  node->AddDevice(dev);
142 
143  return dev;
144 }
145 
146 uint32_t
148  Time txTime2,
149  uint32_t r1,
150  uint32_t r2,
151  Ptr<UanPropModel> prop,
152  uint16_t mode1,
153  uint16_t mode2)
154 {
155  Ptr<UanChannel> channel = CreateObject<UanChannel>();
156  channel->SetAttribute("PropagationModel", PointerValue(prop));
157 
158  Ptr<UanNetDevice> dev0 = CreateNode(Vector(r1, 50, 50), channel);
159  Ptr<UanNetDevice> dev1 = CreateNode(Vector(0, 50, 50), channel);
160  Ptr<UanNetDevice> dev2 = CreateNode(Vector(r1 + r2, 50, 50), channel);
161 
162  dev0->SetReceiveCallback(MakeCallback(&UanTest::RxPacket, this));
163 
164  Simulator::Schedule(txTime1, &UanTest::SendOnePacket, this, dev1, mode1);
165  Simulator::Schedule(txTime2, &UanTest::SendOnePacket, this, dev2, mode2);
166 
167  m_bytesRx = 0;
168  Simulator::Stop(Seconds(20.0));
169  Simulator::Run();
170  Simulator::Destroy();
171 
172  return m_bytesRx;
173 }
174 
175 bool
177 {
178  // Phy Gen / Default PER / Default SINR
179  UanModesList mList;
180  UanTxMode mode =
181  UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode");
182  mList.AppendMode(UanTxMode(mode));
183  Ptr<UanPhyPerGenDefault> perDef = CreateObject<UanPhyPerGenDefault>();
184  Ptr<UanPhyCalcSinrDefault> sinrDef = CreateObject<UanPhyCalcSinrDefault>();
185  m_phyFac.SetTypeId("ns3::UanPhyGen");
186  m_phyFac.Set("PerModel", PointerValue(perDef));
187  m_phyFac.Set("SinrModel", PointerValue(sinrDef));
188  m_phyFac.Set("SupportedModes", UanModesListValue(mList));
189 
190  Ptr<UanPropModelIdeal> prop = CreateObject<UanPropModelIdeal>();
191 
192  // No collision (Get 2 packets)
194  34,
195  "Should have received 34 bytes from 2 disjoint packets");
196 
197  // Collision (Lose both packets)
199  0,
200  "Expected collision resulting in loss of both packets");
201 
202  // Phy Gen / FH-FSK SINR check
203 
204  Ptr<UanPhyCalcSinrFhFsk> sinrFhfsk = CreateObject<UanPhyCalcSinrFhFsk>();
205  m_phyFac.Set("PerModel", PointerValue(perDef));
206  m_phyFac.Set("SinrModel", PointerValue(sinrFhfsk));
207  m_phyFac.Set("SupportedModes", UanModesListValue(mList));
208 
209 #ifdef UAN_PROP_BH_INSTALLED
210  Ptr<UanPropModelBh> prop = CreateObject<UanPropModelBh>();
211 #endif // UAN_PROP_BH_INSTALLED
212 
213  // No collision (Get 2 packets)
215  34,
216  "Should have received 34 bytes from 2 disjoint packets");
217 
218  // Should correctly receive first arriving packet
219  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(DoOnePhyTest(Seconds(1.0), Seconds(1.0126), 50, 50, prop),
220  17,
221  "Should have received 17 bytes from first arriving packet");
222 
223  // Packets should collide and both be lost
225  DoOnePhyTest(Seconds(1.0), Seconds(1.0 + 7.01 * (13.0 / 80.0)), 50, 50, prop),
226  0,
227  "Packets should collide, but received data");
228 
229  // Phy Dual
230  UanTxMode mode00 =
231  UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode00");
232  UanTxMode mode10 =
233  UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode10");
234  UanTxMode mode20 =
235  UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode20");
236  UanTxMode mode01 =
237  UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode01");
238  UanTxMode mode11 =
239  UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode11");
240  UanTxMode mode21 =
241  UanTxModeFactory::CreateMode(UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode21");
242 
243  UanModesList m0;
244  m0.AppendMode(mode00);
245  m0.AppendMode(mode10);
246  m0.AppendMode(mode20);
248  m1.AppendMode(mode01);
249  m1.AppendMode(mode11);
250  m1.AppendMode(mode21);
251 
253  m_phyFac.SetTypeId("ns3::UanPhyDual");
254  m_phyFac.Set("SupportedModesPhy1", UanModesListValue(m0));
255  m_phyFac.Set("SupportedModesPhy2", UanModesListValue(m1));
256 
257  // No collision (Get 2 packets)
259  34,
260  "Expected no collision");
261 
263  DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 0, 0),
264  0,
265  "Expected collision with both packets lost");
266 
268  DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 0, 2),
269  17,
270  "Expected collision with only one packets lost");
271 
273  DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 0, 5),
274  34,
275  "Expected no collision");
276 
278  DoOnePhyTest(Seconds(1.0), Seconds(2.99), 50, 50, prop, 2, 3),
279  34,
280  "Expected no collision");
281 
282  return false;
283 }
284 
285 void
287 {
288  Ptr<UanPhyPerUmodem> per = CreateObject<UanPhyPerUmodem>();
289  Ptr<Packet> pkt = Create<Packet>(1000);
290  double error = per->CalcPer(pkt, 9, UanPhyGen::GetDefaultModes()[0]);
291  NS_TEST_ASSERT_MSG_EQ_TOL(error, 0.539, 0.001, "Got PER outside of tolerance");
292 
293 #ifdef UAN_PROP_BH_INSTALLED
294  // Correct DB lookup for BH prop.
295  Ptr<UanPropModelBh> propBh = CreateObject<UanPropModelBh>();
296  BellhopResp resp = propBh->GetResp(10000, 50, 50, 1000);
297 
298  NS_TEST_ASSERT_MSG_EQ_TOL(resp.GetPathLossDb(),
299  -44.1753,
300  0.001,
301  "Got BH Pathloss outside of tolerance");
302 
303  NS_TEST_ASSERT_MSG_EQ_TOL(resp.GetPdp().GetTap(4).GetAmp(),
304  0.14159,
305  0.001,
306  "Got BH arrival outside of tolerance");
307 
308 #endif // UAN_PROP_BH_INSTALLED
309 
310  DoPhyTests();
311 }
312 
319 class UanTestSuite : public TestSuite
320 {
321  public:
322  UanTestSuite();
323 };
324 
326  : TestSuite("devices-uan", UNIT)
327 {
328  AddTestCase(new UanTest, TestCase::QUICK);
329 }
330 
Uan Test.
Definition: uan-test.cc:43
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Receive packet function.
Definition: uan-test.cc:105
Ptr< UanNetDevice > CreateNode(Vector pos, Ptr< UanChannel > chan)
Create node function.
Definition: uan-test.cc:123
void SendOnePacket(Ptr< UanNetDevice > dev, uint16_t mode)
Send one packet function.
Definition: uan-test.cc:115
uint32_t DoOnePhyTest(Time t1, Time t2, uint32_t r1, uint32_t r2, Ptr< UanPropModel > prop, uint16_t mode1=0, uint16_t mode2=0)
Do one Phy test function.
Definition: uan-test.cc:147
uint32_t m_bytesRx
byes received
Definition: uan-test.cc:96
ObjectFactory m_phyFac
Phy.
Definition: uan-test.cc:95
UanTest()
Definition: uan-test.cc:99
void DoRun() override
Implementation to actually run this TestCase.
Definition: uan-test.cc:286
bool DoPhyTests()
Phy test function.
Definition: uan-test.cc:176
Uan Test Suite.
Definition: uan-test.cc:320
a polymophic address class
Definition: address.h:101
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Container for UanTxModes.
Definition: uan-tx-mode.h:259
void AppendMode(UanTxMode mode)
Add mode to this list.
Definition: uan-tx-mode.cc:230
Base class for UAN Phy models.
Definition: uan-phy.h:178
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:43
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
#define NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:199
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:337
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
Send one packet.
const double m1
First component modulus, 232 - 209.
Definition: rng-stream.cc:60
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
channel
Definition: third.py:88
mac
Definition: third.py:92
mobility
Definition: third.py:105
phy
Definition: third.py:89
static UanTestSuite g_uanTestSuite
the test suite
Definition: uan-test.cc:331