-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
354 lines (304 loc) · 8.39 KB
/
main.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
// main executable.
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"reflect"
"regexp"
"strconv"
"sync"
"time"
"github.com/alecthomas/kong"
"github.com/bluenviron/gomavlib/v3"
"github.com/bluenviron/gomavlib/v3/pkg/dialect"
"github.com/bluenviron/gomavlib/v3/pkg/dialects/common"
"github.com/bluenviron/gomavlib/v3/pkg/message"
)
var version = "v0.0.0"
var (
reArgs = regexp.MustCompile("^([a-z]+):(.+)$")
reSerial = regexp.MustCompile("^(.+?):([0-9]+)$")
)
// decode/encode only a minimal set of messages.
// other messages change too frequently and cannot be integrated into a static tool.
func generateDialect(hbDisable bool, streamreqDisable bool) *dialect.Dialect {
msgs := []message.Message{}
// add all messages with the TargetSystem and TargetComponent fields
var zero reflect.Value
for _, msg := range common.Dialect.Messages {
rv := reflect.ValueOf(msg).Elem()
if rv.FieldByName("TargetSystem") != zero && rv.FieldByName("TargetComponent") != zero {
msgs = append(msgs, msg)
}
}
if !hbDisable || !streamreqDisable {
msgs = append(msgs, &common.MessageHeartbeat{})
}
return &dialect.Dialect{Version: 3, Messages: msgs}
}
type endpointType struct {
args string
desc string
make func(args string) (gomavlib.EndpointConf, error)
}
var endpointTypes = map[string]endpointType{
"serial": {
"port:baudrate",
"serial",
func(args string) (gomavlib.EndpointConf, error) {
matches := reSerial.FindStringSubmatch(args)
if matches == nil {
return nil, fmt.Errorf("invalid address")
}
dev := matches[1]
baud, _ := strconv.Atoi(matches[2])
return gomavlib.EndpointSerial{
Device: dev,
Baud: baud,
}, nil
},
},
"udps": {
"listen_ip:port",
"udp, server mode",
func(args string) (gomavlib.EndpointConf, error) {
return gomavlib.EndpointUDPServer{Address: args}, nil
},
},
"udpc": {
"dest_ip:port",
"udp, client mode",
func(args string) (gomavlib.EndpointConf, error) {
return gomavlib.EndpointUDPClient{Address: args}, nil
},
},
"udpb": {
"broadcast_ip:port",
"udp, broadcast mode",
func(args string) (gomavlib.EndpointConf, error) {
return gomavlib.EndpointUDPBroadcast{BroadcastAddress: args}, nil
},
},
"tcps": {
"listen_ip:port",
"tcp, server mode",
func(args string) (gomavlib.EndpointConf, error) {
return gomavlib.EndpointTCPServer{Address: args}, nil
},
},
"tcpc": {
"dest_ip:port",
"tcp, client mode",
func(args string) (gomavlib.EndpointConf, error) {
return gomavlib.EndpointTCPClient{Address: args}, nil
},
},
}
func generateEndpointConfs(endpoints []string) ([]gomavlib.EndpointConf, error) {
if len(endpoints) < 1 {
return nil, fmt.Errorf("at least one endpoint is required")
}
econfs := make([]gomavlib.EndpointConf, len(endpoints))
for i, e := range endpoints {
matches := reArgs.FindStringSubmatch(e)
if matches == nil {
return nil, fmt.Errorf("invalid endpoint: %s", e)
}
key, args := matches[1], matches[2]
etype, ok := endpointTypes[key]
if !ok {
return nil, fmt.Errorf("invalid endpoint: %s", e)
}
conf, err := etype.make(args)
if err != nil {
return nil, err
}
econfs[i] = conf
}
return econfs, nil
}
var cli struct {
Version bool `help:"print version."`
Quiet bool `short:"q" help:"suppress info messages."`
Print bool `help:"print routed frames."`
PrintErrors bool
ReadTimeout time.Duration `help:"timeout of read operations." default:"10s"`
WriteTimeout time.Duration `help:"timeout of write operations." default:"10s"`
IdleTimeout time.Duration `help:"disconnect idle connections after a timeout." default:"60s"`
HbDisable bool `help:"disable heartbeats."`
HbVersion int `enum:"1,2" help:"set mavlink version of heartbeats." default:"1"`
HbSystemid int `default:"125"`
HbComponentid int `help:"set component ID of heartbeats." default:"191"`
HbPeriod int `help:"set period of heartbeats." default:"5"`
StreamreqDisable bool
StreamreqFrequency int `help:"set the stream frequency to request." default:"4"`
Endpoints []string `arg:"" optional:""`
}
type program struct {
ctx context.Context
ctxCancel func()
wg sync.WaitGroup
node *gomavlib.Node
errorHandler *errorHandler
messageHandler *messageHandler
}
func newProgram(args []string) (*program, error) {
parser, err := kong.New(&cli,
kong.Description("mavp2p "+version),
kong.UsageOnError(),
kong.ValueFormatter(func(value *kong.Value) string {
switch value.Name {
case "print-errors":
return "print parse errors singularly, instead of printing only their quantity every 5 seconds."
case "hb-systemid":
return "set system ID of heartbeats. it is recommended to set a different system id for each router in the network."
case "streamreq-disable":
return "do not request streams to Ardupilot devices," +
" that need an explicit request in order to emit telemetry streams." +
" this task is usually delegated to the router," +
" in order to avoid conflicts when multiple ground stations are active."
case "endpoints":
desc := "Space-separated list of endpoints. At least one endpoint is required. " +
"Possible endpoints types are:\n\n"
for k, etype := range endpointTypes {
desc += fmt.Sprintf("%s:%s (%s)\n\n", k, etype.args, etype.desc)
}
return desc
default:
return kong.DefaultHelpValueFormatter(value)
}
}))
if err != nil {
return nil, err
}
kongCtx, err := parser.Parse(args)
if err != nil {
return nil, err
}
if cli.Version {
fmt.Println(version)
os.Exit(0)
}
// print usage if no args are provided
if len(os.Args) <= 1 {
kongCtx.PrintUsage(false) //nolint:errcheck
os.Exit(1)
}
endpointConfs, err := generateEndpointConfs(cli.Endpoints)
if err != nil {
return nil, err
}
ctx, ctxCancel := context.WithCancel(context.Background())
p := &program{
ctx: ctx,
ctxCancel: ctxCancel,
}
p.node, err = gomavlib.NewNode(gomavlib.NodeConf{
Endpoints: endpointConfs,
Dialect: generateDialect(cli.HbDisable, cli.StreamreqDisable),
OutVersion: func() gomavlib.Version {
if cli.HbVersion == 2 {
return gomavlib.V2
}
return gomavlib.V1
}(),
OutSystemID: byte(cli.HbSystemid),
OutComponentID: byte(cli.HbComponentid),
HeartbeatDisable: cli.HbDisable,
HeartbeatPeriod: (time.Duration(cli.HbPeriod) * time.Second),
StreamRequestEnable: !cli.StreamreqDisable,
StreamRequestFrequency: cli.StreamreqFrequency,
ReadTimeout: cli.ReadTimeout,
WriteTimeout: cli.WriteTimeout,
IdleTimeout: cli.IdleTimeout,
})
if err != nil {
ctxCancel()
return nil, err
}
p.errorHandler, err = newErrorHandler(
ctx,
&p.wg,
cli.PrintErrors,
)
if err != nil {
ctxCancel()
p.wg.Wait()
p.node.Close()
return nil, err
}
p.messageHandler, err = newMessageHandler(
ctx,
&p.wg,
cli.StreamreqDisable,
p.node,
)
if err != nil {
ctxCancel()
p.wg.Wait()
p.node.Close()
return nil, err
}
if cli.Quiet {
log.SetOutput(io.Discard)
}
log.Printf("mavp2p %s", version)
log.Printf("router started with %d %s",
len(endpointConfs),
func() string {
if len(endpointConfs) == 1 {
return "endpoint"
}
return "endpoints"
}())
p.wg.Add(1)
go p.run()
return p, nil
}
func (p *program) close() {
p.ctxCancel()
p.wg.Wait()
}
func (p *program) wait() {
p.wg.Wait()
}
func (p *program) run() {
defer p.wg.Done()
defer p.node.Close()
for {
select {
case e := <-p.node.Events():
switch evt := e.(type) {
case *gomavlib.EventChannelOpen:
log.Printf("channel opened: %s", evt.Channel)
case *gomavlib.EventChannelClose:
log.Printf("channel closed: %s", evt.Channel)
p.messageHandler.onEventChannelClose(evt)
case *gomavlib.EventStreamRequested:
log.Printf("stream requested to chan=%s sid=%d cid=%d", evt.Channel,
evt.SystemID, evt.ComponentID)
case *gomavlib.EventFrame:
if cli.Print {
log.Printf("%#v, %#v\n", evt.Frame, evt.Message())
}
p.messageHandler.onEventFrame(evt)
case *gomavlib.EventParseError:
p.errorHandler.onEventError(evt)
}
case <-p.ctx.Done():
return
}
}
}
func main() {
p, err := newProgram(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "ERR: %s\n", err)
os.Exit(1)
}
defer p.close()
p.wait()
}