gattlib/gattlib-py/gattlib/exception.py

122 lines
3.7 KiB
Python
Raw Permalink Normal View History

2021-09-01 00:00:36 +02:00
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
2021-09-01 00:00:36 +02:00
#
"""Gattlib Exceptions"""
GATTLIB_SUCCESS = 0
GATTLIB_INVALID_PARAMETER = 1
GATTLIB_NOT_FOUND = 2
GATTLIB_TIMEOUT = 3
GATTLIB_OUT_OF_MEMORY = 4
GATTLIB_NOT_SUPPORTED = 5
GATTLIB_DEVICE_ERROR = 6
GATTLIB_DEVICE_NOT_CONNECTED = 7
2024-02-20 23:23:39 +01:00
GATTLIB_NO_ADAPTER = 8
2024-04-03 15:15:05 +02:00
GATTLIB_BUSY = 9
GATTLIB_UNEXPECTED = 10
GATTLIB_ADAPTER_CLOSE = 11
GATTLIB_DEVICE_DISCONNECTED = 12
GATTLIB_ERROR_MODULE_MASK = 0xF0000000
GATTLIB_ERROR_DBUS = 0x10000000
GATTLIB_ERROR_BLUEZ = 0x20000000
GATTLIB_ERROR_INTERNAL = 0x80000000
2019-05-19 12:47:21 +02:00
class GattlibException(Exception):
"""Generic Gattlib exception."""
2019-05-19 12:47:21 +02:00
2024-02-20 23:23:39 +01:00
class NoAdapter(GattlibException):
"""Gattlib exception raised when no adapter is present."""
2024-04-03 15:15:05 +02:00
class Busy(GattlibException):
"""Gattlib busy exception."""
2024-04-03 15:15:05 +02:00
class Unexpected(GattlibException):
"""Gattlib unexpected exception."""
2024-04-03 15:15:05 +02:00
class AdapterNotOpened(GattlibException):
"""Gattlib exception raised when adapter is not opened yet."""
2019-05-19 12:47:21 +02:00
class InvalidParameter(GattlibException):
"""Gattlib invalid parameter exception."""
2019-05-19 12:47:21 +02:00
class NotFound(GattlibException):
"""Gattlib not found exception."""
2019-05-19 12:47:21 +02:00
class OutOfMemory(GattlibException):
"""Gattlib out of memory exception."""
2019-05-19 12:47:21 +02:00
class NotSupported(GattlibException):
"""Gattlib not supported exception."""
2019-05-19 12:47:21 +02:00
class NotConnected(GattlibException):
"""Gattlib exception raised when device is not connected."""
class AdapterClose(GattlibException):
"""Gattlib exception raised when the adapter is closed."""
class Disconnected(GattlibException):
"""Gattlib exception raised when the device is disconnected."""
2019-05-19 12:47:21 +02:00
class DeviceError(GattlibException):
"""Gattlib device exception."""
2024-02-08 00:06:09 +01:00
def __init__(self, adapter: str = None, mac_address: str = None) -> None:
self.adapter = adapter
self.mac_address = mac_address
2019-05-19 12:47:21 +02:00
2024-02-08 00:06:09 +01:00
def __str__(self) -> str:
return f"Error with device {self.mac_address} on adapter {self.adapter}"
2019-05-19 12:47:21 +02:00
class DBusError(GattlibException):
"""Gattlib DBUS exception."""
def __init__(self, domain: int, code: int) -> None:
self.domain = domain
self.code = code
def __str__(self) -> str:
if self.domain == 238 and self.code == 60964:
return "DBus Error: le-connection-abort-by-local"
2024-03-05 22:46:38 +01:00
elif self.domain == 238 and self.code == 60952:
return "DBus Error: Timeout was reached"
2024-02-20 23:23:39 +01:00
elif self.domain == 238 and self.code == 60964:
return "DBus Error: Timeout was reached"
else:
return f"DBus Error domain={self.domain},code={self.code}"
2019-05-19 12:47:21 +02:00
def handle_return(ret):
"""Function to convert gattlib error to Python exception."""
2019-05-19 12:47:21 +02:00
if ret == GATTLIB_INVALID_PARAMETER:
raise InvalidParameter()
if ret == GATTLIB_NOT_FOUND:
2019-05-19 12:47:21 +02:00
raise NotFound()
if ret == GATTLIB_OUT_OF_MEMORY:
2019-05-19 12:47:21 +02:00
raise OutOfMemory()
if ret == GATTLIB_TIMEOUT:
raise TimeoutError()
if ret == GATTLIB_NOT_SUPPORTED:
2019-05-19 12:47:21 +02:00
raise NotSupported()
if ret == GATTLIB_DEVICE_ERROR:
2019-05-19 12:47:21 +02:00
raise DeviceError()
if ret == GATTLIB_DEVICE_NOT_CONNECTED:
raise NotConnected()
if ret == GATTLIB_NO_ADAPTER:
2024-02-20 23:23:39 +01:00
raise NoAdapter()
if ret == GATTLIB_BUSY:
2024-04-03 15:15:05 +02:00
raise Busy()
if ret == GATTLIB_UNEXPECTED:
2024-04-03 15:15:05 +02:00
raise Unexpected()
if ret == GATTLIB_ADAPTER_CLOSE:
raise AdapterClose()
if ret == GATTLIB_DEVICE_DISCONNECTED:
raise Disconnected()
if (ret & GATTLIB_ERROR_MODULE_MASK) == GATTLIB_ERROR_DBUS:
raise DBusError((ret >> 8) & 0xFFF, ret & 0xFFFF)
if ret == -22: # From '-EINVAL'
2022-05-13 22:35:04 +02:00
raise ValueError("Gattlib value error")
if ret != 0:
raise RuntimeError(f"Gattlib exception {ret}")