Refactor xap client

This commit is contained in:
zvecr 2022-06-21 13:27:53 +01:00
parent 90fc901624
commit c22fedb5b2

View File

@ -4,6 +4,9 @@ import cmd
import json import json
import random import random
import gzip import gzip
import threading
import functools
from enum import IntFlag
from platform import platform from platform import platform
from milc import cli from milc import cli
@ -14,53 +17,68 @@ from qmk.xap.common import get_xap_keycodes
KEYCODE_MAP = get_xap_keycodes('latest') KEYCODE_MAP = get_xap_keycodes('latest')
def _is_xap_usage(x): def _u32toBCD(val): # noqa: N802
return x['usage_page'] == 0xFF51 and x['usage'] == 0x0058 """Create BCD string
def _is_filtered_device(x):
name = "%04x:%04x" % (x['vendor_id'], x['product_id'])
return name.lower().startswith(cli.args.device.lower())
def _search():
devices = filter(_is_xap_usage, hid.enumerate())
if cli.args.device:
devices = filter(_is_filtered_device, devices)
return list(devices)
def print_dotted_output(kb_info_json, prefix=''):
"""Print the info.json in a plain text format with dot-joined keys.
""" """
for key in sorted(kb_info_json): return f'{val>>24}.{val>>16 & 0xFF}.{val & 0xFFFF}'
new_prefix = f'{prefix}.{key}' if prefix else key
if key in ['parse_errors', 'parse_warnings']:
continue
elif key == 'layouts' and prefix == '':
cli.echo(' {fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
elif isinstance(kb_info_json[key], bytes):
conv = "".join(["{:02X}".format(b) for b in kb_info_json[key]])
cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, conv)
elif isinstance(kb_info_json[key], dict):
print_dotted_output(kb_info_json[key], new_prefix)
elif isinstance(kb_info_json[key], list):
data = kb_info_json[key]
if len(data) and isinstance(data[0], dict):
for index, item in enumerate(data, start=0):
cli.echo(' {fg_blue}%s.%s{fg_reset}: %s', new_prefix, index, str(item))
else:
cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, ', '.join(sorted(map(str, data))))
else:
cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, kb_info_json[key])
def _xap_transaction(device, sub, route, *args): class XAPFlags(IntFlag):
# gen token SUCCESS = 0x01
tok = random.getrandbits(16)
token = tok.to_bytes(2, byteorder='little')
class XAPDevice:
def __init__(self, dev):
"""Constructor opens hid device and starts dependent services
"""
self.responses = {}
self.dev = hid.Device(path=dev['path'])
self.bg = threading.Thread(target=self._read_loop, daemon=True)
self.bg.start()
def _read_loop(self):
"""Background thread to signal waiting transactions
"""
while 1:
array_alpha = self.dev.read(64, 100)
if array_alpha:
token = str(array_alpha[:2])
event = self.responses.get(token)
if event:
event._ret = array_alpha
event.set()
def _query_device_info(self):
datalen = int.from_bytes(self.transaction(0x01, 0x05) or bytes(0), "little")
if not datalen:
return {}
data = []
offset = 0
while offset < datalen:
chunk = self.transaction(0x01, 0x06, offset)
data += chunk
offset += len(chunk)
str_data = gzip.decompress(bytearray(data[:datalen]))
return json.loads(str_data)
def listen(self):
"""Receive a "broadcast" message
"""
token = b"\xFF\xFF"
event = threading.Event()
self.responses[str(token)] = event
event.wait()
return event._ret
def transaction(self, sub, route, *args):
"""Request/Receive
"""
# token cannot start with zero or be FFFF
token = random.randrange(0x0100, 0xFFFE).to_bytes(2, byteorder='big')
# send with padding # send with padding
# TODO: this code is total garbage # TODO: this code is total garbage
@ -84,85 +102,77 @@ def _xap_transaction(device, sub, route, *args):
if 'windows' in platform().lower(): if 'windows' in platform().lower():
buffer = b"\x00" + buffer buffer = b"\x00" + buffer
device.write(buffer) event = threading.Event()
self.responses[str(token)] = event
# get resp self.dev.write(buffer)
array_alpha = device.read(64, 250) event.wait(timeout=1)
self.responses.pop(str(token), None)
# validate tok sent == resp if not hasattr(event, '_ret'):
if str(token) != str(array_alpha[:2]):
return None return None
if int(array_alpha[2]) != 0x01:
array_alpha = event._ret
if int(array_alpha[2]) != XAPFlags.SUCCESS:
return None return None
payload_len = int(array_alpha[3]) payload_len = int(array_alpha[3])
return array_alpha[4:4 + payload_len] return array_alpha[4:4 + payload_len]
@functools.cache
def version(self):
ver = int.from_bytes(self.transaction(0x00, 0x00) or bytes(0), 'little')
return {'xap': _u32toBCD(ver)}
def _query_device(device): @functools.cache
ver_data = _xap_transaction(device, 0x00, 0x00) def info(self):
if not ver_data: data = self._query_device_info()
return {'xap': 'UNKNOWN', 'secure': 'UNKNOWN'} data['_id'] = self.transaction(0x01, 0x08)
data['xap'] = self.version()['xap']
return data
# to u32 to BCD string def unlock(self):
a = (ver_data[3] << 24) + (ver_data[2] << 16) + (ver_data[1] << 8) + (ver_data[0]) self.transaction(0x00, 0x04)
ver = f'{a>>24}.{a>>16 & 0xFF}.{a & 0xFFFF}'
secure = int.from_bytes(_xap_transaction(device, 0x00, 0x03), 'little')
secure = 'unlocked' if secure == 2 else 'LOCKED'
return {'xap': ver, 'secure': secure}
def _query_device_id(device): class XAPClient:
return _xap_transaction(device, 0x01, 0x08) @staticmethod
def _lazy_imports():
# Lazy load to avoid missing dependency issues
global hid
import hid
@staticmethod
def _query_device_info_len(device): def list(search=None):
len_data = _xap_transaction(device, 0x01, 0x05) """Find compatible XAP devices
if not len_data:
return 0
# to u32
return (len_data[3] << 24) + (len_data[2] << 16) + (len_data[1] << 8) + (len_data[0])
def _query_device_info_chunk(device, offset):
return _xap_transaction(device, 0x01, 0x06, offset)
def _query_device_info(device):
datalen = _query_device_info_len(device)
if not datalen:
return {}
data = []
offset = 0
while offset < datalen:
data += _query_device_info_chunk(device, offset)
offset += 32
str_data = gzip.decompress(bytearray(data[:datalen]))
return json.loads(str_data)
def _list_devices():
"""Dump out available devices
""" """
cli.log.info('Available devices:') XAPClient._lazy_imports()
devices = _search()
for dev in devices:
device = hid.Device(path=dev['path'])
data = _query_device(device) def _is_xap_usage(x):
cli.log.info(" %04x:%04x %s %s [API:%s] %s", dev['vendor_id'], dev['product_id'], dev['manufacturer_string'], dev['product_string'], data['xap'], data['secure']) return x['usage_page'] == 0xFF51 and x['usage'] == 0x0058
if cli.config.general.verbose: def _is_filtered_device(x):
# TODO: better formatting like "lsusb -v"? name = "%04x:%04x" % (x['vendor_id'], x['product_id'])
data = _query_device_info(device) return name.lower().startswith(search.lower())
data["_id"] = _query_device_id(device)
print_dotted_output(data) devices = filter(_is_xap_usage, hid.enumerate())
if search:
devices = filter(_is_filtered_device, devices)
return list(devices)
def connect(self, dev):
"""Connect to a given XAP device
"""
XAPClient._lazy_imports()
return XAPDevice(dev)
# def _query_device_secure(device):
# secure = int.from_bytes(_xap_transaction(device, 0x00, 0x03), 'little')
# secure = 'unlocked' if secure == 2 else 'LOCKED'
# return {'secure': secure}
#
# def xap_dummy(device): # def xap_dummy(device):
# # get layer count # # get layer count
# layers = _xap_transaction(device, 0x04, 0x02) # layers = _xap_transaction(device, 0x04, 0x02)
@ -182,22 +192,46 @@ def _list_devices():
# _xap_transaction(device, 0x05, 0x04, b"\x00\x00\x00\x04\00") # _xap_transaction(device, 0x05, 0x04, b"\x00\x00\x00\x04\00")
def xap_broadcast_listen(device): def print_dotted_output(kb_info_json, prefix=''):
try: """Print the info.json in a plain text format with dot-joined keys.
cli.log.info("Listening for XAP broadcasts...") """
while 1: for key in sorted(kb_info_json):
array_alpha = device.read(64, 100) new_prefix = f'{prefix}.{key}' if prefix else key
if str(b"\xFF\xFF") == str(array_alpha[:2]):
if array_alpha[2] == 1: if key in ['parse_errors', 'parse_warnings']:
cli.log.info(" Broadcast: Secure[%02x]", array_alpha[4]) continue
elif key == 'layouts' and prefix == '':
cli.echo(' {fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
elif isinstance(kb_info_json[key], bytes):
conv = "".join(["{:02X}".format(b) for b in kb_info_json[key]])
cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, conv)
elif isinstance(kb_info_json[key], dict):
print_dotted_output(kb_info_json[key], new_prefix)
elif isinstance(kb_info_json[key], list):
data = kb_info_json[key]
if len(data) and isinstance(data[0], dict):
for index, item in enumerate(data, start=0):
cli.echo(' {fg_blue}%s.%s{fg_reset}: %s', new_prefix, index, str(item))
else: else:
cli.log.info(" Broadcast: type[%02x] data:[%02x]", array_alpha[2], array_alpha[4]) cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, ', '.join(map(str, data)))
except KeyboardInterrupt: else:
cli.log.info("Stopping...") cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, kb_info_json[key])
def xap_unlock(device): def _list_devices():
_xap_transaction(device, 0x00, 0x04) """Dump out available devices
"""
cli.log.info('Available devices:')
devices = XAPClient.list()
for dev in devices:
device = XAPClient().connect(dev)
data = device.info()
cli.log.info(" %04x:%04x %s %s [API:%s]", dev['vendor_id'], dev['product_id'], dev['manufacturer_string'], dev['product_string'], data['xap'])
if cli.config.general.verbose:
# TODO: better formatting like "lsusb -v"?
print_dotted_output(data)
class XAPShell(cmd.Cmd): class XAPShell(cmd.Cmd):
@ -208,24 +242,33 @@ class XAPShell(cmd.Cmd):
cmd.Cmd.__init__(self) cmd.Cmd.__init__(self)
self.device = device self.device = device
# cache keycodes for this device # cache keycodes for this device
self.keycodes = get_xap_keycodes(_query_device(device)['xap']) self.keycodes = get_xap_keycodes(device.version()['xap'])
def do_about(self, arg): def do_about(self, arg):
"""Prints out the current version of QMK with a build date """Prints out the current version of QMK with a build date
""" """
data = _query_device(self.device) # TODO: request stuff?
print(data) print(self.device.info()['xap'])
def do_unlock(self, arg): def do_unlock(self, arg):
"""Initiate secure unlock """Initiate secure unlock
""" """
xap_unlock(self.device) self.device.unlock()
print("Done") print("Done")
def do_listen(self, arg): def do_listen(self, arg):
"""Log out XAP broadcast messages """Log out XAP broadcast messages
""" """
xap_broadcast_listen(self.device) try:
cli.log.info("Listening for XAP broadcasts...")
while 1:
array_alpha = self.device.listen()
if array_alpha[2] == 1:
cli.log.info(" Broadcast: Secure[%02x]", array_alpha[4])
else:
cli.log.info(" Broadcast: type[%02x] data:[%02x]", array_alpha[2], array_alpha[4])
except KeyboardInterrupt:
cli.log.info("Stopping...")
def do_keycode(self, arg): def do_keycode(self, arg):
"""Prints out the keycode value of a certain layer, row, and column """Prints out the keycode value of a certain layer, row, and column
@ -235,7 +278,7 @@ class XAPShell(cmd.Cmd):
cli.log.error("Invalid args") cli.log.error("Invalid args")
return return
keycode = _xap_transaction(self.device, 0x04, 0x03, data) keycode = self.device.transaction(0x04, 0x03, data)
keycode = int.from_bytes(keycode, "little") keycode = int.from_bytes(keycode, "little")
print(f'keycode:{self.keycodes.get(keycode, "unknown")}[{keycode}]') print(f'keycode:{self.keycodes.get(keycode, "unknown")}[{keycode}]')
@ -247,14 +290,14 @@ class XAPShell(cmd.Cmd):
cli.log.error("Invalid args") cli.log.error("Invalid args")
return return
info = _query_device_info(self.device) info = self.device.info()
rows = info['matrix_size']['rows'] rows = info['matrix_size']['rows']
cols = info['matrix_size']['cols'] cols = info['matrix_size']['cols']
for r in range(rows): for r in range(rows):
for c in range(cols): for c in range(cols):
q = data + r.to_bytes(1, byteorder='little') + c.to_bytes(1, byteorder='little') q = data + r.to_bytes(1, byteorder='little') + c.to_bytes(1, byteorder='little')
keycode = _xap_transaction(self.device, 0x04, 0x03, q) keycode = self.device.transaction(0x04, 0x03, q)
keycode = int.from_bytes(keycode, "little") keycode = int.from_bytes(keycode, "little")
print(f'| {self.keycodes.get(keycode, "unknown").ljust(7)} ', end='', flush=True) print(f'| {self.keycodes.get(keycode, "unknown").ljust(7)} ', end='', flush=True)
print('|') print('|')
@ -267,7 +310,7 @@ class XAPShell(cmd.Cmd):
cli.log.error("Invalid args") cli.log.error("Invalid args")
return return
info = _query_device_info(self.device) info = self.device.info()
# Assumptions on selected layout rather than prompt # Assumptions on selected layout rather than prompt
first_layout = next(iter(info['layouts'])) first_layout = next(iter(info['layouts']))
@ -276,7 +319,7 @@ class XAPShell(cmd.Cmd):
keycodes = [] keycodes = []
for item in layout: for item in layout:
q = data + bytes(item['matrix']) q = data + bytes(item['matrix'])
keycode = _xap_transaction(self.device, 0x04, 0x03, q) keycode = self.device.transaction(0x04, 0x03, q)
keycode = int.from_bytes(keycode, "little") keycode = int.from_bytes(keycode, "little")
keycodes.append(self.keycodes.get(keycode, "???")) keycodes.append(self.keycodes.get(keycode, "???"))
@ -311,22 +354,18 @@ class XAPShell(cmd.Cmd):
def xap(cli): def xap(cli):
"""Acquire debugging information from XAP devices """Acquire debugging information from XAP devices
""" """
# Lazy load to avoid issues
global hid
import hid
if cli.args.list: if cli.args.list:
return _list_devices() return _list_devices()
# Connect to first available device # Connect to first available device
devices = _search() devices = XAPClient.list()
if not devices: if not devices:
cli.log.error("No devices found!") cli.log.error("No devices found!")
return False return False
dev = devices[0] dev = devices[0]
device = hid.Device(path=dev['path']) cli.log.info("Connecting to:%04x:%04x %s %s", dev['vendor_id'], dev['product_id'], dev['manufacturer_string'], dev['product_string'])
cli.log.info("Connected to:%04x:%04x %s %s", dev['vendor_id'], dev['product_id'], dev['manufacturer_string'], dev['product_string']) device = XAPClient().connect(dev)
# shell? # shell?
if cli.args.interactive: if cli.args.interactive: