Add ANCS support to G1

This commit is contained in:
James Babcock committed 2026-11-06 21:47:23 -08:00
1 parent 0cc263c5d2
commit 6e4515afa5
12 files changed
+199 -19

No files matched your search

@@ -10,6 +10,8 @@ object G1Ble {
const val PAIR_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4404"
/** Faceclaw's records (VM.md §6.11). */
const val FC_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4405"
/** The iPhone notification relay (VM.md §6.12): the G2's `AN` commands and frames, with this characteristic to itself. */
const val ANCS_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4406"
const val REQUESTED_MTU = 247
/** The oldest firmware this app works with: `PROP_SET` came in here. */
const val REQUIRED_FIRMWARE = "0.5.63"
@@ -188,13 +190,16 @@ object G1Protocol {
@JvmField val inCase: Boolean,
/** `MIC` captures here: the right arm. */
@JvmField val mic: Boolean,
/** The iPhone notification relay answers here (0.5.65+). */
@JvmField val ancs: Boolean,
)
fun parseHello(body: ByteArray): Hello? {
if (body.size < 7 || body[0].toInt() != 5) return null
val flags = body[5].toInt() and 255
return Hello("${body[1].toInt() and 255}.${body[2].toInt() and 255}.${body[3].toInt() and 255}",
body[4].toInt() == 1, flags and 1 != 0, flags and 2 != 0, flags and 64 != 0, flags and 128 != 0, body[6].toInt() and 4 != 0)
body[4].toInt() == 1, flags and 1 != 0, flags and 2 != 0, flags and 64 != 0, flags and 128 != 0, body[6].toInt() and 4 != 0,
body[6].toInt() and 8 != 0)
}
/** The display path's version, or -1 for a body that isn't FC_INFO's. */
@@ -526,7 +526,8 @@ class GlassesSessionCore(
/**
* Write one already-framed packet to an arm outside the message scheduler (the ANCS
* relay client builds its own packets). Blocks like any link write; false when the
* arm is unknown, the link rejects the write, or the session is not running.
* arm is unknown, the link rejects the write, or the session is not running. A G1 has a
* characteristic for them, whose ATT errors say why a command was refused.
*/
fun writeRawPacket(arm: String, packet: ByteArray): Boolean {
val address = when (arm.uppercase()) {
@@ -537,8 +538,11 @@ class GlassesSessionCore(
val active = monitor.withLock { running }
if (!active) return false
return try {
val result = link.writeFrames(address, BleProtocol.WRITE_CHAR_UUID, listOf(packet), ConnectionOptions.WRITE_MODE,
ConnectionOptions.WRITE_TIMEOUT_MS)
val result = if (isG1) {
link.writeFrames(address, G1Ble.ANCS_CHAR_UUID, listOf(packet), GattWriteMode.WITH_RESPONSE, ConnectionOptions.WRITE_TIMEOUT_MS)
} else {
link.writeFrames(address, BleProtocol.WRITE_CHAR_UUID, listOf(packet), ConnectionOptions.WRITE_MODE, ConnectionOptions.WRITE_TIMEOUT_MS)
}
if (result) monitor.withLock { noteWriteCompletedLocked(address, now()) }
result
} catch (t: Throwable) {
@@ -589,6 +593,9 @@ class GlassesSessionCore(
/** True when this session drives an Even G1. */
fun isEvenG1(): Boolean = isG1
/** Whether the glasses can relay iPhone notifications, as far as the session knows: a G2's depends on its firmware revision, which TS checks. */
fun ancsRelayAvailable(): Boolean = !isG1 || monitor.withLock { g1.ancsRelay }
/** True when this session drives a RayNeo iO. */
fun isRayNeoIo(): Boolean = isIo
@@ -54,6 +54,8 @@ internal class G1SessionState {
var micOn = false
var lastMicRenewAtMs = 0L
val mic = G1MicAssembler()
/** The right arm's relay is there and this host listens to it. */
var ancsRelay = false
fun nextTag(): Int {
tag = tag % 255 + 1
@@ -72,6 +74,7 @@ internal class G1SessionState {
micOn = false
lastMicRenewAtMs = 0
mic.reset()
ancsRelay = false
}
}
@@ -519,6 +522,7 @@ internal fun GlassesSessionCore.g1ConnectLoopOnce() {
// A limit the last session left, when the other eye's panel guard had capped it.
g1Control(arm, G1Protocol.OP_PROP_SET, G1Protocol.propSet(G1Protocol.PROP_BRIGHTNESS_MAX, G1Protocol.BRIGHTNESS_LEVEL_MAX))
}
g1ListenToAncsRelay()
monitor.withLock {
sessionReady = true
shutdownRequested = false
@@ -570,10 +574,31 @@ internal fun GlassesSessionCore.g1ConnectLoopOnce() {
}
}
/**
* Only a host with a relay client listens, and the arm does nothing until that client says START. An arm updated just now
* may still look old to the OS, which keeps the GATT layout it last saw until it hears otherwise: not worth failing the
* session, and the next connection sees it.
*/
private fun GlassesSessionCore.g1ListenToAncsRelay() {
val wanted = rawFrameTap != null && monitor.withLock { g1.arms[1].hello?.ancs == true }
val listening = wanted && link.enableNotifications(rightAddress, G1Ble.ANCS_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)
if (wanted && !listening) logWarn("G1: the right arm has a notification relay, but subscribing to it failed")
monitor.withLock { g1.ancsRelay = listening }
}
// ---------------------------------------------------------------------------------------------
// Inbound (BLE callback thread)
internal fun GlassesSessionCore.g1OnNotification(address: String, characteristicUuid: String, data: ByteArray) {
if (G1Ble.ANCS_CHAR_UUID == characteristicUuid) {
val tap = rawFrameTap
if (tap != null && data.size >= 2 && data[0] == GlassesSessionCore.RELAY_TAG_A && data[1] == GlassesSessionCore.RELAY_TAG_N
&& address.equals(rightAddress, ignoreCase = true)) {
monitor.withLock { lastIncomingAtMs = now() }
tap(data.copyOf())
}
return
}
if (G1Ble.EVT_CHAR_UUID != characteristicUuid) return
val arm = armIndex(address)
if (arm < 0) return
@@ -108,6 +108,8 @@ class IosGlassesSession internal constructor(
fun isAncsAuthorized(): Boolean = central?.isAncsAuthorized(rightIdentifier) ?: false
fun ancsRelayAvailable(): Boolean = core.ancsRelayAvailable()
fun setAncsListener(listener: IosAncsListener?) {
if (listener == null) {
core.setRawFrameTap(null)