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-06-26 19:29:29 +00:00
|
|
|
from .transport import TransportV1, TransportV2, ConnectionError
|
|
|
|
|
|
|
|
def enumerate():
|
|
|
|
"""
|
|
|
|
Return a list of available TREZOR devices.
|
|
|
|
"""
|
|
|
|
devices = {}
|
|
|
|
for d in hid.enumerate(0, 0):
|
|
|
|
vendor_id = d['vendor_id']
|
|
|
|
product_id = d['product_id']
|
|
|
|
serial_number = d['serial_number']
|
|
|
|
interface_number = d['interface_number']
|
|
|
|
path = d['path']
|
|
|
|
|
|
|
|
# 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 [ x[0:2] for x in DEVICE_IDS]:
|
|
|
|
devices.setdefault(serial_number, [None, None])
|
|
|
|
if interface_number == 0 or interface_number == -1: # normal link
|
|
|
|
devices[serial_number][0] = path
|
|
|
|
elif interface_number == 1: # debug link
|
|
|
|
devices[serial_number][1] = path
|
|
|
|
|
|
|
|
# List of two-tuples (path_normal, path_debuglink)
|
|
|
|
return list(devices.values())
|
|
|
|
|
|
|
|
def path_to_transport(path):
|
|
|
|
try:
|
|
|
|
device = [ d for d in hid.enumerate(0, 0) if d['path'] == path ][0]
|
|
|
|
except IndexError:
|
|
|
|
raise ConnectionError("Connection failed")
|
|
|
|
|
|
|
|
# VID/PID found, let's find proper transport
|
|
|
|
vid, pid = device['vendor_id'], device['product_id']
|
|
|
|
try:
|
|
|
|
transport = [ transport for (_vid, _pid, transport) in DEVICE_IDS if _vid == vid and _pid == pid ][0]
|
|
|
|
except IndexError:
|
|
|
|
raise Exception("Unknown transport for VID:PID %04x:%04x" % (vid, pid))
|
|
|
|
|
|
|
|
return transport
|
|
|
|
|
|
|
|
class _HidTransport(object):
|
2013-03-10 15:55:59 +00:00
|
|
|
def __init__(self, device, *args, **kwargs):
|
|
|
|
self.hid = None
|
2016-06-23 16:38:34 +00:00
|
|
|
self.hid_version = None
|
2013-09-09 13:37:39 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
device = device[int(bool(kwargs.get('debug_link')))]
|
|
|
|
super(_HidTransport, self).__init__(device, *args, **kwargs)
|
2014-07-09 22:44:46 +00:00
|
|
|
|
|
|
|
def is_connected(self):
|
2014-08-26 14:06:19 +00:00
|
|
|
"""
|
|
|
|
Check if the device is still connected.
|
|
|
|
"""
|
2014-07-09 22:44:46 +00:00
|
|
|
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):
|
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-06-26 19:29:29 +00:00
|
|
|
|
2016-06-23 16:38:34 +00:00
|
|
|
# determine hid_version
|
|
|
|
r = self.hid.write([0, 63, ] + [0xFF] * 63)
|
|
|
|
if r == 65:
|
|
|
|
self.hid_version = 2
|
|
|
|
return
|
|
|
|
r = self.hid.write([63, ] + [0xFF] * 63)
|
|
|
|
if r == 64:
|
|
|
|
self.hid_version = 1
|
|
|
|
return
|
|
|
|
raise ConnectionError("Unknown HID version")
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2013-03-10 15:55:59 +00:00
|
|
|
def _close(self):
|
|
|
|
self.hid.close()
|
|
|
|
self.hid = None
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
def _write_chunk(self, chunk):
|
|
|
|
if len(chunk) != 64:
|
|
|
|
raise Exception("Unexpected data length")
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
if self.hid_version == 2:
|
|
|
|
self.hid.write([0,] + chunk)
|
|
|
|
else:
|
|
|
|
self.hid.write(chunk)
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
def _read_chunk(self):
|
2014-07-09 22:44:46 +00:00
|
|
|
start = time.time()
|
2016-06-26 19:29:29 +00:00
|
|
|
|
|
|
|
while True:
|
2013-03-10 15:55:59 +00:00
|
|
|
data = self.hid.read(64)
|
2013-09-24 23:14:54 +00:00
|
|
|
if not len(data):
|
2015-04-29 17:31:48 +00:00
|
|
|
if time.time() - start > 10:
|
|
|
|
# Over 10 s of no response, let's check if
|
2014-07-09 22:44:46 +00:00
|
|
|
# device is still alive
|
2015-04-29 17:31:48 +00:00
|
|
|
if not self.is_connected():
|
|
|
|
raise ConnectionError("Connection failed")
|
2016-06-26 19:29:29 +00:00
|
|
|
|
|
|
|
# Restart timer
|
|
|
|
start = time.time()
|
2014-07-09 22:44:46 +00:00
|
|
|
|
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
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if len(data) != 64:
|
|
|
|
raise Exception("Unexpected chunk size: %d" % len(data))
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
return bytearray(data)
|
|
|
|
|
|
|
|
class HidTransportV1(_HidTransport, TransportV1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class HidTransportV2(_HidTransport, TransportV2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
DEVICE_IDS = [
|
|
|
|
(0x534c, 0x0001, HidTransportV1), # TREZOR
|
|
|
|
(0x1209, 0x53C0, HidTransportV2), # TREZORv2 Bootloader
|
|
|
|
(0x1209, 0x53C1, HidTransportV2), # TREZORv2
|
|
|
|
]
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
# Backward compatible wrapper, decides for proper transport
|
|
|
|
# based on VID/PID of given path
|
|
|
|
def HidTransport(device, *args, **kwargs):
|
|
|
|
transport = path_to_transport(device[0])
|
|
|
|
return transport(device, *args, **kwargs)
|
2013-03-10 15:55:59 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
# Backward compatibility hack; HidTransport is a function, not a class like before
|
|
|
|
HidTransport.enumerate = enumerate
|