A Discrete-Event Network Simulator
API
lr-wpan-cca-test.cc
Go to the documentation of this file.
1/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014 Fraunhofer FKIE
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author:
19 * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
20 */
21
22#include <ns3/log.h>
23#include <ns3/core-module.h>
24#include <ns3/lr-wpan-module.h>
25#include <ns3/propagation-loss-model.h>
26#include <ns3/propagation-delay-model.h>
27#include <ns3/simulator.h>
28#include <ns3/single-model-spectrum-channel.h>
29#include <ns3/constant-position-mobility-model.h>
30#include <ns3/packet.h>
31#include "ns3/rng-seed-manager.h"
32
33#include <iostream>
34#include <iomanip>
35
36using namespace ns3;
37
38NS_LOG_COMPONENT_DEFINE ("lr-wpan-clear-channel-assessment-test");
39
47{
48public:
50
51private:
58 static void PlmeCcaConfirm (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, LrWpanPhyEnumeration status);
65 static void PhyTxBegin (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
72 static void PhyTxEnd (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
79 static void PhyRxBegin (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
87 static void PhyRxEnd (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet, double sinr);
94 static void PhyRxDrop (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
95
96 virtual void DoRun (void);
97
99
100};
101
103 : TestCase ("Test the 802.15.4 clear channel assessment")
104{
106}
107
108void
110{
111 std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().As (Time::S) << "] " << device->GetMac ()->GetShortAddress () << " PlmeCcaConfirm: " << LrWpanHelper::LrWpanPhyEnumerationPrinter (status) << std::endl;
112
113 testcase->m_status = status;
114}
115
116void
118{
119 std::ostringstream os;
120 packet->Print (os);
121 std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().As (Time::S) << "] " << device->GetMac ()->GetShortAddress () << " PhyTxBegin: " << os.str () << std::endl;
122}
123
124void
126{
127 std::ostringstream os;
128 packet->Print (os);
129 std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().As (Time::S) << "] " << device->GetMac ()->GetShortAddress () << " PhyTxEnd: " << os.str () << std::endl;
130}
131
132void
134{
135 std::ostringstream os;
136 packet->Print (os);
137 std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().As (Time::S) << "] " << device->GetMac ()->GetShortAddress () << " PhyRxBegin: " << os.str () << std::endl;
138}
139
140void
142{
143 std::ostringstream os;
144 packet->Print (os);
145 std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().As (Time::S) << "] " << device->GetMac ()->GetShortAddress () << " PhyRxEnd (" << sinr << "): " << os.str () << std::endl;
146
147 // The first packet was received. Now start a CCA, to try to detect the second packet which is still being transmitted.
148 device->GetPhy ()->PlmeCcaRequest ();
149}
150
152{
153 std::ostringstream os;
154 packet->Print (os);
155 std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().As (Time::S) << "] " << device->GetMac ()->GetShortAddress () << " PhyRxDrop: " << os.str () << std::endl;
156}
157
158void
160{
161 // Tx Power: 0 dBm
162 // Receiver Sensitivity: -106.58 dBm
163 // CCA channel busy condition: Rx power > -96.58 dBm
164 // Log distance reference loss at 1 m distance for channel 11 (2405 MHz): 40.0641 dB
165 // Log distance free space path loss exponent: 2
166
167 // Test setup:
168 // Start transmission of a short packet from node 0 to node 1 and at the same
169 // time a transmission of a large packet from node 2 to node 1.
170 // Both transmissions should start without backoff (per configuration) because
171 // the CCA on both nodes should detect a free medium.
172 // The shorter packet will be received first. After reception of the short
173 // packet, which might be destroyed due to interference of the large
174 // packet, node 1 will start a CCA. Depending on the distance between node 1
175 // and node 2, node 1 should detect a busy medium, because the transmission of
176 // the large packet is still in progress. For the above mentioned scenario
177 // parameters, the distance for the CCA detecting a busy medium is about
178 // 669.5685 m.
179
180 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
181 // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
182
183 // Set the random seed and run number for this test
184 RngSeedManager::SetSeed (1);
185 RngSeedManager::SetRun (6);
186
187 // Create 3 nodes, and a NetDevice for each one
188 Ptr<Node> n0 = CreateObject <Node> ();
189 Ptr<Node> n1 = CreateObject <Node> ();
190 Ptr<Node> n2 = CreateObject <Node> ();
191
192 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
193 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
194 Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice> ();
195
196 // Make random variable stream assignment deterministic
197 dev0->AssignStreams (0);
198 dev1->AssignStreams (10);
199 dev2->AssignStreams (20);
200
201 dev0->SetAddress (Mac16Address ("00:01"));
202 dev1->SetAddress (Mac16Address ("00:02"));
203 dev2->SetAddress (Mac16Address ("00:03"));
204
205 // Each device must be attached to the same channel
206 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
207 Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
208 propModel->SetReference (1.0, 40.0641); // Reference loss at 1m distance for 2405 MHz (channel 11)
209 propModel->SetPathLossExponent (2); // Free space path loss exponent
210 Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
211 channel->AddPropagationLossModel (propModel);
212 channel->SetPropagationDelayModel (delayModel);
213
214 dev0->SetChannel (channel);
215 dev1->SetChannel (channel);
216 dev2->SetChannel (channel);
217
218 // To complete configuration, a LrWpanNetDevice must be added to a node
219 n0->AddDevice (dev0);
220 n1->AddDevice (dev1);
221 n2->AddDevice (dev2);
222
223 Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
224 sender0Mobility->SetPosition (Vector (0, 0, 0));
225 dev0->GetPhy ()->SetMobility (sender0Mobility);
226 Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
227 sender1Mobility->SetPosition (Vector (0, 669, 0));
228 dev1->GetPhy ()->SetMobility (sender1Mobility);
229 Ptr<ConstantPositionMobilityModel> sender2Mobility = CreateObject<ConstantPositionMobilityModel> ();
230 sender2Mobility->SetPosition (Vector (0, 1338, 0));
231 dev2->GetPhy ()->SetMobility (sender2Mobility);
232
233 // Disable the NetDevices queue management.
234 dev0->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
235 dev1->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
236 dev2->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
237
238 // Set the CCA confirm callback.
239 dev1->GetPhy ()->SetPlmeCcaConfirmCallback (MakeBoundCallback (&LrWpanCcaTestCase::PlmeCcaConfirm, this, dev1));
240
241 // Start sending without backoff, if the channel is free.
242 dev0->GetCsmaCa ()->SetMacMinBE (0);
243 dev2->GetCsmaCa ()->SetMacMinBE (0);
244
245 // Connect trace sources.
246 dev0->GetPhy ()->TraceConnectWithoutContext ("PhyTxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxBegin, this, dev0));
247 dev0->GetPhy ()->TraceConnectWithoutContext ("PhyTxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxEnd, this, dev0));
248 dev2->GetPhy ()->TraceConnectWithoutContext ("PhyTxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxBegin, this, dev2));
249 dev2->GetPhy ()->TraceConnectWithoutContext ("PhyTxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxEnd, this, dev2));
250 dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxBegin, this, dev1));
251 dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxEnd, this, dev1));
252 dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxDrop", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxDrop, this, dev1));
253
255
256 Ptr<Packet> p0 = Create<Packet> (1); // 1 byte of dummy data
257 McpsDataRequestParams params0;
258 params0.m_srcAddrMode = SHORT_ADDR;
259 params0.m_dstAddrMode = SHORT_ADDR;
260 params0.m_dstPanId = 0;
261 params0.m_dstAddr = Mac16Address ("00:02");
262 params0.m_msduHandle = 0;
263 params0.m_txOptions = TX_OPTION_NONE;
264 Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params0, p0);
265
266 Ptr<Packet> p1 = Create<Packet> (100); // 100 bytes of dummy data
267 McpsDataRequestParams params1;
268 params1.m_srcAddrMode = SHORT_ADDR;
269 params1.m_dstAddrMode = SHORT_ADDR;
270 params1.m_dstPanId = 0;
271 params1.m_dstAddr = Mac16Address ("00:02");
272 params1.m_msduHandle = 0;
273 params1.m_txOptions = TX_OPTION_NONE;
274 Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev2->GetMac (), params1, p1);
275
276 Simulator::Run ();
277
278 NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_BUSY, "CCA status BUSY (as expected)");
279
281
282 sender2Mobility->SetPosition (Vector (0, 1340, 0));
283
284 Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params0, p0);
285 Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev2->GetMac (), params1, p1);
286
287 Simulator::Run ();
288
289 NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_IDLE, "CCA status IDLE (as expected)");
290
291 Simulator::Destroy ();
292}
293
301{
302public:
304};
305
307 : TestSuite ("lr-wpan-clear-channel-assessment", UNIT)
308{
309 AddTestCase (new LrWpanCcaTestCase, TestCase::QUICK);
310}
311
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
static void PhyRxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyRxBegin is hit.
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.
virtual void SetAddress(Address address)
This method indirects to LrWpanMac::SetShortAddress ()
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
Ptr< LrWpanCsmaCa > GetCsmaCa(void) const
Get the CSMA/CA implementation used by this NetDevice.
This class can contain 16 bit addresses.
Definition: mac16-address.h:42
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:434
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:418
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:106
@ IEEE_802_15_4_PHY_BUSY
Definition: lr-wpan-phy.h:107
@ IEEE_802_15_4_PHY_UNSPECIFIED
Definition: lr-wpan-phy.h:119
@ IEEE_802_15_4_PHY_IDLE
Definition: lr-wpan-phy.h:111
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:58
@ SHORT_ADDR
Definition: lr-wpan-mac.h:141
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1709
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
#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:240
static LrWpanCcaTestSuite g_lrWpanCcaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:92
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:237
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:246
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:247
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:248
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:249
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:251
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:252