forked from Blendman974/faceclaw
Refactor iOS version to use a bunch more shared code
This commit is contained in:
1 parent
c3a0ba9f56
commit
4a194e37bb
40 files changed
+1874
-2605
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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()
|
||||
@@ -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)))
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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]))
|
||||
}
|
||||
@@ -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
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user