lora-periodic-sender.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2017 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  * Author: Davide Magrin <magrinda@dei.unipd.it>
19  *
20  * Modified by: Bastien Tauran <bastien.tauran@viveris.fr>
21  */
22 
23 #include "lora-periodic-sender.h"
24 
26 
27 #include <ns3/double.h>
28 #include <ns3/log.h>
29 #include <ns3/pointer.h>
30 #include <ns3/string.h>
31 
32 namespace ns3
33 {
34 
35 NS_LOG_COMPONENT_DEFINE("LoraPeriodicSender");
36 
37 NS_OBJECT_ENSURE_REGISTERED(LoraPeriodicSender);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId("ns3::LoraPeriodicSender")
43  .SetParent<Application>()
44  .AddConstructor<LoraPeriodicSender>()
45  .AddAttribute("Interval",
46  "The interval between packet sends of this app",
47  TimeValue(Seconds(0)),
48  MakeTimeAccessor(&LoraPeriodicSender::GetInterval,
50  MakeTimeChecker());
51  // .AddAttribute ("PacketSizeRandomVariable", "The random variable that determines the shape of
52  // the packet size, in bytes",
53  // StringValue ("ns3::UniformRandomVariable[Min=0,Max=10]"),
54  // MakePointerAccessor (&LoraPeriodicSender::m_pktSizeRV),
55  // MakePointerChecker <RandomVariableStream>());
56  return tid;
57 }
58 
60  : m_interval(Seconds(10)),
61  m_initialDelay(Seconds(1)),
62  m_basePktSize(10),
63  m_pktSizeRV(0)
64 
65 {
66  NS_LOG_FUNCTION_NOARGS();
67 }
68 
70 {
71  NS_LOG_FUNCTION_NOARGS();
72 }
73 
74 void
76 {
77  NS_LOG_FUNCTION(this << interval);
78  m_interval = interval;
79 }
80 
81 Time
83 {
84  NS_LOG_FUNCTION(this);
85  return m_interval;
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION(this << delay);
92  m_initialDelay = delay;
93 }
94 
95 void
97 {
98  m_pktSizeRV = rv;
99 }
100 
101 void
103 {
104  m_basePktSize = size;
105 }
106 
107 void
109 {
110  NS_LOG_FUNCTION(this);
111 
112  // Create and send a new packet
113  Ptr<Packet> packet;
114  if (m_pktSizeRV)
115  {
116  int randomsize = m_pktSizeRV->GetInteger();
117  packet = Create<Packet>(m_basePktSize + randomsize);
118  }
119  else
120  {
121  packet = Create<Packet>(m_basePktSize);
122  }
123  m_mac->Send(packet);
124 
125  // Schedule the next SendPacket event
126  m_sendEvent = Simulator::Schedule(m_interval, &LoraPeriodicSender::SendPacket, this);
127 
128  NS_LOG_DEBUG("Sent a packet of size " << packet->GetSize());
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION(this);
135 
136  // Make sure we have a MAC layer
137  if (m_mac == nullptr)
138  {
139  Ptr<SatLorawanNetDevice> loraNetDevice;
140  for (uint32_t i = 0; i < m_node->GetNDevices(); i++)
141  {
142  loraNetDevice = DynamicCast<SatLorawanNetDevice>(m_node->GetDevice(i));
143  if (loraNetDevice)
144  {
145  break;
146  }
147  }
148 
149  m_mac = DynamicCast<LorawanMac>(loraNetDevice->GetMac());
150  NS_ASSERT(m_mac != nullptr);
151  }
152 
153  // Schedule the next SendPacket event
154  Simulator::Cancel(m_sendEvent);
155  NS_LOG_DEBUG("Starting up application with a first event with a " << m_initialDelay.GetSeconds()
156  << " seconds delay");
157  m_sendEvent = Simulator::Schedule(m_initialDelay, &LoraPeriodicSender::SendPacket, this);
158  NS_LOG_DEBUG("Event Id: " << m_sendEvent.GetUid());
159 }
160 
161 void
163 {
164  NS_LOG_FUNCTION_NOARGS();
165  Simulator::Cancel(m_sendEvent);
166 }
167 
168 } // namespace ns3
Time m_initialDelay
The initial delay of this application.
void StartApplication(void)
Start the application by scheduling the first SendPacket event.
void SendPacket(void)
Send a packet using the LoraNetDevice's Send method.
uint8_t m_basePktSize
The packet size.
Ptr< LorawanMac > m_mac
The MAC layer of this node.
void SetPacketSizeRandomVariable(Ptr< RandomVariableStream > rv)
Set if using randomness in the packet size.
void SetPacketSize(uint8_t size)
Set packet size.
Time GetInterval(void) const
Get the sending inteval.
void SetInterval(Time interval)
Set the sending interval.
static TypeId GetTypeId(void)
Time m_interval
The interval between to consecutive send events.
Ptr< RandomVariableStream > m_pktSizeRV
The random variable that adds bytes to the packet size.
EventId m_sendEvent
The sending event scheduled as next.
void SetInitialDelay(Time delay)
Set the initial delay of this application.
void StopApplication(void)
Stop the application.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.