From acfcf797874bb5264606a57990572920f3d74b38 Mon Sep 17 00:00:00 2001 From: Om Alve Date: Sun, 1 Nov 2026 20:32:21 +0530 Subject: [PATCH 1/8] Read the charging case battery (sid 0x81 GlassesCaseInfo) The glasses relay the charging case's state on UX_GLASSES_CASE_APP_ID (sid 0x81): GlassesCaseDataPackage{commandId=CASE_INFO, magicRandom, caseInfo{soc, chargeStatus, lidStatus, inCaseStatus, errorCode}}, the schema in g2-kit-unofficial's glasses_case.proto. The Even app reads it with an empty caseInfo (ProtoBaseSettings.getGlassesCaseInfo). Observed on G2 2.3.0.24 / Faceclaw/38: while docked the right temple pushes a GlassesCaseInfo (flag 0x01, its own magic) on every change -- docking, lid, USB, each 1% -- and answers a read with full data; out of the case a read comes back with an empty caseInfo. Docking pushes {inCaseStatus=1} before the level, and proto3 drops zero fields, so a missing soc means no level rather than 0%. So read it once at session start (docked glasses may be fully charged and never enter charging mode) and again on entering charging mode, which clears the queue; pushes carry every later change. Every 0x81 frame is taken as state in onNotification (pushes skip the ack path by flag). New readings reach TypeScript through FaceclawBleCommunicatorListener.onCaseState (level, case-charging), on Android and iOS. Captured frames are tests. --- app/native/faceclaw-communicator.ios.ts | 17 ++++ app/native/faceclaw-communicator.ts | 13 +++ .../FaceclawBleCommunicatorListener.kt | 3 + .../faceclaw/app/g2protocol/BleProtocol.kt | 67 +++++++++++++++ .../faceclaw/app/g2protocol/MessageBuilder.kt | 15 ++++ .../session/GlassesSessionControls.kt | 18 ++++ .../g2protocol/session/GlassesSessionCore.kt | 12 +++ .../g2protocol/session/GlassesSessionSend.kt | 3 + .../faceclaw/app/GlassesSessionCoreTest.kt | 82 +++++++++++++++++++ .../com/faceclaw/app/IosSessionFacadeTest.kt | 4 + 10 files changed, 234 insertions(+) diff --git a/app/native/faceclaw-communicator.ios.ts b/app/native/faceclaw-communicator.ios.ts index 6589e6b4..638109af 100644 --- a/app/native/faceclaw-communicator.ios.ts +++ b/app/native/faceclaw-communicator.ios.ts @@ -42,6 +42,9 @@ export type HeadsetBatteryState = { ringChargingStatus?: number; }; +/** See the Android communicator's CaseBatteryState. */ +export type CaseBatteryState = { battery: number; charging: boolean }; + export type FrameMetrics = { paintMs: number; transmitMs: number; @@ -120,6 +123,9 @@ class SessionListener extends NSObject { onBatteryStateHeadsetBatteryHeadsetChargingRingBatteryRingCharging(headsetBattery: number, headsetCharging: number, ringBattery: number, ringCharging: number): void { this.bridge.handleBatteryState(Number(headsetBattery), Number(headsetCharging), Number(ringBattery), Number(ringCharging)); } + onCaseStateCaseBatteryCaseCharging(caseBattery: number, caseCharging: number): void { + this.bridge.handleCaseState(Number(caseBattery), Number(caseCharging) === 1); + } onSilentModeSilent(silent: boolean): void { this.bridge.handleSilentMode(Boolean(silent)); } onWearStateWearing(wearing: boolean): void { this.bridge.handleWearState(Boolean(wearing)); } onPhoneLockStateLocked(locked: boolean): void { this.bridge.handlePhoneLockState(Boolean(locked)); } @@ -215,6 +221,7 @@ export class FaceclawCommunicatorBridge { private readonly stateListeners = new Set<(state: CommunicatorState) => void>(); private readonly ringListeners = new Set<(event: RawInputEvent) => void>(); private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); + private readonly caseListeners = new Set<(state: CaseBatteryState) => void>(); private readonly silentModeListeners = new Set<(silent: boolean) => void>(); private readonly wearStateListeners = new Set<(wearing: boolean) => void>(); private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>(); @@ -280,6 +287,11 @@ export class FaceclawCommunicatorBridge { this.emitAsync(this.batteryListeners, { battery, chargingStatus, ringBattery, ringChargingStatus }); } + /** @internal */ + handleCaseState(battery: number, charging: boolean): void { + this.emitAsync(this.caseListeners, { battery, charging }); + } + /** @internal */ handleSilentMode(silent: boolean): void { this.emitAsync(this.silentModeListeners, silent); } @@ -371,6 +383,11 @@ export class FaceclawCommunicatorBridge { return () => this.batteryListeners.delete(listener); } + onCaseState(listener: (state: CaseBatteryState) => void): () => void { + this.caseListeners.add(listener); + return () => this.caseListeners.delete(listener); + } + onSilentMode(listener: (silent: boolean) => void): () => void { this.silentModeListeners.add(listener); return () => this.silentModeListeners.delete(listener); diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 481ae62b..dc94c50d 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -29,6 +29,9 @@ export type HeadsetBatteryState = { ringChargingStatus?: number; }; +/** The charging case's level (0..100) and whether it is itself charging, as the glasses relay it. */ +export type CaseBatteryState = { battery: number; charging: boolean }; + export type FrameMetrics = { paintMs: number; transmitMs: number; @@ -151,6 +154,7 @@ export class FaceclawCommunicatorBridge { private readonly stateListeners = new Set<(state: CommunicatorState) => void>(); private readonly ringListeners = new Set<(event: RawInputEvent) => void>(); private readonly batteryListeners = new Set<(state: HeadsetBatteryState) => void>(); + private readonly caseListeners = new Set<(state: CaseBatteryState) => void>(); private readonly silentModeListeners = new Set<(silent: boolean) => void>(); private readonly wearStateListeners = new Set<(wearing: boolean) => void>(); private readonly phoneLockStateListeners = new Set<(locked: boolean) => void>(); @@ -212,6 +216,9 @@ export class FaceclawCommunicatorBridge { }; this.emitAsync(this.batteryListeners, state); }, + onCaseState: (caseBattery: number, caseCharging: number) => { + this.emitAsync(this.caseListeners, { battery: Number(caseBattery), charging: Number(caseCharging) === 1 }); + }, onSilentMode: (silent: boolean) => { this.emitAsync(this.silentModeListeners, Boolean(silent)); }, @@ -332,6 +339,12 @@ export class FaceclawCommunicatorBridge { return () => this.batteryListeners.delete(listener); } + /** Charging-case readings: read on docking, then pushed by the glasses on every change. */ + onCaseState(listener: (state: CaseBatteryState) => void): () => void { + this.caseListeners.add(listener); + return () => this.caseListeners.delete(listener); + } + /** * Silent mode, toggled on the glasses by long-pressing both touchpads. While * it is on the firmware ignores all input and blanks the display. diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt index 106b41ee..54a9cd71 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt @@ -23,6 +23,9 @@ interface FaceclawBleCommunicatorListener { ringCharging: Int, ): Unit + /** A new charging-case level (0..100) or charge state; caseCharging 1 = the case is on USB. */ + fun onCaseState(caseBattery: Int, caseCharging: Int): Unit + fun onSilentMode(silent: Boolean): Unit fun onWearState(wearing: Boolean): Unit diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt index 1a6fa4d9..89928144 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt @@ -52,6 +52,12 @@ class BleProtocol { const val SID_ONBOARDING: Int = 0x10 + /** Charging case (UX_GLASSES_CASE_APP_ID): GlassesCaseDataPackage, see [CaseInfoSnapshot]. */ + const val SID_GLASSES_CASE: Int = 0x81 + + /** eGlassesCaseCommandId.CASE_INFO. */ + const val GLASSES_CASE_CMD_CASE_INFO: Int = 1 + /** G2SettingPackage.commandId for device-initiated pushes (vs. 2 = app read). */ const val SETTINGS_CMD_DEVICE_SEND_TO_APP: Int = 3 @@ -762,6 +768,44 @@ class BleProtocol { return BatterySnapshot(battery, charging, silentMode) } + /** GlassesCaseDataPackage{commandId=CASE_INFO, magicRandom, caseInfo={}}, as the Even app sends it. */ + @JvmStatic + fun buildCaseInfoQuery(magic: Int): ByteArray { + return concat( + CollectionUtils.listOf( + encodeVarintField(1, GLASSES_CASE_CMD_CASE_INFO), + encodeVarintField(2, magic), + encodeMessageField(3, ByteArray(0)), + ) + ) + } + + /** + * The GlassesCaseInfo (field 3) of a sid-0x81 CASE_INFO reply or push, or null when there + * is none or it is empty. + */ + @JvmStatic + fun parseCaseInfo(pb: ByteArray?): CaseInfoSnapshot? { + if (pb == null) { + return null + } + val root: ByteArray = stripTrailingCrc(pb) + if (readVarintFieldValue(root, 1, -1) != GLASSES_CASE_CMD_CASE_INFO) { + return null + } + val info: ByteArray = readFieldBytes(root, 3) ?: return null + if (info.isEmpty()) { + return null + } + return CaseInfoSnapshot( + readVarintFieldValue(info, 1, -1), + readVarintFieldValue(info, 2, 0), + readVarintFieldValue(info, 3, 0), + readVarintFieldValue(info, 4, 0), + readVarintFieldValue(info, 5, 0), + ) + } + /** * Silent mode from an unsolicited settings push. The wearer toggles silent mode by * long-pressing both touchpads at once; the firmware then sends @@ -1453,6 +1497,29 @@ class BleProtocol { } } + /** + * GlassesCaseInfo {soc=1, chargeStatus=2, lidStatus=3, inCaseStatus=4, errorCode=5}. As seen + * on G2 2.3.0.24 / Faceclaw/38: docked, the right temple pushes one (flag 0x01, its own + * magic) on every change -- docking, lid, USB, each 1% -- and answers a CASE_INFO read with + * full data; out of the case it answers with an empty caseInfo. Docking pushes + * {inCaseStatus=1} first and the level about 0.7 s later; proto3 drops zero fields, so a + * missing soc reads as -1 ("no level"), never 0%. + */ + data class CaseInfoSnapshot( + /** State of charge, 1..100; -1 when the message left it out. */ + @JvmField val soc: Int, + /** 1 = the case itself is charging (on USB). */ + @JvmField val chargeStatus: Int, + /** 1 = lid closed, 0 = open. */ + @JvmField val lidStatus: Int, + /** 1 = glasses docked. */ + @JvmField val inCaseStatus: Int, + /** g2.common.eErrorCode; 0 = SUCCESS. */ + @JvmField val errorCode: Int, + ) { + fun hasLevel(): Boolean = errorCode == 0 && soc in 0..100 + } + class FirmwareInfo { companion object { /** Prefix of the firmware-extension string on Faceclaw's custom firmware. */ diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt index 64d1f151..c82180d2 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt @@ -246,6 +246,21 @@ class MessageBuilder { ) } + fun caseInfoQuery(): OutboundMessage { + val magic: Int = magicPool.allocate() + return OutboundMessage( + "case-info", + "case info", + BleProtocol.SID_GLASSES_CASE, + BleProtocol.FLAG_REQUEST, + magic, + BleProtocol.buildCaseInfoQuery(magic), + ACK_TIMEOUT_MS, + -1, + false, + ) + } + fun faceclawWakeControl(operation: Int, nonce: Int, leftArm: Boolean): OutboundMessage { return OutboundMessage( "wake-lease-control", 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 2f853d19..23674fa1 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 @@ -721,6 +721,24 @@ internal fun GlassesSessionCore.emitBatteryState(headsetBattery: Int, headsetCha } } +/** Report a charging-case reading when it carries a level that differs from the last one. */ +internal fun GlassesSessionCore.updateCaseInfoLocked(info: BleProtocol.CaseInfoSnapshot) { + if (!info.hasLevel() || (info.soc == caseBattery && info.chargeStatus == caseCharging)) { + return + } + caseBattery = info.soc + caseCharging = info.chargeStatus + logLine("charging case: $info") + val current = listener ?: return + host.postToMain { + try { + current.onCaseState(info.soc, info.chargeStatus) + } catch (t: Throwable) { + logWarn("listener onCaseState failed", t) + } + } +} + internal fun GlassesSessionCore.emitFirmwareInfo(info: BleProtocol.FirmwareInfo) { val current = listener ?: return host.postToMain { 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 0de6da9d..42756b7d 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 @@ -230,6 +230,9 @@ class GlassesSessionCore( internal var headsetCharging = -1 internal var ringBattery = -1 internal var ringCharging = -1 + // Last charging-case level and charge state reported to the listener. + internal var caseBattery = -1 + internal var caseCharging = -1 // Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked. internal var silentMode = -1 internal var wearState = -1 @@ -1547,6 +1550,11 @@ class GlassesSessionCore( return } } + if (frame.ok && frame.sid == BleProtocol.SID_GLASSES_CASE) { + // Read replies and the glasses' own pushes (flag 0x01, which + // skips the ack path below) carry the same reading. + BleProtocol.parseCaseInfo(frame.pb)?.let { updateCaseInfoLocked(it) } + } if (!faceclawWakeNotification && decodedWearState < 0 && frame.ok @@ -1834,6 +1842,10 @@ class GlassesSessionCore( lastBatteryRefreshAtMs = now() pendingMessages.addLast(createBatteryQueryMessageLocked()) logLine("queue settings query for firmware info") + // The case reports only while the glasses are docked, possibly + // fully charged and so not in charging mode; out of the case + // this read comes back empty. Later changes are pushed. + pendingMessages.addLast(messageBuilder.caseInfoQuery()) } tryConnectRing("initial") } catch (t: Throwable) { 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 58bd4325..c3afbc12 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 @@ -1099,6 +1099,9 @@ internal fun GlassesSessionCore.updateChargingModeLocked(charging: Boolean, batt if (charging) { chargingMode = true clearAllMessagesLocked("glasses charging") + // Clearing may have dropped the session-start case read; docked is + // when the case reports, so read it again. + pendingMessages.addLast(messageBuilder.caseInfoQuery()) fixedLayoutCreated = false startupProbePending = false displayedFingerprint = "" 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 a615af76..46ff968d 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/GlassesSessionCoreTest.kt @@ -70,6 +70,8 @@ private class FakeLink(private val platform: ProtocolPlatform) : SessionLink { * arms ("" is stock firmware, which omits the field), or null for an ack without firmware info. */ @Volatile var firmwareExtension: String? = null + /** The GlassesCaseInfo body case-info queries are answered with; null answers without one. */ + @Volatile var caseInfo: ByteArray? = null @Volatile var connects = 0 var seq = 0 @@ -129,6 +131,8 @@ private class FakeLink(private val platform: ProtocolPlatform) : SessionLink { BleProtocol.encodeBytesField(5, STOCK_VERSION.encodeToByteArray()) + BleProtocol.encodeBytesField(6, STOCK_VERSION.encodeToByteArray())) + (if (extension.isEmpty()) ByteArray(0) else BleProtocol.encodeBytesField(100, extension.encodeToByteArray())) + else if (kind == "case-info") + casePackage(magic, caseInfo) else BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic) return BleProtocol.framePb(body, sid, 0x00, seq++)[0] @@ -152,6 +156,19 @@ private class FakeLink(private val platform: ProtocolPlatform) : SessionLink { override fun recordDisplayFrameSent() {} } +private fun hex(value: String): ByteArray = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray() + +/** GlassesCaseDataPackage{commandId=CASE_INFO, magicRandom, caseInfo}; caseInfo omitted when null. */ +private fun casePackage(magic: Int, caseInfo: ByteArray?): ByteArray = + BleProtocol.encodeVarintField(1, BleProtocol.GLASSES_CASE_CMD_CASE_INFO) + BleProtocol.encodeVarintField(2, magic) + + (if (caseInfo == null) ByteArray(0) else BleProtocol.encodeBytesField(3, caseInfo)) + +/** GlassesCaseInfo with proto3 zero-omission: soc=1, chargeStatus=2, lidStatus=3, inCaseStatus=4, errorCode=5. */ +private fun caseInfoBody(soc: Int, charge: Int = 0, lid: Int = 0, inCase: Int = 0, error: Int = 0): ByteArray = + listOf(soc, charge, lid, inCase, error).withIndex() + .filter { it.value != 0 } + .fold(ByteArray(0)) { acc, field -> acc + BleProtocol.encodeVarintField(field.index + 1, field.value) } + private fun cfwAck(streamId: Int, lens: Int, size: Int, checksum: Int): ByteArray { val packet = ByteArray(19) packet[0] = 0xaa.toByte() @@ -237,6 +254,12 @@ private class FakeListener : FaceclawBleCommunicatorListener { batteries.add("$headsetBattery/$headsetCharging/$ringBattery/$ringCharging") } + val cases = ArrayList() + + override fun onCaseState(caseBattery: Int, caseCharging: Int) { + cases.add("$caseBattery/$caseCharging") + } + override fun onSilentMode(silent: Boolean) {} override fun onWearState(wearing: Boolean) {} @@ -691,4 +714,63 @@ class GlassesSessionCoreTest { assertTrue(info("Faceclaw/1").isCompatible(0)) assertFalse(info("EVENCFW/22").isCompatible(0)) } + + @Test + fun caseInfoQueryMatchesTheEvenAppAndRepliesParse() { + // commandId=CASE_INFO, magicRandom=42, empty caseInfo. + assertContentEquals(hex("0801102a1a00"), BleProtocol.buildCaseInfoQuery(42)) + // Through a real frame, so the trailing CRC is present as it is on the wire. + fun parse(pb: ByteArray) = BleProtocol.parseCaseInfo( + BleProtocol.parseFrame(BleProtocol.framePb(pb, BleProtocol.SID_GLASSES_CASE, 0x00, 3)[0]).pb) + assertEquals(BleProtocol.CaseInfoSnapshot(64, 1, 1, 1, 0), parse(casePackage(7, caseInfoBody(64, charge = 1, lid = 1, inCase = 1)))) + assertFalse(parse(casePackage(7, caseInfoBody(80, error = 7)))!!.hasLevel()) + // No data, an all-default body, or another command is not a reading. + assertEquals(null, parse(casePackage(7, null))) + assertEquals(null, parse(casePackage(7, ByteArray(0)))) + assertEquals(null, parse(BleProtocol.encodeVarintField(2, 7) + BleProtocol.encodeBytesField(3, caseInfoBody(50)))) + assertEquals(null, BleProtocol.parseCaseInfo(null)) + // Captured from G2 2.3.0.24 / Faceclaw/38 (CRC stripped). Out of the case the reply is empty; + // docking pushes inCase alone (proto3 dropped soc: no level, not 0%), then the level. + assertEquals(null, parse(hex("08011086011a00"))) + val docked = parse(hex("08011a022001"))!! + assertEquals(BleProtocol.CaseInfoSnapshot(-1, 0, 0, 1, 0), docked) + assertFalse(docked.hasLevel()) + assertEquals(BleProtocol.CaseInfoSnapshot(88, 0, 0, 1, 0), parse(hex("080110011a0408582001"))) + } + + @Test + fun theCaseIsReadAtSessionStartAndOnDockingThenPushesCarryIt() { + val s = Session() + try { + // Out of the case the session-start read answers without a caseInfo. + s.startAndAwaitLayout() + fun queries() = s.link.writes.filter { it.kind == "case-info" } + assertTrue(waitUntil(5_000) { queries().size == 1 }) + assertEquals(listOf(), s.listener.cases) + s.link.caseInfo = caseInfoBody(64, charge = 1, inCase = 1) + s.core.monitor.withLock { s.core.updateChargingModeLocked(true, 80) } + s.core.interruptibleSleep.interrupt() + assertTrue(waitUntil(5_000) { s.listener.cases.isNotEmpty() }, s.host.logs().takeLast(10).toString()) + assertEquals(listOf("64/1"), s.listener.cases) + assertEquals(listOf(RIGHT, RIGHT), queries().map { it.address }) + // Pushes (flag 0x01, the glasses' own magic) update it without being taken for acks. + fun push(info: ByteArray) = s.core.onNotification(RIGHT, BleProtocol.NOTIFY_CHAR_UUID, + BleProtocol.framePb(casePackage(200, info), BleProtocol.SID_GLASSES_CASE, BleProtocol.FLAG_NOTIFY, 99)[0]) + push(caseInfoBody(63, lid = 1, inCase = 1)) + assertEquals("63/0", s.listener.cases.last()) + // Unchanged level and charge, no level yet, or a failed read: nothing to report. + push(caseInfoBody(63, inCase = 1)) + push(caseInfoBody(0, inCase = 1)) + push(caseInfoBody(70, inCase = 1, error = 3)) + assertEquals(2, s.listener.cases.size) + assertFalse(s.host.hasLog("unexpected"), s.host.logs().filter { it.contains("unexpected") }.toString()) + // Later charging-mode battery polls do not read the case again. + s.platform.offsetMs += 2L * ConnectionOptions.CHARGING_BATTERY_POLL_MS + s.core.interruptibleSleep.interrupt() + sleepMs(300) + assertEquals(2, queries().size) + } finally { + s.core.close() + } + } } diff --git a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosSessionFacadeTest.kt b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosSessionFacadeTest.kt index 17fd93a5..4c9cbfe9 100644 --- a/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosSessionFacadeTest.kt +++ b/tests/kotlin/src/iosTest/kotlin/com/faceclaw/app/IosSessionFacadeTest.kt @@ -130,6 +130,10 @@ private class MainThreadListener : FaceclawBleCommunicatorListener { if (!NSThread.isMainThread) offMainCallbacks++ } + override fun onCaseState(caseBattery: Int, caseCharging: Int) { + if (!NSThread.isMainThread) offMainCallbacks++ + } + override fun onSilentMode(silent: Boolean) {} override fun onWearState(wearing: Boolean) {} From 9c0535014a28c96439198fbf9c53bc4d0a37e419 Mon Sep 17 00:00:00 2001 From: Om Alve Date: Sun, 1 Nov 2026 21:29:32 +0530 Subject: [PATCH 2/8] Phone UI: show the glasses, charging case and R1 battery levels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A line under the mirror lists each device whose level is known, e.g. "G2 100% · Case 98% ⚡ · R1 72%" (⚡ = charging), in portrait and landscape. The case level joins the shell's battery state beside the glasses and R1 (it is not drawn in the glasses' top bar) and reaches the page through the dashboard snapshot. Like the glasses level it is kept across disconnects: the case only reports while the glasses are docked and only drains while charging them. --- app/app.css | 7 +++++++ app/g2/dashboard-controller.ts | 14 +++++++++++++ app/phone-ui/main-page.xml | 12 ++++++----- app/phone-ui/main-view-model.ts | 36 ++++++++++++++++++++++++++++++++- app/ui/shell/chrome-layer.ts | 6 ++++++ app/ui/shell/shell.ts | 1 + 6 files changed, 70 insertions(+), 6 deletions(-) diff --git a/app/app.css b/app/app.css index 4dbacd37..8b4ba544 100644 --- a/app/app.css +++ b/app/app.css @@ -136,6 +136,13 @@ Button.-primary { text-align: center; } +/* Battery levels under the mirror (main-page.xml). */ +.battery-line { + font-size: 14; + text-align: center; + padding: 12 16 0 16; +} + .system-name-modal { margin: 16; padding: 16; diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 0b995653..f6d92f3a 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -103,6 +103,8 @@ type ConnectionPhase = "disconnected" | "connecting" | "connected" | "charging" export type DashboardSnapshot = { phase: ConnectionPhase; + /** Last-known battery levels: the glasses, their charging case, the R1 ring and a Wear OS watch. */ + batteries: ReturnType; status: string; displayPreview: ImageSource | null; /** @@ -333,6 +335,7 @@ class DashboardController { private offState: (() => void) | null = null; private offRing: (() => void) | null = null; private offBattery: (() => void) | null = null; + private offCaseBattery: (() => void) | null = null; private offSilentMode: (() => void) | null = null; private offWearState: (() => void) | null = null; private offPhoneLockState: (() => void) | null = null; @@ -1130,6 +1133,7 @@ class DashboardController { alarmReliabilityMessage: this.alarmReliabilityMessage, previewMode: this.isPreviewDisplayActive(), evenHubPhoneUi: phoneUiButton(), + batteries: shell.getBatteryLevels(), }; } @@ -1584,6 +1588,12 @@ class DashboardController { // the watch state, so refresh both consumers on every report. this.emit(); }); + this.offCaseBattery = communicator.onCaseState((state) => { + // Kept across disconnects: the case only reports while the glasses are + // docked and only drains while charging them, so the last level holds. + shell.setBatteryLevels({ case: state.battery, caseCharging: state.charging }); + this.emit(); + }); this.offEvenAppConflict = communicator.onEvenAppConflict((message) => { this.refreshEvenAppStatus(); if (!this.evenNotificationActive) { @@ -1704,6 +1714,8 @@ class DashboardController { this.offRing = null; this.offBattery?.(); this.offBattery = null; + this.offCaseBattery?.(); + this.offCaseBattery = null; this.offSilentMode?.(); this.offSilentMode = null; this.offWearState?.(); @@ -1816,6 +1828,8 @@ class DashboardController { this.offRing = null; this.offBattery?.(); this.offBattery = null; + this.offCaseBattery?.(); + this.offCaseBattery = null; this.offSilentMode?.(); this.offSilentMode = null; this.offWearState?.(); diff --git a/app/phone-ui/main-page.xml b/app/phone-ui/main-page.xml index 846fa253..880b9032 100644 --- a/app/phone-ui/main-page.xml +++ b/app/phone-ui/main-page.xml @@ -19,7 +19,7 @@ - + @@ -31,9 +31,10 @@ visibility="{{ displayPreviewMessageVisibility }}"> +