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 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE ("lr-wpan-slotted-csma-test");
36 
46 {
47 public:
49  virtual ~LrWpanSlottedCsmacaTestCase ();
50 
51 
52 
53 private:
60  static void TransEndIndication (LrWpanSlottedCsmacaTestCase *testcase,
62  McpsDataConfirmParams params);
73  Ptr<Packet> p);
80  static void StartConfirm (LrWpanSlottedCsmacaTestCase *testcase,
82  MlmeStartConfirmParams params);
83 
91  static void IncomingSuperframeStatus (LrWpanSlottedCsmacaTestCase *testcase,
93  SuperframeStatus oldValue,
94  SuperframeStatus newValue);
95 
102  static void TransactionCost (LrWpanSlottedCsmacaTestCase *testcase,
104  uint32_t trans);
105 
106  virtual void DoRun (void);
107 
111  uint32_t m_transCost;
112 };
113 
114 
116  : TestCase ("Lrwpan: Slotted CSMA-CA test")
117 {
118  m_transCost = 0;
119 }
120 
122 {
123 
124 }
125 
126 void
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 
138 void
140 {
141  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Coordinator Received DATA packet (size " << p->GetSize () << " bytes)");
142 }
143 
144 void
146 {
147  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Beacon Sent");
148 }
149 
150 void
152 {
153  if (newValue == SuperframeStatus::CAP)
154  {
155  testcase->m_startCap = Simulator::Now ();
156  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Incoming superframe CAP starts");
157  }
158 }
159 
160 void
162 {
163  testcase->m_apBoundary = Simulator::Now ();
164  testcase->m_transCost = trans;
165  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Transaction Cost is:" << trans);
166 
167 }
168 
169 
170 
171 
172 
173 void
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
247  MlmeStartRequestParams params;
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  transactionTime = MicroSeconds ((m_transCost - ifsSize) * 1000 * 1000 / symbolRate);
313 
314  NS_TEST_EXPECT_MSG_EQ (m_sentTime,(m_apBoundary + transactionTime),"Error, the transaction time is not the expected value");
315 
316  Simulator::Destroy ();
317 
318 }
319 
320 
329 {
330 public:
332 };
333 
335  : TestSuite ("lr-wpan-slotted-csmaca", UNIT)
336 {
337  AddTestCase (new LrWpanSlottedCsmacaTestCase, TestCase::QUICK);
338 }
339 
341 
342 
static void StartConfirm(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, MlmeStartConfirmParams params)
Function called when MlmeStartConfirm is hit.
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:248
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
static void TransEndIndication(McpsDataConfirmParams params)
Definition: lr-wpan-mlme.cc:60
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Time m_sentTime
Indicates the time after a successful transmission.
LrWpan Slotted CSMA-CA TestSuite.
A suite of tests to run.
Definition: test.h:1343
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1847
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:283
Time m_startCap
The time of the start of the Contention Access Period (CAP).
encapsulates test code
Definition: test.h:1153
SuperframeStatus
Superframe status.
Definition: lr-wpan-mac.h:88
MLME-START.request params.
Definition: lr-wpan-mac.h:288
channel
Definition: third.py:92
static void IncomingSuperframeStatus(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, SuperframeStatus oldValue, SuperframeStatus newValue)
Function called on each Superframe status change (CAP|CFP|INACTIVE).
uint32_t m_transCost
The current transaction cost in symbols.
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
Definition: lr-wpan-mac.h:307
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:260
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:306
uint16_t m_PanId
Pan Identifier used by the device.
Definition: lr-wpan-mac.h:302
LrWpanMcpsDataConfirmStatus m_status
The status of the last MSDU transmission.
Definition: lr-wpan-mac.h:263
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:369
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
static void TransEndIndication(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, McpsDataConfirmParams params)
Function called when McpsDataConfirm is hit.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
Test the correct allocation of DIRECT transmissions in the contention access period (CAP) of the supe...
static void DataIndicationCoordinator(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, McpsDataIndicationParams params, Ptr< Packet > p)
Function called when McpsDataIndication is hit.
void SetPosition(const Vector &position)
Contention Access Period.
Definition: lr-wpan-mac.h:90
Time m_apBoundary
Indicates the time after the calculation of the transaction cost (A boundary of an Active Period in t...
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
static LrWpanSlottedCsmacaTestSuite lrWpanSlottedCsmacaTestSuite
Static variable for test initialization.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
static void StartConfirm(MlmeStartConfirmParams params)
Definition: lr-wpan-mlme.cc:75
virtual void DoRun(void)
Implementation to actually run this TestCase.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1086
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:309
bool m_panCoor
On true this device will become coordinator.
Definition: lr-wpan-mac.h:308
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:235
static void TransactionCost(LrWpanSlottedCsmacaTestCase *testcase, Ptr< LrWpanNetDevice > dev, uint32_t trans)
Function called to indicated the calculated transaction cost in slotted CSMA-CA.
MLME-START.confirm params.
Definition: lr-wpan-mac.h:353
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:270
static void DataIndicationCoordinator(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:70