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.
This commit is contained in:
Om Alve committed 2026-11-01 20:38:32 +05:30
1 parent a4adb73182
commit acfcf79787
10 files changed
+234

No files matched your search

@@ -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<String>()
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<String>(), 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()
}
}
}
@@ -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) {}