forked from Blendman974/faceclaw
Less-sensitive disconnect detection to avoid reconnect loops under BLE contention
This commit is contained in:
1 parent
7d8f03ffe5
commit
7684403491
6 files changed
+128
-6
No files matched your search
+15
-3
@@ -5,11 +5,23 @@ import kotlin.jvm.JvmStatic
|
||||
/** Ordered completion and go-back-N recovery for the custom-message window. */
|
||||
class CfwMessageWindow private constructor() {
|
||||
companion object {
|
||||
// Flappy capture: p99 ACK 63ms, max 77ms. Allow full-frame processing
|
||||
// while recovering a lost reply well before the stock timeout.
|
||||
const val ACK_TIMEOUT_MS = 500
|
||||
// An unresolved message is presumed lost only once both kinds of progress on
|
||||
// its arm have stopped: no write completion for DATA_STALL_MS, and no CFW ack
|
||||
// for ACK_STALL_MS since it was sent. Android withholds write completions
|
||||
// while its outgoing queue is congested, so completions mean data is still
|
||||
// draining; acks mean the firmware is still working through the window. A
|
||||
// single 500 ms deadline from send replayed large uploads whose acks were
|
||||
// just slow (an 11 KB upload acked by one lens at 536 ms), and under
|
||||
// bandwidth pressure (LE Audio streaming) it tore the session down.
|
||||
const val DATA_STALL_MS = 500
|
||||
const val ACK_STALL_MS = 3_000
|
||||
const val MAX_RETRIES = 3
|
||||
|
||||
/** When a message sent at [sentAtMs] counts as lost, given its arm's latest write completion and CFW ack. */
|
||||
@JvmStatic
|
||||
fun ackDeadline(sentAtMs: Long, lastArmWriteAtMs: Long, lastArmAckAtMs: Long): Long =
|
||||
maxOf(lastArmWriteAtMs + DATA_STALL_MS, maxOf(sentAtMs, lastArmAckAtMs) + ACK_STALL_MS)
|
||||
|
||||
/** ID reuse must not invalidate an earlier command still eligible for replay. */
|
||||
@JvmStatic
|
||||
fun canSend(messages: Iterable<OutboundMessage>, candidate: OutboundMessage): Boolean =
|
||||
|
||||
+1
-1
@@ -97,7 +97,7 @@ class MessageBuilder {
|
||||
0,
|
||||
streamId,
|
||||
payload,
|
||||
CfwMessageWindow.ACK_TIMEOUT_MS,
|
||||
CfwMessageWindow.ACK_STALL_MS,
|
||||
tileIndex,
|
||||
leftArm,
|
||||
)
|
||||
|
||||
+17
-1
@@ -314,6 +314,11 @@ class GlassesSessionCore(
|
||||
|
||||
internal val pendingMessages = ArrayDeque<OutboundMessage>()
|
||||
internal val cfwTransports = arrayOf(CfwTransport(platform), CfwTransport(platform))
|
||||
// Per arm (0 = left, 1 = right, like cfwTransports): when a write to it last
|
||||
// completed and when a CFW ack last arrived from it. The CFW window's liveness
|
||||
// signals (see CfwMessageWindow.ackDeadline).
|
||||
internal val lastArmWriteAtMs = LongArray(2)
|
||||
internal val lastArmCfwAckAtMs = LongArray(2)
|
||||
internal val inFlightMessages = ArrayDeque<OutboundMessage>()
|
||||
internal var prewrittenMessage: OutboundMessage? = null
|
||||
internal var prewrittenFrames: List<ByteArray> = emptyList()
|
||||
@@ -350,8 +355,10 @@ class GlassesSessionCore(
|
||||
val active = monitor.withLock { running }
|
||||
if (!active) return false
|
||||
return try {
|
||||
link.writeFrames(address, BleProtocol.WRITE_CHAR_UUID, listOf(packet), ConnectionOptions.WRITE_MODE,
|
||||
val result = 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) {
|
||||
logLine("raw packet write failed: " + safeMessage(t))
|
||||
false
|
||||
@@ -1348,6 +1355,8 @@ class GlassesSessionCore(
|
||||
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
|
||||
@@ -2018,6 +2027,13 @@ class GlassesSessionCore(
|
||||
return id
|
||||
}
|
||||
|
||||
/** 0 for the left arm, 1 for the right (the cfwTransports order), -1 for anything else. */
|
||||
internal fun armIndex(address: String): Int = when {
|
||||
address.equals(leftAddress, ignoreCase = true) -> 0
|
||||
address.equals(rightAddress, ignoreCase = true) -> 1
|
||||
else -> -1
|
||||
}
|
||||
|
||||
internal fun hasRingAddress(): Boolean {
|
||||
return ringAddress.trim().isNotEmpty()
|
||||
}
|
||||
|
||||
+30
-1
@@ -79,6 +79,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
)
|
||||
}
|
||||
if (!inFlightMessages.isEmpty()) {
|
||||
refreshCfwAckDeadlinesLocked()
|
||||
val oldest = inFlightMessages.firstOrNull()
|
||||
val replay = CfwMessageWindow.replayWindow(inFlightMessages, now)
|
||||
if (!replay.isEmpty()) {
|
||||
@@ -415,6 +416,7 @@ internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean
|
||||
|
||||
monitor.withLock {
|
||||
val sentAtMs = now()
|
||||
if (result) noteWriteCompletedLocked(writeAddress, sentAtMs)
|
||||
message.sentAtMs = sentAtMs
|
||||
message.ackDeadlineAtMs = sentAtMs + message.ackTimeoutMs
|
||||
logImageUpdateSendLandmarkLocked(message)
|
||||
@@ -463,6 +465,7 @@ internal fun GlassesSessionCore.prewriteMessage(message: OutboundMessage): Boole
|
||||
if (!result) {
|
||||
return false
|
||||
}
|
||||
monitor.withLock { noteWriteCompletedLocked(writeAddress, now()) }
|
||||
|
||||
prewrittenMessage = message
|
||||
prewrittenFrames = frames.toList()
|
||||
@@ -486,13 +489,39 @@ internal fun GlassesSessionCore.spoilPrewrittenMessage(reason: String): Boolean
|
||||
|
||||
val writeAddress = if (message.isLeftArmMessage) leftAddress else rightAddress
|
||||
logLine("spoiling prewritten " + message.label + ": " + reason)
|
||||
return link.writeFrames(
|
||||
val result = link.writeFrames(
|
||||
writeAddress,
|
||||
BleProtocol.WRITE_CHAR_UUID,
|
||||
listOf(finalFrame),
|
||||
ConnectionOptions.WRITE_MODE,
|
||||
ConnectionOptions.WRITE_TIMEOUT_MS
|
||||
)
|
||||
if (result) monitor.withLock { noteWriteCompletedLocked(writeAddress, now()) }
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Record that writes to [address] just completed: every frame's onCharacteristicWrite
|
||||
* fired. The drive loop only checks ack deadlines between writes, so noting it when
|
||||
* writeFrames returns is as good as noting each callback.
|
||||
*/
|
||||
internal fun GlassesSessionCore.noteWriteCompletedLocked(address: String, atMs: Long) {
|
||||
val arm = armIndex(address)
|
||||
if (arm >= 0) lastArmWriteAtMs[arm] = atMs
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-derive each written, unresolved CFW message's ack deadline from its arm's latest
|
||||
* write completion and CFW ack (CfwMessageWindow.ackDeadline), so either kind of
|
||||
* progress keeps the window waiting. Deadlines only move later.
|
||||
*/
|
||||
internal fun GlassesSessionCore.refreshCfwAckDeadlinesLocked() {
|
||||
for (message in inFlightMessages) {
|
||||
if (message.sid != CfwTransport.SID || message.ackDeadlineAtMs <= 0) continue
|
||||
val arm = if (message.isLeftArmMessage) 0 else 1
|
||||
message.ackDeadlineAtMs = CfwMessageWindow.ackDeadline(
|
||||
message.sentAtMs, lastArmWriteAtMs[arm], lastArmCfwAckAtMs[arm])
|
||||
}
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.removePreparedMessageLocked(message: OutboundMessage?): Boolean {
|
||||
|
||||
Reference in new issue
Block a user