A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
lr-wpan-collision-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #include <ns3/log.h>
21 #include <ns3/lr-wpan-module.h>
22 #include <ns3/mac16-address.h>
23 #include <ns3/mac64-address.h>
24 #include <ns3/mobility-module.h>
25 #include <ns3/packet.h>
26 #include <ns3/propagation-module.h>
27 #include <ns3/spectrum-module.h>
28 #include <ns3/test.h>
29 
30 using namespace ns3;
31 
32 NS_LOG_COMPONENT_DEFINE("lr-wpan-collision-test");
33 
41 {
42  public:
44  ~LrWpanCollisionTestCase() override;
45 
52 
53  private:
54  void DoRun() override;
55 
56  uint8_t m_rxPackets;
57 };
58 
60  : TestCase("Test the 802.15.4 collision handling")
61 {
62  m_rxPackets = 0;
63 }
64 
66 {
67 }
68 
69 void
71 {
72  m_rxPackets++;
73 }
74 
75 void
77 {
78  // Create 3 nodes, and a NetDevice for each one
79  Ptr<Node> n0 = CreateObject<Node>();
80  Ptr<Node> n1 = CreateObject<Node>();
81  Ptr<Node> n2 = CreateObject<Node>();
82 
83  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
84  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
85  Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice>();
86 
87  dev0->SetAddress(Mac16Address("00:01"));
88  dev1->SetAddress(Mac16Address("00:02"));
89  dev2->SetAddress(Mac16Address("00:03"));
90 
91  // Each device must be attached to the same channel
92  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
94  CreateObject<LogDistancePropagationLossModel>();
96  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 =
110  CreateObject<ConstantPositionMobilityModel>();
111  sender0Mobility->SetPosition(Vector(0, 0, 0));
112  dev0->GetPhy()->SetMobility(sender0Mobility);
113  n0->AggregateObject(sender0Mobility);
114 
115  Ptr<ConstantPositionMobilityModel> sender1Mobility =
116  CreateObject<ConstantPositionMobilityModel>();
117  // Configure position 10 m distance
118  sender1Mobility->SetPosition(Vector(0, 1, 0));
119  dev1->GetPhy()->SetMobility(sender1Mobility);
120  n1->AggregateObject(sender1Mobility);
121 
122  Ptr<ConstantPositionMobilityModel> sender2Mobility =
123  CreateObject<ConstantPositionMobilityModel>();
124  // Configure position 10 m distance
125  sender2Mobility->SetPosition(Vector(30, 0, 0));
126  dev2->GetPhy()->SetMobility(sender2Mobility);
127  n2->AggregateObject(sender2Mobility);
128 
129  dev0->GetMac()->SetMcpsDataIndicationCallback(
131 
132  // Disable first backoff
133  dev0->GetCsmaCa()->SetMacMinBE(0);
134  dev1->GetCsmaCa()->SetMacMinBE(0);
135  dev2->GetCsmaCa()->SetMacMinBE(0);
136 
137  Ptr<Packet> p0 = Create<Packet>(20);
138  Ptr<Packet> p1 = Create<Packet>(60);
139  Ptr<Packet> p2 = Create<Packet>(100);
140 
142  params.m_srcAddrMode = SHORT_ADDR;
143  params.m_dstAddrMode = SHORT_ADDR;
144  params.m_dstPanId = 0;
145  params.m_msduHandle = 0;
146  // params.m_txOptions = TX_OPTION_ACK;
147 
148  // First case: concurrent tx and no ACKs
149  std::cout << "*** First test " << std::endl;
150  m_rxPackets = 0;
151  params.m_dstAddr = Mac16Address("00:02");
152  Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
153 
154  params.m_dstAddr = Mac16Address("00:01");
155  Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, 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), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
168 
169  params.m_dstAddr = Mac16Address("00:01");
170  Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p1);
171 
172  Simulator::Run();
173 
174  NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 1, "Received a packet (as expected)");
175 
176  // Third case: two concurrent tx and no ACKs
177  std::cout << "*** Third test " << std::endl;
178  m_rxPackets = 0;
179  params.m_txOptions = 0;
180 
181  // LogComponentEnable("LrWpanMac",LOG_LEVEL_ALL);
182  // LogComponentEnable("LrWpanPhy",LOG_LEVEL_ALL);
183  // LogComponentEnableAll (LOG_PREFIX_TIME);
184 
185  params.m_dstAddr = Mac16Address("00:01");
186  Simulator::Schedule(Seconds(0.0001), &LrWpanMac::McpsDataRequest, dev2->GetMac(), params, p2);
187 
188  params.m_dstAddr = Mac16Address("00:01");
189  Simulator::Schedule(Seconds(0.0002), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p0);
190 
191  Simulator::Run();
192 
193  std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
194  NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 1, "Received a packet (as expected)");
195 
196  // Fourth case: two concurrent tx and ACKs
197  std::cout << "*** Fourth test " << std::endl;
198  m_rxPackets = 0;
199  params.m_txOptions = TX_OPTION_ACK;
200 
201  params.m_dstAddr = Mac16Address("00:01");
202  Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p0);
203 
204  params.m_dstAddr = Mac16Address("00:01");
205  Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev2->GetMac(), params, p1);
206 
207  Simulator::Run();
208 
209  std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
210  NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 2, "Received two packets (as expected)");
211 
212  Simulator::Destroy();
213 }
214 
222 {
223  public:
225 };
226 
228  : TestSuite("lr-wpan-collision", UNIT)
229 {
230  AddTestCase(new LrWpanCollisionTestCase, TestCase::QUICK);
231 }
232 
LrWpan Collision Test.
void DoRun() override
Implementation to actually run this TestCase.
uint8_t m_rxPackets
Rx packets counter.
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit.
LrWpan Collision TestSuite.
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
Ptr< LrWpanCsmaCa > GetCsmaCa() const
Get the CSMA/CA implementation used by this NetDevice.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:61
@ SHORT_ADDR
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static LrWpanCollisionTestSuite g_lrWpanCollisionTestSuite
Static variable for test initialization.
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:56
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
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
MCPS-DATA.indication params.
MCPS-DATA.request params.