A Discrete-Event Network Simulator
API
uan-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
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: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #include "ns3/uan-net-device.h"
22 #include "ns3/uan-channel.h"
23 #include "ns3/uan-mac-aloha.h"
24 #include "ns3/uan-phy-gen.h"
25 #include "ns3/uan-transducer-hd.h"
26 #include "ns3/uan-prop-model-ideal.h"
27 #include "ns3/constant-position-mobility-model.h"
28 #include "ns3/simulator.h"
29 #include "ns3/test.h"
30 #include "ns3/node.h"
31 #include "ns3/object-factory.h"
32 #include "ns3/pointer.h"
33 #include "ns3/callback.h"
34 
35 using namespace ns3;
36 
43 class UanTest : public TestCase
44 {
45 public:
46  UanTest ();
47 
48  virtual void DoRun (void);
49 private:
56  Ptr<UanNetDevice> CreateNode (Vector pos, Ptr<UanChannel> chan);
61  bool DoPhyTests ();
73  uint32_t DoOnePhyTest (Time t1, Time t2, uint32_t r1, uint32_t r2, Ptr<UanPropModel> prop, uint32_t mode1 = 0, uint32_t mode2 = 0);
82  bool RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender);
88  void SendOnePacket (Ptr<UanNetDevice> dev, uint32_t mode);
90  uint32_t m_bytesRx;
91 
92 };
93 
94 
96 {
97 
98 }
99 
100 bool
101 UanTest::RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender)
102 {
103  m_bytesRx += pkt->GetSize ();
104  return true;
105 }
106 void
108 {
109  Ptr<Packet> pkt = Create<Packet> (17);
110  dev->Send (pkt, dev->GetBroadcast (), mode);
111 
112 }
115 {
116 
117 
119  Ptr<Node> node = CreateObject<Node> ();
120  Ptr<UanNetDevice> dev = CreateObject<UanNetDevice> ();
121  Ptr<UanMacAloha> mac = CreateObject<UanMacAloha> ();
122  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
123 
124  Ptr<UanTransducerHd> trans = CreateObject<UanTransducerHd> ();
125 
126  mobility->SetPosition (pos);
127  node->AggregateObject (mobility);
128  mac->SetAddress (UanAddress::Allocate ());
129 
130  dev->SetPhy (phy);
131  dev->SetMac (mac);
132  dev->SetChannel (chan);
133  dev->SetTransducer (trans);
134  node->AddDevice (dev);
135 
136  return dev;
137 }
138 
139 
140 uint32_t
142  Time txTime2,
143  uint32_t r1,
144  uint32_t r2,
145  Ptr<UanPropModel> prop,
146  uint32_t mode1,
147  uint32_t mode2)
148 {
149 
150  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
151  channel->SetAttribute ("PropagationModel", PointerValue (prop));
152 
153  Ptr<UanNetDevice> dev0 = CreateNode (Vector (r1,50,50), channel);
154  Ptr<UanNetDevice> dev1 = CreateNode (Vector (0,50,50), channel);
155  Ptr<UanNetDevice> dev2 = CreateNode (Vector (r1 + r2, 50, 50), channel);
156 
158 
159  Simulator::Schedule (txTime1, &UanTest::SendOnePacket, this, dev1, mode1);
160  Simulator::Schedule (txTime2, &UanTest::SendOnePacket, this, dev2, mode2);
161 
162  m_bytesRx = 0;
163  Simulator::Stop (Seconds (20.0));
164  Simulator::Run ();
165  Simulator::Destroy ();
166 
167  return m_bytesRx;
168 }
169 
170 
171 bool
173 {
174  // Phy Gen / Default PER / Default SINR
175  UanModesList mList;
176  UanTxMode mode = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode");
177  mList.AppendMode (UanTxMode (mode));
178  Ptr<UanPhyPerGenDefault> perDef = CreateObject<UanPhyPerGenDefault> ();
179  Ptr<UanPhyCalcSinrDefault> sinrDef = CreateObject<UanPhyCalcSinrDefault> ();
180  m_phyFac.SetTypeId ("ns3::UanPhyGen");
181  m_phyFac.Set ("PerModel", PointerValue (perDef));
182  m_phyFac.Set ("SinrModel", PointerValue (sinrDef));
183  m_phyFac.Set ("SupportedModes", UanModesListValue (mList));
184 
185  Ptr<UanPropModelIdeal> prop = CreateObject<UanPropModelIdeal> ();
186 
187  // No collision (Get 2 packets)
188  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.001), 50, 50, prop),
189  34, "Should have received 34 bytes from 2 disjoint packets");
190 
191  // Collision (Lose both packets)
192  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop),
193  0, "Expected collision resulting in loss of both packets");
194 
195 
196  // Phy Gen / FH-FSK SINR check
197 
198  Ptr<UanPhyCalcSinrFhFsk> sinrFhfsk = CreateObject <UanPhyCalcSinrFhFsk> ();
199  m_phyFac.Set ("PerModel", PointerValue (perDef));
200  m_phyFac.Set ("SinrModel", PointerValue (sinrFhfsk));
201  m_phyFac.Set ("SupportedModes", UanModesListValue (mList));
202 
203 #ifdef UAN_PROP_BH_INSTALLED
204  Ptr<UanPropModelBh> prop = CreateObject<UanPropModelBh> ();
205 #endif // UAN_PROP_BH_INSTALLED
206 
207  // No collision (Get 2 packets)
208  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.001), 50, 50, prop),
209  34, "Should have received 34 bytes from 2 disjoint packets");
210 
211  // Should correctly receive first arriving packet
212  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (1.0126), 50, 50, prop),
213  17, "Should have recieved 17 bytes from first arriving packet");
214 
215  // Packets should collide and both be lost
216  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (1.0 + 7.01 * (13.0 / 80.0)), 50, 50, prop),
217  0, "Packets should collide, but received data");
218 
219  // Phy Dual
220  UanTxMode mode00 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode00");
221  UanTxMode mode10 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode10");
222  UanTxMode mode20 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode20");
223  UanTxMode mode01 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode01");
224  UanTxMode mode11 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode11");
225  UanTxMode mode21 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode21");
226 
227  UanModesList m0;
228  m0.AppendMode (mode00);
229  m0.AppendMode (mode10);
230  m0.AppendMode (mode20);
232  m1.AppendMode (mode01);
233  m1.AppendMode (mode11);
234  m1.AppendMode (mode21);
235 
236  m_phyFac = ObjectFactory ();
237  m_phyFac.SetTypeId ("ns3::UanPhyDual");
238  m_phyFac.Set ("SupportedModesPhy1", UanModesListValue (m0));
239  m_phyFac.Set ("SupportedModesPhy2", UanModesListValue (m1));
240 
241  // No collision (Get 2 packets)
242  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.01), 50, 50, prop),
243  34, "Expected no collision");
244 
245  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 0),
246  0, "Expected collision with both packets lost");
247 
248  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 2),
249  17, "Expected collision with only one packets lost");
250 
251  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 5),
252  34, "Expected no collision");
253 
254  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 2, 3),
255  34, "Expected no collision");
256 
257  return false;
258 }
259 
260 void
262 {
263 
264  Ptr<UanPhyPerUmodem> per = CreateObject<UanPhyPerUmodem> ();
265  Ptr<Packet> pkt = Create<Packet> (1000);
266  double error = per->CalcPer (pkt, 9, UanPhyGen::GetDefaultModes ()[0]);
267  NS_TEST_ASSERT_MSG_EQ_TOL (error, 0.539, 0.001, "Got PER outside of tolerance");
268 
269 #ifdef UAN_PROP_BH_INSTALLED
270  // Correct DB lookup for BH prop.
271  Ptr<UanPropModelBh> propBh = CreateObject<UanPropModelBh> ();
272  BellhopResp resp = propBh->GetResp (10000, 50, 50, 1000);
273 
274  NS_TEST_ASSERT_MSG_EQ_TOL (resp.GetPathLossDb (), -44.1753, 0.001, "Got BH Pathloss outside of tolerance");
275 
276  NS_TEST_ASSERT_MSG_EQ_TOL (resp.GetPdp ().GetTap (4).GetAmp (), 0.14159, 0.001, "Got BH arrival outside of tolerance");
277 
278 #endif // UAN_PROP_BH_INSTALLED
279 
280  DoPhyTests ();
281 }
282 
289 class UanTestSuite : public TestSuite
290 {
291 public:
292  UanTestSuite ();
293 };
294 
296  : TestSuite ("devices-uan", UNIT)
297 {
298  AddTestCase (new UanTest, TestCase::QUICK);
299 }
300 
302 
uint32_t m_bytesRx
byes received
Definition: uan-test.cc:90
tuple channel
Definition: third.py:85
Uan Test.
Definition: uan-test.cc:43
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
A suite of tests to run.
Definition: test.h:1342
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:796
Container for UanTxModes.
Definition: uan-tx-mode.h:257
const double m1
First component modulus, 232 - 209.
Definition: rng-stream.cc:57
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
encapsulates test code
Definition: test.h:1155
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition: uan-test.cc:261
virtual Address GetBroadcast(void) const
a polymophic address class
Definition: address.h:90
#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:227
tuple mobility
Definition: third.py:101
tuple phy
Definition: third.py:86
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Vector3D Vector
Definition: vector.h:217
void SendOnePacket(Ptr< UanNetDevice > dev, uint32_t mode)
Send one packet function.
Definition: uan-test.cc:107
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:41
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Ptr< UanNetDevice > CreateNode(Vector pos, Ptr< UanChannel > chan)
Create node function.
Definition: uan-test.cc:114
tuple mac
Definition: third.py:92
#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:380
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Receive packet function.
Definition: uan-test.cc:101
ObjectFactory m_phyFac
Phy.
Definition: uan-test.cc:89
uint32_t DoOnePhyTest(Time t1, Time t2, uint32_t r1, uint32_t r2, Ptr< UanPropModel > prop, uint32_t mode1=0, uint32_t mode2=0)
Do one Phy test function.
Definition: uan-test.cc:141
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
static UanTestSuite g_uanTestSuite
the test suite
Definition: uan-test.cc:301
virtual double CalcPer(Ptr< Packet > pkt, double sinrDb, UanTxMode mode)
Calculate the packet error probability based on SINR at the receiver and a tx mode.
Definition: uan-phy-gen.cc:428
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
Base class for UAN Phy models.
Definition: uan-phy.h:175
Instantiate subclasses of ns3::Object.
bool DoPhyTests()
Phy test function.
Definition: uan-test.cc:172
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void AppendMode(UanTxMode mode)
Add mode to this list.
Definition: uan-tx-mode.cc:232
UanTest()
Definition: uan-test.cc:95
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
Uan Test Suite.
Definition: uan-test.cc:289
AttributeValue implementation for UanModesList.
Definition: uan-tx-mode.h:314