Build out Ring Status debug tool; fix some aspects of ring direct communication

This commit is contained in:
James Babcock committed 2026-10-06 19:53:43 -07:00
1 parent e12cfaaf38
commit f38595422c
16 files changed
+812 -173

No files matched your search

@@ -7,11 +7,11 @@ class GlassesRingLinkTest {
private fun toHex(bytes: ByteArray) = bytes.joinToString("") { (it.toInt() and 0xff).toString(16).padStart(2, '0') }
private val mac = hex("b16a9f2f43dc")
@Test fun ringMacWireBytesAreLeastSignificantFirst() {
assertEquals("b16a9f2f43dc", toHex(assertNotNull(ringMacWireBytes("DC:43:2F:9F:6A:B1"))))
assertNull(ringMacWireBytes(""))
assertNull(ringMacWireBytes("DC:43:2F:9F:6A"))
assertNull(ringMacWireBytes("DC:43:2F:9F:6A:ZZ"))
@Test fun macWireBytesAreLeastSignificantFirst() {
assertEquals("b16a9f2f43dc", toHex(assertNotNull(macWireBytes("DC:43:2F:9F:6A:B1"))))
assertNull(macWireBytes(""))
assertNull(macWireBytes("DC:43:2F:9F:6A"))
assertNull(macWireBytes("DC:43:2F:9F:6A:ZZ"))
}
@Test fun disconnectRequestWireShape() {
@@ -44,4 +44,64 @@ class GlassesRingLinkTest {
assertNull(BleProtocol.parseRingConnectInfo(BleProtocol.buildAuthenticationRequest(3)))
}
@Test fun unpairRequestWireShape() {
// commandId=9 magic=0x21 unpairInfo(8){dev=RING(0), ringMac}
assertEquals("0809" + "1021" + "420a" + "0800" + "1206b16a9f2f43dc", toHex(BleProtocol.buildRingUnpairRequest(0x21, mac)))
}
@Test fun ringChecksumMatchesCapturedEvenFrames() {
// Frames from HCI snoops of Even's app (g2-kit-unofficial examples/ring-direct.ts).
assertEquals("00971953f964016401000000080d003f0101", toHex(RingProtocol.buildPairAuth(1)))
assertEquals("00f979f7b164016404000000020c001b64",
toHex(RingProtocol.buildCheckedFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_REQ, 0x00, 0x02, 4, ByteArray(0))))
assertEquals("002d946c0164026405000001010c002ac6",
toHex(RingProtocol.buildCheckedFrame(RingProtocol.CHAN_HEALTH, RingProtocol.KIND_REQ, 0x01, 0x01, 5, ByteArray(0))))
assertEquals("00d4c9ba7064016403000200051200807710ff3348bb69",
toHex(RingProtocol.buildCheckedFrame(RingProtocol.CHAN_DEVICE, RingProtocol.KIND_DATA, 0x00, 0x05, 3, hex("10ff3348bb69"))))
}
@Test fun advStartCarriesBothTargetsLeastSignificantFirst() {
val right = assertNotNull(macWireBytes("EC:D7:82:69:3C:CB"))
val left = assertNotNull(macWireBytes("C4:5A:23:3D:F5:F8"))
val frame = RingProtocol.buildAdvStart(9, right, left)
assertValidRingFrame(frame)
assertEquals("00" + "000a", toHex(frame.copyOfRange(10, 13)))
assertEquals("cb3c6982d7ec" + "f8f53d235ac4", toHex(frame.copyOfRange(17, frame.size)))
assertEquals("ffffffffffff".repeat(2),
toHex(RingProtocol.buildAdvStart(9, ByteArray(6) { 0xff.toByte() }, ByteArray(6) { 0xff.toByte() }).copyOfRange(17, 29)))
}
@Test fun touchSwitchAndRemoveRingAreSets() {
val on = RingProtocol.buildTouchSwitch(2, RingProtocol.TOUCH_SWITCH_SELECTOR_GLASSES, true)
assertValidRingFrame(on)
assertEquals("02" + "0007", toHex(on.copyOfRange(10, 13)))
assertEquals("0201", toHex(on.copyOfRange(17, on.size)))
assertEquals("0200", toHex(RingProtocol.buildTouchSwitch(2, 2, false).copyOfRange(17, 19)))
val remove = RingProtocol.buildRemoveRingNotify(3)
assertValidRingFrame(remove)
assertEquals("02" + "0082" + "01", toHex(remove.copyOfRange(10, 13)) + toHex(remove.copyOfRange(17, remove.size)))
}
@Test fun ringResponseStatusDecodes() {
assertEquals("ok", RingProtocol.responseResult(0x03))
assertEquals("error", RingProtocol.responseResult(0x07))
assertEquals("refused", RingProtocol.responseResult(0x0b))
assertEquals("unsupported", RingProtocol.responseResult(0x0f))
assertNull(RingProtocol.responseResult(0x00))
assertNull(RingProtocol.responseResult(0x02))
}
/** Outer CRC-32C and inner phone checksum both match the bytes. */
private fun assertValidRingFrame(frame: ByteArray) {
val crc32 = (frame[1].toInt() and 0xff) or ((frame[2].toInt() and 0xff) shl 8) or
((frame[3].toInt() and 0xff) shl 16) or ((frame[4].toInt() and 0xff) shl 24)
assertEquals(RingProtocol.crc32(frame, 5, frame.size), crc32)
val args = frame.copyOfRange(17, frame.size)
val checksum = RingProtocol.phoneChecksum(frame[6].toInt(), frame[8].toInt() and 0xff,
frame[10].toInt(), frame[11].toInt(), frame[12].toInt() and 0xff, args)
assertEquals(checksum, (frame[15].toInt() and 0xff) or ((frame[16].toInt() and 0xff) shl 8))
assertEquals(frame.size - 5, (frame[13].toInt() and 0xff) or ((frame[14].toInt() and 0xff) shl 8))
}
}
@@ -22,6 +22,34 @@ class RingInputTest {
assertEquals(decoded.ringTick, native.ringTick)
}
@Test fun directReportsMapLikeTheGlassesRelay() {
// Frames captured on the direct link (bae80011), 2026-10-06.
val down = assertNotNull(FaceclawRingEventDecoder.decode(hex("000961000a000013cb0000"))).event
assertEquals(BleProtocol.EVENT_RING_PRESS, down.eventType)
assertEquals(BleProtocol.EVENT_SOURCE_RING, down.eventSource)
assertEquals(10, down.ringType)
assertEquals(0xcb13L, down.ringTick)
val tapHold = assertNotNull(FaceclawRingEventDecoder.decode(hex("0009610009020055d60200"))).event
assertEquals(BleProtocol.EVENT_SHORT_THEN_LONG_PRESS, tapHold.eventType)
assertEquals(2, tapHold.ringAux)
assertEquals(0x02d655L, tapHold.ringTick)
val swipe = assertNotNull(FaceclawRingEventDecoder.decode(hex("0009610004350009cc0000"))).event
assertEquals(BleProtocol.EVENT_SCROLL_TOP, swipe.eventType)
assertEquals(0x35, swipe.ringAux)
assertEquals(0x00, swipe.ringSpeed)
// Same mapping as the CFW relay for every wire type (gesture_fwd.c).
val relayed = mapOf(0 to 9, 1 to 0, 2 to 3, 4 to 1, 5 to 2, 8 to 10, 9 to 11, 10 to 14, 3 to 127, 0xff to 127)
for ((wire, sysEvent) in relayed) {
val frame = hex("0009610000abcdefcdab89").also { it[4] = wire.toByte() }
val event = assertNotNull(FaceclawRingEventDecoder.decode(frame)).event
assertEquals(sysEvent, event.eventType, "wire type $wire")
assertEquals(0x89abcdefL, event.ringTick)
}
}
@Test fun legacyAndInvalidExtensionsNeverInventATimestamp() {
assertEquals(-1L, assertNotNull(G2Event.decodePayload(224, hex("08026a041a02080e"))).ringTick)
for (offset in listOf(9,13,14,15,16,20)) {