Skip to contentSkip to conversation

shared/att: Reset fd when disconnected

PATCH In review Luiz Augusto von Dentz 1 participant 5 messages Oct 2, 2018 – Oct 3, 2018 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v3 1/3 shared/att: Reset fd when disconnected

From: Luiz Augusto von Dentz <[email protected]>

Set att->fd to -1 when considered disconnected.
---
v3: Pass btd_device instead of bt_att since the later may have been disconnected already.

 src/shared/att.c | 1 +
 1 file changed, 1 insertion(+)
src/shared/att.c +1 -0
index 8bf3f4611..04577eddd 100644
@@ -570,6 +570,7 @@ static bool disconnect_cb(struct io *io, void *user_data)
 
 	io_destroy(att->io);
 	att->io = NULL;
+	att->fd = -1;
 
 	/* Notify request callbacks */
 	queue_remove_all(att->req_queue, NULL, NULL, disc_att_send_op);
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v3 2/3 gatt: Fix attempting to create device on disconnection

From: Luiz Augusto von Dentz <[email protected]>

If ATT is disconnected and the state points to an invalid device it must have been destroyed in the meantime and should not be recreated.

---
 src/gatt-database.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/gatt-database.c +2 -2
index abcf7b759..febc2f840 100644
@@ -326,8 +326,8 @@ static void att_disconnected(int err, void *user_data)
 
 	state->disc_id = 0;
 
-	device = btd_adapter_get_device(state->db->adapter, &state->bdaddr,
-					state->bdaddr_type);
+	device = btd_adapter_find_device(state->db->adapter, &state->bdaddr,
+							state->bdaddr_type);
 	if (!device)
 		goto remove;
 
Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH v3 3/3 gatt: Fix not cleaning up device state properly

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

If the device is removed locally device_free would end up calling bt_att_unref which won't trigger any disconnect callback necessary to remove device states.

---
 src/device.c        |  3 +++
 src/gatt-database.c | 28 ++++++++++++++++++++++++++--
 src/gatt-database.h |  2 ++
 3 files changed, 31 insertions(+), 2 deletions(-)
src/device.c +3 -0
index ef5b8f86a..024e670e0 100644
@@ -580,6 +580,9 @@ static void gatt_server_cleanup(struct btd_device *device)
 
 	bt_gatt_server_unref(device->server);
 	device->server = NULL;
+
+	btd_gatt_database_att_disconnected(
+			btd_adapter_get_database(device->adapter), device);
 }
 
 static void attio_cleanup(struct btd_device *device)
src/gatt-database.c +26 -2
index febc2f840..783b692d5 100644
@@ -242,7 +242,7 @@ static bool dev_state_match(const void *a, const void *b)
 }
 
 static struct device_state *
-find_device_state(struct btd_gatt_database *database, bdaddr_t *bdaddr,
+find_device_state(struct btd_gatt_database *database, const bdaddr_t *bdaddr,
 							uint8_t bdaddr_type)
 {
 	struct device_info dev_info;
@@ -357,8 +357,13 @@ static bool get_dst_info(struct bt_att *att, bdaddr_t *dst, uint8_t *dst_type)
 {
 	GIOChannel *io = NULL;
 	GError *gerr = NULL;
+	int fd;
 
-	io = g_io_channel_unix_new(bt_att_get_fd(att));
+	fd = bt_att_get_fd(att);
+	if (fd < 0)
+		return false;
+
+	io = g_io_channel_unix_new(fd);
 	if (!io)
 		return false;
 
@@ -3349,6 +3354,25 @@ void btd_gatt_database_att_connected(struct btd_gatt_database *database,
 	state->pending = NULL;
 }
 
+void btd_gatt_database_att_disconnected(struct btd_gatt_database *database,
+						struct btd_device *device)
+{
+	struct device_state *state;
+	const bdaddr_t *addr;
+	uint8_t type;
+
+	DBG("");
+
+	addr = device_get_address(device);
+	type = btd_device_get_bdaddr_type(device);
+
+	state = find_device_state(database, addr, type);
+	if (!state)
+		return;
+
+	att_disconnected(0, state);
+}
+
 static void restore_ccc(struct btd_gatt_database *database,
 			const bdaddr_t *addr, uint8_t addr_type, uint16_t value)
 {
src/gatt-database.h +2 -0
index a6c3139c4..a77a0fb20 100644
@@ -25,5 +25,7 @@ void btd_gatt_database_destroy(struct btd_gatt_database *database);
 struct gatt_db *btd_gatt_database_get_db(struct btd_gatt_database *database);
 void btd_gatt_database_att_connected(struct btd_gatt_database *database,
 						struct bt_att *att);
+void btd_gatt_database_att_disconnected(struct btd_gatt_database *database,
+						struct btd_device *device);
 
 void btd_gatt_database_restore_svc_chng_ccc(struct btd_gatt_database *database);
Signature · 1 line
2.17.1
author maintainer
#3 lore raw
In reply to Luiz Augusto von Dentz
Quoted 26 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Set att->fd to -1 when considered disconnected.
---
v3: Pass btd_device instead of bt_att since the later may have been disconnected already.

 src/shared/att.c | 1 +
 1 file changed, 1 insertion(+)
src/shared/att.c +1 -1
index 8bf3f4611..04577eddd 100644
@@ -570,6 +570,7 @@ static bool disconnect_cb(struct io *io, void *user_data)
 
        io_destroy(att->io);
        att->io = NULL;
+       att->fd = -1;
 
        /* Notify request callbacks */
        queue_remove_all(att->req_queue, NULL, NULL, disc_att_send_op);
--

2.17.1

Signature · 1 line
Luiz Augusto von Dentz
author maintainer Applied
#4 lore raw

Hi,

Quoted 24 lines · Luiz Augusto von Dentz
Quoted 21 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Set att->fd to -1 when considered disconnected.
---
v3: Pass btd_device instead of bt_att since the later may have been disconnected already.

 src/shared/att.c | 1 +
 1 file changed, 1 insertion(+)
src/shared/att.c +1 -0
index 8bf3f4611..04577eddd 100644
@@ -570,6 +570,7 @@ static bool disconnect_cb(struct io *io, void *user_data)
 
        io_destroy(att->io);
        att->io = NULL;
+       att->fd = -1;
 
Quoted 5 lines
Quoted 5 lines
src/shared/att.c +0 -1
        /* Notify request callbacks */
        queue_remove_all(att->req_queue, NULL, NULL, disc_att_send_op);
--

2.17.1

Applied.

Signature · 1 line
Luiz Augusto von Dentz