feat: Implemented scope on Ethernet for Icom
This commit is contained in:
@@ -32,6 +32,16 @@ type Backend interface {
|
||||
SetPTT(on bool) error
|
||||
}
|
||||
|
||||
// interruptible is an OPTIONAL backend capability: abort an in-progress Connect
|
||||
// quickly. The network Icom backend's Connect blocks for up to tens of seconds
|
||||
// (UDP handshake + login + waiting for the rig to boot from standby); without a
|
||||
// way to interrupt it, Stop()/Start() would freeze on the poll goroutine until
|
||||
// the dial gives up — which is why Settings "Save & Close" hung for ~1 min once
|
||||
// the link was lost. Backends that don't implement it are simply not interrupted.
|
||||
type interruptible interface {
|
||||
Interrupt()
|
||||
}
|
||||
|
||||
// RigState is the snapshot exchanged with the frontend.
|
||||
//
|
||||
// FreqHz follows the ADIF FREQ convention: it is the TX frequency. When the
|
||||
@@ -156,6 +166,7 @@ func (m *Manager) stopLocked() {
|
||||
m.mu.Lock()
|
||||
stop := m.stopCh
|
||||
done := m.doneCh
|
||||
b := m.backend
|
||||
m.stopCh = nil
|
||||
m.doneCh = nil
|
||||
m.cmdCh = nil
|
||||
@@ -164,6 +175,11 @@ func (m *Manager) stopLocked() {
|
||||
if stop != nil {
|
||||
close(stop)
|
||||
}
|
||||
// Abort any in-progress Connect so we don't block on a slow network dial
|
||||
// (the poll goroutine can be tens of seconds deep in the Icom UDP handshake).
|
||||
if iv, ok := b.(interruptible); ok {
|
||||
iv.Interrupt()
|
||||
}
|
||||
if done != nil {
|
||||
<-done
|
||||
}
|
||||
@@ -403,6 +419,22 @@ type IcomTXState struct {
|
||||
Preamp int `json:"preamp"` // 0=off, 1=P.AMP1, 2=P.AMP2
|
||||
Att int `json:"att"` // dB attenuation, 0=off
|
||||
Filter int `json:"filter"` // 1 | 2 | 3 (FIL1/2/3)
|
||||
// Antenna (IC-7610 = ANT1/ANT2).
|
||||
Antenna int `json:"antenna"` // 1 | 2 (0 = unknown)
|
||||
// Filter fine controls: Twin PBT + manual notch (0-100, 50 = centre).
|
||||
PBTInner int `json:"pbt_inner"`
|
||||
PBTOuter int `json:"pbt_outer"`
|
||||
ManualNotch bool `json:"manual_notch"`
|
||||
NotchPos int `json:"notch_pos"`
|
||||
// TX extras.
|
||||
Squelch int `json:"squelch"`
|
||||
Comp bool `json:"comp"`
|
||||
CompLevel int `json:"comp_level"`
|
||||
Monitor bool `json:"monitor"`
|
||||
MonLevel int `json:"mon_level"`
|
||||
VOX bool `json:"vox"`
|
||||
VOXGain int `json:"vox_gain"`
|
||||
AntiVOX int `json:"anti_vox"`
|
||||
}
|
||||
|
||||
// IcomController is an OPTIONAL backend capability (the Icom CI-V backend): the
|
||||
@@ -436,6 +468,20 @@ type IcomController interface {
|
||||
StopCW() error // abort the CW message being sent
|
||||
SetKeySpeed(int) error // CW keyer speed in WPM
|
||||
SetBreakIn(int) error // CW break-in: 0=OFF, 1=SEMI, 2=FULL
|
||||
SetAntenna(int) error // 1 = ANT1, 2 = ANT2
|
||||
SetPBTInner(int) error // Twin PBT inside (0-100, 50 = centre)
|
||||
SetPBTOuter(int) error // Twin PBT outside (0-100, 50 = centre)
|
||||
SetManualNotch(bool) error
|
||||
SetNotchPos(int) error // manual-notch position (0-100, 50 = centre)
|
||||
SetSquelch(int) error
|
||||
SetComp(bool) error
|
||||
SetCompLevel(int) error
|
||||
SetMonitor(bool) error
|
||||
SetMonLevel(int) error
|
||||
SetVOX(bool) error
|
||||
SetVOXGain(int) error
|
||||
SetAntiVOX(int) error
|
||||
SetPower(bool) error // turn the transceiver on/off (manual — never auto on connect)
|
||||
}
|
||||
|
||||
// ScopeSweep is one complete spectrum-scope sweep reassembled from the Icom's
|
||||
|
||||
@@ -37,7 +37,9 @@ const (
|
||||
CmdPTT = 0x1C // sub 0x00 = PTT
|
||||
CmdExtra = 0x1A // sub 0x06 = data mode on modern Icoms
|
||||
CmdReadID = 0x19 // sub 0x00 = rig's own CI-V address (identifies model)
|
||||
CmdPower = 0x18 // power on/off (sub 0x01 = on, 0x00 = off; on needs an FE wake preamble)
|
||||
|
||||
CmdAnt = 0x12 // antenna selector (sub 0x00 = ANT1, 0x01 = ANT2; read = no sub)
|
||||
CmdAtt = 0x11 // attenuator (1 BCD byte of dB; 0x00 = off)
|
||||
CmdLevel = 0x14 // analogue levels (sub + 2 BCD bytes, 0000-0255)
|
||||
CmdMeter = 0x15 // meters (sub + 2 BCD bytes, 0000-0255): S-meter/Po/SWR
|
||||
@@ -67,8 +69,16 @@ const (
|
||||
// CmdLevel sub-commands.
|
||||
SubLevelAF = 0x01 // AF (volume)
|
||||
SubLevelRF = 0x02 // RF gain
|
||||
SubLevelSQL = 0x03 // squelch level
|
||||
SubLevelPBTIn = 0x07 // Twin PBT (inside) — 0-255, 128 = centre
|
||||
SubLevelPBTOut = 0x08 // Twin PBT (outside) — 0-255, 128 = centre
|
||||
SubLevelNR = 0x06 // noise-reduction depth
|
||||
SubLevelNotch = 0x0D // manual-notch position — 0-255, 128 = centre
|
||||
SubLevelComp = 0x0E // speech-compressor level
|
||||
SubLevelNB = 0x12 // noise-blanker depth
|
||||
SubLevelMon = 0x15 // monitor gain
|
||||
SubLevelVOXGain = 0x16 // VOX gain
|
||||
SubLevelAntiVOX = 0x17 // anti-VOX level
|
||||
SubLevelRFPower = 0x0A // TX RF output power
|
||||
SubLevelMic = 0x0B // mic gain
|
||||
|
||||
@@ -94,7 +104,11 @@ const (
|
||||
SubSwNB = 0x22 // noise blanker on/off
|
||||
SubSwNR = 0x40 // noise reduction on/off
|
||||
SubSwANF = 0x41 // auto-notch on/off
|
||||
SubSwComp = 0x44 // speech compressor on/off
|
||||
SubSwMon = 0x45 // monitor on/off
|
||||
SubSwVOX = 0x46 // VOX on/off
|
||||
SubSwBreakIn = 0x47 // CW break-in: 0=OFF, 1=SEMI, 2=FULL (needed so 0x17 CW keys TX)
|
||||
SubSwMN = 0x48 // manual notch on/off
|
||||
)
|
||||
|
||||
// CW break-in modes (CmdSwitch 0x47).
|
||||
|
||||
+313
-105
@@ -26,6 +26,7 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -53,11 +54,31 @@ func NewIcomNet(host, user, pass string, civAddr int, digitalDefault string) *Ic
|
||||
if strings.TrimSpace(host) == "" {
|
||||
return nil, fmt.Errorf("no rig host configured")
|
||||
}
|
||||
return dialIcomNet(host, user, pass, "OpsLog", b.rigAddr)
|
||||
b.dialMu.Lock()
|
||||
cancel := b.dialCancel
|
||||
b.dialMu.Unlock()
|
||||
return dialIcomNet(host, user, pass, "OpsLog", b.rigAddr, cancel)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// errDialCanceled is returned by dialIcomNet when Interrupt() aborts the dial
|
||||
// (Stop/Start). The Manager treats it like any connect error and simply stops.
|
||||
var errDialCanceled = fmt.Errorf("dial canceled")
|
||||
|
||||
// icnCanceled reports whether the dial has been asked to abort.
|
||||
func icnCanceled(cancel <-chan struct{}) bool {
|
||||
if cancel == nil {
|
||||
return false
|
||||
}
|
||||
select {
|
||||
case <-cancel:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// icomNet is the connected network transport. It satisfies civTransport.
|
||||
type icomNet struct {
|
||||
ctrl *net.UDPConn // control stream (50001)
|
||||
@@ -71,7 +92,9 @@ type icomNet struct {
|
||||
vTracked uint16
|
||||
vCivSeq uint16
|
||||
|
||||
rx chan []byte // CI-V byte chunks from civPump → Read
|
||||
rx chan []byte // CI-V byte chunks from civPump → Read (control replies)
|
||||
scopeRx chan []byte // scope (0x27) frames, kept off rx so the panadapter
|
||||
// stream can't crowd control replies out (→ ScopeChan)
|
||||
leftover []byte // partial chunk not yet returned by Read (Read-only)
|
||||
readTO time.Duration // Read timeout (SetReadTimeout)
|
||||
|
||||
@@ -81,14 +104,77 @@ type icomNet struct {
|
||||
sentMu sync.Mutex
|
||||
sentBuf map[uint16][]byte
|
||||
|
||||
// Control-stream auth state, carried out of dial so ctrlPump can RENEW the
|
||||
// login token every ~45 s. The rig invalidates the session ~2 min after login
|
||||
// without renewal (this was the "loses control after 2 min" drop — RS-BA1/the
|
||||
// Remote Utility renew too). Owned solely by ctrlPump after dial → no lock.
|
||||
cTracked uint16 // control-stream tracked seq (continues after dial)
|
||||
cAuthSeq uint16 // token-packet innerseq
|
||||
cToken uint32 // login token (opaque, echoed back verbatim)
|
||||
cTokReq uint16 // token-request id (echoed)
|
||||
cSentBuf map[uint16][]byte // control-stream retransmit buffer (token renewals)
|
||||
|
||||
// Receive-side retransmit (CI-V stream): track the rig's data-packet send seq
|
||||
// and ask it to resend any gap. Under the scope stream, UDP drops are common;
|
||||
// without recovering them the gaps accumulate and the rig drops the WHOLE
|
||||
// session after ~20 s (RS-BA1/wfview request retransmits, which is why they
|
||||
// stay up with the panadapter on). Owned solely by civPump → no lock.
|
||||
rxHaveSeq bool
|
||||
rxLastSeq uint16
|
||||
rxMissing map[uint16]int
|
||||
|
||||
// lastRx is the UnixNano of the last packet received from the rig (any type),
|
||||
// updated by both pumps. The rig's network server answers pings/idles even
|
||||
// when the RADIO is in standby, so this tracks the CONTROL-LINK liveness
|
||||
// independently of whether CI-V is replying — letting ReadState tell "rig off
|
||||
// but link fine" (stay connected) from "link dead" (reconnect). See Alive().
|
||||
lastRx atomic.Int64
|
||||
|
||||
done chan struct{}
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
// ScopeChan exposes the raw scope (0x27) CI-V frames for the scope feeder.
|
||||
// Satisfies scopeTransport in icomserial.go.
|
||||
func (n *icomNet) ScopeChan() <-chan []byte { return n.scopeRx }
|
||||
|
||||
// icnEnqueueDrop pushes onto a bounded channel, discarding the oldest entry when
|
||||
// full — a lagging consumer never blocks the producer (used for the scope stream,
|
||||
// where only the latest sweep matters).
|
||||
func icnEnqueueDrop(ch chan []byte, v []byte) {
|
||||
select {
|
||||
case ch <- v:
|
||||
default:
|
||||
select {
|
||||
case <-ch:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case ch <- v:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *icomNet) SetReadTimeout(d time.Duration) error { n.readTO = d; return nil }
|
||||
func (n *icomNet) SetDTR(bool) error { return nil } // n/a on the network
|
||||
func (n *icomNet) SetRTS(bool) error { return nil }
|
||||
|
||||
// markRx records that a packet just arrived from the rig (control-link liveness).
|
||||
func (n *icomNet) markRx() { n.lastRx.Store(time.Now().UnixNano()) }
|
||||
|
||||
// Alive reports whether the rig's network server is still talking to us. The rig
|
||||
// pings/idles continuously (even in standby), so a gap means the link — not just
|
||||
// the radio — is gone. Independent of CI-V replies, so a powered-off rig still
|
||||
// reads as Alive and the session isn't torn down. Satisfies aliveTransport.
|
||||
func (n *icomNet) Alive() bool {
|
||||
last := n.lastRx.Load()
|
||||
if last == 0 {
|
||||
return true // just connected, nothing received yet — give it a chance
|
||||
}
|
||||
return time.Since(time.Unix(0, last)) < 6*time.Second
|
||||
}
|
||||
|
||||
// Read returns tunnelled CI-V bytes, mimicking a serial port: (0,nil) on
|
||||
// timeout, (n,nil) with data, (0,err) when the link is closed.
|
||||
func (n *icomNet) Read(p []byte) (int, error) {
|
||||
@@ -129,7 +215,8 @@ func (n *icomNet) Write(p []byte) (int, error) {
|
||||
n.vCivSeq++
|
||||
n.sentMu.Lock()
|
||||
n.sentBuf[seq] = pkt
|
||||
delete(n.sentBuf, seq-256) // keep the buffer bounded (~last 256 packets)
|
||||
delete(n.sentBuf, seq-1024) // keep the buffer bounded (~last 1024 packets) so
|
||||
// the rig's retransmit requests still hit even under sustained CW + poll load
|
||||
n.sentMu.Unlock()
|
||||
if _, err := n.civ.Write(pkt); err != nil {
|
||||
return 0, err
|
||||
@@ -137,28 +224,43 @@ func (n *icomNet) Write(p []byte) (int, error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// icnTrace toggles verbose CI-V request/reply logging for diagnosing the
|
||||
// network transport (temporary).
|
||||
var icnTrace = true
|
||||
// icnTrace toggles verbose per-frame CI-V request/reply logging for diagnosing
|
||||
// the network transport. Off by default (the connect-step logs stay); flip to
|
||||
// true to trace every TX/RX again.
|
||||
var icnTrace = false
|
||||
|
||||
func (n *icomNet) Close() error {
|
||||
n.closeOnce.Do(func() {
|
||||
close(n.done)
|
||||
// Best-effort clean teardown.
|
||||
_, _ = n.civ.Write(icnOpenClose(n.vTracked, n.vID, n.vRemote, n.vCivSeq, 0x00)) // close
|
||||
_, _ = n.civ.Write(icnCtrl(0x05, 0, n.vID, n.vRemote)) // disconnect
|
||||
_, _ = n.ctrl.Write(icnCtrl(0x05, 0, n.cID, n.cRemote))
|
||||
// Tell the rig we're leaving so it frees its SINGLE control session at
|
||||
// once. If it never gets a disconnect it holds the session for minutes and
|
||||
// refuses every new login — which is why a lost link (or a hard app exit)
|
||||
// left the rig un-reconnectable, even from the Icom Remote Utility. UDP is
|
||||
// lossy, so send openClose(close) + disconnect on both streams a few times.
|
||||
// The whole teardown is bounded to ~90 ms so it never stalls the caller
|
||||
// (Settings "Save & Close" / a reconnect's Disconnect).
|
||||
for i := 0; i < 3; i++ {
|
||||
_, _ = n.civ.Write(icnOpenClose(n.vTracked, n.vID, n.vRemote, n.vCivSeq, 0x00)) // close CI-V
|
||||
_, _ = n.civ.Write(icnCtrl(0x05, 0, n.vID, n.vRemote)) // disconnect civ
|
||||
_, _ = n.ctrl.Write(icnCtrl(0x05, 0, n.cID, n.cRemote)) // disconnect ctrl
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
debugLog.Printf("icom net: sent disconnect to rig (session released)")
|
||||
_ = n.civ.Close()
|
||||
_ = n.ctrl.Close()
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// ctrlPump keeps the control stream (50001) alive: replies to the rig's pings
|
||||
// and sends idle keepalives. Its own goroutine so it never throttles civPump.
|
||||
// ctrlPump keeps the control stream (50001) alive: replies to the rig's pings,
|
||||
// sends idle keepalives, RENEWS the login token every ~45 s (without this the rig
|
||||
// invalidates the session after ~2 min → total loss of control), and answers the
|
||||
// rig's retransmit requests for those tracked control packets. Its own goroutine
|
||||
// so it never throttles civPump.
|
||||
func (n *icomNet) ctrlPump() {
|
||||
buf := make([]byte, 4096)
|
||||
lastIdle := time.Now()
|
||||
lastToken := time.Now() // token was just granted during dial
|
||||
for {
|
||||
select {
|
||||
case <-n.done:
|
||||
@@ -167,19 +269,49 @@ func (n *icomNet) ctrlPump() {
|
||||
}
|
||||
_ = n.ctrl.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||
if k, err := n.ctrl.Read(buf); err == nil && k >= 16 {
|
||||
n.markRx()
|
||||
switch icnLE.Uint16(buf[4:]) {
|
||||
case 0x07: // ping
|
||||
_, _ = n.ctrl.Write(icnPingReply(buf[:k], n.cID, n.cRemote))
|
||||
case 0x01: // retransmit request
|
||||
case 0x01: // retransmit request — resend from the CONTROL sent-buffer
|
||||
if k >= 8 {
|
||||
n.resend(icnLE.Uint16(buf[6:]))
|
||||
n.ctrlResend(icnLE.Uint16(buf[6:]))
|
||||
}
|
||||
case 0x05: // rig-initiated disconnect — it dropped US
|
||||
debugLog.Printf("icom net: rig sent DISCONNECT on control stream — session dropped by the rig")
|
||||
}
|
||||
}
|
||||
if time.Since(lastIdle) > 150*time.Millisecond {
|
||||
if time.Since(lastIdle) > 100*time.Millisecond {
|
||||
_, _ = n.ctrl.Write(icnCtrl(0x00, 0, n.cID, n.cRemote))
|
||||
lastIdle = time.Now()
|
||||
}
|
||||
if time.Since(lastToken) > 45*time.Second {
|
||||
n.renewToken()
|
||||
lastToken = time.Now()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// renewToken re-authorizes the session (control 0x40 token packet, requesttype
|
||||
// 0x05). Tracked so a lost renewal can be retransmitted. Runs only on ctrlPump,
|
||||
// the sole owner of the control-stream auth state, so no locking is needed.
|
||||
func (n *icomNet) renewToken() {
|
||||
seq := n.cTracked
|
||||
pkt := icnTokenRenew(seq, n.cAuthSeq, n.cTokReq, n.cID, n.cRemote, n.cToken)
|
||||
n.cTracked++
|
||||
n.cAuthSeq++
|
||||
n.cSentBuf[seq] = pkt
|
||||
delete(n.cSentBuf, seq-256)
|
||||
_, _ = n.ctrl.Write(pkt)
|
||||
debugLog.Printf("icom net: token renewed (seq %d)", seq)
|
||||
}
|
||||
|
||||
// ctrlResend answers a control-stream retransmit request from the control
|
||||
// sent-buffer (token renewals). Separate from resend(), which owns the CI-V
|
||||
// buffer — the two streams have independent sequence spaces.
|
||||
func (n *icomNet) ctrlResend(seq uint16) {
|
||||
if pkt := n.cSentBuf[seq]; pkt != nil {
|
||||
_, _ = n.ctrl.Write(pkt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,6 +322,7 @@ func (n *icomNet) ctrlPump() {
|
||||
func (n *icomNet) civPump() {
|
||||
buf := make([]byte, 8192)
|
||||
lastIdle := time.Now()
|
||||
lastReq := time.Now()
|
||||
for {
|
||||
select {
|
||||
case <-n.done:
|
||||
@@ -198,6 +331,7 @@ func (n *icomNet) civPump() {
|
||||
}
|
||||
_ = n.civ.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||
if k, err := n.civ.Read(buf); err == nil && k >= 16 {
|
||||
n.markRx()
|
||||
switch typ := icnLE.Uint16(buf[4:]); {
|
||||
case typ == 0x07: // ping
|
||||
_, _ = n.civ.Write(icnPingReply(buf[:k], n.vID, n.vRemote))
|
||||
@@ -205,29 +339,36 @@ func (n *icomNet) civPump() {
|
||||
if k >= 8 {
|
||||
n.resend(icnLE.Uint16(buf[6:]))
|
||||
}
|
||||
case typ == 0x05: // rig-initiated disconnect — it dropped US
|
||||
debugLog.Printf("icom net: rig sent DISCONNECT on CI-V stream — session dropped by the rig")
|
||||
case typ == 0x00 && k > 0x15 && buf[0x10] == 0xc1: // CI-V data
|
||||
n.trackRxSeq(icnLE.Uint16(buf[6:])) // note gaps for retransmit
|
||||
civBytes := buf[0x15:k]
|
||||
// Skip scope (0x27) frames: over the network the rig streams the
|
||||
// panadapter continuously as large frames that would crowd control
|
||||
// replies out of rx. (Network scope is handled separately.)
|
||||
if !(len(civBytes) >= 5 && civBytes[4] == 0x27) {
|
||||
if icnTrace {
|
||||
debugLog.Printf("icom net RX: % X", civBytes)
|
||||
cp := append([]byte(nil), civBytes...)
|
||||
// Scope (0x27) frames go to their OWN channel: the panadapter streams
|
||||
// continuously as large frames and would otherwise crowd control
|
||||
// replies out of rx (every command would then time out). The scope
|
||||
// feeder in IcomSerial picks them up. Everything else is a control
|
||||
// reply → rx → Read.
|
||||
if len(civBytes) >= 5 && civBytes[4] == 0x27 {
|
||||
icnEnqueueDrop(n.scopeRx, cp)
|
||||
break
|
||||
}
|
||||
if icnTrace {
|
||||
debugLog.Printf("icom net RX: % X", civBytes)
|
||||
}
|
||||
select {
|
||||
case n.rx <- cp:
|
||||
case <-n.done:
|
||||
return
|
||||
default: // buffer full — drop oldest, enqueue newest
|
||||
select {
|
||||
case <-n.rx:
|
||||
default:
|
||||
}
|
||||
cp := append([]byte(nil), civBytes...)
|
||||
select {
|
||||
case n.rx <- cp:
|
||||
case <-n.done:
|
||||
return
|
||||
default: // buffer full — drop oldest, enqueue newest
|
||||
select {
|
||||
case <-n.rx:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case n.rx <- cp:
|
||||
default:
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -236,6 +377,82 @@ func (n *icomNet) civPump() {
|
||||
_, _ = n.civ.Write(icnCtrl(0x00, 0, n.vID, n.vRemote))
|
||||
lastIdle = time.Now()
|
||||
}
|
||||
if time.Since(lastReq) > 100*time.Millisecond {
|
||||
n.sendRetransmitReq()
|
||||
lastReq = time.Now()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// icnMaxMissing caps the outstanding retransmit backlog; a bigger jump is treated
|
||||
// as a wrap/desync and the tracker resets rather than requesting a storm.
|
||||
const icnMaxMissing = 50
|
||||
|
||||
// trackRxSeq records the rig's data-packet send seq (outer seq @0x06) and flags
|
||||
// any forward gap as missing so sendRetransmitReq can ask for it. Handles uint16
|
||||
// wrap via the signed distance; ignores duplicates and already-seen packets.
|
||||
func (n *icomNet) trackRxSeq(seq uint16) {
|
||||
if !n.rxHaveSeq {
|
||||
n.rxHaveSeq = true
|
||||
n.rxLastSeq = seq
|
||||
return
|
||||
}
|
||||
switch d := int16(seq - n.rxLastSeq); {
|
||||
case d == 0: // duplicate
|
||||
case d < 0: // an older seq arrived — a retransmit we were missing
|
||||
delete(n.rxMissing, seq)
|
||||
case d == 1: // in order
|
||||
n.rxLastSeq = seq
|
||||
case int(d) <= icnMaxMissing: // forward gap — mark the in-between seqs missing
|
||||
for f := n.rxLastSeq + 1; f != seq; f++ {
|
||||
n.rxMissing[f] = 0
|
||||
}
|
||||
n.rxLastSeq = seq
|
||||
default: // huge jump (wrap/desync) — reset to avoid a false retransmit storm
|
||||
n.rxMissing = make(map[uint16]int)
|
||||
n.rxLastSeq = seq
|
||||
}
|
||||
}
|
||||
|
||||
// sendRetransmitReq asks the rig to resend any CI-V data packets we detected as
|
||||
// missing. Each seq is requested up to 4 times then dropped. Mirrors the Remote
|
||||
// Utility/wfview format: a single miss = a 16-byte control (type 0x01, seq set);
|
||||
// several = a control header + a list of [lo hi lo hi] per seq.
|
||||
func (n *icomNet) sendRetransmitReq() {
|
||||
if len(n.rxMissing) == 0 {
|
||||
return
|
||||
}
|
||||
if len(n.rxMissing) > icnMaxMissing {
|
||||
n.rxMissing = make(map[uint16]int) // hopelessly behind — flush and move on
|
||||
return
|
||||
}
|
||||
var seqs []uint16
|
||||
for s, cnt := range n.rxMissing {
|
||||
if cnt >= 4 {
|
||||
delete(n.rxMissing, s)
|
||||
continue
|
||||
}
|
||||
n.rxMissing[s] = cnt + 1
|
||||
seqs = append(seqs, s)
|
||||
}
|
||||
switch {
|
||||
case len(seqs) == 0:
|
||||
return
|
||||
case len(seqs) == 1:
|
||||
_, _ = n.civ.Write(icnCtrl(0x01, seqs[0], n.vID, n.vRemote))
|
||||
default:
|
||||
b := make([]byte, 16+4*len(seqs))
|
||||
icnLE.PutUint32(b[0:], uint32(len(b)))
|
||||
icnLE.PutUint16(b[4:], 0x01) // type = retransmit request
|
||||
icnLE.PutUint32(b[8:], n.vID)
|
||||
icnLE.PutUint32(b[12:], n.vRemote)
|
||||
off := 16
|
||||
for _, s := range seqs {
|
||||
icnLE.PutUint16(b[off:], s)
|
||||
icnLE.PutUint16(b[off+2:], s)
|
||||
off += 4
|
||||
}
|
||||
_, _ = n.civ.Write(b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,12 +465,17 @@ func (n *icomNet) resend(seq uint16) {
|
||||
n.sentMu.Unlock()
|
||||
if pkt != nil {
|
||||
_, _ = n.civ.Write(pkt)
|
||||
} else {
|
||||
// The rig asked for a packet we've already evicted (>256 sent since). It
|
||||
// can't fill its gap → it eventually drops the session. If this shows up in
|
||||
// the log around a disconnect, the send buffer is too small for the load.
|
||||
debugLog.Printf("icom net: retransmit MISS for seq %d (already evicted)", seq)
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------- connect -------------------------
|
||||
|
||||
func dialIcomNet(host, user, pass, compName string, rigAddr byte) (*icomNet, error) {
|
||||
func dialIcomNet(host, user, pass, compName string, rigAddr byte, cancel <-chan struct{}) (*icomNet, error) {
|
||||
debugLog.Printf("icom net: connecting to %s (user %q, comp %q, rig addr 0x%02X)", host, user, compName, rigAddr)
|
||||
// ---- control stream (50001): handshake → login → token → conninfo ----
|
||||
craddr, err := net.ResolveUDPAddr("udp4", net.JoinHostPort(host, "50001"))
|
||||
@@ -265,7 +487,7 @@ func dialIcomNet(host, user, pass, compName string, rigAddr byte) (*icomNet, err
|
||||
return nil, fmt.Errorf("dial control: %w", err)
|
||||
}
|
||||
cID := icnLocalID(ctrl)
|
||||
cRemote, err := icnHandshake(ctrl, cID)
|
||||
cRemote, err := icnHandshake(ctrl, cID, cancel)
|
||||
if err != nil {
|
||||
_ = ctrl.Close()
|
||||
debugLog.Printf("icom net: control handshake FAILED (rig unreachable at %s:50001?): %v", host, err)
|
||||
@@ -283,6 +505,10 @@ func dialIcomNet(host, user, pass, compName string, rigAddr byte) (*icomNet, err
|
||||
buf := make([]byte, 2048)
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for token == 0 && time.Now().Before(deadline) {
|
||||
if icnCanceled(cancel) {
|
||||
_ = ctrl.Close()
|
||||
return nil, errDialCanceled
|
||||
}
|
||||
p, ok := icnRecv(ctrl, 200, buf)
|
||||
if !ok {
|
||||
continue
|
||||
@@ -314,6 +540,10 @@ func dialIcomNet(host, user, pass, compName string, rigAddr byte) (*icomNet, err
|
||||
var rigMAC []byte
|
||||
macEnd := time.Now().Add(1200 * time.Millisecond)
|
||||
for time.Now().Before(macEnd) {
|
||||
if icnCanceled(cancel) {
|
||||
_ = ctrl.Close()
|
||||
return nil, errDialCanceled
|
||||
}
|
||||
p, ok := icnRecv(ctrl, 150, buf)
|
||||
if !ok {
|
||||
continue
|
||||
@@ -337,6 +567,10 @@ func dialIcomNet(host, user, pass, compName string, rigAddr byte) (*icomNet, err
|
||||
cInner++
|
||||
drainEnd := time.Now().Add(500 * time.Millisecond)
|
||||
for time.Now().Before(drainEnd) {
|
||||
if icnCanceled(cancel) {
|
||||
_ = ctrl.Close()
|
||||
return nil, errDialCanceled
|
||||
}
|
||||
if p, ok := icnRecv(ctrl, 100, buf); ok && icnLE.Uint16(p[4:]) == 0x07 {
|
||||
_, _ = ctrl.Write(icnPingReply(p, cID, cRemote))
|
||||
}
|
||||
@@ -356,14 +590,14 @@ func dialIcomNet(host, user, pass, compName string, rigAddr byte) (*icomNet, err
|
||||
return nil, fmt.Errorf("dial CI-V (local :50002 — is the Icom Remote Utility still running?): %w", err)
|
||||
}
|
||||
vID := icnLocalID(civ)
|
||||
vRemote, err := icnHandshake(civ, vID)
|
||||
vRemote, err := icnHandshake(civ, vID, cancel)
|
||||
if err != nil {
|
||||
_ = civ.Close()
|
||||
_ = ctrl.Close()
|
||||
debugLog.Printf("icom net: CI-V handshake FAILED: %v", err)
|
||||
return nil, fmt.Errorf("CI-V handshake: %w", err)
|
||||
}
|
||||
debugLog.Printf("icom net: CI-V link up — sending power-on, waiting for the rig to boot (~15s)")
|
||||
debugLog.Printf("icom net: CI-V link up — opening the CI-V data flow (rig power left to the ON button)")
|
||||
|
||||
// Bigger receive buffers so a burst of scope/CI-V packets doesn't overflow
|
||||
// (dropped packets → the rig's retransmit requests → session drop).
|
||||
@@ -374,93 +608,46 @@ func dialIcomNet(host, user, pass, compName string, rigAddr byte) (*icomNet, err
|
||||
ctrl: ctrl, civ: civ,
|
||||
cID: cID, cRemote: cRemote, vID: vID, vRemote: vRemote,
|
||||
vTracked: 1, vCivSeq: 1,
|
||||
rx: make(chan []byte, 256),
|
||||
sentBuf: make(map[uint16][]byte),
|
||||
done: make(chan struct{}),
|
||||
rx: make(chan []byte, 256),
|
||||
scopeRx: make(chan []byte, 8),
|
||||
sentBuf: make(map[uint16][]byte),
|
||||
rxMissing: make(map[uint16]int),
|
||||
done: make(chan struct{}),
|
||||
// Auth state for periodic token renewal (see ctrlPump). cTracked/cAuthSeq
|
||||
// continue the control-stream sequences from where the dial's login/token/
|
||||
// conninfo left off.
|
||||
cTracked: cTracked, cAuthSeq: cInner,
|
||||
cToken: token, cTokReq: tokReq,
|
||||
cSentBuf: make(map[uint16][]byte),
|
||||
}
|
||||
// openClose(open) starts the CI-V data flow.
|
||||
n.markRx() // the successful handshake counts as initial rig activity
|
||||
// openClose(open) starts the CI-V data flow. We intentionally DO NOT power the
|
||||
// rig on here — that's a manual ON button now (the user asked not to wake the
|
||||
// rig at launch). If the rig is in standby the control/CI-V streams still stay
|
||||
// up and Alive() stays true (the rig's server answers pings even when the radio
|
||||
// is off), so the session doesn't flap; CI-V just stays silent until ON.
|
||||
ocPkt := icnOpenClose(n.vTracked, vID, vRemote, n.vCivSeq, 0x04)
|
||||
n.sentBuf[n.vTracked] = ocPkt
|
||||
_, _ = civ.Write(ocPkt)
|
||||
n.vTracked++
|
||||
n.vCivSeq++
|
||||
// Power-on (the rig may be in standby — it NG's every command until on):
|
||||
// an FE wake preamble then FE FE <rig> E0 18 01 FD. Harmless if already on.
|
||||
po := make([]byte, 0, 32)
|
||||
for i := 0; i < 25; i++ {
|
||||
po = append(po, 0xFE)
|
||||
}
|
||||
po = append(po, 0xFE, 0xFE, rigAddr, 0xE0, 0x18, 0x01, 0xFD)
|
||||
poPkt := icnCivData(n.vTracked, vID, vRemote, n.vCivSeq, po)
|
||||
n.sentBuf[n.vTracked] = poPkt
|
||||
_, _ = civ.Write(poPkt)
|
||||
n.vTracked++
|
||||
n.vCivSeq++
|
||||
|
||||
// Wait for the rig to finish booting: a rig woken from standby NG's/ignores
|
||||
// commands for ~10-15 s. Poll read-freq (keeping both streams alive) until it
|
||||
// answers, so Connect only returns a READY link — otherwise the manager's
|
||||
// read-timeouts would flap the connection during boot. Give up after 25 s and
|
||||
// return anyway (the rig may already be on and just quiet).
|
||||
if n.waitReady(rigAddr, 25*time.Second) {
|
||||
debugLog.Printf("icom net: rig is READY (answered read-freq) — connection up ✓")
|
||||
} else {
|
||||
debugLog.Printf("icom net: boot wait timed out (25s, no freq reply) — proceeding anyway")
|
||||
}
|
||||
|
||||
go n.ctrlPump()
|
||||
go n.civPump()
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// waitReady polls read-freq until the rig replies (booted) or timeout, replying
|
||||
// to pings and sending idle keepalives so the session stays up. Runs before the
|
||||
// pump goroutine starts, so it owns the socket reads.
|
||||
func (n *icomNet) waitReady(rigAddr byte, timeout time.Duration) bool {
|
||||
cbuf := make([]byte, 4096)
|
||||
vbuf := make([]byte, 4096)
|
||||
readFreq := []byte{0xFE, 0xFE, rigAddr, 0xE0, 0x03, 0xFD}
|
||||
end := time.Now().Add(timeout)
|
||||
var lastPoll, lastIdle time.Time
|
||||
for time.Now().Before(end) {
|
||||
if p, ok := icnRecv(n.ctrl, 25, cbuf); ok && icnLE.Uint16(p[4:]) == 0x07 {
|
||||
_, _ = n.ctrl.Write(icnPingReply(p, n.cID, n.cRemote))
|
||||
}
|
||||
if p, ok := icnRecv(n.civ, 25, vbuf); ok {
|
||||
typ := icnLE.Uint16(p[4:])
|
||||
if typ == 0x07 {
|
||||
_, _ = n.civ.Write(icnPingReply(p, n.vID, n.vRemote))
|
||||
} else if typ == 0x00 && len(p) > 0x15 && p[0x10] == 0xc1 {
|
||||
f := p[0x15:]
|
||||
// A frequency reply from the rig: FE FE E0 <rig> 03 …
|
||||
if len(f) >= 6 && f[0] == 0xFE && f[1] == 0xFE && f[3] == rigAddr && f[4] == 0x03 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if time.Since(lastIdle) > 180*time.Millisecond {
|
||||
_, _ = n.ctrl.Write(icnCtrl(0x00, 0, n.cID, n.cRemote))
|
||||
_, _ = n.civ.Write(icnCtrl(0x00, 0, n.vID, n.vRemote))
|
||||
lastIdle = time.Now()
|
||||
}
|
||||
if time.Since(lastPoll) > 1000*time.Millisecond {
|
||||
_, _ = n.civ.Write(icnCivData(n.vTracked, n.vID, n.vRemote, n.vCivSeq, readFreq))
|
||||
n.vTracked++
|
||||
n.vCivSeq++
|
||||
lastPoll = time.Now()
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// icnHandshake: areYouThere(seq0) → iAmHere → areYouReady(seq1) → iAmReady.
|
||||
func icnHandshake(c *net.UDPConn, myID uint32) (uint32, error) {
|
||||
func icnHandshake(c *net.UDPConn, myID uint32, cancel <-chan struct{}) (uint32, error) {
|
||||
buf := make([]byte, 2048)
|
||||
_, _ = c.Write(icnCtrl(0x03, 0, myID, 0))
|
||||
var remoteID uint32
|
||||
deadline := time.Now().Add(4 * time.Second)
|
||||
lastTry := time.Now()
|
||||
for time.Now().Before(deadline) {
|
||||
if icnCanceled(cancel) {
|
||||
return 0, errDialCanceled
|
||||
}
|
||||
p, ok := icnRecv(c, 200, buf)
|
||||
if !ok {
|
||||
if remoteID == 0 && time.Since(lastTry) > 500*time.Millisecond {
|
||||
@@ -564,6 +751,27 @@ func icnToken(seq, innerSeq, tokReq uint16, sentid, rcvdid, token uint32) []byte
|
||||
return b
|
||||
}
|
||||
|
||||
// icnTokenRenew builds the periodic token-renewal packet (control 0x40). Same as
|
||||
// the login-time token confirm but requesttype 0x05 (renew) with the resetcap
|
||||
// field (0x0798 BE @0x24) the Remote Utility sends on renewals. Keeps the rig
|
||||
// from invalidating the session (~2-min timeout without renewal). Offsets per the
|
||||
// wfview token_packet struct (verified) — protocol facts, not copied code.
|
||||
func icnTokenRenew(seq, innerSeq, tokReq uint16, sentid, rcvdid, token uint32) []byte {
|
||||
b := make([]byte, 0x40)
|
||||
icnLE.PutUint32(b[0:], 0x40)
|
||||
icnLE.PutUint16(b[6:], seq)
|
||||
icnLE.PutUint32(b[8:], sentid)
|
||||
icnLE.PutUint32(b[12:], rcvdid)
|
||||
icnBE.PutUint32(b[0x10:], 0x40-0x10)
|
||||
b[0x14] = 0x01 // requestreply = request
|
||||
b[0x15] = 0x05 // requesttype = token renewal
|
||||
icnBE.PutUint16(b[0x16:], innerSeq)
|
||||
icnLE.PutUint16(b[0x1a:], tokReq)
|
||||
icnLE.PutUint32(b[0x1c:], token)
|
||||
icnBE.PutUint16(b[0x24:], 0x0798) // resetcap
|
||||
return b
|
||||
}
|
||||
|
||||
func icnConnInfo(seq, innerSeq, tokReq uint16, sentid, rcvdid, token uint32, user string, rigMAC []byte, civPort, audioPort uint16) []byte {
|
||||
b := make([]byte, 0x90)
|
||||
icnLE.PutUint32(b[0:], 0x90)
|
||||
|
||||
+358
-6
@@ -27,6 +27,26 @@ type civTransport interface {
|
||||
SetRTS(bool) error
|
||||
}
|
||||
|
||||
// aliveTransport is an OPTIONAL transport capability: report whether the link is
|
||||
// still up independently of whether the rig answers CI-V. The network transport
|
||||
// implements it (the rig's server pings even in standby), letting ReadState keep
|
||||
// the session "connected but rig off" instead of tearing it down and flapping.
|
||||
// USB doesn't implement it (no such out-of-band signal), so it keeps the bounded
|
||||
// read-failure tolerance instead.
|
||||
type aliveTransport interface {
|
||||
Alive() bool
|
||||
}
|
||||
|
||||
// scopeTransport is an OPTIONAL transport capability: deliver spectrum-scope
|
||||
// (0x27) frames on a SEPARATE channel from control replies. The network transport
|
||||
// implements it so the continuous panadapter stream can't crowd control replies
|
||||
// out of the main Read path (which made every command time out with the scope
|
||||
// on). USB doesn't implement it — there the scope frames ride the normal Read
|
||||
// path and the reader splits them off to specCh.
|
||||
type scopeTransport interface {
|
||||
ScopeChan() <-chan []byte
|
||||
}
|
||||
|
||||
// IcomSerial controls an Icom transceiver over the shared civ protocol. The
|
||||
// transport is pluggable via `open`: NewIcomSerial opens a USB/serial port;
|
||||
// NewIcomNet (later) returns one configured with a network transport. Implements
|
||||
@@ -75,6 +95,8 @@ type IcomSerial struct {
|
||||
splitOn bool // last read split state (refreshed every few cycles)
|
||||
splitTXFreq int64 // last read unselected/TX VFO freq while in split
|
||||
readFails int // consecutive ReadState freq-read failures (transient tolerance)
|
||||
dspLoaded bool // readDSP has run since the rig became responsive (loads all
|
||||
// the panel's set-once controls once the rig actually answers)
|
||||
lastSetFreq int64 // last frequency commanded (spot click: freq then mode)
|
||||
lastSetFreqAt time.Time
|
||||
|
||||
@@ -83,6 +105,29 @@ type IcomSerial struct {
|
||||
// / setters) — hence the mutex.
|
||||
dspMu sync.Mutex
|
||||
dsp IcomTXState
|
||||
|
||||
// dialCancel is closed by Interrupt() to abort an in-progress network dial
|
||||
// (icomnet's handshake/login/boot-wait can block ~tens of seconds). A fresh
|
||||
// channel is made by each Connect. Guarded by dialMu: written on the CAT
|
||||
// goroutine, closed from the goroutine calling Stop.
|
||||
dialMu sync.Mutex
|
||||
dialCancel chan struct{}
|
||||
}
|
||||
|
||||
// Interrupt aborts an in-progress network Connect so Stop()/Start() don't block
|
||||
// on a slow UDP handshake (or the 25 s boot-from-standby wait). Safe to call at
|
||||
// any time and from another goroutine; harmless when no dial is in progress and
|
||||
// a no-op for the USB transport (which dials instantly).
|
||||
func (b *IcomSerial) Interrupt() {
|
||||
b.dialMu.Lock()
|
||||
if b.dialCancel != nil {
|
||||
select {
|
||||
case <-b.dialCancel: // already closed
|
||||
default:
|
||||
close(b.dialCancel)
|
||||
}
|
||||
}
|
||||
b.dialMu.Unlock()
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -130,6 +175,11 @@ func (b *IcomSerial) Connect() error {
|
||||
if b.open == nil {
|
||||
return fmt.Errorf("no transport configured")
|
||||
}
|
||||
// Fresh cancel channel for this dial so Interrupt() (called by Stop) can abort
|
||||
// a slow network handshake instead of freezing the UI.
|
||||
b.dialMu.Lock()
|
||||
b.dialCancel = make(chan struct{})
|
||||
b.dialMu.Unlock()
|
||||
port, err := b.open()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -154,6 +204,11 @@ func (b *IcomSerial) Connect() error {
|
||||
b.readerDone = make(chan struct{})
|
||||
go b.reader(port, b.readerDone)
|
||||
go b.scopeLoop(b.specCh, b.readerDone)
|
||||
// On the network the scope frames come on their own channel (kept off the
|
||||
// control Read path); feed them into the same scope pipeline.
|
||||
if sc, ok := port.(scopeTransport); ok {
|
||||
go b.netScopeFeeder(sc.ScopeChan(), b.readerDone)
|
||||
}
|
||||
|
||||
// Best-effort model identification: ask the rig for its own CI-V address.
|
||||
if err := b.write(civ.CmdReadID, civ.SubPTT); err == nil {
|
||||
@@ -166,7 +221,11 @@ func (b *IcomSerial) Connect() error {
|
||||
// Dual-scope rigs (IC-7610/9700) prefix each waveform frame with a main/sub
|
||||
// selector byte; single-scope rigs (IC-7300…) do not.
|
||||
b.dualScope = b.rigAddr == 0x98 || b.rigAddr == 0xA2
|
||||
b.readDSP() // best-effort initial snapshot for the control tab
|
||||
// Defer the DSP snapshot until the rig actually answers CI-V. Over the network
|
||||
// the rig may still be booting (or off) at Connect, so an immediate readDSP
|
||||
// would time out and leave every control at 0 / off with no retry. ReadState
|
||||
// loads it once on the first successful freq read instead (see dspLoaded).
|
||||
b.dspLoaded = false
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -191,11 +250,38 @@ func (b *IcomSerial) ReadState() (RigState, error) {
|
||||
|
||||
hz, err := b.readFreq()
|
||||
if err != nil {
|
||||
// The rig briefly stops answering CI-V while it switches band/VFO. Treat a
|
||||
// few consecutive misses as transient — keep the connection and report the
|
||||
// last known state — so a band change doesn't trigger a full disconnect +
|
||||
// 5 s reconnect (which showed the new frequency ~10 s late). Only after
|
||||
// several failures do we declare the rig lost so the Manager reconnects.
|
||||
// Network transport: if the control link is still alive, the rig is simply
|
||||
// silent — either in standby / powered OFF (the ON button is manual now), or
|
||||
// mid band-change. Stay CONNECTED and show last-known state (empty until the
|
||||
// rig is switched on) rather than tearing the whole UDP session down and
|
||||
// flapping every few seconds. The panel stays up so the ON button works.
|
||||
if at, ok := b.port.(aliveTransport); ok {
|
||||
if at.Alive() {
|
||||
b.readFails = 0
|
||||
s.FreqHz = b.curFreq // 0 until the rig is powered on and first read
|
||||
if b.curModeByte != 0 {
|
||||
s.Mode = civ.ModeToADIF(b.curModeByte, false)
|
||||
if s.Mode == "DATA" {
|
||||
s.Mode = b.digital
|
||||
}
|
||||
}
|
||||
// Keep the Icom panel visible (so ON/OFF are reachable) but show no
|
||||
// live meters while the rig is silent.
|
||||
b.dspMu.Lock()
|
||||
b.dsp.Available = true
|
||||
b.dsp.Model = b.model
|
||||
b.dsp.Transmitting = false
|
||||
b.dsp.SMeter, b.dsp.PowerMeter, b.dsp.SWRMeter = 0, 0, 0
|
||||
b.dspMu.Unlock()
|
||||
return s, nil
|
||||
}
|
||||
return RigState{}, err // control link dead → let the Manager reconnect
|
||||
}
|
||||
// USB (no liveness signal): the rig briefly stops answering CI-V while it
|
||||
// switches band/VFO. Tolerate a few consecutive misses as transient — keep
|
||||
// the connection and report last-known state — so a band change doesn't
|
||||
// trigger a full disconnect + 5 s reconnect. Only after several failures do
|
||||
// we declare the rig lost so the Manager reconnects.
|
||||
b.readFails++
|
||||
if b.readFails <= 6 && b.curFreq > 0 {
|
||||
s.FreqHz = b.curFreq
|
||||
@@ -268,6 +354,15 @@ func (b *IcomSerial) ReadState() (RigState, error) {
|
||||
b.dsp.PowerMeter = po
|
||||
b.dsp.SWRMeter = swr
|
||||
b.dspMu.Unlock()
|
||||
|
||||
// First time the rig answers (it's booted/responsive): load the full DSP
|
||||
// snapshot so the panel's antenna, sliders, RIT, notch, etc. reflect the rig
|
||||
// instead of sitting at their zero defaults. Runs once; ↻ Refresh re-reads on
|
||||
// demand, and a reconnect re-arms it (Connect clears dspLoaded).
|
||||
if !b.dspLoaded {
|
||||
b.readDSP()
|
||||
b.dspLoaded = true
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@@ -306,6 +401,30 @@ func (b *IcomSerial) SetPTT(on bool) error {
|
||||
return b.exec(civ.CmdPTT, civ.SubPTT, state)
|
||||
}
|
||||
|
||||
// SetPower turns the transceiver on or off (CI-V 0x18). Power-ON is prefixed with
|
||||
// a run of 0xFE — the wake preamble Icom rigs need to notice a command while
|
||||
// asleep (harmless when already awake); after it the rig boots for ~10-15 s.
|
||||
// Sent raw with no ack wait, since a rig waking up or shutting down won't
|
||||
// reliably answer. On the network transport the whole buffer becomes one data
|
||||
// packet, exactly as the Remote Utility sends it. Power is manual (the app never
|
||||
// wakes the rig on connect), so this is driven by the panel's ON/OFF button.
|
||||
func (b *IcomSerial) SetPower(on bool) error {
|
||||
if b.port == nil {
|
||||
return fmt.Errorf("icom: not connected")
|
||||
}
|
||||
if on {
|
||||
buf := make([]byte, 0, 32)
|
||||
for i := 0; i < 25; i++ {
|
||||
buf = append(buf, 0xFE)
|
||||
}
|
||||
buf = append(buf, 0xFE, 0xFE, b.rigAddr, civ.AddrController, civ.CmdPower, 0x01, 0xFD)
|
||||
_, err := b.port.Write(buf)
|
||||
return err
|
||||
}
|
||||
_, err := b.port.Write(civ.Frame(b.rigAddr, civ.AddrController, civ.CmdPower, 0x00))
|
||||
return err
|
||||
}
|
||||
|
||||
// ── helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
func (b *IcomSerial) write(payload ...byte) error {
|
||||
@@ -374,6 +493,37 @@ func (b *IcomSerial) reader(port civTransport, done chan struct{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// netScopeFeeder decodes the raw scope (0x27) CI-V frames the network transport
|
||||
// delivers on its own channel and routes them into specCh — the same pipeline
|
||||
// the USB reader feeds — so scopeLoop assembles them identically. Exits when the
|
||||
// connection's reader does (done closes on Disconnect).
|
||||
func (b *IcomSerial) netScopeFeeder(ch <-chan []byte, done chan struct{}) {
|
||||
var buf []byte
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case raw, ok := <-ch:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
buf = append(buf, raw...)
|
||||
frames, consumed := civ.Scan(buf)
|
||||
if consumed > 0 {
|
||||
buf = append(buf[:0], buf[consumed:]...)
|
||||
}
|
||||
for _, f := range frames {
|
||||
if f.From == b.rigAddr && f.Cmd == civ.CmdScope {
|
||||
b.route(b.specCh, f)
|
||||
}
|
||||
}
|
||||
if len(buf) > 1<<16 { // a frame that never completes — don't grow forever
|
||||
buf = buf[:0]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// route delivers a frame without ever blocking the reader: if the channel is
|
||||
// full it drops the oldest entry to make room for the newest.
|
||||
func (b *IcomSerial) route(ch chan civ.Decoded, f civ.Decoded) {
|
||||
@@ -451,6 +601,32 @@ func (b *IcomSerial) scopeLoop(spec chan civ.Decoded, done chan struct{}) {
|
||||
if seq == 0 || tot == 0 {
|
||||
continue
|
||||
}
|
||||
if tot == 1 {
|
||||
// Network single-frame sweep: the WHOLE sweep is in one frame —
|
||||
// region = [info][low 5-BCD][high 5-BCD][amplitude bytes…]. Parse the
|
||||
// edges and take the rest as the trace, then publish immediately.
|
||||
// (USB splits this across 21 frames; the net rig sends it as one.)
|
||||
if len(region) >= 11 {
|
||||
low := civ.BCDToFreq(region[1:6])
|
||||
high := civ.BCDToFreq(region[6:11])
|
||||
amp := append([]byte(nil), region[11:]...)
|
||||
b.scopeMu.Lock()
|
||||
b.scopeLow, b.scopeHigh = low, high
|
||||
b.scopeAmp = amp
|
||||
b.scopeSeq++
|
||||
firstLog := !b.scopeSeen
|
||||
b.scopeSeen = true
|
||||
b.scopeMu.Unlock()
|
||||
if firstLog {
|
||||
head := region
|
||||
if len(head) > 24 {
|
||||
head = head[:24]
|
||||
}
|
||||
applog.Printf("icom scope (net 1-frame): region=%dB head=[% X] → edges %d..%d Hz points=%d", len(region), head, low, high, len(amp))
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if seq == 1 { // header frame — begins a new sweep, no waveform data
|
||||
regions = make(map[byte][]byte)
|
||||
total = tot
|
||||
@@ -935,6 +1111,46 @@ func (b *IcomSerial) readDSP() {
|
||||
if v, ok := b.readSwitch(civ.SubSwBreakIn); ok {
|
||||
st.BreakIn = int(v)
|
||||
}
|
||||
// Antenna + filter fine controls + TX extras.
|
||||
if v, ok := b.readAnt(); ok {
|
||||
st.Antenna = v
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelPBTIn); ok {
|
||||
st.PBTInner = from255(v)
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelPBTOut); ok {
|
||||
st.PBTOuter = from255(v)
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwMN); ok {
|
||||
st.ManualNotch = v != 0
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelNotch); ok {
|
||||
st.NotchPos = from255(v)
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelSQL); ok {
|
||||
st.Squelch = from255(v)
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwComp); ok {
|
||||
st.Comp = v != 0
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelComp); ok {
|
||||
st.CompLevel = from255(v)
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwMon); ok {
|
||||
st.Monitor = v != 0
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelMon); ok {
|
||||
st.MonLevel = from255(v)
|
||||
}
|
||||
if v, ok := b.readSwitch(civ.SubSwVOX); ok {
|
||||
st.VOX = v != 0
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelVOXGain); ok {
|
||||
st.VOXGain = from255(v)
|
||||
}
|
||||
if v, ok := b.readLevel(civ.SubLevelAntiVOX); ok {
|
||||
st.AntiVOX = from255(v)
|
||||
}
|
||||
|
||||
b.dspMu.Lock()
|
||||
b.dsp = st
|
||||
@@ -982,6 +1198,22 @@ func (b *IcomSerial) readAtt() (int, bool) {
|
||||
return civ.BCDToByte(f.Data[0]), true
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readAnt() (int, bool) {
|
||||
if err := b.write(civ.CmdAnt); err != nil {
|
||||
return 0, false
|
||||
}
|
||||
f, err := b.recv(icomDSPTimeout, func(d civ.Decoded) bool {
|
||||
return d.Cmd == civ.CmdAnt && len(d.Data) >= 1
|
||||
})
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
if f.Data[0] == 0x01 {
|
||||
return 2, true
|
||||
}
|
||||
return 1, true
|
||||
}
|
||||
|
||||
func (b *IcomSerial) readModeFilter() (mode, filter byte, ok bool) {
|
||||
if err := b.write(civ.CmdReadMode); err != nil {
|
||||
return 0, 0, false
|
||||
@@ -1123,6 +1355,126 @@ func (b *IcomSerial) SetIcomSplit(on bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ── Antenna ────────────────────────────────────────────────────────────────
|
||||
|
||||
func (b *IcomSerial) SetAntenna(n int) error {
|
||||
sub := byte(0x00) // ANT1
|
||||
if n == 2 {
|
||||
sub = 0x01 // ANT2
|
||||
}
|
||||
if err := b.exec(civ.CmdAnt, sub); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) {
|
||||
if n == 2 {
|
||||
s.Antenna = 2
|
||||
} else {
|
||||
s.Antenna = 1
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// ── Filter: Twin PBT + manual notch ────────────────────────────────────────
|
||||
|
||||
func (b *IcomSerial) SetPBTInner(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelPBTIn}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.PBTInner = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetPBTOuter(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelPBTOut}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.PBTOuter = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetManualNotch(on bool) error {
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwMN, boolByte(on)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.ManualNotch = on })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetNotchPos(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelNotch}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.NotchPos = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
// ── TX extras: squelch / compressor / monitor / VOX ────────────────────────
|
||||
|
||||
func (b *IcomSerial) SetSquelch(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelSQL}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.Squelch = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetComp(on bool) error {
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwComp, boolByte(on)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.Comp = on })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetCompLevel(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelComp}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.CompLevel = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetMonitor(on bool) error {
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwMon, boolByte(on)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.Monitor = on })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetMonLevel(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelMon}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.MonLevel = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetVOX(on bool) error {
|
||||
if err := b.exec(civ.CmdSwitch, civ.SubSwVOX, boolByte(on)); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.VOX = on })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetVOXGain(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelVOXGain}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.VOXGain = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *IcomSerial) SetAntiVOX(p int) error {
|
||||
if err := b.exec(append([]byte{civ.CmdLevel, civ.SubLevelAntiVOX}, civ.LevelToBCD(to255(p))...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
b.setCache(func(s *IcomTXState) { s.AntiVOX = clampPct(p) })
|
||||
return nil
|
||||
}
|
||||
|
||||
// TuneATU triggers a one-shot antenna-tuner tune (CI-V 0x1C 0x01 0x02).
|
||||
func (b *IcomSerial) TuneATU() error {
|
||||
return b.exec(civ.CmdATU, civ.SubATU, 0x02)
|
||||
|
||||
Reference in New Issue
Block a user