2022-03-28 20:06:11 +00:00
|
|
|
"""Interactions with compatible XAP devices
|
|
|
|
"""
|
2022-05-05 21:16:38 +00:00
|
|
|
import cmd
|
2022-03-28 20:06:11 +00:00
|
|
|
import json
|
|
|
|
import random
|
|
|
|
import gzip
|
2022-06-21 12:27:53 +00:00
|
|
|
import threading
|
|
|
|
import functools
|
|
|
|
from enum import IntFlag
|
2022-03-29 17:36:08 +00:00
|
|
|
from platform import platform
|
2022-03-28 20:06:11 +00:00
|
|
|
|
|
|
|
from milc import cli
|
|
|
|
|
2022-05-11 00:52:48 +00:00
|
|
|
from qmk.keyboard import render_layout
|
2022-05-10 01:29:30 +00:00
|
|
|
from qmk.xap.common import get_xap_keycodes
|
2022-05-09 22:51:43 +00:00
|
|
|
|
2022-05-10 02:48:48 +00:00
|
|
|
KEYCODE_MAP = get_xap_keycodes('latest')
|
2022-05-05 21:16:38 +00:00
|
|
|
|
2022-03-28 20:06:11 +00:00
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
def _u32toBCD(val): # noqa: N802
|
|
|
|
"""Create BCD string
|
|
|
|
"""
|
|
|
|
return f'{val>>24}.{val>>16 & 0xFF}.{val & 0xFFFF}'
|
|
|
|
|
2022-03-28 20:06:11 +00:00
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
class XAPFlags(IntFlag):
|
|
|
|
SUCCESS = 0x01
|
2022-03-28 20:06:11 +00:00
|
|
|
|
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
class XAPDevice:
|
|
|
|
def __init__(self, dev):
|
|
|
|
"""Constructor opens hid device and starts dependent services
|
|
|
|
"""
|
|
|
|
self.responses = {}
|
|
|
|
|
|
|
|
self.dev = hid.Device(path=dev['path'])
|
2022-03-28 20:06:11 +00:00
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
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
|
|
|
|
# TODO: this code is total garbage
|
|
|
|
args_data = []
|
|
|
|
args_len = 2
|
|
|
|
if len(args) == 1:
|
|
|
|
if isinstance(args[0], (bytes, bytearray)):
|
|
|
|
args_len += len(args[0])
|
|
|
|
args_data = args[0]
|
|
|
|
else:
|
|
|
|
args_len += 2
|
|
|
|
args_data = args[0].to_bytes(2, byteorder='little')
|
|
|
|
|
|
|
|
padding_len = 64 - 3 - args_len
|
|
|
|
padding = b"\x00" * padding_len
|
|
|
|
if args_data:
|
|
|
|
padding = args_data + padding
|
|
|
|
buffer = token + args_len.to_bytes(1, byteorder='little') + sub.to_bytes(1, byteorder='little') + route.to_bytes(1, byteorder='little') + padding
|
|
|
|
|
|
|
|
# prepend 0 on windows because reasons...
|
|
|
|
if 'windows' in platform().lower():
|
|
|
|
buffer = b"\x00" + buffer
|
|
|
|
|
|
|
|
event = threading.Event()
|
|
|
|
self.responses[str(token)] = event
|
|
|
|
|
|
|
|
self.dev.write(buffer)
|
|
|
|
event.wait(timeout=1)
|
|
|
|
self.responses.pop(str(token), None)
|
|
|
|
if not hasattr(event, '_ret'):
|
|
|
|
return None
|
|
|
|
|
|
|
|
array_alpha = event._ret
|
|
|
|
if int(array_alpha[2]) != XAPFlags.SUCCESS:
|
|
|
|
return None
|
|
|
|
|
|
|
|
payload_len = int(array_alpha[3])
|
|
|
|
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)}
|
|
|
|
|
|
|
|
@functools.cache
|
|
|
|
def info(self):
|
|
|
|
data = self._query_device_info()
|
|
|
|
data['_id'] = self.transaction(0x01, 0x08)
|
|
|
|
data['xap'] = self.version()['xap']
|
|
|
|
return data
|
|
|
|
|
|
|
|
def unlock(self):
|
|
|
|
self.transaction(0x00, 0x04)
|
|
|
|
|
|
|
|
|
|
|
|
class XAPClient:
|
|
|
|
@staticmethod
|
|
|
|
def _lazy_imports():
|
|
|
|
# Lazy load to avoid missing dependency issues
|
|
|
|
global hid
|
|
|
|
import hid
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def list(search=None):
|
|
|
|
"""Find compatible XAP devices
|
|
|
|
"""
|
|
|
|
XAPClient._lazy_imports()
|
2022-03-28 20:06:11 +00:00
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
def _is_xap_usage(x):
|
|
|
|
return x['usage_page'] == 0xFF51 and x['usage'] == 0x0058
|
|
|
|
|
|
|
|
def _is_filtered_device(x):
|
|
|
|
name = "%04x:%04x" % (x['vendor_id'], x['product_id'])
|
|
|
|
return name.lower().startswith(search.lower())
|
|
|
|
|
|
|
|
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):
|
|
|
|
# # get layer count
|
|
|
|
# layers = _xap_transaction(device, 0x04, 0x02)
|
|
|
|
# layers = int.from_bytes(layers, "little")
|
|
|
|
# print(f'layers:{layers}')
|
|
|
|
|
|
|
|
# # get keycode [layer:0, row:0, col:0]
|
|
|
|
# # keycode = _xap_transaction(device, 0x04, 0x03, b"\x00\x00\x00")
|
|
|
|
|
|
|
|
# # get encoder [layer:0, index:0, clockwise:0]
|
|
|
|
# keycode = _xap_transaction(device, 0x04, 0x04, b"\x00\x00\x00")
|
|
|
|
|
|
|
|
# keycode = int.from_bytes(keycode, "little")
|
|
|
|
# print(f'keycode:{KEYCODE_MAP.get(keycode, "unknown")}[{keycode}]')
|
|
|
|
|
|
|
|
# # set encoder [layer:0, index:0, clockwise:0, keycode:KC_A]
|
|
|
|
# _xap_transaction(device, 0x05, 0x04, b"\x00\x00\x00\x04\00")
|
2022-03-28 20:06:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
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())))
|
2022-04-18 23:04:22 +00:00
|
|
|
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)
|
2022-03-28 20:06:11 +00:00
|
|
|
elif isinstance(kb_info_json[key], dict):
|
|
|
|
print_dotted_output(kb_info_json[key], new_prefix)
|
|
|
|
elif isinstance(kb_info_json[key], list):
|
2022-03-29 23:43:25 +00:00
|
|
|
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:
|
2022-06-21 12:27:53 +00:00
|
|
|
cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, ', '.join(map(str, data)))
|
2022-03-28 20:06:11 +00:00
|
|
|
else:
|
|
|
|
cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, kb_info_json[key])
|
|
|
|
|
|
|
|
|
2022-03-29 17:36:08 +00:00
|
|
|
def _list_devices():
|
|
|
|
"""Dump out available devices
|
|
|
|
"""
|
|
|
|
cli.log.info('Available devices:')
|
2022-06-21 12:27:53 +00:00
|
|
|
devices = XAPClient.list()
|
2022-03-29 17:36:08 +00:00
|
|
|
for dev in devices:
|
2022-06-21 12:27:53 +00:00
|
|
|
device = XAPClient().connect(dev)
|
2022-03-28 20:06:11 +00:00
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
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'])
|
2022-03-28 20:06:11 +00:00
|
|
|
|
2022-03-29 17:36:08 +00:00
|
|
|
if cli.config.general.verbose:
|
|
|
|
# TODO: better formatting like "lsusb -v"?
|
|
|
|
print_dotted_output(data)
|
2022-04-05 17:54:28 +00:00
|
|
|
|
|
|
|
|
2022-05-05 21:16:38 +00:00
|
|
|
class XAPShell(cmd.Cmd):
|
|
|
|
intro = 'Welcome to the XAP shell. Type help or ? to list commands.\n'
|
|
|
|
prompt = 'Ψ> '
|
|
|
|
|
|
|
|
def __init__(self, device):
|
|
|
|
cmd.Cmd.__init__(self)
|
|
|
|
self.device = device
|
2022-05-10 01:29:30 +00:00
|
|
|
# cache keycodes for this device
|
2022-06-21 12:27:53 +00:00
|
|
|
self.keycodes = get_xap_keycodes(device.version()['xap'])
|
2022-05-05 21:16:38 +00:00
|
|
|
|
|
|
|
def do_about(self, arg):
|
|
|
|
"""Prints out the current version of QMK with a build date
|
|
|
|
"""
|
2022-06-21 12:27:53 +00:00
|
|
|
# TODO: request stuff?
|
|
|
|
print(self.device.info()['xap'])
|
2022-05-05 21:16:38 +00:00
|
|
|
|
|
|
|
def do_unlock(self, arg):
|
|
|
|
"""Initiate secure unlock
|
|
|
|
"""
|
2022-06-21 12:27:53 +00:00
|
|
|
self.device.unlock()
|
2022-05-05 21:16:38 +00:00
|
|
|
print("Done")
|
|
|
|
|
|
|
|
def do_listen(self, arg):
|
|
|
|
"""Log out XAP broadcast messages
|
|
|
|
"""
|
2022-06-21 12:27:53 +00:00
|
|
|
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...")
|
2022-05-05 21:16:38 +00:00
|
|
|
|
|
|
|
def do_keycode(self, arg):
|
|
|
|
"""Prints out the keycode value of a certain layer, row, and column
|
|
|
|
"""
|
|
|
|
data = bytes(map(int, arg.split()))
|
|
|
|
if len(data) != 3:
|
|
|
|
cli.log.error("Invalid args")
|
|
|
|
return
|
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
keycode = self.device.transaction(0x04, 0x03, data)
|
2022-05-05 21:16:38 +00:00
|
|
|
keycode = int.from_bytes(keycode, "little")
|
2022-05-10 01:29:30 +00:00
|
|
|
print(f'keycode:{self.keycodes.get(keycode, "unknown")}[{keycode}]')
|
2022-05-05 21:16:38 +00:00
|
|
|
|
2022-05-10 00:38:14 +00:00
|
|
|
def do_keymap(self, arg):
|
|
|
|
"""Prints out the keycode values of a certain layer
|
|
|
|
"""
|
|
|
|
data = bytes(map(int, arg.split()))
|
|
|
|
if len(data) != 1:
|
|
|
|
cli.log.error("Invalid args")
|
|
|
|
return
|
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
info = self.device.info()
|
2022-05-10 00:38:14 +00:00
|
|
|
rows = info['matrix_size']['rows']
|
|
|
|
cols = info['matrix_size']['cols']
|
|
|
|
|
|
|
|
for r in range(rows):
|
|
|
|
for c in range(cols):
|
|
|
|
q = data + r.to_bytes(1, byteorder='little') + c.to_bytes(1, byteorder='little')
|
2022-06-21 12:27:53 +00:00
|
|
|
keycode = self.device.transaction(0x04, 0x03, q)
|
2022-05-10 00:38:14 +00:00
|
|
|
keycode = int.from_bytes(keycode, "little")
|
2022-05-10 01:29:30 +00:00
|
|
|
print(f'| {self.keycodes.get(keycode, "unknown").ljust(7)} ', end='', flush=True)
|
2022-05-10 00:38:14 +00:00
|
|
|
print('|')
|
|
|
|
|
2022-05-11 00:52:48 +00:00
|
|
|
def do_layer(self, arg):
|
|
|
|
"""Renders keycode values of a certain layer
|
|
|
|
"""
|
|
|
|
data = bytes(map(int, arg.split()))
|
|
|
|
if len(data) != 1:
|
|
|
|
cli.log.error("Invalid args")
|
|
|
|
return
|
|
|
|
|
2022-06-21 12:27:53 +00:00
|
|
|
info = self.device.info()
|
2022-05-11 00:52:48 +00:00
|
|
|
|
|
|
|
# Assumptions on selected layout rather than prompt
|
|
|
|
first_layout = next(iter(info['layouts']))
|
|
|
|
layout = info['layouts'][first_layout]['layout']
|
|
|
|
|
|
|
|
keycodes = []
|
|
|
|
for item in layout:
|
|
|
|
q = data + bytes(item['matrix'])
|
2022-06-21 12:27:53 +00:00
|
|
|
keycode = self.device.transaction(0x04, 0x03, q)
|
2022-05-11 00:52:48 +00:00
|
|
|
keycode = int.from_bytes(keycode, "little")
|
|
|
|
keycodes.append(self.keycodes.get(keycode, "???"))
|
|
|
|
|
|
|
|
print(render_layout(layout, False, keycodes))
|
|
|
|
|
2022-05-05 21:16:38 +00:00
|
|
|
def do_exit(self, line):
|
|
|
|
"""Quit shell
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
def do_EOF(self, line): # noqa: N802
|
|
|
|
"""Quit shell (ctrl+D)
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
"""Wrapper for cmdloop that handles ctrl+C
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.cmdloop()
|
|
|
|
print('')
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('^C')
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-03-28 20:06:11 +00:00
|
|
|
@cli.argument('-d', '--device', help='device to select - uses format <pid>:<vid>.')
|
|
|
|
@cli.argument('-l', '--list', arg_only=True, action='store_true', help='List available devices.')
|
2022-05-05 21:16:38 +00:00
|
|
|
@cli.argument('-i', '--interactive', arg_only=True, action='store_true', help='Start interactive shell.')
|
2022-05-05 21:35:04 +00:00
|
|
|
@cli.argument('action', nargs='*', default=['listen'], arg_only=True)
|
2022-03-28 20:06:11 +00:00
|
|
|
@cli.subcommand('Acquire debugging information from usb XAP devices.', hidden=False if cli.config.user.developer else True)
|
|
|
|
def xap(cli):
|
|
|
|
"""Acquire debugging information from XAP devices
|
|
|
|
"""
|
|
|
|
if cli.args.list:
|
|
|
|
return _list_devices()
|
|
|
|
|
2022-04-01 22:37:58 +00:00
|
|
|
# Connect to first available device
|
2022-06-21 12:27:53 +00:00
|
|
|
devices = XAPClient.list()
|
2022-04-05 17:54:28 +00:00
|
|
|
if not devices:
|
|
|
|
cli.log.error("No devices found!")
|
|
|
|
return False
|
|
|
|
|
|
|
|
dev = devices[0]
|
2022-06-21 12:27:53 +00:00
|
|
|
cli.log.info("Connecting to:%04x:%04x %s %s", dev['vendor_id'], dev['product_id'], dev['manufacturer_string'], dev['product_string'])
|
|
|
|
device = XAPClient().connect(dev)
|
2022-04-01 22:37:58 +00:00
|
|
|
|
2022-05-05 21:16:38 +00:00
|
|
|
# shell?
|
|
|
|
if cli.args.interactive:
|
|
|
|
XAPShell(device).loop()
|
|
|
|
return True
|
2022-04-10 23:43:18 +00:00
|
|
|
|
2022-05-05 21:35:04 +00:00
|
|
|
XAPShell(device).onecmd(" ".join(cli.args.action))
|