diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index cf83c648..26735a88 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -11,6 +11,7 @@ import { onAndroidNotificationPosted } from "../native/notification-icons"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { grayImageToPreviewSource } from "../native/gray-image-preview"; import { firmwareIncompatibilityMessage } from "./firmware-compat"; +import { resumeAutoReconnect, suppressAutoReconnect } from "./reconnect-policy"; import { findSoundEffect, playSoundEffect } from "../ui/sound-effects"; import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboarding-state"; import { beginRenderPass, endRenderPass } from "../util/render-freshness"; @@ -217,6 +218,11 @@ class DashboardController { // the boot-time registry (just the launcher) never clobbers the saved list. private openAppsRestored = false; private suppressOpenAppsPersist = false; + // connect() is a long sequence of awaits against this.communicator; the + // incompatible-firmware disconnect must not tear that communicator down + // underneath it, so it waits for this to clear. + private connectRunning = false; + private incompatibleDisconnectPending = false; constructor() { const sharedActions = { @@ -823,6 +829,8 @@ class DashboardController { async connect(): Promise { if (this.phase !== "disconnected") return; + // Connecting is the explicit way out of the manual-disconnected state. + resumeAutoReconnect(); const addresses = loadDeviceAddresses(); if (!addresses.right || !addresses.left) { @@ -853,6 +861,7 @@ class DashboardController { this.wearNotifySupported = false; this.lockSurfaceConfigured = false; this.evenHubResumePromise = null; + this.connectRunning = true; try { await ensureBlePermissions(); @@ -993,6 +1002,9 @@ class DashboardController { } this.emit(); } + if (warning) { + this.scheduleIncompatibleFirmwareDisconnect(); + } }); // The Music and Nightscout apps subscribe to their bridges directly and // repaint their own windows, so bridge updates need no controller action. @@ -1091,10 +1103,58 @@ class DashboardController { this.setStatus(`Failed: ${message}`); this.appendLog(`error: ${message}`); throw error; + } finally { + this.connectRunning = false; } } - async disconnect(): Promise { + /** + * Incompatible firmware means every message Faceclaw sends is one the + * glasses may misinterpret, and a live session fights the flash flow's own + * connection. Drop the connection (without the CFW-directed cleanup + * messages) and hold in the manual-disconnected state until the user + * connects explicitly or installs the custom firmware. + */ + private scheduleIncompatibleFirmwareDisconnect(): void { + if (this.incompatibleDisconnectPending) return; + this.incompatibleDisconnectPending = true; + const attempt = () => { + // Firmware info can arrive while connect() is still mid-flight; let it + // finish so the teardown doesn't race its surface setup. + if (this.connectRunning) { + setTimeout(attempt, 200); + return; + } + this.incompatibleDisconnectPending = false; + if (this.phase === "disconnected" || this.phase === "disconnecting") { + // The session ended some other way; still stop auto-reconnect from + // re-dialing glasses we know can't run Faceclaw. + suppressAutoReconnect(); + return; + } + this.appendLog( + "Disconnecting: the glasses firmware is incompatible. Auto-reconnect is disabled until you connect manually or install the custom firmware.", + ); + void this.disconnect({ skipFirmwareCleanup: true }) + .then(() => this.setStatus("Disconnected (incompatible firmware).")) + .catch((error) => { + this.appendLog(`incompatible-firmware disconnect failed: ${this.formatError(error)}`); + }); + }; + setTimeout(attempt, 0); + } + + /** + * Tear down the connection and enter the manual-disconnected state: every + * caller is deliberate (the phone/glasses Disconnect actions, entering the + * flash flow, an incompatible-firmware bailout), so auto-reconnect stays + * off until an explicit connect or a successful firmware install. + * + * skipFirmwareCleanup: don't send the CFW cleanup/shutdown or wake-lease + * messages — used when the firmware is incompatible and would misread them. + */ + async disconnect(options?: { skipFirmwareCleanup?: boolean }): Promise { + suppressAutoReconnect(); if (this.phase === "disconnected" || this.phase === "disconnecting") return; this.setPhase("disconnecting"); @@ -1144,12 +1204,15 @@ class DashboardController { } try { - const leaseReleased = await communicator?.setFaceclawWakeLeaseEnabled(false).catch((error) => { - this.appendLog(`wake takeover lease release failed: ${this.formatError(error)}`); - return false; - }); - if (leaseReleased === true) { - this.faceclawWakeLeaseState = false; + const skipFirmwareCleanup = options?.skipFirmwareCleanup === true; + if (!skipFirmwareCleanup) { + const leaseReleased = await communicator?.setFaceclawWakeLeaseEnabled(false).catch((error) => { + this.appendLog(`wake takeover lease release failed: ${this.formatError(error)}`); + return false; + }); + if (leaseReleased === true) { + this.faceclawWakeLeaseState = false; + } } // Quiesce every producer that can enqueue a glasses command before the @@ -1159,13 +1222,15 @@ class DashboardController { await nightscoutBridge.stop().catch(() => {}); voiceControlBridge.stop(); - const cleanupAcked = await communicator?.sendCfwCleanup().catch((error) => { - this.appendLog(`CFW cleanup failed: ${this.formatError(error)}`); - return false; - }); + const cleanupAcked = skipFirmwareCleanup + ? false + : await communicator?.sendCfwCleanup().catch((error) => { + this.appendLog(`CFW cleanup failed: ${this.formatError(error)}`); + return false; + }); if (cleanupAcked === true) { this.appendLog("CFW cleanup completed."); - } else if (communicator) { + } else if (communicator && !skipFirmwareCleanup) { // Older CFWs do not advertise cleanup11. Preserve their established // teardown behavior; close() will also send the legacy FB lease release. const shutdownAcked = await communicator.sendShutdown(0).catch((error) => { diff --git a/app/g2/reconnect-policy.ts b/app/g2/reconnect-policy.ts new file mode 100644 index 00000000..a7390f89 --- /dev/null +++ b/app/g2/reconnect-policy.ts @@ -0,0 +1,26 @@ +/** + * Manual-disconnected state: distinguishes "disconnected and happy to + * auto-reconnect" (app launch, a BLE drop) from "disconnected on purpose" + * (the user picked Disconnect, the flash flow needs the glasses to itself, + * or the connected firmware turned out to be incompatible). While suppressed, + * the main page's auto-connect must not re-dial the glasses; only an explicit + * connect or a successful firmware install lifts it. + * + * Lives in its own module (not on DashboardController) so the onboarding + * flash flow can set it without instantiating the whole controller singleton. + * Deliberately in-memory: a fresh app launch is a regular disconnect. + */ + +let suppressed = false; + +export function suppressAutoReconnect(): void { + suppressed = true; +} + +export function resumeAutoReconnect(): void { + suppressed = false; +} + +export function isAutoReconnectSuppressed(): boolean { + return suppressed; +} diff --git a/app/phone-ui/main-view-model.ts b/app/phone-ui/main-view-model.ts index 1ca120a1..049c7421 100644 --- a/app/phone-ui/main-view-model.ts +++ b/app/phone-ui/main-view-model.ts @@ -1,6 +1,7 @@ import { Application, Frame, ImageSource, Observable, Screen } from "@nativescript/core"; import { dashboardController } from "../g2/dashboard-controller"; import { isValidMacAddress, loadDeviceAddresses } from "../g2/device-addresses"; +import { isAutoReconnectSuppressed } from "../g2/reconnect-policy"; import { G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image"; const LENS_ASPECT_RATIO = G2_LENS_WIDTH / G2_LENS_HEIGHT; @@ -487,11 +488,14 @@ export class MainViewModel extends Observable { /** * Try to connect automatically on reaching the main page. No-op if already - * connecting/connected, or if no glasses are configured (e.g. preview-only - * users, who have nothing to connect to). + * connecting/connected, if the app is in the manual-disconnected state + * (the user picked Disconnect, the flash flow owns the glasses, or the + * firmware was found incompatible), or if no glasses are configured + * (e.g. preview-only users, who have nothing to connect to). */ async autoConnect(): Promise { if (this.phase !== "disconnected") return; + if (isAutoReconnectSuppressed()) return; const addresses = loadDeviceAddresses(); if (!isValidMacAddress(addresses.right) || !isValidMacAddress(addresses.left)) return; try { diff --git a/app/phone-ui/onboarding-flash-view-model.ts b/app/phone-ui/onboarding-flash-view-model.ts index 81372657..b3be62dd 100644 --- a/app/phone-ui/onboarding-flash-view-model.ts +++ b/app/phone-ui/onboarding-flash-view-model.ts @@ -15,6 +15,7 @@ import { import { buildAddressSet, DeviceDiscoveryBridge } from "../native/device-discovery"; import { FirmwareFlasher, FlashProgress, FlashState } from "../native/firmware-flasher"; import { FlashPromptCommunicator, FlashPromptState } from "../native/flash-prompt-communicator"; +import { resumeAutoReconnect, suppressAutoReconnect } from "../g2/reconnect-policy"; import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state"; type FlashPhase = "intro" | "prompt" | "building" | "ready" | "flashing" | "flashed" | "error"; @@ -44,6 +45,10 @@ export class OnboardingFlashViewModel extends Observable { constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean }) { super(); + // The flash flow needs the glasses to itself: from here on the main + // page must not auto-reconnect, until an install succeeds (below) or + // the user connects explicitly. + suppressAutoReconnect(); this.mode = options?.mode ?? "install"; this.fromOnboarding = options?.fromOnboarding ?? true; this._headline = this.mode === "uninstall" ? "Uninstall Custom Firmware" : "Flash Custom Firmware"; @@ -431,6 +436,13 @@ export class OnboardingFlashViewModel extends Observable { this.disposeFlasher(); this.busy = false; if (success) { + if (this.mode === "install") { + // A successful install ends the manual-disconnected state: the main + // page may auto-connect to the freshly flashed glasses. (After an + // uninstall the glasses run stock firmware, which Faceclaw would + // immediately flag as incompatible, so stay manually disconnected.) + resumeAutoReconnect(); + } this.progress = 100; this.setPhase("flashed"); this.headline = "All Done";