A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-network-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
9/*
10 * This script simulates a simple network in which one end device sends one
11 * packet to the gateway.
12 */
13
14#include "ns3/core-module.h"
15#include "ns3/lorawan-module.h"
16#include "ns3/mobility-helper.h"
17
18using namespace ns3;
19using namespace lorawan;
20
21NS_LOG_COMPONENT_DEFINE("SimpleLorawanNetworkExample");
22
23int
24main(int argc, char* argv[])
25{
26 // Set up logging
27 LogComponentEnable("SimpleLorawanNetworkExample", LOG_LEVEL_ALL);
28 LogComponentEnable("LoraChannel", LOG_LEVEL_INFO);
30 LogComponentEnable("EndDeviceLoraPhy", LOG_LEVEL_ALL);
31 LogComponentEnable("GatewayLoraPhy", LOG_LEVEL_ALL);
32 LogComponentEnable("LoraInterferenceHelper", LOG_LEVEL_ALL);
33 LogComponentEnable("LorawanMac", LOG_LEVEL_ALL);
34 LogComponentEnable("EndDeviceLorawanMac", LOG_LEVEL_ALL);
35 LogComponentEnable("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
36 LogComponentEnable("GatewayLorawanMac", LOG_LEVEL_ALL);
37 LogComponentEnable("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
38 LogComponentEnable("LogicalLoraChannel", LOG_LEVEL_ALL);
39 LogComponentEnable("LoraHelper", LOG_LEVEL_ALL);
40 LogComponentEnable("LoraPhyHelper", LOG_LEVEL_ALL);
41 LogComponentEnable("LorawanMacHelper", LOG_LEVEL_ALL);
42 LogComponentEnable("OneShotSenderHelper", LOG_LEVEL_ALL);
43 LogComponentEnable("OneShotSender", LOG_LEVEL_ALL);
44 LogComponentEnable("LorawanMacHeader", LOG_LEVEL_ALL);
45 LogComponentEnable("LoraFrameHeader", LOG_LEVEL_ALL);
49
50 /************************
51 * Create the channel *
52 ************************/
53
54 NS_LOG_INFO("Creating the channel...");
55
56 // Create the lora channel object
58 loss->SetPathLossExponent(3.76);
59 loss->SetReference(1, 7.7);
60
62
64
65 /************************
66 * Create the helpers *
67 ************************/
68
69 NS_LOG_INFO("Setting up helpers...");
70
73 allocator->Add(Vector(1000, 0, 0));
74 allocator->Add(Vector(0, 0, 0));
75 mobility.SetPositionAllocator(allocator);
76 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
77
78 // Create the LoraPhyHelper
79 LoraPhyHelper phyHelper = LoraPhyHelper();
80 phyHelper.SetChannel(channel);
81
82 // Create the LorawanMacHelper
84
85 // Create the LoraHelper
86 LoraHelper helper = LoraHelper();
87
88 /************************
89 * Create End Devices *
90 ************************/
91
92 NS_LOG_INFO("Creating the end device...");
93
94 // Create a set of nodes
95 NodeContainer endDevices;
96 endDevices.Create(1);
97
98 // Assign a mobility model to the node
99 mobility.Install(endDevices);
100
101 // Create the LoraNetDevices of the end devices
104 helper.Install(phyHelper, macHelper, endDevices);
105
106 /*********************
107 * Create Gateways *
108 *********************/
109
110 NS_LOG_INFO("Creating the gateway...");
111 NodeContainer gateways;
112 gateways.Create(1);
113
114 mobility.Install(gateways);
115
116 // Create a netdevice for each gateway
119 helper.Install(phyHelper, macHelper, gateways);
120
121 /*********************************************
122 * Install applications on the end devices *
123 *********************************************/
124
125 OneShotSenderHelper oneShotSenderHelper;
126 oneShotSenderHelper.SetSendTime(Seconds(2));
127
128 oneShotSenderHelper.Install(endDevices);
129
130 /******************
131 * Set Data Rates *
132 ******************/
133 std::vector<int> sfQuantity(6);
134 sfQuantity = LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
135
136 /****************
137 * Simulation *
138 ****************/
139
141
143
145
146 return 0;
147}
Helper class used to assign positions and mobility models to nodes.
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
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
Helps to create LoraNetDevice objects.
Definition lora-helper.h:34
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
Helper to install LoraPhy instances on multiple Nodes.
void SetDeviceType(enum DeviceType dt)
Set the kind of PHY this helper will create.
void SetChannel(Ptr< LoraChannel > channel)
Set the LoraChannel to connect the PHYs to.
Helper class for configuring and installing the LorawanMac class on devices and gateways.
void SetDeviceType(enum DeviceType dt)
Set the kind of MAC this helper will create.
static std::vector< int > SetSpreadingFactorsUp(NodeContainer endDevices, NodeContainer gateways, Ptr< LoraChannel > channel)
Initialize the end devices' data rate parameter.
This class can be used to install OneShotSender applications on multiple nodes at once.
void SetSendTime(Time sendTime)
Set the send time of the applications.
ApplicationContainer Install(NodeContainer c) const
Install a OneShotSender application on each node of the input container configured with all the attri...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:267
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
Time Hours(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:279
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:108
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:110
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:111
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:112
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:96
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:297
channel
Definition third.py:77
mobility
Definition third.py:92