fix(device): select the AT profile for legacy port bindings

This commit is contained in:
Jiahui Zhu committed 2026-11-20 12:51:22 +08:00
1 parent 60658cb4be
commit 63667bc7e0
3 files changed
+95 -6

No files matched your search

+10 -5
View File
@@ -291,11 +291,7 @@ func (idx WorkerDiscoveryIndex) observeLiveATIdentity(dev CompatibleModem, probe
return dev, nil
}
observation := &atScanObservation{usbPath: dev.USBPath, atPorts: dev.ATPorts}
for _, port := range dev.ATPorts {
owner := idx.atIdentityByPort[port]
if owner.query == nil {
continue
}
if port, owner := idx.ownedATPort(dev.ATPorts); port != "" {
dev.ATPort, dev.IMEI = port, ""
if imei, err := owner.query(); err == nil {
dev.IMEI = imei
@@ -311,6 +307,15 @@ func (idx WorkerDiscoveryIndex) observeLiveATIdentity(dev CompatibleModem, probe
return resolved, observation
}
func (idx WorkerDiscoveryIndex) ownedATPort(ports []string) (string, workerATIdentityReader) {
for _, port := range ports {
if owner := idx.atIdentityByPort[port]; owner.query != nil {
return port, owner
}
}
return "", workerATIdentityReader{}
}
type ConfiguredDeviceIndex struct {
byControl map[string]string
byUSB map[string]string
+50
View File
@@ -15,6 +15,56 @@ import (
"github.com/yibaiba/hideck/internal/modem"
)
func TestConfiguredATPortSelectsDialectWithoutIMEI(t *testing.T) {
port := filepath.Join(t.TempDir(), "ttyUSB8")
if err := os.WriteFile(port, nil, 0o600); err != nil {
t.Fatal(err)
}
alias := filepath.Join(filepath.Dir(port), "by-id")
if err := os.Symlink(port, alias); err != nil {
t.Fatal(err)
}
realPort, err := filepath.EvalSymlinks(port)
if err != nil {
t.Fatal(err)
}
original := discoverFallbackModemsFn
t.Cleanup(func() { discoverFallbackModemsFn = original })
discoverFallbackModemsFn = func() ([]CompatibleModem, error) {
return []CompatibleModem{{
VendorID: cmIoTVendorID, ProductID: ml307cProductID, ATPorts: []string{realPort},
}}, nil
}
configured, ok := configuredATDevice(config.DeviceConfig{ManagePort: alias})
if !ok || configured.ATProfile() != modem.ATProfileML307C || configured.ATPort != realPort {
t.Fatalf("aliased legacy port = %+v, %t", configured, ok)
}
if got, ok := configuredATDevice(config.DeviceConfig{ATPort: "/dev/ttyUSB2", ManagePort: alias}); ok {
t.Fatalf("unmatched AT port selected %+v", got)
}
// Simulate the same modem already managed over its other AT interface.
const managedPort = "/dev/ttyUSB9"
devices := discoverFallbackModemsFn
discoverFallbackModemsFn = func() ([]CompatibleModem, error) {
found, _ := devices()
found[0].ATPorts = append(found[0].ATPorts, managedPort)
return found, nil
}
p := NewPool(&config.Config{})
t.Cleanup(func() { _ = p.Shutdown() })
managed := config.DeviceConfig{ID: "active", DeviceBackend: "at", ATPort: managedPort}
m, err := modem.NewWithATProfile(managed, modem.ATProfileML307C)
if err != nil {
t.Fatal(err)
}
p.AttachWorkerForTest(&Worker{ID: managed.ID, Pool: p, Config: managed, Modem: m})
_, err = p.AddWorkerFromConfig(config.DeviceConfig{ID: "duplicate", DeviceBackend: "at", ManagePort: alias})
if err == nil || !strings.Contains(err.Error(), "已由现有设备接管") {
t.Fatalf("startup over a second AT interface returned %v", err)
}
}
func TestML307CDiscoveryAndRebind(t *testing.T) {
usbPath := filepath.Join(t.TempDir(), "1-1")
for _, node := range []string{"1-1:1.2/ttyUSB8", "1-1:1.3/ttyUSB2", "1-1:1.4/ttyUSB3"} {
+35 -1
View File
@@ -178,6 +178,29 @@ func configuredPortRequiresLiveATIdentity(cfg config.DeviceConfig, observations
return false
}
// configuredATDevice finds the explicitly selected port before a legacy config
// has learned its IMEI. Keep the real port for runtime ownership checks.
func configuredATDevice(cfg config.DeviceConfig) (CompatibleModem, bool) {
port := strings.TrimSpace(cfg.ATPort)
if port == "" {
port = strings.TrimSpace(cfg.ManagePort)
}
if port == "" {
return CompatibleModem{}, false
}
port = resolveUSBPathForScan(port)
devices, err := discoverFallbackModemsFn()
if err == nil {
for _, dev := range devices {
if containsPort(dev.ATPorts, port) {
dev.ATPort = port
return dev, true
}
}
}
return CompatibleModem{}, false
}
type qmiBootstrapDiscoveryCache struct {
loaded bool
list []QMIDevice
@@ -345,7 +368,7 @@ func (p *Pool) AddWorkerFromConfig(devCfg config.DeviceConfig) (*Worker, error)
devCfg.ModemIMEI = resolved.Matched[0].BackfillIMEI
logger.Info(fmt.Sprintf("[%s] MBIM/AT 通过 USB 路径认领成功,回填实时 IMEI", devCfg.ID), "imei", devCfg.ModemIMEI)
}
} else if (strings.TrimSpace(devCfg.ATPort) == "" && strings.TrimSpace(devCfg.ControlDevice) == "") ||
} else if (strings.TrimSpace(devCfg.ATPort) == "" && strings.TrimSpace(devCfg.ManagePort) == "" && strings.TrimSpace(devCfg.ControlDevice) == "") ||
(atIMEIConfigured && configuredPortRequiresLiveATIdentity(devCfg, atObservations)) {
return nil, fmt.Errorf("未找到匹配的非 QMI 硬件(AT-only 或 MBIM),跳过启动")
}
@@ -420,6 +443,17 @@ func (p *Pool) AddWorkerFromConfig(devCfg config.DeviceConfig) (*Worker, error)
}
backendMode := resolvedBackendMode(devCfg)
if backendMode == backend.BackendAT && strings.TrimSpace(devCfg.ModemIMEI) == "" && atProfile == modem.ATProfileDefault {
if hw, ok := configuredATDevice(devCfg); ok {
atProfile = hw.ATProfile()
if atDeviceRulesFor(atProfile).verifyLiveIMEI {
if port, _ := liveWorkerIndex.ownedATPort(hw.ATPorts); port != "" {
return nil, fmt.Errorf("AT 口 %s 已由现有设备接管", port)
}
devCfg.USBPath, devCfg.ATPort, devCfg.ManagePort = hw.USBPath, hw.ATPort, hw.ATPort
}
}
}
m, err := newWorkerModem(devCfg, backendMode, atProfile)
if err != nil {
return nil, fmt.Errorf("初始化 Modem 失败: %w", err)