Skip to content

aioble/l2cap: Catch disconnection and raise as DeviceDisconnectedError. #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions micropython/bluetooth/aioble/aioble/l2cap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import asyncio

from .core import ble, log_error, register_irq_handler
from .device import DeviceConnection
from .device import DeviceConnection, DeviceDisconnectedError


_IRQ_L2CAP_ACCEPT = const(22)
Expand Down Expand Up @@ -180,35 +180,46 @@
# Use connection.l2cap_accept() instead of calling this directly.
async def accept(connection, psm, mtu, timeout_ms):
global _listening
try:

channel = L2CAPChannel(connection)
channel = L2CAPChannel(connection)

# Start the stack listening if necessary.
if not _listening:
ble.l2cap_listen(psm, mtu)
_listening = True
# Start the stack listening if necessary.
if not _listening:
ble.l2cap_listen(psm, mtu)
_listening = True

# Wait for the connect irq from the remote connection.
with connection.timeout(timeout_ms):
await channel._event.wait()
return channel
# Wait for the connect irq from the remote connection.
with connection.timeout(timeout_ms):
await channel._event.wait()
return channel
except ValueError as ex:
if ex.value == 'Not connected':
raise DeviceDisconnectedError()

Check failure on line 198 in micropython/bluetooth/aioble/aioble/l2cap.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RSE102)

micropython/bluetooth/aioble/aioble/l2cap.py:198:42: RSE102 Unnecessary parentheses on raised exception
raise


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

channel = L2CAPChannel(connection)
try:
channel = L2CAPChannel(connection)

with connection.timeout(timeout_ms):
ble.l2cap_connect(connection._conn_handle, psm, mtu)
with connection.timeout(timeout_ms):
ble.l2cap_connect(connection._conn_handle, psm, mtu)

# Wait for the connect irq from the remote connection.
# If the connection fails, we get a disconnect event (with status) instead.
await channel._event.wait()
# Wait for the connect irq from the remote connection.
# If the connection fails, we get a disconnect event (with status) instead.
await channel._event.wait()

if channel._cid is not None:
return channel
else:
raise L2CAPConnectionError(channel._status)
if channel._cid is not None:
return channel
else:
raise L2CAPConnectionError(channel._status)

except ValueError as ex:
if ex.value == 'Not connected':
raise DeviceDisconnectedError()

Check failure on line 224 in micropython/bluetooth/aioble/aioble/l2cap.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RSE102)

micropython/bluetooth/aioble/aioble/l2cap.py:224:42: RSE102 Unnecessary parentheses on raised exception
raise

Check failure on line 225 in micropython/bluetooth/aioble/aioble/l2cap.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

micropython/bluetooth/aioble/aioble/l2cap.py:225:14: W292 No newline at end of file
Loading