28 TypeId(
"ns3::AdrComponent")
31 .SetParent<NetworkControllerComponent>()
32 .AddAttribute(
"MultipleGwCombiningMethod",
33 "Whether to average the received power of gateways or to use the maximum",
42 .AddAttribute(
"MultiplePacketsCombiningMethod",
43 "Whether to average SNRs from multiple packets or to use the maximum",
52 .AddAttribute(
"HistoryRange",
53 "Number of packets to use for averaging",
57 .AddAttribute(
"ChangeTransmissionPower",
58 "Whether to toggle the transmission power or not",
89 Ptr<Packet> myPacket = status->GetLastPacketReceivedFromDevice()->Copy();
93 myPacket->RemoveHeader(mHdr);
94 myPacket->RemoveHeader(fHdr);
99 if (
int(status->GetReceivedPacketList().size()) <
historyRange)
101 NS_LOG_ERROR(
"Not enough packets received by this device ("
102 << status->GetReceivedPacketList().size()
103 <<
") for the algorithm to work (need " <<
historyRange <<
")");
110 uint8_t spreadingFactor = status->GetFirstReceiveWindowSpreadingFactor();
113 uint8_t transmissionPower = status->GetMac()->GetTransmissionPower();
125 newTxPower = transmissionPower;
128 if (newDataRate !=
SfToDr(spreadingFactor) || newTxPower != transmissionPower)
131 int channels[] = {0, 1, 2};
132 std::list<int> enabledChannels(channels, channels +
sizeof(channels) /
sizeof(
int));
137 NS_LOG_DEBUG(
"Sending LinkAdrReq with DR = " << (
unsigned)newDataRate
138 <<
" and TP = " << (
unsigned)newTxPower
141 status->m_reply.frameHeader.AddLinkAdrReq(newDataRate,
145 status->m_reply.frameHeader.SetAsDownlink();
148 status->m_reply.needsReply =
true;
190 uint8_t spreadingFactor = status->GetFirstReceiveWindowSpreadingFactor();
200 double transmissionPower = status->GetMac()->GetTransmissionPower();
202 NS_LOG_DEBUG(
"Transmission Power = " << transmissionPower);
206 double margin_SNR = m_SNR - req_SNR;
212 int steps = std::floor(margin_SNR / 3);
233 transmissionPower -= 2;
239 transmissionPower += 2;
244 *newDataRate =
SfToDr(spreadingFactor);
245 *newTxPower = transmissionPower;
278 return transmissionPower + 174 - 10 * log10(
B) -
NF;
285 auto it = gwList.begin();
286 double min = it->second.rxPower;
288 for (; it != gwList.end(); it++)
290 if (it->second.rxPower < min)
292 min = it->second.rxPower;
303 auto it = gwList.begin();
304 double max = it->second.rxPower;
306 for (; it != gwList.end(); it++)
308 if (it->second.rxPower > max)
310 max = it->second.rxPower;
323 for (
auto it = gwList.begin(); it != gwList.end(); it++)
325 NS_LOG_DEBUG(
"Gateway at " << it->first <<
" has TP " << it->second.rxPower);
326 sum += it->second.rxPower;
329 double average = sum / gwList.size();
359 auto it = packetList.rbegin();
386 auto it = packetList.rbegin();
414 auto it = packetList.rbegin();
439 else if (txPower >= 14)
443 else if (txPower >= 12)
447 else if (txPower >= 10)
451 else if (txPower >= 8)
455 else if (txPower >= 6)
459 else if (txPower >= 4)
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 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)
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.
void AdrImplementation(uint8_t *newDataRate, uint8_t *newTxPower, Ptr< EndDeviceStatus > status)
Implementation of the default Adaptive Data Rate (ADR) procedure.
static TypeId GetTypeId()
Register this type.
const int B
Bandwidth (Hz)
int GetTxPowerIndex(int txPower)
Get the LoRaWAN protocol TXPower configuration index from the Equivalent Isotropically Radiated Power...
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_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)