mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-05 08:01:10 +00:00
fix(esim): reload switched ML307C profiles with RF off
This commit is contained in:
5 files changed
+139
-1
No files matched your search
@@ -261,6 +261,32 @@ func (a *ATBackend) GetOperatingMode(ctx context.Context) (OperatingMode, error)
|
||||
return OperatingMode(mode), nil
|
||||
}
|
||||
|
||||
func (a *ATBackend) SupportsOfflineSIMReload() bool {
|
||||
return a.modem.ATProfile().SupportsOfflineSIMReload()
|
||||
}
|
||||
|
||||
// ReloadSIMOffline refreshes the SIM while keeping the radio off.
|
||||
func (a *ATBackend) ReloadSIMOffline(ctx context.Context) error {
|
||||
mode, err := a.GetOperatingMode(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if mode != ModeRFOff {
|
||||
return fmt.Errorf("offline SIM reload requires CFUN=4, got %d", mode)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reloadErr := a.SetOperatingMode(ctx, ModeLowPower)
|
||||
if reloadErr == nil {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
// A command that timed out might still have taken effect.
|
||||
restoreErr := a.SetOperatingMode(ctx, ModeRFOff)
|
||||
return errors.Join(reloadErr, restoreErr)
|
||||
}
|
||||
|
||||
func (a *ATBackend) Reboot(ctx context.Context) error {
|
||||
_, err := a.modem.ExecuteAT("AT+CFUN=1,1", 5*time.Second)
|
||||
return err
|
||||
|
||||
@@ -1276,7 +1276,7 @@ func (p *Pool) handleESIMSwitchAfter(deviceID string, token uint64) {
|
||||
"network_enabled_before", snapshot.NetworkEnabledBefore)
|
||||
|
||||
convergence := p.runPostSwitchConvergence(deviceID, token, worker, snapshot)
|
||||
if !p.esimSwitchOwnerCurrent(deviceID, worker, snapshot) {
|
||||
if !p.switchTokenStillCurrent(deviceID, token, "sim_convergence") || !p.esimSwitchOwnerCurrent(deviceID, worker, snapshot) {
|
||||
return
|
||||
}
|
||||
if convergence.Degraded {
|
||||
|
||||
@@ -3,6 +3,7 @@ package device
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -56,6 +57,13 @@ type postSwitchSIMReloader interface {
|
||||
UIMPostSwitchReload(ctx context.Context, readiness qmimanager.UIMReadiness, opts qmimanager.UIMPostSwitchReloadOptions) (uint8, error)
|
||||
}
|
||||
|
||||
// This capability is used only for eSIM recovery, apart from regular radio readiness.
|
||||
type postSwitchOfflineSIMReloader interface {
|
||||
SupportsOfflineSIMReload() bool
|
||||
GetICCIDLive(context.Context) (string, error)
|
||||
ReloadSIMOffline(context.Context) error
|
||||
}
|
||||
|
||||
type postSwitchSIMPowerController interface {
|
||||
UIMPowerOffSIM(ctx context.Context, slot uint8) error
|
||||
UIMPowerOnSIM(ctx context.Context, slot uint8) error
|
||||
@@ -311,6 +319,48 @@ func effectivePostSwitchReinitWindow(cfg config.ESIMSwitchConfig) time.Duration
|
||||
return 5 * time.Second
|
||||
}
|
||||
|
||||
func (p *Pool) convergePostSwitchOfflineSIM(deviceID string, token uint64, worker *Worker,
|
||||
snapshot esimSwitchContext, reloader postSwitchOfflineSIMReloader,
|
||||
) postSwitchConvergenceResult {
|
||||
releaseOperation, err := p.AcquireDeviceOperation(deviceID)
|
||||
if err != nil {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: err.Error()}
|
||||
}
|
||||
defer releaseOperation()
|
||||
|
||||
transition := p.policyTransitionFor(deviceID)
|
||||
transition.Lock()
|
||||
defer transition.Unlock()
|
||||
if !p.switchTokenStillCurrent(deviceID, token, "offline_sim_reload") || !p.esimSwitchOwnerCurrent(deviceID, worker, snapshot) {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: "switch_superseded"}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(p.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
releaseAT, err := p.acquireDeviceAT(ctx, deviceID)
|
||||
if err != nil {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: err.Error()}
|
||||
}
|
||||
defer releaseAT()
|
||||
|
||||
iccid, err := reloader.GetICCIDLive(ctx)
|
||||
if err != nil {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: err.Error()}
|
||||
}
|
||||
current := normalizeSIMIdentityForCompare(iccid)
|
||||
if current == normalizeSIMIdentityForCompare(snapshot.TargetICCID) {
|
||||
return postSwitchConvergenceResult{Ready: true, Reason: "target_iccid_already_active"}
|
||||
}
|
||||
if current == "" || current != normalizeSIMIdentityForCompare(snapshot.ICCIDBefore) {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: "unexpected_iccid_before_reload"}
|
||||
}
|
||||
if err := reloader.ReloadSIMOffline(ctx); err != nil {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: fmt.Sprintf("offline SIM reload: %v", err)}
|
||||
}
|
||||
// The common identity refresh still waits for the target ICCID and IMSI.
|
||||
return postSwitchConvergenceResult{Ready: true, Reason: "offline_sim_reloaded"}
|
||||
}
|
||||
|
||||
func (p *Pool) runPostSwitchConvergence(deviceID string, token uint64, worker *Worker, snapshot esimSwitchContext) postSwitchConvergenceResult {
|
||||
if worker == nil || worker.Backend == nil {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: "worker_or_backend_missing"}
|
||||
@@ -318,6 +368,10 @@ func (p *Pool) runPostSwitchConvergence(deviceID string, token uint64, worker *W
|
||||
if resolvedBackendMode(worker.Config) == backend.BackendPCSC {
|
||||
return postSwitchConvergenceResult{Ready: true, Reason: "pcsc_card_reset_complete"}
|
||||
}
|
||||
if reloader, ok := worker.Backend.(postSwitchOfflineSIMReloader); ok && reloader.SupportsOfflineSIMReload() &&
|
||||
snapshot.FlightModeBefore && !worker.Config.ESIMSwitch.RadioCycle && normalizeSIMIdentityForCompare(snapshot.TargetICCID) != "" {
|
||||
return p.convergePostSwitchOfflineSIM(deviceID, token, worker, snapshot, reloader)
|
||||
}
|
||||
readiness, ok := worker.Backend.(postSwitchReadinessProvider)
|
||||
if !ok {
|
||||
return postSwitchConvergenceResult{Degraded: true, Reason: "uim_readiness_not_supported"}
|
||||
|
||||
@@ -206,6 +206,57 @@ func (s *postSwitchRunBackendStub) UIMPowerOnSIM(ctx context.Context, slot uint8
|
||||
return nil
|
||||
}
|
||||
|
||||
type offlineSIMReloadBackendStub struct {
|
||||
workerStatusBackendStub
|
||||
iccid string
|
||||
reloadCalls int
|
||||
}
|
||||
|
||||
func (s *offlineSIMReloadBackendStub) SupportsOfflineSIMReload() bool { return true }
|
||||
|
||||
func (s *offlineSIMReloadBackendStub) GetICCIDLive(context.Context) (string, error) {
|
||||
return s.iccid, nil
|
||||
}
|
||||
|
||||
func (s *offlineSIMReloadBackendStub) ReloadSIMOffline(context.Context) error {
|
||||
s.reloadCalls++
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestPostSwitchOfflineSIMReloadUsesLiveICCID(t *testing.T) {
|
||||
const deviceID = "dev-1"
|
||||
const previousICCID = "8985203103011907194"
|
||||
const targetICCID = "8985203103011907195"
|
||||
for _, tc := range []struct {
|
||||
name, liveICCID string
|
||||
token uint64
|
||||
radioCycle bool
|
||||
wantReady bool
|
||||
wantReloads int
|
||||
}{
|
||||
{name: "target already active", liveICCID: targetICCID + "F", token: 2, wantReady: true},
|
||||
{name: "old ICCID needs reload", liveICCID: previousICCID, token: 2, wantReady: true, wantReloads: 1},
|
||||
{name: "different live card", liveICCID: "8985203103011907196", token: 2},
|
||||
{name: "newer switch owns device", liveICCID: previousICCID, token: 1},
|
||||
{name: "radio cycle may enable RF", liveICCID: previousICCID, token: 2, radioCycle: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
deviceBackend := &offlineSIMReloadBackendStub{iccid: tc.liveICCID}
|
||||
worker := &Worker{
|
||||
ID: deviceID, Backend: deviceBackend,
|
||||
Config: config.DeviceConfig{ESIMSwitch: config.ESIMSwitchConfig{RadioCycle: tc.radioCycle}},
|
||||
}
|
||||
pool := &Pool{workers: map[string]*Worker{deviceID: worker}, switchTokens: map[string]uint64{deviceID: 2}}
|
||||
snapshot := esimSwitchContext{ICCIDBefore: previousICCID, TargetICCID: targetICCID, FlightModeBefore: true}
|
||||
result := pool.runPostSwitchConvergence(deviceID, tc.token, worker, snapshot)
|
||||
if result.Ready != tc.wantReady || result.Degraded == tc.wantReady || deviceBackend.reloadCalls != tc.wantReloads {
|
||||
t.Fatalf("result=%+v, reloads=%d; want ready=%t and reloads=%d",
|
||||
result, deviceBackend.reloadCalls, tc.wantReady, tc.wantReloads)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPostSwitchConvergenceFlagOffUsesLegacyPollingOnly(t *testing.T) {
|
||||
worker := &Worker{Config: config.DeviceConfig{}}
|
||||
backend := &postSwitchRunBackendStub{
|
||||
|
||||
@@ -50,6 +50,7 @@ type atDialect struct {
|
||||
simStatus string
|
||||
cpinPresence *atCPINPresenceQuery
|
||||
parseCCHO func(string) (int, bool)
|
||||
offlineSIMReload bool
|
||||
voiceInit []string
|
||||
ownedSMSSubmit *atOwnedSMSSubmit
|
||||
}
|
||||
@@ -83,6 +84,7 @@ var ml307cATDialect = atDialect{
|
||||
radio: atRadioQuery{"AT+COPS?", parseCOPSRadio},
|
||||
cpinPresence: &atCPINPresenceQuery{parse: parseML307CCPIN, isAbsentError: isML307CNoSIMError},
|
||||
parseCCHO: parseML307CCCHO,
|
||||
offlineSIMReload: true,
|
||||
ownedSMSSubmit: &atOwnedSMSSubmit{promptTimeout: ml307cSMSPromptTimeout, finalTimeout: ml307cSMSResultTimeout},
|
||||
}
|
||||
|
||||
@@ -93,6 +95,11 @@ func (profile ATProfile) dialect() *atDialect {
|
||||
return &defaultATDialect
|
||||
}
|
||||
|
||||
// SupportsOfflineSIMReload reports whether the SIM can reload without enabling RF.
|
||||
func (profile ATProfile) SupportsOfflineSIMReload() bool {
|
||||
return profile.dialect().offlineSIMReload
|
||||
}
|
||||
|
||||
func parseML307CIMEI(resp string) string {
|
||||
if line, ok := findLineWithPrefix(resp, "+CGSN:"); ok {
|
||||
return parseIMEI(strings.TrimPrefix(line, "+CGSN:"))
|
||||
|
||||
Reference in new issue
Block a user