forked from Blendman974/faceclaw
Faceclaw support for G1s
Relies on the glasses having the firmware from https://github.com/jimrandomh/scouter_kit, based on Mafik's Scouter Kit.
This commit is contained in:
1 parent
f803f4212c
commit
67aff19923
27 files changed
+1902
-45
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* One lens's update on an Even G1: the rectangles that differ from what the arm's frame holds, as RLE bbox calls
|
||||
* in draw messages. The transport's deflate runs over these messages, as on the G2; there is no resource cache,
|
||||
* so everything visible arrives as pixels.
|
||||
*/
|
||||
object G1FramePlanner {
|
||||
/** The firmware's limit on a decoded message is 16384 bytes (scouter_kit fc.c MAX_MESSAGE). */
|
||||
const val MAX_MESSAGE = 16000
|
||||
|
||||
/** A big rectangle goes out in bands, so that one noisy area doesn't need a long binary split. */
|
||||
private const val BAND_ROWS = 32
|
||||
private const val MAX_RECTS = ConnectionOptions.MULTI_RECT_MAX_RECTS
|
||||
|
||||
/** [previous] null or of another size: a full frame. Empty when nothing changed. */
|
||||
fun plan(previous: ByteArray?, next: ByteArray, width: Int, height: Int): List<ByteArray> {
|
||||
val stride = (width + 1) / 2
|
||||
require(next.size == stride * height)
|
||||
val rects = changedRects(previous, next, width, height)
|
||||
if (rects.isEmpty()) return emptyList()
|
||||
val calls = ArrayList<ByteArray>()
|
||||
for (r in rects) {
|
||||
var top = r[1]
|
||||
val bottom = r[1] + r[3]
|
||||
while (top < bottom) {
|
||||
val rows = minOf(BAND_ROWS, bottom - top)
|
||||
addRect(calls, next, stride, r[0], top, r[2], rows)
|
||||
top += rows
|
||||
}
|
||||
}
|
||||
return DrawProtocol.messages(calls, MAX_MESSAGE)
|
||||
}
|
||||
|
||||
private fun changedRects(previous: ByteArray?, next: ByteArray, width: Int, height: Int): List<IntArray> {
|
||||
if (previous == null || previous.size != next.size) return listOf(intArrayOf(0, 0, width, height))
|
||||
BleImageOptimizer.computeChangedRects(previous, next, width, height, MAX_RECTS)?.let { return it }
|
||||
return listOfNotNull(BleImageOptimizer.computeChangedBox(previous, next, width, height))
|
||||
}
|
||||
|
||||
/** Splits until the call fits a message; a single pixel always does. */
|
||||
private fun addRect(calls: MutableList<ByteArray>, packed: ByteArray, stride: Int, x: Int, y: Int, w: Int, h: Int) {
|
||||
val call = DrawProtocol.bbox(packed, stride, x, y, w, h)
|
||||
if (call.size + MESSAGE_OVERHEAD <= MAX_MESSAGE) {
|
||||
calls.add(call)
|
||||
} else if (h > 1) {
|
||||
addRect(calls, packed, stride, x, y, w, h / 2)
|
||||
addRect(calls, packed, stride, x, y + h / 2, w, h - h / 2)
|
||||
} else {
|
||||
addRect(calls, packed, stride, x, y, w / 2, h)
|
||||
addRect(calls, packed, stride, x + w / 2, y, w - w / 2, h)
|
||||
}
|
||||
}
|
||||
|
||||
/** The message type, the call count and the call's length (DrawProtocol.sequence). */
|
||||
private const val MESSAGE_OVERHEAD = 5
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.jvm.JvmField
|
||||
|
||||
/** The GATT service of g1v0, the firmware of an Even G1 (scouter_kit g1_firmware/docs/VM.md §6.1). */
|
||||
object G1Ble {
|
||||
const val CTRL_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4402"
|
||||
const val EVT_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4403"
|
||||
/** Readable and writable only on an encrypted link: a write with response makes the OS pair, then proves the link is encrypted. */
|
||||
const val PAIR_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4404"
|
||||
/** Faceclaw's records (VM.md §6.11). */
|
||||
const val FC_CHAR_UUID = "f3da4d59-277a-4fc9-a1ed-41c9c22d4405"
|
||||
const val REQUESTED_MTU = 247
|
||||
}
|
||||
|
||||
/**
|
||||
* The wire formats of an Even G1 on g1v0: control requests and events (VM.md §6.3, §6.4), which Faceclaw uses as they
|
||||
* are, and the display path's packets and answers (§6.11). No VM instruction is sent; the records inside the packets
|
||||
* are [CfwTransport]'s, the same as on the G2.
|
||||
*/
|
||||
object G1Protocol {
|
||||
const val OP_HELLO = 0x01
|
||||
const val OP_HEALTH_OK = 0x07
|
||||
const val OP_PROPS = 0x0A
|
||||
const val OP_EVENTS = 0x14
|
||||
const val OP_FC_INFO = 0x1D
|
||||
|
||||
const val EV_INPUT = 0x07
|
||||
const val EV_FC_ACK = 0x08
|
||||
private const val EV_REPLY = 0x80
|
||||
private const val EVENT_HEADER = 9
|
||||
private const val OPT_START = 0x80
|
||||
private const val OPT_END = 0x40
|
||||
private const val MAX_PACKET = 244
|
||||
private const val PACKET_HEADER = 2
|
||||
|
||||
const val TRIGGER_BATTERY = 5
|
||||
const val TRIGGER_CASE = 6
|
||||
const val TRIGGER_WEAR = 7
|
||||
const val TRIGGER_TOUCH = 8
|
||||
const val EVENTS_MASK = (1 shl TRIGGER_BATTERY) or (1 shl TRIGGER_CASE) or (1 shl TRIGGER_WEAR) or (1 shl TRIGGER_TOUCH)
|
||||
|
||||
const val PROP_BATT_PCT = 0x11
|
||||
const val PROP_CHARGE = 0x12
|
||||
const val PROP_IN_CASE = 0x13
|
||||
const val PROP_WORN = 0x14
|
||||
/** A property without a value yet (VM_UNKNOWN). */
|
||||
const val UNKNOWN = Int.MIN_VALUE
|
||||
|
||||
const val CHARGE_CHARGING = 1
|
||||
|
||||
const val GESTURE_TAP = 1
|
||||
const val GESTURE_DOUBLE_TAP = 2
|
||||
const val GESTURE_TRIPLE_TAP = 3
|
||||
const val GESTURE_LONG_START = 4
|
||||
const val GESTURE_LONG_END = 5
|
||||
|
||||
/** The packets of one record: `[0x80 first | 0x40 last][stream][the next bytes]`. */
|
||||
fun packets(record: ByteArray, stream: Int, mtu: Int): List<ByteArray> {
|
||||
val chunk = minOf(mtu - 3, MAX_PACKET) - PACKET_HEADER
|
||||
require(record.isNotEmpty() && chunk >= 5 && stream in 0..255)
|
||||
val packets = ArrayList<ByteArray>(record.size / chunk + 1)
|
||||
var offset = 0
|
||||
while (offset < record.size) {
|
||||
val count = minOf(chunk, record.size - offset)
|
||||
val packet = ByteArray(PACKET_HEADER + count)
|
||||
packet[0] = ((if (offset == 0) OPT_START else 0) or (if (offset + count == record.size) OPT_END else 0)).toByte()
|
||||
packet[1] = stream.toByte()
|
||||
record.copyInto(packet, PACKET_HEADER, offset, offset + count)
|
||||
packets.add(packet)
|
||||
offset += count
|
||||
}
|
||||
return packets
|
||||
}
|
||||
|
||||
fun control(op: Int, tag: Int, body: ByteArray = ByteArray(0)): ByteArray =
|
||||
byteArrayOf(op.toByte(), tag.toByte()) + body
|
||||
|
||||
fun eventsRequest(mask: Int): ByteArray = byteArrayOf(mask.toByte(), (mask ushr 8).toByte())
|
||||
|
||||
/** An event's type, or -1 for bytes too short to be one. */
|
||||
fun eventType(data: ByteArray): Int = if (data.size >= EVENT_HEADER) data[0].toInt() and 255 else -1
|
||||
|
||||
class Reply(@JvmField val op: Int, @JvmField val tag: Int, @JvmField val status: Int, @JvmField val body: ByteArray)
|
||||
|
||||
fun parseReply(data: ByteArray): Reply? {
|
||||
val type = eventType(data)
|
||||
if (type < EV_REPLY || data.size < EVENT_HEADER + 2) return null
|
||||
return Reply(type and 0x7f, data[EVENT_HEADER].toInt() and 255, data[EVENT_HEADER + 1].toInt() and 255,
|
||||
data.copyOfRange(EVENT_HEADER + 2, data.size))
|
||||
}
|
||||
|
||||
/** Same shape as [CfwTransport.parseAcks]'s, so the shared window takes it. */
|
||||
fun parseFcAck(data: ByteArray): CfwTransport.Ack? {
|
||||
if (eventType(data) != EV_FC_ACK || data.size < EVENT_HEADER + 9) return null
|
||||
val kind = data[EVENT_HEADER].toInt() and 255
|
||||
if (kind != 1 && kind != 3) return null
|
||||
return CfwTransport.Ack(kind == 3, data[EVENT_HEADER + 1].toInt() and 255, u16(data, EVENT_HEADER + 2),
|
||||
data[EVENT_HEADER + 4].toInt() and 255, u16(data, EVENT_HEADER + 5), u16(data, EVENT_HEADER + 7))
|
||||
}
|
||||
|
||||
class Input(@JvmField val trigger: Int, @JvmField val value: Int)
|
||||
|
||||
fun parseInput(data: ByteArray): Input? {
|
||||
if (eventType(data) != EV_INPUT || data.size < EVENT_HEADER + 7) return null
|
||||
return Input(data[EVENT_HEADER + 2].toInt() and 255, u32(data, EVENT_HEADER + 3))
|
||||
}
|
||||
|
||||
class Hello(
|
||||
@JvmField val version: String,
|
||||
@JvmField val leftArm: Boolean,
|
||||
@JvmField val safeMode: Boolean,
|
||||
@JvmField val panelOk: Boolean,
|
||||
@JvmField val encrypted: Boolean,
|
||||
@JvmField val inCase: Boolean,
|
||||
)
|
||||
|
||||
fun parseHello(body: ByteArray): Hello? {
|
||||
if (body.size < 7 || body[0].toInt() != 5) return null
|
||||
val flags = body[5].toInt() and 255
|
||||
return Hello("${body[1].toInt() and 255}.${body[2].toInt() and 255}.${body[3].toInt() and 255}",
|
||||
body[4].toInt() == 1, flags and 1 != 0, flags and 2 != 0, flags and 64 != 0, flags and 128 != 0)
|
||||
}
|
||||
|
||||
/** The display path's version, or -1 for a body that isn't FC_INFO's. */
|
||||
fun parseFcVersion(body: ByteArray): Int = if (body.size >= 14) body[0].toInt() and 255 else -1
|
||||
|
||||
/** One value per requested property, or null if the reply is short. */
|
||||
fun parseProps(body: ByteArray, count: Int): IntArray? =
|
||||
if (body.size < 4 * count) null else IntArray(count) { u32(body, 4 * it) }
|
||||
|
||||
/**
|
||||
* What a TouchBar gesture means to Faceclaw, which expects a G2's temple: a tap, a double tap and two slides. A
|
||||
* G1's TouchBar cannot slide, but it has one per arm, so a tap moves (left up, right down), a double tap selects
|
||||
* and a triple tap or long press goes back. Null: no event. With the display [dark] nothing can scroll: a tap is
|
||||
* the G2's tap, which shows the Glanceboard, and the session reports a double tap as a wake.
|
||||
*/
|
||||
fun gestureEventType(gesture: Int, leftArm: Boolean, dark: Boolean = false): Int? = when (gesture) {
|
||||
GESTURE_TAP -> if (dark) BleProtocol.EVENT_CLICK else if (leftArm) BleProtocol.EVENT_SCROLL_TOP else BleProtocol.EVENT_SCROLL_BOTTOM
|
||||
GESTURE_DOUBLE_TAP -> if (dark) BleProtocol.EVENT_DOUBLE_CLICK else BleProtocol.EVENT_CLICK
|
||||
GESTURE_TRIPLE_TAP, GESTURE_LONG_START -> BleProtocol.EVENT_DOUBLE_CLICK
|
||||
else -> null
|
||||
}
|
||||
|
||||
private fun u16(b: ByteArray, p: Int): Int = (b[p].toInt() and 255) or ((b[p + 1].toInt() and 255) shl 8)
|
||||
|
||||
private fun u32(b: ByteArray, p: Int): Int = u16(b, p) or (u16(b, p + 2) shl 16)
|
||||
}
|
||||
+2
-2
@@ -33,7 +33,7 @@ class CfwMessageWindow private constructor() {
|
||||
fun acknowledgedHead(messages: Iterable<OutboundMessage>): OutboundMessage? {
|
||||
for (message in messages) {
|
||||
if (message.sid != CfwTransport.SID) continue
|
||||
return if (!message.cfwRetryPending && message.cfwAckLenses == CfwTransport.BOTH)
|
||||
return if (!message.cfwRetryPending && message.isCfwAcked)
|
||||
message
|
||||
else null
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class CfwMessageWindow private constructor() {
|
||||
failed =
|
||||
failed ||
|
||||
message.cfwRetryPending ||
|
||||
(message.cfwAckLenses != CfwTransport.BOTH &&
|
||||
(!message.isCfwAcked &&
|
||||
message.ackDeadlineAtMs > 0 &&
|
||||
message.ackDeadlineAtMs <= nowMs)
|
||||
replay.add(message)
|
||||
|
||||
+5
-1
@@ -18,6 +18,8 @@ class OutboundMessage(
|
||||
@JvmField
|
||||
val cfwChecksum: Int = if (sid == CfwTransport.SID) CfwTransport.crc(this.message) else 0
|
||||
@JvmField var cfwAckLenses = 0
|
||||
/** The lenses that must ACK this attempt: both on a G2, whichever the message went to on a G1. */
|
||||
@JvmField var cfwRequiredLenses = CfwTransport.BOTH
|
||||
@JvmField var cfwRetries = 0
|
||||
@JvmField var cfwRetryPending = false
|
||||
@JvmField var imageUpdateId = 0
|
||||
@@ -47,9 +49,11 @@ class OutboundMessage(
|
||||
}
|
||||
if (message.size != ack.size || cfwChecksum != ack.checksum) return false
|
||||
cfwAckLenses = cfwAckLenses or ack.lens
|
||||
return !cfwRetryPending && cfwAckLenses == CfwTransport.BOTH
|
||||
return !cfwRetryPending && isCfwAcked
|
||||
}
|
||||
|
||||
val isCfwAcked: Boolean get() = cfwAckLenses and cfwRequiredLenses == cfwRequiredLenses
|
||||
|
||||
/** Replace an attempt atomically under the communicator lock. */
|
||||
fun prepareCfwReplay(streamId: Int) {
|
||||
magic = streamId
|
||||
|
||||
+9
-4
@@ -19,15 +19,20 @@ internal fun GlassesSessionCore.enqueueBrightnessLocked(frameVisible: Boolean? =
|
||||
else if (brightnessPolicy.automatic) 1200 else 120
|
||||
val payload = byteArrayOf(CFW_MSG_BRIGHTNESS.toByte(), 1, target.toByte(),
|
||||
(if (visible) 1 else 0).toByte(), duration.toByte(), (duration shr 8).toByte())
|
||||
val message = messageBuilder.customMessage("brightness-output", payload,
|
||||
"brightness target=$target visible=$visible fade=$duration", -1, connectionOptions.sendImagesToLeft)
|
||||
message.onTimeout = MessageCallback {
|
||||
val label = "brightness target=$target visible=$visible fade=$duration"
|
||||
val retry = MessageCallback {
|
||||
brightnessSentLevel = -1
|
||||
brightnessSentVisible = null
|
||||
// Retry with a PRESENT even if the scene did not change during recovery.
|
||||
lastEnqueuedFingerprint = ""
|
||||
}
|
||||
pendingMessages.addLast(message)
|
||||
if (isG1) {
|
||||
g1EnqueueBrightnessLocked(payload, label, brightnessSentVisible != visible, retry)
|
||||
} else {
|
||||
val message = messageBuilder.customMessage("brightness-output", payload, label, -1, connectionOptions.sendImagesToLeft)
|
||||
message.onTimeout = retry
|
||||
pendingMessages.addLast(message)
|
||||
}
|
||||
brightnessSentLevel = target
|
||||
brightnessSentVisible = visible
|
||||
if (isIo) io.lastBrightnessAtMs = now()
|
||||
|
||||
+36
-3
@@ -111,6 +111,8 @@ class GlassesSessionCore(
|
||||
@Volatile internal var glassesType = GlassesType.G2
|
||||
/** RayNeo iO link state (GlassesSessionIo.kt); guarded by [monitor]. */
|
||||
internal val io = IoSessionState()
|
||||
/** Even G1 link state (GlassesSessionG1.kt); guarded by [monitor]. */
|
||||
internal val g1 = G1SessionState()
|
||||
|
||||
/**
|
||||
* Optional tap for relay frames the session does not interpret: notifications on the
|
||||
@@ -578,6 +580,14 @@ class GlassesSessionCore(
|
||||
}
|
||||
}
|
||||
|
||||
/** Drive an Even G1 on g1v0 instead of a G2 (see [G1SessionState]). Before [start]. */
|
||||
fun useG1() {
|
||||
monitor.withLock { glassesType = GlassesType.G1 }
|
||||
}
|
||||
|
||||
/** True when this session drives an Even G1. */
|
||||
fun isEvenG1(): Boolean = isG1
|
||||
|
||||
/** True when this session drives a RayNeo iO. */
|
||||
fun isRayNeoIo(): Boolean = isIo
|
||||
|
||||
@@ -613,7 +623,7 @@ class GlassesSessionCore(
|
||||
* quiescing its producers. Also cover direct/early close callers here;
|
||||
* a successful cleanup must remain the final BLE message. Older CFWs
|
||||
* fall back to the standalone framebuffer-lease release. */
|
||||
if (!cfwCleanupDelivered && !sendCfwCleanup() && !isIo) {
|
||||
if (!cfwCleanupDelivered && !sendCfwCleanup() && isG2) {
|
||||
releaseFaceclawFramebufferLease()
|
||||
}
|
||||
monitor.withLock {
|
||||
@@ -683,6 +693,10 @@ class GlassesSessionCore(
|
||||
}
|
||||
// The iO's packets are Opus from its remote audio stream (see ioStartAudioCapture).
|
||||
if (isIo) return ioStartAudioCapture(listener)
|
||||
if (isG1) {
|
||||
logLine("skip G1 mic: not in this version")
|
||||
return false
|
||||
}
|
||||
var magic = 0
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) {
|
||||
@@ -701,6 +715,7 @@ class GlassesSessionCore(
|
||||
|
||||
fun stopG2AudioCapture() {
|
||||
if (isIo) return ioStopAudioCapture()
|
||||
if (isG1) return
|
||||
var magic = 0
|
||||
monitor.withLock {
|
||||
audioPacketListener = null
|
||||
@@ -834,7 +849,7 @@ class GlassesSessionCore(
|
||||
monitor.withLock {
|
||||
faceclawWakeLeaseEnabled = enabled
|
||||
// A G2 firmware lease; the iO's wake word is setVoiceWakeEnabled.
|
||||
if (isIo) return true
|
||||
if (!isG2) return true
|
||||
if (!running || !sessionReady) {
|
||||
return !enabled
|
||||
}
|
||||
@@ -1594,6 +1609,7 @@ class GlassesSessionCore(
|
||||
|
||||
fun sendShutdown(exitMode: Int): Boolean {
|
||||
if (isIo) return ioSuspend()
|
||||
if (isG1) return g1Suspend()
|
||||
return sendShutdownInternal(exitMode, true)
|
||||
}
|
||||
|
||||
@@ -1604,6 +1620,17 @@ class GlassesSessionCore(
|
||||
* can use the legacy shutdown-and-lease-release path.
|
||||
*/
|
||||
fun sendCfwCleanup(): Boolean {
|
||||
if (isG1) {
|
||||
// Fire and forget: the firmware keeps no session that could outlive the link.
|
||||
monitor.withLock {
|
||||
if (cfwCleanupDelivered) return true
|
||||
shutdownRequested = true
|
||||
cfwCleanupDelivered = true
|
||||
clearPendingMessagesLocked("G1 cleanup")
|
||||
}
|
||||
g1HideBeforeDisconnect()
|
||||
return true
|
||||
}
|
||||
val magic: Int
|
||||
monitor.withLock {
|
||||
if (cfwCleanupDelivered) {
|
||||
@@ -1686,6 +1713,7 @@ class GlassesSessionCore(
|
||||
*/
|
||||
fun suspendEvenHubSession(): Boolean {
|
||||
if (isIo) return ioSuspend()
|
||||
if (isG1) return g1Suspend()
|
||||
// Ending the plugin task releases the fb lease, which frees the
|
||||
// on-glasses resource cache; forget it phone-side either way (a lost
|
||||
// ack may still have taken effect).
|
||||
@@ -1710,6 +1738,7 @@ class GlassesSessionCore(
|
||||
*/
|
||||
fun resumeEvenHubSession(): Boolean {
|
||||
if (isIo) return ioResume()
|
||||
if (isG1) return g1Resume()
|
||||
var claimGeneration = 0
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady || chargingMode) {
|
||||
@@ -1790,7 +1819,7 @@ class GlassesSessionCore(
|
||||
continue
|
||||
}
|
||||
logWarn("Attempting to connect")
|
||||
if (isIo) ioConnectLoopOnce() else connectLoopOnce()
|
||||
if (isIo) ioConnectLoopOnce() else if (isG1) g1ConnectLoopOnce() else connectLoopOnce()
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1826,6 +1855,10 @@ class GlassesSessionCore(
|
||||
ioOnNotification(address, uuid, data)
|
||||
return
|
||||
}
|
||||
if (isG1) {
|
||||
g1OnNotification(address, uuid, data)
|
||||
return
|
||||
}
|
||||
if (BleProtocol.RENDER_NOTIFY_UUID == uuid) {
|
||||
handleRenderNotification(address, data)
|
||||
return
|
||||
|
||||
+553
@@ -0,0 +1,553 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import com.faceclaw.app.GlassesSessionCore.Companion.DASHBOARD_TILE
|
||||
import com.faceclaw.app.GlassesSessionCore.Companion.safeMessage
|
||||
|
||||
/** Bytes of the arm's frame once everything queued for it has run, and what each arm last reported. */
|
||||
internal class G1ArmState {
|
||||
var hello: G1Protocol.Hello? = null
|
||||
var battery = -1
|
||||
var charge = -1
|
||||
var inCase = -1
|
||||
var worn = -1
|
||||
|
||||
/** Null: unknown, so the next update is a full frame. */
|
||||
var frame: ByteArray? = null
|
||||
|
||||
fun resetLink() {
|
||||
hello = null
|
||||
battery = -1
|
||||
charge = -1
|
||||
inCase = -1
|
||||
worn = -1
|
||||
frame = null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Even G1 session state ([GlassesSessionCore.g1]); guarded by the core's monitor.
|
||||
*
|
||||
* A G1 is two independent devices on two BLE links, and neither hears what the other is sent. Each arm has its own
|
||||
* deflate stream (cfwTransports) and frame, each message goes to one arm and only that lens ACKs it, and the phone
|
||||
* composes each lens: the firmware has no display lists, so the shell scene is drawn here in its settled state.
|
||||
* Firmware: scouter_kit's g1v0 (g1_firmware/docs/VM.md §6.11, firmware/src/fc.c).
|
||||
*/
|
||||
internal class G1SessionState {
|
||||
val arms = arrayOf(G1ArmState(), G1ArmState())
|
||||
private var tag = 0
|
||||
val replies = HashMap<Int, G1Protocol.Reply>()
|
||||
var lastPropsAtMs = 0L
|
||||
|
||||
/** The panel may be blank: the next update ends in PRESENT even with no pixels to send. */
|
||||
var presentPending = false
|
||||
|
||||
fun nextTag(): Int {
|
||||
tag = tag % 255 + 1
|
||||
return tag
|
||||
}
|
||||
|
||||
fun resetLink() {
|
||||
for (arm in arms) arm.resetLink()
|
||||
replies.clear()
|
||||
lastPropsAtMs = 0
|
||||
presentPending = true
|
||||
}
|
||||
}
|
||||
|
||||
internal object G1Timings {
|
||||
const val CONTROL_REPLY_MS = 1_500L
|
||||
const val CONTROL_TRIES = 4
|
||||
/** The OS may be showing its pairing dialog. */
|
||||
const val PAIRING_MS = 30_000
|
||||
const val PROPS_MS = 60_000L
|
||||
const val LENS_LEFT = 1
|
||||
const val LENS_RIGHT = 2
|
||||
}
|
||||
|
||||
private val G1_PROPS = intArrayOf(G1Protocol.PROP_BATT_PCT, G1Protocol.PROP_CHARGE, G1Protocol.PROP_IN_CASE, G1Protocol.PROP_WORN)
|
||||
|
||||
internal val GlassesSessionCore.isG1: Boolean get() = glassesType == GlassesType.G1
|
||||
|
||||
internal val GlassesSessionCore.isG2: Boolean get() = glassesType == GlassesType.G2
|
||||
|
||||
/** What the display path takes (fc.c); anything else a shared feature queues never goes out. */
|
||||
internal fun g1SupportsMessage(message: OutboundMessage): Boolean {
|
||||
if (message.sid != CfwTransport.SID || message.message.isEmpty()) return false
|
||||
return when (message.message[0].toInt() and CFW_MSG_TYPE_MASK) {
|
||||
CFW_MSG_DRAW_CALLS, CFW_MSG_PRESENT, CFW_MSG_BRIGHTNESS -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1ArmAddress(arm: Int): String = if (arm == 0) leftAddress else rightAddress
|
||||
|
||||
private fun g1Lens(arm: Int): Int = if (arm == 0) G1Timings.LENS_LEFT else G1Timings.LENS_RIGHT
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Writes
|
||||
|
||||
/** The packets of one queued message, its record from this arm's persistent deflate stream. */
|
||||
internal fun GlassesSessionCore.g1CfwWrites(message: OutboundMessage, mtu: Int): List<ByteArray> {
|
||||
val arm = if (message.isLeftArmMessage) 0 else 1
|
||||
val transport = cfwTransports[arm]
|
||||
if (message.cfwRetries > 0) transport.reset()
|
||||
return G1Protocol.packets(transport.encodeRecord(message.message, g1Lens(arm)), message.magic, mtu)
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1WriteControl(address: String, op: Int, tag: Int, body: ByteArray): Boolean =
|
||||
link.writeFrames(address, G1Ble.CTRL_CHAR_UUID, listOf(G1Protocol.control(op, tag, body)),
|
||||
GattWriteMode.WITHOUT_RESPONSE, ConnectionOptions.WRITE_TIMEOUT_MS)
|
||||
|
||||
/**
|
||||
* A control request and its reply; worker thread, outside the monitor. Writes on a link the arm doesn't yet obey
|
||||
* (out of the case, before encryption) are ignored and control requests can be lost, so it asks again.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1Control(arm: Int, op: Int, body: ByteArray = ByteArray(0)): G1Protocol.Reply {
|
||||
val address = g1ArmAddress(arm)
|
||||
repeat(G1Timings.CONTROL_TRIES) {
|
||||
val tag = monitor.withLock { g1.nextTag() }
|
||||
val key = arm * 256 + tag
|
||||
if (!g1WriteControl(address, op, tag, body)) throw IllegalStateException("control $op write failed")
|
||||
val deadline = now() + G1Timings.CONTROL_REPLY_MS
|
||||
monitor.withLock {
|
||||
while (true) {
|
||||
g1.replies.remove(key)?.let { return it }
|
||||
if (!running || userDisconnectRequested) throw IllegalStateException("stopped while waiting for control $op")
|
||||
if (!(if (arm == 0) leftConnected else rightConnected)) throw IllegalStateException("link lost waiting for control $op")
|
||||
val remaining = deadline - now()
|
||||
if (remaining <= 0) break
|
||||
monitor.awaitMs(minOf(remaining, 100L))
|
||||
}
|
||||
}
|
||||
}
|
||||
throw IllegalStateException("no reply to control $op from the " + (if (arm == 0) "left" else "right") + " arm")
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1RequestProps(arm: Int) {
|
||||
val tag = monitor.withLock { g1.nextTag() }
|
||||
g1WriteControl(g1ArmAddress(arm), G1Protocol.OP_PROPS, tag, ByteArray(G1_PROPS.size) { G1_PROPS[it].toByte() })
|
||||
}
|
||||
|
||||
/** Nothing here keeps a lease; this only refreshes the battery. Worker thread, outside the monitor. */
|
||||
internal fun GlassesSessionCore.g1Maintain() {
|
||||
val due = monitor.withLock {
|
||||
val now = now()
|
||||
val due = sessionReady && now - g1.lastPropsAtMs >= G1Timings.PROPS_MS
|
||||
if (due) g1.lastPropsAtMs = now
|
||||
due
|
||||
}
|
||||
if (due) {
|
||||
g1RequestProps(0)
|
||||
g1RequestProps(1)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Frames (under the monitor, from enqueueDesiredImageLocked)
|
||||
|
||||
/** Whatever was queued is gone, so what the arms' frames will hold is unknown. */
|
||||
internal fun GlassesSessionCore.g1ForgetFramesLocked() {
|
||||
if (!isG1) return
|
||||
for (arm in g1.arms) arm.frame = null
|
||||
g1.presentPending = true
|
||||
for (transport in cfwTransports) transport.reset()
|
||||
}
|
||||
|
||||
/**
|
||||
* Compose each lens in the frame's settled state, send what changed against the arm's frame, and end with PRESENT.
|
||||
* The two arms' messages alternate so that neither eye runs far ahead, and the PRESENTs go out last and side by side.
|
||||
* A blanked frame sends nothing: the panel waits in standby with the last picture in the arm's frame, and the next
|
||||
* visible frame is a delta against it.
|
||||
*/
|
||||
internal fun GlassesSessionCore.g1EnqueueFrameLocked(
|
||||
packed: ByteArray, width: Int, height: Int, scene: ShellScene, fingerprint: String, paintMs: Int, frameId: Int,
|
||||
) {
|
||||
if (fingerprint.startsWith("blanked:")) {
|
||||
lastEnqueuedFingerprint = fingerprint
|
||||
displayedFingerprint = fingerprint
|
||||
finishFrame(frameId, "blanked: the panel is in standby")
|
||||
return
|
||||
}
|
||||
frameTimings.spanStart(frameId, "compose-lenses")
|
||||
val updates: List<List<ByteArray>>
|
||||
val composed: Array<ByteArray>
|
||||
try {
|
||||
val left = scene.composed(packed, width, height, false)
|
||||
val right = if (scene.hasStereoDepth) scene.composed(packed, width, height, true) else left
|
||||
composed = arrayOf(left, right)
|
||||
updates = List(2) { arm -> G1FramePlanner.plan(g1.arms[arm].frame, composed[arm], width, height) }
|
||||
} catch (e: Exception) {
|
||||
// As in the G2's path: this frame would fail the same way after a reconnect (#42), so drop it.
|
||||
lastEnqueuedFingerprint = fingerprint
|
||||
logLine("frame plan failed; frame dropped: " + safeMessage(e))
|
||||
finishFrame(frameId, "discarded: frame plan failed")
|
||||
return
|
||||
}
|
||||
frameTimings.spanEnd(frameId, "compose-lenses")
|
||||
val force = g1.presentPending
|
||||
val queues = List(2) { arm ->
|
||||
ArrayDeque(updates[arm]).also { if (it.isNotEmpty() || force) it.addLast(byteArrayOf(CFW_MSG_PRESENT.toByte())) }
|
||||
}
|
||||
g1.presentPending = false
|
||||
for (arm in 0..1) g1.arms[arm].frame = composed[arm]
|
||||
lastEnqueuedFingerprint = fingerprint
|
||||
val messageCount = queues.sumOf { it.size }
|
||||
if (messageCount == 0) {
|
||||
displayedFingerprint = fingerprint
|
||||
finishFrame(frameId, "no change on either lens")
|
||||
return
|
||||
}
|
||||
val updateId = nextImageUpdateId++
|
||||
imageUpdateStats[updateId] = BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId)
|
||||
var number = 0
|
||||
var payloadBytes = 0
|
||||
var arm = 0
|
||||
while (queues[0].isNotEmpty() || queues[1].isNotEmpty()) {
|
||||
// The other arm's turn once this one is down to its PRESENT, so that both PRESENTs come last.
|
||||
val other = 1 - arm
|
||||
val waiting = queues[arm].size == 1 && queues[other].size > 1
|
||||
if (queues[arm].isEmpty() || waiting) {
|
||||
arm = other
|
||||
continue
|
||||
}
|
||||
val payload = queues[arm].removeFirst()
|
||||
payloadBytes += payload.size
|
||||
number++
|
||||
val plan = BleImageOptimizer.TileImagePlan(arm, DASHBOARD_TILE, composed[arm], width, height, nextMapSessionId(), payload)
|
||||
enqueueCustomImageLocked(plan, payload, fingerprint, updateId, number, messageCount, arm == 0, g1Lens(arm))
|
||||
arm = other
|
||||
}
|
||||
frameTimings.log(frameId, "queued image update#$updateId messages=$messageCount payload=${payloadBytes}B")
|
||||
logLine("queue image update#$updateId fingerprint=$fingerprint messages=$messageCount")
|
||||
}
|
||||
|
||||
/** Message 30 for both arms, its visibility ordered with the frames around it. */
|
||||
internal fun GlassesSessionCore.g1EnqueueBrightnessLocked(payload: ByteArray, label: String, visibleChanged: Boolean, onTimeout: MessageCallback) {
|
||||
for (arm in 0..1) {
|
||||
val message = messageBuilder.customMessage("brightness-output", payload, label, -1, arm == 0)
|
||||
message.cfwRequiredLenses = g1Lens(arm)
|
||||
message.onTimeout = onTimeout
|
||||
pendingMessages.addLast(message)
|
||||
}
|
||||
if (visibleChanged) g1.presentPending = true
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1HidePayload(): ByteArray =
|
||||
byteArrayOf(CFW_MSG_BRIGHTNESS.toByte(), 1, brightnessPolicy.target.coerceIn(0, 100).toByte(), 0, 0, 0)
|
||||
|
||||
/** Stop drawing, keeping the links and the arms' frames: the panels wait in standby (the EvenHub suspend analogue). */
|
||||
internal fun GlassesSessionCore.g1Suspend(): Boolean {
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady) return false
|
||||
val idle = pendingMessages.isEmpty() && inFlightMessages.isEmpty()
|
||||
val frames = g1.arms.map { it.frame }
|
||||
shutdownRequested = true
|
||||
clearAllMessagesLocked("G1 suspend")
|
||||
// Nothing was lost, so the arms still hold what the phone thinks and a wake is a delta.
|
||||
if (idle) for (arm in 0..1) g1.arms[arm].frame = frames[arm]
|
||||
g1EnqueueBrightnessLocked(g1HidePayload(), "suspend: hide", true) { }
|
||||
brightnessSentVisible = false
|
||||
displayedFingerprint = ""
|
||||
}
|
||||
interruptibleSleep.interrupt()
|
||||
return true
|
||||
}
|
||||
|
||||
/** The drive loop resends the desired frame; its PRESENT brings the panels back. */
|
||||
internal fun GlassesSessionCore.g1Resume(): Boolean {
|
||||
monitor.withLock {
|
||||
if (!running || !sessionReady) return false
|
||||
shutdownRequested = false
|
||||
displayedFingerprint = ""
|
||||
lastEnqueuedFingerprint = ""
|
||||
imageRetryAfterMs = 0
|
||||
g1.presentPending = true
|
||||
}
|
||||
interruptibleSleep.interrupt()
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* At the end of a session: the panels off now rather than 30 s from now, when the firmware's own rule would do it.
|
||||
* A raw record that resets the stream, not waited on; nothing follows it.
|
||||
*/
|
||||
internal fun GlassesSessionCore.g1HideBeforeDisconnect() {
|
||||
val message = g1HidePayload()
|
||||
val record = CfwTransport.record(message, CfwTransport.crc(message), CfwTransport.BOTH or CfwTransport.RESET_CONTEXT)
|
||||
for (arm in 0..1) {
|
||||
val address = g1ArmAddress(arm)
|
||||
if (!link.isConnected(address)) continue
|
||||
try {
|
||||
link.writeFrames(address, G1Ble.FC_CHAR_UUID, G1Protocol.packets(record, 0, link.negotiatedMtu(address)),
|
||||
GattWriteMode.WITHOUT_RESPONSE, ConnectionOptions.WRITE_TIMEOUT_MS)
|
||||
} catch (t: Throwable) {
|
||||
logLine("G1 hide failed: " + safeMessage(t))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Connect (worker thread)
|
||||
|
||||
private fun GlassesSessionCore.g1ConnectArm(arm: Int, deadlineMs: Long) {
|
||||
val address = g1ArmAddress(arm)
|
||||
val timeoutMs = maxOf(deadlineMs - now(), ConnectionOptions.CONNECT_TIMEOUT_MS.toLong())
|
||||
if (!link.connect(address, timeoutMs.toInt())) {
|
||||
throw ArmUnreachableException(if (arm == 0) "left" else "right", address)
|
||||
}
|
||||
link.requestHighPriority(address)
|
||||
link.requestMtu(address, G1Ble.REQUESTED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS)
|
||||
if (!link.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) {
|
||||
throw IllegalStateException("discoverServices failed: $address")
|
||||
}
|
||||
if (!link.enableNotifications(address, G1Ble.EVT_CHAR_UUID, true, ConnectionOptions.DESCRIPTOR_TIMEOUT_MS)) {
|
||||
throw IllegalStateException("no event characteristic on $address: not g1v0 firmware?")
|
||||
}
|
||||
link.requestLe2mPhy(address)
|
||||
monitor.withLock {
|
||||
if (arm == 0) leftConnected = true else rightConnected = true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A write with response to PAIR needs an encrypted link. A bonded arm gets one at once; an arm in the case bonds here,
|
||||
* through whatever the OS does (Android's dialog, iOS's); out of the case the arm refuses a new bond and the write fails.
|
||||
* Either way this is where the link is known to be encrypted, so the arm obeys everything that follows.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1Pair(arm: Int) {
|
||||
val address = g1ArmAddress(arm)
|
||||
if (!link.isBonded(address)) {
|
||||
logLine("G1 " + (if (arm == 0) "left" else "right") + " arm: asking the OS to pair")
|
||||
setStateDisplay("connecting", "Pairing with the glasses...")
|
||||
link.requestBond(address)
|
||||
val deadline = now() + G1Timings.PAIRING_MS
|
||||
while (!link.isBonded(address) && now() < deadline) {
|
||||
interruptibleSleep.sleep(200)
|
||||
if (!monitor.withLock { running && !userDisconnectRequested }) throw IllegalStateException("stopped while pairing")
|
||||
}
|
||||
}
|
||||
if (!link.writeFrames(address, G1Ble.PAIR_CHAR_UUID, listOf(byteArrayOf(1)), GattWriteMode.WITH_RESPONSE, G1Timings.PAIRING_MS)) {
|
||||
throw IllegalStateException("no encrypted link to the " + (if (arm == 0) "left" else "right") + " arm")
|
||||
}
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.g1ConnectLoopOnce() {
|
||||
monitor.withLock {
|
||||
wearState = -1
|
||||
g1.resetLink()
|
||||
}
|
||||
setStateDisplay("connecting", "Connecting to the glasses...")
|
||||
ConnectionCounters.increment("g1.connect-attempt")
|
||||
try {
|
||||
val deadlineMs = now() + ConnectionOptions.ARM_CONNECT_WINDOW_MS
|
||||
link.beginConnect(rightAddress, false)
|
||||
link.beginConnect(leftAddress, false)
|
||||
// The left first, as the arms were installed; each stands alone.
|
||||
g1ConnectArm(0, deadlineMs)
|
||||
g1ConnectArm(1, deadlineMs)
|
||||
g1Pair(0)
|
||||
g1Pair(1)
|
||||
|
||||
val versions = arrayOf("", "")
|
||||
var compatible = true
|
||||
for (arm in 0..1) {
|
||||
val reply = g1Control(arm, G1Protocol.OP_HELLO)
|
||||
val hello = G1Protocol.parseHello(reply.body) ?: throw IllegalStateException("bad HELLO from arm $arm")
|
||||
monitor.withLock { g1.arms[arm].hello = hello }
|
||||
versions[arm] = "g1v0 " + hello.version + (if (hello.safeMode) " (safe mode)" else "")
|
||||
if (hello.leftArm != (arm == 0)) {
|
||||
logWarn("G1: the arm paired as " + (if (arm == 0) "left" else "right") + " says it is the other one; its lens gets the other eye's frame")
|
||||
}
|
||||
val info = g1Control(arm, G1Protocol.OP_FC_INFO)
|
||||
val fcVersion = if (info.status == 0) G1Protocol.parseFcVersion(info.body) else -1
|
||||
logLine("G1 " + (if (arm == 0) "left" else "right") + " arm: " + versions[arm]
|
||||
+ " display path " + fcVersion + " encrypted=" + hello.encrypted + " inCase=" + hello.inCase
|
||||
+ " panelOk=" + hello.panelOk + " phy=" + link.describePhy(g1ArmAddress(arm)))
|
||||
compatible = compatible && fcVersion >= 1
|
||||
}
|
||||
// The firmware's own number, not the G2 custom firmware's: the path has one version so far, and TS compares this.
|
||||
val firmware = BleProtocol.FirmwareInfo(versions[0], versions[1],
|
||||
if (compatible) BleProtocol.FirmwareInfo.FACECLAW_EXTENSION_PREFIX + requiredFirmwareRevision else "")
|
||||
emitFirmwareInfo(firmware)
|
||||
if (!compatible) {
|
||||
monitor.withLock { incompatibleFirmware = firmware }
|
||||
return
|
||||
}
|
||||
for (arm in 0..1) {
|
||||
val events = g1Control(arm, G1Protocol.OP_EVENTS, G1Protocol.eventsRequest(G1Protocol.EVENTS_MASK))
|
||||
if (events.status != 0) throw IllegalStateException("EVENTS refused: " + events.status)
|
||||
// The arm counts a boot healthy only after a host has said so (VM.md §6.4, safe mode).
|
||||
g1Control(arm, G1Protocol.OP_HEALTH_OK)
|
||||
}
|
||||
monitor.withLock {
|
||||
sessionReady = true
|
||||
shutdownRequested = false
|
||||
customFirmwareDetected = true
|
||||
clearAllMessagesLocked("session ready")
|
||||
fixedLayoutCreated = true
|
||||
displayedFingerprint = ""
|
||||
lastAckAtMs = now()
|
||||
lastIncomingAtMs = lastAckAtMs
|
||||
lastConnectionOrInputAtMs = lastAckAtMs
|
||||
lastSessionReadyAtMs = lastAckAtMs
|
||||
imageRetryAfterMs = 0
|
||||
consecutiveAckTimeouts = 0
|
||||
cfwCleanupDelivered = false
|
||||
lastCfwCleanupAckMagic = 0
|
||||
g1.lastPropsAtMs = 0
|
||||
monitor.signalAll()
|
||||
}
|
||||
setStateDisplay("connected", "Connected.")
|
||||
interruptibleSleep.interrupt()
|
||||
} catch (t: Throwable) {
|
||||
logLine("G1 connect failed: " + safeMessage(t))
|
||||
val unpaired = firstUnpairedArm()
|
||||
if (unpaired != null) {
|
||||
logError("G1 connect failed and $unpaired is not paired; suspending reconnect")
|
||||
ConnectionCounters.increment("g1.unpaired")
|
||||
monitor.withLock { haltReconnectLocked("arm not paired: $unpaired") }
|
||||
if (!userDisconnectRequested) {
|
||||
setStateDisplay("unpaired", "The glasses are not paired with this phone. Put them in their charging case"
|
||||
+ " with the lid open, then connect: they only pair in the case.")
|
||||
}
|
||||
interruptibleSleep.interrupt()
|
||||
} else if (t is ArmUnreachableException) {
|
||||
val other = if (t.side == "left") rightAddress else leftAddress
|
||||
if (link.isConnected(other)) {
|
||||
handleTransportFailure(t.side + " arm not found", "Can't reach the " + t.side + " arm of the glasses."
|
||||
+ " It may be connected to another device, or be off or out of range.")
|
||||
} else {
|
||||
handleTransportFailure("glasses not found", "Can't find the glasses. They may be off or out of range.")
|
||||
}
|
||||
} else {
|
||||
handleTransportFailure("connect failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Inbound (BLE callback thread)
|
||||
|
||||
internal fun GlassesSessionCore.g1OnNotification(address: String, characteristicUuid: String, data: ByteArray) {
|
||||
if (G1Ble.EVT_CHAR_UUID != characteristicUuid) return
|
||||
val arm = armIndex(address)
|
||||
if (arm < 0) return
|
||||
when (val type = G1Protocol.eventType(data)) {
|
||||
G1Protocol.EV_FC_ACK -> {
|
||||
val ack = G1Protocol.parseFcAck(data) ?: return
|
||||
monitor.withLock { acceptCfwAcksLocked(address, arrayOf(ack), 0, data) }
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
G1Protocol.EV_INPUT -> g1HandleInput(arm, G1Protocol.parseInput(data) ?: return)
|
||||
else -> if (type >= 0x80) {
|
||||
val reply = G1Protocol.parseReply(data) ?: return
|
||||
if (reply.op == G1Protocol.OP_PROPS) {
|
||||
if (reply.status == 0) G1Protocol.parseProps(reply.body, G1_PROPS.size)?.let { g1HandleProps(arm, it) }
|
||||
return
|
||||
}
|
||||
monitor.withLock {
|
||||
lastIncomingAtMs = now()
|
||||
g1.replies[arm * 256 + reply.tag] = reply
|
||||
monitor.signalAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1HandleInput(arm: Int, input: G1Protocol.Input) {
|
||||
when (input.trigger) {
|
||||
G1Protocol.TRIGGER_TOUCH -> g1HandleGesture(arm, input.value)
|
||||
G1Protocol.TRIGGER_BATTERY -> g1HandleProps(arm, intArrayOf(input.value, G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, G1Protocol.UNKNOWN), true)
|
||||
G1Protocol.TRIGGER_WEAR -> g1HandleProps(arm, intArrayOf(G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, input.value))
|
||||
G1Protocol.TRIGGER_CASE -> g1HandleProps(arm, intArrayOf(G1Protocol.UNKNOWN, G1Protocol.UNKNOWN, input.value and 1, G1Protocol.UNKNOWN))
|
||||
}
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1HandleGesture(arm: Int, gesture: Int) {
|
||||
val leftArm = arm == 0
|
||||
val dark = monitor.withLock { shutdownRequested || brightnessSentVisible == false }
|
||||
val eventType = G1Protocol.gestureEventType(gesture, leftArm, dark)
|
||||
monitor.withLock {
|
||||
lastIncomingAtMs = now()
|
||||
if (eventType != null) lastConnectionOrInputAtMs = lastIncomingAtMs
|
||||
}
|
||||
logLine("G1 gesture " + gesture + " on the " + (if (leftArm) "left" else "right") + " arm" + (if (eventType == null) " (ignored)" else ""))
|
||||
if (eventType == null) return
|
||||
val source = if (leftArm) BleProtocol.EVENT_SOURCE_GLASSES_L else BleProtocol.EVENT_SOURCE_GLASSES_R
|
||||
// As the G2's firmware reports its own double tap on a dark display: it can only wake, whatever the delay.
|
||||
val event = if (dark && gesture == G1Protocol.GESTURE_DOUBLE_TAP) G2Event("display-wake", "", eventType, 0, 0)
|
||||
else G2Event("sys-event", "", eventType, source, 0)
|
||||
val frameId = frameTimings.startFrame("input:" + event.kind + " type=$eventType src=$source")
|
||||
frameTimings.log(frameId, "input event decoded from G1 notification")
|
||||
emitRingEvent(event, frameId)
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
|
||||
/**
|
||||
* New values in the order of [G1_PROPS], [G1Protocol.UNKNOWN] for those it doesn't say. The pair's battery is the lower
|
||||
* arm's, the pair is charging if either arm is, worn if either arm says so, and in the case when both are.
|
||||
* [batteryChanged]: a BATTERY event, which may also mean CHARGE changed, so that is read again.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1HandleProps(arm: Int, values: IntArray, batteryChanged: Boolean = false) {
|
||||
var batteryEmit = false
|
||||
var wearEmit = -1
|
||||
var putOn = false
|
||||
val state = g1.arms[arm]
|
||||
monitor.withLock {
|
||||
lastIncomingAtMs = now()
|
||||
if (values[0] != G1Protocol.UNKNOWN) state.battery = values[0]
|
||||
if (values[1] != G1Protocol.UNKNOWN) state.charge = values[1]
|
||||
if (values[2] != G1Protocol.UNKNOWN) state.inCase = values[2]
|
||||
if (values[3] != G1Protocol.UNKNOWN) state.worn = values[3]
|
||||
|
||||
val batteries = g1.arms.map { it.battery }.filter { it >= 0 }
|
||||
val battery = if (batteries.isEmpty()) -1 else batteries.min()
|
||||
val charging = if (g1.arms.any { it.charge == G1Protocol.CHARGE_CHARGING }) 1 else if (g1.arms.all { it.charge < 0 }) -1 else 0
|
||||
if (battery != headsetBattery || charging != headsetCharging) {
|
||||
headsetBattery = battery
|
||||
headsetCharging = charging
|
||||
batteryEmit = true
|
||||
}
|
||||
val worn = if (g1.arms.any { it.worn == 1 }) 1 else if (g1.arms.all { it.worn < 0 }) -1 else 0
|
||||
if (worn >= 0 && worn != wearState) {
|
||||
putOn = chargingMode && wearState == 0 && worn == 1
|
||||
wearState = worn
|
||||
wearEmit = worn
|
||||
}
|
||||
if (sessionReady) {
|
||||
val inCase = g1.arms.all { it.inCase == 1 }
|
||||
if (putOn) {
|
||||
endChargingModeLocked("glasses put on")
|
||||
} else if (inCase != chargingMode && g1.arms.all { it.inCase >= 0 }) {
|
||||
g1SetInCaseLocked(inCase, battery)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (batteryEmit) emitBatteryState(headsetBattery, headsetCharging)
|
||||
if (wearEmit >= 0) emitWearState(wearEmit == 1)
|
||||
if (batteryChanged) g1RequestPropsLater()
|
||||
interruptibleSleep.interrupt()
|
||||
}
|
||||
|
||||
private fun GlassesSessionCore.g1RequestPropsLater() {
|
||||
monitor.withLock { g1.lastPropsAtMs = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* In the case nobody can see the panels, and lit pixels disturb the case's charging link: they go dark and frames stop.
|
||||
* Leaving the case reconnects, as on a G2, which also picks up a firmware the arms got meanwhile.
|
||||
*/
|
||||
private fun GlassesSessionCore.g1SetInCaseLocked(inCase: Boolean, battery: Int) {
|
||||
if (inCase) {
|
||||
chargingMode = true
|
||||
clearAllMessagesLocked("glasses in the case")
|
||||
fixedLayoutCreated = false
|
||||
displayedFingerprint = ""
|
||||
g1EnqueueBrightnessLocked(g1HidePayload(), "in the case: hide", true) { }
|
||||
finishDesiredFrameLocked("discarded: glasses in the case")
|
||||
logLine("glasses are in the case; pausing display communication")
|
||||
setStateDisplay("charging", GlassesSessionCore.chargingStatusText(battery))
|
||||
} else {
|
||||
endChargingModeLocked("glasses out of the case")
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -5,7 +5,7 @@ import com.faceclaw.app.GlassesSessionCore.Companion.safeMessage
|
||||
import kotlin.random.Random
|
||||
|
||||
/** Which glasses a session drives. Set before [GlassesSessionCore.start]; see [GlassesSessionCore.useRayNeoIo]. */
|
||||
enum class GlassesType { G2, RAYNEO_IO }
|
||||
enum class GlassesType { G2, RAYNEO_IO, G1 }
|
||||
|
||||
/**
|
||||
* RayNeo iO session state ([GlassesSessionCore.io]); guarded by the core's monitor.
|
||||
@@ -210,9 +210,9 @@ internal fun GlassesSessionCore.ioCfwWrites(message: OutboundMessage, mtu: Int):
|
||||
internal fun GlassesSessionCore.ioDropUnsupported(message: OutboundMessage) {
|
||||
monitor.withLock {
|
||||
inFlightMessages.remove(message)
|
||||
if (message.magic != 0) magicPool.release(message.sid, message.magic, message.label, "unsupported on RayNeo iO")
|
||||
if (message.magic != 0) magicPool.release(message.sid, message.magic, message.label, "unsupported on these glasses")
|
||||
}
|
||||
logDebug("RayNeo iO: dropped unsupported message " + message.label + " sid=" + message.sid)
|
||||
logDebug("dropped a message these glasses don't take: " + message.label + " sid=" + message.sid)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
||||
+5
-5
@@ -971,11 +971,11 @@ internal fun macWireBytes(address: String): ByteArray? {
|
||||
internal fun GlassesSessionCore.queueGlassesRingLinkCommandLocked(
|
||||
action: String, fallbackAddress: String, fallbackName: String,
|
||||
): Boolean {
|
||||
if (isIo) {
|
||||
// A RayNeo iO has no ring link of its own; the phone holds the ring directly.
|
||||
lastRingLinkCommand = "$action: not applicable (RayNeo iO)"
|
||||
if (!isG2) {
|
||||
// A RayNeo iO or a G1 has no ring link of its own; the phone holds the ring directly.
|
||||
lastRingLinkCommand = "$action: not applicable ($glassesType)"
|
||||
lastRingLinkCommandAtMs = now()
|
||||
logLine("glasses ring $action skipped: RayNeo iO glasses have no ring link")
|
||||
logLine("glasses ring $action skipped: $glassesType glasses have no ring link")
|
||||
return false
|
||||
}
|
||||
val reported = glassesRingMac
|
||||
@@ -1100,7 +1100,7 @@ internal fun GlassesSessionCore.queueRingConfigCommandLocked(
|
||||
"targets-glasses" -> {
|
||||
// The iO never connects to the ring: naming it would make the ring drop
|
||||
// the phone's glasses-role link (the advStart check, ~20 s later).
|
||||
if (isIo) return "not sent: RayNeo iO glasses don't hold the ring"
|
||||
if (!isG2) return "not sent: these glasses don't hold the ring"
|
||||
val right = macWireBytes(rightAddress)
|
||||
val left = macWireBytes(leftAddress)
|
||||
if (right == null || left == null) return "not sent: glasses addresses unparseable"
|
||||
|
||||
+32
-13
@@ -45,6 +45,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
}
|
||||
}
|
||||
if (isIo) ioMaintain()
|
||||
if (isG1) g1Maintain()
|
||||
var messageToWrite: OutboundMessage? = null
|
||||
var messageToPrewrite: OutboundMessage? = null
|
||||
var heartbeatToWrite: OutboundMessage? = null
|
||||
@@ -75,7 +76,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
clearPendingMessagesLocked("after CFW cleanup")
|
||||
return 250
|
||||
}
|
||||
if (sessionReady && !isIo
|
||||
if (sessionReady && isG2
|
||||
&& now - lastFaceclawFramebufferLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS
|
||||
&& !hasPendingOrInflightKindLocked("framebuffer-lease-control")) {
|
||||
enqueueFaceclawFramebufferControlLocked(
|
||||
@@ -84,7 +85,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
)
|
||||
}
|
||||
if (faceclawWakeLeaseEnabled
|
||||
&& sessionReady && !isIo
|
||||
&& sessionReady && isG2
|
||||
&& now - lastFaceclawWakeLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS
|
||||
&& !hasPendingOrInflightKindLocked("wake-lease-control")) {
|
||||
enqueueFaceclawWakeControlLocked(
|
||||
@@ -159,7 +160,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
}
|
||||
} else {
|
||||
if (messageToPrewrite == null
|
||||
&& !isIo // the iO's page lease is ioMaintain's
|
||||
&& isG2 // the iO's page lease is ioMaintain's; a G1 has none
|
||||
&& !shutdownRequested
|
||||
&& !fixedLayoutCreated
|
||||
&& pendingMessages.isEmpty()
|
||||
@@ -170,14 +171,14 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
// Prewrite outside the lock; the logical message remains pending until
|
||||
// its final BLE frame is sent after the current protocol ACK.
|
||||
}
|
||||
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
|
||||
if (messageToPrewrite == null && isG2 && !shutdownRequested && fixedLayoutCreated
|
||||
&& (if (firmwareDebugFlagsEnabled) 2 else 1) != firmwareDebugFlagsLastSent
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
logLine("enqueueing firmware debug flags " + (if (firmwareDebugFlagsEnabled) "show" else "hide"))
|
||||
enqueueFirmwareDebugFlagsLocked()
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
|
||||
if (messageToPrewrite == null && isG2 && !shutdownRequested && fixedLayoutCreated
|
||||
&& caseInfoReadPending
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
caseInfoReadPending = false
|
||||
@@ -185,7 +186,7 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
pendingMessages.addLast(messageBuilder.caseInfoQuery())
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && !isIo && !shutdownRequested && fixedLayoutCreated
|
||||
if (messageToPrewrite == null && isG2 && !shutdownRequested && fixedLayoutCreated
|
||||
&& (if (compassOwners.isEmpty()) 0 else 1) != compassControlLastSent
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
val wanted = compassOwners.isNotEmpty()
|
||||
@@ -196,7 +197,9 @@ internal fun GlassesSessionCore.driveSession(): Long {
|
||||
if (benchmarkActive) {
|
||||
maintainBenchmarkLocked(now)
|
||||
}
|
||||
if (!benchmarkActive && (!isIo || ioFirmwareOwnsBrightness)) maintainBrightnessLocked(now)
|
||||
// A G1's brightness follows the policy's target; its light sensor isn't wired up yet.
|
||||
if (!benchmarkActive && isG1) enqueueBrightnessLocked()
|
||||
else if (!benchmarkActive && (!isIo || ioFirmwareOwnsBrightness)) maintainBrightnessLocked(now)
|
||||
|
||||
// Up to WINDOW_SIZE messages may be in flight at once (full
|
||||
// pipelining); a slot frees when an ack arrives. An active
|
||||
@@ -339,7 +342,7 @@ internal enum class HeartbeatStep {
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.heartbeatStepLocked(imageWaiting: Boolean): HeartbeatStep {
|
||||
if (isIo) return HeartbeatStep.NONE
|
||||
if (!isG2) return HeartbeatStep.NONE
|
||||
val now = now()
|
||||
val heartbeatEligible = !shutdownRequested && fixedLayoutCreated
|
||||
val heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat")
|
||||
@@ -390,6 +393,11 @@ internal fun GlassesSessionCore.createHeartbeatMessage(): OutboundMessage {
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean {
|
||||
if (isG1 && !g1SupportsMessage(message)) {
|
||||
// G2-only traffic a shared feature queued anyway: it would only wait for an ACK that never comes.
|
||||
ioDropUnsupported(message)
|
||||
return true
|
||||
}
|
||||
if (isIo && !ioSupportsMessage(message)) {
|
||||
// G2-only traffic (EvenHub/settings frames, the G2 hardware CFW modes) that a feature
|
||||
// queued anyway: the iO would NACK it into the replay limit, so it never goes out.
|
||||
@@ -427,6 +435,8 @@ internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean
|
||||
prewrittenFrames = emptyList()
|
||||
} else if (isIo) {
|
||||
frames = ioCfwWrites(message, link.negotiatedMtu(writeAddress))
|
||||
} else if (isG1) {
|
||||
frames = g1CfwWrites(message, link.negotiatedMtu(writeAddress))
|
||||
} else if (message.sid == CfwTransport.SID) {
|
||||
if (message.cfwRetries > 0) cfwTransports[if (message.isLeftArmMessage) 0 else 1].reset()
|
||||
frames = cfwTransports[if (message.isLeftArmMessage) 0 else 1].encode(
|
||||
@@ -442,9 +452,9 @@ internal fun GlassesSessionCore.writeMessage(message: OutboundMessage): Boolean
|
||||
}
|
||||
val result = link.writeFrames(
|
||||
writeAddress,
|
||||
if (isIo) RayNeoIoBle.WRITE_CHAR_UUID else BleProtocol.WRITE_CHAR_UUID,
|
||||
if (isIo) RayNeoIoBle.WRITE_CHAR_UUID else if (isG1) G1Ble.FC_CHAR_UUID else BleProtocol.WRITE_CHAR_UUID,
|
||||
frames,
|
||||
if (isIo) GattWriteMode.WITHOUT_RESPONSE else ConnectionOptions.WRITE_MODE,
|
||||
if (isIo || isG1) GattWriteMode.WITHOUT_RESPONSE else ConnectionOptions.WRITE_MODE,
|
||||
ConnectionOptions.WRITE_TIMEOUT_MS
|
||||
)
|
||||
|
||||
@@ -847,6 +857,10 @@ internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
frameTimings.spanEnd(frameId, "pack-4bpp")
|
||||
// Visibility belongs to this immutable composite, not the latest UI request.
|
||||
enqueueBrightnessLocked(!fingerprint.startsWith("blanked:"))
|
||||
if (isG1 && packedFrame.size > 0) {
|
||||
g1EnqueueFrameLocked(packedFrame, width, height, scene, fingerprint, paintMs, frameId)
|
||||
return
|
||||
}
|
||||
if (customFirmwareDetected && packedFrame.size > 0) {
|
||||
val rendered = try {
|
||||
scenePlanner.plan(packedFrame, width, height, draws, scene, nextImageFrameId,
|
||||
@@ -992,10 +1006,13 @@ internal fun GlassesSessionCore.enqueueCustomImageLocked(
|
||||
fingerprint: String,
|
||||
updateId: Int,
|
||||
messageNumber: Int,
|
||||
messageCount: Int
|
||||
messageCount: Int,
|
||||
leftArm: Boolean = connectionOptions.sendImagesToLeft,
|
||||
requiredLenses: Int = CfwTransport.BOTH,
|
||||
) {
|
||||
val message = messageBuilder.customMessage("image", payload,
|
||||
"image " + plan.tile.name + "#" + messageNumber, plan.tileIndex, connectionOptions.sendImagesToLeft)
|
||||
"image " + plan.tile.name + "#" + messageNumber, plan.tileIndex, leftArm)
|
||||
message.cfwRequiredLenses = requiredLenses
|
||||
message.setImageUpdatePosition(updateId, messageNumber, messageCount)
|
||||
message.onAck = MessageCallback {
|
||||
imageRetryAfterMs = 0
|
||||
@@ -1048,7 +1065,7 @@ internal fun GlassesSessionCore.createAudioControlMessageLocked(enable: Boolean)
|
||||
}
|
||||
|
||||
internal fun GlassesSessionCore.shouldPollBatteryLocked(now: Long): Boolean {
|
||||
return !isIo // the iO's status polls are ioMaintain's
|
||||
return isG2 // the others' status polls are their own maintain's
|
||||
&& !shutdownRequested
|
||||
&& sessionReady
|
||||
&& pendingMessages.isEmpty()
|
||||
@@ -1356,6 +1373,7 @@ internal fun GlassesSessionCore.clearMessagesOfKindLocked(kind: String) {
|
||||
// drop the pipelined delta base so the next image is a full keyframe.
|
||||
lastEnqueuedPacked = ByteArray(0)
|
||||
lastEnqueuedFingerprint = ""
|
||||
g1ForgetFramesLocked()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1370,6 +1388,7 @@ internal fun GlassesSessionCore.clearAllMessagesLocked(reason: String) {
|
||||
// image is a full keyframe rather than a delta onto a stale base.
|
||||
lastEnqueuedPacked = ByteArray(0)
|
||||
lastEnqueuedFingerprint = ""
|
||||
g1ForgetFramesLocked()
|
||||
// Queued resource commands (if any) were dropped with the rest, and the
|
||||
// session churn behind a full clear may have freed the on-glasses
|
||||
// cache; forget it phone-side so glyphs re-upload lazily.
|
||||
|
||||
+3
@@ -68,6 +68,9 @@ interface SessionLink {
|
||||
/** Whether the OS still holds a pairing bond for the address (iOS: always true). */
|
||||
fun isBonded(address: String): Boolean
|
||||
|
||||
/** Ask the OS to bond with a connected device, without waiting. A no-op where it pairs on demand only (CoreBluetooth). */
|
||||
fun requestBond(address: String) {}
|
||||
|
||||
/** Bandwidth-benchmark link tuning; 0: current link, 1: HIGH priority, 2: 2M PHY, 3: both. */
|
||||
fun prepareBenchmarkLink(address: String, mode: Int)
|
||||
|
||||
|
||||
@@ -64,6 +64,21 @@ class ShellScene(val layers: List<Layer>, val selections: List<RetainedDrawing>
|
||||
return output
|
||||
}
|
||||
|
||||
/** Whether the lenses differ: stereo depth somewhere in the scene. */
|
||||
val hasStereoDepth: Boolean get() = screenDepth != 0 || layers.any { it.depth != 0 } || allSelections.isNotEmpty()
|
||||
|
||||
/**
|
||||
* What one lens shows of [screen] (packed 4bpp) with this scene over it and every animation settled, for
|
||||
* glasses that have no display lists (a G1). [screen] itself when there is no scene.
|
||||
*/
|
||||
fun composed(screen: ByteArray, width: Int, height: Int, rightLens: Boolean): ByteArray {
|
||||
if (layers.isEmpty() && selections.isEmpty() && screenDepth == 0) return screen
|
||||
val output = ByteArray(screen.size)
|
||||
DisplayListRenderer(rendererResources(width, height), rightLens = rightLens).render(511,
|
||||
DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height), SETTLED_MS)
|
||||
return output
|
||||
}
|
||||
|
||||
/** Layer surfaces, then retained resources, by index, and the root list at 511. */
|
||||
private fun rendererResources(width: Int, height: Int): HashMap<Int, ByteArray> {
|
||||
val resources = HashMap<Int, ByteArray>()
|
||||
|
||||
@@ -86,6 +86,9 @@ class IosGlassesSession internal constructor(
|
||||
|
||||
fun setRequiredFirmwareRevision(revision: Int) = core.setRequiredFirmwareRevision(revision)
|
||||
|
||||
/** Drive an Even G1 on g1v0 (see GlassesSessionG1.kt). Before [start]. */
|
||||
fun useG1() = core.useG1()
|
||||
|
||||
fun start(): Boolean = core.start()
|
||||
|
||||
fun disconnect() = core.disconnect()
|
||||
|
||||
Reference in new issue
Block a user