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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include <ns3/log.h>
10#include <ns3/lr-wpan-module.h>
11#include <ns3/mac16-address.h>
12#include <ns3/mac64-address.h>
13#include <ns3/mobility-module.h>
14#include <ns3/packet.h>
15#include <ns3/propagation-module.h>
16#include <ns3/spectrum-module.h>
17#include <ns3/test.h>
18
19using namespace ns3;
20using namespace ns3::lrwpan;
21
22NS_LOG_COMPONENT_DEFINE("lr-wpan-collision-test");
23
24/**
25 * @ingroup lr-wpan-test
26 * @ingroup tests
27 *
28 * @brief LrWpan Collision Test
29 */
31{
32 public:
34 ~LrWpanCollisionTestCase() override;
35
36 /**
37 * @brief Function called when DataIndication is hit.
38 * @param params The MCPS params.
39 * @param p The packet.
40 */
42
43 private:
44 void DoRun() override;
45
46 uint8_t m_rxPackets; //!< Rx packets counter.
47};
48
50 : TestCase("Test the 802.15.4 collision handling")
51{
52 m_rxPackets = 0;
53}
54
58
59void
64
65void
67{
68 // Create 3 nodes, and a NetDevice for each one
72
76
77 dev0->SetAddress(Mac16Address("00:01"));
78 dev1->SetAddress(Mac16Address("00:02"));
79 dev2->SetAddress(Mac16Address("00:03"));
80
81 // Each device must be attached to the same channel
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
101 sender0Mobility->SetPosition(Vector(0, 0, 0));
102 dev0->GetPhy()->SetMobility(sender0Mobility);
103 n0->AggregateObject(sender0Mobility);
104
105 Ptr<ConstantPositionMobilityModel> sender1Mobility =
107 // Configure position 10 m distance
108 sender1Mobility->SetPosition(Vector(0, 1, 0));
109 dev1->GetPhy()->SetMobility(sender1Mobility);
110 n1->AggregateObject(sender1Mobility);
111
112 Ptr<ConstantPositionMobilityModel> sender2Mobility =
114 // Configure position 10 m distance
115 sender2Mobility->SetPosition(Vector(30, 0, 0));
116 dev2->GetPhy()->SetMobility(sender2Mobility);
117 n2->AggregateObject(sender2Mobility);
118
119 dev0->GetMac()->SetMcpsDataIndicationCallback(
121
122 // Disable first backoff
123 dev0->GetCsmaCa()->SetMacMinBE(0);
124 dev1->GetCsmaCa()->SetMacMinBE(0);
125 dev2->GetCsmaCa()->SetMacMinBE(0);
126
129 Ptr<Packet> p2 = Create<Packet>(100);
130
132 params.m_srcAddrMode = SHORT_ADDR;
133 params.m_dstAddrMode = SHORT_ADDR;
134 params.m_dstPanId = 0;
135 params.m_msduHandle = 0;
136 // params.m_txOptions = TX_OPTION_ACK;
137
138 // First case: concurrent tx and no ACKs
139 std::cout << "*** First test " << std::endl;
140 m_rxPackets = 0;
141 params.m_dstAddr = Mac16Address("00:02");
142 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
143
144 params.m_dstAddr = Mac16Address("00:01");
145 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p1);
146
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), &LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
158
159 params.m_dstAddr = Mac16Address("00:01");
160 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p1);
161
163
164 NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 1, "Received a packet (as expected)");
165
166 // Third case: two concurrent tx and no ACKs
167 std::cout << "*** Third test " << std::endl;
168 m_rxPackets = 0;
169 params.m_txOptions = 0;
170
171 // LogComponentEnable("LrWpanMac",LOG_LEVEL_ALL);
172 // LogComponentEnable("LrWpanPhy",LOG_LEVEL_ALL);
173 // LogComponentEnableAll (LOG_PREFIX_TIME);
174
175 params.m_dstAddr = Mac16Address("00:01");
176 Simulator::Schedule(Seconds(0.0001), &LrWpanMac::McpsDataRequest, dev2->GetMac(), params, p2);
177
178 params.m_dstAddr = Mac16Address("00:01");
179 Simulator::Schedule(Seconds(0.0002), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p0);
180
182
183 std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
184 NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 1, "Received a packet (as expected)");
185
186 // Fourth case: two concurrent tx and ACKs
187 std::cout << "*** Fourth test " << std::endl;
188 m_rxPackets = 0;
189 params.m_txOptions = TX_OPTION_ACK;
190
191 params.m_dstAddr = Mac16Address("00:01");
192 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev1->GetMac(), params, p0);
193
194 params.m_dstAddr = Mac16Address("00:01");
195 Simulator::Schedule(Seconds(0.1), &LrWpanMac::McpsDataRequest, dev2->GetMac(), params, p1);
196
198
199 std::cout << "m_rxPackets = " << int(m_rxPackets) << std::endl;
200 NS_TEST_EXPECT_MSG_EQ(m_rxPackets, 2, "Received two packets (as expected)");
201
203}
204
205/**
206 * @ingroup lr-wpan-test
207 * @ingroup tests
208 *
209 * @brief LrWpan Collision TestSuite
210 */
212{
213 public:
215};
216
218 : TestSuite("lr-wpan-collision", Type::UNIT)
219{
220 AddTestCase(new LrWpanCollisionTestCase, TestCase::Duration::QUICK);
221}
222
224 g_lrWpanCollisionTestSuite; //!< Static variable for test initialization
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.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:53
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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:684