Ensure adapter is present

pull/268/head
Olivier Martin 2024-02-20 23:23:39 +01:00 committed by Olivier Martin
parent 61043afd98
commit fbd65421d0
5 changed files with 30 additions and 4 deletions

View File

@ -931,6 +931,10 @@ int get_bluez_device_from_mac(struct gattlib_adapter *adapter, const char *mac_a
char object_path[100];
int ret;
if (adapter->adapter_proxy == NULL) {
return GATTLIB_NO_ADAPTER;
}
if (adapter != NULL) {
get_device_path_from_mac_with_adapter(adapter->adapter_proxy, mac_address, object_path, sizeof(object_path));
} else {

View File

@ -313,6 +313,10 @@ static int _gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid
GVariant *rssi_variant = NULL;
int ret;
if ((gattlib_adapter == NULL) || (gattlib_adapter->adapter_proxy == NULL)) {
return GATTLIB_NO_ADAPTER;
}
g_variant_builder_init(&arg_properties_builder, G_VARIANT_TYPE("a{sv}"));
if (enabled_filters & GATTLIB_DISCOVER_FILTER_USE_UUID) {
@ -444,6 +448,10 @@ int gattlib_adapter_scan_disable(void* adapter) {
struct gattlib_adapter *gattlib_adapter = adapter;
GError *error = NULL;
if (gattlib_adapter->adapter_proxy == NULL) {
return GATTLIB_NO_ADAPTER;
}
org_bluez_adapter1_call_stop_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error);
// Ignore the error

View File

@ -4,6 +4,7 @@
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
#
import threading
from uuid import UUID
from gattlib import *
@ -42,6 +43,7 @@ class Adapter:
self._name = name
self._adapter = c_void_p(None)
self._is_opened = False # Note: 'self._adapter != c_void_p(None)' does not seem to return the expected result
self._lock = threading.Lock()
def __str__(self) -> str:
if self._name:
@ -59,20 +61,28 @@ class Adapter:
return []
def open(self):
self._lock.acquire()
if self._is_opened:
self._lock.release()
return 0
self._adapter = c_void_p(None)
ret = gattlib_adapter_open(self._name, byref(self._adapter))
if ret == 0:
self._is_opened = True
if self._name is None:
self._name = gattlib_adapter_get_name(self._adapter)
self._lock.release()
return ret
def close(self):
self._lock.acquire()
ret = 0
if self._adapter:
ret = gattlib.gattlib_adapter_close(self._adapter)
self._is_opened = False
self._adapter = None
self._lock.release()
return ret
# Use a closure to return a method that can be called by the C-library (see: https://stackoverflow.com/a/7261524/6267288)

View File

@ -12,6 +12,7 @@ GATTLIB_OUT_OF_MEMORY = 4
GATTLIB_NOT_SUPPORTED = 5
GATTLIB_DEVICE_ERROR = 6
GATTLIB_DEVICE_NOT_CONNECTED = 7
GATTLIB_NO_ADAPTER = 8
GATTLIB_ERROR_MODULE_MASK = 0xF0000000
GATTLIB_ERROR_DBUS = 0x10000000
@ -22,23 +23,21 @@ GATTLIB_ERROR_INTERNAL = 0x80000000
class GattlibException(Exception):
pass
class NoAdapter(GattlibException):
pass
class AdapterNotOpened(GattlibException):
pass
class InvalidParameter(GattlibException):
pass
class NotFound(GattlibException):
pass
class OutOfMemory(GattlibException):
pass
class NotSupported(GattlibException):
pass
@ -61,6 +60,8 @@ class DBusError(GattlibException):
def __str__(self) -> str:
if self.domain == 238 and self.code == 60964:
return f"DBus Error: le-connection-abort-by-local"
elif self.domain == 238 and self.code == 60964:
return f"DBus Error: Timeout was reached"
else:
return f"DBus Error domain={self.domain},code={self.code}"
@ -79,6 +80,8 @@ def handle_return(ret):
raise DeviceError()
elif ret == GATTLIB_DEVICE_NOT_CONNECTED:
raise NotConnected()
elif ret == GATTLIB_NO_ADAPTER:
raise NoAdapter()
elif (ret & GATTLIB_ERROR_MODULE_MASK) == GATTLIB_ERROR_DBUS:
raise DBusError((ret >> 8) & 0xFFF, ret & 0xFFFF)
elif ret == -22: # From '-EINVAL'

View File

@ -43,6 +43,7 @@ extern "C" {
#define GATTLIB_NOT_SUPPORTED 5
#define GATTLIB_DEVICE_ERROR 6
#define GATTLIB_DEVICE_NOT_CONNECTED 7
#define GATTLIB_NO_ADAPTER 8
#define GATTLIB_ERROR_MODULE_MASK 0xF0000000
#define GATTLIB_ERROR_DBUS 0x10000000
#define GATTLIB_ERROR_BLUEZ 0x20000000