Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviermartin committed Apr 9, 2024
1 parent db04a0e commit 23d2567
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions common/gattlib_callback_connected_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ void gattlib_connected_device_python_callback(gattlib_adapter_t* adapter, const

static gpointer _gattlib_connected_device_thread(gpointer data) {
gattlib_connection_t* connection = data;
//puts("_gattlib_connected_device_thread A");
const gchar *device_mac_address = org_bluez_device1_get_address(connection->backend.device);

// Mutex to ensure the handler is valid
g_rec_mutex_lock(&m_gattlib_mutex);
//puts("_gattlib_connected_device_thread B");

if (!gattlib_connection_is_connected(connection)) {
GATTLIB_LOG(GATTLIB_ERROR, "_gattlib_connected_device_thread: Device is not connected (state:%s)",
Expand All @@ -66,18 +68,22 @@ static gpointer _gattlib_connected_device_thread(gpointer data) {
return NULL;
}

//puts("_gattlib_connected_device_thread C");
// Ensure we increment device reference counter to prevent the device/connection is freed during the execution
gattlib_device_ref(connection->device);

// We need to release the lock here to ensure the connection callback that is actually
// doing the application sepcific work is not locking the BLE state.
g_rec_mutex_unlock(&m_gattlib_mutex);
//puts("_gattlib_connected_device_thread D");

connection->on_connection.callback.connection_handler(
connection->device->adapter, device_mac_address, connection, 0 /* no error */,
connection->on_connection.user_data);

//puts("_gattlib_connected_device_thread E");
gattlib_device_unref(connection->device);
//puts("_gattlib_connected_device_thread DONE");
return NULL;
}

Expand Down
4 changes: 4 additions & 0 deletions common/gattlib_callback_disconnected_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ void gattlib_disconnected_device_python_callback(gattlib_connection_t* connectio
#endif

void gattlib_on_disconnected_device(gattlib_connection_t* connection) {
//puts("--- gattlib_on_disconnected_device");
g_rec_mutex_lock(&m_gattlib_mutex);
//puts("--- gattlib_on_disconnected_device A");

if (!gattlib_connection_is_valid(connection)) {
GATTLIB_LOG(GATTLIB_ERROR, "gattlib_on_disconnected_device: Device not valid");
Expand Down Expand Up @@ -62,4 +64,6 @@ void gattlib_on_disconnected_device(gattlib_connection_t* connection) {
m_gattlib_signal.signals |= GATTLIB_SIGNAL_DEVICE_DISCONNECTION;
g_cond_broadcast(&m_gattlib_signal.condition);
g_mutex_unlock(&m_gattlib_signal.mutex);

//puts("--- gattlib_on_disconnected_device DONE");
}
1 change: 1 addition & 0 deletions common/gattlib_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ void gattlib_free_mem(void *ptr) {
int gattlib_device_ref(gattlib_device_t* device) {
g_rec_mutex_lock(&m_gattlib_mutex);
device->reference_counter++;
//printf("DEVICE INC REF_COUNTER:%d\n", device->reference_counter);
g_rec_mutex_unlock(&m_gattlib_mutex);
return GATTLIB_SUCCESS;
}
7 changes: 7 additions & 0 deletions common/gattlib_common_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ struct _connection_is_valid {

static gint _is_device_connection(gconstpointer a, gconstpointer b) {
const gattlib_device_t* device = a;
//printf("- %s connection:%p / %p\n", device->device_id, &device->connection);
return (&device->connection == b) ? 0 : -1; // We need to return 0 when it matches
}

Expand Down Expand Up @@ -152,12 +153,16 @@ static void _gattlib_connection_is_connected(gpointer data, gpointer user_data)
gattlib_adapter_t* adapter = data;
struct _connection_is_connected* connection_is_connected = user_data;

//printf("_gattlib_connection_is_connected: Check device in adapter:%s\n", adapter->id);

GSList *device_entry = g_slist_find_custom(adapter->devices, connection_is_connected->connection, _is_device_connection);
if (device_entry == NULL) {
//printf("_gattlib_connection_is_connected: Did not find device %s\n", connection_is_connected->connection->device->device_id);
return;
}

gattlib_device_t* device = device_entry->data;
//printf("_gattlib_connection_is_connected: device %s state is %s\n", device->device_id, device_state_str[device->state]);
connection_is_connected->is_connected = (device->state == CONNECTED);
}

Expand All @@ -168,7 +173,9 @@ bool gattlib_connection_is_connected(gattlib_connection_t* connection) {
};

g_rec_mutex_lock(&m_gattlib_mutex);
//printf("gattlib_connection_is_connected A");
g_slist_foreach(m_adapter_list, _gattlib_connection_is_connected, &connection_is_connected);
//printf("gattlib_connection_is_connected B");
g_rec_mutex_unlock(&m_gattlib_mutex);

return connection_is_connected.is_connected;
Expand Down
1 change: 1 addition & 0 deletions common/gattlib_device_state_management.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ int gattlib_devices_free(gattlib_adapter_t* adapter) {
int gattlib_device_unref(gattlib_device_t* device) {
g_rec_mutex_lock(&m_gattlib_mutex);
device->reference_counter--;
//printf("DEVICE DEC REF_COUNTER:%d\n", device->reference_counter);
if (device->reference_counter > 0) {
goto EXIT;
}
Expand Down
13 changes: 12 additions & 1 deletion dbus/gattlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ static void _on_device_connect(gattlib_connection_t* connection) {

gattlib_device_set_state(connection->device->adapter, connection->device->device_id, CONNECTED);

//puts("Go gattlib_on_connected_device");
gattlib_on_connected_device(connection);
//puts("Go gattlib_on_connected_device DONE");

EXIT:
g_rec_mutex_unlock(&m_gattlib_mutex);
Expand Down Expand Up @@ -249,6 +251,7 @@ int gattlib_connect(gattlib_adapter_t* adapter, const char *dst,
}

// Register a handle for notification
//printf("DEVICE CONNECT property change 3\n");
device->connection.backend.on_handle_device_property_change_id = g_signal_connect(bluez_device,
"g-properties-changed",
G_CALLBACK(on_handle_device_property_change),
Expand Down Expand Up @@ -316,6 +319,8 @@ int gattlib_connect(gattlib_adapter_t* adapter, const char *dst,
void gattlib_connection_free(gattlib_connection_t* connection) {
char* device_id;

//puts("gattlib_connection_free()");

device_id = connection->device->device_id;

// Remove signal
Expand Down Expand Up @@ -350,6 +355,7 @@ void gattlib_connection_free(gattlib_connection_t* connection) {

// Mark the device has disconnected
gattlib_device_set_state(connection->device->adapter, device_id, DISCONNECTED);
//puts("gattlib_connection_free() DONE");
}

int gattlib_disconnect(gattlib_connection_t* connection, bool wait_disconnection) {
Expand Down Expand Up @@ -899,6 +905,8 @@ static void add_characteristics_from_service(struct _gattlib_connection_backend*
continue;
}

GATTLIB_LOG(GATTLIB_DEBUG, "- count %d with characteristic %s", *count, object_path);

characteristic_list[*count].handle = handle;
characteristic_list[*count].value_handle = handle;
characteristic_list[*count].properties = 0;
Expand Down Expand Up @@ -975,7 +983,9 @@ int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start
g_object_unref(interface);

count_max++;
GATTLIB_LOG(GATTLIB_DEBUG, "- count_max %d with %s", count_max, object_path);
}
GATTLIB_LOG(GATTLIB_DEBUG, "count_max:%d\n", count_max);

gattlib_characteristic_t* characteristic_list = calloc(count_max * sizeof(gattlib_characteristic_t), 1);
if (characteristic_list == NULL) {
Expand All @@ -989,7 +999,7 @@ int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start
const char* object_path = g_dbus_object_get_object_path(G_DBUS_OBJECT(object));

GDBusInterface *interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.GattService1");
if (!interface) {
if (interface == NULL) {
// Check if this DBUS Path is actually the Battery interface. In this case,
// we add a fake characteristic for the battery.
interface = g_dbus_object_manager_get_interface(device_manager, object_path, "org.bluez.Battery1");
Expand All @@ -1004,6 +1014,7 @@ int gattlib_discover_char_range(gattlib_connection_t* connection, uint16_t start
"00002a19-0000-1000-8000-00805f9b34fb",
MAX_LEN_UUID_STR + 1,
&characteristic_list[count].uuid);
GATTLIB_LOG(GATTLIB_DEBUG, "- count with (battery) %s", object_path);
count++;
}

Expand Down
2 changes: 2 additions & 0 deletions dbus/gattlib_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ int gattlib_adapter_close(gattlib_adapter_t* adapter) {
int gattlib_adapter_ref(gattlib_adapter_t* adapter) {
g_rec_mutex_lock(&m_gattlib_mutex);
adapter->reference_counter++;
//printf("ADAPTER INC REF_COUNTER:%d\n", adapter->reference_counter);
g_rec_mutex_unlock(&m_gattlib_mutex);
return GATTLIB_SUCCESS;
}
Expand All @@ -714,6 +715,7 @@ int gattlib_adapter_unref(gattlib_adapter_t* adapter) {

g_rec_mutex_lock(&m_gattlib_mutex);
adapter->reference_counter--;
//printf("ADAPTER DEC REF_COUNTER:%d\n", adapter->reference_counter);

if (adapter->reference_counter > 0) {
goto EXIT;
Expand Down
2 changes: 2 additions & 0 deletions dbus/gattlib_notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ static int connect_signal_to_characteristic_uuid(gattlib_connection_t* connectio
#if BLUEZ_VERSION > BLUEZ_VERSIONS(5, 40)
else if (dbus_characteristic.type == TYPE_BATTERY_LEVEL) {
// Register a handle for notification
//printf("DEVICE CONNECT property change 1\n");
g_signal_connect(dbus_characteristic.battery,
"g-properties-changed",
G_CALLBACK (on_handle_battery_level_property_change),
Expand All @@ -196,6 +197,7 @@ static int connect_signal_to_characteristic_uuid(gattlib_connection_t* connectio
#endif

// Register a handle for notification
//printf("DEVICE CONNECT property change 2\n");
gulong signal_id = g_signal_connect(dbus_characteristic.gatt,
"g-properties-changed",
G_CALLBACK(callback),
Expand Down

0 comments on commit 23d2567

Please sign in to comment.