A Discrete-Event Network Simulator
API
lr-wpan-slotted-csmaca-test.cc
Go to the documentation of this file.
1/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author:
19 * Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
20 */
21
22#include <ns3/log.h>
23#include <ns3/core-module.h>
24#include <ns3/lr-wpan-module.h>
25#include <ns3/propagation-loss-model.h>
26#include <ns3/propagation-delay-model.h>
27#include <ns3/simulator.h>
28#include <ns3/single-model-spectrum-channel.h>
29#include <ns3/constant-position-mobility-model.h>
30#include <ns3/packet.h>
31
32
33using namespace ns3;
34
35NS_LOG_COMPONENT_DEFINE ("lr-wpan-slotted-csma-test");
36
46{
47public:
50
51
52
53private:
73 Ptr<Packet> p);
80 static void StartConfirm (LrWpanSlottedCsmacaTestCase *testcase,
83
93 SuperframeStatus oldValue,
94 SuperframeStatus newValue);
95
102 static void TransactionCost (LrWpanSlottedCsmacaTestCase *testcase,
104 uint32_t trans);
105
106 virtual void DoRun (void);
107
112};
113
114
116 : TestCase ("Lrwpan: Slotted CSMA-CA test")
117{
118 m_transCost = 0;
119}
120
122{
123
124}
125
126void
128{
129 // In the case of transmissions with the acknowledgment flag activated, the transmission is only
130 // successful if the acknowledgment was received.
132 {
133 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Transmission successfully sent");
134 testcase->m_sentTime = Simulator::Now ();
135 }
136}
137
138void
140{
141 NS_LOG_UNCOND (Simulator::Now ().As(Time::S) << "s Coordinator Received DATA packet (size " << p->GetSize () << " bytes)");
142}
143
144void
146{
147 NS_LOG_UNCOND (Simulator::Now ().As(Time::S) << "s Beacon Sent");
148}
149
150void
152{
153 if (newValue == SuperframeStatus::CAP)
154 {
155 testcase->m_startCap = Simulator::Now ();
156 NS_LOG_UNCOND (Simulator::Now ().As(Time::S) << "s Incoming superframe CAP starts");
157 }
158}
159
160void
162{
163 testcase->m_apBoundary = Simulator::Now ();
164 testcase->m_transCost = trans;
165 NS_LOG_UNCOND (Simulator::Now ().As(Time::S) << "s Transaction Cost is:" << trans);
166
167}
168
169
170
171
172
173void
175{
176 // Create 2 nodes, and a NetDevice for each one
177 Ptr<Node> n0 = CreateObject <Node> ();
178 Ptr<Node> n1 = CreateObject <Node> ();
179
180 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
181 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
182
183 dev0->SetAddress (Mac16Address ("00:01"));
184 dev1->SetAddress (Mac16Address ("00:02"));
185
186 // Each device must be attached to the same channel
187 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
188 Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
189 Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
190 channel->AddPropagationLossModel (propModel);
191 channel->SetPropagationDelayModel (delayModel);
192
193 dev0->SetChannel (channel);
194 dev1->SetChannel (channel);
195
196 // To complete configuration, a LrWpanNetDevice must be added to a node
197 n0->AddDevice (dev0);
198 n1->AddDevice (dev1);
199
200
201 // Set mobility
202 Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
203 sender0Mobility->SetPosition (Vector (0,0,0));
204 dev0->GetPhy ()->SetMobility (sender0Mobility);
205 Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
206
207 sender1Mobility->SetPosition (Vector (0,10,0));
208 dev1->GetPhy ()->SetMobility (sender1Mobility);
209
210
211 // MAC layer and CSMA-CA callback hooks
212
215 dev0->GetMac ()->SetMlmeStartConfirmCallback (cb0);
216
219 dev1->GetMac ()->SetMcpsDataConfirmCallback (cb1);
220
223 dev1->GetCsmaCa ()->SetLrWpanMacTransCostCallback (cb2);
224
227 dev0->GetMac ()->SetMcpsDataIndicationCallback (cb5);
228
229
230 // Connect to trace in the MAC layer
231 dev1->GetMac ()->TraceConnectWithoutContext ("MacIncSuperframeStatus",
233
234
235 // Manual Device Association
236 // Note: We manually associate dev1 device to a PAN coordinator
237 // because currently there is no automatic association behavior;
238 // The PAN COORDINATOR does not need to associate, set
239 // PAN Id or its own coordinator id, these are set
240 // by the MLME-start.request primitive when used.
241
242 dev1->GetMac ()->SetPanId (5);
243 dev1->GetMac ()->SetAssociatedCoor (Mac16Address ("00:01"));
244
245
246 // Dev0 sets the start time for beacons
248 params.m_panCoor = true;
249 params.m_PanId = 5;
250 params.m_bcnOrd = 14;
251 params.m_sfrmOrd = 6;
252 Simulator::ScheduleWithContext (1, Seconds (2.0),
253 &LrWpanMac::MlmeStartRequest,
254 dev0->GetMac (), params);
255
256 // Dev1 sets the transmission of data packet
257
258 Ptr<Packet> p1 = Create<Packet> (5); // 5 bytes of dummy data
259 McpsDataRequestParams params2;
260 params2.m_dstPanId = 5;
261 params2.m_srcAddrMode = SHORT_ADDR;
262 params2.m_dstAddrMode = SHORT_ADDR;
263 params2.m_dstAddr = Mac16Address ("00:01");
264 params2.m_msduHandle = 0;
265
266
267 // Beacon-enabled | Device to Coordinator | Direct transmission
268 Simulator::ScheduleWithContext (1, Seconds (2.93),
269 &LrWpanMac::McpsDataRequest,
270 dev1->GetMac (), params2, p1);
271
272
273 Simulator::Stop (Seconds (4));
274 Simulator::Run ();
275
276 Time activePeriodsSum;
277 Time transactionTime;
278 uint64_t symbolRate;
279 uint32_t activePeriodSize = 20;
280 double boundary;
281
282
283 // Verifies that the CCA checks and the rest of the transaction runs
284 // on a boundary of an Active Period in the slotted CSMA-CA.
285
286 symbolRate = (uint64_t) dev1->GetMac ()->GetPhy ()->GetDataOrSymbolRate (false);
287 activePeriodsSum = m_apBoundary - m_startCap;
288 boundary = (activePeriodsSum.GetMicroSeconds () * 1000 * 1000 * symbolRate) % activePeriodSize;
289
290 NS_TEST_EXPECT_MSG_EQ (boundary, 0, "Error, the transaction is not calculated on a boundary of an Active Period in the CAP");
291
292
293 // Slotted CSMA-CA needs to precalculate the cost of the transaction to ensure there
294 // is enough time in the CAP to complete the transmission. The following checks that such
295 // pre-calculation matches the time it took to complete the transmission.
296
297 // The calculated transaction includes the IFS time, so we need to subtract its value to compare it.
298 // MPDU = MAC Header + MSDU (payload)
299 // Mac Header = 13 bytes
300 // If the MPDU is > aMaxSIFSFrameSize (18 bytes) then IFS = LIFS (40 symbols), else IFS = SIFS (12 symbols)
301
302 uint32_t ifsSize;
303 if (p1->GetSize () > 18)
304 {
305 ifsSize = 40;
306 }
307 else
308 {
309 ifsSize = 12;
310 }
311
312 // The transaction cost here includes the ifsSize and the turnAroundTime (Tx->Rx)
313 // therefore we subtract these before the final comparison
314 //
315 // Transmission Start Transmission End
316 // | |
317 // +-------+--------------------+--------+------------------------+------+
318 // | 2 CCA | TurnAround(Rx->Tx)| Data | TurnAround(Tx->Rx) | IFS |
319 // +-------+--------------------+--------+------------------------+------+
320
321 // TODO: This test need some rework to make it more clear
322
323 transactionTime = Seconds ((double)(m_transCost-(ifsSize + 12)) / symbolRate);
324 NS_LOG_UNCOND ("Transmission start time(On a boundary): "<<m_apBoundary.As(Time::S));
325 NS_LOG_UNCOND ("Transmission End time (McpsData.confirm): "<<m_sentTime.As(Time::S));
326
327 NS_TEST_EXPECT_MSG_EQ (m_sentTime,(m_apBoundary + transactionTime),"Error, the transaction time is not the expected value");
328
329 Simulator::Destroy ();
330
331}
332
333
342{
343public:
345};
346
348 : TestSuite ("lr-wpan-slotted-csmaca", UNIT)
349{
350 AddTestCase (new LrWpanSlottedCsmacaTestCase, TestCase::QUICK);
351}
352
354
355
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.
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...
virtual void DoRun(void)
Implementation to actually run this TestCase.
LrWpan Slotted CSMA-CA TestSuite.
virtual void SetAddress(Address address)
This method indirects to LrWpanMac::SetShortAddress ()
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
Ptr< LrWpanCsmaCa > GetCsmaCa(void) const
Get the CSMA/CA implementation used by this NetDevice.
This class can contain 16 bit addresses.
Definition: mac16-address.h:42
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:387
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:432
#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:205
SuperframeStatus
Superframe status.
Definition: lr-wpan-mac.h:89
@ IEEE_802_15_4_SUCCESS
Definition: lr-wpan-mac.h:166
@ CAP
Contention Access Period.
Definition: lr-wpan-mac.h:91
@ SHORT_ADDR
Definition: lr-wpan-mac.h:141
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1709
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static LrWpanSlottedCsmacaTestSuite lrWpanSlottedCsmacaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:92
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:261
LrWpanMcpsDataConfirmStatus m_status
The status of the last MSDU transmission.
Definition: lr-wpan-mac.h:263
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:271
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:237
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:246
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:247
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:248
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:249
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:251
MLME-START.confirm params.
Definition: lr-wpan-mac.h:351
MLME-START.request params.
Definition: lr-wpan-mac.h:289
bool m_panCoor
On true this device will become coordinator.
Definition: lr-wpan-mac.h:307
uint8_t m_bcnOrd
Beacon Order, Used to calculate the beacon interval, a value of 15 indicates no periodic beacons will...
Definition: lr-wpan-mac.h:305
uint16_t m_PanId
Pan Identifier used by the device.
Definition: lr-wpan-mac.h:301
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
Definition: lr-wpan-mac.h:306