A Discrete-Event Network Simulator
API
ocb-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Dalian University of Technology
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: Junling Bu <linlinjavaer@gmail.com>
18  */
19 
20 #include "ns3/config.h"
21 #include "ns3/data-rate.h"
22 #include "ns3/mobility-helper.h"
23 #include "ns3/mobility-model.h"
24 #include "ns3/ocb-wifi-mac.h"
25 #include "ns3/packet-socket-address.h"
26 #include "ns3/packet-socket-client.h"
27 #include "ns3/packet-socket-helper.h"
28 #include "ns3/packet-socket-server.h"
29 #include "ns3/position-allocator.h"
30 #include "ns3/qos-txop.h"
31 #include "ns3/rng-seed-manager.h"
32 #include "ns3/sta-wifi-mac.h"
33 #include "ns3/string.h"
34 #include "ns3/test.h"
35 #include "ns3/vector.h"
36 #include "ns3/wave-mac-helper.h"
37 #include "ns3/wifi-80211p-helper.h"
38 #include "ns3/wifi-net-device.h"
39 #include "ns3/yans-wifi-helper.h"
40 
41 #include <iostream>
42 
43 using namespace ns3;
44 
45 // helper function to assign streams to random variables, to control
46 // randomness in the tests
47 static void
49 {
50  int64_t currentStream = stream;
51  PointerValue ptr;
52  if (!mac->GetQosSupported())
53  {
54  mac->GetAttribute("Txop", ptr);
55  Ptr<Txop> txop = ptr.Get<Txop>();
56  currentStream += txop->AssignStreams(currentStream);
57  }
58  else
59  {
60  mac->GetAttribute("VO_Txop", ptr);
61  Ptr<QosTxop> vo_txop = ptr.Get<QosTxop>();
62  currentStream += vo_txop->AssignStreams(currentStream);
63 
64  mac->GetAttribute("VI_Txop", ptr);
65  Ptr<QosTxop> vi_txop = ptr.Get<QosTxop>();
66  currentStream += vi_txop->AssignStreams(currentStream);
67 
68  mac->GetAttribute("BE_Txop", ptr);
69  Ptr<QosTxop> be_txop = ptr.Get<QosTxop>();
70  currentStream += be_txop->AssignStreams(currentStream);
71 
72  mac->GetAttribute("BK_Txop", ptr);
73  Ptr<QosTxop> bk_txop = ptr.Get<QosTxop>();
74  currentStream += bk_txop->AssignStreams(currentStream);
75  }
76 }
77 
85 {
86  public:
88  ~OcbWifiMacTestCase() override;
89 
90  private:
91  void DoRun() override;
92 
98  void MacAssoc(std::string context, Mac48Address bssid);
107  void PhyRxOkTrace(std::string context,
108  Ptr<const Packet> packet,
109  double snr,
110  WifiMode mode,
111  WifiPreamble preamble);
120  void PhyTxTrace(std::string context,
121  Ptr<const Packet> packet,
122  WifiMode mode,
123  WifiPreamble preamble,
124  uint8_t txPower);
130  Vector GetCurrentPosition(uint32_t i);
135  void AdvancePosition(Ptr<Node> node);
136 
138  void PreRandomConfiguration();
144  void ConfigureApStaMode(Ptr<Node> static_node, Ptr<Node> mobile_node);
150  void ConfigureAdhocMode(Ptr<Node> static_node, Ptr<Node> mobile_node);
156  void ConfigureOcbMode(Ptr<Node> static_node, Ptr<Node> mobile_node);
162  void PostDeviceConfiguration(Ptr<Node> static_node, Ptr<Node> mobile_node);
163 
165  Vector phytx_pos;
166 
168  Vector macassoc_pos;
169 
171  Vector phyrx_pos;
172 
173  // nodes.Get (0) is static node
174  // nodes.Get (1) is mobile node
176 };
177 
179  : TestCase("Association time: Ap+Sta mode vs Adhoc mode vs Ocb mode")
180 {
181 }
182 
184 {
185 }
186 
187 // mobility is like walk on line with velocity 5 m/s
188 // We prefer to update 0.5m every 0.1s rather than 5m every 1s
189 void
191 {
193  Vector pos = mobility->GetPosition();
194  pos.x -= 0.5;
195  if (pos.x < 1.0)
196  {
197  pos.x = 1.0;
198  return;
199  }
200  mobility->SetPosition(pos);
201 
202  Simulator::Schedule(Seconds(0.1), &OcbWifiMacTestCase::AdvancePosition, this, node);
203 }
204 
205 // here are only two nodes, a stationary and a mobile one
206 // the i value of the first = 0; the i value of second = 1.
207 Vector
209 {
210  NS_ASSERT(i < 2);
211  Ptr<Node> node = nodes.Get(i);
213  Vector pos = mobility->GetPosition();
214  return pos;
215 }
216 
217 void
218 OcbWifiMacTestCase::MacAssoc(std::string context, Mac48Address bssid)
219 {
220  if (macassoc_time == Time(0))
221  {
222  macassoc_time = Now();
224  std::cout << "MacAssoc time = " << macassoc_time.As(Time::NS)
225  << " position = " << macassoc_pos << std::endl;
226  }
227 }
228 
229 // We want to get the time that sta receives the first beacon frame from AP
230 // it means that in this time this sta has ability to receive frame
231 void
233  Ptr<const Packet> packet,
234  double snr,
235  WifiMode mode,
236  WifiPreamble preamble)
237 {
238  if (phyrx_time == Time(0))
239  {
240  phyrx_time = Now();
242  std::cout << "PhyRxOk time = " << phyrx_time.As(Time::NS) << " position = " << phyrx_pos
243  << std::endl;
244  }
245 }
246 
247 // We want to get the time that STA sends the first data packet successfully
248 void
249 OcbWifiMacTestCase::PhyTxTrace(std::string context,
250  Ptr<const Packet> packet,
251  WifiMode mode,
252  WifiPreamble preamble,
253  uint8_t txPower)
254 {
255  WifiMacHeader h;
256  packet->PeekHeader(h);
257  if ((phytx_time == Time(0)) && h.IsData())
258  {
259  phytx_time = Now();
261  std::cout << "PhyTx data time = " << phytx_time.As(Time::NS) << " position = " << phytx_pos
262  << std::endl;
263  }
264 }
265 
266 void
268 {
269  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
270  YansWifiPhyHelper wifiPhy;
271  wifiPhy.SetChannel(wifiChannel.Create());
272 
273  Ssid ssid = Ssid("wifi-default");
274  WifiMacHelper wifiStaMac;
275  wifiStaMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
276  WifiMacHelper wifiApMac;
277  wifiApMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
278 
280  wifi.SetStandard(WIFI_STANDARD_80211p);
281  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
282  "DataMode",
283  StringValue("OfdmRate6MbpsBW10MHz"),
284  "ControlMode",
285  StringValue("OfdmRate6MbpsBW10MHz"));
286  wifi.Install(wifiPhy, wifiStaMac, mobile_node);
287  wifi.Install(wifiPhy, wifiApMac, static_node);
288 }
289 
290 void
292 {
293  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
294  YansWifiPhyHelper wifiPhy;
295  wifiPhy.SetChannel(wifiChannel.Create());
296 
297  WifiMacHelper wifiMac;
298  wifiMac.SetType("ns3::AdhocWifiMac");
299 
301  wifi.SetStandard(WIFI_STANDARD_80211p);
302  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
303  "DataMode",
304  StringValue("OfdmRate6MbpsBW10MHz"),
305  "ControlMode",
306  StringValue("OfdmRate6MbpsBW10MHz"));
307  wifi.Install(wifiPhy, wifiMac, mobile_node);
308  wifi.Install(wifiPhy, wifiMac, static_node);
309 }
310 
311 void
313 {
314  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
315  YansWifiPhyHelper wifiPhy;
316  wifiPhy.SetChannel(wifiChannel.Create());
317 
318  NqosWaveMacHelper wifi80211pMac = NqosWaveMacHelper::Default();
319 
320  Wifi80211pHelper wifi80211p = Wifi80211pHelper::Default();
321  wifi80211p.SetRemoteStationManager("ns3::ConstantRateWifiManager",
322  "DataMode",
323  StringValue("OfdmRate6MbpsBW10MHz"),
324  "ControlMode",
325  StringValue("OfdmRate6MbpsBW10MHz"));
326  wifi80211p.Install(wifiPhy, wifi80211pMac, mobile_node);
327  wifi80211p.Install(wifiPhy, wifi80211pMac, static_node);
328 }
329 
330 void
332 {
333  Ptr<WifiNetDevice> static_device = DynamicCast<WifiNetDevice>(static_node->GetDevice(0));
334  Ptr<WifiNetDevice> mobile_device = DynamicCast<WifiNetDevice>(mobile_node->GetDevice(0));
335 
336  // Fix the stream assignment to the Dcf Txop objects (backoffs)
337  // The below stream assignment will result in the Txop object
338  // using a backoff value of zero for this test when the
339  // Txop::EndTxNoAck() calls to StartBackoffNow()
340  AssignWifiRandomStreams(static_device->GetMac(), 21);
341  AssignWifiRandomStreams(mobile_device->GetMac(), 22);
342 
343  // setup mobility
344  // the initial position of static node is at 0,
345  // and the initial position of mobile node is 350.
347  mobility.Install(mobile_node);
348  mobility.Install(static_node);
349  Ptr<MobilityModel> mm = mobile_node->GetObject<MobilityModel>();
350  Vector possta = mm->GetPosition();
351  possta.x = 350;
352  mm->SetPosition(possta);
353  Simulator::Schedule(Seconds(1.0), &OcbWifiMacTestCase::AdvancePosition, this, mobile_node);
354 
355  PacketSocketAddress socket;
356  socket.SetSingleDevice(mobile_device->GetIfIndex());
357  socket.SetPhysicalAddress(static_device->GetAddress());
358  socket.SetProtocol(1);
359 
360  // give packet socket powers to nodes.
361  PacketSocketHelper packetSocket;
362  packetSocket.Install(static_node);
363  packetSocket.Install(mobile_node);
364 
365  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient>();
366  client->SetRemote(socket);
367  mobile_node->AddApplication(client);
368  client->SetStartTime(Seconds(0.5));
369  client->SetStopTime(Seconds(70.0));
370 
371  Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer>();
372  server->SetLocal(socket);
373  static_node->AddApplication(server);
374  server->SetStartTime(Seconds(0.0));
375  server->SetStopTime(Seconds(70.5));
376 
378  phytx_pos = macassoc_pos = phyrx_pos = Vector();
379 
380  if (DynamicCast<StaWifiMac>(mobile_device->GetMac()))
381  {
382  // This trace is available only in a StaWifiMac
383  Config::Connect("/NodeList/1/DeviceList/*/Mac/Assoc",
385  }
386  Config::Connect("/NodeList/1/DeviceList/*/Phy/State/RxOk",
388  Config::Connect("/NodeList/1/DeviceList/*/Phy/State/Tx",
390 }
391 
405 void
407 {
408  std::cout << "test time point for Ap-Sta mode" << std::endl;
410  nodes = NodeContainer();
411  nodes.Create(2);
412  Ptr<Node> static_node = nodes.Get(0);
413  Ptr<Node> mobile_node = nodes.Get(1);
414  ConfigureApStaMode(static_node, mobile_node);
415  PostDeviceConfiguration(static_node, mobile_node);
416  Simulator::Stop(Seconds(71.0));
417  Simulator::Run();
418  Simulator::Destroy();
420  phyrx_time,
422  "In Sta mode with AP, you cannot associate until receive beacon or AssocResponse frame");
424  phytx_time,
425  "In Sta mode with AP, you cannot send data packet until associate");
426  // Are these position tests redundant with time check tests?
427  // NS_TEST_ASSERT_MSG_GT ((phyrx_pos.x - macassoc_pos.x), 0.0, "");
428  // actually macassoc_pos.x - phytx_pos.x is greater than 0
429  // however associate switch to send is so fast with less than 100ms
430  // and in our mobility model that every 0.1s update position,
431  // so turn out to be that macassoc_pos.x - phytx_pos.x is equal to 0
432  // NS_TEST_ASSERT_MSG_GT ((macassoc_pos.x - phytx_pos.x), 0.0, "");
433 
434  std::cout << "test time point for Adhoc mode" << std::endl;
436  nodes = NodeContainer();
437  nodes.Create(2);
438  static_node = nodes.Get(0);
439  mobile_node = nodes.Get(1);
440  ConfigureAdhocMode(static_node, mobile_node);
441  PostDeviceConfiguration(static_node, mobile_node);
442  Simulator::Stop(Seconds(71.0));
443  Simulator::Run();
444  Simulator::Destroy();
445  // below test assert will fail, because AdhocWifiMac has not implement state machine.
446  // if someone takes a look at the output in adhoc mode and in Ocb mode
447  // he will find these two outputs are almost same.
448  // NS_TEST_ASSERT_MSG_LT (phyrx_time, macassoc_time, "In Adhoc mode, you cannot associate until
449  // receive beacon or AssocResponse frame" ); NS_TEST_ASSERT_MSG_LT (macassoc_time, phytx_time,
450  // "In Adhoc mode, you cannot send data packet until associate" ); NS_TEST_ASSERT_MSG_GT
451  // ((phyrx_pos.x - macassoc_pos.x), 0.0, "");
452  // below test assert result refer to Ap-Sta mode
453  // NS_TEST_ASSERT_MSG_GT ((macassoc_pos.x - phytx_pos.x), 0.0, "");
454 
455  std::cout << "test time point for Ocb mode" << std::endl;
457  nodes = NodeContainer();
458  nodes.Create(2);
459  static_node = nodes.Get(0);
460  mobile_node = nodes.Get(1);
461  ConfigureOcbMode(static_node, mobile_node);
462  PostDeviceConfiguration(static_node, mobile_node);
463  Simulator::Stop(Seconds(71.0));
464  Simulator::Run();
465  Simulator::Destroy();
467  0,
468  "In Ocb mode, there is no associate state machine");
470  phyrx_time,
471  "before mobile node receives frames from far static node, it can send "
472  "data packet directly");
474  NS_TEST_ASSERT_MSG_GT((phytx_pos.x - phyrx_pos.x), 0.0, "");
475 }
476 
477 void
479 {
480  // Assign a seed and run number, and later fix the assignment of streams to
481  // WiFi random variables, so that the first backoff used is zero slots
482  RngSeedManager::SetSeed(1);
483  RngSeedManager::SetRun(17);
484  // the WiFi random variables is set in PostDeviceConfiguration method.
485 }
486 
493 class OcbTestSuite : public TestSuite
494 {
495  public:
496  OcbTestSuite();
497 };
498 
500  : TestSuite("wave-80211p-ocb", UNIT)
501 {
502  // TestDuration for TestCase can be QUICK, EXTENSIVE or TAKES_FOREVER
503  AddTestCase(new OcbWifiMacTestCase, TestCase::QUICK);
504 }
505 
506 // Do not forget to allocate an instance of this TestSuite
Ocb Test Suite.
Ocb Wifi Mac Test Case.
Time phytx_time
Phy transmit time.
void PhyTxTrace(std::string context, Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
Phy transmit trace function.
Vector macassoc_pos
MAC associate position.
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
Phy receive ok trace function.
void ConfigureApStaMode(Ptr< Node > static_node, Ptr< Node > mobile_node)
Configure AP STA mode function.
void PostDeviceConfiguration(Ptr< Node > static_node, Ptr< Node > mobile_node)
Post device configuration function.
~OcbWifiMacTestCase() override
Vector GetCurrentPosition(uint32_t i)
Get current position function.
void DoRun() override
static-node:0 <-— mobile-node:1
Time macassoc_time
MAC associate time.
void ConfigureAdhocMode(Ptr< Node > static_node, Ptr< Node > mobile_node)
Configure adhoc mode function.
void MacAssoc(std::string context, Mac48Address bssid)
MAC associate function.
void AdvancePosition(Ptr< Node > node)
Advance position function.
void ConfigureOcbMode(Ptr< Node > static_node, Ptr< Node > mobile_node)
Configure OCB mode function.
Vector phytx_pos
Phy transmit position.
NodeContainer nodes
the nodes
Time phyrx_time
Phy receive time.
Vector phyrx_pos
Phy receive position.
void PreRandomConfiguration()
Pre random configuration function.
an EUI-48 address
Definition: mac48-address.h:46
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
Vector GetPosition() const
void SetPosition(const Vector &position)
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:152
Nqos Wave Mac Helper class.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:305
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Ptr< T > Get() const
Definition: pointer.h:206
Handle packet fragmentation and retransmissions for QoS data frames as well as MSDU aggregation (A-MS...
Definition: qos-txop.h:73
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Hold variables of type string.
Definition: string.h:56
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:305
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:417
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
Handle packet fragmentation and retransmissions for data and management frames.
Definition: txop.h:71
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Definition: txop.cc:536
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create wifi 802.11p objects of WifiNetDevice class
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &macHelper, NodeContainer c) const override
helps to create WifiNetDevice objects
Definition: wifi-helper.h:325
void SetRemoteStationManager(std::string type, Args &&... args)
Helper function used to set the station manager.
Definition: wifi-helper.h:605
Implements the IEEE 802.11 MAC header.
bool IsData() const
Return true if the Type is DATA.
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
represent a single transmission mode
Definition: wifi-mode.h:50
Ptr< WifiMac > GetMac() const
uint32_t GetIfIndex() const override
Address GetAddress() const override
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:975
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report and abort if not.
Definition: test.h:709
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:144
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
Definition: test.h:874
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
WifiPreamble
The type of preamble to be used by an IEEE 802.11 transmission.
@ WIFI_STANDARD_80211p
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:848
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:707
ssid
Definition: third.py:86
mac
Definition: third.py:85
wifi
Definition: third.py:88
mobility
Definition: third.py:96
static void AssignWifiRandomStreams(Ptr< WifiMac > mac, int64_t stream)
static OcbTestSuite ocbTestSuite
the test suite
void PhyRxOkTrace(std::string context, Ptr< const Packet > packet, double snr, WifiMode mode, WifiPreamble preamble)
PHY-level RX OK trace.
Definition: wifi-ap.cc:79
void PhyTxTrace(std::string context, Ptr< const Packet > packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
PHY-level TX trace.
Definition: wifi-ap.cc:117
static void AdvancePosition(Ptr< Node > node)
Move a node position by 5m on the x axis every second, up to 210m.
Definition: wifi-ap.cc:153