mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Faceclaw support for G1s
Relies on the glasses having the firmware from https://github.com/jimrandomh/scouter_kit, based on Mafik's Scouter Kit.
This commit is contained in:
1 parent
f803f4212c
commit
67aff19923
27 files changed
+1902
-45
No files matched your search
@@ -46,6 +46,8 @@ class AndroidSessionLink(private val bleManager: FaceclawBleManager) : SessionLi
|
||||
|
||||
override fun isBonded(address: String): Boolean = bleManager.isBonded(address)
|
||||
|
||||
override fun requestBond(address: String) = bleManager.requestBond(address)
|
||||
|
||||
override fun prepareBenchmarkLink(address: String, mode: Int) = bleManager.prepareBenchmarkLink(address, mode)
|
||||
|
||||
override fun recordDisplayFrameSent() = FaceclawBleManager.recordDisplayFrameSent()
|
||||
|
||||
@@ -208,6 +208,9 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
|
||||
|
||||
fun isRayNeoIo(): Boolean = core.isRayNeoIo()
|
||||
|
||||
/** Drive an Even G1 on g1v0 (two arms, as a G2 pair's addresses). */
|
||||
fun useG1() = core.useG1()
|
||||
|
||||
fun start() {
|
||||
if (core.start()) {
|
||||
activeInstance = this
|
||||
|
||||
@@ -160,6 +160,21 @@ class FaceclawBleManager(context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Start bonding, without waiting: the OS may ask the user, and [isBonded] tells when it is done. */
|
||||
fun requestBond(address: String?) {
|
||||
if (address == null || address.trim().isEmpty()) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
val device = bluetoothAdapter.getRemoteDevice(address)
|
||||
if (device.bondState == BluetoothDevice.BOND_NONE) {
|
||||
Log.i(TAG, "requestBond " + address + ": " + device.createBond())
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "requestBond failed: " + address, t)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Android's bond state for this address (BluetoothDevice.BOND_NONE /
|
||||
* BOND_BONDING / BOND_BONDED), or -1 when it cannot be determined.
|
||||
|
||||
@@ -141,7 +141,9 @@ class FaceclawDeviceDiscovery(context: Context) {
|
||||
val upper = name.uppercase()
|
||||
// A bare "R1" substring also matches unrelated hardware ("Oppo Enco
|
||||
// R1"), and TS only classifies rings from the stock name prefix.
|
||||
return upper.contains("G2") || upper.startsWith("EVEN R1") || upper.startsWith("RAYNEO")
|
||||
// Even G1: its stock name, or g1v0's G1v0_L / G1v0S_R (safe mode).
|
||||
return upper.contains("G2") || upper.startsWith("EVEN R1") || upper.startsWith("RAYNEO") ||
|
||||
upper.startsWith("EVEN G1") || upper.startsWith("G1V0")
|
||||
}
|
||||
|
||||
private fun buildJson(
|
||||
|
||||
@@ -32,6 +32,9 @@ export const EVEN_COMPANY_IDENTIFIER = 0x5245;
|
||||
|
||||
export type EvenDeviceRole = "left" | "right" | "ring" | "rayneo-io";
|
||||
|
||||
/** The firmware an Even G1 arm's name shows: Even's, or g1v0 (in safe mode: only updates work). */
|
||||
export type G1Firmware = "stock" | "g1v0" | "g1v0-safe";
|
||||
|
||||
/** True when the manufacturer data opens with Even's `"ER"` marker. */
|
||||
export function hasEvenManufacturerSignature(mfg: Uint8Array): boolean {
|
||||
return mfg.length >= 2 && mfg[0] === 0x45 && mfg[1] === 0x52;
|
||||
@@ -71,6 +74,42 @@ export function extractG2AdvertisementFlag(mfg: Uint8Array): number | null {
|
||||
return mfg[22]!;
|
||||
}
|
||||
|
||||
/**
|
||||
* ## Even G1 arm
|
||||
*
|
||||
* On Even's firmware the name is `Even G1_<token>_<L|R>_<hex6>` and the manufacturer data `[01 right | 02 left]` +
|
||||
* the ASCII serial, zero-padded (the first two bytes read as a "company id" that differs per arm). On g1v0 the name
|
||||
* is `G1v0_L` or `G1v0_R` (`G1v0S_…` in safe mode) and the scan response holds company 0xFFFF, a format byte (1), a
|
||||
* flags byte, and the pair ID in ASCII: the frame serial, the same on both arms.
|
||||
*/
|
||||
export function g1FromName(name: string | null | undefined): { side: "left" | "right"; firmware: G1Firmware } | null {
|
||||
if (!name) return null;
|
||||
const g1v0 = /^G1V0(S?)_([LR])$/i.exec(name.trim());
|
||||
if (g1v0) {
|
||||
return {
|
||||
side: g1v0[2]!.toUpperCase() === "L" ? "left" : "right",
|
||||
firmware: g1v0[1] ? "g1v0-safe" : "g1v0",
|
||||
};
|
||||
}
|
||||
if (!/^EVEN G1_/i.test(name.trim())) return null;
|
||||
const side = g2SideFromName(name);
|
||||
return side ? { side, firmware: "stock" } : null;
|
||||
}
|
||||
|
||||
/** The pair's serial from an arm's manufacturer data, on either firmware; null when absent. */
|
||||
export function extractG1Serial(mfg: Uint8Array, firmware: G1Firmware): string | null {
|
||||
const start = firmware === "stock" ? 1 : mfg.length >= 4 && mfg[0] === 0xff && mfg[1] === 0xff && mfg[2] === 1 ? 4 : -1;
|
||||
if (start < 0 || mfg.length <= start) return null;
|
||||
let serial = "";
|
||||
for (let i = start; i < mfg.length; i++) {
|
||||
const byte = mfg[i]!;
|
||||
if (byte === 0) break;
|
||||
if (byte < 0x20 || byte > 0x7e) return null;
|
||||
serial += String.fromCharCode(byte);
|
||||
}
|
||||
return serial.length ? serial : null;
|
||||
}
|
||||
|
||||
/** RayNeo iO manufacturer data (company 0x5716, little-endian first): `[07][MAC x6]...`, outside pairing mode. */
|
||||
export const RAYNEO_COMPANY_IDENTIFIER = 0x5716;
|
||||
|
||||
@@ -218,6 +257,8 @@ export type EvenAdvertisement = {
|
||||
manufacturerData: string;
|
||||
/** Why an otherwise Even-looking advertisement could not be fully parsed. */
|
||||
note: string | null;
|
||||
/** Set on an Even G1 arm (role left or right): its firmware. */
|
||||
g1Firmware?: G1Firmware;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -241,6 +282,30 @@ export function classifyAdvertisement(raw: RawAdvertisement): EvenAdvertisement
|
||||
const rayneoMac = extractRayNeoMac(mfg);
|
||||
const looksLikeRayNeo = upper.startsWith("RAYNEO") || rayneoMac !== null;
|
||||
|
||||
const g1 = g1FromName(name);
|
||||
if (g1) {
|
||||
const serial = extractG1Serial(mfg, g1.firmware);
|
||||
return {
|
||||
address: normalizeMacAddress(raw.address),
|
||||
name,
|
||||
role: g1.side,
|
||||
serial,
|
||||
embeddedMac: null,
|
||||
embeddedMacMismatch: false,
|
||||
flag: null,
|
||||
ringSerialSuffix: null,
|
||||
rssi: raw.rssi,
|
||||
txPower: raw.txPower,
|
||||
connectable: raw.connectable,
|
||||
bonded: raw.bonded,
|
||||
source: raw.source,
|
||||
seenAtMs: raw.seenAtMs,
|
||||
manufacturerData: raw.manufacturerData ?? "",
|
||||
note: serial ? null : "no serial in the advertisement (it may still be arriving)",
|
||||
g1Firmware: g1.firmware,
|
||||
};
|
||||
}
|
||||
|
||||
if (!looksLikeRing && !looksLikeG2 && !hasSignature && !looksLikeRayNeo) return null;
|
||||
|
||||
if (looksLikeRayNeo && !hasSignature) {
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
type ProximityEstimate,
|
||||
type ProximityZone,
|
||||
} from "./ble-proximity";
|
||||
import { classifyAdvertisement, normalizeMacAddress, type EvenAdvertisement, type RawAdvertisement } from "./even-advertisement";
|
||||
import { classifyAdvertisement, normalizeMacAddress, type EvenAdvertisement, type G1Firmware, type RawAdvertisement } from "./even-advertisement";
|
||||
import { GENERIC_G2_IMAGE_PATH, glassesImagePath, R1_IMAGE_PATH } from "./glasses-artwork";
|
||||
import {
|
||||
colorwayDisplayName,
|
||||
@@ -48,6 +48,8 @@ export type GlassesPairCandidate = {
|
||||
rssi: number | null;
|
||||
proximity: ProximityEstimate | null;
|
||||
lastSeenMs: number;
|
||||
/** An Even G1 pair: the arms' firmware, the "lower" of the two (stock, then safe mode). Absent on a G2. */
|
||||
g1Firmware?: G1Firmware;
|
||||
/**
|
||||
* Set when this lone arm and a lone arm of the other side carry different
|
||||
* serials — the mixed-pair situation the serial check exists to name.
|
||||
@@ -187,7 +189,8 @@ export class DiscoveryAggregator {
|
||||
for (const record of this.byAddress.values()) {
|
||||
if (record.role === "ring" || record.role === "rayneo-io") continue;
|
||||
const canonical = serialKey(record.serial);
|
||||
const key = canonical ? `serial:${canonical}` : `arm:${record.address}`;
|
||||
const family = record.g1Firmware ? "g1:" : "";
|
||||
const key = canonical ? `${family}serial:${canonical}` : `arm:${record.address}`;
|
||||
const group = groups.get(key) ?? { left: null, right: null };
|
||||
const slot = record.role === "left" ? "left" : "right";
|
||||
const incumbent = group[slot];
|
||||
@@ -208,7 +211,9 @@ export class DiscoveryAggregator {
|
||||
pairs.push({
|
||||
id: serialKey(serial) ?? `arm:${any.address}`,
|
||||
serial,
|
||||
identity: GlassesHardwareIdentity.decode(serial),
|
||||
// Even's G2 serial scheme; a G1's says nothing to it.
|
||||
identity: any.g1Firmware ? null : GlassesHardwareIdentity.decode(serial),
|
||||
...(any.g1Firmware ? { g1Firmware: g1PairFirmware(left, right) } : {}),
|
||||
left: left ? stripSmoothing(left) : null,
|
||||
right: right ? stripSmoothing(right) : null,
|
||||
completeness: left && right ? "complete" : left ? "left-only" : "right-only",
|
||||
@@ -367,7 +372,50 @@ function embeddedMacMismatchWarning(subject: string, ad: EvenAdvertisement | nul
|
||||
return `${subject} advertised address ${ad.embeddedMac} differs from its radio address ${ad.address}.`;
|
||||
}
|
||||
|
||||
/** The pair can only do what its less capable arm can. */
|
||||
function g1PairFirmware(left: EvenAdvertisement | null, right: EvenAdvertisement | null): G1Firmware {
|
||||
const firmwares = [left?.g1Firmware, right?.g1Firmware];
|
||||
if (firmwares.includes("stock")) return "stock";
|
||||
return firmwares.includes("g1v0-safe") ? "g1v0-safe" : "g1v0";
|
||||
}
|
||||
|
||||
export function g1Row(pair: GlassesPairCandidate, context: RowContext = {}): PairingRowPresentation {
|
||||
const isPreviouslyPairedRow = isPreviouslyPaired(pair.serial, context.pairedSerial);
|
||||
const isNearest = !!context.nearestId && context.nearestId === pair.id;
|
||||
const warnings: string[] = [];
|
||||
if (pair.mismatchWarning) warnings.push(pair.mismatchWarning);
|
||||
let armsSummary: string;
|
||||
let canSelect = false;
|
||||
if (pair.g1Firmware === "stock") {
|
||||
armsSummary = "Needs the Faceclaw firmware for the G1, which is installed from a computer";
|
||||
} else if (pair.g1Firmware === "g1v0-safe") {
|
||||
armsSummary = "In safe mode: the firmware restarted repeatedly. Reinstall it from a computer";
|
||||
} else if (pair.completeness !== "complete") {
|
||||
armsSummary = pair.completeness === "left-only"
|
||||
? "Only the left arm has been seen — waiting for the right"
|
||||
: "Only the right arm has been seen — waiting for the left";
|
||||
} else {
|
||||
armsSummary = "Left + right arms found. To pair, keep them in the case with the lid open";
|
||||
canSelect = true;
|
||||
}
|
||||
return {
|
||||
id: pair.id,
|
||||
kind: "glasses",
|
||||
modelTitle: "Even Realities G1",
|
||||
deviceName: pair.serial ?? (pair.left ?? pair.right)!.name,
|
||||
variantSummary: null,
|
||||
colorway: null,
|
||||
swatchHex: null,
|
||||
imagePath: GENERIC_G2_IMAGE_PATH,
|
||||
hasVariantImage: false,
|
||||
...presentationBase(pair.proximity, isNearest, isPreviouslyPairedRow, warnings),
|
||||
armsSummary,
|
||||
canSelect,
|
||||
};
|
||||
}
|
||||
|
||||
export function glassesRow(pair: GlassesPairCandidate, context: RowContext = {}): PairingRowPresentation {
|
||||
if (pair.g1Firmware) return g1Row(pair, context);
|
||||
const identity = pair.identity;
|
||||
const imagePath = glassesImagePath(identity);
|
||||
const isPreviouslyPairedRow = isPreviouslyPaired(pair.serial, context.pairedSerial);
|
||||
|
||||
@@ -260,6 +260,7 @@ export class FaceclawCommunicatorBridge {
|
||||
);
|
||||
// The shared session halts on any other firmware ("incompatible-firmware").
|
||||
this.communicator.setRequiredFirmwareRevisionRevision(REQUIRED_FACECLAW_FIRMWARE_VERSION);
|
||||
if (addresses.glassesType === "g1") this.communicator.useG1();
|
||||
// Guarded: an older framework build may not export it yet.
|
||||
if (addresses.ringRole && typeof this.communicator.setDirectRingRoleRole === "function") {
|
||||
this.communicator.setDirectRingRoleRole(addresses.ringRole);
|
||||
|
||||
@@ -184,6 +184,8 @@ export class FaceclawCommunicatorBridge {
|
||||
this.communicator.setRequiredFirmwareRevision(REQUIRED_FACECLAW_FIRMWARE_VERSION);
|
||||
// RayNeo iO: one link (its address in both arm slots), its own handshake and page lease.
|
||||
if (addresses.glassesType === "rayneo-io") this.communicator.useRayNeoIo();
|
||||
// Even G1 on g1v0: two links, like a G2's arms, but frames composed here and sent to each arm.
|
||||
if (addresses.glassesType === "g1") this.communicator.useG1();
|
||||
// Which R1 role the direct ring link claims; only matters with a ring address.
|
||||
if (addresses.ringRole) this.communicator.setDirectRingRole(addresses.ringRole);
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawBleCommunicatorListener({
|
||||
|
||||
@@ -186,9 +186,9 @@ export class PairingViewModel extends Observable {
|
||||
|
||||
get instructions(): string {
|
||||
return (
|
||||
"Select your G2 glasses or RayNeo iO, and your R1 ring if you have one, below. Wear the glasses or take them out of the case, " +
|
||||
"and keep the glasses and phone nearby. If your glasses are not detected, make sure no other " +
|
||||
"apps are connected."
|
||||
"Select your G2 or G1 glasses or RayNeo iO, and your R1 ring if you have one, below. Wear the glasses or take them out of the case " +
|
||||
"(a G1 pairs only in its case, with the lid open), and keep the glasses and phone nearby. If your glasses are not detected, " +
|
||||
"make sure no other apps are connected."
|
||||
);
|
||||
}
|
||||
|
||||
@@ -358,7 +358,8 @@ export class PairingViewModel extends Observable {
|
||||
ring: ringAddress,
|
||||
};
|
||||
saveDeviceAddresses(addresses);
|
||||
if (pair) glassesTypeSetting.set("g2");
|
||||
const g1 = !!pair?.g1Firmware;
|
||||
if (pair) glassesTypeSetting.set(g1 ? "g1" : "g2");
|
||||
if (pair) {
|
||||
savePairedGlassesIdentity({
|
||||
serial: pair.serial ?? "",
|
||||
@@ -385,6 +386,12 @@ export class PairingViewModel extends Observable {
|
||||
this.dispose();
|
||||
if (this.onboarding) {
|
||||
setPreviewOnlyMode(false);
|
||||
if (g1) {
|
||||
// The G2 firmware check and flash pages don't apply: a G1's firmware is installed from a
|
||||
// computer, and the session reports incompatible firmware if it lacks the display path.
|
||||
finishOnboardingNavigation();
|
||||
return;
|
||||
}
|
||||
// Continue the onboarding chain: check the glasses' firmware next.
|
||||
Frame.topmost()?.navigate({ moduleName: "phone-ui/onboarding-firmware-check-page" });
|
||||
return;
|
||||
|
||||
@@ -385,14 +385,14 @@ export const distanceUnitSetting = new ConfigSettingEnum<DistanceUnit>({
|
||||
export type AppSwitcherPosition = "left" | "right" | "bottom" | "popup";
|
||||
|
||||
/** Which glasses the saved addresses belong to (set by pairing; see g2/device-addresses.ts). */
|
||||
export type GlassesType = "g2" | "rayneo-io";
|
||||
export type GlassesType = "g2" | "rayneo-io" | "g1";
|
||||
|
||||
export const glassesTypeSetting = new ConfigSettingEnum<GlassesType>({
|
||||
id: "glasses-type",
|
||||
label: "Glasses",
|
||||
storageKey: "deviceType", // graphics/screen-size.ts GLASSES_TYPE_KEY reads it directly
|
||||
defaultValue: "g2",
|
||||
values: ["g2", "rayneo-io"],
|
||||
values: ["g2", "rayneo-io", "g1"],
|
||||
reportInAnalytics: false,
|
||||
});
|
||||
|
||||
@@ -909,11 +909,11 @@ export const ringConnectionModeSetting = new ConfigSettingEnum<RingConnectionMod
|
||||
|
||||
/**
|
||||
* The ring connection in effect: the setting with G2 glasses; Direct with a
|
||||
* RayNeo iO, which cannot relay a ring, so the phone holds the ring's
|
||||
* RayNeo iO or Even G1, which cannot relay a ring, so the phone holds the ring's
|
||||
* glasses role itself (gestures, battery; health in brief phone-role windows).
|
||||
*/
|
||||
export function effectiveRingConnectionMode(): RingConnectionMode {
|
||||
return glassesTypeSetting.get() === "rayneo-io" ? "direct" : ringConnectionModeSetting.get();
|
||||
return glassesTypeSetting.get() !== "g2" ? "direct" : ringConnectionModeSetting.get();
|
||||
}
|
||||
|
||||
// "whisper" (no "onboard-" prefix) is OpenAI's CLOUD realtime model
|
||||
|
||||
@@ -608,13 +608,13 @@ function readBundledDoc(fileName: string): string {
|
||||
*/
|
||||
function ringConnectionMenuItem(): MenuItem {
|
||||
const item = enumSettingMenuItem(ringConnectionModeSetting);
|
||||
const io = () => glassesTypeSetting.get() === "rayneo-io";
|
||||
const io = () => glassesTypeSetting.get() !== "g2";
|
||||
return {
|
||||
...item,
|
||||
disabled: io,
|
||||
render: (args) => {
|
||||
if (!io()) return item.render?.(args);
|
||||
drawRightValueMenuItem(args.image, getDefaultSmallFont(), args.x, args.y, args.width, item.label, "Direct (RayNeo iO)");
|
||||
drawRightValueMenuItem(args.image, getDefaultSmallFont(), args.x, args.y, args.width, item.label, glassesTypeSetting.get() === "g1" ? "Direct (Even G1)" : "Direct (RayNeo iO)");
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* One lens's update on an Even G1: the rectangles that differ from what the arm's frame holds, as RLE bbox calls
|
||||
* in draw messages. The transport's deflate runs over these messages, as on the G2; there is no resource cache,
|
||||
* so everything visible arrives as pixels.
|
||||
*/
|
||||
object G1FramePlanner {
|
||||
/** The firmware's limit on a decoded message is 16384 bytes (scouter_kit fc.c MAX_MESSAGE). */
|
||||
const val MAX_MESSAGE = 16000
|
||||
|
||||
/** A big rectangle goes out in bands, so that one noisy area doesn't need a long binary split. */
|
||||
private const val BAND_ROWS = 32
|
||||
private const val MAX_RECTS = ConnectionOptions.MULTI_RECT_MAX_RECTS
|
||||
|
||||
/** [previous] null or of another size: a full frame. Empty when nothing changed. */
|
||||
fun plan(previous: ByteArray?, next: ByteArray, width: Int, height: Int): List<ByteArray> {
|
||||
val stride = (width + 1) / 2
|
||||
require(next.size == stride * height)
|
||||
val rects = changedRects(previous, next, width, height)
|
||||
if (rects.isEmpty()) return emptyList()
|
||||
val calls = ArrayList<ByteArray>()
|
||||
for (r in rects) {
|
||||
var top = r[1]
|
||||
val bottom = r[1] + r[3]
|
||||
while (top < bottom) {
|
||||
val rows = minOf(BAND_ROWS, bottom - top)
|
||||
addRect(calls, next, stride, r[0], top, r[2], rows)
|
||||
top += rows
|
||||
}
|
||||
}
|
||||
return DrawProtocol.messages(calls, MAX_MESSAGE)
|
||||
}
|
||||
|
||||
private fun changedRects(previous: ByteArray?, next: ByteArray, width: Int, height: Int): List<IntArray> {
|
||||
if (previous == null || previous.size != next.size) return listOf(intArrayOf(0, 0, width, height))
|
||||
BleImageOptimizer.computeChangedRects(previous, next, width, height, MAX_RECTS)?.let { return it }
|
||||
return listOfNotNull(BleImageOptimizer.computeChangedBox(previous, next, width, height))
|
||||
}
|
||||
|
||||
/** Splits until the call fits a message; a single pixel always does. */
|
||||
private fun addRect(calls: MutableList<ByteArray>, packed: ByteArray, stride: Int, x: Int, y: Int, w: Int, h: Int) {
|
||||
val call = DrawProtocol.bbox(packed, stride, x, y, w, h)
|
||||
if (call.size + MESSAGE_OVERHEAD <= MAX_MESSAGE) {
|
||||
calls.add(call)
|
||||
} else if (h > 1) {
|
||||
addRect(calls, packed, stride, x, y, w, h / 2)
|
||||
addRect(calls, packed, stride, x, y + h / 2, w, h - h / 2)
|
||||
} else {
|
||||
addRect(calls, packed, stride, x, y, w / 2, h)
|
||||
addRect(calls, packed, stride, x + w / 2, y, w - w / 2, h)
|
||||
}
|
||||
}
|
||||
|
||||
/** The message type, the call count and the call's length (DrawProtocol.sequence). */
|
||||
private const val MESSAGE_OVERHEAD = 5
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.jvm.JvmField
|
||||
|
||||
/** The GATT service of g1v0, the firmware of an Even G1 (scouter_kit g1_firmware/docs/VM.md §6.1). */
|
||||
object G1Ble {
|
||||
const val CTRL_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4402"
|
||||
const val EVT_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4403"
|
||||
/** Readable and writable only on an encrypted link: a write with response makes the OS pair, then proves the link is encrypted. */
|
||||
const val PAIR_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4404"
|
||||
/** Faceclaw's records (VM.md §6.11). */
|
||||
const val FC_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4405"
|
||||
const val REQUESTED_MTU = 247
|
||||
}
|
||||
|
||||
/**
|
||||
* The wire formats of an Even G1 on g1v0: control requests and events (VM.md §6.3, §6.4), which Faceclaw uses as they
|
||||
* are, and the display path's packets and answers (§6.11). No VM instruction is sent; the records inside the packets
|
||||
* are [CfwTransport]'s, the same as on the G2.
|
||||
*/
|
||||
object G1Protocol {
|
||||
const val OP_HELLO = 0x01
|
||||
const val OP_HEALTH_OK = 0x07
|
||||
const val OP_PROPS = 0x0A
|
||||
const val OP_EVENTS = 0x14
|
||||
const val OP_FC_INFO = 0x1D
|
||||
|
||||
const val EV_INPUT = 0x07
|
||||
const val EV_FC_ACK = 0x08
|
||||
private const val EV_REPLY = 0x80
|
||||
private const val EVENT_HEADER = 9
|
||||
private const val OPT_START = 0x80
|
||||
private const val OPT_END = 0x40
|
||||
private const val MAX_PACKET = 244
|
||||
private const val PACKET_HEADER = 2
|
||||
|
||||
const val TRIGGER_BATTERY = 5
|
||||
const val TRIGGER_CASE = 6
|
||||
const val TRIGGER_WEAR = 7
|
||||
const val TRIGGER_TOUCH = 8
|
||||
const val EVENTS_MASK = (1 shl TRIGGER_BATTERY) or (1 shl TRIGGER_CASE) or (1 shl TRIGGER_WEAR) or (1 shl TRIGGER_TOUCH)
|
||||
|
||||
const val PROP_BATT_PCT = 0x11
|
||||
const val PROP_CHARGE = 0x12
|
||||
const val PROP_IN_CASE = 0x13
|
||||
const val PROP_WORN = 0x14
|
||||
/** A property without a value yet (VM_UNKNOWN). */
|
||||
const val UNKNOWN = Int.MIN_VALUE
|
||||
|
||||
const val CHARGE_CHARGING = 1
|
||||
|
||||
const val GESTURE_TAP = 1
|
||||
const val GESTURE_DOUBLE_TAP = 2
|
||||
const val GESTURE_TRIPLE_TAP = 3
|
||||
const val GESTURE_LONG_START = 4
|
||||
const val GESTURE_LONG_END = 5
|
||||
|
||||
/** The packets of one record: `[0x80 first | 0x40 last][stream][the next bytes]`. */
|
||||
fun packets(record: ByteArray, stream: Int, mtu: Int): List<ByteArray> {
|
||||
val chunk = minOf(mtu - 3, MAX_PACKET) - PACKET_HEADER
|
||||
require(record.isNotEmpty() && chunk >= 5 && stream in 0..255)
|
||||
val packets = ArrayList<ByteArray>(record.size / chunk + 1)
|
||||
var offset = 0
|
||||
while (offset < record.size) {
|
||||
val count = minOf(chunk, record.size - offset)
|
||||
val packet = ByteArray(PACKET_HEADER + count)
|
||||
packet[0] = ((if (offset == 0) OPT_START else 0) or (if (offset + count == record.size) OPT_END else 0)).toByte()
|
||||
packet[1] = stream.toByte()
|
||||
record.copyInto(packet, PACKET_HEADER, offset, offset + count)
|
||||
packets.add(packet)
|
||||
offset += count
|
||||
}
|
||||
return packets
|
||||
}
|
||||
|
||||
fun control(op: Int, tag: Int, body: ByteArray = ByteArray(0)): ByteArray =
|
||||
byteArrayOf(op.toByte(), tag.toByte()) + body
|
||||
|
||||
fun eventsRequest(mask: Int): ByteArray = byteArrayOf(mask.toByte(), (mask ushr 8).toByte())
|
||||
|
||||
/** An event's type, or -1 for bytes too short to be one. */
|
||||
fun eventType(data: ByteArray): Int = if (data.size >= EVENT_HEADER) data[0].toInt() and 255 else -1
|
||||
|
||||
class Reply(@JvmField val op: Int, @JvmField val tag: Int, @JvmField val status: Int, @JvmField val body: ByteArray)
|
||||
|
||||
fun parseReply(data: ByteArray): Reply? {
|
||||
val type = eventType(data)
|
||||
if (type < EV_REPLY || data.size < EVENT_HEADER + 2) return null
|
||||
return Reply(type and 0x7f, data[EVENT_HEADER].toInt() and 255, data[EVENT_HEADER + 1].toInt() and 255,
|
||||
data.copyOfRange(EVENT_HEADER + 2, data.size))
|
||||
}
|
||||
|
||||
/** Same shape as [CfwTransport.parseAcks]'s, so the shared window takes it. */
|
||||
fun parseFcAck(data: ByteArray): CfwTransport.Ack? {
|
||||
if (eventType(data) != EV_FC_ACK || data.size < EVENT_HEADER + 9) return null
|
||||
val kind = data[EVENT_HEADER].toInt() and 255
|
||||
if (kind != 1 && kind != 3) return null
|
||||
return CfwTransport.Ack(kind == 3, data[EVENT_HEADER + 1].toInt() and 255, u16(data, EVENT_HEADER + 2),
|
||||
data[EVENT_HEADER + 4].toInt() and 255, u16(data, EVENT_HEADER + 5), u16(data, EVENT_HEADER + 7))
|
||||
}
|
||||
|
||||
class Input(@JvmField val trigger: Int, @JvmField val value: Int)
|
||||
|
||||
fun parseInput(data: ByteArray): Input? {
|
||||
if (eventType(data) != EV_INPUT || data.size < EVENT_HEADER + 7) return null
|
||||
return Input(data[EVENT_HEADER + 2].toInt() and 255, u32(data, EVENT_HEADER + 3))
|
||||
}
|
||||
|
||||
class Hello(
|
||||
@JvmField val version: String,
|
||||
@JvmField val leftArm: Boolean,
|
||||
@JvmField val safeMode: Boolean,
|
||||
@JvmField val panelOk: Boolean,
|
||||
@JvmField val encrypted: Boolean,
|
||||
@JvmField val inCase: Boolean,
|
||||
)
|
||||
|
||||
fun parseHello(body: ByteArray): Hello? {
|
||||
if (body.size < 7 || body[0].toInt() != 5) return null
|
||||
val flags = body[5].toInt() and 255
|
||||
return Hello("${body[1].toInt() and 255}.${body[2].toInt() and 255}.${body[3].toInt() and 255}",
|
||||
body[4].toInt() == 1, flags and 1 != 0, flags and 2 != 0, flags and 64 != 0, flags and 128 != 0)
|
||||
}
|
||||
|
||||
/** The display path's version, or -1 for a body that isn't FC_INFO's. */
|
||||
fun parseFcVersion(body: ByteArray): Int = if (body.size >= 14) body[0].toInt() and 255 else -1
|
||||
|
||||
/** One value per requested property, or null if the reply is short. */
|
||||
fun parseProps(body: ByteArray, count: Int): IntArray? =
|
||||
if (body.size < 4 * count) null else IntArray(count) { u32(body, 4 * it) }
|
||||
|
||||
/**
|
||||
* What a TouchBar gesture means to Faceclaw, which expects a G2's temple: a tap, a double tap and two slides. A
|
||||
* G1's TouchBar cannot slide, but it has one per arm, so a tap moves (left up, right down), a double tap selects
|
||||
* and a triple tap or long press goes back. Null: no event. With the display [dark] nothing can scroll: a tap is
|
||||
* the G2's tap, which shows the Glanceboard, and the session reports a double tap as a wake.
|
||||
*/
|
||||
fun gestureEventType(gesture: Int, leftArm: Boolean, dark: Boolean = false): Int? = when (gesture) {
|
||||
GESTURE_TAP -> if (dark) BleProtocol.EVENT_CLICK else if (leftArm) BleProtocol.EVENT_SCROLL_TOP else BleProtocol.EVENT_SCROLL_BOTTOM
|
||||
GESTURE_DOUBLE_TAP -> if (dark) BleProtocol.EVENT_DOUBLE_CLICK else BleProtocol.EVENT_CLICK
|
||||
GESTURE_TRIPLE_TAP, GESTURE_LONG_START -> BleProtocol.EVENT_DOUBLE_CLICK
|
||||
else -> null
|
||||
}
|
||||
|
||||
private fun u16(b: ByteArray, p: Int): Int = (b[p].toInt() and 255) or ((b[p + 1].toInt() and 255) shl 8)
|
||||
|
||||
private fun u32(b: ByteArray, p: Int): Int = u16(b, p) or (u16(b, p + 2) shl 16)
|
||||
}
|
||||
+2
-2
@@ -33,7 +33,7 @@ class CfwMessageWindow private constructor() {
|
||||
fun acknowledgedHead(messages: Iterable<OutboundMessage>): OutboundMessage? {
|
||||
for (message in messages) {
|
||||
if (message.sid != CfwTransport.SID) continue
|
||||
return if (!message.cfwRetryPending && message.cfwAckLenses == CfwTransport.BOTH)
|
||||
return if (!message.cfwRetryPending && message.isCfwAcked)
|
||||
message
|
||||
else null
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class CfwMessageWindow private constructor() {
|
||||
failed =
|
||||
failed ||
|
||||
message.cfwRetryPending ||
|
||||
(message.cfwAckLenses != CfwTransport.BOTH &&
|
||||
(!message.isCfwAcked &&
|
||||
message.ackDeadlineAtMs > 0 &&
|
||||
message.ackDeadlineAtMs <= nowMs)
|
||||
replay.add(message)
|
||||
|
||||
+5
-1
@@ -18,6 +18,8 @@ class OutboundMessage(
|
||||
@JvmField
|
||||
val cfwChecksum: Int = if (sid == CfwTransport.SID) CfwTransport.crc(this.message) else 0
|
||||
@JvmField var cfwAckLenses = 0
|
||||
/** The lenses that must ACK this attempt: both on a G2, whichever the message went to on a G1. */
|
||||
@JvmField var cfwRequiredLenses = CfwTransport.BOTH
|
||||
@JvmField var cfwRetries = 0
|
||||
@JvmField var cfwRetryPending = false
|
||||
@JvmField var imageUpdateId = 0
|
||||
@@ -47,9 +49,11 @@ class OutboundMessage(
|
||||
}
|
||||
if (message.size != ack.size || cfwChecksum != ack.checksum) return false
|
||||
cfwAckLenses = cfwAckLenses or ack.lens
|
||||
return !cfwRetryPending && cfwAckLenses == CfwTransport.BOTH
|
||||
return !cfwRetryPending && isCfwAcked
|
||||
}
|
||||
|
||||
val isCfwAcked: Boolean get() = cfwAckLenses and cfwRequiredLenses == cfwRequiredLenses
|
||||
|
||||
/** Replace an attempt atomically under the communicator lock. */
|
||||
fun prepareCfwReplay(streamId: Int) {
|
||||
magic = streamId
|
||||
|
||||
+9
-4
@@ -19,15 +19,20 @@ internal fun GlassesSessionCore.enqueueBrightnessLocked(frameVisible: Boolean? =
|
||||
else if (brightnessPolicy.automatic) 1200 else 120
|
||||
val payload = byteArrayOf(CFW_MSG_BRIGHTNESS.toByte(), 1, target.toByte(),
|
||||
(if (visible) 1 else 0).toByte(), duration.toByte(), (duration shr 8).toByte())
|
||||
val message = messageBuilder.customMessage("brightness-output", payload,
|
||||
"brightness target=$target visible=$visible fade=$duration", -1, connectionOptions.sendImagesToLeft)
|
||||
message.onTimeout = MessageCallback {
|
||||
val label = "brightness target=$target visible=$visible fade=$duration"
|
||||
val retry = MessageCallback {
|
||||
brightnessSentLevel = -1
|
||||
brightnessSentVisible = null
|
||||
// Retry with a PRESENT even if the scene did not change during recovery.
|
||||
lastEnqueuedFingerprint = ""
|
||||
}
|
||||
pendingMessages.addLast(message)
|
||||
if (isG1) {
|
||||
g1EnqueueBrightnessLocked(payload, label, brightnessSentVisible != visible, retry)
|
||||
} else {
|
||||
val message = messageBuilder.customMessage("brightness-output", payload, label, -1, connectionOptions.sendImagesToLeft)
|
||||
message.onTimeout = retry
|
||||
pendingMessages.addLast(message)
|
||||
}
|
||||
brightnessSentLevel = target
|
||||
brightnessSentVisible = visible
|
||||
if (isIo) io.lastBrightnessAtMs = now()
|
||||
|
||||
+36
-3
@@ -111,6 +111,8 @@ class GlassesSessionCore(
|
||||
@Volatile internal var glassesType = GlassesType.G2
|
||||
/** RayNeo iO link state (GlassesSessionIo.kt); guarded by [monitor]. */
|
||||
internal val io = IoSessionState()
|
||||
/** Even G1 link state (GlassesSessionG1.kt); guarded by [monitor]. */
|
||||
internal val g1 = G1SessionState()
|
||||
|
||||
/**
|
||||
* Optional tap for relay frames the session does not interpret: notifications on the
|
||||
@@ -578,6 +580,14 @@ class GlassesSessionCore(
|
||||
}
|
||||
}
|
||||
|
||||
/** Drive an Even G1 on g1v0 instead of a G2 (see [G1SessionState]). Before [start]. */
|
||||
fun useG1() {
|
||||
monitor.withLock { glassesType = GlassesType.G1 }
|
||||
}
|
||||
|
||||
/** True when this session drives an Even G1. */
|
||||
fun isEvenG1(): Boolean = isG1
|
||||
|
||||
/** True when this session drives a RayNeo iO. */
|
||||
fun isRayNeoIo(): Boolean = isIo
|
||||
|
||||
@@ -613,7 +623,7 @@ class GlassesSessionCore(
|
||||
* quiescing its producers. Also cover direct/early close callers here;
|
||||
* a successful cleanup must remain the final BLE message. Older CFWs
|
||||
* fall back to the standalone framebuffer-lease release. */
|
||||
if (!cfwCleanupDelivered && !sendCfwCleanup() && !isIo) {
|
||||
if (!cfwCleanupDelivered && !sendCfwCleanup() && isG2) {
|
||||
releaseFaceclawFramebufferLease()
|
||||
}
|
||||
monitor.withLock {
|
||||
@@ -683,6 +693,10 @@ class GlassesSessionCore(
|
||||
}
|
||||
// The iO's packets are Opus from its remote audio stream (see ioStartAudioCapture).
|
||||
if (isIo) return ioStartAudioCapture(listener)
|
||||
if (isG1) {
|
||||
logLine("skip G1 mic: not in this version")
|
||||
return false
|
||||
}
|
||||
var magic = 0
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) {
|
||||
@@ -701,6 +715,7 @@ class GlassesSessionCore(
|
||||
|
||||
fun stopG2AudioCapture() {
|
||||
if (isIo) return ioStopAudioCapture()
|
||||
if (isG1) return
|
||||
var magic = 0
|
||||
monitor.withLock {
|
||||
audioPacketListener = null
|
||||
@@ -834,7 +849,7 @@ class GlassesSessionCore(
|
||||
monitor.withLock {
|
||||
faceclawWakeLeaseEnabled = enabled
|
||||
// A G2 firmware lease; the iO's wake word is setVoiceWakeEnabled.
|
||||
if (isIo) return true
|
||||
if (!isG2) return true
|
||||
if (!running || !sessionReady) {
|
||||
return !enabled
|
||||
}
|
||||
@@ -1594,6 +1609,7 @@ class GlassesSessionCore(
|
||||
|
||||
fun sendShutdown(exitMode: Int): Boolean {
|
||||
if (isIo) return ioSuspend()
|
||||
if (isG1) return g1Suspend()
|
||||
return sendShutdownInternal(exitMode, true)
|
||||
}
|
||||
|
||||
@@ -1604,6 +1620,17 @@ class GlassesSessionCore(
|
||||
* can use the legacy shutdown-and-lease-release path.
|
||||
*/
|
||||
fun sendCfwCleanup(): Boolean {
|
||||
if (isG1) {
|
||||
// Fire and forget: the firmware keeps no session that could outlive the link.
|
||||
monitor.withLock {
|
||||
if (cfwCleanupDelivered) return true
|
||||
shutdownRequested = true
|
||||
cfwCleanupDelivered = true
|
||||
clearPendingMessagesLocked("G1 cleanup")
|
||||
}
|
||||
g1HideBeforeDisconnect()
|
||||
return true
|
||||
}
|
||||
val magic: Int
|
||||
monitor.withLock {
|
||||
if (cfwCleanupDelivered) {
|
||||
@@ -1686,6 +1713,7 @@ class GlassesSessionCore(
|
||||
*/
|
||||
fun suspendEvenHubSession(): Boolean {
|
||||
if (isIo) return ioSuspend()
|
||||
if (isG1) return g1Suspend()
|
||||
// Ending the plugin task releases the fb lease, which frees the
|
||||
// on-glasses resource cache; forget it phone-side either way (a lost
|
||||
// ack may still have taken effect).
|
||||
@@ -1710,6 +1738,7 @@ class GlassesSessionCore(
|
||||
*/
|
||||
fun resumeEvenHubSession(): Boolean {
|
||||
if (isIo) return ioResume()
|
||||
if (isG1) return g1Resume()
|
||||
var claimGeneration = 0
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady || chargingMode) {
|
||||
@@ -1790,7 +1819,7 @@ class GlassesSessionCore(
|
||||
continue
|
||||
}
|
||||
logWarn("Attempting to connect")
|
||||
if (isIo) ioConnectLoopOnce() else connectLoopOnce()
|
||||
if (isIo) ioConnectLoopOnce() else if (isG1) g1ConnectLoopOnce() else connectLoopOnce()
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1826,6 +1855,10 @@ class GlassesSessionCore(
|
||||
ioOnNotification(address, uuid, data)
|
||||
return
|
||||
}
|
||||
if (isG1) {
|
||||
g1OnNotification(address, uuid, data)
|
||||
return
|
||||
}
|
||||
if (BleProtocol.RENDER_NOTIFY_UUID == uuid) {
|
||||
handleRenderNotification(address, data)
|
||||
return
|
||||
|
||||
+553
@@ -0,0 +1,553 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import com.faceclaw.app.GlassesSessionCore.Companion.DASHBOARD_TILE
|
||||
import com.faceclaw.app.GlassesSessionCore.Companion.safeMessage
|
||||
|
||||
/** Bytes of the arm's frame once everything queued for it has run, and what each arm last reported. */
|
||||
internal class G1ArmState {
|
||||
var hello: G1Protocol.Hello? = null
|
||||
var battery = -1
|
||||
var charge = -1
|
||||
var inCase = -1
|
||||
var worn = -1
|
||||
|
||||
/** Null: unknown, so the next update is a full frame. */
|
||||
var frame: ByteArray? = null
|
||||
|
||||
fun resetLink() {
|
||||
hello = null
|
||||
battery = -1
|
||||
charge = -1
|
||||
inCase = -1
|
||||
worn = -1
|
||||
frame = null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Even G1 session state ([GlassesSessionCore.g1]); guarded by the core's monitor.
|
||||
*
|
||||
* A G1 is two independent devices on two BLE links, and neither hears what the other is sent. Each arm has its own
|
||||
* deflate stream (cfwTransports) and frame, each message goes to one arm and only that lens ACKs it, and the phone
|
||||
* composes each lens: the firmware has no display lists, so the shell scene is drawn here in its settled state.
|
||||
* Firmware: scouter_kit's g1v0 (g1_firmware/docs/VM.md §6.11, firmware/src/fc.c).
|
||||
*/
|
||||
internal class G1SessionState {
|
||||
val arms = arrayOf(G1ArmState(), G1ArmState())
|
||||
private var tag = 0
|
||||
val replies = HashMap<Int, G1Protocol.Reply>()
|
||||
var lastPropsAtMs = 0L
|
||||
|
||||
/** The panel may be blank: the next update ends in PRESENT even with no pixels to send. */
|
||||
var presentPending = false
|
||||
|
||||
fun nextTag(): Int {
|
||||
tag = tag % 255 + 1
|
||||
return tag
|
||||
}
|
||||
|
||||
fun resetLink() {
|
||||
for (arm in arms) arm.resetLink()
|
||||
replies.clear()
|
||||
lastPropsAtMs = 0
|
||||
presentPending = true
|
||||
}
|
||||
}
|
||||
|
||||
internal object G1Timings {
|
||||
const val CONTROL_REPLY_MS = 1_500L
|
||||
const val CONTROL_TRIES = 4
|
||||
/** The OS may be showing its pairing dialog. */
|
||||
const val PAIRING_MS = 30_000
|
||||
const val PROPS_MS = 60_000L
|
||||
const val LENS_LEFT = 1
|
||||
const val LENS_RIGHT = 2
|
||||
}
|
||||
|
||||
private val G1_PROPS = intArrayOf(G1Protocol.PROP_BATT_PCT, G1Protocol.PROP_CHARGE, G1Protocol.PROP_IN_CASE, G1Protocol.PROP_WORN)
|
||||
|
||||
internal val GlassesSessionCore.isG1: Boolean get() = glassesType == GlassesType.G1
|
||||
|
||||
internal val GlassesSessionCore.isG2: Boolean get() = glassesType == GlassesType.G2
|
||||
|
||||
/** What the display path takes (fc.c); anything else a shared feature queues never goes out. */
|
||||
internal fun g1SupportsMessage(message: OutboundMessage): Boolean {
|
||||
if (message.sid != CfwTransport.SID || message.message.isEmpty()) return false
|
||||
return when (message.message[0].toInt() and CFW_MSG_TYPE_MASK) {
|
||||
CFW_MSG_DRAW_CALLS, CFW_MSG_PRESENT, CFW_MSG_BRIGHTNESS -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1ArmAddress(arm: Int): String = if (arm == 0) leftAddress else rightAddress
|
||||
|
||||
private fun g1Lens(arm: Int): Int = if (arm == 0) G1Timings.LENS_LEFT else G1Timings.LENS_RIGHT
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Writes
|
||||
|
||||
/** The packets of one queued message, its record from this arm's persistent deflate stream. */
|
||||
internal fun GlassesSessionCore.g1CfwWrites(message: OutboundMessage, mtu: Int): List<ByteArray> {
|
||||
val arm = if (message.isLeftArmMessage) 0 else 1
|
||||
val transport = cfwTransports[arm]
|
||||
if (message.cfwRetries > 0) transport.reset()
|
||||
return G1Protocol.packets(transport.encodeRecord(message.message, g1Lens(arm)), message.magic, mtu)
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1WriteControl(address: String, op: Int, tag: Int, body: ByteArray): Boolean =
|
||||
link.writeFrames(address, G1Ble.CTRL_CHAR_UUID, listOf(G1Protocol.control(op, tag, body)),
|
||||
GattWriteMode.WITHOUT_RESPONSE, ConnectionOptions.WRITE_TIMEOUT_MS)
|
||||
|
||||
/**
|
||||
* A control request and its reply; worker thread, outside the monitor. Writes on a link the arm doesn't yet obey
|
||||
* (out of the case, before encryption) are ignored and control requests can be lost, so it asks again.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1Control(arm: Int, op: Int, body: ByteArray = ByteArray(0)): G1Protocol.Reply {
|
||||
val address = g1ArmAddress(arm)
|
||||
repeat(G1Timings.CONTROL_TRIES) {
|
||||
val tag = monitor.withLock { g1.nextTag() }
|
||||
val key = arm * 256 + tag
|
||||
if (!g1WriteControl(address, op, tag, body)) throw IllegalStateException("control $op write failed")
|
||||
val deadline = now() + G1Timings.CONTROL_REPLY_MS
|
||||
monitor.withLock {
|
||||
while (true) {
|
||||
g1.replies.remove(key)?.let { return it }
|
||||
if (!running || userDisconnectRequested) throw IllegalStateException("stopped while waiting for control $op")
|
||||
if (!(if (arm == 0) leftConnected else rightConnected)) throw IllegalStateException("link lost waiting for control $op")
|
||||
val remaining = deadline - now()
|
||||
if (remaining <= 0) break
|
||||
monitor.awaitMs(minOf(remaining, 100L))
|
||||
}
|
||||
}
|
||||
}
|
||||
throw IllegalStateException("no reply to control $op from the " + (if (arm == 0) "left" else "right") + " arm")
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1RequestProps(arm: Int) {
|
||||
val tag = monitor.withLock { g1.nextTag() }
|
||||
g1WriteControl(g1ArmAddress(arm), G1Protocol.OP_PROPS, tag, ByteArray(G1_PROPS.size) { G1_PROPS[it].toByte() })
|
||||
}
|
||||
|
||||
/** Nothing here keeps a lease; this only refreshes the battery. Worker thread, outside the monitor. */
|
||||
internal fun GlassesSessionCore.g1Maintain() {
|
||||
val due = monitor.withLock {
|
||||
val now = now()
|
||||
val due = sessionReady && now - g1.lastPropsAtMs >= G1Timings.PROPS_MS
|
||||
if (due) g1.lastPropsAtMs = now
|
||||
due
|
||||
}
|
||||
if (due) {
|
||||
g1RequestProps(0)
|
||||
g1RequestProps(1)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Frames (under the monitor, from enqueueDesiredImageLocked)
|
||||
|
||||
/** Whatever was queued is gone, so what the arms' frames will hold is unknown. */
|
||||
internal fun GlassesSessionCore.g1ForgetFramesLocked() {
|
||||
if (!isG1) return
|
||||
for (arm in g1.arms) arm.frame = null
|
||||
g1.presentPending = true
|
||||
for (transport in cfwTransports) transport.reset()
|
||||
}
|
||||
|
||||
/**
|
||||
* Compose each lens in the frame's settled state, send what changed against the arm's frame, and end with PRESENT.
|
||||
* The two arms' messages alternate so that neither eye runs far ahead, and the PRESENTs go out last and side by side.
|
||||
* A blanked frame sends nothing: the panel waits in standby with the last picture in the arm's frame, and the next
|
||||
* visible frame is a delta against it.
|
||||
*/
|
||||
internal fun GlassesSessionCore.g1EnqueueFrameLocked(
|
||||
packed: ByteArray, width: Int, height: Int, scene: ShellScene, fingerprint: String, paintMs: Int, frameId: Int,
|
||||
) {
|
||||
if (fingerprint.startsWith("blanked:")) {
|
||||
lastEnqueuedFingerprint = fingerprint
|
||||
displayedFingerprint = fingerprint
|
||||
finishFrame(frameId, "blanked: the panel is in standby")
|
||||
return
|
||||
}
|
||||
frameTimings.spanStart(frameId, "compose-lenses")
|
||||
val updates: List<List<ByteArray>>
|
||||
val composed: Array<ByteArray>
|
||||
try {
|
||||
val left = scene.composed(packed, width, height, false)
|
||||
val right = if (scene.hasStereoDepth) scene.composed(packed, width, height, true) else left
|
||||
composed = arrayOf(left, right)
|
||||
updates = List(2) { arm -> G1FramePlanner.plan(g1.arms[arm].frame, composed[arm], width, height) }
|
||||
} catch (e: Exception) {
|
||||
// As in the G2's path: this frame would fail the same way after a reconnect (#42), so drop it.
|
||||
lastEnqueuedFingerprint = fingerprint
|
||||
logLine("frame plan failed; frame dropped: " + safeMessage(e))
|
||||
finishFrame(frameId, "discarded: frame plan failed")
|
||||
return
|
||||
}
|
||||
frameTimings.spanEnd(frameId, "compose-lenses")
|
||||
val force = g1.presentPending
|
||||
val queues = List(2) { arm ->
|
||||
ArrayDeque(updates[arm]).also { if (it.isNotEmpty() || force) it.addLast(byteArrayOf(CFW_MSG_PRESENT.toByte())) }
|
||||
}
|
||||
g1.presentPending = false
|
||||
for (arm in 0..1) g1.arms[arm].frame = composed[arm]
|
||||
lastEnqueuedFingerprint = fingerprint
|
||||
val messageCount = queues.sumOf { it.size }
|
||||
if (messageCount == 0) {
|
||||
displayedFingerprint = fingerprint
|
||||
finishFrame(frameId, "no change on either lens")
|
||||
return
|
||||
}
|
||||
val updateId = nextImageUpdateId++
|
||||
imageUpdateStats[updateId] = BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId)
|
||||
var number = 0
|
||||
var payloadBytes = 0
|
||||
var arm = 0
|
||||
while (queues[0].isNotEmpty() || queues[1].isNotEmpty()) {
|
||||
// The other arm's turn once this one is down to its PRESENT, so that both PRESENTs come last.
|
||||
val other = 1 - arm
|
||||
val waiting = queues[arm].size == 1 && queues[other].size > 1
|
||||
if (queues[arm].isEmpty() || waiting) {
|
||||
arm = other
|
||||
continue
|
||||
}
|
||||
val payload = queues[arm].removeFirst()
|
||||
payloadBytes += payload.size
|
||||
number++
|
||||
val plan = BleImageOptimizer.TileImagePlan(arm, DASHBOARD_TILE, composed[arm], width, height, nextMapSessionId(), payload)
|
||||
enqueueCustomImageLocked(plan, payload, fingerprint, updateId, number, messageCount, arm == 0, g1Lens(arm))
|
||||
arm = other
|
||||
}
|
||||
frameTimings.log(frameId, "queued image update#$updateId messages=$messageCount payload=${payloadBytes}B")
|
||||
logLine("queue image update#$updateId fingerprint=$fingerprint messages=$messageCount")
|
||||
}
|
||||
|
||||
/** Message 30 for both arms, its visibility ordered with the frames around it. */
|
||||
internal fun GlassesSessionCore.g1EnqueueBrightnessLocked(payload: ByteArray, label: String, visibleChanged: Boolean, onTimeout: MessageCallback) {
|
||||
for (arm in 0..1) {
|
||||
val message = messageBuilder.customMessage("brightness-output", payload, label, -1, arm == 0)
|
||||
message.cfwRequiredLenses = g1Lens(arm)
|
||||
message.onTimeout = onTimeout
|
||||
pendingMessages.addLast(message)
|
||||
}
|
||||
if (visibleChanged) g1.presentPending = true
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1HidePayload(): ByteArray =
|
||||
byteArrayOf(CFW_MSG_BRIGHTNESS.toByte(), 1, brightnessPolicy.target.coerceIn(0, 100).toByte(), 0, 0, 0)
|
||||
|
||||
/** Stop drawing, keeping the links and the arms' frames: the panels wait in standby (the EvenHub suspend analogue). */
|
||||
internal fun GlassesSessionCore.g1Suspend(): Boolean {
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady) return false
|
||||
val idle = pendingMessages.isEmpty() && inFlightMessages.isEmpty()
|
||||
val frames = g1.arms.map { it.frame }
|
||||
shutdownRequested = true
|
||||
clearAllMessagesLocked("G1 suspend")
|
||||
// Nothing was lost, so the arms still hold what the phone thinks and a wake is a delta.
|
||||
if (idle) for (arm in 0..1) g1.arms[arm].frame = frames[arm]
|
||||
g1EnqueueBrightnessLocked(g1HidePayload(), "suspend: hide", true) { }
|
||||
brightnessSentVisible = false
|
||||
displayedFingerprint = ""
|
||||
}
|
||||
interruptibleSleep.interrupt()
|
||||
return true
|
||||
}
|
||||
|
||||
/** The drive loop resends the desired frame; its PRESENT brings the panels back. */
|
||||
internal fun GlassesSessionCore.g1Resume(): Boolean {
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady) return false
|
||||
shutdownRequested = false
|
||||
displayedFingerprint = ""
|
||||
lastEnqueuedFingerprint = ""
|
||||
imageRetryAfterMs = 0
|
||||
g1.presentPending = true
|
||||
}
|
||||
interruptibleSleep.interrupt()
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* At the end of a session: the panels off now rather than 30 s from now, when the firmware's own rule would do it.
|
||||
* A raw record that resets the stream, not waited on; nothing follows it.
|
||||
*/
|
||||
internal fun GlassesSessionCore.g1HideBeforeDisconnect() {
|
||||
val message = g1HidePayload()
|
||||
val record = CfwTransport.record(message, CfwTransport.crc(message), CfwTransport.BOTH or CfwTransport.RESET_CONTEXT)
|
||||
for (arm in 0..1) {
|
||||
val address = g1ArmAddress(arm)
|
||||
if (!link.isConnected(address)) continue
|
||||
try {
|
||||
link.writeFrames(address, G1Ble.FC_CHAR_UUID, G1Protocol.packets(record, 0, link.negotiatedMtu(address)),
|
||||
GattWriteMode.WITHOUT_RESPONSE, ConnectionOptions.WRITE_TIMEOUT_MS)
|
||||
} catch (t: Throwable) {
|
||||
logLine("G1 hide failed: " + safeMessage(t))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Connect (worker thread)
|
||||
|
||||
private fun GlassesSessionCore.g1ConnectArm(arm: Int, deadlineMs: Long) {
|
||||
val address = g1ArmAddress(arm)
|
||||
val timeoutMs = maxOf(deadlineMs - now(), ConnectionOptions.CONNECT_TIMEOUT_MS.toLong())
|
||||
if (!link.connect(address, timeoutMs.toInt())) {
|
||||
throw ArmUnreachableException(if (arm == 0) "left" else "right", address)
|
||||
}
|
||||
link.requestHighPriority(address)
|
||||
link.requestMtu(address, G1Ble.REQUESTED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS)
|
||||
if (!link.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) {
|
||||
throw IllegalStateException("discoverServices failed: $address")
|
||||
}
|
||||
if (!link.enableNotifications(address, G1Ble.EVT_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) {
|
||||
throw IllegalStateException("no event characteristic on $address: not g1v0 firmware?")
|
||||
}
|
||||
link.requestLe2mPhy(address)
|
||||
monitor.withLock {
|
||||
if (arm == 0) leftConnected = true else rightConnected = true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A write with response to PAIR needs an encrypted link. A bonded arm gets one at once; an arm in the case bonds here,
|
||||
* through whatever the OS does (Android's dialog, iOS's); out of the case the arm refuses a new bond and the write fails.
|
||||
* Either way this is where the link is known to be encrypted, so the arm obeys everything that follows.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1Pair(arm: Int) {
|
||||
val address = g1ArmAddress(arm)
|
||||
if (!link.isBonded(address)) {
|
||||
logLine("G1 " + (if (arm == 0) "left" else "right") + " arm: asking the OS to pair")
|
||||
setStateDisplay("connecting", "Pairing with the glasses...")
|
||||
link.requestBond(address)
|
||||
val deadline = now() + G1Timings.PAIRING_MS
|
||||
while (!link.isBonded(address) && now() < deadline) {
|
||||
interruptibleSleep.sleep(200)
|
||||
if (!monitor.withLock { running && !userDisconnectRequested }) throw IllegalStateException("stopped while pairing")
|
||||
}
|
||||
}
|
||||
if (!link.writeFrames(address, G1Ble.PAIR_CHAR_UUID, listOf(byteArrayOf(1)), GattWriteMode.WITH_RESPONSE, G1Timings.PAIRING_MS)) {
|
||||
throw IllegalStateException("no encrypted link to the " + (if (arm == 0) "left" else "right") + " arm")
|
||||
}
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.g1ConnectLoopOnce() {
|
||||
monitor.withLock {
|
||||
wearState = -1
|
||||
g1.resetLink()
|
||||
}
|
||||
setStateDisplay("connecting", "Connecting to the glasses...")
|
||||
ConnectionCounters.increment("g1.connect-attempt")
|
||||
try {
|
||||
val deadlineMs = now() + ConnectionOptions.ARM_CONNECT_WINDOW_MS
|
||||
link.beginConnect(rightAddress, false)
|
||||
link.beginConnect(leftAddress, false)
|
||||
// The left first, as the arms were installed; each stands alone.
|
||||
g1ConnectArm(0, deadlineMs)
|
||||
g1ConnectArm(1, deadlineMs)
|
||||
g1Pair(0)
|
||||
g1Pair(1)
|
||||
|
||||
val versions = arrayOf("", "")
|
||||
var compatible = true
|
||||
for (arm in 0..1) {
|
||||
val reply = g1Control(arm, G1Protocol.OP_HELLO)
|
||||
val hello = G1Protocol.parseHello(reply.body) ?: throw IllegalStateException("bad HELLO from arm $arm")
|
||||
monitor.withLock { g1.arms[arm].hello = hello }
|
||||
versions[arm] = "g1v0 " + hello.version + (if (hello.safeMode) " (safe mode)" else "")
|
||||
if (hello.leftArm != (arm == 0)) {
|
||||
logWarn("G1: the arm paired as " + (if (arm == 0) "left" else "right") + " says it is the other one; its lens gets the other eye's frame")
|
||||
}
|
||||
val info = g1Control(arm, G1Protocol.OP_FC_INFO)
|
||||
val fcVersion = if (info.status == 0) G1Protocol.parseFcVersion(info.body) else -1
|
||||
logLine("G1 " + (if (arm == 0) "left" else "right") + " arm: " + versions[arm]
|
||||
+ " display path " + fcVersion + " encrypted=" + hello.encrypted + " inCase=" + hello.inCase
|
||||
+ " panelOk=" + hello.panelOk + " phy=" + link.describePhy(g1ArmAddress(arm)))
|
||||
compatible = compatible && fcVersion >= 1
|
||||
}
|
||||
// The firmware's own number, not the G2 custom firmware's: the path has one version so far, and TS compares this.
|
||||
val firmware = BleProtocol.FirmwareInfo(versions[0], versions[1],
|
||||
if (compatible) BleProtocol.FirmwareInfo.FACECLAW_EXTENSION_PREFIX + requiredFirmwareRevision else "")
|
||||
emitFirmwareInfo(firmware)
|
||||
if (!compatible) {
|
||||
monitor.withLock { incompatibleFirmware = firmware }
|
||||
return
|
||||
}
|
||||
for (arm in 0..1) {
|
||||
val events = g1Control(arm, G1Protocol.OP_EVENTS, G1Protocol.eventsRequest(G1Protocol.EVENTS_MASK))
|
||||
if (events.status != 0) throw IllegalStateException("EVENTS refused: " + events.status)
|
||||
// The arm counts a boot healthy only after a host has said so (VM.md §6.4, safe mode).
|
||||
g1Control(arm, G1Protocol.OP_HEALTH_OK)
|
||||
}
|
||||
monitor.withLock {
|
||||
sessionReady = true
|
||||
shutdownRequested = false
|
||||
customFirmwareDetected = true
|
||||
clearAllMessagesLocked("session ready")
|
||||
fixedLayoutCreated = true
|
||||
displayedFingerprint = ""
|
||||
lastAckAtMs = now()
|
||||
lastIncomingAtMs = lastAckAtMs
|
||||
lastConnectionOrInputAtMs = lastAckAtMs
|
||||
lastSessionReadyAtMs = lastAckAtMs
|
||||
imageRetryAfterMs = 0
|
||||
consecutiveAckTimeouts = 0
|
||||
cfwCleanupDelivered = false
|
||||
lastCfwCleanupAckMagic = 0
|
||||
g1.lastPropsAtMs = 0
|
||||
monitor.signalAll()
|
||||
}
|
||||
setStateDisplay("connected", "Connected.")
|
||||
interruptibleSleep.interrupt()
|
||||
} catch (t: Throwable) {
|
||||
logLine("G1 connect failed: " + safeMessage(t))
|
||||
val unpaired = firstUnpairedArm()
|
||||
if (unpaired != null) {
|
||||
logError("G1 connect failed and $unpaired is not paired; suspending reconnect")
|
||||
ConnectionCounters.increment("g1.unpaired")
|
||||
monitor.withLock { haltReconnectLocked("arm not paired: $unpaired") }
|
||||
if (!userDisconnectRequested) {
|
||||
setStateDisplay("unpaired", "The glasses are not paired with this phone. Put them in their charging case"
|
||||
+ " with the lid open, then connect: they only pair in the case.")
|
||||
}
|
||||
interruptibleSleep.interrupt()
|
||||
} else if (t is ArmUnreachableException) {
|
||||
val other = if (t.side == "left") rightAddress else leftAddress
|
||||
if (link.isConnected(other)) {
|
||||
handleTransportFailure(t.side + " arm not found", "Can't reach the " + t.side + " arm of the glasses."
|
||||
+ " It may be connected to another device, or be off or out of range.")
|
||||
} else {
|
||||
handleTransportFailure("glasses not found", "Can't find the glasses. They may be off or out of range.")
|
||||
}
|
||||
} else {
|
||||
handleTransportFailure("connect failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Inbound (BLE callback thread)
|
||||
|
||||
internal fun GlassesSessionCore.g1OnNotification(address: String, characteristicUuid: String, data: ByteArray) {
|
||||
if (G1Ble.EVT_CHAR_UUID != characteristicUuid) return
|
||||
val arm = armIndex(address)
|
||||
if (arm < 0) return
|
||||
when (val type = G1Protocol.eventType(data)) {
|
||||
G1Protocol.EV_FC_ACK -> {
|
||||
val ack = G1Protocol.parseFcAck(data) ?: return
|
||||
monitor.withLock { acceptCfwAcksLocked(address, arrayOf(ack), 0, data) }
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
G1Protocol.EV_INPUT -> g1HandleInput(arm, G1Protocol.parseInput(data) ?: return)
|
||||
else -> if (type >= 0x80) {
|
||||
val reply = G1Protocol.parseReply(data) ?: return
|
||||
if (reply.op == G1Protocol.OP_PROPS) {
|
||||
if (reply.status == 0) G1Protocol.parseProps(reply.body, G1_PROPS.size)?.let { g1HandleProps(arm, it) }
|
||||
return
|
||||
}
|
||||
monitor.withLock {
|
||||
lastIncomingAtMs = now()
|
||||
g1.replies[arm * 256 + reply.tag] = reply
|
||||
monitor.signalAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1HandleInput(arm: Int, input: G1Protocol.Input) {
|
||||
when (input.trigger) {
|
||||
G1Protocol.TRIGGER_TOUCH -> g1HandleGesture(arm, input.value)
|
||||
G1Protocol.TRIGGER_BATTERY -> g1HandleProps(arm, intArrayOf(input.value, G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, G1Protocol.UNKNOWN), true)
|
||||
G1Protocol.TRIGGER_WEAR -> g1HandleProps(arm, intArrayOf(G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, input.value))
|
||||
G1Protocol.TRIGGER_CASE -> g1HandleProps(arm, intArrayOf(G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, input.value and 1, G1Protocol.UNKNOWN))
|
||||
}
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1HandleGesture(arm: Int, gesture: Int) {
|
||||
val leftArm = arm == 0
|
||||
val dark = monitor.withLock { shutdownRequested || brightnessSentVisible == false }
|
||||
val eventType = G1Protocol.gestureEventType(gesture, leftArm, dark)
|
||||
monitor.withLock {
|
||||
lastIncomingAtMs = now()
|
||||
if (eventType != null) lastConnectionOrInputAtMs = lastIncomingAtMs
|
||||
}
|
||||
logLine("G1 gesture " + gesture + " on the " + (if (leftArm) "left" else "right") + " arm" + (if (eventType == null) " (ignored)" else ""))
|
||||
if (eventType == null) return
|
||||
val source = if (leftArm) BleProtocol.EVENT_SOURCE_GLASSES_L else BleProtocol.EVENT_SOURCE_GLASSES_R
|
||||
// As the G2's firmware reports its own double tap on a dark display: it can only wake, whatever the delay.
|
||||
val event = if (dark && gesture == G1Protocol.GESTURE_DOUBLE_TAP) G2Event("display-wake", "", eventType, 0, 0)
|
||||
else G2Event("sys-event", "", eventType, source, 0)
|
||||
val frameId = frameTimings.startFrame("input:" + event.kind + " type=$eventType src=$source")
|
||||
frameTimings.log(frameId, "input event decoded from G1 notification")
|
||||
emitRingEvent(event, frameId)
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
|
||||
/**
|
||||
* New values in the order of [G1_PROPS], [G1Protocol.UNKNOWN] for those it doesn't say. The pair's battery is the lower
|
||||
* arm's, the pair is charging if either arm is, worn if either arm says so, and in the case when both are.
|
||||
* [batteryChanged]: a BATTERY event, which may also mean CHARGE changed, so that is read again.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1HandleProps(arm: Int, values: IntArray, batteryChanged: Boolean = false) {
|
||||
var batteryEmit = false
|
||||
var wearEmit = -1
|
||||
var putOn = false
|
||||
val state = g1.arms[arm]
|
||||
monitor.withLock {
|
||||
lastIncomingAtMs = now()
|
||||
if (values[0] != G1Protocol.UNKNOWN) state.battery = values[0]
|
||||
if (values[1] != G1Protocol.UNKNOWN) state.charge = values[1]
|
||||
if (values[2] != G1Protocol.UNKNOWN) state.inCase = values[2]
|
||||
if (values[3] != G1Protocol.UNKNOWN) state.worn = values[3]
|
||||
|
||||
val batteries = g1.arms.map { it.battery }.filter { it >= 0 }
|
||||
val battery = if (batteries.isEmpty()) -1 else batteries.min()
|
||||
val charging = if (g1.arms.any { it.charge == G1Protocol.CHARGE_CHARGING }) 1 else if (g1.arms.all { it.charge < 0 }) -1 else 0
|
||||
if (battery != headsetBattery || charging != headsetCharging) {
|
||||
headsetBattery = battery
|
||||
headsetCharging = charging
|
||||
batteryEmit = true
|
||||
}
|
||||
val worn = if (g1.arms.any { it.worn == 1 }) 1 else if (g1.arms.all { it.worn < 0 }) -1 else 0
|
||||
if (worn >= 0 && worn != wearState) {
|
||||
putOn = chargingMode && wearState == 0 && worn == 1
|
||||
wearState = worn
|
||||
wearEmit = worn
|
||||
}
|
||||
if (sessionReady) {
|
||||
val inCase = g1.arms.all { it.inCase == 1 }
|
||||
if (putOn) {
|
||||
endChargingModeLocked("glasses put on")
|
||||
} else if (inCase != chargingMode && g1.arms.all { it.inCase >= 0 }) {
|
||||
g1SetInCaseLocked(inCase, battery)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (batteryEmit) emitBatteryState(headsetBattery, headsetCharging)
|
||||
if (wearEmit >= 0) emitWearState(wearEmit == 1)
|
||||
if (batteryChanged) g1RequestPropsLater()
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1RequestPropsLater() {
|
||||
monitor.withLock { g1.lastPropsAtMs = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* In the case nobody can see the panels, and lit pixels disturb the case's charging link: they go dark and frames stop.
|
||||
* Leaving the case reconnects, as on a G2, which also picks up a firmware the arms got meanwhile.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1SetInCaseLocked(inCase: Boolean, battery: Int) {
|
||||
if (inCase) {
|
||||
chargingMode = true
|
||||
clearAllMessagesLocked("glasses in the case")
|
||||
fixedLayoutCreated = false
|
||||
displayedFingerprint = ""
|
||||
g1EnqueueBrightnessLocked(g1HidePayload(), "in the case: hide", true) { }
|
||||
finishDesiredFrameLocked("discarded: glasses in the case")
|
||||
logLine("glasses are in the case; pausing display communication")
|
||||
setStateDisplay("charging", GlassesSessionCore.chargingStatusText(battery))
|
||||
} else {
|
||||
endChargingModeLocked("glasses out of the case")
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -5,7 +5,7 @@ import com.faceclaw.app.GlassesSessionCore.Companion.safeMessage
|
||||
import kotlin.random.Random
|
||||
|
||||
/** Which glasses a session drives. Set before [GlassesSessionCore.start]; see [GlassesSessionCore.useRayNeoIo]. */
|
||||
enum class GlassesType { G2, RAYNEO_IO }
|
||||
enum class GlassesType { G2, RAYNEO_IO, G1 }
|
||||
|
||||
/**
|
||||
* RayNeo iO session state ([GlassesSessionCore.io]); guarded by the core's monitor.
|
||||
@@ -210,9 +210,9 @@ internal fun GlassesSessionCore.ioCfwWrites(message: OutboundMessage, mtu: Int):
|
||||
internal fun GlassesSessionCore.ioDropUnsupported(message: OutboundMessage) {
|
||||
monitor.withLock {
|
||||
inFlightMessages.remove(message)
|
||||
if (message.magic != 0) magicPool.release(message.sid, message.magic, message.label, "unsupported on RayNeo iO")
|
||||
if (message.magic != 0) magicPool.release(message.sid, message.magic, message.label, "unsupported on these glasses")
|
||||
}
|
||||
logDebug("RayNeo iO: dropped unsupported message " + message.label + " sid=" + message.sid)
|
||||
logDebug("dropped a message these glasses don't take: " + message.label + " sid=" + message.sid)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
||||
+5
-5
@@ -971,11 +971,11 @@ internal fun macWireBytes(address: String): ByteArray? {
|
||||
internal fun GlassesSessionCore.queueGlassesRingLinkCommandLocked(
|
||||
action: String, fallbackAddress: String, fallbackName: String,
|
||||
): Boolean {
|
||||
if (isIo) {
|
||||
// A RayNeo iO has no ring link of its own; the phone holds the ring directly.
|
||||
lastRingLinkCommand = "$action: not applicable (RayNeo iO)"
|
||||
if (!isG2) {
|
||||
// A RayNeo iO or a G1 has no ring link of its own; the phone holds the ring directly.
|
||||
lastRingLinkCommand = "$action: not applicable ($glassesType)"
|
||||
lastRingLinkCommandAtMs = now()
|
||||
logLine("glasses ring $action skipped: RayNeo iO glasses have no ring link")
|
||||
logLine("glasses ring $action skipped: $glassesType glasses have no ring link")
|
||||
return false
|
||||
}
|
||||
val reported = glassesRingMac
|
||||
@@ -1100,7 +1100,7 @@ internal fun GlassesSessionCore.queueRingConfigCommandLocked(
|
||||
"targets-glasses" -> {
|
||||
// The iO never connects to the ring: naming it would make the ring drop
|
||||
// the phone's glasses-role link (the advStart check, ~20 s later).
|
||||
if (isIo) return "not sent: RayNeo iO glasses don't hold the ring"
|
||||
if (!isG2) return "not sent: these glasses don't hold the ring"
|
||||
val right = macWireBytes(rightAddress)
|
||||
val left = macWireBytes(leftAddress)
|
||||
if (right == null || left == null) return "not sent: glasses addresses unparseable"
|
||||
|
||||
+32
-13
@@ -45,6 +45,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
}
|
||||
}
|
||||
if (isIo) ioMaintain()
|
||||
if (isG1) g1Maintain()
|
||||
var messageToWrite: OutboundMessage? = null
|
||||
var messageToPrewrite: OutboundMessage? = null
|
||||
var heartbeatToWrite: OutboundMessage? = null
|
||||
@@ -75,7 +76,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
clearPendingMessagesLocked("after CFW cleanup")
|
||||
return 250
|
||||
}
|
||||
if (sessionReady && !isIo
|
||||
if (sessionReady && isG2
|
||||
&& now - lastFaceclawFramebufferLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS
|
||||
&& !hasPendingOrInflightKindLocked("framebuffer-lease-control")) {
|
||||
enqueueFaceclawFramebufferControlLocked(
|
||||
@@ -84,7 +85,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
)
|
||||
}
|
||||
if (faceclawWakeLeaseEnabled
|
||||
&& sessionReady && !isIo
|
||||
&& sessionReady && isG2
|
||||
&& now - lastFaceclawWakeLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS
|
||||
&& !hasPendingOrInflightKindLocked("wake-lease-control")) {
|
||||
enqueueFaceclawWakeControlLocked(
|
||||
@@ -159,7 +160,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
}
|
||||
} else {
|
||||
if (messageToPrewrite == null
|
||||
&& !isIo // the iO's page lease is ioMaintain's
|
||||
&& isG2 // the iO's page lease is ioMaintain's; a G1 has none
|
||||
&& !shutdownRequested
|
||||
&& !fixedLayoutCreated
|
||||
&& pendingMessages.isEmpty()
|
||||
@@ -170,14 +171,14 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
// Prewrite outside the lock; the logical message remains pending until
|
||||
// its final BLE frame is sent after the current protocol ACK.
|
||||
}
|
||||
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
|
||||
if (messageToPrewrite == null && isG2 && !shutdownRequested && fixedLayoutCreated
|
||||
&& (if (firmwareDebugFlagsEnabled) 2 else 1) != firmwareDebugFlagsLastSent
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
logLine("enqueueing firmware debug flags " + (if (firmwareDebugFlagsEnabled) "show" else "hide"))
|
||||
enqueueFirmwareDebugFlagsLocked()
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
|
||||
if (messageToPrewrite == null && isG2 && !shutdownRequested && fixedLayoutCreated
|
||||
&& caseInfoReadPending
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
caseInfoReadPending = false
|
||||
@@ -185,7 +186,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
pendingMessages.addLast(messageBuilder.caseInfoQuery())
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
|
||||
if (messageToPrewrite == null && isG2 && !shutdownRequested && fixedLayoutCreated
|
||||
&& (if (compassOwners.isEmpty()) 0 else 1) != compassControlLastSent
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
val wanted = compassOwners.isNotEmpty()
|
||||
@@ -196,7 +197,9 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
if (benchmarkActive) {
|
||||
maintainBenchmarkLocked(now)
|
||||
}
|
||||
if (!benchmarkActive && (!isIo || ioFirmwareOwnsBrightness)) maintainBrightnessLocked(now)
|
||||
// A G1's brightness follows the policy's target; its light sensor isn't wired up yet.
|
||||
if (!benchmarkActive && isG1) enqueueBrightnessLocked()
|
||||
else if (!benchmarkActive && (!isIo || ioFirmwareOwnsBrightness)) maintainBrightnessLocked(now)
|
||||
|
||||
// Up to WINDOW_SIZE messages may be in flight at once (full
|
||||
// pipelining); a slot frees when an ack arrives. An active
|
||||
@@ -339,7 +342,7 @@ internal enum class HeartbeatStep {
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.heartbeatStepLocked(imageWaiting: Boolean): HeartbeatStep {
|
||||
if (isIo) return HeartbeatStep.NONE
|
||||
if (!isG2) return HeartbeatStep.NONE
|
||||
val now = now()
|
||||
val heartbeatEligible = !shutdownRequested && fixedLayoutCreated
|
||||
val heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat")
|
||||
@@ -390,6 +393,11 @@ internal fun GlassesSessionCore.createHeartbeatMessage(): OutboundMessage {
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean {
|
||||
if (isG1 && !g1SupportsMessage(message)) {
|
||||
// G2-only traffic a shared feature queued anyway: it would only wait for an ACK that never comes.
|
||||
ioDropUnsupported(message)
|
||||
return true
|
||||
}
|
||||
if (isIo && !ioSupportsMessage(message)) {
|
||||
// G2-only traffic (EvenHub/settings frames, the G2 hardware CFW modes) that a feature
|
||||
// queued anyway: the iO would NACK it into the replay limit, so it never goes out.
|
||||
@@ -427,6 +435,8 @@ internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean
|
||||
prewrittenFrames = emptyList()
|
||||
} else if (isIo) {
|
||||
frames = ioCfwWrites(message, link.negotiatedMtu(writeAddress))
|
||||
} else if (isG1) {
|
||||
frames = g1CfwWrites(message, link.negotiatedMtu(writeAddress))
|
||||
} else if (message.sid == CfwTransport.SID) {
|
||||
if (message.cfwRetries > 0) cfwTransports[if (message.isLeftArmMessage) 0 else 1].reset()
|
||||
frames = cfwTransports[if (message.isLeftArmMessage) 0 else 1].encode(
|
||||
@@ -442,9 +452,9 @@ internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean
|
||||
}
|
||||
val result = link.writeFrames(
|
||||
writeAddress,
|
||||
if (isIo) RayNeoIoBle.WRITE_CHAR_UUID else BleProtocol.WRITE_CHAR_UUID,
|
||||
if (isIo) RayNeoIoBle.WRITE_CHAR_UUID else if (isG1) G1Ble.FC_CHAR_UUID else BleProtocol.WRITE_CHAR_UUID,
|
||||
frames,
|
||||
if (isIo) GattWriteMode.WITHOUT_RESPONSE else ConnectionOptions.WRITE_MODE,
|
||||
if (isIo || isG1) GattWriteMode.WITHOUT_RESPONSE else ConnectionOptions.WRITE_MODE,
|
||||
ConnectionOptions.WRITE_TIMEOUT_MS
|
||||
)
|
||||
|
||||
@@ -847,6 +857,10 @@ internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
frameTimings.spanEnd(frameId, "pack-4bpp")
|
||||
// Visibility belongs to this immutable composite, not the latest UI request.
|
||||
enqueueBrightnessLocked(!fingerprint.startsWith("blanked:"))
|
||||
if (isG1 && packedFrame.size > 0) {
|
||||
g1EnqueueFrameLocked(packedFrame, width, height, scene, fingerprint, paintMs, frameId)
|
||||
return
|
||||
}
|
||||
if (customFirmwareDetected && packedFrame.size > 0) {
|
||||
val rendered = try {
|
||||
scenePlanner.plan(packedFrame, width, height, draws, scene, nextImageFrameId,
|
||||
@@ -992,10 +1006,13 @@ internal fun GlassesSessionCore.enqueueCustomImageLocked(
|
||||
fingerprint: String,
|
||||
updateId: Int,
|
||||
messageNumber: Int,
|
||||
messageCount: Int
|
||||
messageCount: Int,
|
||||
leftArm: Boolean = connectionOptions.sendImagesToLeft,
|
||||
requiredLenses: Int = CfwTransport.BOTH,
|
||||
) {
|
||||
val message = messageBuilder.customMessage("image", payload,
|
||||
"image " + plan.tile.name + "#" + messageNumber, plan.tileIndex, connectionOptions.sendImagesToLeft)
|
||||
"image " + plan.tile.name + "#" + messageNumber, plan.tileIndex, leftArm)
|
||||
message.cfwRequiredLenses = requiredLenses
|
||||
message.setImageUpdatePosition(updateId, messageNumber, messageCount)
|
||||
message.onAck = MessageCallback {
|
||||
imageRetryAfterMs = 0
|
||||
@@ -1048,7 +1065,7 @@ internal fun GlassesSessionCore.createAudioControlMessageLocked(enable: Boolean)
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.shouldPollBatteryLocked(now: Long): Boolean {
|
||||
return !isIo // the iO's status polls are ioMaintain's
|
||||
return isG2 // the others' status polls are their own maintain's
|
||||
&& !shutdownRequested
|
||||
&& sessionReady
|
||||
&& pendingMessages.isEmpty()
|
||||
@@ -1356,6 +1373,7 @@ internal fun GlassesSessionCore.clearMessagesOfKindLocked(kind: String) {
|
||||
// drop the pipelined delta base so the next image is a full keyframe.
|
||||
lastEnqueuedPacked = ByteArray(0)
|
||||
lastEnqueuedFingerprint = ""
|
||||
g1ForgetFramesLocked()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1370,6 +1388,7 @@ internal fun GlassesSessionCore.clearAllMessagesLocked(reason: String) {
|
||||
// image is a full keyframe rather than a delta onto a stale base.
|
||||
lastEnqueuedPacked = ByteArray(0)
|
||||
lastEnqueuedFingerprint = ""
|
||||
g1ForgetFramesLocked()
|
||||
// Queued resource commands (if any) were dropped with the rest, and the
|
||||
// session churn behind a full clear may have freed the on-glasses
|
||||
// cache; forget it phone-side so glyphs re-upload lazily.
|
||||
|
||||
+3
@@ -68,6 +68,9 @@ interface SessionLink {
|
||||
/** Whether the OS still holds a pairing bond for the address (iOS: always true). */
|
||||
fun isBonded(address: String): Boolean
|
||||
|
||||
/** Ask the OS to bond with a connected device, without waiting. A no-op where it pairs on demand only (CoreBluetooth). */
|
||||
fun requestBond(address: String) {}
|
||||
|
||||
/** Bandwidth-benchmark link tuning; 0: current link, 1: HIGH priority, 2: 2M PHY, 3: both. */
|
||||
fun prepareBenchmarkLink(address: String, mode: Int)
|
||||
|
||||
|
||||
@@ -64,6 +64,21 @@ class ShellScene(val layers: List<Layer>, val selections: List<RetainedDrawing>
|
||||
return output
|
||||
}
|
||||
|
||||
/** Whether the lenses differ: stereo depth somewhere in the scene. */
|
||||
val hasStereoDepth: Boolean get() = screenDepth != 0 || layers.any { it.depth != 0 } || allSelections.isNotEmpty()
|
||||
|
||||
/**
|
||||
* What one lens shows of [screen] (packed 4bpp) with this scene over it and every animation settled, for
|
||||
* glasses that have no display lists (a G1). [screen] itself when there is no scene.
|
||||
*/
|
||||
fun composed(screen: ByteArray, width: Int, height: Int, rightLens: Boolean): ByteArray {
|
||||
if (layers.isEmpty() && selections.isEmpty() && screenDepth == 0) return screen
|
||||
val output = ByteArray(screen.size)
|
||||
DisplayListRenderer(rendererResources(width, height), rightLens = rightLens).render(511,
|
||||
DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height), SETTLED_MS)
|
||||
return output
|
||||
}
|
||||
|
||||
/** Layer surfaces, then retained resources, by index, and the root list at 511. */
|
||||
private fun rendererResources(width: Int, height: Int): HashMap<Int, ByteArray> {
|
||||
val resources = HashMap<Int, ByteArray>()
|
||||
|
||||
@@ -86,6 +86,9 @@ class IosGlassesSession internal constructor(
|
||||
|
||||
fun setRequiredFirmwareRevision(revision: Int) = core.setRequiredFirmwareRevision(revision)
|
||||
|
||||
/** Drive an Even G1 on g1v0 (see GlassesSessionG1.kt). Before [start]. */
|
||||
fun useG1() = core.useG1()
|
||||
|
||||
fun start(): Boolean = core.start()
|
||||
|
||||
fun disconnect() = core.disconnect()
|
||||
|
||||
@@ -180,3 +180,43 @@ test("RayNeo iO advertisements classify as one device with its MAC", () => {
|
||||
// G2 advertisements are untouched
|
||||
assert.equal(classifyAdvertisement(raw({})).role, "left");
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Even G1
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const g1Ascii = (text) => Array.from(text).map((c) => c.charCodeAt(0).toString(16).padStart(2, "0")).join("");
|
||||
const g1Raw = (name, manufacturerData, address = "F4:27:E9:CA:E8:3F") => ({
|
||||
address,
|
||||
name,
|
||||
manufacturerData,
|
||||
rssi: -50,
|
||||
txPower: null,
|
||||
connectable: true,
|
||||
bonded: false,
|
||||
source: "scan",
|
||||
seenAtMs: 1000,
|
||||
});
|
||||
|
||||
test("a G1 arm on Even's firmware: side from the name, serial after the side byte", () => {
|
||||
const ad = classifyAdvertisement(g1Raw("Even G1_70_L_EF50E5", "02" + g1Ascii("S110LABC030139") + "0000000000"));
|
||||
assert.equal(ad.role, "left");
|
||||
assert.equal(ad.g1Firmware, "stock");
|
||||
assert.equal(ad.serial, "S110LABC030139");
|
||||
assert.equal(ad.embeddedMac, null);
|
||||
});
|
||||
|
||||
test("a G1 arm on g1v0: the name says all, the serial is in the 0xFFFF record", () => {
|
||||
const ad = classifyAdvertisement(g1Raw("G1v0_R", "ffff" + "01" + "04" + g1Ascii("S110LABC030139")));
|
||||
assert.equal(ad.role, "right");
|
||||
assert.equal(ad.g1Firmware, "g1v0");
|
||||
assert.equal(ad.serial, "S110LABC030139");
|
||||
assert.equal(classifyAdvertisement(g1Raw("G1v0S_L", "")).g1Firmware, "g1v0-safe");
|
||||
assert.equal(classifyAdvertisement(g1Raw("G1v0S_L", "")).serial, null);
|
||||
});
|
||||
|
||||
test("names that only look like a G1 are not admitted", () => {
|
||||
for (const name of ["G1v0_X", "G1v0", "G1 Tracker", "Even G1", "Pixel G1v0_L"]) {
|
||||
assert.equal(classifyAdvertisement(g1Raw(name, "")), null, name);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,777 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.concurrent.Volatile
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private const val LEFT = "F4:27:E9:CA:E8:3F"
|
||||
private const val RIGHT = "CD:EB:89:BF:E9:EC"
|
||||
private const val W = 640
|
||||
private const val H = 480
|
||||
private const val STRIDE = W / 2
|
||||
|
||||
private fun awaitTrue(timeoutMs: Long, condition: () -> Boolean): Boolean {
|
||||
val platform = testPlatform()
|
||||
val deadline = platform.elapsedRealtimeMs() + timeoutMs
|
||||
while (platform.elapsedRealtimeMs() < deadline) {
|
||||
if (condition()) return true
|
||||
sleepMs(10)
|
||||
}
|
||||
return condition()
|
||||
}
|
||||
|
||||
private fun nibble(frame: ByteArray, x: Int, y: Int): Int {
|
||||
val v = frame[y * STRIDE + x / 2].toInt() and 255
|
||||
return if (x % 2 == 0) v ushr 4 else v and 15
|
||||
}
|
||||
|
||||
private fun setNibble(frame: ByteArray, x: Int, y: Int, c: Int) {
|
||||
val i = y * STRIDE + x / 2
|
||||
val v = frame[i].toInt() and 255
|
||||
frame[i] = (if (x % 2 == 0) (v and 0x0f) or (c shl 4) else (v and 0xf0) or c).toByte()
|
||||
}
|
||||
|
||||
/** What fc.c does with a draw message: bbox and clear calls into [frame]; returns the calls' rectangles. */
|
||||
private fun applyDraw(frame: ByteArray, m: ByteArray): List<IntArray> {
|
||||
val rects = ArrayList<IntArray>()
|
||||
fun u16(p: Int) = (m[p].toInt() and 255) or ((m[p + 1].toInt() and 255) shl 8)
|
||||
val count = u16(1)
|
||||
var p = 3
|
||||
repeat(count) {
|
||||
val len = u16(p)
|
||||
p += 2
|
||||
val end = p + len
|
||||
assertEquals(0, m[p + 1].toInt(), "a G1 call carries no target, depth or clip")
|
||||
assertEquals(DRAW_OP_BOUNDING_BOX, m[p].toInt(), "only pixels on a G1")
|
||||
var q = p + 2
|
||||
val x: Int
|
||||
val y: Int
|
||||
val w: Int
|
||||
val h: Int
|
||||
if (m[q].toInt() == 0) {
|
||||
x = (m[q + 1].toInt() and 255) * 4
|
||||
y = (m[q + 2].toInt() and 255) * 2
|
||||
w = (m[q + 3].toInt() and 255) * 4
|
||||
h = (m[q + 4].toInt() and 255) * 2
|
||||
q += 5
|
||||
} else {
|
||||
x = u16(q + 1)
|
||||
y = u16(q + 3)
|
||||
w = u16(q + 5)
|
||||
h = u16(q + 7)
|
||||
q += 9
|
||||
}
|
||||
assertTrue(x + w <= W && y + h <= H, "rect $x,$y ${w}x$h")
|
||||
var left = w * h
|
||||
var cx = x
|
||||
var cy = y
|
||||
while (q < end) {
|
||||
val token = m[q++].toInt() and 255
|
||||
val color = token and 15
|
||||
var run = token ushr 4
|
||||
if (run == 0) {
|
||||
run = m[q++].toInt() and 255
|
||||
if (run == 0) {
|
||||
run = u16(q)
|
||||
q += 2
|
||||
}
|
||||
}
|
||||
left -= run
|
||||
assertTrue(left >= 0, "RLE overruns its rectangle")
|
||||
repeat(run) {
|
||||
setNibble(frame, cx, cy, color)
|
||||
cx++
|
||||
if (cx == x + w) {
|
||||
cx = x
|
||||
cy++
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals(0, left, "RLE must cover the rectangle exactly")
|
||||
rects.add(intArrayOf(x, y, w, h))
|
||||
p = end
|
||||
}
|
||||
assertEquals(m.size, p)
|
||||
return rects
|
||||
}
|
||||
|
||||
/** One arm on g1v0, as the phone sees it over GATT. */
|
||||
private class FakeG1Arm(val left: Boolean) {
|
||||
val frame = ByteArray(STRIDE * H)
|
||||
/** What a look through the lens shows: the frame as of the last PRESENT while visible. */
|
||||
val panel = ByteArray(STRIDE * H)
|
||||
var visible = true
|
||||
var panelOn = false
|
||||
val records = ArrayList<ByteArray>()
|
||||
var current = ByteArray(0)
|
||||
val messageTypes = ArrayList<Int>()
|
||||
val rects = ArrayList<IntArray>()
|
||||
var nackNext = false
|
||||
var brightness = -1
|
||||
var wireBytes = 0
|
||||
var pairedWrites = 0
|
||||
var eventsMask = 0
|
||||
var healthOk = 0
|
||||
var bonded = false
|
||||
var inCase = 0
|
||||
var battery = 80
|
||||
}
|
||||
|
||||
private class FakeG1Glasses(platform: ProtocolPlatform) : SessionLink {
|
||||
lateinit var core: GlassesSessionCore
|
||||
val lock = platform.createLock()
|
||||
val leftArm = FakeG1Arm(true)
|
||||
val rightArm = FakeG1Arm(false)
|
||||
@Volatile var hasDisplayPath = true
|
||||
/** Out of the case, or the OS said no: no bond can be made. */
|
||||
@Volatile var pairingRefused = false
|
||||
private val out = ArrayList<Pair<String, ByteArray>>()
|
||||
|
||||
private fun arm(address: String) = if (address == LEFT) leftArm else rightArm
|
||||
|
||||
/** Delivered off the writer's stack, as the BLE callback thread does: the session writes outside its monitor. */
|
||||
private fun event(address: String, type: Int, body: ByteArray) {
|
||||
val data = ByteArray(9) + body
|
||||
data[0] = type.toByte()
|
||||
core.onNotification(address, G1Ble.EVT_CHAR_UUID, data)
|
||||
}
|
||||
|
||||
fun input(address: String, trigger: Int, value: Int) {
|
||||
val body = ByteArray(7)
|
||||
body[2] = trigger.toByte()
|
||||
for (i in 0..3) body[3 + i] = (value ushr (8 * i)).toByte()
|
||||
event(address, G1Protocol.EV_INPUT, body)
|
||||
}
|
||||
|
||||
override fun connect(address: String, timeoutMs: Int): Boolean = true
|
||||
|
||||
override fun isConnected(address: String): Boolean = true
|
||||
|
||||
override fun requestHighPriority(address: String) {}
|
||||
|
||||
override fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean = true
|
||||
|
||||
override fun negotiatedMtu(address: String): Int = 185
|
||||
|
||||
override fun discoverServices(address: String, timeoutMs: Int): Boolean = true
|
||||
|
||||
override fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean =
|
||||
characteristicUuid == G1Ble.EVT_CHAR_UUID
|
||||
|
||||
override fun requestBond(address: String) {
|
||||
if (!pairingRefused) lock.withLock { arm(address).bonded = true }
|
||||
}
|
||||
|
||||
override fun writeFrames(address: String, characteristicUuid: String, frames: List<ByteArray>, mode: GattWriteMode, timeoutMs: Int): Boolean {
|
||||
val a = arm(address)
|
||||
when (characteristicUuid) {
|
||||
G1Ble.PAIR_CHAR_UUID -> {
|
||||
assertEquals(GattWriteMode.WITH_RESPONSE, mode, "only a write with response can make the OS pair")
|
||||
if (pairingRefused) return false
|
||||
lock.withLock {
|
||||
a.pairedWrites++
|
||||
a.bonded = true
|
||||
}
|
||||
}
|
||||
G1Ble.CTRL_CHAR_UUID -> for (f in frames) control(address, a, f)
|
||||
G1Ble.FC_CHAR_UUID -> {
|
||||
assertEquals(GattWriteMode.WITHOUT_RESPONSE, mode)
|
||||
assertTrue(frames.all { it.size <= 182 }, "writes must fit the MTU")
|
||||
for (f in frames) packet(address, a, f)
|
||||
}
|
||||
else -> throw AssertionError("write to $characteristicUuid")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun reply(address: String, op: Int, tag: Int, status: Int, body: ByteArray = ByteArray(0)) =
|
||||
event(address, 0x80 or op, byteArrayOf(tag.toByte(), status.toByte()) + body)
|
||||
|
||||
private fun control(address: String, a: FakeG1Arm, f: ByteArray) {
|
||||
val op = f[0].toInt() and 255
|
||||
val tag = f[1].toInt() and 255
|
||||
when (op) {
|
||||
G1Protocol.OP_HELLO -> {
|
||||
val body = ByteArray(35)
|
||||
body[0] = 5
|
||||
body[1] = 0
|
||||
body[2] = 5
|
||||
body[3] = 62
|
||||
body[4] = (if (a.left) 1 else 0).toByte()
|
||||
body[5] = (2 or 64).toByte()
|
||||
reply(address, op, tag, 0, body)
|
||||
}
|
||||
G1Protocol.OP_FC_INFO -> if (hasDisplayPath) {
|
||||
val body = ByteArray(26)
|
||||
body[0] = 1
|
||||
reply(address, op, tag, 0, body)
|
||||
} else reply(address, op, tag, 2)
|
||||
G1Protocol.OP_EVENTS -> {
|
||||
lock.withLock { a.eventsMask = (f[2].toInt() and 255) or ((f[3].toInt() and 255) shl 8) }
|
||||
reply(address, op, tag, 0, f.copyOfRange(2, 4))
|
||||
}
|
||||
G1Protocol.OP_HEALTH_OK -> {
|
||||
lock.withLock { a.healthOk++ }
|
||||
reply(address, op, tag, 0, ByteArray(1))
|
||||
}
|
||||
G1Protocol.OP_PROPS -> {
|
||||
val values = lock.withLock { intArrayOf(a.battery, 0, a.inCase, 1) }
|
||||
val body = ByteArray(4 * values.size)
|
||||
for (i in values.indices) for (b in 0..3) body[4 * i + b] = (values[i] ushr (8 * b)).toByte()
|
||||
reply(address, op, tag, 0, body)
|
||||
}
|
||||
else -> reply(address, op, tag, 2)
|
||||
}
|
||||
}
|
||||
|
||||
private fun packet(address: String, a: FakeG1Arm, p: ByteArray) {
|
||||
val opts = p[0].toInt() and 255
|
||||
val stream = p[1].toInt() and 255
|
||||
val complete = lock.withLock {
|
||||
if (opts and 0x80 != 0) a.current = ByteArray(0)
|
||||
a.current += p.copyOfRange(2, p.size)
|
||||
if (opts and 0x40 != 0) a.current.also { a.current = ByteArray(0) } else null
|
||||
} ?: return
|
||||
var size = 0
|
||||
val crc = (complete[3].toInt() and 255) or ((complete[4].toInt() and 255) shl 8)
|
||||
var ok = false
|
||||
lock.withLock {
|
||||
a.wireBytes += complete.size
|
||||
val lens = complete[0].toInt() and CfwTransport.BOTH
|
||||
assertEquals(if (a.left) 1 else 2, lens, "a record names the arm it goes to")
|
||||
if (a.nackNext) {
|
||||
a.nackNext = false
|
||||
} else {
|
||||
if (complete[0].toInt() and CfwTransport.RESET_CONTEXT != 0) a.records.clear()
|
||||
a.records.add(complete)
|
||||
val message = inflateRecords(a.records).last()
|
||||
size = message.size
|
||||
assertTrue(size <= 16384, "the firmware's limit on a decoded message: $size")
|
||||
if (CfwTransport.crc(message) == crc) {
|
||||
ok = true
|
||||
apply(a, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
val body = byteArrayOf((if (ok) 1 else 3).toByte(), stream.toByte(), 0, 0, (if (a.left) 1 else 2).toByte(),
|
||||
size.toByte(), (size ushr 8).toByte(), crc.toByte(), (crc ushr 8).toByte())
|
||||
event(address, G1Protocol.EV_FC_ACK, body)
|
||||
}
|
||||
|
||||
private fun apply(a: FakeG1Arm, m: ByteArray) {
|
||||
val type = m[0].toInt() and 255
|
||||
a.messageTypes.add(type)
|
||||
when (type) {
|
||||
CFW_MSG_DRAW_CALLS -> a.rects.addAll(applyDraw(a.frame, m))
|
||||
CFW_MSG_PRESENT -> if (a.visible) {
|
||||
a.frame.copyInto(a.panel)
|
||||
a.panelOn = true
|
||||
}
|
||||
CFW_MSG_BRIGHTNESS -> {
|
||||
a.brightness = m[2].toInt() and 255
|
||||
if (a.visible && m[3].toInt() == 0) a.panelOn = false
|
||||
a.visible = m[3].toInt() != 0
|
||||
}
|
||||
else -> throw AssertionError("message type $type must not reach a G1")
|
||||
}
|
||||
}
|
||||
|
||||
override fun disconnect(address: String) {}
|
||||
|
||||
override fun close() {}
|
||||
|
||||
override fun isBonded(address: String): Boolean = lock.withLock { arm(address).bonded }
|
||||
|
||||
override fun prepareBenchmarkLink(address: String, mode: Int) {}
|
||||
|
||||
override fun recordDisplayFrameSent() {}
|
||||
}
|
||||
|
||||
private class G1Listener(platform: ProtocolPlatform) : FaceclawBleCommunicatorListener {
|
||||
private val lock = platform.createLock()
|
||||
private val phaseList = ArrayList<String>()
|
||||
private val eventList = ArrayList<String>()
|
||||
private val batteryList = ArrayList<String>()
|
||||
private val firmwareList = ArrayList<String>()
|
||||
private val wearList = ArrayList<Boolean>()
|
||||
@Volatile var lastStatus = ""
|
||||
val phases get() = lock.withLock { phaseList.toList() }
|
||||
val events get() = lock.withLock { eventList.toList() }
|
||||
val batteries get() = lock.withLock { batteryList.toList() }
|
||||
val firmware get() = lock.withLock { firmwareList.toList() }
|
||||
val wear get() = lock.withLock { wearList.toList() }
|
||||
|
||||
override fun onStateChange(phase: String?, status: String?) {
|
||||
lock.withLock { phaseList.add(phase ?: "") }
|
||||
lastStatus = status ?: ""
|
||||
}
|
||||
|
||||
override fun onRingEvent(kind: String?, containerName: String?, eventType: Int, eventSource: Int, systemExitReasonCode: Int, frameId: Int, ringTick: Long, ringType: Int, ringAux: Int, ringSpeed: Int) {
|
||||
lock.withLock { eventList.add("$kind:$eventType:$eventSource") }
|
||||
}
|
||||
|
||||
override fun onBatteryState(headsetBattery: Int, headsetCharging: Int, ringBattery: Int, ringCharging: Int) {
|
||||
lock.withLock { batteryList.add("$headsetBattery/$headsetCharging") }
|
||||
}
|
||||
|
||||
override fun onSilentMode(silent: Boolean) {}
|
||||
|
||||
override fun onWearState(wearing: Boolean) {
|
||||
lock.withLock { wearList.add(wearing) }
|
||||
}
|
||||
|
||||
override fun onPhoneLockState(locked: Boolean) {}
|
||||
|
||||
override fun onCaseState(caseBattery: Int, caseCharging: Int, caseDocked: Boolean) {}
|
||||
|
||||
override fun onEvenAppConflict(message: String?) {}
|
||||
|
||||
override fun onFrameMetrics(paintMs: Int, transmitMs: Int, tileCount: Int) {}
|
||||
|
||||
override fun onFrameFinished(frameId: Int, outcome: String?) {}
|
||||
|
||||
override fun onFirmwareInfo(leftVersion: String?, rightVersion: String?, extension: String?) {
|
||||
lock.withLock { firmwareList.add("$leftVersion|$rightVersion|$extension") }
|
||||
}
|
||||
}
|
||||
|
||||
private class G1Session {
|
||||
val platform = testPlatform()
|
||||
val glasses = FakeG1Glasses(platform)
|
||||
private val host = object : SessionHost {
|
||||
private val lock = platform.createLock()
|
||||
private val lines = ArrayList<String>()
|
||||
private var done: Latch? = null
|
||||
|
||||
fun logs(): List<String> = lock.withLock { lines.toList() }
|
||||
|
||||
override fun postToMain(action: () -> Unit) = action()
|
||||
|
||||
override fun currentThreadDispatcher(): SessionDispatcher = SessionDispatcher { it() }
|
||||
|
||||
override fun isPhoneLocked(): Boolean = false
|
||||
|
||||
override fun setScreenWakeLock(on: Boolean) {}
|
||||
|
||||
override fun isEvenAppActive(): Boolean = false
|
||||
|
||||
override fun startWorker(body: () -> Unit) {
|
||||
val latch = Latch(1, platform)
|
||||
done = latch
|
||||
startThread("g1-test-session", true) {
|
||||
try {
|
||||
body()
|
||||
} finally {
|
||||
latch.countDown()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun joinWorker(timeoutMs: Long) {
|
||||
done?.await(timeoutMs)
|
||||
}
|
||||
|
||||
override fun log(level: SessionLogLevel, tag: String, message: String, error: Throwable?) {
|
||||
lock.withLock { lines.add("$level $tag: $message") }
|
||||
}
|
||||
}
|
||||
val listener = G1Listener(platform)
|
||||
val core = GlassesSessionCore(glasses, host, FrameTimingsCore(platform), platform, RIGHT, LEFT, "")
|
||||
|
||||
init {
|
||||
glasses.core = core
|
||||
core.setListener(listener)
|
||||
core.setRequiredFirmwareRevision(38)
|
||||
core.useG1()
|
||||
core.configureCompositorScreen(W, H)
|
||||
core.configureSurface("s", 0, 0, W, 64, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
|
||||
}
|
||||
|
||||
fun ready(): Boolean = core.monitor.withLock { core.sessionReady && core.fixedLayoutCreated }
|
||||
|
||||
fun displayedMatchesDesired(): Boolean = core.monitor.withLock {
|
||||
core.desiredTilesLock.locked { core.desiredFingerprint.isNotEmpty() && core.desiredFingerprint == core.displayedFingerprint }
|
||||
}
|
||||
|
||||
fun pixels(seed: Int) = ByteArray(W * 64) { if ((it + seed) % 11 == 0) 0xF0.toByte() else if (it % 17 == 0) 0x70 else 0 }
|
||||
|
||||
fun submit(seed: Int, frameId: Int) =
|
||||
core.submitSurfaceFrame(ArrayByteReader(pixels(seed)), "s", 0, 0, W, 64, "content-$seed", 0, frameId, null)
|
||||
|
||||
/** The packed picture the surface's gray pixels should give on both lenses. */
|
||||
fun expected(seed: Int): ByteArray {
|
||||
val packed = ByteArray(STRIDE * H)
|
||||
BmpUtil.pack4bppFromGray8(pixels(seed), W, 64).copyInto(packed)
|
||||
return packed
|
||||
}
|
||||
|
||||
fun tail(): String = host.logs().takeLast(30).joinToString("\n")
|
||||
|
||||
fun shownOnBoth(picture: ByteArray): Boolean = glasses.lock.withLock {
|
||||
glasses.leftArm.panel.contentEquals(picture) && glasses.rightArm.panel.contentEquals(picture)
|
||||
}
|
||||
}
|
||||
|
||||
class G1SessionTest {
|
||||
@Test
|
||||
fun pairsOnFirstConnectAndShowsEachLensItsFrame() {
|
||||
val s = G1Session()
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.ready() }, "never ready:\n" + s.tail())
|
||||
assertTrue(s.core.isEvenG1())
|
||||
assertEquals(1, s.glasses.leftArm.pairedWrites, "each arm's encryption is proved by a PAIR write")
|
||||
assertEquals(1, s.glasses.rightArm.pairedWrites)
|
||||
assertEquals(G1Protocol.EVENTS_MASK, s.glasses.leftArm.eventsMask)
|
||||
assertEquals(G1Protocol.EVENTS_MASK, s.glasses.rightArm.eventsMask)
|
||||
assertEquals(1, s.glasses.leftArm.healthOk, "the boot is confirmed healthy")
|
||||
assertTrue(s.listener.firmware.single().startsWith("g1v0 0.5.62|g1v0 0.5.62|Faceclaw/"), s.listener.firmware.toString())
|
||||
|
||||
s.submit(1, 1)
|
||||
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, "frame never displayed:\n" + s.tail())
|
||||
val picture = s.expected(1)
|
||||
assertTrue(s.shownOnBoth(picture), "both lenses show the whole frame:\n" + s.tail())
|
||||
for (arm in listOf(s.glasses.leftArm, s.glasses.rightArm)) {
|
||||
assertEquals(CFW_MSG_PRESENT, arm.messageTypes.last(), "an update ends in PRESENT")
|
||||
assertTrue(arm.messageTypes.all { it == CFW_MSG_DRAW_CALLS || it == CFW_MSG_PRESENT || it == CFW_MSG_BRIGHTNESS },
|
||||
arm.messageTypes.toString())
|
||||
}
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aSecondFrameIsADeltaThatCostsLittle() {
|
||||
val s = G1Session()
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.ready() }, s.tail())
|
||||
s.submit(1, 1)
|
||||
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail())
|
||||
val first = s.glasses.lock.withLock { s.glasses.leftArm.wireBytes }
|
||||
val rectsBefore = s.glasses.lock.withLock { s.glasses.leftArm.rects.size }
|
||||
// One small area of the surface changes.
|
||||
val pixels = s.pixels(1)
|
||||
for (y in 10 until 14) for (x in 100 until 120) pixels[y * W + x] = 0x80.toByte()
|
||||
s.core.submitSurfaceFrame(ArrayByteReader(pixels), "s", 0, 0, W, 64, "content-delta", 0, 2, null)
|
||||
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail())
|
||||
val packed = ByteArray(STRIDE * H)
|
||||
BmpUtil.pack4bppFromGray8(pixels, W, 64).copyInto(packed)
|
||||
assertTrue(s.shownOnBoth(packed), s.tail())
|
||||
s.glasses.lock.withLock {
|
||||
val rects = s.glasses.leftArm.rects.drop(rectsBefore)
|
||||
assertTrue(rects.isNotEmpty() && rects.all { it[2] * it[3] < 64 * 16 }, "tight rects: " + rects.map { it.toList() })
|
||||
assertTrue(s.glasses.leftArm.wireBytes - first < first, "${s.glasses.leftArm.wireBytes - first} after $first")
|
||||
}
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun anUnchangedFrameSendsNothing() {
|
||||
val s = G1Session()
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.ready() }, s.tail())
|
||||
s.submit(1, 1)
|
||||
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail())
|
||||
val before = s.glasses.lock.withLock { s.glasses.leftArm.messageTypes.size }
|
||||
s.core.submitSurfaceFrame(ArrayByteReader(s.pixels(1)), "s", 0, 0, W, 64, "content-again", 0, 2, null)
|
||||
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail())
|
||||
assertEquals(before, s.glasses.lock.withLock { s.glasses.leftArm.messageTypes.size })
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aNackReplaysFromAResetStreamAndTheFrameStillArrives() {
|
||||
val s = G1Session()
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.ready() }, s.tail())
|
||||
s.glasses.lock.withLock { s.glasses.rightArm.nackNext = true }
|
||||
s.submit(2, 1)
|
||||
assertTrue(awaitTrue(8_000) { s.displayedMatchesDesired() }, "never displayed:\n" + s.tail())
|
||||
assertTrue(s.shownOnBoth(s.expected(2)), s.tail())
|
||||
assertFalse(s.glasses.lock.withLock { s.glasses.rightArm.nackNext })
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun gesturesWearAndBatteryReachTheListener() {
|
||||
val s = G1Session()
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.ready() }, s.tail())
|
||||
s.glasses.input(LEFT, G1Protocol.TRIGGER_TOUCH, G1Protocol.GESTURE_TAP)
|
||||
s.glasses.input(RIGHT, G1Protocol.TRIGGER_TOUCH, G1Protocol.GESTURE_TAP)
|
||||
s.glasses.input(RIGHT, G1Protocol.TRIGGER_TOUCH, G1Protocol.GESTURE_DOUBLE_TAP)
|
||||
s.glasses.input(LEFT, G1Protocol.TRIGGER_TOUCH, G1Protocol.GESTURE_LONG_START)
|
||||
s.glasses.input(LEFT, G1Protocol.TRIGGER_TOUCH, G1Protocol.GESTURE_LONG_END)
|
||||
assertEquals(
|
||||
listOf(
|
||||
"sys-event:${BleProtocol.EVENT_SCROLL_TOP}:${BleProtocol.EVENT_SOURCE_GLASSES_L}",
|
||||
"sys-event:${BleProtocol.EVENT_SCROLL_BOTTOM}:${BleProtocol.EVENT_SOURCE_GLASSES_R}",
|
||||
"sys-event:${BleProtocol.EVENT_CLICK}:${BleProtocol.EVENT_SOURCE_GLASSES_R}",
|
||||
"sys-event:${BleProtocol.EVENT_DOUBLE_CLICK}:${BleProtocol.EVENT_SOURCE_GLASSES_L}",
|
||||
),
|
||||
s.listener.events,
|
||||
)
|
||||
// The first props poll tells battery and wear; the pair reads its lower arm.
|
||||
assertTrue(awaitTrue(3_000) { s.listener.batteries.contains("80/0") }, s.listener.batteries.toString())
|
||||
assertTrue(awaitTrue(3_000) { s.listener.wear.contains(true) }, s.listener.wear.toString())
|
||||
s.glasses.input(LEFT, G1Protocol.TRIGGER_BATTERY, 55)
|
||||
assertTrue(awaitTrue(3_000) { s.listener.batteries.contains("55/0") }, s.listener.batteries.toString())
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun blankingHidesThePanelsAndWakingIsADelta() {
|
||||
val s = G1Session()
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.ready() }, s.tail())
|
||||
s.submit(1, 1)
|
||||
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail())
|
||||
val wire = s.glasses.lock.withLock { s.glasses.leftArm.wireBytes }
|
||||
val draws = s.glasses.lock.withLock { s.glasses.leftArm.messageTypes.count { it == CFW_MSG_DRAW_CALLS } }
|
||||
|
||||
s.core.setScreenBlanked(true)
|
||||
assertTrue(awaitTrue(5_000) { s.glasses.lock.withLock { !s.glasses.leftArm.visible && !s.glasses.rightArm.visible } }, s.tail())
|
||||
assertTrue(s.shownOnBoth(s.expected(1)), "the arms keep the picture for the wake")
|
||||
assertFalse(s.glasses.lock.withLock { s.glasses.leftArm.panelOn })
|
||||
|
||||
// In the dark a tap is for the Glanceboard and a double tap wakes, as on a G2.
|
||||
s.glasses.input(LEFT, G1Protocol.TRIGGER_TOUCH, G1Protocol.GESTURE_TAP)
|
||||
s.glasses.input(RIGHT, G1Protocol.TRIGGER_TOUCH, G1Protocol.GESTURE_DOUBLE_TAP)
|
||||
assertEquals(
|
||||
listOf("sys-event:${BleProtocol.EVENT_CLICK}:${BleProtocol.EVENT_SOURCE_GLASSES_L}",
|
||||
"display-wake:${BleProtocol.EVENT_DOUBLE_CLICK}:0"),
|
||||
s.listener.events,
|
||||
)
|
||||
|
||||
s.core.setScreenBlanked(false)
|
||||
assertTrue(awaitTrue(5_000) {
|
||||
s.glasses.lock.withLock { s.glasses.leftArm.visible && s.glasses.leftArm.panelOn && s.glasses.rightArm.panelOn }
|
||||
}, "the panels must come back with a PRESENT:\n" + s.tail())
|
||||
assertEquals(draws, s.glasses.lock.withLock { s.glasses.leftArm.messageTypes.count { it == CFW_MSG_DRAW_CALLS } },
|
||||
"the same picture needs no pixels")
|
||||
assertTrue(s.glasses.lock.withLock { s.glasses.leftArm.wireBytes } - wire < 256)
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun bothArmsInTheCaseDarkenThePanelsAndStopFrames() {
|
||||
val s = G1Session()
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.ready() }, s.tail())
|
||||
s.submit(1, 1)
|
||||
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, s.tail())
|
||||
s.glasses.input(LEFT, G1Protocol.TRIGGER_CASE, 1)
|
||||
assertFalse(s.core.monitor.withLock { s.core.chargingMode }, "one arm isn't the pair")
|
||||
s.glasses.input(RIGHT, G1Protocol.TRIGGER_CASE, 1)
|
||||
assertTrue(awaitTrue(3_000) { s.listener.phases.contains("charging") }, s.listener.phases.toString())
|
||||
assertTrue(awaitTrue(3_000) { s.glasses.lock.withLock { !s.glasses.leftArm.visible && !s.glasses.rightArm.visible } }, s.tail())
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun anArmThatCannotBondParksWithInstructions() {
|
||||
val s = G1Session()
|
||||
s.glasses.pairingRefused = true
|
||||
try {
|
||||
// A short window for the test: the session waits for the OS's pairing dialog.
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(40_000) { s.listener.phases.contains("unpaired") }, s.listener.phases.toString() + "\n" + s.tail())
|
||||
assertTrue(s.listener.lastStatus.contains("case"), s.listener.lastStatus)
|
||||
assertFalse(s.ready())
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun firmwareWithoutTheDisplayPathIsIncompatible() {
|
||||
val s = G1Session()
|
||||
s.glasses.hasDisplayPath = false
|
||||
try {
|
||||
assertTrue(s.core.start())
|
||||
assertTrue(awaitTrue(5_000) { s.listener.phases.contains("incompatible-firmware") }, s.listener.phases.toString() + "\n" + s.tail())
|
||||
assertTrue(s.listener.firmware.single().endsWith("|"), s.listener.firmware.toString())
|
||||
} finally {
|
||||
s.core.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class G1ProtocolTest {
|
||||
@Test
|
||||
fun packetsCarryARecordWithinTheMtu() {
|
||||
val record = ByteArray(1000) { it.toByte() }
|
||||
val packets = G1Protocol.packets(record, 42, 185)
|
||||
assertTrue(packets.all { it.size <= 182 })
|
||||
assertEquals(0x80, packets.first()[0].toInt() and 0xC0)
|
||||
assertEquals(0x40, packets.last()[0].toInt() and 0xC0)
|
||||
assertTrue(packets.drop(1).dropLast(1).all { it[0].toInt() == 0 })
|
||||
assertTrue(packets.all { it[1].toInt() == 42 })
|
||||
assertTrue(record.contentEquals(packets.flatMap { it.drop(2) }.toByteArray()))
|
||||
val one = G1Protocol.packets(byteArrayOf(1, 2, 3, 4, 5), 7, 247)
|
||||
assertEquals(1, one.size)
|
||||
assertEquals(0xC0, one[0][0].toInt() and 0xC0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theFirmwaresPacketLimitHoldsAtAnyMtu() {
|
||||
assertEquals(244, G1Protocol.packets(ByteArray(5000), 1, 512).maxOf { it.size })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun eventsAreParsed() {
|
||||
val ack = ByteArray(9) + byteArrayOf(1, 9, 0, 0, 2, 0x34, 0x12, 0x78, 0x56)
|
||||
ack[0] = G1Protocol.EV_FC_ACK.toByte()
|
||||
val parsed = assertNotNull(G1Protocol.parseFcAck(ack))
|
||||
assertFalse(parsed.nack)
|
||||
assertEquals(9, parsed.streamId)
|
||||
assertEquals(2, parsed.lens)
|
||||
assertEquals(0x1234, parsed.size)
|
||||
assertEquals(0x5678, parsed.checksum)
|
||||
ack[9] = 3
|
||||
assertTrue(assertNotNull(G1Protocol.parseFcAck(ack)).nack)
|
||||
ack[9] = 2
|
||||
assertNull(G1Protocol.parseFcAck(ack))
|
||||
assertNull(G1Protocol.parseFcAck(ByteArray(5)))
|
||||
|
||||
val input = ByteArray(9) + byteArrayOf(1, 0, G1Protocol.TRIGGER_TOUCH.toByte(), 2, 0, 0, 0)
|
||||
input[0] = G1Protocol.EV_INPUT.toByte()
|
||||
val parsedInput = assertNotNull(G1Protocol.parseInput(input))
|
||||
assertEquals(G1Protocol.TRIGGER_TOUCH, parsedInput.trigger)
|
||||
assertEquals(2, parsedInput.value)
|
||||
|
||||
val reply = assertNotNull(G1Protocol.parseReply(ByteArray(9).also { it[0] = 0x81.toByte() } + byteArrayOf(7, 0, 42)))
|
||||
assertEquals(1, reply.op)
|
||||
assertEquals(7, reply.tag)
|
||||
assertEquals(42, reply.body[0].toInt())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun helloAndPropsAreParsed() {
|
||||
val body = ByteArray(35)
|
||||
body[0] = 5
|
||||
body[1] = 0
|
||||
body[2] = 5
|
||||
body[3] = 62
|
||||
body[4] = 1
|
||||
body[5] = (2 or 64 or 128).toByte()
|
||||
val hello = assertNotNull(G1Protocol.parseHello(body))
|
||||
assertEquals("0.5.62", hello.version)
|
||||
assertTrue(hello.leftArm && hello.panelOk && hello.encrypted && hello.inCase)
|
||||
assertFalse(hello.safeMode)
|
||||
body[0] = 4
|
||||
assertNull(G1Protocol.parseHello(body), "protocol 4 is not this firmware")
|
||||
|
||||
val unknown = byteArrayOf(0, 0, 0, 0x80.toByte(), 7, 0, 0, 0)
|
||||
val props = assertNotNull(G1Protocol.parseProps(unknown, 2))
|
||||
assertEquals(G1Protocol.UNKNOWN, props[0])
|
||||
assertEquals(7, props[1])
|
||||
assertNull(G1Protocol.parseProps(unknown, 3))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun gesturesMapToWhatTheUiExpects() {
|
||||
assertEquals(BleProtocol.EVENT_SCROLL_TOP, G1Protocol.gestureEventType(G1Protocol.GESTURE_TAP, true))
|
||||
assertEquals(BleProtocol.EVENT_SCROLL_BOTTOM, G1Protocol.gestureEventType(G1Protocol.GESTURE_TAP, false))
|
||||
assertEquals(BleProtocol.EVENT_CLICK, G1Protocol.gestureEventType(G1Protocol.GESTURE_DOUBLE_TAP, true))
|
||||
assertEquals(BleProtocol.EVENT_DOUBLE_CLICK, G1Protocol.gestureEventType(G1Protocol.GESTURE_TRIPLE_TAP, false))
|
||||
assertEquals(BleProtocol.EVENT_DOUBLE_CLICK, G1Protocol.gestureEventType(G1Protocol.GESTURE_LONG_START, true))
|
||||
assertNull(G1Protocol.gestureEventType(G1Protocol.GESTURE_LONG_END, true))
|
||||
assertEquals(BleProtocol.EVENT_CLICK, G1Protocol.gestureEventType(G1Protocol.GESTURE_TAP, false, dark = true))
|
||||
assertEquals(BleProtocol.EVENT_DOUBLE_CLICK, G1Protocol.gestureEventType(G1Protocol.GESTURE_DOUBLE_TAP, false, dark = true))
|
||||
}
|
||||
}
|
||||
|
||||
class G1FramePlannerTest {
|
||||
private fun noise(seed: Int): ByteArray {
|
||||
var state = seed
|
||||
return ByteArray(STRIDE * H) {
|
||||
state = state * 1103515245 + 12345
|
||||
(state ushr 16).toByte()
|
||||
}
|
||||
}
|
||||
|
||||
private fun replay(start: ByteArray?, messages: List<ByteArray>): ByteArray {
|
||||
val frame = start?.copyOf() ?: ByteArray(STRIDE * H)
|
||||
for (m in messages) applyDraw(frame, m)
|
||||
return frame
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aFullFrameOfNoiseFitsTheMessageLimit() {
|
||||
val next = noise(5)
|
||||
val messages = G1FramePlanner.plan(null, next, W, H)
|
||||
assertTrue(messages.size > 1)
|
||||
assertTrue(messages.all { it.size <= G1FramePlanner.MAX_MESSAGE }, messages.maxOf { it.size }.toString())
|
||||
assertTrue(replay(null, messages).contentEquals(next))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nothingChangedIsEmpty() {
|
||||
val next = noise(5)
|
||||
assertTrue(G1FramePlanner.plan(next, next.copyOf(), W, H).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deltasReconstructTheFrameAtAnyPosition() {
|
||||
val before = noise(5)
|
||||
for ((x, y, w, h) in listOf(listOf(0, 0, 1, 1), listOf(639, 479, 1, 1), listOf(101, 51, 33, 17), listOf(0, 100, 640, 3), listOf(300, 0, 2, 480))) {
|
||||
val next = before.copyOf()
|
||||
for (yy in y until y + h) for (xx in x until x + w) setNibble(next, xx, yy, 15 - nibble(next, xx, yy))
|
||||
val messages = G1FramePlanner.plan(before, next, W, H)
|
||||
assertTrue(messages.isNotEmpty())
|
||||
assertTrue(replay(before, messages).contentEquals(next), "rect $x,$y ${w}x$h")
|
||||
}
|
||||
// Two far-apart changes
|
||||
val next = before.copyOf()
|
||||
setNibble(next, 3, 3, 15 - nibble(next, 3, 3))
|
||||
setNibble(next, 600, 440, 15 - nibble(next, 600, 440))
|
||||
assertTrue(replay(before, G1FramePlanner.plan(before, next, W, H)).contentEquals(next))
|
||||
}
|
||||
}
|
||||
|
||||
class G1ShellSceneTest {
|
||||
@Test
|
||||
fun aSceneWithNoLayersIsTheScreen() {
|
||||
val screen = ByteArray(STRIDE * H)
|
||||
assertTrue(ShellScene.EMPTY.composed(screen, W, H, false) === screen)
|
||||
assertFalse(ShellScene.EMPTY.hasStereoDepth)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun depthShiftsEachLensItsOwnWay() {
|
||||
val screen = ByteArray(STRIDE * H)
|
||||
setNibble(screen, 100, 50, 9)
|
||||
val scene = ShellScene(emptyList(), screenDepth = 2)
|
||||
assertTrue(scene.hasStereoDepth)
|
||||
val left = scene.composed(screen, W, H, false)
|
||||
val right = scene.composed(screen, W, H, true)
|
||||
assertEquals(9, nibble(left, 101, 50), "depth +2 moves the left lens +1")
|
||||
assertEquals(9, nibble(right, 99, 50), "and the right lens -1")
|
||||
assertEquals(0, nibble(left, 100, 50))
|
||||
assertEquals(9, nibble(screen, 100, 50), "the screen itself is left alone")
|
||||
}
|
||||
}
|
||||
@@ -306,3 +306,58 @@ test("a RayNeo iO is its own candidate, never half of a G2 pair", () => {
|
||||
assert.equal(row.canSelect, true);
|
||||
assert.equal(row.isPreviouslyPaired, true);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Even G1
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const G1_SERIAL = "S110LABC030139";
|
||||
const g1Arm = (name, address, manufacturerData) => ({
|
||||
address, name, manufacturerData, rssi: -50, txPower: null, connectable: true, bonded: false, source: "scan", seenAtMs: 1000,
|
||||
});
|
||||
const g1v0Arm = (side, address) => g1Arm(`G1v0_${side}`, address, "ffff0104" + ascii(G1_SERIAL));
|
||||
|
||||
test("G1 arms on g1v0 join on the frame serial and can be selected", () => {
|
||||
const aggregator = new DiscoveryAggregator();
|
||||
aggregator.ingest(g1v0Arm("L", "F4:27:E9:CA:E8:3F"));
|
||||
aggregator.ingest(g1v0Arm("R", "CD:EB:89:BF:E9:EC"));
|
||||
const [pair, ...rest] = aggregator.pairs();
|
||||
assert.equal(rest.length, 0);
|
||||
assert.equal(pair.completeness, "complete");
|
||||
assert.equal(pair.g1Firmware, "g1v0");
|
||||
assert.equal(pair.identity, null);
|
||||
const row = glassesRow(pair);
|
||||
assert.equal(row.modelTitle, "Even Realities G1");
|
||||
assert.equal(row.canSelect, true);
|
||||
assert.match(row.armsSummary, /case/);
|
||||
});
|
||||
|
||||
test("G1 arms on Even's firmware show up but can't be selected", () => {
|
||||
const aggregator = new DiscoveryAggregator();
|
||||
aggregator.ingest(g1Arm("Even G1_70_L_EF50E5", "C0:66:16:EF:50:E5", "02" + ascii(G1_SERIAL)));
|
||||
aggregator.ingest(g1Arm("Even G1_70_R_CBE689", "E4:46:BA:CB:E6:89", "01" + ascii(G1_SERIAL)));
|
||||
const [pair] = aggregator.pairs();
|
||||
assert.equal(pair.g1Firmware, "stock");
|
||||
assert.equal(pair.completeness, "complete");
|
||||
const row = glassesRow(pair);
|
||||
assert.equal(row.canSelect, false);
|
||||
assert.match(row.armsSummary, /computer/);
|
||||
});
|
||||
|
||||
test("a G1 pair with one arm in safe mode can't be selected", () => {
|
||||
const aggregator = new DiscoveryAggregator();
|
||||
aggregator.ingest(g1v0Arm("L", "F4:27:E9:CA:E8:3F"));
|
||||
aggregator.ingest(g1Arm("G1v0S_R", "CD:EB:89:BF:E9:EC", "ffff0104" + ascii(G1_SERIAL)));
|
||||
const [pair] = aggregator.pairs();
|
||||
assert.equal(pair.g1Firmware, "g1v0-safe");
|
||||
assert.equal(glassesRow(pair).canSelect, false);
|
||||
});
|
||||
|
||||
test("G2 pairs carry no G1 firmware", () => {
|
||||
const aggregator = new DiscoveryAggregator();
|
||||
aggregator.ingest(arm("left", "S110LABC030139", "EC:D7:82:69:3C:CB", -50));
|
||||
aggregator.ingest(arm("right", "S110LABC030139", "D0:7A:47:82:09:67", -50));
|
||||
const pairs = aggregator.pairs();
|
||||
assert.equal(pairs.length, 1);
|
||||
assert.equal(pairs[0].g1Firmware, undefined);
|
||||
});
|
||||
Reference in new issue
Block a user