Skip to content

Commit

Permalink
migrate to go-tty for better portabiliy
Browse files Browse the repository at this point in the history
reinstall signal handler and proc
rework TX sender
  • Loading branch information
stronnag committed Mar 12, 2023
1 parent 3ec4a2d commit 96b2a1e
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 148 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deet/simpleline v0.0.0-20140919022041-9d297ff784a2
github.com/eclipse/paho.mqtt.golang v1.4.2
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203
github.com/gorilla/websocket v1.5.0 // indirect
github.com/mattn/go-tty v0.0.4
github.com/mazznoer/colorgrad v0.9.1
github.com/mazznoer/csscolorparser v0.1.3 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/deet/simpleline v0.0.0-20140919022041-9d297ff784a2 h1:pDneKHtEn84/jIL
github.com/deet/simpleline v0.0.0-20140919022041-9d297ff784a2/go.mod h1:dKjFZHH1uVU5Ui2LWp5vewMIybnstN1rw01ajCIpIl8=
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg=
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand All @@ -23,6 +21,13 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-tty v0.0.4 h1:NVikla9X8MN0SQAqCYzpGyXv0jY7MNl3HOWD2dkle7E=
github.com/mattn/go-tty v0.0.4/go.mod h1:u5GGXBtZU6RQoKV8gY5W6UhMudbR5vXnUe7j3pxse28=
github.com/mazznoer/colorgrad v0.9.1 h1:MB80JYVndKWSMEM1beNqnuOowWGhoQc3DXWXkFp6JlM=
github.com/mazznoer/colorgrad v0.9.1/go.mod h1:WX2R9wt9B47+txJZVVpM9LY+LAGIdi4lTI5wIyreDH4=
github.com/mazznoer/csscolorparser v0.1.2/go.mod h1:Aj22+L/rYN/Y6bj3bYqO3N6g1dtdHtGfQ32xZ5PJQic=
Expand Down Expand Up @@ -65,7 +70,10 @@ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
4 changes: 2 additions & 2 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Configuration struct {
RedIsLow bool `json:"low-is-red"`
SitlEEprom string `json:"-"`
SitlListen string `json:"-"`
SitlPort string `json:"-"`
SitlPort int `json:"-"`
Verbose int `json:"-"`
}

Expand Down Expand Up @@ -160,7 +160,7 @@ func ParseCLI(gv func() string) ([]string, string) {
Config.Idx = 1
flag.StringVar(&Config.SitlEEprom, "eeprom", "", "EEprom name")
flag.StringVar(&Config.SitlListen, "listen", ":49000", "Listening port")
flag.StringVar(&Config.SitlPort, "txport", "127.0.0.1:5761", "host:port for serial TX")
flag.IntVar(&Config.SitlPort, "txport", 5761, "host:port for serial TX")
flag.IntVar(&Config.Verbose, "verbose", 0, "Verbosity")
} else {
flag.BoolVar(&Config.Kml, "kml", Config.Kml, "Generate KML (vice default KMZ)")
Expand Down
172 changes: 110 additions & 62 deletions pkg/sitlgen/msp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ const (
RX_DISARM
)

const (
SEND_NONE = iota
SEND_MSP
SEND_RSSI
SEND_CHANS
SEND_TIMEOUT
)

const SETTING_STR string = "nav_extra_arming_safety"
const MAX_MODE_ACTIVATION_CONDITION_COUNT int = 40

Expand All @@ -78,6 +86,7 @@ type MSPSerial struct {
bypass bool
c0 chan SChan
mranges []ModeRange
ok bool
}

type ModeRange struct {
Expand Down Expand Up @@ -251,13 +260,18 @@ func (m *MSPSerial) Read_msp(c0 chan SChan) {
}
}
} else {
m.ok = false
if err != nil {
log.Printf("Serial Read %v\n", err)
if options.Config.Verbose > 1 {
log.Printf("Serial Read %v\n", err)
}
} else {
log.Println("serial EOF")
}
c0 <- SChan{}

c0 <- SChan{ok: false}
m.conn.Close()
return
}
}
}
Expand All @@ -271,14 +285,16 @@ func NewMSPSerial(remote string) (*MSPSerial, error) {
if err != nil {
return nil, err
}
return &MSPSerial{conn: conn}, nil
return &MSPSerial{conn: conn, ok: true}, nil
}

