This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
459 lines (397 loc) · 11.8 KB
/
parser.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package gomavlib
import (
"bufio"
"crypto/sha256"
"encoding/binary"
"fmt"
"io"
"time"
)
// 1st January 2015 GMT
var signatureReferenceDate = time.Date(2015, 01, 01, 0, 0, 0, 0, time.UTC)
// ParserError is the error returned in case of non-fatal parsing errors.
type ParserError struct {
str string
}
func (e *ParserError) Error() string {
return e.str
}
func newParserError(format string, args ...interface{}) *ParserError {
return &ParserError{
str: fmt.Sprintf(format, args...),
}
}
// ParserConf configures a Parser.
type ParserConf struct {
// the reader from which frames will be read.
Reader io.Reader
// the writer to which frames will be written.
Writer io.Writer
// (optional) the messages which will be automatically decoded and
// encoded. If not provided, messages are decoded in the MessageRaw struct.
Dialect *Dialect
// (optional) the secret key used to validate incoming frames.
// Non-signed frames are discarded. This feature requires Mavlink v2.
InKey *Key
// the system id, added to every outgoing frame and used to identify this
// node in the network.
OutSystemId byte
// (optional) the component id, added to every outgoing frame, defaults to 1.
OutComponentId byte
// (optional) the value to insert into the signature link id
OutSignatureLinkId byte
// (optional) the secret key used to sign outgoing frames.
// This feature requires Mavlink v2.
OutKey *Key
}
// Parser is a low-level Mavlink encoder and decoder that works with a Reader and a Writer.
type Parser struct {
conf ParserConf
readBuffer *bufio.Reader
writeBuffer []byte
curWriteSequenceId byte
curReadSignatureTime uint64
}
// NewParser allocates a Parser, a low level frame encoder and decoder.
// See ParserConf for the options.
func NewParser(conf ParserConf) (*Parser, error) {
if conf.Reader == nil {
return nil, fmt.Errorf("reader not provided")
}
if conf.Writer == nil {
return nil, fmt.Errorf("writer not provided")
}
if conf.OutSystemId < 1 {
return nil, fmt.Errorf("SystemId must be >= 1")
}
if conf.OutComponentId < 1 {
conf.OutComponentId = 1
}
p := &Parser{
conf: conf,
readBuffer: bufio.NewReaderSize(conf.Reader, _NET_BUFFER_SIZE),
writeBuffer: make([]byte, 0, _NET_BUFFER_SIZE),
}
return p, nil
}
// Checksum computes the checksum of a given frame.
func (p *Parser) Checksum(f Frame) uint16 {
msg := f.GetMessage().(*MessageRaw)
h := NewX25()
// the checksum covers the whole message, excluding magic byte, checksum and signature
switch ff := f.(type) {
case *FrameV1:
h.Write([]byte{byte(len(msg.Content))})
h.Write([]byte{ff.SequenceId})
h.Write([]byte{ff.SystemId})
h.Write([]byte{ff.ComponentId})
h.Write([]byte{byte(msg.Id)})
h.Write(msg.Content)
case *FrameV2:
buf := make([]byte, 3)
h.Write([]byte{byte(len(msg.Content))})
h.Write([]byte{ff.IncompatibilityFlag})
h.Write([]byte{ff.CompatibilityFlag})
h.Write([]byte{ff.SequenceId})
h.Write([]byte{ff.SystemId})
h.Write([]byte{ff.ComponentId})
h.Write(uint24Encode(buf, msg.Id))
h.Write(msg.Content)
}
// CRC_EXTRA byte is added at the end of the data
h.Write([]byte{p.conf.Dialect.Messages[msg.GetId()].crcExtra})
return h.Sum16()
}
// Signature computes the signature of a given frame with the given key.
func (p *Parser) Signature(ff *FrameV2, key *Key) *Signature {
msg := ff.GetMessage().(*MessageRaw)
h := sha256.New()
// secret key
h.Write(key[:])
// the signature covers the whole message, excluding the signature itself
buf := make([]byte, 6)
h.Write([]byte{v2MagicByte})
h.Write([]byte{byte(len(msg.Content))})
h.Write([]byte{ff.IncompatibilityFlag})
h.Write([]byte{ff.CompatibilityFlag})
h.Write([]byte{ff.SequenceId})
h.Write([]byte{ff.SystemId})
h.Write([]byte{ff.ComponentId})
h.Write(uint24Encode(buf, ff.Message.GetId()))
h.Write(msg.Content)
binary.LittleEndian.PutUint16(buf, ff.Checksum)
h.Write(buf[:2])
h.Write([]byte{ff.SignatureLinkId})
h.Write(uint48Encode(buf, ff.SignatureTimestamp))
sig := new(Signature)
copy(sig[:], h.Sum(nil)[:6])
return sig
}
// Read returns the first Frame parsed from the reader. It must not be called
// by multiple routines in parallel.
func (p *Parser) Read() (Frame, error) {
magicByte, err := p.readBuffer.ReadByte()
if err != nil {
return nil, err
}
var f Frame
switch magicByte {
case v1MagicByte:
ff := &FrameV1{}
f = ff
// header
buf, err := p.readBuffer.Peek(5)
if err != nil {
return nil, err
}
p.readBuffer.Discard(5)
msgLen := buf[0]
ff.SequenceId = buf[1]
ff.SystemId = buf[2]
ff.ComponentId = buf[3]
msgId := buf[4]
// message
var msgContent []byte
if msgLen > 0 {
msgContent = make([]byte, msgLen)
_, err = io.ReadFull(p.readBuffer, msgContent)
if err != nil {
return nil, err
}
}
ff.Message = &MessageRaw{
Id: uint32(msgId),
Content: msgContent,
}
// checksum
buf, err = p.readBuffer.Peek(2)
if err != nil {
return nil, err
}
p.readBuffer.Discard(2)
ff.Checksum = binary.LittleEndian.Uint16(buf)
case v2MagicByte:
ff := &FrameV2{}
f = ff
// header
buf, err := p.readBuffer.Peek(9)
if err != nil {
return nil, err
}
p.readBuffer.Discard(9)
msgLen := buf[0]
ff.IncompatibilityFlag = buf[1]
ff.CompatibilityFlag = buf[2]
ff.SequenceId = buf[3]
ff.SystemId = buf[4]
ff.ComponentId = buf[5]
msgId := uint24Decode(buf[6:])
// discard frame if incompatibility flag is not understood, as in recommendations
if ff.IncompatibilityFlag != 0 && ff.IncompatibilityFlag != flagSigned {
return nil, newParserError("unknown incompatibility flag (%d)", ff.IncompatibilityFlag)
}
// message
var msgContent []byte
if msgLen > 0 {
msgContent = make([]byte, msgLen)
_, err = io.ReadFull(p.readBuffer, msgContent)
if err != nil {
return nil, err
}
}
ff.Message = &MessageRaw{
Id: msgId,
Content: msgContent,
}
// checksum
buf, err = p.readBuffer.Peek(2)
if err != nil {
return nil, err
}
p.readBuffer.Discard(2)
ff.Checksum = binary.LittleEndian.Uint16(buf)
// signature
if ff.IsSigned() {
buf, err := p.readBuffer.Peek(13)
if err != nil {
return nil, err
}
p.readBuffer.Discard(13)
ff.SignatureLinkId = buf[0]
ff.SignatureTimestamp = uint48Decode(buf[1:])
ff.Signature = new(Signature)
copy(ff.Signature[:], buf[7:])
}
default:
return nil, newParserError("unrecognized magic byte: %x", magicByte)
}
if p.conf.InKey != nil {
ff, ok := f.(*FrameV2)
if ok == false {
return nil, newParserError("signature required but packet is not v2")
}
if sig := p.Signature(ff, p.conf.InKey); *sig != *ff.Signature {
return nil, newParserError("wrong signature")
}
// in UDP, packet order is not guaranteed. Therefore, we accept frames
// with a timestamp within 10 seconds with respect to the previous frame.
if p.curReadSignatureTime > 0 &&
ff.SignatureTimestamp < (p.curReadSignatureTime-(10*100000)) {
return nil, newParserError("signature timestamp is too old")
}
if ff.SignatureTimestamp > p.curReadSignatureTime {
p.curReadSignatureTime = ff.SignatureTimestamp
}
}
// decode message if in dialect and validate checksum
if p.conf.Dialect != nil {
if mp, ok := p.conf.Dialect.Messages[f.GetMessage().GetId()]; ok {
if sum := p.Checksum(f); sum != f.GetChecksum() {
return nil, newParserError("wrong checksum (expected %.4x, got %.4x, id=%d)",
sum, f.GetChecksum(), f.GetMessage().GetId())
}
_, isFrameV2 := f.(*FrameV2)
msg, err := mp.decode(f.GetMessage().(*MessageRaw).Content, isFrameV2)
if err != nil {
return nil, newParserError(err.Error())
}
switch ff := f.(type) {
case *FrameV1:
ff.Message = msg
case *FrameV2:
ff.Message = msg
}
}
}
return f, nil
}
// Write writes a Frame into the writer. It must not be called by multiple
// routines in parallel. If route is false, the following fields will be filled
// by the library:
// IncompatibilityFlag
// SequenceId
// SystemId
// ComponentId
// Checksum
// SignatureLinkId
// SignatureTimestamp
// Signature
// if route is true, the frame will be written untouched.
func (p *Parser) Write(f Frame, route bool) error {
if f.GetMessage() == nil {
return fmt.Errorf("message is nil")
}
if _, ok := f.GetMessage().(*MessageRaw); ok && route == false {
return fmt.Errorf("raw messages can only be routed, since we cannot always compute their checksum")
}
// do not touch the original frame, but work with a separate object
// in such way that the frame can be encoded in parallel by other parsers
safeFrame := f.Clone()
if route == false {
switch ff := safeFrame.(type) {
case *FrameV1:
ff.SequenceId = p.curWriteSequenceId
ff.SystemId = p.conf.OutSystemId
ff.ComponentId = p.conf.OutComponentId
case *FrameV2:
ff.SequenceId = p.curWriteSequenceId
ff.SystemId = p.conf.OutSystemId
ff.ComponentId = p.conf.OutComponentId
}
p.curWriteSequenceId++
}
// message must be encoded
if _, ok := safeFrame.GetMessage().(*MessageRaw); !ok {
if p.conf.Dialect == nil {
return fmt.Errorf("message cannot be encoded since dialect is nil")
}
mp, ok := p.conf.Dialect.Messages[safeFrame.GetMessage().GetId()]
if ok == false {
return fmt.Errorf("message cannot be encoded since it is not in the dialect")
}
_, isFrameV2 := safeFrame.(*FrameV2)
byt, err := mp.encode(safeFrame.GetMessage(), isFrameV2)
if err != nil {
return err
}
msgRaw := &MessageRaw{safeFrame.GetMessage().GetId(), byt}
switch ff := safeFrame.(type) {
case *FrameV1:
ff.Message = msgRaw
case *FrameV2:
ff.Message = msgRaw
}
if route == false {
switch ff := safeFrame.(type) {
case *FrameV1:
ff.Checksum = p.Checksum(ff)
case *FrameV2:
// set incompatibility flag before computing checksum
if p.conf.OutKey != nil {
ff.IncompatibilityFlag |= flagSigned
}
ff.Checksum = p.Checksum(ff)
}
}
}
msgContent := safeFrame.GetMessage().(*MessageRaw).Content
msgLen := len(msgContent)
switch ff := safeFrame.(type) {
case *FrameV1:
if ff.Message.GetId() > 0xFF {
return fmt.Errorf("cannot send a message with an id > 0xFF and a V1 frame")
}
bufferLen := 6 + msgLen + 2
p.writeBuffer = p.writeBuffer[:bufferLen]
// header
p.writeBuffer[0] = v1MagicByte
p.writeBuffer[1] = byte(msgLen)
p.writeBuffer[2] = ff.SequenceId
p.writeBuffer[3] = ff.SystemId
p.writeBuffer[4] = ff.ComponentId
p.writeBuffer[5] = byte(ff.Message.GetId())
// message
if msgLen > 0 {
copy(p.writeBuffer[6:], msgContent)
}
// checksum
binary.LittleEndian.PutUint16(p.writeBuffer[6+msgLen:], ff.Checksum)
case *FrameV2:
if route == false && p.conf.OutKey != nil {
ff.SignatureLinkId = p.conf.OutSignatureLinkId
// Timestamp in 10 microsecond units since 1st January 2015 GMT time
ff.SignatureTimestamp = uint64(time.Since(signatureReferenceDate)) / 10000
ff.Signature = p.Signature(ff, p.conf.OutKey)
}
bufferLen := 10 + msgLen + 2
if ff.IsSigned() {
bufferLen += 13
}
p.writeBuffer = p.writeBuffer[:bufferLen]
// header
p.writeBuffer[0] = v2MagicByte
p.writeBuffer[1] = byte(msgLen)
p.writeBuffer[2] = ff.IncompatibilityFlag
p.writeBuffer[3] = ff.CompatibilityFlag
p.writeBuffer[4] = ff.SequenceId
p.writeBuffer[5] = ff.SystemId
p.writeBuffer[6] = ff.ComponentId
uint24Encode(p.writeBuffer[7:], ff.Message.GetId())
// message
if msgLen > 0 {
copy(p.writeBuffer[10:], msgContent)
}
// checksum
binary.LittleEndian.PutUint16(p.writeBuffer[10+msgLen:], ff.Checksum)
// signature
if ff.IsSigned() {
p.writeBuffer[12+msgLen] = ff.SignatureLinkId
uint48Encode(p.writeBuffer[13+msgLen:], ff.SignatureTimestamp)
copy(p.writeBuffer[19+msgLen:], ff.Signature[:])
}
}
// do not check n, since io.Writer is not allowed to return n < len(buf)
// without throwing an error
_, err := p.conf.Writer.Write(p.writeBuffer)
return err
}