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:
52 
59  static void PlmeCcaConfirm (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, LrWpanPhyEnumeration status);
66  static void PhyTxBegin (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
73  static void PhyTxEnd (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
80  static void PhyRxBegin (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
88  static void PhyRxEnd (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet, double sinr);
95  static void PhyRxDrop (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
96 
97  virtual void DoRun (void);
98 
100 
101 };
102 
104  : TestCase ("Test the 802.15.4 clear channel assessment")
105 {
107 }
108 
109 void
111 {
112  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PlmeCcaConfirm: " << LrWpanHelper::LrWpanPhyEnumerationPrinter (status) << std::endl;
113 
114  testcase->m_status = status;
115 }
116 
117 void
119 {
120  std::ostringstream os;
121  packet->Print (os);
122  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyTxBegin: " << os.str () << std::endl;
123 }
124 
125 void
127 {
128  std::ostringstream os;
129  packet->Print (os);
130  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyTxEnd: " << os.str () << std::endl;
131 }
132 
133 void
135 {
136  std::ostringstream os;
137  packet->Print (os);
138  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyRxBegin: " << os.str () << std::endl;
139 }
140 
141 void
143 {
144  std::ostringstream os;
145  packet->Print (os);
146  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyRxEnd (" << sinr << "): " << os.str () << std::endl;
147 
148  // The first packet was received. Now start a CCA, to try to detect the second packet which is still being transmitted.
149  device->GetPhy ()->PlmeCcaRequest ();
150 }
151 
153 {
154  std::ostringstream os;
155  packet->Print (os);
156  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyRxDrop: " << os.str () << std::endl;
157 }
158 
159 void
161 {
162  // Tx Power: 0 dBm
163  // Receiver Sensitivity: -106.58 dBm
164  // CCA channel busy condition: Rx power > -96.58 dBm
165  // Log distance reference loss at 1 m distance for channel 11 (2405 MHz): 40.0641 dB
166  // Log distance free space path loss exponent: 2
167 
168  // Test setup:
169  // Start transmission of a short packet from node 0 to node 1 and at the same
170  // time a transmission of a large packet from node 2 to node 1.
171  // Both transmissions should start without backoff (per configuration) because
172  // the CCA on both nodes should detect a free medium.
173  // The shorter packet will be received first. After reception of the short
174  // packet, which might be destroyed due to interference of the large
175  // packet, node 1 will start a CCA. Depending on the distance between node 1
176  // and node 2, node 1 should detect a busy medium, because the transmission of
177  // the large packet is still in progress. For the above mentioned scenario
178  // parameters, the distance for the CCA detecting a busy medium is about
179  // 669.5685 m.
180 
181  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
182  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
183 
184  // Set the random seed and run number for this test
185  RngSeedManager::SetSeed (1);
186  RngSeedManager::SetRun (6);
187 
188  // Create 3 nodes, and a NetDevice for each one
189  Ptr<Node> n0 = CreateObject <Node> ();
190  Ptr<Node> n1 = CreateObject <Node> ();
191  Ptr<Node> n2 = CreateObject <Node> ();
192 
193  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
194  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
195  Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice> ();
196 
197  // Make random variable stream assignment deterministic
198  dev0->AssignStreams (0);
199  dev1->AssignStreams (10);
200  dev2->AssignStreams (20);
201 
202  dev0->SetAddress (Mac16Address ("00:01"));
203  dev1->SetAddress (Mac16Address ("00:02"));
204  dev2->SetAddress (Mac16Address ("00:03"));
205 
206  // Each device must be attached to the same channel
207  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
208  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
209  propModel->SetReference (1.0, 40.0641); // Reference loss at 1m distance for 2405 MHz (channel 11)
210  propModel->SetPathLossExponent (2); // Free space path loss exponent
211  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
212  channel->AddPropagationLossModel (propModel);
213  channel->SetPropagationDelayModel (delayModel);
214 
215  dev0->SetChannel (channel);
216  dev1->SetChannel (channel);
217  dev2->SetChannel (channel);
218 
219  // To complete configuration, a LrWpanNetDevice must be added to a node
220  n0->AddDevice (dev0);
221  n1->AddDevice (dev1);
222  n2->AddDevice (dev2);
223 
224  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
225  sender0Mobility->SetPosition (Vector (0, 0, 0));
226  dev0->GetPhy ()->SetMobility (sender0Mobility);
227  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
228  sender1Mobility->SetPosition (Vector (0, 669, 0));
229  dev1->GetPhy ()->SetMobility (sender1Mobility);
230  Ptr<ConstantPositionMobilityModel> sender2Mobility = CreateObject<ConstantPositionMobilityModel> ();
231  sender2Mobility->SetPosition (Vector (0, 1338, 0));
232  dev2->GetPhy ()->SetMobility (sender2Mobility);
233 
234  // Disable the NetDevices queue management.
235  dev0->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
236  dev1->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
237  dev2->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
238 
239  // Set the CCA confirm callback.
240  dev1->GetPhy ()->SetPlmeCcaConfirmCallback (MakeBoundCallback (&LrWpanCcaTestCase::PlmeCcaConfirm, this, dev1));
241 
242  // Start sending without backoff, if the channel is free.
243  dev0->GetCsmaCa ()->SetMacMinBE (0);
244  dev2->GetCsmaCa ()->SetMacMinBE (0);
245 
246  // Connect trace sources.
247  dev0->GetPhy ()->TraceConnectWithoutContext ("PhyTxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxBegin, this, dev0));
248  dev0->GetPhy ()->TraceConnectWithoutContext ("PhyTxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxEnd, this, dev0));
249  dev2->GetPhy ()->TraceConnectWithoutContext ("PhyTxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxBegin, this, dev2));
250  dev2->GetPhy ()->TraceConnectWithoutContext ("PhyTxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxEnd, this, dev2));
251  dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxBegin, this, dev1));
252  dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxEnd, this, dev1));
253  dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxDrop", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxDrop, this, dev1));
254 
256 
257  Ptr<Packet> p0 = Create<Packet> (1); // 1 byte of dummy data
258  McpsDataRequestParams params0;
259  params0.m_srcAddrMode = SHORT_ADDR;
260  params0.m_dstAddrMode = SHORT_ADDR;
261  params0.m_dstPanId = 0;
262  params0.m_dstAddr = Mac16Address ("00:02");
263  params0.m_msduHandle = 0;
264  params0.m_txOptions = TX_OPTION_NONE;
265  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params0, p0);
266 
267  Ptr<Packet> p1 = Create<Packet> (100); // 100 bytes of dummy data
268  McpsDataRequestParams params1;
269  params1.m_srcAddrMode = SHORT_ADDR;
270  params1.m_dstAddrMode = SHORT_ADDR;
271  params1.m_dstPanId = 0;
272  params1.m_dstAddr = Mac16Address ("00:02");
273  params1.m_msduHandle = 0;
274  params1.m_txOptions = TX_OPTION_NONE;
275  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev2->GetMac (), params1, p1);
276 
277  Simulator::Run ();
278 
279  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_BUSY, "CCA status BUSY (as expected)");
280 
282 
283  sender2Mobility->SetPosition (Vector (0, 1340, 0));
284 
285  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params0, p0);
286  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev2->GetMac (), params1, p1);
287 
288  Simulator::Run ();
289 
290  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_IDLE, "CCA status IDLE (as expected)");
291 
292  Simulator::Destroy ();
293 }
294 
302 {
303 public:
305 };
306 
308  : TestSuite ("lr-wpan-clear-channel-assessment", UNIT)
309 {
310  AddTestCase (new LrWpanCcaTestCase, TestCase::QUICK);
311 }
312 
tuple channel
Definition: third.py:85
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
static void PhyTxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyTxBegin is hit.
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
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:1342
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1686
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#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:285
virtual void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:1155
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Add the single-frequency propagation loss model to be used.
static void PhyRxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
Function called when PhyRxBegin is hit.
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:411
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.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
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:157
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:128
virtual void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
Set the propagation delay model to be used.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:365
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:146
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:56
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.