func (m *MSPSerial) Send_msp(cmd uint16, payload []byte) {
buf := encode_msp2(cmd, payload)
_, err := m.conn.Write(buf)
if err != nil {
log.Println(err)
if m.ok {
buf := encode_msp2(cmd, payload)
_, err := m.conn.Write(buf)
if err != nil {
log.Println(err)
}
}
}

Expand Down Expand Up @@ -353,9 +369,9 @@ func (m *MSPSerial) init(nchan chan MSPChans, schan chan byte, rssich chan byte)
} else {
log.Println("MAP error")
}
txinfo := []byte{0x02, 0x6c, 0x07, 0xdc, 0x05, 0x4c, 0x04, 0x00, 0x75, 0x03, 0x43, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
m.Send_msp(msp_SET_RX_CONFIG, txinfo)
case msp_SET_RX_CONFIG:
// txinfo := []byte{0x02, 0x6c, 0x07, 0xdc, 0x05, 0x4c, 0x04, 0x00, 0x75, 0x03, 0x43, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
// m.Send_msp(msp_SET_RX_CONFIG, txinfo)
// case msp_SET_RX_CONFIG:
m.Send_msp(msp_NAME, nil)
case msp_NAME:
if v.len > 0 {
Expand Down Expand Up @@ -435,6 +451,7 @@ func (m *MSPSerial) run(nchan chan MSPChans, schan chan byte, rssich chan byte)
ichan := MSPChans{}
xstatus := uint64(0)
rssi := byte(0)
lrssi := byte(0)
ichan[0] = 1500
ichan[1] = 1500
if m.bypass {
Expand All @@ -446,81 +463,112 @@ func (m *MSPSerial) run(nchan chan MSPChans, schan chan byte, rssich chan byte)
for j := 4; j < 16; j++ {
ichan[j] = 1001
}
tdata := m.serialise_rx(ichan)
m.Send_msp(msp_SET_RAW_RC, tdata)
<-m.c0

rvstat := byte(0)

mcnt := byte(0)
var sv SChan
tdata := m.serialise_rx(ichan)
m.Send_msp(msp_SET_RAW_RC, tdata)
log.Printf("RC init done\n")
for {
event := SEND_NONE
select {
case r := <-rssich:
rssi = r
case <-time.After(100 * time.Millisecond):
case v := <-m.c0:
// sv_cmd = v.cmd
//sv_ok = v.ok
sv = v
mcnt += 1
event = SEND_MSP
case v := <-rssich:
event = SEND_RSSI
rssi = v
case <-time.After(50 * time.Millisecond):
event = SEND_TIMEOUT
case v := <-nchan:
event = SEND_CHANS
for j, u := range v {
if u != 0xffff {
ichan[j] = u
}
}
}
switch event {
case SEND_TIMEOUT:
tdata := m.serialise_rx(ichan)
m.Send_msp(msp_SET_RAW_RC, tdata)
sv := <-m.c0
if sv.ok && sv.cmd == msp_SET_RAW_RC {
status, armflags := m.get_status()
// Unarmed, able to arm
if (status&1) == 0 && armflags < 0x200 {
if rvstat != 1 {
rvstat = 1
if options.Config.Verbose > 1 {
log.Printf("Set status %d (%x)\n", rvstat, armflags)
}
schan <- 1
case SEND_MSP:
if sv.ok {
send_chans := true
if sv.cmd == msp_SET_RAW_RC && mcnt%5 == 0 {
send_chans = false
m.Send_msp(msp2_INAV_STATUS, nil)
} else if sv.cmd == msp2_INAV_STATUS {
status := binary.LittleEndian.Uint64(sv.data[13:21])
armflags := binary.LittleEndian.Uint32(sv.data[9:13])
if options.Config.Verbose > 2 {
log.Printf("Status, Armflags %x %x\n", status, armflags)
}
}
if (xstatus & 1) != (status & 1) {
if options.Config.Verbose > 0 {
log.Printf("status changed %x -> %x (%x)\n", xstatus, status, armflags)
}
xstatus = status
if (status & 1) == 0 {
if rvstat != 2 {
rvstat = 2
// Unarmed, able to arm
if (status&1) == 0 && armflags < 0x200 {
if rvstat != 1 {
rvstat = 1
if options.Config.Verbose > 1 {
log.Printf("Set status %d (%x)\n", rvstat, armflags)
}
schan <- 2
schan <- 1
}
} else {
if rvstat != 3 {
rvstat = 3
if options.Config.Verbose > 1 {
log.Printf("Set status %d (%x)\n", rvstat, armflags)
}
if (xstatus & 1) != (status & 1) {
if options.Config.Verbose > 0 {
log.Printf("status changed %x -> %x (%x)\n", xstatus, status, armflags)
}
xstatus = status
if (status & 1) == 0 {
if rvstat != 2 {
rvstat = 2
if options.Config.Verbose > 1 {
log.Printf("Set status %d (%x)\n", rvstat, armflags)
}
schan <- 2
}
} else {
if rvstat != 3 {
rvstat = 3
if options.Config.Verbose > 1 {
log.Printf("Set status %d (%x)\n", rvstat, armflags)
}
schan <- 3
}
schan <- 3
}
}
}
m.Rssi(rssi)
<-m.c0

if send_chans {
tdata := m.serialise_rx(ichan)
m.Send_msp(msp_SET_RAW_RC, tdata)
}
} else {
log.Println("RC data send failed")
if options.Config.Verbose > 1 {
log.Println("RC data send failed")
}
schan <- 0xff
}

case v := <-nchan:
for j, u := range v {
if u != 0xffff {
ichan[j] = u
}
case SEND_RSSI:
if lrssi != rssi {
m.Rssi(rssi)
lrssi = rssi
}
case SEND_CHANS:
tdata := m.serialise_rx(ichan)
m.Send_msp(msp_SET_RAW_RC, tdata)
}
}
}

func (m *MSPSerial) get_status() (uint64, uint32) {
status := uint64(0)
armf := uint32(0)

m.Send_msp(msp2_INAV_STATUS, nil)
v := <-m.c0
if v.ok {
status = binary.LittleEndian.Uint64(v.data[13:21])
armf = binary.LittleEndian.Uint32(v.data[9:13])
func (m *MSPSerial) Close() {
if m.ok {
m.ok = false
m.conn.Close()
}
return status, armf
}
23 changes: 23 additions & 0 deletions pkg/sitlgen/proc_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build !windows
// +build !windows

package sitlgen

import (
"os"
"os/exec"
"syscall"
)

func proc_start(args ...string) (p *os.Process, err error) {
if args[0], err = exec.LookPath(args[0]); err == nil {
var procAttr os.ProcAttr
procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
procAttr.Sys = &syscall.SysProcAttr{Foreground: false, Setsid: true}
p, err := os.StartProcess(args[0], args, &procAttr)
if err == nil {
return p, nil
}
}
return nil, err
}
21 changes: 21 additions & 0 deletions pkg/sitlgen/proc_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build windows
// +build windows

package sitlgen

import (
"os"
"os/exec"
)

func proc_start(args ...string) (p *os.Process, err error) {
if args[0], err = exec.LookPath(args[0]); err == nil {
var procAttr os.ProcAttr
procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
p, err := os.StartProcess(args[0], args, &procAttr)
if err == nil {
return p, nil
}
}
return nil, err
}
Loading

0 comments on commit 96b2a1e

Please sign in to comment.