mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-06 16:01:13 +00:00
QMI reuses call slot 1, so volte-{device}-{qmiID} overwrote history.
Identify live calls by QMI id and mint a unique persistent call_id.
Cap ALSA open at 800ms, refine hwparams before HW_PARAMS, and skip
later opens after a failure so dialing is not blocked by set_interface.
488 lines
12 KiB
Go
488 lines
12 KiB
Go
package volte
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/iniwex5/quectel-qmi-go/pkg/qmi"
|
|
"github.com/iniwex5/vowifi-go/runtimehost/voicehost"
|
|
"github.com/yibaiba/hideck/pkg/logger"
|
|
)
|
|
|
|
type Controller struct {
|
|
host Host
|
|
provision *Provisioner
|
|
audio *AudioRuntime
|
|
media mediaTable
|
|
lteWait time.Duration
|
|
imsWait time.Duration
|
|
mu sync.Mutex
|
|
locks map[string]*sync.Mutex
|
|
sess map[string]*session
|
|
globalIncoming []func(voicehost.IncomingCall)
|
|
globalEvents []func(voicehost.CallEvent)
|
|
}
|
|
|
|
type session struct {
|
|
gen uint64
|
|
imsHooked bool
|
|
status Status
|
|
voice *voiceSession
|
|
alsaUnavailable bool
|
|
}
|
|
|
|
func NewController(host Host) *Controller {
|
|
c := NewControllerWithBackup(host, DefaultBackupDir)
|
|
c.lteWait = 45 * time.Second
|
|
c.imsWait = 45 * time.Second
|
|
return c
|
|
}
|
|
|
|
func NewControllerWithBackup(host Host, backupDir string) *Controller {
|
|
c := &Controller{host: host, sess: make(map[string]*session), locks: make(map[string]*sync.Mutex)}
|
|
c.provision = NewProvisioner(host, &FileStore{Dir: backupDir})
|
|
return c
|
|
}
|
|
|
|
func (c *Controller) SetAudioRuntime(audio *AudioRuntime) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.audio = audio
|
|
}
|
|
|
|
func (c *Controller) Enable(ctx context.Context, deviceID string) error {
|
|
deviceID = strings.TrimSpace(deviceID)
|
|
if c == nil || c.host == nil || deviceID == "" {
|
|
return errors.New("volte: controller is not configured")
|
|
}
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return c.withDevice(deviceID, func() error {
|
|
c.mu.Lock()
|
|
s := c.sess[deviceID]
|
|
if s == nil {
|
|
s = &session{status: Status{DeviceID: deviceID, Phase: PhaseEnabling}}
|
|
c.sess[deviceID] = s
|
|
}
|
|
s.gen++
|
|
gen := s.gen
|
|
s.status.Phase = PhaseEnabling
|
|
s.status.LastError = ""
|
|
c.mu.Unlock()
|
|
|
|
if err := c.host.StopSoftwareIMS(deviceID); err != nil {
|
|
return c.fail(deviceID, fmt.Errorf("stop software IMS: %w", err))
|
|
}
|
|
if _, err := c.host.ExecuteAT(deviceID, COPSNumericFormatCommand(), defaultATTimeout); err != nil {
|
|
logger.Debug("AT+COPS=3,2 失败,继续用当前 COPS 格式", "device", deviceID, "err", err)
|
|
}
|
|
if err := c.waitLTE(ctx, deviceID); err != nil {
|
|
return c.fail(deviceID, err)
|
|
}
|
|
if err := c.provisionConfig(ctx, deviceID); err != nil {
|
|
return err
|
|
}
|
|
if err := c.activateIMSPDN(deviceID); err != nil {
|
|
logger.Warn("IMS PDN 未激活,继续等注册", "device", deviceID, "err", err)
|
|
c.setError(deviceID, err)
|
|
}
|
|
c.patch(deviceID, func(st *Status) {
|
|
if st.Phase == PhaseEnabling {
|
|
st.Phase = PhaseRegistering
|
|
}
|
|
})
|
|
c.attachIMS(deviceID, gen)
|
|
c.attachVoice(deviceID)
|
|
c.waitIMS(ctx, deviceID)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (c *Controller) Restore(ctx context.Context, deviceID string) error {
|
|
deviceID = strings.TrimSpace(deviceID)
|
|
if c == nil || c.provision == nil || deviceID == "" {
|
|
return errors.New("volte: controller is not configured")
|
|
}
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
res, err := c.provision.Restore(ctx, deviceID)
|
|
c.applyProvision(deviceID, res)
|
|
if err != nil {
|
|
return c.fail(deviceID, err)
|
|
}
|
|
c.Disable(deviceID)
|
|
return nil
|
|
}
|
|
|
|
func (c *Controller) Disable(deviceID string) {
|
|
deviceID = strings.TrimSpace(deviceID)
|
|
if c == nil || deviceID == "" {
|
|
return
|
|
}
|
|
_ = c.withDevice(deviceID, func() error {
|
|
if active := c.ActiveCall(deviceID); active != nil {
|
|
_ = c.HangupCall(context.Background(), deviceID, active.CallID)
|
|
if m := c.media.take(active.CallID); m != nil {
|
|
_ = m.Close()
|
|
}
|
|
}
|
|
if c.host != nil {
|
|
if err := c.host.ReleaseIMSClients(deviceID); err != nil {
|
|
logger.Warn("释放原生 IMS 客户端失败", "device", deviceID, "err", err)
|
|
}
|
|
}
|
|
c.mu.Lock()
|
|
if s := c.sess[deviceID]; s != nil {
|
|
s.gen++
|
|
}
|
|
delete(c.sess, deviceID)
|
|
c.mu.Unlock()
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (c *Controller) Status(deviceID string) Status {
|
|
if c == nil {
|
|
return Status{DeviceID: deviceID, Phase: PhaseIdle}
|
|
}
|
|
deviceID = strings.TrimSpace(deviceID)
|
|
c.mu.Lock()
|
|
s := c.sess[deviceID]
|
|
if s == nil {
|
|
c.mu.Unlock()
|
|
return Status{DeviceID: deviceID, Phase: PhaseIdle}
|
|
}
|
|
st := s.status
|
|
c.mu.Unlock()
|
|
audio := ""
|
|
if c.host != nil {
|
|
audio = strings.TrimSpace(c.host.AudioDevice(deviceID))
|
|
}
|
|
if audio == "" {
|
|
return st
|
|
}
|
|
st.AudioDevice = audio
|
|
st.UACEnabled = true
|
|
st.RebootRequired = false
|
|
c.patch(deviceID, func(s *Status) {
|
|
s.AudioDevice = audio
|
|
s.UACEnabled = true
|
|
s.RebootRequired = false
|
|
})
|
|
return st
|
|
}
|
|
|
|
func (c *Controller) Active(deviceID string) bool {
|
|
st := c.Status(deviceID)
|
|
return st.Phase == PhaseRegistered || st.Phase == PhaseUnverified || st.Phase == PhaseEnabling || st.Phase == PhaseRegistering
|
|
}
|
|
|
|
func (c *Controller) provisionConfig(ctx context.Context, deviceID string) error {
|
|
mcc, mnc, plmnErr := c.readPLMN(deviceID)
|
|
if plmnErr != nil {
|
|
return c.fail(deviceID, fmt.Errorf("read serving PLMN: %w", plmnErr))
|
|
}
|
|
c.patch(deviceID, func(st *Status) { st.PLMN = NormalizePLMN(mcc, mnc) })
|
|
name, mbnErr := c.resolveMBN(deviceID, mcc, mnc)
|
|
if mbnErr != nil {
|
|
return c.fail(deviceID, mbnErr)
|
|
}
|
|
c.patch(deviceID, func(st *Status) { st.MBNName = name })
|
|
desired := Desired{
|
|
IMSEnabled: boolPtr(true),
|
|
VoLTEEnabled: boolPtr(true),
|
|
UACEnabled: boolPtr(true),
|
|
MBNSelect: &name,
|
|
MBNAutoSel: boolPtr(false),
|
|
}
|
|
res, err := c.provision.Ensure(ctx, deviceID, desired)
|
|
c.applyProvision(deviceID, res)
|
|
if err != nil && !res.Current.IMSEnabled && !errors.Is(err, ErrRebootRequired) && !errors.Is(err, ErrFieldDrift) {
|
|
return c.fail(deviceID, err)
|
|
}
|
|
if err != nil {
|
|
c.setError(deviceID, err)
|
|
}
|
|
if err := c.host.EnsureIMSClients(ctx, deviceID); err != nil {
|
|
logger.Warn("QMI IMS/IMSA 客户端不可用", "device", deviceID, "err", err)
|
|
c.patch(deviceID, func(st *Status) { st.QMIIMSUnavailable = true })
|
|
return c.fail(deviceID, err)
|
|
}
|
|
if err := c.host.SetNativeIMS(ctx, deviceID, true); err != nil {
|
|
logger.Warn("QMI 打开原生 IMS 失败,继续用 AT 结果", "device", deviceID, "err", err)
|
|
}
|
|
c.syncAudioStatus(deviceID, res.Current.UACEnabled)
|
|
if !res.Current.IMSEnabled {
|
|
return c.fail(deviceID, fmt.Errorf("native IMS did not enable"))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Controller) applyProvision(deviceID string, res Result) {
|
|
c.patch(deviceID, func(st *Status) {
|
|
st.IMSEnabled = res.Current.IMSEnabled
|
|
st.VoLTEEnabled = res.Current.VoLTEEnabled
|
|
st.UACEnabled = false
|
|
st.RebootRequired = res.RebootRequired || res.Current.UACEnabled
|
|
st.ProvisionStage = res.Stage
|
|
st.IMEITail = res.IMEITail
|
|
})
|
|
}
|
|
|
|
func (c *Controller) syncAudioStatus(deviceID string, nvUAC bool) {
|
|
audio := ""
|
|
if c.host != nil {
|
|
audio = strings.TrimSpace(c.host.AudioDevice(deviceID))
|
|
}
|
|
c.patch(deviceID, func(st *Status) {
|
|
st.AudioDevice = audio
|
|
if audio != "" {
|
|
st.UACEnabled = true
|
|
st.RebootRequired = false
|
|
return
|
|
}
|
|
st.UACEnabled = false
|
|
if nvUAC {
|
|
st.RebootRequired = true
|
|
}
|
|
})
|
|
}
|
|
|
|
func (c *Controller) attachIMS(deviceID string, gen uint64) {
|
|
if c == nil || c.host == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
s := c.sess[deviceID]
|
|
if s == nil || s.imsHooked {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
s.imsHooked = true
|
|
c.mu.Unlock()
|
|
_ = c.host.OnIMSRegistration(deviceID, func(info *qmi.IMSARegistrationStatus) {
|
|
if !c.generationLive(deviceID, gen) {
|
|
return
|
|
}
|
|
c.patch(deviceID, func(st *Status) { applyIMSARegistration(st, info) })
|
|
})
|
|
_ = c.host.OnIMSServices(deviceID, func(info *qmi.IMSAServicesStatus) {
|
|
if !c.generationLive(deviceID, gen) {
|
|
return
|
|
}
|
|
c.patch(deviceID, func(st *Status) {
|
|
if info != nil && info.HasVoiceServiceStatus {
|
|
st.VoiceAvailable = info.VoiceServiceStatus == qmi.IMSAServiceAvailabilityAvailable
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
func (c *Controller) generationLive(deviceID string, gen uint64) bool {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
s := c.sess[deviceID]
|
|
return s != nil && s.gen == gen
|
|
}
|
|
|
|
func (c *Controller) withDevice(deviceID string, fn func() error) error {
|
|
c.mu.Lock()
|
|
if c.locks == nil {
|
|
c.locks = map[string]*sync.Mutex{}
|
|
}
|
|
lk := c.locks[deviceID]
|
|
if lk == nil {
|
|
lk = &sync.Mutex{}
|
|
c.locks[deviceID] = lk
|
|
}
|
|
c.mu.Unlock()
|
|
lk.Lock()
|
|
defer lk.Unlock()
|
|
return fn()
|
|
}
|
|
|
|
func applyIMSARegistration(st *Status, info *qmi.IMSARegistrationStatus) {
|
|
if st == nil || info == nil || !info.HasStatus {
|
|
return
|
|
}
|
|
switch info.Status {
|
|
case qmi.IMSARegistrationStateRegistered, qmi.IMSARegistrationStateLimitedRegistered:
|
|
st.IMSRegistered = true
|
|
st.Phase = PhaseRegistered
|
|
st.LastError = ""
|
|
case qmi.IMSARegistrationStateRegistering:
|
|
st.IMSRegistered = false
|
|
st.Phase = PhaseRegistering
|
|
default:
|
|
st.IMSRegistered = false
|
|
st.Phase = PhaseFailed
|
|
}
|
|
}
|
|
|
|
func (c *Controller) refreshRegistration(ctx context.Context, deviceID string) {
|
|
reg, err := c.host.IMSAStatus(ctx, deviceID)
|
|
c.patch(deviceID, func(st *Status) {
|
|
if err != nil {
|
|
if st.IMSEnabled && st.VoLTEEnabled {
|
|
st.Phase = PhaseUnverified
|
|
}
|
|
if st.LastError == "" {
|
|
st.LastError = err.Error()
|
|
}
|
|
return
|
|
}
|
|
st.IMSRegistered = reg.Registered
|
|
st.VoiceAvailable = reg.VoiceAvailable
|
|
if reg.Registered {
|
|
st.Phase = PhaseRegistered
|
|
st.LastError = ""
|
|
} else if st.IMSEnabled && st.VoLTEEnabled {
|
|
st.Phase = PhaseUnverified
|
|
}
|
|
})
|
|
}
|
|
|
|
func (c *Controller) fail(deviceID string, err error) error {
|
|
c.patch(deviceID, func(st *Status) {
|
|
st.Phase = PhaseFailed
|
|
st.LastError = err.Error()
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (c *Controller) setError(deviceID string, err error) {
|
|
if err == nil {
|
|
return
|
|
}
|
|
c.patch(deviceID, func(st *Status) { st.LastError = err.Error() })
|
|
}
|
|
|
|
func (c *Controller) patch(deviceID string, fn func(*Status)) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
s := c.sess[deviceID]
|
|
if s == nil {
|
|
s = &session{status: Status{DeviceID: deviceID}}
|
|
c.sess[deviceID] = s
|
|
}
|
|
fn(&s.status)
|
|
}
|
|
|
|
func (c *Controller) readPLMN(deviceID string) (string, string, error) {
|
|
resp, err := c.host.ExecuteAT(deviceID, COPSQueryCommand(), defaultATTimeout)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return ParseCOPS(resp)
|
|
}
|
|
|
|
func (c *Controller) resolveMBN(deviceID, mcc, mnc string) (string, error) {
|
|
resp, err := c.host.ExecuteAT(deviceID, MBNListQueryCommand(), defaultATTimeout)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
entries, err := ParseMBNList(resp)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return UniqueMBN(mcc, mnc, entries)
|
|
}
|
|
|
|
func (c *Controller) waitLTE(ctx context.Context, deviceID string) error {
|
|
check := func() error {
|
|
resp, err := c.host.ExecuteAT(deviceID, CEREGQueryCommand(), defaultATTimeout)
|
|
if err != nil {
|
|
return fmt.Errorf("query CEREG: %w", err)
|
|
}
|
|
lte, err := ParseCEREG(resp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.patch(deviceID, func(st *Status) { st.LTERegistered = lte.Registered })
|
|
if lte.Registered {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("LTE not registered (CEREG %d)", lte.Stat)
|
|
}
|
|
if err := check(); err == nil || c.lteWait <= 0 {
|
|
return err
|
|
}
|
|
deadline := time.Now().Add(c.lteWait)
|
|
for time.Now().Before(deadline) {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
if err := check(); err == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return check()
|
|
}
|
|
|
|
func (c *Controller) activateIMSPDN(deviceID string) error {
|
|
resp, err := c.host.ExecuteAT(deviceID, CGDCONTQueryCommand(), defaultATTimeout)
|
|
if err != nil {
|
|
return fmt.Errorf("query CGDCONT: %w", err)
|
|
}
|
|
ctxs, err := ParseCGDCONT(resp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ims, ok := IMSContext(ctxs)
|
|
if !ok {
|
|
return fmt.Errorf("volte: no IMS APN in CGDCONT")
|
|
}
|
|
actResp, err := c.host.ExecuteAT(deviceID, CGACTQueryCommand(), defaultATTimeout)
|
|
if err != nil {
|
|
return fmt.Errorf("query CGACT: %w", err)
|
|
}
|
|
active, err := ParseCGACT(actResp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !active[ims.CID] {
|
|
if _, err := c.host.ExecuteAT(deviceID, CGACTSetCommand(ims.CID, true), defaultATTimeout); err != nil {
|
|
return fmt.Errorf("activate IMS PDN cid %d: %w", ims.CID, err)
|
|
}
|
|
actResp, err = c.host.ExecuteAT(deviceID, CGACTQueryCommand(), defaultATTimeout)
|
|
if err != nil {
|
|
return fmt.Errorf("re-query CGACT: %w", err)
|
|
}
|
|
active, err = ParseCGACT(actResp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !active[ims.CID] {
|
|
return fmt.Errorf("IMS PDN cid %d still down", ims.CID)
|
|
}
|
|
}
|
|
c.patch(deviceID, func(st *Status) { st.IMSPDNActive = true })
|
|
return nil
|
|
}
|
|
|
|
func (c *Controller) waitIMS(ctx context.Context, deviceID string) {
|
|
c.refreshRegistration(ctx, deviceID)
|
|
if c.Status(deviceID).IMSRegistered || c.imsWait <= 0 {
|
|
return
|
|
}
|
|
deadline := time.Now().Add(c.imsWait)
|
|
for time.Now().Before(deadline) {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
c.refreshRegistration(ctx, deviceID)
|
|
if c.Status(deviceID).IMSRegistered {
|
|
return
|
|
}
|
|
}
|
|
}
|