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;
31
32NS_LOG_COMPONENT_DEFINE("lr-wpan-collision-test");
33
34/**
35 * \ingroup lr-wpan-test
36 * \ingroup tests
37 *
38 * \brief LrWpan Collision Test
39 */
41{
42 public:
44 ~LrWpanCollisionTestCase() override;
45
46 /**
47 * \brief Function called when DataIndication is hit.
48 * \param params The MCPS params.
49 * \param p The packet.
50 */
52
53 private:
54 void DoRun() override;
55
56 uint8_t m_rxPackets; //!< Rx packets counter.
57};
58
60 : TestCase("Test the 802.15.4 collision handling")
61{
62 m_rxPackets = 0;
63}
64
66{
67}
68
69void
71{
73}
74
75void
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
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
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
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
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
213}
214
215/**
216 * \ingroup lr-wpan-test
217 * \ingroup tests
218 *
219 * \brief LrWpan Collision TestSuite
220 */
222{
223 public:
225};
226
228 : TestSuite("lr-wpan-collision", Type::UNIT)
229{
230 AddTestCase(new LrWpanCollisionTestCase, TestCase::Duration::QUICK);
231}
232
234 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.
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:384
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
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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:704
MCPS-DATA.indication params.
MCPS-DATA.request params.