A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
30using namespace ns3;
31using namespace ns3::lrwpan;
32
33NS_LOG_COMPONENT_DEFINE("lr-wpan-collision-test");
34
35/**
36 * \ingroup lr-wpan-test
37 * \ingroup tests
38 *
39 * \brief LrWpan Collision Test
40 */
42{
43 public:
45 ~LrWpanCollisionTestCase() override;
46
47 /**
48 * \brief Function called when DataIndication is hit.
49 * \param params The MCPS params.
50 * \param p The packet.
51 */
53
54 private:
55 void DoRun() override;
56
57 uint8_t m_rxPackets; //!< Rx packets counter.
58};
59
61 : TestCase("Test the 802.15.4 collision handling")
62{
63 m_rxPackets = 0;
64}
65
67{
68}
69
70void
72{
74}
75
76void
78{
79 // Create 3 nodes, and a NetDevice for each one
80 Ptr<Node> n0 = CreateObject<Node>();
81 Ptr<Node> n1 = CreateObject<Node>();
82 Ptr<Node> n2 = CreateObject<Node>();
83
84 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
85 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
86 Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice>();
87
88 dev0->SetAddress(Mac16Address("00:01"));
89 dev1->SetAddress(Mac16Address("00:02"));
90 dev2->SetAddress(Mac16Address("00:03"));
91
92 // Each device must be attached to the same channel
93 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
95 CreateObject<LogDistancePropagationLossModel>();
97 CreateObject<ConstantSpeedPropagationDelayModel>();
98 channel->AddPropagationLossModel(propModel);
99 channel->SetPropagationDelayModel(delayModel);
100
101 dev0->SetChannel(channel);
102 dev1->SetChannel(channel);
103 dev2->SetChannel(channel);
104
105 // To complete configuration, a LrWpanNetDevice must be added to a node
106 n0->AddDevice(dev0);
107 n1->AddDevice(dev1);
108 n2->AddDevice(dev2);
109
110 Ptr<ConstantPositionMobilityModel> sender0Mobility =
111 CreateObject<ConstantPositionMobilityModel>();
112 sender0Mobility->SetPosition(Vector(0, 0, 0));
113 dev0->GetPhy()->SetMobility(sender0Mobility);
114 n0->AggregateObject(sender0Mobility);
115
116 Ptr<ConstantPositionMobilityModel> sender1Mobility =
117 CreateObject<ConstantPositionMobilityModel>();
118 // Configure position 10 m distance
119 sender1Mobility->SetPosition(Vector(0, 1, 0));
120 dev1->GetPhy()->SetMobility(sender1Mobility);
121 n1->AggregateObject(sender1Mobility);
122
123 Ptr<ConstantPositionMobilityModel> sender2Mobility =
124 CreateObject<ConstantPositionMobilityModel>();
125 // Configure position 10 m distance
126 sender2Mobility->SetPosition(Vector(30, 0, 0));
127 dev2->GetPhy()->SetMobility(sender2Mobility);
128 n2->AggregateObject(sender2Mobility);
129
130 dev0->GetMac()->SetMcpsDataIndicationCallback(
132
133 // Disable first backoff
134 dev0->GetCsmaCa()->SetMacMinBE(0);
135 dev1->GetCsmaCa()->SetMacMinBE(0);
136 dev2->GetCsmaCa()->SetMacMinBE(0);
137
138 Ptr<Packet> p0 = Create<Packet>(20);
139 Ptr<Packet> p1 = Create<Packet>(60);
140 Ptr<Packet> p2 = Create<Packet>(100);
141
143 params.m_srcAddrMode = SHORT_ADDR;
144 params.m_dstAddrMode = SHORT_ADDR;
145 params.m_dstPanId = 0;
146 params.m_msduHandle = 0;
147 // params.m_txOptions = TX_OPTION_ACK;
148
149 // First case: concurrent tx and no ACKs
150 std::cout << "*** First test " << std::endl;
151 m_rxPackets = 0;
152 params.m_dstAddr = Mac16Address("00:02");
153 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
154
155 params.m_dstAddr = Mac16Address("00:01");
156 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p1);
157
159
160 NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 0, "Not received a packet (as expected)");
161
162 // Second case: concurrent tx and ACKs
163 std::cout << "*** Second test " << std::endl;
164 m_rxPackets = 0;
165 params.m_txOptions = TX_OPTION_ACK;
166
167 params.m_dstAddr = Mac16Address("00:02");
168 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
169
170 params.m_dstAddr = Mac16Address("00:01");
171 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p1);
172
174
175 NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 1, "Received a packet (as expected)");
176
177 // Third case: two concurrent tx and no ACKs
178 std::cout << "*** Third test " << std::endl;
179 m_rxPackets = 0;
180 params.m_txOptions = 0;
181
182 // LogComponentEnable("LrWpanMac",LOG_LEVEL_ALL);
183 // LogComponentEnable("LrWpanPhy",LOG_LEVEL_ALL);
184 // LogComponentEnableAll (LOG_PREFIX_TIME);
185
186 params.m_dstAddr = Mac16Address("00:01");
187 Simulator::Schedule(Seconds(0.0001), &LrWpanMac::McpsDataRequest, dev2->GetMac(), params, p2);
188
189 params.m_dstAddr = Mac16Address("00:01");
190 Simulator::Schedule(Seconds(0.0002), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p0);
191
193
194 std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
195 NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 1, "Received a packet (as expected)");
196
197 // Fourth case: two concurrent tx and ACKs
198 std::cout << "*** Fourth test " << std::endl;
199 m_rxPackets = 0;
200 params.m_txOptions = TX_OPTION_ACK;
201
202 params.m_dstAddr = Mac16Address("00:01");
203 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p0);
204
205 params.m_dstAddr = Mac16Address("00:01");
206 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev2->GetMac(), params, p1);
207
209
210 std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
211 NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 2, "Received two packets (as expected)");
212
214}
215
216/**
217 * \ingroup lr-wpan-test
218 * \ingroup tests
219 *
220 * \brief LrWpan Collision TestSuite
221 */
223{
224 public:
226};
227
229 : TestSuite("lr-wpan-collision", Type::UNIT)
230{
231 AddTestCase(new LrWpanCollisionTestCase, TestCase::Duration::QUICK);
232}
233
235 g_lrWpanCollisionTestSuite; //!< Static variable for test initialization
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.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
Definition: lr-wpan-mac.cc:386
#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:64
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
static LrWpanCollisionTestSuite g_lrWpanCollisionTestSuite
Static variable for test initialization.
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:706
MCPS-DATA.indication params.