Various fixes/logging/features

fix-build
Olivier Martin 2024-02-08 00:06:09 +01:00
parent 642556f8b9
commit a41061c1d4
3 changed files with 37 additions and 10 deletions

View File

@ -133,6 +133,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
// even after init_default_adapter() - the adapter can be NULL // even after init_default_adapter() - the adapter can be NULL
if (gattlib_adapter == NULL) { if (gattlib_adapter == NULL) {
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: No adapter");
return NULL; return NULL;
} }
@ -140,12 +141,14 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
gattlib_context_t* conn_context = calloc(sizeof(gattlib_context_t), 1); gattlib_context_t* conn_context = calloc(sizeof(gattlib_context_t), 1);
if (conn_context == NULL) { if (conn_context == NULL) {
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Cannot allocate context");
return NULL; return NULL;
} }
conn_context->adapter = gattlib_adapter; conn_context->adapter = gattlib_adapter;
gatt_connection_t* connection = calloc(sizeof(gatt_connection_t), 1); gatt_connection_t* connection = calloc(sizeof(gatt_connection_t), 1);
if (connection == NULL) { if (connection == NULL) {
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Cannot allocate connection");
goto FREE_CONN_CONTEXT; goto FREE_CONN_CONTEXT;
} else { } else {
connection->context = conn_context; connection->context = conn_context;
@ -162,6 +165,8 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
if (error) { if (error) {
GATTLIB_LOG(GATTLIB_ERROR, "Failed to connect to DBus Bluez Device: %s", error->message); GATTLIB_LOG(GATTLIB_ERROR, "Failed to connect to DBus Bluez Device: %s", error->message);
g_error_free(error); g_error_free(error);
} else {
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Failed to connect to DBus Bluez Device");
} }
goto FREE_CONNECTION; goto FREE_CONNECTION;
} else { } else {
@ -206,6 +211,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
// Get list of objects belonging to Device Manager // Get list of objects belonging to Device Manager
device_manager = get_device_manager_from_adapter(conn_context->adapter); device_manager = get_device_manager_from_adapter(conn_context->adapter);
if (device_manager == NULL) { if (device_manager == NULL) {
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Failed to get device manager from adapter");
goto FREE_DEVICE; goto FREE_DEVICE;
} }
conn_context->dbus_objects = g_dbus_object_manager_get_objects(device_manager); conn_context->dbus_objects = g_dbus_object_manager_get_objects(device_manager);
@ -227,8 +233,7 @@ FREE_CONN_CONTEXT:
free(conn_context); free(conn_context);
// destroy default adapter // destroy default adapter
if(adapter == NULL) if(adapter == NULL) {
{
gattlib_adapter_close(gattlib_adapter); gattlib_adapter_close(gattlib_adapter);
} }
@ -250,9 +255,21 @@ gatt_connection_t *gattlib_connect_async(void *adapter, const char *dst,
} }
int gattlib_disconnect(gatt_connection_t* connection) { int gattlib_disconnect(gatt_connection_t* connection) {
gattlib_context_t* conn_context = connection->context; gattlib_context_t* conn_context;
GError *error = NULL; GError *error = NULL;
if (connection == NULL) {
GATTLIB_LOG(GATTLIB_ERROR, "Cannot disconnect - connection parameter is not valid.");
return GATTLIB_INVALID_PARAMETER;
}
conn_context = connection->context;
if (conn_context == NULL) {
GATTLIB_LOG(GATTLIB_ERROR, "Cannot disconnect - connection context is not valid.");
return GATTLIB_NOT_SUPPORTED;
}
org_bluez_device1_call_disconnect_sync(conn_context->device, NULL, &error); org_bluez_device1_call_disconnect_sync(conn_context->device, NULL, &error);
if (error) { if (error) {
GATTLIB_LOG(GATTLIB_ERROR, "Failed to disconnect DBus Bluez Device: %s", error->message); GATTLIB_LOG(GATTLIB_ERROR, "Failed to disconnect DBus Bluez Device: %s", error->message);
@ -266,7 +283,7 @@ int gattlib_disconnect(gatt_connection_t* connection) {
pthread_join(conn_context->event_thread, NULL); pthread_join(conn_context->event_thread, NULL);
g_main_loop_unref(conn_context->connection_loop); g_main_loop_unref(conn_context->connection_loop);
disconnect_all_notifications(conn_context); disconnect_all_notifications(conn_context);
free(conn_context->adapter->adapter_name); free(conn_context->adapter->adapter_name);
free(conn_context->adapter); free(conn_context->adapter);
@ -790,7 +807,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.Battery1"); interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.Battery1");
if (interface) { if (interface) {
g_object_unref(interface); g_object_unref(interface);
characteristic_list[count].handle = 0; characteristic_list[count].handle = 0;
characteristic_list[count].value_handle = 0; characteristic_list[count].value_handle = 0;
characteristic_list[count].properties = GATTLIB_CHARACTERISTIC_READ | GATTLIB_CHARACTERISTIC_NOTIFY; characteristic_list[count].properties = GATTLIB_CHARACTERISTIC_READ | GATTLIB_CHARACTERISTIC_NOTIFY;

View File

@ -42,7 +42,7 @@ class Device:
self._gatt_characteristic_callbacks = {} self._gatt_characteristic_callbacks = {}
@property @property
def mac_address(self): def mac_address(self) -> str:
"""Return Device MAC Address""" """Return Device MAC Address"""
return self._addr.decode("utf-8") return self._addr.decode("utf-8")
@ -53,6 +53,10 @@ class Device:
else: else:
return c_void_p(None) return c_void_p(None)
@property
def is_connected(self) -> bool:
return (self._connection is not None)
def connect(self, options=CONNECTION_OPTIONS_LEGACY_DEFAULT): def connect(self, options=CONNECTION_OPTIONS_LEGACY_DEFAULT):
if self._adapter: if self._adapter:
adapter_name = self._adapter.name adapter_name = self._adapter.name
@ -61,7 +65,7 @@ class Device:
self._connection = gattlib_connect(adapter_name, self._addr, options) self._connection = gattlib_connect(adapter_name, self._addr, options)
if self._connection is None: if self._connection is None:
raise DeviceError() raise DeviceError(adapter=adapter_name, mac_address=self._addr)
@property @property
def rssi(self): def rssi(self):
@ -86,8 +90,10 @@ class Device:
gattlib_register_on_disconnect(self.connection, Device.on_disconnection, self) gattlib_register_on_disconnect(self.connection, Device.on_disconnection, self)
def disconnect(self): def disconnect(self):
ret = gattlib_disconnect(self.connection) if self._connection:
handle_return(ret) ret = gattlib_disconnect(self.connection)
handle_return(ret)
self._connection = None
def discover(self): def discover(self):
# #

View File

@ -38,8 +38,12 @@ class NotSupported(GattlibException):
class DeviceError(GattlibException): class DeviceError(GattlibException):
pass def __init__(self, adapter: str = None, mac_address: str = None) -> None:
self.adapter = adapter
self.mac_address = mac_address
def __str__(self) -> str:
return f"Error with device {self.mac_address} on adapter {self.adapter}"
class DBusError(GattlibException): class DBusError(GattlibException):
pass pass