vial/src/main/python/util.py

47 lines
1.3 KiB
Python
Raw Normal View History

2020-10-14 22:21:33 -04:00
# SPDX-License-Identifier: GPL-2.0-or-later
2020-10-14 12:56:25 -04:00
from PyQt5.QtCore import QCoreApplication
2020-12-02 10:10:59 -05:00
from hidproxy import hid
2020-10-14 15:16:14 -04:00
2020-10-14 12:56:25 -04:00
tr = QCoreApplication.translate
2020-10-14 15:16:14 -04:00
2020-12-02 10:10:59 -05:00
# For Vial keyboard
2020-10-14 15:16:14 -04:00
VIAL_SERIAL_NUMBER_MAGIC = "vial:f64c2b3c"
2020-12-02 10:10:59 -05:00
# For bootloader
VIBL_SERIAL_NUMBER_MAGIC = "vibl:d4f8159c"
2020-10-14 15:16:14 -04:00
MSG_LEN = 32
2020-10-16 15:26:11 -04:00
2020-10-14 15:16:14 -04:00
def hid_send(dev, msg):
if len(msg) > MSG_LEN:
raise RuntimeError("message must be less than 32 bytes")
msg += b"\x00" * (MSG_LEN - len(msg))
# add 00 at start for hidapi report id
dev.write(b"\x00" + msg)
return bytes(dev.read(MSG_LEN))
2020-10-16 15:26:11 -04:00
2020-10-14 15:16:14 -04:00
def is_rawhid(dev):
# TODO: this is only broken on linux, other platforms should be able to check usage_page
return dev["interface_number"] == 1
2020-10-16 15:26:11 -04:00
2020-12-02 10:10:59 -05:00
def find_vial_devices(sideload_vid, sideload_pid):
from vial_device import VialBootloader, VialKeyboard
2020-10-14 15:16:14 -04:00
filtered = []
for dev in hid.enumerate():
if VIAL_SERIAL_NUMBER_MAGIC in dev["serial_number"] and is_rawhid(dev):
2020-12-02 10:10:59 -05:00
filtered.append(VialKeyboard(dev))
elif VIBL_SERIAL_NUMBER_MAGIC in dev["serial_number"]:
filtered.append(VialBootloader(dev))
2020-10-18 00:58:01 -04:00
elif dev["vendor_id"] == sideload_vid and dev["product_id"] == sideload_pid and is_rawhid(dev):
2020-12-02 10:10:59 -05:00
filtered.append(VialKeyboard(dev, sideload=True))
2020-10-14 15:16:14 -04:00
return filtered