lora-network-server.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2018 University of Padova
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  * Authors: Davide Magrin <magrinda@dei.unipd.it>
19  * Martina Capuzzo <capuzzom@dei.unipd.it>
20  *
21  * Modified by: Bastien Tauran <bastien.tauran@viveris.fr>
22  */
23 
24 #include "lora-network-server.h"
25 
26 #include "lora-device-address.h"
27 #include "lora-frame-header.h"
28 #include "lora-network-status.h"
29 #include "lorawan-mac-command.h"
31 #include "lorawan-mac-header.h"
33 
34 #include <ns3/log.h>
35 
36 namespace ns3
37 {
38 
39 NS_LOG_COMPONENT_DEFINE("LoraNetworkServer");
40 
41 NS_OBJECT_ENSURE_REGISTERED(LoraNetworkServer);
42 
43 TypeId
45 {
46  static TypeId tid =
47  TypeId("ns3::LoraNetworkServer")
48  .SetParent<Application>()
49  .AddConstructor<LoraNetworkServer>()
50  .AddTraceSource(
51  "ReceivedPacket",
52  "Trace source that is fired when a packet arrives at the Network Server",
53  MakeTraceSourceAccessor(&LoraNetworkServer::m_receivedPacket),
54  "ns3::Packet::TracedCallback");
55  return tid;
56 }
57 
59  : m_status(Create<LoraNetworkStatus>()),
60  m_controller(Create<LoraNetworkController>(m_status)),
61  m_scheduler(Create<LoraNetworkScheduler>(m_status, m_controller))
62 {
63  NS_LOG_FUNCTION_NOARGS();
64 }
65 
67 {
68  NS_LOG_FUNCTION_NOARGS();
69 }
70 
71 void
73 {
74  NS_LOG_FUNCTION_NOARGS();
75 }
76 
77 void
79 {
80  NS_LOG_FUNCTION_NOARGS();
81 }
82 
83 void
84 LoraNetworkServer::AddGateway(Ptr<Node> gateway, Ptr<NetDevice> netDevice)
85 {
86  NS_LOG_FUNCTION(this << gateway);
87 
88  // Get the PointToPointNetDevice
89  Ptr<PointToPointNetDevice> p2pNetDevice;
90  for (uint32_t i = 0; i < gateway->GetNDevices(); i++)
91  {
92  p2pNetDevice = gateway->GetDevice(i)->GetObject<PointToPointNetDevice>();
93  if (p2pNetDevice != nullptr)
94  {
95  // We found a p2pNetDevice on the gateway
96  break;
97  }
98  }
99 
100  // Get the gateway's LoRa MAC layer (assumes gateway's MAC is configured as first device)
101  Ptr<SatLorawanNetDevice> satLoraNetDevice =
102  DynamicCast<SatLorawanNetDevice>(gateway->GetDevice(1));
103  Ptr<LorawanMacGateway> gwMac = DynamicCast<LorawanMacGateway>(satLoraNetDevice->GetMac());
104  NS_ASSERT(gwMac != nullptr);
105 
106  // Get the Address
107  Address gatewayAddress = p2pNetDevice->GetAddress();
108 
109  // Create new gatewayStatus
110  Ptr<LoraGatewayStatus> gwStatus = Create<LoraGatewayStatus>(gatewayAddress, netDevice, gwMac);
111 
112  m_status->AddGateway(gatewayAddress, gwStatus);
113 }
114 
115 void
116 LoraNetworkServer::AddNodes(NodeContainer nodes)
117 {
118  NS_LOG_FUNCTION_NOARGS();
119 
120  // For each node in the container, call the function to add that single node
121  NodeContainer::Iterator it;
122  for (it = nodes.Begin(); it != nodes.End(); it++)
123  {
124  AddNode(*it);
125  }
126 }
127 
128 void
130 {
131  NS_LOG_FUNCTION(this << node);
132 
133  // Get the SatLorawanNetDevice
134  Ptr<SatLorawanNetDevice> loraNetDevice;
135  for (uint32_t i = 0; i < node->GetNDevices(); i++)
136  {
137  loraNetDevice = DynamicCast<SatLorawanNetDevice>(node->GetDevice(i));
138  if (loraNetDevice != nullptr)
139  {
140  // We found a SatLorawanNetDevice on the node
141  break;
142  }
143  }
144 
145  // Get the MAC
146  Ptr<LorawanMacEndDeviceClassA> edLorawanMac =
147  DynamicCast<LorawanMacEndDeviceClassA>(loraNetDevice->GetMac());
148 
149  // Update the NetworkStatus about the existence of this node
150  m_status->AddNode(edLorawanMac);
151 }
152 
153 bool
154 LoraNetworkServer::Receive(Ptr<NetDevice> device,
155  Ptr<const Packet> packet,
156  uint16_t protocol,
157  const Address& address)
158 {
159  NS_LOG_FUNCTION(this << packet << protocol << address);
160 
161  // Fire the trace source
162  m_receivedPacket(packet);
163 
164  // Inform the scheduler of the newly arrived packet
165  m_scheduler->OnReceivedPacket(packet);
166 
167  // Inform the status of the newly arrived packet
168  m_status->OnReceivedPacket(packet, address);
169 
170  // Inform the controller of the newly arrived packet
171  m_controller->OnNewPacket(packet);
172 
173  return true;
174 }
175 
176 void
177 LoraNetworkServer::AddComponent(Ptr<LoraNetworkControllerComponent> component)
178 {
179  NS_LOG_FUNCTION(this << component);
180 
181  m_controller->Install(component);
182 }
183 
184 Ptr<LoraNetworkStatus>
186 {
187  return m_status;
188 }
189 
190 } // namespace ns3
This class collects a series of components that deal with various aspects of managing the network,...
bool Receive(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &address)
Receive a packet from a gateway.
TracedCallback< Ptr< const Packet > > m_receivedPacket
static TypeId GetTypeId(void)
void StartApplication(void)
Start the NS application.
void AddGateway(Ptr< Node > gateway, Ptr< NetDevice > netDevice)
Add this gateway to the list of gateways connected to this NS.
Ptr< LoraNetworkStatus > m_status
void AddNodes(NodeContainer nodes)
Inform the LoraNetworkServer that these nodes are connected to the network.
Ptr< LoraNetworkScheduler > m_scheduler
void AddNode(Ptr< Node > node)
Inform the LoraNetworkServer that this node is connected to the network.
void StopApplication(void)
Stop the NS application.
void AddComponent(Ptr< LoraNetworkControllerComponent > component)
A NetworkControllerComponent to this LoraNetworkServer instance.
Ptr< LoraNetworkController > m_controller
Ptr< LoraNetworkStatus > GetNetworkStatus(void)
This class represents the knowledge about the state of the network that is available at the Network S...
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.