-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastpacket_test.go
369 lines (343 loc) · 11.7 KB
/
fastpacket_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package nmea
import (
test_test "github.com/aldas/go-nmea-client/test"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
// Example fast-packet
// PGN: 1FD13 - Meteorological Station Data
// candump output:
//
// Canboat analyzer output:
//
// {
// "timestamp": "2023-03-17T00:05:10.046",
// "prio": 6,
// "src": 35,
// "dst": 255,
// "pgn": 130323,
// "description": "Meteorological Station Data",
// "fields": {
// "Mode": 0,
// "Measurement Date": "2022.09.13",
// "Measurement Time": "08:23:36.5000",
// "Station Latitude": 58.2156683,
// "Station Longitude": 22.39509,
// "Wind Speed": 1.64,
// "Wind Direction": 293.3,
// "Wind Reference": "Apparent",
// "Atmospheric Pressure": 1.008,
// "Ambient Temperature": 12.5
// }
// }
//
//00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
//00:05:10.038 R 19FD1323 61 12 8B 01 B3 22 34 38
//00:05:10.041 R 19FD1323 62 59 0D A4 00 F5 C7 FA
//00:05:10.041 R 19FD1323 63 FF FF F0 03 95 6F 02
//00:05:10.046 R 19FD1323 64 01 02 01 FF FF FF FF
func exampleFPS() fastPacketSequence {
return fastPacketSequence{
header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
lastReceivedFrameTime: test_test.UTCTime(1665488842),
receivedFramesCount: 5,
sequence: 6,
length: 30, // 0x1E, 5 frames, 6,7,7,7,3
completeFramesMask: 0b11111,
receivedFramesMask: 0b11111,
data: [223]byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02, // 00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38, //00:05:10.038 R 19FD1323 61 12 8B 01 B3 22 34 38
0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA, //00:05:10.041 R 19FD1323 62 59 0D A4 00 F5 C7 FA
0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02, //00:05:10.041 R 19FD1323 63 FF FF F0 03 95 6F 02
0x01, 0x02, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, //00:05:10.046 R 19FD1323 64 01 02 01 FF FF FF FF
},
}
}
func TestFastPacketMessage_Append(t *testing.T) {
now := test_test.UTCTime(1665488842) // Tue Oct 11 2022 11:47:22 GMT+0000
var testCases = []struct {
name string
given fastPacketSequence
when RawFrame
expect fastPacketSequence
expectDone bool
}{
{
name: "ok, append second frame, in order",
given: fastPacketSequence{
header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
lastReceivedFrameTime: now.Add(-50 * time.Millisecond),
receivedFramesCount: 1,
sequence: 6,
length: 30, // 0x1E, 5 frames, 6,7,7,7,3
completeFramesMask: 0b11111,
receivedFramesMask: 0b1,
data: [223]byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02, // 00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
},
},
when: RawFrame{
Time: now,
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Length: 8,
Data: [8]byte{0x61, 0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38},
},
expectDone: false,
expect: fastPacketSequence{
header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
lastReceivedFrameTime: now,
receivedFramesCount: 2,
completeFramesMask: 0b11111,
sequence: 6,
length: 30, // 0x1E, 5 frames, 6,7,7,7,3
receivedFramesMask: 0b11,
data: [223]byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02, //00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38, //00:05:10.038 R 19FD1323 61 12 8B 01 B3 22 34 38
},
},
},
{
name: "ok, append last frame, in order",
given: fastPacketSequence{
header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
lastReceivedFrameTime: now.Add(-50 * time.Millisecond),
receivedFramesCount: 4,
sequence: 6,
length: 30, // 0x1E, 5 frames, 6,7,7,7,3
completeFramesMask: 0b11111,
receivedFramesMask: 0b1111,
data: [223]byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02, // 00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38, //00:05:10.038 R 19FD1323 61 12 8B 01 B3 22 34 38
0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA, //00:05:10.041 R 19FD1323 62 59 0D A4 00 F5 C7 FA
0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02, //00:05:10.041 R 19FD1323 63 FF FF F0 03 95 6F 02
},
},
when: RawFrame{
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}, //00:05:10.046 R 19FD1323 64 01 02 01 FF FF FF FF
},
expectDone: true,
expect: fastPacketSequence{
header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
lastReceivedFrameTime: now,
receivedFramesCount: 5,
sequence: 6,
length: 30, // 0x1E, 5 frames, 6,7,7,7,3
completeFramesMask: 0b11111,
receivedFramesMask: 0b11111,
data: [223]byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02, // 00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38, //00:05:10.038 R 19FD1323 61 12 8B 01 B3 22 34 38
0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA, //00:05:10.041 R 19FD1323 62 59 0D A4 00 F5 C7 FA
0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02, //00:05:10.041 R 19FD1323 63 FF FF F0 03 95 6F 02
0x01, 0x02, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, //00:05:10.046 R 19FD1323 64 01 02 01 FF FF FF FF
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fpm := tc.given
done := fpm.Append(tc.when)
assert.Equal(t, tc.expectDone, done)
assert.Equal(t, tc.expect, fpm)
})
}
}
func TestFastPacketSequence_To(t *testing.T) {
fp := exampleFPS()
msg := RawMessage{} // allocates data
fp.To(&msg)
expected := RawMessage{
Time: test_test.UTCTime(1665488842),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Data: []byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02,
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38,
0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA,
0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02,
0x01, 0x02, 0x01,
},
}
assert.Equal(t, expected, msg)
}
func TestFastPacketSequence_As(t *testing.T) {
fp := exampleFPS()
msg := fp.As()
expected := RawMessage{
Time: test_test.UTCTime(1665488842),
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Data: []byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02,
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38,
0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA,
0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02,
0x01, 0x02, 0x01,
},
}
assert.Equal(t, expected, msg)
}
func TestFastPacketSequence_Reset(t *testing.T) {
fp := exampleFPS()
fp.Reset()
expected := fastPacketSequence{
data: [223]byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02, // 00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38, //00:05:10.038 R 19FD1323 61 12 8B 01 B3 22 34 38
0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA, //00:05:10.041 R 19FD1323 62 59 0D A4 00 F5 C7 FA
0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02, //00:05:10.041 R 19FD1323 63 FF FF F0 03 95 6F 02
0x01, 0x02, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, //00:05:10.046 R 19FD1323 64 01 02 01 FF FF FF FF
},
}
assert.Equal(t, expected, fp)
}
func TestFastPacketAssembler_Assemble(t *testing.T) {
now := test_test.UTCTime(1665488842)
var testCases = []struct {
name string
whenFrames []RawFrame
expectComplete bool
expectMessage RawMessage
}{
{
name: "ok, 130323 fast-packet",
whenFrames: []RawFrame{
{
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}, // +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}, // +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}, // +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}, // +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}, // +3 = 30
},
},
expectComplete: true,
expectMessage: RawMessage{
Time: now,
Header: CanBusHeader{PGN: 130323, Priority: 6, Source: 35, Destination: 255},
Data: []byte{
0xF0, 0x30, 0x4B, 0x08, 0xAC, 0x02, // 00:05:10.032 R 19FD1323 60 1E F0 30 4B 08 AC 02
0x12, 0x8B, 0x01, 0xB3, 0x22, 0x34, 0x38, //00:05:10.038 R 19FD1323 61 12 8B 01 B3 22 34 38
0x59, 0x0D, 0xA4, 0x00, 0xF5, 0xC7, 0xFA, //00:05:10.041 R 19FD1323 62 59 0D A4 00 F5 C7 FA
0xFF, 0xFF, 0xF0, 0x03, 0x95, 0x6F, 0x02, //00:05:10.041 R 19FD1323 63 FF FF F0 03 95 6F 02
0x01, 0x02, 0x01, //00:05:10.046 R 19FD1323 64 01 02 01 FF FF FF FF
},
},
},
{
name: "ok, single packet",
whenFrames: []RawFrame{
{
Time: now,
Header: CanBusHeader{PGN: uint32(PGNISORequest), Priority: 6, Source: AddressNull, Destination: 32},
Length: 3,
Data: [8]byte{0x0, 0xEE, 0x0},
},
},
expectComplete: true,
expectMessage: RawMessage{
Time: now,
Header: CanBusHeader{PGN: uint32(PGNISORequest), Priority: 6, Source: AddressNull, Destination: 32},
Data: []byte{
0x0, 0xEE, 0x0,
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fpa := NewFastPacketAssembler([]uint32{126983, 61184, 130323})
fpa.now = func() time.Time {
return now
}
complete := false
var msg RawMessage
for _, f := range tc.whenFrames {
complete = fpa.Assemble(f, &msg)
}
assert.Equal(t, tc.expectComplete, complete)
assert.Equal(t, tc.expectMessage, msg)
})
}
}
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)
}