A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
example-sixlowpan-nd-basic.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Boh Jie Qi <jieqiboh5836@gmail.com>
7 */
8
9// Network Topology:
10//
11// +---------+
12// | 6LBR | (Node 0)
13// +----+----+
14// |
15// +--------+-------- ... --------+
16// | | |
17// +--+--+ +--+--+ +--+--+
18// |6LN1 | |6LN2 | |6LNn |
19// +-----+ +-----+ +-----+
20// (Node 1) (Node 2) (Node n)
21
22#include "ns3/core-module.h"
23#include "ns3/internet-apps-module.h"
24#include "ns3/internet-module.h"
25#include "ns3/lr-wpan-module.h"
26#include "ns3/mobility-module.h"
27#include "ns3/sixlowpan-module.h"
28#include "ns3/spectrum-module.h"
29
30#include <fstream>
31
32using namespace ns3;
33
34int
35main(int argc, char* argv[])
36{
37 // Command‑line flags:
38 bool disablePcap = false;
39 uint32_t numLn = 4; // default number of 6LNs
40
41 CommandLine cmd(__FILE__);
42 cmd.AddValue("disable-pcap", "Disable PCAP generation on all LR‑WPAN devices", disablePcap);
43 cmd.AddValue("num-ln", "Number of 6LNs (in addition to the single 6LBR)", numLn);
44 cmd.Parse(argc, argv);
45
46 // Create 1 6LBR and numLn 6LNs
48 nodes.Create(1 + numLn); // node 0 = 6LBR, node 1-4 = 6LNs
49
50 // Set constant positions
52 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
53 mobility.Install(nodes);
54
55 // Install LrWpanNetDevices
56 LrWpanHelper lrWpanHelper;
57 NetDeviceContainer lrwpanDevices = lrWpanHelper.Install(nodes);
58 lrWpanHelper.CreateAssociatedPan(lrwpanDevices, 0);
59
60 // Install Internet stack
61 InternetStackHelper internetv6;
62 internetv6.Install(nodes);
63
64 // Install 6LoWPAN on top of LrWpan
65 SixLowPanHelper sixlowpan;
66 NetDeviceContainer devices = sixlowpan.Install(lrwpanDevices);
67
68 // Configure 6LoWPAN ND
69 // Node 0 = 6LBR
70 sixlowpan.InstallSixLowPanNdBorderRouter(devices.Get(0), "2001::");
71 sixlowpan.SetAdvertisedPrefix(devices.Get(0), Ipv6Prefix("2001::", 64));
72 sixlowpan.AddAdvertisedContext(devices.Get(0), Ipv6Prefix("2001::", 64));
73
74 // Nodes 1-n = 6LNs
75 for (uint32_t i = 1; i <= numLn; ++i)
76 {
77 sixlowpan.InstallSixLowPanNdNode(devices.Get(i));
78 }
79
80 // Set up ping from each 6LN to 6LBR (link-local address of 6LBR is fe80::ff:fe00:1)
81 for (uint32_t i = 1; i <= numLn; ++i)
82 {
83 std::ostringstream oss;
84 oss << "fe80::ff:fe00:" << std::hex << (i + 1); // 6LN link-local address
85
86 PingHelper ping6;
87 ping6.SetAttribute("Count", UintegerValue(1));
88 ping6.SetAttribute("Interval", TimeValue(Seconds(1.0)));
89 ping6.SetAttribute("Size", UintegerValue(16));
90 ping6.SetAttribute("Destination", AddressValue(Ipv6Address("fe80::ff:fe00:1"))); // 6LBR
91 ping6.SetAttribute("InterfaceAddress", AddressValue(Ipv6Address(oss.str().c_str()))); // 6LN
92
93 ApplicationContainer apps = ping6.Install(nodes.Get(i));
94 Time startTime = Seconds(15.0); // wait for all nodes to finish registration
95 Time stopTime = Seconds(20.0);
96
97 apps.Start(startTime);
98 apps.Stop(stopTime);
99 }
100
101 AsciiTraceHelper ascii;
102 if (!disablePcap)
103 {
104 lrWpanHelper.EnablePcapAll(std::string("example-sixlowpan-nd-basic"), true);
105 }
106
110 return 0;
111}
AttributeValue implementation for Address.
Definition address.h:329
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void SetAttribute(const std::string &name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
Manage ASCII trace files for device models.
Parse command-line arguments.
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...
Describes an IPv6 address.
Describes an IPv6 prefix.
helps to manage and create IEEE 802.15.4 NetDevice objects
void CreateAssociatedPan(NetDeviceContainer c, uint16_t panId)
Creates an PAN with associated nodes and assigned addresses(16 and 64) from the nodes in the node con...
NetDeviceContainer Install(NodeContainer c)
Install a LrWpanNetDevice and the associated structures (e.g., channel) in the nodes.
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 ...
Create a ping application and associate it to a node.
Definition ping-helper.h:31
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
Ipv6InterfaceContainer InstallSixLowPanNdNode(NetDeviceContainer c)
Install the SixLoWPAN-ND stack, associate it with a NetDevice, and set it as a 6LN.
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
void SetAdvertisedPrefix(const Ptr< NetDevice > nd, Ipv6Prefix prefix)
Add a new prefix to be advertised by 6LoWPAN-ND.
Ipv6InterfaceContainer InstallSixLowPanNdBorderRouter(NetDeviceContainer c, Ipv6Address baseAddr)
Install the SixLoWPAN-ND stack, associate it with a NetDevice, and set it as a 6LBR.
void AddAdvertisedContext(const Ptr< NetDevice > nd, Ipv6Prefix context)
Add a new context to be advertised by 6LoWPAN-ND.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
AttributeValue implementation for Time.
Definition nstime.h:1375
Hold an unsigned integer type.
Definition uinteger.h:34
Time stopTime
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
NodeContainer nodes
devices
Definition first.py:31
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition third.py:92