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 using namespace ns3;
38 
39 NS_LOG_COMPONENT_DEFINE ("lr-wpan-energy-detection-test");
40 
41 class LrWpanEdTestCase : public TestCase
42 {
43 public:
45 
46 private:
47  virtual void DoRun (void);
48 
49  void PlmeEdConfirm (LrWpanPhyEnumeration status, uint8_t level);
50 
52  uint8_t m_level;
53 };
54 
56  : TestCase ("Test the 802.15.4 energie detection")
57 {
59  m_level = 0;
60 }
61 
62 void
64 {
65  NS_LOG_UNCOND ("Energy Detection completed with status " << LrWpanHelper::LrWpanPhyEnumerationPrinter (status) << " and energy level " << static_cast<uint32_t> (level));
66  m_status = status;
67  m_level = level;
68 }
69 
70 void
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 or wireshark.
90  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
91 
92  // Set the random seed and run number for this test
93  RngSeedManager::SetSeed (1);
94  RngSeedManager::SetRun (6);
95 
96  // Create 2 nodes, and a NetDevice for each one
97  Ptr<Node> n0 = CreateObject <Node> ();
98  Ptr<Node> n1 = CreateObject <Node> ();
99 
100  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
101  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
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
111  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
112  Ptr<FixedRssLossModel> propModel = CreateObject<FixedRssLossModel> ();
113  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
114  channel->AddPropagationLossModel (propModel);
115  channel->SetPropagationDelayModel (delayModel);
116 
117  dev0->SetChannel (channel);
118  dev1->SetChannel (channel);
119 
120  // To complete configuration, a LrWpanNetDevice must be added to a node
121  n0->AddDevice (dev0);
122  n1->AddDevice (dev1);
123 
124  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
125  sender0Mobility->SetPosition (Vector (0, 0, 0));
126  dev0->GetPhy ()->SetMobility (sender0Mobility);
127  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
128  // Configure position 10 m distance
129  sender1Mobility->SetPosition (Vector (0, 10, 0));
130  dev1->GetPhy ()->SetMobility (sender1Mobility);
131 
132  // Set the ED confirm callback.
133  dev0->GetPhy ()->SetPlmeEdConfirmCallback (MakeCallback (&LrWpanEdTestCase::PlmeEdConfirm, this));
134  dev1->GetPhy ()->SetPlmeEdConfirmCallback (MakeCallback (&LrWpanEdTestCase::PlmeEdConfirm, this));
135 
136  // Configure the RX Power to be -107.58 dBm, i.e. 1 dB below receiver sensitivity.
137  propModel->SetRss (-107.58);
138 
140  m_level = 0;
141  Ptr<Packet> p0 = Create<Packet> (100); // 100 bytes of dummy data
142  McpsDataRequestParams params;
143  params.m_srcAddrMode = SHORT_ADDR;
144  params.m_dstAddrMode = SHORT_ADDR;
145  params.m_dstPanId = 0;
146  params.m_dstAddr = Mac16Address ("00:02");
147  params.m_msduHandle = 0;
148  params.m_txOptions = TX_OPTION_NONE;
149  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p0);
150 
151  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy ());
152 
153  Simulator::Run ();
154 
155  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
156  NS_TEST_EXPECT_MSG_EQ (m_level, 0, "ED reported signal level 0 (as expected)");
157 
158 
159  // Configure the RX Power to be -106.58 dBm, i.e. exectly to receiver sensitivity.
160  propModel->SetRss (-106.58);
161 
163  m_level = 0;
164  Ptr<Packet> p1 = Create<Packet> (100); // 100 bytes of dummy data
165  params.m_srcAddrMode = SHORT_ADDR;
166  params.m_dstAddrMode = SHORT_ADDR;
167  params.m_dstPanId = 0;
168  params.m_dstAddr = Mac16Address ("00:02");
169  params.m_msduHandle = 0;
170  params.m_txOptions = TX_OPTION_NONE;
171  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p1);
172 
173  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy ());
174 
175  Simulator::Run ();
176 
177  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
178  NS_TEST_EXPECT_MSG_EQ (m_level, 0, "ED reported signal level 0 (as expected)");
179 
180 
181  // Configure the RX Power to be -81.58 dBm, i.e. 25 dB above receiver sensitivity.
182  propModel->SetRss (-81.58);
183 
185  m_level = 0;
186  Ptr<Packet> p2 = Create<Packet> (100); // 100 bytes of dummy data
187  params.m_srcAddrMode = SHORT_ADDR;
188  params.m_dstAddrMode = SHORT_ADDR;
189  params.m_dstPanId = 0;
190  params.m_dstAddr = Mac16Address ("00:02");
191  params.m_msduHandle = 0;
192  params.m_txOptions = TX_OPTION_NONE;
193  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p2);
194 
195  Simulator::Schedule (Seconds (0.0025), &LrWpanPhy::PlmeEdRequest, dev1->GetPhy ());
196 
197  Simulator::Run ();
198 
199  NS_TEST_EXPECT_MSG_EQ (m_status, IEEE_802_15_4_PHY_SUCCESS, "ED status SUCCESS (as expected)");
200  NS_TEST_EXPECT_MSG_EQ (m_level, 127, "ED reported signal level 127 (as expected)");
201 
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 
219  Simulator::Run ();
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 
238  Simulator::Run ();
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 
243  Simulator::Destroy ();
244 }
245 
247 {
248 public:
250 };
251 
253  : TestSuite ("lr-wpan-energy-detection", UNIT)
254 {
255  AddTestCase (new LrWpanEdTestCase, TestCase::QUICK);
256 }
257 
tuple channel
Definition: third.py:85
static LrWpanEdTestSuite g_lrWpanEdTestSuite
A suite of tests to run.
Definition: test.h:1333
#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
encapsulates test code
Definition: test.h:1147
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
Set the single-frequency propagation loss model to be used.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
LrWpanPhyEnumeration m_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)
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:895
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