From 71d2cfb652e92d624f9e3a7e7df5246a92ff621c Mon Sep 17 00:00:00 2001 From: Torsten Harenberg Date: Mon, 3 Feb 2025 15:37:45 +0100 Subject: [PATCH] Avoid calling GetModemStatusBits on Android. Should fix #1 --- cts.go | 14 ++++++++++++++ cts_android.go | 9 +++++++++ ptc.go | 31 ++++++++++++++----------------- 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 cts.go create mode 100644 cts_android.go diff --git a/cts.go b/cts.go new file mode 100644 index 0000000..c25cb69 --- /dev/null +++ b/cts.go @@ -0,0 +1,14 @@ +//go:build !android + +package main + +import "github.com/albenik/go-serial/v2" + +// cts returns the clear-to-send status of a device. On Android it will return true as GetModemStatusBits() fails there. +func cts(p *serial.Port) (bool, error) { + r, err := p.GetModemStatusBits() + if err != nil { + return false, err + } + return r.CTS, nil +} diff --git a/cts_android.go b/cts_android.go new file mode 100644 index 0000000..cfddd38 --- /dev/null +++ b/cts_android.go @@ -0,0 +1,9 @@ +//go:build android + +package main + +import "github.com/albenik/go-serial/v2" + +func cts(_ *serial.Port) (bool, error) { + return true, nil +} diff --git a/ptc.go b/ptc.go index 1ca139e..7d25b9c 100644 --- a/ptc.go +++ b/ptc.go @@ -761,12 +761,15 @@ func (p *Modem) _write(cmd string) error { defer p.mux.device.Unlock() out := cmd + "\r" for { - status, err := p.device.GetModemStatusBits() + + // check if the serial line is clear-to-send + status, err := cts(p.device) if err != nil { writeDebug("GetModemStatusBits failed. cmd: "+cmd+" Error: "+err.Error(), 1) return err } - if status.CTS { + + if status { for { sent, err := p.device.Write([]byte(out)) if err == nil { @@ -788,13 +791,13 @@ func (p *Modem) _write(cmd string) error { // Although these functions are small, I prefer to keep their functionality // separate. They follow the steps in the SCS documentation -// helper function: de-hexlify and write to debug channel +// printhex: helper function: de-hexlify and write to debug channel func printhex(s string) { t, _ := hex.DecodeString(s) writeDebug(string(t), 3) } -// helper function: "unstuff" a string +// unstuff: helper function: "unstuff" a string func unstuff(s string) string { //Expect: the string contains aa aa at the beginning, that should NOT be //stuffed @@ -806,7 +809,7 @@ func unstuff(s string) string { return re } -// helper function: "stuff" a string: replaces every #170 with #170#0 +// stuff: helper function: "stuff" a string: replaces every #170 with #170#0 func stuff(s string) string { //Expect: the string contains aa aa at the beginning, that should NOT be //stuffed @@ -822,14 +825,14 @@ func stuff(s string) string { return re } -// helper function: calculates the CCITT-CRC16 checksum +// checksum: helper function: calculates the CCITT-CRC16 checksum func checksum(s string) uint16 { tochecksum, _ := hex.DecodeString(s[4:]) chksum := bits.ReverseBytes16(crc16.ChecksumCCITT([]byte(tochecksum))) return chksum } -// helper fuction: check the checksum by comparing +// checkcrc: helper fuction: check the checksum by comparing func checkcrc(s []byte) bool { tochecksum := s[2 : len(s)-2] chksum := bits.ReverseBytes16(crc16.ChecksumCCITT(tochecksum)) @@ -837,7 +840,7 @@ func checkcrc(s []byte) bool { return (binary.BigEndian.Uint16(pksum) == chksum) } -// super helper fuction: convert an ordinary WA8DED message into a CRC-Hostmode message +// docrc: super helper fuction: convert an ordinary WA8DED message into a CRC-Hostmode message func docrc(msg string) string { // step 1: add a #170170 msg = fmt.Sprintf("%02x%02x%s", 170, 170, msg) @@ -850,9 +853,7 @@ func docrc(msg string) string { } -// Write channel to serial connection (NOT thread safe) -// -// If used, make shure to lock/unlock p.mux.pactor mutex! +// writeChannel: Write channel to serial connection (NOT thread safe)- If used, make sure to mutex! func (p *Modem) writeChannel(msg string, ch int, isCommand bool) error { if err := p.checkSerialDevice(); err != nil { writeDebug(err.Error(), 1) @@ -905,18 +906,14 @@ func (p *Modem) writeChannel(msg string, ch int, isCommand bool) error { return nil } -// Read from serial connection (thread safe) -// -// No other read/write operation allowed during this time +// read: Read from serial connection (thread safe) func (p *Modem) read(chunkSize int) (int, []byte, error) { p.mux.pactor.Lock() defer p.mux.pactor.Unlock() return p._read(chunkSize) } -// Read from serial connection (NOT thread safe) -// -// If used, make shure to lock/unlock p.mux.pactor mutex! +// _read: Read from serial connection (NOT thread safe). To be used from read func (p *Modem) _read(chunkSize int) (int, []byte, error) { if err := p.checkSerialDevice(); err != nil { writeDebug(err.Error(), 1)