Refactor iOS version to use a bunch more shared code

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

No files matched your search

+4
View File
@@ -8,6 +8,10 @@ OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -f
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
HEADER_SEARCH_PATHS = $(inherited) "$(SRCROOT)/../../App_Resources/iOS/src/lc3" HEADER_SEARCH_PATHS = $(inherited) "$(SRCROOT)/../../App_Resources/iOS/src/lc3"
// NativeScript's xcconfig merge keeps only the CocoaPods HEADER_SEARCH_PATHS when the
// generated project is rebuilt, so the vendored liblc3 include dir is also passed as a flag
// (OTHER_CFLAGS merges like OTHER_LDFLAGS).
OTHER_CFLAGS = $(inherited) -I"$(SRCROOT)/../../App_Resources/iOS/src/lc3"
// FaceclawKit ships arm64 device and Apple-silicon simulator slices. // FaceclawKit ships arm64 device and Apple-silicon simulator slices.
EXCLUDED_ARCHS[sdk=iphonesimulator*] = x86_64; EXCLUDED_ARCHS[sdk=iphonesimulator*] = x86_64;
-3
View File
@@ -1,17 +1,14 @@
module FaceclawGraphics { module FaceclawGraphics {
header "FaceclawGraphics.h" header "FaceclawGraphics.h"
header "FaceclawFontRenderer.h" header "FaceclawFontRenderer.h"
header "FaceclawBluetooth.h"
header "FaceclawSocket.h" header "FaceclawSocket.h"
header "FaceclawLc3Decoder.h" header "FaceclawLc3Decoder.h"
header "FaceclawSpeech.h" header "FaceclawSpeech.h"
header "FaceclawSseRequest.h"
header "FaceclawLocation.h" header "FaceclawLocation.h"
header "FaceclawCalendar.h" header "FaceclawCalendar.h"
header "FaceclawConfigPort.h" header "FaceclawConfigPort.h"
header "FaceclawEvenHubWebView.h" header "FaceclawEvenHubWebView.h"
header "FaceclawCrypto.h" header "FaceclawCrypto.h"
header "FaceclawRemoteInput.h"
header "FaceclawQrScanner.h" header "FaceclawQrScanner.h"
export * export *
} }
-15
View File
@@ -1,15 +0,0 @@
import { fromData, toData, nativeArray } from '../native/kotlin-data'
declare const FaceclawKitIosProtocol: any
const protocol = FaceclawKitIosProtocol.new()
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, width: number, height: number, frameId: number): Uint8Array | null {
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
const result = protocol.boundingBoxPreviousNextWidthHeightFrameId(toData(previous), toData(next), width, height, frameId)
return result ? fromData(result) : null
}
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
throw new Error('Invalid framebuffer dimensions')
return nativeArray(protocol.fullFrameBandsDataWidthHeightFirstId(toData(packed), width, height, firstId), fromData)
}
-50
View File
@@ -1,50 +0,0 @@
import { CFW_MSG_BOUNDING_BOX } from "./cfw-message-type";
import { concat, rle4 } from './ble-protocol'
/** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in
* units of 4 columns / 2 rows; pixels are top-down packed 4bpp, high nibble first.
* null means identical, incomparable, or a full-screen box: use dedup/mode 6. Compression belongs to the CFW transport.
*/
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array,
width: number, height: number, frameId: number): Uint8Array | null {
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
const stride = width / 2
let minX = stride, maxX = -1, minY = height, maxY = -1
for (let y = 0; y < height; y++) {
const row = y * stride
for (let x = 0; x < stride; x++) {
if (previous[row + x] === next[row + x]) continue
minX = Math.min(minX, x); maxX = Math.max(maxX, x)
minY = Math.min(minY, y); maxY = y
}
}
if (maxY < 0) return null
const left = (minX * 2) & ~3, right = Math.min(width, ((maxX + 1) * 2 + 3) & ~3)
const top = minY & ~1, bottom = Math.min(height, (maxY + 2) & ~1)
const boxWidth = right - left, boxHeight = bottom - top
if (boxWidth === width && boxHeight === height) return null
const regionStride = boxWidth / 2, region = new Uint8Array(regionStride * boxHeight)
for (let y = 0; y < boxHeight; y++) {
const start = (top + y) * stride + left / 2
region.set(next.subarray(start, start + regionStride), y * regionStride)
}
return concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, left / 4, top / 2, boxWidth / 4, boxHeight / 2,
frameId & 255, (frameId >>> 8) & 255]), rle4(region))
}
/** Full repaint as independent 64-row mode-3 bands when RLE exceeds uint16. */
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
throw new Error('Invalid framebuffer dimensions')
const bands: Uint8Array[] = []
let id = firstId
for (let top = 0; top < height; top += 64) {
const rows = Math.min(64, height - top)
bands.push(concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]),
rle4(packed.subarray(top * width / 2, (top + rows) * width / 2))))
id = id >= 0xfffe ? 1 : id + 1
}
return bands
}
-203
View File
@@ -1,203 +0,0 @@
import { type RingInput, decodeRingMetadata } from "./ring-input";
import { type CompassEvent } from '../native/compass-types'
/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */
import { OsEventTypeList } from './events'
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402'
export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402'
export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f']
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9, cfw: 0xf0 } as const
export function concat(...parts: Uint8Array[]): Uint8Array {
const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
let offset = 0; for (const part of parts) { output.set(part, offset); offset += part.length }; return output
}
function varint(value: number): Uint8Array {
const bytes: number[] = []
do { const next = value & 127; value >>>= 7; bytes.push(next | (value ? 128 : 0)) } while (value)
return new Uint8Array(bytes)
}
export const integer = (field: number, value: number): Uint8Array => concat(varint(field * 8), varint(value))
export const bytes = (field: number, value: Uint8Array): Uint8Array => concat(varint(field * 8 + 2), varint(value.length), value)
// Outbound names/text here are fixed ASCII protocol identifiers.
const string = (field: number, value: string): Uint8Array => bytes(field, new Uint8Array(Array.from(value, c => c.charCodeAt(0))))
const wrap = (command: number, magic: number, field: number, body: Uint8Array) => concat(integer(1, command), integer(2, magic), bytes(field, body))
export const authentication = (magic: number) => wrap(4, magic, 3, concat(integer(1, 1), integer(2, 4)))
export const prelude = () => wrap(2, 156, 4, bytes(3, bytes(2, bytes(2, concat(integer(1, 0), integer(2, 0))))))
export const heartbeat = (magic: number) => wrap(12, magic, 14, integer(1, 0))
export const audioControl = (magic: number, enabled: boolean) => wrap(15, magic, 18, integer(1, enabled ? 1 : 0))
export const settingsQuery = (magic: number) => wrap(2, magic, 4, integer(1, 1))
export const shutdown = (magic: number) => wrap(9, magic, 11, integer(1, 1))
export const framebufferLease = (acquire: boolean) => concat(integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, acquire ? 5 : 6, 0, 0])))
export function createLayout(magic: number): Uint8Array {
const geometry = concat(integer(1, 0), integer(2, 0), integer(3, 576), integer(4, 288))
const text = concat(geometry, integer(9, 1), string(10, 'dashboard'), integer(11, 1), string(12, ' '))
return wrap(0, magic, 3, concat(integer(1, 1), bytes(3, text), integer(5, 10000)))
}
export function crc16(data: Uint8Array): number {
let crc = 0xffff
for (const byte of data) {
crc ^= byte << 8
for (let i = 0; i < 8; i++) crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0)) & 0xffff
}
return crc
}
export function frameMessage(payload: Uint8Array, sid: number, flag: number, sequence: number, maxWrite = 240): Uint8Array[] {
const chunkSize = Math.min(232, Math.floor(maxWrite) - 8)
if (chunkSize < 12) throw new Error('Negotiated BLE write size is too small')
const crc = crc16(payload), withCrc = concat(payload, new Uint8Array([crc & 255, crc >>> 8]))
const count = Math.ceil(withCrc.length / chunkSize)
if (count > 255) throw new Error('Protocol message exceeds the fragment limit')
return Array.from({ length: count }, (_, i) => {
const chunk = withCrc.subarray(i * chunkSize, (i + 1) * chunkSize)
return concat(new Uint8Array([0xaa, 0x21, sequence & 255, chunk.length, count, i + 1, sid, flag]), chunk)
})
}
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number; packet?: Uint8Array }
/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */
export class MessageReceiver {
private streams = new Map<string, Uint8Array>()
private messages = new Map<string, { chunks: Uint8Array[]; total: number; seen: number; size: number }>()
clear(): void { this.streams.clear(); this.messages.clear() }
receive(link: string, data: Uint8Array, now = Date.now()): ProtocolMessage[] {
for (const [key, message] of this.messages) if (now - message.seen > 5000) this.messages.delete(key)
let stream = concat(this.streams.get(link) ?? new Uint8Array(), data)
const output: ProtocolMessage[] = []
while (stream.length >= 8) {
if (stream[0] !== 0xaa || ![0x21, 0x12].includes(stream[1])) { stream = stream.subarray(1); continue }
const length = stream[3], count = stream[4], index = stream[5]
if (stream.length < length + 8) break
const sid = stream[6], flag = stream[7], key = `${link}:${stream[2]}:${sid}:${flag}`
const packet = stream.slice(0, length + 8)
const chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8)
if (sid === SID.cfw) {
output.push({ sid, flag, payload: chunk, command: -1, magic: -1, packet })
continue
}
if (!count || !index || index > count) { this.messages.delete(key); continue }
if (index === 1) this.messages.set(key, { chunks: [], total: count, seen: now, size: 0 })
const message = this.messages.get(key)
if (!message || message.total !== count || message.chunks.length + 1 !== index) { this.messages.delete(key); continue }
message.chunks.push(chunk); message.size += chunk.length; message.seen = now
if (message.size > 65536) { this.messages.delete(key); continue }
if (index !== count) continue
this.messages.delete(key)
const complete = concat(...message.chunks)
if (complete.length < 2) continue
const payload = complete.subarray(0, -2), crc = complete[complete.length - 2] | (complete[complete.length - 1] << 8)
if (crc16(payload) !== crc) continue
try { output.push({ sid, flag, payload, command: readInteger(payload, 1, -1), magic: readInteger(payload, 2, -1) }) }
catch { /* Malformed notifications cannot satisfy an acknowledgement. */ }
}
this.streams.set(link, stream.slice())
return output
}
}
function readVarint(data: Uint8Array, offset: number): [number, number] {
let value = 0
for (let i = 0; i < 5 && offset < data.length; i++) {
const byte = data[offset++]; value += (byte & 127) * 2 ** (i * 7)
if (!(byte & 128)) return [value, offset]
}
throw new Error('Truncated or oversized protobuf varint')
}
function field(data: Uint8Array, number: number): number | Uint8Array | undefined {
let offset = 0
while (offset < data.length) {
let key: number; [key, offset] = readVarint(data, offset)
const tag = key >>> 3, wire = key & 7
if (!tag) throw new Error('Invalid protobuf field')
if (wire === 0) {
let value: number; [value, offset] = readVarint(data, offset)
if (tag === number) return value
} else {
let length: number
if (wire === 2) [length, offset] = readVarint(data, offset)
else if (wire === 1) length = 8
else if (wire === 5) length = 4
else throw new Error('Unsupported protobuf wire type')
if (offset + length > data.length) throw new Error('Truncated protobuf field')
if (tag === number && wire === 2) return data.subarray(offset, offset + length)
offset += length
}
}
return undefined
}
export function readInteger(data: Uint8Array, number: number, fallback = 0): number { const value = field(data, number); return typeof value === 'number' ? value : fallback }
export function readBytes(data: Uint8Array, number: number): Uint8Array | undefined { const value = field(data, number); return value instanceof Uint8Array ? value : undefined }
export function readString(data: Uint8Array, number: number): string { return Array.from(readBytes(data, number) ?? [], value => String.fromCharCode(value)).join('') }
export function authenticationSucceeded(message: ProtocolMessage, magic: number): boolean {
return message.sid === SID.auth && message.command === 4 && message.magic === magic && readBytes(message.payload, 3)?.length === 0
}
export type GlassesInput = { ringInput?: RingInput; kind: 'sys-event' | 'list-click' | 'text-click' | 'display-wake' | 'even-ai'; eventType: number; eventSource: number; systemExitReasonCode: number; containerName: string; frameId: number }
export function decodeGlassesInput(message: ProtocolMessage): GlassesInput | null {
if (message.sid !== SID.hub || ![1, 6].includes(message.flag)) return null
const events = readBytes(message.payload, 13); if (!events) return null
for (const [field, kind, typeField] of [[1, 'list-click', 5], [2, 'text-click', 3], [3, 'sys-event', 1]] as const) {
const event = readBytes(events, field)
if (event) return { kind: kind === 'sys-event' && readInteger(event, typeField) === OsEventTypeList.HEAD_UP_EVENT ? 'display-wake' : kind,
eventType: readInteger(event, typeField), eventSource: field === 3 ? readInteger(event, 2) : 0,
ringInput: field === 3 && readInteger(event, 2) === 2 ? decodeRingMetadata(readBytes(event, 100)) : undefined,
systemExitReasonCode: field === 3 ? readInteger(event, 4) : 0, containerName: field === 3 ? '' : readString(event, 2), frameId: 0 }
}
return null
}
export function decodeRingInput(data: Uint8Array): GlassesInput | null {
let eventType: number | undefined
if (data.length === 11 && data[0] === 0 && data[1] === 9 && data[2] === 0x61 && data[3] === 0)
eventType = ({ 0: 9, 1: 0, 2: 3, 4: 1, 5: 2, 8: 10 } as Record<number, number>)[data[4]]
if (data.length === 3 && data[0] === 255) {
if (data[1] === 3 && data[2] === 32) eventType = 9
if (data[1] === 4 && data[2] === 1) eventType = 0
if (data[1] === 4 && data[2] === 2) eventType = 3
if (data[1] === 5) eventType = data[2] <= 1 ? 2 : 1
}
return eventType === undefined ? null : { kind: 'sys-event', eventType, eventSource: 2, systemExitReasonCode: 0, containerName: '', frameId: 0 }
}
export function packGray4(gray: Uint8Array, width: number, height: number): Uint8Array {
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || gray.length !== width * height) throw new Error('Invalid display frame')
const stride = Math.ceil(width / 2), packed = new Uint8Array(stride * height)
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
const nibble = Math.min(15, (gray[y * width + x] + 8) >> 4)
packed[y * stride + (x >> 1)] |= nibble << (x % 2 ? 0 : 4)
}
return packed
}
export function rle4(packed: Uint8Array): Uint8Array {
const output = new Uint8Array(packed.length * 2)
const nibble = (i: number) => (packed[i >> 1] >> (i % 2 ? 0 : 4)) & 15
let used = 0, i = 0
while (i < packed.length * 2) {
const color = nibble(i); let end = i + 1
while (end < packed.length * 2 && nibble(end) === color) end++
let remaining = end - i
while (remaining) {
const count = Math.min(65535, remaining)
if (count <= 15) output[used++] = (count << 4) | color
else { output[used++] = color; if (count <= 255) output[used++] = count
else { output[used++] = 0; output[used++] = count & 255; output[used++] = count >>> 8 } }
remaining -= count
}
i = end
}
return output.slice(0, used)
}
/** Reference decoder for host tests; iOS uses the shared Kotlin implementation. */
export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null {
if (message.sid !== 8 || ![1, 6].includes(message.flag)) return null
const command = readInteger(message.payload, 1, -1)
if (command === 16 || command === 17) return { command, headingDegrees: -1 }
if (command !== 15) return null
const compass = readBytes(message.payload, 10)
const headingDegrees = compass ? readInteger(compass, 1, -1) : -1
if (headingDegrees < 0 || headingDegrees >= 360) return null
const event: CompassEvent = { command, headingDegrees }
let d: Uint8Array | undefined
try { d = readBytes(message.payload, 100) } catch { return event }
if (d?.length === 12 && d[0] === 67 && d[1] === 77 && d[2] === 1 && d[7] === 0 &&
(d[3] <= 3 || d[3] === 255) && (d[4] <= 2 || d[4] === 255) && d[5] <= 3) {
event.diagnostics = { magneticAccuracy: d[3] === 255 ? -1 : d[3], magneticAnomalies: d[4] === 255 ? -1 : d[4],
orientationSource: d[5], flags: d[6], sampleTimeMs: new DataView(d.buffer, d.byteOffset + 8, 4).getUint32(0, true) }
}
return event
}
-17
View File
@@ -1,17 +0,0 @@
/** Same units as Android FaceclawBleManager: protocol messages, framed write
* bytes accepted by the transport, and fully acknowledged display updates.
* Totals survive reconnects and the setting being switched off.
*/
export class BleTrafficCounters {
private messages = 0
private bytes = 0
private frames = 0
recordWrite(bytes: number): void { this.bytes += bytes }
recordMessage(): void { this.messages++ }
recordDisplayFrame(): void { this.frames++ }
sample(): { messages: number; bytes: number; frames: number } {
return { messages: this.messages, bytes: this.bytes, frames: this.frames }
}
}
// The iOS BLE session and phone UI both run in the main isolate.
export const iosBleTraffic = new BleTrafficCounters()
-8
View File
@@ -1,8 +0,0 @@
import { bytes, concat, integer } from './ble-protocol'
/** Same G2SettingPackage as Kotlin BleProtocol.buildSetBrightness. */
export function setBrightness(magic: number, level: number | null): Uint8Array {
const fields = level === null ? integer(1, 1)
: concat(integer(1, 0), integer(2, Math.max(0, Math.min(100, Math.round(level)))))
return concat(integer(1, 1), integer(2, magic), bytes(3, bytes(1, fields)))
}
-21
View File
@@ -1,21 +0,0 @@
import { fromData, toData, nativeArray } from '../native/kotlin-data'
declare const FaceclawKitIosCfwTransport: any, FaceclawKitIosProtocol: any
export const CFW_MAX_MESSAGE = 65535
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
/** maxWrite is the ATT value limit; Kotlin owns the persistent zlib stream. */
export class CfwTransport {
private native = FaceclawKitIosCfwTransport.new()
reset(): void { this.native.reset() }
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
throw new Error('Invalid CFW stream parameters')
return nativeArray(this.native.encodeDataStreamIdLensesMaxWrite(toData(message), streamId, lenses, maxWrite), fromData)
}
}
const protocol = FaceclawKitIosProtocol.new()
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
const acks = protocol.acksData(toData(packet))
return acks ? nativeArray(acks, (ack: any) => ({ nack: ack.nack, streamId: ack.streamId, messageId: ack.messageId,
lens: ack.lens, size: ack.size, checksum: ack.checksum })) : null
}
-57
View File
@@ -1,57 +0,0 @@
import { concat, crc16, SID } from './ble-protocol'
declare const require: (name: string) => any
/** Mirrors CFW_MESSAGE_MAX in g2flash/patches/message_transport.h. */
export const CFW_MAX_MESSAGE = 65535
export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number }
const u16 = (data: Uint8Array, offset: number) => data[offset] | (data[offset + 1] << 8)
/** Matches Android CfwTransport. maxWrite is the ATT value limit (MTU - 3). */
export class CfwTransport {
private compressor: any
private resetPending = true
private lenses = 0
reset(): void { this.compressor = null; this.resetPending = true; this.lenses = 0 }
encode(message: Uint8Array, streamId: number, lenses: number, maxWrite: number): Uint8Array[] {
if (message.length > CFW_MAX_MESSAGE || !Number.isInteger(streamId) || streamId < 0 || streamId > 255 ||
![1, 2, 3].includes(lenses) || !Number.isInteger(maxWrite) || maxWrite < 20 || maxWrite > 514)
throw new Error('Invalid CFW stream parameters')
if (this.lenses !== lenses) this.reset()
if (!this.compressor) this.compressor = new (require('pako').Deflate)()
const chunks: Uint8Array[] = []
// SYNC_FLUSH emits this record while retaining history for the next one.
this.compressor.onData = (chunk: Uint8Array) => chunks.push(chunk.slice())
if (!this.compressor.push(message, 2)) throw new Error(this.compressor.msg || 'CFW compression failed')
let body = concat(...chunks), flags = lenses | 4 | (this.resetPending ? 8 : 0)
if (!body.length) flags &= ~4
if (body.length > CFW_MAX_MESSAGE) {
this.reset(); body = message; flags = lenses | 8
} else { this.resetPending = false; this.lenses = lenses }
const checksum = crc16(message)
const record = concat(new Uint8Array([flags, body.length & 255, body.length >>> 8, checksum & 255, checksum >>> 8]), body)
const capacity = Math.min(252, maxWrite - 11), packets: Uint8Array[] = []
for (let offset = 0, index = 0; offset < record.length; offset += capacity, index++) {
const body = concat(new Uint8Array([lenses | (offset === 0 ? 0x80 : 0) |
(offset + capacity >= record.length ? 0x40 : 0)]), record.subarray(offset, offset + capacity))
const crc = crc16(body)
packets.push(concat(new Uint8Array([0xaa, 0x21, (streamId + index) & 255, body.length + 2, 1, 1, SID.cfw, 0]),
body, new Uint8Array([crc & 255, crc >>> 8])))
}
return packets
}
}
/** Validate the entire primary + redundant-ACK packet before applying any ACK. */
export function parseCfwAcks(packet: Uint8Array): CfwAck[] | null {
const n = packet.length
if (n < 19 || n > 40 || (n - 19) % 7 || packet[0] !== 0xaa || packet[1] !== 0x12 ||
packet[3] !== n - 8 || packet[4] !== 1 || packet[5] !== 1 || packet[6] !== SID.cfw || packet[7] !== 0 ||
![1, 3].includes(packet[8]) || ![1, 2].includes(packet[12]) || (packet[8] === 3 && n !== 19) ||
crc16(packet.subarray(8, -2)) !== u16(packet, n - 2)) return null
const acks = [{ nack: packet[8] === 3, streamId: packet[9], messageId: u16(packet, 10),
lens: packet[12], size: u16(packet, 13), checksum: u16(packet, 15) }]
for (let offset = 17; offset < n - 2; offset += 7)
acks.push({ nack: false, streamId: packet[offset], messageId: u16(packet, offset + 1), lens: packet[12],
size: u16(packet, offset + 3), checksum: u16(packet, offset + 5) })
return acks
}
-15
View File
@@ -1,15 +0,0 @@
import { CFW_MSG_BOUNDING_BOX, CFW_MSG_DRAW_CALLS, CFW_MSG_FULL_FRAME, CFW_MSG_MULTI_SEGMENT, CFW_MSG_TYPE_MASK } from "./cfw-message-type";
/** Adapter for raster-only senders; optimized legacy layouts remain internal to the phone. */
export function drawMessages(payload: Uint8Array, width = 640, height = 480): Uint8Array[] {
const word = (v: number) => [v & 255, (v >>> 8) & 255]
const mode = payload[0] & CFW_MSG_TYPE_MASK
let calls: Uint8Array[]
if (mode === CFW_MSG_MULTI_SEGMENT) {
const output: Uint8Array[] = []; let p=2
for(let i=0;i<payload[1];i++) { const n=payload[p] | payload[p+1]<<8; p+=2; output.push(...drawMessages(payload.subarray(p,p+n),width,height));p+=n }
return output
} else if (mode === CFW_MSG_BOUNDING_BOX) calls=[new Uint8Array([1,0,0,...payload.subarray(1,5),...payload.subarray(7)])]
else if(mode === CFW_MSG_FULL_FRAME) calls=[new Uint8Array([1,0,1,0,0,0,0,...word(width),...word(height),...payload.subarray(1)])]
else throw new Error(`Unsupported raster draw ${mode}`)
return calls.map(call => new Uint8Array([CFW_MSG_DRAW_CALLS,1,0,...word(call.length),...call]))
}
-645
View File
@@ -1,645 +0,0 @@
import { CFW_MSG_CLEANUP, CFW_MSG_COMPASS, CFW_MSG_EVICT_RESOURCE, CFW_MSG_FULL_FRAME, CFW_MSG_PRESENT } from "./cfw-message-type";
import { drawMessages } from './draw-protocol'
import { setBrightness } from './brightness-protocol'
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
import { type CompassEvent } from '../native/compass-types'
import * as protocol from './ble-protocol'
import { iosBleTraffic } from './ble-traffic-counters'
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
import { CfwTransport, CFW_MAX_MESSAGE, parseCfwAcks } from './cfw-transport'
import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLAW_FIRMWARE_VERSION } from './firmware-compat'
import { hexToBytes } from '../util/hex-util'
import { deviceAddressError } from './ios-peripheral-identity'
import { decodeWearState, enableWearDetection, queryWearState } from './wear-protocol'
import type { SessionTexturePlanner, TextureFrame } from './texture-planner'
declare function setTimeout(callback: () => void, ms: number): number
declare function clearTimeout(id: number): void
export type SessionAddresses = { left: string; right: string; ring: string }
export type TransportEvent = { kind: string; identifier?: string; characteristic?: string; data?: string; message?: string; state?: number; authorized?: boolean }
export interface SessionTransport {
onEvent(listener: (event: TransportEvent) => void): () => void
resolveDevices(addresses: SessionAddresses): Promise<Record<string, string>>
connect(identifier: string, requiresANCS?: boolean): Promise<{ characteristics: string[]; maxWrite: number }>
subscribe(identifier: string, characteristic: string): Promise<void>
write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void>
disconnect(identifier: string): void
stopScan(): void
}
export type SessionState = { phase: 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error';
status: string; battery: number | null; charging: boolean | null; ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
type PendingAck = { resolve: (message: protocol.ProtocolMessage) => void; reject: (error: Error) => void;
timer: number | null; command: number; label: string }
class AckTimeout extends Error {}
// Match Android ConnectionOptions.WINDOW_SIZE. This bounds complete image
// messages queued for writing or awaiting ACK, not individual BLE packets.
const DISPLAY_WINDOW_SIZE = 3
type CfwPending = { payload: Uint8Array; checksum: number; role: string; magic: number; ackLenses: number; retries: number;
retryPending: boolean; deadline: number; resolve: () => void; reject: (error: Error) => void }
type DisplayFrame = { packed: Uint8Array; commands: Uint8Array[]; offset: number; pending: number }
/** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */
export class GlassesSession {
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false,
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
readonly notifications: AncsClient
private generation = 0
private ids: Record<string, string> = {}
private limits: Record<string, number> = {}
private cfwPending: CfwPending[] = []
private cfwTransports = new Map<string, CfwTransport>()
private cfwTimer: number | null = null
private pending = new Map<string, PendingAck>()
private receiver = new protocol.MessageReceiver()
private writes = new Map<string, Promise<void>>()
private magic = 100
private sequence = 0x40
private timer: number | null = null
private retryTimer: number | null = null
private ringRetryTimer: number | null = null
private ringRetryCount = 0
private ringConnecting = false
private retryCount = 0
private wanted = false
private addresses: SessionAddresses | null = null
private latest: Uint8Array | null = null
private latestTextures: TextureFrame | null = null
private displayed: Uint8Array | null = null
private lastEnqueued: Uint8Array | null = null
// Match Android: advance only for emitted deltas, skipping 0 / 0xffff.
private nextImageFrameId = 1
private displayFrames: DisplayFrame[] = []
private displaySending: DisplayFrame | null = null
private displayInFlight = 0
private pumping = false
private lastHeartbeat = 0
private lastLease = 0
private lastSettings = 0
private charging = false
private layoutCreated = false
private off: () => void
private stopping: Promise<void> | null = null
private audioListener: ((packet: Uint8Array) => void) | null = null
private microphoneToken = 0
private microphoneWork: Promise<void> = Promise.resolve()
private microphoneEnabled = false
private compassWanted = false
private compassSent: boolean | null = null
private compassStopping = false
private compassWork: Promise<void> = Promise.resolve()
constructor(private readonly transport: SessionTransport,
private readonly onState: (state: SessionState) => void,
private readonly onInput: (input: protocol.GlassesInput) => void,
private readonly log: (message: string) => void = () => {},
private readonly onActivity: () => void = () => {},
private readonly onCompass: (event: CompassEvent) => void = () => {},
private readonly onWearState: (wearing: boolean) => void = () => {},
onNotificationsChanged: (key?: string, popup?: boolean) => void = () => {},
private readonly textures?: SessionTexturePlanner) {
this.notifications = new AncsClient(packet => this.writePackets('right', () => [packet]), onNotificationsChanged, message => this.log('ANCS: ' + message))
this.off = transport.onEvent(event => this.receive(event))
}
private update(phase: SessionState['phase'], status: string): void {
this.state = { ...this.state, phase, status }; this.log(status); this.onState({ ...this.state })
}
async start(addresses: SessionAddresses, retry = false): Promise<void> {
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
const invalid = deviceAddressError(addresses)
if (invalid) { this.update('error', invalid); return }
this.addresses = { ...addresses }; this.wanted = true; this.compassStopping = false
if (!retry) this.retryCount = 0
const generation = ++this.generation
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
this.charging = false
this.update('connecting', 'Finding configured devices…')
try {
const ids = await this.transport.resolveDevices(addresses); this.check(generation)
this.ids = ids
for (const role of ['right', 'left']) {
this.update('connecting', `Connecting ${role} arm…`)
const details = await this.transport.connect(ids[role], role === 'right'); this.check(generation)
this.limits[role] = details.maxWrite
if (!details.characteristics.includes(protocol.G2_WRITE) || !details.characteristics.includes(protocol.G2_NOTIFY)) throw new Error(`${role} arm is missing G2 communication characteristics.`)
await this.transport.subscribe(ids[role], protocol.G2_NOTIFY); this.check(generation)
}
this.update('connecting', 'Authenticating… Accept pairing if iOS asks.')
await Promise.all(['right', 'left'].map(async role => {
try {
const answer = await this.request(role, protocol.SID.auth, protocol.authentication, 'Security authentication', 30_000, 0)
if (!protocol.authenticationSucceeded(answer, answer.magic)) throw new Error(`${role} arm rejected authentication.`)
} catch (error) {
if (!(error instanceof AckTimeout)) throw error
// Older CFW may not implement the reply. A settings response below
// still has to prove we have a working, compatible session.
this.log(`${role} authentication unconfirmed; checking firmware response`)
}
}))
this.check(generation)
this.update('connecting', 'Checking glasses firmware…')
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Settings query')
this.applySettings(settings)
const firmware = { leftVersion: this.state.leftVersion, rightVersion: this.state.rightVersion, extension: this.state.capabilities }
if (!hasCompatibleFirmware(firmware))
throw new Error((firmwareIncompatibilityMessage(firmware) || `The glasses must report Faceclaw firmware revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION} or newer.`) + ' Use Devices → Check firmware to install it.')
this.update('connecting', 'Starting glasses display…')
await this.request('right', protocol.SID.launch, () => protocol.prelude(), 'App launch', 3500, 0x20, 156)
await this.lease(true)
await this.request('right', protocol.SID.hub, protocol.createLayout, 'Create display layout')
this.check(generation); this.layoutCreated = true
await this.enableWearDetectionAndRequestState(); this.check(generation)
this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
this.displayed = null; this.retryCount = 0
this.update('connected', 'Glasses connected')
if (/^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION)
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
else this.notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
await this.syncCompass(); this.check(generation)
this.schedule()
if (addresses.ring && ids.ring) await this.connectRing(generation)
else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found')
} catch (error) {
if (generation !== this.generation) return
this.fail(error, retry || error instanceof AckTimeout)
}
}
private async connectRing(generation: number): Promise<void> {
if (this.ringConnecting || generation !== this.generation || this.state.phase !== 'connected' || !this.ids.ring) return
this.ringConnecting = true
const identifier = this.ids.ring
try {
const ring = await this.transport.connect(identifier); this.check(generation)
this.log(`R1 services discovered: ${ring.characteristics.join(', ')}`)
const notifications = protocol.RING_NOTIFY.filter(uuid => ring.characteristics.includes(uuid))
if (!notifications.length) throw new Error('No R1 gesture notifications found')
let subscribed = 0
for (const uuid of notifications) {
this.log(`Subscribing R1 ${uuid}`)
try { await this.transport.subscribe(identifier, uuid); this.check(generation); subscribed++; this.log(`R1 subscribed ${uuid}`) }
catch (error) { this.check(generation); this.log(`R1 channel unavailable ${uuid}: ${this.message(error)}`) }
}
if (!subscribed) throw new Error('Neither R1 notification channel could be subscribed')
this.check(generation); this.state.ring = true; this.ringRetryCount = 0
this.update('connected', 'Glasses and ring connected')
} catch (error) {
if (generation !== this.generation) return
this.state.ring = false
this.transport.disconnect(identifier)
this.update('connected', `Glasses connected; ring unavailable: ${this.message(error)}`)
} finally {
if (generation === this.generation) {
this.ringConnecting = false
if (!this.state.ring) this.scheduleRingRetry()
}
}
}
private scheduleRingRetry(): void {
if (!this.wanted || this.state.phase !== 'connected' || !this.ids.ring || this.ringConnecting || this.ringRetryTimer !== null || this.ringRetryCount >= 5) return
const generation = this.generation, delay = Math.min(30_000, 2000 * 2 ** this.ringRetryCount++)
this.log(`Retrying ring in ${delay / 1000}s`)
this.ringRetryTimer = setTimeout(() => {
this.ringRetryTimer = null
if (generation === this.generation) void this.connectRing(generation)
}, delay)
}
private check(generation: number): void { if (generation !== this.generation) throw new Error('Connection cancelled') }
private nextMagic(): number {
for (let i = 0; i < 156; i++) {
const magic = this.magic; this.magic = this.magic === 255 ? 100 : this.magic + 1
if (magic !== 156 && !this.cfwPending.some(item => item.magic === magic) && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
}
throw new Error('All message identifiers are in use')
}
private request(role: string, sid: number, build: (magic: number) => Uint8Array, label: string,
timeout = 3500, flag = 0x20, fixedMagic?: number): Promise<protocol.ProtocolMessage> {
const magic = fixedMagic ?? this.nextMagic(), payload = build(magic), key = `${sid}:${magic}`
this.log(`TX ${label} ${role} sid=${sid.toString(16)} magic=${magic}`)
const generation = this.generation
return new Promise((resolve, reject) => {
const pending: PendingAck = { resolve, reject, timer: null, command: protocol.readInteger(payload, 1, -1), label }
this.pending.set(key, pending)
this.send(role, sid, flag, payload).then(() => {
if (this.pending.get(key) !== pending || generation !== this.generation) return
pending.timer = setTimeout(() => {
this.pending.delete(key); reject(new AckTimeout(`${label} was not acknowledged by the glasses.`))
}, timeout)
}).catch(error => {
if (this.pending.get(key) !== pending) return
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer); reject(error)
})
})
}
private send(role: string, sid: number, flag: number, payload: Uint8Array): Promise<void> {
return this.writePackets(role, () => protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role]))
}
private writePackets(role: string, build: () => Uint8Array[], current = () => true): Promise<void> {
const identifier = this.ids[role], generation = this.generation
if (!identifier) return Promise.reject(new Error(`No ${role} device connected`))
const previous = this.writes.get(identifier) ?? Promise.resolve()
// A failed write poisons this link's queue until reset: do not transmit
// later fragments after a hole in the image stream.
const work = previous.then(async () => {
this.check(generation)
if (!current()) return
for (const frame of build()) {
this.check(generation)
if (!current()) return
await this.transport.write(identifier, protocol.G2_WRITE, frame)
iosBleTraffic.recordWrite(frame.length)
}
iosBleTraffic.recordMessage()
})
// A whole message owns the write queue; heartbeat packets cannot split an image message.
this.writes.set(identifier, work)
return work
}
private lease(acquire: boolean): Promise<void[]> {
return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire))))
}
private requestCfw(role: string, payload: Uint8Array): Promise<void> {
if (payload.length > CFW_MAX_MESSAGE - 16) return Promise.reject(new Error('CFW message too large'))
return new Promise((resolve, reject) => {
const item: CfwPending = { role, payload, checksum: protocol.crc16(payload), magic: this.nextMagic(), ackLenses: 0, retries: 0,
retryPending: false, deadline: 0, resolve, reject }
this.cfwPending.push(item)
this.sendCfw(item)
})
}
private sendCfw(item: CfwPending): void {
const generation = this.generation, magic = item.magic
const current = () => generation === this.generation && item.magic === magic && this.cfwPending.includes(item)
void this.writePackets(item.role, () => {
let transport = this.cfwTransports.get(item.role)
if (!transport) { transport = new CfwTransport(); this.cfwTransports.set(item.role, transport) }
if (item.retries) transport.reset()
return transport.encode(item.payload, magic, 3, this.limits[item.role])
}, current).then(() => {
if (!current()) return
item.deadline = Date.now() + 500
this.scheduleCfwRecovery()
}).catch(error => { if (current()) this.fail(error, true) })
}
private receiveCfw(identifier: string, packet: Uint8Array): void {
const acks = parseCfwAcks(packet)
if (!acks) return
for (const ack of acks) {
// The ingress temple relays the other lens's result on the SAME link.
const item = this.cfwPending.find(p => this.ids[p.role] === identifier && p.magic === ack.streamId)
if (!item || ack.messageId !== 0) continue
if (ack.nack) item.retryPending = true
else if (ack.size === item.payload.length && ack.checksum === item.checksum) item.ackLenses |= ack.lens
this.log(`CFW ${ack.nack ? 'NACK' : 'ACK'} stream=${ack.streamId} lens=${ack.lens} size=${ack.size} crc=${ack.checksum} confirmed=${item.ackLenses}`)
}
while (this.cfwPending.length) {
const head = this.cfwPending[0]
if (head.retryPending || head.ackLenses !== 3) break
this.cfwPending.shift(); head.resolve()
}
this.scheduleCfwRecovery()
}
private scheduleCfwRecovery(): void {
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
this.cfwTimer = null
let deadline = Infinity
for (const item of this.cfwPending) {
if (item.retryPending) deadline = 0
else if (item.ackLenses !== 3 && item.deadline) deadline = Math.min(deadline, item.deadline)
}
if (deadline === Infinity) return
this.cfwTimer = setTimeout(() => {
this.cfwTimer = null
this.recoverCfw()
}, Math.max(0, deadline - Date.now()))
}
private recoverCfw(): void {
if (!this.cfwPending.some(p => p.retryPending || (p.ackLenses !== 3 && p.deadline > 0 && p.deadline <= Date.now()))) {
this.scheduleCfwRecovery(); return
}
if (this.cfwPending.some(p => p.retries >= 3)) { this.fail(new Error('CFW recovery retry limit'), true); return }
this.log(`CFW recovery: replay ${this.cfwPending.length} unresolved messages`)
// Go back to the oldest unresolved command, including any ACKed tail.
// Fresh stream IDs reject stale ACKs; each replay resets compression history.
for (const item of this.cfwPending) {
item.magic = this.nextMagic(); item.retries++; item.ackLenses = 0
item.deadline = 0; item.retryPending = false
}
for (const item of this.cfwPending) this.sendCfw(item)
}
private receive(event: TransportEvent): void {
if (!Object.values(this.ids).includes(event.identifier ?? '')) return
if (event.kind === 'ancs-authorization' && event.identifier === this.ids.right) {
if (event.authorized === false) {
const active = this.notifications.state !== 'disconnected'
const command = this.notifications.stopCommand()
this.notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.')
if (active && this.state.phase === 'connected') void this.writePackets('right', () => [command]).catch(() => {})
} else if (this.state.phase === 'connected' && /^Faceclaw\/(\d+)/.test(this.state.capabilities) && Number(this.state.capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION) {
this.notifications.start((Date.now() ^ Math.floor(Math.random()*0xffffffff)) >>> 0, this.limits.right)
}
return
}
if (event.kind === 'disconnected') {
if (event.identifier === this.ids.ring) {
this.state.ring = false
if (this.state.phase === 'connected') {
this.update('connected', `Glasses connected; ring disconnected${event.message ? ': ' + event.message : ''}`)
this.scheduleRingRetry()
}
} else if (['connected', 'connecting'].includes(this.state.phase)) this.fail(new Error(event.message || 'Glasses disconnected'), true)
return
}
if (event.kind !== 'notification' || !event.data) return
if (event.characteristic === protocol.G2_RENDER_NOTIFY && event.identifier !== this.ids.ring) {
if (this.audioListener && this.state.phase === 'connected') {
this.audioListener(hexToBytes(event.data))
this.onActivity(); this.wake()
}
return
}
const ring = event.identifier === this.ids.ring
if (ring ? !protocol.RING_NOTIFY.includes(event.characteristic ?? '') : event.characteristic !== protocol.G2_NOTIFY) return
try {
const data = hexToBytes(event.data)
if (ring) {
const input = protocol.decodeRingInput(data)
if (input) { this.log(`Direct R1 gesture ${input.eventType}`); this.onInput(input) }
return
}
if (event.identifier === this.ids.right && this.notifications.receive(data)) return
for (const message of this.receiver.receive(event.identifier!, data)) {
this.log(`RX ${event.identifier === this.ids.left ? 'L' : 'R'} sid=${message.sid.toString(16)} flag=${message.flag.toString(16)} cmd=${message.command} magic=${message.magic}`)
if (message.sid === protocol.SID.cfw) {
this.receiveCfw(event.identifier!, message.packet!)
continue
}
if (![1, 6].includes(message.flag)) {
// A master arm can relay the other arm's ACK; magic is allocated
// across BOTH links, matching Android's communicator.
const key = `${message.sid}:${message.magic}`, pending = this.pending.get(key)
if (pending) {
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer)
pending.resolve(message)
}
}
if (message.sid === protocol.SID.settings) this.applySettings(message)
const wearing = decodeWearState(message)
if (wearing !== null) this.onWearState(wearing)
if (event.identifier === this.ids.right) {
if (this.state.phase === 'connected') {
const compass = protocol.decodeCompassInput(message)
if (compass) this.onCompass(compass)
}
const input = protocol.decodeGlassesInput(message)
if (input) {
if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') {
// Respect an explicit glasses-side exit; the user can reconnect.
void this.stop(); continue
}
this.onInput(input)
}
}
}
} catch (error) { this.log(`Ignored malformed BLE notification: ${this.message(error)}`) }
finally {
// CoreBluetooth can wake a suspended iOS process for a notification.
// Service wall-clock deadlines now, without waiting for a suspended timer.
if (this.state.phase === 'connected') { this.onActivity(); this.wake() }
}
}
private applySettings(message: protocol.ProtocolMessage): void {
const values = protocol.readBytes(message.payload, 4)
const capabilities = protocol.readString(message.payload, 100)
if (capabilities) this.state.capabilities = capabilities
if (values) {
const left = protocol.readString(values, 5), right = protocol.readString(values, 6)
if (left) this.state.leftVersion = left
if (right) this.state.rightVersion = right
const battery = protocol.readInteger(values, 12, -1), charging = protocol.readInteger(values, 13, -1)
if (battery >= 0 && battery <= 100) this.state.battery = battery
if (charging >= 0) this.state.charging = this.charging = charging > 0
}
this.onState({ ...this.state })
}
async enableWearDetectionAndRequestState(): Promise<void> {
const generation = this.generation
// Like Android, a missing enable ACK must not suppress the current-state query.
try { await this.request('right', protocol.SID.settings, enableWearDetection, 'Enable wear detection') }
catch (error) { if (!(error instanceof AckTimeout)) throw error; this.log(this.message(error)) }
this.check(generation)
await this.send('right', protocol.SID.settings, 0x20, queryWearState())
this.check(generation)
await this.send('left', protocol.SID.settings, 0x20, queryWearState())
}
async setBrightness(level: number | null): Promise<void> {
if (this.state.phase !== 'connected') return
await this.request('right', protocol.SID.settings, magic => setBrightness(magic, level), 'Brightness')
}
setFrame(gray: Uint8Array, textures?: TextureFrame): void {
if (this.state.phase !== 'connected') return
this.latest = protocol.packGray4(gray, 640, 480)
this.latestTextures = textures ?? null
this.schedule(0)
}
/** Same mode-5 kind-4 sequencer payload and acknowledged image path as Android. */
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
if (this.state.phase !== 'connected' || !this.layoutCreated || payload.length < 3) return
const generation = this.generation
try { await this.requestCfw('left', new Uint8Array(payload)) }
catch (error) { if (generation === this.generation) this.fail(error, true) }
}
/** Serialize mic enable/disable, including a release while enable awaits ACK. */
setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise<void> {
const token = ++this.microphoneToken, generation = this.generation
this.audioListener = enabled ? listener ?? null : null
const current = () => generation === this.generation && token === this.microphoneToken
const work = this.microphoneWork.catch(() => {}).then(async () => {
if (!current()) return
if (this.state.phase !== 'connected') {
if (enabled) throw new Error('Connect the glasses before starting voice input.')
return
}
if (enabled) {
await Promise.all(['left', 'right'].map(role => this.transport.subscribe(this.ids[role], protocol.G2_RENDER_NOTIFY)))
if (!current()) return
}
await this.request('right', protocol.SID.hub, magic => protocol.audioControl(magic, enabled), enabled ? 'Microphone enable' : 'Microphone disable')
if (current()) { this.microphoneEnabled = enabled; this.log(`Glasses microphone ${enabled ? 'enabled' : 'disabled'}`) }
}).catch(error => {
if (current()) { this.audioListener = null; this.microphoneEnabled = false }
throw error
})
this.microphoneWork = work
return work
}
setCompassEnabled(enabled: boolean): void {
this.compassWanted = enabled
void this.syncCompass()
}
/** Serialize changes so a release cannot overtake an unacknowledged enable. */
private syncCompass(): Promise<void> {
const generation = this.generation
const work = this.compassWork.then(async () => {
if (generation !== this.generation || this.state.phase !== 'connected') return
const enabled = !this.compassStopping && this.compassWanted
if (this.compassSent === enabled) return
// Same CFW mode 10, 100 ms report interval and zero minimum change as Android.
await this.requestCfw('left', new Uint8Array(enabled ? [CFW_MSG_COMPASS, 2, 100, 0, 0, 0] : [CFW_MSG_COMPASS, 0]))
if (generation === this.generation) this.compassSent = enabled
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
this.compassWork = work
return work
}
wake(): void {
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
void this.pump()
}
private schedule(delay = 1000): void {
if (delay === 0 && this.timer !== null) { clearTimeout(this.timer); this.timer = null }
if (this.timer !== null || this.pumping || this.state.phase !== 'connected') return
this.timer = setTimeout(() => { this.timer = null; void this.pump() }, delay)
}
private canSendDisplay(): boolean {
if (this.displaySending?.commands[this.displaySending.offset][0] === CFW_MSG_EVICT_RESOURCE && this.cfwPending.length) return false
// Also bound completed frames retained behind a missing/out-of-order ACK.
return this.displayInFlight < DISPLAY_WINDOW_SIZE && !!(this.displaySending ||
(this.latest && !this.charging && this.displayFrames.length < DISPLAY_WINDOW_SIZE))
}
private fillDisplayWindow(generation: number): void {
while (this.canSendDisplay()) {
if (!this.displaySending) {
const packed = this.latest!; this.latest = null
const textureFrame = this.latestTextures; this.latestTextures = null
// Compare with the last enqueued image, not the last ACKed one. In an
// A -> B -> A sequence, B may still be in flight when A is requested.
if (!textureFrame && this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
const texturePlan = textureFrame ? this.textures?.plan(this.lastEnqueued, packed, textureFrame, this.nextImageFrameId) : null
let commands: Uint8Array[]
if (texturePlan) {
// Resource commands participate in the same ordered, acknowledged command window as
// the image. Coalescing only happens before planning mutates residency.
commands = [...texturePlan.resourceCommands, texturePlan.payload]
this.nextImageFrameId = texturePlan.nextFid
this.log(`Display textures (${texturePlan.resourceCommands.length} resource commands, cache=${texturePlan.usedBytes}B)`)
} else {
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
const payload = delta ?? protocol.concat(new Uint8Array([CFW_MSG_FULL_FRAME]), protocol.rle4(packed))
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
commands = payload.length <= CFW_MAX_MESSAGE - 16 ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
if (payload.length > CFW_MAX_MESSAGE - 16) for (const _ of commands)
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
commands = [...commands.flatMap(command => drawMessages(command)), new Uint8Array([CFW_MSG_PRESENT])]
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
}
this.displaySending = { packed, commands, offset: 0, pending: 0 }
this.displayFrames.push(this.displaySending); this.lastEnqueued = packed
}
const frame = this.displaySending
// Drain earlier commands before evicting: recovery may replay any unresolved draw/upload.
if (frame.commands[frame.offset][0] === CFW_MSG_EVICT_RESOURCE && this.cfwPending.length) return
const payload = frame.commands[frame.offset++]
frame.pending++; this.displayInFlight++
if (frame.offset >= frame.commands.length) this.displaySending = null
// Writes serialize on L while up to three complete commands await their
// two-lens ACKs. Retire commands in submission order.
void this.requestCfw('left', payload).then(() => {
if (generation !== this.generation) return
frame.pending--; this.displayInFlight--; this.lastHeartbeat = Date.now()
// Retire in submission order even if relayed ACKs arrive out of order.
// A frame counts only after every command has been acknowledged.
while (this.displayFrames.length) {
const completed = this.displayFrames[0]
if (completed.offset < completed.commands.length || completed.pending) break
this.displayFrames.shift(); this.displayed = completed.packed
this.state.frames++; iosBleTraffic.recordDisplayFrame(); this.onState({ ...this.state })
this.log(`Display frame ${this.state.frames} acknowledged (${completed.commands.length} commands)`)
}
this.wake()
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
}
}
private async pump(): Promise<void> {
if (this.pumping || this.state.phase !== 'connected') return
this.pumping = true; const generation = this.generation
try {
const now = Date.now()
// Firmware frees resources when its 90-second framebuffer lease expires.
// After a long iOS suspension, reconnect rather than replaying pending
// draws against unknown memory. Leave a margin for the two arm writes.
if (now - this.lastLease >= 80_000) throw new Error('Display lease lapsed; restarting session')
if (now - this.lastHeartbeat >= 4000) {
const gap = now - this.lastHeartbeat
await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now()
this.log(`Heartbeat acknowledged (gap ${gap}ms)`)
}
if (now - this.lastLease >= 45_000) { await this.lease(true); this.lastLease = Date.now() }
if (now - this.lastSettings >= (this.charging ? 30_000 : 300_000)) {
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Battery query')
this.applySettings(settings); this.lastSettings = Date.now()
}
this.check(generation)
this.fillDisplayWindow(generation)
} catch (error) { if (generation === this.generation) this.fail(error, true) }
finally { if (generation === this.generation) { this.pumping = false; this.schedule(this.canSendDisplay() ? 0 : 1000) } }
}
private reset(): void {
this.notifications.stop()
++this.generation
this.compassSent = null; this.compassWork = Promise.resolve()
++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false
if (this.timer !== null) clearTimeout(this.timer)
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
if (this.ringRetryTimer !== null) clearTimeout(this.ringRetryTimer)
this.ringRetryTimer = null; this.ringConnecting = false; this.ringRetryCount = 0
this.timer = this.retryTimer = null; this.pumping = false
const pending = [...this.pending.values()]; this.pending.clear()
for (const request of pending) { if (request.timer) clearTimeout(request.timer); request.reject(new Error('Connection ended')) }
if (this.cfwTimer !== null) clearTimeout(this.cfwTimer)
this.cfwTimer = null
const custom = this.cfwPending; this.cfwPending = []
for (const item of custom) item.reject(new Error('Connection ended'))
this.cfwTransports.clear()
this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null
this.latestTextures = null; this.textures?.reset()
this.lastEnqueued = null; this.displayFrames = []; this.displaySending = null; this.displayInFlight = 0
}
private closeLinks(): void {
const ids = this.ids; this.ids = {}; this.limits = {}
for (const identifier of Object.values(ids)) this.transport.disconnect(identifier)
this.transport.stopScan(); this.layoutCreated = false; this.state.ring = false
}
private fail(error: unknown, retry: boolean): void {
const message = this.message(error); this.reset(); this.closeLinks()
if (retry && this.wanted && this.addresses && this.retryCount < 5) {
const delay = Math.min(30_000, 2000 * 2 ** this.retryCount++)
this.update('retrying', `${message} Retrying in ${delay / 1000}s…`)
this.retryTimer = setTimeout(() => { this.retryTimer = null; if (this.wanted && this.addresses) void this.start(this.addresses, true) }, delay)
} else { this.wanted = false; this.update('error', message) }
}
stop(): Promise<void> {
if (this.stopping) return this.stopping
this.stopping = this.stopInternal().finally(() => { this.stopping = null })
return this.stopping
}
private async stopInternal(): Promise<void> {
this.wanted = false
this.compassStopping = true
await this.syncCompass()
// Stop mic capture before ending the display session. Late enable ACKs
// cannot leave a microphone streaming after an explicit Disconnect.
if (this.audioListener || this.microphoneEnabled) {
try { await this.setMicrophone(false) } catch (error) { this.log(`Microphone cleanup: ${this.message(error)}`) }
}
if (this.notifications.state !== 'disconnected') {
try { await this.writePackets('right', () => [this.notifications.stopCommand()]) } catch {}
}
const cleanup = this.layoutCreated && this.state.phase === 'connected'
this.update('disconnecting', 'Disconnecting…'); this.reset()
try {
if (cleanup) {
await this.requestCfw('left', new Uint8Array([CFW_MSG_CLEANUP]))
}
} catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) }
finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') }
}
dispose(): void { void this.stop().finally(() => this.off()) }
private message(error: unknown): string { return error instanceof Error ? error.message : String(error) }
}
+390 -123
View File
@@ -17,10 +17,11 @@ import { IosNavigationSensors } from '../native/ios-navigation-sensors'
import { Utils } from '@nativescript/core' import { Utils } from '@nativescript/core'
import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios' import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core' import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
import { iosBluetooth } from '../native/ios-bluetooth'
import { iosVoiceInput } from '../native/ios-voice-input' import { iosVoiceInput } from '../native/ios-voice-input'
import { nightscoutBridge } from '../native/nightscout-bridge' 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 { GlanceHost, type GlanceDisplay } from './glance-host'
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen' import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
import { OsEventTypeList } from './events' import { OsEventTypeList } from './events'
@@ -34,12 +35,9 @@ import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type
import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings' import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings'
import { readPhoneBatteryState } from '../native/phone-battery' import { readPhoneBatteryState } from '../native/phone-battery'
import { iosAppUnavailableReason } from '../apps/ios-availability' import { iosAppUnavailableReason } from '../apps/ios-availability'
import { SurfaceCompositor } from '../graphics/surface-compositor' import { flattenPlanesWithDraws, planesFingerprint, type Plane } from '../graphics/plane'
import { flattenPlanesWithDraws, type Plane } from '../graphics/plane'
import { prepareFrameDraws } from '../graphics/glyph-wire' import { prepareFrameDraws } from '../graphics/glyph-wire'
import { IosTexturePlanner } from '../native/texture-planner.ios'
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image' 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 { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/gestures'
import { noopLayerActions, type LayerActions } from '../ui/layers' import { noopLayerActions, type LayerActions } from '../ui/layers'
import { TextViewerLayer } from '../apps/files/text-viewer' 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 { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state'
import { findSoundEffect, playSoundEffect } from '../ui/sound-effects' 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 { 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 = { private readonly glanceDisplay: GlanceDisplay = {
configureSurface: async (id, options) => { configureSurface: async (id, options) => { await this.display?.configureSurface(id, options) },
this.compositor.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() },
setSurfaceVisible: async (id, visible) => { submitSurfaceFrame: async (id, pixels, rect, fingerprint, paintMs, frameId, draws) => {
this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() await this.display?.submitSurfaceFrame(id, pixels, rect, fingerprint, paintMs, frameId, draws); this.schedulePreviewUpdate()
},
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()
}, },
} }
private readonly glance = new GlanceHost({ private readonly glance = new GlanceHost({
getDisplay: () => this.glanceDisplay, getDisplay: () => this.display ? this.glanceDisplay : null,
getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null, getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null,
canShow: () => this.runtimeNeeded && !this.glassesLocked, canShow: () => this.runtimeNeeded && !this.glassesLocked,
// iOS retains the EvenHub session while the shell sleeps. The board's // iOS retains the EvenHub session while the shell sleeps. The board's
// opaque first frame is ready before we unblank the compositor. // opaque first frame is ready before we unblank the compositor.
ensureSessionActive: async () => { ensureSessionActive: async () => {
this.compositor.setScreenBlanked(false); this.scheduleFrame(); return true await this.display?.setScreenBlanked(false); this.schedulePreviewUpdate(); return true
}, },
onHiddenWhileAsleep: () => { onHiddenWhileAsleep: () => {
if (!shell.isScreenOn()) this.compositor.setScreenBlanked(true) if (!shell.isScreenOn()) void this.display?.setScreenBlanked(true).then(() => this.schedulePreviewUpdate())
this.scheduleFrame() else this.schedulePreviewUpdate()
}, },
onVisibilityChanged: () => this.scheduleFrame(), onVisibilityChanged: () => this.schedulePreviewUpdate(),
appendLog: message => this.logBluetooth(message), 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 clockTimer: ReturnType<typeof setInterval> | null = null
private offSettings: (() => void) | null = null private offSettings: (() => void) | null = null
private lastBrightness: string | null = null private lastBrightness: string | null = null
private active = false private active = false
private runtimeRunning = false private runtimeRunning = false
private clockMinute = -1 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 inputQueue: Promise<void> = Promise.resolve()
private lastLayout = '' private lastLayout = ''
private readonly appHosts = new Map<string, WorkerAppHost>() private readonly appHosts = new Map<string, WorkerAppHost>()
@@ -102,13 +128,12 @@ export class IosPreviewController {
private glassesWorn: boolean | null = null private glassesWorn: boolean | null = null
private glassesLocked = false private glassesLocked = false
private lockEnabled = lockScreenEnabledSetting.get() private lockEnabled = lockScreenEnabledSetting.get()
private session: GlassesSession | null = null
private acknowledgedFrames = 0 private acknowledgedFrames = 0
private logLines: string[] = [] private logLines: string[] = []
private logTimer: ReturnType<typeof setTimeout> | null = null private logTimer: ReturnType<typeof setTimeout> | null = null
private readonly actions: LayerActions = { private readonly actions: LayerActions = {
...noopLayerActions, ...noopLayerActions,
playBuzzerSequence: payload => this.session?.playBuzzerSequence(payload), playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload),
requestRender: () => this.requestShellRender(), requestRender: () => this.requestShellRender(),
disconnect: () => this.disconnect(), disconnect: () => this.disconnect(),
startVoiceCapture: endpointing => this.startVoiceCapture(endpointing), startVoiceCapture: endpointing => this.startVoiceCapture(endpointing),
@@ -130,11 +155,7 @@ export class IosPreviewController {
private readonly onError: (message: string) => void, private readonly onError: (message: string) => void,
private readonly onConnectionState: (state: SessionState) => void = () => {}, private readonly onConnectionState: (state: SessionState) => void = () => {},
private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => 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.ensurePreviewDisplay()
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)
shell.configure({ shell.configure({
actions: this.actions, actions: this.actions,
voiceInputEnabled: true, voiceInputEnabled: true,
@@ -151,7 +172,8 @@ export class IosPreviewController {
onWindowsChanged: () => this.requestShellRender(), onWindowsChanged: () => this.requestShellRender(),
onScreenStateChanged: on => { onScreenStateChanged: on => {
if (on) this.glance.dismiss() 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({ const launcher = createLauncherWindow({
@@ -160,9 +182,9 @@ export class IosPreviewController {
launchApp: id => this.launchApp(id), launchApp: id => this.launchApp(id),
uninstallApp: id => this.uninstallApp(id), uninstallApp: id => this.uninstallApp(id),
submitFrame: planes => this.submit(LAUNCHER_SURFACE_ID, planes), 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.registerWindow(launcher)
shell.wake('window') shell.wake('window')
shell.focusWindow(launcher.windowId) 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)) 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 { resume(): void {
if (this.active) return if (this.active) return
this.active = true this.active = true
this.syncRuntime() this.syncRuntime()
this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable)
this.logBluetooth('Phone foreground') this.logBluetooth('Phone foreground')
if (this.session) this.onConnectionState({ ...this.session.state }) if (this.communicator) this.onConnectionState({ ...this.state })
this.session?.wake()
this.relayout() this.relayout()
shell.foregroundWindow()?.requestRender() shell.foregroundWindow()?.requestRender()
this.requestShellRender() this.requestShellRender()
@@ -216,12 +278,11 @@ export class IosPreviewController {
iosVoiceInput.stopPhoneCapture() iosVoiceInput.stopPhoneCapture()
// The phone preview is hidden; the glasses still own their screen and input. // The phone preview is hidden; the glasses still own their screen and input.
this.syncRuntime() this.syncRuntime()
this.logBluetooth(`Phone background; glasses ${this.session?.state.phase ?? 'disconnected'}`) this.logBluetooth(`Phone background; glasses ${this.state.phase}`)
this.flushBluetoothLog() this.flushBluetoothLog()
this.session?.wake()
} }
private get runtimeNeeded(): boolean { 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 { private syncRuntime(): void {
const running = this.runtimeNeeded const running = this.runtimeNeeded
@@ -231,14 +292,14 @@ export class IosPreviewController {
if (!running) { if (!running) {
void nightscoutBridge.stop().catch(error => this.fail(error)) void nightscoutBridge.stop().catch(error => this.fail(error))
this.glance.dismiss() this.glance.dismiss()
this.compositor.setScreenBlanked(!shell.isScreenOn()) void this.display?.setScreenBlanked(!shell.isScreenOn()).catch(error => this.fail(error))
this.offSettings?.(); this.offSettings = null this.offSettings?.(); this.offSettings = null
for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer) for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer) for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
UIDevice.currentDevice.batteryMonitoringEnabled = false UIDevice.currentDevice.batteryMonitoringEnabled = false
if (this.clockTimer !== null) clearInterval(this.clockTimer) if (this.clockTimer !== null) clearInterval(this.clockTimer)
if (this.renderTimer !== null) clearTimeout(this.renderTimer) if (this.previewTimer !== null) clearTimeout(this.previewTimer)
this.clockTimer = this.renderTimer = null this.clockTimer = this.previewTimer = null
return return
} }
UIDevice.currentDevice.batteryMonitoringEnabled = true UIDevice.currentDevice.batteryMonitoringEnabled = true
@@ -268,12 +329,13 @@ export class IosPreviewController {
this.clockTimer = setInterval(() => this.refreshClock(), 60_000) this.clockTimer = setInterval(() => this.refreshClock(), 60_000)
} }
private pushBrightness(): void { private pushBrightness(): void {
const session = this.session const communicator = this.communicator
if (session?.state.phase !== 'connected') { this.lastBrightness = null; return } if (!communicator || this.state.phase !== 'connected') { this.lastBrightness = null; return }
const value = brightnessSetting.get() const value = brightnessSetting.get()
if (value === this.lastBrightness) return if (value === this.lastBrightness) return
this.lastBrightness = value 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.lastBrightness = null
this.logBluetooth(`Brightness: ${String(error)}`) this.logBluetooth(`Brightness: ${String(error)}`)
}) })
@@ -285,12 +347,13 @@ export class IosPreviewController {
if (!enabled) this.setGlassesLocked(false) if (!enabled) this.setGlassesLocked(false)
else { else {
if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true) if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true)
if (this.session?.state.phase === 'connected') if (this.communicator && this.state.phase === 'connected')
void this.session.enableWearDetectionAndRequestState().catch(error => this.fail(error)) void this.communicator.enableWearDetectionAndRequestState().catch(error => this.fail(error))
} }
} }
private handlePhoneLockState(locked: boolean): void { private handlePhoneLockState(locked: boolean): void {
this.phoneLocked = locked this.phoneLocked = locked
this.communicator?.onPhoneLockSignal()
if (!locked) this.setGlassesLocked(false) if (!locked) this.setGlassesLocked(false)
else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true) else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true)
} }
@@ -304,17 +367,21 @@ export class IosPreviewController {
} }
private setGlassesLocked(locked: boolean): void { private setGlassesLocked(locked: boolean): void {
if (locked === this.glassesLocked) return 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 this.glassesLocked = locked
if (locked) { if (locked) {
this.glance.dismiss() this.glance.dismiss()
iosVoiceInput.handleSessionEnded('Glasses locked. Unlock your phone to start voice input again.') 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.logBluetooth(`Glasses ${locked ? 'locked' : 'unlocked'}`)
this.requestShellRender() this.requestShellRender()
} }
@@ -322,45 +389,90 @@ export class IosPreviewController {
const minute = Math.floor(Date.now() / 60_000) const minute = Math.floor(Date.now() / 60_000)
if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() } if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() }
} }
private configureWindow(window: ShellWindow): void { private async configureWindow(window: ShellWindow, target: DisplayTarget | null = this.display): Promise<void> {
this.compositor.configureSurface(window.surfaceId, { if (!target) return
await target.configureSurface(window.surfaceId, {
...appViewportRect(window.heightMode, window.appId), zOrder: 0, transparency: 'opaque', ...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 { private relayout(): void {
const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";") const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";")
if (layout === this.lastLayout) return if (layout === this.lastLayout) return
this.lastLayout = layout this.lastLayout = layout
for (const window of shell.getWindows()) { for (const window of shell.getWindows()) {
this.configureWindow(window) void this.configureWindow(window).catch(error => this.fail(error))
window.relayout?.() window.relayout?.()
} }
} }
/** Submit a painted frame for an in-process window (e.g. the launcher). */
private async submit(id: string, planes: Plane[]): Promise<void> { 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) const { image, draws } = flattenPlanesWithDraws(planes)
this.compositor.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, prepareFrameDraws(draws)) await display.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height },
this.scheduleFrame() fingerprint, -1, 0, prepareFrameDraws(draws))
this.schedulePreviewUpdate()
} }
private requestShellRender(): void { this.shellDirty = true; this.scheduleFrame() } /** Worker windows ship baked pixels; every submit is new content to the compositor. */
private scheduleFrame(): void { private async submitWorkerPixels(id: string, pixels: Uint8Array, width: number, height: number, draws: ArrayBuffer | null): Promise<void> {
if (!this.runtimeNeeded || this.renderTimer !== null) return await this.displayReady
// Coalesce window/chrome updates into one glasses frame, capped at 30 fps. const display = this.display
this.renderTimer = setTimeout(() => { if (!display) return
this.renderTimer = null await display.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, `${id}#${++this.nextWorkerFrame}`, -1, 0, draws)
if (!this.runtimeNeeded) return 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 { try {
if (this.shellDirty) { do {
this.shellDirty = false this.shellRenderQueued = false
this.compositor.setShellScene(shell.paintScene()) await this.renderShell()
} } while (this.shellRenderQueued)
const { pixels, textures } = this.compositor.compositeFrame() } catch (error) { this.fail(error) }
this.session?.setFrame(pixels, textures) finally { this.shellRenderInProgress = false }
if (this.active) { })()
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green') }
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard' private async renderShell(): Promise<void> {
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`) 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) } } catch (error) { this.fail(error) }
}, 33) }, 33)
} }
@@ -469,7 +581,7 @@ export class IosPreviewController {
launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create), launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create),
ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create), ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create),
submitWindowFrame: (id, planes) => this.submit(id, planes), 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}`), requestShellRender: () => this.requestShellRender(), appendLog: message => console.log(`[ios-app] ${message}`),
setTextEditorHost: () => {}, setTextEditorHost: () => {},
} }
@@ -484,13 +596,17 @@ export class IosPreviewController {
let surfaceReady = false let surfaceReady = false
const app = create({ const app = create({
actions: this.actions, submitFrame: planes => surfaceReady ? this.submit(surfaceId, planes) : Promise.resolve(), actions: this.actions, submitFrame: planes => surfaceReady ? this.submit(surfaceId, planes) : Promise.resolve(),
setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(surfaceId, visible); this.scheduleFrame() }, setSurfaceVisible: visible => this.setWindowSurfaceVisible(surfaceId, visible),
removeSurface: () => { surfaceReady = false; this.compositor.removeSurface(surfaceId); this.scheduleFrame() }, removeSurface: () => { surfaceReady = false; this.removeWindowSurface(surfaceId) },
reconfigureSurface: () => { const window = shell.getWindows().find(w => w.windowId === windowId); if (window) this.configureWindow(window); this.requestShellRender() }, 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) }, onClosed: () => { this.inProcessApps.delete(windowId) },
}) })
this.inProcessApps.set(windowId, app) this.inProcessApps.set(windowId, app)
this.configureWindow(app.window) await this.configureWindow(app.window)
surfaceReady = true surfaceReady = true
shell.registerWindow(app.window); shell.focusWindow(windowId) shell.registerWindow(app.window); shell.focusWindow(windowId)
app.requestRender(); this.requestShellRender() app.requestRender(); this.requestShellRender()
@@ -507,13 +623,15 @@ export class IosPreviewController {
playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload), playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload),
openUrl: url => { void Utils.openUrl(url) }, openUrl: url => { void Utils.openUrl(url) },
configureSurface: async (id, visible, heightMode) => { configureSurface: async (id, visible, heightMode) => {
this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' }) const display = this.display
this.compositor.setSurfaceVisible(id, visible) 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() }, setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
removeSurface: id => { this.compositor.removeSurface(id); this.scheduleFrame() }, removeSurface: id => this.removeWindowSurface(id),
submitPixels: (id, pixels, width, height, draws) => { 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(), requestShellRender: () => this.requestShellRender(),
openSettings: section => { void this.launchApp('settings', { section }) }, openSettings: section => { void this.launchApp('settings', { section }) },
@@ -529,45 +647,193 @@ export class IosPreviewController {
displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length]) displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length])
} }
get displayModeLabel(): string { return displayModeLabel(displayModeSetting.get()) } 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 { get connectionDetails(): string {
const state = this.connectionState 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.' 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> { 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) const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
if (error) { this.onError(error); return } if (error) { this.onError(error); return }
if (!this.session) { this.connecting = true
this.session = new GlassesSession(iosBluetooth(), state => { this.sessionCreated = true
if (state.phase !== "connected") resetRingInputFilter() try {
this.pushBrightness() this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
this.maybePlayWelcomeSound(state) this.update('connecting', 'Finding configured devices…')
if (state.phase !== 'connected' && state.phase !== 'connecting') this.glassesWorn = null let identifiers
if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded() try { identifiers = await resolveIosPeripherals(addresses) }
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging }) catch (failure) {
this.syncRuntime() this.update('error', failure instanceof Error ? failure.message : String(failure))
if (this.active) this.onConnectionState(state) return
this.requestShellRender() }
}, input => { if (this.communicator) await this.closeCommunicator()
this.inputQueue = this.inputQueue.then(async () => { const communicator = new FaceclawCommunicatorBridge(identifiers)
if (this.session?.state.phase !== 'connected') return this.teardownPreviewDisplay()
await this.receiveInput(rawInputEventToInputEvent(input), this.communicator = communicator
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT) this.wearDetectionRequested = false
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`) this.bindCommunicator(communicator)
}).catch(error => this.fail(error)) this.displayReady = this.registerSurfaces(communicator)
}, message => this.logBluetooth(message), () => { await this.displayReady
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true) this.update('connecting', 'Connecting to the glasses…')
this.refreshClock() this.startedCommunicators.add(communicator)
}, receiveCompassEvent, wearing => this.handleWearState(wearing), (key, popup) => { await communicator.start()
iosNotificationsChanged(key, popup); this.requestShellRender() for (const window of shell.getWindows()) window.requestRender()
}, new IosTexturePlanner()) this.requestShellRender()
bindIosNotifications(this.session.notifications) } catch (failure) {
bindCompassSession(this.session) 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 { private maybePlayWelcomeSound(state: SessionState): void {
const firstNewFrame = state.frames > this.acknowledgedFrames const firstNewFrame = state.frames > this.acknowledgedFrames
this.acknowledgedFrames = state.frames this.acknowledgedFrames = state.frames
@@ -582,17 +848,18 @@ export class IosPreviewController {
startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() } startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() }
private async prepareVoiceCapture(): Promise<boolean> { private async prepareVoiceCapture(): Promise<boolean> {
if (this.glassesLocked) return false if (this.glassesLocked) return false
const usePhoneMic = this.session?.state.phase !== 'connected' const usePhoneMic = this.state.phase !== 'connected'
if (usePhoneMic && !this.active) return false if (usePhoneMic && !this.active) return false
const ready = await iosVoiceInput.prepare(this.active, usePhoneMic) const ready = await iosVoiceInput.prepare(this.active, usePhoneMic)
if (!ready && this.active) this.onError(iosVoiceInput.statusText) 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> { private async startVoiceCapture(endpointing = false): Promise<void> {
if (this.glassesLocked) return if (this.glassesLocked) return
const log = (message: string) => this.logBluetooth(message) const log = (message: string) => this.logBluetooth(message)
if (this.session?.state.phase === 'connected') { const communicator = this.communicator
await iosVoiceInput.startGlassesCapture(this.session, log, endpointing) if (communicator && this.state.phase === 'connected') {
await iosVoiceInput.startGlassesCapture(communicator, log, endpointing)
} else if (this.active) { } else if (this.active) {
// Recheck microphone permission if the glasses disconnected after prepare. // Recheck microphone permission if the glasses disconnected after prepare.
if (!await iosVoiceInput.prepare(this.active, true) || !this.active || this.glassesLocked) return if (!await iosVoiceInput.prepare(this.active, true) || !this.active || this.glassesLocked) return
-8
View File
@@ -1,8 +0,0 @@
/** Platform-owned snapshot of the same composite as the submitted pixels. */
export type TextureFrame = { readonly native: object }
export type TexturePlan = { resourceCommands: Uint8Array[]; payload: Uint8Array; nextFid: number; usedBytes: number }
/** Sessions own residency; frame snapshots may be coalesced without changing it. */
export interface SessionTexturePlanner {
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null
reset(): void
}
-15
View File
@@ -1,15 +0,0 @@
import { bytes, concat, integer, readBytes, readInteger, type ProtocolMessage } from './ble-protocol'
// Matches BleProtocol.buildSetWearDetection/buildFaceclawWakeControl on Android.
export const enableWearDetection = (magic: number): Uint8Array => concat(
integer(1, 1), integer(2, magic), bytes(3, bytes(5, integer(1, 1))))
export const queryWearState = (): Uint8Array => concat(
integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, 7, 0, 0])))
export function decodeWearState(message: ProtocolMessage): boolean | null {
if (message.sid !== 0x10 || message.command !== 3) return null
const event = readBytes(message.payload, 5)
if (!event || readInteger(event, 1, -1) !== 1) return null
const state = readInteger(event, 2, -1)
return state === 0 ? false : state === 1 ? true : null
}
-5
View File
@@ -1,5 +1,4 @@
import { fromData, toData } from '../native/kotlin-data' import { fromData, toData } from '../native/kotlin-data'
import type { TextureFrame } from '../g2/texture-planner'
import type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' import type { SurfaceConfiguration, SurfaceRect } from './surface-compositor'
export type { SurfaceConfiguration, SurfaceRect } from './surface-compositor' export type { SurfaceConfiguration, SurfaceRect } from './surface-compositor'
declare const FaceclawKitIosSurfaceCompositor: any declare const FaceclawKitIosSurfaceCompositor: any
@@ -35,8 +34,4 @@ export class SurfaceCompositor {
} }
setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) } setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) }
composite(): Uint8Array { return fromData(this.native.composite()) } composite(): Uint8Array { return fromData(this.native.composite()) }
compositeFrame(): { pixels: Uint8Array; textures: TextureFrame } {
const native = this.native.compositeFrame()
return { pixels: fromData(native.pixels), textures: { native } }
}
} }
-2
View File
@@ -1,6 +1,5 @@
import { presentationRecords, readPresentation, paintPresentation, type Selection } from "./presentation-wire" import { presentationRecords, readPresentation, paintPresentation, type Selection } from "./presentation-wire"
/** Platform-independent 8bpp surface composition for the local display. */ /** Platform-independent 8bpp surface composition for the local display. */
import type { TextureFrame } from '../g2/texture-planner'
export type SurfaceRect = { x: number; y: number; width: number; height: number } export type SurfaceRect = { x: number; y: number; width: number; height: number }
export type SurfaceConfiguration = SurfaceRect & { export type SurfaceConfiguration = SurfaceRect & {
zOrder: number zOrder: number
@@ -55,7 +54,6 @@ export class SurfaceCompositor {
surface.pixels.set(pixels.subarray(source, source + right - left), y * surface.width + left) surface.pixels.set(pixels.subarray(source, source + right - left), y * surface.width + left)
} }
} }
compositeFrame(): { pixels: Uint8Array; textures?: TextureFrame } { return { pixels: this.composite() } }
composite(): Uint8Array { composite(): Uint8Array {
const output = new Uint8Array(this.width * this.height) const output = new Uint8Array(this.width * this.height)
if (this.blanked) return output if (this.blanked) return output
+14 -2
View File
@@ -1,3 +1,15 @@
import { iosBleTraffic } from '../g2/ble-traffic-counters'
export type { BleTrafficSample } from './ble-traffic' export type { BleTrafficSample } from './ble-traffic'
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } { return iosBleTraffic.sample() } declare const FaceclawKitIosBleTraffic: any
// The Kotlin counters reset on every sample; accumulate here so callers see
// running totals like FaceclawBleManager.sampleOutboundTraffic on Android.
// CoreBluetooth has no per-message boundary; messages counts write calls.
const totals = { messages: 0, bytes: 0, frames: 0 }
export function sampleBleTraffic(): { messages: number; bytes: number; frames: number } {
const sample = FaceclawKitIosBleTraffic.shared.sample()
const written = Number(sample.getIndex(0)), frames = Number(sample.getIndex(2))
totals.bytes += written
totals.frames += frames
if (written > 0) totals.messages++
return { ...totals }
}
+79 -58
View File
@@ -1,68 +1,89 @@
import { StockConnection, StockTimeout } from '../g2/stock-connection'
import * as p from '../g2/ble-protocol'
import { iosBluetooth } from './ios-bluetooth' import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { DeviceInfo, DeviceInfoState } from './device-info-probe' import type { DeviceInfo, DeviceInfoState } from './device-info-probe'
export type { DeviceInfo, DeviceInfoState } from './device-info-probe' export type { DeviceInfo, DeviceInfoState } from './device-info-probe'
declare const FaceclawKitIosDeviceInfoProbe: any, FaceclawKitFaceclawDeviceInfoProbeListener: any
/**
* iOS twin of the Android DeviceInfoProbe wrapper: the shared Kotlin DeviceInfoProbeFlow
* runs over a CoreBluetooth central (FaceclawKitIosDeviceInfoProbe); TypeScript only maps
* the stored MAC addresses to peripheral identifiers first. Callbacks arrive on the main
* queue and are fanned out asynchronously like the Android wrapper does.
*/
export class DeviceInfoProbe { export class DeviceInfoProbe {
private logs = new Set<(line: string) => void>() private probe: any = null
private states = new Set<(state: DeviceInfoState, detail: string) => void>() private listenerProxy: object | null = null
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) }) private readonly logListeners = new Set<(line: string) => void>()
constructor(private right: string, private left = '') {} private readonly stateListeners = new Set<(state: DeviceInfoState, detail: string) => void>()
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) } private settled = false
onStateChange(fn: (state: DeviceInfoState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) } private cancelled = false
async run(): Promise<DeviceInfo> { private resolveFn: ((info: DeviceInfo) => void) | null = null
const snapshots = new Map<string, DeviceInfo>() private rejectFn: ((error: Error) => void) | null = null
const parse = (message: p.ProtocolMessage): DeviceInfo => {
const values = p.readBytes(message.payload, 4) constructor(private readonly rightAddress: string, private readonly leftAddress = '') {}
return { leftVersion: values ? p.readString(values, 5) : '', rightVersion: values ? p.readString(values, 6) : '', extension: p.readString(message.payload, 100) }
} onLog(listener: (line: string) => void): () => void {
const hasVersion = (info: DeviceInfo) => !!(info.leftVersion || info.rightVersion || info.extension) this.logListeners.add(listener)
// Some stock builds push settings using their own magic instead of the return () => this.logListeners.delete(listener)
// query's magic. Keep a valid snapshot as the Android probe does. }
this.link.onMessage((role, message) => {
if (message.sid === p.SID.settings) { const info = parse(message); if (hasVersion(info)) snapshots.set(role, info) } onStateChange(listener: (state: DeviceInfoState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
run(): Promise<DeviceInfo> {
return new Promise<DeviceInfo>((resolve, reject) => {
this.resolveFn = resolve
this.rejectFn = reject
void this.start()
}) })
}
private async start(): Promise<void> {
let identifiers: Record<string, string>
try { try {
await this.link.resolve({ right: this.right, left: this.left, ring: '' }) this.emit(this.stateListeners, 'connecting', 'right')
for (const role of ['right', ...(this.left ? ['left'] : [])]) { identifiers = await iosBluetooth().resolveDevices({ right: this.rightAddress, left: this.leftAddress, ring: '' })
await this.bringUp(role) } catch (error) {
} this.settle(error instanceof Error ? error : new Error(String(error)), null)
let lastError: unknown return
for (const role of ['right', ...(this.left ? ['left'] : [])]) {
for (const fn of this.states) fn('querying', role)
try {
if (!this.link.isConnected(role)) await this.bringUp(role)
await this.link.request(role, p.SID.launch, p.prelude, 3500, 156)
for (let attempt = 0; attempt < 2; attempt++) {
try {
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
const info = parse(message)
if (hasVersion(info)) return info
} catch (error) { this.link.check(); lastError = error }
if (snapshots.has(role)) return snapshots.get(role)!
}
} catch (error) { this.link.check(); lastError = error }
}
throw lastError ?? new Error("Connected, but couldn't read a firmware version.")
} finally { this.close() }
}
private async bringUp(role: string): Promise<void> {
for (let attempt = 0; ; attempt++) {
try {
for (const fn of this.states) fn('connecting', role)
await this.link.connect(role)
for (const fn of this.states) fn('authenticating', role)
// Version reads remain available on older stock builds without an auth ACK.
try { await this.link.authenticate(role) } catch (error) { if (!(error instanceof StockTimeout)) throw error }
return
} catch (error) {
this.link.check(); this.link.disconnect(role)
if (attempt === 2) throw error
await new Promise(resolve => setTimeout(resolve, 1500)); this.link.check()
}
} }
if (this.cancelled) { this.settle(new Error('Cancelled.'), null); return }
this.probe = FaceclawKitIosDeviceInfoProbe.alloc().initWithRightAddressLeftAddress(identifiers.right ?? '', identifiers.left ?? '')
this.listenerProxy = kotlinListener(FaceclawKitFaceclawDeviceInfoProbeListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onStateStateDetail: (state: string, detail: string) =>
this.emit(this.stateListeners, String(state) as DeviceInfoState, String(detail ?? '')),
onResultLeftVersionRightVersionExtension: (leftVersion: string, rightVersion: string, extension: string) =>
this.settle(null, { leftVersion: String(leftVersion ?? ''), rightVersion: String(rightVersion ?? ''), extension: String(extension ?? '') }),
onErrorMessage: (message: string) => this.settle(new Error(String(message ?? '')), null),
})
this.probe.setListenerListener(this.listenerProxy)
this.probe.start()
}
cancel(): void {
this.cancelled = true
try { this.probe?.cancel() } catch { /* ignore */ }
}
close(): void {
this.cancelled = true
try { this.probe?.close() } catch { /* ignore */ }
}
private settle(error: Error | null, info: DeviceInfo | null): void {
if (this.settled) return
this.settled = true
this.close()
if (error) this.rejectFn?.(error)
else if (info) this.resolveFn?.(info)
}
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const snapshot = Array.from(listeners)
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
} }
cancel(): void { this.link.close() }
close(): void { this.link.close() }
} }
+677
View File
@@ -0,0 +1,677 @@
import type { RingInput } from "../g2/ring-input";
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { type FirmwareInfo } from "../g2/firmware-compat";
import { type CompassEvent } from "./compass-types";
import { fromData, toData } from "./kotlin-data";
import { previewPixels } from "./ios-graphics";
import { iosBluetooth } from "./ios-bluetooth";
export type { FirmwareInfo };
// Kotlin/Native facades exported by FaceclawKit (see native/kotlin/shared/src/iosMain/.../ble).
declare const FaceclawKitIosGlassesSession: any;
declare const FaceclawKitIosProtocolPlatform: any;
declare const FaceclawKitFaceclawBleCommunicatorListener: any;
declare const FaceclawKitIosAncsListener: any;
declare const FaceclawKitIosAudioPacketListener: any;
declare const FaceclawKitFaceclawCompassListener: any;
export type CommunicatorPhase =
| "disconnected"
| "connecting"
| "connected"
| "charging"
| "retrying"
| "unpaired"
| "disconnecting";
export type CommunicatorState = {
phase: CommunicatorPhase;
status: string;
};
export type HeadsetBatteryState = {
battery: number;
chargingStatus: number;
ringBattery?: number;
ringChargingStatus?: number;
};
export type FrameMetrics = {
paintMs: number;
transmitMs: number;
tileCount: number;
};
/** See the Android twin: compositor surface configuration in screen pixels. */
export type SurfaceOptions = {
x: number;
y: number;
width: number;
height: number;
zOrder: number;
transparency: "opaque" | "color-key";
};
export type RawInputEvent = { ringInput?: RingInput } & (
| { kind: "list-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "text-click"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "sys-event"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "even-ai"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "watch-gesture"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
| { kind: "display-wake"; containerName: string; eventType: number; eventSource: number; systemExitReasonCode: number; frameId: number }
);
function nonNegativeNumber(value: number): number {
const numeric = Number(value);
return Number.isFinite(numeric) ? Math.max(0, numeric) : 0;
}
/** A 0..1 brightness factor as the compositor's 0..256 fixed-point form. */
export function dimFactor256(factor: number): number {
return Math.round(Math.max(0, Math.min(1, factor)) * 256);
}
function timestamp(): string {
const now = new Date();
const pad = (value: number, width = 2) => String(value).padStart(width, "0");
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`;
}
export type PeripheralIdentifiers = { right: string; left: string; ring: string };
/**
* Map the configured MAC addresses to CoreBluetooth identifiers through the scan
* layer (app/native/ios-bluetooth.ts): the mapping a previous scan persisted, else a
* fresh scan that classifies Even advertisements. The scan layer also owns Bluetooth
* readiness (creating the central triggers the permission prompt on first use).
*/
export async function resolveIosPeripherals(addresses: { right: string; left: string; ring?: string }): Promise<PeripheralIdentifiers> {
const resolved = await iosBluetooth().resolveDevices({ right: addresses.right, left: addresses.left, ring: addresses.ring ?? "" });
return {
right: String(resolved.right ?? "").toUpperCase(),
left: String(resolved.left ?? "").toUpperCase(),
ring: String(resolved.ring ?? "").toUpperCase(),
};
}
/** Bluetooth powered on and authorized (prompts for permission the first time); rejects with the user-facing reason. */
export function ensureIosBluetoothReady(): Promise<void> {
return iosBluetooth().ensureReady();
}
@NativeClass()
class SessionListener extends NSObject {
static ObjCProtocols = [FaceclawKitFaceclawBleCommunicatorListener];
bridge!: FaceclawCommunicatorBridge;
onStateChangePhaseStatus(phase: string, status: string): void { this.bridge.handleStateChange(String(phase ?? ""), String(status ?? "")); }
onRingEventKindContainerNameEventTypeEventSourceSystemExitReasonCodeFrameIdRingTickRingTypeRingAuxRingSpeed(
kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number,
): void {
this.bridge.handleRingEvent(String(kind ?? ""), String(containerName ?? ""), Number(eventType), Number(eventSource),
Number(systemExitReasonCode), Number(frameId), Number(ringTick), Number(ringType), Number(ringAux), Number(ringSpeed));
}
onBatteryStateHeadsetBatteryHeadsetChargingRingBatteryRingCharging(headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number): void {
this.bridge.handleBatteryState(Number(headsetBattery), Number(headsetCharging), Number(ringBattery), Number(ringCharging));
}
onSilentModeSilent(silent: boolean): void { this.bridge.handleSilentMode(Boolean(silent)); }
onWearStateWearing(wearing: boolean): void { this.bridge.handleWearState(Boolean(wearing)); }
onPhoneLockStateLocked(locked: boolean): void { this.bridge.handlePhoneLockState(Boolean(locked)); }
onEvenAppConflictMessage(message: string): void { this.bridge.handleEvenAppConflict(String(message ?? "")); }
onFrameMetricsPaintMsTransmitMsTileCount(paintMs: number, transmitMs: number, tileCount: number): void {
this.bridge.handleFrameMetrics(Number(paintMs), Number(transmitMs), Number(tileCount));
}
onFrameFinishedFrameIdOutcome(frameId: number, outcome: string): void { this.bridge.handleFrameFinished(Number(frameId), String(outcome ?? "")); }
onFirmwareInfoLeftVersionRightVersionExtension(leftVersion: string, rightVersion: string, extension: string): void {
this.bridge.handleFirmwareInfo(String(leftVersion ?? ""), String(rightVersion ?? ""), String(extension ?? ""));
}
}
@NativeClass()
class AncsListener extends NSObject {
static ObjCProtocols = [FaceclawKitIosAncsListener];
bridge!: FaceclawCommunicatorBridge;
onAncsAuthorizationAuthorized(authorized: boolean): void { this.bridge.handleAncsAuthorization(Boolean(authorized)); }
onRelayFrameData(data: NSData): void { this.bridge.handleAncsRelayFrame(fromData(data)); }
}
@NativeClass()
class AudioPacketListener extends NSObject {
static ObjCProtocols = [FaceclawKitIosAudioPacketListener];
handler!: (packet: Uint8Array, arm: string, arrivalMs: number) => void;
onAudioPacketDataArmArrivalMs(data: NSData, arm: string, arrivalMs: number): void {
this.handler(fromData(data), String(arm ?? ""), Number(arrivalMs));
}
}
@NativeClass()
class CompassListener extends NSObject {
static ObjCProtocols = [FaceclawKitFaceclawCompassListener];
handler!: (event: CompassEvent) => void;
onCompassEventCommandHeadingDegreesMagneticAccuracyMagneticAnomaliesOrientationSourceDiagnosticFlagsSampleTimeMs(
command: number, headingDegrees: number, magneticAccuracy: number, magneticAnomalies: number,
orientationSource: number, diagnosticFlags: number, sampleTimeMs: number,
): void {
this.handler({
command: Number(command), headingDegrees: Number(headingDegrees),
diagnostics: diagnosticFlags >= 0 ? {
magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies),
orientationSource: Number(orientationSource), flags: Number(diagnosticFlags), sampleTimeMs: Number(sampleTimeMs),
} : undefined,
});
}
}
/**
* The iOS twin of the Android communicator bridge: the same public API over
* the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth
* underneath). Addresses are CoreBluetooth identifiers (resolveIosPeripherals
* maps the configured MACs). Every Kotlin callback already arrives on the
* main queue; listener fan-out is still deferred with setTimeout like on
* Android so callers never re-enter the bridge from a Kotlin frame.
*/
export class FaceclawCommunicatorBridge {
private readonly communicator: any;
private readonly listenerProxy: SessionListener;
private ancsProxy: AncsListener | null = null;
private nativeCallQueue: Promise<void> = Promise.resolve();
private queuedNativeCalls = 0;
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
private readonly finishedFrameOutcomes = new Map<number, string>();
private readonly frameFinishedWaiters = new Map<number, Set<(outcome: string) => void>>();
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>();
private readonly silentModeListeners = new Set<(silent: boolean) => void>();
private readonly wearStateListeners = new Set<(wearing: boolean) => void>();
private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>();
private latestWearState: boolean | null = null;
private latestPhoneLockState: boolean | null = null;
private readonly evenAppConflictListeners = new Set<(message: string) => void>();
private readonly frameMetricsListeners = new Set<(metrics: FrameMetrics) => void>();
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
private readonly ancsAuthorizationListeners = new Set<(authorized: boolean) => void>();
private readonly ancsRelayListeners = new Set<(frame: Uint8Array) => void>();
private audioListener: AudioPacketListener | null = null;
private microphoneWork: Promise<void> = Promise.resolve();
private microphoneToken = 0;
constructor(addresses: { right: string; left: string; ring?: string }) {
this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress(
String(addresses.right ?? "").toUpperCase(),
String(addresses.left ?? "").toUpperCase(),
String(addresses.ring ?? "").toUpperCase(),
);
this.listenerProxy = SessionListener.new() as SessionListener;
this.listenerProxy.bridge = this;
this.communicator.setListenerListener(this.listenerProxy);
}
// ----- Kotlin listener entry points (main queue) -----------------------------------
/** @internal */
handleStateChange(phase: string, status: string): void {
this.emitAsync(this.stateListeners, { phase: phase as CommunicatorPhase, status });
}
/** @internal */
handleRingEvent(kind: string, containerName: string, eventType: number, eventSource: number, systemExitReasonCode: number,
frameId: number, ringTick: number, ringType: number, ringAux: number, ringSpeed: number): void {
const event = {
kind: kind as RawInputEvent["kind"], containerName, eventType, eventSource, systemExitReasonCode, frameId,
ringInput: ringTick >= 0 ? { tick: ringTick, type: ringType, aux: ringAux, speed: ringSpeed } : undefined,
} as RawInputEvent;
this.emitAsync(this.ringListeners, event);
}
/** @internal */
handleBatteryState(battery: number, chargingStatus: number, ringBattery: number, ringChargingStatus: number): void {
this.emitAsync(this.batteryListeners, { battery, chargingStatus, ringBattery, ringChargingStatus });
}
/** @internal */
handleSilentMode(silent: boolean): void { this.emitAsync(this.silentModeListeners, silent); }
/** @internal */
handleWearState(wearing: boolean): void {
this.latestWearState = wearing;
this.emitAsync(this.wearStateListeners, wearing);
}
/** @internal */
handlePhoneLockState(locked: boolean): void {
this.latestPhoneLockState = locked;
this.emitAsync(this.phoneLockStateListeners, locked);
}
/** @internal */
handleEvenAppConflict(message: string): void { this.emitAsync(this.evenAppConflictListeners, message); }
/** @internal */
handleFrameMetrics(paintMs: number, transmitMs: number, tileCount: number): void {
const metrics = { paintMs: nonNegativeNumber(paintMs), transmitMs: nonNegativeNumber(transmitMs), tileCount: nonNegativeNumber(tileCount) };
const waiters = Array.from(this.frameMetricWaiters);
this.frameMetricWaiters.clear();
for (const waiter of waiters) setTimeout(() => waiter(metrics), 0);
this.emitAsync(this.frameMetricsListeners, metrics);
}
/** @internal */
handleFrameFinished(frameId: number, outcome: string): void { this.recordFrameFinished(frameId, outcome); }
/** @internal */
handleFirmwareInfo(leftVersion: string, rightVersion: string, extension: string): void {
this.emitAsync(this.firmwareInfoListeners, { leftVersion, rightVersion, extension });
}
/** @internal */
handleAncsAuthorization(authorized: boolean): void { this.emitAsync(this.ancsAuthorizationListeners, authorized); }
/** @internal */
handleAncsRelayFrame(frame: Uint8Array): void { this.emitAsync(this.ancsRelayListeners, frame); }
private emitAsync<T>(listeners: Set<(value: T) => void>, value: T): void {
const snapshot = Array.from(listeners);
setTimeout(() => {
for (const listener of snapshot) listener(value);
}, 0);
}
/** Same FIFO as the Android bridge: native calls stay ordered; the fast path skips a task hop when idle. */
private enqueueNativeCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
if (inlineWhenIdle && this.queuedNativeCalls === 0) {
try {
return Promise.resolve(operation());
} catch (error) {
return Promise.reject(error);
}
}
this.queuedNativeCalls++;
const run = () =>
new Promise<T>((resolve, reject) => {
setTimeout(() => {
this.queuedNativeCalls--;
try {
resolve(operation());
} catch (error) {
reject(error);
}
}, 0);
});
const result = this.nativeCallQueue.then(run, run);
this.nativeCallQueue = result.then(() => undefined, () => undefined);
return result;
}
// ----- listeners -------------------------------------------------------------------
onStateChange(listener: (state: CommunicatorState) => void): () => void {
this.stateListeners.add(listener);
return () => this.stateListeners.delete(listener);
}
onRingEvent(listener: (event: RawInputEvent) => void): () => void {
this.ringListeners.add(listener);
return () => this.ringListeners.delete(listener);
}
onBatteryState(listener: (state: HeadsetBatteryState) => void): () => void {
this.batteryListeners.add(listener);
return () => this.batteryListeners.delete(listener);
}
onSilentMode(listener: (silent: boolean) => void): () => void {
this.silentModeListeners.add(listener);
return () => this.silentModeListeners.delete(listener);
}
onWearState(listener: (wearing: boolean) => void): () => void {
this.wearStateListeners.add(listener);
if (this.latestWearState !== null) {
const wearing = this.latestWearState;
setTimeout(() => { if (this.wearStateListeners.has(listener)) listener(wearing); }, 0);
}
return () => this.wearStateListeners.delete(listener);
}
onPhoneLockState(listener: (locked: boolean) => void): () => void {
this.phoneLockStateListeners.add(listener);
if (this.latestPhoneLockState !== null) {
const locked = this.latestPhoneLockState;
setTimeout(() => { if (this.phoneLockStateListeners.has(listener)) listener(locked); }, 0);
}
return () => this.phoneLockStateListeners.delete(listener);
}
onEvenAppConflict(listener: (message: string) => void): () => void {
this.evenAppConflictListeners.add(listener);
return () => this.evenAppConflictListeners.delete(listener);
}
onFrameMetrics(listener: (metrics: FrameMetrics) => void): () => void {
this.frameMetricsListeners.add(listener);
return () => this.frameMetricsListeners.delete(listener);
}
onFirmwareInfo(listener: (info: FirmwareInfo) => void): () => void {
this.firmwareInfoListeners.add(listener);
return () => this.firmwareInfoListeners.delete(listener);
}
getNativeCommunicator(): any {
return this.communicator;
}
private recordFrameFinished(frameId: number, outcome: string): void {
if (!Number.isFinite(frameId) || frameId <= 0) return;
this.finishedFrameOutcomes.set(frameId, outcome);
while (this.finishedFrameOutcomes.size > 128) {
const oldest = this.finishedFrameOutcomes.keys().next().value;
if (oldest === undefined) break;
this.finishedFrameOutcomes.delete(oldest);
}
const waiters = this.frameFinishedWaiters.get(frameId);
if (waiters) {
this.frameFinishedWaiters.delete(frameId);
for (const waiter of waiters) setTimeout(() => waiter(outcome), 0);
}
}
waitForFrameFinished(frameId: number, timeoutMs: number): Promise<string | null> {
const known = this.finishedFrameOutcomes.get(frameId);
if (known !== undefined) return Promise.resolve(known);
if (frameId <= 0) return Promise.resolve(null);
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
return new Promise((resolve) => {
let settled = false;
const onFinished = (outcome: string) => {
if (settled) return;
settled = true;
clearTimeout(timeoutHandle);
resolve(outcome);
};
let waiters = this.frameFinishedWaiters.get(frameId);
if (!waiters) {
waiters = new Set();
this.frameFinishedWaiters.set(frameId, waiters);
}
waiters.add(onFinished);
const timeoutHandle = setTimeout(() => {
if (settled) return;
settled = true;
const pending = this.frameFinishedWaiters.get(frameId);
if (pending) {
pending.delete(onFinished);
if (pending.size === 0) this.frameFinishedWaiters.delete(frameId);
}
resolve(null);
}, delayMs);
});
}
waitForNextFrameMetrics(timeoutMs: number): Promise<FrameMetrics | null> {
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
return new Promise((resolve) => {
let settled = false;
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
const complete = (metrics: FrameMetrics | null) => {
if (settled) return;
settled = true;
if (timeoutHandle !== null) clearTimeout(timeoutHandle);
this.frameMetricWaiters.delete(onMetrics);
resolve(metrics);
};
const onMetrics = (metrics: FrameMetrics) => complete(metrics);
this.frameMetricWaiters.add(onMetrics);
timeoutHandle = setTimeout(() => complete(null), delayMs);
});
}
// ----- session ---------------------------------------------------------------------
async start(): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.start(); });
}
async setG2ScreenOn(screenOn: boolean): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.setG2ScreenOnScreenOn(Boolean(screenOn)));
}
async setFirmwareDebugFlags(enabled: boolean): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.setFirmwareDebugFlagsEnabled(Boolean(enabled)));
}
/** Set lens brightness: auto (ambient sensor) or an explicit 0-100 level. */
async setBrightness(autoAdjust: boolean, level: number): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.setBrightnessAutoAdjustBrightnessLevel(Boolean(autoAdjust), Math.round(level)));
}
async enableWearDetectionAndRequestState(): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.enableWearDetectionAndRequestState());
}
isSessionReady(): boolean {
return Boolean(this.communicator.isSessionReady());
}
/** The platform's lock/screen signal (the iOS host also observes protected data itself). */
onPhoneLockSignal(): void {
this.communicator.onPhoneLockSignal();
}
// ----- compositor ------------------------------------------------------------------
async configureCompositorScreen(width: number, height: number): Promise<void> {
await this.enqueueNativeCall(() => {
this.communicator.configureCompositorScreenWidthHeight(Math.round(width), Math.round(height));
});
}
/** Phone-UI preview of the current composited screen, or null if none yet. */
getCompositePreview(green = false): ImageSource | null {
const frame = this.communicator.previewComposite();
if (!frame) return null;
const width = Number(this.communicator.compositeWidth()), height = Number(this.communicator.compositeHeight());
if (width <= 0 || height <= 0) return null;
return previewPixels(fromData(frame.pixels), width, height, green);
}
/** Save the current composite as a 4-bit grayscale PNG under Documents; returns the path (empty if none). Cropping is not supported on iOS. */
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
if (crop) return "";
const png = this.communicator.screenshotPng();
if (!png) return "";
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
File.fromPath(target).writeSync(png);
return target;
}
/** Screen recording is not available on iOS yet. */
startScreenRecording(): void {}
recordScreenFrame(): void {}
stopScreenRecording(): string { return ""; }
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
const transparency = options.transparency === "color-key" ? 1 : 0;
await this.enqueueNativeCall(() => {
this.communicator.configureSurfaceIdXYWidthHeightZOrderTransparency(
id, Math.round(options.x), Math.round(options.y), Math.round(options.width), Math.round(options.height),
Math.round(options.zOrder), transparency,
);
});
}
async removeSurface(id: string): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.removeSurfaceId(id); });
}
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
const snapshot = new Uint8Array(bytes);
await this.enqueueNativeCall(() => this.communicator.submitShellSceneBytesPaintMsFrameId(toData(snapshot), Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId))), true);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
await this.enqueueNativeCall(() => {
this.communicator.setUnderlayDimBelowZOrderFactor256(Math.round(belowZOrder), dimFactor256(factor));
});
}
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.setSurfaceVisibleIdVisible(id, Boolean(visible)); });
}
async setScreenBlanked(blanked: boolean): Promise<void> {
await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); });
}
async submitSurfaceFrame(
surfaceId: string,
pixels8bpp: Uint8Array,
rect: { x: number; y: number; width: number; height: number },
fingerprint: string,
paintMs = -1,
frameId = 0,
glyphs: ArrayBuffer | null = null,
): Promise<void> {
const snapshot = new Uint8Array(pixels8bpp);
const draws = glyphs ? toData(new Uint8Array(glyphs)) : null;
await this.enqueueNativeCall(() => {
this.communicator.submitSurfaceFramePixels8bppSurfaceIdRectXRectYRectWidthRectHeightContentFingerprintPaintMsFrameIdGlyphs(
toData(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), Math.round(rect.width), Math.round(rect.height),
fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), draws,
);
}, true);
}
// ----- controls --------------------------------------------------------------------
async disconnect(): Promise<void> {
await this.enqueueNativeCall(() => this.communicator.disconnect());
}
async sendShutdown(exitMode = 0): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.sendShutdownExitMode(exitMode)));
}
async sendCfwCleanup(): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.sendCfwCleanup()));
}
async suspendEvenHubSession(): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.suspendEvenHubSession()));
}
async resumeEvenHubSession(): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.resumeEvenHubSession()));
}
async setFaceclawWakeLeaseEnabled(enabled: boolean): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.setFaceclawWakeLeaseEnabledEnabled(Boolean(enabled))));
}
async awaitEvenHubSessionReady(timeoutMs: number): Promise<boolean> {
return this.enqueueNativeCall(() => Boolean(this.communicator.awaitEvenHubSessionReadyTimeoutMs(Math.round(nonNegativeNumber(timeoutMs)))));
}
/** Play a CFW mode-5 kind-4 tone sequence (complete wire payload). */
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
const snapshot = new Uint8Array(payload);
await this.enqueueNativeCall(() => { this.communicator.playBuzzerSequencePayload(toData(snapshot)); });
}
async close(): Promise<void> {
this.setAncsListeners(false);
await this.enqueueNativeCall(() => this.communicator.close());
}
// ----- iOS extras: compass, microphone, iPhone notifications (ANCS) ----------------------
/** CFW mode-10 compass on behalf of `owner`; the magnetometer runs while any owner wants it. */
setCompassEnabled(enabled: boolean, owner = "compass"): void {
this.communicator.setCompassEnabledForOwnerOwnerEnable(owner, Boolean(enabled));
}
/** Compass heading/calibration events, delivered on the main queue. */
addCompassListener(listener: (event: CompassEvent) => void): () => void {
const proxy = CompassListener.new() as CompassListener;
proxy.handler = listener;
this.communicator.addCompassListenerListener(proxy);
return () => { this.communicator.removeCompassListenerListener(proxy); };
}
/**
* Serialized glasses microphone enable/disable (the voice bridge's
* IosMicrophoneSession contract). A release while an enable awaits its ACK
* still wins: the token check drops the stale packets.
*/
setMicrophone(enabled: boolean, listener?: (packet: Uint8Array) => void): Promise<void> {
const token = ++this.microphoneToken;
const work = this.microphoneWork.catch(() => {}).then(async () => {
if (token !== this.microphoneToken) return;
if (!enabled) {
this.audioListener = null;
await this.enqueueNativeCall(() => this.communicator.stopG2AudioCapture());
return;
}
if (!this.isSessionReady()) throw new Error("Connect the glasses before starting voice input.");
const proxy = AudioPacketListener.new() as AudioPacketListener;
proxy.handler = (packet) => { if (token === this.microphoneToken && listener) listener(packet); };
this.audioListener = proxy;
const started = await this.enqueueNativeCall(() => Boolean(this.communicator.startG2AudioCaptureNsDataListener(proxy)));
if (!started) {
this.audioListener = null;
throw new Error("The glasses did not enable their microphone. Reconnect and try again.");
}
});
this.microphoneWork = work;
return work;
}
/** Ask CoreBluetooth for ANCS on the right arm's next connection (call before start()). */
setRequiresAncs(required: boolean): void {
this.communicator.setRequiresAncsRequired(Boolean(required));
this.setAncsListeners(required);
}
isAncsAuthorized(): boolean {
return Boolean(this.communicator.isAncsAuthorized());
}
onAncsAuthorization(listener: (authorized: boolean) => void): () => void {
this.ancsAuthorizationListeners.add(listener);
return () => this.ancsAuthorizationListeners.delete(listener);
}
/** Relay frames ('A','N',...) received on the right arm, for the ANCS client. */
onAncsRelayFrame(listener: (frame: Uint8Array) => void): () => void {
this.ancsRelayListeners.add(listener);
return () => this.ancsRelayListeners.delete(listener);
}
/** Queue one relay packet for the right arm; the Kotlin side writes it off the JS thread. */
async writeRawToRight(packet: Uint8Array): Promise<void> {
if (!this.isSessionReady()) throw new Error("Glasses are not connected.");
const snapshot = new Uint8Array(packet);
this.communicator.writeRawToRightPacket(toData(snapshot));
}
/** The right arm's usable write payload for relay packets (20 until connected). */
rightWriteLimit(): number {
return Number(this.communicator.rightWriteLimit()) || 20;
}
private setAncsListeners(install: boolean): void {
if (install && !this.ancsProxy) {
this.ancsProxy = AncsListener.new() as AncsListener;
this.ancsProxy.bridge = this;
this.communicator.setAncsListenerListener(this.ancsProxy);
} else if (!install && this.ancsProxy) {
this.communicator.setAncsListenerListener(null);
this.ancsProxy = null;
}
}
}
+97 -56
View File
@@ -1,83 +1,124 @@
import { Application } from '@nativescript/core' import { Application } from '@nativescript/core'
import { StockConnection } from '../g2/stock-connection'
import { flashLensImage, validateFirmware } from '../g2/firmware-ota'
import { CFW_PATCH_SET } from '../g2/firmware/cfw-patches' import { CFW_PATCH_SET } from '../g2/firmware/cfw-patches'
import { firmwareSha256, readFirmwareFile } from './firmware-files.ios' import { firmwareSha256, readFirmwareFile } from './firmware-files.ios'
import { iosBluetooth } from './ios-bluetooth' import { iosBluetooth } from './ios-bluetooth'
import type { FlashState, FlashProgress } from './firmware-flasher' import { kotlinListener } from './kotlin-listener.ios'
export type { FlashState, FlashProgress } from './firmware-flasher' import type { FlashState, FlashProgress } from '../g2/firmware-types'
export type { FlashState, FlashProgress } from '../g2/firmware-types'
declare const FaceclawKitIosFirmwareFlasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any
/**
* iOS twin of the Android FirmwareFlasher wrapper. The OTA transfer itself is the shared
* Kotlin OtaFlashFlow over a CoreBluetooth central (FaceclawKitIosFirmwareFlasher); this
* wrapper keeps the iOS-only policy around it: the image must still hash to the prepared
* custom (or pinned stock) firmware, the phone must stay in the foreground with the screen
* awake, and MAC addresses are mapped to peripheral identifiers first.
*/
export class FirmwareFlasher { export class FirmwareFlasher {
private logs = new Set<(line: string) => void>() private flasher: any = null
private states = new Set<(state: FlashState, detail: string) => void>() private listenerProxy: object | null = null
private progresses = new Set<(value: FlashProgress) => void>() private readonly logListeners = new Set<(line: string) => void>()
private completes = new Set<(success: boolean, detail: string) => void>() private readonly progressListeners = new Set<(progress: FlashProgress) => void>()
private link = new StockConnection(iosBluetooth(), line => this.log(line)) private readonly stateListeners = new Set<(state: FlashState, detail: string) => void>()
private readonly completeListeners = new Set<(success: boolean, detail: string) => void>()
private started = false private started = false
private finished = false
private cancelled = false private cancelled = false
private sleeps = new Set<() => void>()
private foregroundError = '' private foregroundError = ''
constructor(private addresses: { right: string; left: string }, private path: string) {} private previousIdle = false
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: FlashState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) } constructor(private readonly addresses: { right: string; left: string }, private readonly path: string) {}
onProgress(fn: (value: FlashProgress) => void): () => void { this.progresses.add(fn); return () => this.progresses.delete(fn) }
onComplete(fn: (success: boolean, detail: string) => void): () => void { this.completes.add(fn); return () => this.completes.delete(fn) } onLog(listener: (line: string) => void): () => void {
private log(line: string): void { for (const fn of this.logs) fn(line) } this.logListeners.add(listener)
private state(state: FlashState, detail = ''): void { for (const fn of this.states) fn(state, detail) } return () => this.logListeners.delete(listener)
start(): void { if (!this.started) { this.started = true; void this.run() } } }
onProgress(listener: (progress: FlashProgress) => void): () => void {
this.progressListeners.add(listener)
return () => this.progressListeners.delete(listener)
}
onStateChange(listener: (state: FlashState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
onComplete(listener: (success: boolean, detail: string) => void): () => void {
this.completeListeners.add(listener)
return () => this.completeListeners.delete(listener)
}
start(): void {
if (this.started) return
this.started = true
void this.run()
}
private readonly backgrounded = () => { private readonly backgrounded = () => {
this.foregroundError = 'Flashing was interrupted because Faceclaw left the foreground. Keep both lenses charged, return to Faceclaw, and retry both lenses.' this.foregroundError = 'Flashing was interrupted because Faceclaw left the foreground. Keep both lenses charged, return to Faceclaw, and retry both lenses.'
this.cancel() this.cancel()
} }
private async run(): Promise<void> { private async run(): Promise<void> {
const previousIdle = UIApplication.sharedApplication.idleTimerDisabled this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled
UIApplication.sharedApplication.idleTimerDisabled = true UIApplication.sharedApplication.idleTimerDisabled = true
Application.on(Application.suspendEvent, this.backgrounded) Application.on(Application.suspendEvent, this.backgrounded)
let success = false, detail = ''
try { try {
this.link.check()
if (UIApplication.sharedApplication.applicationState !== UIApplicationState.Active) throw new Error('Keep Faceclaw open on the iPhone while flashing.') if (UIApplication.sharedApplication.applicationState !== UIApplicationState.Active) throw new Error('Keep Faceclaw open on the iPhone while flashing.')
this.state('validating') this.emit(this.stateListeners, 'validating', '')
const image = readFirmwareFile(this.path) const image = readFirmwareFile(this.path)
const hash = firmwareSha256(image.buffer) const hash = firmwareSha256(image.buffer)
if (![CFW_PATCH_SET.outputSha256, CFW_PATCH_SET.baseSha256].includes(hash)) throw new Error('Prepared firmware failed SHA-256 verification. Prepare the firmware again before flashing.') if (![CFW_PATCH_SET.outputSha256, CFW_PATCH_SET.baseSha256].includes(hash)) throw new Error('Prepared firmware failed SHA-256 verification. Prepare the firmware again before flashing.')
const segments = validateFirmware(image) const identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
await this.link.resolve({ ...this.addresses, ring: '' }) if (this.cancelled) throw new Error('Cancelled.')
for (const role of ['left', 'right']) { this.flasher = FaceclawKitIosFirmwareFlasher.alloc().initWithRightAddressLeftAddressFirmwarePath(identifiers.right ?? '', identifiers.left ?? '', this.path)
if (role === 'right') { this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, {
this.state('rebooting', 'Left lens done. Both lenses reboot briefly; reconnecting for the right lens.') onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
await this.delay(5000) onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: (
} lens: string, componentIndex: number, componentCount: number, blockIndex: number, blockCount: number, bytesSent: number, bytesTotal: number,
this.state('connecting', role) ) => this.emit(this.progressListeners, {
const deadline = Date.now() + (role === 'left' ? 30_000 : 120_000) lens: String(lens ?? ''), componentIndex: Number(componentIndex), componentCount: Number(componentCount),
for (;;) { blockIndex: Number(blockIndex), blockCount: Number(blockCount), bytesSent: Number(bytesSent), bytesTotal: Number(bytesTotal),
try { await this.link.connect(role, true); await this.link.authenticate(role); break } }),
catch (error) { this.link.check(); this.link.disconnect(role); if (Date.now() >= deadline) throw error; await this.delay(2500) } onStateStateDetail: (state: string, detail: string) => {
} // The terminal state is reported by finish() so a foreground/cancel reason can replace the detail.
await this.delay(2500) const name = String(state) as FlashState
this.state('flashing', role) if (name !== 'done' && name !== 'error') this.emit(this.stateListeners, name, String(detail ?? ''))
await flashLensImage(this.link, role, image, segments, value => { for (const fn of this.progresses) fn(value) }, line => this.log(line), ms => this.delay(ms)) },
this.link.disconnect(role) onCompleteSuccessDetail: (success: boolean, detail: string) => this.finish(Boolean(success), String(detail ?? '')),
} })
success = true; detail = 'Both lenses flashed. Your glasses are rebooting.' this.flasher.setListenerListener(this.listenerProxy)
this.flasher.start()
} catch (error) { } catch (error) {
detail = this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : String((error as Error).message ?? error)) this.finish(false, String((error as Error).message ?? error))
} finally {
Application.off(Application.suspendEvent, this.backgrounded)
UIApplication.sharedApplication.idleTimerDisabled = previousIdle
this.link.close()
} }
this.state(success ? 'done' : 'error', detail)
for (const fn of this.completes) fn(success, detail)
} }
private delay(ms: number): Promise<void> {
this.link.check() private finish(success: boolean, detail: string): void {
return new Promise((resolve, reject) => { if (this.finished) return
const cancel = () => { clearTimeout(timer); this.sleeps.delete(cancel); reject(new Error('Cancelled.')) } this.finished = true
const timer = setTimeout(() => { this.sleeps.delete(cancel); resolve() }, ms) Application.off(Application.suspendEvent, this.backgrounded)
this.sleeps.add(cancel) UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle
}) try { this.flasher?.close() } catch { /* ignore */ }
const message = success ? detail
: this.foregroundError || (this.cancelled ? 'Cancelled. Retry with both lenses powered on to complete installation.' : detail)
this.emit(this.stateListeners, success ? 'done' : 'error', message)
this.emit(this.completeListeners, success, message)
} }
cancel(): void { this.cancelled = true; this.link.close(); for (const cancel of [...this.sleeps]) cancel() }
cancel(): void {
this.cancelled = true
try { this.flasher?.cancel() } catch { /* ignore */ }
// Before the Kotlin flow exists (validation, address resolution) nothing will report completion.
if (!this.flasher) this.finish(false, 'Cancelled.')
}
close(): void { this.cancel() } close(): void { this.cancel() }
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const snapshot = Array.from(listeners)
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
}
} }
+87 -77
View File
@@ -1,88 +1,98 @@
import { StockConnection } from '../g2/stock-connection'
import { createFlashPrompt, flashPromptSelection } from '../g2/flash-prompt-protocol'
import * as p from '../g2/ble-protocol'
import { iosBluetooth } from './ios-bluetooth' import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator' import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator' export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
declare const FaceclawKitIosFlashPrompt: any, FaceclawKitFaceclawFlashPromptListener: any
function normalizePercent(value: number): number | null {
const n = Number(value)
return Number.isFinite(n) && n >= 0 ? n : null
}
/**
* iOS twin of the Android FlashPromptCommunicator wrapper over the shared Kotlin
* FlashPromptFlow (FaceclawKitIosFlashPrompt): connects and authenticates both arms, shows
* the pre-flash Yes/No page on the glasses (unless skipPrompt) and reads each arm's battery.
*/
export class FlashPromptCommunicator { export class FlashPromptCommunicator {
private logs = new Set<(line: string) => void>() private communicator: any = null
private states = new Set<(state: FlashPromptState, detail: string) => void>() private listenerProxy: object | null = null
private results = new Set<(approved: boolean) => void>() private readonly logListeners = new Set<(line: string) => void>()
private batteries = new Set<(battery: FlashPromptBattery) => void>() private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>()
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) }) private readonly resultListeners = new Set<(approved: boolean) => void>()
private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>()
private started = false
private closed = false private closed = false
private started = false
private heartbeat: ReturnType<typeof setInterval> | null = null constructor(
private stopSelection: (() => void) | null = null private readonly addresses: { right: string; left: string },
constructor(private addresses: { right: string; left: string }, private warning: string, private options?: { skipPrompt?: boolean }) {} private readonly warningText: string,
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) } private readonly options?: { skipPrompt?: boolean },
onStateChange(fn: (state: FlashPromptState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) } ) {}
onResult(fn: (approved: boolean) => void): () => void { this.results.add(fn); return () => this.results.delete(fn) }
onBattery(fn: (battery: FlashPromptBattery) => void): () => void { this.batteries.add(fn); return () => this.batteries.delete(fn) } onLog(listener: (line: string) => void): () => void {
private state(state: FlashPromptState, detail = ''): void { for (const fn of this.states) fn(state, detail) } this.logListeners.add(listener)
start(): void { if (!this.started) { this.started = true; void this.run() } } return () => this.logListeners.delete(listener)
}
onStateChange(listener: (state: FlashPromptState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
onBattery(listener: (battery: FlashPromptBattery) => void): () => void {
this.batteryListeners.add(listener)
return () => this.batteryListeners.delete(listener)
}
onResult(listener: (approved: boolean) => void): () => void {
this.resultListeners.add(listener)
return () => this.resultListeners.delete(listener)
}
start(): void {
if (this.started) return
this.started = true
void this.run()
}
private async run(): Promise<void> { private async run(): Promise<void> {
let identifiers: Record<string, string>
try { try {
this.state('connecting') this.emit(this.stateListeners, 'connecting', '')
await this.link.resolve({ ...this.addresses, ring: '' }) identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
// Complete pairing for one peripheral before starting the other. } catch (error) {
for (const role of ['right', 'left']) { if (!this.closed) this.emit(this.stateListeners, 'error', String((error as Error).message ?? error))
await this.link.connect(role) return
if (role === 'right') this.state('connected') }
await this.link.authenticate(role) if (this.closed) return
} this.communicator = FaceclawKitIosFlashPrompt.alloc().initWithRightAddressLeftAddressWarningTextSkipPrompt(
await this.link.request('right', p.SID.launch, p.prelude, 3500, 156) identifiers.right ?? '', identifiers.left ?? '', this.warningText, Boolean(this.options?.skipPrompt))
if (!this.options?.skipPrompt) { this.listenerProxy = kotlinListener(FaceclawKitFaceclawFlashPromptListener, {
// Listen before creating the page: an immediate tap may precede its ACK. onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
const selection = this.selection() onStateStateDetail: (state: string, detail: string) =>
// Attach a rejection handler immediately, even while create-page is pending. this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? '')),
void selection.catch(() => {}) onBatteryRightPercentLeftPercent: (rightPercent: number, leftPercent: number) =>
for (let attempt = 0; ; attempt++) { this.emit(this.batteryListeners, { right: normalizePercent(rightPercent), left: normalizePercent(leftPercent) }),
try { await this.link.request('right', p.SID.hub, magic => createFlashPrompt(magic, this.warning)); break } onResultApproved: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)),
catch (error) { this.link.check(); if (attempt) throw error } })
} this.communicator.setListenerListener(this.listenerProxy)
this.state('prompting') this.communicator.start()
this.heartbeat = setInterval(() => { }
void this.link.write('right', p.SID.hub, p.heartbeat(this.link.nextMagic())).catch(error => {
if (!this.closed) this.state('error', String(error.message ?? error)) cancel(): void {
this.close() this.closed = true
}) try { this.communicator?.cancel() } catch { /* ignore */ }
}, 4000)
const approved = await selection
this.stopHeartbeat()
// Stock shutdown returns the glasses to their normal UI before OTA.
await this.link.write('right', p.SID.hub, p.concat(p.integer(1, 9), p.integer(2, this.link.nextMagic()), p.bytes(11, p.integer(1, 0))))
if (!approved) { for (const fn of this.results) fn(false); return }
}
this.state('battery')
const battery: FlashPromptBattery = { right: null, left: null }
for (const role of ['right', 'left'] as const) {
for (let attempt = 0; attempt < 2; attempt++) {
try {
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
const values = p.readBytes(message.payload, 4), value = values ? p.readInteger(values, 12, -1) : -1
if (value >= 0 && value <= 100) { battery[role] = value; break }
} catch (error) { this.link.check() }
}
}
for (const fn of this.batteries) fn(battery)
for (const fn of this.results) fn(true)
} catch (error) { if (!this.closed) this.state('error', String((error as Error).message ?? error)) }
finally { this.close() }
} }
private selection(): Promise<boolean> {
return new Promise((resolve, reject) => { close(): void {
const cleanup = () => { clearTimeout(timer); off(); this.stopSelection = null } this.closed = true
const off = this.link.onMessage((role, message) => { try { this.communicator?.close() } catch { /* ignore */ }
const result = role === 'right' ? flashPromptSelection(message) : null }
if (result !== null) { cleanup(); resolve(result) }
}) private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const timer = setTimeout(() => { cleanup(); reject(new Error('No response from the glasses.')) }, 120_000) const snapshot = Array.from(listeners)
this.stopSelection = () => { cleanup(); reject(new Error('Cancelled.')) } setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
})
} }
private stopHeartbeat(): void { if (this.heartbeat) clearInterval(this.heartbeat); this.heartbeat = null }
cancel(): void { this.close() }
close(): void { this.closed = true; this.stopHeartbeat(); this.stopSelection?.(); this.link.close() }
} }
+56 -73
View File
@@ -1,75 +1,80 @@
import { ApplicationSettings } from '@nativescript/core' import { ApplicationSettings } from '@nativescript/core'
import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity' import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity'
import { hexToBytes } from '../util/hex-util' import { hexToBytes } from '../util/hex-util'
import { kotlinListener } from './kotlin-listener.ios'
declare const FaceclawBluetooth: any declare const FaceclawKitIosBleCentral: any, FaceclawKitIosProtocolPlatform: any, FaceclawKitIosBleScanListener: any
type NativeEvent = { kind: string; id?: number; error?: string; details?: ConnectionDetails; state?: number;
identifier?: string; characteristic?: string; data?: string; message?: string } & Partial<IosAdvertisement>
export type ConnectionDetails = { characteristics: string[]; maxWrite: number }
export type BluetoothEvent = NativeEvent | { kind: 'device'; device: IosDevice }
type Pending = { resolve: (details: ConnectionDetails) => void; reject: (error: Error) => void; timer: ReturnType<typeof setTimeout> }
/** One central manager per app, with asynchronous main-queue GATT operations. */ /** CBManagerState raw values reported by the Kotlin central. */
export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_POWERED_OFF = 4, BLE_STATE_POWERED_ON = 5
export type BluetoothEvent =
| { kind: 'state'; state: number; authorization: number }
| { kind: 'advertisement'; identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean }
| { kind: 'device'; device: IosDevice }
| { kind: 'scan-started' }
| { kind: 'scan-stopped' }
/**
* Scanning and MAC-address-to-identifier resolution over the shared Kotlin CoreBluetooth
* central (FaceclawKitIosBleCentral). GATT sessions no longer pass through here: the
* session and stock-firmware flows own their own Kotlin centrals. What stays in TypeScript
* is the Even classification of advertisements and the persisted identifier map, which the
* Android address model (MACs) needs on iOS.
*/
export class IosBluetooth { export class IosBluetooth {
private readonly native = FaceclawBluetooth.new() private readonly native = FaceclawKitIosBleCentral.alloc().initWithPlatform(FaceclawKitIosProtocolPlatform.shared)
private readonly listeners = new Set<(event: BluetoothEvent) => void>() private readonly listeners = new Set<(event: BluetoothEvent) => void>()
private readonly pending = new Map<number, Pending>()
private readonly advertisements = new Map<string, IosAdvertisement>()
readonly devices = new Map<string, IosDevice>() readonly devices = new Map<string, IosDevice>()
private requestId = 0
private scanTimer: ReturnType<typeof setTimeout> | null = null private scanTimer: ReturnType<typeof setTimeout> | null = null
private scanGeneration = 0 private scanGeneration = 0
private readonly scanListener: object
state = 0 state = 0
authorization = 0
scanning = false scanning = false
constructor() { constructor() {
this.native.eventHandler = (json: string) => { this.scanListener = kotlinListener(FaceclawKitIosBleScanListener, {
try { this.receive(JSON.parse(json)) } catch (error) { console.error(`[ios-ble] ${error}`) } onBluetoothStateStateAuthorization: (state: number, authorization: number) => {
} this.state = Number(state); this.authorization = Number(authorization)
this.emit({ kind: 'state', state: this.state, authorization: this.authorization })
},
onAdvertisementIdentifierNameManufacturerDataHexRssiConnectable: (identifier: string, name: string, manufacturerData: string, rssi: number, connectable: boolean) => {
// The Kotlin central already merges split advertisements and bounds its cache.
const raw: IosAdvertisement = { identifier: String(identifier), name: String(name ?? ''), manufacturerData: String(manufacturerData ?? ''), rssi: Number(rssi), connectable: connectable !== false }
this.emit({ kind: 'advertisement', ...raw })
const device = identifyIosPeripheral(raw)
if (device) {
this.devices.set(device.address, device)
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
this.emit({ kind: 'device', device })
}
},
})
this.native.setScanListenerListener(this.scanListener)
} }
onEvent(listener: (event: BluetoothEvent) => void): () => void { onEvent(listener: (event: BluetoothEvent) => void): () => void {
this.listeners.add(listener); return () => { this.listeners.delete(listener) } this.listeners.add(listener); return () => { this.listeners.delete(listener) }
} }
private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) } private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) }
private receive(event: NativeEvent): void { /** Creates the central (first use shows the Bluetooth permission prompt) without blocking the JS thread. */
if (event.kind === 'completion') { private ensureCentral(): void {
const pending = this.pending.get(event.id!) // A zero timeout returns immediately after creating the central; the state arrives
if (pending) { // through the scan listener on the main queue.
this.pending.delete(event.id!); clearTimeout(pending.timer) this.native.waitForPoweredOnTimeoutMs(0)
if (event.error) pending.reject(new Error(event.error)); else pending.resolve(event.details!)
}
return
}
if (event.kind === 'state') this.state = event.state!
if (event.kind === 'advertisement') {
const previous = this.advertisements.get(event.identifier!)
const raw: IosAdvertisement = { identifier: event.identifier!, name: event.name || previous?.name || '',
manufacturerData: event.manufacturerData || previous?.manufacturerData || '', rssi: event.rssi!, connectable: event.connectable !== false }
// Bound the cache even in a crowded radio environment.
if (this.advertisements.size >= 512 && !this.advertisements.has(raw.identifier)) this.advertisements.delete(this.advertisements.keys().next().value!)
this.advertisements.set(raw.identifier, raw)
const device = identifyIosPeripheral(raw)
if (device) {
this.devices.set(device.address, device)
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
this.emit({ kind: 'device', device })
}
return
}
this.emit(event)
} }
ensureReady(): Promise<void> { ensureReady(): Promise<void> {
if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve()
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() } const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() }
const off = this.onEvent(event => { const off = this.onEvent(event => {
if (event.kind !== 'state') return if (event.kind !== 'state') return
const state = (event as NativeEvent).state if (event.state === BLE_STATE_POWERED_ON) finish()
if (state === 5) finish() else if (event.state === BLE_STATE_UNSUPPORTED) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
else if (state === 2) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.')) else if (event.state === BLE_STATE_UNAUTHORIZED) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
else if (state === 3) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.')) else if (event.state === BLE_STATE_POWERED_OFF) finish(new Error('Turn on Bluetooth, then try again.'))
else if (state === 4) finish(new Error('Turn on Bluetooth, then try again.'))
}) })
const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000) const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000)
this.native.initializeBluetooth() this.ensureCentral()
}) })
} }
async startScan(durationMs = 12_000): Promise<void> { async startScan(durationMs = 12_000): Promise<void> {
@@ -77,17 +82,18 @@ export class IosBluetooth {
const generation = this.scanGeneration const generation = this.scanGeneration
await this.ensureReady() await this.ensureReady()
if (generation !== this.scanGeneration) throw new Error('Bluetooth scan cancelled') if (generation !== this.scanGeneration) throw new Error('Bluetooth scan cancelled')
this.scanning = true; this.native.scan(true) this.scanning = true; this.native.startScan()
this.scanTimer = setTimeout(() => this.stopScan(), durationMs) this.scanTimer = setTimeout(() => this.stopScan(), durationMs)
this.emit({ kind: 'scan-started' }) this.emit({ kind: 'scan-started' })
} }
stopScan(): void { stopScan(): void {
++this.scanGeneration ++this.scanGeneration
if (this.scanTimer) clearTimeout(this.scanTimer) if (this.scanTimer) clearTimeout(this.scanTimer)
this.scanTimer = null; this.native.scan(false) this.scanTimer = null
const wasScanning = this.scanning; this.scanning = false const wasScanning = this.scanning; this.scanning = false
if (wasScanning) this.emit({ kind: 'scan-stopped' }) if (wasScanning) { this.native.stopScan(); this.emit({ kind: 'scan-stopped' }) }
} }
/** Peripheral identifiers for the stored MAC addresses, scanning for any that are not remembered. */
async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise<Record<string, string>> { async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise<Record<string, string>> {
const generation = this.scanGeneration const generation = this.scanGeneration
await this.ensureReady() await this.ensureReady()
@@ -109,7 +115,7 @@ export class IosBluetooth {
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
const off = this.onEvent(event => { const off = this.onEvent(event => {
if (event.kind === 'device') { if (event.kind === 'device') {
const device = (event as { device: IosDevice }).device const device = event.device
for (const role of ['left', 'right', 'ring'] as const) { for (const role of ['left', 'right', 'ring'] as const) {
if (addresses[role] === device.address && device.role === role) result[role] = device.identifier if (addresses[role] === device.address && device.role === role) result[role] = device.identifier
} }
@@ -123,29 +129,6 @@ export class IosBluetooth {
}) })
return result return result
} }
private operation(identifier: string, call: (id: number) => void, timeoutMs = 10_000, disconnectOnTimeout = true): Promise<ConnectionDetails> {
const id = ++this.requestId
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
this.pending.delete(id); if (disconnectOnTimeout) this.native.disconnect(identifier)
reject(new Error('Bluetooth operation timed out. Wake the device and try connecting again.'))
}, timeoutMs)
this.pending.set(id, { resolve, reject, timer })
try { call(id) } catch (error) { clearTimeout(timer); this.pending.delete(id); reject(error) }
})
}
connect(identifier: string, requiresANCS = false): Promise<ConnectionDetails> { return this.operation(identifier, id => this.native.connectRequiresANCSRequestId(identifier, requiresANCS, id), 20_000) }
async subscribe(identifier: string, characteristic: string): Promise<void> {
// R1 exposes two notification channels; one may never complete its CCCD
// request. Keep the link alive so the other channel can still be tried.
await this.operation(identifier, id => this.native.subscribeCharacteristicRequestId(identifier, characteristic, id), 30_000, false)
}
async write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void> {
const copy = new Uint8Array(bytes)
const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength)
await this.operation(identifier, id => this.native.writeCharacteristicDataRequestId(identifier, characteristic, data, id))
}
disconnect(identifier: string): void { this.native.disconnect(identifier) }
forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) } forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) }
} }
let instance: IosBluetooth | null = null let instance: IosBluetooth | null = null
+10
View File
@@ -0,0 +1,10 @@
/**
* Implements a FaceclawKit (Kotlin) listener protocol with plain JavaScript methods.
* NativeScript maps the method names to the protocol's selectors by camel-casing the
* selector parts (`onLog:` -> `onLogLine`, `onState:detail:` -> `onStateStateDetail`, ...).
* Keep a reference to the returned object for as long as the Kotlin side may call it.
*/
export function kotlinListener<T extends object>(protocol: unknown, methods: T): T {
const NativeListener = (NSObject as any).extend(methods, { protocols: [protocol] })
return NativeListener.new() as T
}
+2 -28
View File
@@ -1,14 +1,9 @@
import { CFW_MSG_BOUNDING_BOX, CFW_MSG_CREATE_SURFACE, CFW_MSG_PRESENT, CFW_MSG_UPLOAD_RESOURCE } from "../g2/cfw-message-type";
import { GrayImage } from "../graphics/image" import { GrayImage } from "../graphics/image"
import { encodeShellScene } from "../graphics/shell-scene" import { encodeShellScene } from "../graphics/shell-scene"
import { decodeImageBytes } from './image-bytes.ios' import { decodeImageBytes } from './image-bytes.ios'
import * as protocol from '../g2/ble-protocol.ios' import * as protocol from '../g2/ble-protocol.ios'
import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios'
import { SurfaceCompositor } from '../graphics/surface-compositor.ios' import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios'
import { lvglMetrics } from './lvgl-font.ios' import { lvglMetrics } from './lvgl-font.ios'
import { textureImageId } from './texture-atlas.ios'
import { IosTexturePlanner } from './texture-planner.ios'
/** Development-only checks of real NativeScript selectors and bulk data crossings. */ /** Development-only checks of real NativeScript selectors and bulk data crossings. */
export function runKotlinProtocolSmokeTest(): void { export function runKotlinProtocolSmokeTest(): void {
@@ -24,11 +19,6 @@ export function runKotlinProtocolSmokeTest(): void {
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2)))) const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, 12), protocol.integer(2, 2))))
const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 }) const event = protocol.decodeGlassesInput({ sid: 224, flag: 1, payload, command: 0, magic: 0 })
assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event') assert(event?.kind === 'display-wake' && event.eventSource === 2, 'event')
const transport = new CfwTransport()
const packets = transport.encode(new Uint8Array(128).fill(9), 255, 3, 185)
assert(packets.length > 0 && packets.every(p => p.length <= 185), 'compression')
transport.reset()
assert(parseCfwAcks(new Uint8Array()) === null, 'invalid ACK')
const c = new SurfaceCompositor(3, 2) const c = new SurfaceCompositor(3, 2)
c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' }) c.configureSurface('test', { x: 0, y: 0, width: 3, height: 2, zOrder: 0, transparency: 'opaque' })
c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 }) c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 })
@@ -37,28 +27,12 @@ export function runKotlinProtocolSmokeTest(): void {
c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255])) c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,0,0,255]))
assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition') assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition')
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking') c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
const textures = new IosTexturePlanner(), textured = new SurfaceCompositor(4, 2)
textured.configureSurface('icon', { x: 0, y: 0, width: 2, height: 2, zOrder: 0, transparency: 'opaque' })
const white = new Uint8Array(4).fill(255)
const icon = textureImageId('protocol-smoke-white', 2, 2, white)
const draw = new DataView(new ArrayBuffer(9)); draw.setUint8(0, 1); draw.setUint32(1, icon, true)
textured.submitSurfaceFrame('icon', white, { x: 0, y: 0, width: 2, height: 2 }, draw.buffer)
const snapshot = textured.compositeFrame(), packed = protocol.packGray4(snapshot.pixels, 4, 2)
const cold = textures.plan(null, packed, snapshot.textures, 1)
assert(!!cold && cold.resourceCommands.some(command => command[0] === CFW_MSG_UPLOAD_RESOURCE) && cold.payload[0] === CFW_MSG_PRESENT, 'texture upload and draw')
assert(textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length === 0, 'texture reuse')
textures.reset()
assert((textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length ?? 0) > 0, 'texture reset')
const selectedSurface = new GrayImage(8, 4, 1) const selectedSurface = new GrayImage(8, 4, 1)
selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2) selectedSurface.drawMenuSelection(new GrayImage(4, 2, 255), 1, 1, 16, 48, 0, 2)
const selectedPreview = new SurfaceCompositor(8, 4) const selectedPreview = new SurfaceCompositor(8, 4)
selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }])) selectedPreview.setShellScene(encodeShellScene([{ image: selectedSurface, x: 0, y: 0, shellKey: 100 }]))
const selectedSnapshot = selectedPreview.compositeFrame() const selectedPixels = selectedPreview.composite()
assert(selectedSnapshot.pixels[10] === 240 && selectedSnapshot.pixels[9] === 0, 'menu selection depth and bridge') assert(selectedPixels[10] === 240 && selectedPixels[9] === 0, 'menu selection depth and bridge')
const selectedPlan = new IosTexturePlanner().plan(null, protocol.packGray4(selectedSnapshot.pixels, 8, 4), selectedSnapshot.textures, 1)
assert(!!selectedPlan && selectedPlan.payload[0] === CFW_MSG_PRESENT && selectedPlan.resourceCommands.some(c => c[0] === CFW_MSG_CREATE_SURFACE), 'menu resource planning')
const changed = new Uint8Array(16); changed[0] = 255
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === CFW_MSG_BOUNDING_BOX, 'image update')
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge') assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0, const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0,
payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) }) payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
+141
View File
@@ -0,0 +1,141 @@
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
import { SurfaceCompositor } from "../graphics/surface-compositor";
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from "../graphics/image";
import { previewPixels } from "./ios-graphics";
import { toData } from "./kotlin-data";
import type { FaceclawCommunicatorBridge, SurfaceOptions } from "./faceclaw-communicator.ios";
declare const FaceclawKitIosProtocol: any;
/** The display subset of the communicator bridge; see the Android twin. */
export type DisplayTarget = Pick<
FaceclawCommunicatorBridge,
| "configureCompositorScreen"
| "configureSurface"
| "removeSurface"
| "setSurfaceVisible"
| "setUnderlayDim"
| "setScreenBlanked"
| "submitSurfaceFrame"
| "submitShellScene"
| "waitForFrameFinished"
| "getCompositePreview"
| "saveScreenshot"
| "startScreenRecording"
| "recordScreenFrame"
| "stopScreenRecording"
>;
function timestamp(): string {
const now = new Date();
const pad = (value: number, width = 2) => String(value).padStart(width, "0");
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}-${pad(now.getMilliseconds(), 3)}`;
}
/**
* Headless display target for iOS while no glasses session exists: the same
* shared Kotlin SurfaceCompositor a live session composes with, so the phone
* mirror, apps and shell render identically before connecting. Frames end
* at the compositor; there is no transmit backpressure to wait on.
*/
export class PreviewDisplayTarget implements DisplayTarget {
private compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT);
private onFrameComposited: (() => void) | null = null;
private released = false;
/** Receive a callback per applied frame, the stand-in for the connected path's frame-finished callback. */
activate(onFrameComposited: () => void): void {
this.onFrameComposited = onFrameComposited;
}
release(): void {
this.released = true;
this.onFrameComposited = null;
}
private composited(): void {
if (!this.released) this.onFrameComposited?.();
}
async configureCompositorScreen(width: number, height: number): Promise<void> {
if (Math.round(width) !== this.compositor.width || Math.round(height) !== this.compositor.height) {
this.compositor = new SurfaceCompositor(Math.round(width), Math.round(height));
}
}
async configureSurface(id: string, options: SurfaceOptions): Promise<void> {
this.compositor.configureSurface(id, {
x: Math.round(options.x), y: Math.round(options.y), width: Math.round(options.width), height: Math.round(options.height),
zOrder: Math.round(options.zOrder), transparency: options.transparency,
});
}
async removeSurface(id: string): Promise<void> {
this.compositor.removeSurface(id);
this.composited();
}
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
this.compositor.setSurfaceVisible(id, Boolean(visible));
this.composited();
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
this.compositor.setUnderlayDim(Math.round(belowZOrder), Math.max(0, Math.min(1, factor)));
this.composited();
}
async setScreenBlanked(blanked: boolean): Promise<void> {
this.compositor.setScreenBlanked(Boolean(blanked));
this.composited();
}
async submitSurfaceFrame(
surfaceId: string,
pixels8bpp: Uint8Array,
rect: { x: number; y: number; width: number; height: number },
_fingerprint: string,
_paintMs = -1,
_frameId = 0,
glyphs: ArrayBuffer | null = null,
): Promise<void> {
this.compositor.submitSurfaceFrame(surfaceId, pixels8bpp,
{ x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }, glyphs);
this.composited();
}
async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise<void> {
this.compositor.setShellScene(bytes);
this.composited();
}
/** Nothing transmits: every frame is finished as soon as it is composited. */
waitForFrameFinished(frameId: number, _timeoutMs: number): Promise<string | null> {
return Promise.resolve(frameId > 0 ? "composited" : null);
}
/** The current composite as gray pixels (row-major, compositor size). */
composite(): Uint8Array {
return this.compositor.composite();
}
getCompositePreview(green = false): ImageSource | null {
return previewPixels(this.compositor.composite(), this.compositor.width, this.compositor.height, green);
}
/** Save the current composite as a 4-bit grayscale PNG under Documents; cropping is not supported on iOS. */
saveScreenshot(crop?: { x: number; y: number; width: number; height: number }): string {
if (crop) return "";
const png = FaceclawKitIosProtocol.new().pngDataWidthHeight(toData(this.compositor.composite()), this.compositor.width, this.compositor.height);
if (!png) return "";
const target = path.join(knownFolders.documents().path, `screen-${timestamp()}.png`);
File.fromPath(target).writeSync(png);
return target;
}
startScreenRecording(): void {}
recordScreenFrame(): void {}
stopScreenRecording(): string { return ""; }
}
+10 -8
View File
@@ -1,14 +1,16 @@
import { createListenerPool, type InterfaceAddress } from '../remote/listeners'; import { createListenerPool, type InterfaceAddress } from '../remote/listeners';
declare const FaceclawRemoteInput: any; declare const FaceclawKitIosRemoteInput: any;
/** The shared Kotlin RemoteInputSession over POSIX sockets; request notifications arrive on the main queue. */
export function remoteNative(onRequestReady: () => void) { export function remoteNative(onRequestReady: () => void) {
return createListenerPool(() => { return createListenerPool(() => {
const native = FaceclawRemoteInput.alloc().init(); const native = FaceclawKitIosRemoteInput.new();
native.setRequestListener(onRequestReady); native.setRequestListenerListener(onRequestReady);
return { start: (port, address) => String(native.startAddress(port, address)), stop: () => { native.setRequestListener(null); native.stop(); }, return { start: (port, address) => String(native.startAddressPortAddress(port, address)), stop: () => { native.setRequestListenerListener(null); native.stop(); },
nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeResponse(id, response) }; nextRequest: () => native.nextRequest(), complete: (id, response) => native.completeIdValue(id, response) };
}); });
} }
export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(FaceclawRemoteInput.interfaces())); } function shared(): any { return FaceclawKitIosRemoteInput.companion.shared; }
export function randomTokenSecret(): string { return String(FaceclawRemoteInput.shared().randomSecret()); } export function remoteInterfaces(): InterfaceAddress[] { return JSON.parse(String(shared().interfaces())); }
export function tokenHash(value: string): string { return String(FaceclawRemoteInput.shared().tokenDigest(value)); } export function randomTokenSecret(): string { return String(shared().randomSecret()); }
export function tokenHash(value: string): string { return String(shared().tokenDigestValue(value)); }
export function copyRemoteToken(value: string): void { UIPasteboard.generalPasteboard.string = value; } export function copyRemoteToken(value: string): void { UIPasteboard.generalPasteboard.string = value; }
+12 -21
View File
@@ -1,28 +1,19 @@
import { type SseListener } from './sse-types'; import { type SseListener } from './sse-types';
import { withUserAgent } from '../util/http'; import { withUserAgent } from '../util/http';
import { kotlinListener } from './kotlin-listener.ios';
export { type SseListener } from './sse-types'; export { type SseListener } from './sse-types';
declare const FaceclawSseRequest: any; declare const FaceclawKitIosSseRequest: any, FaceclawKitFaceclawSseListener: any;
/** Drain native events on the requesting JS isolate, never on a URLSession thread. */ /** The shared Kotlin SseStream over NSURLSession; the Kotlin side delivers every callback on the main queue, in order. */
export function openSseRequest(url: string, body: string, headers: Record<string, string>, listener: SseListener): { cancel(): void } { export function openSseRequest(url: string, body: string, headers: Record<string, string>, listener: SseListener): { cancel(): void } {
const native = FaceclawSseRequest.alloc().initWithURLBodyHeaders(url, body, JSON.stringify(withUserAgent(headers)));
let ended = false; let ended = false;
const cancel = () => { if (ended) return; ended = true; clearInterval(timer); native.cancel(); }; const once = (deliver: () => void) => { if (ended) return; ended = true; deliver(); };
const timer = setInterval(() => { const nativeListener = kotlinListener(FaceclawKitFaceclawSseListener, {
try { onLineLine: (line: string) => { if (!ended) listener.onLine(String(line ?? '')); },
for (const event of JSON.parse(native.takeEvents())) { onHttpErrorCodeBody: (code: number, errorBody: string) => once(() => listener.onHttpError(Number(code), String(errorBody ?? ''))),
if (ended) break; onComplete: () => once(() => listener.onComplete()),
if (event.kind === 'line') listener.onLine(event.text); onFailureMessage_: (message: string) => once(() => listener.onFailure(String(message ?? ''))),
else { });
cancel(); const native = FaceclawKitIosSseRequest.alloc().initWithUrlBodyHeadersJsonListener(url, body, JSON.stringify(withUserAgent(headers)), nativeListener);
if (event.kind === 'complete') listener.onComplete(); return { cancel: () => { if (ended) return; ended = true; native.cancel(); } };
else if (event.kind === 'http-error') listener.onHttpError(event.code, event.text);
else listener.onFailure(event.text);
}
}
} catch (error) {
cancel(); listener.onFailure(String((error as Error)?.message ?? error));
}
}, 25);
return { cancel };
} }
-13
View File
@@ -1,13 +0,0 @@
import { fromData, toData, nativeArray } from './kotlin-data'
import type { SessionTexturePlanner, TextureFrame, TexturePlan } from '../g2/texture-planner'
declare const FaceclawKitIosTexturePlanner: any
export class IosTexturePlanner implements SessionTexturePlanner {
private readonly native = FaceclawKitIosTexturePlanner.new()
reset(): void { this.native.reset() }
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null {
const result = this.native.planPreviousNextFrameFirstId(previous ? toData(previous) : null, toData(next), frame.native, firstId)
return result ? { payload: fromData(result.payload), resourceCommands: nativeArray(result.resourceCommands, fromData),
nextFid: result.nextFid, usedBytes: result.usedBytes } : null
}
}
+8 -5
View File
@@ -62,11 +62,14 @@ that remains: the sherpa-onnx classes, which are the vendored Android JNI ABI.
PNG/GIF adapters retain only Android storage responsibilities; their codecs are PNG/GIF adapters retain only Android storage responsibilities; their codecs are
shared. shared.
On `ios-port`, platform adapters route the existing TypeScript session and UI On iOS (since 2026-09-22) `iosMain` also hosts the platform actuals for the shared
logic through shared Kotlin for protocol builders, CRC/framing/reassembly, cores: `ble/IosBleCentral` (CoreBluetooth, implements `SessionLink` and `StockLink`
event decoding, CFW compression/ACKs, changed-image regions, surface composition, plus scanning and ANCS authorization), `ble/IosSessionHost`, `ble/IosGlassesSession`
and LVGL glyphs. The original pure TypeScript implementations remain as Node (the facade `app/native/faceclaw-communicator.ios.ts` wraps), `ble/IosStockFlows`
test references; NativeScript selects the `.ios.ts` native adapters. (device-info probe, flash prompt, OTA flasher), `net/IosRemoteInput`,
`net/IosSseRequest`, `net/IosModelDownloader` and `IosFrameTimings`. Callbacks into
TypeScript are always dispatched to the main queue. The former pure-TypeScript
session/protocol duplicates were deleted; their coverage lives in `tests/kotlin`.
Texture rendering uses the same `GlyphAtlas`, `ImageAtlas`, `TextureCacheState` Texture rendering uses the same `GlyphAtlas`, `ImageAtlas`, `TextureCacheState`
and `TexturePlanner` on both platforms. iOS's `IosTextureAtlas` and and `TexturePlanner` on both platforms. iOS's `IosTextureAtlas` and
@@ -182,8 +182,14 @@ class IosBleCentral(private val platform: ProtocolPlatform = IosProtocolPlatform
central = CBCentralManager(delegate = delegate, queue = queue) central = CBCentralManager(delegate = delegate, queue = queue)
} }
/**
* Registers the scan/state listener and creates the central if needed, so a listener that
* only waits for readiness (no scan) still receives the state report; an existing central's
* current state is re-reported to the new listener on the main queue.
*/
fun setScanListener(listener: IosBleScanListener?) { fun setScanListener(listener: IosBleScanListener?) {
scanListener = listener scanListener = listener
if (listener != null && !closed) ensureCentral()
} }
/** Starts (or keeps) scanning for all peripherals with duplicates; events reach the scan listener on main. */ /** Starts (or keeps) scanning for all peripherals with duplicates; events reach the scan listener on main. */
@@ -15,6 +15,11 @@ interface IosAncsListener {
fun onRelayFrame(data: NSData) fun onRelayFrame(data: NSData)
} }
/** Audio packets for the NativeScript side as NSData (a Kotlin ByteArray crosses per byte). Main queue. */
interface IosAudioPacketListener {
fun onAudioPacket(data: NSData, arm: String, arrivalMs: Long)
}
/** /**
* ObjC-facing facade over the shared [GlassesSessionCore] for the NativeScript app: the iOS * ObjC-facing facade over the shared [GlassesSessionCore] for the NativeScript app: the iOS
* counterpart of Android's FaceclawBleCommunicator with the same method names. GATT goes * counterpart of Android's FaceclawBleCommunicator with the same method names. GATT goes
@@ -119,6 +124,16 @@ class IosGlassesSession internal constructor(
fun stopG2AudioCapture() = core.stopG2AudioCapture() fun stopG2AudioCapture() = core.stopG2AudioCapture()
/** As [startG2AudioCapture], with packets delivered as NSData on the main queue. */
fun startG2AudioCaptureNsData(listener: IosAudioPacketListener): Boolean =
core.startG2AudioCapture(NsDataAudioListener(listener))
/** The right arm's usable write payload (ATT MTU minus the 3-byte header), or 20 when unknown. */
fun rightWriteLimit(): Int {
val mtu = try { central?.negotiatedMtu(rightIdentifier) ?: 0 } catch (t: Throwable) { 0 }
return if (mtu > 3) mtu - 3 else 20
}
fun isAudioCaptureActive(): Boolean = core.isAudioCaptureActive() fun isAudioCaptureActive(): Boolean = core.isAudioCaptureActive()
fun setFaceclawWakeLeaseEnabled(enabled: Boolean): Boolean = core.setFaceclawWakeLeaseEnabled(enabled) fun setFaceclawWakeLeaseEnabled(enabled: Boolean): Boolean = core.setFaceclawWakeLeaseEnabled(enabled)
@@ -237,6 +252,14 @@ class IosGlassesSession internal constructor(
internal fun coreForTests(): GlassesSessionCore = core internal fun coreForTests(): GlassesSessionCore = core
private class NsDataAudioListener(private val delegate: IosAudioPacketListener) : FaceclawAudioPacketListener {
override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) {
val packet = (data ?: ByteArray(0)).data()
val label = arm ?: ""
dispatch_async(dispatch_get_main_queue()) { delegate.onAudioPacket(packet, label, arrivalMs) }
}
}
private class MainQueueAudioListener(private val delegate: FaceclawAudioPacketListener) : FaceclawAudioPacketListener { private class MainQueueAudioListener(private val delegate: FaceclawAudioPacketListener) : FaceclawAudioPacketListener {
override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) { override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) {
dispatch_async(dispatch_get_main_queue()) { delegate.onAudioPacket(data, arm, arrivalMs) } dispatch_async(dispatch_get_main_queue()) { delegate.onAudioPacket(data, arm, arrivalMs) }
-66
View File
@@ -1,66 +0,0 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { deflate } = require('pako');
const { buildBoundingBoxPayload: bbox } = require('../.test-build/app/g2/ble-image-optimizer.js');
const { applyDisplayPayload } = require('./helpers/display-payload.cjs');
const { rle4 } = require('../.test-build/app/g2/ble-protocol.js');
test('mode-3 payload matches the production Android encoder byte for byte', () => {
// Generated with javac from g2protocol/BleImageOptimizer.java, 2026-09-08.
const vectors = [
[0, 0, 0xab, '030000010134121a1b60'],
[319, 479, 0x12, '039fef01013412601112'],
[3, 5, 0xa5, '03010201013412601a15'],
];
for (const [x, y, value, expected] of vectors) {
const base = new Uint8Array(320 * 480), next = base.slice(); next[y * 320 + x] = value;
const payload = bbox(base, next, 640, 480, 0x1234);
assert.equal(Buffer.from(payload).toString('hex'), expected);
assert.deepEqual(applyDisplayPayload(base, payload), next);
}
});
test('bounding boxes include both nibbles, preserve neighboring pixels and cover all four screen edges', () => {
const base = Uint8Array.from({ length: 320 * 480 }, (_, i) => i & 255);
for (const x of [0, 1, 2, 3, 4, 637, 638, 639]) for (const y of [0, 1, 2, 477, 478, 479]) {
const next = base.slice(); next[y * 320 + (x >> 1)] ^= x % 2 ? 15 : 240;
const payload = bbox(base, next, 640, 480, 1);
assert.deepEqual([...payload.slice(1, 5)], [Math.floor(x / 4), Math.floor(y / 2), 1, 1]);
assert.deepEqual(applyDisplayPayload(base, payload), next);
}
});
test('unchanged, incompatible and full-screen boxes fall back without compressing a region', () => {
const base = new Uint8Array(320 * 480), next = base.slice(); next[0] = next[next.length - 1] = 255;
const compress = () => assert.fail('A fallback must not compress a bounding box');
assert.equal(bbox(base, base, 640, 480, 1, compress), null);
assert.equal(bbox(base, next, 640, 480, 1, compress), null);
assert.equal(bbox(null, next, 640, 480, 1, compress), null);
assert.equal(bbox(base.subarray(1), next, 640, 480, 1, compress), null);
assert.equal(bbox(base, next.subarray(1), 640, 480, 1, compress), null);
for (const [w, h] of [[0, 480], [639, 480], [640, 479], [1024, 480], [640, 512]])
assert.equal(bbox(base, next, w, h, 1, compress), null);
});
test('seeded sparse edits reconstruct exactly across successive bounding-box updates', () => {
let seed = 42;
const random = limit => { seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; return seed % limit; };
let base = Uint8Array.from({ length: 320 * 480 }, () => random(256));
for (let fid = 1; fid <= 60; fid++) {
const next = base.slice(), bx = random(280), by = random(440);
for (let n = 0; n < 20; n++) next[(by + random(40)) * 320 + bx + random(40)] ^= 0xff;
const payload = bbox(base, next, 640, 480, fid);
assert.ok(payload); assert.equal(payload[5] | (payload[6] << 8), fid);
assert.deepEqual(applyDisplayPayload(base, payload), next);
base = next;
}
});
test('a small edit encodes only its region on a busy screen', () => {
const base = Uint8Array.from({ length: 320 * 480 }, (_, i) => i & 255), next = base.slice();
next[100 * 320 + 100] ^= 255;
const payload = bbox(base, next, 640, 480, 1);
assert.ok(payload.length <= 15);
assert.ok(payload.length < (1 + rle4(next).length) / 10);
assert.deepEqual(applyDisplayPayload(base, payload), next);
});
-53
View File
@@ -1,53 +0,0 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
const { CfwTransport, parseCfwAcks } = require('../.test-build/app/g2/cfw-transport.js');
const { crc16, concat, MessageReceiver } = require('../.test-build/app/g2/ble-protocol.js');
const hex = value => Buffer.from(value).toString('hex');
test('iOS transport matches Android packets including persistent compression, reset, raw fallback and ATT limits', () => {
const random = new Uint8Array(65535); let seed = 42;
for (let i = 0; i < random.length; i++) { seed ^= seed << 13; seed ^= seed >>> 17; seed ^= seed << 5; random[i] = seed & 255; }
const cases = [[100, 3, 185, new Uint8Array([6, 0, 0, 255, 255])],
[101, 3, 185, new Uint8Array([6, 0, 0, 255, 255])], [102, 1, 20, new Uint8Array(500).fill(42)],
'reset', [255, 3, 514, random], [103, 3, 240, new Uint8Array([11])]];
// Captured from the original Java encoder before migration; production now uses Kotlin.
const expected = require('./fixtures/cfw-java-golden.json');
const hash = value => require('node:crypto').createHash('sha256').update(value).digest('hex');
const transport = new CfwTransport(); let index = 0;
for (const c of cases) {
if (c === 'reset') { transport.reset(); continue; }
const [id, lenses, maxWrite, payload] = c, packets = transport.encode(payload, id, lenses, maxWrite);
assert.ok(packets.every(p => p.length <= maxWrite));
assert.equal(hash(packets.map(hex).join(' ')), expected[index++]);
}
});
function ackPacket(records, lens = 1, nack = false) {
const words = (id, ordinal, size, crc) => new Uint8Array([id, ordinal & 255, ordinal >>> 8, size & 255, size >>> 8, crc & 255, crc >>> 8]);
const first = words(...records[0]);
const body = concat(new Uint8Array([nack ? 3 : 1]), first.subarray(0, 3), new Uint8Array([lens]),
first.subarray(3), ...records.slice(1).map(r => words(...r)));
const crc = crc16(body);
return concat(new Uint8Array([0xaa, 0x12, 50, body.length + 2, 1, 1, 0xf0, 0]), body, new Uint8Array([crc & 255, crc >>> 8]));
}
test('redundant ACKs are validated atomically and survive split/coalesced notifications', () => {
const packet = ackPacket([[104, 0, 50, 123], [103, 0, 40, 321], [102, 0, 30, 222], [101, 0, 20, 111]], 2);
assert.deepEqual(parseCfwAcks(packet).map(a => [a.streamId, a.lens, a.size]), [[104, 2, 50], [103, 2, 40], [102, 2, 30], [101, 2, 20]]);
const receiver = new MessageReceiver();
assert.deepEqual(receiver.receive('L', packet.subarray(0, 6)), []);
const messages = receiver.receive('L', concat(packet.subarray(6), packet));
assert.equal(messages.length, 2); assert.deepEqual(parseCfwAcks(messages[0].packet), parseCfwAcks(packet));
for (let offset = 0; offset < packet.length; offset++) {
const corrupt = packet.slice(); corrupt[offset] ^= 1;
// Transport sequence byte is not covered by the ACK's CRC or identity.
if (offset !== 2) assert.equal(parseCfwAcks(corrupt), null, `offset ${offset}`);
}
assert.equal(parseCfwAcks(ackPacket([[104, 0, 50, 123]], 3)), null);
assert.equal(parseCfwAcks(ackPacket([[104, 0, 50, 123], [103, 0, 40, 321]], 1, true)), null);
assert.equal(parseCfwAcks(ackPacket([[104, 0, 0, 0]], 1, true))[0].nack, true);
});
+64 -41
View File
@@ -82,7 +82,7 @@ test('iOS host forwards hands-free capture mode to the voice bridge', async () =
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '../native/ios-voice-input' const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '../native/ios-voice-input'
? { iosVoiceInput: { startGlassesCapture: (_session, _log, endpointing) => captures.push(endpointing) } } : {} }); ? { iosVoiceInput: { startGlassesCapture: (_session, _log, endpointing) => captures.push(endpointing) } } : {} });
const host = Object.create(IosPreviewController.prototype); const host = Object.create(IosPreviewController.prototype);
host.session = { state: { phase: 'connected' } }; host.communicator = {}; host.state = { phase: 'connected' };
await host.startVoiceCapture(true); await host.startVoiceCapture(true);
await host.startVoiceCapture(); await host.startVoiceCapture();
assert.deepEqual(captures, [true, false]); assert.deepEqual(captures, [true, false]);
@@ -207,22 +207,43 @@ test('settings-driven repaint runs after font cache invalidation, regardless of
test('background glasses input still composites frames; phone resume preserves the session; explicit stop stays stopped', async () => { test('background glasses input still composites frames; phone resume preserves the session; explicit stop stays stopped', async () => {
const tasks = new Map(), screenStates = [], inputs = [], frames = [], previews = [], states = [], sounds = []; const tasks = new Map(), screenStates = [], inputs = [], frames = [], previews = [], states = [], sounds = [];
let nextTask = 0, starts = 0, stops = 0, pollStarts = 0, pollStops = 0, session; let nextTask = 0, starts = 0, stops = 0, pollStarts = 0, pollStops = 0, bridge;
const window = { windowId: 'launcher', surfaceId: 'launcher', appId: 'launcher', title: 'Apps', const window = { windowId: 'launcher', surfaceId: 'launcher', appId: 'launcher', title: 'Apps',
setScreenOn: on => screenStates.push(on), requestRender() {} }; setScreenOn: on => screenStates.push(on), requestRender() {} };
const shell = { configure() {}, registerWindow() {}, wake() {}, focusWindow() {}, const shell = { configure() {}, registerWindow() {}, wake() {}, focusWindow() {},
getWindows: () => [window], foregroundWindow: () => window, isScreenOn: () => true, getWindows: () => [window], foregroundWindow: () => window, isScreenOn: () => true,
setBatteryLevels() {}, paintScene() { return new Uint8Array([0,0]); }, paintSurface() {}, underlayDim: () => 0, getFocus: () => 'app', setBatteryLevels() {}, paintScene() { return new Uint8Array([0,0]); }, paintSurface() {}, underlayDim: () => 0, getFocus: () => 'app',
receiveInput: async input => { inputs.push(input); } }; receiveInput: async input => { inputs.push(input); } };
class Session { // The shared Kotlin session, as seen through the iOS bridge: frames reach it
state = { phase: 'disconnected' }; // only while connected, callbacks come back on the JS thread.
constructor(_transport, onState, onInput) { session = this; this.onState = onState; this.onInput = onInput; } const noop = () => () => {};
async start() { starts++; this.state = { phase: 'connected' }; this.onState(this.state); } class FaceclawCommunicatorBridge {
async stop() { stops++; this.state = { phase: 'disconnected' }; this.onState(this.state); } phase = 'disconnected';
async setBrightness() {} constructor() { bridge = this; }
setFrame(pixels) { if (this.state.phase === 'connected') frames.push(pixels); } onStateChange(fn) { this.stateListener = fn; return noop(); }
playBuzzerSequence(payload) { sounds.push([...payload]); } onRingEvent(fn) { this.ringListener = fn; return noop(); }
wake() {} onBatteryState() { return noop(); } onFirmwareInfo() { return noop(); } onFrameMetrics() { return noop(); }
onWearState() { return noop(); } addCompassListener() { return noop(); } onAncsRelayFrame() { return noop(); }
onAncsAuthorization() { return noop(); } setRequiresAncs() {} rightWriteLimit() { return 20; } onPhoneLockSignal() {}
async writeRawToRight() {}
async start() { starts++; this.phase = 'connected'; this.stateListener({ phase: 'connected', status: 'Connected.' }); }
async disconnect() { stops++; this.phase = 'disconnected'; this.stateListener({ phase: 'disconnected', status: 'Disconnected.' }); }
async close() {}
async setBrightness() {} async enableWearDetectionAndRequestState() {}
async configureCompositorScreen() {} async configureSurface() {} async setUnderlayDim() {} async setSurfaceVisible() {}
async setScreenBlanked() {} async removeSurface() {}
async submitSurfaceFrame(_id, pixels) { if (this.phase === 'connected') frames.push(pixels); }
async submitShellScene(bytes) { if (this.phase === 'connected') frames.push(bytes); }
waitForFrameFinished() { return Promise.resolve('sent'); }
getCompositePreview() { return new Uint8Array([1, 2]); }
async playBuzzerSequence(payload) { sounds.push([...payload]); }
}
class PreviewDisplayTarget {
activate() {} release() {}
async configureCompositorScreen() {} async configureSurface() {} async setUnderlayDim() {} async setSurfaceVisible() {}
async setScreenBlanked() {} async removeSurface() {} async submitSurfaceFrame() {} async submitShellScene() {}
waitForFrameFinished() { return Promise.resolve('composited'); }
getCompositePreview() { return new Uint8Array([1, 2]); }
} }
const settings = { brightnessSetting: { get: () => 'auto' }, brightnessSettingToLevel: () => null, lockScreenEnabledSetting: { get: () => true }, onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } }; const settings = { brightnessSetting: { get: () => 'auto' }, brightnessSettingToLevel: () => null, lockScreenEnabledSetting: { get: () => true }, onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
const modules = { const modules = {
@@ -238,22 +259,20 @@ test('background glasses input still composites frames; phone resume preserves t
'../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} }, '../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path }, '@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {}, stopPhoneCapture() {} } }, '../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {}, stopPhoneCapture() {} } },
'../native/ios-bluetooth': { iosBluetooth: () => ({}) }, './glasses-session': { GlassesSession: Session }, '../native/faceclaw-communicator.ios': { FaceclawCommunicatorBridge, resolveIosPeripherals: async addresses => addresses },
'../native/preview-display.ios': { PreviewDisplayTarget },
'./ancs-client': { ANCS_FIRMWARE_VERSION: 16, AncsClient: class { state = 'disconnected'; start() {} stop() {} stopCommand() { return new Uint8Array(); } receive() { return false; } } },
'../native/nightscout-bridge': { nightscoutBridge: { async start() { pollStarts++; }, async stop() { pollStops++; } } }, '../native/nightscout-bridge': { nightscoutBridge: { async start() { pollStarts++; }, async stop() { pollStops++; } } },
'./glance-host': { GlanceHost: class { dismiss() {} isVisible() { return false; } } }, './glance-host': { GlanceHost: class { dismiss() {} reset() {} isVisible() { return false; } } },
'./device-addresses': { loadDeviceAddresses: () => ({}) }, './ios-peripheral-identity': { deviceAddressError: () => null }, './device-addresses': { loadDeviceAddresses: () => ({ right: 'AA', left: 'BB', ring: '' }) }, './ios-peripheral-identity': { deviceAddressError: () => null },
'../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' }, '../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' },
'../apps/launcher': { launcherEntries: () => [] }, '../apps/launcher': { launcherEntries: () => [] },
'../apps/all-apps': { ALL_APPS: [] }, '../ui/dashboard-settings': settings, '../apps/all-apps': { ALL_APPS: [] }, '../ui/dashboard-settings': settings,
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) }, '../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
'../graphics/surface-compositor': { SurfaceCompositor: class { '../graphics/plane': { flattenPlanesWithDraws: () => ({ image: { pixels: new Uint8Array([1, 2]), width: 2, height: 1 }, draws: [] }), planesFingerprint: () => 'fp' },
setShellScene() {} configureSurface() {} setSurfaceVisible() {} submitSurfaceFrame() {} setUnderlayDim() {} setScreenBlanked() {}
compositeFrame() { return { pixels: new Uint8Array([1, 2]) }; }
} },
'../graphics/plane': { flattenPlanesWithDraws: () => ({ image: { pixels: new Uint8Array([1, 2]), width: 2, height: 1 }, draws: [] }) },
'../graphics/glyph-wire': { prepareFrameDraws: () => null }, '../graphics/glyph-wire': { prepareFrameDraws: () => null },
'../native/texture-planner.ios': { IosTexturePlanner: class {} }, '../graphics/image': { G2_LENS_WIDTH: 640, G2_LENS_HEIGHT: 480 },
'../native/ios-graphics': { previewPixels: pixels => pixels }, './lock-screen': { LOCK_SCREEN_SURFACE_ID: 'lock-screen', createLockScreenImage: () => ({ width: 1, height: 1, to8bppBuffer: () => new Uint8Array(1) }) },
'../ui/shell/shell': { shell, rawInputEventToInputEvent: input => input }, '../ui/shell/shell': { shell, rawInputEventToInputEvent: input => input },
'../ui/shell/geometry': { appViewportRect: () => ({ x: 0, y: 0, width: 640, height: 480 }) }, '../ui/shell/geometry': { appViewportRect: () => ({ x: 0, y: 0, width: 640, height: 480 }) },
pako: { deflate: x => x }, pako: { deflate: x => x },
@@ -269,29 +288,32 @@ test('background glasses input still composites frames; phone resume preserves t
NSOperationQueue: { mainQueue: {} }, NSOperationQueue: { mainQueue: {} },
}); });
const flush = () => { for (const [id, fn] of [...tasks]) { if (tasks.delete(id)) fn(); } }; const flush = () => { for (const [id, fn] of [...tasks]) { if (tasks.delete(id)) fn(); } };
const settle = async () => { for (let i = 0; i < 4; i++) { flush(); await new Promise(resolve => setImmediate(resolve)); } };
const controller = new api.IosPreviewController(image => previews.push(image), assert.fail, state => states.push(state)); const controller = new api.IosPreviewController(image => previews.push(image), assert.fail, state => states.push(state));
await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0])); await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0]));
assert.equal(sounds.length, 0); assert.equal(sounds.length, 0);
controller.resume(); await controller.connect(); flush(); controller.resume(); await controller.connect(); await settle();
await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0])); await controller.actions.playBuzzerSequence(new Uint8Array([5, 4, 0]));
assert.deepEqual(sounds, [[5, 4, 0]]); assert.deepEqual(sounds, [[5, 4, 0]]);
const previewCount = previews.length, stateCount = states.length; const previewCount = previews.length, stateCount = states.length;
controller.pause(); controller.pause(); // NativeScript also unloads its root page on background entry. controller.pause(); controller.pause(); // NativeScript also unloads its root page on background entry.
assert.equal(stops, 0); assert.ok(screenStates.every(Boolean)); assert.equal(stops, 0); assert.ok(screenStates.every(Boolean));
assert.equal(pollStarts, 1); assert.equal(pollStops, 0, 'connected glasses keep Nightscout polling in background'); assert.equal(pollStarts, 1); assert.equal(pollStops, 0, 'connected glasses keep Nightscout polling in background');
session.onInput({ eventType: 3, eventSource: 1 }); const frameCount = frames.length;
await controller.inputQueue; flush(); bridge.ringListener({ kind: 'sys-event', containerName: '', eventType: 3, eventSource: 1, systemExitReasonCode: 0, frameId: 0 });
assert.equal(inputs.length, 1); assert.ok(frames.length >= 2); await controller.inputQueue; await settle();
assert.equal(inputs.length, 1); assert.ok(frames.length > frameCount, 'background input still composites frames');
assert.equal(previews.length, previewCount); assert.equal(states.length, stateCount); assert.equal(previews.length, previewCount); assert.equal(states.length, stateCount);
controller.resume(); flush(); controller.resume(); await settle();
assert.equal(starts, 1); assert.ok(previews.length > previewCount); assert.equal(starts, 1); assert.ok(previews.length > previewCount);
controller.pause(); await controller.disconnect(); flush(); controller.pause(); await controller.disconnect(); await settle();
assert.equal(stops, 1); assert.equal(screenStates.at(-1), false); assert.equal(stops, 1); assert.equal(screenStates.at(-1), false);
assert.equal(pollStops, 1); assert.equal(pollStops, 1);
const frameCount = frames.length; const stoppedFrames = frames.length;
session.onInput({ eventType: 3, eventSource: 1 }); await controller.inputQueue; flush(); bridge.ringListener({ kind: 'sys-event', containerName: '', eventType: 3, eventSource: 1, systemExitReasonCode: 0, frameId: 0 });
assert.equal(inputs.length, 1); assert.equal(frames.length, frameCount); await controller.inputQueue; await settle();
controller.resume(); flush(); assert.equal(inputs.length, 1); assert.equal(frames.length, stoppedFrames);
controller.resume(); await settle();
assert.equal(starts, 1); assert.equal(controller.connectionState.phase, 'disconnected'); assert.equal(starts, 1); assert.equal(controller.connectionState.phase, 'disconnected');
assert.equal(pollStarts, 2, 'resuming the phone restarts Nightscout polling'); assert.equal(pollStarts, 2, 'resuming the phone restarts Nightscout polling');
}); });
@@ -368,21 +390,22 @@ test('iOS configures an in-process surface before submitting constructor-trigger
require: name => ({ require: name => ({
'../ui/shell/shell': { shell }, '../ui/shell/shell': { shell },
'../ui/shell/geometry': { appViewportRect: () => ({ x: 0, y: 0, width: 2, height: 1 }) }, '../ui/shell/geometry': { appViewportRect: () => ({ x: 0, y: 0, width: 2, height: 1 }) },
'../graphics/plane': { flattenPlanesWithDraws: planes => ({ image: planes[0], draws: [] }) }, '../graphics/plane': { flattenPlanesWithDraws: planes => ({ image: planes[0], draws: [] }), planesFingerprint: () => 'fp' },
'../graphics/glyph-wire': { prepareFrameDraws: () => null }, '../graphics/glyph-wire': { prepareFrameDraws: () => null },
})[name] ?? {}, })[name] ?? {},
console, console,
}); });
// Exercise the real launch method with a strict compositor boundary. App // Exercise the real launch method with a strict display boundary. App
// construction paints synchronously, as Nightscout's tray subscription does. // construction paints synchronously, as Nightscout's tray subscription does.
const controller = Object.create(api.IosPreviewController.prototype); const controller = Object.create(api.IosPreviewController.prototype);
controller.inProcessApps = new Map(); controller.inProcessApps = new Map();
controller.requestShellRender = () => {}; controller.requestShellRender = () => {};
controller.scheduleFrame = () => {}; controller.schedulePreviewUpdate = () => {};
controller.compositor = { controller.communicator = null;
configureSurface(id) { surfaces.add(id); }, setSurfaceVisible() {}, controller.previewTarget = {
removeSurface(id) { surfaces.delete(id); }, async configureSurface(id) { surfaces.add(id); }, async setSurfaceVisible() {},
submitSurfaceFrame(id, pixels) { assert.ok(surfaces.has(id), `unconfigured ${id}`); submitted.push([...pixels]); }, async removeSurface(id) { surfaces.delete(id); },
async submitSurfaceFrame(id, pixels) { assert.ok(surfaces.has(id), `unconfigured ${id}`); submitted.push([...pixels]); },
}; };
let plumbing, early; let plumbing, early;
const fresh = [{ pixels: new Uint8Array([30, 40]), width: 2, height: 1 }]; const fresh = [{ pixels: new Uint8Array([30, 40]), width: 2, height: 1 }];
@@ -453,14 +476,14 @@ test('iOS preview voice uses the phone microphone, while connected capture stays
require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : {}, require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : {},
}); });
const host = Object.create(IosPreviewController.prototype); const host = Object.create(IosPreviewController.prototype);
host.active = true; host.glassesLocked = false; host.active = true; host.glassesLocked = false; host.communicator = null; host.state = { phase: 'disconnected' };
assert.equal(await host.prepareVoiceCapture(), true); assert.equal(await host.prepareVoiceCapture(), true);
await host.startVoiceCapture(true); await host.startVoiceCapture(true);
assert.deepEqual(calls, [['prepare', true, true], ['prepare', true, true], ['phone', true]]); assert.deepEqual(calls, [['prepare', true, true], ['prepare', true, true], ['phone', true]]);
calls.length = 0; host.session = { state: { phase: 'connected' } }; calls.length = 0; host.communicator = {}; host.state = { phase: 'connected' };
assert.equal(await host.prepareVoiceCapture(), true); await host.startVoiceCapture(false); assert.equal(await host.prepareVoiceCapture(), true); await host.startVoiceCapture(false);
assert.deepEqual(calls, [['prepare', true, false], ['glasses', false]]); assert.deepEqual(calls, [['prepare', true, false], ['glasses', false]]);
calls.length = 0; host.active = false; host.session = null; calls.length = 0; host.active = false; host.communicator = null; host.state = { phase: 'disconnected' };
assert.equal(await host.prepareVoiceCapture(), false); await host.startVoiceCapture(); assert.equal(await host.prepareVoiceCapture(), false); await host.startVoiceCapture();
assert.deepEqual(calls, []); assert.deepEqual(calls, []);
}); });
+18 -14
View File
@@ -77,23 +77,27 @@ for (const provider of ['openai', 'anthropic']) {
}); });
} }
test('iOS SSE adapter invokes listeners on its polling thread and stops polling at cancellation/EOF', () => { test('iOS SSE adapter forwards the Kotlin stream events once and stops after cancellation/EOF', () => {
const timers = new Set(), instances = [], seen = []; const instances = [], seen = [];
const nsObject = { extend: methods => ({ new: () => ({ ...methods }) }) };
const listenerModule = loader({ NSObject: nsObject }, {})('app/native/kotlin-listener.ios.ts');
const api = loader({ const api = loader({
setInterval: fn => { timers.add(fn); return fn; }, clearInterval: fn => timers.delete(fn), FaceclawKitIosSseRequest: { alloc: () => ({ initWithUrlBodyHeadersJsonListener(url, body, headers, listener) {
FaceclawSseRequest: { alloc: () => ({ initWithURLBodyHeaders(url, body, headers) { const instance = { listener, cancelled: false, cancel() { this.cancelled = true; } };
const instance = { events: [], cancelled: false, takeEvents() { const events = this.events; this.events = []; return JSON.stringify(events); }, cancel() { this.cancelled = true; } }; instances.push(instance); assert.equal(JSON.parse(headers)['User-Agent'], 'fixture'); assert.equal(url, 'https://fixture'); return instance;
instances.push(instance); assert.equal(JSON.parse(headers)['User-Agent'], 'fixture'); return instance; } }) }, FaceclawKitFaceclawSseListener: {},
} }) }, }, { '../util/http': { withUserAgent: headers => ({ ...headers, 'User-Agent': 'fixture' }) }, './kotlin-listener.ios': listenerModule })('app/native/sse.ios.ts');
}, { '../util/http': { withUserAgent: headers => ({ ...headers, 'User-Agent': 'fixture' }) } })('app/native/sse.ios.ts'); const listener = { onLine: line => seen.push(line), onComplete: () => seen.push('complete'), onHttpError: (code, body) => seen.push(`http:${code}:${body}`), onFailure: error => seen.push(`fail:${error}`) };
const listener = { onLine: line => seen.push(line), onComplete: () => seen.push('complete'), onHttpError() {}, onFailure: error => assert.fail(error) };
const handle = api.openSseRequest('https://fixture', '{}', {}, listener); const handle = api.openSseRequest('https://fixture', '{}', {}, listener);
instances[0].events.push({ kind: 'line', text: 'first' }); assert.deepEqual(seen, []); instances[0].listener.onLineLine('first'); assert.deepEqual(seen, ['first']);
for (const tick of timers) tick(); assert.deepEqual(seen, ['first']); handle.cancel(); handle.cancel(); assert.equal(instances[0].cancelled, true);
handle.cancel(); assert.equal(timers.size, 0); assert.equal(instances[0].cancelled, true); instances[0].listener.onLineLine('after cancel'); instances[0].listener.onComplete(); assert.deepEqual(seen, ['first']);
api.openSseRequest('https://fixture', '{}', {}, listener); api.openSseRequest('https://fixture', '{}', {}, listener);
instances[1].events.push({ kind: 'line', text: 'last' }, { kind: 'complete' }); instances[1].listener.onLineLine('last'); instances[1].listener.onComplete(); instances[1].listener.onFailureMessage_('late');
for (const tick of [...timers]) tick(); assert.deepEqual(seen, ['first', 'last', 'complete']); assert.equal(timers.size, 0); assert.deepEqual(seen, ['first', 'last', 'complete']);
api.openSseRequest('https://fixture', '{}', {}, listener);
instances[2].listener.onHttpErrorCodeBody(429, 'slow down'); instances[2].listener.onComplete();
assert.deepEqual(seen.at(-1), 'http:429:slow down');
}); });
test('iOS only offers cloud assistant models and preserves old local-model conversations as Auto', () => { test('iOS only offers cloud assistant models and preserves old local-model conversations as Auto', () => {
-751
View File
@@ -1,751 +0,0 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const p = require('../.test-build/app/g2/ble-protocol.js');
const { identifyIosPeripheral, deviceAddressError } = require('../.test-build/app/g2/ios-peripheral-identity.js');
const { GlassesSession } = require('../.test-build/app/g2/glasses-session.js');
const { deflate, inflate } = require('pako');
const { inflateSync, constants } = require('node:zlib');
const { REQUIRED_FACECLAW_FIRMWARE_VERSION } = require('../.test-build/app/g2/firmware-compat.js');
const hex = bytes => Buffer.from(bytes).toString('hex');
const string = (field, text) => p.bytes(field, new Uint8Array(Buffer.from(text)));
// Generated by compiling the existing Android BleProtocol.java/BmpUtil.java,
// plus BleImageOptimizer's unchanged RLE method, with javac 25.0.2.
test('iOS/shared protocol matches Android-generated wire vectors byte for byte', () => {
const vectors = [
[p.authentication(101), '080410651a0408011004'],
[p.prelude(), '0802109c01220a1a081206120408001000'],
[p.createLayout(102), '080010661a2308011a1c0800100018c00420a0024801520964617368626f617264580162012028904e'],
[p.heartbeat(103), '080c106772020800'],
[p.settingsQuery(104), '0802106822020801'],
[p.shutdown(105), '080910695a020801'],
[p.framebufferLease(true), '08011000aa0606464301050000'],
[p.framebufferLease(false), '08011000aa0606464301060000'],
[p.frameMessage(p.authentication(101), 128, 0, 64)[0], 'aa21400c01018000080410651a04080110041024'],
];
for (const [actual, expected] of vectors) assert.equal(hex(actual), expected);
const packed = p.packGray4(new Uint8Array([0, 1, 7, 8, 15, 16, 23, 24, 127, 128, 247, 255]), 3, 4);
assert.equal(hex(packed), '0000111012808ff0');
assert.equal(hex(p.rle4(packed)), '40311011121810182f10');
});
test('negotiated-MTU framing reassembles split and coalesced notifications and rejects bad CRC', () => {
const payload = p.concat(p.integer(1, 3), p.integer(2, 110), p.bytes(8, new Uint8Array(3900).fill(55)));
const frames = p.frameMessage(payload, 0xe0, 0x20, 17, 185);
assert.ok(frames.every(f => f.length <= 185));
const receiver = new p.MessageReceiver(), stream = p.concat(...frames);
assert.deepEqual(receiver.receive('L', stream.slice(0, 7)), []);
const messages = receiver.receive('L', stream.slice(7));
assert.equal(messages.length, 1); assert.deepEqual(messages[0].payload, payload);
stream[stream.length - 1] ^= 1;
assert.deepEqual(receiver.receive('L', stream), []);
assert.throws(() => p.frameMessage(payload, 1, 0, 1, 10), /too small/);
assert.throws(() => p.readBytes(new Uint8Array([0x1a, 0x7f]), 3), /Truncated/);
});
test('interleaved links and missing/expired fragments cannot produce false ACKs', () => {
const payload = p.concat(p.integer(1, 3), p.integer(2, 110), p.bytes(8, new Uint8Array(300)));
const frames = p.frameMessage(payload, 224, 0, 17), receiver = new p.MessageReceiver();
assert.deepEqual(receiver.receive('L', frames[0], 0), []);
assert.deepEqual(receiver.receive('R', frames[1], 1), []);
assert.deepEqual(receiver.receive('L', frames[1], 6000), []);
assert.equal(receiver.receive('L', p.concat(...frames), 6001).length, 1);
});
test('full-frame RLE splits 65535-pixel runs and preserves opaque near-black', () => {
const packed = p.packGray4(new Uint8Array(640 * 480).fill(1), 640, 480);
assert.ok(packed.every(v => v === 0));
const encoded = p.rle4(packed);
let count = 0, offset = 0;
while (offset < encoded.length) {
const token = encoded[offset++]; let run = token >>> 4;
if (!run) { run = encoded[offset++]; if (!run) { run = encoded[offset] | (encoded[offset + 1] << 8); offset += 2; } }
assert.equal(token & 15, 0); count += run;
}
assert.equal(count, 640 * 480);
assert.deepEqual(inflate(deflate(encoded)), encoded);
});
test('iOS mapping requires the full advertised MAC, never just a name suffix', () => {
const raw = { identifier: '8A2DD845-B208-F292-EB77-F4645C5AC42F', name: 'Even G2_32_L_B6ECE0',
manufacturerData: '4552' + Buffer.from('S211GBBC180304').toString('hex') + 'e0ecb61412e001', rssi: -45, connectable: true };
const device = identifyIosPeripheral(raw);
assert.equal(device.address, 'E0:12:14:B6:EC:E0'); assert.equal(device.role, 'left');
assert.equal(device.identifier, raw.identifier); assert.equal(device.serial, 'S211GBBC180304');
assert.equal(identifyIosPeripheral({ ...raw, manufacturerData: '' }), null);
assert.equal(identifyIosPeripheral({ ...raw, identifier: 'not-a-uuid' }), null);
assert.match(deviceAddressError({ left: device.address, right: device.address, ring: '' }), /different/);
assert.equal(deviceAddressError({ left: device.address, right: 'AA:BB:CC:DD:EE:FF', ring: '' }), null);
});
test('ring and arm input decoders preserve gesture and source', () => {
assert.equal(p.decodeRingInput(new Uint8Array([255, 4, 2])).eventType, 3);
assert.equal(p.decodeRingInput(new Uint8Array([255, 5, 0])).eventType, 2);
assert.equal(p.decodeRingInput(new Uint8Array([255, 5, 2])).eventType, 1);
assert.equal(p.decodeRingInput(new Uint8Array([0, 9, 97, 0, 8, 0, 0, 0, 0, 0, 0])).eventType, 10);
assert.equal(p.decodeRingInput(new Uint8Array([255, 4, 99])), null);
const payload = p.bytes(13, p.bytes(3, p.concat(p.integer(1, 9), p.integer(2, 3))));
assert.equal(p.decodeGlassesInput({ sid: 224, flag: 1, payload }).eventSource, 3);
assert.equal(p.decodeGlassesInput({ sid: 224, flag: 32, payload }), null);
const headUp = p.bytes(13, p.bytes(3, p.concat(p.integer(1, 12), p.integer(2, 1))));
const wake = p.decodeGlassesInput({ sid: 224, flag: 1, payload: headUp });
assert.equal(wake.kind, 'display-wake');
assert.equal(wake.eventType, 12);
assert.equal(wake.eventSource, 1);
// The stock G2 sender omits source for dedicated Faceclaw event 14.
const press = p.decodeGlassesInput({ sid: 224, flag: 1,
payload: p.bytes(13, p.bytes(3, p.integer(1, 14))) });
assert.equal(press.kind, 'sys-event');
assert.equal(press.eventType, 14);
assert.equal(press.eventSource, 0);
});
class FakeTransport {
events = new Set(); receiver = new p.MessageReceiver(); sent = []; subscriptions = []; closed = [];
hold = false; held = []; stock = false; cfw = new Map();
onEvent(fn) { this.events.add(fn); return () => this.events.delete(fn); }
emit(event) { for (const fn of [...this.events]) fn(event); }
async resolveDevices() { return { left: 'L', right: 'R', ring: 'ring' }; }
async connect(id) { return { characteristics: id === 'ring' ? p.RING_NOTIFY : [p.G2_WRITE, p.G2_NOTIFY], maxWrite: 185 }; }
async subscribe(id, characteristic) { this.subscriptions.push([id, characteristic]); }
disconnect(id) { this.closed.push(id); this.emit({ kind: 'disconnected', identifier: id }); }
stopScan() {}
ack(id, message, overrides = {}) {
if (message.sid === p.SID.cfw) {
for (const lens of overrides.lenses ?? [1, 2]) {
const size = overrides.size ?? message.payload.length, crc = overrides.checksum ?? p.crc16(message.payload);
const body = new Uint8Array([overrides.nack ? 3 : 1, overrides.magic ?? message.magic, overrides.ordinal ?? 0, 0, lens, size & 255, size >>> 8, crc & 255, crc >>> 8]);
const checksum = p.crc16(body);
const packet = p.concat(new Uint8Array([0xaa, 0x12, 0, 11, 1, 1, overrides.sid ?? p.SID.cfw, 0]), body, new Uint8Array([checksum & 255, checksum >>> 8]));
this.emit({ kind: 'notification', identifier: overrides.identifier ?? id, characteristic: p.G2_NOTIFY, data: hex(packet) });
}
return;
}
let body = new Uint8Array();
if (message.sid === p.SID.auth) body = p.bytes(3, new Uint8Array());
if (message.sid === p.SID.settings) body = p.concat(p.bytes(4, p.concat(string(5, '2.2.9.22'), string(6, '2.2.9.22'), p.integer(12, 90), p.integer(13, 0))), string(100, this.stock ? '' : `Faceclaw/${this.firmware ?? REQUIRED_FACECLAW_FIRMWARE_VERSION}`));
const responseCommand = message.sid === p.SID.launch ? 1 : message.sid === p.SID.hub && message.command === 0 ? 1 : message.sid === p.SID.hub && message.command === 3 ? 4 : message.command;
const payload = p.concat(p.integer(1, responseCommand), p.integer(2, overrides.magic ?? message.magic), body);
const data = hex(p.concat(...p.frameMessage(payload, overrides.sid ?? message.sid, 0, 12)));
// The right/master link also carries ACKs for image writes to L.
this.emit({ kind: 'notification', identifier: overrides.identifier ?? 'R', characteristic: p.G2_NOTIFY, data });
}
async write(id, characteristic, frame) {
assert.equal(characteristic, p.G2_WRITE); assert.ok(frame.length <= 185);
if (frame[0] === 65 && frame[1] === 78) { (this.ancs ??= []).push({ id, frame: new Uint8Array(frame) }); return; }
let messages;
if (frame[6] === p.SID.cfw) {
assert.equal(p.crc16(frame.subarray(8, -2)), frame[frame.length - 2] | frame[frame.length - 1] << 8);
let state = this.cfw.get(id) ?? { history: Buffer.alloc(0), decoded: 0 };
if (frame[8] & 0x80) { state.record = []; state.magic = frame[2]; }
state.record.push(frame.subarray(9, -2)); this.cfw.set(id, state);
if (!(frame[8] & 0x40)) return;
const record = p.concat(...state.record), flags = record[0];
assert.equal(record.length - 5, record[1] | record[2] << 8);
if (flags & 8) { state.history = Buffer.alloc(0); state.decoded = 0; }
let payload = record.subarray(5);
if (flags & 4) {
state.history = Buffer.concat([state.history, payload]);
const all = inflateSync(state.history, { finishFlush: constants.Z_SYNC_FLUSH });
payload = new Uint8Array(all.subarray(state.decoded)); state.decoded = all.length;
}
assert.equal(p.crc16(payload), record[3] | record[4] << 8);
messages = [{ sid: p.SID.cfw, magic: state.magic, payload, flags, command: -1 }];
} else messages = this.receiver.receive(id, frame);
for (const message of messages) {
this.sent.push({ id, message });
if (!message.magic) continue;
if (this.hold) this.held.push({ id, message });
else queueMicrotask(() => this.ack(id, message));
}
}
}
const addresses = { right: 'AA:BB:CC:DD:EE:01', left: 'AA:BB:CC:DD:EE:02', ring: '' };
async function until(predicate) {
for (let i = 0; i < 100; i++) { if (predicate()) return; await new Promise(resolve => setTimeout(resolve, 5)); }
assert.fail('Timed out waiting for test state');
}
function harness(t, transport = new FakeTransport(), textures) {
const states = [], input = [], compass = [], wear = [];
const session = new GlassesSession(transport, state => states.push(state), event => input.push(event), undefined, undefined, event => compass.push(event), wearing => wear.push(wearing), undefined, textures);
t.after(async () => { transport.hold = false; await session.stop(); });
return { transport, session, states, input, compass, wear };
}
test('session authenticates both arms, checks firmware and sends acknowledged display data to L', async t => {
const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
assert.equal(h.session.state.phase, 'connected'); assert.equal(h.session.state.ring, true);
assert.equal(h.session.state.battery, 90);
assert.equal(h.session.state.charging, false);
assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']);
h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1);
const image = h.transport.sent.find(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 26);
assert.equal(image.id, 'L');
assert.equal(image.message.payload[0], 26);
const layout = h.transport.sent.find(s => s.message.sid === p.SID.hub && s.message.command === 0);
assert.equal(p.readBytes(p.readBytes(layout.message.payload, 3), 4), undefined);
await h.session.stop();
assert.equal(h.session.state.phase, 'disconnected');
assert.ok(h.transport.sent.some(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 11));
});
test('stock firmware is rejected before framebuffer/layout/image commands', async t => {
const transport = new FakeTransport(); transport.stock = true;
const h = harness(t, transport); await h.session.start(addresses);
assert.equal(h.session.state.phase, 'error'); assert.match(h.session.state.status, /firmware/);
assert.ok(!transport.sent.some(s => s.message.sid === p.SID.hub));
});
test('one unavailable R1 notification channel does not discard the usable channel', async t => {
const transport = new FakeTransport();
const subscribe = transport.subscribe.bind(transport);
transport.subscribe = async (id, uuid) => {
if (uuid === p.RING_NOTIFY[0]) throw new Error('CCCD timed out');
return subscribe(id, uuid);
};
const h = harness(t, transport); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
assert.equal(h.session.state.ring, true);
assert.ok(transport.subscriptions.some(([id, uuid]) => id === 'ring' && uuid === p.RING_NOTIFY[1]));
assert.ok(!transport.closed.includes('ring'));
});
test('disconnect cancels in-flight authentication and ignores late acknowledgements', async t => {
const h = harness(t); h.transport.hold = true;
const start = h.session.start(addresses); await until(() => h.transport.held.length === 2);
await h.session.stop();
for (const held of h.transport.held) h.transport.ack(held.id, held.message);
await start;
assert.equal(h.session.state.phase, 'disconnected');
assert.ok(!h.states.some(s => s.phase === 'connected'));
assert.equal(h.transport.sent.length, 2);
});
test('wrong peer, service or magic cannot satisfy a pending request; timeout rejects it', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const request = h.session.request('right', p.SID.hub, p.heartbeat, 'test heartbeat', 30);
const rejected = assert.rejects(request, /not acknowledged/);
await until(() => h.transport.held.length > 0);
const held = h.transport.held[0];
h.transport.ack(held.id, held.message, { identifier: 'stranger' });
h.transport.ack(held.id, held.message, { sid: 9 });
h.transport.ack(held.id, held.message, { magic: held.message.magic + 1 });
await rejected;
});
test('ring disconnect retries independently and stop cancels further retries', async t => {
const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
const armMessages = h.transport.sent.length;
h.transport.emit({ kind: 'disconnected', identifier: 'ring', message: 'Link lost' });
assert.equal(h.session.state.phase, 'connected'); assert.equal(h.session.state.ring, false);
assert.notEqual(h.session.ringRetryTimer, null);
await new Promise(resolve => setTimeout(resolve, 2100));
assert.equal(h.session.state.ring, true);
assert.equal(h.transport.sent.length, armMessages);
assert.equal(h.transport.subscriptions.filter(([id]) => id === 'ring').length, 4);
h.transport.emit({ kind: 'disconnected', identifier: 'ring' });
await h.session.stop();
assert.equal(h.session.ringRetryTimer, null);
});
test('a BLE wake services overdue heartbeat and lease without waiting for a suspended poll timer', async t => {
const h = harness(t); await h.session.start(addresses);
clearTimeout(h.session.timer); h.session.timer = null;
h.session.lastHeartbeat = Date.now() - 12_000;
h.session.lastLease = Date.now() - 60_000;
const before = h.transport.sent.length;
const data = hex(p.concat(...p.frameMessage(p.integer(1, 6), p.SID.auth, 1, 22)));
h.transport.emit({ kind: 'notification', identifier: 'L', characteristic: p.G2_NOTIFY, data });
// pump starts in the notification callback, before any timers can fire.
assert.equal(h.session.pumping, true);
await until(() => !h.session.pumping);
const sent = h.transport.sent.slice(before);
assert.equal(sent.filter(s => s.message.sid === p.SID.hub && s.message.command === 12).length, 1);
assert.deepEqual(sent.filter(s => s.message.sid === p.SID.settings).map(s => s.id).sort(), ['L', 'R']);
assert.equal(h.session.state.phase, 'connected');
assert.deepEqual(h.transport.closed, []);
await h.session.stop();
const count = h.transport.sent.length;
h.session.wake();
h.transport.emit({ kind: 'notification', identifier: 'L', characteristic: p.G2_NOTIFY, data });
assert.equal(h.transport.sent.length, count);
assert.equal(h.session.state.phase, 'disconnected');
});
test('glasses mic uses Android wire control and only routes bound-arm audio during capture', async t => {
assert.equal(hex(p.audioControl(101, true)), '080f10659201020801');
assert.equal(hex(p.audioControl(101, false)), '080f10659201020800');
const h = harness(t), audio = [];
await h.session.start(addresses);
const send = id => h.transport.emit({ kind: 'notification', identifier: id, characteristic: p.G2_RENDER_NOTIFY, data: '010203' });
send('R'); assert.equal(audio.length, 0);
await h.session.setMicrophone(true, bytes => audio.push(hex(bytes)));
assert.ok(h.transport.subscriptions.some(([id, c]) => id === 'R' && c === p.G2_RENDER_NOTIFY));
assert.ok(h.transport.subscriptions.some(([id, c]) => id === 'L' && c === p.G2_RENDER_NOTIFY));
send('stranger'); send('ring'); send('L'); send('R');
assert.deepEqual(audio, ['010203', '010203']);
const disabling = h.session.setMicrophone(false);
send('R'); assert.equal(audio.length, 2); // Immediate, before the disable ACK.
await disabling;
assert.deepEqual(h.transport.sent.filter(s => s.message.command === 15).map(s =>
[s.id, p.readInteger(p.readBytes(s.message.payload, 18), 1)]), [['R', 1], ['R', 0]]);
});
test('release while microphone enable awaits ACK sends disable last and rejects further audio', async t => {
const h = harness(t); await h.session.start(addresses);
h.transport.hold = true;
let packets = 0;
const enabling = h.session.setMicrophone(true, () => packets++);
await until(() => h.transport.held.some(s => s.message.command === 15));
const disabling = h.session.setMicrophone(false);
h.transport.emit({ kind: 'notification', identifier: 'R', characteristic: p.G2_RENDER_NOTIFY, data: '01' });
assert.equal(packets, 0);
h.transport.hold = false;
for (const s of h.transport.held) h.transport.ack(s.id, s.message);
await Promise.all([enabling, disabling]);
const mic = h.transport.sent.filter(s => s.message.command === 15);
assert.equal(p.readInteger(p.readBytes(mic.at(-1).message.payload, 18), 1), 0);
});
const imageMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && ![10, 11].includes(s.message.payload[0]));
const imageBody = sent => sent.message.payload;
const ack = (transport, sent, overrides) => transport.ack(sent.id, sent.message, overrides);
const tick = () => new Promise(resolve => setImmediate(resolve));
const noisy = () => Uint8Array.from({ length: 640 * 480 }, (_, i) => (i % 2) * 255);
test('resource evictions and uploads precede their frame, share ACK accounting, and coalesce pixels with draw identities', async t => {
const plans = [];
let resets = 0;
const textures = {
reset() { resets++; },
plan(previous, next, frame, firstId) {
plans.push({ previous, next, frame, firstId });
return { resourceCommands: plans.length === 1 ? [new Uint8Array([22, 1]), new Uint8Array([21, 2])] : [],
payload: new Uint8Array([8, plans.length]), nextFid: firstId + 2, usedBytes: 100 };
},
};
const h = harness(t, new FakeTransport(), textures);
await h.session.start(addresses); h.transport.hold = true;
const a = { native: { id: 'a' } }, b = { native: { id: 'b' } }, c = { native: { id: 'c' } };
h.session.setFrame(new Uint8Array(640 * 480), a);
await until(() => imageMessages(h.transport).length === 3);
const sent = imageMessages(h.transport);
assert.deepEqual(sent.map(s => s.message.payload[0]), [22, 21, 8]);
h.session.setFrame(new Uint8Array(640 * 480).fill(80), b);
h.session.setFrame(new Uint8Array(640 * 480).fill(160), c);
assert.equal(plans.length, 1, 'coalesced frames must not allocate textures');
ack(h.transport, sent[2]); ack(h.transport, sent[1]); await tick();
assert.equal(h.session.state.frames, 0, 'a draw ACK cannot retire an unacknowledged upload');
ack(h.transport, sent[0]); await until(() => imageMessages(h.transport).length === 4);
assert.equal(h.session.state.frames, 1);
assert.equal(plans.length, 2); assert.equal(plans[1].frame, c);
assert.deepEqual(plans[1].previous, plans[0].next);
assert.equal(plans[1].firstId, plans[0].firstId + 2);
assert.ok(plans[1].next.every(value => value === 0xaa));
ack(h.transport, imageMessages(h.transport)[3]); await until(() => h.session.state.frames === 2);
h.transport.hold = false; await h.session.stop();
assert.ok(resets > 0);
});
test('evictions drain earlier replayable draws before resource IDs can be reused', async t => {
let plans = 0;
const textures = {
reset() {},
plan() { return { resourceCommands: ++plans === 1 ? [] : [new Uint8Array([22]), new Uint8Array([21])],
payload: new Uint8Array([8]), nextFid: 1, usedBytes: 100 }; },
};
const h = harness(t, new FakeTransport(), textures); await h.session.start(addresses);
h.transport.hold = true;
h.session.setFrame(new Uint8Array(640 * 480), { native: {} });
await until(() => imageMessages(h.transport).length === 1);
h.session.setFrame(new Uint8Array(640 * 480).fill(80), { native: {} });
await until(() => plans === 2); await tick();
assert.equal(imageMessages(h.transport).length, 1, 'eviction cannot enter an unresolved window');
h.session.recoverCfw(); await tick();
assert.equal(imageMessages(h.transport).length, 1);
ack(h.transport, imageMessages(h.transport)[0]);
await until(() => imageMessages(h.transport).length === 4);
assert.deepEqual(imageMessages(h.transport).map(s => s.message.payload[0]), [8, 22, 21, 8]);
h.transport.hold = false;
for (const sent of imageMessages(h.transport).slice(1)) ack(h.transport, sent);
await until(() => h.session.state.frames === 2);
});
test('a failed resource upload invalidates residency and the pipelined frame base', async t => {
let resets = 0;
const textures = {
reset() { resets++; },
plan() { return { resourceCommands: [new Uint8Array([21])], payload: new Uint8Array([8]), nextFid: 1, usedBytes: 4 }; },
};
const h = harness(t, new FakeTransport(), textures); await h.session.start(addresses);
const original = h.session.requestCfw.bind(h.session);
h.session.requestCfw = (role, payload) => payload[0] === 21 ? Promise.reject(new Error('texture upload failed')) : original(role, payload);
h.session.setFrame(new Uint8Array(640 * 480), { native: {} });
await until(() => h.session.state.phase === 'retrying');
assert.equal(resets, 1); assert.equal(h.session.lastEnqueued, null);
assert.equal(h.session.latestTextures, null); assert.equal(h.session.state.frames, 0);
});
test('resuming near framebuffer lease expiry discards cached draws before transmission', async t => {
let resets = 0, plans = 0;
const textures = { reset() { resets++; }, plan() { plans++; return null; } };
const h = harness(t, new FakeTransport(), textures); await h.session.start(addresses);
h.session.lastLease = Date.now() - 85_000;
h.session.setFrame(new Uint8Array(640 * 480), { native: {} });
await h.session.pump();
assert.equal(h.session.state.phase, 'retrying');
assert.equal(resets, 1); assert.equal(plans, 0);
assert.equal(imageMessages(h.transport).length, 0);
});
// Both eyes must finish; wrong ingress, size, CRC, ordinal and legacy replies do not count.
test('CFW ACK identity and both-lens completion gate display metrics', async t => {
const { iosBleTraffic } = require('../.test-build/app/g2/ble-traffic-counters.js');
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const before = iosBleTraffic.sample(); let written = 0;
const write = h.transport.write.bind(h.transport);
h.transport.write = async (...args) => { written += args[2].length; await write(...args); };
h.session.setFrame(new Uint8Array(640 * 480));
await until(() => imageMessages(h.transport).length === 2);
const [draw, sent] = imageMessages(h.transport);
ack(h.transport,draw);
for (const wrong of [{ identifier: 'R' }, { size: 99 }, { checksum: 0 }, { ordinal: 1 }, { magic: sent.message.magic + 1 }]) ack(h.transport, sent, wrong);
ack(h.transport, sent, { lenses: [1] }); await tick();
assert.equal(h.session.state.frames, 0);
ack(h.transport, sent, { lenses: [2] }); await until(() => h.session.state.frames === 1);
assert.equal(iosBleTraffic.sample().bytes - before.bytes, written);
assert.equal(iosBleTraffic.sample().messages - before.messages, 2);
assert.equal(iosBleTraffic.sample().frames - before.frames, 1);
ack(h.transport, sent); await tick(); assert.equal(h.session.state.frames, 1);
});
test('large frames use bounded RLE bands and ordered three-message pipelining', async t => {
const { applyDisplayPayload } = require('./helpers/display-payload.cjs');
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const gray = noisy(); h.session.setFrame(gray);
await until(() => imageMessages(h.transport).length === 3);
const first = imageMessages(h.transport);
ack(h.transport, first[2]); ack(h.transport, first[1]); await tick();
assert.equal(imageMessages(h.transport).length, 3); assert.equal(h.session.state.frames, 0);
ack(h.transport, first[0]); await until(() => imageMessages(h.transport).length === 6);
for (const sent of imageMessages(h.transport).slice(3)) ack(h.transport, sent);
await until(() => imageMessages(h.transport).length === 9);
assert.equal(h.session.state.frames, 0);
for (const sent of imageMessages(h.transport).slice(6)) ack(h.transport, sent);
await until(() => h.session.state.frames === 1);
let shadow = new Uint8Array(320 * 480);
for (const sent of imageMessages(h.transport)) {
assert.ok(sent.message.payload.length <= 65535);
shadow = applyDisplayPayload(shadow, sent.message.payload);
}
assert.deepEqual(shadow, p.packGray4(gray, 640, 480));
});
test('draw/present pairs pipeline and coalesce waiting frames', async t => {
const h=harness(t);await h.session.start(addresses);h.transport.hold=true;
const submit=async v=>{h.session.setFrame(new Uint8Array(640*480).fill(v));await h.session.pump();await tick();};
await submit(0);await submit(0);assert.equal(imageMessages(h.transport).length,2);
await submit(255);await until(()=>imageMessages(h.transport).length===3);
await submit(80);await submit(160);
const first=imageMessages(h.transport);ack(h.transport,first[2]);ack(h.transport,first[1]);await tick();
assert.equal(h.session.state.frames,0);assert.equal(imageMessages(h.transport).length,3);
ack(h.transport,first[0]);await until(()=>imageMessages(h.transport).length===6);
for(const message of imageMessages(h.transport).slice(3)) ack(h.transport,message);
await until(()=>h.session.state.frames===3);
const {applyDisplayPayload}=require('./helpers/display-payload.cjs');let screen=new Uint8Array(320*480);
for(const {message} of imageMessages(h.transport)) screen=applyDisplayPayload(screen,message.payload);
assert.deepEqual(screen,p.packGray4(new Uint8Array(640*480).fill(160),640,480));
assert.deepEqual(imageMessages(h.transport).map(s=>s.message.payload[0]),[26,28,26,28,26,28]);
});
test('NACK replays the unresolved window with fresh IDs and reset compression; stale ACKs are ignored', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
for (const value of [0, 255, 0]) { h.session.setFrame(new Uint8Array(640 * 480).fill(value)); await h.session.pump(); await tick(); }
await until(() => imageMessages(h.transport).length === 3);
const old = imageMessages(h.transport);
ack(h.transport, old[2]); ack(h.transport, old[1], { nack: true, lenses: [2] });
await until(() => imageMessages(h.transport).length === 6);
const replay = imageMessages(h.transport).slice(3);
for (const sent of old) ack(h.transport, sent);
await tick(); assert.equal(h.session.state.frames, 0);
replay.forEach((sent, i) => { assert.notEqual(sent.message.magic, old[i].message.magic); assert.ok(sent.message.flags & 8); assert.deepEqual(sent.message.payload, old[i].message.payload); ack(h.transport, sent); });
await until(() => imageMessages(h.transport).length === 9);
imageMessages(h.transport).slice(6).forEach(sent=>ack(h.transport,sent));
await until(() => h.session.state.frames === 3);
});
test('timeout starts after writes; missing ACK retries are bounded and invalidate the delta base', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const write = h.transport.write.bind(h.transport); let release;
const blocked = new Promise(resolve => { release = resolve; });
h.transport.write = async (...args) => { if (args[0] === 'L') await blocked; await write(...args); };
h.session.setFrame(new Uint8Array(640 * 480)); await h.session.pump();
await new Promise(resolve => setTimeout(resolve, 550));
assert.equal(h.session.state.phase, 'connected'); assert.equal(imageMessages(h.transport).length, 0);
release(); await until(() => imageMessages(h.transport).length === 2); await tick();
for (let i = 1; i <= 4; i++) {
h.session.cfwPending.forEach(p => p.deadline = 1); h.session.recoverCfw();
if (i < 4) await until(() => imageMessages(h.transport).length === 2 * (i + 1));
}
assert.equal(h.session.state.phase, 'retrying'); assert.match(h.session.state.status, /retry limit/);
assert.equal(h.session.lastEnqueued, null); assert.equal(h.session.cfwPending.length, 0);
for (const sent of imageMessages(h.transport)) ack(h.transport, sent);
await tick(); assert.equal(h.session.state.frames, 0);
});
test('failed write poisons queued display work; disconnect cleanup uses the new SID', async t => {
const h = harness(t); await h.session.start(addresses);
const write = h.transport.write.bind(h.transport); let attempts = 0;
h.transport.write = async (...args) => { if (args[0] === 'L') { attempts++; throw new Error('test write failed'); } await write(...args); };
h.session.setFrame(noisy()); await until(() => h.session.state.phase === 'retrying');
assert.equal(attempts, 1); assert.equal(h.session.displayInFlight, 0);
await h.session.stop(); h.transport.write = write; await h.session.start(addresses);
h.transport.hold = true; h.session.setFrame(noisy()); await until(() => imageMessages(h.transport).length === 3);
const stale = imageMessages(h.transport); h.transport.hold = false; await h.session.stop();
stale.forEach(s => ack(h.transport, s)); await tick();
assert.equal(h.session.state.phase, 'disconnected'); assert.equal(h.session.state.frames, 0);
assert.equal(h.transport.sent.at(-1).message.sid, p.SID.cfw);
assert.deepEqual(h.transport.sent.at(-1).message.payload, new Uint8Array([11]));
});
test('full display window still services input and microphone control', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
h.session.setFrame(noisy()); await until(() => imageMessages(h.transport).length === 3);
const input = p.bytes(13, p.bytes(3, p.concat(p.integer(1, 9), p.integer(2, 3))));
h.transport.emit({ kind: 'notification', identifier: 'R', characteristic: p.G2_NOTIFY, data: hex(p.concat(...p.frameMessage(input, p.SID.hub, 1, 22))) });
assert.equal(h.input.length, 1);
const audio = [], enabling = h.session.setMicrophone(true, packet => audio.push(hex(packet)));
await until(() => h.transport.sent.some(s => s.message.command === 15));
ack(h.transport, h.transport.sent.find(s => s.message.command === 15)); await enabling;
h.transport.emit({ kind: 'notification', identifier: 'L', characteristic: p.G2_RENDER_NOTIFY, data: '010203' });
assert.deepEqual(audio, ['010203']); assert.equal(h.session.displayInFlight, 3);
});
test('draw updates reconstruct moving and erased pixels while present gates frame completion', async t => {
const {applyDisplayPayload}=require('./helpers/display-payload.cjs');
const h=harness(t);await h.session.start(addresses);h.transport.hold=true;
const base=new Uint8Array(640*480),a=base.slice(),b=base.slice();a[11*640+13]=255;b[11*640+101]=160;
let screen=new Uint8Array(320*480);
for(const [i,frame] of [base,a,b,base].entries()) {
h.session.setFrame(frame);await h.session.pump();await until(()=>imageMessages(h.transport).length===(i+1)*2);
const [draw,present]=imageMessages(h.transport).slice(i*2);
assert.equal(draw.message.payload[0],26);assert.equal(present.message.payload[0],28);
screen=applyDisplayPayload(screen,draw.message.payload);assert.deepEqual(screen,p.packGray4(frame,640,480));
ack(h.transport,draw);await tick();assert.equal(h.session.state.frames,i);
ack(h.transport,present);await until(()=>h.session.state.frames===i+1);
}
assert.equal(imageMessages(h.transport)[2].message.payload[7],0); // compact bbox
});
test('reconnect reseeds the screen and unified draw calls carry no legacy frame id', async t => {
const h=harness(t);await h.session.start(addresses);
const submit=async gray=>{const before=h.session.state.frames;h.session.setFrame(gray);await until(()=>h.session.state.frames===before+1);return imageBody(imageMessages(h.transport).at(-2));};
const base=new Uint8Array(640*480),next=base.slice();next[1234]=255;
assert.equal((await submit(base))[7],1); // wide full-screen bounds
const delta=await submit(next);assert.equal(delta[7],0); // compact bbox, immediately followed by RLE
const count=imageMessages(h.transport).length;h.session.setFrame(next);await h.session.pump();await tick();assert.equal(imageMessages(h.transport).length,count);
await h.session.stop();await h.session.start(addresses);assert.equal((await submit(next))[7],1);
});
const compassMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 10);
const buzzerMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 5);
const buzzerPayload = () => new Uint8Array([5, 4, 2, 0xb8, 0x0b, 25, 80, 0, 1, 0, 0, 20, 0]);
test('buzzer snapshots the complete Android payload and waits for both lens ACKs without counting a frame', async t => {
const h = harness(t);
await h.session.playBuzzerSequence(buzzerPayload());
assert.equal(h.transport.sent.length, 0);
await h.session.start(addresses);
await h.session.playBuzzerSequence(new Uint8Array());
assert.equal(buzzerMessages(h.transport).length, 0);
h.transport.hold = true;
const payload = buzzerPayload(), expected = payload.slice();
let complete = false;
const playing = h.session.playBuzzerSequence(payload).then(() => { complete = true; });
payload.fill(0);
await until(() => buzzerMessages(h.transport).length === 1);
const sent = buzzerMessages(h.transport)[0];
assert.equal(sent.id, 'L'); assert.deepEqual(sent.message.payload, expected);
h.transport.ack(sent.id, sent.message, { lenses: [1] }); await tick();
assert.equal(complete, false);
h.transport.ack(sent.id, sent.message, { lenses: [2] }); await playing;
assert.equal(h.session.state.frames, 0);
});
test('buzzer shares display compression and ACK recovery while the frame window is full', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
h.session.setFrame(noisy()); await until(() => imageMessages(h.transport).length === 3);
const playing = h.session.playBuzzerSequence(buzzerPayload());
await until(() => buzzerMessages(h.transport).length === 1);
const sent = buzzerMessages(h.transport)[0];
h.transport.ack(sent.id, sent.message, { nack: true });
await until(() => buzzerMessages(h.transport).length === 2);
const replay = buzzerMessages(h.transport)[1];
assert.notEqual(replay.message.magic, sent.message.magic);
assert.deepEqual(replay.message.payload, buzzerPayload());
h.transport.hold = false; // Subsequent bands of the large frame can finish too.
for (const item of h.transport.sent.filter(s => s.message.sid === p.SID.cfw).slice(-4)) ack(h.transport, item);
await playing;
assert.equal(h.session.state.phase, 'connected');
await until(() => h.session.state.frames === 1);
});
test('disconnect cancels queued buzzer work and does not play it on reconnect', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const playing = h.session.playBuzzerSequence(buzzerPayload());
await until(() => buzzerMessages(h.transport).length === 1);
h.transport.emit({ kind: 'disconnected', identifier: 'R' }); await playing;
await h.session.playBuzzerSequence(buzzerPayload());
h.transport.hold = false; await h.session.start(addresses, true);
assert.equal(buzzerMessages(h.transport).length, 1);
});
test('compass desired state survives connecting and reconnecting; explicit stop disables it first', async t => {
const h = harness(t);
h.session.setCompassEnabled(true);
await h.session.start(addresses);
assert.equal(compassMessages(h.transport).length, 1);
assert.equal(compassMessages(h.transport)[0].id, 'L');
assert.deepEqual([...compassMessages(h.transport)[0].message.payload], [10, 2, 100, 0, 0, 0]);
await h.session.stop();
assert.deepEqual([...compassMessages(h.transport).at(-1).message.payload], [10, 0]);
assert.equal(h.transport.sent.at(-1).message.payload[0], 11);
await h.session.start(addresses);
assert.equal(compassMessages(h.transport).at(-1).message.payload[1], 2);
h.transport.emit({ kind: 'disconnected', identifier: 'R' });
assert.equal(h.session.state.phase, 'retrying');
await h.session.start(addresses, true);
assert.equal(compassMessages(h.transport).at(-1).message.payload[1], 2);
});
test('release during pending compass enable sends disable after the enable ACK', async t => {
const h = harness(t); await h.session.start(addresses);
assert.deepEqual([...compassMessages(h.transport)[0].message.payload], [10, 0]);
h.transport.hold = true;
h.session.setCompassEnabled(true);
await until(() => h.transport.held.length === 1);
h.session.setCompassEnabled(false);
const enabling = h.transport.held.shift();
h.transport.hold = false; h.transport.ack(enabling.id, enabling.message);
await until(() => compassMessages(h.transport).length === 3);
assert.deepEqual(compassMessages(h.transport).map(s => s.message.payload[1]), [0, 2, 0]);
});
test('compass notifications only come from the right arm and preserve diagnostics and calibration', async t => {
const h = harness(t); await h.session.start(addresses);
const payload = new Uint8Array(Buffer.from('080f1000520308e702a2060c434d010302038c0098badcfe', 'hex'));
const emit = (id, sid = 8, flag = 1, body = payload) => h.transport.emit({ kind: 'notification', identifier: id,
characteristic: p.G2_NOTIFY, data: hex(p.concat(...p.frameMessage(body, sid, flag, 20))) });
emit('L'); emit('other'); emit('R', 9); emit('R', 8, 0x20);
assert.equal(h.compass.length, 0);
emit('R'); emit('R', 8, 6, new Uint8Array([8, 16, 16, 0]));
emit('R', 8, 1, new Uint8Array([8, 17, 16, 0]));
assert.deepEqual(h.compass, [{ command: 15, headingDegrees: 359, diagnostics: {
magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 140, sampleTimeMs: 0xfedcba98,
} }, { command: 16, headingDegrees: -1 }, { command: 17, headingDegrees: -1 }]);
});
test('wear detection matches Android wire format and queries both arms on every connection', async t => {
const wear = require('../.test-build/app/g2/wear-protocol.js');
assert.equal(hex(wear.enableWearDetection(42)), '0801102a1a042a020801');
assert.equal(hex(wear.queryWearState()), '08011000aa0606464301070000');
const h = harness(t); await h.session.start(addresses);
const queries = () => h.transport.sent.filter(({ message }) => hex(message.payload) === hex(wear.queryWearState()));
assert.deepEqual(queries().map(q => q.id).sort(), ['L', 'R']);
const enable = h.transport.sent.find(({ message }) => {
const body = p.readBytes(message.payload, 3);
return message.sid === p.SID.settings && message.command === 1 && body && p.readBytes(body, 5);
});
assert.equal(enable.id, 'R'); assert.equal(enable.message.flag, 0x20);
await h.session.stop(); await h.session.start(addresses);
assert.equal(queries().length, 4);
});
test('wear notifications validate the event and peer and work during initial connection', async t => {
const h = harness(t);
const payload = state => p.concat(p.integer(1, 3), p.bytes(5, p.concat(p.integer(1, 1), p.integer(2, state))));
const emit = (id, data, sid = 0x10) => h.transport.emit({ kind: 'notification', identifier: id,
characteristic: p.G2_NOTIFY, data: hex(p.concat(...p.frameMessage(data, sid, 1, 12))) });
const originalWrite = h.transport.write.bind(h.transport);
h.transport.write = async (...args) => {
await originalWrite(...args);
if (h.transport.sent.at(-1)?.message.magic === 0) emit(args[0], payload(0));
};
await h.session.start(addresses);
assert.ok(h.wear.length >= 2); assert.ok(h.wear.every(value => value === false));
h.wear.length = 0;
emit('stranger', payload(0)); emit('ring', payload(0)); emit('R', payload(2));
emit('R', payload(0), p.SID.hub); emit('R', p.integer(1, 3));
assert.deepEqual(h.wear, []);
emit('L', payload(0)); emit('R', payload(1));
assert.deepEqual(h.wear, [false, true]);
await h.session.stop(); emit('R', payload(0));
assert.deepEqual(h.wear, [false, true]);
});
test('ANCS packets are scoped to the right link and cleared on disconnect and authorization revocation', async t => {
const transport = new FakeTransport(); transport.firmware = REQUIRED_FACECLAW_FIRMWARE_VERSION;
const h = harness(t, transport); await h.session.start(addresses);
await until(() => transport.ancs?.length);
const token = Array.from(transport.ancs[0].frame.subarray(4,8));
assert.equal(transport.ancs[0].id, 'R');
const frame = new Uint8Array([65,78,1,0,...token,0,0,3,1]);
const emit = id => transport.emit({kind:'notification',identifier:id,characteristic:p.G2_NOTIFY,data:hex(frame)});
emit('L'); assert.equal(h.session.notifications.state, 'starting');
emit('R'); assert.equal(h.session.notifications.state, 'ready');
transport.emit({kind:'ancs-authorization',identifier:'R',authorized:false});
assert.equal(h.session.notifications.state,'disconnected');
emit('R'); assert.equal(h.session.notifications.state,'disconnected');
await h.session.stop(); assert.deepEqual(h.session.notifications.read(),[]);
});
test('firmware 15 requires an upgrade before starting notification relay', async t => {
const transport = new FakeTransport(); transport.firmware = 15;
const h = harness(t, transport); await h.session.start(addresses);
assert.equal(h.session.notifications.state, 'disconnected');
assert.equal(h.session.state.phase, 'error');
assert.match(h.session.state.status, new RegExp(`requires revision ${REQUIRED_FACECLAW_FIRMWARE_VERSION}`));
assert.equal(transport.ancs?.length ?? 0, 0);
});
test('ANCS app-name lookup uses the right-link write limit, including after reauthorization', async t => {
const transport = new FakeTransport(); transport.firmware = REQUIRED_FACECLAW_FIRMWARE_VERSION;
const h = harness(t, transport); await h.session.start(addresses);
await until(() => transport.ancs?.length);
const id = 'net.superblock.Pushover';
const attr = (key, value) => { const bytes = [...Buffer.from(value)]; return [key, bytes.length & 255, bytes.length >> 8, ...bytes]; };
for (let attempt = 0; attempt < 2; attempt++) {
if (attempt) {
transport.emit({kind:'ancs-authorization',identifier:'R',authorized:false});
const count = transport.ancs.length;
transport.emit({kind:'ancs-authorization',identifier:'R',authorized:true});
await until(() => transport.ancs.slice(count).some(p => p.frame[3] === 0));
}
const start = transport.ancs.filter(p => p.frame[3] === 0).at(-1);
const token = [...start.frame.subarray(4,8)];
let sequence = 0;
const feed = (kind, bytes) => transport.emit({kind:'notification',identifier:'R',characteristic:p.G2_NOTIFY,
data:hex(new Uint8Array([65,78,1,kind,...token,sequence++,0,3,...bytes]))});
const count = transport.ancs.length;
feed(0,[1]);feed(1,[0,0,1,1,7,0,0,0]);
await until(() => transport.ancs.length === count+1);
feed(3,[]);feed(2,[0,7,0,0,0,...attr(0,id),...attr(1,'Title'),...attr(3,'Message')]);
await until(() => transport.ancs.length === count+2);
feed(3,[]);feed(2,[0,7,0,0,0,...attr(2,''),...attr(4,'7'),...attr(5,''),...attr(6,''),...attr(7,'')]);
await until(() => transport.ancs.length === count+3);
const request = transport.ancs.at(-1);
assert.equal(request.id,'R');
assert.deepEqual([...request.frame.subarray(8)],[1,...Buffer.from(id),0,0]);
feed(3,[]);feed(2,[1,...Buffer.from(id),0,...attr(0,'Pushover Notifications')]);
assert.equal(h.session.notifications.read()[0].appName,'Pushover Notifications');
}
});
test('brightness sends the Android settings payload to the right temple, including explicit zero and Auto', async () => {
const transport = new FakeTransport(), session = new GlassesSession(transport, () => {}, () => {});
await session.setBrightness(50);
assert.equal(transport.sent.length, 0, 'preview mode must not send Bluetooth commands');
const requests = [];
session.state.phase = 'connected';
session.request = async (role, sid, build) => { requests.push({ role, sid, payload: hex(build(105)) }); };
await session.setBrightness(0);
await session.setBrightness(80);
await session.setBrightness(null);
assert.deepEqual(requests, [
{ role: 'right', sid: 9, payload: '080110691a060a0408001000' },
{ role: 'right', sid: 9, payload: '080110691a060a0408001050' },
{ role: 'right', sid: 9, payload: '080110691a040a020801' },
]);
});
+73 -43
View File
@@ -6,7 +6,6 @@ const vm = require('node:vm');
const ts = require('typescript'); const ts = require('typescript');
const images = require('../.test-build/app/graphics/image.js'); const images = require('../.test-build/app/graphics/image.js');
const { SurfaceCompositor } = require('../.test-build/app/graphics/surface-compositor.js'); const { SurfaceCompositor } = require('../.test-build/app/graphics/surface-compositor.js');
const protocol = require('../.test-build/app/g2/ble-protocol.js');
const events = require('../.test-build/app/g2/events.js'); const events = require('../.test-build/app/g2/events.js');
function load(file, modules, globals = {}) { function load(file, modules, globals = {}) {
@@ -24,8 +23,8 @@ const timings = load('app/native/frame-timings.ts', {});
function fixture() { function fixture() {
const observers = new Map(), phoneState = { protectedDataAvailable: true }; const observers = new Map(), phoneState = { protectedDataAvailable: true };
let settingsChanged, remoteHost; let settingsChanged, remoteHost, controller, bridge = null;
let now = 1000, nextTask = 0, screenOn = true, shellOptions, session; let now = 1000, nextTask = 0, screenOn = true, shellOptions;
const tasks = new Map(), sent = [], previews = [], received = [], errors = []; const tasks = new Map(), sent = [], previews = [], received = [], errors = [];
const boardStats = { starts: 0, stops: 0, paints: 0 }; const boardStats = { starts: 0, stops: 0, paints: 0 };
const settings = { lock: true, enabled: true, tap: true, hold: true, tilt: true, duration: 3000 }; const settings = { lock: true, enabled: true, tap: true, hold: true, tilt: true, duration: 3000 };
@@ -39,8 +38,10 @@ function fixture() {
'../util/render-freshness': { beginRenderPass() {}, endRenderPass: () => false }, '../util/render-freshness': { beginRenderPass() {}, endRenderPass: () => false },
'./glance-state': require('../.test-build/app/g2/glance-state.js'), './glance-state': require('../.test-build/app/g2/glance-state.js'),
}, clock); }, clock);
const launcherPixels = new Uint8Array(640 * 480).fill(75), fullRect = { x: 0, y: 0, width: 640, height: 480 };
const window = { windowId: 'launcher', surfaceId: 'launcher', appId: 'launcher', title: 'Apps', const window = { windowId: 'launcher', surfaceId: 'launcher', appId: 'launcher', title: 'Apps',
requestRender() {}, setScreenOn() {} }; // The launcher repaints into whichever display target the controller has after a switch.
requestRender() { void controller?.displayReady.then(() => controller.display?.submitSurfaceFrame('launcher', launcherPixels, fullRect, 'launcher')); }, setScreenOn() {} };
const shell = { const shell = {
configure: options => { shellOptions = options; }, registerWindow() {}, focusWindow() {}, configure: options => { shellOptions = options; }, registerWindow() {}, focusWindow() {},
wake: () => { screenOn = true; shellOptions.onScreenStateChanged(true); }, wake: () => { screenOn = true; shellOptions.onScreenStateChanged(true); },
@@ -54,15 +55,43 @@ function fixture() {
if (input.type === 'display-wake' || !screenOn && input.type === 'double-click') shell.wake(); if (input.type === 'display-wake' || !screenOn && input.type === 'double-click') shell.wake();
}, },
}; };
class Session { // Both display targets compose with the real (TypeScript reference) compositor so
state = { phase: 'disconnected' }; // the pixel assertions below exercise real layering, blanking and lock surfaces.
constructor(_transport, onState, onInput, _log, _activity, _compass, onWear) { session = this; this.onState = onState; this.onInput = onInput; this.onWear = onWear; } class Display {
async enableWearDetectionAndRequestState() {} compositor = new SurfaceCompositor(640, 480);
async start() { this.state = { phase: 'connected' }; this.onState(this.state); } async configureCompositorScreen() {}
async stop() { this.state = { phase: 'disconnected' }; this.onState(this.state); } async configureSurface(id, options) { this.compositor.configureSurface(id, options); }
async setBrightness() {} async removeSurface(id) { this.compositor.removeSurface(id); this.changed(); }
setFrame(pixels) { if (this.state.phase === 'connected') sent.push(pixels); } async setSurfaceVisible(id, visible) { this.compositor.setSurfaceVisible(id, visible); this.changed(); }
wake() {} async setUnderlayDim(below, factor) { this.compositor.setUnderlayDim(below, factor); this.changed(); }
async setScreenBlanked(blanked) { this.compositor.setScreenBlanked(blanked); this.changed(); }
async submitSurfaceFrame(id, pixels, rect, _fingerprint, _paintMs, _frameId, draws = null) { this.compositor.submitSurfaceFrame(id, pixels, rect, draws); this.changed(); }
async submitShellScene(bytes) { this.compositor.setShellScene(bytes); this.changed(); }
getCompositePreview() { return this.compositor.composite(); }
changed() {}
}
class PreviewDisplayTarget extends Display {
activate(fn) { this.onFrame = fn; } release() { this.onFrame = null; }
changed() { this.onFrame?.(); }
waitForFrameFinished() { return Promise.resolve('composited'); }
}
const noop = () => () => {};
class FaceclawCommunicatorBridge extends Display {
phase = 'disconnected'; listeners = {};
constructor() { super(); bridge = this; }
on(kind, fn) { this.listeners[kind] = fn; return () => { if (this.listeners[kind] === fn) delete this.listeners[kind]; }; }
onStateChange(fn) { return this.on('state', fn); } onRingEvent(fn) { return this.on('ring', fn); }
onWearState(fn) { return this.on('wear', fn); } onBatteryState() { return noop(); } onFirmwareInfo() { return noop(); }
onFrameMetrics() { return noop(); } addCompassListener() { return noop(); } onAncsRelayFrame() { return noop(); }
onAncsAuthorization() { return noop(); } setRequiresAncs() {} rightWriteLimit() { return 20; } onPhoneLockSignal() {}
async writeRawToRight() {} async setBrightness() {} async enableWearDetectionAndRequestState() {} async playBuzzerSequence() {} async close() {}
emitState(phase, status = phase) { if (phase === 'connected' || phase === 'disconnected') this.phase = phase; this.listeners.state?.({ phase, status }); }
emitRing(input) { this.listeners.ring?.(input); }
emitWear(wearing) { this.listeners.wear?.(wearing); }
async start() { this.emitState('connected', 'Connected.'); }
async disconnect() { this.emitState('disconnected', 'Disconnected.'); }
waitForFrameFinished() { return Promise.resolve('sent'); }
changed() { if (this.phase === 'connected') sent.push(this.compositor.composite()); }
} }
const provider = { const provider = {
isEnabled: () => settings.enabled, showOnTap: () => settings.tap, isEnabled: () => settings.enabled, showOnTap: () => settings.tap,
@@ -85,13 +114,14 @@ function fixture() {
"../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} }, "../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} },
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, '@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
knownFolders: { documents: () => ({ path: '/tmp' }) }, path }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
'../native/ios-bluetooth': { iosBluetooth: () => ({}) },
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {}, stopPhoneCapture() {} } }, '../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {}, stopPhoneCapture() {} } },
'./glasses-session': { GlassesSession: Session }, '../native/faceclaw-communicator.ios': { FaceclawCommunicatorBridge, resolveIosPeripherals: async addresses => addresses },
'../native/preview-display.ios': { PreviewDisplayTarget },
'./ancs-client': { ANCS_FIRMWARE_VERSION: 16, AncsClient: class { state = 'disconnected'; start() {} stop() {} stopCommand() { return new Uint8Array(); } receive() { return false; } } },
'../native/nightscout-bridge': { nightscoutBridge: { async start() {}, async stop() {} } }, '../native/nightscout-bridge': { nightscoutBridge: { async start() {}, async stop() {} } },
'./glance-host': { GlanceHost }, './events': events, './glance-host': { GlanceHost }, './events': events,
'./lock-screen': { LOCK_SCREEN_SURFACE_ID: 'lock-screen', createLockScreenImage: () => new images.GrayImage(640, 480, 123) }, './lock-screen': { LOCK_SCREEN_SURFACE_ID: 'lock-screen', createLockScreenImage: () => new images.GrayImage(640, 480, 123) },
'./device-addresses': { loadDeviceAddresses: () => ({}) }, './ios-peripheral-identity': { deviceAddressError: () => null }, './device-addresses': { loadDeviceAddresses: () => ({ right: 'AA', left: 'BB', ring: '' }) }, './ios-peripheral-identity': { deviceAddressError: () => null },
'../apps/launcher': { launcherEntries: () => [] }, '../apps/launcher': { launcherEntries: () => [] },
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': {}, '../apps/evenhub/updates': {}, '../apps/evenhub': {}, '../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': {}, '../apps/evenhub/updates': {}, '../apps/evenhub': {},
'../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' }, '../apps/launcher/launcher-app': { createLauncherWindow: () => window, LAUNCHER_SURFACE_ID: 'launcher' },
@@ -103,9 +133,7 @@ function fixture() {
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) }, '../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
'../apps/ios-availability': { iosAppUnavailableReason: () => null }, '../apps/ios-availability': { iosAppUnavailableReason: () => null },
'../graphics/glyph-wire': { prepareFrameDraws: () => null }, '../graphics/glyph-wire': { prepareFrameDraws: () => null },
'../native/texture-planner.ios': { IosTexturePlanner: class {} }, '../graphics/plane': planes, '../graphics/image': images,
'../graphics/surface-compositor': { SurfaceCompositor }, '../graphics/plane': planes, '../graphics/image': images,
'../native/ios-graphics': { previewPixels: pixels => pixels },
'../ui/gestures': { makeInputEvent: event => event }, '../ui/layers': { noopLayerActions: {} }, '../ui/gestures': { makeInputEvent: event => event }, '../ui/layers': { noopLayerActions: {} },
'../apps/files/text-viewer': {}, '../apps/files/text-viewer': {},
'../ui/shell/shell': { shell, rawInputEventToInputEvent: event => ({ '../ui/shell/shell': { shell, rawInputEventToInputEvent: event => ({
@@ -124,8 +152,8 @@ function fixture() {
NSNotificationCenter: { defaultCenter: { addObserverForNameObjectQueueUsingBlock(name, _object, _queue, fn) { observers.set(name, fn); return name; }, removeObserver(name) { observers.delete(name); } } }, NSNotificationCenter: { defaultCenter: { addObserverForNameObjectQueueUsingBlock(name, _object, _queue, fn) { observers.set(name, fn); return name; }, removeObserver(name) { observers.delete(name); } } },
NSOperationQueue: { mainQueue: {} }, NSOperationQueue: { mainQueue: {} },
}); });
const controller = new IosPreviewController((pixels, title) => previews.push({ pixels, title }), message => errors.push(message)); controller = new IosPreviewController((pixels, title) => previews.push({ pixels, title }), message => errors.push(message));
controller.compositor.submitSurfaceFrame('launcher', new Uint8Array(640 * 480).fill(75), { x: 0, y: 0, width: 640, height: 480 }); window.requestRender();
controller.resume(); controller.resume();
async function drain() { await controller.inputQueue; await controller.glance.queue; } async function drain() { await controller.inputQueue; await controller.glance.queue; }
async function advance(ms) { async function advance(ms) {
@@ -133,16 +161,18 @@ function fixture() {
for (const [id, task] of [...tasks]) if (task.at <= now && tasks.delete(id)) task.fn(); for (const [id, task] of [...tasks]) if (task.at <= now && tasks.delete(id)) task.fn();
await drain(); await drain();
} }
async function render() { await drain(); await advance(33); assert.deepEqual(errors, []); } async function render() { await drain(); await advance(33); await drain(); assert.deepEqual(errors, []); }
async function hardware(type, source = 2, ringTick) { async function hardware(type, source = 2, ringTick) {
const payload = protocol.bytes(13, protocol.bytes(3, protocol.concat(protocol.integer(1, type), protocol.integer(2, source)))); const input = { kind: type === 12 ? 'display-wake' : 'sys-event', containerName: '', eventType: type, eventSource: source,
const input = protocol.decodeGlassesInput({ sid: protocol.SID.hub, flag: 1, payload }); systemExitReasonCode: 0, frameId: 0 };
if (ringTick !== undefined) input.ringInput = { type: type === 3 ? 2 : 1, tick: ringTick, aux:0, speed:0 }; if (ringTick !== undefined) input.ringInput = { type: type === 3 ? 2 : 1, tick: ringTick, aux: 0, speed: 0 };
session.onInput(input); bridge.emitRing(input);
await render(); await render();
} }
async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); } async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); }
return { controller, shell, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers, wear: wearing => session.onWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render }; async function connect() { await controller.connect(); await render(); }
return { controller, shell, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
get bridge() { return bridge; }, connect, wear: wearing => bridge.emitWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
} }
const assertBlank = pixels => assert.ok(pixels.every(p => p === 0)); const assertBlank = pixels => assert.ok(pixels.every(p => p === 0));
const assertBoard = pixels => { const assertBoard = pixels => {
@@ -151,7 +181,7 @@ const assertBoard = pixels => {
}; };
test('iOS sleep tap shows the shared board; repeated tap renews timeout and hides to black', async () => { test('iOS sleep tap shows the shared board; repeated tap renews timeout and hides to black', async () => {
const f = fixture(); await f.controller.connect(); f.shell.sleep(); await f.render(); const f = fixture(); await f.connect(); f.shell.sleep(); await f.render();
assertBlank(f.sent.at(-1)); assertBlank(f.sent.at(-1));
await f.hardware(0); assertBoard(f.sent.at(-1)); assert.equal(f.shell.isScreenOn(), false); await f.hardware(0); assertBoard(f.sent.at(-1)); assert.equal(f.shell.isScreenOn(), false);
assert.equal(f.previews.at(-1).title, 'Glanceboard'); assert.equal(f.received.length, 0); assert.equal(f.previews.at(-1).title, 'Glanceboard'); assert.equal(f.received.length, 0);
@@ -165,7 +195,7 @@ test('iOS sleep tap shows the shared board; repeated tap renews timeout and hide
}); });
test('iOS holds and tap-then-holds last until release, including with phone backgrounded', async () => { test('iOS holds and tap-then-holds last until release, including with phone backgrounded', async () => {
const f = fixture(); await f.controller.connect(); f.shell.sleep(); f.controller.pause(); const f = fixture(); await f.connect(); f.shell.sleep(); f.controller.pause();
const previews = f.previews.length; const previews = f.previews.length;
for (const type of [9, 11]) { for (const type of [9, 11]) {
await f.hardware(type, 1); assertBoard(f.sent.at(-1)); await f.hardware(type, 1); assertBoard(f.sent.at(-1));
@@ -176,7 +206,7 @@ test('iOS holds and tap-then-holds last until release, including with phone back
}); });
test('double-tap and other shell wakes replace Glanceboard without waking it again', async () => { test('double-tap and other shell wakes replace Glanceboard without waking it again', async () => {
const f = fixture(); await f.controller.connect(); f.shell.sleep(); const f = fixture(); await f.connect(); f.shell.sleep();
await f.hardware(9); await f.hardware(3); await f.hardware(9); await f.hardware(3);
assert.equal(f.shell.isScreenOn(), true); assert.equal(f.controller.glance.isVisible(), false); assert.equal(f.shell.isScreenOn(), true); assert.equal(f.controller.glance.isVisible(), false);
assert.equal(f.sent.at(-1)[0], 80); assert.equal(f.boardStats.stops, 1); assert.equal(f.sent.at(-1)[0], 80); assert.equal(f.boardStats.stops, 1);
@@ -186,14 +216,14 @@ test('double-tap and other shell wakes replace Glanceboard without waking it aga
}); });
test('head tilt uses Glanceboard when enabled and otherwise wakes the regular UI', async () => { test('head tilt uses Glanceboard when enabled and otherwise wakes the regular UI', async () => {
const f = fixture(); await f.controller.connect(); f.shell.sleep(); const f = fixture(); await f.connect(); f.shell.sleep();
await f.hardware(12, 1); assertBoard(f.sent.at(-1)); assert.equal(f.shell.isScreenOn(), false); await f.hardware(12, 1); assertBoard(f.sent.at(-1)); assert.equal(f.shell.isScreenOn(), false);
await f.hardware(3); f.shell.sleep(); f.settings.tilt = false; await f.hardware(3); f.shell.sleep(); f.settings.tilt = false;
await f.hardware(12, 1); assert.equal(f.shell.isScreenOn(), true); assert.equal(f.sent.at(-1)[0], 80); await f.hardware(12, 1); assert.equal(f.shell.isScreenOn(), true); assert.equal(f.sent.at(-1)[0], 80);
}); });
test('disabled triggers and scrolls do not show the board; awake taps still reach the shell', async () => { test('disabled triggers and scrolls do not show the board; awake taps still reach the shell', async () => {
const f = fixture(); await f.controller.connect(); const f = fixture(); await f.connect();
await f.hardware(0); assert.equal(f.received.at(-1).type, 'click'); assert.equal(f.boardStats.starts, 0); await f.hardware(0); assert.equal(f.received.at(-1).type, 'click'); assert.equal(f.boardStats.starts, 0);
f.shell.sleep(); f.settings.tap = false; f.settings.hold = false; f.shell.sleep(); f.settings.tap = false; f.settings.hold = false;
for (const type of [0, 9, 11, 10, 1, 2]) await f.hardware(type); for (const type of [0, 9, 11, 10, 1, 2]) await f.hardware(type);
@@ -218,7 +248,7 @@ test('phone preview gestures use Glanceboard; losing runtime cleans up holds and
test('iOS lock hides apps and Glanceboard, blocks input, and stays locked when put back on', async () => { test('iOS lock hides apps and Glanceboard, blocks input, and stays locked when put back on', async () => {
const f = fixture(); await f.controller.connect(); f.shell.sleep(); await f.hardware(0); const f = fixture(); await f.connect(); f.shell.sleep(); await f.hardware(0);
f.controller.pause(); f.controller.pause();
// The notification precedes the property change: do not immediately undo it. // The notification precedes the property change: do not immediately undo it.
f.observers.get('lock')(); f.wear(false); await f.render(); f.observers.get('lock')(); f.wear(false); await f.render();
@@ -239,7 +269,7 @@ test('iOS lock hides apps and Glanceboard, blocks input, and stays locked when p
}); });
test('iOS removal before phone lock, setting changes, and observer lifetime', async () => { test('iOS removal before phone lock, setting changes, and observer lifetime', async () => {
const f = fixture(); await f.controller.connect(); f.wear(false); await f.render(); const f = fixture(); await f.connect(); f.wear(false); await f.render();
assert.equal(f.controller.glassesLocked, false); assert.equal(f.controller.glassesLocked, false);
f.observers.get('lock')(); await f.render(); f.observers.get('lock')(); await f.render();
assert.equal(f.controller.glassesLocked, true); assert.equal(f.controller.glassesLocked, true);
@@ -255,7 +285,7 @@ test('iOS removal before phone lock, setting changes, and observer lifetime', as
}); });
test('iOS wear notifications catch protected-data changes missed while suspended', async () => { test('iOS wear notifications catch protected-data changes missed while suspended', async () => {
const f = fixture(); await f.controller.connect(); f.controller.pause(); const f = fixture(); await f.connect(); f.controller.pause();
f.phoneState.protectedDataAvailable = false; f.wear(false); await f.render(); f.phoneState.protectedDataAvailable = false; f.wear(false); await f.render();
assert.equal(f.controller.glassesLocked, true); assert.equal(f.controller.glassesLocked, true);
f.controller.resume(); await f.render(); f.controller.resume(); await f.render();
@@ -267,7 +297,7 @@ test('iOS wear notifications catch protected-data changes missed while suspended
test('iOS locked phone controls cannot dispatch mirror, voice, or keyboard input', async () => { test('iOS locked phone controls cannot dispatch mirror, voice, or keyboard input', async () => {
const f = fixture(); await f.controller.connect(); f.wear(false); const f = fixture(); await f.connect(); f.wear(false);
f.observers.get('lock')(); await f.render(); f.observers.get('lock')(); await f.render();
await f.phone('tap', 'mirror'); await f.phone('long-press', 'watch'); await f.phone('tap', 'mirror'); await f.phone('long-press', 'watch');
f.controller.startVoiceInput(); f.controller.startVoiceInput();
@@ -276,16 +306,16 @@ test('iOS locked phone controls cannot dispatch mirror, voice, or keyboard input
assert.equal(f.received.length, 0); assert.equal(f.received.length, 0);
assert.equal(f.controller.glassesLocked, true); assert.equal(f.controller.glassesLocked, true);
// Locked state also survives transport recovery until a phone unlock. // Locked state also survives transport recovery until a phone unlock.
f.controller.session.onState({ phase: 'retrying' }); f.bridge.emitState('retrying');
f.controller.session.onState({ phase: 'connecting' }); f.bridge.emitState('connecting');
f.controller.session.onState({ phase: 'connected' }); f.bridge.emitState('connected');
await f.render(); await f.render();
assert.ok(f.sent.at(-1).every(p => p === 123)); assert.ok(f.sent.at(-1).every(p => p === 123));
}); });
test('iOS deduplicates original ring ticks before apps, lock toggles and Glanceboard', async () => { test('iOS deduplicates original ring ticks before apps, lock toggles and Glanceboard', async () => {
const f = fixture(); await f.controller.connect(); const f = fixture(); await f.connect();
await f.hardware(0,2,1000); await f.hardware(0,2,1050); await f.hardware(0,2,1000); await f.hardware(0,2,1050);
assert.equal(f.received.length,1); assert.equal(f.received.length,1);
await f.hardware(0,2,1100); assert.equal(f.received.length,2); await f.hardware(0,2,1100); assert.equal(f.received.length,2);
@@ -299,8 +329,8 @@ test('iOS deduplicates original ring ticks before apps, lock toggles and Glanceb
assert.equal(f.shell.isScreenOn(),true); assert.equal(f.shell.isScreenOn(),true);
await f.hardware(3,2,2050); assert.equal(f.shell.isScreenOn(),true); await f.hardware(3,2,2050); assert.equal(f.shell.isScreenOn(),true);
// A new transport session starts a fresh suppression window. // A new transport session starts a fresh suppression window.
f.controller.session.onState({ phase:'retrying' }); f.bridge.emitState('retrying');
f.controller.session.onState({ phase:'connected' }); f.bridge.emitState('connected');
await f.hardware(3,2,2051); assert.equal(f.shell.isScreenOn(),false); await f.hardware(3,2,2051); assert.equal(f.shell.isScreenOn(),false);
}); });
@@ -325,7 +355,7 @@ test('keyboard sessions cannot deliver text after the glasses lock or phone leav
test('external input shares iOS dispatch, including hold release and phone-lock gating', async () => { test('external input shares iOS dispatch, including hold release and phone-lock gating', async () => {
const f = fixture(); await f.controller.connect(); f.wear(false); const f = fixture(); await f.connect(); f.wear(false);
assert.equal(f.remoteHost.ready(), true); assert.equal(f.remoteHost.ready(), true);
assert.equal(f.remoteHost.locked(), false); assert.equal(f.remoteHost.locked(), false);
await f.remoteHost.input('swipe-left', 'watch'); await f.remoteHost.input('swipe-left', 'watch');
+100 -71
View File
@@ -13,78 +13,110 @@ function load(file, modules = {}, globals = {}) {
return context.exports; return context.exports;
} }
const flush = () => new Promise(resolve => setImmediate(resolve)); const flush = () => new Promise(resolve => setImmediate(resolve));
function flasherFixture({ hash = 'custom', failRole = '', backgroundRole = '' } = {}) { const settle = async (rounds = 6) => { for (let i = 0; i < rounds; i++) { await flush(); await new Promise(resolve => setTimeout(resolve, 0)); } };
const calls = [], events = new Map(), native = { idleTimerDisabled: false, applicationState: 0 }; // The Kotlin facades (FaceclawKitIos*) deliver listener callbacks on the main queue; the
// fakes call the TypeScript listener object directly. NSObject.extend is how the wrappers
// implement Kotlin listener protocols.
const nsObject = { extend: methods => ({ new: () => ({ ...methods }) }) };
function bluetooth(identifiers = { right: 'R-ID', left: 'L-ID' }, fail = null) {
return { iosBluetooth: () => ({ resolveDevices: async addresses => {
if (fail) throw new Error(fail);
return Object.fromEntries(Object.entries(identifiers).filter(([role]) => addresses[role]));
} }) };
}
function flasherFixture({ hash = 'custom', outcome = 'success', backgroundOn = '', resolveError = null } = {}) {
const calls = [], events = new Map(), native = { idleTimerDisabled: false, applicationState: 0 }, instances = [];
const app = { suspendEvent: 'suspend', on: (e, cb) => events.set(e, cb), off: e => events.delete(e) }; const app = { suspendEvent: 'suspend', on: (e, cb) => events.set(e, cb), off: e => events.delete(e) };
class Link { const FaceclawKitIosFirmwareFlasher = { alloc: () => ({ initWithRightAddressLeftAddressFirmwarePath(right, left, path) {
closed = false; const flasher = { listener: null, cancelled: false, closed: false,
check() { if (this.closed) throw new Error('Cancelled'); } setListenerListener(listener) { this.listener = listener; },
async resolve() { calls.push('resolve'); } start() {
async connect(role, ota) { this.check(); calls.push(`connect:${role}:${ota}`); } calls.push(`start:${right}:${left}:${path}`);
async authenticate(role) { this.check(); calls.push(`auth:${role}`); } setTimeout(() => {
disconnect(role) { calls.push(`disconnect:${role}`); } for (const lens of ['left', 'right']) {
close() { this.closed = true; calls.push('close'); } if (this.cancelled) break;
} this.listener.onStateStateDetail('connecting', lens); this.listener.onStateStateDetail('flashing', lens);
this.listener.onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal(lens, 1, 1, 1, 1, 16, 16);
if (lens === backgroundOn) events.get('suspend')();
if (outcome === 'fail' && lens === 'right') { this.listener.onCompleteSuccessDetail(false, 'END rejected'); return; }
}
if (this.cancelled) { this.listener.onCompleteSuccessDetail(false, 'Cancelled. Retry with both lenses powered on to complete installation.'); return; }
this.listener.onStateStateDetail('done', 'Both lenses flashed. Your glasses are rebooting.');
this.listener.onCompleteSuccessDetail(true, 'Both lenses flashed. Your glasses are rebooting.');
}, 0);
},
cancel() { this.cancelled = true; calls.push('cancel'); }, close() { this.closed = true; calls.push('close'); } };
instances.push(flasher); return flasher;
} }) };
const { FirmwareFlasher } = load('app/native/firmware-flasher.ios.ts', { const { FirmwareFlasher } = load('app/native/firmware-flasher.ios.ts', {
'@nativescript/core': { Application: app }, '../g2/stock-connection': { StockConnection: Link }, '@nativescript/core': { Application: app },
'../g2/firmware/cfw-patches': { CFW_PATCH_SET: { outputSha256: 'custom', baseSha256: 'stock' } }, '../g2/firmware/cfw-patches': { CFW_PATCH_SET: { outputSha256: 'custom', baseSha256: 'stock' } },
'./firmware-files.ios': { readFirmwareFile: () => new Uint8Array(16), firmwareSha256: () => hash }, './firmware-files.ios': { readFirmwareFile: () => new Uint8Array(16), firmwareSha256: () => hash },
'./ios-bluetooth': { iosBluetooth: () => ({}) }, './ios-bluetooth': bluetooth({ right: 'R-ID', left: 'L-ID' }, resolveError), './kotlin-listener.ios': listenerModule,
'../g2/firmware-ota': { validateFirmware: () => { calls.push('validate'); return []; }, }, { UIApplication: { sharedApplication: native }, UIApplicationState: { Active: 0 }, FaceclawKitIosFirmwareFlasher, FaceclawKitFaceclawFirmwareFlasherListener: {} });
flashLensImage: async (link, role) => {
calls.push(`flash:${role}`);
if (role === backgroundRole) events.get('suspend')();
link.check();
if (role === failRole) throw new Error('END rejected');
} },
}, { UIApplication: { sharedApplication: native }, UIApplicationState: { Active: 0 },
setTimeout: fn => setTimeout(fn, 0) });
const flasher = new FirmwareFlasher({ right: 'r', left: 'l' }, 'prepared.bin'); const flasher = new FirmwareFlasher({ right: 'r', left: 'l' }, 'prepared.bin');
const states = [], progress = [];
flasher.onStateChange((state, detail) => states.push(`${state}:${detail}`)); flasher.onProgress(value => progress.push(value.lens));
const result = new Promise(resolve => flasher.onComplete((success, detail) => resolve({ success, detail }))); const result = new Promise(resolve => flasher.onComplete((success, detail) => resolve({ success, detail })));
return { calls, events, native, flasher, result }; return { calls, events, native, flasher, result, instances, states, progress };
} }
const listenerModule = load('app/native/kotlin-listener.ios.ts', {}, { NSObject: nsObject });
test('iOS flashing authenticates and verifies both lenses in order before reporting success', async () => { test('iOS flashing checks the image hash, resolves identifiers, runs the Kotlin OTA flow and reports success', async () => {
for (const hash of ['custom', 'stock']) { for (const hash of ['custom', 'stock']) {
const f = flasherFixture({ hash }); f.flasher.start(); f.flasher.start(); const f = flasherFixture({ hash }); f.flasher.start(); f.flasher.start();
assert.equal((await f.result).success, true); assert.equal((await f.result).success, true);
assert.deepEqual(f.calls, ['validate', 'resolve', 'connect:left:true', 'auth:left', 'flash:left', 'disconnect:left', assert.deepEqual(f.calls, ['start:R-ID:L-ID:prepared.bin', 'close']);
'connect:right:true', 'auth:right', 'flash:right', 'disconnect:right', 'close']);
assert.equal(f.native.idleTimerDisabled, false); assert.equal(f.events.size, 0); assert.equal(f.native.idleTimerDisabled, false); assert.equal(f.events.size, 0);
await settle();
assert.deepEqual(f.states, ['validating:', 'connecting:left', 'flashing:left', 'connecting:right', 'flashing:right', 'done:Both lenses flashed. Your glasses are rebooting.']);
assert.deepEqual(f.progress, ['left', 'right']);
} }
}); });
test('iOS flashing refuses a changed image before Bluetooth and never reports partial success', async () => { test('iOS flashing refuses a changed image before Bluetooth and never reports partial success', async () => {
const invalid = flasherFixture({ hash: 'changed' }); invalid.flasher.start(); const invalid = flasherFixture({ hash: 'changed' }); invalid.flasher.start();
assert.match((await invalid.result).detail, /SHA-256/); assert.deepEqual(invalid.calls, ['close']); assert.match((await invalid.result).detail, /SHA-256/); assert.deepEqual(invalid.calls, []); assert.equal(invalid.native.idleTimerDisabled, false);
const failure = flasherFixture({ failRole: 'right' }); failure.flasher.start(); const unresolved = flasherFixture({ resolveError: 'Could not find the left arm.' }); unresolved.flasher.start();
assert.match((await unresolved.result).detail, /left arm/); assert.deepEqual(unresolved.calls, []);
const failure = flasherFixture({ outcome: 'fail' }); failure.flasher.start();
const result = await failure.result; assert.equal(result.success, false); assert.match(result.detail, /END rejected/); const result = await failure.result; assert.equal(result.success, false); assert.match(result.detail, /END rejected/);
assert.equal(failure.native.idleTimerDisabled, false); assert.equal(failure.events.size, 0); assert.equal(failure.native.idleTimerDisabled, false); assert.equal(failure.events.size, 0); assert.equal(failure.calls.includes('close'), true);
}); });
test('backgrounding during iOS OTA stops the transfer and directs retry of both lenses', async () => { test('backgrounding during iOS OTA stops the transfer and directs retry of both lenses', async () => {
const f = flasherFixture({ backgroundRole: 'left' }); f.flasher.start(); const f = flasherFixture({ backgroundOn: 'left' }); f.flasher.start();
const result = await f.result; const result = await f.result;
assert.equal(result.success, false); assert.match(result.detail, /left the foreground.*retry both lenses/); assert.equal(result.success, false); assert.match(result.detail, /left the foreground.*retry both lenses/);
assert.equal(f.calls.includes('flash:right'), false); assert.equal(f.native.idleTimerDisabled, false); assert.equal(f.instances[0].cancelled, true); assert.equal(f.progress.includes('right'), false); assert.equal(f.native.idleTimerDisabled, false);
}); });
test('iOS firmware prompt surfaces heartbeat disconnection instead of leaving confirmation stuck', async () => { test('iOS firmware prompt maps addresses to identifiers, forwards the Kotlin flow events and surfaces its errors', async () => {
let heartbeat; const instances = [];
const p = require('../.test-build/app/g2/ble-protocol.js'); const FaceclawKitIosFlashPrompt = { alloc: () => ({ initWithRightAddressLeftAddressWarningTextSkipPrompt(right, left, warning, skip) {
class Link { const prompt = { args: [right, left, warning, skip], listener: null, closed: false, started: 0,
async resolve() {} async connect() {} async authenticate() {} async request() {} setListenerListener(listener) { this.listener = listener; }, start() { this.started++; }, cancel() {}, close() { this.closed = true; } };
onMessage() { return () => {}; } nextMagic() { return 1; } close() {} instances.push(prompt); return prompt;
async write() { throw new Error('Lost connection to the right lens.'); } } }) };
}
const { FlashPromptCommunicator } = load('app/native/flash-prompt-communicator.ios.ts', { const { FlashPromptCommunicator } = load('app/native/flash-prompt-communicator.ios.ts', {
'../g2/stock-connection': { StockConnection: Link }, '../g2/flash-prompt-protocol': {}, '../g2/ble-protocol': p, './ios-bluetooth': bluetooth(), './kotlin-listener.ios': listenerModule,
'./ios-bluetooth': { iosBluetooth: () => ({}) }, }, { FaceclawKitIosFlashPrompt, FaceclawKitFaceclawFlashPromptListener: {} });
}, { setInterval: fn => { heartbeat = fn; return 1; }, clearInterval() {} }); const prompt = new FlashPromptCommunicator({ right: 'r', left: 'l' }, 'warning', { skipPrompt: true }), states = [], results = [], batteries = [];
const prompt = new FlashPromptCommunicator({ right: 'r', left: 'l' }, 'warning'), errors = []; prompt.onStateChange((state, detail) => states.push(`${state}:${detail}`)); prompt.onResult(approved => results.push(approved)); prompt.onBattery(b => batteries.push(b));
prompt.onStateChange((state, detail) => { if (state === 'error') errors.push(detail); }); prompt.start(); prompt.start(); await settle();
prompt.start(); await flush(); heartbeat(); await flush(); assert.equal(instances.length, 1); assert.deepEqual(instances[0].args, ['R-ID', 'L-ID', 'warning', true]); assert.equal(instances[0].started, 1);
assert.deepEqual(errors, ['Lost connection to the right lens.']); prompt.close(); const listener = instances[0].listener;
listener.onStateStateDetail('connected', ''); listener.onBatteryRightPercentLeftPercent(88, -1); listener.onResultApproved(true);
listener.onStateStateDetail('error', 'Lost connection to the right lens.'); await settle();
assert.deepEqual(states, ['connecting:', 'connected:', 'error:Lost connection to the right lens.']);
assert.deepEqual(JSON.parse(JSON.stringify(batteries)), [{ right: 88, left: null }]); assert.deepEqual(results, [true]);
prompt.close(); assert.equal(instances[0].closed, true);
const unresolved = load('app/native/flash-prompt-communicator.ios.ts', {
'./ios-bluetooth': bluetooth({}, 'Wake the glasses.'), './kotlin-listener.ios': listenerModule,
}, { FaceclawKitIosFlashPrompt, FaceclawKitFaceclawFlashPromptListener: {} });
const failed = new unresolved.FlashPromptCommunicator({ right: 'r', left: 'l' }, 'warning'), errors = [];
failed.onStateChange((state, detail) => { if (state === 'error') errors.push(detail); });
failed.start(); await settle(); assert.deepEqual(errors, ['Wake the glasses.']); assert.equal(instances.length, 1);
}); });
test('iOS onboarding returns to the existing main controller, or creates it after first setup', () => { test('iOS onboarding returns to the existing main controller, or creates it after first setup', () => {
@@ -117,37 +149,34 @@ test('pinned stock firmware reproduces the custom image, extracts fonts, and pas
'../util/hex-util': require('../.test-build/app/util/hex-util.js'), '../graphics/evenhub-font': { EvenHubFont: { invalidate() {} } }, '../util/hex-util': require('../.test-build/app/util/hex-util.js'), '../graphics/evenhub-font': { EvenHubFont: { invalidate() {} } },
'../native/firmware-files': { firmwareSha256: hash, writeFirmwareFile: (file, buffer) => fs.writeFileSync(file, new Uint8Array(buffer)) }, '../native/firmware-files': { firmwareSha256: hash, writeFirmwareFile: (file, buffer) => fs.writeFileSync(file, new Uint8Array(buffer)) },
}); });
const { validateFirmware } = require('../.test-build/app/g2/firmware-ota.js');
const stock = await builder.buildStockFirmware(); assert.equal(stock.sha256, patches.CFW_PATCH_SET.baseSha256); const stock = await builder.buildStockFirmware(); assert.equal(stock.sha256, patches.CFW_PATCH_SET.baseSha256);
assert.equal(validateFirmware(fs.readFileSync(stock.path)).length, 6);
const custom = await builder.buildCustomFirmware(); assert.equal(hash(fs.readFileSync(custom.path)), patches.CFW_PATCH_SET.outputSha256); const custom = await builder.buildCustomFirmware(); assert.equal(hash(fs.readFileSync(custom.path)), patches.CFW_PATCH_SET.outputSha256);
assert.equal(validateFirmware(fs.readFileSync(custom.path)).length, 6);
assert.equal(builder.hasExtractedEvenHubFonts(), true); assert.equal(builder.hasExtractedEvenHubFonts(), true);
}); });
for (const unsolicited of [false, true]) test(`iOS firmware probe ${unsolicited ? 'accepts a version push with device-owned magic' : 'falls back to the left lens after a silent right lens'}`, async () => { test('iOS firmware probe resolves identifiers, runs the Kotlin flow and settles once', async () => {
const p = require('../.test-build/app/g2/ble-protocol.js'), calls = []; const instances = [];
let listener; const FaceclawKitIosDeviceInfoProbe = { alloc: () => ({ initWithRightAddressLeftAddress(right, left) {
const str = (field, text) => p.bytes(field, new Uint8Array(Buffer.from(text))); const probe = { args: [right, left], listener: null, closed: 0, setListenerListener(listener) { this.listener = listener; }, start() { this.started = true; }, cancel() {}, close() { this.closed++; } };
const version = { sid: p.SID.settings, payload: p.concat(p.bytes(4, p.concat(str(5, '2.2.9.22'), str(6, '2.2.9.22'))), str(100, 'faceclaw/13')) }; instances.push(probe); return probe;
class Link { } }) };
onMessage(fn) { listener = fn; } async resolve() {} check() {} isConnected() { return true; }
async connect(role) { calls.push(`connect:${role}`); }
async authenticate(role) { calls.push(`auth:${role}`); }
async request(role, sid) {
if (sid === p.SID.launch) return {};
if (role === 'right') { if (unsolicited) listener(role, version); throw new Error('Timeout'); }
return version;
}
close() { calls.push('close'); }
}
const { DeviceInfoProbe } = load('app/native/device-info-probe.ios.ts', { const { DeviceInfoProbe } = load('app/native/device-info-probe.ios.ts', {
'../g2/stock-connection': { StockConnection: Link, StockTimeout: class extends Error {} }, './ios-bluetooth': bluetooth(), './kotlin-listener.ios': listenerModule,
'../g2/ble-protocol': p, './ios-bluetooth': { iosBluetooth: () => ({}) }, }, { FaceclawKitIosDeviceInfoProbe, FaceclawKitFaceclawDeviceInfoProbeListener: {} });
}); const probe = new DeviceInfoProbe('r', 'l'), states = [];
const info = await new DeviceInfoProbe('r', 'l').run(); probe.onStateChange((state, detail) => states.push(`${state}:${detail}`));
assert.equal(info.rightVersion, '2.2.9.22'); assert.equal(info.leftVersion, '2.2.9.22'); assert.equal(info.extension, 'faceclaw/13'); const pending = probe.run(); await settle();
assert.deepEqual(calls, ['connect:right', 'auth:right', 'connect:left', 'auth:left', 'close']); assert.deepEqual(instances[0].args, ['R-ID', 'L-ID']); assert.equal(instances[0].started, true);
instances[0].listener.onStateStateDetail('querying', 'right');
instances[0].listener.onResultLeftVersionRightVersionExtension('2.2.9.22', '2.2.9.22', 'faceclaw/13');
instances[0].listener.onErrorMessage('late error is ignored');
const info = await pending; await settle();
assert.deepEqual(JSON.parse(JSON.stringify(info)), { leftVersion: '2.2.9.22', rightVersion: '2.2.9.22', extension: 'faceclaw/13' });
assert.deepEqual(states, ['connecting:right', 'querying:right']); assert.equal(instances[0].closed, 1);
const failing = new DeviceInfoProbe('r'); const failed = failing.run(); await settle();
assert.deepEqual(instances[1].args, ['R-ID', '']);
instances[1].listener.onErrorMessage("Connected, but couldn't read a firmware version.");
await assert.rejects(failed, /firmware version/);
}); });
test('iOS discovery stops pending scans on cancellation and ignores a late permission result', async () => { test('iOS discovery stops pending scans on cancellation and ignores a late permission result', async () => {
-32
View File
@@ -1,32 +0,0 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const protocol = require('../.test-build/app/g2/ble-protocol.js');
const decode = (payload) => protocol.decodeGlassesInput({ sid:224,flag:1,payload });
const packet = Buffer.from('08026a151a13080e1002a2060c524901010aabcd00efcdab89','hex');
test('timestamped CFW packet decodes atomically with source and original unsigned tick', () => {
const input = decode(packet);
assert.equal(input.eventType,14); assert.equal(input.eventSource,2);
assert.deepEqual(input.ringInput,{tick:0x89abcdef,type:10,aux:171,speed:205});
for (const tick of [0,1,0xffffffff]) {
const p = Buffer.from(packet); p.writeUInt32LE(tick,21);
assert.equal(decode(p).ringInput.tick,tick);
}
});
test('legacy/invalid metadata leaves input intact without inventing a clock', () => {
assert.equal(decode(Buffer.from('08026a041a02080e','hex')).ringInput,undefined);
for (const offset of [9,13,14,15,16,20]) {
const p = Buffer.from(packet); p[offset]++;
assert.equal(decode(p).ringInput,undefined);
}
});
// Firmware regression: MsgPbTxByBle sets flag 0; Thread_MsgPbNotifyByBle
// (also used by stock SysEvent) sets flag 1. Payload validity is insufficient.
test('ring input must use the notification envelope, never the reply envelope', () => {
assert.equal(protocol.decodeGlassesInput({ sid:224,flag:0,payload:packet }),null);
for (const flag of [1,6]) {
const input = protocol.decodeGlassesInput({ sid:224,flag,payload:packet });
assert.equal(input.eventType,14); assert.equal(input.ringInput.tick,0x89abcdef);
}
});
+3 -6
View File
@@ -2,7 +2,9 @@
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "ES2020", "target": "ES2020",
"lib": ["ES2020"], "lib": [
"ES2020"
],
"strict": true, "strict": true,
"esModuleInterop": true, "esModuleInterop": true,
"skipLibCheck": true, "skipLibCheck": true,
@@ -14,15 +16,10 @@
"../app/remote/protocol.ts", "../app/remote/protocol.ts",
"../app/remote/listeners.ts", "../app/remote/listeners.ts",
"../app/g2/ancs-client.ts", "../app/g2/ancs-client.ts",
"../app/g2/ble-protocol.ts",
"../app/graphics/lvgl-font.ts", "../app/graphics/lvgl-font.ts",
"../app/phone-ui/ble-bandwidth-meter.ts", "../app/phone-ui/ble-bandwidth-meter.ts",
"../app/apps/terminal/hub-header.ts", "../app/apps/terminal/hub-header.ts",
"../app/g2/ios-peripheral-identity.ts", "../app/g2/ios-peripheral-identity.ts",
"../app/g2/glasses-session.ts",
"../app/g2/stock-connection.ts",
"../app/g2/firmware-ota.ts",
"../app/g2/flash-prompt-protocol.ts",
"../app/graphics/surface-compositor.ts", "../app/graphics/surface-compositor.ts",
"../app/graphics/image.ts", "../app/graphics/image.ts",
"../app/graphics/plane.ts", "../app/graphics/plane.ts",