A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-cca-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 Fraunhofer FKIE
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:
18 * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
19 */
20
21#include "ns3/rng-seed-manager.h"
22#include <ns3/constant-position-mobility-model.h>
23#include <ns3/core-module.h>
24#include <ns3/log.h>
25#include <ns3/lr-wpan-module.h>
26#include <ns3/packet.h>
27#include <ns3/propagation-delay-model.h>
28#include <ns3/propagation-loss-model.h>
29#include <ns3/simulator.h>
30#include <ns3/single-model-spectrum-channel.h>
31
32#include <iomanip>
33#include <iostream>
34
35using namespace ns3;
36using namespace ns3::lrwpan;
37
38NS_LOG_COMPONENT_DEFINE("lr-wpan-clear-channel-assessment-test");
39
40/**
41 * \ingroup lr-wpan-test
42 * \ingroup tests
43 *
44 * \brief LrWpan CCA Test
45 */
47{
48 public:
50
51 private:
52 /**
53 * \brief Function called when PlmeCcaConfirm is hit.
54 * \param testcase The TestCase.
55 * \param device The LrWpanNetDevice.
56 * \param status The device status.
57 */
58 static void PlmeCcaConfirm(LrWpanCcaTestCase* testcase,
60 PhyEnumeration status);
61 /**
62 * \brief Function called when PhyTxBegin is hit.
63 * \param testcase The TestCase.
64 * \param device The LrWpanNetDevice.
65 * \param packet The packet.
66 */
67 static void PhyTxBegin(LrWpanCcaTestCase* testcase,
69 Ptr<const Packet> packet);
70 /**
71 * \brief Function called when PhyTxEnd is hit.
72 * \param testcase The TestCase.
73 * \param device The LrWpanNetDevice.
74 * \param packet The packet.
75 */
76 static void PhyTxEnd(LrWpanCcaTestCase* testcase,
78 Ptr<const Packet> packet);
79 /**
80 * \brief Function called when PhyRxBegin is hit.
81 * \param testcase The TestCase.
82 * \param device The LrWpanNetDevice.
83 * \param packet The packet.
84 */
85 static void PhyRxBegin(LrWpanCcaTestCase* testcase,
87 Ptr<const Packet> packet);
88 /**
89 * \brief Function called when PhyRxEnd is hit.
90 * \param testcase The TestCase.
91 * \param device The LrWpanNetDevice.
92 * \param packet The packet.
93 * \param sinr The received SINR.
94 */
95 static void PhyRxEnd(LrWpanCcaTestCase* testcase,
97 Ptr<const Packet> packet,
98 double sinr);
99 /**
100 * \brief Function called when PhyRxDrop is hit.
101 * \param testcase The TestCase.
102 * \param device The LrWpanNetDevice.
103 * \param packet The packet.
104 */
105 static void PhyRxDrop(LrWpanCcaTestCase* testcase,
107 Ptr<const Packet> packet);
108
109 void DoRun() override;
110
111 PhyEnumeration m_status; //!< PHY status.
112};
113
115 : TestCase("Test the 802.15.4 clear channel assessment")
116{
118}
119
120void
123 PhyEnumeration status)
124{
125 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
126 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
127 << " PlmeCcaConfirm: " << LrWpanHelper::LrWpanPhyEnumerationPrinter(status)
128 << std::endl;
129
130 testcase->m_status = status;
131}
132
133void
136 Ptr<const Packet> packet)
137{
138 std::ostringstream os;
139 packet->Print(os);
140 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
141 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
142 << " PhyTxBegin: " << os.str() << std::endl;
143}
144
145void
148 Ptr<const Packet> packet)
149{
150 std::ostringstream os;
151 packet->Print(os);
152 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
153 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
154 << " PhyTxEnd: " << os.str() << std::endl;
155}
156
157void
160 Ptr<const Packet> packet)
161{
162 std::ostringstream os;
163 packet->Print(os);
164 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
165 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
166 << " PhyRxBegin: " << os.str() << std::endl;
167}
168
169void
172 Ptr<const Packet> packet,
173 double sinr)
174{
175 std::ostringstream os;
176 packet->Print(os);
177 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
178 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
179 << " PhyRxEnd (" << sinr << "): " << os.str() << std::endl;
180
181 // The first packet was received. Now start a CCA, to try to detect the second packet which is
182 // still being transmitted.
183 device->GetPhy()->PlmeCcaRequest();
184}
185
186void
189 Ptr<const Packet> packet)
190{
191 std::ostringstream os;
192 packet->Print(os);
193 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
194 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
195 << " PhyRxDrop: " << os.str() << std::endl;
196}
197
198void
200{
201 // Tx Power: 0 dBm
202 // Receiver Sensitivity: -106.58 dBm
203 // CCA channel busy condition: Rx power > -96.58 dBm
204 // Log distance reference loss at 1 m distance for channel 11 (2405 MHz): 40.0641 dB
205 // Log distance free space path loss exponent: 2
206
207 // Test setup:
208 // Start transmission of a short packet from node 0 to node 1 and at the same
209 // time a transmission of a large packet from node 2 to node 1.
210 // Both transmissions should start without backoff (per configuration) because
211 // the CCA on both nodes should detect a free medium.
212 // The shorter packet will be received first. After reception of the short
213 // packet, which might be destroyed due to interference of the large
214 // packet, node 1 will start a CCA. Depending on the distance between node 1
215 // and node 2, node 1 should detect a busy medium, because the transmission of
216 // the large packet is still in progress. For the above mentioned scenario
217 // parameters, the distance for the CCA detecting a busy medium is about
218 // 669.5685 m.
219
220 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
221 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
222
223 // Set the random seed and run number for this test
226
227 // Create 3 nodes, and a NetDevice for each one
228 Ptr<Node> n0 = CreateObject<Node>();
229 Ptr<Node> n1 = CreateObject<Node>();
230 Ptr<Node> n2 = CreateObject<Node>();
231
232 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
233 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
234 Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice>();
235
236 // Make random variable stream assignment deterministic
237 dev0->AssignStreams(0);
238 dev1->AssignStreams(10);
239 dev2->AssignStreams(20);
240
241 dev0->SetAddress(Mac16Address("00:01"));
242 dev1->SetAddress(Mac16Address("00:02"));
243 dev2->SetAddress(Mac16Address("00:03"));
244
245 // Each device must be attached to the same channel
246 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
248 CreateObject<LogDistancePropagationLossModel>();
249 propModel->SetReference(1.0,
250 40.0641); // Reference loss at 1m distance for 2405 MHz (channel 11)
251 propModel->SetPathLossExponent(2); // Free space path loss exponent
253 CreateObject<ConstantSpeedPropagationDelayModel>();
254 channel->AddPropagationLossModel(propModel);
255 channel->SetPropagationDelayModel(delayModel);
256
257 dev0->SetChannel(channel);
258 dev1->SetChannel(channel);
259 dev2->SetChannel(channel);
260
261 // To complete configuration, a LrWpanNetDevice must be added to a node
262 n0->AddDevice(dev0);
263 n1->AddDevice(dev1);
264 n2->AddDevice(dev2);
265
266 Ptr<ConstantPositionMobilityModel> sender0Mobility =
267 CreateObject<ConstantPositionMobilityModel>();
268 sender0Mobility->SetPosition(Vector(0, 0, 0));
269 dev0->GetPhy()->SetMobility(sender0Mobility);
270 Ptr<ConstantPositionMobilityModel> sender1Mobility =
271 CreateObject<ConstantPositionMobilityModel>();
272 sender1Mobility->SetPosition(Vector(0, 669, 0));
273 dev1->GetPhy()->SetMobility(sender1Mobility);
274 Ptr<ConstantPositionMobilityModel> sender2Mobility =
275 CreateObject<ConstantPositionMobilityModel>();
276 sender2Mobility->SetPosition(Vector(0, 1338, 0));
277 dev2->GetPhy()->SetMobility(sender2Mobility);
278
279 // Disable the NetDevices queue management.
280 dev0->GetMac()->SetMcpsDataConfirmCallback(MakeNullCallback<void, McpsDataConfirmParams>());
281 dev1->GetMac()->SetMcpsDataConfirmCallback(MakeNullCallback<void, McpsDataConfirmParams>());
282 dev2->GetMac()->SetMcpsDataConfirmCallback(MakeNullCallback<void, McpsDataConfirmParams>());
283
284 // Set the CCA confirm callback.
285 dev1->GetPhy()->SetPlmeCcaConfirmCallback(
287
288 // Start sending without backoff, if the channel is free.
289 dev0->GetCsmaCa()->SetMacMinBE(0);
290 dev2->GetCsmaCa()->SetMacMinBE(0);
291
292 // Connect trace sources.
293 dev0->GetPhy()->TraceConnectWithoutContext(
294 "PhyTxBegin",
296 dev0->GetPhy()->TraceConnectWithoutContext(
297 "PhyTxEnd",
299 dev2->GetPhy()->TraceConnectWithoutContext(
300 "PhyTxBegin",
302 dev2->GetPhy()->TraceConnectWithoutContext(
303 "PhyTxEnd",
305 dev1->GetPhy()->TraceConnectWithoutContext(
306 "PhyRxBegin",
308 dev1->GetPhy()->TraceConnectWithoutContext(
309 "PhyRxEnd",
311 dev1->GetPhy()->TraceConnectWithoutContext(
312 "PhyRxDrop",
314
316
317 Ptr<Packet> p0 = Create<Packet>(1); // 1 byte of dummy data
318 McpsDataRequestParams params0;
319 params0.m_srcAddrMode = SHORT_ADDR;
320 params0.m_dstAddrMode = SHORT_ADDR;
321 params0.m_dstPanId = 0;
322 params0.m_dstAddr = Mac16Address("00:02");
323 params0.m_msduHandle = 0;
324 params0.m_txOptions = TX_OPTION_NONE;
325 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params0, p0);
326
327 Ptr<Packet> p1 = Create<Packet>(100); // 100 bytes of dummy data
328 McpsDataRequestParams params1;
329 params1.m_srcAddrMode = SHORT_ADDR;
330 params1.m_dstAddrMode = SHORT_ADDR;
331 params1.m_dstPanId = 0;
332 params1.m_dstAddr = Mac16Address("00:02");
333 params1.m_msduHandle = 0;
334 params1.m_txOptions = TX_OPTION_NONE;
335 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev2->GetMac(), params1, p1);
336
338
339 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_BUSY, "CCA status BUSY (as expected)");
340
342
343 sender2Mobility->SetPosition(Vector(0, 1340, 0));
344
345 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params0, p0);
346 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev2->GetMac(), params1, p1);
347
349
350 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_IDLE, "CCA status IDLE (as expected)");
351
353}
354
355/**
356 * \ingroup lr-wpan-test
357 * \ingroup tests
358 *
359 * \brief LrWpan ACK TestSuite
360 */
362{
363 public:
365};
366
368 : TestSuite("lr-wpan-clear-channel-assessment", Type::UNIT)
369{
370 AddTestCase(new LrWpanCcaTestCase, TestCase::Duration::QUICK);
371}
372
373static LrWpanCcaTestSuite g_lrWpanCcaTestSuite; //!< Static variable for test initialization
LrWpan CCA Test.
static void PhyTxEnd(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyTxEnd is hit.
static void PhyTxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyTxBegin is hit.
static void PhyRxDrop(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyRxDrop is hit.
PhyEnumeration m_status
PHY status.
static void PlmeCcaConfirm(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, PhyEnumeration status)
Function called when PlmeCcaConfirm is hit.
static void PhyRxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyRxBegin is hit.
void DoRun() override
Implementation to actually run this TestCase.
static void PhyRxEnd(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet, double sinr)
Function called when PhyRxEnd is hit.
LrWpan ACK TestSuite.
static std::string LrWpanPhyEnumerationPrinter(lrwpan::PhyEnumeration e)
Transform the LrWpanPhyEnumeration enumeration into a printable string.
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 void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
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
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
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
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:115
@ IEEE_802_15_4_PHY_BUSY
Definition: lr-wpan-phy.h:116
@ IEEE_802_15_4_PHY_IDLE
Definition: lr-wpan-phy.h:120
@ IEEE_802_15_4_PHY_UNSPECIFIED
Definition: lr-wpan-phy.h:128
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:63
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:767
#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
static LrWpanCcaTestSuite g_lrWpanCcaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
AddressMode m_dstAddrMode
Destination address mode.
Mac16Address m_dstAddr
Destination address.
uint16_t m_dstPanId
Destination PAN identifier.
AddressMode m_srcAddrMode
Source address mode.
uint8_t m_txOptions
Tx Options (bitfield)