gpsd.go includes a mini-gpsd now.

This commit is contained in:
Torsten Harenberg
2025-02-16 17:22:54 +01:00
parent 0c543393d8
commit 561f597231
4 changed files with 429 additions and 14 deletions

48
ptc.go
View File

@@ -10,6 +10,7 @@ import (
"github.com/TwiN/go-color"
"github.com/albenik/go-serial/v2"
"log"
"math"
"math/bits"
"net"
"os"
@@ -55,7 +56,6 @@ type pmux struct {
type Modem struct {
devicePath string
localAddr string
remoteAddr string
@@ -77,6 +77,7 @@ type Modem struct {
const (
SerialTimeout = 1
PactorChannel = 4
NMEAChannel = 249
MaxSendData = 255
MaxFrameNotTX = 2
)
@@ -104,8 +105,6 @@ func debugEnabled() int {
}
func writeDebug(message string, level int) {
//debugMux.Lock()
// defer debugMux.Unlock()
if debugEnabled() >= level {
_, file, no, ok := runtime.Caller(1)
if ok {
@@ -126,7 +125,6 @@ func OpenModem(path string, baudRate int, myCall string, initfile string, cmdlin
p = &Modem{
// Initialise variables
devicePath: path,
localAddr: myCall,
remoteAddr: "",
@@ -160,6 +158,7 @@ func OpenModem(path string, baudRate int, myCall string, initfile string, cmdlin
//Setup serial device
if p.device, err = serial.Open(p.devicePath, serial.WithBaudrate(baudRate), serial.WithReadTimeout(SerialTimeout)); err != nil {
writeDebug(err.Error(), 1)
time.Sleep(3 * time.Second)
return nil, err
}
}
@@ -196,7 +195,10 @@ func (p *Modem) init() (err error) {
if _, _, err = p.writeAndGetResponse("", -1, false, 10240); err != nil {
return err
}
if _, _, err = p.writeAndGetResponse("", -1, false, 10240); err != nil {
return err
}
time.Sleep(time.Second)
// Make sure, modem is in main menu. Will respose with "ERROR:" when already in it -> Just discard answer!
_, ans, err := p.writeAndGetResponse("Quit", -1, false, 1024)
if err != nil {
@@ -223,6 +225,7 @@ func (p *Modem) init() (err error) {
return errors.New("Found a modem type: " + ver + " which this driver doesn't support. Please contact the author.")
}
writeDebug("Found a "+modem+" modem at "+p.devicePath, 0)
s.DeviceType = modem
writeDebug("Running init commands", 1)
ct := time.Now()
commands := []string{"DD", "RESTART", "MYcall " + p.localAddr, "PTCH " + strconv.Itoa(PactorChannel),
@@ -241,7 +244,7 @@ func (p *Modem) init() (err error) {
for _, cmd := range commands {
var res string
writeDebug("Sending command to modem: "+cmd, 0)
writeDebug("Sending command to modem: "+cmd, 1)
_, res, err = p.writeAndGetResponse(cmd, -1, false, 1024)
if err != nil {
return err
@@ -562,16 +565,22 @@ func (p *Modem) checkResponse(resp string, ch int) (n int, data []byte, err erro
return 0, nil, fmt.Errorf("Channel missmatch")
}
if int(head[1]) == 1 { //sucess,message follows
writeDebug("*** SUCCESS: "+string(payload), 0)
writeDebug(fmt.Sprintf("*** SUCCESS on channel %d: %s", ch, string(payload)), 1)
if ch == NMEAChannel && s.GPSdMode {
if s.GPSStream.GetLen() == 0 {
s.GPSStream.Enqueue("$" + string(bytes.Trim(payload, "\x00"))) //need to remove the trailing NULL byte
}
}
}
if int(head[1]) == 2 {
writeDebug("*** ERROR: "+string(payload), 0)
}
if int(head[1]) != 7 && int(head[1]) != 3 {
if !s.VARAMode {
if !s.VARAMode && ch == PactorChannel {
s.Command.Response.Enqueue(fmt.Sprintf("%s\n", payload))
}
writeDebug("Message from Modem: "+string(payload), 0)
writeDebug("Message from Modem: "+string(payload), 1)
return 0, nil, fmt.Errorf("Not a data response")
}
if int(head[1]) == 3 { //Link status
@@ -685,7 +694,7 @@ func (p *Modem) writeAndGetResponse(msg string, ch int, isCommand bool, chunkSiz
return 0, "", err
}
}
br, b, err := p.read(1)
br, b, err := p.read(-1)
if err != nil {
writeDebug("ERROR at _read: "+error.Error(err), 1)
}
@@ -850,14 +859,28 @@ func (p *Modem) writeChannel(msg string, ch int, isCommand bool) error {
writeDebug(err.Error(), 2)
return err
}
writeDebug("Done writing channel", 2)
/*if !isCommand {
p.cmdBuf <- "%Q"
}*/
return nil
}
// read: Read from serial connection (thread safe)
func (p *Modem) read(chunkSize int) (int, []byte, error) {
// read: Read readsize devices from serial connection (thread safe). If readsize==-1 try to check how many data is there
func (p *Modem) read(readsize int) (int, []byte, error) {
var chunkSize int
if readsize == -1 {
t, err := p.device.ReadyToRead()
if err != nil {
chunkSize = math.MaxInt
writeDebug("ERROR in ReadyToRead: "+err.Error(), 3)
} else {
chunkSize = int(t)
writeDebug(fmt.Sprintf("chunksize: %d", chunkSize), 3)
}
} else {
chunkSize = readsize
}
buf := make([]byte, chunkSize)
if strings.HasPrefix(p.devicePath, "tcp://") {
p.tcpdevice.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
@@ -875,6 +898,7 @@ func (p *Modem) read(chunkSize int) (int, []byte, error) {
p.mux.device.Lock()
defer p.mux.device.Unlock()
p.device.SetReadTimeout(100) // 100 ms
n, err := p.device.Read(buf)
if err != nil {
writeDebug("Error received during read: "+err.Error(), 1)