mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-22 05:10:56 +00:00
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
from trezor.loop import wait
|
|
|
|
|
|
class MockHID:
|
|
def __init__(self, num):
|
|
self.num = num
|
|
self.data = []
|
|
self.packet = None
|
|
|
|
def pad_packet(self, data):
|
|
if len(data) > 64:
|
|
raise Exception("Too long packet")
|
|
padding_length = 64 - len(data)
|
|
return data + b"\x00" * padding_length
|
|
|
|
def iface_num(self):
|
|
return self.num
|
|
|
|
def write(self, msg):
|
|
self.data.append(bytearray(msg))
|
|
return len(msg)
|
|
|
|
def mock_read(self, packet, gen):
|
|
self.packet = self.pad_packet(packet)
|
|
return gen.send(64)
|
|
|
|
def read(self, buffer, offset=0):
|
|
if self.packet is None:
|
|
raise Exception("No packet to read")
|
|
|
|
if offset > len(buffer):
|
|
raise Exception("Offset out of bounds")
|
|
|
|
buffer_space = len(buffer) - offset
|
|
|
|
if len(self.packet) > buffer_space:
|
|
raise Exception("Buffer too small")
|
|
else:
|
|
end = offset + len(self.packet)
|
|
buffer[offset:end] = self.packet
|
|
read = len(self.packet)
|
|
self.packet = None
|
|
return read
|
|
|
|
def wait_object(self, mode):
|
|
return wait(mode | self.num)
|