-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanmessages.go
48 lines (46 loc) · 1.7 KB
/
scanmessages.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
package xsens
import (
"bytes"
"encoding/binary"
)
// ScanMessages is a bufio.Scanner SplitFunc that splits a stream into Xsens messages.
func ScanMessages(data []byte, atEOF bool) (advance int, token []byte, err error) {
if len(data) == 0 && atEOF {
return 0, nil, nil
}
// scan to next message header index
indexOfMessage := bytes.Index(data, []byte{valueOfPreamble, valueOfBusIdentifier})
if indexOfMessage == -1 {
if data[len(data)-1] == valueOfPreamble {
// edge case: latest byte is a preamble, so keep it
return len(data) - 1, nil, nil
}
// found no message header, discard all data and read more
return len(data), nil, nil
}
message := data[indexOfMessage:]
if len(message) < indexOfLength+lengthOfLength {
// not enough data to read length: advance to message start and read more data
return indexOfMessage, nil, nil
}
indexOfData := indexOfData
lengthOfData := int(message[indexOfLength])
if lengthOfData == valueOfLengthExtended {
if len(message) < indexOfExtendedLength+lengthOfExtendedLength {
// not enough data to read extended length: advance to message start and read more data
return indexOfMessage, nil, nil
}
indexOfData = indexOfExtendedData
lengthOfData = int(binary.BigEndian.Uint16(
message[indexOfExtendedLength : indexOfExtendedLength+lengthOfExtendedLength],
))
}
lengthOfMessage := indexOfData + lengthOfData + lengthOfChecksum
if len(message) < lengthOfMessage {
// not enough data to read data and checksum, advance to message start and read more data
return indexOfMessage, nil, nil
}
// message now contains the exact bytes of the next scanned message
message = message[:lengthOfMessage]
return indexOfMessage + lengthOfMessage, message, nil
}