Merge remote-tracking branch 'upstream/main' into pr36-merge

# Conflicts:
#	app/g2/dashboard-controller.ts
This commit is contained in:
JMO committed 2026-09-25 08:08:31 -07:00
commit 6a3f5640d5
411 files changed
+37477 -23831

No files matched your search

-15
View File
@@ -1,15 +0,0 @@
import { fromData, toData, nativeArray } from '../native/kotlin-data'
declare const FaceclawKitIosProtocol: any
const protocol = FaceclawKitIosProtocol.new()
export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, width: number, height: number, frameId: number): Uint8Array | null {
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 ||
width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 ||
!previous || previous.length !== width * height / 2 || next.length !== previous.length) return null
const result = protocol.boundingBoxPreviousNextWidthHeightFrameId(toData(previous), toData(next), width, height, frameId)
return result ? fromData(result) : null
}
export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] {
if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2)
throw new Error('Invalid framebuffer dimensions')
return nativeArray(protocol.fullFrameBandsDataWidthHeightFirstId(toData(packed), width, height, firstId), fromData)
}
-49
View File
@@ -1,49 +0,0 @@
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([3, 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([3, 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()
+26
View File
@@ -0,0 +1,26 @@
export const DEFAULT_BRIGHTNESS_CURVE = "0:0,0.2:7,1.5:33,5:100";
const PREVIOUS_DEFAULT_CURVE = "0:0,1:10,10:30,100:65,1000:100";
/** Percent of the configured range; defaults (20–100) give 20/26/46/100. */
export function brightnessCurveError(value: string): string | null {
const pairs = value.split(",").map(part => part.trim().split(":"));
if (pairs.length < 2 || pairs.length > 16 || pairs.some(p => p.length !== 2 || p.some(s => !s.trim())))
return "Use 2–16 lux:percent pairs separated by commas.";
const points = pairs.map(pair => pair.map(Number));
if (points.some(([lux, percent]) => !Number.isFinite(lux) || !Number.isFinite(percent)
|| lux < 0 || lux > 1_000_000 || percent < 0 || percent > 100))
return "Lux must be 0–1000000 and percent must be 0–100.";
if (points[0][0] !== 0 || points[0][1] !== 0 || points[points.length - 1][1] !== 100)
return "Start at 0:0 and end at 100%. Set dark-room brightness with Auto minimum.";
if (points.some(([lux, percent], i) => i > 0 && (lux <= points[i-1][0] || percent < points[i-1][1])))
return "Lux must increase and brightness must not decrease.";
return null;
}
/** Keep incomplete drafts intact. Validation must never replace typed input. */
export function normalizeBrightnessCurve(value: string | null | undefined): string {
if (value == null) return DEFAULT_BRIGHTNESS_CURVE;
if (brightnessCurveError(value)) return value;
const canonical = value.split(",").map(pair => pair.split(":").map(Number).join(":")).join(",");
return canonical === PREVIOUS_DEFAULT_CURVE ? DEFAULT_BRIGHTNESS_CURVE : canonical;
}
-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)))
}
+37
View File
@@ -0,0 +1,37 @@
/** Message IDs and header flags mirror g2flash/patches/zlib_glue.c. */
/** Retired wire ID; used internally by the phone optimizer. */
export const CFW_MSG_BOUNDING_BOX = 3
export const CFW_MSG_BUZZER = 5
/** Retired wire ID; used internally by the phone optimizer. */
export const CFW_MSG_FULL_FRAME = 6
export const CFW_MSG_DIAGNOSTICS = 7
/** Retired wire ID; used internally by the phone optimizer. */
export const CFW_MSG_MULTI_SEGMENT = 8
/** Retired wire ID; used internally by the phone optimizer. */
export const CFW_MSG_RECT_COPY = 9
export const CFW_MSG_COMPASS = 10
export const CFW_MSG_CLEANUP = 11
export const CFW_MSG_RETIRED_12 = 12
export const CFW_MSG_RETIRED_13 = 13
export const CFW_MSG_RETIRED_14 = 14
/** Retired wire ID; used internally by the phone optimizer. */
export const CFW_MSG_STOCK_FONT_STRING = 15
export const CFW_MSG_AMBIENT_LIGHT = 16
export const CFW_MSG_RING_BATTERY = 17
export const CFW_MSG_RETIRED_CACHE_WRITE = 18
/** Retired wire ID; used internally by the phone optimizer. */
export const CFW_MSG_CACHED_IMAGE = 19
/** Retired wire ID; used internally by the phone optimizer. */
export const CFW_MSG_CACHED_TEXT = 20
export const CFW_MSG_UPLOAD_RESOURCE = 21
export const CFW_MSG_EVICT_RESOURCE = 22
export const CFW_MSG_PANEL_READ = 23
export const CFW_MSG_PANEL_WRITE = 24
export const CFW_MSG_PANEL_PATTERN = 25
export const CFW_MSG_DRAW_CALLS = 26
export const CFW_MSG_SET_ROOT_DISPLAY_LIST = 27
export const CFW_MSG_PRESENT = 28
export const CFW_MSG_CREATE_SURFACE = 29
export const CFW_MSG_BRIGHTNESS = 30
export const CFW_MSG_TYPE_MASK = 127
export const CFW_MSG_FLAG_LENSES_DIFFER = 128
-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
}
-56
View File
@@ -1,56 +0,0 @@
import { concat, crc16, SID } from './ble-protocol'
declare const require: (name: string) => any
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
}
+83 -47
View File
@@ -1,6 +1,6 @@
import { startRemoteInput } from "../remote/service";
import { acceptInput, resetRingInputFilter } from "../ui/input-monitor";
import { Application, ImageSource } from "@nativescript/core";
import { Application, Dialogs, ImageSource } from "@nativescript/core";
import { EvenAIStatus, EvenAIStatusName, EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName, WatchGestureType, WatchGestureTypeName } from "./events";
import { isValidMacAddress, loadDeviceAddresses } from "./device-addresses";
import {
@@ -71,7 +71,7 @@ import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-a
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible, type WindowHeightMode } from "../ui/shell/geometry";
import { type LayerActions, type TextSettingsEditToggle } from "../ui/layers";
import { type KeyboardInputSession } from "../ui/shell/keyboard-input";
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, brightnessSetting, brightnessSettingToLevel, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
import { assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayModeSetting, navigateDisplayModeSetting, navigateVerticalPositionSetting, terminalDisplayModeSetting, terminalVerticalPositionSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, verticalPositionSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings";
import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization";
import {
getInstalledEvenHubAppById,
@@ -213,6 +213,13 @@ class DashboardController {
*/
private activeTextEditorOnCancel: (() => void) | null = null;
private activeTextEditorToggle: TextSettingsEditToggle | null = null;
/**
* The active settings' values (and the toggle's) when the edit began. The
* phone fields write through on every keystroke, so this is what the
* editor's Cancel button puts back.
*/
private activeTextSettingOriginalValues: string[] = [];
private activeTextEditorToggleOriginalValue = false;
private evenNotificationActive = false;
private evenAppConflictMessage = "";
private firmwareWarningMessage = "";
@@ -276,7 +283,6 @@ class DashboardController {
private lockSurfaceConfigured = false;
private lastLockScreenEnabled = lockScreenEnabledSetting.get();
private offState: (() => void) | null = null;
private offLog: (() => void) | null = null;
private offRing: (() => void) | null = null;
private offBattery: (() => void) | null = null;
private offSilentMode: (() => void) | null = null;
@@ -909,11 +915,14 @@ class DashboardController {
*/
private pushBrightness(force = false): void {
if (!this.communicator) return;
const value = brightnessSetting.get();
const preferences = getBrightnessPreferences();
const value = JSON.stringify(preferences);
if (!force && value === this.lastPushedBrightness) return;
this.lastPushedBrightness = value;
const level = brightnessSettingToLevel(value);
void this.communicator.setBrightness(level === null, level ?? 0).catch(() => {});
void this.communicator.configureBrightness(preferences).catch((error) => {
this.lastPushedBrightness = null;
this.appendLog(`brightness configuration failed: ${this.formatError(error)}`);
});
}
/** Save the occupied part of the composited screen as a 4-bit grayscale PNG. */
@@ -1336,9 +1345,13 @@ class DashboardController {
ring: ringAddress,
});
this.communicator = communicator;
this.offLog = communicator.onLog((line) => {
this.appendLog(line);
});
// Size the compositor before anything else is awaited: windows opened
// during the bridge start-ups below (the terminal hub, restored apps)
// configure their surfaces through this.communicator, and the Java call
// queue is FIFO, so enqueuing the screen size first guarantees it lands
// ahead of them. Mirrors ensurePreviewDisplay.
await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT);
await communicator.configureBrightness(getBrightnessPreferences());
this.offState = communicator.onStateChange((state) => {
if (state.phase !== "connected") resetRingInputFilter();
if (state.phase === "unpaired") {
@@ -1506,7 +1519,6 @@ class DashboardController {
await nightscoutBridge.start();
// Register the compositor surfaces: the shell chrome above all windows,
// and a surface per live window (only the foreground one is composited).
await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT);
await communicator.configureSurface(SHELL_SURFACE_ID, {
x: 0,
y: 0,
@@ -1551,8 +1563,6 @@ class DashboardController {
const message = this.formatError(error);
this.offState?.();
this.offState = null;
this.offLog?.();
this.offLog = null;
this.offRing?.();
this.offRing = null;
this.offBattery?.();
@@ -1691,8 +1701,6 @@ class DashboardController {
this.clearDashboardTimer();
this.offState?.();
this.offState = null;
this.offLog?.();
this.offLog = null;
this.offRing?.();
this.offRing = null;
this.offBattery?.();
@@ -1896,9 +1904,11 @@ class DashboardController {
onCancel?: () => void,
): void {
this.activeTextSettings = Array.from(settings.slice(0, 2));
this.activeTextSettingOriginalValues = this.activeTextSettings.map((setting) => setting.get());
this.activeTextEditorTitle = title;
this.activeTextEditorOnFinish = onFinish ?? null;
this.activeTextEditorToggle = toggle ?? null;
this.activeTextEditorToggleOriginalValue = toggle?.setting.get() ?? false;
this.activeTextEditorOnCancel = onCancel ?? null;
this.emit();
}
@@ -2020,6 +2030,7 @@ class DashboardController {
// finishTextSettingEdit clears this first, so a completed edit never fires it.
const onCancel = this.activeTextEditorOnCancel;
this.activeTextSettings = [];
this.activeTextSettingOriginalValues = [];
this.activeTextEditorTitle = "";
this.activeTextEditorOnFinish = null;
this.activeTextEditorToggle = null;
@@ -2038,6 +2049,11 @@ class DashboardController {
*/
finishActiveTextSettingEdit(): void {
if (!this.activeTextSettings.length) return;
const invalid = this.activeTextSettings.find(setting => setting.validationError());
if (invalid) {
void Dialogs.alert({ title: invalid.editorTitle, message: invalid.validationError()!, okButtonText: "OK" });
return;
}
const onFinish = this.activeTextEditorOnFinish;
// Completing is not cancelling: drop the cancel callback before
// endTextSettingEdit, which fires whatever is still set.
@@ -2050,6 +2066,28 @@ class DashboardController {
onFinish?.();
}
/**
* The phone editor's Cancel button: put back the values the edit started
* with, end the edit as a cancel (the caller's onCancel fires), and
* navigate the Settings app's glasses editor out of the edit page.
*/
cancelActiveTextSettingEdit(): void {
if (!this.activeTextSettings.length) return;
this.activeTextSettings.forEach((setting, index) => {
const original = this.activeTextSettingOriginalValues[index] ?? "";
if (setting.get() !== original) setting.set(original);
});
const toggle = this.activeTextEditorToggle;
if (toggle && toggle.setting.get() !== this.activeTextEditorToggleOriginalValue) {
toggle.setting.set(this.activeTextEditorToggleOriginalValue);
}
const closesGlassesEditor = this.activeTextSettings.length === 1;
this.endTextSettingEdit();
if (closesGlassesEditor && this.textEditorHost?.closeTextEditor()) {
this.textEditorHost.requestRender();
}
}
private updateTextSetting(setting: ConfigSettingString, value: string): void {
if (setting.get() !== value) {
setting.set(value);
@@ -2255,6 +2293,7 @@ class DashboardController {
const host = new WorkerAppHost({
appId,
worker: createWorker(),
onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId); },
configureSurface: (surfaceId, visible, heightMode) =>
this.configureWindowSurface(surfaceId, visible, heightMode),
setSurfaceVisible: (surfaceId, visible) => this.setWindowSurfaceVisible(surfaceId, visible),
@@ -2499,7 +2538,7 @@ class DashboardController {
beginRenderPass(!wantFreshData);
const paintStartedAtMs = Date.now();
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => shell.paintSurface()),
frameTimings.runWithFrame(frameId, () => shell.paintScene()),
);
const paintMs = Date.now() - paintStartedAtMs;
const paintUsedStaleData = endRenderPass();
@@ -2513,36 +2552,11 @@ class DashboardController {
}
// The display-target check that used to live here now runs before the
// paint, at the top of this function. Nothing between the two can change
// it -- paintSurface is synchronous and there is no await in between -- so
// it -- paintScene is synchronous and there is no await in between -- so
// repeating it here is dead code, and the compiler agrees: the early
// return narrows this.phase, making the "charging" comparison impossible.
const display = this.display;
// A shell overlay that dims what it covers (a context menu) must dim the
// window surfaces too, which live below the shell surface in the
// compositor: forward the factor before this frame composites.
const underlayDim = shell.underlayDim();
if (underlayDim !== this.appliedUnderlayDim) {
this.appliedUnderlayDim = underlayDim;
await display.setUnderlayDim(SHELL_SURFACE_Z_ORDER, underlayDim);
}
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
// Spanned because the bridge serializes Java calls: a frame can sit here
// behind another surface's submission, which is otherwise an unexplained
// jump between the paint spans and the composite.
await frameTimings.spanAsync(frameId, "submit", () =>
display.submitSurfaceFrame(
SHELL_SURFACE_ID,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
preparedDraws,
),
);
await frameTimings.spanAsync(frameId, "submit", () => display.submitShellScene(planes, paintMs, frameId));
// Backpressure: the next shell render waits for this one to reach the
// glasses. Timing out here means the loop was blocked for the full timeout
// and any input arriving meanwhile had its chrome repaint delayed, so say
@@ -2651,7 +2665,7 @@ class DashboardController {
}
private appendLog(line: string): void {
console.log(`[${formatTimestamp(new Date())}] ${line}`);
//console.log(`[${formatTimestamp(new Date())}] ${line}`);
}
private setDisplayPreview(preview: ImageSource | null): void {
@@ -2668,6 +2682,22 @@ class DashboardController {
*/
private previewTrailingTimer: ReturnType<typeof setTimeout> | null = null;
private lastRecordCaptureAtMs = 0;
private phonePreviewVisible: (() => boolean) | null = null;
/** The main page owns the mirror; other phone pages must not keep it rendering. */
attachPhonePreview(isVisible: () => boolean): () => void {
this.phonePreviewVisible = isVisible;
this.lastConnectedPreviewUpdateAtMs = 0;
this.updateCompositePreview();
return () => {
if (this.phonePreviewVisible !== isVisible) return;
this.phonePreviewVisible = null;
if (this.previewTrailingTimer) {
clearTimeout(this.previewTrailingTimer);
this.previewTrailingTimer = null;
}
};
}
/**
* Refresh the preview now if the floor allows, otherwise once the floor
@@ -2675,6 +2705,7 @@ class DashboardController {
* of waiting for the safety-net poll.
*/
private schedulePreviewUpdate(): void {
if (!this.phonePreviewVisible && !this.screenRecordingActive) return;
if (this.previewTrailingTimer) return;
const wait = this.lastConnectedPreviewUpdateAtMs + CONNECTED_PREVIEW_MIN_UPDATE_MS - Date.now();
if (wait <= 0) {
@@ -2702,6 +2733,13 @@ class DashboardController {
return;
}
this.lastConnectedPreviewUpdateAtMs = now;
// A user-requested GIF is an independent consumer, including while the
// phone is asleep or another page/app is foregrounded.
if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) {
this.lastRecordCaptureAtMs = now;
display.recordScreenFrame();
}
if (!this.phonePreviewVisible?.()) return;
// The connected foreground service intentionally keeps this controller
// alive after the phone UI is backgrounded. Do not keep constructing
// 640x480 Android Bitmaps for a window that cannot display them: besides
@@ -2710,10 +2748,8 @@ class DashboardController {
if (global.isAndroid) {
const activity = Application.android.foregroundActivity;
if (!activity || !activity.hasWindowFocus()) return;
}
if (this.screenRecordingActive && now - this.lastRecordCaptureAtMs >= RECORDING_MIN_CAPTURE_MS) {
this.lastRecordCaptureAtMs = now;
display.recordScreenFrame();
const power = activity.getSystemService(android.content.Context.POWER_SERVICE) as android.os.PowerManager;
if (!power?.isInteractive()) return;
}
const preview = display.getCompositePreview(previewColorSetting.get() === "green");
if (preview) {
+1 -1
View File
@@ -26,7 +26,7 @@ export type FirmwareInfo = {
};
/** The Faceclaw firmware revision this build of the app needs. */
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 22;
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 34;
const FACECLAW_PREFIX = "Faceclaw/";
const LEGACY_PREFIX = "EVENCFW";
-91
View File
@@ -1,91 +0,0 @@
/** Stock EVENOTA validation and transfer, matching Android's flasher/g2flash.py. */
import { StockConnection, OTA_WRITE, OTA_NOTIFY } from './stock-connection'
import { concat } from './ble-protocol'
import type { FlashProgress } from './firmware-types'
export type FirmwareSegment = { offset: number; size: number; name: string }
const CRC_TABLE = Array.from({ length: 256 }, (_, b) => {
let c = b << 24
for (let i = 0; i < 8; i++) c = c & 0x80000000 ? (c << 1) ^ 0x1edc6f41 : c << 1
return c >>> 0
})
export function firmwareCrc(data: Uint8Array): number {
let crc = 0
for (const byte of data) crc = (crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ byte) & 255]
return crc >>> 0
}
export function validateFirmware(image: Uint8Array): FirmwareSegment[] {
const u32 = (offset: number) => {
if (offset < 0 || offset + 4 > image.length) throw new Error('Firmware header is truncated.')
return new DataView(image.buffer, image.byteOffset, image.byteLength).getUint32(offset, true)
}
if (image.length < 0x40) throw new Error('File is too small to be a firmware image.')
const count = u32(8)
if (count < 5 || count > 6 || 0x40 + count * 16 > image.length) throw new Error('Expected 5–6 firmware components.')
const segments: FirmwareSegment[] = []
for (let i = 0; i < count; i++) {
const toc = 0x40 + i * 16, offset = u32(toc + 4)
if (offset < 0x40 + count * 16 || offset + 128 > image.length) throw new Error('Firmware component header is out of bounds.')
const size = u32(offset + 8), end = offset + 128 + size
if (!size || end > image.length) throw new Error('Firmware component payload is truncated.')
if (segments.some(s => offset < s.offset + 128 + s.size && s.offset < end)) throw new Error('Firmware components overlap.')
const nameBytes = image.subarray(offset + 48, offset + 128)
const name = String.fromCharCode(...nameBytes.subarray(0, nameBytes.indexOf(0) < 0 ? 80 : nameBytes.indexOf(0)))
const crc = firmwareCrc(image.subarray(offset + 128, end))
if (crc !== u32(toc + 12) || crc !== u32(offset + 12)) throw new Error(`Component ${name} CRC32C is stale (image not checksum-fixed).`)
segments.push({ offset, size, name })
}
const mains = segments.filter(s => s.name === 'ota/s200_firmware_ota.bin')
if (mains.length !== 1) throw new Error('Required main firmware component is missing or duplicated.')
const main = mains[0], payload = main.offset + 128
if (main.size < 32 || u32(payload + 0x14) !== 0x00438000 || (u32(payload) & 0xffffff) !== main.size)
throw new Error('Main-app preamble load address or length is invalid.')
if (0x00438000 + main.size - 32 > 0x007f0000) throw new Error('Main-app exceeds the safe MRAM ceiling — refusing to flash.')
return segments
}
export async function flashLensImage(link: StockConnection, role: string, image: Uint8Array, segments: FirmwareSegment[],
progress: (value: FlashProgress) => void, log: (line: string) => void,
delay: (ms: number) => Promise<void>): Promise<void> {
let sequence = 0
const ctrl = async (op: number, payload = new Uint8Array(), data?: Uint8Array): Promise<number> => {
sequence = (sequence + 1) & 255
const message = await link.exchange(role, 0xc0, concat(new Uint8Array([op]), payload),
message => message.command === op, data ? 4000 : 8000, OTA_WRITE, OTA_NOTIFY, 0, sequence, data)
return message.payload[1]
}
const accepted = (status: number) => [0, 8, 9].includes(status)
const begin = await ctrl(0)
if (!accepted(begin)) throw new Error(`OTA BEGIN rejected (status ${begin}).`)
const total = segments.reduce((sum, segment) => sum + segment.size, 0)
let before = 0
for (const [index, segment] of segments.entries()) {
let verified = false, failure: unknown
for (let attempt = 0; attempt < 3; attempt++) {
link.check()
if (attempt) { log(`${segment.name}: re-flash attempt ${attempt + 1}/3`); await delay(1500) }
try {
const status = await ctrl(1, image.slice(segment.offset, segment.offset + 128))
if (status !== 0) throw new Error(`FILE_CHECK rejected (status ${status}).`)
const blocks = Math.ceil(segment.size / 4096)
for (let block = 0; block < blocks; block++) {
const start = segment.offset + 128 + block * 4096
const data = image.slice(start, Math.min(start + 4096, segment.offset + 128 + segment.size))
let ok = false
for (let retry = 0; retry < 3; retry++) {
if (await ctrl(2, new Uint8Array(), data) === 0) { ok = true; break }
}
if (!ok) throw new Error(`Block ${block + 1} rejected three times.`)
if (block % 20 === 0 || block === blocks - 1)
progress({ lens: role, componentIndex: index + 1, componentCount: segments.length, blockIndex: block + 1,
blockCount: blocks, bytesSent: before + Math.min((block + 1) * 4096, segment.size), bytesTotal: total })
}
const end = await ctrl(3)
if (!accepted(end)) throw new Error(`END verification failed (status ${end}).`)
log(`${role} ${segment.name}: END verify OK (status ${end})`); verified = true; break
} catch (error) { link.check(); failure = error }
}
if (!verified) throw new Error(`Component ${segment.name} failed after 3 attempts: ${failure}`)
before += segment.size
}
}
+34 -34
View File
@@ -19,12 +19,12 @@ export type FirmwarePatchSet = {
export const CFW_PATCH_SET: FirmwarePatchSet = {
"base": "g2_2.3.0.24.bin",
"baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979",
"outputSha256": "059ff37a5455b9b272b715ab62c2910ee4dc4966fbfb2e117159bda746bd7315",
"outputSha256": "7d8764f8b720252354dcd1695d23578b11f9b7cf5df9895d08b9f6668e9d0ee7",
"patches": [
{
"offset": 1045797,
"old": "38b584b0",
"new": "5af317bf",
"new": "5df3a9b8",
"desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease"
},
{
@@ -36,49 +36,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1437923,
"old": "fef788ff",
"new": "fbf226fc",
"new": "fdf2b8fd",
"desc": "cfw_ancs_event"
},
{
"offset": 1437951,
"old": "fef7e8fb",
"new": "fbf260f9",
"new": "fdf2f2fa",
"desc": "cfw_ancs_open"
},
{
"offset": 1437969,
"old": "fef7e7fb",
"new": "fbf277f9",
"new": "fdf209fb",
"desc": "cfw_ancs_close"
},
{
"offset": 1388387,
"old": "39694d00",
"new": "35407d00",
"new": "59637d00",
"desc": "connection-bound ANCS EUS write callback"
},
{
"offset": 971497,
"old": "fff717f8",
"new": "6df347f8",
"new": "6ff3d9f9",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 971845,
"old": "fef769ff",
"new": "6cf399ff",
"new": "6ff32bf9",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 979579,
"old": "fdf74ef8",
"new": "6bf37ef8",
"new": "6df310fa",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 1428895,
"old": "f8f76cfd",
"new": "faf224f9",
"new": "fcf228fa",
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
},
{
@@ -108,145 +108,145 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
{
"offset": 1248487,
"old": "d4f712fb",
"new": "28f378f9",
"new": "2af3f2fa",
"desc": "bl settings_send_wrapper (append caps field 100)"
},
{
"offset": 1246183,
"old": "f5f7e2f8",
"new": "28f310fb",
"new": "2af376fc",
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
},
{
"offset": 975219,
"old": "02f083fc",
"new": "6af372fb",
"new": "6cf3e2fc",
"desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)"
},
{
"offset": 975411,
"old": "02f023fc",
"new": "6af3e0fb",
"new": "6cf346fd",
"desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)"
},
{
"offset": 975103,
"old": "07f085fb",
"new": "6cf350f8",
"new": "6ef3e2f9",
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
},
{
"offset": 830549,
"old": "19f03efb",
"new": "8ff3fff9",
"new": "91f391fb",
"desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)"
},
{
"offset": 830795,
"old": "1ff071ff",
"new": "8ff3b8f9",
"new": "91f34afb",
"desc": "bl gesture_press (owned event 9 before stock menu gate)"
},
{
"offset": 830887,
"old": "1ff043ff",
"new": "8ff3b2f9",
"new": "91f344fb",
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
},
{
"offset": 831763,
"old": "1ff08dfd",
"new": "8ef3e8ff",
"new": "91f37af9",
"desc": "bl gesture_release (owned event 10 before stock menu gate)"
},
{
"offset": 1570209,
"old": "7fb50600",
"new": "d9f209bd",
"new": "dbf283be",
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
},
{
"offset": 1051545,
"old": "f6f787f9",
"new": "53f3d7fe",
"new": "55f311f9",
"desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)"
},
{
"offset": 1051861,
"old": "f6f7e9f8",
"new": "53f339fe",
"new": "55f373f8",
"desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)"
},
{
"offset": 1262237,
"old": "d9f7d0fd",
"new": "24f3cff9",
"new": "26f333fb",
"desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)"
},
{
"offset": 1262337,
"old": "d9f79efd",
"new": "24f39df9",
"new": "26f301fb",
"desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)"
},
{
"offset": 1309405,
"old": "67f02dff",
"new": "18f3b7fa",
"new": "1af30bfc",
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
},
{
"offset": 1307865,
"old": "fff7c9fb",
"new": "18f335fe",
"new": "1af389ff",
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
},
{
"offset": 1300937,
"old": "00210170",
"new": "1af31bfb",
"new": "1cf36ffc",
"desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)"
},
{
"offset": 4537963,
"old": "",
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6b4ff29c5416460d46ccf2ff0bc2f229044ff6ff7900274ff00108019006e048f2a351c0f24501bff32f8f8847d4f800a0bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c3804ff4407005f073f9002800f0b7824ff60051cff6ff714218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001f51457ff42fafbff35f8f00bf44e8000121b154e8001f5145f8d023e7aa4645ea064100260127c0f8701206e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c2804ff4407005f090f8002800f0d4814ff60051cff6ff7100bf4218403182f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382f8196382f81a6382f81b6382f81c6382f81d6382f81e6382f81f6382f8206382f8216382f8226382f8236382f8246382f8256382f8266382f8276382f8286382f8296382f82a6382f82b6382f82c6382f82d6382f82e6382f82f6382f8306382f8316382f8326382f8336382f8346382f8356382f8366382f8376382f8386382f8396382f83a6382f83b6382f83c6382f83d6382f83e6382f83f637ff47cafc0f800b08772a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff430afbff35f8f44e8000121b154e8001fa942f8d025e7d0f8746242f6f767c0f24407002ebff35f8f00f0f38007f19a0230464ff0ff314ff0ff379047002840f0498147f6df40c0f24f00804700274ff001080397adf8107006e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0e9804ff4407004f094ff002800f0e1804ff60051cff6ff7100bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff42fafbff35f8f00bf44e8000149b354e8001fa942f8d023e705460020b847b0b105f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b10646f1e64ff0ff3742e007f5a9718847eae60121f1e70020019d42f6f764002d4ff0010bc0f2440418bfbaf1000f33d10df108098046484602f061fa03ab28465146012206f093fa9df8111007464846002918bf4ff0ff3702f08dfbbbf1000f044608d19df8100028b94af21f30c0f247004c308047b8f1000f18bfc8f80c4042f6f764c0f2440404f5807130468847384625b0bde8f08f2a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629b7d140b14af21f31c0f247010746884797f8641111b14ff0ff37d6e738464ff0000ba6e72de9f04f81b04ef66c6b4ff29c554ff6005848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0b2804ff4407004f019fe002800f0aa80414600bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b738Line truncated
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf80b5002a18bf10f0030301d186460ce000f1010c864600bf013a0ef8011b04d01cf003030cf1010cf6d1102a11d35ffa81fc4ff001330cfb03fc00bf103acef80cc0cef808c0cef804c04ef810cb0f2af4d8042a0bd35ffa81fc4ff001330cfb03fc00bf043a032a4ef804cbfad8002a08bf80bdaef10103013a03f8011ffbd180bd00bf2de9f04fc1b04ff62c484ef66c694ff29c5b1446cff6ff78ccf2ff09c2f2290b4ff6ff75002701260993cde90a0106e048f2a351c0f24501bff32f8f8847dbf800a0bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0a6fa002800f0078241464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001f51457ff436afbff35f8f4be8000121b15be8001f5145f8d02be70b99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847dbf80070bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0ba804ff4757007f0caf9002800f02b8141464218463182f8d44382f8d54382f8d64382f8d74382f8d84382f8d94382f8da4382f8db4382f8dc4382f8dd4382f8de4382f8df4382f8e04382f8e14382f8e24382f8e34382f8e44382f8e54382f8e64382f8e74382f8e84382f8e94382f8ea4382f8eb4382f8ec4382f8ed4382f8ee4382f8ef4382f8f04382f8f14382f8f24382f8f34382f8f44382f8f54382f8f64382f8f74382f8f84382f8f94382f8fa4382f8fb4382f8fc4382f8fd4382f8fe4382f8ff4382f8004482f8014482f8024482f8034482f8044482f8054482f8064482f8074482f8084482f8094482f80a4482f80b4482f80c4482f80d4482f80e4482f80f4482f8104482f8114482f8124482f8134482f8144482f8154482f8164482f8174482f8184482f819447ff470afc0f8009086728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb9427ff436afbff35f8f4be8000121b15be8001fb942f8d02be7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f09d810b98002861d00a980778a7f1170002285bd80b980c2840f086810a9e7078b178084300f08081f0780138022800f27b81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f01082172f40f02682002a40f06281fe2800f06d86812800f06086002840f0738651ea030040f0558100f0bdbe04460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0f58051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0e480824692e74ff0ff3737e147f6df40c0f24f008047002701240d97adf8387006e048f2a351c0f24501bff32f8f8847dbf80060bff35f8fdbf800000321cff680710140b1f1005fbff35f8f03d10168494500f0c1804ff4757007f070f8002800f0b98041464218463182f8d47382f8d57382f8d67382f8d77382f8d87382f8d97382f8da7382f8db7382f8dc7382f8dd7382f8de7382f8df7382f8e07382f8e17382f8e27382f8e37382f8e47382f8e57382f8e67382f8e77382f8e87382f8e97382f8ea7382f8eb7382f8ec7382f8ed7382f8ee7382f8ef7382f8f07382f8f17382f8f27382f8f37382f8f47382f8f57382f8f67382f8f77382f8f87382f8f97382f8fa7382f8fb7382f8fc7382f8fd7382f8fe7382f8ff7382f8007482f8017482f8027482f8037482f8047482f8057482f8067482f8077482f8087482f8097482f80a7482f80b7482f80c7482f80d7482f80e7482f80f7482f8107482f8117482f8127482f8137482f8147482f8157482f8167482f8177482f8187482f819747ff470afc0f8009084728583c583058445848584c584058545858585c585058645868586c586058745875be8001fb1427ff436afbff35f8f4be8000159b15be8001fb142f8d02be706f5a9718847ace6012110e700200a9999b10b9a8ab1097801f07f02b2f10b0618bf0126b2f11c0718bf01278a2924dc0b2926d01c2924d03fe009900ca804f097fa4ff0ff3701249df8390000280ca818bf4ff0ff3704f0d3fb054644b99df8380028b94af21f30c0f247004c3080470998002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010446884794f8641119b994f80413204669b14ff0ff3742f6f760c0f2440000f5807150468847384641b0bde8f08f09900ca806ea070404f055fa0a98017801f07f061e2e04d10b9908f076fb0746b5e71d2e01f07e0018bf1a2811d1dbf800100322cff680720a40b2f1005fbff35f8f0ad10a684a4504bf002281f8812303e0a6f115010129e9d90f2e0adc092e00f37381052e00f0ef82072e40f01c8300279ae0142e00f32182102e00f0eb82112e40f011830b98022841f0c6800a9908781Line truncated
"desc": "append injected blobs to main-app payload"
},
{
"offset": 779123,
"old": "805a3900",
"new": "f0173a00",
"new": "e06a3a00",
"desc": "main-app subheader payload size (ps)"
},
{
"offset": 152,
"old": "005b3900",
"new": "70183a00",
"new": "606b3a00",
"desc": "main-app TOC entry size (ps + 128)"
},
{
"offset": 779243,
"old": "805a3904",
"new": "f0173a04",
"new": "e06a3a04",
"desc": "main-app preamble length (low 24 bits)"
},
{
"offset": 779247,
"old": "effdbcea",
"new": "ed6be905",
"new": "38f29188",
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
},
{
"offset": 156,
"old": "c0c6322a",
"new": "18540264",
"new": "f8d63d83",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
},
{
"offset": 779127,
"old": "c0c6322a",
"new": "18540264",
"new": "f8d63d83",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
}
]
-18
View File
@@ -1,18 +0,0 @@
import { bytes, concat, integer, readBytes, readInteger, readString, type ProtocolMessage } from './ble-protocol'
const text = (field: number, value: string) => bytes(field, new Uint8Array(Array.from(value, ch => ch.charCodeAt(0))))
export function createFlashPrompt(magic: number, warning: string): Uint8Array {
const geometry = (y: number, height: number, id: number, name: string) => concat(
integer(1, 0), integer(2, y), integer(3, 280), integer(4, height), integer(9, id), text(10, name))
const list = concat(geometry(150, 120, 2, 'flashmenu'), bytes(11, concat(integer(1, 2), integer(3, 1), text(4, 'No, cancel'), text(4, 'Yes, flash'))), integer(12, 1))
const label = concat(geometry(0, 130, 1, 'flashwarn'), text(12, warning))
return concat(integer(1, 0), integer(2, magic), bytes(3, concat(integer(1, 2), bytes(2, list), bytes(3, label), integer(5, 10000))))
}
export function flashPromptSelection(message: ProtocolMessage): boolean | null {
if (message.sid !== 0xe0 || ![1, 6].includes(message.flag)) return null
const event = readBytes(message.payload, 13), list = event && readBytes(event, 1)
if (!list || readString(list, 2) !== 'flashmenu' || readInteger(list, 5, 0) !== 0) return null
const index = readInteger(list, 4, -1), name = readString(list, 3).toLowerCase()
if (index === 1 || name.startsWith('yes')) return true
if (index === 0 || name.startsWith('no')) return false
return null
}
+10 -1
View File
@@ -23,7 +23,7 @@ export const GLANCE_SURFACE_Z_ORDER = 900;
/** Surface operations needed by the board, shared by Android and iOS. */
export type GlanceDisplay = Pick<DisplayTarget,
"configureSurface" | "setSurfaceVisible" | "setScreenBlanked" | "submitSurfaceFrame">;
"configureSurface" | "setSurfaceVisible" | "setSurfaceDepth" | "setScreenBlanked" | "submitSurfaceFrame">;
export type GlanceHostOptions = {
getDisplay: () => GlanceDisplay | null;
@@ -52,6 +52,8 @@ export class GlanceHost {
private shown = false;
private hideTimer: ReturnType<typeof setTimeout> | null = null;
private configuredFor: GlanceDisplay | null = null;
/** Last depth given to the surface; null forces the next render to set it. */
private configuredDepth: number | null = null;
/** Serializes show/hide against each other and against in-flight renders. */
private queue: Promise<void> = Promise.resolve();
private rendering = false;
@@ -158,6 +160,7 @@ export class GlanceHost {
transparency: "opaque",
});
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
this.configuredDepth = null;
this.configuredFor = display;
}
@@ -256,6 +259,12 @@ export class GlanceHost {
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
// The glasses copy the screen shifted per lens while the board covers it.
const depth = this.options.getProvider()?.depth() ?? 0;
if (depth !== this.configuredDepth) {
await display.setSurfaceDepth(GLANCE_SURFACE_ID, depth);
this.configuredDepth = depth;
}
await frameTimings.spanAsync(frameId, "submit", () =>
display.submitSurfaceFrame(
GLANCE_SURFACE_ID,
-638
View File
@@ -1,638 +0,0 @@
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) 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 ? [10, 2, 100, 0, 0, 0] : [10, 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 {
// 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 (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) {
// Uploads participate in the same ordered, acknowledged command window as
// the image. Coalescing only happens before planning mutates residency.
commands = [...texturePlan.uploads, texturePlan.payload]
this.nextImageFrameId = texturePlan.nextFid
this.log(`Display textures (${texturePlan.uploads.length} uploads, cache=${texturePlan.usedBytes}B)`)
} else {
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed))
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands)
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
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, 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 textures 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([11]))
}
} 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) }
}
+408 -132
View File
@@ -17,10 +17,11 @@ import { IosNavigationSensors } from '../native/ios-navigation-sensors'
import { Utils } from '@nativescript/core'
import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
import { iosBluetooth } from '../native/ios-bluetooth'
import { iosVoiceInput } from '../native/ios-voice-input'
import { nightscoutBridge } from '../native/nightscout-bridge'
import { GlassesSession, type SessionState } from './glasses-session'
import { FaceclawCommunicatorBridge, resolveIosPeripherals, type CommunicatorState, type RawInputEvent } from '../native/faceclaw-communicator.ios'
import { PreviewDisplayTarget, type DisplayTarget } from '../native/preview-display.ios'
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
import { GlanceHost, type GlanceDisplay } from './glance-host'
import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from './lock-screen'
import { OsEventTypeList } from './events'
@@ -34,64 +35,90 @@ import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type
import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetting } from '../ui/dashboard-settings'
import { readPhoneBatteryState } from '../native/phone-battery'
import { iosAppUnavailableReason } from '../apps/ios-availability'
import { SurfaceCompositor } from '../graphics/surface-compositor'
import { flattenPlanesWithDraws, type Plane } from '../graphics/plane'
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from '../graphics/plane'
import { prepareFrameDraws } from '../graphics/glyph-wire'
import { IosTexturePlanner } from '../native/texture-planner.ios'
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image'
import { previewPixels } from '../native/ios-graphics'
import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/gestures'
import { noopLayerActions, type LayerActions } from '../ui/layers'
import { TextViewerLayer } from '../apps/files/text-viewer'
import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell'
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible } from '../ui/shell/geometry'
import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySettingChanged,
previewColorSetting, lockScreenEnabledSetting, brightnessSetting, brightnessSettingToLevel } from '../ui/dashboard-settings'
previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences } from '../ui/dashboard-settings'
import type { PhoneGesture } from '../phone-ui/phone-gestures'
import { isWelcomeSoundPending, setWelcomeSoundPending } from '../phone-ui/onboarding-state'
import { findSoundEffect, playSoundEffect } from '../ui/sound-effects'
/** iOS host for the shared app registry, shell, compositor and BLE session. */
const SHELL_SURFACE_ID = 'shell'
const SHELL_SURFACE_Z_ORDER = 1
const LOCK_SURFACE_Z_ORDER = 1000
/** The shell render loop waits this long for the glasses to take a frame before moving on. */
const FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS = 2000
export type SessionPhase = 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error'
export type SessionState = { phase: SessionPhase; status: string; battery: number | null; charging: boolean | null;
ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
function ancsCapable(capabilities: string): boolean {
return /^Faceclaw\/(\d+)/.test(capabilities) && Number(capabilities.split('/')[1]) >= ANCS_FIRMWARE_VERSION
}
/**
* iOS host for the shared app registry, shell and BLE session. The session,
* compositor and transport live in shared Kotlin (FaceclawCommunicatorBridge
* over FaceclawKitIosGlassesSession); with no glasses connected the same
* compositor runs headless (PreviewDisplayTarget) so the phone mirror works.
*/
export class IosPreviewController {
private readonly compositor = new SurfaceCompositor(G2_LENS_WIDTH, G2_LENS_HEIGHT)
private previewTarget: PreviewDisplayTarget | null = null
private communicator: FaceclawCommunicatorBridge | null = null
private readonly startedCommunicators = new WeakSet<FaceclawCommunicatorBridge>()
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, charging: null, ring: false,
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
private notifications: AncsClient | null = null
private readonly sessionOffs: (() => void)[] = []
private stopping: Promise<void> | null = null
/** Resolves once the current display target has every surface configured. */
private displayReady: Promise<void> = Promise.resolve()
private connecting = false
private sessionCreated = false
private wearDetectionRequested = false
private readonly glanceDisplay: GlanceDisplay = {
configureSurface: async (id, options) => {
this.compositor.configureSurface(id, options)
},
setSurfaceVisible: async (id, visible) => {
this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame()
},
setScreenBlanked: async blanked => {
this.compositor.setScreenBlanked(blanked); this.scheduleFrame()
},
submitSurfaceFrame: async (id, pixels, rect, _fingerprint, _paintMs, _frameId, draws) => {
this.compositor.submitSurfaceFrame(id, pixels, rect, draws); this.scheduleFrame()
configureSurface: async (id, options) => { await this.display?.configureSurface(id, options) },
setSurfaceVisible: async (id, visible) => { await this.display?.setSurfaceVisible(id, visible); this.schedulePreviewUpdate() },
setSurfaceDepth: async (id, depth) => { await this.display?.setSurfaceDepth(id, depth) },
setScreenBlanked: async blanked => { await this.display?.setScreenBlanked(blanked); this.schedulePreviewUpdate() },
submitSurfaceFrame: async (id, pixels, rect, fingerprint, paintMs, frameId, draws) => {
await this.display?.submitSurfaceFrame(id, pixels, rect, fingerprint, paintMs, frameId, draws); this.schedulePreviewUpdate()
},
}
private readonly glance = new GlanceHost({
getDisplay: () => this.glanceDisplay,
getDisplay: () => this.display ? this.glanceDisplay : null,
getProvider: () => ALL_APPS.find(app => app.glanceboard)?.glanceboard ?? null,
canShow: () => this.runtimeNeeded && !this.glassesLocked,
// iOS retains the EvenHub session while the shell sleeps. The board's
// opaque first frame is ready before we unblank the compositor.
ensureSessionActive: async () => {
this.compositor.setScreenBlanked(false); this.scheduleFrame(); return true
await this.display?.setScreenBlanked(false); this.schedulePreviewUpdate(); return true
},
onHiddenWhileAsleep: () => {
if (!shell.isScreenOn()) this.compositor.setScreenBlanked(true)
this.scheduleFrame()
if (!shell.isScreenOn()) void this.display?.setScreenBlanked(true).then(() => this.schedulePreviewUpdate())
else this.schedulePreviewUpdate()
},
onVisibilityChanged: () => this.scheduleFrame(),
onVisibilityChanged: () => this.schedulePreviewUpdate(),
appendLog: message => this.logBluetooth(message),
})
private renderTimer: ReturnType<typeof setTimeout> | null = null
private previewTimer: ReturnType<typeof setTimeout> | null = null
private clockTimer: ReturnType<typeof setInterval> | null = null
private offSettings: (() => void) | null = null
private lastBrightness: string | null = null
private active = false
private runtimeRunning = false
private clockMinute = -1
private shellDirty = true
private shellRenderInProgress = false
private shellRenderQueued = false
private nextFrameId = 1
private nextWorkerFrame = 0
private inputQueue: Promise<void> = Promise.resolve()
private lastLayout = ''
private readonly appHosts = new Map<string, WorkerAppHost>()
@@ -102,13 +129,12 @@ export class IosPreviewController {
private glassesWorn: boolean | null = null
private glassesLocked = false
private lockEnabled = lockScreenEnabledSetting.get()
private session: GlassesSession | null = null
private acknowledgedFrames = 0
private logLines: string[] = []
private logTimer: ReturnType<typeof setTimeout> | null = null
private readonly actions: LayerActions = {
...noopLayerActions,
playBuzzerSequence: payload => this.session?.playBuzzerSequence(payload),
playBuzzerSequence: payload => this.communicator?.playBuzzerSequence(payload),
requestRender: () => this.requestShellRender(),
disconnect: () => this.disconnect(),
startVoiceCapture: endpointing => this.startVoiceCapture(endpointing),
@@ -130,11 +156,7 @@ export class IosPreviewController {
private readonly onError: (message: string) => void,
private readonly onConnectionState: (state: SessionState) => void = () => {},
private readonly onKeyboardInputChanged: (session: KeyboardInputSession | null) => void = () => {}) {
this.compositor.configureSurface('shell', { x: 0, y: 0, width: 640, height: 480, zOrder: 1, transparency: 'color-key' })
this.compositor.configureSurface(LOCK_SCREEN_SURFACE_ID, {
x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: 1000, transparency: 'opaque',
})
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, false)
this.ensurePreviewDisplay()
shell.configure({
actions: this.actions,
voiceInputEnabled: true,
@@ -151,7 +173,8 @@ export class IosPreviewController {
onWindowsChanged: () => this.requestShellRender(),
onScreenStateChanged: on => {
if (on) this.glance.dismiss()
this.compositor.setScreenBlanked(!on); this.requestShellRender()
void this.display?.setScreenBlanked(!on).catch(error => this.fail(error))
this.requestShellRender()
},
})
const launcher = createLauncherWindow({
@@ -160,9 +183,9 @@ export class IosPreviewController {
launchApp: id => this.launchApp(id),
uninstallApp: id => this.uninstallApp(id),
submitFrame: planes => this.submit(LAUNCHER_SURFACE_ID, planes),
setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(LAUNCHER_SURFACE_ID, visible); this.scheduleFrame() },
setSurfaceVisible: visible => this.setWindowSurfaceVisible(LAUNCHER_SURFACE_ID, visible),
})
this.configureWindow(launcher)
void this.configureWindow(launcher)
shell.registerWindow(launcher)
shell.wake('window')
shell.focusWindow(launcher.windowId)
@@ -198,14 +221,54 @@ export class IosPreviewController {
for (const app of ALL_APPS) if (app.appId !== 'launcher' && !iosAppUnavailableReason(app.appId)) app.boot?.(this.buildAppContext(app))
}
/** The compositor frames go to: the live session, else the headless preview. */
private get display(): DisplayTarget | null { return this.communicator ?? this.previewTarget }
private ensurePreviewDisplay(): void {
if (this.communicator || this.previewTarget) return
const target = new PreviewDisplayTarget()
this.previewTarget = target
target.activate(() => this.schedulePreviewUpdate())
this.displayReady = this.registerSurfaces(target).catch(error => this.fail(error))
}
private teardownPreviewDisplay(): void {
const target = this.previewTarget
if (!target) return
this.previewTarget = null
this.glance.reset()
target.release()
}
/** (Re)create every compositor surface on a fresh display target. */
private async registerSurfaces(target: DisplayTarget): Promise<void> {
await target.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT)
await target.configureSurface(SHELL_SURFACE_ID, { x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT,
zOrder: SHELL_SURFACE_Z_ORDER, transparency: 'color-key' })
await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1)
await target.configureSurface(LOCK_SCREEN_SURFACE_ID, {
x: 0, y: 0, width: G2_LENS_WIDTH, height: G2_LENS_HEIGHT, zOrder: LOCK_SURFACE_Z_ORDER, transparency: 'opaque',
})
if (this.glassesLocked) await this.submitLockImage(target)
await target.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, this.glassesLocked)
await target.setScreenBlanked(!shell.isScreenOn())
this.lastLayout = ''
for (const window of shell.getWindows()) await this.configureWindow(window, target)
}
private async submitLockImage(target: DisplayTarget): Promise<void> {
const image = createLockScreenImage()
await target.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(),
{ x: 0, y: 0, width: image.width, height: image.height }, 'lock-screen', 0, 0)
}
resume(): void {
if (this.active) return
this.active = true
this.syncRuntime()
this.handlePhoneLockState(!UIApplication.sharedApplication.protectedDataAvailable)
this.logBluetooth('Phone foreground')
if (this.session) this.onConnectionState({ ...this.session.state })
this.session?.wake()
if (this.communicator) this.onConnectionState({ ...this.state })
this.relayout()
shell.foregroundWindow()?.requestRender()
this.requestShellRender()
@@ -216,12 +279,11 @@ export class IosPreviewController {
iosVoiceInput.stopPhoneCapture()
// The phone preview is hidden; the glasses still own their screen and input.
this.syncRuntime()
this.logBluetooth(`Phone background; glasses ${this.session?.state.phase ?? 'disconnected'}`)
this.logBluetooth(`Phone background; glasses ${this.state.phase}`)
this.flushBluetoothLog()
this.session?.wake()
}
private get runtimeNeeded(): boolean {
return this.active || !!this.session && ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.session.state.phase)
return this.active || ['connected', 'connecting', 'retrying', 'disconnecting'].includes(this.state.phase)
}
private syncRuntime(): void {
const running = this.runtimeNeeded
@@ -231,14 +293,14 @@ export class IosPreviewController {
if (!running) {
void nightscoutBridge.stop().catch(error => this.fail(error))
this.glance.dismiss()
this.compositor.setScreenBlanked(!shell.isScreenOn())
void this.display?.setScreenBlanked(!shell.isScreenOn()).catch(error => this.fail(error))
this.offSettings?.(); this.offSettings = null
for (const observer of this.batteryObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
for (const observer of this.lockObservers.splice(0)) NSNotificationCenter.defaultCenter.removeObserver(observer)
UIDevice.currentDevice.batteryMonitoringEnabled = false
if (this.clockTimer !== null) clearInterval(this.clockTimer)
if (this.renderTimer !== null) clearTimeout(this.renderTimer)
this.clockTimer = this.renderTimer = null
if (this.previewTimer !== null) clearTimeout(this.previewTimer)
this.clockTimer = this.previewTimer = null
return
}
UIDevice.currentDevice.batteryMonitoringEnabled = true
@@ -268,12 +330,13 @@ export class IosPreviewController {
this.clockTimer = setInterval(() => this.refreshClock(), 60_000)
}
private pushBrightness(): void {
const session = this.session
if (session?.state.phase !== 'connected') { this.lastBrightness = null; return }
const value = brightnessSetting.get()
const communicator = this.communicator
if (!communicator || this.state.phase !== 'connected') { this.lastBrightness = null; return }
const preferences = getBrightnessPreferences()
const value = JSON.stringify(preferences)
if (value === this.lastBrightness) return
this.lastBrightness = value
void session.setBrightness(brightnessSettingToLevel(value)).catch(error => {
void communicator.configureBrightness(preferences).catch(error => {
this.lastBrightness = null
this.logBluetooth(`Brightness: ${String(error)}`)
})
@@ -285,12 +348,13 @@ export class IosPreviewController {
if (!enabled) this.setGlassesLocked(false)
else {
if (this.phoneLocked && this.glassesWorn === false) this.setGlassesLocked(true)
if (this.session?.state.phase === 'connected')
void this.session.enableWearDetectionAndRequestState().catch(error => this.fail(error))
if (this.communicator && this.state.phase === 'connected')
void this.communicator.enableWearDetectionAndRequestState().catch(error => this.fail(error))
}
}
private handlePhoneLockState(locked: boolean): void {
this.phoneLocked = locked
this.communicator?.onPhoneLockSignal()
if (!locked) this.setGlassesLocked(false)
else if (this.lockEnabled && this.glassesWorn === false) this.setGlassesLocked(true)
}
@@ -304,17 +368,21 @@ export class IosPreviewController {
}
private setGlassesLocked(locked: boolean): void {
if (locked === this.glassesLocked) return
if (locked) {
const image = createLockScreenImage()
this.compositor.submitSurfaceFrame(LOCK_SCREEN_SURFACE_ID, image.to8bppBuffer(),
{ x: 0, y: 0, width: image.width, height: image.height })
}
this.glassesLocked = locked
if (locked) {
this.glance.dismiss()
iosVoiceInput.handleSessionEnded('Glasses locked. Unlock your phone to start voice input again.')
}
this.compositor.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked)
void (async () => {
await this.displayReady
const display = this.display
if (!display) return
{
if (locked) await this.submitLockImage(display)
await display.setSurfaceVisible(LOCK_SCREEN_SURFACE_ID, locked)
this.schedulePreviewUpdate()
}
})().catch(error => this.fail(error))
this.logBluetooth(`Glasses ${locked ? 'locked' : 'unlocked'}`)
this.requestShellRender()
}
@@ -322,47 +390,90 @@ export class IosPreviewController {
const minute = Math.floor(Date.now() / 60_000)
if (minute !== this.clockMinute) { this.clockMinute = minute; this.requestShellRender() }
}
private configureWindow(window: ShellWindow): void {
this.compositor.configureSurface(window.surfaceId, {
private async configureWindow(window: ShellWindow, target: DisplayTarget | null = this.display): Promise<void> {
if (!target) return
await target.configureSurface(window.surfaceId, {
...appViewportRect(window.heightMode, window.appId), zOrder: 0, transparency: 'opaque',
})
this.compositor.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId)
await target.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId)
}
private relayout(): void {
const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";")
if (layout === this.lastLayout) return
this.lastLayout = layout
for (const window of shell.getWindows()) {
this.configureWindow(window)
void this.configureWindow(window).catch(error => this.fail(error))
window.relayout?.()
}
}
/** Submit a painted frame for an in-process window (e.g. the launcher). */
private async submit(id: string, planes: Plane[]): Promise<void> {
await this.displayReady
const display = this.display
if (!display) return
const fingerprint = planesFingerprint(planes)
const { image, draws } = flattenPlanesWithDraws(planes)
this.compositor.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, prepareFrameDraws(draws))
this.scheduleFrame()
await display.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height },
fingerprint, -1, 0, prepareFrameDraws(draws))
this.schedulePreviewUpdate()
}
private requestShellRender(): void { this.shellDirty = true; this.scheduleFrame() }
private scheduleFrame(): void {
if (!this.runtimeNeeded || this.renderTimer !== null) return
// Coalesce window/chrome updates into one glasses frame, capped at 30 fps.
this.renderTimer = setTimeout(() => {
this.renderTimer = null
if (!this.runtimeNeeded) return
/** Worker windows ship baked pixels; every submit is new content to the compositor. */
private async submitWorkerPixels(id: string, pixels: Uint8Array, width: number, height: number, draws: ArrayBuffer | null): Promise<void> {
await this.displayReady
const display = this.display
if (!display) return
await display.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, `${id}#${++this.nextWorkerFrame}`, -1, 0, draws)
this.schedulePreviewUpdate()
}
private setWindowSurfaceVisible(id: string, visible: boolean): void {
void this.display?.setSurfaceVisible(id, visible).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error))
}
private removeWindowSurface(id: string): void {
void this.display?.removeSurface(id).then(() => this.schedulePreviewUpdate()).catch(error => this.fail(error))
}
/** Re-render and resubmit the shell surface; one render in flight, at most one queued. */
private requestShellRender(): void {
if (this.shellRenderInProgress) { this.shellRenderQueued = true; return }
this.shellRenderInProgress = true
void (async () => {
try {
if (this.shellDirty) {
this.shellDirty = false
const { image, draws } = flattenPlanesWithDraws(shell.paintSurface(), { width: 640, height: 480 })
this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 }, prepareFrameDraws(draws))
this.compositor.setUnderlayDim(1, shell.underlayDim())
}
const { pixels, textures } = this.compositor.compositeFrame()
this.session?.setFrame(pixels, textures)
if (this.active) {
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
}
do {
this.shellRenderQueued = false
await this.renderShell()
} while (this.shellRenderQueued)
} catch (error) { this.fail(error) }
finally { this.shellRenderInProgress = false }
})()
}
private async renderShell(): Promise<void> {
await this.displayReady
const display = this.display
if (!display) return
const startedAt = Date.now()
const bytes = shell.paintScene()
const frameId = this.allocateFrameId()
await display.submitShellScene(bytes, Date.now() - startedAt, frameId)
// Backpressure: the next shell render waits for this one to reach the
// glasses (the preview target resolves immediately; nothing transmits).
await display.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS)
this.schedulePreviewUpdate()
}
private allocateFrameId(): number {
const id = this.nextFrameId
this.nextFrameId = this.nextFrameId >= 0x7fffffff ? 1 : this.nextFrameId + 1
return id
}
/** Refresh the phone mirror from the composited screen, coalesced at 30 fps. */
private schedulePreviewUpdate(): void {
if (!this.active || this.previewTimer !== null) return
this.previewTimer = setTimeout(() => {
this.previewTimer = null
if (!this.active) return
try {
const image = this.display?.getCompositePreview(previewColorSetting.get() === 'green')
if (!image) return
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
: `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
} catch (error) { this.fail(error) }
}, 33)
}
@@ -471,7 +582,7 @@ export class IosPreviewController {
launchInProcessApp: (id, surface, create) => this.launchInProcessApp(id, surface, create),
ensureWorkerHost: create => this.ensureWorkerHost(app.appId, create),
submitWindowFrame: (id, planes) => this.submit(id, planes),
setWindowSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() },
setWindowSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
requestShellRender: () => this.requestShellRender(), appendLog: message => console.log(`[ios-app] ${message}`),
setTextEditorHost: () => {},
}
@@ -486,13 +597,17 @@ export class IosPreviewController {
let surfaceReady = false
const app = create({
actions: this.actions, submitFrame: planes => surfaceReady ? this.submit(surfaceId, planes) : Promise.resolve(),
setSurfaceVisible: visible => { this.compositor.setSurfaceVisible(surfaceId, visible); this.scheduleFrame() },
removeSurface: () => { surfaceReady = false; this.compositor.removeSurface(surfaceId); this.scheduleFrame() },
reconfigureSurface: () => { const window = shell.getWindows().find(w => w.windowId === windowId); if (window) this.configureWindow(window); this.requestShellRender() },
setSurfaceVisible: visible => this.setWindowSurfaceVisible(surfaceId, visible),
removeSurface: () => { surfaceReady = false; this.removeWindowSurface(surfaceId) },
reconfigureSurface: () => {
const window = shell.getWindows().find(w => w.windowId === windowId)
if (window) void this.configureWindow(window).catch(error => this.fail(error))
this.requestShellRender()
},
onClosed: () => { this.inProcessApps.delete(windowId) },
})
this.inProcessApps.set(windowId, app)
this.configureWindow(app.window)
await this.configureWindow(app.window)
surfaceReady = true
shell.registerWindow(app.window); shell.focusWindow(windowId)
app.requestRender(); this.requestShellRender()
@@ -505,16 +620,19 @@ export class IosPreviewController {
? new IosNavigationSensors(event => worker.postMessage({ type: 'navigation-sensors', event })) : undefined
const host = new WorkerAppHost({
appId, worker, navigationSensors,
onStopping: () => { if (this.appHosts.get(appId) === host) this.appHosts.delete(appId) },
playBuzzerSequence: payload => this.actions.playBuzzerSequence(payload),
openUrl: url => { void Utils.openUrl(url) },
configureSurface: async (id, visible, heightMode) => {
this.compositor.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
this.compositor.setSurfaceVisible(id, visible)
const display = this.display
if (!display) return
await display.configureSurface(id, { ...appViewportRect(heightMode, appId), zOrder: 0, transparency: 'opaque' })
await display.setSurfaceVisible(id, visible)
},
setSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() },
removeSurface: id => { this.compositor.removeSurface(id); this.scheduleFrame() },
setSurfaceVisible: (id, visible) => this.setWindowSurfaceVisible(id, visible),
removeSurface: id => this.removeWindowSurface(id),
submitPixels: (id, pixels, width, height, draws) => {
this.compositor.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, draws); this.scheduleFrame()
void this.submitWorkerPixels(id, pixels, width, height, draws).catch(error => this.fail(error))
},
requestShellRender: () => this.requestShellRender(),
openSettings: section => { void this.launchApp('settings', { section }) },
@@ -530,45 +648,194 @@ export class IosPreviewController {
displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length])
}
get displayModeLabel(): string { return displayModeLabel(displayModeSetting.get()) }
get connectionState(): SessionState | null { return this.session?.state ?? null }
get connectionState(): SessionState | null { return this.sessionCreated ? this.state : null }
get connectionDetails(): string {
const state = this.connectionState
return state ? `${state.status}\nL: ${state.leftVersion || 'unknown'}\nR: ${state.rightVersion || 'unknown'}\nBattery: ${state.battery ?? '?'}%\nRing: ${state.ring ? 'connected' : 'disconnected'}\nFrames acknowledged: ${state.frames}\n${state.capabilities}\n\n${this.logLines.slice(-12).join('\n')}` : 'Preview only. Configure devices, then connect.'
}
private update(phase: SessionPhase, status: string): void {
this.state = { ...this.state, phase, status }
this.logBluetooth(status)
this.emitState()
}
private emitState(): void {
if (this.state.phase !== 'connected') resetRingInputFilter()
this.pushBrightness()
this.maybePlayWelcomeSound(this.state)
if (this.state.phase !== 'connected' && this.state.phase !== 'connecting') this.glassesWorn = null
if (this.state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging })
this.syncRuntime()
if (this.active) this.onConnectionState({ ...this.state })
this.requestShellRender()
}
async connect(): Promise<void> {
if (!this.active) return
if (!this.active || this.connecting || this.stopping) return
if (this.communicator && ['connecting', 'connected', 'retrying', 'disconnecting'].includes(this.state.phase)) return
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
if (error) { this.onError(error); return }
if (!this.session) {
this.session = new GlassesSession(iosBluetooth(), state => {
if (state.phase !== "connected") resetRingInputFilter()
this.pushBrightness()
this.maybePlayWelcomeSound(state)
if (state.phase !== 'connected' && state.phase !== 'connecting') this.glassesWorn = null
if (state.phase !== 'connected') iosVoiceInput.handleSessionEnded()
shell.setBatteryLevels({ headset: state.battery, headsetCharging: state.charging })
this.syncRuntime()
if (this.active) this.onConnectionState(state)
this.requestShellRender()
}, input => {
this.inputQueue = this.inputQueue.then(async () => {
if (this.session?.state.phase !== 'connected') return
await this.receiveInput(rawInputEventToInputEvent(input),
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
}).catch(error => this.fail(error))
}, message => this.logBluetooth(message), () => {
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
this.refreshClock()
}, receiveCompassEvent, wearing => this.handleWearState(wearing), (key, popup) => {
iosNotificationsChanged(key, popup); this.requestShellRender()
}, new IosTexturePlanner())
bindIosNotifications(this.session.notifications)
bindCompassSession(this.session)
this.connecting = true
this.sessionCreated = true
try {
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
this.update('connecting', 'Finding configured devices…')
let identifiers
try { identifiers = await resolveIosPeripherals(addresses) }
catch (failure) {
this.update('error', failure instanceof Error ? failure.message : String(failure))
return
}
if (this.communicator) await this.closeCommunicator()
const communicator = new FaceclawCommunicatorBridge(identifiers)
this.teardownPreviewDisplay()
this.communicator = communicator
this.wearDetectionRequested = false
this.bindCommunicator(communicator)
this.displayReady = this.registerSurfaces(communicator)
await this.displayReady
this.update('connecting', 'Connecting to the glasses…')
await communicator.configureBrightness(getBrightnessPreferences())
this.startedCommunicators.add(communicator)
await communicator.start()
for (const window of shell.getWindows()) window.requestRender()
this.requestShellRender()
} catch (failure) {
this.fail(failure)
await this.closeCommunicator().catch(() => {})
this.ensurePreviewDisplay()
this.update('error', failure instanceof Error ? failure.message : String(failure))
} finally {
this.connecting = false
}
await this.session.start(addresses)
}
async disconnect(): Promise<void> { await this.session?.stop() }
private bindCommunicator(communicator: FaceclawCommunicatorBridge): void {
const notifications = new AncsClient(packet => communicator.writeRawToRight(packet), (key, popup) => {
iosNotificationsChanged(key, popup); this.requestShellRender()
}, message => this.logBluetooth('ANCS: ' + message))
this.notifications = notifications
bindIosNotifications(notifications)
bindCompassSession(communicator)
communicator.setRequiresAncs(true)
this.sessionOffs.push(
communicator.onStateChange(state => this.handleSessionState(communicator, state)),
communicator.onRingEvent(input => this.handleRingEvent(input)),
communicator.onBatteryState(state => {
if (this.communicator !== communicator) return
this.state = { ...this.state, battery: state.battery >= 0 && state.battery <= 100 ? state.battery : this.state.battery,
charging: state.chargingStatus >= 0 ? state.chargingStatus > 0 : this.state.charging,
ring: (state.ringBattery ?? -1) >= 0 || this.state.ring }
shell.setBatteryLevels({ headset: this.state.battery, headsetCharging: this.state.charging })
if (this.active) this.onConnectionState({ ...this.state })
this.requestShellRender()
}),
communicator.onFirmwareInfo(info => {
if (this.communicator !== communicator) return
this.state = { ...this.state, leftVersion: info.leftVersion || this.state.leftVersion,
rightVersion: info.rightVersion || this.state.rightVersion, capabilities: info.extension || this.state.capabilities }
if (this.active) this.onConnectionState({ ...this.state })
this.syncNotifications(communicator)
}),
communicator.onFrameMetrics(() => {
if (this.communicator !== communicator) return
this.state = { ...this.state, frames: this.state.frames + 1 }
this.onActivity()
this.maybePlayWelcomeSound(this.state)
if (this.active) this.onConnectionState({ ...this.state })
this.schedulePreviewUpdate()
}),
communicator.onWearState(wearing => { if (this.communicator === communicator) this.handleWearState(wearing) }),
communicator.addCompassListener(receiveCompassEvent),
communicator.onAncsRelayFrame(frame => { if (this.communicator === communicator) notifications.receive(frame) }),
communicator.onAncsAuthorization(authorized => {
if (this.communicator !== communicator) return
if (authorized) { this.syncNotifications(communicator); return }
const active = notifications.state !== 'disconnected'
const command = notifications.stopCommand()
notifications.stop('Enable Share System Notifications in iPhone Settings → Bluetooth → right lens.')
if (active && this.state.phase === 'connected') void communicator.writeRawToRight(command).catch(() => {})
}),
)
}
private handleSessionState(communicator: FaceclawCommunicatorBridge, state: CommunicatorState): void {
if (this.communicator !== communicator) return
// Registering the listener makes the Kotlin session report its current state, which is
// "disconnected" before start(); that must not be mistaken for the session ending.
if (!this.startedCommunicators.has(communicator)) return
const phase: SessionPhase = state.phase === 'charging' ? 'connected' : state.phase === 'unpaired' ? 'error' : state.phase
if (phase === 'connected' && this.state.phase !== 'connected') {
this.state = { ...this.state, frames: this.state.frames }
if (!this.wearDetectionRequested) {
this.wearDetectionRequested = true
void communicator.enableWearDetectionAndRequestState().catch(error => this.logBluetooth(`Wear detection: ${String(error)}`))
}
}
this.update(phase, state.status)
if (phase === 'connected') this.syncNotifications(communicator)
else if ((phase === 'disconnected' || phase === 'error') && !this.stopping) {
// The session ended on its own (retries exhausted, an arm unpaired): release it and go headless.
void this.disconnect(state.status)
}
}
private handleRingEvent(input: RawInputEvent): void {
this.inputQueue = this.inputQueue.then(async () => {
if (this.state.phase !== 'connected') return
this.onActivity()
if (input.ringInput) this.state = { ...this.state, ring: true }
await this.receiveInput(rawInputEventToInputEvent(input),
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
}).catch(error => this.fail(error))
}
/** Any glasses traffic: CoreBluetooth can wake a suspended process, so refresh what timers would have. */
private onActivity(): void {
if (!UIApplication.sharedApplication.protectedDataAvailable) this.handlePhoneLockState(true)
this.refreshClock()
}
/** Start iPhone notification relay once the firmware is known to support it and ANCS is authorized. */
private syncNotifications(communicator: FaceclawCommunicatorBridge): void {
const notifications = this.notifications
if (!notifications || this.state.phase !== 'connected') return
if (!ancsCapable(this.state.capabilities)) {
if (this.state.capabilities) notifications.stop(`Update glasses to Faceclaw firmware ${ANCS_FIRMWARE_VERSION} or newer for iPhone notifications.`)
return
}
if (notifications.state !== 'disconnected') return
notifications.start((Date.now() ^ Math.floor(Math.random() * 0xffffffff)) >>> 0, communicator.rightWriteLimit())
}
async disconnect(reason?: string): Promise<void> {
if (this.stopping) return this.stopping
const communicator = this.communicator
if (!communicator) return
// Marked before any bridge call: the session may report "disconnected" from inside disconnect().
let finished!: () => void
this.stopping = new Promise<void>(resolve => { finished = resolve })
try {
const notifications = this.notifications
if (notifications && notifications.state !== 'disconnected') {
const command = notifications.stopCommand()
notifications.stop()
if (this.state.phase === 'connected') { try { await communicator.writeRawToRight(command) } catch {} }
}
if (this.state.phase === 'connected' || this.state.phase === 'connecting' || this.state.phase === 'retrying')
this.update('disconnecting', 'Disconnecting…')
await communicator.disconnect().catch(error => this.logBluetooth(`Disconnect: ${String(error)}`))
} finally {
await this.closeCommunicator()
this.ensurePreviewDisplay()
this.update(reason ? 'error' : 'disconnected', reason ?? 'Preview only')
this.stopping = null
finished()
}
}
private async closeCommunicator(): Promise<void> {
const communicator = this.communicator
if (!communicator) return
for (const off of this.sessionOffs.splice(0)) off()
this.notifications = null
this.communicator = null
this.glance.reset()
await communicator.close().catch(error => this.logBluetooth(`Close: ${String(error)}`))
}
private maybePlayWelcomeSound(state: SessionState): void {
const firstNewFrame = state.frames > this.acknowledgedFrames
this.acknowledgedFrames = state.frames
@@ -583,17 +850,18 @@ export class IosPreviewController {
startVoiceInput(): void { if (!this.glassesLocked) shell.startVoiceInput() }
private async prepareVoiceCapture(): Promise<boolean> {
if (this.glassesLocked) return false
const usePhoneMic = this.session?.state.phase !== 'connected'
const usePhoneMic = this.state.phase !== 'connected'
if (usePhoneMic && !this.active) return false
const ready = await iosVoiceInput.prepare(this.active, usePhoneMic)
if (!ready && this.active) this.onError(iosVoiceInput.statusText)
return ready && !this.glassesLocked && (this.session?.state.phase === 'connected' || this.active)
return ready && !this.glassesLocked && (this.state.phase === 'connected' || this.active)
}
private async startVoiceCapture(endpointing = false): Promise<void> {
if (this.glassesLocked) return
const log = (message: string) => this.logBluetooth(message)
if (this.session?.state.phase === 'connected') {
await iosVoiceInput.startGlassesCapture(this.session, log, endpointing)
const communicator = this.communicator
if (communicator && this.state.phase === 'connected') {
await iosVoiceInput.startGlassesCapture(communicator, log, endpointing)
} else if (this.active) {
// Recheck microphone permission if the glasses disconnected after prepare.
if (!await iosVoiceInput.prepare(this.active, true) || !this.active || this.glassesLocked) return
@@ -620,13 +888,21 @@ export class IosPreviewController {
if (this.glassesLocked || !this.active) return
shell.startKeyboardInput()
}
private async editSetting(setting: { editorTitle: string; inputKind?: string; get(): string; set(value: string): void }): Promise<boolean> {
private async editSetting(setting: { editorTitle: string; inputKind?: string; get(): string; set(value: string): void; validationError?(value?: string): string | null }): Promise<boolean> {
if (!this.active) { this.logBluetooth('Editing text settings requires opening Faceclaw on the phone'); return false }
const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: setting.get(), inputType: setting.inputKind, okButtonText: 'Save', cancelButtonText: 'Cancel' })
if (result.result) {
setting.set(result.text)
let draft = setting.get()
while (true) {
const result = await Dialogs.prompt({ title: setting.editorTitle, defaultText: draft, inputType: setting.inputKind, okButtonText: 'Save', cancelButtonText: 'Cancel' })
if (!result.result) return false
draft = result.text
const error = setting.validationError?.(draft)
if (error) {
await Dialogs.alert({ title: setting.editorTitle, message: error, okButtonText: 'OK' })
continue
}
setting.set(draft)
if (setting === nightscoutSiteUrlSetting || setting === nightscoutApiTokenSetting) await nightscoutBridge.refreshNow()
return true
}
return result.result
}
}
-152
View File
@@ -1,152 +0,0 @@
import * as p from './ble-protocol'
import type { SessionTransport, SessionAddresses, TransportEvent } from './glasses-session'
import { hexToBytes } from '../util/hex-util'
declare function setTimeout(callback: () => void, ms: number): number
declare function clearTimeout(id: number): void
export const OTA_WRITE = '00002760-08c2-11e1-9073-0e8ac72e0001'
export const OTA_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e0002'
export class StockTimeout extends Error {}
type Waiter = { role: string; characteristic: string; match: (message: p.ProtocolMessage) => boolean;
resolve: (message: p.ProtocolMessage) => void; reject: (error: Error) => void; timer: ReturnType<typeof setTimeout> | null }
/** Exclusive, short-lived stock-protocol connection for onboarding/OTA. No display pump. */
export class StockConnection {
ids: Record<string, string> = {}
private limits: Record<string, number> = {}
private receiver = new p.MessageReceiver()
private waiters = new Set<Waiter>()
private listeners = new Set<(role: string, message: p.ProtocolMessage) => void>()
private lost = new Set<string>()
private writes = new Map<string, Promise<void>>()
private closed = false
private magic = 0x60
private sequence = 0
private off: () => void
constructor(private transport: SessionTransport, private log: (line: string) => void = () => {}) {
this.off = transport.onEvent(event => this.receive(event))
}
check(): void { if (this.closed) throw new Error('Cancelled.') }
isConnected(role: string): boolean { return !this.closed && !!this.limits[role] && !this.lost.has(role) }
async resolve(addresses: SessionAddresses): Promise<void> {
this.ids = await this.transport.resolveDevices(addresses); this.check()
}
async connect(role: string, ota = false): Promise<void> {
this.check(); this.lost.delete(role)
const id = this.ids[role]
if (!id) throw new Error(`No ${role} lens configured.`)
const details = await this.transport.connect(id)
if (this.closed) { this.transport.disconnect(id); this.check() }
this.limits[role] = details.maxWrite
for (const characteristic of [p.G2_WRITE, p.G2_NOTIFY, ...(ota ? [OTA_WRITE, OTA_NOTIFY] : [])])
if (!details.characteristics.includes(characteristic)) throw new Error(`${role} lens is missing ${ota ? 'firmware update' : 'G2'} characteristics.`)
await this.transport.subscribe(id, p.G2_NOTIFY); this.check()
if (ota) { await this.transport.subscribe(id, OTA_NOTIFY); this.check() }
}
nextMagic(): number { this.magic = this.magic >= 0x7f ? 0x60 : this.magic + 1; return this.magic }
async authenticate(role: string): Promise<void> {
for (let attempt = 0; attempt < 2; attempt++) {
const magic = this.nextMagic()
try {
await this.exchange(role, p.SID.auth, p.authentication(magic),
message => p.authenticationSucceeded(message, magic), 30_000, p.G2_WRITE, p.G2_NOTIFY, 0)
this.log(`Security authentication complete: ${role} lens`); return
} catch (error) { this.check(); if (!(error instanceof StockTimeout) || attempt === 1) throw error }
}
}
request(role: string, sid: number, build: (magic: number) => Uint8Array, timeout = 3500, fixedMagic?: number): Promise<p.ProtocolMessage> {
const magic = fixedMagic ?? this.nextMagic()
return this.exchange(role, sid, build(magic), message => message.sid === sid && message.magic === magic && ![1, 6].includes(message.flag), timeout)
}
write(role: string, sid: number, payload: Uint8Array, characteristic = p.G2_WRITE, flag = 0x20, sequence = ++this.sequence): Promise<void> {
// Keep a fragmented control message together when the prompt heartbeat
// overlaps a settings request or shutdown.
const pending = (this.writes.get(role) ?? Promise.resolve()).catch(() => {}).then(async () => {
this.check()
for (const frame of p.frameMessage(payload, sid, flag, sequence, this.limits[role])) {
this.check()
if (this.lost.has(role)) throw new Error(`Lost connection to the ${role} lens.`)
await this.transport.write(this.ids[role], characteristic, frame)
}
this.check()
if (this.lost.has(role)) throw new Error(`Lost connection to the ${role} lens.`)
})
this.writes.set(role, pending)
return pending
}
exchange(role: string, sid: number, payload: Uint8Array, match: Waiter['match'], timeout: number,
write = p.G2_WRITE, notify = p.G2_NOTIFY, flag = 0x20, sequence?: number, data?: Uint8Array): Promise<p.ProtocolMessage> {
this.check()
return new Promise((resolve, reject) => {
let written = false, reply: p.ProtocolMessage | null = null
const waiter: Waiter = { role, characteristic: notify, match,
resolve: message => { reply = message; if (written) resolve(message) }, reject,
timer: null }
this.waiters.add(waiter)
void (async () => {
await this.write(role, sid, payload, write, flag, sequence)
if (data) await this.write(role, 0xc1, data, write, flag, sequence)
this.check()
if (!reply && !this.waiters.has(waiter)) return
written = true
if (reply) resolve(reply)
else waiter.timer = setTimeout(() => {
this.waiters.delete(waiter)
reject(new StockTimeout(`No response from the ${role} lens (0x${sid.toString(16)}).`))
}, timeout)
})().catch(error => { this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); reject(error) })
})
}
onMessage(listener: (role: string, message: p.ProtocolMessage) => void): () => void {
this.listeners.add(listener); return () => this.listeners.delete(listener)
}
private receive(event: TransportEvent): void {
if (this.closed) return
const role = Object.keys(this.ids).find(role => this.ids[role] === event.identifier)
if (!role) return
if (event.kind === 'disconnected') {
this.lost.add(role)
for (const waiter of [...this.waiters]) if (waiter.role === role) {
this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.reject(new Error(`Lost connection to the ${role} lens.`))
}
return
}
if (event.kind !== 'notification' || !event.data || ![p.G2_NOTIFY, OTA_NOTIFY].includes(event.characteristic!)) return
try {
const data = hexToBytes(event.data)
const messages = event.characteristic === OTA_NOTIFY ? decodeOtaAcks(data) : this.receiver.receive(event.identifier!, data)
for (const message of messages) {
for (const waiter of [...this.waiters]) if (waiter.role === role && waiter.characteristic === event.characteristic && waiter.match(message)) {
this.waiters.delete(waiter); if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.resolve(message)
}
if (event.characteristic === p.G2_NOTIFY) for (const listener of this.listeners) listener(role, message)
}
} catch (error) { this.log(`Ignored malformed onboarding notification: ${error}`) }
}
disconnect(role: string): void { this.lost.add(role); if (this.ids[role]) this.transport.disconnect(this.ids[role]); this.receiver.clear() }
close(): void {
if (this.closed) return
this.closed = true; this.off(); this.transport.stopScan()
for (const waiter of this.waiters) { if (waiter.timer !== null) clearTimeout(waiter.timer); waiter.reject(new Error('Cancelled.')) }
this.waiters.clear(); this.listeners.clear()
for (const role of Object.keys(this.ids)) this.disconnect(role)
}
}
/** OTA ACKs are raw opcode/status bytes, not protobuf; validate the aa21 CRC envelope. */
export function decodeOtaAcks(data: Uint8Array): p.ProtocolMessage[] {
const messages: p.ProtocolMessage[] = []
for (let offset = 0; offset + 8 <= data.length;) {
const n = data[offset + 3], end = offset + 8 + n
if (end > data.length) break
if (data[offset] === 0xaa && [0x12, 0x21].includes(data[offset + 1]) && data[offset + 4] === 1 && data[offset + 5] === 1 && n >= 4) {
const payload = data.slice(offset + 8, end - 2)
if (p.crc16(payload) === (data[end - 2] | data[end - 1] << 8))
messages.push({ sid: data[offset + 6], flag: data[offset + 7], payload, command: payload[0], magic: -1 })
}
offset = end
}
return messages
}
-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 = { uploads: 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
}