diff --git a/app/src/main/java/com/droiddeck/launcher/glasses/Glasses.kt b/app/src/main/java/com/droiddeck/launcher/glasses/Glasses.kt index 3c20e3ce7d..86f363f5ab 100644 --- a/app/src/main/java/com/droiddeck/launcher/glasses/Glasses.kt +++ b/app/src/main/java/com/droiddeck/launcher/glasses/Glasses.kt @@ -337,18 +337,24 @@ object Glasses { } private fun refresh(ask: Boolean = false) { - val device = usb.deviceList.values.firstOrNull { it.vendorId == VitureProtocol.VENDOR_ID } - val permitted = device != null && usb.hasPermission(device) + val devices = usb.deviceList.values.filter { it.vendorId == VitureProtocol.VENDOR_ID } + val known = devices.mapNotNull { device -> + VitureProtocol.model(device.productId, runCatching { device.productName }.getOrNull())?.let { device to it } + } + val selected = known.firstOrNull { it.second.controllable } ?: known.firstOrNull() + val device = selected?.first + val m = selected?.second + val controllable = m?.controllable == true + val permitted = controllable && device != null && usb.hasPermission(device) val identity = device?.let { GlassesModeState.Device(it.productId, if (permitted) runCatching { it.serialNumber }.getOrNull() else null) } if (modeState.updateDevice(identity, SystemClock.uptimeMillis())) { rememberSbs(GlassesState.SBS_UNKNOWN) } - val m = device?.let { VitureProtocol.model(it.productId, runCatching { it.productName }.getOrNull()) } if (device == null || (linkDevice != null && linkDevice?.deviceName != device.deviceName)) closeLink() model = m - val display = findDisplay(device != null) + val display = findDisplay(devices.isNotEmpty()) val mode = display?.mode val width = mode?.physicalWidth ?: 0 val height = mode?.physicalHeight ?: 0 @@ -362,15 +368,15 @@ object Glasses { else -> Unit } if (sbsByDisplay) rememberSbs(GlassesState.SBS_WORKS) - if (device != null && !permitted && (ask || !asked) && _state.value.permission != Permission.ASKING) askPermission(device) - if (device != null && permitted && link == null && m?.controllable == true) openLink(device) + if (controllable && device != null && !permitted && (ask || !asked) && _state.value.permission != Permission.ASKING) askPermission(device) + if (device != null && permitted && link == null) openLink(device) publish { it.copy( - present = device != null || display != null, + present = devices.isNotEmpty() || display != null, model = m?.name ?: display?.let(::displayName) ?: "VITURE", - controllable = m?.controllable == true, + controllable = controllable, permission = when { - device == null -> Permission.NONE + !controllable -> Permission.NONE permitted -> Permission.GRANTED it.permission == Permission.ASKING || it.permission == Permission.DENIED -> it.permission else -> Permission.NONE diff --git a/app/src/main/java/com/droiddeck/launcher/glasses/VitureProtocol.kt b/app/src/main/java/com/droiddeck/launcher/glasses/VitureProtocol.kt index 45c115ee9d..1b943b284d 100644 --- a/app/src/main/java/com/droiddeck/launcher/glasses/VitureProtocol.kt +++ b/app/src/main/java/com/droiddeck/launcher/glasses/VitureProtocol.kt @@ -127,13 +127,13 @@ object VitureProtocol { class Frame(val magic: Int, val msgId: Int, val payload: ByteArray, val crcOk: Boolean) fun parse(buf: ByteArray, size: Int): Frame? { - if (size < HEADER || (buf[0].toInt() and 0xFF) != 0xFF) return null + if (size < HEADER || size > buf.size || (buf[0].toInt() and 0xFF) != 0xFF) return null val magic = buf[1].toInt() and 0xFF if (magic != MAGIC_COMMAND && magic != MAGIC_REPORT && magic != MAGIC_IMU) return null val len = u16(buf, 4) - if (len < 0x0C) return null - val dataLen = (len - 0x0C).coerceAtMost(size - HEADER) - val crcOk = len + 6 <= size && crc16(buf, 4, len + 2) == u16(buf, 2) + if (len < 0x0C || len + 6 > size) return null + val dataLen = len - 0x0C + val crcOk = crc16(buf, 4, len + 2) == u16(buf, 2) return Frame(magic, u16(buf, 14), buf.copyOfRange(HEADER, HEADER + dataLen), crcOk) } diff --git a/app/src/test/java/com/droiddeck/launcher/glasses/GlassesUsbTest.kt b/app/src/test/java/com/droiddeck/launcher/glasses/GlassesUsbTest.kt new file mode 100644 index 0000000000..451dc7fb74 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/glasses/GlassesUsbTest.kt @@ -0,0 +1,129 @@ +package com.droiddeck.launcher.glasses + +import android.app.PendingIntent +import android.content.Context +import android.hardware.display.DisplayManager +import android.hardware.usb.UsbDevice +import android.hardware.usb.UsbManager +import android.os.Handler +import android.os.Looper +import android.view.Display +import kotlinx.coroutines.flow.MutableStateFlow +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements +import org.robolectric.annotation.LooperMode +import org.robolectric.shadow.api.Shadow +import org.robolectric.shadows.ShadowDisplayManager +import org.robolectric.shadows.ShadowUsbManager +import org.robolectric.util.ReflectionHelpers +import org.robolectric.util.ReflectionHelpers.ClassParameter.from + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE, shadows = [GlassesUsbTest.RecordingUsbManager::class]) +@LooperMode(LooperMode.Mode.PAUSED) +class GlassesUsbTest { + private lateinit var manager: RecordingUsbManager + private lateinit var state: MutableStateFlow + private fun field(name: String) = Glasses::class.java.getDeclaredField(name).apply { isAccessible = true } + + @Before fun prepare() { + val context = RuntimeEnvironment.getApplication() + val usb = context.getSystemService(Context.USB_SERVICE) as UsbManager + manager = Shadow.extract(usb) + field("app").set(null, context) + field("usb").set(null, usb) + field("displays").set(null, context.getSystemService(Context.DISPLAY_SERVICE)) + field("handler").set(null, Handler(Looper.getMainLooper())) + field("asked").setBoolean(null, false) + @Suppress("UNCHECKED_CAST") + state = field("_state").get(null) as MutableStateFlow + state.value = GlassesState() + } + + @Test fun controllableGlassesTakePriorityOverDockAndUnsupportedGlasses() { + val dock = device(0x3011) + val beast = device(0x1201) + val pro = device(0x101d) + manager.devices.putAll(listOf(dock, beast, pro).associateBy { it.deviceName }) + refresh() + assertEquals(listOf(pro), manager.requests) + assertTrue(state.value.controllable) + assertEquals(Glasses.Permission.ASKING, state.value.permission) + } + + @Test fun unsupportedDevicesNeverRequestOrAdvertiseUsbPermission() { + for (pid in listOf(0x3011, 0xffff, 0x1201)) { + val device = device(pid) + manager.devices.clear() + manager.devices[device.deviceName] = device + state.value = GlassesState() + refresh(ask = true) + assertTrue(manager.requests.isEmpty()) + assertEquals(Glasses.Permission.NONE, state.value.permission) + manager.granted.add(device.deviceName) + state.value = GlassesState(permission = Glasses.Permission.ASKING) + refresh(ask = true) + assertTrue(manager.requests.isEmpty()) + assertEquals(Glasses.Permission.NONE, state.value.permission) + assertTrue(state.value.present) + assertFalse(state.value.controllable) + assertFalse(state.value.connected) + } + } + + @Test fun dockPresenceStillIdentifiesAnUnnamedPresentationDisplay() { + val hdmi = ReflectionHelpers.getStaticField(Display::class.java, "TYPE_HDMI") + val displayId = ShadowDisplayManager.addDisplay("w1920dp-h1080dp", hdmi) + val displays = RuntimeEnvironment.getApplication().getSystemService(DisplayManager::class.java) + val display = displays.getDisplay(displayId) + shadowOf(display).setFlags(Display.FLAG_PRESENTATION) + assertEquals(displayId, displays.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).single().displayId) + assertFalse(display.name.contains("VITURE", ignoreCase = true)) + val dock = device(0x3011) + manager.devices[dock.deviceName] = dock + refresh() + assertEquals(displayId, state.value.displayId) + assertTrue(manager.requests.isEmpty()) + assertEquals(Glasses.Permission.NONE, state.value.permission) + } + + private fun refresh(ask: Boolean = false) { + Glasses::class.java.getDeclaredMethod("refresh", Boolean::class.javaPrimitiveType) + .apply { isAccessible = true }.invoke(Glasses, ask) + } + + private fun device(pid: Int): UsbDevice = ReflectionHelpers.callConstructor( + UsbDevice::class.java, + from(String::class.java, "/dev/bus/usb/$pid"), + from(Int::class.javaPrimitiveType, VitureProtocol.VENDOR_ID), + from(Int::class.javaPrimitiveType, pid), + from(Int::class.javaPrimitiveType, 0), + from(Int::class.javaPrimitiveType, 0), + from(Int::class.javaPrimitiveType, 0), + from(String::class.java, "VITURE"), + from(String::class.java, "VITURE device"), + from(String::class.java, "1.0"), + from(String::class.java, "test-$pid"), + ) + + @Implements(UsbManager::class) + class RecordingUsbManager : ShadowUsbManager() { + val devices = linkedMapOf() + val granted = mutableSetOf() + val requests = mutableListOf() + + @Implementation override fun getDeviceList(): HashMap = LinkedHashMap(devices) + @Implementation override fun hasPermission(device: UsbDevice): Boolean = device.deviceName in granted + @Implementation fun requestPermission(device: UsbDevice, intent: PendingIntent) { requests += device } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/glasses/VitureProtocolTest.kt b/app/src/test/java/com/droiddeck/launcher/glasses/VitureProtocolTest.kt index cea0a0db8a..f0696caf7f 100644 --- a/app/src/test/java/com/droiddeck/launcher/glasses/VitureProtocolTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/glasses/VitureProtocolTest.kt @@ -41,7 +41,40 @@ class VitureProtocolTest { assertArrayEquals(byteArrayOf(0x34), f.payload) assertTrue(f.crcOk) p[18] = 0x31 - assertFalse(VitureProtocol.parse(p, p.size)!!.crcOk) + val badCrc = VitureProtocol.parse(p, p.size)!! + assertFalse(badCrc.crcOk) + assertArrayEquals(byteArrayOf(0x31), badCrc.payload) + } + + @Test fun parseAcceptsPaddingOutsideDeclaredFrame() { + val payload = byteArrayOf(0, 0x32) + val p = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, payload) + val end = 18 + payload.size + p.fill(0x5A, end) + for (size in listOf(end, p.size)) { + val frame = VitureProtocol.parse(p, size)!! + assertArrayEquals(payload, frame.payload) + assertTrue(frame.crcOk) + } + } + + @Test fun parseRejectsTruncatedDeclaredFrame() { + val p = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, byteArrayOf(0, 0x32)) + val received = 19 + assertNull(VitureProtocol.parse(p, received)) + assertNull(VitureProtocol.parse(p.copyOf(received), received)) + p[4] = 0xFF.toByte() + p[5] = 0xFF.toByte() + assertNull(VitureProtocol.parse(p, p.size)) + } + + @Test fun parseRejectsInvalidReceiveSize() { + val p = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, byteArrayOf(0, 0x32)) + assertNull(VitureProtocol.parse(p, p.size + 1)) + assertNull(VitureProtocol.parse(p, 17)) + assertNull(VitureProtocol.parse(ByteArray(0), 18)) + assertNull(VitureProtocol.parse(ByteArray(0), 0)) + assertNull(VitureProtocol.parse(p, -1)) } @Test fun parseReadsRepliesAndRejectsJunk() { diff --git a/docs/development/astra-viture-stereo.md b/docs/development/astra-viture-stereo.md index fd9f8307a5..9d0a346833 100644 --- a/docs/development/astra-viture-stereo.md +++ b/docs/development/astra-viture-stereo.md @@ -52,6 +52,25 @@ Ultra capability. The connected Pro firmware matches both the N6PL entry in the N6P entry in the [public release-note service](https://cloud.viture.dev/api/v1/system/releasenote) checked on 2026-10-14. This does not rule out unpublished, device-specific updates. +## Powered dock results + +A VITURE Pro Mobile Dock (`35ca:3011`) was connected between the Astra and Pro XR. +Its official read-only P9 version query returned `2.01.03.0.03` with a valid +checksum. The Astra negotiated 1920×1080/120, but enumerated only the dock's +USB interfaces, not the glasses' `35ca:101d` control/IMU interfaces. A one-second +DroidDeck IMU sample returned zero packets. + +DroidDeck now keeps the dock's display available without requesting unsupported +USB control access. The updated build was installed on this connection: no USB +permission prompt appeared, video remained at 1080p/120 and tracking stayed off. + +[VITURE's Pro Dock specifications](https://www.viture.com/product/viture-pro-mobile-dock) +confirm 3840×1080/60 video support and exclude phone features requiring +communication with the glasses. The vendor SDK also treats the dock as a separate +accessory, not an IMU device. This dock may test the video path, but it does not +provide DroidDeck's current head-tracking path. Stereo negotiation and mode +retention during an upstream-only reconnect still require a physical test. + ## No-root hardware design to test A powered pass-through connection may let the tablet see the stereo EDID on a @@ -104,8 +123,9 @@ temperatures rather than cached thermal-service samples. - Full debug APK build, including the Rust store engine; native-library, session asset and runtime-input packaging checks passed. -- 840 Android unit tests across 140 suites passed, including mode deadlines, - reconnect ownership, stale USB callbacks and model-specific protocol cases. +- 846 Android unit tests across 141 suites passed, including mode deadlines, + reconnect ownership, stale USB callbacks, dock selection and permission handling, + truncated USB frames and model-specific protocol cases. - Four host tests passed, including native refresh notification, event coalescing, full-pipe handling and Cardboard output checks. - On the Astra: brightness changed and was restored; IMU delivered 400 packets @@ -118,6 +138,7 @@ temperatures rather than cached thermal-service samples. - DroidDeck was force-stopped and relaunched after testing. Debugging tools and temporary logging/display overrides were restored. Current HAL temperatures after the session were 25°C battery and 28.3°C skin. +- The dock checks ran without a game load; the idle battery temperature was 24°C. These checks do not establish stereo-game performance or a sustained thermal baseline. Direct-cable stereo and the powered-adapter design remain unverified