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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author:
7 * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
8 */
9
10#include "ns3/rng-seed-manager.h"
11#include <ns3/constant-position-mobility-model.h>
12#include <ns3/core-module.h>
13#include <ns3/log.h>
14#include <ns3/lr-wpan-module.h>
15#include <ns3/packet.h>
16#include <ns3/propagation-delay-model.h>
17#include <ns3/propagation-loss-model.h>
18#include <ns3/simulator.h>
19#include <ns3/single-model-spectrum-channel.h>
20
21#include <iostream>
22
23using namespace ns3;
24using namespace ns3::lrwpan;
25
26NS_LOG_COMPONENT_DEFINE("lr-wpan-energy-detection-test");
27
28/**
29 * @ingroup lr-wpan-test
30 * @ingroup tests
31 *
32 * @brief LrWpan Energy Detection Test
33 */
35{
36 public:
38
39 private:
40 void DoRun() override;
41
42 /**
43 * @brief Function called when PlmeEdConfirm is hit.
44 * @param status The PHY status.
45 * @param level The ED level.
46 */
47 void PlmeEdConfirm(PhyEnumeration status, uint8_t level);
48
49 PhyEnumeration m_status; //!< PHY status.
50 uint8_t m_level; //!< ED level.
51};
52
54 : TestCase("Test the 802.15.4 energie detection")
55{
57 m_level = 0;
58}
59
60void
62{
63 NS_LOG_UNCOND("Energy Detection completed with status "
64 << LrWpanHelper::LrWpanPhyEnumerationPrinter(status) << " and energy level "
65 << static_cast<uint32_t>(level));
66 m_status = status;
67 m_level = level;
68}
69
70void
72{
73 // Tx Power: 0 dBm
74 // Receiver Sensitivity: -106.58 dBm
75 // Do energy detection for a single packet, arriving with 5 dB, 10 dB, 25 dB,
76 // 40 dB, relative to RX Power / Sensitivity. This should yield 0, 0, 127,
77 // and 255 as the reported energy levels.
78 // TODO: Maybe there should be a test for several interfering packets.
79 // TODO: There should be tests for signals not originating from 802.15.4
80 // devices.
81
82 // Test setup:
83 // Two nodes in communication range. The propagation model is adjusted to
84 // give us the above mentioned RX powers.
85 // Node 1 sends a packet to node 2. Node 2 starts energy detection, while the
86 // packet reception is in progress. The detected energy level is compared to
87 // the expected values.
88
89 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
90 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
91
92 // Set the random seed and run number for this test
95
96 // Create 2 nodes, and a NetDevice for each one
99
102
103 // Make random variable stream assignment deterministic
104 dev0->AssignStreams(0);
105 dev1->AssignStreams(10);
106
107 dev0->SetAddress(Mac16Address("00:01"));
108 dev1->SetAddress(Mac16Address("00:02"));
109
110 // Each device must be attached to the same channel
115 channel->AddPropagationLossModel(propModel);
116 channel->SetPropagationDelayModel(delayModel);
117
118 dev0->SetChannel(channel);
119 dev1->SetChannel(channel);
120
121 // To complete configuration, a LrWpanNetDevice must be added to a node
122 n0->AddDevice(dev0);
123 n1->AddDevice(dev1);
124
125 Ptr<ConstantPositionMobilityModel> sender0Mobility =
127 sender0Mobility->SetPosition(Vector(0, 0, 0));
128 dev0->GetPhy()->SetMobility(sender0Mobility);
129 Ptr<ConstantPositionMobilityModel> sender1Mobility =
131 // Configure position 10 m distance
132 sender1Mobility->SetPosition(Vector(0, 10, 0));
133 dev1->GetPhy()->SetMobility(sender1Mobility);
134
135 // Set the ED confirm callback.
136 dev0->GetPhy()->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanEdTestCase::PlmeEdConfirm, this));
137 dev1->GetPhy()->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanEdTestCase::PlmeEdConfirm, this));
138
139 // Configure the RX Power to be -107.58 dBm, i.e. 1 dB below receiver sensitivity.
140 propModel->SetRss(-107.58);
141
143 m_level = 0;
144 Ptr<Packet> p0 = Create<Packet>(100); // 100 bytes of dummy data
146 params.m_srcAddrMode = SHORT_ADDR;
147 params.m_dstAddrMode = SHORT_ADDR;
148 params.m_dstPanId = 0;
149 params.m_dstAddr = Mac16Address("00:02");
150 params.m_msduHandle = 0;
151 params.m_txOptions = TX_OPTION_NONE;
152 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p0);
153
154 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
155
157
158 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
159 NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
160
161 // Configure the RX Power to be -106.58 dBm, i.e. exactly to receiver sensitivity.
162 propModel->SetRss(-106.58);
163
165 m_level = 0;
166 Ptr<Packet> p1 = Create<Packet>(100); // 100 bytes of dummy data
167 params.m_srcAddrMode = SHORT_ADDR;
168 params.m_dstAddrMode = SHORT_ADDR;
169 params.m_dstPanId = 0;
170 params.m_dstAddr = Mac16Address("00:02");
171 params.m_msduHandle = 0;
172 params.m_txOptions = TX_OPTION_NONE;
173 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p1);
174
175 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
176
178
179 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
180 NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
181
182 // Configure the RX Power to be -81.58 dBm, i.e. 25 dB above receiver sensitivity.
183 propModel->SetRss(-81.58);
184
186 m_level = 0;
187 Ptr<Packet> p2 = Create<Packet>(100); // 100 bytes of dummy data
188 params.m_srcAddrMode = SHORT_ADDR;
189 params.m_dstAddrMode = SHORT_ADDR;
190 params.m_dstPanId = 0;
191 params.m_dstAddr = Mac16Address("00:02");
192 params.m_msduHandle = 0;
193 params.m_txOptions = TX_OPTION_NONE;
194 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p2);
195
196 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
197
199
200 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
201 NS_TEST_EXPECT_MSG_EQ(m_level, 127, "ED reported signal level 127 (as expected)");
202
203 // Configure the RX Power to be -66.58 dBm, i.e. 40 dB above receiver sensitivity.
204 propModel->SetRss(-66.58);
205
207 m_level = 0;
208 Ptr<Packet> p3 = Create<Packet>(100); // 100 bytes of dummy data
209 params.m_srcAddrMode = SHORT_ADDR;
210 params.m_dstAddrMode = SHORT_ADDR;
211 params.m_dstPanId = 0;
212 params.m_dstAddr = Mac16Address("00:02");
213 params.m_msduHandle = 0;
214 params.m_txOptions = TX_OPTION_NONE;
215 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p3);
216
217 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy());
218
220
221 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
222 NS_TEST_EXPECT_MSG_EQ(m_level, 255, "ED reported signal level 255 (as expected)");
223
224 // Test ED at sender.
226 m_level = 0;
227 Ptr<Packet> p4 = Create<Packet>(100); // 100 bytes of dummy data
228 params.m_srcAddrMode = SHORT_ADDR;
229 params.m_dstAddrMode = SHORT_ADDR;
230 params.m_dstPanId = 0;
231 params.m_dstAddr = Mac16Address("00:02");
232 params.m_msduHandle = 0;
233 params.m_txOptions = TX_OPTION_NONE;
234 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev0->GetMac(), params, p4);
235
236 Simulator::Schedule(Seconds(0.0025), &LrWpanPhy::PlmeEdRequest, dev0->GetPhy());
237
239
240 NS_TEST_EXPECT_MSG_EQ(m_status, IEEE_802_15_4_PHY_TX_ON, "ED status TX_ON (as expected)");
241 NS_TEST_EXPECT_MSG_EQ(m_level, 0, "ED reported signal level 0 (as expected)");
242
244}
245
246/**
247 * @ingroup lr-wpan-test
248 * @ingroup tests
249 *
250 * @brief LrWpan Energy Detection TestSuite
251 */
253{
254 public:
256};
257
259 : TestSuite("lr-wpan-energy-detection", Type::UNIT)
260{
261 AddTestCase(new LrWpanEdTestCase, TestCase::Duration::QUICK);
262}
263
264static 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.
Smart pointer class similar to boost::intrusive_ptr.
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:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
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.
void PlmeEdRequest()
IEEE 802.15.4-2006 section 6.2.2.3 PLME-ED.request Perform an ED per section 6.9.7.
#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:191
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
@ IEEE_802_15_4_PHY_TX_ON
@ IEEE_802_15_4_PHY_SUCCESS
@ IEEE_802_15_4_PHY_UNSPECIFIED
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition lr-wpan-mac.h:52
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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:684