1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-25 07:48:10 +00:00
trezor-firmware/trezorlib/transport_udp.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

48 lines
1.3 KiB
Python

'''UDP Socket implementation of Transport.'''
import socket
from select import select
import time
from .transport import TransportV2, ConnectionError
class UdpTransport(TransportV2):
def __init__(self, device, *args, **kwargs):
device = device.split(':')
if len(device) < 2:
if not device[0]:
# Default port used by trezor v2
device = ('127.0.0.1', 21324)
else:
device = ('127.0.0.1', int(device[0]))
else:
device = (device[0], int(device[1]))
self.socket = None
super(UdpTransport, self).__init__(device, *args, **kwargs)
def _open(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.connect(self.device)
self.socket.settimeout(10)
def _close(self):
self.socket.close()
self.socket = None
def _ready_to_read(self):
rlist, _, _ = select([self.socket], [], [], 0)
return len(rlist) > 0
def _write_chunk(self, chunk):
if len(chunk) != 64:
raise Exception("Unexpected data length")
self.socket.sendall(chunk)
def _read_chunk(self):
data = self.socket.recv(64)
if len(data) != 64:
raise Exception("Unexpected chunk size: %d" % len(data))
return bytearray(data)