This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
forked from queue-b/go-mavlink2
/
flexifunction_set.go
192 lines (152 loc) · 5.26 KB
/
flexifunction_set.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
package matrixpilot
/*
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"
"encoding/binary"
"fmt"
"text/tabwriter"
"github.com/botlink/go-mavlink2"
"github.com/botlink/go-mavlink2/util"
)
/*FlexifunctionSet Depreciated but used as a compiler flag. Do not remove */
type FlexifunctionSet struct {
/*TargetSystem System ID */
TargetSystem uint8
/*TargetComponent Component ID */
TargetComponent uint8
/*HasExtensionFieldValues indicates if this message has any extensions and */
HasExtensionFieldValues bool
}
func (m *FlexifunctionSet) String() string {
format := ""
var buffer bytes.Buffer
writer := tabwriter.NewWriter(&buffer, 0, 0, 2, ' ', 0)
format += "Name:\t%v/%v\n"
// Output field values based on the decoded message type
format += "TargetSystem:\t%v \n"
format += "TargetComponent:\t%v \n"
fmt.Fprintf(
writer,
format,
m.GetDialect(),
m.GetMessageName(),
m.TargetSystem,
m.TargetComponent,
)
writer.Flush()
return string(buffer.Bytes())
}
// GetVersion gets the MAVLink version of the Message contents
func (m *FlexifunctionSet) GetVersion() int {
if m.HasExtensionFieldValues {
return 2
}
return 1
}
// GetDialect gets the name of the dialect that defines the Message
func (m *FlexifunctionSet) GetDialect() string {
return "matrixpilot"
}
// GetMessageName gets the name of the Message
func (m *FlexifunctionSet) GetMessageName() string {
return "FlexifunctionSet"
}
// GetID gets the ID of the Message
func (m *FlexifunctionSet) GetID() uint32 {
return 150
}
// HasExtensionFields returns true if the message definition contained extensions; false otherwise
func (m *FlexifunctionSet) HasExtensionFields() bool {
return false
}
func (m *FlexifunctionSet) getV1Length() int {
return 2
}
func (m *FlexifunctionSet) getIOSlice() []byte {
return make([]byte, 2+1)
}
// Read sets the field values of the message from the raw message payload
func (m *FlexifunctionSet) Read(frame mavlink2.Frame) (err error) {
version := frame.GetVersion()
// Ensure only Version 1 or Version 2 were specified
if version != 1 && version != 2 {
err = mavlink2.ErrUnsupportedVersion
return
}
// Don't attempt to Read V2 messages from V1 frames
if m.GetID() > 255 && version < 2 {
err = mavlink2.ErrDecodeV2MessageV1Frame
return
}
// binary.Read can panic; swallow the panic and return a sane error
defer func() {
if r := recover(); r != nil {
err = mavlink2.ErrPrivateField
}
}()
// Get a slice of bytes long enough for the all the FlexifunctionSet fields
// binary.Read requires enough bytes in the reader to read all fields, even if
// the fields are just zero values. This also simplifies handling MAVLink2
// extensions and trailing zero truncation.
ioSlice := m.getIOSlice()
copy(ioSlice, frame.GetMessageBytes())
// Indicate if
if version == 2 && m.HasExtensionFields() {
ioSlice[len(ioSlice)-1] = 1
}
reader := bytes.NewReader(ioSlice)
err = binary.Read(reader, binary.LittleEndian, m)
return
}
// Write encodes the field values of the message to a byte array
func (m *FlexifunctionSet) Write(version int) (output []byte, err error) {
var buffer bytes.Buffer
// Ensure only Version 1 or Version 2 were specified
if version != 1 && version != 2 {
err = mavlink2.ErrUnsupportedVersion
return
}
// Don't attempt to Write V2 messages to V1 bodies
if m.GetID() > 255 && version < 2 {
err = mavlink2.ErrEncodeV2MessageV1Frame
return
}
err = binary.Write(&buffer, binary.LittleEndian, *m)
if err != nil {
return
}
output = buffer.Bytes()
// V1 uses fixed message lengths and does not include any extension fields
// Truncate the byte slice to the correct length
// This also removes the trailing extra byte written for HasExtensionFieldValues
if version == 1 {
output = output[:m.getV1Length()]
}
// V2 uses variable message lengths and includes extension fields
// The variable length is caused by truncating any trailing zeroes from
// the end of the message before it is added to a frame
if version == 2 {
// Set HasExtensionFieldValues to zero so that it doesn't interfere with V2 truncation
output[len(output)-1] = 0
output = util.TruncateV2(buffer.Bytes())
}
return
}