11package adr
22
3- import "github.com/brocaar/chirpstack-network-server/v3/adr"
3+ import (
4+ "github.com/brocaar/chirpstack-network-server/v3/adr"
5+ "github.com/brocaar/chirpstack-network-server/v3/internal/band"
6+ loraband "github.com/brocaar/lorawan/band"
7+ )
48
59// DefaultHandler implements the default ADR handler.
610type DefaultHandler struct {}
@@ -12,7 +16,7 @@ func (h *DefaultHandler) ID() (string, error) {
1216
1317// Name returns the default name.
1418func (h * DefaultHandler ) Name () (string , error ) {
15- return "Default ADR algorithm" , nil
19+ return "Default ADR algorithm (LoRa only) " , nil
1620}
1721
1822// Handle handles the ADR request.
@@ -30,9 +34,31 @@ func (h *DefaultHandler) Handle(req adr.HandleRequest) (adr.HandleResponse, erro
3034 return resp , nil
3135 }
3236
37+ // The max DR might be configured to a non LoRa (125kHz) data-rate.
38+ // As this algorithm works on LoRa (125kHz) data-rates only, we need to
39+ // find the max LoRa (125 kHz) data-rate.
40+ maxDR := req .MaxDR
41+ maxLoRaDR := 0
42+ enabledDRs := band .Band ().GetEnabledUplinkDataRates ()
43+ for _ , i := range enabledDRs {
44+ dr , err := band .Band ().GetDataRate (i )
45+ if err != nil {
46+ return resp , err
47+ }
48+
49+ if dr .Modulation == loraband .LoRaModulation && dr .Bandwidth == 125 {
50+ maxLoRaDR = i
51+ }
52+ }
53+
54+ // Reduce to max LoRa DR.
55+ if maxDR > maxLoRaDR {
56+ maxDR = maxLoRaDR
57+ }
58+
3359 // Lower the DR only if it exceeds the max. allowed DR.
34- if req .DR > req . MaxDR {
35- resp .DR = req . MaxDR
60+ if req .DR > maxDR {
61+ resp .DR = maxDR
3662 }
3763
3864 // Set the new NbTrans.
@@ -50,7 +76,7 @@ func (h *DefaultHandler) Handle(req adr.HandleRequest) (adr.HandleResponse, erro
5076 return resp , nil
5177 }
5278
53- resp .TxPowerIndex , resp .DR = h .getIdealTxPowerIndexAndDR (nStep , resp .TxPowerIndex , resp .DR , req .MaxTxPowerIndex , req . MaxDR )
79+ resp .TxPowerIndex , resp .DR = h .getIdealTxPowerIndexAndDR (nStep , resp .TxPowerIndex , resp .DR , req .MaxTxPowerIndex , maxDR )
5480
5581 return resp , nil
5682}
0 commit comments