@@ -76,7 +76,7 @@ def enumerate(
76
76
# TODO use manufacturer_data
77
77
if models and T3W1 not in models :
78
78
return []
79
- devices = cls .ble ().scan ()
79
+ devices = cls .ble_proxy ().scan ()
80
80
return [BleTransport (device [0 ]) for device in devices ]
81
81
82
82
@classmethod
@@ -98,26 +98,26 @@ def find_by_path(cls, path: str, prefix_search: bool = False) -> BleTransport:
98
98
raise TransportException (f"No BLE device: { path } " )
99
99
100
100
def open (self ) -> None :
101
- self .ble ().connect (self .device )
101
+ self .ble_proxy ().connect (self .device )
102
102
103
103
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()
105
105
# instead we rely on atexit handler to avoid reconnecting
106
106
pass
107
107
108
108
def write_chunk (self , chunk : bytes ) -> None :
109
109
LOG .log (DUMP_PACKETS , f"sending packet: { chunk .hex ()} " )
110
- self .ble ().write (self .device , chunk )
110
+ self .ble_proxy ().write (self .device , chunk )
111
111
112
112
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 )
114
114
LOG .log (DUMP_PACKETS , f"received packet: { chunk .hex ()} " )
115
115
if len (chunk ) not in (64 , 244 ):
116
116
LOG .error (f"{ __name__ } : unexpected chunk size: { len (chunk )} " )
117
117
return bytearray (chunk )
118
118
119
119
@classmethod
120
- def ble (cls ) -> BleProxy :
120
+ def ble_proxy (cls ) -> BleProxy :
121
121
if cls ._ble is None :
122
122
cls ._ble = BleProxy ()
123
123
return cls ._ble
@@ -188,14 +188,22 @@ async def main(self, pipe: Connection):
188
188
self .did_scan = False
189
189
LOG .debug ("async BLE process started" )
190
190
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 ):
191
199
while True :
192
200
await ready (pipe )
193
201
cmd , args , kwargs = pipe .recv ()
194
202
try :
195
203
result = await getattr (self , cmd )(* args , ** kwargs )
196
204
except self .Shutdown :
197
205
LOG .debug ("async BLE exit loop" )
198
- break
206
+ return
199
207
except Timeout as e :
200
208
await ready (pipe , write = True )
201
209
pipe .send (e )
@@ -207,9 +215,6 @@ async def main(self, pipe: Connection):
207
215
await ready (pipe , write = True )
208
216
pipe .send (result )
209
217
210
- for address in self .devices .keys ():
211
- await self .disconnect (address )
212
-
213
218
# throws exception when no adapters found
214
219
async def scan (self ) -> list [tuple [str , str ]]:
215
220
LOG .debug ("scanning BLE" )
0 commit comments