A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-ack-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 * Modifications:
9 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
10 */
11
12#include "ns3/constant-position-mobility-model.h"
13#include "ns3/log.h"
14#include "ns3/lr-wpan-helper.h"
15#include "ns3/lr-wpan-net-device.h"
16#include "ns3/net-device-container.h"
17#include "ns3/node-container.h"
18#include "ns3/packet.h"
19#include "ns3/propagation-delay-model.h"
20#include "ns3/propagation-loss-model.h"
21#include "ns3/rng-seed-manager.h"
22#include "ns3/simulator.h"
23#include "ns3/single-model-spectrum-channel.h"
24#include "ns3/test.h"
25
26#include <fstream>
27#include <iostream>
28#include <streambuf>
29#include <string>
30
31using namespace ns3;
32using namespace ns3::lrwpan;
33
34NS_LOG_COMPONENT_DEFINE("lr-wpan-ack-test");
35
36/**
37 * @ingroup lr-wpan
38 * @defgroup lr-wpan-test LrWpan module tests
39 */
40
41/**
42 * @ingroup lr-wpan-test
43 * @ingroup tests
44 *
45 * @brief LrWpan ACK Test
46 */
48{
49 public:
50 /**
51 * Test modes
52 */
54 {
55 EXTENDED_ADDRESS_UNICAST, //!< extended addresses
56 SHORT_ADDRESS_UNICAST, //!< short addresses, unicast
57 SHORT_ADDRESS_MULTICAST, //!< short addresses, multicast
58 SHORT_ADDRESS_BROADCAST, //!< short addresses, broadcast
59 };
60
61 /**
62 * Create test case
63 *
64 * @param prefix Unique file names prefix
65 * @param mode Test mode
66 */
67 LrWpanAckTestCase(const char* const prefix, TestMode_e mode);
68
69 /**
70 * @brief Function called when DataIndication is hit on dev0.
71 * @param params The MCPS params.
72 * @param p the packet.
73 */
75 /**
76 * @brief Function called when DataIndication is hit on dev1.
77 * @param params The MCPS params.
78 * @param p the packet.
79 */
81 /**
82 * @brief Function called when DataConfirm is hit on dev0.
83 * @param params The MCPS params.
84 */
86 /**
87 * @brief Function called when DataConfirm is hit on dev1.
88 * @param params The MCPS params.
89 */
91
92 private:
93 void DoRun() override;
94
95 std::string m_prefix; //!< Filename prefix
96 Time m_requestTime; //!< Request time.
97 Time m_requestSentTime; //!< Request successfully sent time.
98 Time m_replyTime; //!< Reply time.
99 Time m_replySentTime; //!< Reply successfully sent time.
100 Time m_replyArrivalTime; //!< Reply arrival time.
101 TestMode_e m_mode; //!< Test mode.
102 Ptr<LrWpanNetDevice> m_dev0; //!< 1st LrWpanNetDevice.
103 Ptr<LrWpanNetDevice> m_dev1; //!< 2nd LrWpanNetDevice.
104};
105
107 : TestCase("Test the 802.15.4 ACK handling")
108{
109 m_prefix = prefix;
112 m_replyTime = Seconds(0);
115 m_mode = mode;
116}
117
118void
123
124void
126{
127 Ptr<Packet> pkt = Create<Packet>(10); // 10 bytes of dummy data
128 McpsDataRequestParams replyParams;
129 replyParams.m_dstPanId = 0;
130 replyParams.m_msduHandle = 0;
131 replyParams.m_txOptions = TX_OPTION_NONE;
132
134 {
135 replyParams.m_srcAddrMode = EXT_ADDR;
136 replyParams.m_dstAddrMode = EXT_ADDR;
137 replyParams.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
138 }
139 else
140 {
141 replyParams.m_srcAddrMode = SHORT_ADDR;
142 replyParams.m_dstAddrMode = SHORT_ADDR;
143 replyParams.m_dstAddr = Mac16Address("00:01");
144 }
146 m_dev1->GetMac()->McpsDataRequest(replyParams, pkt);
147}
148
149void
154
155void
160
161void
163{
164 SetDataDir(NS_TEST_SOURCEDIR);
165
166 // Test setup:
167 // Two nodes well in communication range.
168 // Node 1 sends a request packet to node 2 with ACK request bit set. Node 2
169 // immediately answers with a reply packet on reception of the request.
170 // We expect the ACK of the request packet to always arrive at node 1 before
171 // the reply packet sent by node 2.
172 // This, of course, unelss the packet is sent to a broadcast or multicast address
173 // in this case we don't expect any ACK.
174
175 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
176 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
177
178 // Set the random seed and run number for this test
181
183
184 // Helper - used to create traces
185 LrWpanHelper helper;
186 std::string asciiPrefix;
187
188 // Create 2 nodes, and a NetDevice for each one
190 nodes.Create(2);
191 helper.SetPropagationDelayModel("ns3::ConstantSpeedPropagationDelayModel");
192 helper.AddPropagationLossModel("ns3::LogDistancePropagationLossModel");
193 NetDeviceContainer devices = helper.Install(nodes);
194 m_dev0 = devices.Get(0)->GetObject<LrWpanNetDevice>();
195 m_dev1 = devices.Get(1)->GetObject<LrWpanNetDevice>();
196
197 // Make random variable stream assignment deterministic
198 m_dev0->AssignStreams(0);
199 m_dev1->AssignStreams(10);
200
201 // Add short addresses.
202 m_dev0->SetAddress(Mac16Address("00:01"));
203 m_dev1->SetAddress(Mac16Address("00:02"));
204 // Add extended addresses.
205 m_dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
206 m_dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
207
208 Ptr<ConstantPositionMobilityModel> sender0Mobility =
210 sender0Mobility->SetPosition(Vector(0, 0, 0));
211 m_dev0->GetPhy()->SetMobility(sender0Mobility);
212 Ptr<ConstantPositionMobilityModel> sender1Mobility =
214 // Configure position 10 m distance
215 sender1Mobility->SetPosition(Vector(0, 10, 0));
216 m_dev1->GetPhy()->SetMobility(sender1Mobility);
217
220 m_dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
221
224 m_dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
225
228 m_dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
229
232 m_dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
233
234 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
236 uint8_t expectedAckCount = 0;
237 switch (m_mode)
238 {
240 params.m_srcAddrMode = SHORT_ADDR;
241 params.m_dstAddrMode = SHORT_ADDR;
242 params.m_dstAddr = Mac16Address("00:02");
243 expectedAckCount = 1;
244 break;
246 params.m_srcAddrMode = SHORT_ADDR;
247 params.m_dstAddrMode = SHORT_ADDR;
249 expectedAckCount = 0;
250 break;
252 params.m_srcAddrMode = SHORT_ADDR;
253 params.m_dstAddrMode = SHORT_ADDR;
254 params.m_dstAddr = Mac16Address::GetBroadcast();
255 expectedAckCount = 0;
256 break;
258 params.m_srcAddrMode = EXT_ADDR;
259 params.m_dstAddrMode = EXT_ADDR;
260 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
261 expectedAckCount = 1;
262 break;
263 }
264 params.m_dstPanId = 0;
265 params.m_msduHandle = 0;
266 params.m_txOptions = TX_OPTION_ACK;
269
272
273 std::ifstream traceFile(CreateTempDirFilename(m_prefix) + "-0-0.tr");
274 uint8_t ackCounter = 0;
275 std::string sub("Frame Type = 2");
276 for (std::string line; getline(traceFile, line);)
277 {
278 if (line.find(sub, 0) != std::string::npos)
279 {
280 ackCounter++;
281 }
282 }
283 traceFile.close();
284
285 // Note: the packet being correctly sent includes receiving an ACK in case of for unicast
286 // packets.
289 "Sent the request before the reply (as expected)");
290 NS_TEST_EXPECT_MSG_EQ(m_requestSentTime.IsStrictlyPositive(),
291 true,
292 "The request was sent (as expected)");
295 "The request was sent before the reply arrived (as expected)");
298 "The reply was sent before the reply arrived (as expected)");
299 NS_TEST_EXPECT_MSG_EQ(ackCounter,
300 expectedAckCount,
301 "The right amount of ACKs have been seen on the channel (as expected)");
302
303 m_dev0 = nullptr;
304 m_dev1 = nullptr;
305
307}
308
309/**
310 * @ingroup lr-wpan-test
311 * @ingroup tests
312 *
313 * @brief LrWpan ACK TestSuite
314 */
316{
317 public:
319};
320
336
337static LrWpanAckTestSuite g_lrWpanAckTestSuite; //!< Static variable for test initialization
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.
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.
void DoRun() override
Implementation to actually run this TestCase.
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.
static Ipv6Address GetAllNodesMulticast()
Get the "all nodes multicast" address.
helps to manage and create IEEE 802.15.4 NetDevice objects
NetDeviceContainer Install(NodeContainer c)
Install a LrWpanNetDevice and the associated structures (e.g., channel) in the nodes.
void SetPropagationDelayModel(std::string name, Ts &&... args)
void AddPropagationLossModel(std::string name, Ts &&... args)
This class can contain 16 bit addresses.
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
static Mac16Address GetBroadcast()
an EUI-64 address
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:586
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:614
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition test.cc:436
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition test.cc:476
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
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.
Network layer to device interface.
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:690
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
Callback< void, McpsDataIndicationParams, Ptr< Packet > > McpsDataIndicationCallback
This callback is called after a Mcps has successfully received a frame and wants to deliver it to the...
Callback< void, McpsDataConfirmParams > McpsDataConfirmCallback
This callback is called after a McpsDataRequest has been called from the higher layer.
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition lr-wpan-mac.h:53
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:54
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#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:779
#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:1313
static LrWpanAckTestSuite g_lrWpanAckTestSuite
Static variable for test initialization.
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
AddressMode m_dstAddrMode
Destination address mode.
Mac16Address m_dstAddr
Destination address.
Mac64Address m_dstExtAddr
Destination extended address.
uint16_t m_dstPanId
Destination PAN identifier.
AddressMode m_srcAddrMode
Source address mode.
uint8_t m_txOptions
Tx Options (bitfield).