satellite-orbiter-net-device-lora.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd.
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  * Author: Sami Rantanen <sami.rantanen@magister.fi>
19  */
20 
22 
23 #include "lorawan-mac-gateway.h"
24 #include "satellite-address-tag.h"
25 #include "satellite-channel.h"
27 #include "satellite-id-mapper.h"
28 #include "satellite-mac.h"
33 #include "satellite-phy-rx.h"
34 #include "satellite-phy-tx.h"
35 #include "satellite-phy.h"
36 #include "satellite-time-tag.h"
38 
39 #include <ns3/channel.h>
40 #include <ns3/error-model.h>
41 #include <ns3/ipv4-header.h>
42 #include <ns3/ipv4-l3-protocol.h>
43 #include <ns3/log.h>
44 #include <ns3/node.h>
45 #include <ns3/object-map.h>
46 #include <ns3/packet.h>
47 #include <ns3/pointer.h>
48 #include <ns3/singleton.h>
49 #include <ns3/trace-source-accessor.h>
50 #include <ns3/uinteger.h>
51 
52 #include <limits>
53 #include <map>
54 #include <set>
55 #include <utility>
56 #include <vector>
57 
58 NS_LOG_COMPONENT_DEFINE("SatOrbiterNetDeviceLora");
59 
60 namespace ns3
61 {
62 
63 NS_OBJECT_ENSURE_REGISTERED(SatOrbiterNetDeviceLora);
64 
65 TypeId
67 {
68  static TypeId tid = TypeId("ns3::SatOrbiterNetDeviceLora")
69  .SetParent<SatOrbiterNetDevice>()
70  .AddConstructor<SatOrbiterNetDeviceLora>();
71  return tid;
72 }
73 
76 {
77  NS_LOG_FUNCTION(this);
78 }
79 
80 void
81 SatOrbiterNetDeviceLora::ReceivePacketUser(Ptr<Packet> packet, const Address& userAddress)
82 {
83  NS_LOG_FUNCTION(this << packet);
84  NS_LOG_INFO("Receiving a packet: " << packet->GetUid());
85 
86  Mac48Address macUserAddress = Mac48Address::ConvertFrom(userAddress);
87 
88  m_packetTrace(Simulator::Now(),
91  m_nodeId,
92  macUserAddress,
95  SatUtils::GetPacketInfo(packet));
96 
97  /*
98  * Invoke the `Rx` and `RxDelay` trace sources. We look at the packet's tags
99  * for information, but cannot remove the tags because the packet is a const.
100  */
102  {
103  Address addr = GetRxUtAddress(packet, SatEnums::LD_RETURN);
104 
105  m_rxUserTrace(packet, addr);
106 
107  SatDevLinkTimeTag linkTimeTag;
108  if (packet->RemovePacketTag(linkTimeTag))
109  {
110  NS_LOG_DEBUG(this << " contains a SatDevLinkTimeTag tag");
111  Time delay = Simulator::Now() - linkTimeTag.GetSenderTimestamp();
112  m_rxUserLinkDelayTrace(delay, addr);
113  if (m_lastDelays[macUserAddress].IsZero() == false)
114  {
115  Time jitter = Abs(delay - m_lastDelays[macUserAddress]);
116  m_rxUserLinkJitterTrace(jitter, addr);
117  }
118  m_lastDelays[macUserAddress] = delay;
119  }
120  }
121 
122  SatGroundStationAddressTag groundStationAddressTag;
123  if (!packet->PeekPacketTag(groundStationAddressTag))
124  {
125  NS_FATAL_ERROR("SatGroundStationAddressTag not found");
126  }
127  Mac48Address destination = groundStationAddressTag.GetGroundStationAddress();
128 
129  SatUplinkInfoTag satUplinkInfoTag;
130  if (!packet->PeekPacketTag(satUplinkInfoTag))
131  {
132  NS_FATAL_ERROR("SatUplinkInfoTag not found");
133  }
134 
135  if (m_gwConnected.count(destination))
136  {
138  {
139  // Add a SatDevLinkTimeTag tag for packet link delay computation at the receiver end.
140  packet->AddPacketTag(SatDevLinkTimeTag(Simulator::Now()));
141  }
142 
143  DynamicCast<SatOrbiterFeederMac>(m_feederMac[satUplinkInfoTag.GetBeamId()])
144  ->EnquePacket(packet);
145  }
146  else
147  {
148  if (m_islNetDevices.size() > 0)
149  {
150  SendToIsl(packet, destination);
151  }
152  }
153 }
154 
155 void
156 SatOrbiterNetDeviceLora::ReceivePacketFeeder(Ptr<Packet> packet, const Address& feederAddress)
157 {
158  NS_LOG_FUNCTION(this << packet);
159  NS_LOG_INFO("Receiving a packet: " << packet->GetUid());
160 
161  Mac48Address macFeederAddress = Mac48Address::ConvertFrom(feederAddress);
162 
163  m_packetTrace(Simulator::Now(),
166  m_nodeId,
167  macFeederAddress,
170  SatUtils::GetPacketInfo(packet));
171 
172  /*
173  * Invoke the `Rx` and `RxDelay` trace sources. We look at the packet's tags
174  * for information, but cannot remove the tags because the packet is a const.
175  */
177  {
178  Address addr = GetRxUtAddress(packet, SatEnums::LD_FORWARD);
179 
180  m_rxFeederTrace(packet, addr);
181 
182  SatDevLinkTimeTag linkTimeTag;
183  if (packet->RemovePacketTag(linkTimeTag))
184  {
185  NS_LOG_DEBUG(this << " contains a SatDevLinkTimeTag tag");
186  Time delay = Simulator::Now() - linkTimeTag.GetSenderTimestamp();
187  m_rxFeederLinkDelayTrace(delay, addr);
188  if (m_lastDelays[macFeederAddress].IsZero() == false)
189  {
190  Time jitter = Abs(delay - m_lastDelays[macFeederAddress]);
191  m_rxFeederLinkJitterTrace(jitter, addr);
192  }
193  m_lastDelays[macFeederAddress] = delay;
194  }
195  }
196 
197  SatGroundStationAddressTag groundStationAddressTag;
198  if (!packet->PeekPacketTag(groundStationAddressTag))
199  {
200  NS_FATAL_ERROR("SatGroundStationAddressTag not found");
201  }
202  Mac48Address destination = groundStationAddressTag.GetGroundStationAddress();
203 
204  if (destination.IsBroadcast())
205  {
206  m_broadcastReceived.insert(packet->GetUid());
207  }
208 
209  SatUplinkInfoTag satUplinkInfoTag;
210  if (!packet->PeekPacketTag(satUplinkInfoTag))
211  {
212  NS_FATAL_ERROR("SatUplinkInfoTag not found");
213  }
214 
215  if (m_utConnected.count(destination) > 0 || destination.IsBroadcast())
216  {
218  {
219  // Add a SatDevLinkTimeTag tag for packet link delay computation at the receiver end.
220  packet->AddPacketTag(SatDevLinkTimeTag(Simulator::Now()));
221  }
222 
223  DynamicCast<LorawanMacGateway>(m_userMac[satUplinkInfoTag.GetBeamId()])->Send(packet);
224  }
225  if ((m_utConnected.count(destination) == 0 || destination.IsBroadcast()) &&
226  m_islNetDevices.size() > 0)
227  {
228  SendToIsl(packet, destination);
229  }
230 }
231 
232 void
233 SatOrbiterNetDeviceLora::ReceiveFromIsl(Ptr<Packet> packet, Mac48Address destination)
234 {
235  NS_LOG_FUNCTION(this << packet << destination);
236 
237  if (destination.IsBroadcast())
238  {
239  if (m_broadcastReceived.count(packet->GetUid()) > 0)
240  {
241  // Packet already received, drop it
242  return;
243  }
244  else
245  {
246  // Insert in list of receuived broadcast
247  m_broadcastReceived.insert(packet->GetUid());
248  }
249  }
250 
251  if (m_gwConnected.count(destination) > 0)
252  {
253  SatUplinkInfoTag satUplinkInfoTag;
254  if (!packet->PeekPacketTag(satUplinkInfoTag))
255  {
256  NS_FATAL_ERROR("SatUplinkInfoTag not found");
257  }
258 
260  {
261  // Add a SatDevLinkTimeTag tag for packet link delay computation at the receiver end.
262  packet->AddPacketTag(SatDevLinkTimeTag(Simulator::Now()));
263  }
264 
265  DynamicCast<SatOrbiterFeederMac>(m_feederMac[satUplinkInfoTag.GetBeamId()])
266  ->EnquePacket(packet);
267  }
268  else
269  {
270  if (m_utConnected.count(destination) > 0 || destination.IsBroadcast())
271  {
272  SatUplinkInfoTag satUplinkInfoTag;
273  if (!packet->PeekPacketTag(satUplinkInfoTag))
274  {
275  NS_FATAL_ERROR("SatUplinkInfoTag not found");
276  }
277 
278  if (m_isStatisticsTagsEnabled && !destination.IsBroadcast())
279  {
280  // Add a SatDevLinkTimeTag tag for packet link delay computation at the receiver
281  // end.
282  packet->AddPacketTag(SatDevLinkTimeTag(Simulator::Now()));
283  }
284 
285  DynamicCast<LorawanMacGateway>(m_userMac[satUplinkInfoTag.GetBeamId()])->Send(packet);
286  }
287  if ((m_utConnected.count(destination) == 0 || destination.IsBroadcast()) &&
288  m_islNetDevices.size() > 0)
289  {
290  SendToIsl(packet, destination);
291  }
292  }
293 }
294 
295 bool
297  const Address& dest,
298  Ptr<SatSignalParameters> rxParams)
299 {
300  NS_LOG_FUNCTION(this << msg << dest);
301 
302  return true;
303 }
304 
305 void
306 SatOrbiterNetDeviceLora::ConnectUt(Mac48Address utAddress, uint32_t beamId)
307 {
308  NS_LOG_FUNCTION(this << utAddress << beamId);
309 
310  NS_ASSERT_MSG(m_utConnected.find(utAddress) == m_utConnected.end(),
311  "Cannot add same UT twice to map");
312 
313  m_utConnected.insert({utAddress, beamId});
314  Singleton<SatIdMapper>::Get()->AttachMacToSatIdIsl(utAddress, m_nodeId);
315 }
316 
317 void
318 SatOrbiterNetDeviceLora::DisconnectUt(Mac48Address utAddress, uint32_t beamId)
319 {
320  NS_LOG_FUNCTION(this << utAddress << beamId);
321 
322  NS_ASSERT_MSG(m_utConnected.find(utAddress) != m_utConnected.end(), "UT not in map");
323 
324  m_utConnected.erase(utAddress);
325  Singleton<SatIdMapper>::Get()->RemoveMacToSatIdIsl(utAddress);
326 }
327 
328 } // namespace ns3
Tag to store ground station destination address.
Mac48Address GetGroundStationAddress(void) const
Get the ground station MAC address.
SatOrbiterNetDevice to be utilized in geostationary satellite.
std::map< Mac48Address, uint32_t > m_gwConnected
Set containing all connected GWs.
std::set< uint32_t > m_broadcastReceived
Keep a count of all incoming broadcast data to avoid handling them several times.
Address GetRxUtAddress(Ptr< Packet > packet, SatEnums::SatLinkDir_t ld)
Get UT MAC address associated to this packet.
std::vector< Ptr< PointToPointIslNetDevice > > m_islNetDevices
List of ISLs starting from this node.
TracedCallback< const Time &, const Address & > m_rxUserLinkJitterTrace
Traced callback for all received packets, including user link jitter information and the address of t...
TracedCallback< Ptr< const Packet >, const Address & > m_rxUserTrace
Traced callback for all received packets on user, including the address of the senders.
TracedCallback< Ptr< const Packet >, const Address & > m_rxFeederTrace
Traced callback for all received packets on feeder, including the address of the senders.
TracedCallback< const Time &, const Address & > m_rxFeederLinkDelayTrace
Traced callback for all received packets, including feeder link delay information and the address of ...
std::map< uint32_t, Ptr< SatMac > > m_feederMac
std::map< Mac48Address, Time > m_lastDelays
std::map< uint32_t, Ptr< SatMac > > m_userMac
void SendToIsl(Ptr< Packet > packet, Mac48Address destination)
Send a packet to ISL.
TracedCallback< Time, SatEnums::SatPacketEvent_t, SatEnums::SatNodeType_t, uint32_t, Mac48Address, SatEnums::SatLogLevel_t, SatEnums::SatLinkDir_t, std::string > m_packetTrace
TracedCallback< const Time &, const Address & > m_rxUserLinkDelayTrace
Traced callback for all received packets, including user link delay information and the address of th...
TracedCallback< const Time &, const Address & > m_rxFeederLinkJitterTrace
Traced callback for all received packets, including feeder link jitter information and the address of...
std::map< Mac48Address, uint32_t > m_utConnected
Set containing all connected UTs.
virtual void DisconnectUt(Mac48Address utAddress, uint32_t beamId)
Disconnect a UT to this satellite.
void ReceivePacketUser(Ptr< Packet > packet, const Address &userAddress)
Receive the packet from the lower layers, in network regeneration on return link.
static TypeId GetTypeId(void)
Get the type ID.
void ReceivePacketFeeder(Ptr< Packet > packet, const Address &feederAddress)
Receive the packet from the lower layers, in network regeneration on forward link.
virtual bool SendControlMsgToFeeder(Ptr< SatControlMessage > msg, const Address &dest, Ptr< SatSignalParameters > rxParams)
Send a control packet on the feeder link.
void ReceiveFromIsl(Ptr< Packet > packet, Mac48Address destination)
Receive a packet from ISL.
virtual void ConnectUt(Mac48Address utAddress, uint32_t beamId)
Connect a UT to this satellite.
static std::string GetPacketInfo(const Ptr< const Packet > p)
Get packet information in std::string for printing purposes.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.