A Discrete-Event Network Simulator
API
lr-wpan-ack-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 * Modifications:
21 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
22 */
23
24#include <ns3/log.h>
25#include <ns3/core-module.h>
26#include <ns3/lr-wpan-module.h>
27#include <ns3/propagation-loss-model.h>
28#include <ns3/propagation-delay-model.h>
29#include <ns3/simulator.h>
30#include <ns3/single-model-spectrum-channel.h>
31#include <ns3/constant-position-mobility-model.h>
32#include <ns3/packet.h>
33#include "ns3/rng-seed-manager.h"
34
35#include <iostream>
36#include <string>
37#include <fstream>
38#include <streambuf>
39
40using namespace ns3;
41
42NS_LOG_COMPONENT_DEFINE ("lr-wpan-ack-test");
43
56{
57public:
58
62 typedef enum
63 {
68 } TestMode_e;
69
76 LrWpanAckTestCase (const char * const prefix, TestMode_e mode);
77
100
101private:
102 virtual void DoRun (void);
103
104 std::string m_prefix;
113};
114
115LrWpanAckTestCase::LrWpanAckTestCase (const char * const prefix, TestMode_e mode)
116 : TestCase ("Test the 802.15.4 ACK handling")
117{
118 m_prefix = prefix;
121 m_replyTime = Seconds (0);
124 m_mode = mode;
125}
126
127void
129{
131}
132
133void
135{
136 Ptr<Packet> pkt = Create<Packet> (10); // 10 bytes of dummy data
137 McpsDataRequestParams replyParams;
138 replyParams.m_dstPanId = 0;
139 replyParams.m_msduHandle = 0;
140 replyParams.m_txOptions = TX_OPTION_NONE;
141
143 {
144 replyParams.m_srcAddrMode = EXT_ADDR;
145 replyParams.m_dstAddrMode = EXT_ADDR;
146 replyParams.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:01");
147 }
148 else
149 {
150 replyParams.m_srcAddrMode = SHORT_ADDR;
151 replyParams.m_dstAddrMode = SHORT_ADDR;
152 replyParams.m_dstAddr = Mac16Address ("00:01");
153 }
155 m_dev1->GetMac ()->McpsDataRequest (replyParams, pkt);
156}
157
158void
160{
162}
163
164void
166{
168}
169
170void
172{
173 // Test setup:
174 // Two nodes well in communication range.
175 // Node 1 sends a request packet to node 2 with ACK request bit set. Node 2
176 // immediately answers with a reply packet on reception of the request.
177 // We expect the ACK of the request packet to always arrive at node 1 before
178 // the reply packet sent by node 2.
179 // This, of course, unelss the packet is sent to a broadcast or multicast address
180 // in this case we don't expect any ACK.
181
182 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
183 // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
184
185 // Set the random seed and run number for this test
186 RngSeedManager::SetSeed (1);
187 RngSeedManager::SetRun (6);
188
189 Packet::EnablePrinting ();
190
191 // Helper - used to create traces
192 LrWpanHelper helper;
193 std::string asciiPrefix;
194
195 // Create 2 nodes, and a NetDevice for each one
196 Ptr<Node> n0 = CreateObject <Node> ();
197 Ptr<Node> n1 = CreateObject <Node> ();
198
199 m_dev0 = CreateObject<LrWpanNetDevice> ();
200 m_dev1 = CreateObject<LrWpanNetDevice> ();
201
202 // Make random variable stream assignment deterministic
204 m_dev1->AssignStreams (10);
205
206 // Add short addresses.
207 m_dev0->SetAddress (Mac16Address ("00:01"));
208 m_dev1->SetAddress (Mac16Address ("00:02"));
209 // Add extended addresses.
210 m_dev0->GetMac()->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:01"));
211 m_dev1->GetMac()->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:02"));
212
213 // Each device must be attached to the same channel
214 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
215 Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
216 Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
217 channel->AddPropagationLossModel (propModel);
218 channel->SetPropagationDelayModel (delayModel);
219
222
223 // To complete configuration, a LrWpanNetDevice must be added to a node
224 n0->AddDevice (m_dev0);
225 n1->AddDevice (m_dev1);
226
227 Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
228 sender0Mobility->SetPosition (Vector (0,0,0));
229 m_dev0->GetPhy ()->SetMobility (sender0Mobility);
230 Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
231 // Configure position 10 m distance
232 sender1Mobility->SetPosition (Vector (0,10,0));
233 m_dev1->GetPhy ()->SetMobility (sender1Mobility);
234
237 m_dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
238
241 m_dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1);
242
245 m_dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2);
246
249 m_dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3);
250
251 Ptr<Packet> p0 = Create<Packet> (50); // 50 bytes of dummy data
253 uint8_t expectedAckCount = 0;
254 switch (m_mode)
255 {
257 params.m_srcAddrMode = SHORT_ADDR;
258 params.m_dstAddrMode = SHORT_ADDR;
259 params.m_dstAddr = Mac16Address ("00:02");
260 expectedAckCount = 1;
261 break;
263 params.m_srcAddrMode = SHORT_ADDR;
264 params.m_dstAddrMode = SHORT_ADDR;
265 params.m_dstAddr = Mac16Address::GetMulticast (Ipv6Address::GetAllNodesMulticast ());
266 expectedAckCount = 0;
267 break;
269 params.m_srcAddrMode = SHORT_ADDR;
270 params.m_dstAddrMode = SHORT_ADDR;
271 params.m_dstAddr = Mac16Address::GetBroadcast ();
272 expectedAckCount = 0;
273 break;
275 params.m_srcAddrMode = EXT_ADDR;
276 params.m_dstAddrMode = EXT_ADDR;
277 params.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:02");
278 expectedAckCount = 1;
279 break;
280 }
281 params.m_dstPanId = 0;
282 params.m_msduHandle = 0;
283 params.m_txOptions = TX_OPTION_ACK;
285 Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, m_dev0->GetMac (), params, p0);
286
289
290 std::ifstream traceFile (CreateTempDirFilename (m_prefix)+"-0-0.tr");
291 uint8_t ackCounter = 0;
292 std::string sub ("Frame Type = 2");
293 for( std::string line; getline ( traceFile, line ); )
294 {
295 if (line.find(sub, 0) != std::string::npos)
296 {
297 ackCounter++;
298 }
299 }
300 traceFile.close ();
301
302 // Note: the packet being correctly sent includes receiving an ACK in case of for unicact packets.
303 NS_TEST_EXPECT_MSG_LT (m_requestTime, m_replyTime, "Sent the request before the reply (as expected)");
304 NS_TEST_EXPECT_MSG_GT (m_requestSentTime, Time (0), "The request was sent (as expected)");
305 NS_TEST_EXPECT_MSG_LT (m_requestSentTime, m_replyArrivalTime, "The request was sent before the reply arrived (as expected)");
306 NS_TEST_EXPECT_MSG_LT (m_replySentTime, m_replyArrivalTime, "The reply was sent before the reply arrived (as expected)");
307 NS_TEST_EXPECT_MSG_EQ (ackCounter, expectedAckCount, "The right amount of ACKs have been seen on the channel (as expected)");
308
309 m_dev0 = 0;
310 m_dev1 = 0;
311
312 Simulator::Destroy ();
313}
314
315
323{
324public:
326};
327
329 : TestSuite ("lr-wpan-ack", UNIT)
330{
331 AddTestCase (new LrWpanAckTestCase ("short-unicast", LrWpanAckTestCase::SHORT_ADDRESS_UNICAST), TestCase::QUICK);
332 AddTestCase (new LrWpanAckTestCase ("short-multicast", LrWpanAckTestCase::SHORT_ADDRESS_MULTICAST), TestCase::QUICK);
333 AddTestCase (new LrWpanAckTestCase ("short-broadcast", LrWpanAckTestCase::SHORT_ADDRESS_BROADCAST), TestCase::QUICK);
334 AddTestCase (new LrWpanAckTestCase ("extended-unicast", LrWpanAckTestCase::EXTENDED_ADDRESS_UNICAST), TestCase::QUICK);
335}
336
void Run(ObjectFactory &factory, uint32_t pop, uint32_t total, uint32_t runs, Ptr< RandomVariableStream > eventStream, bool calRev)
Perform the runs for a single scheduler type.
LrWpan ACK Test.
@ SHORT_ADDRESS_MULTICAST
short addresses, multicast
@ SHORT_ADDRESS_BROADCAST
short addresses, broadcast
@ EXTENDED_ADDRESS_UNICAST
extended addresses
@ SHORT_ADDRESS_UNICAST
short addresses, unicast
Time m_requestSentTime
Request successfully sent time.
void DataIndicationDev0(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit on dev0.
Time m_requestTime
Request time.
Time m_replyTime
Reply time.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DataIndicationDev1(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when DataIndication is hit on dev1.
std::string m_prefix
Filename prefix.
Time m_replySentTime
Reply successfully sent time.
void DataConfirmDev0(McpsDataConfirmParams params)
Function called when DataConfirm is hit on dev0.
Ptr< LrWpanNetDevice > m_dev0
1st LrWpanNetDevice.
LrWpanAckTestCase(const char *const prefix, TestMode_e mode)
Create test case.
TestMode_e m_mode
Test mode.
Ptr< LrWpanNetDevice > m_dev1
2nd LrWpanNetDevice.
Time m_replyArrivalTime
Reply arrival time.
void DataConfirmDev1(McpsDataConfirmParams params)
Function called when DataConfirm is hit on dev1.
LrWpan ACK TestSuite.
void EnableAscii(std::string prefix, Ptr< NetDevice > nd, bool explicitFilename=false)
Enable ascii trace output on the indicated net device.
helps to manage and create IEEE 802.15.4 NetDevice objects
virtual void SetAddress(Address address)
This method indirects to LrWpanMac::SetShortAddress ()
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
This class can contain 16 bit addresses.
Definition: mac16-address.h:42
an EUI-64 address
Definition: mac64-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition: test.cc:430
A suite of tests to run.
Definition: test.h:1188
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:61
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition: lr-wpan-mac.h:60
@ SHORT_ADDR
Definition: lr-wpan-mac.h:156
@ EXT_ADDR
Definition: lr-wpan-mac.h:157
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
#define NS_TEST_EXPECT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report if not.
Definition: test.h:748
#define NS_TEST_EXPECT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report if not.
Definition: test.h:899
#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:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1245
static LrWpanAckTestSuite g_lrWpanAckTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
channel
Definition: third.py:83
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:353
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:364
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:337
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:338
Mac64Address m_dstExtAddr
Destination extended address.
Definition: lr-wpan-mac.h:342
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:339
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:340
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:341
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:343
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:344