diff mbox series

[BlueZ] device: Add connectable flag to bearer state

Message ID 20220613233419.177421-1-luiz.dentz@gmail.com
State New
Headers show
Series [BlueZ] device: Add connectable flag to bearer state | expand

Commit Message

Luiz Augusto von Dentz June 13, 2022, 11:34 p.m. UTC
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds connectable flag to state so it can be used to detect which is
the last seen connectable bearer.
---
 src/adapter.c |  6 +++---
 src/device.c  | 41 +++++++++++++++++++++++------------------
 src/device.h  |  3 ++-
 3 files changed, 28 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/src/adapter.c b/src/adapter.c
index a75dd61bf..aeef6b7a8 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -3484,7 +3484,7 @@  static void device_connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
 
 	/* continue with service discovery and connection */
 	btd_device_set_temporary(device, false);
-	device_update_last_seen(device, data->dst_type);
+	device_update_last_seen(device, data->dst_type, true);
 
 	if (data->dst_type != BDADDR_BREDR){
 		g_io_channel_set_close_on_unref(io, FALSE);
@@ -6994,7 +6994,7 @@  void btd_adapter_update_found_device(struct btd_adapter *adapter,
 		return;
 	}
 
-	device_update_last_seen(dev, bdaddr_type);
+	device_update_last_seen(dev, bdaddr_type, !not_connectable);
 
 	/*
 	 * FIXME: We need to check for non-zero flags first because
@@ -7006,7 +7006,7 @@  void btd_adapter_update_found_device(struct btd_adapter *adapter,
 					!(eir_data.flags & EIR_BREDR_UNSUP)) {
 		device_set_bredr_support(dev);
 		/* Update last seen for BR/EDR in case its flag is set */
-		device_update_last_seen(dev, BDADDR_BREDR);
+		device_update_last_seen(dev, BDADDR_BREDR, !not_connectable);
 	}
 
 	if (eir_data.name != NULL && eir_data.name_complete)
diff --git a/src/device.c b/src/device.c
index 3da09578f..7b451e458 100644
--- a/src/device.c
+++ b/src/device.c
@@ -155,6 +155,8 @@  struct bearer_state {
 	bool connected;
 	bool svc_resolved;
 	bool initiator;
+	bool connectable;
+	time_t last_seen;
 };
 
 struct csrk_info {
@@ -255,9 +257,6 @@  struct btd_device {
 
 	sdp_list_t	*tmp_records;
 
-	time_t		bredr_seen;
-	time_t		le_seen;
-
 	gboolean	trusted;
 	gboolean	blocked;
 	gboolean	auto_connect;
@@ -2273,14 +2272,14 @@  static uint8_t select_conn_bearer(struct btd_device *dev)
 	if (dev->bdaddr_type == BDADDR_LE_RANDOM)
 		return dev->bdaddr_type;
 
-	if (dev->bredr_seen) {
-		bredr_last = current - dev->bredr_seen;
+	if (dev->bredr_state.last_seen) {
+		bredr_last = current - dev->bredr_state.last_seen;
 		if (bredr_last > SEEN_TRESHHOLD)
 			bredr_last = NVAL_TIME;
 	}
 
-	if (dev->le_seen) {
-		le_last = current - dev->le_seen;
+	if (dev->le_state.last_seen) {
+		le_last = current - dev->le_state.last_seen;
 		if (le_last > SEEN_TRESHHOLD)
 			le_last = NVAL_TIME;
 	}
@@ -3022,7 +3021,7 @@  void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type)
 {
 	struct bearer_state *state = get_state(dev, bdaddr_type);
 
-	device_update_last_seen(dev, bdaddr_type);
+	device_update_last_seen(dev, bdaddr_type, true);
 
 	if (state->connected) {
 		char addr[18];
@@ -3151,7 +3150,7 @@  void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type)
 	if (device->bredr_state.connected || device->le_state.connected)
 		return;
 
-	device_update_last_seen(device, bdaddr_type);
+	device_update_last_seen(device, bdaddr_type, true);
 
 	g_slist_free_full(device->eir_uuids, g_free);
 	device->eir_uuids = NULL;
@@ -4168,7 +4167,7 @@  void device_update_addr(struct btd_device *device, const bdaddr_t *bdaddr,
 
 void device_set_bredr_support(struct btd_device *device)
 {
-	if (device->bredr)
+	if (btd_opts.mode == BT_MODE_LE || device->bredr)
 		return;
 
 	device->bredr = true;
@@ -4177,7 +4176,7 @@  void device_set_bredr_support(struct btd_device *device)
 
 void device_set_le_support(struct btd_device *device, uint8_t bdaddr_type)
 {
-	if (device->le)
+	if (btd_opts.mode == BT_MODE_BREDR || device->le)
 		return;
 
 	device->le = true;
@@ -4186,12 +4185,15 @@  void device_set_le_support(struct btd_device *device, uint8_t bdaddr_type)
 	store_device_info(device);
 }
 
-void device_update_last_seen(struct btd_device *device, uint8_t bdaddr_type)
+void device_update_last_seen(struct btd_device *device, uint8_t bdaddr_type,
+							bool connectable)
 {
-	if (bdaddr_type == BDADDR_BREDR)
-		device->bredr_seen = time(NULL);
-	else
-		device->le_seen = time(NULL);
+	struct bearer_state *state;
+
+	state = get_state(device, bdaddr_type);
+
+	state->last_seen = time(NULL);
+	state->connectable = connectable;
 
 	if (!device_is_temporary(device))
 		return;
@@ -5902,14 +5904,17 @@  void device_set_flags(struct btd_device *device, uint8_t flags)
 
 bool device_is_connectable(struct btd_device *device)
 {
+	struct bearer_state *state;
+
 	if (!device)
 		return false;
 
 	if (device->bredr)
 		return true;
 
-	/* Check if either Limited or General discoverable are set */
-	return (device->ad_flags[0] & 0x03);
+	state = get_state(device, device->bdaddr_type);
+
+	return state->connectable;
 }
 
 static bool start_discovery(gpointer user_data)
diff --git a/src/device.h b/src/device.h
index 5e8d1c3e1..1bf7b3989 100644
--- a/src/device.h
+++ b/src/device.h
@@ -33,7 +33,8 @@  void device_update_addr(struct btd_device *device, const bdaddr_t *bdaddr,
 							uint8_t bdaddr_type);
 void device_set_bredr_support(struct btd_device *device);
 void device_set_le_support(struct btd_device *device, uint8_t bdaddr_type);
-void device_update_last_seen(struct btd_device *device, uint8_t bdaddr_type);
+void device_update_last_seen(struct btd_device *device, uint8_t bdaddr_type,
+							bool last_seen);
 void device_merge_duplicate(struct btd_device *dev, struct btd_device *dup);
 uint32_t btd_device_get_class(struct btd_device *device);
 uint16_t btd_device_get_vendor(struct btd_device *device);