Merge branch 'main' into rayneo-io-support

This commit is contained in:
James Babcock committed 2026-11-03 14:06:19 -08:00
commit b2b6c5c8d8
36 files changed
+822 -39

No files matched your search

@@ -23,6 +23,13 @@ interface FaceclawBleCommunicatorListener {
ringCharging: 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
fun onWearState(wearing: Boolean): Unit
@@ -90,6 +90,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
@@ -899,6 +905,42 @@ 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. 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? {
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
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
@@ -1590,6 +1632,33 @@ 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); only 0 and 1 have been seen. */
@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
fun isDocked(): Boolean = inCaseStatus > 0
fun isCaseCharging(): Boolean = chargeStatus > 0
}
class FirmwareInfo {
companion object {
/** Prefix of the firmware-extension string on Faceclaw's custom firmware. */
@@ -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
@@ -262,6 +262,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",
@@ -728,6 +728,50 @@ internal fun GlassesSessionCore.emitBatteryState(headsetBattery: Int, headsetCha
}
}
/**
* 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.errorCode != 0) {
return
}
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
}
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 {
current.onCaseState(level, charging, docked)
} catch (t: Throwable) {
logWarn("listener onCaseState failed", t)
}
}
}
internal fun GlassesSessionCore.emitFirmwareInfo(info: BleProtocol.FirmwareInfo) {
val current = listener ?: return
host.postToMain {
@@ -393,6 +393,17 @@ class GlassesSessionCore(
// frames while claiming the glasses role).
internal var lastDirectRingRaw = ""
internal var lastDirectRingRawAtMs = 0L
// 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
// 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
@@ -1975,6 +1986,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
@@ -2255,6 +2271,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")
@@ -2286,6 +2307,7 @@ class GlassesSessionCore(
lastConnectionOrInputAtMs = lastAckAtMs
lastSessionReadyAtMs = lastAckAtMs
lastBatteryRefreshAtMs = 0
dockedBatteryCheckAtMs = 0
imageRetryAfterMs = 0
lastHeartbeatSentAtMs = 0
lastHeartbeatAckedAtMs = 0
@@ -2324,6 +2346,11 @@ 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. The
// drive loop sends it once the layout is up.
caseInfoReadPending = true
}
// Wake the ring worker; it owns the (blocking) ring connect.
monitor.signalAll()
@@ -58,6 +58,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
@@ -167,6 +177,14 @@ internal fun GlassesSessionCore.driveSession(): Long {
enqueueFirmwareDebugFlagsLocked()
}
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
&& caseInfoReadPending
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
caseInfoReadPending = false
logLine("enqueueing case info read")
pendingMessages.addLast(messageBuilder.caseInfoQuery())
}
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
&& (if (compassOwners.isEmpty()) 0 else 1) != compassControlLastSent
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
@@ -1112,6 +1130,10 @@ internal fun GlassesSessionCore.updateChargingModeLocked(charging: Boolean, batt
if (charging) {
chargingMode = true
clearAllMessagesLocked("glasses charging")
// 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
displayedFingerprint = ""