fix: Icom over LAN froze on the last frequency after WSJT-X released the rig

The network backend treated "control link alive but no CI-V reply" as the rig
being in standby, and tolerated it WITHOUT BOUND. When another program takes the
CI-V session — WSJT-X through OmniRig, or the Remote Utility — the rig goes on
answering pings on the control stream while sending us nothing at all. Alive()
stayed true, so ReadState returned the cached frequency with err == nil, the
Manager never saw a failure, never reconnected, and re-published a frozen number
with a fresh timestamp on every poll. Only restarting OpsLog cleared it.

Bounded now, on the last SUCCESSFUL read. Past the grace the error is reported so
the Manager tears the session down and reconnects, which re-takes the CI-V
stream. Also fatal immediately: the CI-V reader goroutine having exited — no read
can ever succeed after that, however healthy the control link looks.

The grace backs off to minutes when the silence persists, because the two cases
pull opposite ways: a stolen session recovers on the first attempt, while a rig
switched OFF is silent for hours and re-tearing its session every 30 s would
blink the panel — and its ON button — away continuously. A good read resets it.

The decision table is pinned by a test; the standby case (never answered since
connect) keeps the old tolerate-for-ever behaviour.
This commit is contained in:
2026-07-28 22:24:32 +02:00
parent 29591c5f0c
commit bd5f9c0746
3 changed files with 137 additions and 10 deletions
+73 -10
View File
@@ -37,6 +37,19 @@ type aliveTransport interface {
Alive() bool
}
const (
// How long the network backend accepts "the control link answers but the rig
// sends no CI-V" before declaring the session lost. Long enough to cover a
// band change or a rig booting from standby; short enough that an operator
// whose CI-V session was stolen (WSJT-X via OmniRig, the Remote Utility) gets
// a live frequency back on his own rather than restarting OpsLog.
icomSilentGrace = 30 * time.Second
// Ceiling for the backoff applied when the silence persists — a rig switched
// OFF is silent for hours, and re-tearing its session every 30 s would blink
// the panel (and its ON button) away continuously.
icomSilentGraceMax = 4 * time.Minute
)
// 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
@@ -89,13 +102,17 @@ type IcomSerial struct {
scopeFixed bool // true = fixed-span mode (tracked optimistically)
scopeSeen bool // logged the first sweep's structure once (on-rig verification)
curFreq int64 // last frequency read (for sideband choice)
curModeByte byte // last raw Icom mode byte (for filter re-send)
pollN int // ReadState cycle counter (staggers slow reads)
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
curFreq int64 // last frequency read (for sideband choice)
curModeByte byte // last raw Icom mode byte (for filter re-send)
pollN int // ReadState cycle counter (staggers slow reads)
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)
lastGoodAt time.Time // last SUCCESSFUL frequency read — bounds the network
// "alive but silent" tolerance below, which used to be
// unbounded and left the display frozen for ever
silentGrace time.Duration // current width of that tolerance (backs off, see ReadState)
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
@@ -267,7 +284,34 @@ func (b *IcomSerial) ReadState() (RigState, error) {
// 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() {
// The reader goroutine is what feeds every CI-V reply. If it has
// exited, no read can ever succeed again, however healthy the control
// link looks — that is not a silent rig, it is a dead connection.
readerGone := false
if b.readerDone != nil {
select {
case <-b.readerDone:
readerGone = true
default:
}
}
// "Alive but silent" is the rig in standby, or mid band-change. It was
// tolerated for ever, and that is the freeze: when ANOTHER program takes
// the CI-V session (WSJT-X through OmniRig, say) the rig keeps answering
// pings on the control stream while sending us no CI-V at all. Alive()
// stayed true, the cached frequency was re-published with a fresh
// timestamp on every poll, and the operator saw a confident, frozen
// number until OpsLog was restarted. Bounded now: past the grace period
// the error is reported so the Manager tears the session down and
// reconnects, which re-takes the CI-V stream.
silentFor := time.Duration(0)
if !b.lastGoodAt.IsZero() {
silentFor = time.Since(b.lastGoodAt)
}
if b.silentGrace <= 0 {
b.silentGrace = icomSilentGrace
}
if at.Alive() && !readerGone && (b.lastGoodAt.IsZero() || silentFor < b.silentGrace) {
b.readFails = 0
s.FreqHz = b.curFreq // 0 until the rig is powered on and first read
if b.curModeByte != 0 {
@@ -286,8 +330,25 @@ func (b *IcomSerial) ReadState() (RigState, error) {
b.dspMu.Unlock()
return s, nil
}
debugLog.Printf("icom net: control link went quiet (no rig packets for >6 s) → reconnecting. If this recurs every ~2-3 min, the rig is invalidating the session (token renewal rejected).")
return RigState{}, err // control link dead → let the Manager reconnect
switch {
case readerGone:
debugLog.Printf("icom net: the CI-V reader has exited — the connection is dead however alive the control link looks → reconnecting")
case at.Alive():
debugLog.Printf("icom net: control link answers but no CI-V reply for %s → reconnecting. Another program (WSJT-X/OmniRig, the Remote Utility) has most likely taken the CI-V session.", silentFor.Round(time.Second))
default:
debugLog.Printf("icom net: control link went quiet (no rig packets for >6 s) → reconnecting. If this recurs every ~2-3 min, the rig is invalidating the session (token renewal rejected).")
}
// Restart the clock, and widen the window each time it fires without a
// good read in between. A hijacked session recovers on the first shot;
// a rig simply switched OFF never will, and re-tearing its session every
// 30 s would make the panel — and its ON button — blink away
// continuously. Backing off to minutes keeps the standby case quiet
// while still recovering on its own.
b.lastGoodAt = time.Now()
if b.silentGrace < icomSilentGraceMax {
b.silentGrace *= 2
}
return RigState{}, err // 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
@@ -308,6 +369,8 @@ func (b *IcomSerial) ReadState() (RigState, error) {
return RigState{}, err
}
b.readFails = 0
b.lastGoodAt = time.Now()
b.silentGrace = icomSilentGrace // the rig answers: back to the short window
s.FreqHz = hz
b.curFreq = hz