30 TypeId(
"ns3::AdrComponent")
33 .SetParent<NetworkControllerComponent>()
34 .AddAttribute(
"MultipleGwCombiningMethod",
35 "Whether to average the received power of gateways or to use the maximum",
44 .AddAttribute(
"MultiplePacketsCombiningMethod",
45 "Whether to average SNRs from multiple packets or to use the maximum",
54 .AddAttribute(
"HistoryRange",
55 "Number of packets to use for averaging",
59 .AddAttribute(
"ChangeTransmissionPower",
60 "Whether to toggle the transmission power or not",
91 Ptr<Packet> myPacket = status->GetLastPacketReceivedFromDevice()->Copy();
95 myPacket->RemoveHeader(mHdr);
96 myPacket->RemoveHeader(fHdr);
101 if (
int(status->GetReceivedPacketList().size()) <
historyRange)
103 NS_LOG_ERROR(
"Not enough packets received by this device ("
104 << status->GetReceivedPacketList().size()
105 <<
") for the algorithm to work (need " <<
historyRange <<
")");
112 uint8_t spreadingFactor = status->GetFirstReceiveWindowSpreadingFactor();
115 double transmissionPowerDbm = status->GetMac()->GetTransmissionPowerDbm();
119 double newTxPowerDbm;
127 newTxPowerDbm = transmissionPowerDbm;
130 if (newDataRate !=
SfToDr(spreadingFactor) || newTxPowerDbm != transmissionPowerDbm)
133 int channels[] = {0, 1, 2};
134 std::list<int> enabledChannels(channels, channels +
sizeof(channels) /
sizeof(
int));
140 << (
unsigned)newDataRate <<
" and TP = " << newTxPowerDbm <<
"dBm");
142 status->m_reply.frameHeader.AddLinkAdrReq(newDataRate,
146 status->m_reply.frameHeader.SetAsDownlink();
149 status->m_reply.needsReply =
true;
191 uint8_t spreadingFactor = status->GetFirstReceiveWindowSpreadingFactor();
201 double transmissionPower = status->GetMac()->GetTransmissionPowerDbm();
203 NS_LOG_DEBUG(
"Transmission Power = " << transmissionPower);
207 double margin_SNR = m_SNR - req_SNR;
213 int steps = std::floor(margin_SNR / 3);
234 transmissionPower -= 2;
240 transmissionPower += 2;
245 *newDataRate =
SfToDr(spreadingFactor);
246 *newTxPower = transmissionPower;
279 return transmissionPower + 174 - 10 * log10(
B) -
NF;
286 auto it = gwList.begin();
287 double min = it->second.rxPower;
289 for (; it != gwList.end(); it++)
291 if (it->second.rxPower < min)
293 min = it->second.rxPower;
304 auto it = gwList.begin();
305 double max = it->second.rxPower;
307 for (; it != gwList.end(); it++)
309 if (it->second.rxPower > max)
311 max = it->second.rxPower;
324 for (
auto it = gwList.begin(); it != gwList.end(); it++)
326 NS_LOG_DEBUG(
"Gateway at " << it->first <<
" has TP " << it->second.rxPower);
327 sum += it->second.rxPower;
330 double average = sum / gwList.size();
360 auto it = packetList.rbegin();
387 auto it = packetList.rbegin();
415 auto it = packetList.rbegin();
436 NS_ASSERT_MSG(txPower >= 0 && txPower <= 14,
"TxPower dBm value out of supported range");
437 NS_ASSERT_MSG(fmod(txPower, 2) == 0,
"Invalid TxPower value");
438 return 7 - txPower / 2;
Hold variables of type enum.
Hold a signed integer type.
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
TypeId SetGroupName(std::string groupName)
Set the group name.
LinkAdrRequest commands management.
double GetAverageSNR(EndDeviceStatus::ReceivedPacketList packetList, int historyRange)
Get the average Signal to Noise Ratio (SNR) of the received packet history.
enum CombiningMethod historyAveraging
Received SNR history policy.
double GetMinSNR(EndDeviceStatus::ReceivedPacketList packetList, int historyRange)
Get the min Signal to Noise Ratio (SNR) of the receive packet history.
void OnReceivedPacket(Ptr< const Packet > packet, Ptr< EndDeviceStatus > status, Ptr< NetworkStatus > networkStatus) override
Function called as a new uplink packet is received by the NetworkServer application.
double threshold[6]
Vector containing the required SNR for the 6 allowed spreading factor levels ranging from 7 to 12 (th...
void OnFailedReply(Ptr< EndDeviceStatus > status, Ptr< NetworkStatus > networkStatus) override
Method that is called when a packet cannot be sent in the downlink.
AdrComponent()
Default constructor.
void AdrImplementation(uint8_t *newDataRate, double *newTxPower, Ptr< EndDeviceStatus > status)
Implementation of the default Adaptive Data Rate (ADR) procedure.
void BeforeSendingReply(Ptr< EndDeviceStatus > status, Ptr< NetworkStatus > networkStatus) override
Function called as a downlink reply is about to leave the NetworkServer application.
const int NF
Noise Figure (dB).
uint8_t GetTxPowerIndex(double txPower)
Get the LoRaWAN protocol TxPower parameter from the Equivalent Radiated Power (ERP) in dBm.
double GetMinTxFromGateways(EndDeviceStatus::GatewayList gwList)
Get the min RSSI (dBm) among gateways receiving the same transmission.
int historyRange
Number of previous packets to consider.
const int min_spreadingFactor
Spreading factor lower limit.
double GetMaxTxFromGateways(EndDeviceStatus::GatewayList gwList)
Get the max RSSI (dBm) among gateways receiving the same transmission.
enum CombiningMethod tpAveraging
TX power from gateways policy.
bool m_toggleTxPower
Whether to control transmission power of end devices or not.
double GetReceivedPower(EndDeviceStatus::GatewayList gwList)
Get RSSI metric for a transmission according to chosen gateway aggregation policy.
~AdrComponent() override
Destructor.
static TypeId GetTypeId()
Register this type.
const int B
Bandwidth (Hz).
double GetMaxSNR(EndDeviceStatus::ReceivedPacketList packetList, int historyRange)
Get the max Signal to Noise Ratio (SNR) of the receive packet history.
double RxPowerToSNR(double transmissionPower) const
Convert reception power values [dBm] to Signal to Noise Ratio (SNR) values [dB].
double GetAverageTxFromGateways(EndDeviceStatus::GatewayList gwList)
Get the average RSSI (dBm) of gateways receiving the same transmission.
const int min_transmissionPower
Minimum transmission power (dBm) (Europe).
uint8_t SfToDr(uint8_t sf)
Convert spreading factor values [7:12] to respective data rate values [0:5].
const int max_transmissionPower
Maximum transmission power (dBm) (Europe).
std::list< std::pair< Ptr< const Packet >, ReceivedPacketInfo > > ReceivedPacketList
typedef of a list of packets paired to their reception info.
std::map< Address, PacketInfoPerGw > GatewayList
typedef of a list of gateways with relative reception information.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Ptr< const AttributeChecker > MakeIntegerChecker()
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)