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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
10
11namespace ns3
12{
13namespace lorawan
14{
15
16NS_LOG_COMPONENT_DEFINE("EndDeviceLoraPhy");
17
19
20/**************************
21 * Listener destructor *
22 *************************/
23
27
30{
31 static TypeId tid =
32 TypeId("ns3::EndDeviceLoraPhy")
34 .SetGroupName("lorawan")
35 .AddTraceSource("LostPacketBecauseWrongFrequency",
36 "Trace source indicating a packet "
37 "could not be correctly decoded because"
38 "the end device was listening on a different frequency",
40 "ns3::Packet::TracedCallback")
41 .AddTraceSource("LostPacketBecauseWrongSpreadingFactor",
42 "Trace source indicating a packet "
43 "could not be correctly decoded because"
44 "the end device was listening for a different Spreading Factor",
46 "ns3::Packet::TracedCallback")
47 .AddTraceSource("EndDeviceState",
48 "The current state of the device",
50 "ns3::TracedValueCallback::EndDeviceLoraPhy::State");
51 return tid;
52}
53
54// Initialize the device with some common settings.
55// These will then be changed by helpers.
62
66
67// Downlink sensitivity (from SX1272 datasheet)
68// {SF7, SF8, SF9, SF10, SF11, SF12}
69// These sensitivities are for a bandwidth of 125000 Hz
70const double EndDeviceLoraPhy::sensitivity[6] = {-124, -127, -130, -133, -135, -137};
71
72void
74{
75 m_sf = sf;
76}
77
78uint8_t
80{
81 return m_sf;
82}
83
84bool
89
90bool
92{
93 return m_frequencyHz == frequencyHz;
94}
95
96void
98{
99 m_frequencyHz = frequencyHz;
100}
101
102void
104{
105 NS_LOG_FUNCTION(this << packet);
106 // Switch back to STANDBY mode.
107 // For reference see SX1272 datasheet, section 4.1.6
109 // Forward packet to the upper layer (if the callback was set).
110 if (!m_txFinishedCallback.IsNull())
111 {
112 m_txFinishedCallback(packet);
113 }
114}
115
116void
118{
120
122
123 // Notify listeners of the state change
124 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
125 {
126 (*i)->NotifyStandby();
127 }
128}
129
130void
132{
134
136
138
139 // Notify listeners of the state change
140 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
141 {
142 (*i)->NotifyRxStart();
143 }
144}
145
146void
148{
150
152
154
155 // Notify listeners of the state change
156 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
157 {
158 (*i)->NotifyTxStart(txPowerDbm);
159 }
160}
161
162void
164{
166
168
170
171 // Notify listeners of the state change
172 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
173 {
174 (*i)->NotifySleep();
175 }
176}
177
185
186void
188{
189 m_listeners.push_back(listener);
190}
191
192void
194{
195 auto i = find(m_listeners.begin(), m_listeners.end(), listener);
196 if (i != m_listeners.end())
197 {
198 m_listeners.erase(i);
199 }
200}
201
202std::ostream&
203operator<<(std::ostream& os, const EndDeviceLoraPhy::State& state)
204{
205 switch (state)
206 {
208 return (os << "SLEEP");
210 return (os << "STANDBY");
212 return (os << "TX");
214 return (os << "RX");
215 default:
216 NS_FATAL_ERROR("Invalid LoRa device PHY state");
217 }
218}
219
220} // namespace lorawan
221} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
Class representing a LoRa transceiver.
void SwitchToSleep()
Switch to the SLEEP state.
EndDeviceLoraPhy::State GetState()
Return the state this end device is currently in.
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.
bool IsOnFrequency(uint32_t frequencyHz) override
Whether this device is listening on the specified frequency or not.
void SwitchToRx()
Switch to the RX state.
void SwitchToStandby()
Switch to the STANDBY state.
EndDeviceLoraPhy()
Default constructor.
~EndDeviceLoraPhy() override
Destructor.
void SetFrequency(uint32_t frequencyHz)
Set the frequency this end device will listen on.
uint32_t m_frequencyHz
The frequency [Hz] this device is listening on.
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.
@ TX
The PHY layer is sending a packet.
@ STANDBY
The PHY layer is in STANDBY.
@ RX
The PHY layer is receiving a packet.
void TxFinished(Ptr< const Packet > packet) override
Signals the end of a transmission by the EndDeviceLoraPhy.
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.
TxFinishedCallback m_txFinishedCallback
The callback to perform upon the end of a transmission.
Definition lora-phy.h:346
LoraPhy()
Default constructor.
Definition lora-phy.cc:67
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#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:35
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
std::ostream & operator<<(std::ostream &os, const EndDeviceLoraPhy::State &state)
Overloaded operator to print the value of a EndDeviceLoraPhy::State.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ SLEEP
The PHY layer is sleeping.