forked from Blendman974/faceclaw
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:
1 parent
a4adb73182
commit
acfcf79787
10 files changed
+234
No files matched your search
+3
@@ -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
|
||||
|
||||
@@ -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. */
|
||||
|
||||
+15
@@ -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",
|
||||
|
||||
+18
@@ -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 {
|
||||
|
||||
+12
@@ -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) {
|
||||
|
||||
+3
@@ -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 = ""
|
||||
|
||||
Reference in new issue
Block a user