1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 04:18:10 +00:00
trezor-firmware/trezorlib/transport_hid.py

124 lines
4.0 KiB
Python
Raw Normal View History

2013-03-10 15:55:59 +00:00
'''USB HID implementation of Transport.'''
import hid
2013-09-24 23:14:54 +00:00
import time
2016-05-05 01:16:17 +00:00
from .transport import Transport, ConnectionError
2013-03-10 15:55:59 +00:00
DEVICE_IDS = [
2016-05-26 15:20:44 +00:00
# (0x10c4, 0xea80), # TREZOR Shield
2016-02-10 15:46:58 +00:00
(0x534c, 0x0001), # TREZOR
2013-03-10 15:55:59 +00:00
]
2013-03-10 16:52:04 +00:00
class FakeRead(object):
# Let's pretend we have a file-like interface
def __init__(self, func):
self.func = func
2016-02-10 15:46:58 +00:00
2013-03-10 16:52:04 +00:00
def read(self, size):
return self.func(size)
2013-03-10 15:55:59 +00:00
class HidTransport(Transport):
def __init__(self, device, *args, **kwargs):
self.hid = None
self.buffer = ''
# self.read_timeout = kwargs.get('read_timeout')
device = device[int(bool(kwargs.get('debug_link')))]
2013-03-10 15:55:59 +00:00
super(HidTransport, self).__init__(device, *args, **kwargs)
2013-09-09 13:37:39 +00:00
2013-03-10 15:55:59 +00:00
@classmethod
def enumerate(cls):
2014-08-26 14:06:19 +00:00
"""
Return a list of available TREZOR devices.
"""
devices = {}
2013-03-10 15:55:59 +00:00
for d in hid.enumerate(0, 0):
vendor_id = d['vendor_id']
product_id = d['product_id']
serial_number = d['serial_number']
2016-01-12 19:13:17 +00:00
interface_number = d['interface_number']
path = d['path']
2013-11-15 00:43:05 +00:00
2014-02-13 18:04:51 +00:00
# HIDAPI on Mac cannot detect correct HID interfaces, so device with
# DebugLink doesn't work on Mac...
if devices.get(serial_number) != None and devices[serial_number][0] == path:
raise Exception("Two devices with the same path and S/N found. This is Mac, right? :-/")
if (vendor_id, product_id) in DEVICE_IDS:
devices.setdefault(serial_number, [None, None])
2016-01-16 23:37:39 +00:00
if interface_number == 0 or interface_number == -1: # normal link
2016-01-12 19:13:17 +00:00
devices[serial_number][0] = path
2016-01-12 23:15:30 +00:00
elif interface_number == 1: # debug link
2016-01-12 19:13:17 +00:00
devices[serial_number][1] = path
else:
raise Exception("Unknown USB interface number: %d" % interface_number)
# List of two-tuples (path_normal, path_debuglink)
2016-05-20 11:36:17 +00:00
return list(devices.values())
def is_connected(self):
2014-08-26 14:06:19 +00:00
"""
Check if the device is still connected.
"""
for d in hid.enumerate(0, 0):
if d['path'] == self.device:
return True
return False
2016-02-10 15:46:58 +00:00
2013-03-10 15:55:59 +00:00
def _open(self):
self.buffer = bytearray()
2013-10-19 12:19:09 +00:00
self.hid = hid.device()
2013-11-15 00:43:05 +00:00
self.hid.open_path(self.device)
2013-09-24 23:14:54 +00:00
self.hid.set_nonblocking(True)
2016-02-10 15:46:58 +00:00
# the following was needed just for TREZOR Shield
2016-01-12 19:13:17 +00:00
# self.hid.send_feature_report([0x41, 0x01]) # enable UART
# self.hid.send_feature_report([0x43, 0x03]) # purge TX/RX FIFOs
2016-02-10 15:46:58 +00:00
2013-03-10 15:55:59 +00:00
def _close(self):
self.hid.close()
self.buffer = bytearray()
2013-03-10 15:55:59 +00:00
self.hid = None
2016-02-10 15:46:58 +00:00
2013-03-10 15:55:59 +00:00
def ready_to_read(self):
return False
2016-02-10 15:46:58 +00:00
2014-07-26 14:27:28 +00:00
def _write(self, msg, protobuf_msg):
2013-03-10 15:55:59 +00:00
msg = bytearray(msg)
2016-02-10 15:46:58 +00:00
while len(msg):
2013-09-24 23:14:54 +00:00
# Report ID, data padded to 63 bytes
self.hid.write([63, ] + list(msg[:63]) + [0] * (63 - len(msg[:63])))
2013-09-09 13:37:39 +00:00
msg = msg[63:]
2016-02-10 15:46:58 +00:00
2013-03-10 15:55:59 +00:00
def _read(self):
2013-03-10 16:52:04 +00:00
(msg_type, datalen) = self._read_headers(FakeRead(self._raw_read))
2013-03-10 15:55:59 +00:00
return (msg_type, self._raw_read(datalen))
2016-02-10 15:46:58 +00:00
def _raw_read(self, length):
start = time.time()
2013-03-10 15:55:59 +00:00
while len(self.buffer) < length:
data = self.hid.read(64)
2013-09-24 23:14:54 +00:00
if not len(data):
if time.time() - start > 10:
# Over 10 s of no response, let's check if
# device is still alive
if not self.is_connected():
raise ConnectionError("Connection failed")
else:
# Restart timer
start = time.time()
2015-02-25 16:54:27 +00:00
time.sleep(0.001)
2013-09-24 23:14:54 +00:00
continue
2013-09-09 13:37:39 +00:00
2013-03-10 15:55:59 +00:00
report_id = data[0]
2016-02-10 15:46:58 +00:00
2013-03-10 15:55:59 +00:00
if report_id > 63:
# Command report
raise Exception("Not implemented")
2016-02-10 15:46:58 +00:00
2013-09-09 13:37:39 +00:00
# Payload received, skip the report ID
self.buffer.extend(bytearray(data[1:]))
2013-03-10 15:55:59 +00:00
ret = self.buffer[:length]
self.buffer = self.buffer[length:]
return bytes(ret)