satellite-lorawan-net-device.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: Bastien TAURAN <bastien.tauran@viveris.fr>
19  */
20 
22 
23 #include "lora-frame-header.h"
24 #include "lorawan-mac-header.h"
25 
26 #include <ns3/log.h>
27 
28 NS_LOG_COMPONENT_DEFINE("SatLorawanNetDevice");
29 
30 namespace ns3
31 {
32 
33 NS_OBJECT_ENSURE_REGISTERED(SatLorawanNetDevice);
34 
35 TypeId
37 {
38  static TypeId tid =
39  TypeId("ns3::SatLorawanNetDevice")
40  .SetParent<SatNetDevice>()
41  .AddAttribute("ForwardToUtUsers",
42  "Forward to UT users or stop packet transmission here",
43  BooleanValue(false),
44  MakeBooleanAccessor(&SatLorawanNetDevice::m_forwardToUtUsers),
45  MakeBooleanChecker())
46  .AddConstructor<SatLorawanNetDevice>();
47  return tid;
48 }
49 
51 {
52  NS_LOG_FUNCTION(this);
53 }
54 
55 void
56 SatLorawanNetDevice::Receive(Ptr<const Packet> packet)
57 {
58  NS_LOG_FUNCTION(this << packet);
59  NS_LOG_INFO("Receiving a packet: " << packet->GetUid());
60 
61  // Add packet trace entry:
64 
65  m_packetTrace(Simulator::Now(),
67  m_nodeInfo->GetNodeType(),
68  m_nodeInfo->GetNodeId(),
69  m_nodeInfo->GetMacAddress(),
71  ld,
72  SatUtils::GetPacketInfo(packet));
73 
74  /*
75  * Invoke the `Rx` and `RxDelay` trace sources. We look at the packet's tags
76  * for information, but cannot remove the tags because the packet is a const.
77  */
79  {
80  Address addr; // invalid address.
81  bool isTaggedWithAddress = false;
82  ByteTagIterator it = packet->GetByteTagIterator();
83 
84  while (!isTaggedWithAddress && it.HasNext())
85  {
86  ByteTagIterator::Item item = it.Next();
87 
88  if (item.GetTypeId() == SatAddressTag::GetTypeId())
89  {
90  NS_LOG_DEBUG(this << " contains a SatAddressTag tag:"
91  << " start=" << item.GetStart() << " end=" << item.GetEnd());
92  SatAddressTag addrTag;
93  item.GetTag(addrTag);
94  addr = addrTag.GetSourceAddress();
95  isTaggedWithAddress = true; // this will exit the while loop.
96  }
97  }
98 
99  m_rxTrace(packet, addr);
100 
101  SatDevTimeTag timeTag;
102  if (packet->PeekPacketTag(timeTag))
103  {
104  NS_LOG_DEBUG(this << " contains a SatMacTimeTag tag");
105  Time delay = Simulator::Now() - timeTag.GetSenderTimestamp();
106  m_rxDelayTrace(delay, addr);
107  if (m_lastDelay.IsZero() == false)
108  {
109  Time jitter = Abs(delay - m_lastDelay);
110  m_rxJitterTrace(jitter, addr);
111  }
112  m_lastDelay = delay;
113  }
114  }
115 
116  // Forward to Network Server if on GW.
117  if (m_nodeInfo->GetNodeType() == SatEnums::NT_GW)
118  {
119  Ptr<Packet> pktCopy = packet->Copy();
120  m_rxNetworkServerCallback(this, pktCopy, Ipv4L3Protocol::PROT_NUMBER, Address());
121  }
122 
123  // Pass the packet to the upper layer if IP header in packet (GW or UT side)
124  if (m_forwardToUtUsers)
125  {
126  Ptr<Packet> pktCopy = packet->Copy();
127  LorawanMacHeader mHdr;
128  pktCopy->RemoveHeader(mHdr);
129  LoraFrameHeader fHdr;
130  pktCopy->RemoveHeader(fHdr);
131  m_rxCallback(this, pktCopy, Ipv4L3Protocol::PROT_NUMBER, Address());
132  }
133 }
134 
135 bool
136 SatLorawanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
137 {
138  NS_LOG_FUNCTION(this << packet);
139 
140  // Add packet trace entry:
143 
144  m_packetTrace(Simulator::Now(),
146  m_nodeInfo->GetNodeType(),
147  m_nodeInfo->GetNodeId(),
148  m_nodeInfo->GetMacAddress(),
150  ld,
151  SatUtils::GetPacketInfo(packet));
152 
153  m_txTrace(packet);
154 
155  Address addr; // invalid address.
156  bool isTaggedWithAddress = false;
157  ByteTagIterator it = packet->GetByteTagIterator();
158 
159  while (!isTaggedWithAddress && it.HasNext())
160  {
161  ByteTagIterator::Item item = it.Next();
162 
163  if (item.GetTypeId() == SatAddressTag::GetTypeId())
164  {
165  NS_LOG_DEBUG(this << " contains a SatAddressTag tag:"
166  << " start=" << item.GetStart() << " end=" << item.GetEnd());
167  SatAddressTag addrTag;
168  item.GetTag(addrTag);
169  addr = addrTag.GetSourceAddress();
170  isTaggedWithAddress = true; // this will exit the while loop.
171  }
172  }
173 
174  m_lorawanMac->Send(packet, dest, protocolNumber);
175 
176  return true;
177 }
178 
179 bool
180 SatLorawanNetDevice::SendControlMsg(Ptr<SatControlMessage> msg, const Address& dest)
181 {
182  // We send nothing in Lora Mode. Control is made via LorawanMacCommand
183  return true;
184 }
185 
186 Ptr<LorawanMac>
188 {
189  return m_lorawanMac;
190 }
191 
192 void
193 SatLorawanNetDevice::SetLorawanMac(Ptr<LorawanMac> lorawanMac)
194 {
195  SetMac(lorawanMac);
196  m_lorawanMac = lorawanMac;
197 }
198 
199 void
201 {
202  NS_LOG_FUNCTION(this << &cb);
204 }
205 
206 void
208 {
209  NS_LOG_FUNCTION(this);
210 
212 }
213 
214 } // namespace ns3
This class represents the Frame header (FHDR) used in a LoraWAN network.
This class represents the Mac header of a LoRaWAN packet.
This class implements a tag that carries the MAC address of the sender of the packet.
static TypeId GetTypeId()
Inherited from ObjectBase base class.
Address GetSourceAddress() const
Get the source address.
Time tag used to identify the time when packet is enqueued at device level.
Time GetSenderTimestamp(void) const
Get sender time stamp of this tag.
SatLinkDir_t
Link direction used for packet tracing.
void Receive(Ptr< const Packet > packet)
void SetReceiveNetworkServerCallback(SatLorawanNetDevice::ReceiveCallback cb)
void SetLorawanMac(Ptr< LorawanMac > lorawanMac)
bool SendControlMsg(Ptr< SatControlMessage > msg, const Address &dest)
static TypeId GetTypeId(void)
Get the type ID.
SatLorawanNetDevice()
Default constructor.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual void DoDispose(void)
Method called to inform the Scheduler of a newly arrived uplink packet.
Callback< bool, Ptr< SatLorawanNetDevice >, Ptr< const Packet >, uint16_t, const Address & > ReceiveCallback
SatNetDevice to be utilized in the UT and GW nodes.
TracedCallback< const Time &, const Address & > m_rxDelayTrace
Traced callback for all received packets, including delay information and the address of the senders.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced callback for all packets received to be transmitted.
Time m_lastDelay
Last delay measurement.
virtual void DoDispose(void)
Dispose of this class instance.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced callback for all received packets, including the address of the senders.
Ptr< SatNodeInfo > m_nodeInfo
TracedCallback< Time, SatEnums::SatPacketEvent_t, SatEnums::SatNodeType_t, uint32_t, Mac48Address, SatEnums::SatLogLevel_t, SatEnums::SatLinkDir_t, std::string > m_packetTrace
void SetMac(Ptr< SatMac > mac)
TracedCallback< const Time &, const Address & > m_rxJitterTrace
Traced callback for all received packets, including jitter information and the address of the senders...
NetDevice::ReceiveCallback m_rxCallback
bool m_isStatisticsTagsEnabled
EnableStatisticsTags attribute.
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.