A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
19
using namespace
ns3
;
20
using namespace
ns3::lrwpan
;
21
22
NS_LOG_COMPONENT_DEFINE
(
"lr-wpan-collision-test"
);
23
24
/**
25
* @ingroup lr-wpan-test
26
* @ingroup tests
27
*
28
* @brief LrWpan Collision Test
29
*/
30
class
LrWpanCollisionTestCase
:
public
TestCase
31
{
32
public
:
33
LrWpanCollisionTestCase
();
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
*/
41
void
DataIndication
(
McpsDataIndicationParams
params,
Ptr<Packet>
p);
42
43
private
:
44
void
DoRun
()
override
;
45
46
uint8_t
m_rxPackets
;
//!< Rx packets counter.
47
};
48
49
LrWpanCollisionTestCase::LrWpanCollisionTestCase
()
50
:
TestCase
(
"Test the 802.15.4 collision handling"
)
51
{
52
m_rxPackets
= 0;
53
}
54
55
LrWpanCollisionTestCase::~LrWpanCollisionTestCase
()
56
{
57
}
58
59
void
60
LrWpanCollisionTestCase::DataIndication
(
McpsDataIndicationParams
params,
Ptr<Packet>
p)
61
{
62
m_rxPackets
++;
63
}
64
65
void
66
LrWpanCollisionTestCase::DoRun
()
67
{
68
// Create 3 nodes, and a NetDevice for each one
69
Ptr<Node>
n0
=
CreateObject<Node>
();
70
Ptr<Node>
n1
=
CreateObject<Node>
();
71
Ptr<Node>
n2
=
CreateObject<Node>
();
72
73
Ptr<LrWpanNetDevice>
dev0
=
CreateObject<LrWpanNetDevice>
();
74
Ptr<LrWpanNetDevice>
dev1
=
CreateObject<LrWpanNetDevice>
();
75
Ptr<LrWpanNetDevice>
dev2
=
CreateObject<LrWpanNetDevice>
();
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
82
Ptr<SingleModelSpectrumChannel>
channel =
CreateObject<SingleModelSpectrumChannel>
();
83
Ptr<LogDistancePropagationLossModel>
propModel
=
84
CreateObject<LogDistancePropagationLossModel>
();
85
Ptr<ConstantSpeedPropagationDelayModel>
delayModel
=
86
CreateObject<ConstantSpeedPropagationDelayModel>
();
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
99
Ptr<ConstantPositionMobilityModel>
sender0Mobility
=
100
CreateObject<ConstantPositionMobilityModel>
();
101
sender0Mobility
->SetPosition(Vector(0, 0, 0));
102
dev0
->GetPhy()->SetMobility(
sender0Mobility
);
103
n0
->AggregateObject(
sender0Mobility
);
104
105
Ptr<ConstantPositionMobilityModel>
sender1Mobility
=
106
CreateObject<ConstantPositionMobilityModel>
();
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
=
113
CreateObject<ConstantPositionMobilityModel>
();
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(
120
MakeCallback
(&
LrWpanCollisionTestCase::DataIndication
,
this
));
121
122
// Disable first backoff
123
dev0
->GetCsmaCa()->SetMacMinBE(0);
124
dev1
->GetCsmaCa()->SetMacMinBE(0);
125
dev2
->GetCsmaCa()->SetMacMinBE(0);
126
127
Ptr<Packet>
p0
=
Create<Packet>
(20);
128
Ptr<Packet>
p1
=
Create<Packet>
(60);
129
Ptr<Packet>
p2
=
Create<Packet>
(100);
130
131
McpsDataRequestParams
params;
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
147
Simulator::Run
();
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
162
Simulator::Run
();
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
181
Simulator::Run
();
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
197
Simulator::Run
();
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
202
Simulator::Destroy
();
203
}
204
205
/**
206
* @ingroup lr-wpan-test
207
* @ingroup tests
208
*
209
* @brief LrWpan Collision TestSuite
210
*/
211
class
LrWpanCollisionTestSuite
:
public
TestSuite
212
{
213
public
:
214
LrWpanCollisionTestSuite
();
215
};
216
217
LrWpanCollisionTestSuite::LrWpanCollisionTestSuite
()
218
:
TestSuite
(
"lr-wpan-collision"
,
Type
::UNIT)
219
{
220
AddTestCase
(
new
LrWpanCollisionTestCase
, TestCase::Duration::QUICK);
221
}
222
223
static
LrWpanCollisionTestSuite
224
g_lrWpanCollisionTestSuite
;
//!< Static variable for test initialization
LrWpanCollisionTestCase
LrWpan Collision Test.
Definition
lr-wpan-collision-test.cc:31
LrWpanCollisionTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
lr-wpan-collision-test.cc:66
LrWpanCollisionTestCase::~LrWpanCollisionTestCase
~LrWpanCollisionTestCase() override
Definition
lr-wpan-collision-test.cc:55
LrWpanCollisionTestCase::m_rxPackets
uint8_t m_rxPackets
Rx packets counter.
Definition
lr-wpan-collision-test.cc:46
LrWpanCollisionTestCase::DataIndication
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit.
Definition
lr-wpan-collision-test.cc:60
LrWpanCollisionTestCase::LrWpanCollisionTestCase
LrWpanCollisionTestCase()
Definition
lr-wpan-collision-test.cc:49
LrWpanCollisionTestSuite
LrWpan Collision TestSuite.
Definition
lr-wpan-collision-test.cc:212
LrWpanCollisionTestSuite::LrWpanCollisionTestSuite
LrWpanCollisionTestSuite()
Definition
lr-wpan-collision-test.cc:217
ns3::Mac16Address
This class can contain 16 bit addresses.
Definition
mac16-address.h:33
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition
simulator.h:560
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
ns3::lrwpan::LrWpanMac::McpsDataRequest
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:377
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
ns3::lrwpan::SHORT_ADDR
@ SHORT_ADDR
Definition
lr-wpan-mac-base.h:90
ns3::lrwpan::TX_OPTION_ACK
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition
lr-wpan-mac.h:53
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
NS_TEST_EXPECT_MSG_EQ
#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
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
g_lrWpanCollisionTestSuite
static LrWpanCollisionTestSuite g_lrWpanCollisionTestSuite
Static variable for test initialization.
Definition
lr-wpan-collision-test.cc:224
ns3::lrwpan
Definition
lr-wpan-constants.h:23
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeCallback
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
ns3::lrwpan::McpsDataIndicationParams
MCPS-DATA.indication params.
Definition
lr-wpan-mac-base.h:336
ns3::lrwpan::McpsDataRequestParams
MCPS-DATA.request params.
Definition
lr-wpan-mac-base.h:113
src
lr-wpan
test
lr-wpan-collision-test.cc
Generated on Mon Dec 15 2025 15:21:55 for ns-3 by
1.9.8