A Discrete-Event Network Simulator
API
lr-wpan-ed-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 
35 using namespace ns3;
36 
37 NS_LOG_COMPONENT_DEFINE ("lr-wpan-energy-detection-test");
38 
45 class LrWpanEdTestCase : public TestCase
46 {
47 public:
49 
50 private:
51  virtual void DoRun (void);
52 
58  void PlmeEdConfirm (LrWpanPhyEnumeration status, uint8_t level);
59 
61  uint8_t m_level;
62 };
63 
65  : TestCase ("Test the 802.15.4 energie detection")
66 {
68  m_level = 0;
69 }
70 
71 void
73 {
74  NS_LOG_UNCOND ("Energy Detection completed with status " << LrWpanHelper::LrWpanPhyEnumerationPrinter (status) << " and energy level " << static_cast<uint32_t> (level));
75  m_status = status;
76  m_level = level;
77 }
78 
79 void
81 {
82  // Tx Power: 0 dBm
83  // Receiver Sensitivity: -106.58 dBm
84  // Do energy detection for a single packet, arriving with 5 dB, 10 dB, 25 dB,
85  // 40 dB, relative to RX Power / Sensitivity. This should yield 0, 0, 127,
86  // and 255 as the reported energy levels.
87  // TODO: Maybe there should be a test for several interfering packets.
88  // TODO: There should be tests for signals not originating from 802.15.4
89  // devices.
90 
91  // Test setup:
92  // Two nodes in communication range. The propagation model is adjusted to
93  // give us the above mentioned RX powers.
94  // Node 1 sends a packet to node 2. Node 2 starts energy detection, while the
95  // packet reception is in progress. The detected energy level is compared to
96  // the expected values.
97 
98  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
99  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
100 
101  // Set the random seed and run number for this test
102  RngSeedManager::SetSeed (1);
103  RngSeedManager::SetRun (6);
104 
105  // Create 2 nodes, and a NetDevice for each one
106  Ptr<Node> n0 = CreateObject <Node> ();
107  Ptr<Node> n1 = CreateObject <Node> ();
108 
109  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
110  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
111 
112  // Make random variable stream assignment deterministic
113  dev0->AssignStreams (0);
114  dev1->AssignStreams (10);
115 
116  dev0->SetAddress (Mac16Address ("00:01"));
117  dev1->SetAddress (Mac16Address ("00:02"));
118 
119  // Each device must be attached to the same channel
120  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
121  Ptr<FixedRssLossModel> propModel = CreateObject<FixedRssLossModel> ();
122  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
123  channel->AddPropagationLossModel (propModel);
124  channel->SetPropagationDelayModel (delayModel);
125 
126  dev0->SetChannel (channel);
127  dev1->SetChannel (channel);
128 
129  // To complete configuration, a LrWpanNetDevice must be added to a node
130  n0->AddDevice (dev0);
131  n1->AddDevice (dev1);
132 
133  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
134  sender0Mobility->SetPosition (Vector (0, 0, 0));
135  dev0->GetPhy ()->SetMobility (sender0Mobility);
136  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
137  // Configure position 10 m distance
138  sender1Mobility->SetPosition (Vector (0, 10, 0));
139  dev1->GetPhy ()->SetMobility (sender1Mobility);
140 
141  // Set the ED confirm callback.
142  dev0->GetPhy ()->SetPlmeEdConfirmCallback (MakeCallback (&LrWpanEdTestCase::PlmeEdConfirm, this));
143  dev1->GetPhy ()->SetPlmeEdConfirmCallback (MakeCallback (&LrWpanEdTestCase::PlmeEdConfirm, this));
144 
145  // Configure the RX Power to be -107.58 dBm, i.e. 1 dB below receiver sensitivity.
146  propModel->SetRss (-107.58);
147 
149  m_level = 0;
150  Ptr<Packet> p0 = Create<Packet> (100); // 100 bytes of dummy data
151  McpsDataRequestParams params;
152  params.m_srcAddrMode = SHORT_ADDR;
153  params.m_dstAddrMode = SHORT_ADDR;
154  params.m_dstPanId = 0;
155  params.m_dstAddr = Mac16Address ("00:02");
156  params.m_msduHandle = 0;
157  params.m_txOptions = TX_OPTION_NONE;
158  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p0);
159 
160  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy ());
161 
162  Simulator::Run ();
163 
164  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
165  NS_TEST_EXPECT_MSG_EQ (m_level, 0, "ED reported signal level 0 (as expected)");
166 
167 
168  // Configure the RX Power to be -106.58 dBm, i.e. exectly to receiver sensitivity.
169  propModel->SetRss (-106.58);
170 
172  m_level = 0;
173  Ptr<Packet> p1 = Create<Packet> (100); // 100 bytes of dummy data
174  params.m_srcAddrMode = SHORT_ADDR;
175  params.m_dstAddrMode = SHORT_ADDR;
176  params.m_dstPanId = 0;
177  params.m_dstAddr = Mac16Address ("00:02");
178  params.m_msduHandle = 0;
179  params.m_txOptions = TX_OPTION_NONE;
180  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p1);
181 
182  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy ());
183 
184  Simulator::Run ();
185 
186  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
187  NS_TEST_EXPECT_MSG_EQ (m_level, 0, "ED reported signal level 0 (as expected)");
188 
189 
190  // Configure the RX Power to be -81.58 dBm, i.e. 25 dB above receiver sensitivity.
191  propModel->SetRss (-81.58);
192 
194  m_level = 0;
195  Ptr<Packet> p2 = Create<Packet> (100); // 100 bytes of dummy data
196  params.m_srcAddrMode = SHORT_ADDR;
197  params.m_dstAddrMode = SHORT_ADDR;
198  params.m_dstPanId = 0;
199  params.m_dstAddr = Mac16Address ("00:02");
200  params.m_msduHandle = 0;
201  params.m_txOptions = TX_OPTION_NONE;
202  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p2);
203 
204  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy ());
205 
206  Simulator::Run ();
207 
208  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
209  NS_TEST_EXPECT_MSG_EQ (m_level, 127, "ED reported signal level 127 (as expected)");
210 
211 
212  // Configure the RX Power to be -66.58 dBm, i.e. 40 dB above receiver sensitivity.
213  propModel->SetRss (-66.58);
214 
216  m_level = 0;
217  Ptr<Packet> p3 = Create<Packet> (100); // 100 bytes of dummy data
218  params.m_srcAddrMode = SHORT_ADDR;
219  params.m_dstAddrMode = SHORT_ADDR;
220  params.m_dstPanId = 0;
221  params.m_dstAddr = Mac16Address ("00:02");
222  params.m_msduHandle = 0;
223  params.m_txOptions = TX_OPTION_NONE;
224  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p3);
225 
226  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy ());
227 
228  Simulator::Run ();
229 
230  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
231  NS_TEST_EXPECT_MSG_EQ (m_level, 255, "ED reported signal level 255 (as expected)");
232 
233  // Test ED at sender.
235  m_level = 0;
236  Ptr<Packet> p4 = Create<Packet> (100); // 100 bytes of dummy data
237  params.m_srcAddrMode = SHORT_ADDR;
238  params.m_dstAddrMode = SHORT_ADDR;
239  params.m_dstPanId = 0;
240  params.m_dstAddr = Mac16Address ("00:02");
241  params.m_msduHandle = 0;
242  params.m_txOptions = TX_OPTION_NONE;
243  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p4);
244 
245  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev0->GetPhy ());
246 
247  Simulator::Run ();
248 
249  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_TX_ON, "ED status TX_ON (as expected)");
250  NS_TEST_EXPECT_MSG_EQ (m_level, 0, "ED reported signal level 0 (as expected)");
251 
252  Simulator::Destroy ();
253 }
254 
262 {
263 public:
265 };
266 
268  : TestSuite ("lr-wpan-energy-detection", UNIT)
269 {
270  AddTestCase (new LrWpanEdTestCase, TestCase::QUICK);
271 }
272 
tuple channel
Definition: third.py:85
uint8_t m_level
ED level.
static LrWpanEdTestSuite g_lrWpanEdTestSuite
Static variable for test initialization.
A suite of tests to run.
Definition: test.h:1342
LrWpan Energy Detection Test.
#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
encapsulates test code
Definition: test.h:1155
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Add the single-frequency propagation loss model to be used.
LrWpan Energy Detection TestSuite.
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.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionaly.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
void SetPosition(const Vector &position)
void PlmeEdConfirm(LrWpanPhyEnumeration status, uint8_t level)
Function called when PlmeEdConfirm is hit.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
virtual void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
Set the propagation delay model to be used.
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