Skip to content

Commit 4eca838

Browse files
committed
fix(python): enable BLE only if TREZOR_BLE=1 or --ble given
[no changelog]
1 parent 49c4b61 commit 4eca838

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

python/src/trezorlib/cli/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@ def __init__(
139139
session_id: bytes | None,
140140
passphrase_on_host: bool,
141141
script: bool,
142+
ble_enabled: bool,
142143
) -> None:
143144
self.path = path
144145
self.session_id = session_id
145146
self.passphrase_on_host = passphrase_on_host
146147
self.script = script
148+
self.ble_enabled = ble_enabled
147149

148150
def get_session(
149151
self,
@@ -208,15 +210,19 @@ def get_transport(self) -> "Transport":
208210

209211
try:
210212
# look for transport without prefix search
211-
_TRANSPORT = transport.get_transport(self.path, prefix_search=False)
213+
_TRANSPORT = transport.get_transport(
214+
self.path, prefix_search=False, ble_enabled=self.ble_enabled
215+
)
212216
except Exception:
213217
# most likely not found. try again below.
214218
pass
215219

216220
# look for transport with prefix search
217221
# if this fails, we want the exception to bubble up to the caller
218222
if not _TRANSPORT:
219-
_TRANSPORT = transport.get_transport(self.path, prefix_search=True)
223+
_TRANSPORT = transport.get_transport(
224+
self.path, prefix_search=True, ble_enabled=self.ble_enabled
225+
)
220226

221227
_TRANSPORT.open()
222228
atexit.register(_TRANSPORT.close)

python/src/trezorlib/cli/trezorctl.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ def configure_logging(verbose: int) -> None:
167167
help="Select device by specific path.",
168168
default=os.environ.get("TREZOR_PATH"),
169169
)
170+
@click.option(
171+
"-B",
172+
"--ble/--no-ble",
173+
help="Enable/disable support for Bluetooth Low Energy.",
174+
is_flag=True,
175+
default=(os.environ.get("TREZOR_BLE") == "1"),
176+
)
170177
@click.option("-v", "--verbose", count=True, help="Show communication messages.")
171178
@click.option(
172179
"-j", "--json", "is_json", is_flag=True, help="Print result as JSON object"
@@ -200,6 +207,7 @@ def configure_logging(verbose: int) -> None:
200207
def cli_main(
201208
ctx: click.Context,
202209
path: str,
210+
ble: bool,
203211
verbose: int,
204212
is_json: bool,
205213
passphrase_on_host: bool,
@@ -216,7 +224,9 @@ def cli_main(
216224
except ValueError:
217225
raise click.ClickException(f"Not a valid session id: {session_id}")
218226

219-
ctx.obj = TrezorConnection(path, bytes_session_id, passphrase_on_host, script)
227+
ctx.obj = TrezorConnection(
228+
path, bytes_session_id, passphrase_on_host, script, ble_enabled=ble
229+
)
220230

221231
# Optionally record the screen into a specified directory.
222232
if record:
@@ -284,16 +294,19 @@ def format_device_name(features: messages.Features) -> str:
284294

285295
@cli.command(name="list")
286296
@click.option("-n", "no_resolve", is_flag=True, help="Do not resolve Trezor names")
287-
def list_devices(no_resolve: bool) -> Optional[Iterable["Transport"]]:
297+
@click.pass_obj
298+
def list_devices(
299+
obj: TrezorConnection, no_resolve: bool
300+
) -> Optional[Iterable["Transport"]]:
288301
"""List connected Trezor devices."""
289302
if no_resolve:
290-
for d in enumerate_devices():
303+
for d in enumerate_devices(ble_enabled=obj.ble_enabled):
291304
click.echo(d.get_path())
292305
return
293306

294307
from . import get_client
295308

296-
for transport in enumerate_devices():
309+
for transport in enumerate_devices(ble_enabled=obj.ble_enabled):
297310
try:
298311
transport.open()
299312
client = get_client(transport)

python/src/trezorlib/transport/__init__.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import annotations
1818

1919
import logging
20+
import os
2021
import typing as t
2122

2223
from ..exceptions import TrezorException
@@ -95,7 +96,7 @@ def ping(self) -> bool:
9596
CHUNK_SIZE: t.ClassVar[int | None]
9697

9798

98-
def all_transports() -> t.Iterable[t.Type["Transport"]]:
99+
def all_transports(ble_enabled: bool | None = None) -> t.Iterable[t.Type["Transport"]]:
99100
from .ble import BleTransport
100101
from .bridge import BridgeTransport
101102
from .hid import HidTransport
@@ -107,16 +108,20 @@ def all_transports() -> t.Iterable[t.Type["Transport"]]:
107108
HidTransport,
108109
UdpTransport,
109110
WebUsbTransport,
110-
BleTransport,
111111
)
112+
if ble_enabled is None:
113+
ble_enabled = os.environ.get("TREZOR_BLE") == "1"
114+
if ble_enabled:
115+
transports += (BleTransport,)
112116
return set(t for t in transports if t.ENABLED)
113117

114118

115119
def enumerate_devices(
116120
models: t.Iterable[TrezorModel] | None = None,
121+
ble_enabled: bool | None = None,
117122
) -> t.Sequence[Transport]:
118123
devices: t.List[Transport] = []
119-
for transport in all_transports():
124+
for transport in all_transports(ble_enabled=ble_enabled):
120125
name = transport.__name__
121126
try:
122127
found = list(transport.enumerate(models))
@@ -130,10 +135,14 @@ def enumerate_devices(
130135
return devices
131136

132137

133-
def get_transport(path: str | None = None, prefix_search: bool = False) -> Transport:
138+
def get_transport(
139+
path: str | None = None,
140+
prefix_search: bool = False,
141+
ble_enabled: bool | None = None,
142+
) -> Transport:
134143
if path is None:
135144
try:
136-
return next(iter(enumerate_devices()))
145+
return next(iter(enumerate_devices(ble_enabled=ble_enabled)))
137146
except StopIteration:
138147
raise TransportException("No Trezor device found") from None
139148

@@ -148,7 +157,11 @@ def match_prefix(a: str, b: str) -> bool:
148157
"prefix" if prefix_search else "full path", path
149158
)
150159
)
151-
transports = [t for t in all_transports() if match_prefix(path, t.PATH_PREFIX)]
160+
transports = [
161+
t
162+
for t in all_transports(ble_enabled=ble_enabled)
163+
if match_prefix(path, t.PATH_PREFIX)
164+
]
152165
if transports:
153166
return transports[0].find_by_path(path, prefix_search=prefix_search)
154167

0 commit comments

Comments
 (0)