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 
43 {
44 public:
46  virtual ~LrWpanCollisionTestCase ();
47 
54 private:
55  virtual void DoRun (void);
56 
57  uint8_t m_rxPackets;
58 };
59 
61  : TestCase ("Test the 802.15.4 collision handling")
62 {
63  m_rxPackets = 0;
64 }
65 
67 {
68 }
69 
71 {
72  m_rxPackets++;
73 }
74 
75 
76 void
78 {
79 
80  // Create 3 nodes, and a NetDevice for each one
81  Ptr<Node> n0 = CreateObject <Node> ();
82  Ptr<Node> n1 = CreateObject <Node> ();
83  Ptr<Node> n2 = CreateObject <Node> ();
84 
85  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
86  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
87  Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice> ();
88 
89  dev0->SetAddress (Mac16Address ("00:01"));
90  dev1->SetAddress (Mac16Address ("00:02"));
91  dev2->SetAddress (Mac16Address ("00:03"));
92 
93  // Each device must be attached to the same channel
94  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
95  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
96  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
97  channel->AddPropagationLossModel (propModel);
98  channel->SetPropagationDelayModel (delayModel);
99 
100  dev0->SetChannel (channel);
101  dev1->SetChannel (channel);
102  dev2->SetChannel (channel);
103 
104  // To complete configuration, a LrWpanNetDevice must be added to a node
105  n0->AddDevice (dev0);
106  n1->AddDevice (dev1);
107  n2->AddDevice (dev2);
108 
109  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
110  sender0Mobility->SetPosition (Vector (0,0,0));
111  dev0->GetPhy ()->SetMobility (sender0Mobility);
112  n0->AggregateObject (sender0Mobility);
113 
114  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
115  // Configure position 10 m distance
116  sender1Mobility->SetPosition (Vector (0,1,0));
117  dev1->GetPhy ()->SetMobility (sender1Mobility);
118  n1->AggregateObject (sender1Mobility);
119 
120  Ptr<ConstantPositionMobilityModel> sender2Mobility = CreateObject<ConstantPositionMobilityModel> ();
121  // Configure position 10 m distance
122  sender2Mobility->SetPosition (Vector (30,0,0));
123  dev2->GetPhy ()->SetMobility (sender2Mobility);
124  n2->AggregateObject (sender2Mobility);
125 
126  dev0->GetMac ()->SetMcpsDataIndicationCallback (MakeCallback (&LrWpanCollisionTestCase::DataIndication, this));
127 
128  // Disable first backoff
129  dev0->GetCsmaCa ()->SetMacMinBE (0);
130  dev1->GetCsmaCa ()->SetMacMinBE (0);
131  dev2->GetCsmaCa ()->SetMacMinBE (0);
132 
133  Ptr<Packet> p0 = Create<Packet> (20);
134  Ptr<Packet> p1 = Create<Packet> (60);
135  Ptr<Packet> p2 = Create<Packet> (100);
136 
137  McpsDataRequestParams params;
138  params.m_srcAddrMode = SHORT_ADDR;
139  params.m_dstAddrMode = SHORT_ADDR;
140  params.m_dstPanId = 0;
141  params.m_msduHandle = 0;
142  // params.m_txOptions = TX_OPTION_ACK;
143 
144  // First case: concurrent tx and no ACKs
145  std::cout << "*** First test " << std::endl;
146  m_rxPackets = 0;
147  params.m_dstAddr = Mac16Address ("00:02");
148  Simulator::Schedule (Seconds (0.1),
149  &LrWpanMac::McpsDataRequest,
150  dev0->GetMac (), params, p0);
151 
152  params.m_dstAddr = Mac16Address ("00:01");
153  Simulator::Schedule (Seconds (0.1),
154  &LrWpanMac::McpsDataRequest,
155  dev1->GetMac (), params, p1);
156 
157  Simulator::Run ();
158 
159  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 0, "Not received a packet (as expected)");
160 
161  // Second case: concurrent tx and ACKs
162  std::cout << "*** Second test " << std::endl;
163  m_rxPackets = 0;
164  params.m_txOptions = TX_OPTION_ACK;
165 
166  params.m_dstAddr = Mac16Address ("00:02");
167  Simulator::Schedule (Seconds (0.1),
168  &LrWpanMac::McpsDataRequest,
169  dev0->GetMac (), params, p0);
170 
171  params.m_dstAddr = Mac16Address ("00:01");
172  Simulator::Schedule (Seconds (0.1),
173  &LrWpanMac::McpsDataRequest,
174  dev1->GetMac (), params, p1);
175 
176  Simulator::Run ();
177 
178  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 1, "Received a packet (as expected)");
179 
180  // Third case: two concurrent tx and no ACKs
181  std::cout << "*** Third test " << std::endl;
182  m_rxPackets = 0;
183  params.m_txOptions = 0;
184 
185 // LogComponentEnable("LrWpanMac",LOG_LEVEL_ALL);
186 // LogComponentEnable("LrWpanPhy",LOG_LEVEL_ALL);
187 // LogComponentEnableAll (LOG_PREFIX_TIME);
188 
189  params.m_dstAddr = Mac16Address ("00:01");
190  Simulator::Schedule (Seconds (0.0001),
191  &LrWpanMac::McpsDataRequest,
192  dev2->GetMac (), params, p2);
193 
194  params.m_dstAddr = Mac16Address ("00:01");
195  Simulator::Schedule (Seconds (0.0002),
196  &LrWpanMac::McpsDataRequest,
197  dev1->GetMac (), params, p0);
198 
199  Simulator::Run ();
200 
201  std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
202  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 1, "Received a packet (as expected)");
203 
204  // Fourth case: two concurrent tx and ACKs
205  std::cout << "*** Fourth test " << std::endl;
206  m_rxPackets = 0;
207  params.m_txOptions = TX_OPTION_ACK;
208 
209  params.m_dstAddr = Mac16Address ("00:01");
210  Simulator::Schedule (Seconds (0.1),
211  &LrWpanMac::McpsDataRequest,
212  dev1->GetMac (), params, p0);
213 
214  params.m_dstAddr = Mac16Address ("00:01");
215  Simulator::Schedule (Seconds (0.1),
216  &LrWpanMac::McpsDataRequest,
217  dev2->GetMac (), params, p1);
218 
219  Simulator::Run ();
220 
221  std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
222  NS_TEST_EXPECT_MSG_EQ (m_rxPackets, 2, "Received two packets (as expected)");
223 
224  Simulator::Destroy ();
225 }
226 
234 {
235 public:
237 };
238 
240  : TestSuite ("lr-wpan-collision", UNIT)
241 {
242  AddTestCase (new LrWpanCollisionTestCase, TestCase::QUICK);
243 }
244 
A suite of tests to run.
Definition: test.h:1342
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:202
uint8_t m_rxPackets
Rx packets counter.
#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:285
encapsulates test code
Definition: test.h:1155
LrWpan Collision TestSuite.
channel
Definition: third.py:85
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, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
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:1014
static LrWpanCollisionTestSuite g_lrWpanCollisionTestSuite
Static variable for test initialization.
LrWpan Collision Test.
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit.
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:182