A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
end-device-lora-phy.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 */
19
20#include "end-device-lora-phy.h"
21
22#include "lora-tag.h"
23
24#include "ns3/log.h"
25#include "ns3/simulator.h"
26
27#include <algorithm>
28
29namespace ns3
30{
31namespace lorawan
32{
33
34NS_LOG_COMPONENT_DEFINE("EndDeviceLoraPhy");
35
36NS_OBJECT_ENSURE_REGISTERED(EndDeviceLoraPhy);
37
38/**************************
39 * Listener destructor *
40 *************************/
41
43{
44}
45
48{
49 static TypeId tid =
50 TypeId("ns3::EndDeviceLoraPhy")
52 .SetGroupName("lorawan")
53 .AddTraceSource("LostPacketBecauseWrongFrequency",
54 "Trace source indicating a packet "
55 "could not be correctly decoded because"
56 "the end device was listening on a different frequency",
58 "ns3::Packet::TracedCallback")
59 .AddTraceSource("LostPacketBecauseWrongSpreadingFactor",
60 "Trace source indicating a packet "
61 "could not be correctly decoded because"
62 "the end device was listening for a different Spreading Factor",
64 "ns3::Packet::TracedCallback")
65 .AddTraceSource("EndDeviceState",
66 "The current state of the device",
68 "ns3::TracedValueCallback::EndDeviceLoraPhy::State");
69 return tid;
70}
71
72// Initialize the device with some common settings.
73// These will then be changed by helpers.
75 : m_state(SLEEP),
76 m_frequency(868.1),
77 m_sf(7)
78{
79}
80
82{
83}
84
85// Downlink sensitivity (from SX1272 datasheet)
86// {SF7, SF8, SF9, SF10, SF11, SF12}
87// These sensitivities are for a bandwidth of 125000 Hz
88const double EndDeviceLoraPhy::sensitivity[6] = {-124, -127, -130, -133, -135, -137};
89
90void
92{
93 m_sf = sf;
94}
95
96uint8_t
98{
99 return m_sf;
100}
101
102bool
104{
105 return m_state == TX;
106}
107
108bool
110{
111 return m_frequency == frequencyMHz;
112}
113
114void
116{
117 m_frequency = frequencyMHz;
118}
119
120void
122{
123 NS_LOG_FUNCTION(this << packet);
124 // Switch back to STANDBY mode.
125 // For reference see SX1272 datasheet, section 4.1.6
127 // Forward packet to the upper layer (if the callback was set).
129 {
130 m_txFinishedCallback(packet);
131 }
132}
133
134void
136{
138
140
141 // Notify listeners of the state change
142 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
143 {
144 (*i)->NotifyStandby();
145 }
146}
147
148void
150{
152
154
155 m_state = RX;
156
157 // Notify listeners of the state change
158 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
159 {
160 (*i)->NotifyRxStart();
161 }
162}
163
164void
166{
168
169 NS_ASSERT(m_state != RX);
170
171 m_state = TX;
172
173 // Notify listeners of the state change
174 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
175 {
176 (*i)->NotifyTxStart(txPowerDbm);
177 }
178}
179
180void
182{
184
186
187 m_state = SLEEP;
188
189 // Notify listeners of the state change
190 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
191 {
192 (*i)->NotifySleep();
193 }
194}
195
198{
200
201 return m_state;
202}
203
204void
206{
207 m_listeners.push_back(listener);
208}
209
210void
212{
213 auto i = find(m_listeners.begin(), m_listeners.end(), listener);
214 if (i != m_listeners.end())
215 {
216 m_listeners.erase(i);
217 }
218}
219
220} // namespace lorawan
221} // namespace ns3
bool IsNull() const
Check for null implementation.
Definition: callback.h:571
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
bool IsOnFrequency(double frequencyMHz) override
Whether this device is listening on the specified frequency or not.
void SwitchToSleep()
Switch to the SLEEP state.
EndDeviceLoraPhy::State GetState()
Return the state this end device is currently in.
void SetFrequency(double frequencyMHz)
Set the frequency this end device will listen on.
uint8_t GetSpreadingFactor() const
Get the Spreading Factor this end device is listening for.
static const double sensitivity[6]
The sensitivity vector of this device to different SFs.
Listeners m_listeners
PHY listeners.
void RegisterListener(EndDeviceLoraPhyListener *listener)
Add the input listener to the list of objects to be notified of PHY-level events.
bool IsTransmitting() override
Whether this device is transmitting or not.
uint8_t m_sf
The Spreading Factor this device is listening for.
static TypeId GetTypeId()
Register this type.
void SwitchToRx()
Switch to the RX state.
void SwitchToStandby()
Switch to the STANDBY state.
EndDeviceLoraPhy()
Default constructor.
~EndDeviceLoraPhy() override
Destructor.
void SetSpreadingFactor(uint8_t sf)
Set the Spreading Factor this end device will listen for.
TracedValue< State > m_state
The state this PHY is currently in.
TracedCallback< Ptr< const Packet >, uint32_t > m_wrongSf
Trace source for when a packet is lost because it was using a spreading factor different from the one...
void SwitchToTx(double txPowerDbm)
Switch to the TX state.
void UnregisterListener(EndDeviceLoraPhyListener *listener)
Remove the input listener from the list of objects to be notified of PHY-level events.
State
An enumeration of the possible states of an EndDeviceLoraPhy.
@ RX
The PHY layer is receiving a packet.
@ TX
The PHY layer is sending a packet.
@ SLEEP
The PHY layer is sleeping.
@ STANDBY
The PHY layer is in STANDBY.
void TxFinished(Ptr< const Packet > packet) override
Signals the end of a transmission by the EndDeviceLoraPhy.
double m_frequency
The frequency this device is listening on.
TracedCallback< Ptr< const Packet >, uint32_t > m_wrongFrequency
Trace source for when a packet is lost because it was transmitted on a frequency different from the o...
Receive notifications about PHY events.
Base class for PHY layers implementing the LoRa modulation scheme.
Definition: lora-phy.h:76
TxFinishedCallback m_txFinishedCallback
The callback to perform upon the end of a transmission.
Definition: lora-phy.h:341
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ SLEEP
The PHY layer is sleeping.