Skip to content

Commit 2117b6f

Browse files
committed
aioble/l2cap: Catch disconnection and raise as DeviceDisconnectedError.
1 parent 5b496e9 commit 2117b6f

File tree

1 file changed

+31
-20
lines changed
  • micropython/bluetooth/aioble/aioble

1 file changed

+31
-20
lines changed

micropython/bluetooth/aioble/aioble/l2cap.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import asyncio
77

88
from .core import ble, log_error, register_irq_handler
9-
from .device import DeviceConnection
9+
from .device import DeviceConnection, DeviceDisconnectedError
1010

1111

1212
_IRQ_L2CAP_ACCEPT = const(22)
@@ -180,35 +180,46 @@ async def __aexit__(self, exc_type, exc_val, exc_traceback):
180180
# Use connection.l2cap_accept() instead of calling this directly.
181181
async def accept(connection, psm, mtu, timeout_ms):
182182
global _listening
183+
try:
183184

184-
channel = L2CAPChannel(connection)
185+
channel = L2CAPChannel(connection)
185186

186-
# Start the stack listening if necessary.
187-
if not _listening:
188-
ble.l2cap_listen(psm, mtu)
189-
_listening = True
187+
# Start the stack listening if necessary.
188+
if not _listening:
189+
ble.l2cap_listen(psm, mtu)
190+
_listening = True
190191

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
195200

196201

197202
# Use connection.l2cap_connect() instead of calling this directly.
198203
async def connect(connection, psm, mtu, timeout_ms):
199204
if _listening:
200205
raise ValueError("Can't connect while listening")
201206

202-
channel = L2CAPChannel(connection)
207+
try:
208+
channel = L2CAPChannel(connection)
203209

204-
with connection.timeout(timeout_ms):
205-
ble.l2cap_connect(connection._conn_handle, psm, mtu)
210+
with connection.timeout(timeout_ms):
211+
ble.l2cap_connect(connection._conn_handle, psm, mtu)
206212

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()
210216

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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy