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, uint16_t mode1 = 0, uint16_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, uint16_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  NS_UNUSED (mode);
104  NS_UNUSED (sender);
105  m_bytesRx += pkt->GetSize ();
106  return true;
107 }
108 void
110 {
111  Ptr<Packet> pkt = Create<Packet> (17);
112  dev->SetTxModeIndex (mode);
113  dev->Send (pkt, dev->GetBroadcast (), 0);
114 
115 }
118 {
119 
120 
122  Ptr<Node> node = CreateObject<Node> ();
123  Ptr<UanNetDevice> dev = CreateObject<UanNetDevice> ();
124  Ptr<UanMacAloha> mac = CreateObject<UanMacAloha> ();
125  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
126 
127  Ptr<UanTransducerHd> trans = CreateObject<UanTransducerHd> ();
128 
129  mobility->SetPosition (pos);
130  node->AggregateObject (mobility);
131  mac->SetAddress (Mac8Address::Allocate ());
132 
133  dev->SetPhy (phy);
134  dev->SetMac (mac);
135  dev->SetChannel (chan);
136  dev->SetTransducer (trans);
137  node->AddDevice (dev);
138 
139  return dev;
140 }
141 
142 
143 uint32_t
145  Time txTime2,
146  uint32_t r1,
147  uint32_t r2,
148  Ptr<UanPropModel> prop,
149  uint16_t mode1,
150  uint16_t mode2)
151 {
152 
153  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
154  channel->SetAttribute ("PropagationModel", PointerValue (prop));
155 
156  Ptr<UanNetDevice> dev0 = CreateNode (Vector (r1,50,50), channel);
157  Ptr<UanNetDevice> dev1 = CreateNode (Vector (0,50,50), channel);
158  Ptr<UanNetDevice> dev2 = CreateNode (Vector (r1 + r2, 50, 50), channel);
159 
161 
162  Simulator::Schedule (txTime1, &UanTest::SendOnePacket, this, dev1, mode1);
163  Simulator::Schedule (txTime2, &UanTest::SendOnePacket, this, dev2, mode2);
164 
165  m_bytesRx = 0;
166  Simulator::Stop (Seconds (20.0));
167  Simulator::Run ();
168  Simulator::Destroy ();
169 
170  return m_bytesRx;
171 }
172 
173 
174 bool
176 {
177  // Phy Gen / Default PER / Default SINR
178  UanModesList mList;
179  UanTxMode mode = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode");
180  mList.AppendMode (UanTxMode (mode));
181  Ptr<UanPhyPerGenDefault> perDef = CreateObject<UanPhyPerGenDefault> ();
182  Ptr<UanPhyCalcSinrDefault> sinrDef = CreateObject<UanPhyCalcSinrDefault> ();
183  m_phyFac.SetTypeId ("ns3::UanPhyGen");
184  m_phyFac.Set ("PerModel", PointerValue (perDef));
185  m_phyFac.Set ("SinrModel", PointerValue (sinrDef));
186  m_phyFac.Set ("SupportedModes", UanModesListValue (mList));
187 
188  Ptr<UanPropModelIdeal> prop = CreateObject<UanPropModelIdeal> ();
189 
190  // No collision (Get 2 packets)
191  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.001), 50, 50, prop),
192  34, "Should have received 34 bytes from 2 disjoint packets");
193 
194  // Collision (Lose both packets)
195  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop),
196  0, "Expected collision resulting in loss of both packets");
197 
198 
199  // Phy Gen / FH-FSK SINR check
200 
201  Ptr<UanPhyCalcSinrFhFsk> sinrFhfsk = CreateObject <UanPhyCalcSinrFhFsk> ();
202  m_phyFac.Set ("PerModel", PointerValue (perDef));
203  m_phyFac.Set ("SinrModel", PointerValue (sinrFhfsk));
204  m_phyFac.Set ("SupportedModes", UanModesListValue (mList));
205 
206 #ifdef UAN_PROP_BH_INSTALLED
207  Ptr<UanPropModelBh> prop = CreateObject<UanPropModelBh> ();
208 #endif // UAN_PROP_BH_INSTALLED
209 
210  // No collision (Get 2 packets)
211  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.001), 50, 50, prop),
212  34, "Should have received 34 bytes from 2 disjoint packets");
213 
214  // Should correctly receive first arriving packet
215  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (1.0126), 50, 50, prop),
216  17, "Should have received 17 bytes from first arriving packet");
217 
218  // Packets should collide and both be lost
219  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (1.0 + 7.01 * (13.0 / 80.0)), 50, 50, prop),
220  0, "Packets should collide, but received data");
221 
222  // Phy Dual
223  UanTxMode mode00 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode00");
224  UanTxMode mode10 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode10");
225  UanTxMode mode20 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode20");
226  UanTxMode mode01 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 10000, 4000, 2, "TestMode01");
227  UanTxMode mode11 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 11000, 4000, 2, "TestMode11");
228  UanTxMode mode21 = UanTxModeFactory::CreateMode (UanTxMode::FSK, 80, 80, 15000, 4000, 2, "TestMode21");
229 
230  UanModesList m0;
231  m0.AppendMode (mode00);
232  m0.AppendMode (mode10);
233  m0.AppendMode (mode20);
235  m1.AppendMode (mode01);
236  m1.AppendMode (mode11);
237  m1.AppendMode (mode21);
238 
239  m_phyFac = ObjectFactory ();
240  m_phyFac.SetTypeId ("ns3::UanPhyDual");
241  m_phyFac.Set ("SupportedModesPhy1", UanModesListValue (m0));
242  m_phyFac.Set ("SupportedModesPhy2", UanModesListValue (m1));
243 
244  // No collision (Get 2 packets)
245  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (3.01), 50, 50, prop),
246  34, "Expected no collision");
247 
248  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 0),
249  0, "Expected collision with both packets lost");
250 
251  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 2),
252  17, "Expected collision with only one packets lost");
253 
254  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 0, 5),
255  34, "Expected no collision");
256 
257  NS_TEST_ASSERT_MSG_EQ_RETURNS_BOOL (DoOnePhyTest (Seconds (1.0), Seconds (2.99), 50, 50, prop, 2, 3),
258  34, "Expected no collision");
259 
260  return false;
261 }
262 
263 void
265 {
266 
267  Ptr<UanPhyPerUmodem> per = CreateObject<UanPhyPerUmodem> ();
268  Ptr<Packet> pkt = Create<Packet> (1000);
269  double error = per->CalcPer (pkt, 9, UanPhyGen::GetDefaultModes ()[0]);
270  NS_TEST_ASSERT_MSG_EQ_TOL (error, 0.539, 0.001, "Got PER outside of tolerance");
271 
272 #ifdef UAN_PROP_BH_INSTALLED
273  // Correct DB lookup for BH prop.
274  Ptr<UanPropModelBh> propBh = CreateObject<UanPropModelBh> ();
275  BellhopResp resp = propBh->GetResp (10000, 50, 50, 1000);
276 
277  NS_TEST_ASSERT_MSG_EQ_TOL (resp.GetPathLossDb (), -44.1753, 0.001, "Got BH Pathloss outside of tolerance");
278 
279  NS_TEST_ASSERT_MSG_EQ_TOL (resp.GetPdp ().GetTap (4).GetAmp (), 0.14159, 0.001, "Got BH arrival outside of tolerance");
280 
281 #endif // UAN_PROP_BH_INSTALLED
282 
283  DoPhyTests ();
284 }
285 
292 class UanTestSuite : public TestSuite
293 {
294 public:
295  UanTestSuite ();
296 };
297 
299  : TestSuite ("devices-uan", UNIT)
300 {
301  AddTestCase (new UanTest, TestCase::QUICK);
302 }
303 
305 
uint32_t m_bytesRx
byes received
Definition: uan-test.cc:90
void SendOnePacket(Ptr< UanNetDevice > dev, uint16_t mode)
Send one packet function.
Definition: uan-test.cc:109
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
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
A suite of tests to run.
Definition: test.h:1342
void SetTxModeIndex(uint32_t txModeIndex)
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
#define NS_UNUSED(x)
Mark a local variable as unused.
Definition: unused.h:36
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:264
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:92
mobility
Definition: third.py:108
phy
Definition: third.py:93
#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
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Vector3D Vector
Definition: vector.h:217
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:41
mac
Definition: third.py:99
virtual Address GetBroadcast(void) const
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:117
#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
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:144
ObjectFactory m_phyFac
Phy.
Definition: uan-test.cc:89
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
static UanTestSuite g_uanTestSuite
the test suite
Definition: uan-test.cc:304
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:429
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:175
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
void AppendMode(UanTxMode mode)
Add mode to this list.
Definition: uan-tx-mode.cc:232
UanTest()
Definition: uan-test.cc:95
Uan Test Suite.
Definition: uan-test.cc:292
AttributeValue implementation for UanModesList.
Definition: uan-tx-mode.h:314