kappanhang/serialstream.go

216 lines
4.9 KiB
Go
Raw Normal View History

2020-10-20 23:26:05 +02:00
package main
import (
"bytes"
"time"
2020-10-20 23:26:05 +02:00
"github.com/nonoo/kappanhang/log"
)
2020-10-25 12:55:41 +01:00
const maxSerialFrameLength = 80 // Max. frame length according to Hamlib.
2020-10-20 23:26:05 +02:00
type serialStream struct {
common streamCommon
serialPort serialPortStruct
2020-10-25 16:15:39 +01:00
tcpsrv serialTCPSrv
sendSeq uint16
readFromSerialPort struct {
buf bytes.Buffer
frameStarted bool
frameTimeout *time.Timer
}
deinitNeededChan chan bool
deinitFinishedChan chan bool
2020-10-20 23:26:05 +02:00
}
func (s *serialStream) send(d []byte) error {
s.common.pkt0.sendSeqLock()
defer s.common.pkt0.sendSeqUnlock()
l := byte(len(d))
p := append([]byte{0x15 + l, 0x00, 0x00, 0x00, 0x00, 0x00, byte(s.common.pkt0.sendSeq), byte(s.common.pkt0.sendSeq >> 8),
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
0xc1, l, 0x00, byte(s.sendSeq >> 8), byte(s.sendSeq)}, d...)
if err := s.common.send(p); err != nil {
return err
}
s.common.pkt0.sendSeq++
s.sendSeq++
return nil
}
func (s *serialStream) sendOpenClose(close bool) error {
s.common.pkt0.sendSeqLock()
defer s.common.pkt0.sendSeqUnlock()
var magic byte
if close {
magic = 0x00
} else {
magic = 0x05
}
p := []byte{0x16, 0x00, 0x00, 0x00, 0x00, 0x00, byte(s.common.pkt0.sendSeq), byte(s.common.pkt0.sendSeq >> 8),
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
0xc0, 0x01, 0x00, byte(s.sendSeq >> 8), byte(s.sendSeq), magic}
if err := s.common.send(p); err != nil {
return err
}
s.common.pkt0.sendSeq++
s.sendSeq++
return nil
}
2020-10-20 23:26:05 +02:00
func (s *serialStream) handleRead(r []byte) {
if len(r) >= 22 {
if r[16] == 0xc1 && r[0]-0x15 == r[17] {
2020-10-25 16:15:39 +01:00
r = r[21:]
// log.Print("fromradio ", r)
s.serialPort.write <- r
if s.tcpsrv.toClient != nil {
s.tcpsrv.toClient <- r
}
}
}
2020-10-20 23:26:05 +02:00
}
2020-10-25 16:15:39 +01:00
func (s *serialStream) gotDataForRadio(r []byte) {
2020-10-25 14:16:12 +01:00
for len(r) > 0 && !s.readFromSerialPort.frameStarted {
if s.readFromSerialPort.buf.Len() > 1 {
s.readFromSerialPort.buf.Reset()
}
if s.readFromSerialPort.buf.Len() == 0 {
// Cut until we find the frame start byte.
for r[0] != 0xfe {
r = r[1:]
if len(r) == 0 {
return
}
2020-10-25 14:16:12 +01:00
}
// Found the first start byte.
s.readFromSerialPort.buf.WriteByte(r[0])
r = r[1:]
}
if len(r) > 0 && s.readFromSerialPort.buf.Len() == 1 {
if r[0] != 0xfe {
s.readFromSerialPort.buf.Reset()
r = r[1:]
} else {
// Found the second start byte.
s.readFromSerialPort.buf.WriteByte(r[0])
r = r[1:]
2020-10-25 14:16:12 +01:00
s.readFromSerialPort.frameTimeout.Reset(100 * time.Millisecond)
s.readFromSerialPort.frameStarted = true
}
}
2020-10-25 14:16:12 +01:00
}
2020-10-25 14:16:12 +01:00
for _, b := range r {
s.readFromSerialPort.buf.WriteByte(b)
if b == 0xfc || b == 0xfd || s.readFromSerialPort.buf.Len() == maxSerialFrameLength {
2020-10-25 16:15:39 +01:00
// log.Print("toradio ", s.readFromSerialPort.buf.Bytes())
2020-10-25 14:16:12 +01:00
if err := s.send(s.readFromSerialPort.buf.Bytes()); err != nil {
reportError(err)
}
if !s.readFromSerialPort.frameTimeout.Stop() {
<-s.readFromSerialPort.frameTimeout.C
}
2020-10-25 14:16:12 +01:00
s.readFromSerialPort.buf.Reset()
s.readFromSerialPort.frameStarted = false
break
}
}
}
func (s *serialStream) loop() {
2020-10-20 23:26:05 +02:00
for {
select {
case r := <-s.common.readChan:
s.handleRead(r)
case r := <-s.serialPort.read:
2020-10-25 16:15:39 +01:00
s.gotDataForRadio(r)
case r := <-s.tcpsrv.fromClient:
s.gotDataForRadio(r)
case <-s.readFromSerialPort.frameTimeout.C:
s.readFromSerialPort.buf.Reset()
s.readFromSerialPort.frameStarted = false
case <-s.deinitNeededChan:
s.deinitFinishedChan <- true
return
2020-10-20 23:26:05 +02:00
}
}
}
func (s *serialStream) start(devName string) error {
if err := s.serialPort.init(devName); err != nil {
return err
}
if err := s.common.sendPkt3(); err != nil {
return err
}
if err := s.common.waitForPkt4Answer(); err != nil {
return err
}
if err := s.common.sendPkt6(); err != nil {
return err
}
if err := s.common.waitForPkt6Answer(); err != nil {
return err
}
log.Print("stream started")
s.common.pkt7.startPeriodicSend(&s.common, 1, false)
s.common.pkt0.startPeriodicSend(&s.common)
if err := s.sendOpenClose(false); err != nil {
return err
}
2020-10-25 16:15:39 +01:00
if err := s.tcpsrv.start(); err != nil {
return err
}
s.deinitNeededChan = make(chan bool)
s.deinitFinishedChan = make(chan bool)
s.readFromSerialPort.frameTimeout = time.NewTimer(0)
<-s.readFromSerialPort.frameTimeout.C
go s.loop()
return nil
}
func (s *serialStream) init() error {
if err := s.common.init("serial", 50002); err != nil {
return err
}
return nil
}
func (s *serialStream) deinit() {
2020-10-25 16:15:39 +01:00
if s.common.conn != nil {
_ = s.sendOpenClose(true)
}
2020-10-25 16:15:39 +01:00
s.tcpsrv.stop()
s.serialPort.deinit()
if s.deinitNeededChan != nil {
s.deinitNeededChan <- true
<-s.deinitFinishedChan
}
s.common.deinit()
2020-10-20 23:26:05 +02:00
}