-
Notifications
You must be signed in to change notification settings - Fork 2
/
packet.go
99 lines (87 loc) · 2.89 KB
/
packet.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
package vlp16
import (
"encoding/binary"
"fmt"
)
const (
// BlocksPerPacket is the number of blocks in a packet.
BlocksPerPacket = 12
// ChannelsPerBlock is the number of channels in a block.
ChannelsPerBlock = 32
)
// Packet is a VLP-16 packet.
type Packet struct {
Blocks [BlocksPerPacket]Block
Timestamp uint32
ReturnMode ReturnMode
ProductID ProductID
}
// Block is a VLP-16 block.
type Block struct {
Flag uint16
Azimuth uint16
Channels [ChannelsPerBlock]Channel
}
// Channel is a VLP-16 channel.
type Channel struct {
Distance uint16
Reflectivity uint8
}
// SizeOfPacket is the size of a single VLP-16 packet in bytes.
const SizeOfPacket = 1206
// structure of packet.
const (
// lengths.
lengthOfDistance = 2
lengthOfReflectivity = 1
lengthOfAzimuth = 2
lengthOfFlag = 2
lengthOfProductID = 1
lengthOfReturnMode = 1
lengthOfTimestamp = 4
lengthOfChannel = lengthOfDistance + lengthOfReflectivity
lengthOfBlock = lengthOfFlag + lengthOfAzimuth + lengthOfChannel*ChannelsPerBlock
// channel indices.
indexOfDistanceInChannel = 0
indexOfReflectivityInChannel = lengthOfDistance
// block indices.
indexOfFlagInBlock = 0
indexOfAzimuthInBlock = lengthOfFlag
indexOfChannelsInBlock = indexOfAzimuthInBlock + lengthOfAzimuth
// packet indices.
indexOfBlocksInPacket = 0
indexOfTimestampInPacket = lengthOfBlock * BlocksPerPacket
indexOfReturnModeInPacket = indexOfTimestampInPacket + lengthOfTimestamp
indexOfProductIDInPacket = indexOfReturnModeInPacket + lengthOfReturnMode
)
// Compile-time assertion on structure of packet.
//nolint:lll
var _ [SizeOfPacket]struct{} = [lengthOfBlock*BlocksPerPacket + lengthOfTimestamp + lengthOfReturnMode + lengthOfProductID]struct{}{}
// flag is the magic value of the flag field.
const flag = 0xeeff
// Validate the packet.
func (p *Packet) Validate() error {
for i := range p.Blocks {
if p.Blocks[i].Flag != flag {
return fmt.Errorf("validate packet: invalid flag %#x in block %v", p.Blocks[i].Flag, i)
}
}
return nil
}
func (p *Packet) UnmarshalRawPacket(raw *RawPacket) {
for iBlock := range p.Blocks {
block := &p.Blocks[iBlock]
baseBlock := indexOfBlocksInPacket + iBlock*lengthOfBlock
block.Flag = binary.LittleEndian.Uint16(raw[baseBlock+indexOfFlagInBlock:])
block.Azimuth = binary.LittleEndian.Uint16(raw[baseBlock+indexOfAzimuthInBlock:])
for iChannel := range p.Blocks[iBlock].Channels {
channel := &block.Channels[iChannel]
baseChannel := baseBlock + indexOfChannelsInBlock + iChannel*lengthOfChannel
channel.Distance = binary.LittleEndian.Uint16(raw[baseChannel+indexOfDistanceInChannel:])
channel.Reflectivity = raw[baseChannel+indexOfReflectivityInChannel]
}
}
p.Timestamp = binary.LittleEndian.Uint32(raw[indexOfTimestampInPacket:])
p.ReturnMode = ReturnMode(raw[indexOfReturnModeInPacket])
p.ProductID = ProductID(raw[indexOfProductIDInPacket])
}