1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-10 01:30:19 +00:00
trezor-firmware/bitkeylib/transport.py

86 lines
2.4 KiB
Python
Raw Normal View History

2012-11-13 14:09:39 +00:00
import struct
2012-11-15 10:42:19 +00:00
import mapping
2012-11-13 14:09:39 +00:00
2013-01-05 14:42:09 +00:00
class NotImplementedException(Exception):
pass
2012-11-13 14:09:39 +00:00
class Transport(object):
def __init__(self, device, *args, **kwargs):
self.device = device
self._open()
def _open(self):
2013-01-05 14:42:09 +00:00
raise NotImplementedException("Not implemented")
2012-11-13 14:09:39 +00:00
def _close(self):
2013-01-05 14:42:09 +00:00
raise NotImplementedException("Not implemented")
2012-11-13 14:09:39 +00:00
def _write(self, msg):
2013-01-05 14:42:09 +00:00
raise NotImplementedException("Not implemented")
2012-11-13 14:09:39 +00:00
def _read(self):
2013-01-05 14:42:09 +00:00
raise NotImplementedException("Not implemented")
2012-11-13 14:09:39 +00:00
2012-11-15 20:08:02 +00:00
def ready_to_read(self):
2013-01-05 14:42:09 +00:00
raise NotImplementedException("Not implemented")
2012-11-15 20:08:02 +00:00
2012-11-13 14:09:39 +00:00
def close(self):
self._close()
def write(self, msg):
ser = msg.SerializeToString()
header = struct.pack(">HL", mapping.get_type(msg), len(ser))
self._write("##%s%s" % (header, ser))
2012-11-15 20:08:02 +00:00
2012-11-13 14:09:39 +00:00
def read(self):
2012-12-09 13:53:09 +00:00
if not self.ready_to_read():
return None
2012-12-09 13:53:09 +00:00
data = self._read()
if data == None:
return None
return self._parse_message(data)
def read_blocking(self):
while True:
data = self._read()
if data != None:
break
return self._parse_message(data)
def _parse_message(self, data):
2012-12-09 13:53:09 +00:00
(msg_type, data) = data
2012-11-13 14:09:39 +00:00
inst = mapping.get_class(msg_type)()
inst.ParseFromString(data)
return inst
def _read_headers(self, read_f):
# Try to read headers until some sane value are detected
is_ok = False
while not is_ok:
# Align cursor to the beginning of the header ("##")
c = read_f.read(1)
while c != '#':
if c == '':
# timeout
raise Exception("Timed out while waiting for the magic character")
print "Warning: Aligning to magic characters"
c = read_f.read(1)
if read_f.read(1) != "#":
# Second character must be # to be valid header
raise Exception("Second magic character is broken")
# Now we're most likely on the beginning of the header
try:
headerlen = struct.calcsize(">HL")
(msg_type, datalen) = struct.unpack(">HL", read_f.read(headerlen))
break
except:
raise Exception("Cannot parse header length")
2012-11-15 10:42:19 +00:00
return (msg_type, datalen)