A Discrete-Event Network Simulator
API
example-ping-lr-wpan.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 
22 #include <fstream>
23 #include "ns3/core-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/internet-apps-module.h"
26 #include "ns3/mobility-module.h"
27 #include "ns3/spectrum-module.h"
28 #include "ns3/propagation-module.h"
29 #include "ns3/sixlowpan-module.h"
30 #include "ns3/lr-wpan-module.h"
31 
32 using namespace ns3;
33 
34 
35 
36 int main (int argc, char** argv)
37 {
38  bool verbose = false;
39  bool disablePcap = false;
40  bool disableAsciiTrace = false;
41  bool enableLSixlowLogLevelInfo = false;
42 
43  CommandLine cmd (__FILE__);
44  cmd.AddValue ("verbose", "turn on log components", verbose);
45  cmd.AddValue ("disable-pcap", "disable PCAP generation", disablePcap);
46  cmd.AddValue ("disable-asciitrace", "disable ascii trace generation", disableAsciiTrace);
47  cmd.AddValue ("enable-sixlowpan-loginfo", "enable sixlowpan LOG_LEVEL_INFO (used for tests)", enableLSixlowLogLevelInfo);
48  cmd.Parse (argc, argv);
49 
50  if (verbose)
51  {
52  LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL);
53  LogComponentEnable ("LrWpanMac", LOG_LEVEL_ALL);
54  LogComponentEnable ("LrWpanPhy", LOG_LEVEL_ALL);
55  LogComponentEnable ("LrWpanNetDevice", LOG_LEVEL_ALL);
56  LogComponentEnable ("SixLowPanNetDevice", LOG_LEVEL_ALL);
57  }
58 
60  nodes.Create(2);
61 
63  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
64  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
65  "MinX", DoubleValue (0.0),
66  "MinY", DoubleValue (0.0),
67  "DeltaX", DoubleValue (20),
68  "DeltaY", DoubleValue (20),
69  "GridWidth", UintegerValue (3),
70  "LayoutType", StringValue ("RowFirst"));
71  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
72  mobility.Install (nodes);
73 
74  LrWpanHelper lrWpanHelper;
75  // Add and install the LrWpanNetDevice for each node
76  NetDeviceContainer lrwpanDevices = lrWpanHelper.Install(nodes);
77 
78  // Fake PAN association and short address assignment.
79  // This is needed because the lr-wpan module does not provide (yet)
80  // a full PAN association procedure.
81  lrWpanHelper.AssociateToPan (lrwpanDevices, 1);
82 
83  InternetStackHelper internetv6;
84  internetv6.Install (nodes);
85 
86  SixLowPanHelper sixlowpan;
87  NetDeviceContainer devices = sixlowpan.Install (lrwpanDevices);
88 
89  Ipv6AddressHelper ipv6;
90  ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
91  Ipv6InterfaceContainer deviceInterfaces;
92  deviceInterfaces = ipv6.Assign (devices);
93 
94  if (enableLSixlowLogLevelInfo)
95  {
96  std::cout << "Device 0: pseudo-Mac-48 " << Mac48Address::ConvertFrom (devices.Get (0)->GetAddress ())
97  << ", IPv6 Address " << deviceInterfaces.GetAddress (0,1) << std::endl;
98  std::cout << "Device 1: pseudo-Mac-48 " << Mac48Address::ConvertFrom (devices.Get (1)->GetAddress ())
99  << ", IPv6 Address " << deviceInterfaces.GetAddress (1,1) << std::endl;
100  }
101 
102  uint32_t packetSize = 10;
103  uint32_t maxPacketCount = 5;
104  Time interPacketInterval = Seconds (1.);
105  Ping6Helper ping6;
106 
107  ping6.SetLocal (deviceInterfaces.GetAddress (0, 1));
108  ping6.SetRemote (deviceInterfaces.GetAddress (1, 1));
109 
110  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
111  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
112  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
113  ApplicationContainer apps = ping6.Install (nodes.Get (0));
114 
115  apps.Start (Seconds (1.0));
116  apps.Stop (Seconds (10.0));
117 
118  if (!disableAsciiTrace)
119  {
120  AsciiTraceHelper ascii;
121  lrWpanHelper.EnableAsciiAll (ascii.CreateFileStream ("Ping-6LoW-lr-wpan.tr"));
122  }
123  if (!disablePcap)
124  {
125  lrWpanHelper.EnablePcapAll (std::string ("Ping-6LoW-lr-wpan"), true);
126  }
127  if (enableLSixlowLogLevelInfo)
128  {
129  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> (&std::cout);
131  }
132 
133  Simulator::Stop (Seconds (10));
134 
135  Simulator::Run ();
137 
138 }
139 
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:50
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
static void PrintNeighborCacheAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the neighbor cache of all nodes at a particular time.
helps to manage and create IEEE 802.15.4 NetDevice objects
void AssociateToPan(NetDeviceContainer c, uint16_t panId)
Associate the nodes to the same PAN.
NetDeviceContainer Install(NodeContainer c)
Install a LrWpanNetDevice and the associated structures (e.g., channel) in the nodes.
static Mac48Address ConvertFrom(const Address &address)
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Ping6 application helper.
Definition: ping6-helper.h:39
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
void SetLocal(Ipv6Address ip)
Set the local IPv6 address.
Definition: ping6-helper.cc:35
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
Hold variables of type string.
Definition: string.h:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
Hold an unsigned integer type.
Definition: uinteger.h:44
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
devices
Definition: first.py:39
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
cmd
Definition: second.py:35
mobility
Definition: third.py:108
bool verbose
static const uint32_t packetSize