From a51c7371fb1b3530c1481403012d25087af941cc Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 7 Dec 2021 10:58:20 +1100 Subject: [PATCH] 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 --- micropython/bluetooth/aioble/aioble/device.py | 2 +- micropython/bluetooth/aioble/aioble/l2cap.py | 6 +++--- micropython/bluetooth/aioble/aioble/server.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/micropython/bluetooth/aioble/aioble/device.py b/micropython/bluetooth/aioble/aioble/device.py index 93819bc1e..feece62ea 100644 --- a/micropython/bluetooth/aioble/aioble/device.py +++ b/micropython/bluetooth/aioble/aioble/device.py @@ -273,7 +273,7 @@ def timeout(self, timeout_ms): async def exchange_mtu(self, mtu=None, timeout_ms=1000): if not self.is_connected(): - raise ValueError("Not connected") + raise DeviceDisconnectedError if mtu: ble.config(mtu=mtu) diff --git a/micropython/bluetooth/aioble/aioble/l2cap.py b/micropython/bluetooth/aioble/aioble/l2cap.py index 397b7ceb6..8330f6535 100644 --- a/micropython/bluetooth/aioble/aioble/l2cap.py +++ b/micropython/bluetooth/aioble/aioble/l2cap.py @@ -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) @@ -63,7 +63,7 @@ def _l2cap_shutdown(): # The channel was disconnected during a send/recvinto/flush. -class L2CAPDisconnectedError(Exception): +class L2CAPDisconnectedError(DeviceDisconnectedError): pass @@ -75,7 +75,7 @@ class L2CAPConnectionError(Exception): class L2CAPChannel: def __init__(self, connection): if not connection.is_connected(): - raise ValueError("Not connected") + raise L2CAPDisconnectedError if connection._l2cap_channel: raise ValueError("Already has channel") diff --git a/micropython/bluetooth/aioble/aioble/server.py b/micropython/bluetooth/aioble/aioble/server.py index 5d5d7399b..392381923 100644 --- a/micropython/bluetooth/aioble/aioble/server.py +++ b/micropython/bluetooth/aioble/aioble/server.py @@ -15,7 +15,7 @@ register_irq_handler, GattError, ) -from .device import DeviceConnection, DeviceTimeout +from .device import DeviceConnection, DeviceDisconnectedError, DeviceTimeout _registered_characteristics = {} @@ -263,7 +263,7 @@ async def indicate(self, connection, data=None, timeout_ms=1000): if self._indicate_connection is not None: raise ValueError("In progress") if not connection.is_connected(): - raise ValueError("Not connected") + raise DeviceDisconnectedError self._indicate_connection = connection self._indicate_status = None