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
11#include "lora-tag.h"
12
13#include "ns3/log.h"
14#include "ns3/simulator.h"
15
16#include <algorithm>
17
18namespace ns3
19{
20namespace lorawan
21{
22
23NS_LOG_COMPONENT_DEFINE("EndDeviceLoraPhy");
24
25NS_OBJECT_ENSURE_REGISTERED(EndDeviceLoraPhy);
26
27/**************************
28 * Listener destructor *
29 *************************/
30
34
37{
38 static TypeId tid =
39 TypeId("ns3::EndDeviceLoraPhy")
41 .SetGroupName("lorawan")
42 .AddTraceSource("LostPacketBecauseWrongFrequency",
43 "Trace source indicating a packet "
44 "could not be correctly decoded because"
45 "the end device was listening on a different frequency",
47 "ns3::Packet::TracedCallback")
48 .AddTraceSource("LostPacketBecauseWrongSpreadingFactor",
49 "Trace source indicating a packet "
50 "could not be correctly decoded because"
51 "the end device was listening for a different Spreading Factor",
53 "ns3::Packet::TracedCallback")
54 .AddTraceSource("EndDeviceState",
55 "The current state of the device",
57 "ns3::TracedValueCallback::EndDeviceLoraPhy::State");
58 return tid;
59}
60
61// Initialize the device with some common settings.
62// These will then be changed by helpers.
64 : m_state(SLEEP),
65 m_frequency(868.1),
66 m_sf(7)
67{
68}
69
73
74// Downlink sensitivity (from SX1272 datasheet)
75// {SF7, SF8, SF9, SF10, SF11, SF12}
76// These sensitivities are for a bandwidth of 125000 Hz
77const double EndDeviceLoraPhy::sensitivity[6] = {-124, -127, -130, -133, -135, -137};
78
79void
81{
82 m_sf = sf;
83}
84
85uint8_t
87{
88 return m_sf;
89}
90
91bool
96
97bool
99{
100 return m_frequency == frequencyMHz;
101}
102
103void
105{
106 m_frequency = frequencyMHz;
107}
108
109void
111{
112 NS_LOG_FUNCTION(this << packet);
113 // Switch back to STANDBY mode.
114 // For reference see SX1272 datasheet, section 4.1.6
116 // Forward packet to the upper layer (if the callback was set).
118 {
119 m_txFinishedCallback(packet);
120 }
121}
122
123void
125{
127
129
130 // Notify listeners of the state change
131 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
132 {
133 (*i)->NotifyStandby();
134 }
135}
136
137void
139{
141
143
144 m_state = RX;
145
146 // Notify listeners of the state change
147 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
148 {
149 (*i)->NotifyRxStart();
150 }
151}
152
153void
155{
157
158 NS_ASSERT(m_state != RX);
159
160 m_state = TX;
161
162 // Notify listeners of the state change
163 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
164 {
165 (*i)->NotifyTxStart(txPowerDbm);
166 }
167}
168
169void
171{
173
175
176 m_state = SLEEP;
177
178 // Notify listeners of the state change
179 for (auto i = m_listeners.begin(); i != m_listeners.end(); i++)
180 {
181 (*i)->NotifySleep();
182 }
183}
184
192
193void
195{
196 m_listeners.push_back(listener);
197}
198
199void
201{
202 auto i = find(m_listeners.begin(), m_listeners.end(), listener);
203 if (i != m_listeners.end())
204 {
205 m_listeners.erase(i);
206 }
207}
208
209} // namespace lorawan
210} // namespace ns3
bool IsNull() const
Check for null implementation.
Definition callback.h:555
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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:65
TxFinishedCallback m_txFinishedCallback
The callback to perform upon the end of a transmission.
Definition lora-phy.h:330
#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_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ SLEEP
The PHY layer is sleeping.