A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
lr-wpan-ack-test.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Fraunhofer FKIE
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  * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
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 
35 using namespace ns3;
36 
37 NS_LOG_COMPONENT_DEFINE ("lr-wpan-ack-test");
38 
39 using namespace ns3;
40 
42 {
43 public:
45 
47  static void DataConfirm (LrWpanAckTestCase *testCase, Ptr<LrWpanNetDevice> dev, McpsDataConfirmParams params);
48 
49 private:
50  virtual void DoRun (void);
51 
57 };
58 
60  : TestCase ("Test the 802.15.4 ACK handling")
61 {
62  m_requestTime = Seconds (0);
63  m_requestAckTime = Seconds (0);
64  m_replyTime = Seconds (0);
65  m_replyAckTime = Seconds (0);
66  m_replyArrivalTime = Seconds (0);
67 }
68 
69 void
71 {
72  if (dev->GetAddress () == Mac16Address ("00:02"))
73  {
74  Ptr<Packet> p = Create<Packet> (10); // 10 bytes of dummy data
75  McpsDataRequestParams params;
76  params.m_srcAddrMode = SHORT_ADDR;
77  params.m_dstAddrMode = SHORT_ADDR;
78  params.m_dstPanId = 0;
79  params.m_dstAddr = Mac16Address ("00:01");
80  params.m_msduHandle = 0;
81  params.m_txOptions = TX_OPTION_NONE;
82 
83  testCase->m_replyTime = Simulator::Now ();
84  dev->GetMac ()->McpsDataRequest (params, p);
85  }
86  else
87  {
88  testCase->m_replyArrivalTime = Simulator::Now ();
89  }
90 }
91 
92 void
94 {
95  if (dev->GetAddress () == Mac16Address ("00:01"))
96  {
97  testCase->m_requestAckTime = Simulator::Now ();
98  }
99  else
100  {
101  testCase->m_replyAckTime = Simulator::Now ();
102  }
103 }
104 
105 void
107 {
108  // Test setup:
109  // Two nodes well in communication range.
110  // Node 1 sends a request packet to node 2 with ACK request bit set. Node 2
111  // immediately answers with a reply packet on receiption of the request.
112  // We expect the ACK of the request packet to always arrive at node 1 before
113  // the reply packet sent by node 2.
114 
115  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
116  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
117 
118  // Set the random seed and run number for this test
119  RngSeedManager::SetSeed (1);
120  RngSeedManager::SetRun (6);
121 
122  // Create 2 nodes, and a NetDevice for each one
123  Ptr<Node> n0 = CreateObject <Node> ();
124  Ptr<Node> n1 = CreateObject <Node> ();
125 
126  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
127  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
128 
129  // Make random variable stream assignment deterministic
130  dev0->AssignStreams (0);
131  dev1->AssignStreams (10);
132 
133  dev0->SetAddress (Mac16Address ("00:01"));
134  dev1->SetAddress (Mac16Address ("00:02"));
135 
136  // Each device must be attached to the same channel
137  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
138  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
139  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
140  channel->AddPropagationLossModel (propModel);
141  channel->SetPropagationDelayModel (delayModel);
142 
143  dev0->SetChannel (channel);
144  dev1->SetChannel (channel);
145 
146  // To complete configuration, a LrWpanNetDevice must be added to a node
147  n0->AddDevice (dev0);
148  n1->AddDevice (dev1);
149 
150  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
151  sender0Mobility->SetPosition (Vector (0,0,0));
152  dev0->GetPhy ()->SetMobility (sender0Mobility);
153  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
154  // Configure position 10 m distance
155  sender1Mobility->SetPosition (Vector (0,10,0));
156  dev1->GetPhy ()->SetMobility (sender1Mobility);
157 
160  dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
161 
164  dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1);
165 
168  dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2);
169 
172  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3);
173 
174  Ptr<Packet> p0 = Create<Packet> (50); // 50 bytes of dummy data
175  McpsDataRequestParams params;
176  params.m_srcAddrMode = SHORT_ADDR;
177  params.m_dstAddrMode = SHORT_ADDR;
178  params.m_dstPanId = 0;
179  params.m_dstAddr = Mac16Address ("00:02");
180  params.m_msduHandle = 0;
181  params.m_txOptions = TX_OPTION_ACK;
183  Simulator::ScheduleNow (&LrWpanMac::McpsDataRequest, dev0->GetMac (), params, p0);
184 
185 
186  Simulator::Run ();
187 
188  NS_TEST_EXPECT_MSG_LT (m_requestTime, m_replyTime, "Sent the request before the reply (as expected)");
189  NS_TEST_EXPECT_MSG_LT (m_requestAckTime, m_replyArrivalTime, "The request was ACKed before the reply arrived (as expected)");
190  NS_TEST_EXPECT_MSG_LT (m_replyAckTime, m_replyArrivalTime, "The reply was ACKed before the reply arrived (as expected)");
191 
192  Simulator::Destroy ();
193 }
194 
196 {
197 public:
199 };
200 
202  : TestSuite ("lr-wpan-ack", UNIT)
203 {
204  AddTestCase (new LrWpanAckTestCase, TestCase::QUICK);
205 }
206 
static LrWpanAckTestSuite g_lrWpanAckTestSuite
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
A suite of tests to run.
Definition: test.h:1105
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Build bound Callbacks which take varying numbers of arguments, and potentially returning a value...
Definition: callback.h:1467
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
encapsulates test code
Definition: test.h:929
virtual void AddPropagationLossModel(Ptr< PropagationLossModel > loss)
set the single-frequency propagation loss model to be used
a 3d vector
Definition: vector.h:31
static void DataIndication(LrWpanAckTestCase *testCase, Ptr< LrWpanNetDevice > dev, McpsDataIndicationParams params, Ptr< Packet > p)
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-data.cc:42
static void DataConfirm(LrWpanAckTestCase *testCase, Ptr< LrWpanNetDevice > dev, McpsDataConfirmParams params)
LrWpanAddressMode m_srcAddrMode
Definition: lr-wpan-mac.h:119
virtual void DoRun(void)
Implementation to actually run this TestCase.
This class can contain 16 bit addresses.
Definition: mac16-address.h:39
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
void SetPosition(const Vector &position)
virtual Address GetAddress(void) const
This method indirects to LrWpanMac::SetShortAddress ()
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:120
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
virtual void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
set the propagation delay model to be used
#define NS_TEST_EXPECT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report if not.
Definition: test.h:798
static void DataConfirm(McpsDataConfirmParams params)
Definition: lr-wpan-data.cc:47
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.