A Discrete-Event Network Simulator
API
lr-wpan-ed-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Fraunhofer FKIE
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author:
18  * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
19  */
20 
21 #include "ns3/rng-seed-manager.h"
22 #include <ns3/constant-position-mobility-model.h>
23 #include <ns3/core-module.h>
24 #include <ns3/log.h>
25 #include <ns3/lr-wpan-module.h>
26 #include <ns3/packet.h>
27 #include <ns3/propagation-delay-model.h>
28 #include <ns3/propagation-loss-model.h>
29 #include <ns3/simulator.h>
30 #include <ns3/single-model-spectrum-channel.h>
31 
32 #include <iostream>
33 
34 using namespace ns3;
35 
36 NS_LOG_COMPONENT_DEFINE("lr-wpan-energy-detection-test");
37 
44 class LrWpanEdTestCase : public TestCase
45 {
46  public:
48 
49  private:
50  void DoRun() override;
51 
57  void PlmeEdConfirm(LrWpanPhyEnumeration status, uint8_t level);
58 
60  uint8_t m_level;
61 };
62 
64  : TestCase("Test the 802.15.4 energie detection")
65 {
67  m_level = 0;
68 }
69 
70 void
72 {
73  NS_LOG_UNCOND("Energy Detection completed with status "
74  << LrWpanHelper::LrWpanPhyEnumerationPrinter(status) << " and energy level "
75  << static_cast<uint32_t>(level));
76  m_status = status;
77  m_level = level;
78 }
79 
80 void
82 {
83  // Tx Power: 0 dBm
84  // Receiver Sensitivity: -106.58 dBm
85  // Do energy detection for a single packet, arriving with 5 dB, 10 dB, 25 dB,
86  // 40 dB, relative to RX Power / Sensitivity. This should yield 0, 0, 127,
87  // and 255 as the reported energy levels.
88  // TODO: Maybe there should be a test for several interfering packets.
89  // TODO: There should be tests for signals not originating from 802.15.4
90  // devices.
91 
92  // Test setup:
93  // Two nodes in communication range. The propagation model is adjusted to
94  // give us the above mentioned RX powers.
95  // Node 1 sends a packet to node 2. Node 2 starts energy detection, while the
96  // packet reception is in progress. The detected energy level is compared to
97  // the expected values.
98 
99  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
100  // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
101 
102  // Set the random seed and run number for this test
103  RngSeedManager::SetSeed(1);
104  RngSeedManager::SetRun(6);
105 
106  // Create 2 nodes, and a NetDevice for each one
107  Ptr<Node> n0 = CreateObject<Node>();
108  Ptr<Node> n1 = CreateObject<Node>();
109 
110  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
111  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
112 
113  // Make random variable stream assignment deterministic
114  dev0->AssignStreams(0);
115  dev1->AssignStreams(10);
116 
117  dev0->SetAddress(Mac16Address("00:01"));
118  dev1->SetAddress(Mac16Address("00:02"));
119 
120  // Each device must be attached to the same channel
121  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
122  Ptr<FixedRssLossModel> propModel = CreateObject<FixedRssLossModel>();
124  CreateObject<ConstantSpeedPropagationDelayModel>();
125  channel->AddPropagationLossModel(propModel);
126  channel->SetPropagationDelayModel(delayModel);
127 
128  dev0->SetChannel(channel);
129  dev1->SetChannel(channel);
130 
131  // To complete configuration, a LrWpanNetDevice must be added to a node
132  n0->AddDevice(dev0);
133  n1->AddDevice(dev1);
134 
135  Ptr<ConstantPositionMobilityModel> sender0Mobility =
136  CreateObject<ConstantPositionMobilityModel>();
137  sender0Mobility->SetPosition(Vector(0, 0, 0));
138  dev0->GetPhy()->SetMobility(sender0Mobility);
139  Ptr<ConstantPositionMobilityModel> sender1Mobility =
140  CreateObject<ConstantPositionMobilityModel>();
141  // Configure position 10 m distance
142  sender1Mobility->SetPosition(Vector(0, 10, 0));
143  dev1->GetPhy()->SetMobility(sender1Mobility);
144 
145  // Set the ED confirm callback.
146  dev0->GetPhy()->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanEdTestCase::PlmeEdConfirm, this));
147  dev1->GetPhy()->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanEdTestCase::PlmeEdConfirm, this));
148 
149  // Configure the RX Power to be -107.58 dBm, i.e. 1 dB below receiver sensitivity.
150  propModel->SetRss(-107.58);
151 
153  m_level = 0;
154  Ptr<Packet> p0 = Create<Packet>(100); // 100 bytes of dummy data
156  params.m_srcAddrMode = SHORT_ADDR;
157  params.m_dstAddrMode = SHORT_ADDR;
158  params.m_dstPanId = 0;
159  params.m_dstAddr = Mac16Address("00:02");
160  params.m_msduHandle = 0;
161  params.m_txOptions = TX_OPTION_NONE;
162  Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
163 
164  Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
165 
166  Simulator::Run();
167 
168  NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
169  NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
170 
171  // Configure the RX Power to be -106.58 dBm, i.e. exectly to receiver sensitivity.
172  propModel->SetRss(-106.58);
173 
175  m_level = 0;
176  Ptr<Packet> p1 = Create<Packet>(100); // 100 bytes of dummy data
177  params.m_srcAddrMode = SHORT_ADDR;
178  params.m_dstAddrMode = SHORT_ADDR;
179  params.m_dstPanId = 0;
180  params.m_dstAddr = Mac16Address("00:02");
181  params.m_msduHandle = 0;
182  params.m_txOptions = TX_OPTION_NONE;
183  Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p1);
184 
185  Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
186 
187  Simulator::Run();
188 
189  NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
190  NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
191 
192  // Configure the RX Power to be -81.58 dBm, i.e. 25 dB above receiver sensitivity.
193  propModel->SetRss(-81.58);
194 
196  m_level = 0;
197  Ptr<Packet> p2 = Create<Packet>(100); // 100 bytes of dummy data
198  params.m_srcAddrMode = SHORT_ADDR;
199  params.m_dstAddrMode = SHORT_ADDR;
200  params.m_dstPanId = 0;
201  params.m_dstAddr = Mac16Address("00:02");
202  params.m_msduHandle = 0;
203  params.m_txOptions = TX_OPTION_NONE;
204  Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p2);
205 
206  Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
207 
208  Simulator::Run();
209 
210  NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
211  NS_TEST_EXPECT_MSG_EQ(m_level, 127, "ED reported signal level 127 (as expected)");
212 
213  // Configure the RX Power to be -66.58 dBm, i.e. 40 dB above receiver sensitivity.
214  propModel->SetRss(-66.58);
215 
217  m_level = 0;
218  Ptr<Packet> p3 = Create<Packet>(100); // 100 bytes of dummy data
219  params.m_srcAddrMode = SHORT_ADDR;
220  params.m_dstAddrMode = SHORT_ADDR;
221  params.m_dstPanId = 0;
222  params.m_dstAddr = Mac16Address("00:02");
223  params.m_msduHandle = 0;
224  params.m_txOptions = TX_OPTION_NONE;
225  Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p3);
226 
227  Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
228 
229  Simulator::Run();
230 
231  NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
232  NS_TEST_EXPECT_MSG_EQ(m_level, 255, "ED reported signal level 255 (as expected)");
233 
234  // Test ED at sender.
236  m_level = 0;
237  Ptr<Packet> p4 = Create<Packet>(100); // 100 bytes of dummy data
238  params.m_srcAddrMode = SHORT_ADDR;
239  params.m_dstAddrMode = SHORT_ADDR;
240  params.m_dstPanId = 0;
241  params.m_dstAddr = Mac16Address("00:02");
242  params.m_msduHandle = 0;
243  params.m_txOptions = TX_OPTION_NONE;
244  Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p4);
245 
246  Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev0->GetPhy());
247 
248  Simulator::Run();
249 
250  NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_TX_ON, "ED status TX_ON (as expected)");
251  NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
252 
253  Simulator::Destroy();
254 }
255 
263 {
264  public:
266 };
267 
269  : TestSuite("lr-wpan-energy-detection", UNIT)
270 {
271  AddTestCase(new LrWpanEdTestCase, TestCase::QUICK);
272 }
273 
LrWpan Energy Detection Test.
void DoRun() override
Implementation to actually run this TestCase.
LrWpanPhyEnumeration m_status
PHY status.
uint8_t m_level
ED level.
void PlmeEdConfirm(LrWpanPhyEnumeration status, uint8_t level)
Function called when PlmeEdConfirm is hit.
LrWpan Energy Detection TestSuite.
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:111
@ IEEE_802_15_4_PHY_SUCCESS
Definition: lr-wpan-phy.h:119
@ IEEE_802_15_4_PHY_UNSPECIFIED
Definition: lr-wpan-phy.h:124
@ IEEE_802_15_4_PHY_TX_ON
Definition: lr-wpan-phy.h:121
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:60
@ SHORT_ADDR
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static LrWpanEdTestSuite g_lrWpanEdTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
channel
Definition: third.py:88
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
MCPS-DATA.request params.