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

-15
View File
@@ -1,15 +0,0 @@
import { fromData, toData, nativeArray } from '../native/kotlin-data'
declare const FaceclawKitIosProtocol: any
const protocol = FaceclawKitIosProtocol.new()
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, width: number, height: number, frameId: number): Uint8Array | null {
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
const result = protocol.boundingBoxPreviousNextWidthHeightFrameId(toData(previous), toData(next), width, height, frameId)
return result ? fromData(result) : null
}
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
throw new Error('Invalid framebuffer dimensions')
return nativeArray(protocol.fullFrameBandsDataWidthHeightFirstId(toData(packed), width, height, firstId), fromData)
}
-50
View File
@@ -1,50 +0,0 @@
import { CFW_MSG_BOUNDING_BOX } from "./cfw-message-type";
import { concat, rle4 } from './ble-protocol'
/** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in
* units of 4 columns / 2 rows; pixels are top-down packed 4bpp, high nibble first.
* null means identical, incomparable, or a full-screen box: use dedup/mode 6. Compression belongs to the CFW transport.
*/
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array,
width: number, height: number, frameId: number): Uint8Array | null {
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
const stride = width / 2
let minX = stride, maxX = -1, minY = height, maxY = -1
for (let y = 0; y < height; y++) {
const row = y * stride
for (let x = 0; x < stride; x++) {
if (previous[row + x] === next[row + x]) continue
minX = Math.min(minX, x); maxX = Math.max(maxX, x)
minY = Math.min(minY, y); maxY = y
}
}
if (maxY < 0) return null
const left = (minX * 2) & ~3, right = Math.min(width, ((maxX + 1) * 2 + 3) & ~3)
const top = minY & ~1, bottom = Math.min(height, (maxY + 2) & ~1)
const boxWidth = right - left, boxHeight = bottom - top
if (boxWidth === width && boxHeight === height) return null
const regionStride = boxWidth / 2, region = new Uint8Array(regionStride * boxHeight)
for (let y = 0; y < boxHeight; y++) {
const start = (top + y) * stride + left / 2
region.set(next.subarray(start, start + regionStride), y * regionStride)
}
return concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, left / 4, top / 2, boxWidth / 4, boxHeight / 2,
frameId & 255, (frameId >>> 8) & 255]), rle4(region))
}
/** Full repaint as independent 64-row mode-3 bands when RLE exceeds uint16. */
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
throw new Error('Invalid framebuffer dimensions')
const bands: Uint8Array[] = []
let id = firstId
for (let top = 0; top < height; top += 64) {
const rows = Math.min(64, height - top)
bands.push(concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]),
rle4(packed.subarray(top * width / 2, (top + rows) * width / 2))))
id = id >= 0xfffe ? 1 : id + 1
}
return bands
}
-203
View File
@@ -1,203 +0,0 @@
import { type RingInput, decodeRingMetadata } from "./ring-input";
import { type CompassEvent } from '../native/compass-types'
/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */
import { OsEventTypeList } from './events'
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402'
export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402'
export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f']
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const
export function concat(...parts: Uint8Array[]): Uint8Array {
const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
let offset = 0; for (const part of parts) { output.set(part, offset); offset += part.length }; return output
}
function varint(value: number): Uint8Array {
const bytes: number[] = []
do { const next = value & 127; value >>>= 7; bytes.push(next | (value ? 128 : 0)) } while (value)
return new Uint8Array(bytes)
}
export const integer = (field: number, value: number): Uint8Array => concat(varint(field * 8), varint(value))
export const bytes = (field: number, value: Uint8Array): Uint8Array => concat(varint(field * 8 + 2), varint(value.length), value)
// Outbound names/text here are fixed ASCII protocol identifiers.
const string = (field: number, value: string): Uint8Array => bytes(field, new Uint8Array(Array.from(value, c => c.charCodeAt(0))))
const wrap = (command: number, magic: number, field: number, body: Uint8Array) => concat(integer(1, command), integer(2, magic), bytes(field, body))
export const authentication = (magic: number) => wrap(4, magic, 3, concat(integer(1, 1), integer(2, 4)))
export const prelude = () => wrap(2, 156, 4, bytes(3, bytes(2, bytes(2, concat(integer(1, 0), integer(2, 0))))))
export const heartbeat = (magic: number) => wrap(12, magic, 14, integer(1, 0))
export const audioControl = (magic: number, enabled: boolean) => wrap(15, magic, 18, integer(1, enabled ? 1 : 0))
export const settingsQuery = (magic: number) => wrap(2, magic, 4, integer(1, 1))
export const shutdown = (magic: number) => wrap(9, magic, 11, integer(1, 1))
export const framebufferLease = (acquire: boolean) => concat(integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, acquire ? 5 : 6, 0, 0])))
export function createLayout(magic: number): Uint8Array {
const geometry = concat(integer(1, 0), integer(2, 0), integer(3, 576), integer(4, 288))
const text = concat(geometry, integer(9, 1), string(10, 'dashboard'), integer(11, 1), string(12, ' '))
return wrap(0, magic, 3, concat(integer(1, 1), bytes(3, text), integer(5, 10000)))
}
export function crc16(data: Uint8Array): number {
let crc = 0xffff
for (const byte of data) {
crc ^= byte << 8
for (let i = 0; i < 8; i++) crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0)) & 0xffff
}
return crc
}
export function frameMessage(payload: Uint8Array, sid: number, flag: number, sequence: number, maxWrite = 240): Uint8Array[] {
const chunkSize = Math.min(232, Math.floor(maxWrite) - 8)
if (chunkSize < 12) throw new Error('Negotiated BLE write size is too small')
const crc = crc16(payload), withCrc = concat(payload, new Uint8Array([crc & 255, crc >>> 8]))
const count = Math.ceil(withCrc.length / chunkSize)
if (count > 255) throw new Error('Protocol message exceeds the fragment limit')
return Array.from({ length: count }, (_, i) => {
const chunk = withCrc.subarray(i * chunkSize, (i + 1) * chunkSize)
return concat(new Uint8Array([0xaa, 0x21, sequence & 255, chunk.length, count, i + 1, sid, flag]), chunk)
})
}
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array }
/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */
export class MessageReceiver {
private streams = new Map<string, Uint8Array>()
private messages = new Map<string, { chunks: Uint8Array[]; total: number; seen: number; size: number }>()
clear(): void { this.streams.clear(); this.messages.clear() }
receive(link: string, data: Uint8Array, now = Date.now()): ProtocolMessage[] {
for (const [key, message] of this.messages) if (now - message.seen > 5000) this.messages.delete(key)
let stream = concat(this.streams.get(link) ?? new Uint8Array(), data)
const output: ProtocolMessage[] = []
while (stream.length >= 8) {
if (stream[0] !== 0xaa || ![0x21, 0x12].includes(stream[1])) { stream = stream.subarray(1); continue }
const length = stream[3], count = stream[4], index = stream[5]
if (stream.length < length + 8) break
const sid = stream[6], flag = stream[7], key = `${link}:${stream[2]}:${sid}:${flag}`
const packet = stream.slice(0, length + 8)
const chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8)
if (sid === SID.cfw) {
output.push({ sid, flag, payload: chunk, command: -1, magic: -1, packet })
continue
}
if (!count || !index || index > count) { this.messages.delete(key); continue }
if (index === 1) this.messages.set(key, { chunks: [], total: count, seen: now, size: 0 })
const message = this.messages.get(key)
if (!message || message.total !== count || message.chunks.length + 1 !== index) { this.messages.delete(key); continue }
message.chunks.push(chunk); message.size += chunk.length; message.seen = now
if (message.size > 65536) { this.messages.delete(key); continue }
if (index !== count) continue
this.messages.delete(key)
const complete = concat(...message.chunks)
if (complete.length < 2) continue
const payload = complete.subarray(0, -2), crc = complete[complete.length - 2] | (complete[complete.length - 1] << 8)
if (crc16(payload) !== crc) continue
try { output.push({ sid, flag, payload, command: readInteger(payload, 1, -1), magic: readInteger(payload, 2, -1) }) }
catch { /* Malformed notifications cannot satisfy an acknowledgement. */ }
}
this.streams.set(link, stream.slice())
return output
}
}
function readVarint(data: Uint8Array, offset: number): [number, number] {
let value = 0
for (let i = 0; i < 5 && offset < data.length; i++) {
const byte = data[offset++]; value += (byte & 127) * 2 ** (i * 7)
if (!(byte & 128)) return [value, offset]
}
throw new Error('Truncated or oversized protobuf varint')
}
function field(data: Uint8Array, number: number): number | Uint8Array | undefined {
let offset = 0
while (offset < data.length) {
let key: number; [key, offset] = readVarint(data, offset)
const tag = key >>> 3, wire = key & 7
if (!tag) throw new Error('Invalid protobuf field')
if (wire === 0) {
let value: number; [value, offset] = readVarint(data, offset)
if (tag === number) return value
} else {
let length: number
if (wire === 2) [length, offset] = readVarint(data, offset)
else if (wire === 1) length = 8
else if (wire === 5) length = 4
else throw new Error('Unsupported protobuf wire type')
if (offset + length > data.length) throw new Error('Truncated protobuf field')
if (tag === number && wire === 2) return data.subarray(offset, offset + length)
offset += length
}
}
return undefined
}
export function readInteger(data: Uint8Array, number: number, fallback = 0): number { const value = field(data, number); return typeof value === 'number' ? value : fallback }
export function readBytes(data: Uint8Array, number: number): Uint8Array | undefined { const value = field(data, number); return value instanceof Uint8Array ? value : undefined }
export function readString(data: Uint8Array, number: number): string { return Array.from(readBytes(data, number) ?? [], value => String.fromCharCode(value)).join('') }
export function authenticationSucceeded(message: ProtocolMessage, magic: number): boolean {
return message.sid === SID.auth && message.command === 4 && message.magic === magic && readBytes(message.payload, 3)?.length === 0
}
export type GlassesInput = { ringInput?: RingInput; kind: 'sys-event' | 'list-click' | 'text-click' | 'display-wake' | 'even-ai'; eventType: number; eventSource: number; systemExitReasonCode: number; containerName: string; frameId: number }
export function decodeGlassesInput(message: ProtocolMessage): GlassesInput | null {
if (message.sid !== SID.hub || ![1, 6].includes(message.flag)) return null
const events = readBytes(message.payload, 13); if (!events) return null
for (const [field, kind, typeField] of [[1, 'list-click', 5], [2, 'text-click', 3], [3, 'sys-event', 1]] as const) {
const event = readBytes(events, field)
if (event) return { kind: kind === 'sys-event' && readInteger(event, typeField) === OsEventTypeList.HEAD_UP_EVENT ? 'display-wake' : kind,
eventType: readInteger(event, typeField), eventSource: field === 3 ? readInteger(event, 2) : 0,
ringInput: field === 3 && readInteger(event, 2) === 2 ? decodeRingMetadata(readBytes(event, 100)) : undefined,
systemExitReasonCode: field === 3 ? readInteger(event, 4) : 0, containerName: field === 3 ? '' : readString(event, 2), frameId: 0 }
}
return null
}
export function decodeRingInput(data: Uint8Array): GlassesInput | null {
let eventType: number | undefined
if (data.length === 11 && data[0] === 0 && data[1] === 9 && data[2] === 0x61 && data[3] === 0)
eventType = ({ 0: 9, 1: 0, 2: 3, 4: 1, 5: 2, 8: 10 } as Record<number, number>)[data[4]]
if (data.length === 3 && data[0] === 255) {
if (data[1] === 3 && data[2] === 32) eventType = 9
if (data[1] === 4 && data[2] === 1) eventType = 0
if (data[1] === 4 && data[2] === 2) eventType = 3
if (data[1] === 5) eventType = data[2] <= 1 ? 2 : 1
}
return eventType === undefined ? null : { kind: 'sys-event', eventType, eventSource: 2, systemExitReasonCode: 0, containerName: '', frameId: 0 }
}
export function packGray4(gray: Uint8Array, width: number, height: number): Uint8Array {
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || gray.length !== width * height) throw new Error('Invalid display frame')
const stride = Math.ceil(width / 2), packed = new Uint8Array(stride * height)
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
const nibble = Math.min(15, (gray[y * width + x] + 8) >> 4)
packed[y * stride + (x >> 1)] |= nibble << (x % 2 ? 0 : 4)
}
return packed
}
export function rle4(packed: Uint8Array): Uint8Array {
const output = new Uint8Array(packed.length * 2)
const nibble = (i: number) => (packed[i >> 1] >> (i % 2 ? 0 : 4)) & 15
let used = 0, i = 0
while (i < packed.length * 2) {
const color = nibble(i); let end = i + 1
while (end < packed.length * 2 && nibble(end) === color) end++
let remaining = end - i
while (remaining) {
const count = Math.min(65535, remaining)
if (count <= 15) output[used++] = (count << 4) | color
else { output[used++] = color; if (count <= 255) output[used++] = count
else { output[used++] = 0; output[used++] = count & 255; output[used++] = count >>> 8 } }
remaining -= count
}
i = end
}
return output.slice(0, used)
}
/** Reference decoder for host tests; iOS uses the shared Kotlin implementation. */
export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null {
if (message.sid !== 8 || ![1, 6].includes(message.flag)) return null
const command = readInteger(message.payload, 1, -1)
if (command === 16 || command === 17) return { command, headingDegrees: -1 }
if (command !== 15) return null
const compass = readBytes(message.payload, 10)
const headingDegrees = compass ? readInteger(compass, 1, -1) : -1
if (headingDegrees < 0 || headingDegrees >= 360) return null
const event: CompassEvent = { command, headingDegrees }
let d: Uint8Array | undefined
try { d = readBytes(message.payload, 100) } catch { return event }
if (d?.length === 12 && d[0] === 67 && d[1] === 77 && d[2] === 1 && d[7] === 0 &&
(d[3] <= 3 || d[3] === 255) && (d[4] <= 2 || d[4] === 255) && d[5] <= 3) {
event.diagnostics = { magneticAccuracy: d[3] === 255 ? -1 : d[3], magneticAnomalies: d[4] === 255 ? -1 : d[4],
orientationSource: d[5], flags: d[6], sampleTimeMs: new DataView(d.buffer, d.byteOffset + 8, 4).getUint32(0, true) }
}
return event
}
-17
View File
@@ -1,17 +0,0 @@
/** Same units as Android FaceclawBleManager: protocol messages, framed write
* bytes accepted by the transport, and fully acknowledged display updates.
* Totals survive reconnects and the setting being switched off.
*/
export class BleTrafficCounters {
private messages = 0
private bytes = 0
private frames = 0
recordWrite(bytes: number): void { this.bytes += bytes }
recordMessage(): void { this.messages++ }
recordDisplayFrame(): void { this.frames++ }
sample(): { messages: number; bytes: number; frames: number } {
return { messages: this.messages, bytes: this.bytes, frames: this.frames }
}
}
// The iOS BLE session and phone UI both run in the main isolate.
export const iosBleTraffic = new BleTrafficCounters()
-8
View File
@@ -1,8 +0,0 @@
import { bytes, concat, integer } from './ble-protocol'
/** Same G2SettingPackage as Kotlin BleProtocol.buildSetBrightness. */
export function setBrightness(magic: number, level: number | null): Uint8Array {
const fields = level === null ? integer(1, 1)
: concat(integer(1, 0), integer(2, Math.max(0, Math.min(100, Math.round(level)))))
return concat(integer(1, 1), integer(2, magic), bytes(3, bytes(1, fields)))
}
-21
View File
@@ -1,21 +0,0 @@
import { fromData, toData, nativeArray } from '../native/kotlin-data'
declare const FaceclawKitIosCfwTransport: any, FaceclawKitIosProtocol: any
export const CFW_MAX_MESSAGE = 65535
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
/** maxWrite is the ATT value limit; Kotlin owns the persistent zlib stream. */
export class CfwTransport {
private native = FaceclawKitIosCfwTransport.new()
reset(): void { this.native.reset() }
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
throw new Error('Invalid CFW stream parameters')
return nativeArray(this.native.encodeDataStreamIdLensesMaxWrite(toData(message), streamId, lenses, maxWrite), fromData)
}
}
const protocol = FaceclawKitIosProtocol.new()
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
const acks = protocol.acksData(toData(packet))
return acks ? nativeArray(acks, (ack: any) => ({ nack: ack.nack, streamId: ack.streamId, messageId: ack.messageId,
lens: ack.lens, size: ack.size, checksum: ack.checksum })) : null
}
-57
View File
@@ -1,57 +0,0 @@
import { concat, crc16, SID } from './ble-protocol'
declare const require: (name: string) => any
/** Mirrors CFW_MESSAGE_MAX in g2flash/patches/message_transport.h. */
export const CFW_MAX_MESSAGE = 65535
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
const u16 = (data: Uint8Array, offset: number) => data[offset] | (data[offset + 1] << 8)
/** Matches Android CfwTransport. maxWrite is the ATT value limit (MTU - 3). */
export class CfwTransport {
private compressor: any
private resetPending = true
private lenses = 0
reset(): void { this.compressor = null; this.resetPending = true; this.lenses = 0 }
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
throw new Error('Invalid CFW stream parameters')
if (this.lenses !== lenses) this.reset()
if (!this.compressor) this.compressor = new (require('pako').Deflate)()
const chunks: Uint8Array[] = []
// SYNC_FLUSH emits this record while retaining history for the next one.
this.compressor.onData = (chunk: Uint8Array) => chunks.push(chunk.slice())
if (!this.compressor.push(message, 2)) throw new Error(this.compressor.msg || 'CFW compression failed')
let body = concat(...chunks), flags = lenses | 4 | (this.resetPending ? 8 : 0)
if (!body.length) flags &= ~4
if (body.length > CFW_MAX_MESSAGE) {
this.reset(); body = message; flags = lenses | 8
} else { this.resetPending = false; this.lenses = lenses }
const checksum = crc16(message)
const record = concat(new Uint8Array([flags, body.length & 255, body.length >>> 8, checksum & 255, checksum >>> 8]), body)
const capacity = Math.min(252, maxWrite - 11), packets: Uint8Array[] = []
for (let offset = 0, index = 0; offset < record.length; offset += capacity, index++) {
const body = concat(new Uint8Array([lenses | (offset === 0 ? 0x80 : 0) |
(offset + capacity >= record.length ? 0x40 : 0)]), record.subarray(offset, offset + capacity))
const crc = crc16(body)
packets.push(concat(new Uint8Array([0xaa, 0x21, (streamId + index) & 255, body.length + 2, 1, 1, SID.cfw, 0]),
body, new Uint8Array([crc & 255, crc >>> 8])))
}
return packets
}
}
/** Validate the entire primary + redundant-ACK packet before applying any ACK. */
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
const n = packet.length
if (n < 19 || n > 40 || (n - 19) % 7 || packet[0] !== 0xaa || packet[1] !== 0x12 ||
packet[3] !== n - 8 || packet[4] !== 1 || packet[5] !== 1 || packet[6] !== SID.cfw || packet[7] !== 0 ||
![1, 3].includes(packet[8]) || ![1, 2].includes(packet[12]) || (packet[8] === 3 && n !== 19) ||
crc16(packet.subarray(8, -2)) !== u16(packet, n - 2)) return null
const acks = [{ nack: packet[8] === 3, streamId: packet[9], messageId: u16(packet, 10),
lens: packet[12], size: u16(packet, 13), checksum: u16(packet, 15) }]
for (let offset = 17; offset < n - 2; offset += 7)
acks.push({ nack: false, streamId: packet[offset], messageId: u16(packet, offset + 1), lens: packet[12],
size: u16(packet, offset + 3), checksum: u16(packet, offset + 5) })
return acks
}
-15
View File
@@ -1,15 +0,0 @@
import { CFW_MSG_BOUNDING_BOX, CFW_MSG_DRAW_CALLS, CFW_MSG_FULL_FRAME, CFW_MSG_MULTI_SEGMENT, CFW_MSG_TYPE_MASK } from "./cfw-message-type";
/** Adapter for raster-only senders; optimized legacy layouts remain internal to the phone. */
export function drawMessages(payload: Uint8Array, width = 640, height = 480): Uint8Array[] {
const word = (v: number) => [v & 255, (v >>> 8) & 255]
const mode = payload[0] & CFW_MSG_TYPE_MASK
let calls: Uint8Array[]
if (mode === CFW_MSG_MULTI_SEGMENT) {
const output: Uint8Array[] = []; let p=2
for(let i=0;i<payload[1];i++) { const n=payload[p] | payload[p+1]<<8; p+=2; output.push(...drawMessages(payload.subarray(p,p+n),width,height));p+=n }
return output
} else if (mode === CFW_MSG_BOUNDING_BOX) calls=[new Uint8Array([1,0,0,...payload.subarray(1,5),...payload.subarray(7)])]
else if(mode === CFW_MSG_FULL_FRAME) calls=[new Uint8Array([1,0,1,0,0,0,0,...word(width),...word(height),...payload.subarray(1)])]
else throw new Error(`Unsupported raster draw ${mode}`)
return calls.map(call => new Uint8Array([CFW_MSG_DRAW_CALLS,1,0,...word(call.length),...call]))
}
-645
View File
@@ -1,645 +0,0 @@
import { CFW_MSG_CLEANUP, CFW_MSG_COMPASS, CFW_MSG_EVICT_RESOURCE, CFW_MSG_FULL_FRAME, CFW_MSG_PRESENT } from "./cfw-message-type";
import { drawMessages } from './draw-protocol'
import { setBrightness } from './brightness-protocol'
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
import { type CompassEvent } from '../native/compass-types'
import * as protocol from './ble-protocol'
import { iosBleTraffic } from './ble-traffic-counters'
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
import { CfwTransport, CFW_MAX_MESSAGE, parseCfwAcks } from './cfw-transport'
import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLAW_FIRMWARE_VERSION } from './firmware-compat'
import { hexToBytes } from '../util/hex-util'
import { deviceAddressError } from './ios-peripheral-identity'
import { decodeWearState, enableWearDetection, queryWearState } from './wear-protocol'
import type { SessionTexturePlanner, TextureFrame } from './texture-planner'
declare function setTimeout(callback: () => void, ms: number): number
declare function clearTimeout(id: number): void
export type SessionAddresses = { left: string; right: string; ring: string }
export type TransportEvent = { kind: string; identifier?: string; characteristic?: string; data?: string; message?: string; state?: number; authorized?: boolean }
export interface SessionTransport {
onEvent(listener: (event: TransportEvent) => void): () => void
resolveDevices(addresses: SessionAddresses): Promise<Record<string, string>>
connect(identifier: string, requiresANCS?: boolean): Promise<{ characteristics: string[]; maxWrite: number }>
subscribe(identifier: string, characteristic: string): Promise<void>
write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void>
disconnect(identifier: string): void
stopScan(): void
}
export type SessionState = { phase: 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error';
status: string; battery: number | null; charging: boolean | null; ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
type PendingAck = { resolve: (message: protocol.ProtocolMessage) => void; reject: (error: Error) => void;
timer: number | null; command: number; label: string }
class AckTimeout extends Error {}
// Match Android ConnectionOptions.WINDOW_SIZE. This bounds complete image
// messages queued for writing or awaiting ACK, not individual BLE packets.
const DISPLAY_WINDOW_SIZE = 3
type CfwPending = { payload: Uint8Array; checksum: number; role: string; magic: number; ackLenses: number; retries: number;
retryPending: boolean; deadline: number; resolve: () => void; reject: (error: Error) => void }
type DisplayFrame = { packed: Uint8Array; commands: Uint8Array[]; offset: number; pending: number }
/** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */
export class GlassesSession {
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false,
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
readonly notifications: AncsClient
private generation = 0
private ids: Record<string, string> = {}
private limits: Record<string, number> = {}
private cfwPending: CfwPending[] = []
private cfwTransports = new Map<string, CfwTransport>()
private cfwTimer: number | null = null
private pending = new Map<string, PendingAck>()
private receiver = new protocol.MessageReceiver()
private writes = new Map<string, Promise<void>>()
private magic = 100
private sequence = 0x40
private timer: number | null = null
private retryTimer: number | null = null
private ringRetryTimer: number | null = null
private ringRetryCount = 0
private ringConnecting = false
private retryCount = 0
private wanted = false
private addresses: SessionAddresses | null = null
private latest: Uint8Array | null = null
private latestTextures: TextureFrame | null = null
private displayed: Uint8Array | null = null
private lastEnqueued: Uint8Array | null = null
// Match Android: advance only for emitted deltas, skipping 0 / 0xffff.
private nextImageFrameId = 1
private displayFrames: DisplayFrame[] = []
private displaySending: DisplayFrame | null = null
private displayInFlight = 0
private pumping = false
private lastHeartbeat = 0
private lastLease = 0
private lastSettings = 0
private charging = false
private layoutCreated = false
private off: () => void
private stopping: Promise<void> | null = null
private audioListener: ((packet: Uint8Array) => void) | null = null
private microphoneToken = 0
private microphoneWork: Promise<void> = Promise.resolve()
private microphoneEnabled = false
private compassWanted = false
private compassSent: boolean | null = null
private compassStopping = false
private compassWork: Promise<void> = Promise.resolve()
constructor(private readonly transport: SessionTransport,
private readonly onState: (state: SessionState) => void,
private readonly onInput: (input: protocol.GlassesInput) => void,
private readonly log: (message: string) => void = () => {},
private readonly onActivity: () => void = () => {},
private readonly onCompass: (event: CompassEvent) => void = () => {},
private readonly onWearState: (wearing: boolean) => void = () => {},
onNotificationsChanged: (key?: string, popup?: boolean) => void = () => {},
private readonly textures?: SessionTexturePlanner) {
this.notifications = new AncsClient(packet => this.writePackets('right', () => [packet]), onNotificationsChanged, message => this.log('ANCS: ' + message))
this.off = transport.onEvent(event => this.receive(event))
}
private update(phase: SessionState['phase'], status: string): void {
this.state = { ...this.state, phase, status }; this.log(status); this.onState({ ...this.state })
}
async start(addresses: SessionAddresses, retry = false): Promise<void> {
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
const invalid = deviceAddressError(addresses)
if (invalid) { this.update('error', invalid); return }
this.addresses = { ...addresses }; this.wanted = true; this.compassStopping = false
if (!retry) this.retryCount = 0
const generation = ++this.generation
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
this.charging = false
this.update('connecting', 'Finding configured devices…')
try {
const ids = await this.transport.resolveDevices(addresses); this.check(generation)
this.ids = ids
for (const role of ['right', 'left']) {
this.update('connecting', `Connecting ${role} arm…`)
const details = await this.transport.connect(ids[role], role === 'right'); this.check(generation)
this.limits[role] = details.maxWrite
if (!details.characteristics.includes(protocol.G2_WRITE) || !details.characteristics.includes(protocol.G2_NOTIFY)) throw new Error(`${role} arm is missing G2 communication characteristics.`)
await this.transport.subscribe(ids[role], protocol.G2_NOTIFY); this.check(generation)
}
this.update('connecting', 'Authenticating… Accept pairing if iOS asks.')
await Promise.all(['right', 'left'].map(async role => {
try {
const answer = await this.request(role, protocol.SID.auth, protocol.authentication, 'Security authentication', 30_000, 0)
if (!protocol.authenticationSucceeded(answer, answer.magic)) throw new Error(`${role} arm rejected authentication.`)
} catch (error) {
if (!(error instanceof AckTimeout)) throw error
// Older CFW may not implement the reply. A settings response below
// still has to prove we have a working, compatible session.
this.log(`${role} authentication unconfirmed; checking firmware response`)
}
}))
this.check(generation)
this.update('connecting', 'Checking glasses firmware…')
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Settings query')
this.applySettings(settings)
const firmware = { leftVersion: this.state.leftVersion, rightVersion: this.state.rightVersion, extension: this.state.capabilities }
if (!hasCompatibleFirmware(firmware))
throw new Error((firmwareIncompatibilityMessage(firmware) || `The glasses must report Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION} or newer.`) + ' Use Devices → Check firmware to install it.')
this.update('connecting', 'Starting glasses display…')
await this.request('right', protocol.SID.launch, () => protocol.prelude(), 'App launch', 3500, 0x20, 156)
await this.lease(true)
await this.request('right', protocol.SID.hub, protocol.createLayout, 'Create display layout')
this.check(generation); this.layoutCreated = true
await this.enableWearDetectionAndRequestState(); this.check(generation)
this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
this.displayed = null; this.retryCount = 0
this.update('connected', 'Glasses connected')
if (/^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION)
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
else this.notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
await this.syncCompass(); this.check(generation)
this.schedule()
if (addresses.ring && ids.ring) await this.connectRing(generation)
else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found')
} catch (error) {
if (generation !== this.generation) return
this.fail(error, retry || error instanceof AckTimeout)
}
}
private async connectRing(generation: number): Promise<void> {
if (this.ringConnecting || generation !== this.generation || this.state.phase !== 'connected' || !this.ids.ring) return
this.ringConnecting = true
const identifier = this.ids.ring
try {
const ring = await this.transport.connect(identifier); this.check(generation)
this.log(`R1 services discovered: ${ring.characteristics.join(', ')}`)
const notifications = protocol.RING_NOTIFY.filter(uuid => ring.characteristics.includes(uuid))
if (!notifications.length) throw new Error('No R1 gesture notifications found')
let subscribed = 0
for (const uuid of notifications) {
this.log(`Subscribing R1 ${uuid}`)
try { await this.transport.subscribe(identifier, uuid); this.check(generation); subscribed++; this.log(`R1 subscribed ${uuid}`) }
catch (error) { this.check(generation); this.log(`R1 channel unavailable ${uuid}: ${this.message(error)}`) }
}
if (!subscribed) throw new Error('Neither R1 notification channel could be subscribed')
this.check(generation); this.state.ring = true; this.ringRetryCount = 0
this.update('connected', 'Glasses and ring connected')
} catch (error) {
if (generation !== this.generation) return
this.state.ring = false
this.transport.disconnect(identifier)
this.update('connected', `Glasses connected; ring unavailable: ${this.message(error)}`)
} finally {
if (generation === this.generation) {
this.ringConnecting = false
if (!this.state.ring) this.scheduleRingRetry()
}
}
}
private scheduleRingRetry(): void {
if (!this.wanted || this.state.phase !== 'connected' || !this.ids.ring || this.ringConnecting || this.ringRetryTimer !== null || this.ringRetryCount >= 5) return
const generation = this.generation, delay = Math.min(30_000, 2000 * 2 ** this.ringRetryCount++)
this.log(`Retrying ring in ${delay / 1000}s`)
this.ringRetryTimer = setTimeout(() => {
this.ringRetryTimer = null
if (generation === this.generation) void this.connectRing(generation)
}, delay)
}
private check(generation: number): void { if (generation !== this.generation) throw new Error('Connection cancelled') }
private nextMagic(): number {
for (let i = 0; i < 156; i++) {
const magic = this.magic; this.magic = this.magic === 255 ? 100 : this.magic + 1
if (magic !== 156 && !this.cfwPending.some(item => item.magic === magic) && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
}
throw new Error('All message identifiers are in use')
}
private request(role: string, sid: number, build: (magic: number) => Uint8Array, label: string,
timeout = 3500, flag = 0x20, fixedMagic?: number): Promise<protocol.ProtocolMessage> {
const magic = fixedMagic ?? this.nextMagic(), payload = build(magic), key = `${sid}:${magic}`
this.log(`TX ${label} ${role} sid=${sid.toString(16)} magic=${magic}`)
const generation = this.generation
return new Promise((resolve, reject) => {
const pending: PendingAck = { resolve, reject, timer: null, command: protocol.readInteger(payload, 1, -1), label }
this.pending.set(key, pending)
this.send(role, sid, flag, payload).then(() => {
if (this.pending.get(key) !== pending || generation !== this.generation) return
pending.timer = setTimeout(() => {
this.pending.delete(key); reject(new AckTimeout(`${label} was not acknowledged by the glasses.`))
}, timeout)
}).catch(error => {
if (this.pending.get(key) !== pending) return
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer); reject(error)
})
})
}
private send(role: string, sid: number, flag: number, payload: Uint8Array): Promise<void> {
return this.writePackets(role, () => protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role]))
}
private writePackets(role: string, build: () => Uint8Array[], current = () => true): Promise<void> {
const identifier = this.ids[role], generation = this.generation
if (!identifier) return Promise.reject(new Error(`No ${role} device connected`))
const previous = this.writes.get(identifier) ?? Promise.resolve()
// A failed write poisons this link's queue until reset: do not transmit
// later fragments after a hole in the image stream.
const work = previous.then(async () => {
this.check(generation)
if (!current()) return
for (const frame of build()) {
this.check(generation)
if (!current()) return
await this.transport.write(identifier, protocol.G2_WRITE, frame)
iosBleTraffic.recordWrite(frame.length)
}
iosBleTraffic.recordMessage()
})
// A whole message owns the write queue; heartbeat packets cannot split an image message.
this.writes.set(identifier, work)
return work
}
private lease(acquire: boolean): Promise<void[]> {
return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire))))
}
private requestCfw(role: string, payload: Uint8Array): Promise<void> {
if (payload.length > CFW_MAX_MESSAGE - 16) return Promise.reject(new Error('CFW message too large'))
return new Promise((resolve, reject) => {
const item: CfwPending = { role, payload, checksum: protocol.crc16(payload), magic: this.nextMagic(), ackLenses: 0, retries: 0,
retryPending: false, deadline: 0, resolve, reject }
this.cfwPending.push(item)
this.sendCfw(item)
})
}
private sendCfw(item: CfwPending): void {
const generation = this.generation, magic = item.magic
const current = () => generation === this.generation && item.magic === magic && this.cfwPending.includes(item)
void this.writePackets(item.role, () => {
let transport = this.cfwTransports.get(item.role)
if (!transport) { transport = new CfwTransport(); this.cfwTransports.set(item.role, transport) }
if (item.retries) transport.reset()
return transport.encode(item.payload, magic, 3, this.limits[item.role])
}, current).then(() => {
if (!current()) return
item.deadline = Date.now() + 500
this.scheduleCfwRecovery()
}).catch(error => { if (current()) this.fail(error, true) })
}
private receiveCfw(identifier: string, packet: Uint8Array): void {
const acks = parseCfwAcks(packet)
if (!acks) return
for (const ack of acks) {
// The ingress temple relays the other lens's result on the SAME link.
const item = this.cfwPending.find(p => this.ids[p.role] === identifier && p.magic === ack.streamId)
if (!item || ack.messageId !== 0) continue
if (ack.nack) item.retryPending = true
else if (ack.size === item.payload.length && ack.checksum === item.checksum) item.ackLenses |= ack.lens
this.log(`CFW ${ack.nack ? 'NACK' : 'ACK'} stream=${ack.streamId} lens=${ack.lens} size=${ack.size} crc=${ack.checksum} confirmed=${item.ackLenses}`)
}
while (this.cfwPending.length) {
const head = this.cfwPending[0]
if (head.retryPending || head.ackLenses !== 3) break
this.cfwPending.shift(); head.resolve()
}
this.scheduleCfwRecovery()
}
private scheduleCfwRecovery(): void {
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
this.cfwTimer = null
let deadline = Infinity
for (const item of this.cfwPending) {
if (item.retryPending) deadline = 0
else if (item.ackLenses !== 3 && item.deadline) deadline = Math.min(deadline, item.deadline)
}
if (deadline === Infinity) return
this.cfwTimer = setTimeout(() => {
this.cfwTimer = null
this.recoverCfw()
}, Math.max(0, deadline - Date.now()))
}
private recoverCfw(): void {
if (!this.cfwPending.some(p => p.retryPending || (p.ackLenses !== 3 && p.deadline > 0 && p.deadline <= Date.now()))) {
this.scheduleCfwRecovery(); return
}
if (this.cfwPending.some(p => p.retries >= 3)) { this.fail(new Error('CFW recovery retry limit'), true); return }
this.log(`CFW recovery: replay ${this.cfwPending.length} unresolved messages`)
// Go back to the oldest unresolved command, including any ACKed tail.
// Fresh stream IDs reject stale ACKs; each replay resets compression history.
for (const item of this.cfwPending) {
item.magic = this.nextMagic(); item.retries++; item.ackLenses = 0
item.deadline = 0; item.retryPending = false
}
for (const item of this.cfwPending) this.sendCfw(item)
}
private receive(event: TransportEvent): void {
if (!Object.values(this.ids).includes(event.identifier ?? '')) return
if (event.kind === 'ancs-authorization' && event.identifier === this.ids.right) {
if (event.authorized === false) {
const active = this.notifications.state !== 'disconnected'
const command = this.notifications.stopCommand()
this.notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.')
if (active && this.state.phase === 'connected') void this.writePackets('right', () => [command]).catch(() => {})
} else if (this.state.phase === 'connected' && /^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION) {
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
}
return
}
if (event.kind === 'disconnected') {
if (event.identifier === this.ids.ring) {
this.state.ring = false
if (this.state.phase === 'connected') {
this.update('connected', `Glasses connected; ring disconnected${event.message ? ': ' + event.message : ''}`)
this.scheduleRingRetry()
}
} else if (['connected', 'connecting'].includes(this.state.phase)) this.fail(new Error(event.message || 'Glasses disconnected'), true)
return
}
if (event.kind !== 'notification' || !event.data) return
if (event.characteristic === protocol.G2_RENDER_NOTIFY && event.identifier !== this.ids.ring) {
if (this.audioListener && this.state.phase === 'connected') {
this.audioListener(hexToBytes(event.data))
this.onActivity(); this.wake()
}
return
}
const ring = event.identifier === this.ids.ring
if (ring ? !protocol.RING_NOTIFY.includes(event.characteristic ?? '') : event.characteristic !== protocol.G2_NOTIFY) return
try {
const data = hexToBytes(event.data)
if (ring) {
const input = protocol.decodeRingInput(data)
if (input) { this.log(`Direct R1 gesture ${input.eventType}`); this.onInput(input) }
return
}
if (event.identifier === this.ids.right && this.notifications.receive(data)) return
for (const message of this.receiver.receive(event.identifier!, data)) {
this.log(`RX ${event.identifier === this.ids.left ? 'L' : 'R'} sid=${message.sid.toString(16)} flag=${message.flag.toString(16)} cmd=${message.command} magic=${message.magic}`)
if (message.sid === protocol.SID.cfw) {
this.receiveCfw(event.identifier!, message.packet!)
continue
}
if (![1, 6].includes(message.flag)) {
// A master arm can relay the other arm's ACK; magic is allocated
// across BOTH links, matching Android's communicator.
const key = `${message.sid}:${message.magic}`, pending = this.pending.get(key)
if (pending) {
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer)
pending.resolve(message)
}
}
if (message.sid === protocol.SID.settings) this.applySettings(message)
const wearing = decodeWearState(message)
if (wearing !== null) this.onWearState(wearing)
if (event.identifier === this.ids.right) {
if (this.state.phase === 'connected') {
const compass = protocol.decodeCompassInput(message)
if (compass) this.onCompass(compass)
}
const input = protocol.decodeGlassesInput(message)
if (input) {
if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') {
// Respect an explicit glasses-side exit; the user can reconnect.
void this.stop(); continue
}
this.onInput(input)
}
}
}
} catch (error) { this.log(`Ignored malformed BLE notification: ${this.message(error)}`) }
finally {
// CoreBluetooth can wake a suspended iOS process for a notification.
// Service wall-clock deadlines now, without waiting for a suspended timer.
if (this.state.phase === 'connected') { this.onActivity(); this.wake() }
}
}
private applySettings(message: protocol.ProtocolMessage): void {
const values = protocol.readBytes(message.payload, 4)
const capabilities = protocol.readString(message.payload, 100)
if (capabilities) this.state.capabilities = capabilities
if (values) {
const left = protocol.readString(values, 5), right = protocol.readString(values, 6)
if (left) this.state.leftVersion = left
if (right) this.state.rightVersion = right
const battery = protocol.readInteger(values, 12, -1), charging = protocol.readInteger(values, 13, -1)
if (battery >= 0 && battery <= 100) this.state.battery = battery
if (charging >= 0) this.state.charging = this.charging = charging > 0
}
this.onState({ ...this.state })
}
async enableWearDetectionAndRequestState(): Promise<void> {
const generation = this.generation
// Like Android, a missing enable ACK must not suppress the current-state query.
try { await this.request('right', protocol.SID.settings, enableWearDetection, 'Enable wear detection') }
catch (error) { if (!(error instanceof AckTimeout)) throw error; this.log(this.message(error)) }
this.check(generation)
await this.send('right', protocol.SID.settings, 0x20, queryWearState())
this.check(generation)
await this.send('left', protocol.SID.settings, 0x20, queryWearState())
}
async setBrightness(level: number | null): Promise<void> {
if (this.state.phase !== 'connected') return
await this.request('right', protocol.SID.settings, magic => setBrightness(magic, level), 'Brightness')
}
setFrame(gray: Uint8Array, textures?: TextureFrame): void {
if (this.state.phase !== 'connected') return
this.latest = protocol.packGray4(gray, 640, 480)
this.latestTextures = textures ?? null
this.schedule(0)
}
/** Same mode-5 kind-4 sequencer payload and acknowledged image path as Android. */
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
if (this.state.phase !== 'connected' || !this.layoutCreated || payload.length < 3) return
const generation = this.generation
try { await this.requestCfw('left', new Uint8Array(payload)) }
catch (error) { if (generation === this.generation) this.fail(error, true) }
}
/** Serialize mic enable/disable, including a release while enable awaits ACK. */
setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise<void> {
const token = ++this.microphoneToken, generation = this.generation
this.audioListener = enabled ? listener ?? null : null
const current = () => generation === this.generation && token === this.microphoneToken
const work = this.microphoneWork.catch(() => {}).then(async () => {
if (!current()) return
if (this.state.phase !== 'connected') {
if (enabled) throw new Error('Connect the glasses before starting voice input.')
return
}
if (enabled) {
await Promise.all(['left', 'right'].map(role => this.transport.subscribe(this.ids[role], protocol.G2_RENDER_NOTIFY)))
if (!current()) return
}
await this.request('right', protocol.SID.hub, magic => protocol.audioControl(magic, enabled), enabled ? 'Microphone enable' : 'Microphone disable')
if (current()) { this.microphoneEnabled = enabled; this.log(`Glasses microphone ${enabled ? 'enabled' : 'disabled'}`) }
}).catch(error => {
if (current()) { this.audioListener = null; this.microphoneEnabled = false }
throw error
})
this.microphoneWork = work
return work
}
setCompassEnabled(enabled: boolean): void {
this.compassWanted = enabled
void this.syncCompass()
}
/** Serialize changes so a release cannot overtake an unacknowledged enable. */
private syncCompass(): Promise<void> {
const generation = this.generation
const work = this.compassWork.then(async () => {
if (generation !== this.generation || this.state.phase !== 'connected') return
const enabled = !this.compassStopping && this.compassWanted
if (this.compassSent === enabled) return
// Same CFW mode 10, 100 ms report interval and zero minimum change as Android.
await this.requestCfw('left', new Uint8Array(enabled ? [CFW_MSG_COMPASS, 2, 100, 0, 0, 0] : [CFW_MSG_COMPASS, 0]))
if (generation === this.generation) this.compassSent = enabled
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
this.compassWork = work
return work
}
wake(): void {
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
void this.pump()
}
private schedule(delay = 1000): void {
if (delay === 0 && this.timer !== null) { clearTimeout(this.timer); this.timer = null }
if (this.timer !== null || this.pumping || this.state.phase !== 'connected') return
this.timer = setTimeout(() => { this.timer = null; void this.pump() }, delay)
}
private canSendDisplay(): boolean {
if (this.displaySending?.commands[this.displaySending.offset][0] === CFW_MSG_EVICT_RESOURCE && this.cfwPending.length) return false
// Also bound completed frames retained behind a missing/out-of-order ACK.
return this.displayInFlight < DISPLAY_WINDOW_SIZE && !!(this.displaySending ||
(this.latest && !this.charging && this.displayFrames.length < DISPLAY_WINDOW_SIZE))
}
private fillDisplayWindow(generation: number): void {
while (this.canSendDisplay()) {
if (!this.displaySending) {
const packed = this.latest!; this.latest = null
const textureFrame = this.latestTextures; this.latestTextures = null
// Compare with the last enqueued image, not the last ACKed one. In an
// A -> B -> A sequence, B may still be in flight when A is requested.
if (!textureFrame && this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
const texturePlan = textureFrame ? this.textures?.plan(this.lastEnqueued, packed, textureFrame, this.nextImageFrameId) : null
let commands: Uint8Array[]
if (texturePlan) {
// Resource commands participate in the same ordered, acknowledged command window as
// the image. Coalescing only happens before planning mutates residency.
commands = [...texturePlan.resourceCommands, texturePlan.payload]
this.nextImageFrameId = texturePlan.nextFid
this.log(`Display textures (${texturePlan.resourceCommands.length} resource commands, cache=${texturePlan.usedBytes}B)`)
} else {
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
const payload = delta ?? protocol.concat(new Uint8Array([CFW_MSG_FULL_FRAME]), protocol.rle4(packed))
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
commands = payload.length <= CFW_MAX_MESSAGE - 16 ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
if (payload.length > CFW_MAX_MESSAGE - 16) for (const _ of commands)
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
commands = [...commands.flatMap(command => drawMessages(command)), new Uint8Array([CFW_MSG_PRESENT])]
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
}
this.displaySending = { packed, commands, offset: 0, pending: 0 }
this.displayFrames.push(this.displaySending); this.lastEnqueued = packed
}
const frame = this.displaySending
// Drain earlier commands before evicting: recovery may replay any unresolved draw/upload.
if (frame.commands[frame.offset][0] === CFW_MSG_EVICT_RESOURCE && this.cfwPending.length) return
const payload = frame.commands[frame.offset++]
frame.pending++; this.displayInFlight++
if (frame.offset >= frame.commands.length) this.displaySending = null
// Writes serialize on L while up to three complete commands await their
// two-lens ACKs. Retire commands in submission order.
void this.requestCfw('left', payload).then(() => {
if (generation !== this.generation) return
frame.pending--; this.displayInFlight--; this.lastHeartbeat = Date.now()
// Retire in submission order even if relayed ACKs arrive out of order.
// A frame counts only after every command has been acknowledged.
while (this.displayFrames.length) {
const completed = this.displayFrames[0]
if (completed.offset < completed.commands.length || completed.pending) break
this.displayFrames.shift(); this.displayed = completed.packed
this.state.frames++; iosBleTraffic.recordDisplayFrame(); this.onState({ ...this.state })
this.log(`Display frame ${this.state.frames} acknowledged (${completed.commands.length} commands)`)
}
this.wake()
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
}
}
private async pump(): Promise<void> {
if (this.pumping || this.state.phase !== 'connected') return
this.pumping = true; const generation = this.generation
try {
const now = Date.now()
// Firmware frees resources when its 90-second framebuffer lease expires.
// After a long iOS suspension, reconnect rather than replaying pending
// draws against unknown memory. Leave a margin for the two arm writes.
if (now - this.lastLease >= 80_000) throw new Error('Display lease lapsed; restarting session')
if (now - this.lastHeartbeat >= 4000) {
const gap = now - this.lastHeartbeat
await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now()
this.log(`Heartbeat acknowledged (gap ${gap}ms)`)
}
if (now - this.lastLease >= 45_000) { await this.lease(true); this.lastLease = Date.now() }
if (now - this.lastSettings >= (this.charging ? 30_000 : 300_000)) {
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Battery query')
this.applySettings(settings); this.lastSettings = Date.now()
}
this.check(generation)
this.fillDisplayWindow(generation)
} catch (error) { if (generation === this.generation) this.fail(error, true) }
finally { if (generation === this.generation) { this.pumping = false; this.schedule(this.canSendDisplay() ? 0 : 1000) } }
}
private reset(): void {
this.notifications.stop()
++this.generation
this.compassSent = null; this.compassWork = Promise.resolve()
++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false
if (this.timer !== null) clearTimeout(this.timer)
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
if (this.ringRetryTimer !== null) clearTimeout(this.ringRetryTimer)
this.ringRetryTimer = null; this.ringConnecting = false; this.ringRetryCount = 0
this.timer = this.retryTimer = null; this.pumping = false
const pending = [...this.pending.values()]; this.pending.clear()
for (const request of pending) { if (request.timer) clearTimeout(request.timer); request.reject(new Error('Connection ended')) }
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
this.cfwTimer = null
const custom = this.cfwPending; this.cfwPending = []
for (const item of custom) item.reject(new Error('Connection ended'))
this.cfwTransports.clear()
this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null
this.latestTextures = null; this.textures?.reset()
this.lastEnqueued = null; this.displayFrames = []; this.displaySending = null; this.displayInFlight = 0
}
private closeLinks(): void {
const ids = this.ids; this.ids = {}; this.limits = {}
for (const identifier of Object.values(ids)) this.transport.disconnect(identifier)
this.transport.stopScan(); this.layoutCreated = false; this.state.ring = false
}
private fail(error: unknown, retry: boolean): void {
const message = this.message(error); this.reset(); this.closeLinks()
if (retry && this.wanted && this.addresses && this.retryCount < 5) {
const delay = Math.min(30_000, 2000 * 2 ** this.retryCount++)
this.update('retrying', `${message} Retrying in ${delay / 1000}s…`)
this.retryTimer = setTimeout(() => { this.retryTimer = null; if (this.wanted && this.addresses) void this.start(this.addresses, true) }, delay)
} else { this.wanted = false; this.update('error', message) }
}
stop(): Promise<void> {
if (this.stopping) return this.stopping
this.stopping = this.stopInternal().finally(() => { this.stopping = null })
return this.stopping
}
private async stopInternal(): Promise<void> {
this.wanted = false
this.compassStopping = true
await this.syncCompass()
// Stop mic capture before ending the display session. Late enable ACKs
// cannot leave a microphone streaming after an explicit Disconnect.
if (this.audioListener || this.microphoneEnabled) {
try { await this.setMicrophone(false) } catch (error) { this.log(`Microphone cleanup: ${this.message(error)}`) }
}
if (this.notifications.state !== 'disconnected') {
try { await this.writePackets('right', () => [this.notifications.stopCommand()]) } catch {}
}
const cleanup = this.layoutCreated && this.state.phase === 'connected'
this.update('disconnecting', 'Disconnecting…'); this.reset()
try {
if (cleanup) {
await this.requestCfw('left', new Uint8Array([CFW_MSG_CLEANUP]))
}
} catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) }
finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') }
}
dispose(): void { void this.stop().finally(() => this.off()) }
private message(error: unknown): string { return error instanceof Error ? error.message : String(error) }
}
+390 -123
View File
@@ -17,10 +17,11 @@ import { IosNavigationSensors } from '../native/ios-navigation-sensors'
import { Utils } from '@nativescript/core'
import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
import { iosBluetooth } from '../native/ios-bluetooth'
import { iosVoiceInput } from '../native/ios-voice-input'
import { nightscoutBridge } from '../native/nightscout-bridge'
import { GlassesSession, type SessionState } from './glasses-session'
import { FaceclawCommunicatorBridge, resolveIosPeripherals, type CommunicatorState, type RawInputEvent } from '../native/faceclaw-communicator.ios'
import { PreviewDisplayTarget, type DisplayTarget } from '../native/preview-display.ios'
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
import { GlanceHost, type GlanceDisplay } from './glance-host'
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
import { OsEventTypeList } from './events'
@@ -34,12 +35,9 @@ import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type
import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings'
import { readPhoneBatteryState } from '../native/phone-battery'
import { iosAppUnavailableReason } from '../apps/ios-availability'
import { SurfaceCompositor } from '../graphics/surface-compositor'
import { flattenPlanesWithDraws, type Plane } from '../graphics/plane'
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from '../graphics/plane'
import { prepareFrameDraws } from '../graphics/glyph-wire'
import { IosTexturePlanner } from '../native/texture-planner.ios'
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image'
import { previewPixels } from '../native/ios-graphics'
import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/gestures'
import { noopLayerActions, type LayerActions } from '../ui/layers'
import { TextViewerLayer } from '../apps/files/text-viewer'
@@ -51,47 +49,75 @@ import type { PhoneGesture } from '../phone-ui/phone-gestures'
import { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state'
import { findSoundEffect, playSoundEffect } from '../ui/sound-effects'
/** iOS host for the shared app registry, shell, compositor and BLE session. */
const SHELL_SURFACE_ID = 'shell'
const SHELL_SURFACE_Z_ORDER = 1
const LOCK_SURFACE_Z_ORDER = 1000
/** The shell render loop waits this long for the glasses to take a frame before moving on. */
const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 2000
export type SessionPhase = 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error'
export type SessionState = { phase: SessionPhase; status: string; battery: number | null; charging: boolean | null;
ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
function ancsCapable(capabilities: string): boolean {
return /^Faceclaw\/(\d+)/.test(capabilities) && Number(capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION
}
/**
* iOS host for the shared app registry, shell and BLE session. The session,
* compositor and transport live in shared Kotlin (FaceclawCommunicatorBridge
* over FaceclawKitIosGlassesSession); with no glasses connected the same
* compositor runs headless (PreviewDisplayTarget) so the phone mirror works.
*/
export class IosPreviewController {
private readonly compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT)
private previewTarget: PreviewDisplayTarget | null = null
private communicator: FaceclawCommunicatorBridge | null = null
private readonly startedCommunicators = new WeakSet<FaceclawCommunicatorBridge>()
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false,
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
private notifications: AncsClient | null = null
private readonly sessionOffs: (() => void)[] = []
private stopping: Promise<void> | null = null
/** Resolves once the current display target has every surface configured. */
private displayReady: Promise<void> = Promise.resolve()
private connecting = false
private sessionCreated = false
private wearDetectionRequested = false
private readonly glanceDisplay: GlanceDisplay = {
configureSurface: async (id, options) => {
this.compositor.configureSurface(id, options)
},
setSurfaceVisible: async (id, visible) => {
this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame()
},
setScreenBlanked: async blanked => {
this.compositor.setScreenBlanked(blanked); this.scheduleFrame()
},
submitSurfaceFrame: async (id, pixels, rect, _fingerprint, _paintMs, _frameId, draws) => {
this.compositor.submitSurfaceFrame(id, pixels, rect, draws); this.scheduleFrame()
configureSurface: async (id, options) => { await this.display?.configureSurface(id, options) },
setSurfaceVisible: async (id, visible) => { await this.display?.setSurfaceVisible(id, visible); this.schedulePreviewUpdate() },
setScreenBlanked: async blanked => { await this.display?.setScreenBlanked(blanked); this.schedulePreviewUpdate() },
submitSurfaceFrame: async (id, pixels, rect, fingerprint, paintMs, frameId, draws) => {
await this.display?.submitSurfaceFrame(id, pixels, rect, fingerprint, paintMs, frameId, draws); this.schedulePreviewUpdate()
},
}
private readonly glance = new GlanceHost({
getDisplay: () => this.glanceDisplay,
getDisplay: () => this.display ? this.glanceDisplay : null,
getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null,
canShow: () => this.runtimeNeeded && !this.glassesLocked,
// iOS retains the EvenHub session while the shell sleeps. The board's
// opaque first frame is ready before we unblank the compositor.
ensureSessionActive: async () => {
this.compositor.setScreenBlanked(false); this.scheduleFrame(); return true
await this.display?.setScreenBlanked(false); this.schedulePreviewUpdate(); return true
},
onHiddenWhileAsleep: () => {
if (!shell.isScreenOn()) this.compositor.setScreenBlanked(true)
this.scheduleFrame()
if (!shell.isScreenOn()) void this.display?.setScreenBlanked(true).then(() => this.schedulePreviewUpdate())
else this.schedulePreviewUpdate()
},
onVisibilityChanged: () => this.scheduleFrame(),
onVisibilityChanged: () => this.schedulePreviewUpdate(),
appendLog: message => this.logBluetooth(message),
})
private renderTimer: ReturnType<typeof setTimeout> | null = null
private previewTimer: ReturnType<typeof setTimeout> | null = null
private clockTimer: ReturnType<typeof setInterval> | null = null
private offSettings: (() => void) | null = null
private lastBrightness: string | null = null
private active = false
private runtimeRunning = false
private clockMinute = -1
private shellDirty = true
private shellRenderInProgress = false
private shellRenderQueued = false
private nextFrameId = 1
private nextWorkerFrame = 0
private inputQueue: Promise<void> = Promise.resolve()
private lastLayout = ''
private readonly appHosts = new Map<string, WorkerAppHost>()
@@ -102,13 +128,12 @@ export class IosPreviewController {
private glassesWorn: boolean | null = null
private glassesLocked = false
private lockEnabled = lockScreenEnabledSetting.get()
private session: GlassesSession | null = null
private acknowledgedFrames = 0
private logLines: string[] = []
private logTimer: ReturnType<typeof setTimeout> | null = null
private readonly actions: LayerActions = {
...noopLayerActions,
playBuzzerSequence: payload => this.session?.playBuzzerSequence(payload),
playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload),
requestRender: () => this.requestShellRender(),
disconnect: () => this.disconnect(),
startVoiceCapture: endpointing => this.startVoiceCapture(endpointing),
@@ -130,11 +155,7 @@ export class IosPreviewController {
private readonly onError: (message: string) => void,
private readonly onConnectionState: (state: SessionState) => void = () => {},
private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => void = () => {}) {
this.compositor.configureSurface('shell', { x: 0, y: 0, width: 640, height: 480, zOrder: 1, transparency: 'color-key' })
this.compositor.configureSurface(LOCK_SCREEN_SURFACE_ID, {
x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: 1000, transparency: 'opaque',
})
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, false)
this.ensurePreviewDisplay()
shell.configure({
actions: this.actions,
voiceInputEnabled: true,
@@ -151,7 +172,8 @@ export class IosPreviewController {
onWindowsChanged: () => this.requestShellRender(),
onScreenStateChanged: on => {
if (on) this.glance.dismiss()
this.compositor.setScreenBlanked(!on); this.requestShellRender()
void this.display?.setScreenBlanked(!on).catch(error => this.fail(error))
this.requestShellRender()
},
})
const launcher = createLauncherWindow({
@@ -160,9 +182,9 @@ export class IosPreviewController {
launchApp: id => this.launchApp(id),
uninstallApp: id => this.uninstallApp(id),
submitFrame: planes => this.submit(LAUNCHER_SURFACE_ID, planes),
setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(LAUNCHER_SURFACE_ID, visible); this.scheduleFrame() },
setSurfaceVisible: visible => this.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible),
})
this.configureWindow(launcher)
void this.configureWindow(launcher)
shell.registerWindow(launcher)
shell.wake('window')
shell.focusWindow(launcher.windowId)
@@ -198,14 +220,54 @@ export class IosPreviewController {
for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app))
}
/** The compositor frames go to: the live session, else the headless preview. */
private get display(): DisplayTarget | null { return this.communicator ?? this.previewTarget }
private ensurePreviewDisplay(): void {
if (this.communicator || this.previewTarget) return
const target = new PreviewDisplayTarget()
this.previewTarget = target
target.activate(() => this.schedulePreviewUpdate())
this.displayReady = this.registerSurfaces(target).catch(error => this.fail(error))
}
private teardownPreviewDisplay(): void {
const target = this.previewTarget
if (!target) return
this.previewTarget = null
this.glance.reset()
target.release()
}
/** (Re)create every compositor surface on a fresh display target. */
private async registerSurfaces(target: DisplayTarget): Promise<void> {
await target.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT)
await target.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT,
zOrder: SHELL_SURFACE_Z_ORDER, transparency: 'color-key' })
await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1)
await target.configureSurface(LOCK_SCREEN_SURFACE_ID, {
x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: LOCK_SURFACE_Z_ORDER, transparency: 'opaque',
})
if (this.glassesLocked) await this.submitLockImage(target)
await target.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, this.glassesLocked)
await target.setScreenBlanked(!shell.isScreenOn())
this.lastLayout = ''
for (const window of shell.getWindows()) await this.configureWindow(window, target)
}
private async submitLockImage(target: DisplayTarget): Promise<void> {
const image = createLockScreenImage()
await target.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(),
{ x: 0, y: 0, width: image.width, height: image.height }, 'lock-screen', 0, 0)
}
resume(): void {
if (this.active) return
this.active = true
this.syncRuntime()
this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable)
this.logBluetooth('Phone foreground')
if (this.session) this.onConnectionState({ ...this.session.state })
this.session?.wake()
if (this.communicator) this.onConnectionState({ ...this.state })
this.relayout()
shell.foregroundWindow()?.requestRender()
this.requestShellRender()
@@ -216,12 +278,11 @@ export class IosPreviewController {
iosVoiceInput.stopPhoneCapture()
// The phone preview is hidden; the glasses still own their screen and input.
this.syncRuntime()
this.logBluetooth(`Phone background; glasses ${this.session?.state.phase ?? 'disconnected'}`)
this.logBluetooth(`Phone background; glasses ${this.state.phase}`)
this.flushBluetoothLog()
this.session?.wake()
}
private get runtimeNeeded(): boolean {
return this.active || !!this.session && ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.session.state.phase)
return this.active || ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.state.phase)
}
private syncRuntime(): void {
const running = this.runtimeNeeded
@@ -231,14 +292,14 @@ export class IosPreviewController {
if (!running) {
void nightscoutBridge.stop().catch(error => this.fail(error))
this.glance.dismiss()
this.compositor.setScreenBlanked(!shell.isScreenOn())
void this.display?.setScreenBlanked(!shell.isScreenOn()).catch(error => this.fail(error))
this.offSettings?.(); this.offSettings = null
for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
UIDevice.currentDevice.batteryMonitoringEnabled = false
if (this.clockTimer !== null) clearInterval(this.clockTimer)
if (this.renderTimer !== null) clearTimeout(this.renderTimer)
this.clockTimer = this.renderTimer = null
if (this.previewTimer !== null) clearTimeout(this.previewTimer)
this.clockTimer = this.previewTimer = null
return
}
UIDevice.currentDevice.batteryMonitoringEnabled = true
@@ -268,12 +329,13 @@ export class IosPreviewController {
this.clockTimer = setInterval(() => this.refreshClock(), 60_000)
}
private pushBrightness(): void {
const session = this.session
if (session?.state.phase !== 'connected') { this.lastBrightness = null; return }
const communicator = this.communicator
if (!communicator || this.state.phase !== 'connected') { this.lastBrightness = null; return }
const value = brightnessSetting.get()
if (value === this.lastBrightness) return
this.lastBrightness = value
void session.setBrightness(brightnessSettingToLevel(value)).catch(error => {
const level = brightnessSettingToLevel(value)
void communicator.setBrightness(level === null, level ?? 0).catch(error => {
this.lastBrightness = null
this.logBluetooth(`Brightness: ${String(error)}`)
})
@@ -285,12 +347,13 @@ export class IosPreviewController {
if (!enabled) this.setGlassesLocked(false)
else {
if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true)
if (this.session?.state.phase === 'connected')
void this.session.enableWearDetectionAndRequestState().catch(error => this.fail(error))
if (this.communicator && this.state.phase === 'connected')
void this.communicator.enableWearDetectionAndRequestState().catch(error => this.fail(error))
}
}
private handlePhoneLockState(locked: boolean): void {
this.phoneLocked = locked
this.communicator?.onPhoneLockSignal()
if (!locked) this.setGlassesLocked(false)
else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true)
}
@@ -304,17 +367,21 @@ export class IosPreviewController {
}
private setGlassesLocked(locked: boolean): void {
if (locked === this.glassesLocked) return
if (locked) {
const image = createLockScreenImage()
this.compositor.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(),
{ x: 0, y: 0, width: image.width, height: image.height })
}
this.glassesLocked = locked
if (locked) {
this.glance.dismiss()
iosVoiceInput.handleSessionEnded('Glasses locked. Unlock your phone to start voice input again.')
}
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked)
void (async () => {
await this.displayReady
const display = this.display
if (!display) return
{
if (locked) await this.submitLockImage(display)
await display.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked)
this.schedulePreviewUpdate()
}
})().catch(error => this.fail(error))
this.logBluetooth(`Glasses ${locked ? 'locked' : 'unlocked'}`)
this.requestShellRender()
}
@@ -322,45 +389,90 @@ export class IosPreviewController {
const minute = Math.floor(Date.now() / 60_000)
if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() }
}
private configureWindow(window: ShellWindow): void {
this.compositor.configureSurface(window.surfaceId, {
private async configureWindow(window: ShellWindow, target: DisplayTarget | null = this.display): Promise<void> {
if (!target) return
await target.configureSurface(window.surfaceId, {
...appViewportRect(window.heightMode, window.appId), zOrder: 0, transparency: 'opaque',
})
this.compositor.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId)
await target.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId)
}
private relayout(): void {
const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";")
if (layout === this.lastLayout) return
this.lastLayout = layout
for (const window of shell.getWindows()) {
this.configureWindow(window)
void this.configureWindow(window).catch(error => this.fail(error))
window.relayout?.()
}
}
/** Submit a painted frame for an in-process window (e.g. the launcher). */
private async submit(id: string, planes: Plane[]): Promise<void> {
await this.displayReady
const display = this.display
if (!display) return
const fingerprint = planesFingerprint(planes)
const { image, draws } = flattenPlanesWithDraws(planes)
this.compositor.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, prepareFrameDraws(draws))
this.scheduleFrame()
await display.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height },
fingerprint, -1, 0, prepareFrameDraws(draws))
this.schedulePreviewUpdate()
}
private requestShellRender(): void { this.shellDirty = true; this.scheduleFrame() }
private scheduleFrame(): void {
if (!this.runtimeNeeded || this.renderTimer !== null) return
// Coalesce window/chrome updates into one glasses frame, capped at 30 fps.
this.renderTimer = setTimeout(() => {
this.renderTimer = null
if (!this.runtimeNeeded) return
/** Worker windows ship baked pixels; every submit is new content to the compositor. */
private async submitWorkerPixels(id: string, pixels: Uint8Array, width: number, height: number, draws: ArrayBuffer | null): Promise<void> {
await this.displayReady
const display = this.display
if (!display) return
await display.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, `${id}#${++this.nextWorkerFrame}`, -1, 0, draws)
this.schedulePreviewUpdate()
}
private setWindowSurfaceVisible(id: string, visible: boolean): void {
void this.display?.setSurfaceVisible(id, visible).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error))
}
private removeWindowSurface(id: string): void {
void this.display?.removeSurface(id).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error))
}
/** Re-render and resubmit the shell surface; one render in flight, at most one queued. */
private requestShellRender(): void {
if (this.shellRenderInProgress) { this.shellRenderQueued = true; return }
this.shellRenderInProgress = true
void (async () => {
try {
if (this.shellDirty) {
this.shellDirty = false
this.compositor.setShellScene(shell.paintScene())
}
const { pixels, textures } = this.compositor.compositeFrame()
this.session?.setFrame(pixels, textures)
if (this.active) {
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
}
do {
this.shellRenderQueued = false
await this.renderShell()
} while (this.shellRenderQueued)
} catch (error) { this.fail(error) }
finally { this.shellRenderInProgress = false }
})()
}
private async renderShell(): Promise<void> {
await this.displayReady
const display = this.display
if (!display) return
const startedAt = Date.now()
const bytes = shell.paintScene()
const frameId = this.allocateFrameId()
await display.submitShellScene(bytes, Date.now() - startedAt, frameId)
// Backpressure: the next shell render waits for this one to reach the
// glasses (the preview target resolves immediately; nothing transmits).
await display.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS)
this.schedulePreviewUpdate()
}
private allocateFrameId(): number {
const id = this.nextFrameId
this.nextFrameId = this.nextFrameId >= 0x7fffffff ? 1 : this.nextFrameId + 1
return id
}
/** Refresh the phone mirror from the composited screen, coalesced at 30 fps. */
private schedulePreviewUpdate(): void {
if (!this.active || this.previewTimer !== null) return
this.previewTimer = setTimeout(() => {
this.previewTimer = null
if (!this.active) return
try {
const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green')
if (!image) return
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
} catch (error) { this.fail(error) }
}, 33)
}
@@ -469,7 +581,7 @@ export class IosPreviewController {
launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create),
ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create),
submitWindowFrame: (id, planes) => this.submit(id, planes),
setWindowSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() },
setWindowSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
requestShellRender: () => this.requestShellRender(), appendLog: message => console.log(`[ios-app] ${message}`),
setTextEditorHost: () => {},
}
@@ -484,13 +596,17 @@ export class IosPreviewController {
let surfaceReady = false
const app = create({
actions: this.actions, submitFrame: planes => surfaceReady ? this.submit(surfaceId, planes) : Promise.resolve(),
setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(surfaceId, visible); this.scheduleFrame() },
removeSurface: () => { surfaceReady = false; this.compositor.removeSurface(surfaceId); this.scheduleFrame() },
reconfigureSurface: () => { const window = shell.getWindows().find(w => w.windowId === windowId); if (window) this.configureWindow(window); this.requestShellRender() },
setSurfaceVisible: visible => this.setWindowSurfaceVisible(surfaceId, visible),
removeSurface: () => { surfaceReady = false; this.removeWindowSurface(surfaceId) },
reconfigureSurface: () => {
const window = shell.getWindows().find(w => w.windowId === windowId)
if (window) void this.configureWindow(window).catch(error => this.fail(error))
this.requestShellRender()
},
onClosed: () => { this.inProcessApps.delete(windowId) },
})
this.inProcessApps.set(windowId, app)
this.configureWindow(app.window)
await this.configureWindow(app.window)
surfaceReady = true
shell.registerWindow(app.window); shell.focusWindow(windowId)
app.requestRender(); this.requestShellRender()
@@ -507,13 +623,15 @@ export class IosPreviewController {
playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload),
openUrl: url => { void Utils.openUrl(url) },
configureSurface: async (id, visible, heightMode) => {
this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
this.compositor.setSurfaceVisible(id, visible)
const display = this.display
if (!display) return
await display.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
await display.setSurfaceVisible(id, visible)
},
setSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() },
removeSurface: id => { this.compositor.removeSurface(id); this.scheduleFrame() },
setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
removeSurface: id => this.removeWindowSurface(id),
submitPixels: (id, pixels, width, height, draws) => {
this.compositor.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, draws); this.scheduleFrame()
void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error))
},
requestShellRender: () => this.requestShellRender(),
openSettings: section => { void this.launchApp('settings', { section }) },
@@ -529,45 +647,193 @@ export class IosPreviewController {
displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length])
}
get displayModeLabel(): string { return displayModeLabel(displayModeSetting.get()) }
get connectionState(): SessionState | null { return this.session?.state ?? null }
get connectionState(): SessionState | null { return this.sessionCreated ? this.state : null }
get connectionDetails(): string {
const state = this.connectionState
return state ? `${state.status}\nL: ${state.leftVersion || 'unknown'}\nR: ${state.rightVersion || 'unknown'}\nBattery: ${state.battery ?? '?'}%\nRing: ${state.ring ? 'connected' : 'disconnected'}\nFrames acknowledged: ${state.frames}\n${state.capabilities}\n\n${this.logLines.slice(-12).join('\n')}` : 'Preview only. Configure devices, then connect.'
}
private update(phase: SessionPhase, status: string): void {
this.state = { ...this.state, phase, status }
this.logBluetooth(status)
this.emitState()
}
private emitState(): void {
if (this.state.phase !== 'connected') resetRingInputFilter()
this.pushBrightness()
this.maybePlayWelcomeSound(this.state)
if (this.state.phase !== 'connected' && this.state.phase !== 'connecting') this.glassesWorn = null
if (this.state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging })
this.syncRuntime()
if (this.active) this.onConnectionState({ ...this.state })
this.requestShellRender()
}
async connect(): Promise<void> {
if (!this.active) return
if (!this.active || this.connecting || this.stopping) return
if (this.communicator && ['connecting', 'connected', 'retrying', 'disconnecting'].includes(this.state.phase)) return
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
if (error) { this.onError(error); return }
if (!this.session) {
this.session = new GlassesSession(iosBluetooth(), state => {
if (state.phase !== "connected") resetRingInputFilter()
this.pushBrightness()
this.maybePlayWelcomeSound(state)
if (state.phase !== 'connected' && state.phase !== 'connecting') this.glassesWorn = null
if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging })
this.syncRuntime()
if (this.active) this.onConnectionState(state)
this.requestShellRender()
}, input => {
this.inputQueue = this.inputQueue.then(async () => {
if (this.session?.state.phase !== 'connected') return
await this.receiveInput(rawInputEventToInputEvent(input),
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
}).catch(error => this.fail(error))
}, message => this.logBluetooth(message), () => {
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
this.refreshClock()
}, receiveCompassEvent, wearing => this.handleWearState(wearing), (key, popup) => {
iosNotificationsChanged(key, popup); this.requestShellRender()
}, new IosTexturePlanner())
bindIosNotifications(this.session.notifications)
bindCompassSession(this.session)
this.connecting = true
this.sessionCreated = true
try {
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
this.update('connecting', 'Finding configured devices…')
let identifiers
try { identifiers = await resolveIosPeripherals(addresses) }
catch (failure) {
this.update('error', failure instanceof Error ? failure.message : String(failure))
return
}
if (this.communicator) await this.closeCommunicator()
const communicator = new FaceclawCommunicatorBridge(identifiers)
this.teardownPreviewDisplay()
this.communicator = communicator
this.wearDetectionRequested = false
this.bindCommunicator(communicator)
this.displayReady = this.registerSurfaces(communicator)
await this.displayReady
this.update('connecting', 'Connecting to the glasses…')
this.startedCommunicators.add(communicator)
await communicator.start()
for (const window of shell.getWindows()) window.requestRender()
this.requestShellRender()
} catch (failure) {
this.fail(failure)
await this.closeCommunicator().catch(() => {})
this.ensurePreviewDisplay()
this.update('error', failure instanceof Error ? failure.message : String(failure))
} finally {
this.connecting = false
}
await this.session.start(addresses)
}
async disconnect(): Promise<void> { await this.session?.stop() }
private bindCommunicator(communicator: FaceclawCommunicatorBridge): void {
const notifications = new AncsClient(packet => communicator.writeRawToRight(packet), (key, popup) => {
iosNotificationsChanged(key, popup); this.requestShellRender()
}, message => this.logBluetooth('ANCS: ' + message))
this.notifications = notifications
bindIosNotifications(notifications)
bindCompassSession(communicator)
communicator.setRequiresAncs(true)
this.sessionOffs.push(
communicator.onStateChange(state => this.handleSessionState(communicator, state)),
communicator.onRingEvent(input => this.handleRingEvent(input)),
communicator.onBatteryState(state => {
if (this.communicator !== communicator) return
this.state = { ...this.state, battery: state.battery >= 0 && state.battery <= 100 ? state.battery : this.state.battery,
charging: state.chargingStatus >= 0 ? state.chargingStatus > 0 : this.state.charging,
ring: (state.ringBattery ?? -1) >= 0 || this.state.ring }
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging })
if (this.active) this.onConnectionState({ ...this.state })
this.requestShellRender()
}),
communicator.onFirmwareInfo(info => {
if (this.communicator !== communicator) return
this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion,
rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities }
if (this.active) this.onConnectionState({ ...this.state })
this.syncNotifications(communicator)
}),
communicator.onFrameMetrics(() => {
if (this.communicator !== communicator) return
this.state = { ...this.state, frames: this.state.frames + 1 }
this.onActivity()
this.maybePlayWelcomeSound(this.state)
if (this.active) this.onConnectionState({ ...this.state })
this.schedulePreviewUpdate()
}),
communicator.onWearState(wearing => { if (this.communicator === communicator) this.handleWearState(wearing) }),
communicator.addCompassListener(receiveCompassEvent),
communicator.onAncsRelayFrame(frame => { if (this.communicator === communicator) notifications.receive(frame) }),
communicator.onAncsAuthorization(authorized => {
if (this.communicator !== communicator) return
if (authorized) { this.syncNotifications(communicator); return }
const active = notifications.state !== 'disconnected'
const command = notifications.stopCommand()
notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.')
if (active && this.state.phase === 'connected') void communicator.writeRawToRight(command).catch(() => {})
}),
)
}
private handleSessionState(communicator: FaceclawCommunicatorBridge, state: CommunicatorState): void {
if (this.communicator !== communicator) return
// Registering the listener makes the Kotlin session report its current state, which is
// "disconnected" before start(); that must not be mistaken for the session ending.
if (!this.startedCommunicators.has(communicator)) return
const phase: SessionPhase = state.phase === 'charging' ? 'connected' : state.phase === 'unpaired' ? 'error' : state.phase
if (phase === 'connected' && this.state.phase !== 'connected') {
this.state = { ...this.state, frames: this.state.frames }
if (!this.wearDetectionRequested) {
this.wearDetectionRequested = true
void communicator.enableWearDetectionAndRequestState().catch(error => this.logBluetooth(`Wear detection: ${String(error)}`))
}
}
this.update(phase, state.status)
if (phase === 'connected') this.syncNotifications(communicator)
else if ((phase === 'disconnected' || phase === 'error') && !this.stopping) {
// The session ended on its own (retries exhausted, an arm unpaired): release it and go headless.
void this.disconnect(state.status)
}
}
private handleRingEvent(input: RawInputEvent): void {
this.inputQueue = this.inputQueue.then(async () => {
if (this.state.phase !== 'connected') return
this.onActivity()
if (input.ringInput) this.state = { ...this.state, ring: true }
await this.receiveInput(rawInputEventToInputEvent(input),
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
}).catch(error => this.fail(error))
}
/** Any glasses traffic: CoreBluetooth can wake a suspended process, so refresh what timers would have. */
private onActivity(): void {
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
this.refreshClock()
}
/** Start iPhone notification relay once the firmware is known to support it and ANCS is authorized. */
private syncNotifications(communicator: FaceclawCommunicatorBridge): void {
const notifications = this.notifications
if (!notifications || this.state.phase !== 'connected') return
if (!ancsCapable(this.state.capabilities)) {
if (this.state.capabilities) notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
return
}
if (notifications.state !== 'disconnected') return
notifications.start((Date.now() ^ Math.floor(Math.random() * 0xffffffff)) >>> 0, communicator.rightWriteLimit())
}
async disconnect(reason?: string): Promise<void> {
if (this.stopping) return this.stopping
const communicator = this.communicator
if (!communicator) return
// Marked before any bridge call: the session may report "disconnected" from inside disconnect().
let finished!: () => void
this.stopping = new Promise<void>(resolve => { finished = resolve })
try {
const notifications = this.notifications
if (notifications && notifications.state !== 'disconnected') {
const command = notifications.stopCommand()
notifications.stop()
if (this.state.phase === 'connected') { try { await communicator.writeRawToRight(command) } catch {} }
}
if (this.state.phase === 'connected' || this.state.phase === 'connecting' || this.state.phase === 'retrying')
this.update('disconnecting', 'Disconnecting…')
await communicator.disconnect().catch(error => this.logBluetooth(`Disconnect: ${String(error)}`))
} finally {
await this.closeCommunicator()
this.ensurePreviewDisplay()
this.update(reason ? 'error' : 'disconnected', reason ?? 'Preview only')
this.stopping = null
finished()
}
}
private async closeCommunicator(): Promise<void> {
const communicator = this.communicator
if (!communicator) return
for (const off of this.sessionOffs.splice(0)) off()
this.notifications = null
this.communicator = null
this.glance.reset()
await communicator.close().catch(error => this.logBluetooth(`Close: ${String(error)}`))
}
private maybePlayWelcomeSound(state: SessionState): void {
const firstNewFrame = state.frames > this.acknowledgedFrames
this.acknowledgedFrames = state.frames
@@ -582,17 +848,18 @@ export class IosPreviewController {
startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() }
private async prepareVoiceCapture(): Promise<boolean> {
if (this.glassesLocked) return false
const usePhoneMic = this.session?.state.phase !== 'connected'
const usePhoneMic = this.state.phase !== 'connected'
if (usePhoneMic && !this.active) return false
const ready = await iosVoiceInput.prepare(this.active, usePhoneMic)
if (!ready && this.active) this.onError(iosVoiceInput.statusText)
return ready && !this.glassesLocked && (this.session?.state.phase === 'connected' || this.active)
return ready && !this.glassesLocked && (this.state.phase === 'connected' || this.active)
}
private async startVoiceCapture(endpointing = false): Promise<void> {
if (this.glassesLocked) return
const log = (message: string) => this.logBluetooth(message)
if (this.session?.state.phase === 'connected') {
await iosVoiceInput.startGlassesCapture(this.session, log, endpointing)
const communicator = this.communicator
if (communicator && this.state.phase === 'connected') {
await iosVoiceInput.startGlassesCapture(communicator, log, endpointing)
} else if (this.active) {
// Recheck microphone permission if the glasses disconnected after prepare.
if (!await iosVoiceInput.prepare(this.active, true) || !this.active || this.glassesLocked) return
-8
View File
@@ -1,8 +0,0 @@
/** Platform-owned snapshot of the same composite as the submitted pixels. */
export type TextureFrame = { readonly native: object }
export type TexturePlan = { resourceCommands: Uint8Array[]; payload: Uint8Array; nextFid: number; usedBytes: number }
/** Sessions own residency; frame snapshots may be coalesced without changing it. */
export interface SessionTexturePlanner {
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null
reset(): void
}
-15
View File
@@ -1,15 +0,0 @@
import { bytes, concat, integer, readBytes, readInteger, type ProtocolMessage } from './ble-protocol'
// Matches BleProtocol.buildSetWearDetection/buildFaceclawWakeControl on Android.
export const enableWearDetection = (magic: number): Uint8Array => concat(
integer(1, 1), integer(2, magic), bytes(3, bytes(5, integer(1, 1))))
export const queryWearState = (): Uint8Array => concat(
integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, 7, 0, 0])))
export function decodeWearState(message: ProtocolMessage): boolean | null {
if (message.sid !== 0x10 || message.command !== 3) return null
const event = readBytes(message.payload, 5)
if (!event || readInteger(event, 1, -1) !== 1) return null
const state = readInteger(event, 2, -1)
return state === 0 ? false : state === 1 ? true : null
}
-5
View File
@@ -1,5 +1,4 @@
import { fromData, toData } from '../native/kotlin-data'
import type { TextureFrame } from '../g2/texture-planner'
import type { SurfaceConfiguration, SurfaceRect } from './surface-compositor'
export type { SurfaceConfiguration, SurfaceRect } from './surface-compositor'
declare const FaceclawKitIosSurfaceCompositor: any
@@ -35,8 +34,4 @@ export class SurfaceCompositor {
}
setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) }
composite(): Uint8Array { return fromData(this.native.composite()) }
compositeFrame(): { pixels: Uint8Array; textures: TextureFrame } {
const native = this.native.compositeFrame()
return { pixels: fromData(native.pixels), textures: { native } }
}
}
-2
View File
@@ -1,6 +1,5 @@
import { presentationRecords, readPresentation, paintPresentation, type Selection } from "./presentation-wire"
/** Platform-independent 8bpp surface composition for the local display. */
import type { TextureFrame } from '../g2/texture-planner'
export type SurfaceRect = { x: number; y: number; width: number; height: number }
export type SurfaceConfiguration = SurfaceRect & {
zOrder: number
@@ -55,7 +54,6 @@ export class SurfaceCompositor {
surface.pixels.set(pixels.subarray(source, source + right - left), y * surface.width + left)
}
}
compositeFrame(): { pixels: Uint8Array; textures?: TextureFrame } { return { pixels: this.composite() } }
composite(): Uint8Array {
const output = new Uint8Array(this.width * this.height)
if (this.blanked) return output
+14 -2
View File
@@ -1,3 +1,15 @@
import { iosBleTraffic } from '../g2/ble-traffic-counters'
export type { BleTrafficSample } from './ble-traffic'
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } { return iosBleTraffic.sample() }
declare const FaceclawKitIosBleTraffic: any
// The Kotlin counters reset on every sample; accumulate here so callers see
// running totals like FaceclawBleManager.sampleOutboundTraffic on Android.
// CoreBluetooth has no per-message boundary; messages counts write calls.
const totals = { messages: 0, bytes: 0, frames: 0 }
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } {
const sample = FaceclawKitIosBleTraffic.shared.sample()
const written = Number(sample.getIndex(0)), frames = Number(sample.getIndex(2))
totals.bytes += written
totals.frames += frames
if (written > 0) totals.messages++
return { ...totals }
}
+79 -58
View File
@@ -1,68 +1,89 @@
import { StockConnection, StockTimeout } from '../g2/stock-connection'
import * as p from '../g2/ble-protocol'
import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { DeviceInfo, DeviceInfoState } from './device-info-probe'
export type { DeviceInfo, DeviceInfoState } from './device-info-probe'
declare const FaceclawKitIosDeviceInfoProbe: any, FaceclawKitFaceclawDeviceInfoProbeListener: any
/**
* iOS twin of the Android DeviceInfoProbe wrapper: the shared Kotlin DeviceInfoProbeFlow
* runs over a CoreBluetooth central (FaceclawKitIosDeviceInfoProbe); TypeScript only maps
* the stored MAC addresses to peripheral identifiers first. Callbacks arrive on the main
* queue and are fanned out asynchronously like the Android wrapper does.
*/
export class DeviceInfoProbe {
private logs = new Set<(line: string) => void>()
private states = new Set<(state: DeviceInfoState, detail: string) => void>()
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) })
constructor(private right: string, private left = '') {}
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: DeviceInfoState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
async run(): Promise<DeviceInfo> {
const snapshots = new Map<string, DeviceInfo>()
const parse = (message: p.ProtocolMessage): DeviceInfo => {
const values = p.readBytes(message.payload, 4)
return { leftVersion: values ? p.readString(values, 5) : '', rightVersion: values ? p.readString(values, 6) : '', extension: p.readString(message.payload, 100) }
}
const hasVersion = (info: DeviceInfo) => !!(info.leftVersion || info.rightVersion || info.extension)
// Some stock builds push settings using their own magic instead of the
// query's magic. Keep a valid snapshot as the Android probe does.
this.link.onMessage((role, message) => {
if (message.sid === p.SID.settings) { const info = parse(message); if (hasVersion(info)) snapshots.set(role, info) }
private probe: any = null
private listenerProxy: object | null = null
private readonly logListeners = new Set<(line: string) => void>()
private readonly stateListeners = new Set<(state: DeviceInfoState, detail: string) => void>()
private settled = false
private cancelled = false
private resolveFn: ((info: DeviceInfo) => void) | null = null
private rejectFn: ((error: Error) => void) | null = null
constructor(private readonly rightAddress: string, private readonly leftAddress = '') {}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener)
return () => this.logListeners.delete(listener)
}
onStateChange(listener: (state: DeviceInfoState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
run(): Promise<DeviceInfo> {
return new Promise<DeviceInfo>((resolve, reject) => {
this.resolveFn = resolve
this.rejectFn = reject
void this.start()
})
}
private async start(): Promise<void> {
let identifiers: Record<string, string>
try {
await this.link.resolve({ right: this.right, left: this.left, ring: '' })
for (const role of ['right', ...(this.left ? ['left'] : [])]) {
await this.bringUp(role)
}
let lastError: unknown
for (const role of ['right', ...(this.left ? ['left'] : [])]) {
for (const fn of this.states) fn('querying', role)
try {
if (!this.link.isConnected(role)) await this.bringUp(role)
await this.link.request(role, p.SID.launch, p.prelude, 3500, 156)
for (let attempt = 0; attempt < 2; attempt++) {
try {
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
const info = parse(message)
if (hasVersion(info)) return info
} catch (error) { this.link.check(); lastError = error }
if (snapshots.has(role)) return snapshots.get(role)!
}
} catch (error) { this.link.check(); lastError = error }
}
throw lastError ?? new Error("Connected, but couldn't read a firmware version.")
} finally { this.close() }
}
private async bringUp(role: string): Promise<void> {
for (let attempt = 0; ; attempt++) {
try {
for (const fn of this.states) fn('connecting', role)
await this.link.connect(role)
for (const fn of this.states) fn('authenticating', role)
// Version reads remain available on older stock builds without an auth ACK.
try { await this.link.authenticate(role) } catch (error) { if (!(error instanceof StockTimeout)) throw error }
return
} catch (error) {
this.link.check(); this.link.disconnect(role)
if (attempt === 2) throw error
await new Promise(resolve => setTimeout(resolve, 1500)); this.link.check()
}
this.emit(this.stateListeners, 'connecting', 'right')
identifiers = await iosBluetooth().resolveDevices({ right: this.rightAddress, left: this.leftAddress, ring: '' })
} catch (error) {
this.settle(error instanceof Error ? error : new Error(String(error)), null)
return
}
if (this.cancelled) { this.settle(new Error('Cancelled.'), null); return }
this.probe = FaceclawKitIosDeviceInfoProbe.alloc().initWithRightAddressLeftAddress(identifiers.right ?? '', identifiers.left ?? '')
this.listenerProxy = kotlinListener(FaceclawKitFaceclawDeviceInfoProbeListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onStateStateDetail: (state: string, detail: string) =>
this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? '')),
onResultLeftVersionRightVersionExtension: (leftVersion: string, rightVersion: string, extension: string) =>
this.settle(null, { leftVersion: String(leftVersion ?? ''), rightVersion: String(rightVersion ?? ''), extension: String(extension ?? '') }),
onErrorMessage: (message: string) => this.settle(new Error(String(message ?? '')), null),
})
this.probe.setListenerListener(this.listenerProxy)
this.probe.start()
}
cancel(): void {
this.cancelled = true
try { this.probe?.cancel() } catch { /* ignore */ }
}
close(): void {
this.cancelled = true
try { this.probe?.close() } catch { /* ignore */ }
}
private settle(error: Error | null, info: DeviceInfo | null): void {
if (this.settled) return
this.settled = true
this.close()
if (error) this.rejectFn?.(error)
else if (info) this.resolveFn?.(info)
}
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)
}
cancel(): void { this.link.close() }
close(): void { this.link.close() }
}
+677
View File
@@ -0,0 +1,677 @@
import type { RingInput } from "../g2/ring-input";
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { type FirmwareInfo } from "../g2/firmware-compat";
import { type CompassEvent } from "./compass-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 FaceclawKitIosProtocolPlatform: any;
declare const FaceclawKitFaceclawBleCommunicatorListener: any;
declare const FaceclawKitIosAncsListener: any;
declare const FaceclawKitIosAudioPacketListener: any;
declare const FaceclawKitFaceclawCompassListener: any;
export type CommunicatorPhase =
| "disconnected"
| "connecting"
| "connected"
| "charging"
| "retrying"
| "unpaired"
| "disconnecting";
export type CommunicatorState = {
phase: CommunicatorPhase;
status: string;
};
export type HeadsetBatteryState = {
battery: number;
chargingStatus: number;
ringBattery?: number;
ringChargingStatus?: number;
};
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));
}
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;
onAudioPacketDataArmArrivalMs(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,
});
}
}
/**
* 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 {
private readonly communicator: any;
private readonly listenerProxy: SessionListener;
private ancsProxy: AncsListener | null = null;
private nativeCallQueue: Promise<void> = Promise.resolve();
private queuedNativeCalls = 0;
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 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 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;
constructor(addresses: { right: string; left: string; ring?: string }) {
this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress(
String(addresses.right ?? "").toUpperCase(),
String(addresses.left ?? "").toUpperCase(),
String(addresses.ring ?? "").toUpperCase(),
);
this.listenerProxy = SessionListener.new() as SessionListener;
this.listenerProxy.bridge = this;
this.communicator.setListenerListener(this.listenerProxy);
}
// ----- 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 */
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);
}
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);
}
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(() => { this.communicator.start(); });
}
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: auto (ambient sensor) or an explicit 0-100 level. */
async setBrightness(autoAdjust: boolean, level: number): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.setBrightnessAutoAdjustBrightnessLevel(Boolean(autoAdjust), Math.round(level)));
}
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 null if none yet. */
getCompositePreview(green = false): 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);
}
/** 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(() => {
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,
);
});
}
async removeSurface(id: string): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.removeSurfaceId(id); });
}
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(() => { this.communicator.setSurfaceVisibleIdVisible(id, Boolean(visible)); });
}
async setScreenBlanked(blanked: boolean): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); });
}
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(() => {
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.setAncsListeners(false);
await this.enqueueNativeCall(() => 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); };
}
/**
* 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;
}
}
}
+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)
}
}
+87 -77
View File
@@ -1,88 +1,98 @@
import { StockConnection } from '../g2/stock-connection'
import { createFlashPrompt, flashPromptSelection } from '../g2/flash-prompt-protocol'
import * as p from '../g2/ble-protocol'
import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
declare const FaceclawKitIosFlashPrompt: any, FaceclawKitFaceclawFlashPromptListener: any
function normalizePercent(value: number): number | null {
const n = Number(value)
return Number.isFinite(n) && n >= 0 ? n : null
}
/**
* iOS twin of the Android FlashPromptCommunicator wrapper over the shared Kotlin
* FlashPromptFlow (FaceclawKitIosFlashPrompt): connects and authenticates both arms, shows
* the pre-flash Yes/No page on the glasses (unless skipPrompt) and reads each arm's battery.
*/
export class FlashPromptCommunicator {
private logs = new Set<(line: string) => void>()
private states = new Set<(state: FlashPromptState, detail: string) => void>()
private results = new Set<(approved: boolean) => void>()
private batteries = new Set<(battery: FlashPromptBattery) => void>()
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) })
private communicator: any = null
private listenerProxy: object | null = null
private readonly logListeners = new Set<(line: string) => void>()
private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>()
private readonly resultListeners = new Set<(approved: boolean) => void>()
private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>()
private started = false
private closed = false
private started = false
private heartbeat: ReturnType<typeof setInterval> | null = null
private stopSelection: (() => void) | null = null
constructor(private addresses: { right: string; left: string }, private warning: string, private options?: { skipPrompt?: boolean }) {}
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: FlashPromptState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
onResult(fn: (approved: boolean) => void): () => void { this.results.add(fn); return () => this.results.delete(fn) }
onBattery(fn: (battery: FlashPromptBattery) => void): () => void { this.batteries.add(fn); return () => this.batteries.delete(fn) }
private state(state: FlashPromptState, detail = ''): void { for (const fn of this.states) fn(state, detail) }
start(): void { if (!this.started) { this.started = true; void this.run() } }
constructor(
private readonly addresses: { right: string; left: string },
private readonly warningText: string,
private readonly options?: { skipPrompt?: boolean },
) {}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener)
return () => this.logListeners.delete(listener)
}
onStateChange(listener: (state: FlashPromptState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
onBattery(listener: (battery: FlashPromptBattery) => void): () => void {
this.batteryListeners.add(listener)
return () => this.batteryListeners.delete(listener)
}
onResult(listener: (approved: boolean) => void): () => void {
this.resultListeners.add(listener)
return () => this.resultListeners.delete(listener)
}
start(): void {
if (this.started) return
this.started = true
void this.run()
}
private async run(): Promise<void> {
let identifiers: Record<string, string>
try {
this.state('connecting')
await this.link.resolve({ ...this.addresses, ring: '' })
// Complete pairing for one peripheral before starting the other.
for (const role of ['right', 'left']) {
await this.link.connect(role)
if (role === 'right') this.state('connected')
await this.link.authenticate(role)
}
await this.link.request('right', p.SID.launch, p.prelude, 3500, 156)
if (!this.options?.skipPrompt) {
// Listen before creating the page: an immediate tap may precede its ACK.
const selection = this.selection()
// Attach a rejection handler immediately, even while create-page is pending.
void selection.catch(() => {})
for (let attempt = 0; ; attempt++) {
try { await this.link.request('right', p.SID.hub, magic => createFlashPrompt(magic, this.warning)); break }
catch (error) { this.link.check(); if (attempt) throw error }
}
this.state('prompting')
this.heartbeat = setInterval(() => {
void this.link.write('right', p.SID.hub, p.heartbeat(this.link.nextMagic())).catch(error => {
if (!this.closed) this.state('error', String(error.message ?? error))
this.close()
})
}, 4000)
const approved = await selection
this.stopHeartbeat()
// Stock shutdown returns the glasses to their normal UI before OTA.
await this.link.write('right', p.SID.hub, p.concat(p.integer(1, 9), p.integer(2, this.link.nextMagic()), p.bytes(11, p.integer(1, 0))))
if (!approved) { for (const fn of this.results) fn(false); return }
}
this.state('battery')
const battery: FlashPromptBattery = { right: null, left: null }
for (const role of ['right', 'left'] as const) {
for (let attempt = 0; attempt < 2; attempt++) {
try {
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
const values = p.readBytes(message.payload, 4), value = values ? p.readInteger(values, 12, -1) : -1
if (value >= 0 && value <= 100) { battery[role] = value; break }
} catch (error) { this.link.check() }
}
}
for (const fn of this.batteries) fn(battery)
for (const fn of this.results) fn(true)
} catch (error) { if (!this.closed) this.state('error', String((error as Error).message ?? error)) }
finally { this.close() }
this.emit(this.stateListeners, 'connecting', '')
identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
} catch (error) {
if (!this.closed) this.emit(this.stateListeners, 'error', String((error as Error).message ?? error))
return
}
if (this.closed) return
this.communicator = FaceclawKitIosFlashPrompt.alloc().initWithRightAddressLeftAddressWarningTextSkipPrompt(
identifiers.right ?? '', identifiers.left ?? '', this.warningText, Boolean(this.options?.skipPrompt))
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFlashPromptListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onStateStateDetail: (state: string, detail: string) =>
this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? '')),
onBatteryRightPercentLeftPercent: (rightPercent: number, leftPercent: number) =>
this.emit(this.batteryListeners, { right: normalizePercent(rightPercent), left: normalizePercent(leftPercent) }),
onResultApproved: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)),
})
this.communicator.setListenerListener(this.listenerProxy)
this.communicator.start()
}
cancel(): void {
this.closed = true
try { this.communicator?.cancel() } catch { /* ignore */ }
}
private selection(): Promise<boolean> {
return new Promise((resolve, reject) => {
const cleanup = () => { clearTimeout(timer); off(); this.stopSelection = null }
const off = this.link.onMessage((role, message) => {
const result = role === 'right' ? flashPromptSelection(message) : null
if (result !== null) { cleanup(); resolve(result) }
})
const timer = setTimeout(() => { cleanup(); reject(new Error('No response from the glasses.')) }, 120_000)
this.stopSelection = () => { cleanup(); reject(new Error('Cancelled.')) }
})
close(): void {
this.closed = true
try { this.communicator?.close() } catch { /* ignore */ }
}
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)
}
private stopHeartbeat(): void { if (this.heartbeat) clearInterval(this.heartbeat); this.heartbeat = null }
cancel(): void { this.close() }
close(): void { this.closed = true; this.stopHeartbeat(); this.stopSelection?.(); this.link.close() }
}
+56 -73
View File
@@ -1,75 +1,80 @@
import { ApplicationSettings } from '@nativescript/core'
import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity'
import { hexToBytes } from '../util/hex-util'
import { kotlinListener } from './kotlin-listener.ios'
declare const FaceclawBluetooth: any
type NativeEvent = { kind: string; id?: number; error?: string; details?: ConnectionDetails; state?: number;
identifier?: string; characteristic?: string; data?: string; message?: string } & Partial<IosAdvertisement>
export type ConnectionDetails = { characteristics: string[]; maxWrite: number }
export type BluetoothEvent = NativeEvent | { kind: 'device'; device: IosDevice }
type Pending = { resolve: (details: ConnectionDetails) => void; reject: (error: Error) => void; timer: ReturnType<typeof setTimeout> }
declare const FaceclawKitIosBleCentral: any, FaceclawKitIosProtocolPlatform: any, FaceclawKitIosBleScanListener: any
/** One central manager per app, with asynchronous main-queue GATT operations. */
/** CBManagerState raw values reported by the Kotlin central. */
export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_POWERED_OFF = 4, BLE_STATE_POWERED_ON = 5
export type BluetoothEvent =
| { kind: 'state'; state: number; authorization: number }
| { kind: 'advertisement'; identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean }
| { kind: 'device'; device: IosDevice }
| { kind: 'scan-started' }
| { kind: 'scan-stopped' }
/**
* Scanning and MAC-address-to-identifier resolution over the shared Kotlin CoreBluetooth
* central (FaceclawKitIosBleCentral). GATT sessions no longer pass through here: the
* session and stock-firmware flows own their own Kotlin centrals. What stays in TypeScript
* is the Even classification of advertisements and the persisted identifier map, which the
* Android address model (MACs) needs on iOS.
*/
export class IosBluetooth {
private readonly native = FaceclawBluetooth.new()
private readonly native = FaceclawKitIosBleCentral.alloc().initWithPlatform(FaceclawKitIosProtocolPlatform.shared)
private readonly listeners = new Set<(event: BluetoothEvent) => void>()
private readonly pending = new Map<number, Pending>()
private readonly advertisements = new Map<string, IosAdvertisement>()
readonly devices = new Map<string, IosDevice>()
private requestId = 0
private scanTimer: ReturnType<typeof setTimeout> | null = null
private scanGeneration = 0
private readonly scanListener: object
state = 0
authorization = 0
scanning = false
constructor() {
this.native.eventHandler = (json: string) => {
try { this.receive(JSON.parse(json)) } catch (error) { console.error(`[ios-ble] ${error}`) }
}
this.scanListener = kotlinListener(FaceclawKitIosBleScanListener, {
onBluetoothStateStateAuthorization: (state: number, authorization: number) => {
this.state = Number(state); this.authorization = Number(authorization)
this.emit({ kind: 'state', state: this.state, authorization: this.authorization })
},
onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean) => {
// The Kotlin central already merges split advertisements and bounds its cache.
const raw: IosAdvertisement = { identifier: String(identifier), name: String(name ?? ''), manufacturerData: String(manufacturerData ?? ''), rssi: Number(rssi), connectable: connectable !== false }
this.emit({ kind: 'advertisement', ...raw })
const device = identifyIosPeripheral(raw)
if (device) {
this.devices.set(device.address, device)
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
this.emit({ kind: 'device', device })
}
},
})
this.native.setScanListenerListener(this.scanListener)
}
onEvent(listener: (event: BluetoothEvent) => void): () => void {
this.listeners.add(listener); return () => { this.listeners.delete(listener) }
}
private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) }
private receive(event: NativeEvent): void {
if (event.kind === 'completion') {
const pending = this.pending.get(event.id!)
if (pending) {
this.pending.delete(event.id!); clearTimeout(pending.timer)
if (event.error) pending.reject(new Error(event.error)); else pending.resolve(event.details!)
}
return
}
if (event.kind === 'state') this.state = event.state!
if (event.kind === 'advertisement') {
const previous = this.advertisements.get(event.identifier!)
const raw: IosAdvertisement = { identifier: event.identifier!, name: event.name || previous?.name || '',
manufacturerData: event.manufacturerData || previous?.manufacturerData || '', rssi: event.rssi!, connectable: event.connectable !== false }
// Bound the cache even in a crowded radio environment.
if (this.advertisements.size >= 512 && !this.advertisements.has(raw.identifier)) this.advertisements.delete(this.advertisements.keys().next().value!)
this.advertisements.set(raw.identifier, raw)
const device = identifyIosPeripheral(raw)
if (device) {
this.devices.set(device.address, device)
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
this.emit({ kind: 'device', device })
}
return
}
this.emit(event)
/** Creates the central (first use shows the Bluetooth permission prompt) without blocking the JS thread. */
private ensureCentral(): void {
// A zero timeout returns immediately after creating the central; the state arrives
// through the scan listener on the main queue.
this.native.waitForPoweredOnTimeoutMs(0)
}
ensureReady(): Promise<void> {
if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve()
return new Promise((resolve, reject) => {
const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() }
const off = this.onEvent(event => {
if (event.kind !== 'state') return
const state = (event as NativeEvent).state
if (state === 5) finish()
else if (state === 2) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
else if (state === 3) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
else if (state === 4) finish(new Error('Turn on Bluetooth, then try again.'))
if (event.state === BLE_STATE_POWERED_ON) finish()
else if (event.state === BLE_STATE_UNSUPPORTED) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
else if (event.state === BLE_STATE_UNAUTHORIZED) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
else if (event.state === BLE_STATE_POWERED_OFF) finish(new Error('Turn on Bluetooth, then try again.'))
})
const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000)
this.native.initializeBluetooth()
this.ensureCentral()
})
}
async startScan(durationMs = 12_000): Promise<void> {
@@ -77,17 +82,18 @@ export class IosBluetooth {
const generation = this.scanGeneration
await this.ensureReady()
if (generation !== this.scanGeneration) throw new Error('Bluetooth scan cancelled')
this.scanning = true; this.native.scan(true)
this.scanning = true; this.native.startScan()
this.scanTimer = setTimeout(() => this.stopScan(), durationMs)
this.emit({ kind: 'scan-started' })
}
stopScan(): void {
++this.scanGeneration
if (this.scanTimer) clearTimeout(this.scanTimer)
this.scanTimer = null; this.native.scan(false)
this.scanTimer = null
const wasScanning = this.scanning; this.scanning = false
if (wasScanning) this.emit({ kind: 'scan-stopped' })
if (wasScanning) { this.native.stopScan(); this.emit({ kind: 'scan-stopped' }) }
}
/** Peripheral identifiers for the stored MAC addresses, scanning for any that are not remembered. */
async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise<Record<string, string>> {
const generation = this.scanGeneration
await this.ensureReady()
@@ -109,7 +115,7 @@ export class IosBluetooth {
await new Promise<void>((resolve, reject) => {
const off = this.onEvent(event => {
if (event.kind === 'device') {
const device = (event as { device: IosDevice }).device
const device = event.device
for (const role of ['left', 'right', 'ring'] as const) {
if (addresses[role] === device.address && device.role === role) result[role] = device.identifier
}
@@ -123,29 +129,6 @@ export class IosBluetooth {
})
return result
}
private operation(identifier: string, call: (id: number) => void, timeoutMs = 10_000, disconnectOnTimeout = true): Promise<ConnectionDetails> {
const id = ++this.requestId
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
this.pending.delete(id); if (disconnectOnTimeout) this.native.disconnect(identifier)
reject(new Error('Bluetooth operation timed out. Wake the device and try connecting again.'))
}, timeoutMs)
this.pending.set(id, { resolve, reject, timer })
try { call(id) } catch (error) { clearTimeout(timer); this.pending.delete(id); reject(error) }
})
}
connect(identifier: string, requiresANCS = false): Promise<ConnectionDetails> { return this.operation(identifier, id => this.native.connectRequiresANCSRequestId(identifier, requiresANCS, id), 20_000) }
async subscribe(identifier: string, characteristic: string): Promise<void> {
// R1 exposes two notification channels; one may never complete its CCCD
// request. Keep the link alive so the other channel can still be tried.
await this.operation(identifier, id => this.native.subscribeCharacteristicRequestId(identifier, characteristic, id), 30_000, false)
}
async write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void> {
const copy = new Uint8Array(bytes)
const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength)
await this.operation(identifier, id => this.native.writeCharacteristicDataRequestId(identifier, characteristic, data, id))
}
disconnect(identifier: string): void { this.native.disconnect(identifier) }
forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) }
}
let instance: IosBluetooth | null = null
+10
View File
@@ -0,0 +1,10 @@
/**
* Implements a FaceclawKit (Kotlin) listener protocol with plain JavaScript methods.
* NativeScript maps the method names to the protocol's selectors by camel-casing the
* selector parts (`onLog:` -> `onLogLine`, `onState:detail:` -> `onStateStateDetail`, ...).
* Keep a reference to the returned object for as long as the Kotlin side may call it.
*/
export function kotlinListener<T extends object>(protocol: unknown, methods: T): T {
const NativeListener = (NSObject as any).extend(methods, { protocols: [protocol] })
return NativeListener.new() as T
}
+2 -28
View File
@@ -1,14 +1,9 @@
import { CFW_MSG_BOUNDING_BOX, CFW_MSG_CREATE_SURFACE, CFW_MSG_PRESENT, CFW_MSG_UPLOAD_RESOURCE } from "../g2/cfw-message-type";
import { GrayImage } from "../graphics/image"
import { encodeShellScene } from "../graphics/shell-scene"
import { decodeImageBytes } from './image-bytes.ios'
import * as protocol from '../g2/ble-protocol.ios'
import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios'
import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios'
import { lvglMetrics } from './lvgl-font.ios'
import { textureImageId } from './texture-atlas.ios'
import { IosTexturePlanner } from './texture-planner.ios'
/** Development-only checks of real NativeScript selectors and bulk data crossings. */
export function runKotlinProtocolSmokeTest(): void {
@@ -24,11 +19,6 @@ export function runKotlinProtocolSmokeTest(): void {
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2))))
const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 })
assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event')
const transport = new CfwTransport()
const packets = transport.encode(new Uint8Array(128).fill(9), 255, 3, 185)
assert(packets.length > 0 && packets.every(p => p.length <= 185), 'compression')
transport.reset()
assert(parseCfwAcks(new Uint8Array()) === null, 'invalid ACK')
const c = new SurfaceCompositor(3, 2)
c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' })
c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 })
@@ -37,28 +27,12 @@ export function runKotlinProtocolSmokeTest(): void {
c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255]))
assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition')
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
const textures = new IosTexturePlanner(), textured = new SurfaceCompositor(4, 2)
textured.configureSurface('icon', { x: 0, y: 0, width: 2, height: 2, zOrder: 0, transparency: 'opaque' })
const white = new Uint8Array(4).fill(255)
const icon = textureImageId('protocol-smoke-white', 2, 2, white)
const draw = new DataView(new ArrayBuffer(9)); draw.setUint8(0, 1); draw.setUint32(1, icon, true)
textured.submitSurfaceFrame('icon', white, { x: 0, y: 0, width: 2, height: 2 }, draw.buffer)
const snapshot = textured.compositeFrame(), packed = protocol.packGray4(snapshot.pixels, 4, 2)
const cold = textures.plan(null, packed, snapshot.textures, 1)
assert(!!cold && cold.resourceCommands.some(command => command[0] === CFW_MSG_UPLOAD_RESOURCE) && cold.payload[0] === CFW_MSG_PRESENT, 'texture upload and draw')
assert(textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length === 0, 'texture reuse')
textures.reset()
assert((textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length ?? 0) > 0, 'texture reset')
const selectedSurface = new GrayImage(8, 4, 1)
selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2)
const selectedPreview = new SurfaceCompositor(8, 4)
selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }]))
const selectedSnapshot = selectedPreview.compositeFrame()
assert(selectedSnapshot.pixels[10] === 240 && selectedSnapshot.pixels[9] === 0, 'menu selection depth and bridge')
const selectedPlan = new IosTexturePlanner().plan(null, protocol.packGray4(selectedSnapshot.pixels, 8, 4), selectedSnapshot.textures, 1)
assert(!!selectedPlan && selectedPlan.payload[0] === CFW_MSG_PRESENT && selectedPlan.resourceCommands.some(c => c[0] === CFW_MSG_CREATE_SURFACE), 'menu resource planning')
const changed = new Uint8Array(16); changed[0] = 255
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === CFW_MSG_BOUNDING_BOX, 'image update')
const selectedPixels = selectedPreview.composite()
assert(selectedPixels[10] === 240 && selectedPixels[9] === 0, 'menu selection depth and bridge')
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0,
payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
+141
View File
@@ -0,0 +1,141 @@
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { SurfaceCompositor } from "../graphics/surface-compositor";
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image";
import { previewPixels } from "./ios-graphics";
import { toData } from "./kotlin-data";
import type { FaceclawCommunicatorBridge, SurfaceOptions } from "./faceclaw-communicator.ios";
declare const FaceclawKitIosProtocol: any;
/** The display subset of the communicator bridge; see the Android twin. */
export type DisplayTarget = Pick<
FaceclawCommunicatorBridge,
| "configureCompositorScreen"
| "configureSurface"
| "removeSurface"
| "setSurfaceVisible"
| "setUnderlayDim"
| "setScreenBlanked"
| "submitSurfaceFrame"
| "submitShellScene"
| "waitForFrameFinished"
| "getCompositePreview"
| "saveScreenshot"
| "startScreenRecording"
| "recordScreenFrame"
| "stopScreenRecording"
>;
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)}`;
}
/**
* Headless display target for iOS while no glasses session exists: the same
* shared Kotlin SurfaceCompositor a live session composes with, so the phone
* mirror, apps and shell render identically before connecting. Frames end
* at the compositor; there is no transmit backpressure to wait on.
*/
export class PreviewDisplayTarget implements DisplayTarget {
private compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT);
private onFrameComposited: (() => void) | null = null;
private released = false;
/** Receive a callback per applied frame, the stand-in for the connected path's frame-finished callback. */
activate(onFrameComposited: () => void): void {
this.onFrameComposited = onFrameComposited;
}
release(): void {
this.released = true;
this.onFrameComposited = null;
}
private composited(): void {
if (!this.released) this.onFrameComposited?.();
}
async configureCompositorScreen(width: number, height: number): Promise<void> {
if (Math.round(width) !== this.compositor.width || Math.round(height) !== this.compositor.height) {
this.compositor = new SurfaceCompositor(Math.round(width), Math.round(height));
}
}
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
this.compositor.configureSurface(id, {
x: Math.round(options.x), y: Math.round(options.y), width: Math.round(options.width), height: Math.round(options.height),
zOrder: Math.round(options.zOrder), transparency: options.transparency,
});
}
async removeSurface(id: string): Promise<void> {
this.compositor.removeSurface(id);
this.composited();
}
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
this.compositor.setSurfaceVisible(id, Boolean(visible));
this.composited();
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
this.compositor.setUnderlayDim(Math.round(belowZOrder), Math.max(0, Math.min(1, factor)));
this.composited();
}
async setScreenBlanked(blanked: boolean): Promise<void> {
this.compositor.setScreenBlanked(Boolean(blanked));
this.composited();
}
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> {
this.compositor.submitSurfaceFrame(surfaceId, pixels8bpp,
{ x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }, glyphs);
this.composited();
}
async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise<void> {
this.compositor.setShellScene(bytes);
this.composited();
}
/** Nothing transmits: every frame is finished as soon as it is composited. */
waitForFrameFinished(frameId: number, _timeoutMs: number): Promise<string | null> {
return Promise.resolve(frameId > 0 ? "composited" : null);
}
/** The current composite as gray pixels (row-major, compositor size). */
composite(): Uint8Array {
return this.compositor.composite();
}
getCompositePreview(green = false): ImageSource | null {
return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green);
}
/** Save the current composite as a 4-bit grayscale PNG under Documents; cropping is not supported on iOS. */
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
if (crop) return "";
const png = FaceclawKitIosProtocol.new().pngDataWidthHeight(toData(this.compositor.composite()), this.compositor.width, this.compositor.height);
if (!png) return "";
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
File.fromPath(target).writeSync(png);
return target;
}
startScreenRecording(): void {}
recordScreenFrame(): void {}
stopScreenRecording(): string { return ""; }
}
+10 -8
View File
@@ -1,14 +1,16 @@
import { createListenerPool, type InterfaceAddress } from '../remote/listeners';
declare const FaceclawRemoteInput: any;
declare const FaceclawKitIosRemoteInput: any;
/** The shared Kotlin RemoteInputSession over POSIX sockets; request notifications arrive on the main queue. */
export function remoteNative(onRequestReady: () => void) {
return createListenerPool(() => {
const native = FaceclawRemoteInput.alloc().init();
native.setRequestListener(onRequestReady);
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); },
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeResponse(id, response) };
const native = FaceclawKitIosRemoteInput.new();
native.setRequestListenerListener(onRequestReady);
return { start: (port, address) => String(native.startAddressPortAddress(port, address)), stop: () => { native.setRequestListenerListener(null); native.stop(); },
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeIdValue(id, response) };
});
}
export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(FaceclawRemoteInput.interfaces())); }
export function randomTokenSecret(): string { return String(FaceclawRemoteInput.shared().randomSecret()); }
export function tokenHash(value: string): string { return String(FaceclawRemoteInput.shared().tokenDigest(value)); }
function shared(): any { return FaceclawKitIosRemoteInput.companion.shared; }
export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(shared().interfaces())); }
export function randomTokenSecret(): string { return String(shared().randomSecret()); }
export function tokenHash(value: string): string { return String(shared().tokenDigestValue(value)); }
export function copyRemoteToken(value: string): void { UIPasteboard.generalPasteboard.string = value; }
+12 -21
View File
@@ -1,28 +1,19 @@
import { type SseListener } from './sse-types';
import { withUserAgent } from '../util/http';
import { kotlinListener } from './kotlin-listener.ios';
export { type SseListener } from './sse-types';
declare const FaceclawSseRequest: any;
declare const FaceclawKitIosSseRequest: any, FaceclawKitFaceclawSseListener: any;
/** Drain native events on the requesting JS isolate, never on a URLSession thread. */
/** The shared Kotlin SseStream over NSURLSession; the Kotlin side delivers every callback on the main queue, in order. */
export function openSseRequest(url: string, body: string, headers: Record<string, string>, listener: SseListener): { cancel(): void } {
const native = FaceclawSseRequest.alloc().initWithURLBodyHeaders(url, body, JSON.stringify(withUserAgent(headers)));
let ended = false;
const cancel = () => { if (ended) return; ended = true; clearInterval(timer); native.cancel(); };
const timer = setInterval(() => {
try {
for (const event of JSON.parse(native.takeEvents())) {
if (ended) break;
if (event.kind === 'line') listener.onLine(event.text);
else {
cancel();
if (event.kind === 'complete') listener.onComplete();
else if (event.kind === 'http-error') listener.onHttpError(event.code, event.text);
else listener.onFailure(event.text);
}
}
} catch (error) {
cancel(); listener.onFailure(String((error as Error)?.message ?? error));
}
}, 25);
return { cancel };
const once = (deliver: () => void) => { if (ended) return; ended = true; deliver(); };
const nativeListener = kotlinListener(FaceclawKitFaceclawSseListener, {
onLineLine: (line: string) => { if (!ended) listener.onLine(String(line ?? '')); },
onHttpErrorCodeBody: (code: number, errorBody: string) => once(() => listener.onHttpError(Number(code), String(errorBody ?? ''))),
onComplete: () => once(() => listener.onComplete()),
onFailureMessage_: (message: string) => once(() => listener.onFailure(String(message ?? ''))),
});
const native = FaceclawKitIosSseRequest.alloc().initWithUrlBodyHeadersJsonListener(url, body, JSON.stringify(withUserAgent(headers)), nativeListener);
return { cancel: () => { if (ended) return; ended = true; native.cancel(); } };
}
-13
View File
@@ -1,13 +0,0 @@
import { fromData, toData, nativeArray } from './kotlin-data'
import type { SessionTexturePlanner, TextureFrame, TexturePlan } from '../g2/texture-planner'
declare const FaceclawKitIosTexturePlanner: any
export class IosTexturePlanner implements SessionTexturePlanner {
private readonly native = FaceclawKitIosTexturePlanner.new()
reset(): void { this.native.reset() }
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null {
const result = this.native.planPreviousNextFrameFirstId(previous ? toData(previous) : null, toData(next), frame.native, firstId)
return result ? { payload: fromData(result.payload), resourceCommands: nativeArray(result.resourceCommands, fromData),
nextFid: result.nextFid, usedBytes: result.usedBytes } : null
}
}