python-trezor/trezorlib/transport_pipe.py
slush0 97ce804cb7 Pipe w/ trezor1-emu works
UDP write to trezor2-emu works, reads to be tested
2016-06-28 09:51:57 +02:00

75 lines
2.2 KiB
Python

from __future__ import print_function
import os
from select import select
from transport import TransportV1
"""PipeTransport implements fake wire transport over local named pipe.
Use this transport for talking with trezor simulator."""
class PipeTransport(TransportV1):
def __init__(self, device, is_device, *args, **kwargs):
self.is_device = is_device # Set True if act as device
super(PipeTransport, self).__init__(device, *args, **kwargs)
def _open(self):
if self.is_device:
self.filename_read = self.device+'.to'
self.filename_write = self.device+'.from'
os.mkfifo(self.filename_read, 0o600)
os.mkfifo(self.filename_write, 0o600)
else:
self.filename_read = self.device+'.from'
self.filename_write = self.device+'.to'
if not os.path.exists(self.filename_write):
raise Exception("Not connected")
self.write_fd = os.open(self.filename_write, os.O_RDWR)#|os.O_NONBLOCK)
self.write_f = os.fdopen(self.write_fd, 'w+b', 0)
self.read_fd = os.open(self.filename_read, os.O_RDWR)#|os.O_NONBLOCK)
self.read_f = os.fdopen(self.read_fd, 'rb', 0)
def _close(self):
self.read_f.close()
self.write_f.close()
if self.is_device:
os.unlink(self.filename_read)
os.unlink(self.filename_write)
def _ready_to_read(self):
rlist, _, _ = select([self.read_f], [], [], 0)
return len(rlist) > 0
def _write_chunk(self, chunk):
if len(chunk) != 64:
raise Exception("Unexpected data length")
try:
self.write_f.write(chunk)
self.write_f.flush()
except OSError:
print("Error while writing to socket")
raise
def _read_chunk(self):
while True:
try:
data = self.read_f.read(64)
except IOError:
print("Failed to read from device")
raise
if not len(data):
time.sleep(0.001)
continue
break
if len(data) != 64:
raise Exception("Unexpected chunk size: %d" % len(data))
return bytearray(data)