Files
VoCat/internal/modem/wwan_at_linux_test.go
T
f949001480 fix: support non-Quectel Qualcomm modems and fix 410 dongle AT timeouts (#40)
1. Vendor-neutral modem compatibility:
   - Discovery switched from a vendor-ID whitelist to detecting the QMI
     channel directly (an interface bound to the kernel qmi_wwan driver),
     so SIMCom, Sierra, Telit and other Qualcomm-based modules are found
     automatically while MBIM-only devices stay excluded
   - AT port responses now distinguish an AT command error from firmware
     incompatibility: ERROR / +CME ERROR is returned as a normal response
     (200) instead of being folded into a 502, which only a real transport
     failure produces

2. Fixed the 410 dongle's AT command timeouts:
   - Default WWAN AT port switched from wwan0at0 to wwan0at1: ModemManager
     marks the first AT port that answers its probe as primary (at1 on the
     tested UFI dongles) and closes AT ports once initialization finishes,
     so at1 is the responsive, idle channel for vocat while MM uses the
     QMI port for control
   - Drain the WWAN input buffer before each command write, discarding the
     late bytes of a previous timed-out command so they cannot pollute the
     next response's parsing
   - AT+CGSN now uses an independent short timeout instead of inheriting
     the refresh's 30s deadline (on MHI modems it returns the IMEI line
     but never a final OK). Previously every refresh held the device lock
     for the full 30s, queueing AT terminal commands behind it for 10-20s
   - The QMI UIM ICCID fallback only runs when AT+CPIN? already proved a
     READY card, so a SIM-less slot no longer blocks refresh waiting out
     its long timeout

Tests: added WWAN drain cleanup, drain-before-write ordering, CGSN timeout
bound, skip-QMI-ICCID-without-SIM, CommandError-as-200, WWAN at1 port
selection and vendor-neutral discovery cases. go vet and go test ./... pass.

Co-authored-by: Test <[email protected]>
2026-08-16 23:34:12 +08:00

79 lines
2.3 KiB
Go

//go:build linux
package modem
import (
"errors"
"io"
"testing"
"golang.org/x/sys/unix"
)
// TestNativeWWANATTransportDrainDiscardsPendingBytes verifies Drain discards
// every byte already buffered on the transport. A command that timed out (e.g.
// AT+CGSN on an MHI modem that never answers OK) can leave its late reply in
// the input buffer; the next command's Drain must clear it, however much data
// is pending, before the session writes the new command.
func TestNativeWWANATTransportDrainDiscardsPendingBytes(t *testing.T) {
readFD, writeFD := socketpair(t)
defer unix.Close(writeFD)
// More than one 4096-byte Drain read: a slow CGSN reply (echo + IMEI +
// trailing CRLF) can exceed a single buffer.
payload := make([]byte, 12000)
for index := range payload {
payload[index] = byte('A' + index%26)
}
payload = append(payload, []byte("\r\n+CGSN: 357091089453326\r\n")...)
if _, err := unix.Write(writeFD, payload); err != nil {
t.Fatalf("seed stale bytes: %v", err)
}
transport := &nativeWWANATTransport{fd: readFD, readTimeout: -1}
if err := transport.Drain(); err != nil {
t.Fatalf("Drain: %v", err)
}
assertNoPendingBytes(t, readFD, "after Drain")
// Draining a clean transport is a fast no-op that must not block or error.
if err := transport.Drain(); err != nil {
t.Fatalf("second Drain: %v", err)
}
}
// TestNativeWWANATTransportDrainRejectsClosedTransport covers the guard that
// keeps a poisoned session from draining a wedged, already-closed fd.
func TestNativeWWANATTransportDrainRejectsClosedTransport(t *testing.T) {
readFD, writeFD := socketpair(t)
defer unix.Close(writeFD)
transport := &nativeWWANATTransport{fd: readFD, readTimeout: -1}
if err := transport.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
if err := transport.Drain(); !errors.Is(err, io.ErrClosedPipe) {
t.Fatalf("Drain after Close = %v, want ErrClosedPipe", err)
}
}
func socketpair(t *testing.T) (int, int) {
t.Helper()
fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0)
if err != nil {
t.Fatal(err)
}
return fds[0], fds[1]
}
func assertNoPendingBytes(t *testing.T, fd int, context string) {
t.Helper()
fds := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}}
ready, err := unix.Poll(fds, 0)
if err != nil {
t.Fatalf("poll %s: %v", context, err)
}
if ready != 0 {
t.Fatalf("%s: fd still readable", context)
}
}