diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java index 833a34b2..a0707a19 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java @@ -51,6 +51,15 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { // Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid. private static final String[] ITEMS = new String[] {"No, cancel", "Yes, flash"}; + /** + * Shown instead of a bare "prompt page not acked" timeout. Silent mode is + * entered and left by the same gesture on the glasses and nothing here can + * clear it, so the instruction has to travel with the error. + */ + private static final String SILENT_MODE_MESSAGE = + "Your glasses are in silent mode, so they cannot show the confirmation prompt. " + + "Long-press both touchpads on the glasses to leave silent mode, then try again."; + private static final int HEARTBEAT_INTERVAL_MS = 4_000; private static final int SELECTION_TIMEOUT_MS = 120_000; private static final int CREATE_ACK_TIMEOUT_MS = 3_000; @@ -183,6 +192,12 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { sendPrelude(rightAddress); if (!skipPrompt) { + // Ask before writing a page that cannot be answered: silent mode + // blanks the display and stops the firmware dispatching input while + // BLE stays up, so the create-prompt write is still accepted and the + // flash would spend four attempts timing out on an error that says + // nothing about the reason. + requireNotSilent(rightAddress); showPrompt(rightAddress); startHeartbeat(); emitState("prompting", ""); @@ -376,6 +391,18 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { } private int readBattery(String address, String arm) throws InterruptedException { + BleProtocol.BatterySnapshot snapshot = readSettings(address, arm); + return snapshot == null ? -1 : snapshot.battery; + } + + /** + * The sid-0x09 settings read behind both the battery gate and the + * silent-mode check. One ack carries both fields, so neither caller needs + * a query of its own. Returns null when the arm does not answer or the ack + * carries no settings payload. + */ + private BleProtocol.BatterySnapshot readSettings(String address, String arm) + throws InterruptedException { for (int attempt = 0; attempt < 2 && !cancelled; attempt++) { int magic = allocMagic(); byte[] ack = writeAndAwaitAck( @@ -386,16 +413,38 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { BleProtocol.buildSettingsQuery(magic), BATTERY_ACK_TIMEOUT_MS); if (ack == null) { - emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm"); + emitLog("settings read attempt " + (attempt + 1) + " unacked: " + arm + " arm"); continue; } BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(ack); if (snapshot != null) { - return snapshot.battery; + return snapshot; } - emitLog("battery read ack had no battery field: " + arm + " arm"); + emitLog("settings read ack had no settings payload: " + arm + " arm"); + } + return null; + } + + /** + * Refuse the prompt only when the arm POSITIVELY reports silent mode. An + * arm that does not answer, and firmware whose ack omits the field + * (silentMode -1), both fall through to the prompt, so behavior on older + * firmware is unchanged and a missing answer never blocks a flash. + */ + private void requireNotSilent(String address) throws InterruptedException { + BleProtocol.BatterySnapshot snapshot = readSettings(address, "right"); + String state; + if (snapshot == null) { + state = "unknown (arm did not answer)"; + } else if (snapshot.silentMode < 0) { + state = "unknown (ack omits the field)"; + } else { + state = snapshot.silentMode > 0 ? "on" : "off"; + } + emitLog("silent mode before prompt: " + state); + if (snapshot != null && snapshot.silentMode > 0) { + throw new IllegalStateException(SILENT_MODE_MESSAGE); } - return -1; } /** Write and wait for the matching ack; returns the ack's protobuf (may be empty), or null on timeout/write failure. */ diff --git a/app/phone-ui/onboarding-flash-view-model.ts b/app/phone-ui/onboarding-flash-view-model.ts index a99e4880..3249c97c 100644 --- a/app/phone-ui/onboarding-flash-view-model.ts +++ b/app/phone-ui/onboarding-flash-view-model.ts @@ -56,8 +56,6 @@ export class OnboardingFlashViewModel extends Observable { private flasher: FirmwareFlasher | null = null; private flasherUnsubscribers: Array<() => void> = []; private retryAction: () => void = () => this.beginPrompt(); - /** Overrides the error screen's "Retry" when the action is not a retry. */ - private primaryLabelOverride: string | null = null; constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean; autoStart?: boolean }) { super(); @@ -176,7 +174,7 @@ export class OnboardingFlashViewModel extends Observable { case "flashed": return "Finish"; case "error": - return this.primaryLabelOverride ?? "Retry"; + return "Retry"; default: return ""; } @@ -331,25 +329,6 @@ export class OnboardingFlashViewModel extends Observable { break; case "error": this.busy = false; - // An unacked prompt page is not a connection failure. The lens accepts the - // write at the GATT layer and then never answers, which is what firmware - // older than the revision this build needs does -- measured on revision 13, - // four writes accepted with status 0 and no reply, nothing shown on the lens. - // Flashing is the only way OFF that firmware, so failing here leaves the - // glasses permanently unupgradable. Fall back to the phone-side confirmation - // that the low-battery retry already uses, and say plainly what is being - // given up: the on-lens step is what proves these are the wearer's glasses. - if (/prompt page not acked/i.test(detail)) { - this.toError( - "Your glasses are on older firmware that can't show the confirmation prompt, so it can't be " + - "confirmed on the lens. You can confirm here instead. Only continue if these are your glasses " + - "and you want to install Faceclaw's custom firmware.", - () => this.beginPrompt({ skipPrompt: true }), - "Confirm On Your Phone", - "Confirm & Install", - ); - break; - } this.toError(detail || "Connection failed.", () => this.beginPrompt({ skipPrompt })); break; } @@ -590,13 +569,7 @@ export class OnboardingFlashViewModel extends Observable { }); } - private toError( - message: string, - retry: () => void, - headline = "Something Went Wrong", - primaryLabel: string | null = null, - ): void { - this.primaryLabelOverride = primaryLabel; + private toError(message: string, retry: () => void, headline = "Something Went Wrong"): void { this.retryAction = retry; this.status = message; this.setPhase("error"); diff --git a/tests/flash-prompt-fallback.test.cjs b/tests/flash-prompt-fallback.test.cjs deleted file mode 100644 index a82151a8..00000000 --- a/tests/flash-prompt-fallback.test.cjs +++ /dev/null @@ -1,62 +0,0 @@ -// The flash cannot always ask for confirmation on the lens: firmware older than -// the revision this build needs accepts the create-prompt write and then never -// answers it. Flashing is the only way off that firmware, so an unacked prompt -// must not be a dead end. These cover the decision at that seam. -const test = require('node:test'); -const assert = require('node:assert/strict'); -const { loader } = require('./helpers/load-typescript.cjs'); - -function fixture() { - class Observable { notifyPropertyChange() {} } - const load = loader({}, { - '@nativescript/core': { Observable, Frame: { topmost: () => null } }, - './onboarding-navigation': { finishOnboardingNavigation() {} }, - './onboarding-state': { setOnboardingCompleted() {}, setPreviewOnlyMode() {} }, - '../native/ble-permissions': { ensureBlePermissions: async () => {} }, - '../g2/device-addresses': { - isValidMacAddress: () => true, - loadDeviceAddresses: () => ({ right: 'R', left: 'L' }), - saveDeviceAddresses() {}, - }, - '../g2/firmware-builder': { - buildCustomFirmware: async () => '', buildStockFirmware: async () => '', - FirmwareBuildError: class extends Error {}, FirmwareProgress: {}, - }, - '../native/device-discovery': { buildAddressSet: () => new Set(), DeviceDiscoveryBridge: class {} }, - '../native/firmware-flasher': { FirmwareFlasher: class {} }, - '../native/flash-prompt-communicator': { FlashPromptCommunicator: class {} }, - '../g2/reconnect-policy': { resumeAutoReconnect() {}, suppressAutoReconnect() {} }, - '../util/format-error': { formatErrorMessage: (e) => String(e) }, - }); - const mod = load('app/phone-ui/onboarding-flash-view-model.ts'); - const Model = mod.OnboardingFlashViewModel ?? Object.values(mod).find((v) => typeof v === 'function'); - const model = new Model(); - const beginArgs = []; - model.beginPrompt = async (options) => { beginArgs.push(options); }; - return { model, beginArgs }; -} - -test('an unacked prompt page offers the phone-side confirmation instead of dead-ending', () => { - const { model, beginArgs } = fixture(); - model.handlePromptState('error', 'prompt page not acked: AA:BB:CC:DD:EE:FF', false); - assert.equal(model.phase, 'error'); - assert.equal(model.headline, 'Confirm On Your Phone'); - assert.equal(model.primaryLabel, 'Confirm & Install', 'the action is a confirmation, not a retry'); - assert.match(model.status, /can't show the confirmation prompt/i); - model.retryAction(); - // The loader runs the module in its own vm realm, so the options object has that - // realm's prototype and deepStrictEqual would reject it. Compare the field. - assert.equal(beginArgs.length, 1); - assert.equal(beginArgs[0].skipPrompt, true, 'confirming must skip the lens prompt, not re-ask for it'); -}); - -test('any other prompt failure still reports as an ordinary retry', () => { - const { model, beginArgs } = fixture(); - model.handlePromptState('error', 'discoverServices failed: right arm', false); - assert.equal(model.phase, 'error'); - assert.equal(model.headline, 'Something Went Wrong'); - assert.equal(model.primaryLabel, 'Retry'); - model.retryAction(); - assert.equal(beginArgs.length, 1); - assert.equal(beginArgs[0].skipPrompt, false, 'an ordinary failure must not silently skip the lens prompt'); -});