-
-
Notifications
You must be signed in to change notification settings - Fork 743
Expand file tree
/
Copy pathdevice_handler.py
More file actions
150 lines (118 loc) · 4.78 KB
/
device_handler.py
File metadata and controls
150 lines (118 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from __future__ import annotations
import typing as t
from concurrent.futures import ThreadPoolExecutor
import typing_extensions as tx
from trezorlib.messages import DebugWaitType
from trezorlib.transport import udp
if t.TYPE_CHECKING:
from trezorlib._internal.emulator import Emulator
from trezorlib.client import Session
from trezorlib.debuglink import DebugLink
from trezorlib.debuglink import TrezorTestContext as Client
from trezorlib.messages import Features
P = tx.ParamSpec("P")
udp.SOCKET_TIMEOUT = 0.1
class NullUI:
@staticmethod
def clear(*args, **kwargs):
pass
@staticmethod
def button_request(code):
pass
@staticmethod
def get_pin(code=None):
raise NotImplementedError("NullUI should not be used with T1")
class BackgroundDeviceHandler:
_pool = ThreadPoolExecutor()
def __init__(self, client: "Client", nowait: bool = False) -> None:
self._configure_client(client)
self.task = None
self.nowait = nowait
def _configure_client(self, client: "Client") -> None:
self.client = client
self.client.ui = NullUI # type: ignore [NullUI is OK UI]
self.client.app.button_callback = self.client.ui.button_request
self.client.watch_layout(True)
self.client.debug.input_wait_type = DebugWaitType.CURRENT_LAYOUT
def get_session(self, *args, **kwargs):
if self.task is not None:
raise RuntimeError("Wait for previous task first")
with self.debuglink().wait_for_layout_change():
self.task = self._pool.submit(self.client.get_session, *args, **kwargs)
def run_with_session(
self,
function: t.Callable[tx.Concatenate["Session", P], t.Any],
seedless: bool = False,
*args: P.args,
**kwargs: P.kwargs,
) -> None:
"""Runs some function that interacts with a device.
Makes sure the UI is updated before returning.
"""
if self.task is not None:
raise RuntimeError("Wait for previous task first")
def task_function(*args, **kwargs):
if seedless:
session = self.client.get_seedless_session()
else:
session = self.client.get_session()
return function(session, *args, **kwargs)
# wait for the first UI change triggered by the task running in the background
with self.debuglink().wait_for_layout_change():
self.task = self._pool.submit(task_function, *args, **kwargs)
def run_with_provided_session(
self,
session,
function: t.Callable[tx.Concatenate["Session", P], t.Any],
*args: P.args,
**kwargs: P.kwargs,
) -> None:
"""Runs some function that interacts with a device.
Makes sure the UI is updated before returning.
"""
if self.task is not None:
raise RuntimeError("Wait for previous task first")
# wait for the first UI change triggered by the task running in the background
with self.debuglink().wait_for_layout_change():
self.task = self._pool.submit(function, session, *args, **kwargs)
def kill_task(self) -> None:
if self.task is not None:
# Force close the transport, which should raise an exception in a client
# waiting on IO. Does not work over Bridge, because bridge doesn't have
# a close() method.
self.client.transport.close()
try:
self.task.result(timeout=1)
except Exception:
pass
self.task = None
def restart(self, emulator: "Emulator") -> None:
# TODO handle actual restart as well
self.kill_task()
emulator.restart()
self._configure_client(emulator.client) # type: ignore [client cannot be None]
def result(self, timeout: float | None = None) -> t.Any:
if self.task is None:
raise RuntimeError("No task running")
try:
return self.task.result(timeout=timeout)
finally:
self.task = None
def features(self) -> "Features":
if self.task is not None:
raise RuntimeError("Cannot query features while task is running")
self.client.refresh_features()
return self.client.features
def debuglink(self) -> "DebugLink":
return self.client.debug
def check_finalize(self) -> bool:
if self.task is not None:
self.kill_task()
return False
return True
def __enter__(self) -> "BackgroundDeviceHandler":
return self
def __exit__(self, exc_type, exc_value, traceback) -> None:
finalized_ok = self.check_finalize()
if exc_type is None and not finalized_ok:
raise RuntimeError("Exit while task is unfinished")