Skip to content

Commit a51c737

Browse files
committed
aioble: Use DeviceDisconnectedError for disconnected guards.
Replace ValueError("Not connected") with DeviceDisconnectedError in the is_connected() guards in L2CAPChannel.__init__, DeviceConnection.exchange_mtu, and Characteristic.indicate. Callers already catch DeviceDisconnectedError for disconnect handling; the ValueError was not semantically correct and required string matching to distinguish from other ValueErrors. Also make L2CAPDisconnectedError a subclass of DeviceDisconnectedError so that mid-operation L2CAP disconnections are caught by the same handler (e.g. in the l2cap_file_server example). Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent d0511a4 commit a51c737

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

micropython/bluetooth/aioble/aioble/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def timeout(self, timeout_ms):
273273

274274
async def exchange_mtu(self, mtu=None, timeout_ms=1000):
275275
if not self.is_connected():
276-
raise ValueError("Not connected")
276+
raise DeviceDisconnectedError
277277

278278
if mtu:
279279
ble.config(mtu=mtu)

micropython/bluetooth/aioble/aioble/l2cap.py

Lines changed: 3 additions & 3 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)
@@ -63,7 +63,7 @@ def _l2cap_shutdown():
6363

6464

6565
# The channel was disconnected during a send/recvinto/flush.
66-
class L2CAPDisconnectedError(Exception):
66+
class L2CAPDisconnectedError(DeviceDisconnectedError):
6767
pass
6868

6969

@@ -75,7 +75,7 @@ class L2CAPConnectionError(Exception):
7575
class L2CAPChannel:
7676
def __init__(self, connection):
7777
if not connection.is_connected():
78-
raise ValueError("Not connected")
78+
raise L2CAPDisconnectedError
7979

8080
if connection._l2cap_channel:
8181
raise ValueError("Already has channel")

micropython/bluetooth/aioble/aioble/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
register_irq_handler,
1616
GattError,
1717
)
18-
from .device import DeviceConnection, DeviceTimeout
18+
from .device import DeviceConnection, DeviceDisconnectedError, DeviceTimeout
1919

2020
_registered_characteristics = {}
2121

@@ -263,7 +263,7 @@ async def indicate(self, connection, data=None, timeout_ms=1000):
263263
if self._indicate_connection is not None:
264264
raise ValueError("In progress")
265265
if not connection.is_connected():
266-
raise ValueError("Not connected")
266+
raise DeviceDisconnectedError
267267

268268
self._indicate_connection = connection
269269
self._indicate_status = None

0 commit comments

Comments
 (0)