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;
32
33NS_LOG_COMPONENT_DEFINE("lr-wpan-slotted-csma-test");
34
35/**
36 * \ingroup lr-wpan-test
37 * \ingroup tests
38 *
39 * \brief Test the correct allocation of DIRECT transmissions in the
40 * contention access period (CAP) of the superframe
41 * (Slotted CSMA-CA algorithm).
42 */
44{
45 public:
48
49 private:
50 /**
51 * \brief Function called when McpsDataConfirm is hit.
52 * \param testcase The TestCase.
53 * \param dev The LrWpanNetDevice.
54 * \param params The McpsDataConfirm parameters.
55 */
59 /**
60 * \brief Function called when McpsDataIndication is hit.
61 * \param testcase The TestCase.
62 * \param dev The LrWpanNetDevice.
63 * \param params The McpsDataIndication parameters.
64 * \param p The received packet.
65 */
69 Ptr<Packet> p);
70 /**
71 * \brief Function called when MlmeStartConfirm is hit.
72 * \param testcase The TestCase.
73 * \param dev The LrWpanNetDevice.
74 * \param params The MlmeStartConfirm parameters.
75 */
76 static void StartConfirm(LrWpanSlottedCsmacaTestCase* testcase,
79
80 /**
81 * \brief Function called on each Superframe status change (CAP|CFP|INACTIVE).
82 * \param testcase The TestCase.
83 * \param dev The LrWpanNetDevice.
84 * \param oldValue The previous superframe status.
85 * \param newValue THe new superframe status.
86 */
89 SuperframeStatus oldValue,
90 SuperframeStatus newValue);
91
92 /**
93 * \brief Function called to indicated the calculated transaction cost in slotted CSMA-CA
94 * \param testcase The TestCase.
95 * \param dev The LrWpanNetDevice.
96 * \param trans The transaction cost in symbols.
97 */
98 static void TransactionCost(LrWpanSlottedCsmacaTestCase* testcase,
100 uint32_t trans);
101
102 void DoRun() override;
103
104 Time m_startCap; //!< The time of the start of the Contention Access Period (CAP).
105 Time m_apBoundary; //!< Indicates the time after the calculation of the transaction cost (A
106 //!< boundary of an Active Period in the CAP)
107 Time m_sentTime; //!< Indicates the time after a successful transmission.
108 uint32_t m_transCost; //!< The current transaction cost in symbols.
109};
110
112 : TestCase("Lrwpan: Slotted CSMA-CA test")
113{
114 m_transCost = 0;
115}
116
118{
119}
120
121void
125{
126 // In the case of transmissions with the acknowledgment flag activated, the transmission is only
127 // successful if the acknowledgment was received.
128 if (params.m_status == LrWpanMacStatus::SUCCESS)
129 {
130 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "s Transmission successfully sent");
131 testcase->m_sentTime = Simulator::Now();
132 }
133}
134
135void
139 Ptr<Packet> p)
140{
142 << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
143}
144
145void
149{
150 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Beacon Sent");
151}
152
153void
156 SuperframeStatus oldValue,
157 SuperframeStatus newValue)
158{
159 if (newValue == SuperframeStatus::CAP)
160 {
161 testcase->m_startCap = Simulator::Now();
162 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Incoming superframe CAP starts");
163 }
164}
165
166void
169 uint32_t trans)
170{
171 testcase->m_apBoundary = Simulator::Now();
172 testcase->m_transCost = trans;
173 NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Transaction Cost is:" << trans);
174}
175
176void
178{
179 // Create 2 nodes, and a NetDevice for each one
180 Ptr<Node> n0 = CreateObject<Node>();
181 Ptr<Node> n1 = CreateObject<Node>();
182
183 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
184 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
185
186 dev0->SetAddress(Mac16Address("00:01"));
187 dev1->SetAddress(Mac16Address("00:02"));
188
189 // Each device must be attached to the same channel
190 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
192 CreateObject<LogDistancePropagationLossModel>();
194 CreateObject<ConstantSpeedPropagationDelayModel>();
195 channel->AddPropagationLossModel(propModel);
196 channel->SetPropagationDelayModel(delayModel);
197
198 dev0->SetChannel(channel);
199 dev1->SetChannel(channel);
200
201 // To complete configuration, a LrWpanNetDevice must be added to a node
202 n0->AddDevice(dev0);
203 n1->AddDevice(dev1);
204
205 // Set mobility
206 Ptr<ConstantPositionMobilityModel> sender0Mobility =
207 CreateObject<ConstantPositionMobilityModel>();
208 sender0Mobility->SetPosition(Vector(0, 0, 0));
209 dev0->GetPhy()->SetMobility(sender0Mobility);
210 Ptr<ConstantPositionMobilityModel> sender1Mobility =
211 CreateObject<ConstantPositionMobilityModel>();
212
213 sender1Mobility->SetPosition(Vector(0, 10, 0));
214 dev1->GetPhy()->SetMobility(sender1Mobility);
215
216 // MAC layer and CSMA-CA callback hooks
217
220 dev0->GetMac()->SetMlmeStartConfirmCallback(cb0);
221
224 dev1->GetMac()->SetMcpsDataConfirmCallback(cb1);
225
228 dev1->GetCsmaCa()->SetLrWpanMacTransCostCallback(cb2);
229
232 dev0->GetMac()->SetMcpsDataIndicationCallback(cb5);
233
234 // Connect to trace in the MAC layer
235 dev1->GetMac()->TraceConnectWithoutContext(
236 "MacIncSuperframeStatus",
238
239 // Manual Device Association
240 // Note: We manually associate dev1 device to a PAN coordinator
241 // because currently there is no automatic association behavior;
242 // The PAN COORDINATOR does not need to associate, set
243 // PAN Id or its own coordinator id, these are set
244 // by the MLME-start.request primitive when used.
245
246 dev1->GetMac()->SetPanId(5);
247 dev1->GetMac()->SetAssociatedCoor(Mac16Address("00:01"));
248
249 // Dev0 sets the start time for beacons
251 params.m_panCoor = true;
252 params.m_PanId = 5;
253 params.m_bcnOrd = 14;
254 params.m_sfrmOrd = 6;
256 Seconds(2.0),
258 dev0->GetMac(),
259 params);
260
261 // Dev1 sets the transmission of data packet
262
263 Ptr<Packet> p1 = Create<Packet>(5); // 5 bytes of dummy data
264 McpsDataRequestParams params2;
265 params2.m_dstPanId = 5;
266 params2.m_srcAddrMode = SHORT_ADDR;
267 params2.m_dstAddrMode = SHORT_ADDR;
268 params2.m_dstAddr = Mac16Address("00:01");
269 params2.m_msduHandle = 0;
270
271 // Beacon-enabled | Device to Coordinator | Direct transmission
273 Seconds(2.93),
275 dev1->GetMac(),
276 params2,
277 p1);
278
281
282 Time activePeriodsSum;
283 Time transactionTime;
284 uint64_t symbolRate;
285 uint32_t activePeriodSize = 20;
286 double boundary;
287
288 // Verifies that the CCA checks and the rest of the transaction runs
289 // on a boundary of an Active Period in the slotted CSMA-CA.
290
291 symbolRate = (uint64_t)dev1->GetMac()->GetPhy()->GetDataOrSymbolRate(false);
292 activePeriodsSum = m_apBoundary - m_startCap;
293 boundary = (activePeriodsSum.GetMicroSeconds() * 1000 * 1000 * symbolRate) % activePeriodSize;
294
296 boundary,
297 0,
298 "Error, the transaction is not calculated on a boundary of an Active Period in the CAP");
299
300 // Slotted CSMA-CA needs to precalculate the cost of the transaction to ensure there
301 // is enough time in the CAP to complete the transmission. The following checks that such
302 // pre-calculation matches the time it took to complete the transmission.
303
304 // The calculated transaction includes the IFS time, so we need to subtract its value to compare
305 // it. MPDU = MAC Header + MSDU (payload) Mac Header = 13 bytes If the MPDU is >
306 // aMaxSIFSFrameSize (18 bytes) then IFS = LIFS (40 symbols), else IFS = SIFS (12 symbols)
307
308 uint32_t ifsSize;
309 if (p1->GetSize() > 18)
310 {
311 ifsSize = 40;
312 }
313 else
314 {
315 ifsSize = 12;
316 }
317
318 // The transaction cost here includes the ifsSize and the turnAroundTime (Tx->Rx)
319 // therefore we subtract these before the final comparison
320 //
321 // Transmission Start Transmission End
322 // | |
323 // +-------+--------------------+--------+------------------------+------+
324 // | 2 CCA | TurnAround(Rx->Tx)| Data | TurnAround(Tx->Rx) | IFS |
325 // +-------+--------------------+--------+------------------------+------+
326
327 // TODO: This test need some rework to make it more clear
328
329 transactionTime = Seconds((double)(m_transCost - (ifsSize + 12)) / symbolRate);
330 NS_LOG_UNCOND("Transmission start time(On a boundary): " << m_apBoundary.As(Time::S));
331 NS_LOG_UNCOND("Transmission End time (McpsData.confirm): " << m_sentTime.As(Time::S));
332
334 (m_apBoundary + transactionTime),
335 "Error, the transaction time is not the expected value");
336
338}
339
340/**
341 * \ingroup lr-wpan-test
342 * \ingroup tests
343 *
344 * \brief LrWpan Slotted CSMA-CA TestSuite
345 */
346
348{
349 public:
351};
352
354 : TestSuite("lr-wpan-slotted-csmaca", Type::UNIT)
355{
356 AddTestCase(new LrWpanSlottedCsmacaTestCase, TestCase::Duration::QUICK);
357}
358
360 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.
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:384
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:584
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
#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:100
@ SHORT_ADDR
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:765
#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:1326
static LrWpanSlottedCsmacaTestSuite lrWpanSlottedCsmacaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MCPS-DATA.confirm params.
MCPS-DATA.indication params.
MCPS-DATA.request params.
LrWpanAddressMode m_srcAddrMode
Source address mode.
LrWpanAddressMode m_dstAddrMode
Destination address mode.
uint16_t m_dstPanId
Destination PAN identifier.
Mac16Address m_dstAddr
Destination address.
uint8_t m_msduHandle
MSDU handle.
MLME-START.confirm params.
MLME-START.request params.