mirror of https://github.com/labapart/gattlib
Ensure adapter is present
parent
61043afd98
commit
fbd65421d0
|
@ -931,6 +931,10 @@ int get_bluez_device_from_mac(struct gattlib_adapter *adapter, const char *mac_a
|
||||||
char object_path[100];
|
char object_path[100];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
if (adapter->adapter_proxy == NULL) {
|
||||||
|
return GATTLIB_NO_ADAPTER;
|
||||||
|
}
|
||||||
|
|
||||||
if (adapter != NULL) {
|
if (adapter != NULL) {
|
||||||
get_device_path_from_mac_with_adapter(adapter->adapter_proxy, mac_address, object_path, sizeof(object_path));
|
get_device_path_from_mac_with_adapter(adapter->adapter_proxy, mac_address, object_path, sizeof(object_path));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -313,6 +313,10 @@ static int _gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid
|
||||||
GVariant *rssi_variant = NULL;
|
GVariant *rssi_variant = NULL;
|
||||||
int ret;
|
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}"));
|
g_variant_builder_init(&arg_properties_builder, G_VARIANT_TYPE("a{sv}"));
|
||||||
|
|
||||||
if (enabled_filters & GATTLIB_DISCOVER_FILTER_USE_UUID) {
|
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;
|
struct gattlib_adapter *gattlib_adapter = adapter;
|
||||||
GError *error = NULL;
|
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);
|
org_bluez_adapter1_call_stop_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error);
|
||||||
// Ignore the error
|
// Ignore the error
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
|
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
|
||||||
#
|
#
|
||||||
|
|
||||||
|
import threading
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from gattlib import *
|
from gattlib import *
|
||||||
|
@ -42,6 +43,7 @@ class Adapter:
|
||||||
self._name = name
|
self._name = name
|
||||||
self._adapter = c_void_p(None)
|
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._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:
|
def __str__(self) -> str:
|
||||||
if self._name:
|
if self._name:
|
||||||
|
@ -59,20 +61,28 @@ class Adapter:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
|
self._lock.acquire()
|
||||||
|
if self._is_opened:
|
||||||
|
self._lock.release()
|
||||||
|
return 0
|
||||||
|
|
||||||
self._adapter = c_void_p(None)
|
self._adapter = c_void_p(None)
|
||||||
ret = gattlib_adapter_open(self._name, byref(self._adapter))
|
ret = gattlib_adapter_open(self._name, byref(self._adapter))
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
self._is_opened = True
|
self._is_opened = True
|
||||||
if self._name is None:
|
if self._name is None:
|
||||||
self._name = gattlib_adapter_get_name(self._adapter)
|
self._name = gattlib_adapter_get_name(self._adapter)
|
||||||
|
self._lock.release()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
self._lock.acquire()
|
||||||
ret = 0
|
ret = 0
|
||||||
if self._adapter:
|
if self._adapter:
|
||||||
ret = gattlib.gattlib_adapter_close(self._adapter)
|
ret = gattlib.gattlib_adapter_close(self._adapter)
|
||||||
self._is_opened = False
|
self._is_opened = False
|
||||||
self._adapter = None
|
self._adapter = None
|
||||||
|
self._lock.release()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
# Use a closure to return a method that can be called by the C-library (see: https://stackoverflow.com/a/7261524/6267288)
|
# Use a closure to return a method that can be called by the C-library (see: https://stackoverflow.com/a/7261524/6267288)
|
||||||
|
|
|
@ -12,6 +12,7 @@ GATTLIB_OUT_OF_MEMORY = 4
|
||||||
GATTLIB_NOT_SUPPORTED = 5
|
GATTLIB_NOT_SUPPORTED = 5
|
||||||
GATTLIB_DEVICE_ERROR = 6
|
GATTLIB_DEVICE_ERROR = 6
|
||||||
GATTLIB_DEVICE_NOT_CONNECTED = 7
|
GATTLIB_DEVICE_NOT_CONNECTED = 7
|
||||||
|
GATTLIB_NO_ADAPTER = 8
|
||||||
|
|
||||||
GATTLIB_ERROR_MODULE_MASK = 0xF0000000
|
GATTLIB_ERROR_MODULE_MASK = 0xF0000000
|
||||||
GATTLIB_ERROR_DBUS = 0x10000000
|
GATTLIB_ERROR_DBUS = 0x10000000
|
||||||
|
@ -22,23 +23,21 @@ GATTLIB_ERROR_INTERNAL = 0x80000000
|
||||||
class GattlibException(Exception):
|
class GattlibException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class NoAdapter(GattlibException):
|
||||||
|
pass
|
||||||
|
|
||||||
class AdapterNotOpened(GattlibException):
|
class AdapterNotOpened(GattlibException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class InvalidParameter(GattlibException):
|
class InvalidParameter(GattlibException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class NotFound(GattlibException):
|
class NotFound(GattlibException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class OutOfMemory(GattlibException):
|
class OutOfMemory(GattlibException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class NotSupported(GattlibException):
|
class NotSupported(GattlibException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -61,6 +60,8 @@ class DBusError(GattlibException):
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
if self.domain == 238 and self.code == 60964:
|
if self.domain == 238 and self.code == 60964:
|
||||||
return f"DBus Error: le-connection-abort-by-local"
|
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:
|
else:
|
||||||
return f"DBus Error domain={self.domain},code={self.code}"
|
return f"DBus Error domain={self.domain},code={self.code}"
|
||||||
|
|
||||||
|
@ -79,6 +80,8 @@ def handle_return(ret):
|
||||||
raise DeviceError()
|
raise DeviceError()
|
||||||
elif ret == GATTLIB_DEVICE_NOT_CONNECTED:
|
elif ret == GATTLIB_DEVICE_NOT_CONNECTED:
|
||||||
raise NotConnected()
|
raise NotConnected()
|
||||||
|
elif ret == GATTLIB_NO_ADAPTER:
|
||||||
|
raise NoAdapter()
|
||||||
elif (ret & GATTLIB_ERROR_MODULE_MASK) == GATTLIB_ERROR_DBUS:
|
elif (ret & GATTLIB_ERROR_MODULE_MASK) == GATTLIB_ERROR_DBUS:
|
||||||
raise DBusError((ret >> 8) & 0xFFF, ret & 0xFFFF)
|
raise DBusError((ret >> 8) & 0xFFF, ret & 0xFFFF)
|
||||||
elif ret == -22: # From '-EINVAL'
|
elif ret == -22: # From '-EINVAL'
|
||||||
|
|
|
@ -43,6 +43,7 @@ extern "C" {
|
||||||
#define GATTLIB_NOT_SUPPORTED 5
|
#define GATTLIB_NOT_SUPPORTED 5
|
||||||
#define GATTLIB_DEVICE_ERROR 6
|
#define GATTLIB_DEVICE_ERROR 6
|
||||||
#define GATTLIB_DEVICE_NOT_CONNECTED 7
|
#define GATTLIB_DEVICE_NOT_CONNECTED 7
|
||||||
|
#define GATTLIB_NO_ADAPTER 8
|
||||||
#define GATTLIB_ERROR_MODULE_MASK 0xF0000000
|
#define GATTLIB_ERROR_MODULE_MASK 0xF0000000
|
||||||
#define GATTLIB_ERROR_DBUS 0x10000000
|
#define GATTLIB_ERROR_DBUS 0x10000000
|
||||||
#define GATTLIB_ERROR_BLUEZ 0x20000000
|
#define GATTLIB_ERROR_BLUEZ 0x20000000
|
||||||
|
|
Loading…
Reference in New Issue