Case battery: stop showing stale charging; read after the layout

Review fixes for the charging-case battery:

- The case reports whether the glasses are docked. An empty caseInfo
  (the reply out of the case) now parses as "not docked, no level"
  instead of nothing, and undocking is reported even when the level and
  charge state are unchanged. TS keeps the last level but clears
  caseCharging while the glasses are out of the case.
- The phone line shows the charging mark only while it is live: G2 and
  Case only with a link to the glasses (connected or charging), and Case
  only while docked. Levels stay as the last known ones.
- The session-start case read waits until the layout is up, so a
  firmware that ignored it could no longer hold up the first frame. When
  the glasses are already charging at connect, the charging-mode read is
  the only one; the old duplicate read and its "late ACK" log are gone.
- Case dedup state resets on every connect, as wearState does, so each
  session's read reaches TS.
- caseCharging uses > 0 like the headset, and is normalized to 0/1.
- iOS now feeds the reading into the shell battery state, and the
  assistant's describeState includes the case level.
- The batteryLine accessors move above connectionStatusLabel's doc
  comment.
This commit is contained in:
Om Alve committed 2026-11-02 12:01:12 +05:30
1 parent 9c0535014a
commit fa7f33bfe8
15 files changed
+199 -61

No files matched your search

@@ -23,8 +23,12 @@ 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
/**
* A new charging-case reading. caseBattery is 0..100, or -1 when the reading has no level
* (the glasses are out of the case; keep the last one). caseCharging 1 = the case is on
* USB, known only while caseDocked (0 otherwise).
*/
fun onCaseState(caseBattery: Int, caseCharging: Int, caseDocked: Boolean): Unit
fun onSilentMode(silent: Boolean): Unit
@@ -782,7 +782,8 @@ class BleProtocol {
/**
* The GlassesCaseInfo (field 3) of a sid-0x81 CASE_INFO reply or push, or null when there
* is none or it is empty.
* is none. Out of the case the glasses answer with an empty one, which parses as
* not docked and without a level.
*/
@JvmStatic
fun parseCaseInfo(pb: ByteArray?): CaseInfoSnapshot? {
@@ -794,9 +795,6 @@ class BleProtocol {
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),
@@ -1508,7 +1506,7 @@ class BleProtocol {
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). */
/** 1 = the case itself is charging (on USB); only 0 and 1 have been seen. */
@JvmField val chargeStatus: Int,
/** 1 = lid closed, 0 = open. */
@JvmField val lidStatus: Int,
@@ -1518,6 +1516,10 @@ class BleProtocol {
@JvmField val errorCode: Int,
) {
fun hasLevel(): Boolean = errorCode == 0 && soc in 0..100
fun isDocked(): Boolean = inCaseStatus > 0
fun isCaseCharging(): Boolean = chargeStatus > 0
}
class FirmwareInfo {
@@ -721,18 +721,35 @@ internal fun GlassesSessionCore.emitBatteryState(headsetBattery: Int, headsetCha
}
}
/** Report a charging-case reading when it carries a level that differs from the last one. */
/**
* Report a charging-case reading when it differs from the last one. Docked, a reading counts
* only once it carries a level (docking pushes {inCaseStatus=1} first and the level about 0.7 s
* later). Undocked -- the empty reply to a read, or an undock push -- it is reported even without
* a level, so the listener learns the case's charge state is no longer known.
*/
internal fun GlassesSessionCore.updateCaseInfoLocked(info: BleProtocol.CaseInfoSnapshot) {
if (!info.hasLevel() || (info.soc == caseBattery && info.chargeStatus == caseCharging)) {
if (info.errorCode != 0) {
return
}
caseBattery = info.soc
caseCharging = info.chargeStatus
val docked = info.isDocked()
if (docked && !info.hasLevel()) {
return
}
val level = if (info.hasLevel()) info.soc else -1
// Out of the case the glasses can't see the case's USB state.
val charging = if (docked && info.isCaseCharging()) 1 else 0
val dockedState = if (docked) 1 else 0
if (level == caseBattery && charging == caseCharging && dockedState == caseDocked) {
return
}
caseBattery = level
caseCharging = charging
caseDocked = dockedState
logLine("charging case: $info")
val current = listener ?: return
host.postToMain {
try {
current.onCaseState(info.soc, info.chargeStatus)
current.onCaseState(level, charging, docked)
} catch (t: Throwable) {
logWarn("listener onCaseState failed", t)
}
@@ -230,9 +230,14 @@ 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.
// Last charging-case reading reported to the listener (-1 = none yet this
// connection). See updateCaseInfoLocked.
internal var caseBattery = -1
internal var caseCharging = -1
internal var caseDocked = -1
// 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
// Silent mode: 1 = on, 0 = off, -1 = not yet known. See updateSilentModeLocked.
internal var silentMode = -1
internal var wearState = -1
@@ -1773,6 +1778,11 @@ class GlassesSessionCore(
// reconnect; forget ours too, or the new session's first report
// (the CFW query reply) is deduped away and TS never relearns it.
wearState = -1
// Same for the case: the glasses may have left it while the link
// was down, and the new session's read should reach TS either way.
caseBattery = -1
caseCharging = -1
caseDocked = -1
}
setStateDisplay("connecting", "Connecting to the glasses...")
ConnectionCounters.increment("g2.connect-attempt")
@@ -1844,8 +1854,9 @@ class GlassesSessionCore(
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())
// this read comes back empty. Later changes are pushed. The
// drive loop sends it once the layout is up.
caseInfoReadPending = true
}
tryConnectRing("initial")
} catch (t: Throwable) {
@@ -165,6 +165,14 @@ internal fun GlassesSessionCore.driveSession(): Long {
enqueueFirmwareDebugFlagsLocked()
}
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated
&& caseInfoReadPending
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
caseInfoReadPending = false
logLine("enqueueing case info read")
pendingMessages.addLast(messageBuilder.caseInfoQuery())
}
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated
&& (if (compassOwners.isEmpty()) 0 else 1) != compassControlLastSent
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
@@ -1099,8 +1107,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.
// Docked is when the case reports. No layout is created while
// charging, so this read stands in for the session-start one.
caseInfoReadPending = false
pendingMessages.addLast(messageBuilder.caseInfoQuery())
fixedLayoutCreated = false
startupProbePending = false