Merge pull request #35 from jmomakesstuff/fix-flash-upgrade-unreachable

Refuse the flash prompt with a reason when the glasses are in silent mode
This commit is contained in:
James Babcock authored and GitHub committed 2026-09-25 22:37:26 -07:00
commit 1dd86cd0cf
2 files changed
+89 -9

No files matched your search

@@ -13,7 +13,9 @@ import kotlin.concurrent.Volatile
* pairing prompt, and doing both here means both prompts appear at the start rather than one
* popping up half way through flashing the second lens. While connected, each arm's battery
* level is read (they are independent batteries) so the caller can refuse to flash on a low
* charge.
* charge. Before the prompt is shown, the right arm's settings are read once more so a lens
* that reports silent mode (which blanks the display and ignores input) is refused with the
* way out instead of a prompt nobody can see.
*
* With `skipPrompt` the on-glasses confirmation is not shown: the flow is just connect + auth +
* battery read + result(approved). Used to re-check the battery after the user has already
@@ -38,6 +40,15 @@ class FlashPromptFlow(
const val LIST_CONTAINER_ID = 2
// Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid.
val ITEMS = arrayOf("No, cancel", "Yes, flash")
/**
* Shown instead of a bare "prompt page not acked" error. Silent mode is entered and left
* by the same gesture on the glasses and nothing here can clear it, so the instruction
* has to travel with the error.
*/
const val SILENT_MODE_MESSAGE =
"Your glasses are in silent mode, so they cannot show the confirmation prompt. " +
"Long-press both touchpads on the glasses to leave silent mode, then try again."
}
private val rightAddress = rightAddress.trim()
@@ -121,6 +132,11 @@ class FlashPromptFlow(
session.sendPrelude(rightAddress, ": $rightAddress")
if (!skipPrompt) {
// Ask before writing a page that cannot be answered: silent mode blanks the
// display and stops the firmware dispatching input while BLE stays up, so the
// create-prompt write is still acked and the user is left waiting on a prompt
// that never appears.
requireNotSilent(rightAddress)
showPrompt(rightAddress)
startHeartbeat()
listener.onState("prompting", "")
@@ -238,23 +254,49 @@ class FlashPromptFlow(
listener.onBattery(right, left)
}
private fun readBattery(address: String, arm: String): Int {
private fun readBattery(address: String, arm: String): Int = readSettingsSnapshot(address, arm)?.battery ?: -1
/**
* The sid-0x09 settings read behind both the battery gate and the silent-mode check; one
* ack carries both fields. Two attempts, since on 2.2.9 a request sent right after the
* prelude can be dropped. Null when the arm does not answer or the ack has no battery field.
*/
private fun readSettingsSnapshot(address: String, arm: String): BleProtocol.BatterySnapshot? {
var attempt = 0
while (attempt < 2 && !cancelled) {
val ack = session.readSettings(address, timings.batteryAckTimeoutMs)
if (ack == null) {
emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm")
emitLog("settings read attempt " + (attempt + 1) + " unacked: " + arm + " arm")
attempt++
continue
}
val snapshot = BleProtocol.parseSettingsBattery(ack)
if (snapshot != null) {
return snapshot.battery
return snapshot
}
emitLog("battery read ack had no battery field: $arm arm")
emitLog("settings read ack had no battery field: $arm arm")
attempt++
}
return -1
return null
}
/**
* Refuse the prompt only when the arm POSITIVELY reports silent mode. An arm that does not
* answer, and firmware whose ack omits the field, both fall through to the prompt exactly as
* before, so a missing answer can never block a flash.
*/
private fun requireNotSilent(address: String) {
val snapshot = readSettingsSnapshot(address, "right")
val state = when {
snapshot == null -> "unknown (arm did not answer)"
snapshot.silentMode < 0 -> "unknown (ack omits the field)"
snapshot.silentMode > 0 -> "on"
else -> "off"
}
emitLog("silent mode before prompt: $state")
if (snapshot != null && snapshot.silentMode > 0) {
throw IllegalStateException(SILENT_MODE_MESSAGE)
}
}
private fun startHeartbeat() {
@@ -60,11 +60,12 @@ private fun field(number: Int, text: String): ByteArray = field(number, text.enc
private fun authSuccess(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, ByteArray(0))
private fun authPending(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, BleProtocol.encodeVarintField(1, 1))
private fun ack(magic: Int) = BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic)
private fun settingsAck(magic: Int, battery: Int = -1, left: String = "", right: String = "", extension: String = ""): ByteArray {
private fun settingsAck(magic: Int, battery: Int = -1, left: String = "", right: String = "", extension: String = "", silentMode: Int = -1): ByteArray {
var request = ByteArray(0)
if (left.isNotEmpty()) request += field(5, left)
if (right.isNotEmpty()) request += field(6, right)
if (battery >= 0) request += BleProtocol.encodeVarintField(12, battery)
if (silentMode >= 0) request += BleProtocol.encodeVarintField(14, silentMode)
var pb = BleProtocol.encodeVarintField(1, 2) + BleProtocol.encodeVarintField(2, magic) + field(4, request)
if (extension.isNotEmpty()) pb += field(100, extension)
return pb
@@ -205,11 +206,11 @@ class StockFlowsTest {
override fun onResult(approved: Boolean) { result = approved }
}
private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map<String, Int>): (FakeStockLink.Written) -> Unit = { w ->
private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map<String, Int>, silentMode: Int = -1): (FakeStockLink.Written) -> Unit = { w ->
when (w.sid) {
BleProtocol.SID_SECURITY_AUTH -> link.notify(w.address, authSuccess(w.magic), w.sid)
BleProtocol.PRELUDE_ACK_SID -> link.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid)
BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, batteries[w.address] ?: -1), w.sid)
BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, batteries[w.address] ?: -1, silentMode = silentMode), w.sid)
BleProtocol.SID_EVENHUB -> {
val cmd = BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(w.pb), 1, -1)
link.notify(w.address, ack(w.magic), w.sid)
@@ -275,6 +276,43 @@ class StockFlowsTest {
assertEquals("disconnected:Lost connection to the glasses.", events.states.last())
}
@Test
fun promptRefusesOnlyWhenTheLensReportsSilentMode() {
// Silent mode on: refused with the way out, before any page is written.
val silentOn = FakeStockLink()
silentOn.responder = promptResponder(silentOn, 1, mapOf(R to 85), silentMode = 1)
var events = PromptEvents()
FlashPromptFlow(silentOn, R, "", "w", false, events, fast, testPlatform()).run()
assertEquals(null, events.result)
assertEquals("error:${FlashPromptFlow.SILENT_MODE_MESSAGE}", events.states.last())
assertTrue(events.logs.any { it == "silent mode before prompt: on" })
assertFalse(silentOn.writes.any { it.sid == BleProtocol.SID_EVENHUB })
assertTrue(silentOn.closed)
// Silent mode off: the prompt is shown and answered as usual.
val silentOff = FakeStockLink()
silentOff.responder = promptResponder(silentOff, 1, mapOf(R to 85), silentMode = 0)
events = PromptEvents()
FlashPromptFlow(silentOff, R, "", "w", false, events, fast, testPlatform()).run()
assertEquals(true, events.result, events.logs.joinToString("\n"))
assertTrue(events.logs.any { it == "silent mode before prompt: off" })
// Firmware whose ack omits the field: unknown is not a refusal.
val omitted = FakeStockLink()
omitted.responder = promptResponder(omitted, 1, mapOf(R to 85))
events = PromptEvents()
FlashPromptFlow(omitted, R, "", "w", false, events, fast, testPlatform()).run()
assertEquals(true, events.result, events.logs.joinToString("\n"))
assertTrue(events.logs.any { it == "silent mode before prompt: unknown (ack omits the field)" })
// skipPrompt re-checks the battery only; silent mode does not block it.
val skipped = FakeStockLink()
skipped.responder = promptResponder(skipped, null, mapOf(R to 85), silentMode = 1)
events = PromptEvents()
FlashPromptFlow(skipped, R, "", "w", true, events, fast, testPlatform()).run()
assertEquals(true, events.result)
}
// ---- OTA ---------------------------------------------------------------
private fun putU32(buf: ByteArray, offset: Int, value: Long) { for (i in 0 until 4) buf[offset + i] = ((value ushr (8 * i)) and 0xff).toByte() }