Skip to content

Commit

Permalink
Avoid up / down / up / down TXPower changes on ADR.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocaar committed Jul 4, 2018
1 parent e0c1184 commit b49af13
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/adr/adr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ func HandleADR(ds storage.DeviceSession, linkADRReqBlock *storage.MACCommandBloc

// get the max SNR from the UplinkHistory
var snrM float64 = -999
var historyCount int
for _, uh := range ds.UplinkHistory {
if uh.MaxSNR > snrM && uh.TXPowerIndex == ds.TXPowerIndex {
snrM = uh.MaxSNR
if uh.TXPowerIndex == ds.TXPowerIndex {
historyCount++

if uh.MaxSNR > snrM {
snrM = uh.MaxSNR
}
}
}

Expand All @@ -59,6 +64,13 @@ func HandleADR(ds storage.DeviceSession, linkADRReqBlock *storage.MACCommandBloc
snrMargin := snrM - requiredSNR - config.C.NetworkServer.NetworkSettings.InstallationMargin
nStep := int(snrMargin / 3)

// In case of negative steps the ADR algorithm will increase the TXPower
// if possible. To avoid up / down / up / down TXPower changes, wait until
// we have a full history table before making adjustments.
if nStep < 0 && historyCount != storage.UplinkHistorySize {
return nil, nil
}

maxSupportedDR := getMaxSupportedDRForNode(ds)
maxSupportedTXPowerOffsetIndex := getMaxSupportedTXPowerOffsetIndexForNode(ds)

Expand Down
89 changes: 89 additions & 0 deletions internal/adr/adr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,95 @@ func TestADR(t *testing.T) {
},
ExpectedError: nil,
},
{
Name: "ADR not decreasing tx power (as we don't have a full history table for the currently used TXPower)",
DeviceSession: storage.DeviceSession{
DevAddr: [4]byte{1, 2, 3, 4},
DevEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
EnabledUplinkChannels: []int{0, 1, 2},
DR: 5,
TXPowerIndex: 3,
NbTrans: 1,
UplinkHistory: []storage.UplinkHistory{
{MaxSNR: -20, TXPowerIndex: 0},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
{MaxSNR: -20, TXPowerIndex: 3},
},
ADR: true,
},
ExpectedError: nil,
},
{
Name: "ADR decreasing tx power",
DeviceSession: storage.DeviceSession{
DevAddr: [4]byte{1, 2, 3, 4},
DevEUI: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
EnabledUplinkChannels: []int{0, 1, 2},
DR: 5,
TXPowerIndex: 3,
NbTrans: 1,
UplinkHistory: []storage.UplinkHistory{
{FCnt: 0, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 1, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 2, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 3, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 4, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 5, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 6, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 7, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 8, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 9, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 10, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 11, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 12, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 13, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 14, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 15, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 16, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 17, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 18, MaxSNR: -20, TXPowerIndex: 3},
{FCnt: 19, MaxSNR: -20, TXPowerIndex: 3},
},
ADR: true,
},
Expected: []storage.MACCommandBlock{
{
CID: lorawan.LinkADRReq,
MACCommands: []lorawan.MACCommand{
{
CID: lorawan.LinkADRReq,
Payload: &lorawan.LinkADRReqPayload{
DataRate: 5,
TXPower: 0,
ChMask: lorawan.ChMask{true, true, true},
Redundancy: lorawan.Redundancy{
ChMaskCntl: 0,
NbRep: 1,
},
},
},
},
},
},
ExpectedError: nil,
},
}

for i, tst := range testTable {
Expand Down

0 comments on commit b49af13

Please sign in to comment.