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 double transmissionPowerDbm = status->GetMac()->GetTransmissionPowerDbm();
117 double newTxPowerDbm;
125 newTxPowerDbm = transmissionPowerDbm;
128 if (newDataRate !=
SfToDr(spreadingFactor) || newTxPowerDbm != transmissionPowerDbm)
131 int channels[] = {0, 1, 2};
132 std::list<int> enabledChannels(channels, channels +
sizeof(channels) /
sizeof(
int));
138 << (
unsigned)newDataRate <<
" and TP = " << newTxPowerDbm <<
"dBm");
140 status->m_reply.frameHeader.AddLinkAdrReq(newDataRate,
144 status->m_reply.frameHeader.SetAsDownlink();
147 status->m_reply.needsReply =
true;
189 uint8_t spreadingFactor = status->GetFirstReceiveWindowSpreadingFactor();
199 double transmissionPower = status->GetMac()->GetTransmissionPowerDbm();
201 NS_LOG_DEBUG(
"Transmission Power = " << transmissionPower);
205 double margin_SNR = m_SNR - req_SNR;
211 int steps = std::floor(margin_SNR / 3);
232 transmissionPower -= 2;
238 transmissionPower += 2;
243 *newDataRate =
SfToDr(spreadingFactor);
244 *newTxPower = transmissionPower;
277 return transmissionPower + 174 - 10 * log10(
B) -
NF;
284 auto it = gwList.begin();
285 double min = it->second.rxPower;
287 for (; it != gwList.end(); it++)
289 if (it->second.rxPower < min)
291 min = it->second.rxPower;
302 auto it = gwList.begin();
303 double max = it->second.rxPower;
305 for (; it != gwList.end(); it++)
307 if (it->second.rxPower > max)
309 max = it->second.rxPower;
322 for (
auto it = gwList.begin(); it != gwList.end(); it++)
324 NS_LOG_DEBUG(
"Gateway at " << it->first <<
" has TP " << it->second.rxPower);
325 sum += it->second.rxPower;
328 double average = sum / gwList.size();
358 auto it = packetList.rbegin();
385 auto it = packetList.rbegin();
413 auto it = packetList.rbegin();
434 NS_ASSERT_MSG(txPower >= 0 && txPower <= 14,
"TxPower dBm value out of supported range");
435 NS_ASSERT_MSG(fmod(txPower, 2) == 0,
"Invalid TxPower value");
436 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)