Skip to content

Commit

Permalink
ns: Use minimalistic recent downlink representation
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansmares committed Jul 14, 2022
1 parent 9d339b2 commit ceb5541
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 96 deletions.
26 changes: 19 additions & 7 deletions pkg/networkserver/downlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -1065,11 +1065,27 @@ func loggerWithDownlinkSchedulingErrorFields(logger log.Interface, errs downlink
return logger.WithFields(log.Fields(pairs...))
}

func appendRecentDownlink(recent []*ttnpb.DownlinkMessage, down *ttnpb.DownlinkMessage, window int) []*ttnpb.DownlinkMessage {
func toMACStateDownlinkMessages(downs ...*ttnpb.DownlinkMessage) []*ttnpb.MACState_DownlinkMessage {
if len(downs) == 0 {
return nil
}
recentDowns := make([]*ttnpb.MACState_DownlinkMessage, 0, len(downs))
for _, down := range downs {
recentDowns = append(recentDowns, &ttnpb.MACState_DownlinkMessage{
Payload: down.Payload,
CorrelationIds: down.CorrelationIds,
})
}
return recentDowns
}

func appendRecentDownlink(
recent []*ttnpb.MACState_DownlinkMessage, down *ttnpb.DownlinkMessage, window int,
) []*ttnpb.MACState_DownlinkMessage {
if n := len(recent); n > 0 {
recent[n-1].CorrelationIds = nil
}
recent = append(recent, down)
recent = append(recent, toMACStateDownlinkMessages(down)...)
if extra := len(recent) - window; extra > 0 {
recent = recent[extra:]
}
Expand Down Expand Up @@ -1195,11 +1211,7 @@ func recordDataDownlink(dev *ttnpb.EndDevice, genState generateDownlinkState, ne
dev.MacState.PendingApplicationDownlink = genState.ApplicationDownlink
dev.Session.LastConfFCntDown = macPayload.FullFCnt
}
dev.MacState.RecentDownlinks = appendRecentDownlink(dev.MacState.RecentDownlinks, &ttnpb.DownlinkMessage{
Payload: down.Message.Payload,
Settings: down.Message.Settings,
CorrelationIds: down.Message.CorrelationIds,
}, recentDownlinkCount)
dev.MacState.RecentDownlinks = appendRecentDownlink(dev.MacState.RecentDownlinks, down.Message, recentDownlinkCount)
dev.MacState.RxWindowsAvailable = false
}

Expand Down
82 changes: 54 additions & 28 deletions pkg/networkserver/downlink_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,75 @@ import (
)

func TestAppendRecentDownlink(t *testing.T) {
downs := [...]*ttnpb.DownlinkMessage{
downs := [...]*ttnpb.MACState_DownlinkMessage{
{
RawPayload: []byte("test1"),
Payload: &ttnpb.Message{
Mic: []byte{0x01},
},
},
{
RawPayload: []byte("test2"),
Payload: &ttnpb.Message{
Mic: []byte{0x02},
},
},
{
RawPayload: []byte("test3"),
Payload: &ttnpb.Message{
Mic: []byte{0x03},
},
},
}
for _, tc := range []struct {
Recent []*ttnpb.DownlinkMessage
Recent []*ttnpb.MACState_DownlinkMessage
Down *ttnpb.DownlinkMessage
Window int
Expected []*ttnpb.DownlinkMessage
Expected []*ttnpb.MACState_DownlinkMessage
}{
{
Down: downs[0],
Down: &ttnpb.DownlinkMessage{
Payload: &ttnpb.Message{
Mic: []byte{0x01},
},
},
Window: 1,
Expected: downs[:1],
},
{
Recent: downs[:1],
Down: downs[1],
Recent: downs[:1],
Down: &ttnpb.DownlinkMessage{
Payload: &ttnpb.Message{
Mic: []byte{0x02},
},
},
Window: 1,
Expected: downs[1:2],
},
{
Recent: downs[:2],
Down: downs[2],
Recent: downs[:2],
Down: &ttnpb.DownlinkMessage{
Payload: &ttnpb.Message{
Mic: []byte{0x03},
},
},
Window: 1,
Expected: downs[2:3],
},
{
Recent: downs[:1],
Down: downs[1],
Recent: downs[:1],
Down: &ttnpb.DownlinkMessage{
Payload: &ttnpb.Message{
Mic: []byte{0x02},
},
},
Window: 2,
Expected: downs[:2],
},
{
Recent: downs[:2],
Down: downs[2],
Recent: downs[:2],
Down: &ttnpb.DownlinkMessage{
Payload: &ttnpb.Message{
Mic: []byte{0x03},
},
},
Window: 2,
Expected: downs[1:3],
},
Expand All @@ -96,7 +122,7 @@ func TestAppendRecentDownlink(t *testing.T) {
Name: fmt.Sprintf("recent_length:%d,window:%v", len(tc.Recent), tc.Window),
Parallel: true,
Func: func(ctx context.Context, t *testing.T, a *assertions.Assertion) {
recent := CopyDownlinkMessages(tc.Recent...)
recent := deepcopy.Copy(tc.Recent).([]*ttnpb.MACState_DownlinkMessage)
down := CopyDownlinkMessage(tc.Down)
ret := appendRecentDownlink(recent, down, tc.Window)
a.So(recent, should.Resemble, tc.Recent)
Expand Down Expand Up @@ -284,12 +310,12 @@ func TestGenerateDataDownlink(t *testing.T) {
},
},
}},
RecentDownlinks: []*ttnpb.DownlinkMessage{
RecentDownlinks: ToMACStateDownlinkMessages(
MakeDataDownlink(&DataDownlinkConfig{
DecodePayload: true,
MACVersion: ttnpb.MACVersion_MAC_V1_1,
}),
},
),
RxWindowsAvailable: true,
},
Session: &ttnpb.Session{
Expand Down Expand Up @@ -353,12 +379,12 @@ func TestGenerateDataDownlink(t *testing.T) {
},
},
}},
RecentDownlinks: []*ttnpb.DownlinkMessage{
RecentDownlinks: ToMACStateDownlinkMessages(
MakeDataDownlink(&DataDownlinkConfig{
DecodePayload: true,
MACVersion: ttnpb.MACVersion_MAC_V1_1,
}),
},
),
RxWindowsAvailable: true,
},
Session: &ttnpb.Session{
Expand Down Expand Up @@ -902,12 +928,12 @@ func TestGenerateDataDownlink(t *testing.T) {
}},
},
}},
RecentDownlinks: []*ttnpb.DownlinkMessage{
RecentDownlinks: ToMACStateDownlinkMessages(
MakeDataDownlink(&DataDownlinkConfig{
DecodePayload: true,
MACVersion: ttnpb.MACVersion_MAC_V1_1,
}),
},
),
},
Session: &ttnpb.Session{
DevAddr: devAddr.Bytes(),
Expand Down Expand Up @@ -984,12 +1010,12 @@ func TestGenerateDataDownlink(t *testing.T) {
}},
},
}},
RecentDownlinks: []*ttnpb.DownlinkMessage{
RecentDownlinks: ToMACStateDownlinkMessages(
MakeDataDownlink(&DataDownlinkConfig{
DecodePayload: true,
MACVersion: ttnpb.MACVersion_MAC_V1_1,
}),
},
),
},
Session: &ttnpb.Session{
DevAddr: devAddr.Bytes(),
Expand Down Expand Up @@ -1036,12 +1062,12 @@ func TestGenerateDataDownlink(t *testing.T) {
}},
},
}},
RecentDownlinks: []*ttnpb.DownlinkMessage{
RecentDownlinks: ToMACStateDownlinkMessages(
MakeDataDownlink(&DataDownlinkConfig{
DecodePayload: true,
MACVersion: ttnpb.MACVersion_MAC_V1_1,
}),
},
),
},
Session: &ttnpb.Session{
DevAddr: devAddr.Bytes(),
Expand Down Expand Up @@ -1116,12 +1142,12 @@ func TestGenerateDataDownlink(t *testing.T) {
}},
},
}},
RecentDownlinks: []*ttnpb.DownlinkMessage{
RecentDownlinks: ToMACStateDownlinkMessages(
MakeDataDownlink(&DataDownlinkConfig{
DecodePayload: true,
MACVersion: ttnpb.MACVersion_MAC_V1_1,
}),
},
),
},
Session: &ttnpb.Session{
DevAddr: devAddr.Bytes(),
Expand Down
Loading

0 comments on commit ceb5541

Please sign in to comment.