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 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE ("lr-wpan-clear-channel-assessment-test");
41 
43 {
44 public:
46 
47 private:
48  static void PlmeCcaConfirm (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, LrWpanPhyEnumeration status);
49  static void PhyTxBegin (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
50  static void PhyTxEnd (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
51  static void PhyRxBegin (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
52  static void PhyRxEnd (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet, double sinr);
53  static void PhyRxDrop (LrWpanCcaTestCase *testcase, Ptr<LrWpanNetDevice> device, Ptr<const Packet> packet);
54 
55  virtual void DoRun (void);
56 
58 
59 };
60 
62  : TestCase ("Test the 802.15.4 clear channel assessment")
63 {
65 }
66 
67 void
69 {
70  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PlmeCcaConfirm: " << LrWpanHelper::LrWpanPhyEnumerationPrinter (status) << std::endl;
71 
72  testcase->m_status = status;
73 }
74 
75 void
77 {
78  std::ostringstream os;
79  packet->Print (os);
80  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyTxBegin: " << os.str () << std::endl;
81 }
82 
83 void
85 {
86  std::ostringstream os;
87  packet->Print (os);
88  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyTxEnd: " << os.str () << std::endl;
89 }
90 
91 void
93 {
94  std::ostringstream os;
95  packet->Print (os);
96  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyRxBegin: " << os.str () << std::endl;
97 }
98 
99 void
101 {
102  std::ostringstream os;
103  packet->Print (os);
104  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyRxEnd (" << sinr << "): " << os.str () << std::endl;
105 
106  // The first packet was received. Now start a CCA, to try to detect the second packet which is still being transmitted.
107  device->GetPhy ()->PlmeCcaRequest ();
108 }
109 
111 {
112  std::ostringstream os;
113  packet->Print (os);
114  std::cout << std::setiosflags (std::ios::fixed) << std::setprecision (9) << "[" << Simulator::Now ().GetSeconds () << "] " << device->GetMac ()->GetShortAddress () << " PhyRxDrop: " << os.str () << std::endl;
115 }
116 
117 void
119 {
120  // Tx Power: 0 dBm
121  // Receiver Sensitivity: -106.58 dBm
122  // CCA channel busy condition: Rx power > -96.58 dBm
123  // Log distance reference loss at 1 m distance for channel 11 (2405 MHz): 40.0641 dB
124  // Log distance free space path loss exponent: 2
125 
126  // Test setup:
127  // Start transmission of a short packet from node 0 to node 1 and at the same
128  // time a transmission of a large packet from node 2 to node 1.
129  // Both transmissions should start without backoff (per configuration) because
130  // the CCA on both nodes should detect a free medium.
131  // The shorter packet will be received first. After reception of the short
132  // packet, which might be destroyed due to interference of the large
133  // packet, node 1 will start a CCA. Depending on the distance between node 1
134  // and node 2, node 1 should detect a busy medium, because the transmission of
135  // the large packet is still in progress. For the above mentioned scenario
136  // parameters, the distance for the CCA detecting a busy medium is about
137  // 669.5685 m.
138 
139  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
140  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
141 
142  // Set the random seed and run number for this test
143  RngSeedManager::SetSeed (1);
144  RngSeedManager::SetRun (6);
145 
146  // Create 3 nodes, and a NetDevice for each one
147  Ptr<Node> n0 = CreateObject <Node> ();
148  Ptr<Node> n1 = CreateObject <Node> ();
149  Ptr<Node> n2 = CreateObject <Node> ();
150 
151  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
152  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
153  Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice> ();
154 
155  // Make random variable stream assignment deterministic
156  dev0->AssignStreams (0);
157  dev1->AssignStreams (10);
158  dev2->AssignStreams (20);
159 
160  dev0->SetAddress (Mac16Address ("00:01"));
161  dev1->SetAddress (Mac16Address ("00:02"));
162  dev2->SetAddress (Mac16Address ("00:03"));
163 
164  // Each device must be attached to the same channel
165  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
166  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
167  propModel->SetReference (1.0, 40.0641); // Reference loss at 1m distance for 2405 MHz (channel 11)
168  propModel->SetPathLossExponent (2); // Free space path loss exponent
169  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
170  channel->AddPropagationLossModel (propModel);
171  channel->SetPropagationDelayModel (delayModel);
172 
173  dev0->SetChannel (channel);
174  dev1->SetChannel (channel);
175  dev2->SetChannel (channel);
176 
177  // To complete configuration, a LrWpanNetDevice must be added to a node
178  n0->AddDevice (dev0);
179  n1->AddDevice (dev1);
180  n2->AddDevice (dev2);
181 
182  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
183  sender0Mobility->SetPosition (Vector (0, 0, 0));
184  dev0->GetPhy ()->SetMobility (sender0Mobility);
185  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
186  sender1Mobility->SetPosition (Vector (0, 669, 0));
187  dev1->GetPhy ()->SetMobility (sender1Mobility);
188  Ptr<ConstantPositionMobilityModel> sender2Mobility = CreateObject<ConstantPositionMobilityModel> ();
189  sender2Mobility->SetPosition (Vector (0, 1338, 0));
190  dev2->GetPhy ()->SetMobility (sender2Mobility);
191 
192  // Disable the NetDevices queue management.
193  dev0->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
194  dev1->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
195  dev2->GetMac ()->SetMcpsDataConfirmCallback (MakeNullCallback<void, McpsDataConfirmParams> ());
196 
197  // Set the CCA confirm callback.
198  dev1->GetPhy ()->SetPlmeCcaConfirmCallback (MakeBoundCallback (&LrWpanCcaTestCase::PlmeCcaConfirm, this, dev1));
199 
200  // Start sending without backoff, if the channel is free.
201  dev0->GetCsmaCa ()->SetMacMinBE (0);
202  dev2->GetCsmaCa ()->SetMacMinBE (0);
203 
204  // Connect trace sources.
205  dev0->GetPhy ()->TraceConnectWithoutContext ("PhyTxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxBegin, this, dev0));
206  dev0->GetPhy ()->TraceConnectWithoutContext ("PhyTxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxEnd, this, dev0));
207  dev2->GetPhy ()->TraceConnectWithoutContext ("PhyTxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxBegin, this, dev2));
208  dev2->GetPhy ()->TraceConnectWithoutContext ("PhyTxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyTxEnd, this, dev2));
209  dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxBegin", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxBegin, this, dev1));
210  dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxEnd", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxEnd, this, dev1));
211  dev1->GetPhy ()->TraceConnectWithoutContext ("PhyRxDrop", MakeBoundCallback (&LrWpanCcaTestCase::PhyRxDrop, this, dev1));
212 
214 
215  Ptr<Packet> p0 = Create<Packet> (1); // 1 byte of dummy data
216  McpsDataRequestParams params0;
217  params0.m_srcAddrMode = SHORT_ADDR;
218  params0.m_dstAddrMode = SHORT_ADDR;
219  params0.m_dstPanId = 0;
220  params0.m_dstAddr = Mac16Address ("00:02");
221  params0.m_msduHandle = 0;
222  params0.m_txOptions = TX_OPTION_NONE;
223  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params0, p0);
224 
225  Ptr<Packet> p1 = Create<Packet> (100); // 100 bytes of dummy data
226  McpsDataRequestParams params1;
227  params1.m_srcAddrMode = SHORT_ADDR;
228  params1.m_dstAddrMode = SHORT_ADDR;
229  params1.m_dstPanId = 0;
230  params1.m_dstAddr = Mac16Address ("00:02");
231  params1.m_msduHandle = 0;
232  params1.m_txOptions = TX_OPTION_NONE;
233  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev2->GetMac (), params1, p1);
234 
235  Simulator::Run ();
236 
237  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_BUSY, "CCA status BUSY (as expected)");
238 
240 
241  sender2Mobility->SetPosition (Vector (0, 1340, 0));
242 
243  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params0, p0);
244  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev2->GetMac (), params1, p1);
245 
246  Simulator::Run ();
247 
248  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_IDLE, "CCA status IDLE (as expected)");
249 
250  Simulator::Destroy ();
251 }
252 
254 {
255 public:
257 };
258 
260  : TestSuite ("lr-wpan-clear-channel-assessment", UNIT)
261 {
262  AddTestCase (new LrWpanCcaTestCase, TestCase::QUICK);
263 }
264 
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)
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)
A suite of tests to run.
Definition: test.h:1333
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:278
virtual void DoRun(void)
Implementation to actually run this TestCase.
encapsulates test code
Definition: test.h:1147
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Set the single-frequency propagation loss model to be used.
static void PhyRxBegin(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:412
static LrWpanCcaTestSuite g_lrWpanCcaTestSuite
static void PhyRxDrop(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
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, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
LrWpanPhyEnumeration m_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.
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)
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:340
static void PhyTxEnd(LrWpanCcaTestCase *testcase, Ptr< LrWpanNetDevice > device, Ptr< const Packet > packet)
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.