Improve connection stability, I hope

This commit is contained in:
James Babcock committed 2026-09-29 14:33:23 -07:00
1 parent 48f7f3d01f
commit 6267afaff7
9 files changed
+497 -243

No files matched your search

@@ -8,6 +8,10 @@ class ConnectionOptions {
const val DESIRED_MTU = 512
const val CONNECT_TIMEOUT_MS = 5_000
// How long one session connect attempt keeps both arms' connections pending before
// giving up. Both are dialed at once, so a slow or missing arm gets the whole
// window (it stays under Android's own ~30 s direct-connect timeout).
const val ARM_CONNECT_WINDOW_MS = 20_000
const val SERVICES_TIMEOUT_MS = 5_000
const val DESCRIPTOR_TIMEOUT_MS = 5_000
const val RING_DESIRED_MTU = 247
@@ -36,7 +40,13 @@ class ConnectionOptions {
const val MAX_CONSECUTIVE_ACK_TIMEOUTS = 8
const val EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000
const val IDLE_SLEEP_MS = 100
const val RECONNECT_DELAY_MS = 2_000
// Delay before each consecutive reconnect attempt (the last entry repeats). Android
// keeps an LE link up for a few seconds after its last GATT client closes (4 s on a
// Pixel), so the quick first retry reuses the old links and the glasses never see a
// disconnect; later retries wait long enough for the links to really drop.
@JvmField val RECONNECT_BACKOFF_MS = intArrayOf(2_000, 6_000, 10_000)
// A session that stayed up this long starts the backoff over when it fails.
const val STABLE_SESSION_MS = 30_000
// Cap on rects per batch. Each rect consumes a distinct CFW frame id, and the
// firmware's duplicate-fid ring holds 16, so keep several batches of history
// within it. Above this the split is abandoned for a single bounding box.
@@ -386,11 +386,17 @@ internal fun GlassesSessionCore.handleUnpairedFailure(address: String) {
interruptibleSleep.interrupt()
}
internal fun GlassesSessionCore.handleTransportFailure(reason: String?) {
/**
* Tear the session down and schedule a reconnect. [displayStatus], when given, replaces the
* generic "Reconnecting after <reason>" status line.
*/
internal fun GlassesSessionCore.handleTransportFailure(reason: String?, displayStatus: String? = null) {
logError("Transport failure: $reason")
val retryDelayMs: Long
monitor.withLock {
maybeEmitEvenAppConflictLocked(reason)
finishBenchmarkLocked(true, "transport failure")
val sessionWasReady = sessionReady
sessionReady = false
fixedLayoutCreated = false
startupProbePending = false
@@ -402,16 +408,38 @@ internal fun GlassesSessionCore.handleTransportFailure(reason: String?) {
lastFaceclawWakeLeaseQueuedAtMs = 0
faceclawWakeControlSentCount = 0
clearAllMessagesLocked("transport failure: $reason")
reconnectAfterMs = now() + ConnectionOptions.RECONNECT_DELAY_MS
retryDelayMs = scheduleReconnectLocked(sessionWasReady)
link.disconnect(rightAddress)
link.disconnect(leftAddress)
}
logLine("reconnect attempt in " + retryDelayMs + "ms")
if (!userDisconnectRequested) {
setStateDisplay("retrying", if (reason == null || reason.isEmpty()) "Reconnecting..." else "Reconnecting after $reason")
setStateDisplay("retrying", when {
displayStatus != null -> displayStatus + " Retrying in " + ((retryDelayMs + 999) / 1000) + "s."
reason == null || reason.isEmpty() -> "Reconnecting..."
else -> "Reconnecting after $reason"
})
}
interruptibleSleep.interrupt()
}
/**
* Set [reconnectAfterMs] for the next attempt after a failure and return the delay. Repeated
* failures back off along RECONNECT_BACKOFF_MS; a session that stayed up for
* STABLE_SESSION_MS before failing ([sessionWasReady]) starts the schedule over.
*/
internal fun GlassesSessionCore.scheduleReconnectLocked(sessionWasReady: Boolean): Long {
val now = now()
if (sessionWasReady && now - lastSessionReadyAtMs >= ConnectionOptions.STABLE_SESSION_MS) {
consecutiveReconnects = 0
}
val schedule = ConnectionOptions.RECONNECT_BACKOFF_MS
val delayMs = schedule[minOf(consecutiveReconnects, schedule.size - 1)].toLong()
consecutiveReconnects++
reconnectAfterMs = now + delayMs
return delayMs
}
internal fun GlassesSessionCore.resetSessionStateLocked() {
ringBattery = -1
ringCharging = -1
@@ -424,6 +452,7 @@ internal fun GlassesSessionCore.resetSessionStateLocked() {
ringConnected = false
ringNotificationsReady = false
reconnectAfterMs = 0
consecutiveReconnects = 0
reconnectHalted = false
ringReconnectAfterMs = 0
lastAckAtMs = 0
@@ -192,6 +192,8 @@ class GlassesSessionCore(
internal var lastCfwCleanupAckMagic = 0
internal var reconnectAfterMs = 0L
// Failed connects/sessions since the last stable session; indexes RECONNECT_BACKOFF_MS.
internal var consecutiveReconnects = 0
internal var ringReconnectAfterMs = 0L
internal var lastAckAtMs = 0L
internal var lastIncomingAtMs = 0L
@@ -1611,19 +1613,26 @@ class GlassesSessionCore(
}
override fun onConnectionStateChange(address: String?, connected: Boolean) {
monitor.withLock {
if (address == null) {
return
}
if (isConfiguredRingAddress(address)) {
if (address == null) {
return
}
if (isConfiguredRingAddress(address)) {
monitor.withLock {
ringConnected = connected
ringNotificationsReady = false
if (!connected) {
if (connected) {
// A background connect landed: finish setting it up right away.
ringReconnectAfterMs = 0
} else {
ringNotificationsReady = false
ringReconnectAfterMs = now() + ConnectionOptions.RING_RECONNECT_DELAY_MS
}
logLine(if (connected) "direct ring BLE connected" else "direct ring BLE disconnected")
return
}
logLine(if (connected) "direct ring BLE connected" else "direct ring BLE disconnected")
interruptibleSleep.interrupt()
return
}
var lostSession = false
monitor.withLock {
if (address.equals(rightAddress, ignoreCase = true)) {
rightConnected = connected
} else if (address.equals(leftAddress, ignoreCase = true)) {
@@ -1632,6 +1641,10 @@ class GlassesSessionCore(
return
}
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
// schedules the retry, so leave the backoff alone.
lostSession = sessionReady
sessionReady = false
fixedLayoutCreated = false
startupProbePending = false
@@ -1640,17 +1653,14 @@ class GlassesSessionCore(
audioPacketListener = null
clearAllMessagesLocked("connection lost")
displayedFingerprint = ""
if (!reconnectHalted) {
reconnectAfterMs = now() + ConnectionOptions.RECONNECT_DELAY_MS
if (lostSession && !reconnectHalted) {
scheduleReconnectLocked(true)
}
}
}
interruptibleSleep.interrupt()
if (connected) {
setStateDisplay("connected", "Connected.")
} else if (!reconnectHalted) {
// While parked on a missing bond, keep the "unpaired" display: this
// callback is just the teardown of the arm that did connect.
if (lostSession && !reconnectHalted) {
// While parked on a missing bond, keep the "unpaired" display.
setStateDisplay("connecting", "Connecting to the glasses...")
}
}
@@ -1667,8 +1677,13 @@ class GlassesSessionCore(
}
setStateDisplay("connecting", "Connecting to the glasses...")
try {
connectArm(rightAddress, true)
connectArm(leftAddress, true)
// Dial both arms at once, so the left arm's attempt is already pending (and
// can land) while the right arm is being set up; they share one window.
val deadlineMs = now() + ConnectionOptions.ARM_CONNECT_WINDOW_MS
link.beginConnect(rightAddress, false)
link.beginConnect(leftAddress, false)
connectArm(rightAddress, true, deadlineMs)
connectArm(leftAddress, true, deadlineMs)
if (!sleepDuringConnectSettling(800)) {
return
}
@@ -1733,6 +1748,17 @@ class GlassesSessionCore(
val unpairedArm = firstUnpairedArm()
if (unpairedArm != null) {
handleUnpairedFailure(unpairedArm)
} else if (t is ArmUnreachableException) {
val otherArm = if (t.side == "left") rightAddress else leftAddress
if (link.isConnected(otherArm)) {
handleTransportFailure(
t.side + " arm not found",
"Can't reach the " + t.side + " arm of the glasses. It may be connected to"
+ " another device, or be off or out of range."
)
} else {
handleTransportFailure("glasses not found", "Can't find the glasses. They may be off or out of range.")
}
} else {
handleTransportFailure("connect failed")
}
@@ -1753,9 +1779,10 @@ class GlassesSessionCore(
}
}
internal fun connectArm(address: String, enableRenderNotify: Boolean) {
if (!link.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) {
throw IllegalStateException("connect failed: $address")
internal fun connectArm(address: String, enableRenderNotify: Boolean, deadlineMs: Long) {
val timeoutMs = maxOf(deadlineMs - now(), ConnectionOptions.CONNECT_TIMEOUT_MS.toLong())
if (!link.connect(address, timeoutMs.toInt())) {
throw ArmUnreachableException(if (address.equals(leftAddress, ignoreCase = true)) "left" else "right", address)
}
// requestConnectionPriority has no callback in this Android compile target, so there is
// no reliable completion point to keep it in the global GATT operation pipeline. But it's
@@ -1802,6 +1829,16 @@ class GlassesSessionCore(
return
}
try {
// The ring is often out of range or bound to the glasses. Rather than block this
// worker (and every frame and heartbeat behind it) on a timed connect, keep a
// low-duty background connection pending; its callback wakes us to finish setup.
// The periodic recheck re-arms it if the attempt died without a callback.
if (!link.isConnected(ringAddress) && link.beginConnect(ringAddress, true)) {
monitor.withLock {
ringReconnectAfterMs = now() + ConnectionOptions.RING_RECONNECT_DELAY_MS
}
return
}
connectRing()
} catch (t: Throwable) {
monitor.withLock {
@@ -1810,6 +1847,9 @@ class GlassesSessionCore(
ringReconnectAfterMs = now() + ConnectionOptions.RING_RECONNECT_DELAY_MS
}
logLine("direct ring connect failed (" + reason + "): " + safeMessage(t))
// Start the next attempt from a fresh connection rather than retrying setup on
// one that just failed it.
link.disconnect(ringAddress)
}
}
@@ -1980,3 +2020,7 @@ class GlassesSessionCore(
}
}
}
/** An arm's connection never came up within the connect window ([side] is "left" or "right"). */
internal class ArmUnreachableException(val side: String, address: String) :
IllegalStateException("connect failed: $side arm $address")
@@ -46,6 +46,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
}
var messageToWrite: OutboundMessage? = null
var messageToPrewrite: OutboundMessage? = null
var heartbeatToWrite: OutboundMessage? = null
val now = now()
maybeFinishNoChangeDesiredFrame()
@@ -196,9 +197,16 @@ internal fun GlassesSessionCore.driveSession(): Long {
&& (hasPendingImageLocked()
|| desiredFingerprintSnapshot() != lastEnqueuedFingerprint)))
noteImageStallLocked(now, windowHasRoom)
if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) {
val heartbeatStep = if (messageToPrewrite == null) heartbeatStepLocked(imageWaiting) else HeartbeatStep.NONE
if (heartbeatStep == HeartbeatStep.HOLD) {
return ConnectionOptions.IDLE_SLEEP_MS.toLong()
}
if (heartbeatStep == HeartbeatStep.SEND) {
logLine("Writing heartbeat")
heartbeatToWrite = createHeartbeatMessage()
lastHeartbeatSentAtMs = now()
return@withLock
}
if (messageToPrewrite == null && sessionReady && windowHasRoom && !pendingMessages.isEmpty()
&& CfwMessageWindow.canSend(inFlightMessages, pendingMessages.firstOrNull()!!)) {
@@ -227,6 +235,15 @@ internal fun GlassesSessionCore.driveSession(): Long {
}
}
val heartbeat = heartbeatToWrite
if (heartbeat != null) {
// Like every write, outside `monitor`: waiting for the write to complete while
// holding it would stall notification handling, which needs it. A failed write
// just leaves the heartbeat to its ack timeout (see createHeartbeatMessage).
writeMessage(heartbeat)
return ConnectionOptions.IDLE_SLEEP_MS.toLong()
}
val prewrite = messageToPrewrite
if (prewrite != null) {
if (prewriteMessage(prewrite)) {
@@ -285,7 +302,17 @@ internal fun GlassesSessionCore.canPrewriteCandidate(message: OutboundMessage?):
return message.message.size + 2 > 232
}
internal fun GlassesSessionCore.handleHeartbeat(imageWaiting: Boolean): Boolean {
/** What the heartbeat schedule wants from one driveSession pass. */
internal enum class HeartbeatStep {
/** Nothing due: carry on with other traffic. */
NONE,
/** Send nothing else this pass (a heartbeat is pending, or overdue but blocked). */
HOLD,
/** Write a heartbeat now, then hold. */
SEND,
}
internal fun GlassesSessionCore.heartbeatStepLocked(imageWaiting: Boolean): HeartbeatStep {
val now = now()
val heartbeatEligible = !shutdownRequested && fixedLayoutCreated
val heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat")
@@ -300,22 +327,18 @@ internal fun GlassesSessionCore.handleHeartbeat(imageWaiting: Boolean): Boolean
// the heartbeat still fires once we reach the URGENT threshold
// if rendering goes quiet again. Preserves the pending-heartbeat
// inter-lens-sync invariant below (that path is untouched).
return false
return HeartbeatStep.NONE
}
logLine("Writing heartbeat")
val heartbeatMessage = createHeartbeatMessage()
lastHeartbeatSentAtMs = now
writeMessage(heartbeatMessage)
return true
return HeartbeatStep.SEND
} else if (heartbeatUrgent) {
return true
return HeartbeatStep.HOLD
} else if (heartbeatPending) {
// Don't send other message types while a heartbeat is pending because that
// can lead to inter-lens sync issues
return true
return HeartbeatStep.HOLD
}
return false
return HeartbeatStep.NONE
}
internal fun GlassesSessionCore.createHeartbeatMessage(): OutboundMessage {
@@ -1099,7 +1122,7 @@ internal fun GlassesSessionCore.describeWriteBlockerLocked(now: Long, windowHasR
if (hasPendingOrInflightKindLocked("heartbeat")) {
return "heartbeat in flight"
}
// handleHeartbeat is a barrier: while one is due it holds back every
// heartbeatStepLocked is a barrier: while one is due it holds back every
// other write, so a frame queued at the wrong moment waits a heartbeat
// round trip. Reported explicitly because it is otherwise invisible --
// heartbeats belong to no frame.
@@ -13,8 +13,20 @@ import kotlin.contracts.contract
* must call from whatever thread the platform stack delivers on.
*/
interface SessionLink {
/** Connect, waiting up to [timeoutMs]; waits on an attempt already started by [beginConnect]. */
fun connect(address: String, timeoutMs: Int): Boolean
/**
* Start connecting without waiting, so the attempt can land while the caller does other
* work. [background] asks for a low-duty-cycle attempt that stays pending until the
* device shows up. Returns true while a connection exists or is pending, false when the
* link can't do this (callers then fall back to a blocking [connect]).
*/
fun beginConnect(address: String, background: Boolean): Boolean = false
/** Whether a connection to [address] is up (not merely pending). */
fun isConnected(address: String): Boolean = false
/** Best effort; no completion point (see connectArm). */
fun requestHighPriority(address: String)