1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/*
* Copyright (c) 2014 Universita' di Firenze
*
* SPDX-License-Identifier: GPL-2.0-only
*
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
*/
// Network topology
//
// n0 n1
// | |
// =================
// WSN (802.15.4)
//
// - ICMPv6 echo request flows from n0 to n1 and back with ICMPv6 echo reply
// - DropTail queues
// - Tracing of queues and packet receptions to file "wsn-ping6.tr"
//
// This example is based on the "ping6.cc" example.
#include "ns3/core-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/internet-module.h"
#include "ns3/lr-wpan-module.h"
#include "ns3/mobility-module.h"
#include "ns3/sixlowpan-module.h"
#include <fstream>
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Ping6WsnExample");
int
main(int argc, char** argv)
{
bool verbose = false;
CommandLine cmd(__FILE__);
cmd.AddValue("verbose", "turn on log components", verbose);
cmd.Parse(argc, argv);
if (verbose)
{
LogComponentEnable("Ping6WsnExample", LOG_LEVEL_INFO);
LogComponentEnable("Ipv6EndPointDemux", LOG_LEVEL_ALL);
LogComponentEnable("Ipv6L3Protocol", LOG_LEVEL_ALL);
LogComponentEnable("Ipv6StaticRouting", LOG_LEVEL_ALL);
LogComponentEnable("Ipv6ListRouting", LOG_LEVEL_ALL);
LogComponentEnable("Ipv6Interface", LOG_LEVEL_ALL);
LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_ALL);
LogComponentEnable("Ping", LOG_LEVEL_ALL);
LogComponentEnable("NdiscCache", LOG_LEVEL_ALL);
LogComponentEnable("SixLowPanNetDevice", LOG_LEVEL_ALL);
}
NS_LOG_INFO("Create nodes.");
NodeContainer nodes;
nodes.Create(2);
// Set seed for random numbers
SeedManager::SetSeed(167);
// Install mobility
MobilityHelper mobility;
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
Ptr<ListPositionAllocator> nodesPositionAlloc = CreateObject<ListPositionAllocator>();
nodesPositionAlloc->Add(Vector(0.0, 0.0, 0.0));
nodesPositionAlloc->Add(Vector(50.0, 0.0, 0.0));
mobility.SetPositionAllocator(nodesPositionAlloc);
mobility.Install(nodes);
NS_LOG_INFO("Create channels.");
LrWpanHelper lrWpanHelper;
lrWpanHelper.SetPropagationDelayModel("ns3::ConstantSpeedPropagationDelayModel");
lrWpanHelper.AddPropagationLossModel("ns3::LogDistancePropagationLossModel");
// Add and install the LrWpanNetDevice for each node
// lrWpanHelper.EnableLogComponents();
NetDeviceContainer devContainer = lrWpanHelper.Install(nodes);
lrWpanHelper.CreateAssociatedPan(devContainer, 10);
std::cout << "Created " << devContainer.GetN() << " devices" << std::endl;
std::cout << "There are " << nodes.GetN() << " nodes" << std::endl;
/* Install IPv4/IPv6 stack */
NS_LOG_INFO("Install Internet stack.");
InternetStackHelper internetv6;
internetv6.SetIpv4StackInstall(false);
internetv6.Install(nodes);
// Install 6LowPan layer
NS_LOG_INFO("Install 6LoWPAN.");
SixLowPanHelper sixlowpan;
NetDeviceContainer six1 = sixlowpan.Install(devContainer);
NS_LOG_INFO("Assign addresses.");
Ipv6AddressHelper ipv6;
ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
Ipv6InterfaceContainer i = ipv6.Assign(six1);
NS_LOG_INFO("Create Applications.");
/* Create a Ping application to send ICMPv6 echo request from node zero to
* all-nodes (ff02::1).
*/
uint32_t packetSize = 16;
uint32_t maxPacketCount = 5;
Time interPacketInterval = Seconds(1.);
PingHelper ping(i.GetAddress(1, 1));
ping.SetAttribute("Count", UintegerValue(maxPacketCount));
ping.SetAttribute("Interval", TimeValue(interPacketInterval));
ping.SetAttribute("Size", UintegerValue(packetSize));
ApplicationContainer apps = ping.Install(nodes.Get(0));
apps.Start(Seconds(2));
apps.Stop(Seconds(10));
AsciiTraceHelper ascii;
lrWpanHelper.EnableAsciiAll(ascii.CreateFileStream("ping6wsn.tr"));
lrWpanHelper.EnablePcapAll(std::string("ping6wsn"), true);
NS_LOG_INFO("Run Simulation.");
Simulator::Run();
Simulator::Destroy();
NS_LOG_INFO("Done.");
return 0;
}
|