2016-11-25 21:53:55 +00:00
|
|
|
# This file is part of the TREZOR project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
|
|
|
|
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-05-20 20:27:20 +00:00
|
|
|
from __future__ import print_function
|
2013-09-13 03:32:27 +00:00
|
|
|
import os
|
|
|
|
from select import select
|
2016-09-27 20:49:51 +00:00
|
|
|
from .transport import TransportV1
|
2013-09-13 03:32:27 +00:00
|
|
|
|
2016-05-26 15:20:44 +00:00
|
|
|
"""PipeTransport implements fake wire transport over local named pipe.
|
|
|
|
Use this transport for talking with trezor simulator."""
|
|
|
|
|
2016-06-26 22:14:19 +00:00
|
|
|
class PipeTransport(TransportV1):
|
2013-09-13 03:32:27 +00:00
|
|
|
def __init__(self, device, is_device, *args, **kwargs):
|
|
|
|
self.is_device = is_device # Set True if act as device
|
2016-02-10 15:46:58 +00:00
|
|
|
|
|
|
|
super(PipeTransport, self).__init__(device, *args, **kwargs)
|
|
|
|
|
2013-09-13 03:32:27 +00:00
|
|
|
def _open(self):
|
|
|
|
if self.is_device:
|
|
|
|
self.filename_read = self.device+'.to'
|
|
|
|
self.filename_write = self.device+'.from'
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-05-05 01:16:17 +00:00
|
|
|
os.mkfifo(self.filename_read, 0o600)
|
|
|
|
os.mkfifo(self.filename_write, 0o600)
|
2013-09-13 03:32:27 +00:00
|
|
|
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")
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2013-09-13 03:32:27 +00:00
|
|
|
self.write_fd = os.open(self.filename_write, os.O_RDWR)#|os.O_NONBLOCK)
|
2016-05-05 02:57:14 +00:00
|
|
|
self.write_f = os.fdopen(self.write_fd, 'w+b', 0)
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2013-09-13 03:32:27 +00:00
|
|
|
self.read_fd = os.open(self.filename_read, os.O_RDWR)#|os.O_NONBLOCK)
|
2016-05-05 02:57:14 +00:00
|
|
|
self.read_f = os.fdopen(self.read_fd, 'rb', 0)
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2013-09-13 03:32:27 +00:00
|
|
|
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)
|
|
|
|
|
2016-06-26 22:14:19 +00:00
|
|
|
def _ready_to_read(self):
|
2013-09-13 03:32:27 +00:00
|
|
|
rlist, _, _ = select([self.read_f], [], [], 0)
|
|
|
|
return len(rlist) > 0
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 22:14:19 +00:00
|
|
|
def _write_chunk(self, chunk):
|
|
|
|
if len(chunk) != 64:
|
|
|
|
raise Exception("Unexpected data length")
|
|
|
|
|
2013-09-13 03:32:27 +00:00
|
|
|
try:
|
2016-06-26 22:14:19 +00:00
|
|
|
self.write_f.write(chunk)
|
2013-09-13 03:32:27 +00:00
|
|
|
self.write_f.flush()
|
|
|
|
except OSError:
|
2016-05-05 01:16:17 +00:00
|
|
|
print("Error while writing to socket")
|
2013-09-13 03:32:27 +00:00
|
|
|
raise
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 22:14:19 +00:00
|
|
|
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)
|