From 4d101eefe18118cf55200330d1d22e0abe731cac Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 1 Nov 2026 17:19:55 -0800 Subject: [PATCH] Partial support for Rayneo iO --- ACKNOWLEDGEMENTS.md | 5 + .../faceclaw/app/FaceclawBleCommunicator.kt | 19 + .../faceclaw/app/FaceclawDeviceDiscovery.kt | 8 +- app/g2/dashboard-controller.ts | 19 +- app/g2/even-advertisement.ts | 36 +- app/g2/pairing-candidates.ts | 53 +- app/graphics/image.ts | 3 + app/native/faceclaw-communicator.ios.ts | 6 +- app/native/faceclaw-communicator.ts | 5 +- app/phone-ui/pairing-view-model.ts | 79 ++- app/ui/dashboard-settings.ts | 24 + .../faceclaw/app/g2protocol/CfwTransport.kt | 36 +- .../session/GlassesSessionBrightness.kt | 5 + .../session/GlassesSessionControls.kt | 2 + .../g2protocol/session/GlassesSessionCore.kt | 101 +++- .../g2protocol/session/GlassesSessionIo.kt | 544 ++++++++++++++++++ .../g2protocol/session/GlassesSessionSend.kt | 28 +- .../faceclaw/app/rayneo/RayNeoIoProtocol.kt | 469 +++++++++++++++ tests/display-layout-setting.test.cjs | 14 + tests/even-advertisement.test.cjs | 18 + .../com/faceclaw/app/RayNeoIoProtocolTest.kt | 152 +++++ .../com/faceclaw/app/RayNeoIoSessionTest.kt | 380 ++++++++++++ tests/pairing-candidates.test.cjs | 17 + 23 files changed, 1954 insertions(+), 69 deletions(-) create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionIo.kt create mode 100644 native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/rayneo/RayNeoIoProtocol.kt create mode 100644 tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoProtocolTest.kt create mode 100644 tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoSessionTest.kt diff --git a/ACKNOWLEDGEMENTS.md b/ACKNOWLEDGEMENTS.md index a054eae1..95e41c3f 100644 --- a/ACKNOWLEDGEMENTS.md +++ b/ACKNOWLEDGEMENTS.md @@ -13,6 +13,11 @@ and others who contributed to documenting the G2's bluetooth protocol and firmwa G2 microphone LC3 decoding uses Google's liblc3: https://github.com/google/liblc3 +RayNeo iO support builds on the Glassly SDK and custom firmware by Tetramo, Inc. +and Mentra Labs, Inc. (MIT License): https://github.com/tetramo-labs/glassly and +https://github.com/tetramo-labs/glassly-rayneo-cfw. The iO wire codecs in +native/kotlin/shared/.../rayneo/RayNeoIoProtocol.kt follow their protocol work. + iPhone notification icons are Pebble's notification and timeline icons, from https://github.com/pebble-dev/iconography: the original Pebble Technology icons and Rebble community additions. They are licensed under the Apache License 2.0; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt index e384d0da..5089a039 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.kt @@ -189,6 +189,25 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre fun setRequiredFirmwareRevision(revision: Int) = core.setRequiredFirmwareRevision(revision) + /** + * Drive a RayNeo iO (constructed with its address as both arms and no ring). The phone's + * protocol identity is derived from ANDROID_ID so it survives reinstalls: the glasses bind + * to the first identity that authenticates and refuse others until put in pairing mode. + */ + fun useRayNeoIo() { + val androidId = try { + android.provider.Settings.Secure.getString(appContext.contentResolver, android.provider.Settings.Secure.ANDROID_ID) + } catch (_: Exception) { + null + } + val seed = "faceclaw-rayneo-phone-id:" + (androidId ?: android.os.Build.FINGERPRINT) + val phoneId = java.security.MessageDigest.getInstance("SHA-256").digest(seed.toByteArray(Charsets.UTF_8)).copyOf(6) + val model = android.os.Build.MODEL ?: "Android" + core.useRayNeoIo(phoneId, model, android.os.Build.MANUFACTURER ?: "Android", model) + } + + fun isRayNeoIo(): Boolean = core.isRayNeoIo() + fun start() { if (core.start()) { activeInstance = this diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt index b43bcaae..5cc8f369 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawDeviceDiscovery.kt @@ -129,13 +129,19 @@ class FaceclawDeviceDiscovery(context: Context) { && manufacturerData[0] == 0x45.toByte() && manufacturerData[1] == 0x52.toByte()) { return true } + // RayNeo iO: manufacturer company 0x5716 ([07][MAC]...) outside pairing mode; + // in pairing mode only the "RayNeo iO-xxxx" name identifies it. + if (manufacturerData != null && manufacturerData.size >= 2 + && manufacturerData[0] == 0x16.toByte() && manufacturerData[1] == 0x57.toByte()) { + return true + } if (name == null) { return false } 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") + return upper.contains("G2") || upper.startsWith("EVEN R1") || upper.startsWith("RAYNEO") } private fun buildJson( diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 87e67ac7..83f976db 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -56,7 +56,7 @@ import { claimGlassesOnboardingAutoLaunch, ONBOARDING_APP_ID } from "../apps/onb import { beginRenderPass, endRenderPass } from "../util/render-freshness"; import { voiceControlBridge } from "../native/voice-control"; import { voiceActivity } from "../ui/shell/voice-activity"; -import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image"; +import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, RAYNEO_IO_HEIGHT, RAYNEO_IO_WIDTH } from "../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane"; import { prepareFrameDraws } from "../graphics/glyph-wire"; import { createLockScreenImage, LOCK_SCREEN_SURFACE_ID } from "./lock-screen"; @@ -78,7 +78,7 @@ import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-a import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry"; import { type ContinuousCaptureOptions, type LayerActions, type TextSettingsEditToggle, type TranscriptionChoice } from "../ui/layers"; import { type KeyboardInputSession } from "../ui/shell/keyboard-input"; -import { appSwitcherPositionSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, type RingConnectionMode, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; +import { appSwitcherPositionSetting, glassesTypeSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, ringConnectionModeSetting, type RingConnectionMode, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; import { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } from "../native/battery-optimization"; import { getInstalledEvenHubAppById, @@ -1479,12 +1479,15 @@ class DashboardController { // address disables every direct-ring code path in the communicator. const ringMode = ringConnectionModeSetting.get(); this.ringModeAtConnect = ringMode; - const ringAddress = ringMode !== "glasses" ? addresses.ring : ""; + // A RayNeo iO is one link (its address in both arm slots) and has no ring. + const glassesType = glassesTypeSetting.get(); + const ringAddress = ringMode !== "glasses" && glassesType === "g2" ? addresses.ring : ""; // Direct mode may unpair the ring from the glasses; remember to hand it // back once a later session runs in another mode. if (ringMode === "direct" && ringAddress) setBooleanSetting(RING_TAKEN_FROM_GLASSES_KEY, true); - this.appendLog( - `Using configured arms: R=${addresses.right} L=${addresses.left}${ringAddress ? ` ring=${ringAddress}` : ""}`, + this.appendLog(glassesType === "rayneo-io" + ? `Using configured RayNeo iO: ${addresses.right}` + : `Using configured arms: R=${addresses.right} L=${addresses.left}${ringAddress ? ` ring=${ringAddress}` : ""}`, ); let communicator: FaceclawCommunicatorBridge | null = null; @@ -1506,6 +1509,7 @@ class DashboardController { // sync needs the phone role, which the session borrows briefly for // each pull (GlassesSessionRing.beginRingHealthWindowLocked). ringRole: ringMode === "direct" ? "glasses" : "phone", + glassesType, }); this.communicator = communicator; // Size the compositor before anything else is awaited: windows opened @@ -1513,7 +1517,10 @@ class DashboardController { // configure their surfaces through this.communicator, and the Java call // queue is FIFO, so enqueuing the screen size first guarantees it lands // ahead of them. Mirrors ensurePreviewDisplay. - await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT); + // The iO shows 540x220: the layout stays the G2's and is cropped at the right and + // bottom (geometry.ts pins the band to the top-left while an iO is configured). + if (glassesType === "rayneo-io") await communicator.configureCompositorScreen(RAYNEO_IO_WIDTH, RAYNEO_IO_HEIGHT); + else await communicator.configureCompositorScreen(G2_LENS_WIDTH, G2_LENS_HEIGHT); await communicator.configureBrightness(getBrightnessPreferences()); this.offState = communicator.onStateChange((state) => { if (state.phase !== "connected") resetRingInputFilter(); diff --git a/app/g2/even-advertisement.ts b/app/g2/even-advertisement.ts index 84a9c3c0..d974c87e 100644 --- a/app/g2/even-advertisement.ts +++ b/app/g2/even-advertisement.ts @@ -30,7 +30,7 @@ export { bytesToHex, hexToBytes }; /** Even Realities' manufacturer-data company identifier (`"ER"` read little-endian). */ export const EVEN_COMPANY_IDENTIFIER = 0x5245; -export type EvenDeviceRole = "left" | "right" | "ring"; +export type EvenDeviceRole = "left" | "right" | "ring" | "rayneo-io"; /** True when the manufacturer data opens with Even's `"ER"` marker. */ export function hasEvenManufacturerSignature(mfg: Uint8Array): boolean { @@ -71,6 +71,14 @@ export function extractG2AdvertisementFlag(mfg: Uint8Array): number | null { return mfg[22]!; } +/** RayNeo iO manufacturer data (company 0x5716, little-endian first): `[07][MAC x6]...`, outside pairing mode. */ +export const RAYNEO_COMPANY_IDENTIFIER = 0x5716; + +export function extractRayNeoMac(mfg: Uint8Array): string | null { + if (mfg.length < 9 || mfg[0] !== 0x16 || mfg[1] !== 0x57 || mfg[2] !== 0x07) return null; + return Array.from(mfg.subarray(3, 9), (byte) => byte.toString(16).padStart(2, "0").toUpperCase()).join(":"); +} + /** * Which side a G2 temple advertisement belongs to, from the local name. * Null when the name carries neither or both markers. @@ -230,8 +238,32 @@ export function classifyAdvertisement(raw: RawAdvertisement): EvenAdvertisement const hasSignature = hasEvenManufacturerSignature(mfg); const looksLikeRing = /^EVEN R1([_ ]|$)/.test(upper); const looksLikeG2 = upper.includes("G2"); + const rayneoMac = extractRayNeoMac(mfg); + const looksLikeRayNeo = upper.startsWith("RAYNEO") || rayneoMac !== null; - if (!looksLikeRing && !looksLikeG2 && !hasSignature) return null; + if (!looksLikeRing && !looksLikeG2 && !hasSignature && !looksLikeRayNeo) return null; + + if (looksLikeRayNeo && !hasSignature) { + const address = normalizeMacAddress(raw.address); + return { + address, + name, + role: "rayneo-io", + serial: null, + embeddedMac: rayneoMac, + embeddedMacMismatch: !!rayneoMac && rayneoMac !== address, + 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: null, + }; + } const base = { address: normalizeMacAddress(raw.address), diff --git a/app/g2/pairing-candidates.ts b/app/g2/pairing-candidates.ts index b891581a..05076a5b 100644 --- a/app/g2/pairing-candidates.ts +++ b/app/g2/pairing-candidates.ts @@ -21,7 +21,7 @@ import { type ProximityZone, } from "./ble-proximity"; import { classifyAdvertisement, normalizeMacAddress, type EvenAdvertisement, type RawAdvertisement } from "./even-advertisement"; -import { glassesImagePath, R1_IMAGE_PATH } from "./glasses-artwork"; +import { GENERIC_G2_IMAGE_PATH, glassesImagePath, R1_IMAGE_PATH } from "./glasses-artwork"; import { colorwayDisplayName, colorwaySwatchHex, @@ -56,6 +56,15 @@ export type GlassesPairCandidate = { mismatchWarning: string | null; }; +/** A RayNeo iO: one device, one address (no arms to join). */ +export type RayNeoCandidate = { + id: string; + advertisement: EvenAdvertisement; + rssi: number | null; + proximity: ProximityEstimate | null; + lastSeenMs: number; +}; + export type RingCandidate = { id: string; advertisement: EvenAdvertisement; @@ -176,7 +185,7 @@ export class DiscoveryAggregator { pairs(): GlassesPairCandidate[] { const groups = new Map(); for (const record of this.byAddress.values()) { - if (record.role === "ring") continue; + if (record.role === "ring" || record.role === "rayneo-io") continue; const canonical = serialKey(record.serial); const key = canonical ? `serial:${canonical}` : `arm:${record.address}`; const group = groups.get(key) ?? { left: null, right: null }; @@ -225,6 +234,22 @@ export class DiscoveryAggregator { } /** Every R1 ring seen, closest first. */ + rayneos(): RayNeoCandidate[] { + const found: RayNeoCandidate[] = []; + for (const record of this.byAddress.values()) { + if (record.role !== "rayneo-io") continue; + const rssi = record.smoothedRssi != null ? Math.round(record.smoothedRssi) : null; + found.push({ + id: `rayneo:${record.address}`, + advertisement: stripSmoothing(record), + rssi, + proximity: estimateProximity(rssi, GLASSES_CALIBRATION), + lastSeenMs: record.seenAtMs, + }); + } + return sortedByProximity(found, (candidate) => candidate.proximity, (candidate) => candidate.id); + } + rings(): RingCandidate[] { const rings: RingCandidate[] = []; for (const record of this.byAddress.values()) { @@ -281,7 +306,7 @@ export function isPreviouslyPaired(name: string | null | undefined, pairedSerial export type PairingRowPresentation = { id: string; - kind: "glasses" | "ring"; + kind: "glasses" | "ring" | "rayneo-io"; /** "Even G2 B" / "Even R1" / fallback product name. */ modelTitle: string; /** The serial (glasses) or local name (ring). */ @@ -416,6 +441,28 @@ export function ringRow(ring: RingCandidate, context: RowContext = {}): PairingR }; } +export function rayneoRow(candidate: RayNeoCandidate, context: { pairedAddress?: string; nearestId?: string | null } = {}): PairingRowPresentation { + const ad = candidate.advertisement; + const isPaired = !!context.pairedAddress && normalizeMacAddress(context.pairedAddress) === ad.address; + const isNearest = !!context.nearestId && context.nearestId === candidate.id; + const warnings: string[] = []; + if (ad.embeddedMacMismatch) warnings.push(`It advertises ${ad.embeddedMac} but was heard as ${ad.address}.`); + return { + id: candidate.id, + kind: "rayneo-io", + modelTitle: "RayNeo iO", + deviceName: ad.name || ad.address, + variantSummary: null, + colorway: null, + swatchHex: null, + imagePath: GENERIC_G2_IMAGE_PATH, + hasVariantImage: false, + ...presentationBase(candidate.proximity, isNearest, isPaired, warnings), + armsSummary: "Needs the Faceclaw custom firmware. First time: put it in pairing mode (on the charger, hold the button ~5 s until the light blinks blue).", + canSelect: true, + }; +} + /** Convenience: a colourway's display text, for rows that show the swatch alone. */ export function colorwayLabel(colorway: GlassesColorway | null): string { return colorway ? colorwayDisplayName(colorway) : ""; diff --git a/app/graphics/image.ts b/app/graphics/image.ts index f36a0dd7..f03c6f78 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -6,6 +6,9 @@ import { wrapText } from "./textwrap"; export const G2_LENS_WIDTH = 640; export const G2_LENS_HEIGHT = 480; +/** RayNeo iO screen (per eye) with the Faceclaw firmware: 540x220. */ +export const RAYNEO_IO_WIDTH = 540; +export const RAYNEO_IO_HEIGHT = 220; const DEFAULT_CORNER_RADIUS = 8; /** diff --git a/app/native/faceclaw-communicator.ios.ts b/app/native/faceclaw-communicator.ios.ts index 377dbe07..712a4da0 100644 --- a/app/native/faceclaw-communicator.ios.ts +++ b/app/native/faceclaw-communicator.ios.ts @@ -1,3 +1,4 @@ +import type { GlassesType } from "../ui/dashboard-settings"; import type { RingInput } from "../g2/ring-input"; import { File, knownFolders, path, type ImageSource } from "@nativescript/core"; import { REQUIRED_FACECLAW_FIRMWARE_VERSION, type FirmwareInfo } from "../g2/firmware-compat"; @@ -241,7 +242,10 @@ export class FaceclawCommunicatorBridge { return FaceclawCommunicatorBridge.active; } - constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string }) { + constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string; glassesType?: GlassesType }) { + if (addresses.glassesType === "rayneo-io") { + throw new Error("RayNeo iO glasses are not supported on iOS yet."); + } this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress( String(addresses.right ?? "").toUpperCase(), String(addresses.left ?? "").toUpperCase(), diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 1f7f3965..80d13b24 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -1,3 +1,4 @@ +import type { GlassesType } from "../ui/dashboard-settings"; import type { RingInput } from "../g2/ring-input"; import { ImageSource, Utils } from "@nativescript/core"; import * as frameTimings from "./frame-timings"; @@ -161,7 +162,7 @@ export class FaceclawCommunicatorBridge { private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>(); private readonly previewAnimationListeners = new Set<() => void>(); - constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string }) { + constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string; glassesType?: GlassesType }) { const context = Utils.android.getApplicationContext(); if (!context) throw new Error("Android application context unavailable"); @@ -173,6 +174,8 @@ export class FaceclawCommunicatorBridge { ); // The shared session halts on any other firmware ("incompatible-firmware"). 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(); // 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({ diff --git a/app/phone-ui/pairing-view-model.ts b/app/phone-ui/pairing-view-model.ts index 3323a20a..7336f44e 100644 --- a/app/phone-ui/pairing-view-model.ts +++ b/app/phone-ui/pairing-view-model.ts @@ -10,11 +10,14 @@ import { DiscoveryAggregator, glassesRow, nearestCandidateId, + rayneoRow, ringRow, type GlassesPairCandidate, type PairingRowPresentation, + type RayNeoCandidate, type RingCandidate, } from "../g2/pairing-candidates"; +import { glassesTypeSetting } from "../ui/dashboard-settings"; import { DeviceDiscoveryBridge } from "../native/device-discovery"; import { setPreviewOnlyMode } from "./onboarding-state"; @@ -65,6 +68,8 @@ export class PairingViewModel extends Observable { private _ringRows: PairingRowItem[] = []; private selectedPair: GlassesPairCandidate | null = null; private selectedRing: RingCandidate | null = null; + /** A RayNeo iO instead of a G2 pair (the two are exclusive). */ + private selectedIo: RayNeoCandidate | null = null; private readonly previouslyPairedSerial: string | null; private readonly previouslyPairedRing: string; @@ -181,7 +186,7 @@ export class PairingViewModel extends Observable { get instructions(): string { return ( - "Select your G2 glasses and R1 ring below. Wear the glasses or take them out of the case, " + + "Select your G2 glasses and R1 ring, or your RayNeo iO, 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." ); @@ -249,11 +254,14 @@ export class PairingViewModel extends Observable { } get selectionVisibility(): "visible" | "collapse" { - return this.selectedPair || this.selectedRing ? "visible" : "collapse"; + return this.selectedPair || this.selectedRing || this.selectedIo ? "visible" : "collapse"; } get selectionSummary(): string { const parts: string[] = []; + if (this.selectedIo) { + parts.push(`RayNeo iO · ${this.selectedIo.advertisement.name} · ${this.selectedIo.advertisement.address}`); + } if (this.selectedPair) { const identity = this.selectedPair.identity; const label = identity ? identity.displayName : "Even Realities G2"; @@ -273,7 +281,7 @@ export class PairingViewModel extends Observable { } get primaryEnabled(): boolean { - return !!this.selectedPair || (!this.onboarding && !!this.selectedRing); + return !!this.selectedPair || !!this.selectedIo || (!this.onboarding && !!this.selectedRing); } get secondaryLabel(): string { @@ -287,7 +295,14 @@ export class PairingViewModel extends Observable { readonly onRowTap = (args: EventData): void => { const row = (args.object as View).bindingContext as PairingRowItem | undefined; if (!row) return; - if (row.kind === "glasses") { + if (row.kind === "rayneo-io") { + const io = this.aggregator.rayneos().find((candidate) => candidate.id === row.id) ?? null; + this.selectedIo = this.selectedIo?.id === io?.id ? null : io; + if (this.selectedIo) { + this.selectedPair = null; + this.selectedRing = null; + } + } else if (row.kind === "glasses") { const pair = this.aggregator.pairs().find((candidate) => candidate.id === row.id) ?? null; if (!pair) return; if (!row.canSelect) { @@ -295,14 +310,39 @@ export class PairingViewModel extends Observable { return; } this.selectedPair = this.selectedPair?.id === pair.id ? null : pair; + if (this.selectedPair) this.selectedIo = null; } else { const ring = this.aggregator.rings().find((candidate) => candidate.id === row.id) ?? null; this.selectedRing = this.selectedRing?.id === ring?.id ? null : ring; + if (this.selectedRing) this.selectedIo = null; } this.refresh(); }; onPrimaryTap(): void { + const io = this.selectedIo; + if (io) { + // One link: the same address stands for both arms; no ring. + const address = io.advertisement.address; + saveDeviceAddresses({ right: address, left: address, ring: "" }); + glassesTypeSetting.set("rayneo-io"); + savePairedGlassesIdentity({ + serial: io.advertisement.embeddedMac ?? address, + leftName: io.advertisement.name, + rightName: io.advertisement.name, + leftAddress: address, + rightAddress: address, + ringName: "", + ringAddress: "", + pairedAtMs: Date.now(), + }); + this.dispose(); + if (this.onboarding) setPreviewOnlyMode(false); + // The G2 firmware check/flash pages don't apply; the session reports + // incompatible firmware if the iO lacks the Faceclaw build. + finishOnboardingNavigation(); + return; + } const pair = this.selectedPair; const ring = this.selectedRing; if (!pair && !ring) return; @@ -317,6 +357,7 @@ export class PairingViewModel extends Observable { ring: ring?.advertisement.address ?? existing.ring, }; saveDeviceAddresses(addresses); + if (pair) glassesTypeSetting.set("g2"); if (pair) { savePairedGlassesIdentity({ serial: pair.serial ?? "", @@ -387,6 +428,10 @@ export class PairingViewModel extends Observable { this.aggregator.prune(Date.now(), DEFAULT_STALE_AFTER_MS); const pairs = this.aggregator.pairs(); const rings = this.aggregator.rings(); + const rayneos = this.aggregator.rayneos(); + if (this.selectedIo) { + this.selectedIo = rayneos.find((candidate) => candidate.id === this.selectedIo!.id) ?? null; + } // Keep the selection pointing at live data (addresses can only change if a // side was replaced); drop it if the pair disappeared — or if it degraded @@ -402,13 +447,24 @@ export class PairingViewModel extends Observable { const nearestPair = nearestCandidateId(pairs); const nearestRing = nearestCandidateId(rings); - this._glassesRows = pairs.map((pair) => - toRowItem( - glassesRow(pair, { pairedSerial: this.previouslyPairedSerial, nearestId: nearestPair }), - this.selectedPair?.id === pair.id, - this.onRowTap, + const pairedAddress = glassesTypeSetting.get() === "rayneo-io" ? loadDeviceAddresses().right : ""; + const nearestIo = nearestCandidateId(rayneos); + this._glassesRows = [ + ...rayneos.map((candidate) => + toRowItem( + rayneoRow(candidate, { pairedAddress, nearestId: nearestIo }), + this.selectedIo?.id === candidate.id, + this.onRowTap, + ), ), - ); + ...pairs.map((pair) => + toRowItem( + glassesRow(pair, { pairedSerial: this.previouslyPairedSerial, nearestId: nearestPair }), + this.selectedPair?.id === pair.id, + this.onRowTap, + ), + ), + ]; this._ringRows = rings.map((ring) => toRowItem( ringRow(ring, { pairedRingAddress: this.previouslyPairedRing, nearestId: nearestRing }), @@ -423,13 +479,14 @@ export class PairingViewModel extends Observable { this.status = `Scan failed: ${this.scanFailure}`; } else if (!this._scanning) { this.status = "Scan paused."; - } else if (!pairs.length && !rings.length) { + } else if (!pairs.length && !rings.length && !rayneos.length) { this.status = "Scanning…"; } else { const bits = [ complete ? `${complete} ${complete === 1 ? "pair" : "pairs"} ready` : null, partial ? `${partial} with one arm heard` : null, rings.length ? `${rings.length} ${rings.length === 1 ? "ring" : "rings"}` : null, + rayneos.length ? `${rayneos.length} RayNeo iO` : null, ].filter(Boolean); this.status = `Scanning… ${bits.join(" · ")}. Tap a pair to select it.`; } diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 69381c2e..b158c3fb 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -153,15 +153,19 @@ type ConfigSettingEnumOptions = Confi normalize?: (value: string | null | undefined) => TValue; /** Dynamic availability check used by enum picker rows. */ isDisabled?: (value: TValue) => boolean; + /** A value forced regardless of storage while it returns one (stored choice untouched). */ + pinned?: () => TValue | undefined; }; export class ConfigSettingEnum extends ConfigSetting { readonly values: readonly TValue[]; private readonly normalizer: (value: string | null | undefined) => TValue; private readonly disabledPredicate: (value: TValue) => boolean; + protected readonly pinned: (() => TValue | undefined) | undefined; constructor(options: ConfigSettingEnumOptions) { super(options); + this.pinned = options.pinned; if (options.reportInAnalytics !== false) fixedChoiceSettings.set(options.storageKey, this); this.values = options.values; this.disabledPredicate = options.isDisabled ?? (() => false); @@ -177,6 +181,8 @@ export class ConfigSettingEnum({ */ 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 const glassesTypeSetting = new ConfigSettingEnum({ + id: "glasses-type", + label: "Glasses", + storageKey: "deviceType", + defaultValue: "g2", + values: ["g2", "rayneo-io"], + reportInAnalytics: false, +}); + const APP_SWITCHER_POSITION_LABELS: Record = { left: "Left", right: "Right", @@ -380,6 +398,8 @@ export const appSwitcherPositionSetting = new ConfigSettingEnum (glassesTypeSetting.get() === "rayneo-io" ? "right" : undefined), formatValue: (value) => APP_SWITCHER_POSITION_LABELS[value] ?? value, description: "Where the open-app icons go. Left and right take a column beside windows; bottom puts a row beneath the window, which makes full-height windows a little shorter; popup shows them only while you're in the app switcher, in a box over the dimmed window. Bottom and popup leave room at the sides to set the display's depth. Open apps reopen at their new size when a change resizes windows.", @@ -395,6 +415,8 @@ class ConfigSettingEnumWithLegacy extends ConfigSettingEn } get(): TValue { + const pinned = this.pinned?.(); + if (pinned !== undefined) return pinned; return getStringSetting(this.storageKey, "") === "" ? this.legacyValue() : super.get(); } } @@ -456,6 +478,8 @@ export const displayLayoutSetting = new ConfigSettingEnumWithLegacy (glassesTypeSetting.get() === "rayneo-io" ? "band-top" : undefined), formatValue: displayLayoutLabel, isDisabled: (value) => value === "fullscreen" && !fullScreenAvailable(), description: diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt index 75d47d36..88c252fa 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt @@ -28,7 +28,18 @@ class CfwTransport(private val platform: ProtocolPlatform) { fun encode(message: ByteArray?, streamId: Int, lenses: Int, mtu: Int): List = lock.withLock { validate(message, streamId, lenses, mtu) - val input = message!! + g2Packets(encodeRecord(message!!, lenses), streamId, mtu) + } + + /** + * One record, `[flags][wire length u16][decoded CRC u16][body]`, compressed with this + * transport's persistent context. The packet envelope is the caller's: [encode] wraps it + * in G2 TPL packets, [RayNeoIoProtocol.cfwPackets] in RayNeo iO business-0xF0 packets. + */ + fun encodeRecord(message: ByteArray, lenses: Int): ByteArray = + lock.withLock { + require(message.size <= MAX_MESSAGE && lenses in 1..BOTH) { "invalid CFW record parameters" } + val input = message if (previousLenses != lenses) reset() val reset = resetPending val encoder = deflater ?: platform.createDeflater().also { deflater = it } @@ -44,7 +55,7 @@ class CfwTransport(private val platform: ProtocolPlatform) { resetPending = false previousLenses = lenses } - frameRecord(body, crc(input), streamId, flags, mtu) + record(body, crc(input), flags) } companion object { @@ -71,18 +82,12 @@ class CfwTransport(private val platform: ProtocolPlatform) { @JvmStatic fun frame(message: ByteArray?, streamId: Int, lenses: Int, mtu: Int): List { validate(message, streamId, lenses, mtu) - return frameRecord(message!!, crc(message), streamId, lenses or RESET_CONTEXT, mtu) + return g2Packets(record(message!!, crc(message), lenses or RESET_CONTEXT), streamId, mtu) } - private fun frameRecord( - body: ByteArray, - checksum: Int, - streamId: Int, - flags: Int, - mtu: Int, - ): List { - val lenses = flags and BOTH - val capacity = minOf(252, mtu - 14) + /** `[flags][wire length u16][decoded CRC u16][body]`, shared by every packet envelope. */ + @JvmStatic + fun record(body: ByteArray, checksum: Int, flags: Int): ByteArray { val stream = ByteArray(body.size + 5) stream[0] = flags.toByte() stream[1] = body.size.toByte() @@ -90,6 +95,13 @@ class CfwTransport(private val platform: ProtocolPlatform) { stream[3] = checksum.toByte() stream[4] = (checksum ushr 8).toByte() body.copyInto(stream, 5) + return stream + } + + /** G2 TPL packets (SID 0xf0) carrying one record: options byte = lenses | RESET | END. */ + private fun g2Packets(stream: ByteArray, streamId: Int, mtu: Int): List { + val lenses = stream[0].toInt() and BOTH + val capacity = minOf(252, mtu - 14) val packets = mutableListOf() for (offset in stream.indices step capacity) { val count = minOf(capacity, stream.size - offset) diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionBrightness.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionBrightness.kt index 5a3c0418..3e9a4463 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionBrightness.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionBrightness.kt @@ -4,6 +4,11 @@ package com.faceclaw.app * queued state and sensor freshness do not. Private commands are ordered with * frames, so wake starts on the intended PRESENT, never on an older black frame. */ internal fun GlassesSessionCore.enqueueBrightnessLocked(frameVisible: Boolean? = null) { + if (isIo) { + // The iO has no CFW brightness; ioMaintain drives the launcher's (and the screen) instead. + ioNoteFrameVisibleLocked(frameVisible) + return + } if (!customFirmwareDetected || !sessionReady || !fixedLayoutCreated || shutdownRequested) return val visible = frameVisible ?: brightnessSentVisible ?: return val target = brightnessPolicy.target diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt index 717f2004..31e2192f 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt @@ -345,6 +345,8 @@ internal fun GlassesSessionCore.releaseFaceclawFramebufferLease(): Boolean { /** The address of a configured arm the OS no longer holds a bond for, or null. */ internal fun GlassesSessionCore.firstUnpairedArm(): String? { + // The iO identifies the phone by its protocol id, not an OS bond. + if (isIo) return null if (!link.isBonded(rightAddress)) return rightAddress if (!link.isBonded(leftAddress)) return leftAddress return null diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt index e0af63bb..a471bc4c 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt @@ -107,6 +107,11 @@ class GlassesSessionCore( @Volatile internal var listener: FaceclawBleCommunicatorListener? = null + /** G2 unless [useRayNeoIo] was called before [start]. */ + @Volatile internal var glassesType = GlassesType.G2 + /** RayNeo iO link state (GlassesSessionIo.kt); guarded by [monitor]. */ + internal val io = IoSessionState() + /** * Optional tap for relay frames the session does not interpret: notifications on the * right arm's data characteristic whose first two bytes are 'A','N' (the iOS ANCS @@ -543,6 +548,28 @@ class GlassesSessionCore( requiredFirmwareRevision = revision } + /** + * Drive a RayNeo iO instead of a G2: one BLE link (pass its address as both arms; no + * ring), the iO handshake and page lease, and the Faceclaw display lists of the + * faceclaw-rayneo-cfw firmware. [phoneId] is this phone's stable 6-byte protocol identity: + * the glasses bind to the first one that authenticates and refuse others until they are + * put in pairing mode. Call before [start]. + */ + fun useRayNeoIo(phoneId: ByteArray, phoneName: String, manufacturer: String, model: String) { + require(phoneId.size == 6) { "RayNeo iO phone id must be 6 bytes" } + require(rightAddress.equals(leftAddress, ignoreCase = true)) { "RayNeo iO uses one address for both arms" } + monitor.withLock { + glassesType = GlassesType.RAYNEO_IO + io.phoneId = phoneId.copyOf() + io.phoneName = phoneName.ifEmpty { "Faceclaw" } + io.manufacturer = manufacturer.ifEmpty { "Android" } + io.model = model.ifEmpty { "Android" } + } + } + + /** True when this session drives a RayNeo iO. */ + fun isRayNeoIo(): Boolean = isIo + /** Start the worker (through the host); false when it was already running. */ fun start(): Boolean { monitor.withLock { @@ -575,7 +602,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()) { + if (!cfwCleanupDelivered && !sendCfwCleanup() && !isIo) { releaseFaceclawFramebufferLease() } monitor.withLock { @@ -594,6 +621,14 @@ class GlassesSessionCore( host.joinWorker(5_000) ringDone?.await(5_000) ringWorkerDone = null + if (isIo && link.isConnected(rightAddress)) { + // Otherwise the blank page stays up until the firmware's 120 s lease runs out. + try { + ioWriteControl(RayNeoIoProtocol.CTRL_PAGE_CLOSE) + } catch (t: Throwable) { + logLine("RayNeo iO page close failed: " + safeMessage(t)) + } + } monitor.withLock { resetSessionStateLocked() clearAllMessagesLocked("disconnect") @@ -1533,6 +1568,7 @@ class GlassesSessionCore( // EvenHub page lifecycle fun sendShutdown(exitMode: Int): Boolean { + if (isIo) return ioSuspend() return sendShutdownInternal(exitMode, true) } @@ -1624,6 +1660,7 @@ class GlassesSessionCore( * reconnecting Bluetooth here would defeat the power-saving mode. */ fun suspendEvenHubSession(): Boolean { + if (isIo) return ioSuspend() // 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). @@ -1647,6 +1684,7 @@ class GlassesSessionCore( * the desired frame. */ fun resumeEvenHubSession(): Boolean { + if (isIo) return ioResume() var claimGeneration = 0 monitor.withLock { if (!running || !sessionReady || chargingMode) { @@ -1727,7 +1765,7 @@ class GlassesSessionCore( continue } logWarn("Attempting to connect") - connectLoopOnce() + if (isIo) ioConnectLoopOnce() else connectLoopOnce() continue } @@ -1754,6 +1792,10 @@ class GlassesSessionCore( return } val uuid = characteristicUuid.lowercase() + if (isIo) { + ioOnNotification(address, uuid, data) + return + } if (isDirectRingNotification(address, uuid)) { handleDirectRingNotification(uuid, data) return @@ -1768,28 +1810,7 @@ class GlassesSessionCore( if (data.size >= 7 && (data[6].toInt() and 255) == CfwTransport.SID) { val acks = CfwTransport.parseAcks(data) ?: return monitor.withLock { - lastIncomingAtMs = now() - val arm = armIndex(address) - if (arm >= 0) lastArmCfwAckAtMs[arm] = lastIncomingAtMs - for (ack in acks) { - for (message in inFlightMessages) { - val ingress = if (message.isLeftArmMessage) leftAddress else rightAddress - if (message.sid == CfwTransport.SID && address.equals(ingress, ignoreCase = true) && message.magic == ack.streamId) { - message.acceptCfwAck(ack) - if (ack.nack) ConnectionCounters.increment("g2.cfw-nack") - logLine("CFW " + (if (ack.nack) "NACK" else "ACK") + " id=" + ack.streamId - + " ordinal=" + ack.messageId + " lens=" + ack.lens + " txseq=" + (data[2].toInt() and 255) - + " redundant=" + (ack !== acks[0]) - + " size=" + ack.size + " crc=" + ack.checksum - + " expected=" + message.message.size + "/" + message.cfwChecksum - + " ackedLenses=" + message.cfwAckLenses - + " ageMs=" + (lastIncomingAtMs - message.sentAtMs)) - message.ackPayload = data.copyOf() - break - } - } - } - drainCfwAcknowledgementsLocked() + acceptCfwAcksLocked(address, acks, data[2].toInt() and 255, data) } interruptibleSleep.interrupt() return @@ -2106,6 +2127,35 @@ class GlassesSessionCore( } } + /** + * Apply CFW ACKs/NACKs from [address] (the G2's `aa 12` frames, or the iO's 9-byte bodies) + * to the in-flight messages they name, then complete what they finished. + */ + internal fun acceptCfwAcksLocked(address: String, acks: Array, txSeq: Int, payload: ByteArray) { + lastIncomingAtMs = now() + val arm = armIndex(address) + if (arm >= 0) lastArmCfwAckAtMs[arm] = lastIncomingAtMs + for (ack in acks) { + for (message in inFlightMessages) { + val ingress = if (message.isLeftArmMessage) leftAddress else rightAddress + if (message.sid == CfwTransport.SID && address.equals(ingress, ignoreCase = true) && message.magic == ack.streamId) { + message.acceptCfwAck(ack) + if (ack.nack) ConnectionCounters.increment("g2.cfw-nack") + logLine("CFW " + (if (ack.nack) "NACK" else "ACK") + " id=" + ack.streamId + + " ordinal=" + ack.messageId + " lens=" + ack.lens + " txseq=" + txSeq + + " redundant=" + (ack !== acks[0]) + + " size=" + ack.size + " crc=" + ack.checksum + + " expected=" + message.message.size + "/" + message.cfwChecksum + + " ackedLenses=" + message.cfwAckLenses + + " ageMs=" + (lastIncomingAtMs - message.sentAtMs)) + message.ackPayload = payload.copyOf() + break + } + } + } + drainCfwAcknowledgementsLocked() + } + override fun onConnectionStateChange(address: String?, connected: Boolean) { if (address == null) { return @@ -2141,11 +2191,14 @@ class GlassesSessionCore( monitor.withLock { if (address.equals(rightAddress, ignoreCase = true)) { rightConnected = connected + // The iO's one link stands for both arms. + if (isIo) leftConnected = connected } else if (address.equals(leftAddress, ignoreCase = true)) { leftConnected = connected } else { return } + if (!connected && isIo) io.reassembler.reset() if (!connected) { // Before the session is ready, this is an attempt failing (or an arm // dropping mid-setup): the connect sequence notices on its own and diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionIo.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionIo.kt new file mode 100644 index 00000000..eb4c83d1 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionIo.kt @@ -0,0 +1,544 @@ +package com.faceclaw.app + +import com.faceclaw.app.GlassesSessionCore.Companion.hex +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 } + +/** + * RayNeo iO session state ([GlassesSessionCore.io]); guarded by the core's monitor. + * + * The iO is one SoC on ONE BLE link that drives both eyes, so the core runs it with the same + * address in both arm slots: CFW messages keep going to the "left" arm, per-arm bookkeeping + * resolves to index 0, and the firmware ACKs each lens of a record separately, exactly what + * the existing window expects. What differs lives here: the AA55 envelope, the TLV handshake + * and guest authentication, IDENT-based capability detection, and the page lease + * (PAGE_OPEN + KEEPALIVE) that stands in for the G2's EvenHub page and framebuffer lease. + * Wire codecs: [RayNeoIoProtocol]; firmware: faceclaw-rayneo-cfw (docs/FACECLAW.md). + */ +internal class IoSessionState { + var phoneId = ByteArray(6) + var phoneName = "Faceclaw" + var manufacturer = "Android" + var model = "Android" + + val reassembler = RayNeoIoProtocol.FrameReassembler() + var messageNumber = Random.nextInt(0, 0x10000) + var launcherSequence = 0 + + var versionReply: RayNeoIoProtocol.VersionReply? = null + var authReply: ByteArray? = null + var ident: RayNeoIoProtocol.Ident? = null + var osVersion = "" + var maxChunk = RayNeoIoProtocol.DEFAULT_MAX_CHUNK + + /** Last PAGE state the glasses reported: 1 open, 0 closed, -1 none yet this link. */ + var pageState = -1 + var pageReason = 0 + var pageReplies = 0 + /** Whether the phone wants the page open (false while suspended or after the wearer closed it). */ + var pageWanted = true + var pageRequestSentAtMs = 0L + var lastKeepaliveAtMs = 0L + var lastStatusAtMs = 0L + var refusals = 0 + + /** Whether Faceclaw's latest frame is meant to be seen (false for its blanked frames). */ + var displayVisible = false + var brightnessSentLevel = -1 + var lastBrightnessAtMs = 0L + /** Last `screen_status` push: 1 on, 0 off, -1 unknown. */ + var screenStatus = -1 + + fun resetLink() { + reassembler.reset() + versionReply = null + authReply = null + ident = null + pageState = -1 + pageReason = 0 + pageRequestSentAtMs = 0 + lastKeepaliveAtMs = 0 + lastStatusAtMs = 0 + brightnessSentLevel = -1 + lastBrightnessAtMs = 0 + screenStatus = -1 + } +} + +internal object IoTimings { + const val VERSION_REPLY_MS = 4_000L + const val AUTH_REPLY_MS = 4_000L + const val OS_VERSION_MS = 1_500L + const val IDENT_MS = 3_000L + const val PAGE_REPLY_MS = 4_000L + /** Well inside the firmware's 120 s page lease. */ + const val KEEPALIVE_MS = 30_000L + const val STATUS_MS = 60_000L + const val PAGE_RETRY_MS = 3_000L + /** Retry cadence while the glasses refuse this phone id (waiting for pairing mode). */ + const val PAIRING_RETRY_MS = 2_000L + const val PAGE_REASON_USER = 4 + /** While Faceclaw shows content, re-assert the screen this often (the stock auto-off is short). */ + const val SCREEN_REFRESH_MS = 5_000L +} + +/** The glasses hung up at the version request: bound to another phone id, or not in pairing mode. */ +internal class IoPairingRefusedException : IllegalStateException("glasses hung up at the version request") + +/** CFW message types the iO firmware implements (fc_glue.c + raster cleanup). */ +internal fun ioSupportsCfwType(type: Int): Boolean = + type == CFW_MSG_CLEANUP || type == CFW_MSG_UPLOAD_RESOURCE || type == CFW_MSG_EVICT_RESOURCE || + type == CFW_MSG_DRAW_CALLS || type == CFW_MSG_SET_ROOT_DISPLAY_LIST || + type == CFW_MSG_PRESENT || type == CFW_MSG_CREATE_SURFACE + +internal val GlassesSessionCore.isIo: Boolean get() = glassesType == GlassesType.RAYNEO_IO + +/** Messages the iO cannot take (stock EvenHub/settings traffic, the G2's hardware CFW modes). */ +internal fun GlassesSessionCore.ioSupportsMessage(message: OutboundMessage): Boolean = + message.sid == CfwTransport.SID && message.message.isNotEmpty() && ioSupportsCfwType(message.message[0].toInt() and 255) + +// --------------------------------------------------------------------------------------------- +// Writes + +private fun GlassesSessionCore.ioNextMessageNumberLocked(): Int { + val n = io.messageNumber and 0xffff + io.messageNumber = (io.messageNumber + 1) and 0xffff + return n +} + +/** One AA55 frame on the iO write characteristic; worker thread, outside the monitor. */ +internal fun GlassesSessionCore.ioWriteFrame(business: Int, payload: ByteArray): Boolean { + val frame = monitor.withLock { RayNeoIoProtocol.encodeFrame(ioNextMessageNumberLocked(), business, payload) } + return link.writeFrames( + rightAddress, RayNeoIoBle.WRITE_CHAR_UUID, + RayNeoIoProtocol.writes(frame, link.negotiatedMtu(rightAddress)), + GattWriteMode.WITHOUT_RESPONSE, ConnectionOptions.WRITE_TIMEOUT_MS, + ) +} + +internal fun GlassesSessionCore.ioWriteControl(ctrl: Int): Boolean = + ioWriteFrame(RayNeoIoProtocol.BUSINESS_CFW, RayNeoIoProtocol.controlPacket(ctrl)) + +private fun GlassesSessionCore.ioWriteLauncher(type: Int, json: String): Boolean { + val sequence = monitor.withLock { + io.launcherSequence = if (io.launcherSequence >= 0xffff) 1 else io.launcherSequence + 1 + io.launcherSequence + } + val envelope = RayNeoIoProtocol.Envelope(type, json, sequence = sequence, mode = RayNeoIoProtocol.MODE_REQUEST) + return ioWriteFrame(RayNeoIoProtocol.BUSINESS_LAUNCHER, RayNeoIoProtocol.encodeEnvelope(envelope)) +} + +internal fun GlassesSessionCore.ioRequestStatus(): Boolean = + ioWriteLauncher(RayNeoIoProtocol.LAUNCHER_GENERAL_STATUS, RayNeoIoProtocol.GENERAL_STATUS_REQUEST) + +/** From the image path (enqueueBrightnessLocked): a visible frame wakes the screen at once. */ +internal fun GlassesSessionCore.ioNoteFrameVisibleLocked(frameVisible: Boolean?) { + if (frameVisible == null) return + if (frameVisible && !io.displayVisible) io.lastBrightnessAtMs = 0 + io.displayVisible = frameVisible +} + +/** + * The writes for one queued CFW message: the shared record (persistent deflate context, as + * on the G2) in `[opt][stream][chunk]` packets, each its own AA55 frame. The stream byte is + * the message's magic, which the firmware echoes in its per-lens ACKs. + */ +internal fun GlassesSessionCore.ioCfwWrites(message: OutboundMessage, mtu: Int): List { + val transport = cfwTransports[0] + if (message.cfwRetries > 0) transport.reset() + val record = transport.encodeRecord(message.message, CfwTransport.BOTH) + return monitor.withLock { + RayNeoIoProtocol.cfwPackets(record, message.magic, io.maxChunk).flatMap { packet -> + RayNeoIoProtocol.writes(RayNeoIoProtocol.encodeFrame(ioNextMessageNumberLocked(), RayNeoIoProtocol.BUSINESS_CFW, packet), mtu) + } + } +} + +/** A message the iO can't run (see [ioSupportsMessage]): never sent, never waited on. */ +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") + } + logDebug("RayNeo iO: dropped unsupported message " + message.label + " sid=" + message.sid) +} + +// --------------------------------------------------------------------------------------------- +// Connect (worker thread) + +private fun GlassesSessionCore.ioAwait(timeoutMs: Long, what: String, ready: () -> Boolean) { + val deadline = now() + timeoutMs + monitor.withLock { + while (true) { + if (ready()) return + if (!running || userDisconnectRequested) throw IllegalStateException("stopped while waiting for $what") + if (!rightConnected) throw IoLinkLostException(what) + val remaining = deadline - now() + if (remaining <= 0) throw IllegalStateException("$what timed out") + monitor.awaitMs(minOf(remaining, 100L)) + } + } +} + +private class IoLinkLostException(what: String) : IllegalStateException("link lost while waiting for $what") + +internal fun GlassesSessionCore.ioConnectLoopOnce() { + monitor.withLock { + wearState = -1 + io.resetLink() + } + setStateDisplay("connecting", "Connecting to the glasses...") + ConnectionCounters.increment("io.connect-attempt") + val address = rightAddress + try { + if (!link.connect(address, ConnectionOptions.ARM_CONNECT_WINDOW_MS)) { + handleTransportFailure("glasses not found", "Can't find the glasses. They may be off, in their case, or out of range.") + return + } + link.requestHighPriority(address) + link.requestMtu(address, RayNeoIoBle.REQUESTED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS) + if (!link.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) throw IllegalStateException("discoverServices failed: $address") + if (!link.enableNotifications(address, RayNeoIoBle.NOTIFY_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) { + throw IllegalStateException("enableNotifications failed: $address") + } + link.requestLe2mPhy(address) + monitor.withLock { + rightConnected = true + leftConnected = true + } + + // Version exchange. Glasses bound to another phone id (or not in pairing mode) + // hang up ~100 ms after this request instead of answering. + val phone = monitor.withLock { io.phoneId.copyOf() } + logDebug("RayNeo iO: phone id " + hex(phone)) + if (!ioWriteFrame(RayNeoIoProtocol.BUSINESS_HANDSHAKE, + RayNeoIoProtocol.versionRequest(phone, io.phoneName, io.manufacturer, io.model))) { + throw IllegalStateException("version request write failed") + } + try { + ioAwait(IoTimings.VERSION_REPLY_MS, "version reply") { io.versionReply != null } + } catch (lost: IoLinkLostException) { + throw IoPairingRefusedException() + } + val reply = monitor.withLock { io.versionReply!! } + if (reply.accountBound) throw IoPairingRefusedException() + logLine("RayNeo iO: glasses " + hex(reply.glassesId) + " serial " + reply.serial + " protocol " + reply.protocolVersion) + + val random = Random.nextBytes(4) + if (!ioWriteFrame(RayNeoIoProtocol.BUSINESS_HANDSHAKE, RayNeoIoProtocol.guestAuthRequest(random, phone, reply.glassesId))) { + throw IllegalStateException("guest auth write failed") + } + ioAwait(IoTimings.AUTH_REPLY_MS, "guest auth reply") { io.authReply != null } + val authReply = monitor.withLock { io.authReply!! } + if (!RayNeoIoProtocol.verifyGuestAuthReply(authReply, phone, reply.glassesId)) { + throw IllegalStateException("guest auth proof mismatch") + } + monitor.withLock { io.refusals = 0 } + + // Battery and the OS version: answers arrive asynchronously; the version only labels + // the firmware info, so a missing one is not fatal. + ioRequestStatus() + ioWriteFrame(RayNeoIoProtocol.BUSINESS_OTA, + RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(RayNeoIoProtocol.OTA_VERSION, "{}"))) + try { + ioAwait(IoTimings.OS_VERSION_MS, "OS version") { io.osVersion.isNotEmpty() } + } catch (e: IoLinkLostException) { + throw e + } catch (_: IllegalStateException) { + } + + // Capability: the custom firmware answers QUERY with IDENT; stock firmware stays silent. + ioWriteControl(RayNeoIoProtocol.CTRL_QUERY) + val ident = try { + ioAwait(IoTimings.IDENT_MS, "IDENT") { io.ident != null } + monitor.withLock { io.ident } + } catch (e: IoLinkLostException) { + throw e + } catch (_: IllegalStateException) { + null + } + val os = monitor.withLock { io.osVersion.ifEmpty { "RayNeo iO" } } + val info = BleProtocol.FirmwareInfo(os, os, + if (ident != null && ident.faceclaw) "Faceclaw/" + ident.faceclawRevision else "") + logLine("RayNeo iO: " + os + ", " + (if (ident == null) "no IDENT (stock firmware)" + else ident.name + " " + ident.width + "x" + ident.height + " chunk " + ident.maxChunk + + " flags 0x" + ident.flags.toString(16) + " faceclaw " + ident.faceclawRevision)) + emitFirmwareInfo(info) + if (!info.isCompatible(requiredFirmwareRevision)) { + monitor.withLock { + // The worker loop halts on this and explains it (handleIncompatibleFirmware). + incompatibleFirmware = info + } + return + } + monitor.withLock { + io.maxChunk = ident!!.maxChunk.coerceIn(16, 4000) + io.pageWanted = true + io.pageRequestSentAtMs = now() + } + + ioWriteControl(RayNeoIoProtocol.CTRL_PAGE_OPEN) + try { + ioAwait(IoTimings.PAGE_REPLY_MS, "page open") { io.pageReplies > 0 } + } catch (e: IoLinkLostException) { + throw e + } catch (_: IllegalStateException) { + // The launcher may be busy; ioMaintain keeps asking. + } + + monitor.withLock { + sessionReady = true + shutdownRequested = false + customFirmwareDetected = true + clearAllMessagesLocked("session ready") + fixedLayoutCreated = io.pageState == 1 + displayedFingerprint = "" + lastAckAtMs = now() + lastIncomingAtMs = lastAckAtMs + lastConnectionOrInputAtMs = lastAckAtMs + lastSessionReadyAtMs = lastAckAtMs + imageRetryAfterMs = 0 + consecutiveAckTimeouts = 0 + cfwCleanupDelivered = false + lastCfwCleanupAckMagic = 0 + for (transport in cfwTransports) transport.reset() + io.lastKeepaliveAtMs = lastAckAtMs + io.lastStatusAtMs = lastAckAtMs + } + setStateDisplay("connected", "Connected.") + ConnectionCounters.increment("io.session-ready") + logLine("RayNeo iO session ready (page " + (if (fixedLayoutCreated) "open" else "pending") + ")") + } catch (refused: IoPairingRefusedException) { + val count = monitor.withLock { + io.refusals++ + reconnectAfterMs = now() + IoTimings.PAIRING_RETRY_MS + io.refusals + } + ConnectionCounters.increment("io.pairing-refused") + logLine("RayNeo iO refused this phone (" + count + "); waiting for pairing mode") + link.disconnect(address) + monitor.withLock { + rightConnected = false + leftConnected = false + } + setStateDisplay("retrying", + "The glasses aren't accepting this phone yet. Put them in pairing mode: on the charger, " + + "hold the button about 5 seconds until the light blinks blue.") + } catch (t: Throwable) { + logLine("RayNeo iO connect failed: " + safeMessage(t)) + handleTransportFailure("connect failed") + } +} + +// --------------------------------------------------------------------------------------------- +// Session upkeep (worker thread, from driveSession, outside the monitor) + +/** Page open/close requests, keepalives, status polls and the screen (launcher brightness). */ +internal fun GlassesSessionCore.ioMaintain() { + var ctrl = -1 + var status = false + var brightness = -1 + monitor.withLock { + if (!sessionReady) return + val now = now() + if (io.pageWanted && !fixedLayoutCreated && now - io.pageRequestSentAtMs >= IoTimings.PAGE_RETRY_MS) { + ctrl = RayNeoIoProtocol.CTRL_PAGE_OPEN + io.pageRequestSentAtMs = now + } else if (!io.pageWanted && io.pageState == 1 && now - io.pageRequestSentAtMs >= IoTimings.PAGE_RETRY_MS) { + ctrl = RayNeoIoProtocol.CTRL_PAGE_CLOSE + io.pageRequestSentAtMs = now + } else if (io.pageWanted && fixedLayoutCreated && now - io.lastKeepaliveAtMs >= IoTimings.KEEPALIVE_MS) { + ctrl = RayNeoIoProtocol.CTRL_KEEPALIVE + io.lastKeepaliveAtMs = now + } + if (now - io.lastStatusAtMs >= IoTimings.STATUS_MS) { + status = true + io.lastStatusAtMs = now + } + // Faceclaw decides when the display is dark (blanked frames); while it shows + // content, keep the iO's screen on and at Faceclaw's brightness. + val level = RayNeoIoProtocol.brightnessLevel(brightnessPolicy.target) + if (fixedLayoutCreated && io.pageWanted && io.displayVisible && !shutdownRequested + && (level != io.brightnessSentLevel || now - io.lastBrightnessAtMs >= IoTimings.SCREEN_REFRESH_MS)) { + brightness = level + io.brightnessSentLevel = level + io.lastBrightnessAtMs = now + } + } + if (ctrl >= 0) { + logDebug("RayNeo iO: control $ctrl") + ioWriteControl(ctrl) + } + if (status) ioRequestStatus() + if (brightness > 0) ioWriteLauncher(RayNeoIoProtocol.LAUNCHER_BRIGHTNESS, RayNeoIoProtocol.launcherBrightness(brightness)) +} + +/** Stop drawing and close the page, keeping the BLE link (the EvenHub suspend analogue). */ +internal fun GlassesSessionCore.ioSuspend(): Boolean { + monitor.withLock { + if (!running || !sessionReady) return false + shutdownRequested = true + io.pageWanted = false + io.pageRequestSentAtMs = 0 + clearAllMessagesLocked("RayNeo iO suspend") + displayedFingerprint = "" + } + interruptibleSleep.interrupt() + return true +} + +/** Reopen the page; the drive loop resends the desired frame once it is open. */ +internal fun GlassesSessionCore.ioResume(): Boolean { + monitor.withLock { + if (!running || !sessionReady) return false + shutdownRequested = false + io.pageWanted = true + io.pageRequestSentAtMs = 0 + fixedLayoutCreated = false + clearAllMessagesLocked("RayNeo iO resume") + displayedFingerprint = "" + imageRetryAfterMs = 0 + } + interruptibleSleep.interrupt() + return true +} + +// --------------------------------------------------------------------------------------------- +// Inbound (BLE callback thread) + +internal fun GlassesSessionCore.ioOnNotification(address: String, characteristicUuid: String, data: ByteArray) { + if (RayNeoIoBle.NOTIFY_CHAR_UUID != characteristicUuid || !address.equals(rightAddress, ignoreCase = true)) return + val frames = monitor.withLock { io.reassembler.ingest(data) } + for (frame in frames) { + when (frame.business) { + RayNeoIoProtocol.BUSINESS_HANDSHAKE -> monitor.withLock { + val version = RayNeoIoProtocol.parseVersionReply(frame.payload) + if (version != null) io.versionReply = version + else if (RayNeoIoProtocol.isGuestAuthReply(frame.payload)) io.authReply = frame.payload + monitor.signalAll() + } + RayNeoIoProtocol.BUSINESS_LAUNCHER -> ioHandleLauncher(frame.payload) + RayNeoIoProtocol.BUSINESS_OTA -> { + val envelope = RayNeoIoProtocol.decodeEnvelope(frame.payload) ?: continue + if (envelope.type != RayNeoIoProtocol.OTA_VERSION) continue + val os = try { Json.parse(envelope.json).asObject()?.get("OsVersion").asString() } catch (_: Exception) { null } + if (os != null) monitor.withLock { + io.osVersion = os + monitor.signalAll() + } + } + RayNeoIoProtocol.BUSINESS_CFW -> ioHandleCfw(address, frame) + } + } +} + +private fun GlassesSessionCore.ioHandleLauncher(payload: ByteArray) { + val envelope = RayNeoIoProtocol.decodeEnvelope(payload) ?: return + val screen = RayNeoIoProtocol.screenStatusFromLauncherJson(envelope.json) + if (screen >= 0) { + val changed = monitor.withLock { + val was = io.screenStatus + io.screenStatus = screen + // Woken by the wearer (or the timeout ran out): re-assert promptly if Faceclaw is showing. + if (screen == 0) io.lastBrightnessAtMs = 0 + was != screen + } + if (changed) logLine("RayNeo iO screen " + (if (screen == 1) "on" else "off")) + return + } + val battery = RayNeoIoProtocol.batteryFromLauncherJson(envelope.json) + if (battery < 0) return + val charging = if (envelope.json.contains("\"isCharging\":true")) 1 else if (envelope.json.contains("\"isCharging\":false")) 0 else -1 + var changed = false + monitor.withLock { + lastIncomingAtMs = now() + if (battery != headsetBattery || (charging >= 0 && charging != headsetCharging)) { + headsetBattery = battery + if (charging >= 0) headsetCharging = charging + changed = true + } + } + if (changed) emitBatteryState(headsetBattery, headsetCharging) +} + +private fun GlassesSessionCore.ioHandleCfw(address: String, frame: RayNeoIoProtocol.Frame) { + val payload = frame.payload + if (payload.isEmpty()) return + when (payload[0].toInt() and 255) { + RayNeoIoProtocol.KIND_ACK, RayNeoIoProtocol.KIND_NACK -> { + val ack = RayNeoIoProtocol.parseCfwAck(payload) ?: return + monitor.withLock { + acceptCfwAcksLocked(address, arrayOf(ack), frame.messageNumber and 255, payload) + } + interruptibleSleep.interrupt() + } + RayNeoIoProtocol.KIND_IDENT -> monitor.withLock { + io.ident = RayNeoIoProtocol.parseIdent(payload) + monitor.signalAll() + } + RayNeoIoProtocol.KIND_PAGE -> ioHandlePage(payload) + RayNeoIoProtocol.KIND_GESTURE -> ioHandleGesture(RayNeoIoProtocol.parseGesture(payload) ?: return) + else -> logDebug("RayNeo iO: CFW reply kind " + (payload[0].toInt() and 255) + " " + hex(payload.copyOf(minOf(payload.size, 24)))) + } +} + +private fun GlassesSessionCore.ioHandlePage(payload: ByteArray) { + val state = RayNeoIoProtocol.parsePageState(payload) + val reason = if (payload.size >= 3) payload[2].toInt() and 255 else 0 + var lost = false + monitor.withLock { + io.pageState = state + io.pageReason = reason + io.pageReplies++ + lastIncomingAtMs = now() + if (state == 1) { + if (io.pageWanted && !fixedLayoutCreated && sessionReady) { + // (Re)opened: the drive loop resends the desired frame from scratch. + fixedLayoutCreated = true + displayedFingerprint = "" + imageRetryAfterMs = 0 + io.lastKeepaliveAtMs = now() + io.lastBrightnessAtMs = 0 + } + } else if (state == 0 && fixedLayoutCreated) { + // The page closed under us (lease, the wearer's long press): the firmware freed + // this session's resources, so start over once it reopens. + lost = true + fixedLayoutCreated = false + clearAllMessagesLocked("RayNeo iO page closed (reason $reason)") + displayedFingerprint = "" + if (reason == IoTimings.PAGE_REASON_USER) io.pageWanted = false + io.pageRequestSentAtMs = now() + } + monitor.signalAll() + } + if (lost) logLine("RayNeo iO page closed by the glasses (reason $reason)") + interruptibleSleep.interrupt() +} + +private fun GlassesSessionCore.ioHandleGesture(gesture: RayNeoIoProtocol.Gesture) { + val event: G2Event? = when (gesture.eventType) { + // Same numbering as the G2's OsEventTypeList; head up wakes like the G2 CFW's forward. + BleProtocol.EVENT_HEAD_UP -> G2Event("display-wake", "", BleProtocol.EVENT_HEAD_UP, BleProtocol.EVENT_SOURCE_GLASSES_R, 0) + BleProtocol.EVENT_HEAD_DOWN -> null + else -> G2Event("sys-event", "", gesture.eventType, BleProtocol.EVENT_SOURCE_GLASSES_R, 0) + } + monitor.withLock { + lastIncomingAtMs = now() + if (event != null) lastConnectionOrInputAtMs = lastIncomingAtMs + if (event != null && "sys-event" == event.kind && !io.pageWanted && sessionReady) { + // The wearer touched the glasses after closing our page: take it back. + io.pageWanted = true + shutdownRequested = false + io.pageRequestSentAtMs = 0 + } + } + logLine("RayNeo iO gesture type=" + gesture.eventType + " source=" + gesture.source + (if (event == null) " (ignored)" else "")) + if (event == null) return + val frameId = frameTimings.startFrame("input:" + event.kind + " type=" + event.eventType + " src=" + event.eventSource) + frameTimings.log(frameId, "input event decoded from RayNeo iO notification") + emitRingEvent(event, frameId) +} diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt index c1aa34a2..a935f0ce 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt @@ -44,6 +44,7 @@ internal fun GlassesSessionCore.driveSession(): Long { benchmarkReadyAtMs = now() + 1_000 } } + if (isIo) ioMaintain() var messageToWrite: OutboundMessage? = null var messageToPrewrite: OutboundMessage? = null var heartbeatToWrite: OutboundMessage? = null @@ -64,7 +65,7 @@ internal fun GlassesSessionCore.driveSession(): Long { clearPendingMessagesLocked("after CFW cleanup") return 250 } - if (sessionReady + if (sessionReady && !isIo && now - lastFaceclawFramebufferLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS && !hasPendingOrInflightKindLocked("framebuffer-lease-control")) { enqueueFaceclawFramebufferControlLocked( @@ -73,7 +74,7 @@ internal fun GlassesSessionCore.driveSession(): Long { ) } if (faceclawWakeLeaseEnabled - && sessionReady + && sessionReady && !isIo && now - lastFaceclawWakeLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS && !hasPendingOrInflightKindLocked("wake-lease-control")) { enqueueFaceclawWakeControlLocked( @@ -148,6 +149,7 @@ internal fun GlassesSessionCore.driveSession(): Long { } } else { if (messageToPrewrite == null + && !isIo // the iO's page lease is ioMaintain's && !shutdownRequested && !fixedLayoutCreated && pendingMessages.isEmpty() @@ -158,14 +160,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 && !shutdownRequested && fixedLayoutCreated + if (messageToPrewrite == null && !isIo && !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 && !shutdownRequested && fixedLayoutCreated + if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated && (if (compassOwners.isEmpty()) 0 else 1) != compassControlLastSent && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) { val wanted = compassOwners.isNotEmpty() @@ -176,7 +178,7 @@ internal fun GlassesSessionCore.driveSession(): Long { if (benchmarkActive) { maintainBenchmarkLocked(now) } - if (!benchmarkActive) maintainBrightnessLocked(now) + if (!benchmarkActive && !isIo) maintainBrightnessLocked(now) // Up to WINDOW_SIZE messages may be in flight at once (full // pipelining); a slot frees when an ack arrives. An active @@ -319,6 +321,7 @@ internal enum class HeartbeatStep { } internal fun GlassesSessionCore.heartbeatStepLocked(imageWaiting: Boolean): HeartbeatStep { + if (isIo) return HeartbeatStep.NONE val now = now() val heartbeatEligible = !shutdownRequested && fixedLayoutCreated val heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat") @@ -369,6 +372,12 @@ internal fun GlassesSessionCore.createHeartbeatMessage(): OutboundMessage { } internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean { + 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. + ioDropUnsupported(message) + return true + } val now = now() message.writeStartedAtMs = now message.sentAtMs = now @@ -398,6 +407,8 @@ internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean frames = listOf(prewrittenFrames[prewrittenFrames.size - 1]) prewrittenMessage = null prewrittenFrames = emptyList() + } else if (isIo) { + frames = ioCfwWrites(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( @@ -413,9 +424,9 @@ internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean } val result = link.writeFrames( writeAddress, - BleProtocol.WRITE_CHAR_UUID, + if (isIo) RayNeoIoBle.WRITE_CHAR_UUID else BleProtocol.WRITE_CHAR_UUID, frames, - ConnectionOptions.WRITE_MODE, + if (isIo) GattWriteMode.WITHOUT_RESPONSE else ConnectionOptions.WRITE_MODE, ConnectionOptions.WRITE_TIMEOUT_MS ) @@ -1019,7 +1030,8 @@ internal fun GlassesSessionCore.createAudioControlMessageLocked(enable: Boolean) } internal fun GlassesSessionCore.shouldPollBatteryLocked(now: Long): Boolean { - return !shutdownRequested + return !isIo // the iO's status polls are ioMaintain's + && !shutdownRequested && sessionReady && pendingMessages.isEmpty() && inFlightMessages.isEmpty() diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/rayneo/RayNeoIoProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/rayneo/RayNeoIoProtocol.kt new file mode 100644 index 00000000..c525bc15 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/rayneo/RayNeoIoProtocol.kt @@ -0,0 +1,469 @@ +package com.faceclaw.app + +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic + +/** + * RayNeo iO wire codecs (stock link plus the Faceclaw custom-firmware link). Pure functions + * and small value types; no I/O, threads or platform APIs. + * + * The iO is one SoC driving both eyes over ONE BLE connection. Everything travels in AA55 + * frames on one write characteristic and one notify characteristic; the frame's "business" + * byte selects the subsystem: 0x10 handshake (TLV), 9 OTA and 13..22 apps (protobuf-style + * envelope + JSON), 0xF0 the custom-firmware link. The CFW link carries Faceclaw's G2 records + * unchanged (see [CfwTransport.encodeRecord]) inside `[opt][stream][chunk]` packets, and the + * firmware answers with the G2's 9-byte ACK body, one ACK per targeted lens. + * + * Protocol knowledge from the MIT-licensed Glassly SDK and CFW (tetramo-labs/glassly, + * tetramo-labs/glassly-rayneo-cfw); the Faceclaw firmware fork documents its additions in + * faceclaw-rayneo-cfw/docs/FACECLAW.md. + */ +object RayNeoIoBle { + const val SERVICE_UUID = "0000b81d-0000-1000-8000-00805f9b34fb" + /** Phone -> glasses, write without response. */ + const val WRITE_CHAR_UUID = "ea8b70d5-2bd3-49ab-9c31-9c38b2c3c4f9" + /** Glasses -> phone, notifications. */ + const val NOTIFY_CHAR_UUID = "7db3e235-3608-41f3-a03c-955fcbd2ea4b" + const val REQUESTED_MTU = 512 + /** Advertised manufacturer data company id; the payload is `[07][MAC x6]...` (not in pairing mode). */ + const val MANUFACTURER_ID = 0x5716 + + @JvmStatic fun matchesName(name: String?): Boolean = name != null && name.contains("rayneo", ignoreCase = true) + + /** The glasses' MAC ("00:6A:5E:5F:33:C2") from manufacturer data, or null. */ + @JvmStatic + fun macFromManufacturerData(data: ByteArray?): String? { + if (data == null || data.size < 7 || data[0].toInt() != 7) return null + return (1..6).joinToString(":") { i -> (data[i].toInt() and 255).toString(16).padStart(2, '0').uppercase() } + } +} + +object RayNeoIoProtocol { + const val BUSINESS_OTA = 9 + const val BUSINESS_LAUNCHER = 15 + const val BUSINESS_HANDSHAKE = 0x10 + const val BUSINESS_CFW = 0xf0 + + const val FRAME_OVERHEAD = 10 + const val MIN_FRAME_BYTES = 10 + + /** Program constant of the stock app's guest authentication (not a secret). */ + private val SALT = byteArrayOf( + 0x07, 0x12, 0xc8.toByte(), 0xad.toByte(), 0x27, 0x74, 0x41, 0x95.toByte(), + 0xde.toByte(), 0x5f, 0xa1.toByte(), 0xea.toByte(), 0x6d, 0x02, 0x5f, 0xb8.toByte(), + ) + + // ---- CRC-16/XMODEM (poly 0x1021, init 0) ----------------------------------------------- + + @JvmStatic + fun crc16Xmodem(bytes: ByteArray, from: Int = 0, to: Int = bytes.size): Int { + var crc = 0 + for (i in from until to) { + crc = crc xor ((bytes[i].toInt() and 255) shl 8) + repeat(8) { crc = if (crc and 0x8000 != 0) ((crc shl 1) xor 0x1021) and 0xffff else (crc shl 1) and 0xffff } + } + return crc + } + + // ---- AA55 frames -------------------------------------------------------------------------- + + class Frame( + @JvmField val messageNumber: Int, + @JvmField val packetType: Int, + @JvmField val business: Int, + @JvmField val payload: ByteArray, + ) + + /** `AA 55 [len u16 BE = total-6] [msg u16 BE] [packetType] [business] payload [crc16 BE of bytes 4..end-2]`. */ + @JvmStatic + fun encodeFrame(messageNumber: Int, business: Int, payload: ByteArray, packetType: Int = 0): ByteArray { + val frame = ByteArray(FRAME_OVERHEAD + payload.size) + val length = frame.size - 6 + frame[0] = 0xaa.toByte() + frame[1] = 0x55 + frame[2] = (length ushr 8).toByte() + frame[3] = length.toByte() + frame[4] = (messageNumber ushr 8).toByte() + frame[5] = messageNumber.toByte() + frame[6] = packetType.toByte() + frame[7] = business.toByte() + payload.copyInto(frame, 8) + val crc = crc16Xmodem(frame, 4, frame.size - 2) + frame[frame.size - 2] = (crc ushr 8).toByte() + frame[frame.size - 1] = crc.toByte() + return frame + } + + /** Split a frame into ATT writes of at most `mtu - 3` bytes. */ + @JvmStatic + fun writes(frame: ByteArray, mtu: Int): List { + val chunk = (mtu - 3).coerceAtLeast(20) + return (frame.indices step chunk).map { frame.copyOfRange(it, minOf(it + chunk, frame.size)) } + } + + @JvmStatic + fun decodeFrame(bytes: ByteArray): Frame? { + if (bytes.size < MIN_FRAME_BYTES || bytes[0] != 0xaa.toByte() || bytes[1] != 0x55.toByte()) return null + val total = (((bytes[2].toInt() and 255) shl 8) or (bytes[3].toInt() and 255)) + 6 + if (total != bytes.size) return null + val crc = ((bytes[total - 2].toInt() and 255) shl 8) or (bytes[total - 1].toInt() and 255) + if (crc != crc16Xmodem(bytes, 4, total - 2)) return null + val number = ((bytes[4].toInt() and 255) shl 8) or (bytes[5].toInt() and 255) + return Frame(number, bytes[6].toInt() and 255, bytes[7].toInt() and 255, bytes.copyOfRange(8, total - 2)) + } + + /** + * Frames from notification chunks: a frame may span several notifications. Bytes before a + * frame head are skipped; a head whose CRC fails is skipped past its magic. + */ + class FrameReassembler { + private var buffer = ByteArray(0) + + fun reset() { + buffer = ByteArray(0) + } + + fun ingest(chunk: ByteArray): List { + buffer += chunk + val out = ArrayList() + var offset = 0 + while (true) { + while (offset + 1 < buffer.size && !(buffer[offset] == 0xaa.toByte() && buffer[offset + 1] == 0x55.toByte())) offset++ + if (buffer.size - offset < 4) break + val total = (((buffer[offset + 2].toInt() and 255) shl 8) or (buffer[offset + 3].toInt() and 255)) + 6 + if (total < MIN_FRAME_BYTES) { offset += 2; continue } + if (buffer.size - offset < total) break + val frame = decodeFrame(buffer.copyOfRange(offset, offset + total)) + if (frame == null) { offset += 2; continue } + out.add(frame) + offset += total + } + buffer = buffer.copyOfRange(minOf(offset, buffer.size), buffer.size) + return out + } + + val pendingBytes: Int get() = buffer.size + } + + // ---- TLV (tag u8, length u16 BE) ------------------------------------------------------------ + + class Tlv(@JvmField val tag: Int, @JvmField val value: ByteArray) + + @JvmStatic + fun tlv(tag: Int, value: ByteArray): ByteArray { + val out = ByteArray(3 + value.size) + out[0] = tag.toByte() + out[1] = (value.size ushr 8).toByte() + out[2] = value.size.toByte() + value.copyInto(out, 3) + return out + } + + @JvmStatic + fun decodeTlvs(bytes: ByteArray): List? { + val out = ArrayList() + var i = 0 + while (i < bytes.size) { + if (bytes.size - i < 3) return null + val len = ((bytes[i + 1].toInt() and 255) shl 8) or (bytes[i + 2].toInt() and 255) + if (bytes.size - i - 3 < len) return null + out.add(Tlv(bytes[i].toInt() and 255, bytes.copyOfRange(i + 3, i + 3 + len))) + i += 3 + len + } + return out + } + + private fun List.value(tag: Int): ByteArray? = firstOrNull { it.tag == tag }?.value + + // ---- Handshake (business 0x10) ---------------------------------------------------------------- + + const val PLATFORM_IOS = 1 + const val PLATFORM_ANDROID = 2 + + /** Version request. `phoneId` is a stable 6-byte protocol identity the glasses bind to. */ + @JvmStatic + fun versionRequest(phoneId: ByteArray, phoneName: String, manufacturer: String, model: String, platform: Int = PLATFORM_ANDROID): ByteArray { + require(phoneId.size == 6) + val inner = tlv(0x10, byteArrayOf(1)) + tlv(0x11, phoneId) + tlv(0x12, phoneName.encodeToByteArray()) + + tlv(0x13, byteArrayOf(platform.toByte())) + tlv(0x16, manufacturer.encodeToByteArray()) + + tlv(0x17, model.encodeToByteArray()) + return tlv(0x11, inner) + } + + class VersionReply( + @JvmField val protocolVersion: Int, + /** The glasses' Bluetooth MAC, which the auth proof covers. */ + @JvmField val glassesId: ByteArray, + @JvmField val serial: String?, + /** Tags 0x19/0x1a: bound to another phone's account; guest auth will not work. */ + @JvmField val accountBound: Boolean, + ) + + @JvmStatic + fun parseVersionReply(payload: ByteArray): VersionReply? { + val body = decodeTlvs(payload)?.value(0x11) ?: return null + val fields = decodeTlvs(body) ?: return null + val id = fields.value(0x11)?.takeIf { it.size == 6 } ?: return null + return VersionReply( + fields.value(0x10)?.firstOrNull()?.toInt()?.and(255) ?: 0, + id, + fields.value(0x14)?.decodeToString()?.trimEnd('\u0000'), + fields.value(0x19) != null || fields.value(0x1a) != null, + ) + } + + /** `SHA-256(random4 ‖ phoneId6 ‖ glassesId6 ‖ SALT)`, both directions. */ + @JvmStatic + fun authProof(random: ByteArray, phoneId: ByteArray, glassesId: ByteArray): ByteArray { + val digest = Sha256Digest() + digest.update(random) + digest.update(phoneId) + digest.update(glassesId) + digest.update(SALT) + return digest.digest() + } + + @JvmStatic + fun guestAuthRequest(random: ByteArray, phoneId: ByteArray, glassesId: ByteArray): ByteArray { + require(random.size == 4 && phoneId.size == 6 && glassesId.size == 6) + return tlv(0x18, tlv(0x10, random) + tlv(0x11, authProof(random, phoneId, glassesId))) + } + + /** The glasses' answer proves they know the same phone and glasses ids. */ + @JvmStatic + fun verifyGuestAuthReply(payload: ByteArray, phoneId: ByteArray, glassesId: ByteArray): Boolean { + val body = decodeTlvs(payload)?.value(0x19) ?: return false + val fields = decodeTlvs(body) ?: return false + val random = fields.value(0x10)?.takeIf { it.size == 4 } ?: return false + val proof = fields.value(0x11)?.takeIf { it.size == 32 } ?: return false + return proof.contentEquals(authProof(random, phoneId, glassesId)) + } + + /** True for a guest-auth reply (outer tag 0x19), valid or not. */ + @JvmStatic fun isGuestAuthReply(payload: ByteArray): Boolean = decodeTlvs(payload)?.value(0x19) != null + + // ---- Business envelope (9, 13..22): 08 version, 10 type, 1A json, 22 bytes, 28 seq, 30 mode ---- + + const val MODE_REQUEST = 1 + const val MODE_REPLY = 2 + const val LAUNCHER_GENERAL_STATUS = 1 + const val LAUNCHER_BRIGHTNESS = 2 + const val OTA_VERSION = 1 + const val GENERAL_STATUS_REQUEST = """{"cmd":"request_general_status","payload":{"data":"","mode":0,"value":0}}""" + + class Envelope( + @JvmField val type: Int, + @JvmField val json: String, + @JvmField val binary: ByteArray? = null, + @JvmField val sequence: Int? = null, + @JvmField val mode: Int? = null, + ) + + private fun varint(out: MutableList, value: Long) { + var v = value + while (v and 0x7fL.inv() != 0L) { out.add(((v and 0x7f) or 0x80).toByte()); v = v ushr 7 } + out.add(v.toByte()) + } + + @JvmStatic + fun encodeEnvelope(e: Envelope): ByteArray { + val out = ArrayList() + out.add(0x08); varint(out, 1) + out.add(0x10); varint(out, e.type.toLong()) + val json = e.json.encodeToByteArray() + out.add(0x1a); varint(out, json.size.toLong()); json.forEach { out.add(it) } + e.binary?.let { b -> out.add(0x22); varint(out, b.size.toLong()); b.forEach { out.add(it) } } + e.sequence?.let { out.add(0x28); varint(out, it.toLong()) } + e.mode?.let { out.add(0x30); varint(out, it.toLong()) } + return out.toByteArray() + } + + @JvmStatic + fun decodeEnvelope(bytes: ByteArray): Envelope? { + var pos = 0 + fun readVarint(): Long? { + var shift = 0 + var result = 0L + while (pos < bytes.size && shift <= 63) { + val b = bytes[pos++].toInt() and 255 + result = result or ((b and 0x7f).toLong() shl shift) + if (b and 0x80 == 0) return result + shift += 7 + } + return null + } + var type: Int? = null + var json = "" + var binary: ByteArray? = null + var sequence: Int? = null + var mode: Int? = null + while (pos < bytes.size) { + val key = readVarint() ?: return null + when ((key and 7).toInt()) { + 0 -> { + val v = readVarint() ?: return null + when ((key ushr 3).toInt()) { 2 -> type = v.toInt(); 5 -> sequence = v.toInt(); 6 -> mode = v.toInt() } + } + 2 -> { + val len = readVarint() ?: return null + if (len < 0 || len > bytes.size - pos) return null + val v = bytes.copyOfRange(pos, pos + len.toInt()) + pos += len.toInt() + when ((key ushr 3).toInt()) { 3 -> json = v.decodeToString(); 4 -> binary = v } + } + 1 -> pos += 8 + 5 -> pos += 4 + else -> return null + } + } + return Envelope(type ?: return null, json, binary, sequence, mode) + } + + /** + * Set the launcher's brightness (1..8). The stock handler also turns the screen on + * (BrightnessMonitor::setBrightness -> turnOnScreenForSetting), which is how the phone + * wakes the iO's display and keeps the stock auto-off from blanking a static page. + */ + @JvmStatic + fun launcherBrightness(level: Int): String = + """{"cmd":"brightness_change","payload":{"value":${level.coerceIn(1, 8)},"mode":0,"data":""}}""" + + /** Faceclaw's 0..100 brightness target on the launcher's 1..8 scale. */ + @JvmStatic fun brightnessLevel(target: Int): Int = maxOf(1, (target.coerceIn(0, 100) * 8 + 50) / 100) + + /** `screen_status` push value (1 on, 0 off), or -1 for anything else. */ + @JvmStatic + fun screenStatusFromLauncherJson(json: String): Int { + val root = try { Json.parse(json).asObject() } catch (_: Exception) { null } ?: return -1 + if (root["cmd"].asString() != "screen_status") return -1 + return root["payload"].asObject()?.get("value").asInt() ?: -1 + } + + /** `generalStatus.battery` from a launcher reply or push, or -1. */ + @JvmStatic + fun batteryFromLauncherJson(json: String): Int { + val root = try { Json.parse(json).asObject() } catch (_: Exception) { null } ?: return -1 + val status = root["generalStatus"].asObject() ?: root["payload"].asObject()?.get("generalStatus").asObject() + status?.get("battery").asInt()?.let { return it } + // pushes: {"cmd":"battery_change","payload":{"value":N,...}} + if (root["cmd"].asString() == "battery_change") return root["payload"].asObject()?.get("value").asInt() ?: -1 + return -1 + } + + // ---- Custom-firmware link (business 0xF0) ----------------------------------------------------- + + const val OPT_LENS = 0x01 + const val OPT_CONTROL = 0x10 + const val OPT_END = 0x40 + const val OPT_RESET = 0x80 + const val CTRL_QUERY = 1 + const val CTRL_PAGE_OPEN = 2 + const val CTRL_PAGE_CLOSE = 3 + const val CTRL_KEEPALIVE = 4 + /** Records up to this many bytes per packet (IDENT reports the firmware's maxChunk). */ + const val DEFAULT_MAX_CHUNK = 240 + /** The page closes this long after the last PAGE_OPEN/KEEPALIVE. */ + const val PAGE_LEASE_MS = 120_000L + + @JvmStatic + fun controlPacket(ctrl: Int, arg: Int? = null): ByteArray = + if (arg == null) byteArrayOf(OPT_CONTROL.toByte(), 0, ctrl.toByte()) + else byteArrayOf(OPT_CONTROL.toByte(), 0, ctrl.toByte(), arg.toByte()) + + /** + * One record (from [CfwTransport.encodeRecord]) as business-0xF0 payloads + * `[opt][stream][chunk]`. `stream` is echoed in the ACKs, so it plays the G2 stream id's role. + */ + @JvmStatic + fun cfwPackets(record: ByteArray, streamId: Int, maxChunk: Int = DEFAULT_MAX_CHUNK): List { + require(streamId in 0..255 && maxChunk > 0) + val out = ArrayList() + var offset = 0 + do { + val end = minOf(offset + maxChunk, record.size) + var opt = OPT_LENS + if (offset == 0) opt = opt or OPT_RESET + if (end >= record.size) opt = opt or OPT_END + val p = ByteArray(2 + end - offset) + p[0] = opt.toByte() + p[1] = streamId.toByte() + record.copyInto(p, 2, offset, end) + out.add(p) + offset = end + } while (offset < record.size) + return out + } + + const val KIND_ACK = 1 + const val KIND_NACK = 3 + const val KIND_REPLY = 5 + const val KIND_GESTURE = 7 + const val KIND_IDENT = 0x11 + const val KIND_PAGE = 0x13 + const val KIND_DIAG = 0x15 + + /** `[01|03][stream][ordinal u16][lens][size u16][crc u16]`: the G2 ACK body, one per lens. */ + @JvmStatic + fun parseCfwAck(payload: ByteArray): CfwTransport.Ack? { + if (payload.size != 9) return null + val kind = payload[0].toInt() and 255 + val lens = payload[4].toInt() and 255 + if ((kind != KIND_ACK && kind != KIND_NACK) || (lens != 1 && lens != 2)) return null + fun u16(o: Int) = (payload[o].toInt() and 255) or ((payload[o + 1].toInt() and 255) shl 8) + return CfwTransport.Ack(kind == KIND_NACK, payload[1].toInt() and 255, u16(2), lens, u16(5), u16(7)) + } + + const val IDENT_FLAG_ZLIB = 0x01 + const val IDENT_FLAG_STEREO = 0x08 + const val IDENT_FLAG_FACECLAW = 0x80 + + class Ident( + @JvmField val width: Int, + @JvmField val height: Int, + @JvmField val maxChunk: Int, + @JvmField val revision: Int, + @JvmField val flags: Int, + @JvmField val name: String, + /** "Faceclaw/" -> n when the firmware runs the Faceclaw display lists, else -1. */ + @JvmField val faceclawRevision: Int, + ) { + val acceptsCompressed: Boolean get() = flags and IDENT_FLAG_ZLIB != 0 + val faceclaw: Boolean get() = flags and IDENT_FLAG_FACECLAW != 0 && faceclawRevision > 0 + } + + /** `[11][ver][w u16][h u16][maxChunk u16][storeKiB u16][rev u16][flags]name\0[lineHeight]["Faceclaw/n"\0]`. */ + @JvmStatic + fun parseIdent(payload: ByteArray): Ident? { + if (payload.size < 13 || payload[0].toInt() != KIND_IDENT) return null + fun u16(o: Int) = (payload[o].toInt() and 255) or ((payload[o + 1].toInt() and 255) shl 8) + var end = 13 + while (end < payload.size && payload[end].toInt() != 0) end++ + val name = payload.copyOfRange(13, end).decodeToString() + var faceclaw = -1 + val extra = end + 2 // NUL, line height + if (extra < payload.size) { + var e = extra + while (e < payload.size && payload[e].toInt() != 0) e++ + val text = payload.copyOfRange(extra, e).decodeToString() + if (text.startsWith("Faceclaw/")) faceclaw = text.removePrefix("Faceclaw/").toIntOrNull() ?: -1 + } + return Ident(u16(2), u16(4), u16(6), u16(10), payload[12].toInt() and 255, name, faceclaw) + } + + /** PAGE `[13][state][reason]`: state 1 open. */ + @JvmStatic fun parsePageState(payload: ByteArray): Int = if (payload.size >= 2 && payload[0].toInt() == KIND_PAGE) payload[1].toInt() and 255 else -1 + + class Gesture(@JvmField val eventType: Int, @JvmField val source: Int, @JvmField val tick: Long) + + /** + * GESTURE `[07][eventType][source][tick u32]`. Event types use the G2's OsEventTypeList numbering: + * 0 single tap, 1 swipe up, 2 swipe down, 3 double tap, 11 tap-then-hold, 12 head up, 15 head down. + */ + @JvmStatic + fun parseGesture(payload: ByteArray): Gesture? { + if (payload.size < 7 || payload[0].toInt() != KIND_GESTURE) return null + val tick = (payload[3].toLong() and 255) or ((payload[4].toLong() and 255) shl 8) or + ((payload[5].toLong() and 255) shl 16) or ((payload[6].toLong() and 255) shl 24) + return Gesture(payload[1].toInt() and 255, payload[2].toInt() and 255, tick) + } +} diff --git a/tests/display-layout-setting.test.cjs b/tests/display-layout-setting.test.cjs index 55e44f71..56bbb17c 100644 --- a/tests/display-layout-setting.test.cjs +++ b/tests/display-layout-setting.test.cjs @@ -64,3 +64,17 @@ test('Full Screen is offered only with a bottom or popup app switcher', () => { assert.deepEqual(Array.from(api.DISPLAY_LAYOUT_VALUES, (value) => api.displayLayoutLabel(value)), ['Band · Top', 'Band · High', 'Band · Middle', 'Band · Low', 'Band · Bottom', 'Tall', 'Full Screen']); }); + +test('a RayNeo iO pins the band to the top and the switcher to the right, leaving the stored choice alone', () => { + const store = new Map([['display.layout', 'band-middle'], ['display.appSwitcherPosition', 'bottom']]); + const api = settingsFixture(store); + assert.equal(api.displayLayoutSetting.get(), 'band-middle'); + assert.equal(api.appSwitcherPositionSetting.get(), 'bottom'); + api.glassesTypeSetting.set('rayneo-io'); + assert.equal(api.displayLayoutSetting.get(), 'band-top'); + assert.equal(api.appSwitcherPositionSetting.get(), 'right'); + assert.equal(store.get('display.layout'), 'band-middle'); + api.glassesTypeSetting.set('g2'); + assert.equal(api.displayLayoutSetting.get(), 'band-middle'); + assert.equal(api.appSwitcherPositionSetting.get(), 'bottom'); +}); diff --git a/tests/even-advertisement.test.cjs b/tests/even-advertisement.test.cjs index 37372daf..c2757d65 100644 --- a/tests/even-advertisement.test.cjs +++ b/tests/even-advertisement.test.cjs @@ -162,3 +162,21 @@ test("a bare R1 substring is not a ring — only the stock EVEN R1 prefix is", ( const ad = classifyAdvertisement(raw({ address: "DA:7F:D2:B5:6E:E2", name: "EVEN R1_B56EE2", manufacturerData: "" })); assert.equal(ad.role, "ring"); }); + +// RayNeo iO-2229 as captured 2026-11-01: company 0x5716 + [07][MAC]; pairing mode omits the data. +test("RayNeo iO advertisements classify as one device with its MAC", () => { + const io = classifyAdvertisement(raw({ + address: "00:6A:5E:5F:33:C2", + name: "RayNeo iO-2229", + manufacturerData: "1657" + "07006a5e5f33c2130000000000000001", + })); + assert.equal(io.role, "rayneo-io"); + assert.equal(io.embeddedMac, "00:6A:5E:5F:33:C2"); + assert.equal(io.embeddedMacMismatch, false); + assert.equal(io.serial, null); + const pairing = classifyAdvertisement(raw({ address: "00:6A:5E:5F:33:C2", name: "RayNeo iO-2229", manufacturerData: "" })); + assert.equal(pairing.role, "rayneo-io"); + assert.equal(pairing.embeddedMac, null); + // G2 advertisements are untouched + assert.equal(classifyAdvertisement(raw({})).role, "left"); +}); diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoProtocolTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoProtocolTest.kt new file mode 100644 index 00000000..b92b6709 --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoProtocolTest.kt @@ -0,0 +1,152 @@ +package com.faceclaw.app + +import kotlin.test.* + +/** Vectors are bytes captured from a RayNeo iO (Strix OS 1.0.5.12 / 1.0.4.12 + CFW) on 2026-11-01. */ +class RayNeoIoProtocolTest { + private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray() + + private val phoneId = hex("fac582dfa91e") + private val glassesId = hex("006a5e5f33c2") + + @Test + fun crcAndFrames() { + assertEquals(0x31c3, RayNeoIoProtocol.crc16Xmodem("123456789".encodeToByteArray())) + val payload = hex("100001") + val frame = RayNeoIoProtocol.encodeFrame(0x1234, RayNeoIoProtocol.BUSINESS_CFW, payload) + assertEquals(13, frame.size) + assertContentEquals(hex("aa55000712340" + "0f0100001"), frame.copyOfRange(0, 11)) + val decoded = RayNeoIoProtocol.decodeFrame(frame)!! + assertEquals(0x1234, decoded.messageNumber) + assertEquals(0xf0, decoded.business) + assertContentEquals(payload, decoded.payload) + frame[9] = 9 + assertNull(RayNeoIoProtocol.decodeFrame(frame)) + } + + @Test + fun reassemblerSpansNotificationsAndSkipsJunk() { + val a = RayNeoIoProtocol.encodeFrame(1, 15, ByteArray(300) { it.toByte() }) + val b = RayNeoIoProtocol.encodeFrame(2, 0xf0, hex("0195000001070024cf")) + val stream = hex("0102") + a + b + val r = RayNeoIoProtocol.FrameReassembler() + val frames = RayNeoIoProtocol.writes(stream, 23).flatMap { r.ingest(it) } + assertEquals(listOf(1, 2), frames.map { it.messageNumber }) + assertEquals(300, frames[0].payload.size) + assertEquals(0, r.pendingBytes) + // a corrupt head is skipped, the next good frame still decodes + val bad = a.copyOf().also { it[20] = (it[20] + 1).toByte() } + assertEquals(listOf(2), r.ingest(bad + b).map { it.messageNumber }) + } + + @Test + fun handshakeMatchesCapture() { + // the Mac harness's request (iOS platform byte, name "Amoli", model "Mac") + assertContentEquals( + hex("11002810000101110006fac582dfa91e120005416d6f6c69130001011600066950686f6e651700034d6163"), + RayNeoIoProtocol.versionRequest(phoneId, "Amoli", "iPhone", "Mac", RayNeoIoProtocol.PLATFORM_IOS), + ) + val reply = RayNeoIoProtocol.parseVersionReply( + hex("11004a10000102110006006a5e5f33c212000e5261794e656f20694f2d323232391300010514000f3030364135453546333343323232391500010518000f303036413545354633334332323239"), + )!! + assertContentEquals(glassesId, reply.glassesId) + assertEquals(2, reply.protocolVersion) + assertEquals("006A5E5F33C2229", reply.serial) + assertFalse(reply.accountBound) + assertContentEquals( + hex("18002a100004875e2013110020efed651a6aacd1834e8023891eaf80ec4a25814db5222e42655b71fdd373ab96"), + RayNeoIoProtocol.guestAuthRequest(hex("875e2013"), phoneId, glassesId), + ) + val authReply = hex("19002a1000042316e62111002082289251b37c1ddb76694d5a4bb4e911a79ad8f2867d7379bb8125f36ae6acac") + assertTrue(RayNeoIoProtocol.isGuestAuthReply(authReply)) + assertTrue(RayNeoIoProtocol.verifyGuestAuthReply(authReply, phoneId, glassesId)) + assertFalse(RayNeoIoProtocol.verifyGuestAuthReply(authReply, hex("fac582dfa91f"), glassesId)) + } + + @Test + fun envelopes() { + assertContentEquals( + hex("080110011a027b7d"), + RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(RayNeoIoProtocol.OTA_VERSION, "{}")), + ) + val status = RayNeoIoProtocol.decodeEnvelope(hex(GENERAL_STATUS))!! + assertEquals(1, status.type) + assertEquals(98, RayNeoIoProtocol.batteryFromLauncherJson(status.json)) + assertEquals(100, RayNeoIoProtocol.batteryFromLauncherJson("""{"cmd":"battery_change","payload":{"value":100,"mode":1}}""")) + assertEquals(-1, RayNeoIoProtocol.batteryFromLauncherJson("""{"cmd":"screen_status","payload":{"value":1}}""")) + val request = RayNeoIoProtocol.Envelope(1, RayNeoIoProtocol.GENERAL_STATUS_REQUEST, sequence = 300, mode = RayNeoIoProtocol.MODE_REQUEST) + val back = RayNeoIoProtocol.decodeEnvelope(RayNeoIoProtocol.encodeEnvelope(request))!! + assertEquals(300, back.sequence) + assertEquals(RayNeoIoProtocol.MODE_REQUEST, back.mode) + assertEquals(request.json, back.json) + } + + @Test + fun cfwLink() { + assertContentEquals(hex("100001"), RayNeoIoProtocol.controlPacket(RayNeoIoProtocol.CTRL_QUERY)) + val record = ByteArray(500) { it.toByte() } + val packets = RayNeoIoProtocol.cfwPackets(record, 0x95, 240) + assertEquals(listOf(0x81, 0x01, 0x41), packets.map { it[0].toInt() and 255 }) + assertTrue(packets.all { it[1] == 0x95.toByte() }) + assertContentEquals(record, packets.flatMap { it.drop(2) }.toByteArray()) + assertEquals(0xc1, RayNeoIoProtocol.cfwPackets(ByteArray(5), 1)[0][0].toInt() and 255) + + val ack = RayNeoIoProtocol.parseCfwAck(hex("0195000001070024cf"))!! + assertFalse(ack.nack) + assertEquals(0x95, ack.streamId) + assertEquals(0, ack.messageId) + assertEquals(1, ack.lens) + assertEquals(7, ack.size) + assertEquals(0xcf24, ack.checksum) + assertTrue(RayNeoIoProtocol.parseCfwAck(hex("0395000002070024cf"))!!.nack) + assertNull(RayNeoIoProtocol.parseCfwAck(hex("0195000003070024cf"))) + + // Glassly v13 IDENT, then the Faceclaw fork's with the revision string appended + val v13 = RayNeoIoProtocol.parseIdent(hex("110d1c02dc00f00000010d007f474c4153534c594346572d494f2f31330019"))!! + assertEquals(540, v13.width) + assertEquals(220, v13.height) + assertEquals(240, v13.maxChunk) + assertEquals(13, v13.revision) + assertEquals("GLASSLYCFW-IO/13", v13.name) + assertFalse(v13.faceclaw) + val fc = RayNeoIoProtocol.parseIdent(hex("110d1c02dc00f00000010d00ff474c4153534c594346572d494f2f3133001946616365636c61772f333800"))!! + assertTrue(fc.faceclaw) + assertEquals(38, fc.faceclawRevision) + + assertEquals(1, RayNeoIoProtocol.parsePageState(hex("130100"))) + val g = RayNeoIoProtocol.parseGesture(hex("0703010a000000"))!! + assertEquals(3, g.eventType) + assertEquals(1, g.source) + assertEquals(10L, g.tick) + } + + @Test + fun recordsShareTheG2Encoder() { + // the G2 envelope is unchanged by the record/packet split + val g2 = CfwTransport.frame("123456789abcdef".encodeToByteArray(), 255, 3, 23) + assertContentEquals(hex("aa21ff0c0101f000830b0f0078c331323334862b"), g2[0]) + // an iO record carries the same flags/length/CRC header + val t = CfwTransport(testPlatform()) + val message = ByteArray(64) { (it % 7).toByte() } + val rec = t.encodeRecord(message, CfwTransport.BOTH) + assertEquals(CfwTransport.BOTH or CfwTransport.COMPRESSED or CfwTransport.RESET_CONTEXT, rec[0].toInt()) + assertEquals(CfwTransport.crc(message), (rec[3].toInt() and 255) or ((rec[4].toInt() and 255) shl 8)) + assertContentEquals(message, inflateRecords(listOf(rec))[0]) + // the next record continues the same compression context + val second = t.encodeRecord(message, CfwTransport.BOTH) + assertEquals(CfwTransport.BOTH or CfwTransport.COMPRESSED, second[0].toInt()) + t.close() + } + + @Test + fun advertisement() { + assertEquals("00:6A:5E:5F:33:C2", RayNeoIoBle.macFromManufacturerData(hex("07006a5e5f33c2130000000000000001"))) + assertNull(RayNeoIoBle.macFromManufacturerData(hex("0800"))) + assertTrue(RayNeoIoBle.matchesName("RayNeo iO-2229")) + assertFalse(RayNeoIoBle.matchesName("Even G2_32_L_693CCB")) + } + + private companion object { + const val GENERAL_STATUS = "080110011aa6037b2267656e6572616c537461747573223a7b226175746f6d617469634272696768746e657373223a66616c73652c2262617474657279223a39382c22626174745f74656d70223a33342c22626c7565746f6f746841646472657373223a2230303a36413a35453a35463a33333a4332222c226272696768746e657373223a312c226465766963654e616d65223a225261794e656f20694f2d32323239222c2269734368617267696e67223a747275652c2263686172676554797065223a302c2273637265656e537461747573223a312c2268616c6c537461747573223a302c226d6963537461747573223a302c22666f6375734d6f6465223a7b22656e61626c65223a302c22656e61626c65476c617373436c6f7365223a312c22706f6c696379223a7b226175746f223a312c22626567696e223a2230393a3030222c22656e64223a2231383a3030222c227765656b646179223a5b302c312c322c332c342c352c365d7d7d2c22707265736372697074696f6e223a7b22656e61626c65223a312c22646567726565223a222d332c302c2d322e37352c30222c227064223a223633227d7d7d" + } +} diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoSessionTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoSessionTest.kt new file mode 100644 index 00000000..80d8ae38 --- /dev/null +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/RayNeoIoSessionTest.kt @@ -0,0 +1,380 @@ +package com.faceclaw.app + +import kotlin.concurrent.Volatile +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +private const val IO = "00:6A:5E:5F:33:C2" +private val PHONE_ID = byteArrayOf(1, 2, 3, 4, 5, 6) + +private class IoClock(private val base: ProtocolPlatform) : ProtocolPlatform by base { + @Volatile var offsetMs = 0L + + override fun elapsedRealtimeMs(): Long = base.elapsedRealtimeMs() + offsetMs +} + +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() +} + +/** + * A RayNeo iO running faceclaw-rayneo-cfw, as the phone sees it over GATT: answers the + * handshake, guest auth, launcher status, OS version, QUERY and page controls, reassembles + * and inflates CFW records, and ACKs each lens of every record whose CRC checks out. + */ +private class FakeIoGlasses(platform: ProtocolPlatform) : SessionLink { + lateinit var core: GlassesSessionCore + private val lock = platform.createLock() + val glassesId = byteArrayOf(0x00, 0x6a, 0x5e, 0x5f, 0x33, 0xc2.toByte()) + /** The phone id the glasses accept; null = pairing mode (they bind to the next one). */ + @Volatile var boundTo: ByteArray? = null + @Volatile var faceclaw = true + @Volatile var pageOpen = false + @Volatile var hangUps = 0 + private val controlsList = ArrayList() + private val messagesList = ArrayList() + private val launcherList = ArrayList() + val launcher: List get() = lock.withLock { launcherList.toList() } + private val records = ArrayList() + private var current = ByteArray(0) + private val inbound = RayNeoIoProtocol.FrameReassembler() + private var number = 1 + + val controls: List get() = lock.withLock { controlsList.toList() } + val messages: List get() = lock.withLock { messagesList.toList() } + + fun notify(business: Int, payload: ByteArray) { + val frame = lock.withLock { RayNeoIoProtocol.encodeFrame(number++, business, payload) } + core.onNotification(IO, RayNeoIoBle.NOTIFY_CHAR_UUID, frame) + } + + fun page(state: Int, reason: Int) = notify(0xf0, byteArrayOf(0x13, state.toByte(), reason.toByte())) + + 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 == RayNeoIoBle.NOTIFY_CHAR_UUID + + override fun writeFrames(address: String, characteristicUuid: String, frames: List, mode: GattWriteMode, timeoutMs: Int): Boolean { + assertEquals(RayNeoIoBle.WRITE_CHAR_UUID, characteristicUuid) + assertTrue(frames.all { it.size <= 185 - 3 }, "writes must fit the MTU") + val decoded = lock.withLock { frames.flatMap { inbound.ingest(it) } } + for (frame in decoded) handle(frame) + return true + } + + private fun handle(frame: RayNeoIoProtocol.Frame) { + val p = frame.payload + when (frame.business) { + 0x10 -> { + val tlvs = RayNeoIoProtocol.decodeTlvs(p)!! + val version = tlvs.firstOrNull { it.tag == 0x11 } + val auth = tlvs.firstOrNull { it.tag == 0x18 } + if (version != null) { + val phone = RayNeoIoProtocol.decodeTlvs(version.value)!!.first { it.tag == 0x11 }.value + val bound = boundTo + if (bound != null && !bound.contentEquals(phone)) { + hangUps++ + core.onConnectionStateChange(IO, false) + return + } + notify(0x10, RayNeoIoProtocol.tlv(0x11, RayNeoIoProtocol.tlv(0x10, byteArrayOf(2)) + + RayNeoIoProtocol.tlv(0x11, glassesId) + RayNeoIoProtocol.tlv(0x14, "006A5E5F33C2229".encodeToByteArray()))) + } else if (auth != null) { + val fields = RayNeoIoProtocol.decodeTlvs(auth.value)!! + val phoneRandom = fields.first { it.tag == 0x10 }.value + val phone = PHONE_ID + assertTrue(RayNeoIoProtocol.authProof(phoneRandom, phone, glassesId).contentEquals(fields.first { it.tag == 0x11 }.value)) + boundTo = phone + val random = byteArrayOf(9, 8, 7, 6) + notify(0x10, RayNeoIoProtocol.tlv(0x19, RayNeoIoProtocol.tlv(0x10, random) + + RayNeoIoProtocol.tlv(0x11, RayNeoIoProtocol.authProof(random, phone, glassesId)))) + } + } + 15 -> { + lock.withLock { launcherList.add(RayNeoIoProtocol.decodeEnvelope(p)!!.json) } + notify(15, RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(1, + """{"generalStatus":{"battery":77,"isCharging":false}}""", sequence = RayNeoIoProtocol.decodeEnvelope(p)!!.sequence, mode = 2))) + } + 9 -> notify(9, RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(1, """{"OsVersion":"Strix OS 1.0.4.12"}"""))) + 0xf0 -> cfw(p) + } + } + + private fun cfw(p: ByteArray) { + val opt = p[0].toInt() and 255 + val stream = p[1].toInt() and 255 + if (opt and RayNeoIoProtocol.OPT_CONTROL != 0) { + val ctrl = p[2].toInt() and 255 + lock.withLock { controlsList.add(ctrl) } + when (ctrl) { + RayNeoIoProtocol.CTRL_QUERY -> if (faceclaw) notify(0xf0, ident()) + RayNeoIoProtocol.CTRL_PAGE_OPEN, RayNeoIoProtocol.CTRL_KEEPALIVE -> { pageOpen = true; page(1, 0) } + RayNeoIoProtocol.CTRL_PAGE_CLOSE -> { pageOpen = false; page(0, 0) } + } + return + } + val complete = lock.withLock { + if (opt and RayNeoIoProtocol.OPT_RESET != 0) current = ByteArray(0) + current += p.copyOfRange(2, p.size) + val len = if (current.size >= 5) (current[1].toInt() and 255) or ((current[2].toInt() and 255) shl 8) else -1 + if (len >= 0 && current.size == 5 + len) current.also { records.add(it); current = ByteArray(0) } else null + } ?: return + val message = lock.withLock { inflateRecords(records).last() } + val crc = (complete[3].toInt() and 255) or ((complete[4].toInt() and 255) shl 8) + val ok = pageOpen && CfwTransport.crc(message) == crc && ioSupportsCfwType(message[0].toInt() and 255) + if (ok) lock.withLock { messagesList.add(message) } + for (lens in 1..2) { + notify(0xf0, byteArrayOf(if (ok) 1 else 3, stream.toByte(), 0, 0, lens.toByte(), + message.size.toByte(), (message.size ushr 8).toByte(), crc.toByte(), (crc ushr 8).toByte())) + } + } + + private fun ident(): ByteArray { + val name = "GLASSLYCFW-IO/13".encodeToByteArray() + return byteArrayOf(0x11, 13, 0x1c, 0x02, 0xdc.toByte(), 0x00, 0xf0.toByte(), 0x00, 0x00, 0x01, 0x0d, 0x00, 0xff.toByte()) + + name + byteArrayOf(0, 25) + "Faceclaw/38".encodeToByteArray() + byteArrayOf(0) + } + + override fun disconnect(address: String) {} + + override fun close() {} + + override fun isBonded(address: String): Boolean = false + + override fun prepareBenchmarkLink(address: String, mode: Int) {} + + override fun recordDisplayFrameSent() {} +} + +private class IoListener : FaceclawBleCommunicatorListener { + val phases = ArrayList() + @Volatile var lastStatus = "" + val events = ArrayList() + val batteries = ArrayList() + val firmware = ArrayList() + + override fun onStateChange(phase: String?, status: String?) { + phases.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) { + events.add("$kind:$eventType:$eventSource") + } + + override fun onBatteryState(headsetBattery: Int, headsetCharging: Int, ringBattery: Int, ringCharging: Int) { + batteries.add("$headsetBattery/$headsetCharging") + } + + override fun onSilentMode(silent: Boolean) {} + + override fun onWearState(wearing: Boolean) {} + + override fun onPhoneLockState(locked: 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?) { + firmware.add("$leftVersion|$extension") + } +} + +private class IoSession { + val platform = IoClock(testPlatform()) + val glasses = FakeIoGlasses(platform) + private val host = object : SessionHost { + private val lock = platform.createLock() + private val lines = ArrayList() + private var done: Latch? = null + + fun logs(): List = 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("io-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 = IoListener() + val core = GlassesSessionCore(glasses, host, FrameTimingsCore(platform), platform, IO, IO, "") + + init { + glasses.core = core + core.setListener(listener) + core.setRequiredFirmwareRevision(38) + core.useRayNeoIo(PHONE_ID, "Pixel", "Google", "Pixel 11") + } + + fun pageUp(): 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 submitFrame(value: Byte, frameId: Int) { + val pixels = ByteArray(64 * 32) { if (it % 7 == 0) value else 0 } + core.submitSurfaceFrame(ArrayByteReader(pixels), "s", 0, 0, 64, 32, "content-$value", 0, frameId, null) + } + + fun tail(): String = host.logs().takeLast(25).joinToString("\n") +} + +class RayNeoIoSessionTest { + @Test + fun connectsAuthenticatesOpensThePageAndPresentsFrames() { + val s = IoSession() + s.core.configureBrightness(false, 70, 2, 100, "0:0,1000:100", 280) + s.core.configureCompositorScreen(540, 220) + s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE) + try { + assertTrue(s.core.start()) + assertTrue(awaitTrue(5_000) { s.pageUp() }, "page never opened:\n" + s.tail()) + assertTrue(s.core.isRayNeoIo()) + assertTrue(s.listener.firmware.contains("Strix OS 1.0.4.12|Faceclaw/38"), s.listener.firmware.toString()) + assertTrue(awaitTrue(2_000) { s.listener.batteries.contains("77/0") }, s.listener.batteries.toString()) + assertEquals(listOf(RayNeoIoProtocol.CTRL_QUERY, RayNeoIoProtocol.CTRL_PAGE_OPEN), s.glasses.controls.take(2)) + + s.submitFrame(0x7f, 1) + assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, "frame never displayed:\n" + s.tail()) + val types = s.glasses.messages.map { it[0].toInt() and 255 } + assertTrue(types.contains(CFW_MSG_PRESENT), types.toString()) + // nothing G2-only (brightness 30, ENTER_EVENHUB 31, debug flags 7, ...) reaches the iO + assertTrue(types.all { ioSupportsCfwType(it) }, types.toString()) + // a visible frame wakes the screen through the launcher's brightness (70% -> level 6) + assertTrue(awaitTrue(3_000) { s.glasses.launcher.any { it.contains("brightness_change") && it.contains("\"value\":6") } }, + s.glasses.launcher.toString()) + + // a second, different frame goes out as an update + val before = s.glasses.messages.size + s.submitFrame(0x30, 2) + assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() && s.glasses.messages.size > before }) + } finally { + s.core.close() + } + // disconnect closes the page so the glasses go back to their own UI at once + assertEquals(RayNeoIoProtocol.CTRL_PAGE_CLOSE, s.glasses.controls.last()) + } + + @Test + fun refusalAsksForPairingModeAndConnectsOnceItIsOn() { + val s = IoSession() + s.glasses.boundTo = byteArrayOf(9, 9, 9, 9, 9, 9) + try { + assertTrue(s.core.start()) + assertTrue(awaitTrue(5_000) { s.glasses.hangUps >= 1 && s.listener.lastStatus.contains("pairing mode") }, + "no pairing-mode prompt:\n" + s.tail()) + assertFalse(s.pageUp()) + s.glasses.boundTo = null // the wearer held the button + s.platform.offsetMs += 5_000 + s.core.interruptibleSleep.interrupt() + assertTrue(awaitTrue(5_000) { s.pageUp() }, "never connected after pairing mode:\n" + s.tail()) + assertTrue(s.glasses.boundTo!!.contentEquals(PHONE_ID)) + } finally { + s.core.close() + } + } + + @Test + fun stockFirmwareIsIncompatible() { + val s = IoSession() + s.glasses.faceclaw = false + try { + assertTrue(s.core.start()) + assertTrue(awaitTrue(8_000) { s.listener.phases.contains("incompatible-firmware") }, "not halted:\n" + s.tail()) + assertTrue(s.listener.firmware.contains("Strix OS 1.0.4.12|")) + assertFalse(s.glasses.controls.contains(RayNeoIoProtocol.CTRL_PAGE_OPEN)) + } finally { + s.core.close() + } + } + + @Test + fun gesturesPageLossAndKeepalive() { + val s = IoSession() + s.core.configureCompositorScreen(540, 220) + try { + assertTrue(s.core.start()) + assertTrue(awaitTrue(5_000) { s.pageUp() }, s.tail()) + // taps/crown arrive as G2 sys-events from the right temple; head up wakes; head down is dropped + s.glasses.notify(0xf0, byteArrayOf(7, 3, 1, 0, 0, 0, 0)) + s.glasses.notify(0xf0, byteArrayOf(7, 1, 0, 0, 0, 0, 0)) + s.glasses.notify(0xf0, byteArrayOf(7, 12, 2, 0, 0, 0, 0)) + s.glasses.notify(0xf0, byteArrayOf(7, 15, 2, 0, 0, 0, 0)) + assertEquals(listOf("sys-event:3:1", "sys-event:1:1", "display-wake:12:1"), s.listener.events) + + // the lease ran out on the glasses: the session reopens the page + s.glasses.pageOpen = false + s.glasses.page(0, 3) + assertFalse(s.pageUp()) + s.platform.offsetMs += 4_000 + s.core.interruptibleSleep.interrupt() + assertTrue(awaitTrue(5_000) { s.pageUp() }, "page not reopened:\n" + s.tail()) + + // keepalives well inside the 120 s lease + val keepalives = s.glasses.controls.count { it == RayNeoIoProtocol.CTRL_KEEPALIVE } + s.platform.offsetMs += 31_000 + s.core.interruptibleSleep.interrupt() + assertTrue(awaitTrue(5_000) { s.glasses.controls.count { it == RayNeoIoProtocol.CTRL_KEEPALIVE } > keepalives }) + + // the wearer closed it: stay closed until they touch the glasses again + s.glasses.pageOpen = false + s.glasses.page(0, 4) + s.platform.offsetMs += 4_000 + s.core.interruptibleSleep.interrupt() + sleepMs(300) + assertFalse(s.pageUp()) + s.glasses.notify(0xf0, byteArrayOf(7, 0, 1, 0, 0, 0, 0)) + s.core.interruptibleSleep.interrupt() + assertTrue(awaitTrue(5_000) { s.pageUp() }, "touch did not reopen:\n" + s.tail()) + } finally { + s.core.close() + } + } +} diff --git a/tests/pairing-candidates.test.cjs b/tests/pairing-candidates.test.cjs index 204fe622..50d860fe 100644 --- a/tests/pairing-candidates.test.cjs +++ b/tests/pairing-candidates.test.cjs @@ -12,6 +12,7 @@ const { isPreviouslyPaired, glassesRow, ringRow, + rayneoRow, DEFAULT_STALE_AFTER_MS, } = require("../.test-build/app/g2/pairing-candidates.js"); const { requiredArtworkFileNames, glassesImagePath, GENERIC_G2_IMAGE_PATH } = require("../.test-build/app/g2/glasses-artwork.js"); @@ -289,3 +290,19 @@ test("every artwork file the resolver claims is bundled exists on disk", () => { assert.ok(fs.existsSync(path.join(dir, name)), `missing app/images/glasses/${name}`); } }); + +test("a RayNeo iO is its own candidate, never half of a G2 pair", () => { + const aggregator = new DiscoveryAggregator(); + aggregator.ingest({ + address: "00:6A:5E:5F:33:C2", name: "RayNeo iO-2229", manufacturerData: "1657" + "07006a5e5f33c2130000000000000001", + rssi: -50, txPower: null, connectable: true, bonded: false, source: "scan", seenAtMs: 1000, + }); + assert.equal(aggregator.pairs().length, 0); + assert.equal(aggregator.rings().length, 0); + const found = aggregator.rayneos(); + assert.equal(found.length, 1); + const row = rayneoRow(found[0], { pairedAddress: "00:6a:5e:5f:33:c2" }); + assert.equal(row.kind, "rayneo-io"); + assert.equal(row.canSelect, true); + assert.equal(row.isPreviouslyPaired, true); +});