Files
faceclaw/tests/ios-ble.test.cjs
T

259 lines
15 KiB
JavaScript

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 responseCommand = message.sid === p.SID.launch ? 1 : message.sid === p.SID.hub && message.command === 0 ? 1 : message.sid === p.SID.hub && message.command === 3 ? 4 : message.command;
const payload = p.concat(p.integer(1, responseCommand), p.integer(2, overrides.magic ?? message.magic), body);
const data = hex(p.concat(...p.frameMessage(payload, overrides.sid ?? message.sid, 0, 12)));
// The right/master link also carries ACKs for image writes to L.
this.emit({ kind: 'notification', identifier: overrides.identifier ?? 'R', characteristic: p.G2_NOTIFY, data });
}
async write(id, characteristic, frame) {
assert.equal(characteristic, p.G2_WRITE); assert.ok(frame.length <= 185);
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.equal(h.session.state.charging, false);
assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']);
h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1);
const image = h.transport.sent.find(s => s.message.sid === 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('one unavailable R1 notification channel does not discard the usable channel', async t => {
const transport = new FakeTransport();
const subscribe = transport.subscribe.bind(transport);
transport.subscribe = async (id, uuid) => {
if (uuid === p.RING_NOTIFY[0]) throw new Error('CCCD timed out');
return subscribe(id, uuid);
};
const h = harness(t, transport); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
assert.equal(h.session.state.ring, true);
assert.ok(transport.subscriptions.some(([id, uuid]) => id === 'ring' && uuid === p.RING_NOTIFY[1]));
assert.ok(!transport.closed.includes('ring'));
});
test('disconnect cancels in-flight authentication and ignores late acknowledgements', async t => {
const h = harness(t); h.transport.hold = true;
const start = h.session.start(addresses); await until(() => h.transport.held.length === 2);
await h.session.stop();
for (const held of h.transport.held) h.transport.ack(held.id, held.message);
await start;
assert.equal(h.session.state.phase, 'disconnected');
assert.ok(!h.states.some(s => s.phase === 'connected'));
assert.equal(h.transport.sent.length, 2);
});
test('wrong peer, service or magic cannot satisfy a pending request; timeout rejects it', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const request = h.session.request('right', p.SID.hub, p.heartbeat, 'test heartbeat', 30);
const rejected = assert.rejects(request, /not acknowledged/);
await until(() => h.transport.held.length > 0);
const held = h.transport.held[0];
h.transport.ack(held.id, held.message, { identifier: 'stranger' });
h.transport.ack(held.id, held.message, { sid: 9 });
h.transport.ack(held.id, held.message, { magic: held.message.magic + 1 });
await rejected;
});
test('ring disconnect retries independently and stop cancels further retries', async t => {
const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
const armMessages = h.transport.sent.length;
h.transport.emit({ kind: 'disconnected', identifier: 'ring', message: 'Link lost' });
assert.equal(h.session.state.phase, 'connected'); assert.equal(h.session.state.ring, false);
assert.notEqual(h.session.ringRetryTimer, null);
await new Promise(resolve => setTimeout(resolve, 2100));
assert.equal(h.session.state.ring, true);
assert.equal(h.transport.sent.length, armMessages);
assert.equal(h.transport.subscriptions.filter(([id]) => id === 'ring').length, 4);
h.transport.emit({ kind: 'disconnected', identifier: 'ring' });
await h.session.stop();
assert.equal(h.session.ringRetryTimer, null);
});
test('a BLE wake services overdue heartbeat and lease without waiting for a suspended poll timer', async t => {
const h = harness(t); await h.session.start(addresses);
clearTimeout(h.session.timer); h.session.timer = null;
h.session.lastHeartbeat = Date.now() - 12_000;
h.session.lastLease = Date.now() - 60_000;
const before = h.transport.sent.length;
const data = hex(p.concat(...p.frameMessage(p.integer(1, 6), p.SID.auth, 1, 22)));
h.transport.emit({ kind: 'notification', identifier: 'L', characteristic: p.G2_NOTIFY, data });
// pump starts in the notification callback, before any timers can fire.
assert.equal(h.session.pumping, true);
await until(() => !h.session.pumping);
const sent = h.transport.sent.slice(before);
assert.equal(sent.filter(s => s.message.sid === p.SID.hub && s.message.command === 12).length, 1);
assert.deepEqual(sent.filter(s => s.message.sid === p.SID.settings).map(s => s.id).sort(), ['L', 'R']);
assert.equal(h.session.state.phase, 'connected');
assert.deepEqual(h.transport.closed, []);
await h.session.stop();
const count = h.transport.sent.length;
h.session.wake();
h.transport.emit({ kind: 'notification', identifier: 'L', characteristic: p.G2_NOTIFY, data });
assert.equal(h.transport.sent.length, count);
assert.equal(h.session.state.phase, 'disconnected');
});
test('glasses mic uses Android wire control and only routes bound-arm audio during capture', async t => {
assert.equal(hex(p.audioControl(101, true)), '080f10659201020801');
assert.equal(hex(p.audioControl(101, false)), '080f10659201020800');
const h = harness(t), audio = [];
await h.session.start(addresses);
const send = id => h.transport.emit({ kind: 'notification', identifier: id, characteristic: p.G2_RENDER_NOTIFY, data: '010203' });
send('R'); assert.equal(audio.length, 0);
await h.session.setMicrophone(true, bytes => audio.push(hex(bytes)));
assert.ok(h.transport.subscriptions.some(([id, c]) => id === 'R' && c === p.G2_RENDER_NOTIFY));
assert.ok(h.transport.subscriptions.some(([id, c]) => id === 'L' && c === p.G2_RENDER_NOTIFY));
send('stranger'); send('ring'); send('L'); send('R');
assert.deepEqual(audio, ['010203', '010203']);
const disabling = h.session.setMicrophone(false);
send('R'); assert.equal(audio.length, 2); // Immediate, before the disable ACK.
await disabling;
assert.deepEqual(h.transport.sent.filter(s => s.message.command === 15).map(s =>
[s.id, p.readInteger(p.readBytes(s.message.payload, 18), 1)]), [['R', 1], ['R', 0]]);
});
test('release while microphone enable awaits ACK sends disable last and rejects further audio', async t => {
const h = harness(t); await h.session.start(addresses);
h.transport.hold = true;
let packets = 0;
const enabling = h.session.setMicrophone(true, () => packets++);
await until(() => h.transport.held.some(s => s.message.command === 15));
const disabling = h.session.setMicrophone(false);
h.transport.emit({ kind: 'notification', identifier: 'R', characteristic: p.G2_RENDER_NOTIFY, data: '01' });
assert.equal(packets, 0);
h.transport.hold = false;
for (const s of h.transport.held) h.transport.ack(s.id, s.message);
await Promise.all([enabling, disabling]);
const mic = h.transport.sent.filter(s => s.message.command === 15);
assert.equal(p.readInteger(p.readBytes(mic.at(-1).message.payload, 18), 1), 0);
});