A Discrete-Event Network Simulator
API
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 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE("lr-wpan-slotted-csma-test");
34 
44 {
45  public:
47  ~LrWpanSlottedCsmacaTestCase() override;
48 
49  private:
56  static void TransEndIndication(LrWpanSlottedCsmacaTestCase* testcase,
69  Ptr<Packet> p);
76  static void StartConfirm(LrWpanSlottedCsmacaTestCase* testcase,
79 
87  static void IncomingSuperframeStatus(LrWpanSlottedCsmacaTestCase* testcase,
89  SuperframeStatus oldValue,
90  SuperframeStatus newValue);
91 
98  static void TransactionCost(LrWpanSlottedCsmacaTestCase* testcase,
100  uint32_t trans);
101 
102  void DoRun() override;
103 
108  uint32_t m_transCost;
109 };
110 
112  : TestCase("Lrwpan: Slotted CSMA-CA test")
113 {
114  m_transCost = 0;
115 }
116 
118 {
119 }
120 
121 void
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 
135 void
139  Ptr<Packet> p)
140 {
141  NS_LOG_UNCOND(Simulator::Now().As(Time::S)
142  << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
143 }
144 
145 void
149 {
150  NS_LOG_UNCOND(Simulator::Now().As(Time::S) << "s Beacon Sent");
151 }
152 
153 void
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 
166 void
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 
176 void
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;
255  Simulator::ScheduleWithContext(1,
256  Seconds(2.0),
257  &LrWpanMac::MlmeStartRequest,
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
272  Simulator::ScheduleWithContext(1,
273  Seconds(2.93),
274  &LrWpanMac::McpsDataRequest,
275  dev1->GetMac(),
276  params2,
277  p1);
278 
279  Simulator::Stop(Seconds(4));
280  Simulator::Run();
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 
337  Simulator::Destroy();
338 }
339 
348 {
349  public:
351 };
352 
354  : TestSuite("lr-wpan-slotted-csmaca", UNIT)
355 {
356  AddTestCase(new LrWpanSlottedCsmacaTestCase, TestCase::QUICK);
357 }
358 
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 SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
Ptr< LrWpanCsmaCa > GetCsmaCa() const
Get the CSMA/CA implementation used by this NetDevice.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
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
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
@ CAP
Contention Access Period.
Definition: lr-wpan-mac.h:102
@ SHORT_ADDR
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:765
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void DataIndicationCoordinator(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:74
void TransEndIndication(McpsDataConfirmParams params)
Definition: lr-wpan-mlme.cc:63
void StartConfirm(MlmeStartConfirmParams params)
Definition: lr-wpan-mlme.cc:81
static LrWpanSlottedCsmacaTestSuite lrWpanSlottedCsmacaTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ SUCCESS
Definition: ff-mac-common.h:62
channel
Definition: third.py:88
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
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.