包含本次会话修复: - iOS clickReady 警告 bug(默认 false 永远显示) - BLE 'handle is invalid' 排查(stale handle auto-retry 实验 + 回退) 项目结构: - glass-mouse/: 眼镜端原生 Kotlin App,前台服务 MouseService + 自定义 BLE GATT 桥接 ProxyBleServer + 无障碍 GestureDescription 注入 - ios/GlassRemote/: iPhone 端 SwiftUI + CoreBluetooth,通过 BLE 直连眼镜 - HTTP 投屏服务(:8000) 接 PPT/WPS 备注宏 POST 推送 技术栈: Android Kotlin (gradle 8.14.4, compileSdk 34), iOS Swift 5.10 (Xcode 26.5)
180 lines
5.3 KiB
Python
180 lines
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
INMO 眼镜控制协议 —— ControlCommand protobuf 编码器
|
||
基于官方 APK (2.0.052-CH_79454350.apk) 反编译出的 .proto 还原。
|
||
无需 protoc:手工按 protobuf wire format 编码。
|
||
|
||
已知(已确认):
|
||
- 消息格式 = ControlCommand.proto 定义
|
||
- 鼠标:CommandType.MOUSE_CTRL(23) + CommandMouseEvent(field 16)
|
||
- 导航键:CommandType.TOUCH_CTRL(20) + CommandTouchCtrl(field 13)
|
||
- 动作枚举见下方常量
|
||
|
||
待 jadx 确认(本文件不含):
|
||
- GATT 服务/特征 UUID(候选 e49a25e0/25f8/28e1-f69a-11e8-8eb2-f2801f1b9fd1)
|
||
- 是否需要先切 MOUSE_MODE(CommandType.CHANGE_MODE=3, CommandMode.MOUSE_MODE=2)
|
||
- 传输层是否加密(dex 中出现 encryption/HMAC-SHA256/secretKey 线索)
|
||
- 绑定鉴权(BleConnectBind.userAccount)是否阻塞指令
|
||
"""
|
||
|
||
import struct
|
||
|
||
|
||
# ── 基础 varint / tag 编码 ──
|
||
def _varint(n: int) -> bytes:
|
||
n &= (1 << 64) - 1
|
||
out = b""
|
||
while True:
|
||
b = n & 0x7F
|
||
n >>= 7
|
||
if n:
|
||
out += bytes([b | 0x80])
|
||
else:
|
||
out += bytes([b])
|
||
break
|
||
return out
|
||
|
||
|
||
def _tag(field: int, wire: int) -> bytes:
|
||
return _varint((field << 3) | wire)
|
||
|
||
|
||
def _enum(field: int, val: int) -> bytes:
|
||
return _tag(field, 0) + _varint(val)
|
||
|
||
|
||
def _float(field: int, f: float) -> bytes:
|
||
return _tag(field, 5) + struct.pack("<f", f)
|
||
|
||
|
||
def _bytes(field: int, b: bytes) -> bytes:
|
||
return _tag(field, 2) + _varint(len(b)) + b
|
||
|
||
|
||
# ── CommandType 枚举(部分常用)──
|
||
CMD_VOLUME = 0
|
||
CMD_BRIGHTNESS = 1
|
||
CMD_CHANGE_MODE = 3 # 切换模式(含 MOUSE_MODE)
|
||
CMD_BACK = 4
|
||
CMD_HOME = 5
|
||
CMD_MENU = 6
|
||
CMD_GO = 7
|
||
CMD_TOUCH_CTRL = 20 # 导航键(HOME/MENU/BACK/GO)
|
||
CMD_MOUSE_CTRL = 23 # 鼠标模式
|
||
CMD_INPUT_TEXT = 25 # 文本/KEYCODE
|
||
|
||
# CommandMode
|
||
MODE_ORD = 0
|
||
MODE_RAY = 1
|
||
MODE_MOUSE = 2
|
||
|
||
# CommandMouseAction
|
||
ACTION_DOWN = 0
|
||
ACTION_UP = 1
|
||
ACTION_MOVE = 2
|
||
ACTION_HOVER = 3
|
||
ACTION_SCROLL = 5
|
||
|
||
# TouchKeyCode
|
||
KEY_PAD = 0
|
||
KEY_HOME = 1
|
||
KEY_MENU = 2
|
||
KEY_BACK = 3
|
||
KEY_GO = 4
|
||
|
||
# TouchKeyState
|
||
STATE_RELEASE = 0
|
||
STATE_PRESS = 1
|
||
|
||
# TouchKeySource
|
||
SRC_LOCAL = 0
|
||
SRC_APP = 1
|
||
SRC_RING = 2
|
||
|
||
|
||
# ── 子消息编码 ──
|
||
def encode_mouse_event(action: int, x: float = 0.0, y: float = 0.0) -> bytes:
|
||
"""CommandMouseEvent: action(1), x(2 float), y(3 float)"""
|
||
return _enum(1, action) + _float(2, x) + _float(3, y)
|
||
|
||
|
||
def encode_touch_ctrl(key_source: int, key_code: int,
|
||
key_state: int, systick_ms: int = 0) -> bytes:
|
||
"""CommandTouchCtrl: key_source(1), key_code(2), key_state(3), systick_ms(4)"""
|
||
body = _enum(1, key_source) + _enum(2, key_code) + _enum(3, key_state)
|
||
if systick_ms:
|
||
body += _varint((4 << 3) | 0) + _varint(systick_ms)
|
||
return body
|
||
|
||
|
||
# ── 顶层 ControlCommand 封装 ──
|
||
def build_control_command(cmd_type: int, payload_field: int,
|
||
payload: bytes, type_status: int = 0,
|
||
is_no_open: bool = False) -> bytes:
|
||
"""
|
||
ControlCommand {
|
||
type = 1 (enum varint)
|
||
type_status = 2 (varint)
|
||
is_no_open = 3 (bool varint)
|
||
oneof Command { ... payload_field ... }
|
||
}
|
||
payload_field: 鼠标=16, 导航键=13, 文本=18, 切模式=7
|
||
"""
|
||
msg = _enum(1, cmd_type)
|
||
if type_status:
|
||
msg += _enum(2, type_status)
|
||
if is_no_open:
|
||
msg += _enum(3, 1)
|
||
msg += _bytes(payload_field, payload)
|
||
return msg
|
||
|
||
|
||
# ── 常用指令构造(便捷函数)──
|
||
def mouse_move(x: float, y: float) -> bytes:
|
||
return build_control_command(CMD_MOUSE_CTRL, 16,
|
||
encode_mouse_event(ACTION_MOVE, x, y))
|
||
|
||
|
||
def mouse_click(x: float, y: float) -> list:
|
||
"""点击 = DOWN 后 UP(眼镜端通常需要两次事件)"""
|
||
return [
|
||
build_control_command(CMD_MOUSE_CTRL, 16,
|
||
encode_mouse_event(ACTION_DOWN, x, y)),
|
||
build_control_command(CMD_MOUSE_CTRL, 16,
|
||
encode_mouse_event(ACTION_UP, x, y)),
|
||
]
|
||
|
||
|
||
def mouse_scroll(x: float, y: float, dy: float) -> bytes:
|
||
# SCROLL 用 action=5,y 表示滚动量
|
||
return build_control_command(CMD_MOUSE_CTRL, 16,
|
||
encode_mouse_event(ACTION_SCROLL, x, y + dy))
|
||
|
||
|
||
def key_press(key_code: int) -> list:
|
||
"""导航键按下+抬起(source=APP)"""
|
||
return [
|
||
build_control_command(CMD_TOUCH_CTRL, 13,
|
||
encode_touch_ctrl(SRC_APP, key_code, STATE_PRESS)),
|
||
build_control_command(CMD_TOUCH_CTRL, 13,
|
||
encode_touch_ctrl(SRC_APP, key_code, STATE_RELEASE)),
|
||
]
|
||
|
||
|
||
def switch_mouse_mode() -> bytes:
|
||
"""切到鼠标模式(部分固件发鼠标事件前需要)"""
|
||
# CommandChangeMode { mode=2 } 在 field 7
|
||
return build_control_command(CMD_CHANGE_MODE, 7, _enum(1, MODE_MOUSE))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
import binascii
|
||
# 自测:鼠标移动到 (0.5, 0.5)
|
||
mv = mouse_move(0.5, 0.5)
|
||
print("mouse_move(0.5,0.5):", binascii.hexlify(mv).decode())
|
||
for i, b in enumerate(mouse_click(0.5, 0.5)):
|
||
print(f"mouse_click[{i}]:", binascii.hexlify(b).decode())
|
||
for i, b in enumerate(key_press(KEY_HOME)):
|
||
print(f"home[{i}]:", binascii.hexlify(b).decode())
|
||
print("switch_mouse_mode:", binascii.hexlify(switch_mouse_mode()).decode())
|