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/core-module.h"
14#include "ns3/log.h"
15#include "ns3/lr-wpan-module.h"
16#include "ns3/packet.h"
17#include "ns3/propagation-delay-model.h"
18#include "ns3/propagation-loss-model.h"
19#include "ns3/rng-seed-manager.h"
20#include "ns3/simulator.h"
21#include "ns3/single-model-spectrum-channel.h"
22
23#include <fstream>
24#include <iostream>
25#include <streambuf>
26#include <string>
27
28using namespace ns3;
29using namespace ns3::lrwpan;
30
31NS_LOG_COMPONENT_DEFINE("lr-wpan-ack-test");
32
33/**
34 * @ingroup lr-wpan
35 * @defgroup lr-wpan-test LrWpan module tests
36 */
37
38/**
39 * @ingroup lr-wpan-test
40 * @ingroup tests
41 *
42 * @brief LrWpan ACK Test
43 */
45{
46 public:
47 /**
48 * Test modes
49 */
51 {
52 EXTENDED_ADDRESS_UNICAST, //!< extended addresses
53 SHORT_ADDRESS_UNICAST, //!< short addresses, unicast
54 SHORT_ADDRESS_MULTICAST, //!< short addresses, multicast
55 SHORT_ADDRESS_BROADCAST, //!< short addresses, broadcast
56 };
57
58 /**
59 * Create test case
60 *
61 * @param prefix Unique file names prefix
62 * @param mode Test mode
63 */
64 LrWpanAckTestCase(const char* const prefix, TestMode_e mode);
65
66 /**
67 * @brief Function called when DataIndication is hit on dev0.
68 * @param params The MCPS params.
69 * @param p the packet.
70 */
72 /**
73 * @brief Function called when DataIndication is hit on dev1.
74 * @param params The MCPS params.
75 * @param p the packet.
76 */
78 /**
79 * @brief Function called when DataConfirm is hit on dev0.
80 * @param params The MCPS params.
81 */
83 /**
84 * @brief Function called when DataConfirm is hit on dev1.
85 * @param params The MCPS params.
86 */
88
89 private:
90 void DoRun() override;
91
92 std::string m_prefix; //!< Filename prefix
93 Time m_requestTime; //!< Request time.
94 Time m_requestSentTime; //!< Request successfully sent time.
95 Time m_replyTime; //!< Reply time.
96 Time m_replySentTime; //!< Reply successfully sent time.
97 Time m_replyArrivalTime; //!< Reply arrival time.
98 TestMode_e m_mode; //!< Test mode.
99 Ptr<LrWpanNetDevice> m_dev0; //!< 1st LrWpanNetDevice.
100 Ptr<LrWpanNetDevice> m_dev1; //!< 2nd LrWpanNetDevice.
101};
102
104 : TestCase("Test the 802.15.4 ACK handling")
105{
106 m_prefix = prefix;
109 m_replyTime = Seconds(0);
112 m_mode = mode;
113}
114
115void
120
121void
123{
124 Ptr<Packet> pkt = Create<Packet>(10); // 10 bytes of dummy data
125 McpsDataRequestParams replyParams;
126 replyParams.m_dstPanId = 0;
127 replyParams.m_msduHandle = 0;
128 replyParams.m_txOptions = TX_OPTION_NONE;
129
131 {
132 replyParams.m_srcAddrMode = EXT_ADDR;
133 replyParams.m_dstAddrMode = EXT_ADDR;
134 replyParams.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
135 }
136 else
137 {
138 replyParams.m_srcAddrMode = SHORT_ADDR;
139 replyParams.m_dstAddrMode = SHORT_ADDR;
140 replyParams.m_dstAddr = Mac16Address("00:01");
141 }
143 m_dev1->GetMac()->McpsDataRequest(replyParams, pkt);
144}
145
146void
151
152void
157
158void
160{
161 SetDataDir(NS_TEST_SOURCEDIR);
162
163 // Test setup:
164 // Two nodes well in communication range.
165 // Node 1 sends a request packet to node 2 with ACK request bit set. Node 2
166 // immediately answers with a reply packet on reception of the request.
167 // We expect the ACK of the request packet to always arrive at node 1 before
168 // the reply packet sent by node 2.
169 // This, of course, unelss the packet is sent to a broadcast or multicast address
170 // in this case we don't expect any ACK.
171
172 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
173 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
174
175 // Set the random seed and run number for this test
178
180
181 // Helper - used to create traces
182 LrWpanHelper helper;
183 std::string asciiPrefix;
184
185 // Create 2 nodes, and a NetDevice for each one
187 nodes.Create(2);
188 helper.SetPropagationDelayModel("ns3::ConstantSpeedPropagationDelayModel");
189 helper.AddPropagationLossModel("ns3::LogDistancePropagationLossModel");
190 NetDeviceContainer devices = helper.Install(nodes);
191 m_dev0 = devices.Get(0)->GetObject<LrWpanNetDevice>();
192 m_dev1 = devices.Get(1)->GetObject<LrWpanNetDevice>();
193
194 // Make random variable stream assignment deterministic
196 m_dev1->AssignStreams(10);
197
198 // Add short addresses.
199 m_dev0->SetAddress(Mac16Address("00:01"));
200 m_dev1->SetAddress(Mac16Address("00:02"));
201 // Add extended addresses.
202 m_dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
203 m_dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
204
205 Ptr<ConstantPositionMobilityModel> sender0Mobility =
207 sender0Mobility->SetPosition(Vector(0, 0, 0));
208 m_dev0->GetPhy()->SetMobility(sender0Mobility);
209 Ptr<ConstantPositionMobilityModel> sender1Mobility =
211 // Configure position 10 m distance
212 sender1Mobility->SetPosition(Vector(0, 10, 0));
213 m_dev1->GetPhy()->SetMobility(sender1Mobility);
214
217 m_dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
218
221 m_dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
222
225 m_dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
226
229 m_dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
230
231 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
233 uint8_t expectedAckCount = 0;
234 switch (m_mode)
235 {
237 params.m_srcAddrMode = SHORT_ADDR;
238 params.m_dstAddrMode = SHORT_ADDR;
239 params.m_dstAddr = Mac16Address("00:02");
240 expectedAckCount = 1;
241 break;
243 params.m_srcAddrMode = SHORT_ADDR;
244 params.m_dstAddrMode = SHORT_ADDR;
246 expectedAckCount = 0;
247 break;
249 params.m_srcAddrMode = SHORT_ADDR;
250 params.m_dstAddrMode = SHORT_ADDR;
251 params.m_dstAddr = Mac16Address::GetBroadcast();
252 expectedAckCount = 0;
253 break;
255 params.m_srcAddrMode = EXT_ADDR;
256 params.m_dstAddrMode = EXT_ADDR;
257 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
258 expectedAckCount = 1;
259 break;
260 }
261 params.m_dstPanId = 0;
262 params.m_msduHandle = 0;
263 params.m_txOptions = TX_OPTION_ACK;
266
269
270 std::ifstream traceFile(CreateTempDirFilename(m_prefix) + "-0-0.tr");
271 uint8_t ackCounter = 0;
272 std::string sub("Frame Type = 2");
273 for (std::string line; getline(traceFile, line);)
274 {
275 if (line.find(sub, 0) != std::string::npos)
276 {
277 ackCounter++;
278 }
279 }
280 traceFile.close();
281
282 // Note: the packet being correctly sent includes receiving an ACK in case of for unicast
283 // packets.
286 "Sent the request before the reply (as expected)");
288 true,
289 "The request was sent (as expected)");
292 "The request was sent before the reply arrived (as expected)");
295 "The reply was sent before the reply arrived (as expected)");
296 NS_TEST_EXPECT_MSG_EQ(ackCounter,
297 expectedAckCount,
298 "The right amount of ACKs have been seen on the channel (as expected)");
299
300 m_dev0 = nullptr;
301 m_dev1 = nullptr;
302
304}
305
306/**
307 * @ingroup lr-wpan-test
308 * @ingroup tests
309 *
310 * @brief LrWpan ACK TestSuite
311 */
313{
314 public:
316};
317
319 : TestSuite("lr-wpan-ack", Type::UNIT)
320{
322 TestCase::Duration::QUICK);
325 TestCase::Duration::QUICK);
328 TestCase::Duration::QUICK);
331 TestCase::Duration::QUICK);
332}
333
334static 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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
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 void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
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:595
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
std::string CreateTempDirFilename(std::string filename)
Construct the full path to a file in a temporary directory.
Definition test.cc:432
void SetDataDir(std::string directory)
Set the data directory where reference trace files can be found.
Definition test.cc:472
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
bool IsStrictlyPositive() const
Exactly equivalent to t > 0.
Definition nstime.h:340
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.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
@ TX_OPTION_NONE
TX_OPTION_NONE.
Definition lr-wpan-mac.h:52
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:53
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:439
#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:780
#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:1345
static LrWpanAckTestSuite g_lrWpanAckTestSuite
Static variable for test initialization.
NodeContainer nodes
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
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)