|
6 | 6 | import uasyncio as asyncio
|
7 | 7 |
|
8 | 8 | from .core import ble, log_error, register_irq_handler
|
9 |
| -from .device import DeviceConnection |
| 9 | +from .device import DeviceConnection, DeviceDisconnectedError |
10 | 10 |
|
11 | 11 |
|
12 | 12 | _IRQ_L2CAP_ACCEPT = const(22)
|
@@ -180,35 +180,46 @@ async def __aexit__(self, exc_type, exc_val, exc_traceback):
|
180 | 180 | # Use connection.l2cap_accept() instead of calling this directly.
|
181 | 181 | async def accept(connection, psn, mtu, timeout_ms):
|
182 | 182 | global _listening
|
| 183 | + try: |
183 | 184 |
|
184 |
| - channel = L2CAPChannel(connection) |
| 185 | + channel = L2CAPChannel(connection) |
185 | 186 |
|
186 |
| - # Start the stack listening if necessary. |
187 |
| - if not _listening: |
188 |
| - ble.l2cap_listen(psn, mtu) |
189 |
| - _listening = True |
| 187 | + # Start the stack listening if necessary. |
| 188 | + if not _listening: |
| 189 | + ble.l2cap_listen(psn, mtu) |
| 190 | + _listening = True |
190 | 191 |
|
191 |
| - # Wait for the connect irq from the remote connection. |
192 |
| - with connection.timeout(timeout_ms): |
193 |
| - await channel._event.wait() |
194 |
| - return channel |
| 192 | + # Wait for the connect irq from the remote connection. |
| 193 | + with connection.timeout(timeout_ms): |
| 194 | + await channel._event.wait() |
| 195 | + return channel |
| 196 | + except ValueError as ex: |
| 197 | + if ex.value == 'Not connected': |
| 198 | + raise DeviceDisconnectedError() |
| 199 | + raise |
195 | 200 |
|
196 | 201 |
|
197 | 202 | # Use connection.l2cap_connect() instead of calling this directly.
|
198 | 203 | async def connect(connection, psn, mtu, timeout_ms):
|
199 | 204 | if _listening:
|
200 | 205 | raise ValueError("Can't connect while listening")
|
201 | 206 |
|
202 |
| - channel = L2CAPChannel(connection) |
| 207 | + try: |
| 208 | + channel = L2CAPChannel(connection) |
203 | 209 |
|
204 |
| - with connection.timeout(timeout_ms): |
205 |
| - ble.l2cap_connect(connection._conn_handle, psn, mtu) |
| 210 | + with connection.timeout(timeout_ms): |
| 211 | + ble.l2cap_connect(connection._conn_handle, psn, mtu) |
206 | 212 |
|
207 |
| - # Wait for the connect irq from the remote connection. |
208 |
| - # If the connection fails, we get a disconnect event (with status) instead. |
209 |
| - await channel._event.wait() |
| 213 | + # Wait for the connect irq from the remote connection. |
| 214 | + # If the connection fails, we get a disconnect event (with status) instead. |
| 215 | + await channel._event.wait() |
210 | 216 |
|
211 |
| - if channel._cid is not None: |
212 |
| - return channel |
213 |
| - else: |
214 |
| - raise L2CAPConnectionError(channel._status) |
| 217 | + if channel._cid is not None: |
| 218 | + return channel |
| 219 | + else: |
| 220 | + raise L2CAPConnectionError(channel._status) |
| 221 | + |
| 222 | + except ValueError as ex: |
| 223 | + if ex.value == 'Not connected': |
| 224 | + raise DeviceDisconnectedError() |
| 225 | + raise |
0 commit comments