1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-14 03:30:02 +00:00

Fixes bug with aligning the magic

This commit is contained in:
slush0 2013-09-25 01:14:54 +02:00
parent 98bb17299b
commit ae9444a83b
2 changed files with 12 additions and 5 deletions

View File

@ -81,11 +81,13 @@ class Transport(object):
# Align cursor to the beginning of the header ("##")
c = read_f.read(1)
i = 0
while c != '#':
if c == '':
i += 1
if i >= 64:
# timeout
raise Exception("Timed out while waiting for the magic character")
print "Warning: Aligning to magic characters"
#print "Aligning to magic characters"
c = read_f.read(1)
if read_f.read(1) != "#":

View File

@ -1,6 +1,7 @@
'''USB HID implementation of Transport.'''
import hid
import time
from transport import Transport, NotImplementedException
DEVICE_IDS = [
@ -40,6 +41,7 @@ class HidTransport(Transport):
self.buffer = ''
path = self.device.split(':')
self.hid = hid.device(int(path[0], 16), int(path[1], 16))#, path[2])
self.hid.set_nonblocking(True)
self.hid.send_feature_report([0x41, 0x01]) # enable UART
self.hid.send_feature_report([0x43, 0x03]) # purge TX/RX FIFOs
@ -54,8 +56,8 @@ class HidTransport(Transport):
def _write(self, msg):
msg = bytearray(msg)
while len(msg):
# Report ID, data padded to 62 bytes
self.hid.write([63,] + list(msg[:63]) + [0]*(63-len(msg[0:63])))
# Report ID, data padded to 63 bytes
self.hid.write([63, ] + list(msg[:63]) + [0] * (63 - len(msg[:63])))
msg = msg[63:]
def _read(self):
@ -65,6 +67,9 @@ class HidTransport(Transport):
def _raw_read(self, length):
while len(self.buffer) < length:
data = self.hid.read(64)
if not len(data):
time.sleep(0.05)
continue
report_id = data[0]
@ -73,7 +78,7 @@ class HidTransport(Transport):
raise Exception("Not implemented")
# Payload received, skip the report ID
self.buffer += str(bytearray(data[1:64]))
self.buffer += str(bytearray(data[1:]))
ret = self.buffer[:length]
self.buffer = self.buffer[length:]