From 5e729d1236393bc2432c2896284d48e669253081 Mon Sep 17 00:00:00 2001 From: Om Alve Date: Mon, 2 Nov 2026 12:22:48 +0530 Subject: [PATCH] Case battery: read the glasses battery soon after they dock Only the settings reply says the glasses are charging. Outside charging mode it is polled every five minutes, and not at all while the display is suspended, which it is once the glasses come off the face. So after docking, the phone kept showing the glasses as "Not charging", and charging mode (which pauses display traffic) didn't start until the display woke or the link reconnected. The case push that reports the glasses going from out of the case to docked arrives within a second. It now schedules a battery read about three seconds later. The drive loop queues it as a pending message, which still goes out while the display is suspended. --- .../app/g2protocol/ConnectionOptions.kt | 3 ++ .../session/GlassesSessionControls.kt | 9 +++++ .../g2protocol/session/GlassesSessionCore.kt | 4 ++ .../g2protocol/session/GlassesSessionSend.kt | 10 +++++ .../faceclaw/app/GlassesSessionCoreTest.kt | 38 +++++++++++++++++++ 5 files changed, 64 insertions(+) diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt index 069953b9..490c2aca 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt @@ -42,6 +42,9 @@ class ConnectionOptions { // Poll cadence while in charging mode; also bounds how quickly we notice // the glasses coming back out of the case. const val CHARGING_BATTERY_POLL_MS = 30_000 + // After the case reports the glasses docking, read the battery this + // soon (rather than at the next refresh) so charging mode follows. + const val DOCKED_BATTERY_CHECK_DELAY_MS = 3_000 const val IMAGE_FRAGMENT_SIZE = 3800 const val IMAGE_RETRY_DELAY_MS = 2_000 const val MAX_CONSECUTIVE_ACK_TIMEOUTS = 8 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt index 1731f7de..2e7ad976 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionControls.kt @@ -747,10 +747,19 @@ internal fun GlassesSessionCore.updateCaseInfoLocked(info: BleProtocol.CaseInfoS if (level == caseBattery && charging == caseCharging && dockedState == caseDocked) { return } + val wasOutOfCase = caseDocked == 0 caseBattery = level caseCharging = charging caseDocked = dockedState logLine("charging case: $info") + if (docked && wasOutOfCase && !chargingMode) { + // The glasses just went into the case. Only the battery reply says + // they are charging, and the regular poll is minutes away (and skipped + // altogether while the display is suspended, as it is once they come + // off the face), so read it shortly: charging mode then follows. + dockedBatteryCheckAtMs = now() + ConnectionOptions.DOCKED_BATTERY_CHECK_DELAY_MS + logLine("glasses docked; battery check scheduled") + } val current = listener ?: return host.postToMain { try { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt index 3b403efb..71b4e074 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionCore.kt @@ -396,6 +396,9 @@ class GlassesSessionCore( // The session-start case read is still to be sent. It waits for the layout // (see the drive loop), so a firmware that ignores it can't delay the first frame. internal var caseInfoReadPending = false + // When to read the battery after the case reported the glasses docking + // (0 = not scheduled). See updateCaseInfoLocked and the drive loop. + internal var dockedBatteryCheckAtMs = 0L // Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked. internal var silentMode = -1 internal var wearState = -1 @@ -2235,6 +2238,7 @@ class GlassesSessionCore( lastConnectionOrInputAtMs = lastAckAtMs lastSessionReadyAtMs = lastAckAtMs lastBatteryRefreshAtMs = 0 + dockedBatteryCheckAtMs = 0 imageRetryAfterMs = 0 lastHeartbeatSentAtMs = 0 lastHeartbeatAckedAtMs = 0 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt index 86723582..fbfac3d2 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/session/GlassesSessionSend.kt @@ -57,6 +57,16 @@ internal fun GlassesSessionCore.driveSession(): Long { return 0 } drainCfwAcknowledgementsLocked() + if (dockedBatteryCheckAtMs != 0L && now >= dockedBatteryCheckAtMs) { + // Queued rather than polled: pending messages still go out while + // the display is suspended, which the regular poll waits out. + dockedBatteryCheckAtMs = 0L + if (sessionReady && !chargingMode && !hasPendingOrInflightKindLocked("battery")) { + lastBatteryRefreshAtMs = now + pendingMessages.addLast(createBatteryQueryMessageLocked()) + logLine("queue battery query: glasses docked") + } + } if (cfwCleanupDelivered) { /* Successful mode 11 must be the last Faceclaw write. Drop * anything a late external producer attempted to enqueue diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt index fc236fa1..b457440d 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt @@ -796,6 +796,44 @@ class GlassesSessionCoreTest { } } + @Test + fun dockingReadsTheBatteryEvenWithTheDisplaySuspended() { + val s = Session() + s.link.caseInfo = ByteArray(0) + try { + s.startAndAwaitLayout() + assertTrue(waitUntil(5_000) { s.listener.cases == listOf("-1/0/out") }, s.listener.cases.toString()) + // Off the face the display is suspended, which stops the regular battery poll. + s.core.monitor.withLock { s.core.shutdownRequested = true } + fun batteryQueries() = s.link.writes.count { it.kind == "battery" } + val before = batteryQueries() + val wait = ConnectionOptions.DOCKED_BATTERY_CHECK_DELAY_MS + 1_000L + s.platform.offsetMs += wait + s.core.interruptibleSleep.interrupt() + sleepMs(300) + assertEquals(before, batteryQueries()) + // Docking: the case push schedules a battery read a few seconds later. + s.core.onNotification(RIGHT, BleProtocol.NOTIFY_CHAR_UUID, BleProtocol.framePb( + casePackage(200, caseInfoBody(98, inCase = 1)), BleProtocol.SID_GLASSES_CASE, BleProtocol.FLAG_NOTIFY, 99)[0]) + assertEquals("98/0/docked", s.listener.cases.last()) + s.core.interruptibleSleep.interrupt() + sleepMs(300) + assertEquals(before, batteryQueries(), "not before the delay") + s.platform.offsetMs += wait + s.core.interruptibleSleep.interrupt() + assertTrue(waitUntil(5_000) { batteryQueries() == before + 1 }, s.host.logs().takeLast(10).toString()) + // It reads once; the docking push is not repeated for later level changes. + s.core.onNotification(RIGHT, BleProtocol.NOTIFY_CHAR_UUID, BleProtocol.framePb( + casePackage(201, caseInfoBody(97, inCase = 1)), BleProtocol.SID_GLASSES_CASE, BleProtocol.FLAG_NOTIFY, 100)[0]) + s.platform.offsetMs += wait + s.core.interruptibleSleep.interrupt() + sleepMs(300) + assertEquals(before + 1, batteryQueries()) + } finally { + s.core.close() + } + } + @Test fun glassesChargingAtConnectReadTheCaseOnce() { val s = Session()