A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
34using namespace ns3;
35using namespace ns3::lrwpan;
36
37NS_LOG_COMPONENT_DEFINE("lr-wpan-energy-detection-test");
38
39/**
40 * \ingroup lr-wpan-test
41 * \ingroup tests
42 *
43 * \brief LrWpan Energy Detection Test
44 */
46{
47 public:
49
50 private:
51 void DoRun() override;
52
53 /**
54 * \brief Function called when PlmeEdConfirm is hit.
55 * \param status The PHY status.
56 * \param level The ED level.
57 */
58 void PlmeEdConfirm(PhyEnumeration status, uint8_t level);
59
60 PhyEnumeration m_status; //!< PHY status.
61 uint8_t m_level; //!< ED level.
62};
63
65 : TestCase("Test the 802.15.4 energie detection")
66{
68 m_level = 0;
69}
70
71void
73{
74 NS_LOG_UNCOND("Energy Detection completed with status "
75 << LrWpanHelper::LrWpanPhyEnumerationPrinter(status) << " and energy level "
76 << static_cast<uint32_t>(level));
77 m_status = status;
78 m_level = level;
79}
80
81void
83{
84 // Tx Power: 0 dBm
85 // Receiver Sensitivity: -106.58 dBm
86 // Do energy detection for a single packet, arriving with 5 dB, 10 dB, 25 dB,
87 // 40 dB, relative to RX Power / Sensitivity. This should yield 0, 0, 127,
88 // and 255 as the reported energy levels.
89 // TODO: Maybe there should be a test for several interfering packets.
90 // TODO: There should be tests for signals not originating from 802.15.4
91 // devices.
92
93 // Test setup:
94 // Two nodes in communication range. The propagation model is adjusted to
95 // give us the above mentioned RX powers.
96 // Node 1 sends a packet to node 2. Node 2 starts energy detection, while the
97 // packet reception is in progress. The detected energy level is compared to
98 // the expected values.
99
100 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
101 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
102
103 // Set the random seed and run number for this test
106
107 // Create 2 nodes, and a NetDevice for each one
108 Ptr<Node> n0 = CreateObject<Node>();
109 Ptr<Node> n1 = CreateObject<Node>();
110
111 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
112 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
113
114 // Make random variable stream assignment deterministic
115 dev0->AssignStreams(0);
116 dev1->AssignStreams(10);
117
118 dev0->SetAddress(Mac16Address("00:01"));
119 dev1->SetAddress(Mac16Address("00:02"));
120
121 // Each device must be attached to the same channel
122 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
123 Ptr<FixedRssLossModel> propModel = CreateObject<FixedRssLossModel>();
125 CreateObject<ConstantSpeedPropagationDelayModel>();
126 channel->AddPropagationLossModel(propModel);
127 channel->SetPropagationDelayModel(delayModel);
128
129 dev0->SetChannel(channel);
130 dev1->SetChannel(channel);
131
132 // To complete configuration, a LrWpanNetDevice must be added to a node
133 n0->AddDevice(dev0);
134 n1->AddDevice(dev1);
135
136 Ptr<ConstantPositionMobilityModel> sender0Mobility =
137 CreateObject<ConstantPositionMobilityModel>();
138 sender0Mobility->SetPosition(Vector(0, 0, 0));
139 dev0->GetPhy()->SetMobility(sender0Mobility);
140 Ptr<ConstantPositionMobilityModel> sender1Mobility =
141 CreateObject<ConstantPositionMobilityModel>();
142 // Configure position 10 m distance
143 sender1Mobility->SetPosition(Vector(0, 10, 0));
144 dev1->GetPhy()->SetMobility(sender1Mobility);
145
146 // Set the ED confirm callback.
147 dev0->GetPhy()->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanEdTestCase::PlmeEdConfirm, this));
148 dev1->GetPhy()->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanEdTestCase::PlmeEdConfirm, this));
149
150 // Configure the RX Power to be -107.58 dBm, i.e. 1 dB below receiver sensitivity.
151 propModel->SetRss(-107.58);
152
154 m_level = 0;
155 Ptr<Packet> p0 = Create<Packet>(100); // 100 bytes of dummy data
157 params.m_srcAddrMode = SHORT_ADDR;
158 params.m_dstAddrMode = SHORT_ADDR;
159 params.m_dstPanId = 0;
160 params.m_dstAddr = Mac16Address("00:02");
161 params.m_msduHandle = 0;
162 params.m_txOptions = TX_OPTION_NONE;
163 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
164
165 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
166
168
169 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
170 NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
171
172 // Configure the RX Power to be -106.58 dBm, i.e. exactly to receiver sensitivity.
173 propModel->SetRss(-106.58);
174
176 m_level = 0;
177 Ptr<Packet> p1 = Create<Packet>(100); // 100 bytes of dummy data
178 params.m_srcAddrMode = SHORT_ADDR;
179 params.m_dstAddrMode = SHORT_ADDR;
180 params.m_dstPanId = 0;
181 params.m_dstAddr = Mac16Address("00:02");
182 params.m_msduHandle = 0;
183 params.m_txOptions = TX_OPTION_NONE;
184 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p1);
185
186 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
187
189
190 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
191 NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
192
193 // Configure the RX Power to be -81.58 dBm, i.e. 25 dB above receiver sensitivity.
194 propModel->SetRss(-81.58);
195
197 m_level = 0;
198 Ptr<Packet> p2 = Create<Packet>(100); // 100 bytes of dummy data
199 params.m_srcAddrMode = SHORT_ADDR;
200 params.m_dstAddrMode = SHORT_ADDR;
201 params.m_dstPanId = 0;
202 params.m_dstAddr = Mac16Address("00:02");
203 params.m_msduHandle = 0;
204 params.m_txOptions = TX_OPTION_NONE;
205 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p2);
206
207 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
208
210
211 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
212 NS_TEST_EXPECT_MSG_EQ(m_level, 127, "ED reported signal level 127 (as expected)");
213
214 // Configure the RX Power to be -66.58 dBm, i.e. 40 dB above receiver sensitivity.
215 propModel->SetRss(-66.58);
216
218 m_level = 0;
219 Ptr<Packet> p3 = Create<Packet>(100); // 100 bytes of dummy data
220 params.m_srcAddrMode = SHORT_ADDR;
221 params.m_dstAddrMode = SHORT_ADDR;
222 params.m_dstPanId = 0;
223 params.m_dstAddr = Mac16Address("00:02");
224 params.m_msduHandle = 0;
225 params.m_txOptions = TX_OPTION_NONE;
226 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p3);
227
228 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
229
231
232 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
233 NS_TEST_EXPECT_MSG_EQ(m_level, 255, "ED reported signal level 255 (as expected)");
234
235 // Test ED at sender.
237 m_level = 0;
238 Ptr<Packet> p4 = Create<Packet>(100); // 100 bytes of dummy data
239 params.m_srcAddrMode = SHORT_ADDR;
240 params.m_dstAddrMode = SHORT_ADDR;
241 params.m_dstPanId = 0;
242 params.m_dstAddr = Mac16Address("00:02");
243 params.m_msduHandle = 0;
244 params.m_txOptions = TX_OPTION_NONE;
245 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p4);
246
247 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev0->GetPhy());
248
250
251 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_TX_ON, "ED status TX_ON (as expected)");
252 NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
253
255}
256
257/**
258 * \ingroup lr-wpan-test
259 * \ingroup tests
260 *
261 * \brief LrWpan Energy Detection TestSuite
262 */
264{
265 public:
267};
268
270 : TestSuite("lr-wpan-energy-detection", Type::UNIT)
271{
272 AddTestCase(new LrWpanEdTestCase, TestCase::Duration::QUICK);
273}
274
275static LrWpanEdTestSuite g_lrWpanEdTestSuite; //!< Static variable for test initialization
LrWpan Energy Detection Test.
void PlmeEdConfirm(PhyEnumeration status, uint8_t level)
Function called when PlmeEdConfirm is hit.
void DoRun() override
Implementation to actually run this TestCase.
uint8_t m_level
ED level.
PhyEnumeration m_status
PHY status.
LrWpan Energy Detection TestSuite.
static std::string LrWpanPhyEnumerationPrinter(lrwpan::PhyEnumeration e)
Transform the LrWpanPhyEnumeration enumeration into a printable string.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
Definition: lr-wpan-mac.cc:386
void PlmeEdRequest()
IEEE 802.15.4-2006 section 6.2.2.3 PLME-ED.request Perform an ED per section 6.9.7.
Definition: lr-wpan-phy.cc:744
#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
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:115
@ IEEE_802_15_4_PHY_TX_ON
Definition: lr-wpan-phy.h:125
@ IEEE_802_15_4_PHY_SUCCESS
Definition: lr-wpan-phy.h:123
@ IEEE_802_15_4_PHY_UNSPECIFIED
Definition: lr-wpan-phy.h:128
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:63
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:706