diff --git a/App_Resources/iOS/Info.plist b/App_Resources/iOS/Info.plist index 4be2ce2c..d8615900 100644 --- a/App_Resources/iOS/Info.plist +++ b/App_Resources/iOS/Info.plist @@ -18,6 +18,8 @@ 1 LSRequiresIPhoneOS + NSBluetoothAlwaysUsageDescription + Faceclaw uses Bluetooth to connect to your glasses and ring, display your apps, and receive gestures. UILaunchScreen UISupportedInterfaceOrientations diff --git a/App_Resources/iOS/build.xcconfig b/App_Resources/iOS/build.xcconfig index e11b5908..05b2c3b1 100644 --- a/App_Resources/iOS/build.xcconfig +++ b/App_Resources/iOS/build.xcconfig @@ -3,3 +3,4 @@ TARGETED_DEVICE_FAMILY = 1; CODE_SIGN_STYLE = Automatic; // Select the local signing team in Xcode or pass --teamId to the iOS scripts. +OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth diff --git a/App_Resources/iOS/src/FaceclawBluetooth.h b/App_Resources/iOS/src/FaceclawBluetooth.h new file mode 100644 index 00000000..ecad409a --- /dev/null +++ b/App_Resources/iOS/src/FaceclawBluetooth.h @@ -0,0 +1,13 @@ +#import +#import + +/** Main-queue CoreBluetooth adapter. Protocol and MAC matching live in TS. */ +@interface FaceclawBluetooth : NSObject +@property (nonatomic, copy) void (^eventHandler)(NSString *json); +- (void)initializeBluetooth; +- (void)scan:(BOOL)enabled; +- (void)connect:(NSString *)identifier requestId:(NSInteger)requestId; +- (void)subscribe:(NSString *)identifier characteristic:(NSString *)uuid requestId:(NSInteger)requestId; +- (void)write:(NSString *)identifier characteristic:(NSString *)uuid data:(NSData *)data requestId:(NSInteger)requestId; +- (void)disconnect:(NSString *)identifier; +@end diff --git a/App_Resources/iOS/src/FaceclawBluetooth.m b/App_Resources/iOS/src/FaceclawBluetooth.m new file mode 100644 index 00000000..c0b93719 --- /dev/null +++ b/App_Resources/iOS/src/FaceclawBluetooth.m @@ -0,0 +1,190 @@ +#import "FaceclawBluetooth.h" + +@interface FCBPeripheral : NSObject +@property (nonatomic, strong) CBPeripheral *peripheral; +@property (nonatomic, strong) NSMutableDictionary *characteristics; +@property (nonatomic, strong) NSMutableDictionary *subscriptions; +@property (nonatomic, strong) NSMutableArray *writes; +@property (nonatomic, strong) NSNumber *connectRequest; +@property (nonatomic) NSInteger pendingServices; +@property (nonatomic) BOOL awaitingWriteResponse; +@end +@implementation FCBPeripheral +- (instancetype)init { + if ((self = [super init])) { + _characteristics = [NSMutableDictionary new]; _subscriptions = [NSMutableDictionary new]; + _writes = [NSMutableArray new]; + } + return self; +} +@end + +@interface FaceclawBluetooth () +@property (nonatomic, strong) CBCentralManager *central; +@property (nonatomic, strong) NSMutableDictionary *peers; +@property (nonatomic) BOOL scanRequested; +@end + +@implementation FaceclawBluetooth +- (instancetype)init { if ((self = [super init])) _peers = [NSMutableDictionary new]; return self; } +- (void)emit:(NSDictionary *)event { + if (!self.eventHandler) return; + NSData *data = [NSJSONSerialization dataWithJSONObject:event options:0 error:nil]; + if (data) self.eventHandler([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); +} +- (void)complete:(NSNumber *)request error:(NSString *)error details:(NSDictionary *)details { + if (!request) return; + NSMutableDictionary *event = [@{ @"kind": @"completion", @"id": request } mutableCopy]; + if (error) event[@"error"] = error; + if (details) event[@"details"] = details; + [self emit:event]; +} +- (void)initializeBluetooth { + if (!self.central) self.central = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()]; + else [self centralManagerDidUpdateState:self.central]; +} +- (void)scan:(BOOL)enabled { + self.scanRequested = enabled; + if (!enabled) { [self.central stopScan]; return; } + [self initializeBluetooth]; + if (self.central.state == CBManagerStatePoweredOn) + [self.central scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}]; +} +- (void)centralManagerDidUpdateState:(CBCentralManager *)central { + [self emit:@{@"kind": @"state", @"state": @(central.state), @"authorization": @(CBManager.authorization)}]; + if (central.state != CBManagerStatePoweredOn) { + for (FCBPeripheral *peer in self.peers.allValues) [self failPeer:peer reason:@"Bluetooth is unavailable"]; + } else if (self.scanRequested) { + [central scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}]; + } +} +- (FCBPeripheral *)remember:(CBPeripheral *)peripheral { + NSString *identifier = peripheral.identifier.UUIDString; + FCBPeripheral *peer = self.peers[identifier]; + if (!peer) { peer = [FCBPeripheral new]; peer.peripheral = peripheral; self.peers[identifier] = peer; } + peripheral.delegate = self; + return peer; +} +- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisement RSSI:(NSNumber *)RSSI { + [self remember:peripheral]; + NSData *data = advertisement[CBAdvertisementDataManufacturerDataKey]; + NSMutableString *hex = [NSMutableString new]; + const uint8_t *bytes = data.bytes; + for (NSUInteger i = 0; i < data.length; i++) [hex appendFormat:@"%02x", bytes[i]]; + [self emit:@{@"kind": @"advertisement", @"identifier": peripheral.identifier.UUIDString, + @"name": advertisement[CBAdvertisementDataLocalNameKey] ?: peripheral.name ?: @"", + @"manufacturerData": hex, @"rssi": RSSI, + @"connectable": advertisement[CBAdvertisementDataIsConnectable] ?: @YES}]; +} +- (void)connect:(NSString *)identifier requestId:(NSInteger)requestId { + if (self.central.state != CBManagerStatePoweredOn) { [self complete:@(requestId) error:@"Bluetooth is not ready" details:nil]; return; } + FCBPeripheral *peer = self.peers[identifier.uppercaseString]; + if (!peer) { + NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:identifier]; + CBPeripheral *peripheral = uuid ? [self.central retrievePeripheralsWithIdentifiers:@[uuid]].firstObject : nil; + if (peripheral) peer = [self remember:peripheral]; + } + if (!peer) { [self complete:@(requestId) error:@"Peripheral is unknown; scan for the device first" details:nil]; return; } + if (peer.connectRequest || peer.peripheral.state != CBPeripheralStateDisconnected) { + [self complete:@(requestId) error:@"Peripheral is already connecting or connected" details:nil]; return; + } + [peer.characteristics removeAllObjects]; peer.connectRequest = @(requestId); + [self.central connectPeripheral:peer.peripheral options:nil]; +} +- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { + [peripheral discoverServices:nil]; +} +- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { + [self failPeer:[self remember:peripheral] reason:error.localizedDescription ?: @"Connection failed"]; +} +- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { + [self failPeer:[self remember:peripheral] reason:error.localizedDescription ?: @"Disconnected"]; +} +- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { + FCBPeripheral *peer = [self remember:peripheral]; + if (error || !peripheral.services.count) { [self failPeer:peer reason:error.localizedDescription ?: @"No services found"]; return; } + peer.pendingServices = peripheral.services.count; + for (CBService *service in peripheral.services) [peripheral discoverCharacteristics:nil forService:service]; +} +- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { + FCBPeripheral *peer = [self remember:peripheral]; + if (!peer.connectRequest) return; + if (error) { [self failPeer:peer reason:error.localizedDescription]; return; } + for (CBCharacteristic *characteristic in service.characteristics) peer.characteristics[characteristic.UUID.UUIDString.lowercaseString] = characteristic; + if (--peer.pendingServices == 0) { + NSNumber *request = peer.connectRequest; peer.connectRequest = nil; + [self complete:request error:nil details:@{@"characteristics": peer.characteristics.allKeys, + @"maxWrite": @([peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithoutResponse])}]; + } +} +- (void)subscribe:(NSString *)identifier characteristic:(NSString *)uuid requestId:(NSInteger)requestId { + FCBPeripheral *peer = self.peers[identifier.uppercaseString]; + CBCharacteristic *characteristic = peer.characteristics[uuid.lowercaseString]; + if (!characteristic || !(characteristic.properties & (CBCharacteristicPropertyNotify | CBCharacteristicPropertyIndicate))) { + [self complete:@(requestId) error:@"Notification characteristic is unavailable" details:nil]; return; + } + if (characteristic.isNotifying) { [self complete:@(requestId) error:nil details:nil]; return; } + if (peer.subscriptions[uuid.lowercaseString]) { [self complete:@(requestId) error:@"Subscription already pending" details:nil]; return; } + peer.subscriptions[uuid.lowercaseString] = @(requestId); + [peer.peripheral setNotifyValue:YES forCharacteristic:characteristic]; +} +- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + FCBPeripheral *peer = [self remember:peripheral]; NSString *key = characteristic.UUID.UUIDString.lowercaseString; + NSNumber *request = peer.subscriptions[key]; [peer.subscriptions removeObjectForKey:key]; + [self complete:request error:error.localizedDescription ?: (characteristic.isNotifying ? nil : @"Notifications not enabled") details:nil]; +} +- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + if (error) { [self emit:@{@"kind": @"error", @"message": error.localizedDescription}]; return; } + NSMutableString *hex = [NSMutableString new]; NSData *data = characteristic.value; const uint8_t *bytes = data.bytes; + for (NSUInteger i = 0; i < data.length; i++) [hex appendFormat:@"%02x", bytes[i]]; + [self emit:@{@"kind": @"notification", @"identifier": peripheral.identifier.UUIDString, + @"characteristic": characteristic.UUID.UUIDString.lowercaseString, @"data": hex}]; +} +- (void)write:(NSString *)identifier characteristic:(NSString *)uuid data:(NSData *)data requestId:(NSInteger)requestId { + FCBPeripheral *peer = self.peers[identifier.uppercaseString]; + CBCharacteristic *characteristic = peer.characteristics[uuid.lowercaseString]; + if (!characteristic || peer.peripheral.state != CBPeripheralStateConnected) { + [self complete:@(requestId) error:@"Write characteristic is disconnected or unavailable" details:nil]; return; + } + CBCharacteristicWriteType type = (characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) + ? CBCharacteristicWriteWithoutResponse : CBCharacteristicWriteWithResponse; + if (!(characteristic.properties & (CBCharacteristicPropertyWriteWithoutResponse | CBCharacteristicPropertyWrite)) || + data.length > [peer.peripheral maximumWriteValueLengthForType:type]) { + [self complete:@(requestId) error:@"Write exceeds the negotiated BLE payload size or is unsupported" details:nil]; return; + } + if (peer.writes.count >= 256) { [self complete:@(requestId) error:@"Bluetooth write queue is full" details:nil]; return; } + [peer.writes addObject:@{@"id": @(requestId), @"characteristic": characteristic, @"data": [data copy], @"type": @(type)}]; + [self drain:peer]; +} +- (void)drain:(FCBPeripheral *)peer { + while (peer.writes.count && !peer.awaitingWriteResponse && peer.peripheral.state == CBPeripheralStateConnected) { + NSDictionary *write = peer.writes.firstObject; + CBCharacteristicWriteType type = [write[@"type"] integerValue]; + if (type == CBCharacteristicWriteWithoutResponse && !peer.peripheral.canSendWriteWithoutResponse) return; + if (type == CBCharacteristicWriteWithResponse) peer.awaitingWriteResponse = YES; + [peer.peripheral writeValue:write[@"data"] forCharacteristic:write[@"characteristic"] type:type]; + if (peer.awaitingWriteResponse) return; + [peer.writes removeObjectAtIndex:0]; [self complete:write[@"id"] error:nil details:nil]; + } +} +- (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral *)peripheral { [self drain:[self remember:peripheral]]; } +- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + FCBPeripheral *peer = [self remember:peripheral]; + if (!peer.awaitingWriteResponse || !peer.writes.count) return; + NSDictionary *write = peer.writes.firstObject; [peer.writes removeObjectAtIndex:0]; peer.awaitingWriteResponse = NO; + [self complete:write[@"id"] error:error.localizedDescription details:nil]; [self drain:peer]; +} +- (void)failPeer:(FCBPeripheral *)peer reason:(NSString *)reason { + if (!peer) return; + NSNumber *connect = peer.connectRequest; peer.connectRequest = nil; + [self complete:connect error:reason details:nil]; + NSArray *subscriptions = peer.subscriptions.allValues; [peer.subscriptions removeAllObjects]; + NSArray *writes = [peer.writes copy]; [peer.writes removeAllObjects]; peer.awaitingWriteResponse = NO; + for (NSNumber *request in subscriptions) [self complete:request error:reason details:nil]; + for (NSDictionary *write in writes) [self complete:write[@"id"] error:reason details:nil]; + [peer.characteristics removeAllObjects]; + if (peer.peripheral.state != CBPeripheralStateDisconnected) [self.central cancelPeripheralConnection:peer.peripheral]; + [self emit:@{@"kind": @"disconnected", @"identifier": peer.peripheral.identifier.UUIDString, @"message": reason}]; +} +- (void)disconnect:(NSString *)identifier { [self failPeer:self.peers[identifier.uppercaseString] reason:@"Disconnected"]; } +@end diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap index 560d5493..03ec29ec 100644 --- a/App_Resources/iOS/src/module.modulemap +++ b/App_Resources/iOS/src/module.modulemap @@ -1,4 +1,5 @@ module FaceclawGraphics { header "FaceclawGraphics.h" + header "FaceclawBluetooth.h" export * } diff --git a/app/g2/ble-protocol.ts b/app/g2/ble-protocol.ts new file mode 100644 index 00000000..a85c2219 --- /dev/null +++ b/app/g2/ble-protocol.ts @@ -0,0 +1,178 @@ +/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */ +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 } 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 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, ' ')) + const image = concat(geometry, integer(5, 10), string(6, 'img00')) + return wrap(0, magic, 3, concat(integer(1, 2), bytes(3, text), bytes(4, image), integer(5, 10000))) +} +export function imageFragment(magic: number, payload: Uint8Array, offset: number, size = 3800): Uint8Array { + const fragment = payload.subarray(offset, offset + size) + return wrap(3, magic, 5, concat(integer(1, 10), string(2, 'img00'), integer(3, 10), integer(4, payload.length), + integer(5, 0), integer(6, Math.floor(offset / size)), integer(7, fragment.length), bytes(8, fragment))) +} +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 } +/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */ +export class MessageReceiver { + private streams = new Map() + private messages = new Map() + 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 chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8) + 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 = { kind: 'sys-event' | 'list-click' | 'text-click'; 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, eventType: readInteger(event, typeField), eventSource: field === 3 ? readInteger(event, 2) : 0, + 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)[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) +} diff --git a/app/g2/glasses-session.ts b/app/g2/glasses-session.ts new file mode 100644 index 00000000..8f7a67ae --- /dev/null +++ b/app/g2/glasses-session.ts @@ -0,0 +1,298 @@ +import * as protocol from './ble-protocol' +import { hexToBytes } from '../util/hex-util' +import { deviceAddressError } from './ios-peripheral-identity' + +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 } +export interface SessionTransport { + onEvent(listener: (event: TransportEvent) => void): () => void + resolveDevices(addresses: SessionAddresses): Promise> + connect(identifier: string): Promise<{ characteristics: string[]; maxWrite: number }> + subscribe(identifier: string, characteristic: string): Promise + write(identifier: string, characteristic: string, bytes: Uint8Array): Promise + disconnect(identifier: string): void + stopScan(): void +} +export type SessionState = { phase: 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error'; + status: string; battery: number | 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 {} + +/** 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, ring: false, + frames: 0, capabilities: '', leftVersion: '', rightVersion: '' } + private generation = 0 + private ids: Record = {} + private limits: Record = {} + private pending = new Map() + private receiver = new protocol.MessageReceiver() + private writes = new Map>() + private magic = 100 + private sequence = 0x40 + private timer: number | null = null + private retryTimer: number | null = null + private retryCount = 0 + private wanted = false + private addresses: SessionAddresses | null = null + private latest: Uint8Array | null = null + private displayed: Uint8Array | null = null + private pumping = false + private lastHeartbeat = 0 + private lastLease = 0 + private lastSettings = 0 + private charging = false + private layoutCreated = false + private off: () => void + constructor(private readonly transport: SessionTransport, + private readonly deflate: (data: Uint8Array) => Uint8Array, + private readonly onState: (state: SessionState) => void, + private readonly onInput: (input: protocol.GlassesInput) => void, + private readonly log: (message: string) => void = () => {}) { + 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 { + 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 + if (!retry) this.retryCount = 0 + const generation = ++this.generation + this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: 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]); 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 caps = this.state.capabilities.split(/\s+/) + if (!['img640', 'fbguard', 'wearnotify'].every(cap => caps.includes(cap))) + throw new Error('The glasses do not report compatible Faceclaw firmware (img640, fbguard, wearnotify). Bluetooth is working, but this display requires the modified firmware. Flashing remains available on Android.') + 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 + this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now() + this.displayed = null; this.retryCount = 0 + this.update('connected', 'Glasses connected') + this.schedule() + if (addresses.ring && ids.ring) { + try { + const ring = await this.transport.connect(ids.ring); this.check(generation) + const notifications = protocol.RING_NOTIFY.filter(uuid => ring.characteristics.includes(uuid)) + if (!notifications.length) throw new Error('No R1 gesture notifications found') + for (const uuid of notifications) await this.transport.subscribe(ids.ring, uuid) + this.check(generation); this.state.ring = true; this.update('connected', 'Glasses and ring connected') + } catch (error) { + if (generation !== this.generation) return + this.transport.disconnect(ids.ring) + this.update('connected', `Glasses connected; ring unavailable: ${this.message(error)}`) + } + } 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) + } + } + 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.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 { + const magic = fixedMagic ?? this.nextMagic(), payload = build(magic), key = `${sid}:${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 { + const identifier = this.ids[role], generation = this.generation + if (!identifier) return Promise.reject(new Error(`No ${role} device connected`)) + const frames = protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role]) + const previous = this.writes.get(identifier) ?? Promise.resolve() + const work = previous.catch(() => {}).then(async () => { + for (const frame of frames) { this.check(generation); await this.transport.write(identifier, protocol.G2_WRITE, frame) } + }) + // 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 { + return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire)))) + } + private receive(event: TransportEvent): void { + if (!Object.values(this.ids).includes(event.identifier ?? '')) 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') + } 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 + try { + const data = hexToBytes(event.data) + if (event.identifier === this.ids.ring) { + if (!protocol.RING_NOTIFY.includes(event.characteristic ?? '')) return + const input = protocol.decodeRingInput(data); if (input) this.onInput(input); return + } + if (event.characteristic !== protocol.G2_NOTIFY) return + for (const message of this.receiver.receive(event.identifier!, data)) { + 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) + if (event.identifier === this.ids.right) { + 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)}`) } + } + 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.charging = charging > 0 + } + this.onState({ ...this.state }) + } + setFrame(gray: Uint8Array): void { + if (this.state.phase !== 'connected') return + this.latest = protocol.packGray4(gray, 640, 480); this.schedule(0) + } + 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 async pump(): Promise { + if (this.pumping || this.state.phase !== 'connected') return + this.pumping = true; const generation = this.generation + try { + const now = Date.now() + if (now - this.lastHeartbeat >= 4000) { + await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now() + } + 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() + } + const frame = this.latest; this.latest = null + if (frame && !this.charging && (!this.displayed || !frame.every((value, i) => value === this.displayed![i]))) { + const payload = protocol.concat(new Uint8Array([6]), this.deflate(protocol.rle4(frame))) + if (payload.length > 165888) throw new Error('Compressed frame exceeds the glasses image buffer') + for (let offset = 0; offset < payload.length; offset += 3800) { + await this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, payload, offset), 'Display frame') + this.check(generation); this.lastHeartbeat = Date.now() + } + this.displayed = frame; this.state.frames++; this.onState({ ...this.state }) + this.log(`Display frame ${this.state.frames} acknowledged (${payload.length} bytes)`) + } else if (frame && this.charging) this.latest = frame + } catch (error) { if (generation === this.generation) this.fail(error, true) } + finally { if (generation === this.generation) { this.pumping = false; this.schedule(this.latest && !this.charging ? 0 : 1000) } } + } + private reset(): void { + ++this.generation + if (this.timer !== null) clearTimeout(this.timer) + if (this.retryTimer !== null) clearTimeout(this.retryTimer) + 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')) } + this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null + } + 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) } + } + async stop(): Promise { + this.wanted = false + const cleanup = this.layoutCreated && this.state.phase === 'connected' + this.update('disconnecting', 'Disconnecting…'); this.reset() + try { + if (cleanup) { + if (this.state.capabilities.split(/\s+/).includes('cleanup11')) + await this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, new Uint8Array([11]), 0), 'Display cleanup', 1200) + else { + await this.request('right', protocol.SID.hub, protocol.shutdown, 'Close display', 1200) + await this.lease(false) + } + } + } 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) } +} diff --git a/app/g2/ios-peripheral-identity.ts b/app/g2/ios-peripheral-identity.ts new file mode 100644 index 00000000..4dd32f12 --- /dev/null +++ b/app/g2/ios-peripheral-identity.ts @@ -0,0 +1,25 @@ +import { classifyAdvertisement, hasEvenManufacturerSignature, type EvenAdvertisement } from './even-advertisement' +import { hexToBytes } from '../util/hex-util' + +export type IosAdvertisement = { identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean } +export type IosDevice = EvenAdvertisement & { identifier: string } + +/** Only a full vendor-advertised MAC may bind a manually entered address. */ +export function identifyIosPeripheral(raw: IosAdvertisement): IosDevice | null { + if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(raw.identifier)) return null + if (!hasEvenManufacturerSignature(hexToBytes(raw.manufacturerData))) return null + const candidate = classifyAdvertisement({ ...raw, address: '', source: 'scan', bonded: false, + txPower: null, seenAtMs: Date.now() }) + if (!candidate?.embeddedMac) return null + return { ...candidate, address: candidate.embeddedMac, embeddedMacMismatch: false, identifier: raw.identifier.toUpperCase() } +} + +export function deviceAddressError(addresses: { left: string; right: string; ring: string }): string | null { + for (const role of ['right', 'left', 'ring'] as const) { + const address = addresses[role] + if (role === 'ring' && !address) continue + if (!/^[0-9A-F]{2}(?::[0-9A-F]{2}){5}$/.test(address)) return `${role === 'ring' ? 'Ring' : role === 'left' ? 'Left arm' : 'Right arm'} MAC address is invalid.` + } + const values = Object.values(addresses).filter(Boolean) + return new Set(values).size === values.length ? null : 'Each device must have a different MAC address.' +} diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index c2ff1807..f1da35f9 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -1,4 +1,8 @@ -import { Dialogs, type ImageSource } from '@nativescript/core' +import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core' +import { iosBluetooth } from '../native/ios-bluetooth' +import { GlassesSession, type SessionState } from './glasses-session' +import { loadDeviceAddresses } from './device-addresses' +import { deviceAddressError } from './ios-peripheral-identity' import { createLauncherWindow, LAUNCHER_SURFACE_ID } from '../apps/launcher/launcher-app' import { CalculatorLayer } from '../apps/calculator/calculator' import { MathCoordinator } from '../apps/calculator/math/coordinator' @@ -10,7 +14,7 @@ import { makeInputEvent, type InputEventPayload } from '../ui/gestures' import { noopLayerActions, type LayerActions } from '../ui/layers' import { MenuLayer } from '../ui/menu' import { createInProcessWindow } from '../ui/shell/in-process-window' -import { shell, type ShellWindow } from '../ui/shell/shell' +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, timeFormatSetting, verticalPositionSetting } from '../ui/dashboard-settings' @@ -27,6 +31,11 @@ export class IosPreviewController { private inputQueue: Promise = Promise.resolve() private lastLayout = '' private prompting = false + private session: GlassesSession | null = null + private resumeConnection = false + private suspendedDisconnect: Promise = Promise.resolve() + private logLines: string[] = [] + private logTimer: ReturnType | null = null private readonly actions: LayerActions = { ...noopLayerActions, requestRender: () => this.requestShellRender(), @@ -38,7 +47,8 @@ export class IosPreviewController { } constructor(private readonly onFrame: (image: ImageSource, focus: string) => void, - private readonly onError: (message: string) => void) { + private readonly onError: (message: string) => void, + private readonly onConnectionState: (state: SessionState) => void = () => {}) { this.compositor.configureSurface('shell', { x: 0, y: 0, width: 640, height: 480, zOrder: 1, transparency: 'color-key' }) shell.configure({ actions: this.actions, @@ -78,8 +88,14 @@ export class IosPreviewController { shell.foregroundWindow()?.requestRender() this.requestShellRender() console.log('[ios-preview] Main screen active') + if (this.resumeConnection) { + this.resumeConnection = false + void this.suspendedDisconnect.then(() => { if (this.active) void this.connect() }) + } } pause(): void { + this.resumeConnection = !!this.session && ['connected', 'connecting', 'retrying'].includes(this.session.state.phase) + if (this.session) this.suspendedDisconnect = this.session.stop() this.active = false this.offSettings?.(); this.offSettings = null if (this.clockTimer) clearInterval(this.clockTimer) @@ -119,7 +135,9 @@ export class IosPreviewController { this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 }) this.compositor.setUnderlayDim(1, shell.underlayDim()) } - const image = previewPixels(this.compositor.composite(), 640, 480, previewColorSetting.get() === 'green') + const pixels = this.compositor.composite() + this.session?.setFrame(pixels) + const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green') this.onFrame(image, `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`) } catch (error) { this.fail(error) } }, 33) @@ -203,6 +221,42 @@ 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 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.' + } + async connect(): Promise { + if (!this.active) return + const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses) + if (error) { this.onError(error); return } + if (!this.session) { + const { deflate } = require('pako') as { deflate: (data: Uint8Array) => Uint8Array } + this.session = new GlassesSession(iosBluetooth(), deflate, state => { + this.onConnectionState(state) + if (state.phase === 'connected') this.requestShellRender() + }, input => { + this.inputQueue = this.inputQueue.then(async () => { + if (!this.active) return + await shell.receiveInput(rawInputEventToInputEvent(input)); this.requestShellRender() + this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`) + }).catch(error => this.fail(error)) + }, message => this.logBluetooth(message)) + } + await this.session.start(addresses) + } + async disconnect(): Promise { this.resumeConnection = false; await this.session?.stop() } + private logBluetooth(message: string): void { + const line = `${new Date().toISOString()} ${message}` + console.log(`[ios-ble] ${line}`); this.logLines.push(line) + if (this.logLines.length > 200) this.logLines.shift() + if (this.logTimer !== null) return + this.logTimer = setTimeout(() => { + this.logTimer = null + try { File.fromPath(path.join(knownFolders.documents().path, 'bluetooth.log')).writeTextSync(this.logLines.join('\n')) } + catch (error) { console.warn(`Bluetooth log: ${error}`) } + }, 500) + } async typeIntoApp(): Promise { if (this.prompting || !shell.foregroundWindow()?.receiveTextInput) return this.prompting = true diff --git a/app/native/ios-bluetooth.ts b/app/native/ios-bluetooth.ts new file mode 100644 index 00000000..fab13d6b --- /dev/null +++ b/app/native/ios-bluetooth.ts @@ -0,0 +1,144 @@ +import { ApplicationSettings } from '@nativescript/core' +import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity' +import { hexToBytes } from '../util/hex-util' + +declare const FaceclawBluetooth: any +type NativeEvent = { kind: string; id?: number; error?: string; details?: ConnectionDetails; state?: number; + identifier?: string; characteristic?: string; data?: string; message?: string } & Partial +export type ConnectionDetails = { characteristics: string[]; maxWrite: number } +export type BluetoothEvent = NativeEvent | { kind: 'device'; device: IosDevice } +type Pending = { resolve: (details: ConnectionDetails) => void; reject: (error: Error) => void; timer: ReturnType } + +/** One central manager per app, with asynchronous main-queue GATT operations. */ +export class IosBluetooth { + private readonly native = FaceclawBluetooth.new() + private readonly listeners = new Set<(event: BluetoothEvent) => void>() + private readonly pending = new Map() + private readonly advertisements = new Map() + readonly devices = new Map() + private requestId = 0 + private scanTimer: ReturnType | null = null + state = 0 + scanning = false + constructor() { + this.native.eventHandler = (json: string) => { + try { this.receive(JSON.parse(json)) } catch (error) { console.error(`[ios-ble] ${error}`) } + } + } + onEvent(listener: (event: BluetoothEvent) => void): () => void { + this.listeners.add(listener); return () => { this.listeners.delete(listener) } + } + private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) } + private receive(event: NativeEvent): void { + if (event.kind === 'completion') { + const pending = this.pending.get(event.id!) + if (pending) { + this.pending.delete(event.id!); clearTimeout(pending.timer) + if (event.error) pending.reject(new Error(event.error)); else pending.resolve(event.details!) + } + return + } + if (event.kind === 'state') this.state = event.state! + if (event.kind === 'advertisement') { + const previous = this.advertisements.get(event.identifier!) + const raw: IosAdvertisement = { identifier: event.identifier!, name: event.name || previous?.name || '', + manufacturerData: event.manufacturerData || previous?.manufacturerData || '', rssi: event.rssi!, connectable: event.connectable !== false } + // Bound the cache even in a crowded radio environment. + if (this.advertisements.size >= 512 && !this.advertisements.has(raw.identifier)) this.advertisements.delete(this.advertisements.keys().next().value!) + this.advertisements.set(raw.identifier, raw) + const device = identifyIosPeripheral(raw) + if (device) { + this.devices.set(device.address, device) + ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role })) + this.emit({ kind: 'device', device }) + } + return + } + this.emit(event) + } + ensureReady(): Promise { + return new Promise((resolve, reject) => { + const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() } + const off = this.onEvent(event => { + if (event.kind !== 'state') return + const state = (event as NativeEvent).state + if (state === 5) finish() + else if (state === 2) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.')) + else if (state === 3) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.')) + else if (state === 4) finish(new Error('Turn on Bluetooth, then try again.')) + }) + const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000) + this.native.initializeBluetooth() + }) + } + async startScan(durationMs = 12_000): Promise { + await this.ensureReady() + this.stopScan(); this.scanning = true; this.native.scan(true) + this.scanTimer = setTimeout(() => this.stopScan(), durationMs) + this.emit({ kind: 'scan-started' }) + } + stopScan(): void { + if (this.scanTimer) clearTimeout(this.scanTimer) + this.scanTimer = null; this.native.scan(false) + const wasScanning = this.scanning; this.scanning = false + if (wasScanning) this.emit({ kind: 'scan-stopped' }) + } + async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise> { + await this.ensureReady() + const result: Record = {} + for (const role of ['left', 'right', 'ring'] as const) { + const address = addresses[role] + if (!address) continue + const live = this.devices.get(address) + if (live && live.role !== role) throw new Error(`${address} advertises as ${live.role}, not ${role}. Check Configure devices.`) + try { + const saved = JSON.parse(ApplicationSettings.getString(`ios.ble.peripheral.${address}`, '{}')) + if (saved.role === role && typeof saved.identifier === 'string') result[role] = saved.identifier + } catch { /* A fresh scan can recover malformed settings. */ } + } + const missing = () => (['left', 'right'] as const).filter(role => !result[role]) + if (!missing().length && (!addresses.ring || result.ring)) return result + await this.startScan(15_000) + await new Promise((resolve, reject) => { + const off = this.onEvent(event => { + if (event.kind === 'device') { + const device = (event as { device: IosDevice }).device + for (const role of ['left', 'right', 'ring'] as const) { + if (addresses[role] === device.address && device.role === role) result[role] = device.identifier + } + if (!missing().length && (!addresses.ring || result.ring)) { off(); this.stopScan(); resolve() } + } else if (event.kind === 'scan-stopped') { + off() + if (missing().length) reject(new Error(`Could not find the ${missing().join(' and ')} arm. Wake the glasses, disconnect other apps, and scan again.`)) + else resolve() // An optional missing ring must not block the glasses. + } + }) + }) + return result + } + private operation(identifier: string, call: (id: number) => void, timeoutMs = 10_000): Promise { + const id = ++this.requestId + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + this.pending.delete(id); this.native.disconnect(identifier) + reject(new Error('Bluetooth operation timed out. Wake the device and try connecting again.')) + }, timeoutMs) + this.pending.set(id, { resolve, reject, timer }) + try { call(id) } catch (error) { clearTimeout(timer); this.pending.delete(id); reject(error) } + }) + } + connect(identifier: string): Promise { return this.operation(identifier, id => this.native.connectRequestId(identifier, id), 20_000) } + async subscribe(identifier: string, characteristic: string): Promise { + await this.operation(identifier, id => this.native.subscribeCharacteristicRequestId(identifier, characteristic, id)) + } + async write(identifier: string, characteristic: string, bytes: Uint8Array): Promise { + const copy = new Uint8Array(bytes) + const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength) + await this.operation(identifier, id => this.native.writeCharacteristicDataRequestId(identifier, characteristic, data, id)) + } + disconnect(identifier: string): void { this.native.disconnect(identifier) } + forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) } +} +let instance: IosBluetooth | null = null +export function iosBluetooth(): IosBluetooth { return instance ??= new IosBluetooth() } +export { hexToBytes } diff --git a/app/phone-ui/config-page.ios.ts b/app/phone-ui/config-page.ios.ts new file mode 100644 index 00000000..7099ccbf --- /dev/null +++ b/app/phone-ui/config-page.ios.ts @@ -0,0 +1,68 @@ +import { Button, Color, GridLayout, Label, Page, ScrollView, StackLayout, TextField } from '@nativescript/core' +import { loadDeviceAddresses, normalizeMacAddress, saveDeviceAddresses } from '../g2/device-addresses' +import { deviceAddressError, type IosDevice } from '../g2/ios-peripheral-identity' +import { iosBluetooth } from '../native/ios-bluetooth' + +export function createConfigureDevicesPage(): Page { + const page = new Page(); page.actionBarHidden = true + const root = new GridLayout(); root.rows = 'auto,*,auto'; root.padding = 16; page.content = root + const title = new Label(); title.text = 'Configure devices'; title.fontSize = 24; title.fontWeight = 'bold'; root.addChild(title) + const scroll = new ScrollView(); GridLayout.setRow(scroll, 1); root.addChild(scroll) + const form = new StackLayout(); scroll.content = form + const text = (value: string, size = 15) => { + const label = new Label(); label.text = value; label.fontSize = size; label.textWrap = true; label.marginTop = 12; form.addChild(label); return label + } + text('Enter the G2 arm MAC addresses and optional R1 ring address. Scan once so this iPhone can find the matching devices. Saving does not connect.') + const stored = loadDeviceAddresses() + const fields = {} as Record<'left' | 'right' | 'ring', TextField> + for (const role of ['right', 'left', 'ring'] as const) { + text(role === 'ring' ? 'R1 ring MAC (optional)' : `${role === 'left' ? 'Left' : 'Right'} arm MAC`) + const field = new TextField(); field.text = stored[role]; field.hint = 'AA:BB:CC:DD:EE:FF' + field.autocorrect = false; field.autocapitalizationType = 'allcharacters'; field.fontSize = 18 + field.padding = 10; field.backgroundColor = new Color('#ffffff'); field.accessibilityLabel = `${role} MAC address` + field.accessibilityIdentifier = `device-address-${role}`; form.addChild(field); fields[role] = field + } + const status = text('Addresses are saved only when you tap Save.'); status.color = new Color('#285c45') + const button = (label: string, action: () => void) => { + const view = new Button(); view.text = label; view.fontSize = 15; view.marginTop = 12; view.on('tap', action); return view + } + let off: (() => void) | null = null + let closed = false + const found = new StackLayout() + const rows = new Map() + const showDevice = (device: IosDevice) => { + if (device.connectable === false) return + let row = rows.get(device.address) + if (!row) { + row = button('', () => { fields[device.role].text = device.address; status.text = `${device.role} address selected. Tap Save to keep it.` }) + row.textWrap = true; row.fontSize = 13; rows.set(device.address, row); found.addChild(row) + } + row.text = `${device.role === 'ring' ? 'Ring' : device.role === 'left' ? 'Left arm' : 'Right arm'} · ${device.address}\n${device.serial || device.name} · ${device.rssi ?? '?'} dBm` + } + const scan = button('Scan for devices', () => { + for (const field of Object.values(fields)) field.dismissSoftInput() + const ble = iosBluetooth() + off?.(); off = ble.onEvent(event => { + if (closed) return + if (event.kind === 'device' && 'device' in event) showDevice(event.device) + if (event.kind === 'scan-stopped') { scan.isEnabled = true; status.text = rows.size ? 'Tap a device to fill its address, then Save.' : 'No matching devices found. Wake the glasses and disconnect other apps, then scan again.' } + }) + rows.clear(); found.removeChildren(); scan.isEnabled = false; status.text = 'Scanning… Allow Bluetooth if iOS asks.' + void ble.startScan().catch(error => { if (!closed) { status.text = String(error.message ?? error); scan.isEnabled = true } }) + }) + form.addChild(scan); form.addChild(found) + const footer = new GridLayout(); footer.columns = '*,*'; GridLayout.setRow(footer, 2); root.addChild(footer) + const close = () => { closed = true; off?.(); off = null; iosBluetooth().stopScan(); page.closeModal() } + const back = button('Back', close); footer.addChild(back) + const save = button('Save', () => { + const addresses = { right: normalizeMacAddress(fields.right.text), left: normalizeMacAddress(fields.left.text), ring: normalizeMacAddress(fields.ring.text) } + const error = deviceAddressError(addresses) + if (error) { status.text = error; return } + saveDeviceAddresses(addresses) + for (const role of ['left', 'right', 'ring'] as const) { fields[role].text = addresses[role]; fields[role].dismissSoftInput() } + status.text = 'Saved device addresses. Return to the main screen and choose Connect.' + }) + GridLayout.setColumn(save, 1); footer.addChild(save) + page.on('unloaded', () => { closed = true; off?.(); off = null; iosBluetooth().stopScan() }) + return page +} diff --git a/app/phone-ui/main-page.ios.ts b/app/phone-ui/main-page.ios.ts index 1ade0eab..ba014754 100644 --- a/app/phone-ui/main-page.ios.ts +++ b/app/phone-ui/main-page.ios.ts @@ -1,7 +1,10 @@ -import { Application, Button, Color, GridLayout, Image, Label, Page, StackLayout, type TouchGestureEventData } from '@nativescript/core' +import { Application, Button, Color, Dialogs, GridLayout, Image, Label, Page, StackLayout, type TouchGestureEventData } from '@nativescript/core' +import { createConfigureDevicesPage } from './config-page.ios' import { IosPreviewController } from '../g2/ios-preview-controller' import { PhoneGestureRecognizer } from './phone-gestures' import { onAnySettingChanged, previewColorSetting } from '../ui/dashboard-settings' +import { loadDeviceAddresses } from '../g2/device-addresses' +import { deviceAddressError } from '../g2/ios-peripheral-identity' export function createMainPage(): Page { const page = new Page() @@ -13,10 +16,13 @@ export function createMainPage(): Page { header.columns = '*,auto'; header.padding = '12 20' const title = new Label() title.text = 'Faceclaw'; title.fontSize = 25; title.fontWeight = 'bold' - header.addChild(title) + const heading = new StackLayout(); heading.addChild(title); header.addChild(heading) const state = new Label() - state.text = 'Preview only'; state.fontSize = 14; state.verticalAlignment = 'middle' - GridLayout.setColumn(state, 1); header.addChild(state); root.addChild(header) + state.text = 'Preview only'; state.fontSize = 12; heading.addChild(state) + const devices = new Button() + devices.text = 'Devices'; devices.accessibilityLabel = 'Configure devices'; devices.fontSize = 14; devices.padding = '10 12' + devices.on('tap', () => { void openDevices() }) + GridLayout.setColumn(devices, 1); header.addChild(devices); root.addChild(header) const body = new GridLayout() GridLayout.setRow(body, 1); root.addChild(body) const mirror = new Image() @@ -52,7 +58,21 @@ export function createMainPage(): Page { mirror.imageSource = image; focus.text = selectedTab === 'ring' ? 'Ring' : label }, message => { errorLabel.text = message; errorLabel.visibility = 'visible'; selectTab('settings') + }, connection => { + state.text = connection.phase === 'connected' ? `Connected${connection.battery !== null ? ` · ${connection.battery}%` : ''}${connection.ring ? ' · R1' : ''}` + : ({ disconnected: 'Preview only', connecting: 'Connecting…', retrying: 'Reconnecting…', disconnecting: 'Disconnecting…', error: 'Connection failed · Devices for details' })[connection.phase] || connection.phase }) + async function openDevices(): Promise { + const connected = ['connected', 'connecting', 'retrying'].includes(controller.connectionState?.phase ?? '') + const choice = await Dialogs.action({ title: 'Devices', message: controller.connectionState?.status ?? 'Preview only', cancelButtonText: 'Cancel', + actions: ['Configure devices', connected ? 'Disconnect' : 'Connect', 'Connection details'] }) + if (choice === 'Configure devices') { + await controller.disconnect() + page.showModal(createConfigureDevicesPage(), { fullscreen: true, closeCallback: () => {} }) + } else if (choice === 'Connect') await controller.connect() + else if (choice === 'Disconnect') await controller.disconnect() + else if (choice === 'Connection details') await Dialogs.alert({ title: 'Connection details', message: controller.connectionDetails, okButtonText: 'OK' }) + } const padInput = new PhoneGestureRecognizer(gesture => controller.gesture(gesture, selectedTab === 'ring' ? 'ring' : 'watch')) const mirrorInput = new PhoneGestureRecognizer((gesture, x, y) => { const size = mirror.getActualSize() @@ -128,10 +148,16 @@ export function createMainPage(): Page { } const resume = () => controller.resume() let offSettings: (() => void) | null = null + let firstLoad = true page.on('loaded', () => { Application.on(Application.suspendEvent, pause); Application.on(Application.resumeEvent, resume) offSettings?.(); offSettings = onAnySettingChanged(() => { sizeButton.text = controller.displayModeLabel }) selectTab(selectedTab); controller.resume() + if (firstLoad) { + firstLoad = false + // Match Android's reconnect-on-launch behavior once addresses are saved. + if (!deviceAddressError(loadDeviceAddresses())) void controller.connect() + } }) page.on('unloaded', () => { pause(); offSettings?.(); offSettings = null diff --git a/tests/ios-ble.test.cjs b/tests/ios-ble.test.cjs new file mode 100644 index 00000000..07c186d6 --- /dev/null +++ b/tests/ios-ble.test.cjs @@ -0,0 +1,171 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const p = require('../.test-build/app/g2/ble-protocol.js'); +const { identifyIosPeripheral, deviceAddressError } = require('../.test-build/app/g2/ios-peripheral-identity.js'); +const { GlassesSession } = require('../.test-build/app/g2/glasses-session.js'); +const { deflate, inflate } = require('pako'); +const hex = bytes => Buffer.from(bytes).toString('hex'); +const string = (field, text) => p.bytes(field, new Uint8Array(Buffer.from(text))); + +// Generated by compiling the existing Android BleProtocol.java/BmpUtil.java, +// plus BleImageOptimizer's unchanged RLE method, with javac 25.0.2. +test('iOS/shared protocol matches Android-generated wire vectors byte for byte', () => { + const vectors = [ + [p.authentication(101), '080410651a0408011004'], + [p.prelude(), '0802109c01220a1a081206120408001000'], + [p.createLayout(102), '080010661a3808021a1c0800100018c00420a0024801520964617368626f617264580162012022130800100018c00420a002280a3205696d67303028904e'], + [p.heartbeat(103), '080c106772020800'], + [p.settingsQuery(104), '0802106822020801'], + [p.shutdown(105), '080910695a020801'], + [p.framebufferLease(true), '08011000aa0606464301050000'], + [p.framebufferLease(false), '08011000aa0606464301060000'], + [p.imageFragment(106, new Uint8Array([6, 1, 2, 3, 4]), 0), '0803106a2a1a080a1205696d673030180a200528003000380542050601020304'], + [p.frameMessage(p.authentication(101), 128, 0, 64)[0], 'aa21400c01018000080410651a04080110041024'], + ]; + for (const [actual, expected] of vectors) assert.equal(hex(actual), expected); + const packed = p.packGray4(new Uint8Array([0, 1, 7, 8, 15, 16, 23, 24, 127, 128, 247, 255]), 3, 4); + assert.equal(hex(packed), '0000111012808ff0'); + assert.equal(hex(p.rle4(packed)), '40311011121810182f10'); +}); + +test('negotiated-MTU framing reassembles split and coalesced notifications and rejects bad CRC', () => { + const payload = p.concat(p.integer(1, 3), p.integer(2, 110), p.bytes(8, new Uint8Array(3900).fill(55))); + const frames = p.frameMessage(payload, 0xe0, 0x20, 17, 185); + assert.ok(frames.every(f => f.length <= 185)); + const receiver = new p.MessageReceiver(), stream = p.concat(...frames); + assert.deepEqual(receiver.receive('L', stream.slice(0, 7)), []); + const messages = receiver.receive('L', stream.slice(7)); + assert.equal(messages.length, 1); assert.deepEqual(messages[0].payload, payload); + stream[stream.length - 1] ^= 1; + assert.deepEqual(receiver.receive('L', stream), []); + assert.throws(() => p.frameMessage(payload, 1, 0, 1, 10), /too small/); + assert.throws(() => p.readBytes(new Uint8Array([0x1a, 0x7f]), 3), /Truncated/); +}); + +test('interleaved links and missing/expired fragments cannot produce false ACKs', () => { + const payload = p.concat(p.integer(1, 3), p.integer(2, 110), p.bytes(8, new Uint8Array(300))); + const frames = p.frameMessage(payload, 224, 0, 17), receiver = new p.MessageReceiver(); + assert.deepEqual(receiver.receive('L', frames[0], 0), []); + assert.deepEqual(receiver.receive('R', frames[1], 1), []); + assert.deepEqual(receiver.receive('L', frames[1], 6000), []); + assert.equal(receiver.receive('L', p.concat(...frames), 6001).length, 1); +}); + +test('full-frame RLE splits 65535-pixel runs and preserves opaque near-black', () => { + const packed = p.packGray4(new Uint8Array(640 * 480).fill(1), 640, 480); + assert.ok(packed.every(v => v === 0)); + const encoded = p.rle4(packed); + let count = 0, offset = 0; + while (offset < encoded.length) { + const token = encoded[offset++]; let run = token >>> 4; + if (!run) { run = encoded[offset++]; if (!run) { run = encoded[offset] | (encoded[offset + 1] << 8); offset += 2; } } + assert.equal(token & 15, 0); count += run; + } + assert.equal(count, 640 * 480); + assert.deepEqual(inflate(deflate(encoded)), encoded); +}); + +test('iOS mapping requires the full advertised MAC, never just a name suffix', () => { + const raw = { identifier: '8A2DD845-B208-F292-EB77-F4645C5AC42F', name: 'Even G2_32_L_B6ECE0', + manufacturerData: '4552' + Buffer.from('S211GBBC180304').toString('hex') + 'e0ecb61412e001', rssi: -45, connectable: true }; + const device = identifyIosPeripheral(raw); + assert.equal(device.address, 'E0:12:14:B6:EC:E0'); assert.equal(device.role, 'left'); + assert.equal(device.identifier, raw.identifier); assert.equal(device.serial, 'S211GBBC180304'); + assert.equal(identifyIosPeripheral({ ...raw, manufacturerData: '' }), null); + assert.equal(identifyIosPeripheral({ ...raw, identifier: 'not-a-uuid' }), null); + assert.match(deviceAddressError({ left: device.address, right: device.address, ring: '' }), /different/); + assert.equal(deviceAddressError({ left: device.address, right: 'AA:BB:CC:DD:EE:FF', ring: '' }), null); +}); + +test('ring and arm input decoders preserve gesture and source', () => { + assert.equal(p.decodeRingInput(new Uint8Array([255, 4, 2])).eventType, 3); + assert.equal(p.decodeRingInput(new Uint8Array([255, 5, 0])).eventType, 2); + assert.equal(p.decodeRingInput(new Uint8Array([255, 5, 2])).eventType, 1); + assert.equal(p.decodeRingInput(new Uint8Array([0, 9, 97, 0, 8, 0, 0, 0, 0, 0, 0])).eventType, 10); + assert.equal(p.decodeRingInput(new Uint8Array([255, 4, 99])), null); + const payload = p.bytes(13, p.bytes(3, p.concat(p.integer(1, 9), p.integer(2, 3)))); + assert.equal(p.decodeGlassesInput({ sid: 224, flag: 1, payload }).eventSource, 3); + assert.equal(p.decodeGlassesInput({ sid: 224, flag: 32, payload }), null); +}); + +class FakeTransport { + events = new Set(); receiver = new p.MessageReceiver(); sent = []; subscriptions = []; closed = []; + hold = false; held = []; stock = false; + onEvent(fn) { this.events.add(fn); return () => this.events.delete(fn); } + emit(event) { for (const fn of [...this.events]) fn(event); } + async resolveDevices() { return { left: 'L', right: 'R', ring: 'ring' }; } + async connect(id) { return { characteristics: id === 'ring' ? p.RING_NOTIFY : [p.G2_WRITE, p.G2_NOTIFY], maxWrite: 185 }; } + async subscribe(id, characteristic) { this.subscriptions.push([id, characteristic]); } + disconnect(id) { this.closed.push(id); this.emit({ kind: 'disconnected', identifier: id }); } + stopScan() {} + ack(id, message, overrides = {}) { + let body = new Uint8Array(); + if (message.sid === p.SID.auth) body = p.bytes(3, new Uint8Array()); + if (message.sid === p.SID.settings) body = p.concat(p.bytes(4, p.concat(string(5, '2.2.9.22'), string(6, '2.2.9.22'), p.integer(12, 90), p.integer(13, 0))), string(100, this.stock ? '' : 'EVENCFW/18 img640 fbguard wearnotify cleanup11')); + const payload = p.concat(p.integer(1, message.command), p.integer(2, overrides.magic ?? message.magic), body); + const data = hex(p.concat(...p.frameMessage(payload, overrides.sid ?? message.sid, 0, 12))); + // The right/master link also carries ACKs for image writes to L. + this.emit({ kind: 'notification', identifier: overrides.identifier ?? 'R', characteristic: p.G2_NOTIFY, data }); + } + async write(id, characteristic, frame) { + assert.equal(characteristic, p.G2_WRITE); assert.ok(frame.length <= 185); + for (const message of this.receiver.receive(id, frame)) { + this.sent.push({ id, message }); + if (!message.magic) continue; + if (this.hold) this.held.push({ id, message }); + else queueMicrotask(() => this.ack(id, message)); + } + } +} +const addresses = { right: 'AA:BB:CC:DD:EE:01', left: 'AA:BB:CC:DD:EE:02', ring: '' }; +async function until(predicate) { + for (let i = 0; i < 100; i++) { if (predicate()) return; await new Promise(resolve => setTimeout(resolve, 5)); } + assert.fail('Timed out waiting for test state'); +} +function harness(t, transport = new FakeTransport()) { + const states = [], input = []; + const session = new GlassesSession(transport, deflate, state => states.push(state), event => input.push(event)); + t.after(async () => { transport.hold = false; await session.stop(); }); + return { transport, session, states, input }; +} +test('session authenticates both arms, checks firmware and sends acknowledged display data to L', async t => { + const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' }); + assert.equal(h.session.state.phase, 'connected'); assert.equal(h.session.state.ring, true); + assert.equal(h.session.state.battery, 90); + assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']); + h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1); + const image = h.transport.sent.find(s => s.message.sid === 224 && s.message.command === 3); + assert.equal(image.id, 'L'); + const fragment = p.readBytes(image.message.payload, 5), payload = p.readBytes(fragment, 8); + assert.equal(payload[0], 6); assert.ok(inflate(payload.slice(1)).length > 0); + await h.session.stop(); + assert.equal(h.session.state.phase, 'disconnected'); + assert.ok(h.transport.sent.some(s => s.message.command === 3 && p.readBytes(p.readBytes(s.message.payload, 5), 8)[0] === 11)); +}); +test('stock firmware is rejected before framebuffer/layout/image commands', async t => { + const transport = new FakeTransport(); transport.stock = true; + const h = harness(t, transport); await h.session.start(addresses); + assert.equal(h.session.state.phase, 'error'); assert.match(h.session.state.status, /modified firmware/); + assert.ok(!transport.sent.some(s => s.message.sid === p.SID.hub)); +}); +test('disconnect cancels in-flight authentication and ignores late acknowledgements', async t => { + const h = harness(t); h.transport.hold = true; + const start = h.session.start(addresses); await until(() => h.transport.held.length === 2); + await h.session.stop(); + for (const held of h.transport.held) h.transport.ack(held.id, held.message); + await start; + assert.equal(h.session.state.phase, 'disconnected'); + assert.ok(!h.states.some(s => s.phase === 'connected')); + assert.equal(h.transport.sent.length, 2); +}); +test('wrong peer, service or magic cannot satisfy a pending request; timeout rejects it', async t => { + const h = harness(t); await h.session.start(addresses); h.transport.hold = true; + const request = h.session.request('right', p.SID.hub, p.heartbeat, 'test heartbeat', 30); + const rejected = assert.rejects(request, /not acknowledged/); + await until(() => h.transport.held.length > 0); + const held = h.transport.held[0]; + h.transport.ack(held.id, held.message, { identifier: 'stranger' }); + h.transport.ack(held.id, held.message, { sid: 9 }); + h.transport.ack(held.id, held.message, { magic: held.message.magic + 1 }); + await rejected; +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 0bf5d080..8d1b49d9 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -11,6 +11,9 @@ "outDir": "../.test-build" }, "include": [ + "../app/g2/ble-protocol.ts", + "../app/g2/ios-peripheral-identity.ts", + "../app/g2/glasses-session.ts", "../app/graphics/surface-compositor.ts", "../app/phone-ui/phone-gestures.ts", "../app/g2/glasses-hardware-identity.ts",