From fbd65421d0922f0b11534a8cc7590928bf09aae5 Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Tue, 20 Feb 2024 23:23:39 +0100 Subject: [PATCH] Ensure adapter is present --- dbus/gattlib.c | 4 ++++ dbus/gattlib_adapter.c | 8 ++++++++ gattlib-py/gattlib/adapter.py | 10 ++++++++++ gattlib-py/gattlib/exception.py | 11 +++++++---- include/gattlib.h | 1 + 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/dbus/gattlib.c b/dbus/gattlib.c index 316a4c5..50ada2e 100644 --- a/dbus/gattlib.c +++ b/dbus/gattlib.c @@ -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 { diff --git a/dbus/gattlib_adapter.c b/dbus/gattlib_adapter.c index 6a0cefd..c9002b9 100644 --- a/dbus/gattlib_adapter.c +++ b/dbus/gattlib_adapter.c @@ -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 diff --git a/gattlib-py/gattlib/adapter.py b/gattlib-py/gattlib/adapter.py index 0160f07..9087181 100644 --- a/gattlib-py/gattlib/adapter.py +++ b/gattlib-py/gattlib/adapter.py @@ -4,6 +4,7 @@ # Copyright (c) 2016-2024, Olivier Martin # +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) diff --git a/gattlib-py/gattlib/exception.py b/gattlib-py/gattlib/exception.py index 8f03843..5777199 100644 --- a/gattlib-py/gattlib/exception.py +++ b/gattlib-py/gattlib/exception.py @@ -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' diff --git a/include/gattlib.h b/include/gattlib.h index e8acf3b..2bf7fad 100644 --- a/include/gattlib.h +++ b/include/gattlib.h @@ -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