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;
36
37NS_LOG_COMPONENT_DEFINE("lr-wpan-clear-channel-assessment-test");
38
46{
47 public:
49
50 private:
57 static void PlmeCcaConfirm(LrWpanCcaTestCase* testcase,
66 static void PhyTxBegin(LrWpanCcaTestCase* testcase,
68 Ptr<const Packet> packet);
75 static void PhyTxEnd(LrWpanCcaTestCase* testcase,
77 Ptr<const Packet> packet);
84 static void PhyRxBegin(LrWpanCcaTestCase* testcase,
86 Ptr<const Packet> packet);
94 static void PhyRxEnd(LrWpanCcaTestCase* testcase,
96 Ptr<const Packet> packet,
97 double sinr);
104 static void PhyRxDrop(LrWpanCcaTestCase* testcase,
106 Ptr<const Packet> packet);
107
108 void DoRun() override;
109
111};
112
114 : TestCase("Test the 802.15.4 clear channel assessment")
115{
117}
118
119void
123{
124 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
125 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
126 << " PlmeCcaConfirm: " << LrWpanHelper::LrWpanPhyEnumerationPrinter(status)
127 << std::endl;
128
129 testcase->m_status = status;
130}
131
132void
135 Ptr<const Packet> packet)
136{
137 std::ostringstream os;
138 packet->Print(os);
139 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
140 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
141 << " PhyTxBegin: " << os.str() << std::endl;
142}
143
144void
147 Ptr<const Packet> packet)
148{
149 std::ostringstream os;
150 packet->Print(os);
151 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
152 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
153 << " PhyTxEnd: " << os.str() << std::endl;
154}
155
156void
159 Ptr<const Packet> packet)
160{
161 std::ostringstream os;
162 packet->Print(os);
163 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
164 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
165 << " PhyRxBegin: " << os.str() << std::endl;
166}
167
168void
171 Ptr<const Packet> packet,
172 double sinr)
173{
174 std::ostringstream os;
175 packet->Print(os);
176 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
177 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
178 << " PhyRxEnd (" << sinr << "): " << os.str() << std::endl;
179
180 // The first packet was received. Now start a CCA, to try to detect the second packet which is
181 // still being transmitted.
182 device->GetPhy()->PlmeCcaRequest();
183}
184
185void
188 Ptr<const Packet> packet)
189{
190 std::ostringstream os;
191 packet->Print(os);
192 std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(9) << "["
193 << Simulator::Now().As(Time::S) << "] " << device->GetMac()->GetShortAddress()
194 << " PhyRxDrop: " << os.str() << std::endl;
195}
196
197void
199{
200 // Tx Power: 0 dBm
201 // Receiver Sensitivity: -106.58 dBm
202 // CCA channel busy condition: Rx power > -96.58 dBm
203 // Log distance reference loss at 1 m distance for channel 11 (2405 MHz): 40.0641 dB
204 // Log distance free space path loss exponent: 2
205
206 // Test setup:
207 // Start transmission of a short packet from node 0 to node 1 and at the same
208 // time a transmission of a large packet from node 2 to node 1.
209 // Both transmissions should start without backoff (per configuration) because
210 // the CCA on both nodes should detect a free medium.
211 // The shorter packet will be received first. After reception of the short
212 // packet, which might be destroyed due to interference of the large
213 // packet, node 1 will start a CCA. Depending on the distance between node 1
214 // and node 2, node 1 should detect a busy medium, because the transmission of
215 // the large packet is still in progress. For the above mentioned scenario
216 // parameters, the distance for the CCA detecting a busy medium is about
217 // 669.5685 m.
218
219 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
220 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
221
222 // Set the random seed and run number for this test
225
226 // Create 3 nodes, and a NetDevice for each one
227 Ptr<Node> n0 = CreateObject<Node>();
228 Ptr<Node> n1 = CreateObject<Node>();
229 Ptr<Node> n2 = CreateObject<Node>();
230
231 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
232 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
233 Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice>();
234
235 // Make random variable stream assignment deterministic
236 dev0->AssignStreams(0);
237 dev1->AssignStreams(10);
238 dev2->AssignStreams(20);
239
240 dev0->SetAddress(Mac16Address("00:01"));
241 dev1->SetAddress(Mac16Address("00:02"));
242 dev2->SetAddress(Mac16Address("00:03"));
243
244 // Each device must be attached to the same channel
245 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
247 CreateObject<LogDistancePropagationLossModel>();
248 propModel->SetReference(1.0,
249 40.0641); // Reference loss at 1m distance for 2405 MHz (channel 11)
250 propModel->SetPathLossExponent(2); // Free space path loss exponent
252 CreateObject<ConstantSpeedPropagationDelayModel>();
253 channel->AddPropagationLossModel(propModel);
254 channel->SetPropagationDelayModel(delayModel);
255
256 dev0->SetChannel(channel);
257 dev1->SetChannel(channel);
258 dev2->SetChannel(channel);
259
260 // To complete configuration, a LrWpanNetDevice must be added to a node
261 n0->AddDevice(dev0);
262 n1->AddDevice(dev1);
263 n2->AddDevice(dev2);
264
265 Ptr<ConstantPositionMobilityModel> sender0Mobility =
266 CreateObject<ConstantPositionMobilityModel>();
267 sender0Mobility->SetPosition(Vector(0, 0, 0));
268 dev0->GetPhy()->SetMobility(sender0Mobility);
269 Ptr<ConstantPositionMobilityModel> sender1Mobility =
270 CreateObject<ConstantPositionMobilityModel>();
271 sender1Mobility->SetPosition(Vector(0, 669, 0));
272 dev1->GetPhy()->SetMobility(sender1Mobility);
273 Ptr<ConstantPositionMobilityModel> sender2Mobility =
274 CreateObject<ConstantPositionMobilityModel>();
275 sender2Mobility->SetPosition(Vector(0, 1338, 0));
276 dev2->GetPhy()->SetMobility(sender2Mobility);
277
278 // Disable the NetDevices queue management.
279 dev0->GetMac()->SetMcpsDataConfirmCallback(MakeNullCallback<void, McpsDataConfirmParams>());
280 dev1->GetMac()->SetMcpsDataConfirmCallback(MakeNullCallback<void, McpsDataConfirmParams>());
281 dev2->GetMac()->SetMcpsDataConfirmCallback(MakeNullCallback<void, McpsDataConfirmParams>());
282
283 // Set the CCA confirm callback.
284 dev1->GetPhy()->SetPlmeCcaConfirmCallback(
286
287 // Start sending without backoff, if the channel is free.
288 dev0->GetCsmaCa()->SetMacMinBE(0);
289 dev2->GetCsmaCa()->SetMacMinBE(0);
290
291 // Connect trace sources.
292 dev0->GetPhy()->TraceConnectWithoutContext(
293 "PhyTxBegin",
295 dev0->GetPhy()->TraceConnectWithoutContext(
296 "PhyTxEnd",
298 dev2->GetPhy()->TraceConnectWithoutContext(
299 "PhyTxBegin",
301 dev2->GetPhy()->TraceConnectWithoutContext(
302 "PhyTxEnd",
304 dev1->GetPhy()->TraceConnectWithoutContext(
305 "PhyRxBegin",
307 dev1->GetPhy()->TraceConnectWithoutContext(
308 "PhyRxEnd",
310 dev1->GetPhy()->TraceConnectWithoutContext(
311 "PhyRxDrop",
313
315
316 Ptr<Packet> p0 = Create<Packet>(1); // 1 byte of dummy data
317 McpsDataRequestParams params0;
318 params0.m_srcAddrMode = SHORT_ADDR;
319 params0.m_dstAddrMode = SHORT_ADDR;
320 params0.m_dstPanId = 0;
321 params0.m_dstAddr = Mac16Address("00:02");
322 params0.m_msduHandle = 0;
323 params0.m_txOptions = TX_OPTION_NONE;
324 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params0, p0);
325
326 Ptr<Packet> p1 = Create<Packet>(100); // 100 bytes of dummy data
327 McpsDataRequestParams params1;
328 params1.m_srcAddrMode = SHORT_ADDR;
329 params1.m_dstAddrMode = SHORT_ADDR;
330 params1.m_dstPanId = 0;
331 params1.m_dstAddr = Mac16Address("00:02");
332 params1.m_msduHandle = 0;
333 params1.m_txOptions = TX_OPTION_NONE;
334 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev2->GetMac(), params1, p1);
335
337
338 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_BUSY, "CCA status BUSY (as expected)");
339
341
342 sender2Mobility->SetPosition(Vector(0, 1340, 0));
343
344 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params0, p0);
345 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev2->GetMac(), params1, p1);
346
348
349 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_IDLE, "CCA status IDLE (as expected)");
350
352}
353
361{
362 public:
364};
365
367 : TestSuite("lr-wpan-clear-channel-assessment", UNIT)
368{
370}
371
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.
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.
LrWpanPhyEnumeration m_status
PHY status.
static void PhyRxEnd(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet, double sinr)
Function called when PhyRxEnd is hit.
static void PlmeCcaConfirm(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, LrWpanPhyEnumeration status)
Function called when PlmeCcaConfirm is hit.
LrWpan ACK TestSuite.
static std::string LrWpanPhyEnumerationPrinter(LrWpanPhyEnumeration e)
Transform the LrWpanPhyEnumeration enumeration into a printable string.
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p)
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
Definition: lr-wpan-mac.cc:349
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:140
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
static void Run()
Run the simulation.
Definition: simulator.cc:176
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
@ S
second
Definition: nstime.h:116
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:109
@ IEEE_802_15_4_PHY_BUSY
Definition: lr-wpan-phy.h:110
@ IEEE_802_15_4_PHY_UNSPECIFIED
Definition: lr-wpan-phy.h:122
@ IEEE_802_15_4_PHY_IDLE
Definition: lr-wpan-phy.h:114
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:61
@ SHORT_ADDR
Definition: lr-wpan-mac.h:156
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:763
#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:251
static LrWpanCcaTestSuite g_lrWpanCcaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:402
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:403
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:404
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:405
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:406
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:408
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:409