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
|
2014-02-03 20:49:07 +00:00
|
|
|
import platform
|
2014-07-09 22:44:46 +00:00
|
|
|
from transport import Transport, ConnectionError, NotImplementedException
|
2013-03-10 15:55:59 +00:00
|
|
|
|
|
|
|
DEVICE_IDS = [
|
2013-11-15 00:43:05 +00:00
|
|
|
(0x10c4, 0xea80), # Shield
|
|
|
|
(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
|
|
|
|
|
|
|
|
def read(self, size):
|
|
|
|
return self.func(size)
|
2014-07-09 22:44:46 +00:00
|
|
|
|
2013-03-10 15:55:59 +00:00
|
|
|
class HidTransport(Transport):
|
|
|
|
def __init__(self, device, *args, **kwargs):
|
|
|
|
self.hid = None
|
|
|
|
self.buffer = ''
|
2014-07-09 22:44:46 +00:00
|
|
|
# self.read_timeout = kwargs.get('read_timeout')
|
2014-02-03 20:49:07 +00:00
|
|
|
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
|
|
|
|
2014-02-03 20:49:07 +00:00
|
|
|
@classmethod
|
|
|
|
def _detect_debuglink(cls, path):
|
|
|
|
# Takes platform-specific path of USB and
|
|
|
|
# decide if the HID interface is normal transport
|
|
|
|
# or debuglink
|
|
|
|
|
2014-02-13 18:04:51 +00:00
|
|
|
if platform.system() == 'Linux':
|
2014-02-03 20:49:07 +00:00
|
|
|
# Sample: 0003:0017:00
|
|
|
|
if path.endswith(':00'):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
elif platform.system() == 'Windows':
|
|
|
|
# Sample: \\\\?\\hid#vid_534c&pid_0001&mi_01#7&1d71791f&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
|
|
|
|
# Note: 'mi' parameter is optional and might be unset
|
|
|
|
if '&mi_01#' in path: # ,,,<o.O>,,,~
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-02-13 18:04:51 +00:00
|
|
|
elif platform.system() == 'Darwin':
|
|
|
|
# DebugLink doesn't work on Mac
|
|
|
|
return False
|
|
|
|
|
2014-02-03 20:49:07 +00:00
|
|
|
else:
|
|
|
|
raise Exception("USB interface detection not implemented for %s" % platform.system())
|
|
|
|
|
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.
|
|
|
|
"""
|
2014-02-03 20:49:07 +00:00
|
|
|
devices = {}
|
2013-03-10 15:55:59 +00:00
|
|
|
for d in hid.enumerate(0, 0):
|
2014-02-03 20:49:07 +00:00
|
|
|
vendor_id = d['vendor_id']
|
|
|
|
product_id = d['product_id']
|
|
|
|
serial_number = d['serial_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? :-/")
|
|
|
|
|
2014-02-03 20:49:07 +00:00
|
|
|
if (vendor_id, product_id) in DEVICE_IDS:
|
|
|
|
devices.setdefault(serial_number, [None, None])
|
|
|
|
devices[serial_number][int(bool(cls._detect_debuglink(path)))] = path
|
2013-03-10 15:55:59 +00:00
|
|
|
|
2014-02-03 20:49:07 +00:00
|
|
|
# List of two-tuples (path_normal, path_debuglink)
|
|
|
|
return devices.values()
|
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
|
2013-09-09 13:37:39 +00:00
|
|
|
|
2013-03-10 15:55:59 +00:00
|
|
|
def _open(self):
|
|
|
|
self.buffer = ''
|
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)
|
2013-04-01 14:59:16 +00:00
|
|
|
self.hid.send_feature_report([0x41, 0x01]) # enable UART
|
|
|
|
self.hid.send_feature_report([0x43, 0x03]) # purge TX/RX FIFOs
|
2013-03-10 15:55:59 +00:00
|
|
|
|
|
|
|
def _close(self):
|
|
|
|
self.hid.close()
|
|
|
|
self.buffer = ''
|
|
|
|
self.hid = None
|
|
|
|
|
|
|
|
def ready_to_read(self):
|
|
|
|
return False
|
|
|
|
|
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)
|
2013-09-09 13:37:39 +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:]
|
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))
|
|
|
|
|
2014-07-09 22:44:46 +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):
|
2014-07-09 22:44:46 +00:00
|
|
|
if time.time() - start > 10 and not self.is_connected():
|
|
|
|
# Over 10 of no response, let's check if
|
|
|
|
# device is still alive
|
|
|
|
raise ConnectionError("Connection failed")
|
|
|
|
|
2013-09-24 23:14:54 +00:00
|
|
|
time.sleep(0.05)
|
|
|
|
continue
|
2013-09-09 13:37:39 +00:00
|
|
|
|
2013-03-10 15:55:59 +00:00
|
|
|
report_id = data[0]
|
|
|
|
|
|
|
|
if report_id > 63:
|
|
|
|
# Command report
|
|
|
|
raise Exception("Not implemented")
|
2013-09-09 13:37:39 +00:00
|
|
|
|
|
|
|
# Payload received, skip the report ID
|
2013-09-24 23:14:54 +00:00
|
|
|
self.buffer += str(bytearray(data[1:]))
|
2013-03-10 15:55:59 +00:00
|
|
|
|
|
|
|
ret = self.buffer[:length]
|
|
|
|
self.buffer = self.buffer[length:]
|
2013-04-01 14:59:16 +00:00
|
|
|
return ret
|