Skip to content

Commit 64286bb

Browse files
committed
fixup! feat(python): BLE support via bleak
1 parent c4377df commit 64286bb

File tree

1 file changed

+15
-10
lines changed
  • python/src/trezorlib/transport

1 file changed

+15
-10
lines changed

python/src/trezorlib/transport/ble.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def enumerate(
7676
# TODO use manufacturer_data
7777
if models and T3W1 not in models:
7878
return []
79-
devices = cls.ble().scan()
79+
devices = cls.ble_proxy().scan()
8080
return [BleTransport(device[0]) for device in devices]
8181

8282
@classmethod
@@ -98,26 +98,26 @@ def find_by_path(cls, path: str, prefix_search: bool = False) -> BleTransport:
9898
raise TransportException(f"No BLE device: {path}")
9999

100100
def open(self) -> None:
101-
self.ble().connect(self.device)
101+
self.ble_proxy().connect(self.device)
102102

103103
def close(self) -> None:
104-
# would be a logical place to call self.ble().disconnect()
104+
# would be a logical place to call self.ble_proxy().disconnect()
105105
# instead we rely on atexit handler to avoid reconnecting
106106
pass
107107

108108
def write_chunk(self, chunk: bytes) -> None:
109109
LOG.log(DUMP_PACKETS, f"sending packet: {chunk.hex()}")
110-
self.ble().write(self.device, chunk)
110+
self.ble_proxy().write(self.device, chunk)
111111

112112
def read_chunk(self, timeout: float | None = None) -> bytes:
113-
chunk = self.ble().read(self.device, timeout)
113+
chunk = self.ble_proxy().read(self.device, timeout)
114114
LOG.log(DUMP_PACKETS, f"received packet: {chunk.hex()}")
115115
if len(chunk) not in (64, 244):
116116
LOG.error(f"{__name__}: unexpected chunk size: {len(chunk)}")
117117
return bytearray(chunk)
118118

119119
@classmethod
120-
def ble(cls) -> BleProxy:
120+
def ble_proxy(cls) -> BleProxy:
121121
if cls._ble is None:
122122
cls._ble = BleProxy()
123123
return cls._ble
@@ -188,14 +188,22 @@ async def main(self, pipe: Connection):
188188
self.did_scan = False
189189
LOG.debug("async BLE process started")
190190

191+
try:
192+
await self._main_loop(pipe)
193+
finally:
194+
for address in self.devices.keys():
195+
await self.disconnect(address)
196+
197+
# returns after shutdown, or raises an exception
198+
async def _main_loop(self, pipe: Connection):
191199
while True:
192200
await ready(pipe)
193201
cmd, args, kwargs = pipe.recv()
194202
try:
195203
result = await getattr(self, cmd)(*args, **kwargs)
196204
except self.Shutdown:
197205
LOG.debug("async BLE exit loop")
198-
break
206+
return
199207
except Timeout as e:
200208
await ready(pipe, write=True)
201209
pipe.send(e)
@@ -207,9 +215,6 @@ async def main(self, pipe: Connection):
207215
await ready(pipe, write=True)
208216
pipe.send(result)
209217

210-
for address in self.devices.keys():
211-
await self.disconnect(address)
212-
213218
# throws exception when no adapters found
214219
async def scan(self) -> list[tuple[str, str]]:
215220
LOG.debug("scanning BLE")

0 commit comments

Comments
 (0)