A Discrete-Event Network Simulator
API
lr-wpan-collision-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include <ns3/test.h>
22 #include <ns3/packet.h>
23 #include <ns3/lr-wpan-module.h>
24 #include <ns3/mobility-module.h>
25 #include <ns3/propagation-module.h>
26 #include <ns3/spectrum-module.h>
27 #include <ns3/mac16-address.h>
28 #include <ns3/mac64-address.h>
29 #include <ns3/log.h>
30 
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("lr-wpan-collision-test");
35 
36 // This is an example TestCase.
38 {
39 public:
41  virtual ~LrWpanCollisionTestCase ();
42 
44 private:
45  virtual void DoRun (void);
46 
47  uint8_t m_rxPackets;
48 };
49 
51  : TestCase ("Test the 802.15.4 collision handling")
52 {
53  m_rxPackets = 0;
54 }
55 
57 {
58 }
59 
61 {
62  m_rxPackets++;
63 }
64 
65 
66 void
68 {
69 
70  // Create 3 nodes, and a NetDevice for each one
71  Ptr<Node> n0 = CreateObject <Node> ();
72  Ptr<Node> n1 = CreateObject <Node> ();
73  Ptr<Node> n2 = CreateObject <Node> ();
74 
75  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
76  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
77  Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice> ();
78 
79  dev0->SetAddress (Mac16Address ("00:01"));
80  dev1->SetAddress (Mac16Address ("00:02"));
81  dev2->SetAddress (Mac16Address ("00:03"));
82 
83  // Each device must be attached to the same channel
84  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
85  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
86  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
87  channel->AddPropagationLossModel (propModel);
88  channel->SetPropagationDelayModel (delayModel);
89 
90  dev0->SetChannel (channel);
91  dev1->SetChannel (channel);
92  dev2->SetChannel (channel);
93 
94  // To complete configuration, a LrWpanNetDevice must be added to a node
95  n0->AddDevice (dev0);
96  n1->AddDevice (dev1);
97  n2->AddDevice (dev2);
98 
99  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
100  sender0Mobility->SetPosition (Vector (0,0,0));
101  dev0->GetPhy ()->SetMobility (sender0Mobility);
102  n0->AggregateObject (sender0Mobility);
103 
104  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
105  // Configure position 10 m distance
106  sender1Mobility->SetPosition (Vector (0,1,0));
107  dev1->GetPhy ()->SetMobility (sender1Mobility);
108  n1->AggregateObject (sender1Mobility);
109 
110  Ptr<ConstantPositionMobilityModel> sender2Mobility = CreateObject<ConstantPositionMobilityModel> ();
111  // Configure position 10 m distance
112  sender2Mobility->SetPosition (Vector (30,0,0));
113  dev2->GetPhy ()->SetMobility (sender2Mobility);
114  n2->AggregateObject (sender2Mobility);
115 
116  dev0->GetMac ()->SetMcpsDataIndicationCallback (MakeCallback (&LrWpanCollisionTestCase::DataIndication, this));
117 
118  // Disable first backoff
119  dev0->GetCsmaCa ()->SetMacMinBE (0);
120  dev1->GetCsmaCa ()->SetMacMinBE (0);
121  dev2->GetCsmaCa ()->SetMacMinBE (0);
122 
123  Ptr<Packet> p0 = Create<Packet> (20);
124  Ptr<Packet> p1 = Create<Packet> (60);
125  Ptr<Packet> p2 = Create<Packet> (100);
126 
127  McpsDataRequestParams params;
128  params.m_srcAddrMode = SHORT_ADDR;
129  params.m_dstAddrMode = SHORT_ADDR;
130  params.m_dstPanId = 0;
131  params.m_msduHandle = 0;
132  // params.m_txOptions = TX_OPTION_ACK;
133 
134  // First case: concurrent tx and no ACKs
135  std::cout << "*** First test " << std::endl;
136  m_rxPackets = 0;
137  params.m_dstAddr = Mac16Address ("00:02");
138  Simulator::Schedule (Seconds (0.1),
139  &LrWpanMac::McpsDataRequest,
140  dev0->GetMac (), params, p0);
141 
142  params.m_dstAddr = Mac16Address ("00:01");
143  Simulator::Schedule (Seconds (0.1),
144  &LrWpanMac::McpsDataRequest,
145  dev1->GetMac (), params, p1);
146 
147  Simulator::Run ();
148 
149  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 0, "Not received a packet (as expected)");
150 
151  // Second case: concurrent tx and ACKs
152  std::cout << "*** Second test " << std::endl;
153  m_rxPackets = 0;
154  params.m_txOptions = TX_OPTION_ACK;
155 
156  params.m_dstAddr = Mac16Address ("00:02");
157  Simulator::Schedule (Seconds (0.1),
158  &LrWpanMac::McpsDataRequest,
159  dev0->GetMac (), params, p0);
160 
161  params.m_dstAddr = Mac16Address ("00:01");
162  Simulator::Schedule (Seconds (0.1),
163  &LrWpanMac::McpsDataRequest,
164  dev1->GetMac (), params, p1);
165 
166  Simulator::Run ();
167 
168  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 1, "Received a packet (as expected)");
169 
170  // Third case: two concurrent tx and no ACKs
171  std::cout << "*** Third test " << std::endl;
172  m_rxPackets = 0;
173  params.m_txOptions = 0;
174 
175 // LogComponentEnable("LrWpanMac",LOG_LEVEL_ALL);
176 // LogComponentEnable("LrWpanPhy",LOG_LEVEL_ALL);
177 // LogComponentEnableAll (LOG_PREFIX_TIME);
178 
179  params.m_dstAddr = Mac16Address ("00:01");
180  Simulator::Schedule (Seconds (0.0001),
181  &LrWpanMac::McpsDataRequest,
182  dev2->GetMac (), params, p2);
183 
184  params.m_dstAddr = Mac16Address ("00:01");
185  Simulator::Schedule (Seconds (0.0002),
186  &LrWpanMac::McpsDataRequest,
187  dev1->GetMac (), params, p0);
188 
189  Simulator::Run ();
190 
191  std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
192  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 1, "Received a packet (as expected)");
193 
194  // Fourth case: two concurrent tx and ACKs
195  std::cout << "*** Fourth test " << std::endl;
196  m_rxPackets = 0;
197  params.m_txOptions = TX_OPTION_ACK;
198 
199  params.m_dstAddr = Mac16Address ("00:01");
200  Simulator::Schedule (Seconds (0.1),
201  &LrWpanMac::McpsDataRequest,
202  dev1->GetMac (), params, p0);
203 
204  params.m_dstAddr = Mac16Address ("00:01");
205  Simulator::Schedule (Seconds (0.1),
206  &LrWpanMac::McpsDataRequest,
207  dev2->GetMac (), params, p1);
208 
209  Simulator::Run ();
210 
211  std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
212  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 2, "Received two packets (as expected)");
213 
214  Simulator::Destroy ();
215 }
216 
217 // ==============================================================================
219 {
220 public:
222 };
223 
225  : TestSuite ("lr-wpan-collision", UNIT)
226 {
227  AddTestCase (new LrWpanCollisionTestCase, TestCase::QUICK);
228 }
229 
tuple channel
Definition: third.py:85
A suite of tests to run.
Definition: test.h:1333
virtual void DoRun(void)
Implementation to actually run this TestCase.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:278
encapsulates test code
Definition: test.h:1147
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Set the single-frequency propagation loss model to be used.
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-data.cc:42
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:57
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:157
Every class exported by the ns3 library is enclosed in the ns3 namespace.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
void SetPosition(const Vector &position)
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
virtual void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
Set the propagation delay model to be used.
static LrWpanCollisionTestSuite g_lrWpanCollisionTestSuite
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:181