Stub out more of broadcast messages

This commit is contained in:
zvecr 2022-04-05 18:54:28 +01:00
parent 0d59f8b42d
commit c1b57354f6
8 changed files with 162 additions and 30 deletions

View file

@ -48,7 +48,7 @@ def print_dotted_output(kb_info_json, prefix=''):
cli.echo(' {fg_blue}%s{fg_reset}: %s', new_prefix, kb_info_json[key])
def _xap_transaction(device, sub, route, ret_len, *args):
def _xap_transaction(device, sub, route, *args):
# gen token
tok = random.getrandbits(16)
token = tok.to_bytes(2, byteorder='little')
@ -78,16 +78,20 @@ def _xap_transaction(device, sub, route, ret_len, *args):
device.write(buffer)
# get resp
array_alpha = device.read(4 + ret_len, 100)
array_alpha = device.read(64, 100)
# validate tok sent == resp
if str(token) != str(array_alpha[:2]):
return None
return array_alpha[4:]
if int(array_alpha[2]) != 0x01:
return None
payload_len = int(array_alpha[3])
return array_alpha[4:4 + payload_len]
def _query_device(device):
ver_data = _xap_transaction(device, 0x00, 0x00, 4)
ver_data = _xap_transaction(device, 0x00, 0x00)
if not ver_data:
return {'xap': 'UNKNOWN'}
@ -95,14 +99,14 @@ def _query_device(device):
a = (ver_data[3] << 24) + (ver_data[2] << 16) + (ver_data[1] << 8) + (ver_data[0])
ver = f'{a>>24}.{a>>16 & 0xFF}.{a & 0xFFFF}'
secure = int.from_bytes(_xap_transaction(device, 0x00, 0x03, 1), 'little')
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_info_len(device):
len_data = _xap_transaction(device, 0x01, 0x05, 4)
len_data = _xap_transaction(device, 0x01, 0x05)
if not len_data:
return 0
@ -111,7 +115,7 @@ def _query_device_info_len(device):
def _query_device_info_chunk(device, offset):
return _xap_transaction(device, 0x01, 0x06, 32, offset)
return _xap_transaction(device, 0x01, 0x06, offset)
def _query_device_info(device):
@ -143,7 +147,42 @@ def _list_devices():
# TODO: better formatting like "lsusb -v"?
data = _query_device_info(device)
print_dotted_output(data)
# _xap_transaction(device, 0x01, 0x07, 1)
def xap_doit():
print("xap_doit")
# get layer count
# layers = _xap_transaction(device, 0x04, 0x01)
# layers = int.from_bytes(layers, "little")
# print(f'layers:{layers}')
# get keycode [layer:0, row:0, col:0]
# keycode = _xap_transaction(device, 0x04, 0x02, b"\x00\x00\x00")
# keycode = int.from_bytes(keycode, "little")
# keycode_map = {
# # TODO: this should be data driven...
# 0x04: 'KC_A',
# 0x05: 'KC_B',
# 0x29: 'KC_ESCAPE'
# }
# print('keycode:' + keycode_map.get(keycode, 'unknown'))
# Reboot
# _xap_transaction(device, 0x01, 0x07)
def xap_broadcast_listen(device):
try:
cli.log.info("Listening for XAP broadcasts...")
while 1:
array_alpha = device.read(64, 100)
if str(b"\xFF\xFF") == str(array_alpha[:2]):
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...")
@cli.argument('-d', '--device', help='device to select - uses format <pid>:<vid>.')
@ -161,25 +200,14 @@ def xap(cli):
return _list_devices()
# Connect to first available device
dev = _search()[0]
devices = _search()
if not devices:
cli.log.error("No devices found!")
return False
dev = devices[0]
device = hid.Device(path=dev['path'])
cli.log.info("Connected to:%04x:%04x %s %s", dev['vendor_id'], dev['product_id'], dev['manufacturer_string'], dev['product_string'])
# get layer count
layers = _xap_transaction(device, 0x04, 0x01, 1)
layers = int.from_bytes(layers, "little")
print(f'layers:{layers}')
# get keycode [layer:0, row:0, col:0]
keycode = _xap_transaction(device, 0x04, 0x02, 2, b"\x00\x00\x00")
keycode = int.from_bytes(keycode, "little")
keycode_map = {
# TODO: this should be data driven...
0x04: 'KC_A',
0x05: 'KC_B',
0x29: 'KC_ESCAPE'
}
print('keycode:' + keycode_map.get(keycode, 'unknown'))
# Reboot
# _xap_transaction(device, 0x01, 0x07, 1)
# xap_doit(device)
xap_broadcast_listen(device)