2021-09-01 00:00:36 +02:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
#
|
2024-02-07 17:26:12 +01:00
|
|
|
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
|
2021-09-01 00:00:36 +02:00
|
|
|
#
|
|
|
|
|
2019-07-15 11:39:04 +02:00
|
|
|
GATTLIB_SUCCESS = 0
|
|
|
|
GATTLIB_INVALID_PARAMETER = 1
|
|
|
|
GATTLIB_NOT_FOUND = 2
|
2024-03-28 11:52:57 +01:00
|
|
|
GATTLIB_TIMEOUT = 3
|
2024-02-10 23:55:53 +01:00
|
|
|
GATTLIB_OUT_OF_MEMORY = 4
|
|
|
|
GATTLIB_NOT_SUPPORTED = 5
|
|
|
|
GATTLIB_DEVICE_ERROR = 6
|
2024-02-15 21:44:34 +01:00
|
|
|
GATTLIB_DEVICE_NOT_CONNECTED = 7
|
2024-02-20 23:23:39 +01:00
|
|
|
GATTLIB_NO_ADAPTER = 8
|
2024-02-15 21:44:34 +01:00
|
|
|
|
|
|
|
GATTLIB_ERROR_MODULE_MASK = 0xF0000000
|
|
|
|
GATTLIB_ERROR_DBUS = 0x10000000
|
|
|
|
GATTLIB_ERROR_BLUEZ = 0x20000000
|
|
|
|
GATTLIB_ERROR_INTERNAL = 0x80000000
|
2019-07-15 11:39:04 +02:00
|
|
|
|
2019-05-19 12:47:21 +02:00
|
|
|
|
|
|
|
class GattlibException(Exception):
|
|
|
|
pass
|
|
|
|
|
2024-02-20 23:23:39 +01:00
|
|
|
class NoAdapter(GattlibException):
|
|
|
|
pass
|
2019-07-15 11:39:04 +02:00
|
|
|
|
|
|
|
class AdapterNotOpened(GattlibException):
|
|
|
|
pass
|
|
|
|
|
2019-05-19 12:47:21 +02:00
|
|
|
class InvalidParameter(GattlibException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NotFound(GattlibException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class OutOfMemory(GattlibException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NotSupported(GattlibException):
|
|
|
|
pass
|
|
|
|
|
2024-02-15 21:44:34 +01:00
|
|
|
class NotConnected(GattlibException):
|
|
|
|
pass
|
2019-07-15 11:39:04 +02:00
|
|
|
|
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-07-15 11:39:04 +02:00
|
|
|
|
2019-05-19 12:47:21 +02:00
|
|
|
class DBusError(GattlibException):
|
2024-02-15 21:44:34 +01:00
|
|
|
def __init__(self, domain: int, code: int) -> None:
|
|
|
|
self.domain = domain
|
|
|
|
self.code = code
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2024-02-19 12:11:05 +01:00
|
|
|
if self.domain == 238 and self.code == 60964:
|
|
|
|
return f"DBus Error: le-connection-abort-by-local"
|
2024-03-05 22:46:38 +01:00
|
|
|
elif self.domain == 238 and self.code == 60952:
|
|
|
|
return f"DBus Error: Timeout was reached"
|
2024-02-20 23:23:39 +01:00
|
|
|
elif self.domain == 238 and self.code == 60964:
|
|
|
|
return f"DBus Error: Timeout was reached"
|
2024-02-19 12:11:05 +01:00
|
|
|
else:
|
|
|
|
return f"DBus Error domain={self.domain},code={self.code}"
|
2019-07-15 11:39:04 +02:00
|
|
|
|
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()
|
2024-03-28 11:52:57 +01:00
|
|
|
elif ret == GATTLIB_TIMEOUT:
|
2024-02-10 23:55:53 +01:00
|
|
|
raise TimeoutError()
|
2019-05-19 12:47:21 +02:00
|
|
|
elif ret == GATTLIB_NOT_SUPPORTED:
|
|
|
|
raise NotSupported()
|
|
|
|
elif ret == GATTLIB_DEVICE_ERROR:
|
|
|
|
raise DeviceError()
|
2024-02-15 21:44:34 +01:00
|
|
|
elif ret == GATTLIB_DEVICE_NOT_CONNECTED:
|
|
|
|
raise NotConnected()
|
2024-02-20 23:23:39 +01:00
|
|
|
elif ret == GATTLIB_NO_ADAPTER:
|
|
|
|
raise NoAdapter()
|
2024-02-15 21:44:34 +01:00
|
|
|
elif (ret & GATTLIB_ERROR_MODULE_MASK) == GATTLIB_ERROR_DBUS:
|
|
|
|
raise DBusError((ret >> 8) & 0xFFF, ret & 0xFFFF)
|
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)
|