Skip to content

Commit

Permalink
fix max fastpacket sized message assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
aldas committed May 9, 2023
1 parent 247d582 commit de2f4ab
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
17 changes: 10 additions & 7 deletions fastpacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func (m *fastPacketSequence) Append(frame RawFrame) bool {

copy(m.data[:6], frame.Data[2:])
} else { // subsequent frames, have 7 bytes of data, first byte is for sequence counter and frame counter
idx := 6 + int(frameNr-1)*7
copy(m.data[idx:idx+len(frame.Data)], frame.Data[1:])
start := 6 + int(frameNr-1)*7
end := start + len(frame.Data) - 1
copy(m.data[start:end], frame.Data[1:])
}

return m.completeFramesMask == m.receivedFramesMask
Expand Down Expand Up @@ -164,10 +165,10 @@ func (a *FastPacketAssembler) Assemble(frame RawFrame, to *RawMessage) bool {
tmpFp.sequence != sequence {
continue
}
fp = tmpFp
fp = a.inTransfer[i]
idx = i
if tmpFp.lastReceivedFrameTime.Before(threshold) { // sequence is too old to be this frame sequence
tmpFp.Reset()
if fp.lastReceivedFrameTime.Before(threshold) { // sequence is too old to be this frame sequence
fp.Reset()
}
}
if fp == nil {
Expand All @@ -178,12 +179,14 @@ func (a *FastPacketAssembler) Assemble(frame RawFrame, to *RawMessage) bool {
}
isComplete := fp.Append(frame)
if isComplete { // message is now complete
fp.To(to) // copy data over to rawMessage

// remove item from in transfer list and put it back to pool
a.inTransfer[idx] = a.inTransfer[len(a.inTransfer)-1]
a.inTransfer = a.inTransfer[:len(a.inTransfer)-1]
a.pool.Put(fp)

fp.To(to)
} else {
a.inTransfer[idx] = fp
}
return isComplete
}
71 changes: 66 additions & 5 deletions fastpacket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,31 +229,31 @@ func TestFastPacketAssembler_Assemble(t *testing.T) {
Time: now.Add(-4 * 50 * time.Millisecond),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: 8,
Data: [8]byte{0x60, 0x1E, 0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02},
Data: [8]byte{0x60, 0x1E, 0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02}, // +6
},
{
Time: now.Add(-3 * 50 * time.Millisecond),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: 8,
Data: [8]byte{0x61, 0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38},
Data: [8]byte{0x61, 0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38}, // +7 = 13
},
{
Time: now.Add(-2 * 50 * time.Millisecond),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: 8,
Data: [8]byte{0x62, 0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA},
Data: [8]byte{0x62, 0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA}, // +7 = 20
},
{
Time: now.Add(-1 * 50 * time.Millisecond),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: 8,
Data: [8]byte{0x63, 0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02},
Data: [8]byte{0x63, 0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02}, // +7 = 27
},
{
Time: now,
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: 8,
Data: [8]byte{0x64, 0x01, 0x02, 0x01, 0xFF, 0xFF, 0xFF, 0xFF},
Data: [8]byte{0x64, 0x01, 0x02, 0x01, 0xFF, 0xFF, 0xFF, 0xFF}, // +3 = 30
},
},
expectComplete: true,
Expand Down Expand Up @@ -306,3 +306,64 @@ func TestFastPacketAssembler_Assemble(t *testing.T) {
})
}
}

func TestAssembleMaxSizeFastPacket(t *testing.T) {
now := test_test.UTCTime(1665488842)
fpa := NewFastPacketAssembler([]uint32{130323})
fpa.now = func() time.Time {
return now
}
var msg RawMessage

first := RawFrame{
Time: now.Add(-4 * 50 * time.Millisecond),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: 8,
Data: [8]byte{0x60, FastRawPacketMaxSize, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5},
}
assert.False(t, fpa.Assemble(first, &msg))

frameNR := uint8(1)
for i := 7; i < FastRawPacketMaxSize; {
size := 7
isLast := false
if i+size > FastRawPacketMaxSize {
size -= i + size - FastRawPacketMaxSize
isLast = true
}

startIdx := 6 + (frameNR-1)*7
f := RawFrame{
Time: now.Add(time.Duration(frameNR) * 40 * time.Millisecond),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: uint8(size + 1),
Data: [8]byte{
uint8(0b01100000) + frameNR,
0x0 + startIdx,
0x1 + startIdx,
0x2 + startIdx,
0x3 + startIdx,
0x4 + startIdx,
0x5 + startIdx,
0x6 + startIdx,
},
}

result := fpa.Assemble(f, &msg)
assert.Equal(t, isLast, result)

i += size
frameNR++
}

expected := RawMessage{
Time: now.Add(time.Duration(31) * 40 * time.Millisecond),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Data: make(RawData, FastRawPacketMaxSize),
}
for i := 0; i < FastRawPacketMaxSize; i++ {
expected.Data[i] = uint8(i)
}
assert.Equal(t, expected, msg)

}

0 comments on commit de2f4ab

Please sign in to comment.