A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-ifs-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author:
7 * Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
8 */
9
10#include "ns3/constant-position-mobility-model.h"
11#include "ns3/log.h"
12#include "ns3/lr-wpan-csmaca.h"
13#include "ns3/lr-wpan-mac-header.h"
14#include "ns3/lr-wpan-net-device.h"
15#include "ns3/node.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#include "ns3/test.h"
23
24#include <iomanip>
25#include <iostream>
26
27using namespace ns3;
28using namespace ns3::lrwpan;
29
30NS_LOG_COMPONENT_DEFINE("lr-wpan-ifs-test");
31
32/**
33 * @ingroup lr-wpan-test
34 * @ingroup tests
35 *
36 * @brief LrWpan Dataframe transmission with Interframe Space
37 */
39{
40 public:
42 ~LrWpanDataIfsTestCase() override;
43
44 private:
45 /**
46 * @brief Function called when DataConfirm is hit.
47 * @param testcase pointer to the testcase
48 * @param dev originating NetDevice
49 * @param params the MCPS params
50 */
51 static void DataConfirm(LrWpanDataIfsTestCase* testcase,
54
55 /**
56 * @brief Function called when DataReceived is hit.
57 * @param testcase pointer to the testcase
58 * @param dev originating NetDevice
59 * @param p packet
60 */
61 static void DataReceivedDev0(LrWpanDataIfsTestCase* testcase,
64
65 /**
66 * @brief Function called when PhyDataRxStart is hit.
67 * @param testcase pointer to the testcase
68 * @param dev originating NetDevice
69 * @param p packet
70 */
71 static void PhyDataRxStart(LrWpanDataIfsTestCase* testcase,
74
75 /**
76 * @brief Function called when DataConfirm is hit.
77 * @param testcase pointer to the testcase
78 * @param dev originating NetDevice
79 * @param p packet
80 */
81 static void DataReceivedDev1(LrWpanDataIfsTestCase* testcase,
84
85 /**
86 * @brief Function called when the IFS ends.
87 * @param testcase pointer to the testcase
88 * @param dev originating NetDevice
89 * @param IfsTime the IFS time
90 */
91 static void IfsEnd(LrWpanDataIfsTestCase* testcase, Ptr<LrWpanNetDevice> dev, Time IfsTime);
92
93 void DoRun() override;
94 Time m_lastTxTime; //!< The time of the last transmitted packet
95 Time m_ackRxTime; //!< The time of the received acknowledgment.
96 Time m_endIfs; //!< The time where the Interframe Space ended.
97 Time m_phyStartRx; //!< The time the phy start receiving a packet.
98};
99
101 : TestCase("Lrwpan: IFS tests")
102{
103}
104
108
109void
113{
114 // get the end time of transmission
115 testcase->m_lastTxTime = Simulator::Now();
116}
117
118void
122{
123 // Callback for Data received in the Dev0
124 Ptr<Packet> RxPacket = p->Copy();
125 LrWpanMacHeader receivedMacHdr;
126 RxPacket->RemoveHeader(receivedMacHdr);
127
128 if (receivedMacHdr.IsAcknowledgment())
129 {
130 testcase->m_ackRxTime = Simulator::Now();
131 std::cout << Simulator::Now().GetSeconds() << " | Dev0 (Node 0) received Acknowledgment.\n";
132 }
133 else if (receivedMacHdr.GetShortDstAddr().IsBroadcast())
134 {
135 std::cout << Simulator::Now().GetSeconds() << " | Dev0 (Node 0) received Broadcast. \n";
136 }
137}
138
139void
143{
144 // get the start time the phy in dev 0 ( Node 0) start receiving a frame
145 testcase->m_phyStartRx = Simulator::Now();
146}
147
148void
152{
153 // Callback for Data received in the Dev1
154 Ptr<Packet> RxPacket = p->Copy();
155 LrWpanMacHeader receivedMacHdr;
156 RxPacket->RemoveHeader(receivedMacHdr);
157
158 if (receivedMacHdr.GetShortDstAddr().IsBroadcast())
159 {
160 std::cout << Simulator::Now().GetSeconds() << " | Dev1 (Node 1) received Broadcast. \n";
161
162 // Bcst received, respond with another bcst
163
164 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
165 McpsDataRequestParams params1;
166 params1.m_dstPanId = 0;
167 params1.m_srcAddrMode = SHORT_ADDR;
168 params1.m_dstAddrMode = SHORT_ADDR;
169 params1.m_dstAddr = Mac16Address("ff:ff");
170 params1.m_msduHandle = 0;
171
172 Simulator::ScheduleNow(&LrWpanMac::McpsDataRequest, dev->GetMac(), params1, p0);
173 }
174}
175
176void
179 Time IfsTime)
180{
181 // take the time of the end of the IFS
182 testcase->m_endIfs = Simulator::Now();
183}
184
185void
187{
188 // Test of Interframe Spaces (IFS)
189
190 // The MAC layer needs a finite amount of time to process the data received from the PHY.
191 // To allow this, to successive transmitted frames must be separated for at least one IFS.
192 // The IFS size depends on the transmitted frame. This test verifies that the IFS is correctly
193 // implemented and its size correspond to the situations described by the standard.
194 // For more info see IEEE 802.15.4-2011 Section 5.1.1.3
195
200 LogComponentEnable("LrWpanCsmaCa", LOG_LEVEL_DEBUG);
201
202 // Create 2 nodes, and a NetDevice for each one
205
208
209 dev0->SetAddress(Mac16Address("00:01"));
210 dev1->SetAddress(Mac16Address("00:02"));
211
212 // Each device must be attached to the same channel
218 channel->AddPropagationLossModel(propModel);
219 channel->SetPropagationDelayModel(delayModel);
220
221 dev0->SetChannel(channel);
222 dev1->SetChannel(channel);
223
224 // To complete configuration, a LrWpanNetDevice must be added to a node
225 n0->AddDevice(dev0);
226 n1->AddDevice(dev1);
227
228 // Connect to trace files in the MAC layer
229 dev0->GetMac()->TraceConnectWithoutContext(
230 "IfsEnd",
232 dev0->GetMac()->TraceConnectWithoutContext(
233 "MacRx",
235 dev0->GetPhy()->TraceConnectWithoutContext(
236 "PhyRxBegin",
238 dev1->GetMac()->TraceConnectWithoutContext(
239 "MacRx",
241
242 Ptr<ConstantPositionMobilityModel> sender0Mobility =
244 sender0Mobility->SetPosition(Vector(0, 0, 0));
245 dev0->GetPhy()->SetMobility(sender0Mobility);
246 Ptr<ConstantPositionMobilityModel> sender1Mobility =
248 // Configure position 10 m distance
249 sender1Mobility->SetPosition(Vector(0, 10, 0));
250 dev1->GetPhy()->SetMobility(sender1Mobility);
251
254 dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
255
258 dev1->GetMac()->SetMcpsDataConfirmCallback(cb1);
259
262 params.m_dstPanId = 0;
263
264 params.m_srcAddrMode = SHORT_ADDR;
265 params.m_dstAddrMode = SHORT_ADDR;
266 params.m_dstAddr = Mac16Address("00:02");
267 params.m_msduHandle = 0;
268
269 Time ifsSize;
270
271 // NOTE: // For all the test , PAN SRC and DST are the same (PAN compression is ON) therefore
272 // MAC header is 2 bytes smaller than the usual 11 bytes (see IEEE 802.15.4 Section 7.5.6.1)
273
274 //////////////////////// SIFS ///////////////////////////
275
277 Seconds(0),
279 dev0->GetMac(),
280 params,
281 p0);
282
284
285 // MPDU = MAC header (9 bytes) + MSDU (2 bytes)+ MAC trailer (2 bytes) = 13)
286 // MPDU (13 bytes) < 18 bytes therefore IFS = SIFS
287 // SIFS = 12 symbols (192 Microseconds on a 2.4Ghz O-QPSK PHY)
288 ifsSize = m_endIfs - m_lastTxTime;
289 NS_TEST_EXPECT_MSG_EQ(ifsSize,
290 MicroSeconds(192),
291 "Wrong Short InterFrame Space (SIFS) Size after dataframe Tx");
292 std::cout << "----------------------------------\n";
293
294 //////////////////////// LIFS ///////////////////////////
295
296 p0 = Create<Packet>(8);
297
299 Seconds(0),
301 dev0->GetMac(),
302 params,
303 p0);
304
306
307 // MPDU = MAC header (9 bytes) + MSDU (8 bytes)+ MAC trailer (2 bytes) = 19)
308 // MPDU (19 bytes) > 18 bytes therefore IFS = LIFS
309 // LIFS = 40 symbols (640 Microseconds on a 2.4Ghz O-QPSK PHY)
310 ifsSize = m_endIfs - m_lastTxTime;
311 NS_TEST_EXPECT_MSG_EQ(ifsSize,
312 MicroSeconds(640),
313 "Wrong Long InterFrame Space (LIFS) Size after dataframe Tx");
314 std::cout << "----------------------------------\n";
315
316 //////////////////////// SIFS after ACK //////////////////
317
318 params.m_txOptions = TX_OPTION_ACK;
319 p0 = Create<Packet>(2);
320
322 Seconds(0),
324 dev0->GetMac(),
325 params,
326 p0);
327
329
330 // MPDU = MAC header (9 bytes) + MSDU (2 bytes)+ MAC trailer (2 bytes) = 13)
331 // MPDU (13 bytes) < 18 bytes therefore IFS = SIFS
332 // SIFS = 12 symbols (192 Microseconds on a 2.4Ghz O-QPSK PHY)
333 ifsSize = m_endIfs - m_ackRxTime;
334 NS_TEST_EXPECT_MSG_EQ(ifsSize,
335 MicroSeconds(192),
336 "Wrong Short InterFrame Space (SIFS) Size after ACK Rx");
337 std::cout << "----------------------------------\n";
338
339 //////////////////////// LIFS after ACK //////////////////
340
341 params.m_txOptions = TX_OPTION_ACK;
342 p0 = Create<Packet>(8);
343
345 Seconds(0),
347 dev0->GetMac(),
348 params,
349 p0);
350
352
353 // MPDU = MAC header (9 bytes) + MSDU (8 bytes)+ MAC trailer (2 bytes) = 19)
354 // MPDU (19 bytes) > 18 bytes therefore IFS = LIFS
355 // LIFS = 40 symbols (640 Microseconds on a 2.4Ghz O-QPSK PHY)
356 ifsSize = m_endIfs - m_ackRxTime;
357 NS_TEST_EXPECT_MSG_EQ(ifsSize,
358 MicroSeconds(640),
359 "Wrong Long InterFrame Space (LIFS) Size after ACK Rx");
360 std::cout << "----------------------------------\n";
361
362 /////////////////////// BCST frame with immediate BCST response //////////////////
363
364 // A packet is broadcasted and the receiving device respond with another broadcast.
365 // The devices are configured to not have any backoff delays in their CSMA/CA.
366 // In most cases, a device receive a packet after its IFS, however in this test,
367 // the receiving device of the reply broadcast will still be in its IFS when the
368 // broadcast is received (i.e. a PHY StartRX () occur before the end of IFS).
369 // This demonstrates that a device can start receiving a frame even during an IFS.
370
371 // Makes the backoff delay period = 0 in the CSMA/CA
372 dev0->GetCsmaCa()->SetMacMinBE(0);
373 dev1->GetCsmaCa()->SetMacMinBE(0);
374
375 p0 = Create<Packet>(50); // 50 bytes of dummy data
376 params.m_dstPanId = 0;
377 params.m_srcAddrMode = SHORT_ADDR;
378 params.m_dstAddrMode = SHORT_ADDR;
379 params.m_dstAddr = Mac16Address("ff:ff");
380 params.m_msduHandle = 0;
381
383 Seconds(0),
385 dev0->GetMac(),
386 params,
387 p0);
388
390
393 "Error, IFS end time should be greater than PHY start Rx time");
394
395 //////////////////////////////////////////////////////////////////////////////////
396
397 // Disconnect traces to eliminate Valgrid errors
398 dev0->GetMac()->TraceDisconnectWithoutContext(
399 "IfsEnd",
401 dev0->GetMac()->TraceDisconnectWithoutContext(
402 "MacRx",
404 dev0->GetPhy()->TraceDisconnectWithoutContext(
405 "PhyRxBegin",
407 dev1->GetMac()->TraceDisconnectWithoutContext(
408 "MacRx",
410
412}
413
414/**
415 * @ingroup lr-wpan-test
416 * @ingroup tests
417 *
418 * @brief LrWpan IFS TestSuite
419 */
420
422{
423 public:
425};
426
432
433static LrWpanIfsTestSuite lrWpanIfsTestSuite; //!< Static variable for test initialization
LrWpan Dataframe transmission with Interframe Space.
static void IfsEnd(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, Time IfsTime)
Function called when the IFS ends.
Time m_endIfs
The time where the Interframe Space ended.
static void DataReceivedDev1(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, Ptr< const Packet >)
Function called when DataConfirm is hit.
void DoRun() override
Implementation to actually run this TestCase.
Time m_phyStartRx
The time the phy start receiving a packet.
Time m_lastTxTime
The time of the last transmitted packet.
static void DataReceivedDev0(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, Ptr< const Packet > p)
Function called when DataReceived is hit.
Time m_ackRxTime
The time of the received acknowledgment.
static void PhyDataRxStart(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, Ptr< const Packet > p)
Function called when PhyDataRxStart is hit.
static void DataConfirm(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, McpsDataConfirmParams params)
Function called when DataConfirm is hit.
LrWpan IFS TestSuite.
This class can contain 16 bit addresses.
bool IsBroadcast() const
Checks if the address is a broadcast address according to 802.15.4 scheme (i.e., 0xFFFF).
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
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
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
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:398
Represent the Mac Header with the Frame Control and Sequence Number fields.
Mac16Address GetShortDstAddr() const
Get the Destination Short address.
bool IsAcknowledgment() const
Returns true if the header is an ack.
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
Callback< void, McpsDataConfirmParams > McpsDataConfirmCallback
This callback is called after a McpsDataRequest has been called from the higher layer.
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:54
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:753
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_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:240
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
Definition test.h:863
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1347
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
static LrWpanIfsTestSuite lrWpanIfsTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:491
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:117
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:118
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
Definition log.h:109
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:509
AddressMode m_dstAddrMode
Destination address mode.
Mac16Address m_dstAddr
Destination address.
uint16_t m_dstPanId
Destination PAN identifier.
AddressMode m_srcAddrMode
Source address mode.