forked from Blendman974/faceclaw
We're going to extend the Faceclaw app onboarding process to include checking for and installing the custom firmware (currently handled externally with the scripts in ../g2flash). We'll add script scripts/resetOnboarding.sh that uses adb to reset the app on the attached phone to default/pre-onboarding state for testing. The onboarding process will explain that it's only compatible with custom firmware, with options to either skip and try out the app using only the phone-screen preview (instead of pairing with glasses), or proceed with flashing. If the user proceeds with flashing, we first connect to the glasses (with the existing mechanism), and display an on-glasses confirmation prompt. Because this prompt needs compatibility with stock firmware, it will use a different subset of the BLE protocol than the main app does (in particular, no image containers), so we'll have a separate bluetooth communication class rather than extending FaceclawBleCommunicator. We'll send an init message to start a Hub app, initialize a layout with a text container and a list, populate the text container with a message saying that flashing will void warranty and the list with No and Yes, set up a keepalive/heartbeat message timer, and wait for a menu selection. (Refer to ../g2-kit-unofficial/ble/docs and ../g2-kit-unofficial/ble/gen for protocol details). If the prompt is approved, we go to the flashing screen. On the flashing screen, we first download the official firmware, apply patches to it, and check that it has the expected result hash. See ../g2flash/build_cfw.sh and ../g2flash/apply_patches.py for details. Let's implement everything prior to the update itself now, ie, the on-device confirmation prompt, and downloading, patching and verifying the modded firmware. scripts/resetOnboarding.sh is meant to reset a pref so that Faceclaw reruns its first-run onboarding on next launch, but fails with "No onboarding keys were set. App is already in pre-onboarding state." (And onboarding doesn't trigger on next launch) Thanks. I tested the on-glasses prompt on real hardware and it worked as expected. Next up, let's port the flashing procedure itself. One detail of the flashing procedure worth noting: the two lenses get updated separately, and at the end of updating either lens, both lenses get rebooted, creating a brief period when neither is reachable, so the timeout to look for the second lens should not be too short.
520 lines
16 KiB
TypeScript
520 lines
16 KiB
TypeScript
import { Frame, Observable } from "@nativescript/core";
|
|
|
|
import { ensureBlePermissions } from "../g2/android-permissions";
|
|
import {
|
|
isValidMacAddress,
|
|
loadDeviceAddresses,
|
|
saveDeviceAddresses,
|
|
} from "../g2/device-addresses";
|
|
import {
|
|
buildCustomFirmware,
|
|
FirmwareBuildError,
|
|
FirmwareProgress,
|
|
} from "../g2/firmware-builder";
|
|
import { buildAddressSet, DeviceDiscoveryBridge } from "../native/device-discovery";
|
|
import { FirmwareFlasher, FlashProgress, FlashState } from "../native/firmware-flasher";
|
|
import { FlashPromptCommunicator, FlashPromptState } from "../native/flash-prompt-communicator";
|
|
import { setOnboardingCompleted, setPreviewOnlyMode } from "./onboarding-state";
|
|
|
|
type FlashPhase = "intro" | "prompt" | "building" | "ready" | "flashing" | "flashed" | "error";
|
|
|
|
// Kept short to fit the glasses' ~50-column text grid.
|
|
const GLASSES_WARNING =
|
|
"Flashing custom firmware may void your warranty and can brick the glasses. Continue?";
|
|
|
|
export class OnboardingFlashViewModel extends Observable {
|
|
private _phase: FlashPhase = "intro";
|
|
private _headline = "Flash Custom Firmware";
|
|
private _status = "";
|
|
private _log = "";
|
|
private _busy = false;
|
|
private _progress = 0;
|
|
|
|
private readonly discovery = new DeviceDiscoveryBridge();
|
|
private addresses: { right: string; left: string } | null = null;
|
|
private firmwarePath = "";
|
|
|
|
private prompt: FlashPromptCommunicator | null = null;
|
|
private promptUnsubscribers: Array<() => void> = [];
|
|
private flasher: FirmwareFlasher | null = null;
|
|
private flasherUnsubscribers: Array<() => void> = [];
|
|
private retryAction: () => void = () => this.beginPrompt();
|
|
|
|
// --- observable properties -------------------------------------------------
|
|
|
|
get phase(): FlashPhase {
|
|
return this._phase;
|
|
}
|
|
|
|
get headline(): string {
|
|
return this._headline;
|
|
}
|
|
|
|
set headline(value: string) {
|
|
if (this._headline !== value) {
|
|
this._headline = value;
|
|
this.notifyPropertyChange("headline", value);
|
|
}
|
|
}
|
|
|
|
get status(): string {
|
|
return this._status;
|
|
}
|
|
|
|
set status(value: string) {
|
|
if (this._status !== value) {
|
|
this._status = value;
|
|
this.notifyPropertyChange("status", value);
|
|
}
|
|
}
|
|
|
|
get log(): string {
|
|
return this._log;
|
|
}
|
|
|
|
set log(value: string) {
|
|
if (this._log !== value) {
|
|
this._log = value;
|
|
this.notifyPropertyChange("log", value);
|
|
this.notifyPropertyChange("logVisibility", this.logVisibility);
|
|
}
|
|
}
|
|
|
|
get logVisibility(): "visible" | "collapse" {
|
|
return this._log ? "visible" : "collapse";
|
|
}
|
|
|
|
get busy(): boolean {
|
|
return this._busy;
|
|
}
|
|
|
|
set busy(value: boolean) {
|
|
if (this._busy !== value) {
|
|
this._busy = value;
|
|
this.notifyPropertyChange("busy", value);
|
|
this.notifyPropertyChange("busyVisibility", this.busyVisibility);
|
|
}
|
|
}
|
|
|
|
get busyVisibility(): "visible" | "collapse" {
|
|
return this._busy ? "visible" : "collapse";
|
|
}
|
|
|
|
get progress(): number {
|
|
return this._progress;
|
|
}
|
|
|
|
set progress(value: number) {
|
|
const clamped = Math.max(0, Math.min(100, Math.round(value)));
|
|
if (this._progress !== clamped) {
|
|
this._progress = clamped;
|
|
this.notifyPropertyChange("progress", clamped);
|
|
}
|
|
}
|
|
|
|
get progressVisibility(): "visible" | "collapse" {
|
|
return this._phase === "flashing" ? "visible" : "collapse";
|
|
}
|
|
|
|
get primaryLabel(): string {
|
|
switch (this._phase) {
|
|
case "intro":
|
|
return "Connect & Confirm";
|
|
case "ready":
|
|
return "Flash Now";
|
|
case "flashed":
|
|
return "Finish";
|
|
case "error":
|
|
return "Retry";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
get secondaryLabel(): string {
|
|
switch (this._phase) {
|
|
case "prompt":
|
|
return "Cancel";
|
|
case "ready":
|
|
return "Not Now";
|
|
default:
|
|
return "Back";
|
|
}
|
|
}
|
|
|
|
get primaryVisibility(): "visible" | "collapse" {
|
|
return this.primaryLabel ? "visible" : "collapse";
|
|
}
|
|
|
|
get secondaryVisibility(): "visible" | "collapse" {
|
|
// No escape hatch mid-write (building/flashing) or after success (flashed).
|
|
return this._phase === "building" || this._phase === "flashing" || this._phase === "flashed"
|
|
? "collapse"
|
|
: "visible";
|
|
}
|
|
|
|
// --- button handlers -------------------------------------------------------
|
|
|
|
onPrimaryTap(): void {
|
|
switch (this._phase) {
|
|
case "intro":
|
|
void this.beginPrompt();
|
|
return;
|
|
case "ready":
|
|
this.startFlashing();
|
|
return;
|
|
case "flashed":
|
|
this.finish();
|
|
return;
|
|
case "error":
|
|
this.retryAction();
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
onSecondaryTap(): void {
|
|
if (this._phase === "prompt") {
|
|
this.cancelPrompt();
|
|
return;
|
|
}
|
|
if (this._phase === "building" || this._phase === "flashing" || this._phase === "flashed") {
|
|
return;
|
|
}
|
|
this.leave();
|
|
}
|
|
|
|
// --- prompt flow -----------------------------------------------------------
|
|
|
|
private async beginPrompt(): Promise<void> {
|
|
if (!global.isAndroid) {
|
|
this.toError("Flashing is only available on Android.", () => this.beginPrompt());
|
|
return;
|
|
}
|
|
this.setPhase("prompt");
|
|
this.headline = "Confirm On Your Glasses";
|
|
this.log = "";
|
|
this.busy = true;
|
|
this.status = "Preparing to connect. Make sure your glasses are on and the Even app is disconnected.";
|
|
|
|
try {
|
|
await ensureBlePermissions();
|
|
const addresses = await this.resolveAddresses();
|
|
if (!addresses) {
|
|
this.busy = false;
|
|
this.toError(
|
|
"Couldn't find both glasses arms. Make sure the glasses are powered on and the Even app is disconnected, then retry.",
|
|
() => this.beginPrompt(),
|
|
);
|
|
return;
|
|
}
|
|
this.addresses = addresses;
|
|
|
|
this.status = "Connecting to your glasses. Watch the lens for a Yes/No prompt.";
|
|
this.startCommunicator(addresses);
|
|
} catch (error) {
|
|
this.busy = false;
|
|
this.toError(this.formatError(error), () => this.beginPrompt());
|
|
}
|
|
}
|
|
|
|
private startCommunicator(addresses: { right: string; left: string }): void {
|
|
this.disposePrompt();
|
|
const prompt = new FlashPromptCommunicator(addresses, GLASSES_WARNING);
|
|
this.prompt = prompt;
|
|
|
|
this.promptUnsubscribers.push(
|
|
prompt.onLog((line) => this.appendLog(line)),
|
|
prompt.onStateChange((state, detail) => this.handlePromptState(state, detail)),
|
|
prompt.onResult((approved) => this.handlePromptResult(approved)),
|
|
);
|
|
prompt.start();
|
|
}
|
|
|
|
private handlePromptState(state: FlashPromptState, detail: string): void {
|
|
switch (state) {
|
|
case "connecting":
|
|
this.status = "Connecting to your glasses...";
|
|
break;
|
|
case "connected":
|
|
this.status = "Connected. Showing the confirmation on the lens...";
|
|
break;
|
|
case "prompting":
|
|
this.status =
|
|
"Look at the lens and choose Yes to flash or No to cancel. (Use the ring or a temple tap to select.)";
|
|
break;
|
|
case "result":
|
|
// Handled by onResult.
|
|
break;
|
|
case "cancelled":
|
|
this.busy = false;
|
|
this.toError("Cancelled.", () => this.beginPrompt());
|
|
break;
|
|
case "timeout":
|
|
this.busy = false;
|
|
this.toError(detail || "No response from the glasses.", () => this.beginPrompt());
|
|
break;
|
|
case "disconnected":
|
|
this.busy = false;
|
|
this.toError(detail || "Lost connection to the glasses.", () => this.beginPrompt());
|
|
break;
|
|
case "error":
|
|
this.busy = false;
|
|
this.toError(detail || "Connection failed.", () => this.beginPrompt());
|
|
break;
|
|
}
|
|
}
|
|
|
|
private handlePromptResult(approved: boolean): void {
|
|
this.disposePrompt();
|
|
if (!approved) {
|
|
this.busy = false;
|
|
this.toError("You declined on the glasses. No firmware was written.", () => this.beginPrompt());
|
|
return;
|
|
}
|
|
void this.buildFirmware();
|
|
}
|
|
|
|
private cancelPrompt(): void {
|
|
this.status = "Cancelling...";
|
|
try {
|
|
this.prompt?.cancel();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
this.disposePrompt();
|
|
this.busy = false;
|
|
this.toError("Cancelled.", () => this.beginPrompt());
|
|
}
|
|
|
|
// --- firmware build flow ---------------------------------------------------
|
|
|
|
private async buildFirmware(): Promise<void> {
|
|
this.setPhase("building");
|
|
this.headline = "Preparing Firmware";
|
|
this.busy = true;
|
|
this.status = "Confirmed. Downloading the stock firmware...";
|
|
|
|
try {
|
|
const result = await buildCustomFirmware((progress) => this.reportBuildProgress(progress));
|
|
this.firmwarePath = result.path;
|
|
this.busy = false;
|
|
this.setPhase("ready");
|
|
this.headline = "Firmware Ready";
|
|
this.status =
|
|
`Custom firmware prepared and verified (${result.bytes.toLocaleString()} bytes).\n\n` +
|
|
"Tap Flash Now to write it to your glasses. Keep both lenses powered on and nearby — " +
|
|
"each lens takes a few minutes, and the glasses will reboot when each lens finishes. " +
|
|
"Do not close the app during flashing.";
|
|
this.appendLog(`saved to ${result.path}`);
|
|
} catch (error) {
|
|
this.busy = false;
|
|
const message =
|
|
error instanceof FirmwareBuildError ? error.message : this.formatError(error);
|
|
this.toError(message, () => this.buildFirmware());
|
|
}
|
|
}
|
|
|
|
private reportBuildProgress(progress: FirmwareProgress): void {
|
|
switch (progress.phase) {
|
|
case "downloading":
|
|
this.status = "Downloading the stock firmware from Even's CDN...";
|
|
break;
|
|
case "verifying-base":
|
|
this.status = "Verifying the downloaded firmware...";
|
|
break;
|
|
case "patching":
|
|
this.status = `Applying patches (${progress.applied}/${progress.total})...`;
|
|
break;
|
|
case "verifying-output":
|
|
this.status = "Verifying the patched firmware...";
|
|
break;
|
|
case "writing":
|
|
this.status = "Saving the prepared firmware...";
|
|
break;
|
|
case "done":
|
|
break;
|
|
}
|
|
}
|
|
|
|
// --- flashing flow ---------------------------------------------------------
|
|
|
|
private startFlashing(): void {
|
|
if (!this.addresses || !this.firmwarePath) {
|
|
this.toError("Missing glasses addresses or firmware; start over.", () => this.beginPrompt());
|
|
return;
|
|
}
|
|
this.setPhase("flashing");
|
|
this.headline = "Flashing Firmware";
|
|
this.busy = true;
|
|
this.progress = 0;
|
|
this.status = "Starting. Do not close the app or power off the glasses.";
|
|
|
|
this.disposeFlasher();
|
|
const flasher = new FirmwareFlasher(this.addresses, this.firmwarePath);
|
|
this.flasher = flasher;
|
|
this.flasherUnsubscribers.push(
|
|
flasher.onLog((line) => this.appendLog(line)),
|
|
flasher.onProgress((progress) => this.handleFlashProgress(progress)),
|
|
flasher.onStateChange((state, detail) => this.handleFlashState(state, detail)),
|
|
flasher.onComplete((success, detail) => this.handleFlashComplete(success, detail)),
|
|
);
|
|
flasher.start();
|
|
}
|
|
|
|
private handleFlashState(state: FlashState, detail: string): void {
|
|
switch (state) {
|
|
case "validating":
|
|
this.status = "Validating the firmware image...";
|
|
break;
|
|
case "connecting":
|
|
this.status = `Connecting to the ${detail || ""} lens...`.replace(" ", " ");
|
|
break;
|
|
case "flashing":
|
|
this.status = `Flashing the ${detail || ""} lens. Keep the glasses on and nearby.`.replace(" ", " ");
|
|
break;
|
|
case "rebooting":
|
|
this.status = detail || "Lenses rebooting; reconnecting...";
|
|
break;
|
|
case "done":
|
|
case "error":
|
|
// Handled by onComplete.
|
|
break;
|
|
}
|
|
}
|
|
|
|
private handleFlashProgress(progress: FlashProgress): void {
|
|
const lensIndex = progress.lens === "right" ? 1 : 0;
|
|
const withinLens =
|
|
progress.componentCount > 0
|
|
? (progress.componentIndex - 1 + (progress.blockCount > 0 ? progress.blockIndex / progress.blockCount : 0)) /
|
|
progress.componentCount
|
|
: 0;
|
|
this.progress = ((lensIndex + withinLens) / 2) * 100;
|
|
this.status =
|
|
`Flashing ${progress.lens} lens — part ${progress.componentIndex}/${progress.componentCount}, ` +
|
|
`block ${progress.blockIndex}/${progress.blockCount}. Keep the glasses on and nearby.`;
|
|
}
|
|
|
|
private handleFlashComplete(success: boolean, detail: string): void {
|
|
this.disposeFlasher();
|
|
this.busy = false;
|
|
if (success) {
|
|
this.progress = 100;
|
|
this.setPhase("flashed");
|
|
this.headline = "All Done";
|
|
this.status = detail || "Firmware installed. Your glasses are rebooting into Faceclaw's custom firmware.";
|
|
return;
|
|
}
|
|
this.toError(detail || "Flashing failed.", () => this.startFlashing());
|
|
}
|
|
|
|
// --- helpers ---------------------------------------------------------------
|
|
|
|
private async resolveAddresses(): Promise<{ right: string; left: string } | null> {
|
|
const stored = loadDeviceAddresses();
|
|
if (isValidMacAddress(stored.right) && isValidMacAddress(stored.left)) {
|
|
return { right: stored.right, left: stored.left };
|
|
}
|
|
|
|
this.status = "Scanning for your glasses...";
|
|
const candidates = await this.discovery.scanCandidates(8000);
|
|
const found = buildAddressSet(candidates);
|
|
if (isValidMacAddress(found.right) && isValidMacAddress(found.left)) {
|
|
saveDeviceAddresses({ right: found.right, left: found.left, ring: found.ring || stored.ring });
|
|
return { right: found.right, left: found.left };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private finish(): void {
|
|
setPreviewOnlyMode(false);
|
|
setOnboardingCompleted(true);
|
|
this.disposePrompt();
|
|
this.disposeFlasher();
|
|
Frame.topmost()?.navigate({
|
|
moduleName: "phone-ui/main-page",
|
|
clearHistory: true,
|
|
});
|
|
}
|
|
|
|
private leave(): void {
|
|
this.disposePrompt();
|
|
this.disposeFlasher();
|
|
const frame = Frame.topmost();
|
|
if (frame?.canGoBack()) {
|
|
frame.goBack();
|
|
} else {
|
|
frame?.navigate({ moduleName: "phone-ui/onboarding-page", clearHistory: true });
|
|
}
|
|
}
|
|
|
|
private toError(message: string, retry: () => void): void {
|
|
this.retryAction = retry;
|
|
this.status = message;
|
|
this.setPhase("error");
|
|
this.headline = "Something Went Wrong";
|
|
}
|
|
|
|
private setPhase(phase: FlashPhase): void {
|
|
if (this._phase !== phase) {
|
|
this._phase = phase;
|
|
this.notifyPropertyChange("phase", phase);
|
|
}
|
|
this.notifyPropertyChange("primaryLabel", this.primaryLabel);
|
|
this.notifyPropertyChange("secondaryLabel", this.secondaryLabel);
|
|
this.notifyPropertyChange("primaryVisibility", this.primaryVisibility);
|
|
this.notifyPropertyChange("secondaryVisibility", this.secondaryVisibility);
|
|
this.notifyPropertyChange("progressVisibility", this.progressVisibility);
|
|
}
|
|
|
|
private appendLog(line: string): void {
|
|
const stamp = new Date().toISOString().slice(11, 19);
|
|
this.log = this._log ? `${this._log}\n[${stamp}] ${line}` : `[${stamp}] ${line}`;
|
|
}
|
|
|
|
private disposePrompt(): void {
|
|
for (const unsubscribe of this.promptUnsubscribers) {
|
|
try {
|
|
unsubscribe();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
this.promptUnsubscribers = [];
|
|
if (this.prompt) {
|
|
try {
|
|
this.prompt.close();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
this.prompt = null;
|
|
}
|
|
}
|
|
|
|
private disposeFlasher(): void {
|
|
for (const unsubscribe of this.flasherUnsubscribers) {
|
|
try {
|
|
unsubscribe();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
this.flasherUnsubscribers = [];
|
|
if (this.flasher) {
|
|
try {
|
|
this.flasher.close();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
this.flasher = null;
|
|
}
|
|
}
|
|
|
|
private formatError(error: unknown): string {
|
|
const raw = (error as Error)?.message ?? String(error);
|
|
return raw.replace(/[\x00-\x1f]+/g, " ").replace(/\s+/g, " ").trim();
|
|
}
|
|
}
|