This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
forked from queue-b/go-mavlink2
/
message.go
101 lines (80 loc) · 2.79 KB
/
message.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
package mavlink2
/*
Generated using mavgen - https://github.com/ArduPilot/pymavlink/
Copyright 2020 queue-b <https://github.com/queue-b>
Permission is hereby granted, free of charge, to any person obtaining a copy
of the generated software (the "Generated Software"), to deal
in the Generated Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Generated Software, and to permit persons to whom the Generated
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Generated Software.
THE GENERATED SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE GENERATED SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE GENERATED SOFTWARE.
*/
import (
"bytes"
"fmt"
"text/tabwriter"
)
// MessageMeta stores metadata information about a MAVLink message
type MessageMeta struct {
CRCExtra byte
MinimumLength uint8
MaximumLength uint8
}
// Message defines a common interface for MAVLink messages
type Message interface {
GetVersion() int
GetDialect() string
GetMessageName() string
GetID() uint32
Read(Frame) error
Write(int) ([]byte, error)
}
// UnknownMessage represents a message that is not part of any loaded Dialect
type UnknownMessage struct {
ID uint32
Version int
Raw []byte
}
func (m *UnknownMessage) String() string {
var buffer bytes.Buffer
writer := tabwriter.NewWriter(&buffer, 0, 0, 2, ' ', 0)
fmt.Fprintf(writer, "ID:\t%v\nVersion:\t%v\nRaw:\t%x\n", m.ID, m.Version, m.Raw)
writer.Flush()
return string(buffer.Bytes())
}
// GetVersion gets the MAVLink version of the Message contents
func (m *UnknownMessage) GetVersion() int {
return m.Version
}
// GetDialect gets the name of the dialect that defines the Message
func (m *UnknownMessage) GetDialect() string {
return "unknown"
}
// GetMessageName gets the name of the Message
func (m *UnknownMessage) GetMessageName() string {
return "unknown"
}
// GetID gets the ID of the Message
func (m *UnknownMessage) GetID() uint32 {
return m.ID
}
func (m *UnknownMessage) Read(frame Frame) (err error) {
rawBytes := make([]byte, frame.GetMessageLength())
copy(rawBytes, frame.GetMessageBytes())
m.ID = frame.GetMessageID()
m.Version = frame.GetVersion()
m.Raw = rawBytes
return
}
func (m *UnknownMessage) Write(version int) ([]byte, error) {
return m.Raw, nil
}