Skip to content

Commit

Permalink
Ensure adapter is present
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviermartin committed Feb 20, 2024
1 parent 61043af commit fbd6542
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions dbus/gattlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions dbus/gattlib_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions gattlib-py/gattlib/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
#

import threading
from uuid import UUID

from gattlib import *
Expand Down Expand Up @@ -42,6 +43,7 @@ def __init__(self, name=c_char_p(None)):
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:
Expand All @@ -59,20 +61,28 @@ def list():
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)
Expand Down
11 changes: 7 additions & 4 deletions gattlib-py/gattlib/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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
Expand All @@ -22,23 +23,21 @@
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

Expand All @@ -61,6 +60,8 @@ def __init__(self, domain: int, code: int) -> None:
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}"

Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions include/gattlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fbd6542

Please sign in to comment.