A Discrete-Event Network Simulator
API
lr-wpan-ifs-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 #include "ns3/rng-seed-manager.h"
32 
33 #include <iostream>
34 #include <iomanip>
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE ("lr-wpan-ifs-test");
39 
47 {
48 public:
50  virtual ~LrWpanDataIfsTestCase ();
51 
52 
53 
54 private:
55  static void DataConfirm (LrWpanDataIfsTestCase *testcase,
57  McpsDataConfirmParams params);
58 
59  static void DataReceived (LrWpanDataIfsTestCase *testcase,
62 
63  static void MacState (LrWpanDataIfsTestCase *testcase,
65  LrWpanMacState oldValue,
66  LrWpanMacState newValue);
67 
68 
69 
70  virtual void DoRun (void);
74 
75 
76 };
77 
78 
80  : TestCase ("Lrwpan: IFS with and without ACK")
81 {
82 
83 }
84 
86 {
87 
88 }
89 
90 void
92 {
93  std::cout << Simulator::Now ().GetSeconds () << " | Dataframe Sent\n";
94  testcase->m_lastTxTime = Simulator::Now ();
95 
96 }
97 
98 void
100 {
101  Ptr<Packet> RxPacket = p->Copy ();
102  LrWpanMacHeader receivedMacHdr;
103  RxPacket->RemoveHeader (receivedMacHdr);
104 
105  NS_ASSERT (receivedMacHdr.IsAcknowledgment ());
106  testcase->m_ackRxTime = Simulator::Now ();
107 
108  std::cout << Simulator::Now ().GetSeconds () << " | ACK received\n";
109 }
110 
111 void
113 {
114  // Check the time after the MAC layer go back to IDLE state
115  // (i.e. after the packet has been sent and the IFS is finished)
116 
117  if (newValue == LrWpanMacState::MAC_IDLE)
118  {
119  testcase->m_endIfs = Simulator::Now ();
120  std::cout << Simulator::Now ().GetSeconds () << " | MAC layer is free\n";
121  }
122 
123 }
124 
125 void
127 {
128  // Test of Interframe Spaces (IFS)
129 
130  // The MAC layer needs a finite amount of time to process the data received from the PHY.
131  // To allow this, to successive transmitted frames must be separated for at least one IFS.
132  // The IFS size depends on the transmitted frame. This test verifies that the IFS is correctly
133  // implemented and its size correspond to the situations described by the standard.
134  // For more info see IEEE 802.15.4-2011 Section 5.1.1.3
135 
136 
137  // Create 2 nodes, and a NetDevice for each one
138  Ptr<Node> n0 = CreateObject <Node> ();
139  Ptr<Node> n1 = CreateObject <Node> ();
140 
141  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
142  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
143 
144  dev0->SetAddress (Mac16Address ("00:01"));
145  dev1->SetAddress (Mac16Address ("00:02"));
146 
147  // Each device must be attached to the same channel
148  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
149  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
150  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
151  channel->AddPropagationLossModel (propModel);
152  channel->SetPropagationDelayModel (delayModel);
153 
154  dev0->SetChannel (channel);
155  dev1->SetChannel (channel);
156 
157  // To complete configuration, a LrWpanNetDevice must be added to a node
158  n0->AddDevice (dev0);
159  n1->AddDevice (dev1);
160 
161  // Connect to trace files in the MAC layer
162  dev0->GetMac ()->TraceConnectWithoutContext ("MacStateValue", MakeBoundCallback (&LrWpanDataIfsTestCase::MacState, this, dev0));
163  dev0->GetMac ()->TraceConnectWithoutContext ("MacRx", MakeBoundCallback (&LrWpanDataIfsTestCase::DataReceived, this, dev0));
164 
165  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
166  sender0Mobility->SetPosition (Vector (0,0,0));
167  dev0->GetPhy ()->SetMobility (sender0Mobility);
168  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
169  // Configure position 10 m distance
170  sender1Mobility->SetPosition (Vector (0,10,0));
171  dev1->GetPhy ()->SetMobility (sender1Mobility);
172 
175  dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
176 
177  Ptr<Packet> p0 = Create<Packet> (2);
178  McpsDataRequestParams params;
179  params.m_dstPanId = 0;
180 
181  params.m_srcAddrMode = SHORT_ADDR;
182  params.m_dstAddrMode = SHORT_ADDR;
183  params.m_dstAddr = Mac16Address ("00:02");
184  params.m_msduHandle = 0;
185 
186  Time ifsSize;
187 
189 
190  Simulator::ScheduleWithContext (1, Seconds (0.0),
191  &LrWpanMac::McpsDataRequest,
192  dev0->GetMac (), params, p0);
193 
194 
195  Simulator::Run ();
196 
197  // MPDU = MAC header (11 bytes) + MSDU (2 bytes)+ MAC trailer (2 bytes) = 15)
198  // MPDU (15 bytes) < 18 bytes therefore IFS = SIFS
199  // SIFS = 12 symbols (192 Microseconds on a 2.4Ghz O-QPSK PHY)
200  ifsSize = m_endIfs - m_lastTxTime;
201  NS_TEST_EXPECT_MSG_EQ (ifsSize, Time (MicroSeconds (192)), "Wrong Short InterFrame Space (SIFS) Size after dataframe Tx");
202 
204 
205  p0 = Create<Packet> (6);
206 
207  Simulator::ScheduleWithContext (1, Seconds (0.0),
208  &LrWpanMac::McpsDataRequest,
209  dev0->GetMac (), params, p0);
210 
211 
212  Simulator::Run ();
213 
214  // MPDU = MAC header (11 bytes) + MSDU (6 bytes)+ MAC trailer (2 bytes) = 19)
215  // MPDU (19 bytes) > 18 bytes therefore IFS = LIFS
216  // LIFS = 20 symbols (640 Microseconds on a 2.4Ghz O-QPSK PHY)
217  ifsSize = m_endIfs - m_lastTxTime;
218  NS_TEST_EXPECT_MSG_EQ (ifsSize, Time (MicroSeconds (640)), "Wrong Long InterFrame Space (LIFS) Size after dataframe Tx");
219 
221 
222  params.m_txOptions = TX_OPTION_ACK;
223  p0 = Create<Packet> (2);
224 
225  Simulator::ScheduleWithContext (1, Seconds (0.0),
226  &LrWpanMac::McpsDataRequest,
227  dev0->GetMac (), params, p0);
228 
229  Simulator::Run ();
230 
231  // MPDU = MAC header (11 bytes) + MSDU (2 bytes)+ MAC trailer (2 bytes) = 15)
232  // MPDU (15 bytes) < 18 bytes therefore IFS = SIFS
233  // SIFS = 12 symbols (192 Microseconds on a 2.4Ghz O-QPSK PHY)
234  ifsSize = m_endIfs - m_ackRxTime;
235  NS_TEST_EXPECT_MSG_EQ (ifsSize, Time (MicroSeconds (192)), "Wrong Short InterFrame Space (SIFS) Size after ACK Rx");
236 
238 
239  params.m_txOptions = TX_OPTION_ACK;
240  p0 = Create<Packet> (6);
241 
242  Simulator::ScheduleWithContext (1, Seconds (0.0),
243  &LrWpanMac::McpsDataRequest,
244  dev0->GetMac (), params, p0);
245 
246 
247  Simulator::Run ();
248 
249  // MPDU = MAC header (11 bytes) + MSDU (6 bytes)+ MAC trailer (2 bytes) = 19)
250  // MPDU (19 bytes) > 18 bytes therefore IFS = LIFS
251  // LIFS = 20 symbols (640 Microseconds on a 2.4Ghz O-QPSK PHY)
252  ifsSize = m_endIfs - m_ackRxTime;
253  NS_TEST_EXPECT_MSG_EQ (ifsSize, Time (MicroSeconds (640)), "Wrong Long InterFrame Space (LIFS) Size after ACK Rx");
254 
255  Simulator::Destroy ();
256 
257 }
258 
259 
268 {
269 public:
271 };
272 
274  : TestSuite ("lr-wpan-ifs-test", UNIT)
275 {
276  AddTestCase (new LrWpanDataIfsTestCase, TestCase::QUICK);
277 }
278 
280 
281 
282 
283 
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
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
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:361
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#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
Represent the Mac Header with the Frame Control and Sequence Number fields.
encapsulates test code
Definition: test.h:1153
channel
Definition: third.py:92
static void MacState(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, LrWpanMacState oldValue, LrWpanMacState newValue)
static void DataConfirm(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, McpsDataConfirmParams params)
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:59
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
bool IsAcknowledgment(void) const
Returns true if the header is an ack.
static LrWpanIfsTestSuite lrWpanIfsTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
LrWpan Dataframe transmission with Interframe Space.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
void SetPosition(const Vector &position)
static void DataReceived(LrWpanDataIfsTestCase *testcase, Ptr< LrWpanNetDevice > dev, Ptr< const Packet >)
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
virtual void DoRun(void)
Implementation to actually run this TestCase.
Time m_ackRxTime
The time of the received acknoledgment.
LrWpanMacState
MAC states.
Definition: lr-wpan-mac.h:69
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
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:235
Time m_endIfs
The time where the Interframe Space ended.
LrWpan IFS TestSuite.
static void DataConfirm(McpsDataConfirmParams params)
Definition: lr-wpan-data.cc:47
Time m_lastTxTime
The time of the last transmitted packet.
MAC_IDLE.
Definition: lr-wpan-mac.h:71