Refactor iOS version to use a bunch more shared code

This commit is contained in:
James Babcock committed 2026-09-22 21:26:51 -07:00
1 parent c3a0ba9f56
commit 4a194e37bb
40 files changed
+1874 -2605

No files matched your search

+97 -56
View File
@@ -1,83 +1,124 @@
import { Application } from '@nativescript/core'
import { StockConnection } from '../g2/stock-connection'
import { flashLensImage, validateFirmware } from '../g2/firmware-ota'
import { CFW_PATCH_SET } from '../g2/firmware/cfw-patches'
import { firmwareSha256, readFirmwareFile } from './firmware-files.ios'
import { iosBluetooth } from './ios-bluetooth'
import type { FlashState, FlashProgress } from './firmware-flasher'
export type { FlashState, FlashProgress } from './firmware-flasher'
import { kotlinListener } from './kotlin-listener.ios'
import type { FlashState, FlashProgress } from '../g2/firmware-types'
export type { FlashState, FlashProgress } from '../g2/firmware-types'
declare const FaceclawKitIosFirmwareFlasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any
/**
* iOS twin of the Android FirmwareFlasher wrapper. The OTA transfer itself is the shared
* Kotlin OtaFlashFlow over a CoreBluetooth central (FaceclawKitIosFirmwareFlasher); this
* wrapper keeps the iOS-only policy around it: the image must still hash to the prepared
* custom (or pinned stock) firmware, the phone must stay in the foreground with the screen
* awake, and MAC addresses are mapped to peripheral identifiers first.
*/
export class FirmwareFlasher {
private logs = new Set<(line: string) => void>()
private states = new Set<(state: FlashState, detail: string) => void>()
private progresses = new Set<(value: FlashProgress) => void>()
private completes = new Set<(success: boolean, detail: string) => void>()
private link = new StockConnection(iosBluetooth(), line => this.log(line))
private flasher: any = null
private listenerProxy: object | null = null
private readonly logListeners = new Set<(line: string) => void>()
private readonly progressListeners = new Set<(progress: FlashProgress) => void>()
private readonly stateListeners = new Set<(state: FlashState, detail: string) => void>()
private readonly completeListeners = new Set<(success: boolean, detail: string) => void>()
private started = false
private finished = false
private cancelled = false
private sleeps = new Set<() => void>()
private foregroundError = ''
constructor(private addresses: { right: string; left: string }, private path: string) {}
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: FlashState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
onProgress(fn: (value: FlashProgress) => void): () => void { this.progresses.add(fn); return () => this.progresses.delete(fn) }
onComplete(fn: (success: boolean, detail: string) => void): () => void { this.completes.add(fn); return () => this.completes.delete(fn) }
private log(line: string): void { for (const fn of this.logs) fn(line) }
private state(state: FlashState, detail = ''): void { for (const fn of this.states) fn(state, detail) }
start(): void { if (!this.started) { this.started = true; void this.run() } }
private previousIdle = false
constructor(private readonly addresses: { right: string; left: string }, private readonly path: string) {}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener)
return () => this.logListeners.delete(listener)
}
onProgress(listener: (progress: FlashProgress) => void): () => void {
this.progressListeners.add(listener)
return () => this.progressListeners.delete(listener)
}
onStateChange(listener: (state: FlashState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
onComplete(listener: (success: boolean, detail: string) => void): () => void {
this.completeListeners.add(listener)
return () => this.completeListeners.delete(listener)
}
start(): void {
if (this.started) return
this.started = true
void this.run()
}
private readonly backgrounded = () => {
this.foregroundError = 'Flashing was interrupted because Faceclaw left the foreground. Keep both lenses charged, return to Faceclaw, and retry both lenses.'
this.cancel()
}
private async run(): Promise<void> {
const previousIdle = UIApplication.sharedApplication.idleTimerDisabled
this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled
UIApplication.sharedApplication.idleTimerDisabled = true
Application.on(Application.suspendEvent, this.backgrounded)
let success = false, detail = ''
try {
this.link.check()
if (UIApplication.sharedApplication.applicationState !== UIApplicationState.Active) throw new Error('Keep Faceclaw open on the iPhone while flashing.')
this.state('validating')
this.emit(this.stateListeners, 'validating', '')
const image = readFirmwareFile(this.path)
const hash = firmwareSha256(image.buffer)
if (![CFW_PATCH_SET.outputSha256, CFW_PATCH_SET.baseSha256].includes(hash)) throw new Error('Prepared firmware failed SHA-256 verification. Prepare the firmware again before flashing.')
const segments = validateFirmware(image)
await this.link.resolve({ ...this.addresses, ring: '' })
for (const role of ['left', 'right']) {
if (role === 'right') {
this.state('rebooting', 'Left lens done. Both lenses reboot briefly; reconnecting for the right lens.')
await this.delay(5000)
}
this.state('connecting', role)
const deadline = Date.now() + (role === 'left' ? 30_000 : 120_000)
for (;;) {
try { await this.link.connect(role, true); await this.link.authenticate(role); break }
catch (error) { this.link.check(); this.link.disconnect(role); if (Date.now() >= deadline) throw error; await this.delay(2500) }
}
await this.delay(2500)
this.state('flashing', role)
await flashLensImage(this.link, role, image, segments, value => { for (const fn of this.progresses) fn(value) }, line => this.log(line), ms => this.delay(ms))
this.link.disconnect(role)
}
success = true; detail = 'Both lenses flashed. Your glasses are rebooting.'
const identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
if (this.cancelled) throw new Error('Cancelled.')
this.flasher = FaceclawKitIosFirmwareFlasher.alloc().initWithRightAddressLeftAddressFirmwarePath(identifiers.right ?? '', identifiers.left ?? '', this.path)
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: (
lens: string, componentIndex: number, componentCount: number, blockIndex: number, blockCount: number, bytesSent: number, bytesTotal: number,
) => this.emit(this.progressListeners, {
lens: String(lens ?? ''), componentIndex: Number(componentIndex), componentCount: Number(componentCount),
blockIndex: Number(blockIndex), blockCount: Number(blockCount), bytesSent: Number(bytesSent), bytesTotal: Number(bytesTotal),
}),
onStateStateDetail: (state: string, detail: string) => {
// The terminal state is reported by finish() so a foreground/cancel reason can replace the detail.
const name = String(state) as FlashState
if (name !== 'done' && name !== 'error') this.emit(this.stateListeners, name, String(detail ?? ''))
},
onCompleteSuccessDetail: (success: boolean, detail: string) => this.finish(Boolean(success), String(detail ?? '')),
})
this.flasher.setListenerListener(this.listenerProxy)
this.flasher.start()
} catch (error) {
detail = this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : String((error as Error).message ?? error))
} finally {
Application.off(Application.suspendEvent, this.backgrounded)
UIApplication.sharedApplication.idleTimerDisabled = previousIdle
this.link.close()
this.finish(false, String((error as Error).message ?? error))
}
this.state(success ? 'done' : 'error', detail)
for (const fn of this.completes) fn(success, detail)
}
private delay(ms: number): Promise<void> {
this.link.check()
return new Promise((resolve, reject) => {
const cancel = () => { clearTimeout(timer); this.sleeps.delete(cancel); reject(new Error('Cancelled.')) }
const timer = setTimeout(() => { this.sleeps.delete(cancel); resolve() }, ms)
this.sleeps.add(cancel)
})
private finish(success: boolean, detail: string): void {
if (this.finished) return
this.finished = true
Application.off(Application.suspendEvent, this.backgrounded)
UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle
try { this.flasher?.close() } catch { /* ignore */ }
const message = success ? detail
: this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : detail)
this.emit(this.stateListeners, success ? 'done' : 'error', message)
this.emit(this.completeListeners, success, message)
}
cancel(): void { this.cancelled = true; this.link.close(); for (const cancel of [...this.sleeps]) cancel() }
cancel(): void {
this.cancelled = true
try { this.flasher?.cancel() } catch { /* ignore */ }
// Before the Kotlin flow exists (validation, address resolution) nothing will report completion.
if (!this.flasher) this.finish(false, 'Cancelled.')
}
close(): void { this.cancel() }
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const snapshot = Array.from(listeners)
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
}
}