Skip to content

Commit a41061c

Browse files
committed
Various fixes/logging/features
1 parent 642556f commit a41061c

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

dbus/gattlib.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,22 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
133133

134134
// even after init_default_adapter() - the adapter can be NULL
135135
if (gattlib_adapter == NULL) {
136+
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: No adapter");
136137
return NULL;
137138
}
138139

139140
get_device_path_from_mac(adapter_name, dst, object_path, sizeof(object_path));
140141

141142
gattlib_context_t* conn_context = calloc(sizeof(gattlib_context_t), 1);
142143
if (conn_context == NULL) {
144+
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Cannot allocate context");
143145
return NULL;
144146
}
145147
conn_context->adapter = gattlib_adapter;
146148

147149
gatt_connection_t* connection = calloc(sizeof(gatt_connection_t), 1);
148150
if (connection == NULL) {
151+
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Cannot allocate connection");
149152
goto FREE_CONN_CONTEXT;
150153
} else {
151154
connection->context = conn_context;
@@ -162,6 +165,8 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
162165
if (error) {
163166
GATTLIB_LOG(GATTLIB_ERROR, "Failed to connect to DBus Bluez Device: %s", error->message);
164167
g_error_free(error);
168+
} else {
169+
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Failed to connect to DBus Bluez Device");
165170
}
166171
goto FREE_CONNECTION;
167172
} else {
@@ -206,6 +211,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
206211
// Get list of objects belonging to Device Manager
207212
device_manager = get_device_manager_from_adapter(conn_context->adapter);
208213
if (device_manager == NULL) {
214+
GATTLIB_LOG(GATTLIB_DEBUG, "gattlib_connect: Failed to get device manager from adapter");
209215
goto FREE_DEVICE;
210216
}
211217
conn_context->dbus_objects = g_dbus_object_manager_get_objects(device_manager);
@@ -227,8 +233,7 @@ gatt_connection_t *gattlib_connect(void* adapter, const char *dst, unsigned long
227233
free(conn_context);
228234

229235
// destroy default adapter
230-
if(adapter == NULL)
231-
{
236+
if(adapter == NULL) {
232237
gattlib_adapter_close(gattlib_adapter);
233238
}
234239

@@ -250,9 +255,21 @@ gatt_connection_t *gattlib_connect_async(void *adapter, const char *dst,
250255
}
251256

252257
int gattlib_disconnect(gatt_connection_t* connection) {
253-
gattlib_context_t* conn_context = connection->context;
258+
gattlib_context_t* conn_context;
254259
GError *error = NULL;
255260

261+
if (connection == NULL) {
262+
GATTLIB_LOG(GATTLIB_ERROR, "Cannot disconnect - connection parameter is not valid.");
263+
return GATTLIB_INVALID_PARAMETER;
264+
}
265+
266+
conn_context = connection->context;
267+
268+
if (conn_context == NULL) {
269+
GATTLIB_LOG(GATTLIB_ERROR, "Cannot disconnect - connection context is not valid.");
270+
return GATTLIB_NOT_SUPPORTED;
271+
}
272+
256273
org_bluez_device1_call_disconnect_sync(conn_context->device, NULL, &error);
257274
if (error) {
258275
GATTLIB_LOG(GATTLIB_ERROR, "Failed to disconnect DBus Bluez Device: %s", error->message);
@@ -266,7 +283,7 @@ int gattlib_disconnect(gatt_connection_t* connection) {
266283
pthread_join(conn_context->event_thread, NULL);
267284
g_main_loop_unref(conn_context->connection_loop);
268285
disconnect_all_notifications(conn_context);
269-
286+
270287
free(conn_context->adapter->adapter_name);
271288
free(conn_context->adapter);
272289

@@ -790,7 +807,7 @@ int gattlib_discover_char_range(gatt_connection_t* connection, int start, int en
790807
interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.Battery1");
791808
if (interface) {
792809
g_object_unref(interface);
793-
810+
794811
characteristic_list[count].handle = 0;
795812
characteristic_list[count].value_handle = 0;
796813
characteristic_list[count].properties = GATTLIB_CHARACTERISTIC_READ | GATTLIB_CHARACTERISTIC_NOTIFY;

gattlib-py/gattlib/device.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(self, adapter, addr, name=None):
4242
self._gatt_characteristic_callbacks = {}
4343

4444
@property
45-
def mac_address(self):
45+
def mac_address(self) -> str:
4646
"""Return Device MAC Address"""
4747
return self._addr.decode("utf-8")
4848

@@ -53,6 +53,10 @@ def connection(self):
5353
else:
5454
return c_void_p(None)
5555

56+
@property
57+
def is_connected(self) -> bool:
58+
return (self._connection is not None)
59+
5660
def connect(self, options=CONNECTION_OPTIONS_LEGACY_DEFAULT):
5761
if self._adapter:
5862
adapter_name = self._adapter.name
@@ -61,7 +65,7 @@ def connect(self, options=CONNECTION_OPTIONS_LEGACY_DEFAULT):
6165

6266
self._connection = gattlib_connect(adapter_name, self._addr, options)
6367
if self._connection is None:
64-
raise DeviceError()
68+
raise DeviceError(adapter=adapter_name, mac_address=self._addr)
6569

6670
@property
6771
def rssi(self):
@@ -86,8 +90,10 @@ def register_on_disconnect(self, callback, user_data):
8690
gattlib_register_on_disconnect(self.connection, Device.on_disconnection, self)
8791

8892
def disconnect(self):
89-
ret = gattlib_disconnect(self.connection)
90-
handle_return(ret)
93+
if self._connection:
94+
ret = gattlib_disconnect(self.connection)
95+
handle_return(ret)
96+
self._connection = None
9197

9298
def discover(self):
9399
#

gattlib-py/gattlib/exception.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ class NotSupported(GattlibException):
3838

3939

4040
class DeviceError(GattlibException):
41-
pass
41+
def __init__(self, adapter: str = None, mac_address: str = None) -> None:
42+
self.adapter = adapter
43+
self.mac_address = mac_address
4244

45+
def __str__(self) -> str:
46+
return f"Error with device {self.mac_address} on adapter {self.adapter}"
4347

4448
class DBusError(GattlibException):
4549
pass

0 commit comments

Comments
 (0)