forked from ungerik/go-mavlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mavlink-helper.go
152 lines (131 loc) · 4.55 KB
/
mavlink-helper.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
package mavlink
import (
"bytes"
"encoding/binary"
)
var charParserFactory = map[parserState]parserFunc{
mavlink_parse_state_idle: parseFramestart,
mavlink_parse_state_got_stx: parsePayloadLength,
mavlink_parse_state_got_length: parsePacketSeq,
mavlink_parse_state_got_seq: parseSysID,
mavlink_parse_state_got_sysid: parseCompID,
mavlink_parse_state_got_compid: parseMsgID,
mavlink_parse_state_got_msgid: parsePayload,
mavlink_parse_state_got_payload: parseCrc1,
mavlink_parse_state_got_crc1: parseCrc2,
}
type parserFunc func(byte, *MavPacketInternal) (parserState, error)
type PacketSequenceFunc func() uint8
/**
* Simply incr the packet sequence
*/
type MavPacketManager struct {
get PacketSequenceFunc
}
func getPacketSeqGenerator() PacketSequenceFunc {
packetSequence := uint8(0)
return func() (ret uint8) {
ret = packetSequence
if packetSequence == 0xff {
packetSequence = 0
}
packetSequence++
return
}
}
/**
* Parser
*/
func parseFramestart(c byte, pInternal *MavPacketInternal) (parserState, error) {
if c == frameStart {
pInternal.rawBuffer.Reset()
pInternal.rawBuffer.WriteByte(c)
pInternal.packet = new(MavPacket)
pInternal.packet.crcInit()
pInternal.packet.Header.FrameStart = frameStart
return mavlink_parse_state_got_stx, nil
}
return mavlink_parse_state_idle, ErrInvalidStartframe(c)
}
func parsePayloadLength(c byte, pInternal *MavPacketInternal) (parserState, error) {
if c >= 0 && c <= mav_max_payload_len {
pInternal.rawBuffer.WriteByte(c)
pInternal.packet.Header.PayloadLength = c
pInternal.packet.crcAccumulate(c)
return mavlink_parse_state_got_length, nil
}
return mavlink_parse_state_idle, ErrInvalidPayloadLength(c)
}
func parsePacketSeq(c byte, pInternal *MavPacketInternal) (parserState, error) {
pInternal.rawBuffer.WriteByte(c)
pInternal.packet.Header.PacketSequence = c
pInternal.packet.crcAccumulate(c)
return mavlink_parse_state_got_seq, nil
}
func parseSysID(c byte, pInternal *MavPacketInternal) (parserState, error) {
pInternal.rawBuffer.WriteByte(c)
pInternal.packet.Header.SystemID = c
pInternal.packet.crcAccumulate(c)
return mavlink_parse_state_got_sysid, nil
}
func parseCompID(c byte, pInternal *MavPacketInternal) (parserState, error) {
pInternal.rawBuffer.WriteByte(c)
pInternal.packet.Header.ComponentID = c
pInternal.packet.crcAccumulate(c)
return mavlink_parse_state_got_compid, nil
}
func parseMsgID(c byte, pInternal *MavPacketInternal) (parserState, error) {
pInternal.rawBuffer.WriteByte(c)
pInternal.packet.Header.MessageID = c
pInternal.packet.crcAccumulate(c)
if pInternal.packet.Header.PayloadLength == 0 {
return mavlink_parse_state_got_payload, nil
}
if msgGenerator, ok := messageFactory[c]; ok {
pInternal.packet.Msg = msgGenerator()
if pInternal.packet.Header.PayloadLength != pInternal.packet.Msg.Size() {
return mavlink_parse_state_idle, ErrInvalidPayloadLength(pInternal.packet.Header.PayloadLength)
} else {
return mavlink_parse_state_got_msgid, nil
}
}
return mavlink_parse_state_idle, ErrUnknownMessageID(c)
}
func parsePayload(c byte, pInternal *MavPacketInternal) (parserState, error) {
pInternal.rawBuffer.WriteByte(c)
pInternal.packet.crcAccumulate(c)
if pInternal.rawBuffer.Len()-int(pInternal.packet.Header.Size()) == int(pInternal.packet.Msg.Size()) {
buf := bytes.NewBuffer(pInternal.rawBuffer.Bytes()[pInternal.packet.Header.Size():])
if err := binary.Read(buf, binary.LittleEndian, pInternal.packet.Msg); err != nil {
return mavlink_parse_state_got_msgid, err
}
return mavlink_parse_state_got_payload, nil
}
return mavlink_parse_state_got_msgid, nil
}
func parseCrc1(c byte, pInternal *MavPacketInternal) (parserState, error) {
pInternal.rawBuffer.WriteByte(c)
if mavlink_crc_extra_enabled {
// This nil pointer indicates a deeper parse error, but I haven't
// been able to track it down yet:
if pInternal.packet.Msg == nil {
return mavlink_parse_state_idle, ErrInvalidChecksum(c)
}
pInternal.packet.crcAccumulate(messageCrcs[pInternal.packet.Msg.ID()])
}
if c == uint8(pInternal.packet.Checksum&0xFF) {
return mavlink_parse_state_got_crc1, nil
} else if c == frameStart {
return parseFramestart(c, pInternal)
}
return mavlink_parse_state_idle, ErrInvalidChecksum(c)
}
func parseCrc2(c byte, pInternal *MavPacketInternal) (parserState, error) {
pInternal.rawBuffer.WriteByte(c)
if c == uint8((pInternal.packet.Checksum >> 8)) {
return mavlink_parse_state_got_packet, nil
} else if c == frameStart {
return parseFramestart(c, pInternal)
}
return mavlink_parse_state_idle, ErrInvalidChecksum(c)
}