mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
# Conflicts: # native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt
886 lines
38 KiB
TypeScript
886 lines
38 KiB
TypeScript
import type { RingInput } from "../g2/ring-input";
|
||
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
|
||
import { REQUIRED_FACECLAW_FIRMWARE_VERSION, type FirmwareInfo } from "../g2/firmware-compat";
|
||
import { type CompassEvent } from "./compass-types";
|
||
import { type ImuReading } from "./imu-types";
|
||
import { fromData, toData } from "./kotlin-data";
|
||
import { previewPixels } from "./ios-graphics";
|
||
import { iosBluetooth } from "./ios-bluetooth";
|
||
|
||
export type { FirmwareInfo };
|
||
|
||
// Kotlin/Native facades exported by FaceclawKit (see native/kotlin/shared/src/iosMain/.../ble).
|
||
declare const FaceclawKitIosGlassesSession: any;
|
||
declare const FaceclawKitFaceclawBleCommunicatorListener: any;
|
||
declare const FaceclawKitIosAncsListener: any;
|
||
declare const FaceclawKitIosAudioPacketListener: any;
|
||
declare const FaceclawKitFaceclawCompassListener: any;
|
||
declare const FaceclawKitFaceclawImuListener: any;
|
||
declare const FaceclawKitIosAmbientLightListener: any;
|
||
declare const FaceclawKitIosMicStatusListener: any;
|
||
|
||
|
||
export type CommunicatorPhase =
|
||
| "disconnected"
|
||
| "connecting"
|
||
| "connected"
|
||
| "charging"
|
||
| "retrying"
|
||
| "unpaired"
|
||
| "incompatible-firmware"
|
||
| "disconnecting";
|
||
|
||
export type CommunicatorState = {
|
||
phase: CommunicatorPhase;
|
||
status: string;
|
||
};
|
||
|
||
export type HeadsetBatteryState = {
|
||
battery: number;
|
||
chargingStatus: number;
|
||
ringBattery?: number;
|
||
ringChargingStatus?: number;
|
||
};
|
||
|
||
/** See the Android communicator's CaseBatteryState. */
|
||
export type CaseBatteryState = { battery: number | null; charging: boolean; docked: boolean };
|
||
|
||
export type FrameMetrics = {
|
||
paintMs: number;
|
||
transmitMs: number;
|
||
tileCount: number;
|
||
};
|
||
|
||
/** See the Android twin: compositor surface configuration in screen pixels. */
|
||
export type SurfaceOptions = {
|
||
x: number;
|
||
y: number;
|
||
width: number;
|
||
height: number;
|
||
zOrder: number;
|
||
transparency: "opaque" | "color-key";
|
||
};
|
||
|
||
export type RawInputEvent = { ringInput?: RingInput } & (
|
||
| { kind: "list-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||
| { kind: "text-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||
| { kind: "sys-event"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||
| { kind: "even-ai"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||
| { kind: "watch-gesture"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||
| { kind: "display-wake"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
|
||
);
|
||
|
||
function nonNegativeNumber(value: number): number {
|
||
const numeric = Number(value);
|
||
return Number.isFinite(numeric) ? Math.max(0, numeric) : 0;
|
||
}
|
||
|
||
/** A 0..1 brightness factor as the compositor's 0..256 fixed-point form. */
|
||
export function dimFactor256(factor: number): number {
|
||
return Math.round(Math.max(0, Math.min(1, factor)) * 256);
|
||
}
|
||
|
||
function timestamp(): string {
|
||
const now = new Date();
|
||
const pad = (value: number, width = 2) => String(value).padStart(width, "0");
|
||
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`;
|
||
}
|
||
|
||
export type PeripheralIdentifiers = { right: string; left: string; ring: string };
|
||
|
||
/**
|
||
* Map the configured MAC addresses to CoreBluetooth identifiers through the scan
|
||
* layer (app/native/ios-bluetooth.ts): the mapping a previous scan persisted, else a
|
||
* fresh scan that classifies Even advertisements. The scan layer also owns Bluetooth
|
||
* readiness (creating the central triggers the permission prompt on first use).
|
||
*/
|
||
export async function resolveIosPeripherals(addresses: { right: string; left: string; ring?: string }): Promise<PeripheralIdentifiers> {
|
||
const resolved = await iosBluetooth().resolveDevices({ right: addresses.right, left: addresses.left, ring: addresses.ring ?? "" });
|
||
return {
|
||
right: String(resolved.right ?? "").toUpperCase(),
|
||
left: String(resolved.left ?? "").toUpperCase(),
|
||
ring: String(resolved.ring ?? "").toUpperCase(),
|
||
};
|
||
}
|
||
|
||
/** Bluetooth powered on and authorized (prompts for permission the first time); rejects with the user-facing reason. */
|
||
export function ensureIosBluetoothReady(): Promise<void> {
|
||
return iosBluetooth().ensureReady();
|
||
}
|
||
|
||
@NativeClass()
|
||
class SessionListener extends NSObject {
|
||
static ObjCProtocols = [FaceclawKitFaceclawBleCommunicatorListener];
|
||
bridge!: FaceclawCommunicatorBridge;
|
||
onStateChangePhaseStatus(phase: string, status: string): void { this.bridge.handleStateChange(String(phase ?? ""), String(status ?? "")); }
|
||
onRingEventKindContainerNameEventTypeEventSourceSystemExitReasonCodeFrameIdRingTickRingTypeRingAuxRingSpeed(
|
||
kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
|
||
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number,
|
||
): void {
|
||
this.bridge.handleRingEvent(String(kind ?? ""), String(containerName ?? ""), Number(eventType), Number(eventSource),
|
||
Number(systemExitReasonCode), Number(frameId), Number(ringTick), Number(ringType), Number(ringAux), Number(ringSpeed));
|
||
}
|
||
onBatteryStateHeadsetBatteryHeadsetChargingRingBatteryRingCharging(headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number): void {
|
||
this.bridge.handleBatteryState(Number(headsetBattery), Number(headsetCharging), Number(ringBattery), Number(ringCharging));
|
||
}
|
||
onCaseStateCaseBatteryCaseChargingCaseDocked(caseBattery: number, caseCharging: number, caseDocked: boolean): void {
|
||
this.bridge.handleCaseState(Number(caseBattery), Number(caseCharging), Boolean(caseDocked));
|
||
}
|
||
onSilentModeSilent(silent: boolean): void { this.bridge.handleSilentMode(Boolean(silent)); }
|
||
onWearStateWearing(wearing: boolean): void { this.bridge.handleWearState(Boolean(wearing)); }
|
||
onPhoneLockStateLocked(locked: boolean): void { this.bridge.handlePhoneLockState(Boolean(locked)); }
|
||
onEvenAppConflictMessage(message: string): void { this.bridge.handleEvenAppConflict(String(message ?? "")); }
|
||
onFrameMetricsPaintMsTransmitMsTileCount(paintMs: number, transmitMs: number, tileCount: number): void {
|
||
this.bridge.handleFrameMetrics(Number(paintMs), Number(transmitMs), Number(tileCount));
|
||
}
|
||
onFrameFinishedFrameIdOutcome(frameId: number, outcome: string): void { this.bridge.handleFrameFinished(Number(frameId), String(outcome ?? "")); }
|
||
onFirmwareInfoLeftVersionRightVersionExtension(leftVersion: string, rightVersion: string, extension: string): void {
|
||
this.bridge.handleFirmwareInfo(String(leftVersion ?? ""), String(rightVersion ?? ""), String(extension ?? ""));
|
||
}
|
||
}
|
||
|
||
@NativeClass()
|
||
class AncsListener extends NSObject {
|
||
static ObjCProtocols = [FaceclawKitIosAncsListener];
|
||
bridge!: FaceclawCommunicatorBridge;
|
||
onAncsAuthorizationAuthorized(authorized: boolean): void { this.bridge.handleAncsAuthorization(Boolean(authorized)); }
|
||
onRelayFrameData(data: NSData): void { this.bridge.handleAncsRelayFrame(fromData(data)); }
|
||
}
|
||
|
||
@NativeClass()
|
||
class AudioPacketListener extends NSObject {
|
||
static ObjCProtocols = [FaceclawKitIosAudioPacketListener];
|
||
handler!: (packet: Uint8Array, arm: string, arrivalMs: number) => void;
|
||
onNsDataAudioPacketDataArmArrivalMs(data: NSData, arm: string, arrivalMs: number): void {
|
||
this.handler(fromData(data), String(arm ?? ""), Number(arrivalMs));
|
||
}
|
||
}
|
||
|
||
@NativeClass()
|
||
class CompassListener extends NSObject {
|
||
static ObjCProtocols = [FaceclawKitFaceclawCompassListener];
|
||
handler!: (event: CompassEvent) => void;
|
||
onCompassEventCommandHeadingDegreesMagneticAccuracyMagneticAnomaliesOrientationSourceDiagnosticFlagsSampleTimeMs(
|
||
command: number, headingDegrees: number, magneticAccuracy: number, magneticAnomalies: number,
|
||
orientationSource: number, diagnosticFlags: number, sampleTimeMs: number,
|
||
): void {
|
||
this.handler({
|
||
command: Number(command), headingDegrees: Number(headingDegrees),
|
||
diagnostics: diagnosticFlags >= 0 ? {
|
||
magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies),
|
||
orientationSource: Number(orientationSource), flags: Number(diagnosticFlags), sampleTimeMs: Number(sampleTimeMs),
|
||
} : undefined,
|
||
});
|
||
}
|
||
}
|
||
|
||
@NativeClass()
|
||
class ImuListener extends NSObject {
|
||
static ObjCProtocols = [FaceclawKitFaceclawImuListener];
|
||
handler!: (reading: ImuReading) => void;
|
||
onImuDataXYZEventSource(x: number, y: number, z: number, eventSource: number): void {
|
||
this.handler({ x: Number(x), y: Number(y), z: Number(z), source: Number(eventSource) });
|
||
}
|
||
}
|
||
|
||
@NativeClass()
|
||
class AmbientLightListener extends NSObject {
|
||
static ObjCProtocols = [FaceclawKitIosAmbientLightListener];
|
||
handler!: (body: Uint8Array) => void;
|
||
onNsDataAmbientLightBody(body: NSData): void { this.handler(fromData(body)); }
|
||
}
|
||
|
||
@NativeClass()
|
||
class MicStatusListener extends NSObject {
|
||
static ObjCProtocols = [FaceclawKitIosMicStatusListener];
|
||
handler!: (body: Uint8Array, arm: string) => void;
|
||
onNsDataMicStatusBodyArm(body: NSData, arm: string): void { this.handler(fromData(body), String(arm ?? "")); }
|
||
}
|
||
|
||
/**
|
||
* The iOS twin of the Android communicator bridge: the same public API over
|
||
* the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth
|
||
* underneath). Addresses are CoreBluetooth identifiers (resolveIosPeripherals
|
||
* maps the configured MACs). Every Kotlin callback already arrives on the
|
||
* main queue; listener fan-out is still deferred with setTimeout like on
|
||
* Android so callers never re-enter the bridge from a Kotlin frame.
|
||
*/
|
||
export class FaceclawCommunicatorBridge {
|
||
// The started, not yet closed bridge (see getActive).
|
||
private static active: FaceclawCommunicatorBridge | null = null;
|
||
private readonly communicator: any;
|
||
private readonly listenerProxy: SessionListener;
|
||
private ancsProxy: AncsListener | null = null;
|
||
private nativeCallQueue: Promise<void> = Promise.resolve();
|
||
private queuedNativeCalls = 0;
|
||
// Surfaces Kotlin holds, as of the queued call now running (see hasNativeSurface).
|
||
private readonly nativeSurfaces = new Set<string>();
|
||
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
|
||
private readonly finishedFrameOutcomes = new Map<number, string>();
|
||
private readonly frameFinishedWaiters = new Map<number, Set<(outcome: string) => void>>();
|
||
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
|
||
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
|
||
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
|
||
private readonly caseListeners = new Set<(state: CaseBatteryState) => void>();
|
||
private readonly silentModeListeners = new Set<(silent: boolean) => void>();
|
||
private readonly wearStateListeners = new Set<(wearing: boolean) => void>();
|
||
private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>();
|
||
private latestWearState: boolean | null = null;
|
||
private latestPhoneLockState: boolean | null = null;
|
||
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
|
||
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
|
||
private readonly previewAnimationListeners = new Set<() => void>();
|
||
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
|
||
private readonly ancsAuthorizationListeners = new Set<(authorized: boolean) => void>();
|
||
private readonly ancsRelayListeners = new Set<(frame: Uint8Array) => void>();
|
||
private audioListener: AudioPacketListener | null = null;
|
||
private microphoneWork: Promise<void> = Promise.resolve();
|
||
private microphoneToken = 0;
|
||
private closed = false;
|
||
|
||
/**
|
||
* The bridge whose session is running, for modules that reach the glasses
|
||
* without the bridge threaded through (imu, ambient-light, audio-monitor):
|
||
* the iOS stand-in for Android's FaceclawBleCommunicator.getActive().
|
||
* Set by start(), cleared by close().
|
||
*/
|
||
static getActive(): FaceclawCommunicatorBridge | null {
|
||
return FaceclawCommunicatorBridge.active;
|
||
}
|
||
|
||
constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string }) {
|
||
this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress(
|
||
String(addresses.right ?? "").toUpperCase(),
|
||
String(addresses.left ?? "").toUpperCase(),
|
||
String(addresses.ring ?? "").toUpperCase(),
|
||
);
|
||
// The shared session halts on any other firmware ("incompatible-firmware").
|
||
this.communicator.setRequiredFirmwareRevisionRevision(REQUIRED_FACECLAW_FIRMWARE_VERSION);
|
||
// Guarded: an older framework build may not export it yet.
|
||
if (addresses.ringRole && typeof this.communicator.setDirectRingRoleRole === "function") {
|
||
this.communicator.setDirectRingRoleRole(addresses.ringRole);
|
||
}
|
||
this.listenerProxy = SessionListener.new() as SessionListener;
|
||
this.listenerProxy.bridge = this;
|
||
this.communicator.setListenerListener(this.listenerProxy);
|
||
// Kotlin calls this on the main queue.
|
||
this.communicator.setPreviewAnimationListenerListener(() => {
|
||
for (const listener of Array.from(this.previewAnimationListeners)) listener();
|
||
});
|
||
}
|
||
|
||
// ----- Kotlin listener entry points (main queue) -----------------------------------
|
||
|
||
/** @internal */
|
||
handleStateChange(phase: string, status: string): void {
|
||
this.emitAsync(this.stateListeners, { phase: phase as CommunicatorPhase, status });
|
||
}
|
||
|
||
/** @internal */
|
||
handleRingEvent(kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
|
||
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number): void {
|
||
const event = {
|
||
kind: kind as RawInputEvent["kind"], containerName, eventType, eventSource, systemExitReasonCode, frameId,
|
||
ringInput: ringTick >= 0 ? { tick: ringTick, type: ringType, aux: ringAux, speed: ringSpeed } : undefined,
|
||
} as RawInputEvent;
|
||
this.emitAsync(this.ringListeners, event);
|
||
}
|
||
|
||
/** @internal */
|
||
handleBatteryState(battery: number, chargingStatus: number, ringBattery: number, ringChargingStatus: number): void {
|
||
this.emitAsync(this.batteryListeners, { battery, chargingStatus, ringBattery, ringChargingStatus });
|
||
}
|
||
|
||
/** @internal */
|
||
handleCaseState(battery: number, charging: number, docked: boolean): void {
|
||
this.emitAsync(this.caseListeners, {
|
||
battery: Number.isInteger(battery) && battery >= 0 && battery <= 100 ? battery : null,
|
||
charging: charging > 0,
|
||
docked,
|
||
});
|
||
}
|
||
|
||
/** @internal */
|
||
handleSilentMode(silent: boolean): void { this.emitAsync(this.silentModeListeners, silent); }
|
||
|
||
/** @internal */
|
||
handleWearState(wearing: boolean): void {
|
||
this.latestWearState = wearing;
|
||
this.emitAsync(this.wearStateListeners, wearing);
|
||
}
|
||
|
||
/** @internal */
|
||
handlePhoneLockState(locked: boolean): void {
|
||
this.latestPhoneLockState = locked;
|
||
this.emitAsync(this.phoneLockStateListeners, locked);
|
||
}
|
||
|
||
/** @internal */
|
||
handleEvenAppConflict(message: string): void { this.emitAsync(this.evenAppConflictListeners, message); }
|
||
|
||
/** @internal */
|
||
handleFrameMetrics(paintMs: number, transmitMs: number, tileCount: number): void {
|
||
const metrics = { paintMs: nonNegativeNumber(paintMs), transmitMs: nonNegativeNumber(transmitMs), tileCount: nonNegativeNumber(tileCount) };
|
||
const waiters = Array.from(this.frameMetricWaiters);
|
||
this.frameMetricWaiters.clear();
|
||
for (const waiter of waiters) setTimeout(() => waiter(metrics), 0);
|
||
this.emitAsync(this.frameMetricsListeners, metrics);
|
||
}
|
||
|
||
/** @internal */
|
||
handleFrameFinished(frameId: number, outcome: string): void { this.recordFrameFinished(frameId, outcome); }
|
||
|
||
/** @internal */
|
||
handleFirmwareInfo(leftVersion: string, rightVersion: string, extension: string): void {
|
||
this.emitAsync(this.firmwareInfoListeners, { leftVersion, rightVersion, extension });
|
||
}
|
||
|
||
/** @internal */
|
||
handleAncsAuthorization(authorized: boolean): void { this.emitAsync(this.ancsAuthorizationListeners, authorized); }
|
||
|
||
/** @internal */
|
||
handleAncsRelayFrame(frame: Uint8Array): void { this.emitAsync(this.ancsRelayListeners, frame); }
|
||
|
||
private emitAsync<T>(listeners: Set<(value: T) => void>, value: T): void {
|
||
const snapshot = Array.from(listeners);
|
||
setTimeout(() => {
|
||
for (const listener of snapshot) listener(value);
|
||
}, 0);
|
||
}
|
||
|
||
/** Same FIFO as the Android bridge: native calls stay ordered; the fast path skips a task hop when idle. */
|
||
private enqueueNativeCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
|
||
if (inlineWhenIdle && this.queuedNativeCalls === 0) {
|
||
try {
|
||
return Promise.resolve(operation());
|
||
} catch (error) {
|
||
return Promise.reject(error);
|
||
}
|
||
}
|
||
this.queuedNativeCalls++;
|
||
const run = () =>
|
||
new Promise<T>((resolve, reject) => {
|
||
setTimeout(() => {
|
||
this.queuedNativeCalls--;
|
||
try {
|
||
resolve(operation());
|
||
} catch (error) {
|
||
reject(error);
|
||
}
|
||
}, 0);
|
||
});
|
||
const result = this.nativeCallQueue.then(run, run);
|
||
this.nativeCallQueue = result.then(() => undefined, () => undefined);
|
||
return result;
|
||
}
|
||
|
||
// ----- listeners -------------------------------------------------------------------
|
||
|
||
onStateChange(listener: (state: CommunicatorState) => void): () => void {
|
||
this.stateListeners.add(listener);
|
||
return () => this.stateListeners.delete(listener);
|
||
}
|
||
|
||
onRingEvent(listener: (event: RawInputEvent) => void): () => void {
|
||
this.ringListeners.add(listener);
|
||
return () => this.ringListeners.delete(listener);
|
||
}
|
||
|
||
onBatteryState(listener: (state: HeadsetBatteryState) => void): () => void {
|
||
this.batteryListeners.add(listener);
|
||
return () => this.batteryListeners.delete(listener);
|
||
}
|
||
|
||
onCaseState(listener: (state: CaseBatteryState) => void): () => void {
|
||
this.caseListeners.add(listener);
|
||
return () => this.caseListeners.delete(listener);
|
||
}
|
||
|
||
onSilentMode(listener: (silent: boolean) => void): () => void {
|
||
this.silentModeListeners.add(listener);
|
||
return () => this.silentModeListeners.delete(listener);
|
||
}
|
||
|
||
onWearState(listener: (wearing: boolean) => void): () => void {
|
||
this.wearStateListeners.add(listener);
|
||
if (this.latestWearState !== null) {
|
||
const wearing = this.latestWearState;
|
||
setTimeout(() => { if (this.wearStateListeners.has(listener)) listener(wearing); }, 0);
|
||
}
|
||
return () => this.wearStateListeners.delete(listener);
|
||
}
|
||
|
||
onPhoneLockState(listener: (locked: boolean) => void): () => void {
|
||
this.phoneLockStateListeners.add(listener);
|
||
if (this.latestPhoneLockState !== null) {
|
||
const locked = this.latestPhoneLockState;
|
||
setTimeout(() => { if (this.phoneLockStateListeners.has(listener)) listener(locked); }, 0);
|
||
}
|
||
return () => this.phoneLockStateListeners.delete(listener);
|
||
}
|
||
|
||
onEvenAppConflict(listener: (message: string) => void): () => void {
|
||
this.evenAppConflictListeners.add(listener);
|
||
return () => this.evenAppConflictListeners.delete(listener);
|
||
}
|
||
|
||
onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void {
|
||
this.frameMetricsListeners.add(listener);
|
||
return () => this.frameMetricsListeners.delete(listener);
|
||
}
|
||
|
||
/**
|
||
* Each step of an animation the phone preview is replaying (a menu slide
|
||
* reaches the glasses as one frame, so frame metrics fire only at its start).
|
||
*/
|
||
onPreviewAnimationFrame(listener: () => void): () => void {
|
||
this.previewAnimationListeners.add(listener);
|
||
return () => this.previewAnimationListeners.delete(listener);
|
||
}
|
||
|
||
onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void {
|
||
this.firmwareInfoListeners.add(listener);
|
||
return () => this.firmwareInfoListeners.delete(listener);
|
||
}
|
||
|
||
getNativeCommunicator(): any {
|
||
return this.communicator;
|
||
}
|
||
|
||
private recordFrameFinished(frameId: number, outcome: string): void {
|
||
if (!Number.isFinite(frameId) || frameId <= 0) return;
|
||
this.finishedFrameOutcomes.set(frameId, outcome);
|
||
while (this.finishedFrameOutcomes.size > 128) {
|
||
const oldest = this.finishedFrameOutcomes.keys().next().value;
|
||
if (oldest === undefined) break;
|
||
this.finishedFrameOutcomes.delete(oldest);
|
||
}
|
||
const waiters = this.frameFinishedWaiters.get(frameId);
|
||
if (waiters) {
|
||
this.frameFinishedWaiters.delete(frameId);
|
||
for (const waiter of waiters) setTimeout(() => waiter(outcome), 0);
|
||
}
|
||
}
|
||
|
||
waitForFrameFinished(frameId: number, timeoutMs: number): Promise<string | null> {
|
||
const known = this.finishedFrameOutcomes.get(frameId);
|
||
if (known !== undefined) return Promise.resolve(known);
|
||
if (frameId <= 0) return Promise.resolve(null);
|
||
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
|
||
return new Promise((resolve) => {
|
||
let settled = false;
|
||
const onFinished = (outcome: string) => {
|
||
if (settled) return;
|
||
settled = true;
|
||
clearTimeout(timeoutHandle);
|
||
resolve(outcome);
|
||
};
|
||
let waiters = this.frameFinishedWaiters.get(frameId);
|
||
if (!waiters) {
|
||
waiters = new Set();
|
||
this.frameFinishedWaiters.set(frameId, waiters);
|
||
}
|
||
waiters.add(onFinished);
|
||
const timeoutHandle = setTimeout(() => {
|
||
if (settled) return;
|
||
settled = true;
|
||
const pending = this.frameFinishedWaiters.get(frameId);
|
||
if (pending) {
|
||
pending.delete(onFinished);
|
||
if (pending.size === 0) this.frameFinishedWaiters.delete(frameId);
|
||
}
|
||
resolve(null);
|
||
}, delayMs);
|
||
});
|
||
}
|
||
|
||
waitForNextFrameMetrics(timeoutMs: number): Promise<FrameMetrics | null> {
|
||
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
|
||
return new Promise((resolve) => {
|
||
let settled = false;
|
||
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
|
||
const complete = (metrics: FrameMetrics | null) => {
|
||
if (settled) return;
|
||
settled = true;
|
||
if (timeoutHandle !== null) clearTimeout(timeoutHandle);
|
||
this.frameMetricWaiters.delete(onMetrics);
|
||
resolve(metrics);
|
||
};
|
||
const onMetrics = (metrics: FrameMetrics) => complete(metrics);
|
||
this.frameMetricWaiters.add(onMetrics);
|
||
timeoutHandle = setTimeout(() => complete(null), delayMs);
|
||
});
|
||
}
|
||
|
||
// ----- session ---------------------------------------------------------------------
|
||
|
||
async start(): Promise<void> {
|
||
await this.enqueueNativeCall(() => {
|
||
if (this.communicator.start() && !this.closed) FaceclawCommunicatorBridge.active = this;
|
||
});
|
||
}
|
||
|
||
async setG2ScreenOn(screenOn: boolean): Promise<void> {
|
||
await this.enqueueNativeCall(() => this.communicator.setG2ScreenOnScreenOn(Boolean(screenOn)));
|
||
}
|
||
|
||
async setFirmwareDebugFlags(enabled: boolean): Promise<void> {
|
||
await this.enqueueNativeCall(() => this.communicator.setFirmwareDebugFlagsEnabled(Boolean(enabled)));
|
||
}
|
||
|
||
/** Set lens brightness: Faceclaw auto or a fixed level (clamped to 2–100). */
|
||
async setBrightness(autoAdjust: boolean, level: number): Promise<void> {
|
||
await this.enqueueNativeCall(() => this.communicator.setBrightnessAutoAdjustBrightnessLevel(Boolean(autoAdjust), Math.round(level)));
|
||
}
|
||
|
||
async configureBrightness(p: { auto: boolean; level: number; minimum: number; maximum: number; curve: string; fadeMs: number }): Promise<void> {
|
||
await this.enqueueNativeCall(() => this.communicator.configureBrightnessAutoLevelMinimumMaximumCurveFadeMs(p.auto, p.level, p.minimum, p.maximum, p.curve, p.fadeMs));
|
||
}
|
||
|
||
async enableWearDetectionAndRequestState(): Promise<void> {
|
||
await this.enqueueNativeCall(() => this.communicator.enableWearDetectionAndRequestState());
|
||
}
|
||
|
||
isSessionReady(): boolean {
|
||
return Boolean(this.communicator.isSessionReady());
|
||
}
|
||
|
||
/** The platform's lock/screen signal (the iOS host also observes protected data itself). */
|
||
onPhoneLockSignal(): void {
|
||
this.communicator.onPhoneLockSignal();
|
||
}
|
||
|
||
// ----- compositor ------------------------------------------------------------------
|
||
|
||
async configureCompositorScreen(width: number, height: number): Promise<void> {
|
||
await this.enqueueNativeCall(() => {
|
||
this.communicator.configureCompositorScreenWidthHeight(Math.round(width), Math.round(height));
|
||
});
|
||
}
|
||
|
||
/** Phone-UI preview of the current composited screen (or of `crop`'s area of it), or null if none yet. */
|
||
getCompositePreview(green = false, crop: { x: number; y: number; width: number; height: number } | null = null): ImageSource | null {
|
||
const frame = this.communicator.previewComposite();
|
||
if (!frame) return null;
|
||
const width = Number(this.communicator.compositeWidth()), height = Number(this.communicator.compositeHeight());
|
||
if (width <= 0 || height <= 0) return null;
|
||
return previewPixels(fromData(frame.pixels), width, height, green, crop);
|
||
}
|
||
|
||
/** Save the current composite as a 4-bit grayscale PNG under Documents; returns the path (empty if none). Cropping is not supported on iOS. */
|
||
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
|
||
if (crop) return "";
|
||
const png = this.communicator.screenshotPng();
|
||
if (!png) return "";
|
||
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
|
||
File.fromPath(target).writeSync(png);
|
||
return target;
|
||
}
|
||
|
||
/** Screen recording is not available on iOS yet. */
|
||
startScreenRecording(): void {}
|
||
|
||
recordScreenFrame(): void {}
|
||
|
||
stopScreenRecording(): string { return ""; }
|
||
|
||
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
|
||
const transparency = options.transparency === "color-key" ? 1 : 0;
|
||
await this.enqueueNativeCall(() => {
|
||
const configured = this.communicator.configureSurfaceIdXYWidthHeightZOrderTransparency(
|
||
id, Math.round(options.x), Math.round(options.y), Math.round(options.width), Math.round(options.height),
|
||
Math.round(options.zOrder), transparency,
|
||
);
|
||
// Kotlin logged why; reject so callers don't treat the surface as live (as on Android,
|
||
// where the compositor's exception reaches JS).
|
||
if (configured === false) throw new Error(`Could not configure surface ${id}`);
|
||
this.nativeSurfaces.add(id);
|
||
});
|
||
}
|
||
|
||
async removeSurface(id: string): Promise<void> {
|
||
await this.enqueueNativeCall(() => {
|
||
this.nativeSurfaces.delete(id);
|
||
this.communicator.removeSurfaceId(id);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Checked inside each queued call: a render in flight when its window closed lands
|
||
* behind the removal, and Kotlin aborts on a frame for an unknown surface.
|
||
*/
|
||
private hasNativeSurface(id: string, what: string): boolean {
|
||
if (this.nativeSurfaces.has(id)) return true;
|
||
console.log(`[ios-ble] ${what} for removed surface ${id} dropped`);
|
||
return false;
|
||
}
|
||
|
||
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
|
||
const snapshot = new Uint8Array(bytes);
|
||
await this.enqueueNativeCall(() => this.communicator.submitShellSceneBytesPaintMsFrameId(toData(snapshot), Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId))), true);
|
||
}
|
||
|
||
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
|
||
await this.enqueueNativeCall(() => {
|
||
this.communicator.setUnderlayDimBelowZOrderFactor256(Math.round(belowZOrder), dimFactor256(factor));
|
||
});
|
||
}
|
||
|
||
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
|
||
await this.enqueueNativeCall(() => {
|
||
if (this.hasNativeSurface(id, "setSurfaceVisible")) this.communicator.setSurfaceVisibleIdVisible(id, Boolean(visible));
|
||
});
|
||
}
|
||
|
||
async setSurfaceDepth(id: string, depth: number): Promise<void> {
|
||
await this.enqueueNativeCall(() => {
|
||
if (this.hasNativeSurface(id, "setSurfaceDepth")) this.communicator.setSurfaceDepthIdDepth(id, Math.round(depth));
|
||
});
|
||
}
|
||
|
||
async setScreenBlanked(blanked: boolean): Promise<void> {
|
||
await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); });
|
||
}
|
||
|
||
/**
|
||
* See the Android bridge. Not wired to the shared compositor's check on iOS
|
||
* yet, so every repaint is submitted as before.
|
||
*/
|
||
isSurfaceCurrent(_surfaceId: string, _fingerprint: string): boolean {
|
||
return false;
|
||
}
|
||
|
||
async submitSurfaceFrame(
|
||
surfaceId: string,
|
||
pixels8bpp: Uint8Array,
|
||
rect: { x: number; y: number; width: number; height: number },
|
||
fingerprint: string,
|
||
paintMs = -1,
|
||
frameId = 0,
|
||
glyphs: ArrayBuffer | null = null,
|
||
): Promise<void> {
|
||
const snapshot = new Uint8Array(pixels8bpp);
|
||
const draws = glyphs ? toData(new Uint8Array(glyphs)) : null;
|
||
await this.enqueueNativeCall(() => {
|
||
if (!this.hasNativeSurface(surfaceId, "frame")) return;
|
||
this.communicator.submitSurfaceFramePixels8bppSurfaceIdRectXRectYRectWidthRectHeightContentFingerprintPaintMsFrameIdGlyphs(
|
||
toData(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), Math.round(rect.width), Math.round(rect.height),
|
||
fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), draws,
|
||
);
|
||
}, true);
|
||
}
|
||
|
||
// ----- controls --------------------------------------------------------------------
|
||
|
||
async disconnect(): Promise<void> {
|
||
await this.enqueueNativeCall(() => this.communicator.disconnect());
|
||
}
|
||
|
||
async sendShutdown(exitMode = 0): Promise<boolean> {
|
||
return this.enqueueNativeCall(() => Boolean(this.communicator.sendShutdownExitMode(exitMode)));
|
||
}
|
||
|
||
async sendCfwCleanup(): Promise<boolean> {
|
||
return this.enqueueNativeCall(() => Boolean(this.communicator.sendCfwCleanup()));
|
||
}
|
||
|
||
async suspendEvenHubSession(): Promise<boolean> {
|
||
return this.enqueueNativeCall(() => Boolean(this.communicator.suspendEvenHubSession()));
|
||
}
|
||
|
||
async resumeEvenHubSession(): Promise<boolean> {
|
||
return this.enqueueNativeCall(() => Boolean(this.communicator.resumeEvenHubSession()));
|
||
}
|
||
|
||
async setFaceclawWakeLeaseEnabled(enabled: boolean): Promise<boolean> {
|
||
return this.enqueueNativeCall(() => Boolean(this.communicator.setFaceclawWakeLeaseEnabledEnabled(Boolean(enabled))));
|
||
}
|
||
|
||
async awaitEvenHubSessionReady(timeoutMs: number): Promise<boolean> {
|
||
return this.enqueueNativeCall(() => Boolean(this.communicator.awaitEvenHubSessionReadyTimeoutMs(Math.round(nonNegativeNumber(timeoutMs)))));
|
||
}
|
||
|
||
/** Play a CFW mode-5 kind-4 tone sequence (complete wire payload). */
|
||
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
|
||
const snapshot = new Uint8Array(payload);
|
||
await this.enqueueNativeCall(() => { this.communicator.playBuzzerSequencePayload(toData(snapshot)); });
|
||
}
|
||
|
||
async close(): Promise<void> {
|
||
this.closed = true;
|
||
if (FaceclawCommunicatorBridge.active === this) FaceclawCommunicatorBridge.active = null;
|
||
this.setAncsListeners(false);
|
||
this.previewAnimationListeners.clear();
|
||
await this.enqueueNativeCall(() => {
|
||
this.communicator.setPreviewAnimationListenerListener(null);
|
||
this.communicator.close();
|
||
});
|
||
}
|
||
|
||
// ----- iOS extras: compass, microphone, iPhone notifications (ANCS) ----------------------
|
||
|
||
/** CFW mode-10 compass on behalf of `owner`; the magnetometer runs while any owner wants it. */
|
||
setCompassEnabled(enabled: boolean, owner = "compass"): void {
|
||
this.communicator.setCompassEnabledForOwnerOwnerEnable(owner, Boolean(enabled));
|
||
}
|
||
|
||
/** Compass heading/calibration events, delivered on the main queue. */
|
||
addCompassListener(listener: (event: CompassEvent) => void): () => void {
|
||
const proxy = CompassListener.new() as CompassListener;
|
||
proxy.handler = listener;
|
||
this.communicator.addCompassListenerListener(proxy);
|
||
return () => { this.communicator.removeCompassListenerListener(proxy); };
|
||
}
|
||
|
||
/** Developer > Debug tests > BLE bandwidth; see app/native/bandwidth-benchmark.ios.ts. */
|
||
startBandwidthBenchmark(messageSize: number, windowSize: number, durationMs: number, linkMode: number): boolean {
|
||
return Boolean(this.communicator.startBandwidthBenchmarkWithLinkModeMessageSizeWindowSizeDurationMsLinkMode(
|
||
messageSize, windowSize, durationMs, linkMode));
|
||
}
|
||
|
||
cancelBandwidthBenchmark(): void {
|
||
this.communicator.cancelBandwidthBenchmark();
|
||
}
|
||
|
||
/** Status/results of the current or most recent run, as JSON. */
|
||
getBandwidthBenchmarkStatus(): string {
|
||
return String(this.communicator.getBandwidthBenchmarkStatus());
|
||
}
|
||
|
||
// ----- sensors and passive audio, for the developer demos -----------------------------
|
||
// Fire-and-forget, so they skip the native-call queue like the compass controls do.
|
||
|
||
/** Enable/disable the accelerometer report stream (an ImuReportPace code, 100..1000). */
|
||
setImuReportEnabled(enable: boolean, reportFrq: number): void {
|
||
this.communicator.setImuReportEnabledEnableReportFrq(Boolean(enable), Math.max(0, Math.round(reportFrq)));
|
||
}
|
||
|
||
/** Accelerometer samples, delivered on the main queue. */
|
||
addImuListener(listener: (reading: ImuReading) => void): () => void {
|
||
const proxy = ImuListener.new() as ImuListener;
|
||
proxy.handler = listener;
|
||
this.communicator.addImuListenerListener(proxy);
|
||
return () => { this.communicator.removeImuListenerListener(proxy); };
|
||
}
|
||
|
||
/** Ask the CFW for one ambient-light report (delivered to the ambient-light listeners). */
|
||
queryAmbientLight(): void {
|
||
this.communicator.queryAmbientLight();
|
||
}
|
||
|
||
setAmbientLightPolling(enable: boolean, intervalMs: number, minDelta: number, heartbeatMs: number, bindToLease: boolean): void {
|
||
this.communicator.setAmbientLightPollingEnableIntervalMsMinDeltaHeartbeatMsBindToLease(Boolean(enable),
|
||
Math.round(intervalMs), Math.round(minDelta), Math.round(heartbeatMs), Boolean(bindToLease));
|
||
}
|
||
|
||
/** Raw field-105 ambient-light records, delivered on the main queue. */
|
||
addAmbientLightListener(listener: (body: Uint8Array) => void): () => void {
|
||
const proxy = AmbientLightListener.new() as AmbientLightListener;
|
||
proxy.handler = listener;
|
||
const handle = this.communicator.addAmbientLightListenerNsDataListener(proxy);
|
||
return () => { this.communicator.removeAmbientLightListenerListener(handle); };
|
||
}
|
||
|
||
/** Raw field-104 mic_control status records and their temple ("L", "R" or "?"), on the main queue. */
|
||
addMicStatusListener(listener: (body: Uint8Array, arm: string) => void): () => void {
|
||
const proxy = MicStatusListener.new() as MicStatusListener;
|
||
proxy.handler = listener;
|
||
const handle = this.communicator.addMicStatusListenerNsDataListener(proxy);
|
||
return () => { this.communicator.removeMicStatusListenerListener(handle); };
|
||
}
|
||
|
||
/** Queue a CFW mic_control record for the chosen temples; false when nothing was queued. */
|
||
sendFaceclawMicControl(record: Uint8Array, label: string, rightTemple: boolean, leftTemple: boolean): boolean {
|
||
return Boolean(this.communicator.sendFaceclawMicControlRecordLabelRightTempleLeftTemple(
|
||
toData(new Uint8Array(record)), label, Boolean(rightTemple), Boolean(leftTemple)));
|
||
}
|
||
|
||
/**
|
||
* Every audio-characteristic packet, whatever enabled the mics, on the
|
||
* main queue. Passive: sends nothing and doesn't affect capture.
|
||
*/
|
||
addAudioMonitorListener(listener: (packet: Uint8Array, arm: string, arrivalMs: number) => void): () => void {
|
||
const proxy = AudioPacketListener.new() as AudioPacketListener;
|
||
proxy.handler = listener;
|
||
const handle = this.communicator.addAudioMonitorListenerNsDataListener(proxy);
|
||
return () => { this.communicator.removeAudioMonitorListenerListener(handle); };
|
||
}
|
||
|
||
/** True while Faceclaw itself is forwarding glasses audio (speech capture or an app's mic). */
|
||
isAudioCaptureActive(): boolean {
|
||
return Boolean(this.communicator.isAudioCaptureActive());
|
||
}
|
||
|
||
/**
|
||
* Serialized glasses microphone enable/disable (the voice bridge's
|
||
* IosMicrophoneSession contract). A release while an enable awaits its ACK
|
||
* still wins: the token check drops the stale packets.
|
||
*/
|
||
setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise<void> {
|
||
const token = ++this.microphoneToken;
|
||
const work = this.microphoneWork.catch(() => {}).then(async () => {
|
||
if (token !== this.microphoneToken) return;
|
||
if (!enabled) {
|
||
this.audioListener = null;
|
||
await this.enqueueNativeCall(() => this.communicator.stopG2AudioCapture());
|
||
return;
|
||
}
|
||
if (!this.isSessionReady()) throw new Error("Connect the glasses before starting voice input.");
|
||
const proxy = AudioPacketListener.new() as AudioPacketListener;
|
||
proxy.handler = (packet) => { if (token === this.microphoneToken && listener) listener(packet); };
|
||
this.audioListener = proxy;
|
||
const started = await this.enqueueNativeCall(() => Boolean(this.communicator.startG2AudioCaptureNsDataListener(proxy)));
|
||
if (!started) {
|
||
this.audioListener = null;
|
||
throw new Error("The glasses did not enable their microphone. Reconnect and try again.");
|
||
}
|
||
});
|
||
this.microphoneWork = work;
|
||
return work;
|
||
}
|
||
|
||
/** Ask CoreBluetooth for ANCS on the right arm's next connection (call before start()). */
|
||
setRequiresAncs(required: boolean): void {
|
||
this.communicator.setRequiresAncsRequired(Boolean(required));
|
||
this.setAncsListeners(required);
|
||
}
|
||
|
||
isAncsAuthorized(): boolean {
|
||
return Boolean(this.communicator.isAncsAuthorized());
|
||
}
|
||
|
||
onAncsAuthorization(listener: (authorized: boolean) => void): () => void {
|
||
this.ancsAuthorizationListeners.add(listener);
|
||
return () => this.ancsAuthorizationListeners.delete(listener);
|
||
}
|
||
|
||
/** Relay frames ('A','N',...) received on the right arm, for the ANCS client. */
|
||
onAncsRelayFrame(listener: (frame: Uint8Array) => void): () => void {
|
||
this.ancsRelayListeners.add(listener);
|
||
return () => this.ancsRelayListeners.delete(listener);
|
||
}
|
||
|
||
/** Queue one relay packet for the right arm; the Kotlin side writes it off the JS thread. */
|
||
async writeRawToRight(packet: Uint8Array): Promise<void> {
|
||
if (!this.isSessionReady()) throw new Error("Glasses are not connected.");
|
||
const snapshot = new Uint8Array(packet);
|
||
this.communicator.writeRawToRightPacket(toData(snapshot));
|
||
}
|
||
|
||
/** The right arm's usable write payload for relay packets (20 until connected). */
|
||
rightWriteLimit(): number {
|
||
return Number(this.communicator.rightWriteLimit()) || 20;
|
||
}
|
||
|
||
private setAncsListeners(install: boolean): void {
|
||
if (install && !this.ancsProxy) {
|
||
this.ancsProxy = AncsListener.new() as AncsListener;
|
||
this.ancsProxy.bridge = this;
|
||
this.communicator.setAncsListenerListener(this.ancsProxy);
|
||
} else if (!install && this.ancsProxy) {
|
||
this.communicator.setAncsListenerListener(null);
|
||
this.ancsProxy = null;
|
||
}
|
||
}
|
||
}
|