gattlib/gattlib-py/gattlib/exception.py

74 lines
1.6 KiB
Python
Raw 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_SUCCESS = 0
GATTLIB_INVALID_PARAMETER = 1
GATTLIB_NOT_FOUND = 2
GATTLIB_ERROR_TIMEOUT = 3
GATTLIB_OUT_OF_MEMORY = 4
GATTLIB_NOT_SUPPORTED = 5
GATTLIB_DEVICE_ERROR = 6
GATTLIB_ERROR_DBUS = 7
GATTLIB_ERROR_BLUEZ = 8
GATTLIB_ERROR_INTERNAL = 9
2019-05-19 12:47:21 +02:00
class GattlibException(Exception):
pass
class AdapterNotOpened(GattlibException):
pass
2019-05-19 12:47:21 +02:00
class InvalidParameter(GattlibException):
pass
2019-05-19 12:47:21 +02:00
class NotFound(GattlibException):
pass
2019-05-19 12:47:21 +02:00
class OutOfMemory(GattlibException):
pass
2019-05-19 12:47:21 +02:00
class NotSupported(GattlibException):
pass
2019-05-19 12:47:21 +02:00
class DeviceError(GattlibException):
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):
pass
2019-05-19 12:47:21 +02:00
def handle_return(ret):
if ret == GATTLIB_INVALID_PARAMETER:
raise InvalidParameter()
elif ret == GATTLIB_NOT_FOUND:
raise NotFound()
elif ret == GATTLIB_OUT_OF_MEMORY:
raise OutOfMemory()
elif ret == GATTLIB_ERROR_TIMEOUT:
raise TimeoutError()
2019-05-19 12:47:21 +02:00
elif ret == GATTLIB_NOT_SUPPORTED:
raise NotSupported()
elif ret == GATTLIB_DEVICE_ERROR:
raise DeviceError()
elif ret == GATTLIB_ERROR_DBUS:
raise DBusError()
2022-05-13 22:35:04 +02:00
elif ret == -22: # From '-EINVAL'
raise ValueError("Gattlib value error")
2019-05-19 12:47:21 +02:00
elif ret != 0:
raise RuntimeError("Gattlib exception %d" % ret)