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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author:
18 * Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
19 */
20
21#include <ns3/constant-position-mobility-model.h>
22#include <ns3/core-module.h>
23#include <ns3/log.h>
24#include <ns3/lr-wpan-module.h>
25#include <ns3/packet.h>
26#include <ns3/propagation-delay-model.h>
27#include <ns3/propagation-loss-model.h>
28#include <ns3/simulator.h>
29#include <ns3/single-model-spectrum-channel.h>
30
31using namespace ns3;
32using namespace ns3::lrwpan;
33
34NS_LOG_COMPONENT_DEFINE("lr-wpan-slotted-csma-test");
35
36/**
37 * \ingroup lr-wpan-test
38 * \ingroup tests
39 *
40 * \brief Test the correct allocation of DIRECT transmissions in the
41 * contention access period (CAP) of the superframe
42 * (Slotted CSMA-CA algorithm).
43 */
45{
46 public:
49
50 private:
51 /**
52 * \brief Function called when McpsDataConfirm is hit.
53 * \param testcase The TestCase.
54 * \param dev The LrWpanNetDevice.
55 * \param params The McpsDataConfirm parameters.
56 */
60 /**
61 * \brief Function called when McpsDataIndication is hit.
62 * \param testcase The TestCase.
63 * \param dev The LrWpanNetDevice.
64 * \param params The McpsDataIndication parameters.
65 * \param p The received packet.
66 */
70 Ptr<Packet> p);
71 /**
72 * \brief Function called when MlmeStartConfirm is hit.
73 * \param testcase The TestCase.
74 * \param dev The LrWpanNetDevice.
75 * \param params The MlmeStartConfirm parameters.
76 */
77 static void StartConfirm(LrWpanSlottedCsmacaTestCase* testcase,
80
81 /**
82 * \brief Function called on each Superframe status change (CAP|CFP|INACTIVE).
83 * \param testcase The TestCase.
84 * \param dev The LrWpanNetDevice.
85 * \param oldValue The previous superframe status.
86 * \param newValue THe new superframe status.
87 */
90 SuperframeStatus oldValue,
91 SuperframeStatus newValue);
92
93 /**
94 * \brief Function called to indicated the calculated transaction cost in slotted CSMA-CA
95 * \param testcase The TestCase.
96 * \param dev The LrWpanNetDevice.
97 * \param trans The transaction cost in symbols.
98 */
99 static void TransactionCost(LrWpanSlottedCsmacaTestCase* testcase,
101 uint32_t trans);
102
103 void DoRun() override;
104
105 Time m_startCap; //!< The time of the start of the Contention Access Period (CAP).
106 Time m_apBoundary; //!< Indicates the time after the calculation of the transaction cost (A
107 //!< boundary of an Active Period in the CAP)
108 Time m_sentTime; //!< Indicates the time after a successful transmission.
109 uint32_t m_transCost; //!< The current transaction cost in symbols.
110};
111
113 : TestCase("Lrwpan: Slotted CSMA-CA test")
114{
115 m_transCost = 0;
116}
117
119{
120}
121
122void
126{
127 // In the case of transmissions with the acknowledgment flag activated, the transmission is only
128 // successful if the acknowledgment was received.
129 if (params.m_status == MacStatus::SUCCESS)
130 {
131 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "s Transmission successfully sent");
132 testcase->m_sentTime = Simulator::Now();
133 }
134}
135
136void
140 Ptr<Packet> p)
141{
143 << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
144}
145
146void
150{
151 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Beacon Sent");
152}
153
154void
157 SuperframeStatus oldValue,
158 SuperframeStatus newValue)
159{
160 if (newValue == SuperframeStatus::CAP)
161 {
162 testcase->m_startCap = Simulator::Now();
163 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Incoming superframe CAP starts");
164 }
165}
166
167void
170 uint32_t trans)
171{
172 testcase->m_apBoundary = Simulator::Now();
173 testcase->m_transCost = trans;
174 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Transaction Cost is:" << trans);
175}
176
177void
179{
180 // Create 2 nodes, and a NetDevice for each one
181 Ptr<Node> n0 = CreateObject<Node>();
182 Ptr<Node> n1 = CreateObject<Node>();
183
184 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
185 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
186
187 dev0->SetAddress(Mac16Address("00:01"));
188 dev1->SetAddress(Mac16Address("00:02"));
189
190 // Each device must be attached to the same channel
191 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
193 CreateObject<LogDistancePropagationLossModel>();
195 CreateObject<ConstantSpeedPropagationDelayModel>();
196 channel->AddPropagationLossModel(propModel);
197 channel->SetPropagationDelayModel(delayModel);
198
199 dev0->SetChannel(channel);
200 dev1->SetChannel(channel);
201
202 // To complete configuration, a LrWpanNetDevice must be added to a node
203 n0->AddDevice(dev0);
204 n1->AddDevice(dev1);
205
206 // Set mobility
207 Ptr<ConstantPositionMobilityModel> sender0Mobility =
208 CreateObject<ConstantPositionMobilityModel>();
209 sender0Mobility->SetPosition(Vector(0, 0, 0));
210 dev0->GetPhy()->SetMobility(sender0Mobility);
211 Ptr<ConstantPositionMobilityModel> sender1Mobility =
212 CreateObject<ConstantPositionMobilityModel>();
213
214 sender1Mobility->SetPosition(Vector(0, 10, 0));
215 dev1->GetPhy()->SetMobility(sender1Mobility);
216
217 // MAC layer and CSMA-CA callback hooks
218
221 dev0->GetMac()->SetMlmeStartConfirmCallback(cb0);
222
225 dev1->GetMac()->SetMcpsDataConfirmCallback(cb1);
226
229 dev1->GetCsmaCa()->SetLrWpanMacTransCostCallback(cb2);
230
233 dev0->GetMac()->SetMcpsDataIndicationCallback(cb5);
234
235 // Connect to trace in the MAC layer
236 dev1->GetMac()->TraceConnectWithoutContext(
237 "MacIncSuperframeStatus",
239
240 // Manual Device Association
241 // Note: We manually associate dev1 device to a PAN coordinator
242 // because currently there is no automatic association behavior;
243 // The PAN COORDINATOR does not need to associate, set
244 // PAN Id or its own coordinator id, these are set
245 // by the MLME-start.request primitive when used.
246
247 dev1->GetMac()->SetPanId(5);
248 dev1->GetMac()->SetAssociatedCoor(Mac16Address("00:01"));
249
250 // Dev0 sets the start time for beacons
252 params.m_panCoor = true;
253 params.m_PanId = 5;
254 params.m_bcnOrd = 14;
255 params.m_sfrmOrd = 6;
257 Seconds(2.0),
259 dev0->GetMac(),
260 params);
261
262 // Dev1 sets the transmission of data packet
263
264 Ptr<Packet> p1 = Create<Packet>(5); // 5 bytes of dummy data
265 McpsDataRequestParams params2;
266 params2.m_dstPanId = 5;
267 params2.m_srcAddrMode = SHORT_ADDR;
268 params2.m_dstAddrMode = SHORT_ADDR;
269 params2.m_dstAddr = Mac16Address("00:01");
270 params2.m_msduHandle = 0;
271
272 // Beacon-enabled | Device to Coordinator | Direct transmission
274 Seconds(2.93),
276 dev1->GetMac(),
277 params2,
278 p1);
279
282
283 Time activePeriodsSum;
284 Time transactionTime;
285 uint64_t symbolRate;
286 uint32_t activePeriodSize = 20;
287 double boundary;
288
289 // Verifies that the CCA checks and the rest of the transaction runs
290 // on a boundary of an Active Period in the slotted CSMA-CA.
291
292 symbolRate = (uint64_t)dev1->GetMac()->GetPhy()->GetDataOrSymbolRate(false);
293 activePeriodsSum = m_apBoundary - m_startCap;
294 boundary = (activePeriodsSum.GetMicroSeconds() * 1000 * 1000 * symbolRate) % activePeriodSize;
295
297 boundary,
298 0,
299 "Error, the transaction is not calculated on a boundary of an Active Period in the CAP");
300
301 // Slotted CSMA-CA needs to precalculate the cost of the transaction to ensure there
302 // is enough time in the CAP to complete the transmission. The following checks that such
303 // pre-calculation matches the time it took to complete the transmission.
304
305 // The calculated transaction includes the IFS time, so we need to subtract its value to compare
306 // it. MPDU = MAC Header + MSDU (payload) Mac Header = 13 bytes If the MPDU is >
307 // aMaxSIFSFrameSize (18 bytes) then IFS = LIFS (40 symbols), else IFS = SIFS (12 symbols)
308
309 uint32_t ifsSize;
310 if (p1->GetSize() > 18)
311 {
312 ifsSize = 40;
313 }
314 else
315 {
316 ifsSize = 12;
317 }
318
319 // The transaction cost here includes the ifsSize and the turnAroundTime (Tx->Rx)
320 // therefore we subtract these before the final comparison
321 //
322 // Transmission Start Transmission End
323 // | |
324 // +-------+--------------------+--------+------------------------+------+
325 // | 2 CCA | TurnAround(Rx->Tx)| Data | TurnAround(Tx->Rx) | IFS |
326 // +-------+--------------------+--------+------------------------+------+
327
328 // TODO: This test need some rework to make it more clear
329
330 transactionTime = Seconds((double)(m_transCost - (ifsSize + 12)) / symbolRate);
331 NS_LOG_UNCOND("Transmission start time(On a boundary): " << m_apBoundary.As(Time::S));
332 NS_LOG_UNCOND("Transmission End time (McpsData.confirm): " << m_sentTime.As(Time::S));
333
335 (m_apBoundary + transactionTime),
336 "Error, the transaction time is not the expected value");
337
339}
340
341/**
342 * \ingroup lr-wpan-test
343 * \ingroup tests
344 *
345 * \brief LrWpan Slotted CSMA-CA TestSuite
346 */
347
349{
350 public:
352};
353
355 : TestSuite("lr-wpan-slotted-csmaca", Type::UNIT)
356{
357 AddTestCase(new LrWpanSlottedCsmacaTestCase, TestCase::Duration::QUICK);
358}
359
361 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.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:413
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...
Definition: lr-wpan-mac.cc:586
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.
Definition: lr-wpan-mac.cc:386
#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:202
SuperframeStatus
Superframe status.
Definition: lr-wpan-mac.h:103
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:767
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
static LrWpanSlottedCsmacaTestSuite lrWpanSlottedCsmacaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MCPS-DATA.indication params.
AddressMode m_dstAddrMode
Destination address mode.
Mac16Address m_dstAddr
Destination address.
uint16_t m_dstPanId
Destination PAN identifier.
AddressMode m_srcAddrMode
Source address mode.
MLME-START.confirm params.
MLME-START.request params.