Skip to content

Commit 0613cdc

Browse files
Improve error reporting. Specially when there is a lack of permissions.
1 parent d69777f commit 0613cdc

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

src/usbbluetooth/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
from .list_controllers import list_controllers
88
from .controller import Controller
9-
from .wrong_driver_exception import WrongDriverException
10-
from .device_closed_exception import DeviceClosedException
9+
from .exception.device_closed_exception import DeviceClosedException
10+
from .exception.insufficient_permissions_exception import InsufficientPermissionsException
11+
from .exception.wrong_driver_exception import WrongDriverException

src/usbbluetooth/controller.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
import usb
88
from .hci_hdr_type import HciHdrType
9-
from .wrong_driver_exception import WrongDriverException
10-
from .device_closed_exception import DeviceClosedException
9+
from .exception.wrong_driver_exception import WrongDriverException
10+
from .exception.device_closed_exception import DeviceClosedException
11+
from .exception.insufficient_permissions_exception import InsufficientPermissionsException
1112

1213

1314
class Controller:
@@ -38,6 +39,14 @@ def open(self):
3839
# In windows, set_configuration is not implemented for devices not
3940
# running the correct driver, that should be WinUSB
4041
raise WrongDriverException()
42+
except usb.core.USBError as e:
43+
if e.errno == 13:
44+
# This happens in Linux when the user has insufficient permissions
45+
# to access the USB device
46+
raise InsufficientPermissionsException()
47+
else:
48+
# This may be anything, report it to the user...
49+
raise e
4150

4251
# Find the Bluetooth interface of the usb device...
4352
self._interface_bt = usb.util.find_descriptor(
@@ -51,14 +60,16 @@ def open(self):
5160
try:
5261
if self._dev.is_kernel_driver_active(self._interface_bt.bInterfaceNumber):
5362
# Detach the kernel driver
54-
self._dev.detach_kernel_driver(self._interface_bt.bInterfaceNumber)
63+
self._dev.detach_kernel_driver(
64+
self._interface_bt.bInterfaceNumber)
5565
except NotImplementedError:
5666
# In windows, is_kernel_driver_active and detach_kernel_driver are
5767
# not implemented
5868
pass
5969

6070
# Claim the interface
61-
usb.util.claim_interface(self._dev, self._interface_bt.bInterfaceNumber)
71+
usb.util.claim_interface(
72+
self._dev, self._interface_bt.bInterfaceNumber)
6273

6374
# Get the relevant endpoints
6475
self._ep_events = usb.util.find_descriptor(

src/usbbluetooth/device_closed_exception.py renamed to src/usbbluetooth/exception/device_closed_exception.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ class DeviceClosedException(Exception):
99
"""Exception raised when interacting with a closed device."""
1010

1111
def __init__(self):
12-
super().__init__("The device you are trying to interact with has not "
13-
"been opened. Please, call the open() function "
14-
"before reading or writing to a device. "
15-
"Alternatively, use the `with` syntax to open and "
16-
"close the device.")
12+
super().__init__("The device you are trying to interact with has not been opened. Please, call the open() "
13+
"function before reading or writing to a device. Alternatively, use the `with` syntax to open "
14+
"and close the device.")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
#
3+
# SPDX-License-Identifier: GPL-3.0-only
4+
# SPDX-FileCopyrightText: 2025 Antonio Vázquez Blanco <antoniovazquezblanco@gmail.com>
5+
#
6+
7+
8+
class InsufficientPermissionsException(Exception):
9+
"""Exception raised when trying to access a device with insufficient permissions."""
10+
11+
def __init__(self):
12+
super().__init__("Current user has insufficient permissions to access this device. Please, visit "
13+
"https://usbbluetooth.github.io/quirks/ for more information. Depending on your operating "
14+
"system, you might need to set up udev rules or run this program with elevated privileges.")

src/usbbluetooth/wrong_driver_exception.py renamed to src/usbbluetooth/exception/wrong_driver_exception.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ class WrongDriverException(Exception):
99
"""Exception raised when trying to access a device with wrong driver."""
1010

1111
def __init__(self):
12-
super().__init__("The device you are trying to interact with is not "
13-
"accessible. This is most likely because it is being "
14-
"controlled by the wrong driver. In Windows, please "
15-
"use Zadig to change the driver of your device to "
16-
"WinUSB.")
12+
super().__init__("The device you are trying to interact with is not accessible. This is most likely because it "
13+
"is being controlled by the wrong driver. In Windows, please use Zadig to change the driver "
14+
"of your device to WinUSB.")

0 commit comments

Comments
 (0)