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 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE ("lr-wpan-clear-channel-assessment-test");
39 
47 {
48 public:
50 
51 private:
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 
108 void
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 
116 void
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 
124 void
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 
132 void
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 
140 void
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 
158 void
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 {
302 public:
304 };
305 
307  : TestSuite ("lr-wpan-clear-channel-assessment", UNIT)
308 {
309  AddTestCase (new LrWpanCcaTestCase, TestCase::QUICK);
310 }
311 
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:429
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:434
static void PhyTxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyTxBegin is hit.
static void PhyRxEnd(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet, double sinr)
Function called when PhyRxEnd is hit.
A suite of tests to run.
Definition: test.h:1343
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1703
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:283
virtual void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:1153
static void PhyRxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyRxBegin is hit.
LrWpan ACK TestSuite.
static LrWpanCcaTestSuite g_lrWpanCcaTestSuite
Static variable for test initialization.
static void PhyRxDrop(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyRxDrop is hit.
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
channel
Definition: third.py:92
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
LrWpanPhyEnumeration m_status
PHY status.
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:246
Every class exported by the ns3 library is enclosed in the ns3 namespace.
LrWpan CCA Test.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
void SetPosition(const Vector &position)
static void PlmeCcaConfirm(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, LrWpanPhyEnumeration status)
Function called when PlmeCcaConfirm is hit.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
static void PhyTxEnd(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyTxEnd is hit.
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:105
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:236
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:58