A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-slotted-csmaca-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/core-module.h>
12#include <ns3/log.h>
13#include <ns3/lr-wpan-module.h>
14#include <ns3/packet.h>
15#include <ns3/propagation-delay-model.h>
16#include <ns3/propagation-loss-model.h>
17#include <ns3/simulator.h>
18#include <ns3/single-model-spectrum-channel.h>
19
20using namespace ns3;
21using namespace ns3::lrwpan;
22
23NS_LOG_COMPONENT_DEFINE("lr-wpan-slotted-csma-test");
24
25/**
26 * @ingroup lr-wpan-test
27 * @ingroup tests
28 *
29 * @brief Test the correct allocation of DIRECT transmissions in the
30 * contention access period (CAP) of the superframe
31 * (Slotted CSMA-CA algorithm).
32 */
34{
35 public:
38
39 private:
40 /**
41 * @brief Function called when McpsDataConfirm is hit.
42 * @param testcase The TestCase.
43 * @param dev The LrWpanNetDevice.
44 * @param params The McpsDataConfirm parameters.
45 */
49 /**
50 * @brief Function called when McpsDataIndication is hit.
51 * @param testcase The TestCase.
52 * @param dev The LrWpanNetDevice.
53 * @param params The McpsDataIndication parameters.
54 * @param p The received packet.
55 */
59 Ptr<Packet> p);
60 /**
61 * @brief Function called when MlmeStartConfirm is hit.
62 * @param testcase The TestCase.
63 * @param dev The LrWpanNetDevice.
64 * @param params The MlmeStartConfirm parameters.
65 */
66 static void StartConfirm(LrWpanSlottedCsmacaTestCase* testcase,
69
70 /**
71 * @brief Function called on each Superframe status change (CAP|CFP|INACTIVE).
72 * @param testcase The TestCase.
73 * @param dev The LrWpanNetDevice.
74 * @param oldValue The previous superframe status.
75 * @param newValue THe new superframe status.
76 */
79 SuperframeStatus oldValue,
80 SuperframeStatus newValue);
81
82 /**
83 * @brief Function called to indicated the calculated transaction cost in slotted CSMA-CA
84 * @param testcase The TestCase.
85 * @param dev The LrWpanNetDevice.
86 * @param trans The transaction cost in symbols.
87 */
88 static void TransactionCost(LrWpanSlottedCsmacaTestCase* testcase,
90 uint32_t trans);
91
92 void DoRun() override;
93
94 Time m_startCap; //!< The time of the start of the Contention Access Period (CAP).
95 Time m_apBoundary; //!< Indicates the time after the calculation of the transaction cost (A
96 //!< boundary of an Active Period in the CAP)
97 Time m_sentTime; //!< Indicates the time after a successful transmission.
98 uint32_t m_transCost; //!< The current transaction cost in symbols.
99};
100
102 : TestCase("Lrwpan: Slotted CSMA-CA test")
103{
104 m_transCost = 0;
105}
106
110
111void
115{
116 // In the case of transmissions with the acknowledgment flag activated, the transmission is only
117 // successful if the acknowledgment was received.
118 if (params.m_status == MacStatus::SUCCESS)
119 {
120 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "s Transmission successfully sent");
121 testcase->m_sentTime = Simulator::Now();
122 }
123}
124
125void
129 Ptr<Packet> p)
130{
132 << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
133}
134
135void
142
143void
146 SuperframeStatus oldValue,
147 SuperframeStatus newValue)
148{
149 if (newValue == SuperframeStatus::CAP)
150 {
151 testcase->m_startCap = Simulator::Now();
152 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Incoming superframe CAP starts");
153 }
154}
155
156void
159 uint32_t trans)
160{
161 testcase->m_apBoundary = Simulator::Now();
162 testcase->m_transCost = trans;
163 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Transaction Cost is:" << trans);
164}
165
166void
168{
169 // Create 2 nodes, and a NetDevice for each one
172
175
176 dev0->SetAddress(Mac16Address("00:01"));
177 dev1->SetAddress(Mac16Address("00:02"));
178
179 // Each device must be attached to the same channel
185 channel->AddPropagationLossModel(propModel);
186 channel->SetPropagationDelayModel(delayModel);
187
188 dev0->SetChannel(channel);
189 dev1->SetChannel(channel);
190
191 // To complete configuration, a LrWpanNetDevice must be added to a node
192 n0->AddDevice(dev0);
193 n1->AddDevice(dev1);
194
195 // Set mobility
196 Ptr<ConstantPositionMobilityModel> sender0Mobility =
198 sender0Mobility->SetPosition(Vector(0, 0, 0));
199 dev0->GetPhy()->SetMobility(sender0Mobility);
200 Ptr<ConstantPositionMobilityModel> sender1Mobility =
202
203 sender1Mobility->SetPosition(Vector(0, 10, 0));
204 dev1->GetPhy()->SetMobility(sender1Mobility);
205
206 // MAC layer and CSMA-CA callback hooks
207
210 dev0->GetMac()->SetMlmeStartConfirmCallback(cb0);
211
214 dev1->GetMac()->SetMcpsDataConfirmCallback(cb1);
215
218 dev1->GetCsmaCa()->SetLrWpanMacTransCostCallback(cb2);
219
222 dev0->GetMac()->SetMcpsDataIndicationCallback(cb5);
223
224 // Connect to trace in the MAC layer
225 dev1->GetMac()->TraceConnectWithoutContext(
226 "MacIncSuperframeStatus",
228
229 // Manual Device Association
230 // Note: We manually associate dev1 device to a PAN coordinator
231 // because currently there is no automatic association behavior;
232 // The PAN COORDINATOR does not need to associate, set
233 // PAN Id or its own coordinator id, these are set
234 // by the MLME-start.request primitive when used.
235
236 dev1->GetMac()->SetPanId(5);
237 dev1->GetMac()->SetAssociatedCoor(Mac16Address("00:01"));
238
239 // Dev0 sets the start time for beacons
241 params.m_panCoor = true;
242 params.m_PanId = 5;
243 params.m_bcnOrd = 14;
244 params.m_sfrmOrd = 6;
246 Seconds(2),
248 dev0->GetMac(),
249 params);
250
251 // Dev1 sets the transmission of data packet
252
253 Ptr<Packet> p1 = Create<Packet>(5); // 5 bytes of dummy data
254 McpsDataRequestParams params2;
255 params2.m_dstPanId = 5;
256 params2.m_srcAddrMode = SHORT_ADDR;
257 params2.m_dstAddrMode = SHORT_ADDR;
258 params2.m_dstAddr = Mac16Address("00:01");
259 params2.m_msduHandle = 0;
260
261 // Beacon-enabled | Device to Coordinator | Direct transmission
263 Seconds(2.93),
265 dev1->GetMac(),
266 params2,
267 p1);
268
271
272 Time activePeriodsSum;
273 Time transactionTime;
274 uint64_t symbolRate;
275 uint32_t activePeriodSize = 20;
276 double boundary;
277
278 // Verifies that the CCA checks and the rest of the transaction runs
279 // on a boundary of an Active Period in the slotted CSMA-CA.
280
281 symbolRate = (uint64_t)dev1->GetMac()->GetPhy()->GetDataOrSymbolRate(false);
282 activePeriodsSum = m_apBoundary - m_startCap;
283 boundary = (activePeriodsSum.GetMicroSeconds() * 1000 * 1000 * symbolRate) % activePeriodSize;
284
286 boundary,
287 0,
288 "Error, the transaction is not calculated on a boundary of an Active Period in the CAP");
289
290 // Slotted CSMA-CA needs to precalculate the cost of the transaction to ensure there
291 // is enough time in the CAP to complete the transmission. The following checks that such
292 // pre-calculation matches the time it took to complete the transmission.
293
294 // The calculated transaction includes the IFS time, so we need to subtract its value to compare
295 // it. MPDU = MAC Header + MSDU (payload) Mac Header = 13 bytes If the MPDU is >
296 // aMaxSIFSFrameSize (18 bytes) then IFS = LIFS (40 symbols), else IFS = SIFS (12 symbols)
297
298 uint32_t ifsSize;
299 if (p1->GetSize() > 18)
300 {
301 ifsSize = 40;
302 }
303 else
304 {
305 ifsSize = 12;
306 }
307
308 // The transaction cost here includes the ifsSize and the turnAroundTime (Tx->Rx)
309 // therefore we subtract these before the final comparison
310 //
311 // Transmission Start Transmission End
312 // | |
313 // +-------+--------------------+--------+------------------------+------+
314 // | 2 CCA | TurnAround(Rx->Tx)| Data | TurnAround(Tx->Rx) | IFS |
315 // +-------+--------------------+--------+------------------------+------+
316
317 // TODO: This test need some rework to make it more clear
318
319 transactionTime = Seconds((double)(m_transCost - (ifsSize + 12)) / symbolRate);
320 NS_LOG_UNCOND("Transmission start time(On a boundary): " << m_apBoundary.As(Time::S));
321 NS_LOG_UNCOND("Transmission End time (McpsData.confirm): " << m_sentTime.As(Time::S));
322
324 (m_apBoundary + transactionTime),
325 "Error, the transaction time is not the expected value");
326
328}
329
330/**
331 * @ingroup lr-wpan-test
332 * @ingroup tests
333 *
334 * @brief LrWpan Slotted CSMA-CA TestSuite
335 */
336
338{
339 public:
341};
342
344 : TestSuite("lr-wpan-slotted-csmaca", Type::UNIT)
345{
346 AddTestCase(new LrWpanSlottedCsmacaTestCase, TestCase::Duration::QUICK);
347}
348
350 lrWpanSlottedCsmacaTestSuite; //!< Static variable for test initialization
Test the correct allocation of DIRECT transmissions in the contention access period (CAP) of the supe...
Time m_sentTime
Indicates the time after a successful transmission.
Time m_startCap
The time of the start of the Contention Access Period (CAP).
static void DataIndicationCoordinator(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, McpsDataIndicationParams params, Ptr< Packet > p)
Function called when McpsDataIndication is hit.
static void StartConfirm(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, MlmeStartConfirmParams params)
Function called when MlmeStartConfirm is hit.
static void TransEndIndication(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, McpsDataConfirmParams params)
Function called when McpsDataConfirm is hit.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_transCost
The current transaction cost in symbols.
static void IncomingSuperframeStatus(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, SuperframeStatus oldValue, SuperframeStatus newValue)
Function called on each Superframe status change (CAP|CFP|INACTIVE).
static void TransactionCost(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, uint32_t trans)
Function called to indicated the calculated transaction cost in slotted CSMA-CA.
Time m_apBoundary
Indicates the time after the calculation of the transaction cost (A boundary of an Active Period in t...
LrWpan Slotted CSMA-CA TestSuite.
This class can contain 16 bit addresses.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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
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
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:402
void MlmeStartRequest(MlmeStartRequestParams params) override
IEEE 802.15.4-2006, section 7.1.14.1 MLME-START.request Request to allow a PAN coordinator to initiat...
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_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
SuperframeStatus
Superframe status.
Definition lr-wpan-mac.h:92
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
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:436
#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:1344
static LrWpanSlottedCsmacaTestSuite lrWpanSlottedCsmacaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
AddressMode m_dstAddrMode
Destination address mode.
Mac16Address m_dstAddr
Destination address.
uint16_t m_dstPanId
Destination PAN identifier.
AddressMode m_srcAddrMode
Source address mode.