-
Notifications
You must be signed in to change notification settings - Fork 5
/
msp_set_rx.go
170 lines (156 loc) · 3.54 KB
/
msp_set_rx.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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
)
const (
DevClass_NONE = iota
DevClass_SERIAL
DevClass_TCP
DevClass_UDP
DevClass_BT
)
type DevDescription struct {
klass int
name string
param int
name1 string
param1 int
}
var (
baud = flag.Int("b", 115200, "Baud rate")
device = flag.String("d", "", "Serial Device")
every = flag.Int("every", 100, "Refresh time (ms)")
setthr = flag.Int("throttle", -1, "Low throttle (µs)")
verbose = flag.Bool("verbose", false, "log Rx/Tx stanzas")
auto_arm = flag.Bool("auto-arm", false, "Auto-arm FC when ready")
)
func check_device() DevDescription {
devdesc := parse_device(*device)
if devdesc.name == "" {
for _, v := range []string{"/dev/ttyACM0", "/dev/ttyUSB0"} {
if _, err := os.Stat(v); err == nil {
devdesc.klass = DevClass_SERIAL
devdesc.name = v
devdesc.param = *baud
break
}
}
}
if devdesc.name == "" && devdesc.param == 0 {
log.Fatalln("No device given\n")
} else {
log.Printf("Using device %s\n", devdesc.name)
}
return devdesc
}
func resolve_default_gw() string {
cmds := []string{"ip route show 0.0.0.0/0 | cut -d ' ' -f3",
"route -n | grep UG | awk '{print $2}'",
"route -n show 0.0.0.0 | grep gateway | awk '{print $2}'"}
ostr := os.Getenv("MWP_SERIAL_HOST")
if ostr != "" {
return ostr
}
for _, c := range cmds {
out, err := exec.Command("sh", "-c", c).Output()
ostr := strings.TrimSpace(string(out))
if err != nil {
log.Fatal(err)
} else {
if len(ostr) > 0 {
return ostr
}
}
}
return "__MWP_SERIAL_HOST"
}
func splithost(uhost string) (string, int) {
port := -1
host := ""
if uhost != "" {
if h, p, err := net.SplitHostPort(uhost); err != nil {
host = uhost
} else {
host = h
port, _ = strconv.Atoi(p)
}
}
return host, port
}
func parse_device(devstr string) DevDescription {
dd := DevDescription{name: "", klass: DevClass_NONE}
if devstr == "" {
return dd
}
if len(devstr) == 17 && (devstr)[2] == ':' && (devstr)[8] == ':' && (devstr)[14] == ':' {
dd.name = devstr
dd.klass = DevClass_BT
} else {
u, err := url.Parse(devstr)
if err == nil {
if u.Scheme == "tcp" {
dd.klass = DevClass_TCP
} else if u.Scheme == "udp" {
dd.klass = DevClass_UDP
}
if u.Scheme == "" {
ss := strings.Split(u.Path, "@")
dd.klass = DevClass_SERIAL
dd.name = ss[0]
if len(ss) > 1 {
dd.param, _ = strconv.Atoi(ss[1])
} else {
dd.param = 115200
}
} else {
if u.RawQuery != "" {
m, err := url.ParseQuery(u.RawQuery)
if err == nil {
if p, ok := m["bind"]; ok {
dd.param, _ = strconv.Atoi(p[0])
}
dd.name1, dd.param1 = splithost(u.Host)
}
} else {
if u.Path != "" {
parts := strings.Split(u.Path, ":")
if len(parts) == 2 {
dd.name1 = parts[0][1:]
dd.param1, _ = strconv.Atoi(parts[1])
}
}
dd.name, dd.param = splithost(u.Host)
if dd.name == "__MWP_SERIAL_HOST" {
dd.name = resolve_default_gw()
}
}
}
}
}
return dd
}
func main() {
log.SetPrefix("[msp_set_rx] ")
log.SetFlags(log.Ltime | log.Lmicroseconds)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of msp_set_rx [options]\n")
flag.PrintDefaults()
}
flag.Parse()
devdesc := check_device()
s := MSPInit(devdesc)
if s.swchan == -1 || s.swvalue < 1000 {
log.Fatalln("Mis-configured arm switch --- see README")
} else {
fmt.Printf("Arming set for channel %d / %dus\n", s.swchan+1, s.swvalue)
s.test_rx(*setthr, *verbose, *auto_arm)
}
}