A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-6lowpan-example.cc
Go to the documentation of this file.
1/*
2 *
3 * SPDX-License-Identifier: GPL-2.0-only
4 *
5 * Author: Hossam Khader <hossamkhader@gmail.com>
6 */
7
8#include "ns3/acoustic-modem-energy-model-helper.h"
9#include "ns3/basic-energy-source-helper.h"
10#include "ns3/core-module.h"
11#include "ns3/energy-source-container.h"
12#include "ns3/internet-module.h"
13#include "ns3/mobility-helper.h"
14#include "ns3/mobility-model.h"
15#include "ns3/node-container.h"
16#include "ns3/sixlowpan-helper.h"
17#include "ns3/sixlowpan-net-device.h"
18#include "ns3/uan-channel.h"
19#include "ns3/uan-helper.h"
20
21using namespace ns3;
22using namespace ns3::energy;
23
24/**
25 * \ingroup uan
26 *
27 * This example shows the usage of UDP over 6LoWPAN to transfer data.
28 * Two nodes are sending their remaining energy percentage (1 byte)
29 * to a gateway node, that prints the received data.
30 * The transmissions are scheduled at random times to avoid collisions
31 *
32 */
33
34NS_LOG_COMPONENT_DEFINE("Uan6lowpanExample");
35
37{
38 public:
40
41 /**
42 * Set the UAN nodes position
43 */
44 void SetupPositions();
45
46 /**
47 * Set the UAN nodes energy
48 */
49 void SetupEnergy();
50
51 /**
52 * Set the UAN nodes communication channels
53 */
55
56 /**
57 * Set the UAN nodes communication channels
58 */
59 void SetupApplications();
60
61 /**
62 * Send a packet from all the nodes
63 */
64 void SendPackets();
65
66 /**
67 * Send a packet from one of the nodes
68 * \param node The sending node
69 * \param pkt The packet
70 * \param dst the destination
71 */
73
74 /**
75 * Print the received packet
76 * \param socket The receiving socket
77 */
79
80 /**
81 * Prepare the experiment
82 */
83 void Prepare();
84
85 /**
86 * Teardown the experiment
87 */
88 void Teardown();
89
90 private:
91 NodeContainer m_nodes; //!< UAN nodes
92 std::map<Ptr<Node>, Ptr<Socket>> m_sockets; //!< send and receive sockets
93};
94
98
99void
101{
102 MobilityHelper mobilityHelper;
103 mobilityHelper.SetMobilityModel("ns3::ConstantPositionMobilityModel");
104 mobilityHelper.Install(m_nodes);
105 m_nodes.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(0, 0, 0));
106 m_nodes.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(100, 0, 0));
107 m_nodes.Get(2)->GetObject<MobilityModel>()->SetPosition(Vector(-100, 0, 0));
108}
109
110void
112{
113 BasicEnergySourceHelper energySourceHelper;
114 energySourceHelper.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(900000));
115 energySourceHelper.Install(m_nodes);
116}
117
118void
120{
122 UanHelper uanHelper;
123 NetDeviceContainer netDeviceContainer = uanHelper.Install(m_nodes, channel);
124 EnergySourceContainer energySourceContainer;
125 auto node = m_nodes.Begin();
126 while (node != m_nodes.End())
127 {
128 energySourceContainer.Add((*node)->GetObject<EnergySourceContainer>()->Get(0));
129 node++;
130 }
131 AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
132 acousticModemEnergyModelHelper.Install(netDeviceContainer, energySourceContainer);
133
134 SixLowPanHelper sixLowPanHelper;
135 NetDeviceContainer sixlowpanNetDevices = sixLowPanHelper.Install(netDeviceContainer);
136
137 InternetStackHelper internetStackHelper;
138 internetStackHelper.Install(m_nodes);
139
140 Ipv6AddressHelper ipv6AddressHelper;
141 ipv6AddressHelper.SetBase(Ipv6Address("2002::"), Ipv6Prefix(64));
142 ipv6AddressHelper.Assign(sixlowpanNetDevices);
143
144 node = m_nodes.Begin();
145 while (node != m_nodes.End())
146 {
147 (*node)->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
148 (*node)->GetObject<Icmpv6L4Protocol>()->SetAttribute("ReachableTime",
149 TimeValue(Seconds(3600)));
150 (*node)->GetObject<Icmpv6L4Protocol>()->SetAttribute("RetransmissionTime",
151 TimeValue(Seconds(1000)));
152 node++;
153 }
154}
155
156void
158{
159 Address srcAddress;
160 while (socket->GetRxAvailable() > 0)
161 {
162 Ptr<Packet> packet = socket->RecvFrom(srcAddress);
163 uint8_t energyReading;
164 packet->CopyData(&energyReading, 1);
165
167 {
168 NS_LOG_UNCOND("Time: " << Simulator::Now().GetHours() << "h"
169 << " | Node: "
170 << Inet6SocketAddress::ConvertFrom(srcAddress).GetIpv6()
171 << " | Energy: " << +energyReading << "%");
172 }
173 }
174}
175
176void
178{
179 auto node = m_nodes.Begin();
180 while (node != m_nodes.End())
181 {
182 m_sockets[*node] =
183 Socket::CreateSocket(*node, TypeId::LookupByName("ns3::UdpSocketFactory"));
184 if ((*node)->GetObject<Ipv6>())
185 {
187 m_sockets[*node]->Bind(ipv6_local);
188 }
189
190 m_sockets[*node]->SetRecvCallback(MakeCallback(&UanExperiment::PrintReceivedPacket, this));
191 node++;
192 }
193}
194
195void
197{
199
200 auto node = m_nodes.Begin();
201 Ipv6Address dst =
202 (*node)->GetObject<Ipv6L3Protocol>()->GetInterface(1)->GetAddress(1).GetAddress();
203 node++;
204 while (node != m_nodes.End())
205 {
206 uint8_t energy =
207 ((*node)->GetObject<EnergySourceContainer>()->Get(0)->GetEnergyFraction()) * 100;
208
209 Ptr<Packet> pkt = Create<Packet>(&energy, 1);
210
211 double time = uniformRandomVariable->GetValue(0, 60);
212 Simulator::Schedule(Seconds(time), &UanExperiment::SendSinglePacket, this, *node, pkt, dst);
213 node++;
214 }
216}
217
218void
220{
221 NS_LOG_UNCOND(Simulator::Now().GetHours() << "h"
222 << " packet sent to " << dst);
224 m_sockets[node]->SendTo(pkt, 0, ipv6_destination);
225}
226
227void
237
238void
240{
241 for (auto socket = m_sockets.begin(); socket != m_sockets.end(); socket++)
242 {
243 socket->second->Close();
244 }
245}
246
247int
248main(int argc, char* argv[])
249{
250 CommandLine cmd(__FILE__);
251 cmd.Parse(argc, argv);
252
255
259
260 experiment.Teardown();
261
262 return 0;
263}
This example shows the usage of UDP over 6LoWPAN to transfer data.
void Teardown()
Teardown the experiment.
void PrintReceivedPacket(Ptr< Socket > socket)
Print the received packet.
NodeContainer m_nodes
UAN nodes.
void Prepare()
Prepare the experiment.
void SendSinglePacket(Ptr< Node > node, Ptr< Packet > pkt, Ipv6Address dst)
Send a packet from one of the nodes.
void SendPackets()
Send a packet from all the nodes.
void SetupCommunications()
Set the UAN nodes communication channels.
void SetupPositions()
Set the UAN nodes position.
std::map< Ptr< Node >, Ptr< Socket > > m_sockets
send and receive sockets
void SetupApplications()
Set the UAN nodes communication channels.
void SetupEnergy()
Set the UAN nodes energy.
Assign AcousticModemEnergyModel to uan devices.
a polymophic address class
Definition address.h:90
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v) override
AttributeValue implementation for Boolean.
Definition boolean.h:26
Parse command-line arguments.
energy::DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< energy::EnergySource > source) const
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
energy::EnergySourceContainer Install(Ptr< Node > node) const
An implementation of the ICMPv6 protocol.
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
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.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
Ipv6Address GetAddress() const
Get the IPv6 address.
IPv6 layer implementation.
Ipv6InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const override
Get an address.
Describes an IPv6 prefix.
Helper class used to assign positions and mobility models to nodes.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
void SetMobilityModel(std::string type, Ts &&... args)
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
AttributeValue implementation for Time.
Definition nstime.h:1395
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
UAN configuration helper.
Definition uan-helper.h:31
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Holds a vector of ns3::EnergySource pointers.
void Add(EnergySourceContainer container)
Ptr< EnergySource > Get(uint32_t i) const
Get the i-th Ptr<EnergySource> stored in this container.
void experiment(std::string queue_disc_type)
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time Days(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1272
Time Hours(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1284
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:684