Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH BlueZ 1/2] device: Add timer for removing temporary devices
@ 2020-07-15 20:46 Luiz Augusto von Dentz
  2020-07-15 20:46 ` [PATCH BlueZ 2/2] adapter: Remove " Luiz Augusto von Dentz
  2020-07-15 21:03 ` [PATCH BlueZ 1/2] device: Add " Bastien Nocera
  0 siblings, 2 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2020-07-15 20:46 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds a timer for how long to keep temporary devices, it is updated
everytime the device is seen so devices will be removed even during an
active discovery session which means the discovering for a long period
will no longer just grow without removing devices that disappeared.
---
 src/device.c  | 34 ++++++++++++++++++++++++++++++++++
 src/hcid.h    |  1 +
 src/main.c    | 13 +++++++++++++
 src/main.conf |  5 +++++
 4 files changed, 53 insertions(+)

diff --git a/src/device.c b/src/device.c
index 0deee2707..226216235 100644
--- a/src/device.c
+++ b/src/device.c
@@ -233,6 +233,7 @@ struct btd_device {
 	bool		connectable;
 	guint		disconn_timer;
 	guint		discov_timer;
+	guint		temporary_timer;	/* Temporary/disappear timer */
 	struct browse_req *browse;		/* service discover request */
 	struct bonding_req *bonding;
 	struct authentication_req *authr;	/* authentication request */
@@ -698,6 +699,9 @@ static void device_free(gpointer user_data)
 	if (device->discov_timer)
 		g_source_remove(device->discov_timer);
 
+	if (device->temporary_timer)
+		g_source_remove(device->temporary_timer);
+
 	if (device->connect)
 		dbus_message_unref(device->connect);
 
@@ -4232,12 +4236,34 @@ void device_set_le_support(struct btd_device *device, uint8_t bdaddr_type)
 	store_device_info(device);
 }
 
+static gboolean device_disappeared(gpointer user_data)
+{
+	struct btd_device *dev = user_data;
+
+	dev->temporary_timer = 0;
+
+	btd_adapter_remove_device(dev->adapter, dev);
+
+	return FALSE;
+}
+
 void device_update_last_seen(struct btd_device *device, uint8_t bdaddr_type)
 {
 	if (bdaddr_type == BDADDR_BREDR)
 		device->bredr_seen = time(NULL);
 	else
 		device->le_seen = time(NULL);
+
+	if (!device_is_temporary(device))
+		return;
+
+	/* Restart temporary timer */
+	if (device->temporary_timer)
+		g_source_remove(device->temporary_timer);
+
+	device->temporary_timer = g_timeout_add_seconds(main_opts.tmpto,
+							device_disappeared,
+							device);
 }
 
 /* It is possible that we have two device objects for the same device in
@@ -5610,10 +5636,18 @@ void btd_device_set_temporary(struct btd_device *device, bool temporary)
 
 	device->temporary = temporary;
 
+	if (device->temporary_timer) {
+		g_source_remove(device->temporary_timer);
+		device->temporary_timer = 0;
+	}
+
 	if (temporary) {
 		if (device->bredr)
 			adapter_whitelist_remove(device->adapter, device);
 		adapter_connect_list_remove(device->adapter, device);
+		device->temporary_timer = g_timeout_add_seconds(main_opts.tmpto,
+							device_disappeared,
+							device);
 		return;
 	}
 
diff --git a/src/hcid.h b/src/hcid.h
index c21ac9980..5f249ebf9 100644
--- a/src/hcid.h
+++ b/src/hcid.h
@@ -53,6 +53,7 @@ struct main_opts {
 	gboolean	pairable;
 	uint32_t	pairto;
 	uint32_t	discovto;
+	uint32_t	tmpto;
 	uint8_t		privacy;
 
 	struct {
diff --git a/src/main.c b/src/main.c
index bacb44197..ec7a9fbd7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -67,6 +67,7 @@
 
 #define DEFAULT_PAIRABLE_TIMEOUT       0 /* disabled */
 #define DEFAULT_DISCOVERABLE_TIMEOUT 180 /* 3 minutes */
+#define DEFAULT_TEMPORARY_TIMEOUT     30 /* 30 seconds */
 
 #define SHUTDOWN_GRACE_SECONDS 10
 
@@ -89,6 +90,7 @@ static const char *supported_options[] = {
 	"FastConnectable",
 	"Privacy",
 	"JustWorksRepairing",
+	"TemporaryTimeout",
 	NULL
 };
 
@@ -531,6 +533,16 @@ static void parse_config(GKeyFile *config)
 		g_free(str);
 	}
 
+	val = g_key_file_get_integer(config, "General",
+						"TemporaryTimeout", &err);
+	if (err) {
+		DBG("%s", err->message);
+		g_clear_error(&err);
+	} else {
+		DBG("tmpto=%d", val);
+		main_opts.tmpto = val;
+	}
+
 	str = g_key_file_get_string(config, "General", "Name", &err);
 	if (err) {
 		DBG("%s", err->message);
@@ -672,6 +684,7 @@ static void init_defaults(void)
 	main_opts.class = 0x000000;
 	main_opts.pairto = DEFAULT_PAIRABLE_TIMEOUT;
 	main_opts.discovto = DEFAULT_DISCOVERABLE_TIMEOUT;
+	main_opts.tmpto = DEFAULT_TEMPORARY_TIMEOUT;
 	main_opts.reverse_discovery = TRUE;
 	main_opts.name_resolv = TRUE;
 	main_opts.debug_keys = FALSE;
diff --git a/src/main.conf b/src/main.conf
index 6a6f7d4b4..f41203b96 100644
--- a/src/main.conf
+++ b/src/main.conf
@@ -77,6 +77,11 @@
 # Defaults to "never"
 #JustWorksRepairing = never
 
+# How long to keep temporary devices around
+# The value is in seconds. Default is 30.
+# 0 = disable timer, i.e. never keep temporary devices
+#TemporaryTimeout = 30
+
 [Controller]
 # The following values are used to load default adapter parameters.  BlueZ loads
 # the values into the kernel before the adapter is powered if the kernel
-- 
2.25.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH BlueZ 2/2] adapter: Remove timer for removing temporary devices
  2020-07-15 20:46 [PATCH BlueZ 1/2] device: Add timer for removing temporary devices Luiz Augusto von Dentz
@ 2020-07-15 20:46 ` Luiz Augusto von Dentz
  2020-07-15 21:03 ` [PATCH BlueZ 1/2] device: Add " Bastien Nocera
  1 sibling, 0 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2020-07-15 20:46 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Now that each temporary device object can cleanup after it has detected
that it has disappeared it is no longer necessary to keep this logic as
well in the adapter.
---
 src/adapter.c | 30 ------------------------------
 1 file changed, 30 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 7a80554c5..c757447e2 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -258,7 +258,6 @@ struct btd_adapter {
 	GSList *discovery_found;	/* list of found devices */
 	guint discovery_idle_timeout;	/* timeout between discovery runs */
 	guint passive_scan_timeout;	/* timeout between passive scans */
-	guint temp_devices_timeout;	/* timeout for temporary devices */
 
 	guint pairable_timeout_id;	/* pairable timeout id */
 	guint auth_idle_id;		/* Pending authorization dequeue */
@@ -1484,27 +1483,6 @@ static void invalidate_rssi_and_tx_power(gpointer a)
 	device_set_tx_power(dev, 127);
 }
 
-static gboolean remove_temp_devices(gpointer user_data)
-{
-	struct btd_adapter *adapter = user_data;
-	GSList *l, *next;
-
-	DBG("%s", adapter->path);
-
-	adapter->temp_devices_timeout = 0;
-
-	for (l = adapter->devices; l != NULL; l = next) {
-		struct btd_device *dev = l->data;
-
-		next = g_slist_next(l);
-
-		if (device_is_temporary(dev) && !btd_device_is_connected(dev))
-			btd_adapter_remove_device(adapter, dev);
-	}
-
-	return FALSE;
-}
-
 static void discovery_cleanup(struct btd_adapter *adapter, int timeout)
 {
 	GSList *l, *next;
@@ -1516,11 +1494,6 @@ static void discovery_cleanup(struct btd_adapter *adapter, int timeout)
 		adapter->discovery_idle_timeout = 0;
 	}
 
-	if (adapter->temp_devices_timeout > 0) {
-		g_source_remove(adapter->temp_devices_timeout);
-		adapter->temp_devices_timeout = 0;
-	}
-
 	g_slist_free_full(adapter->discovery_found,
 						invalidate_rssi_and_tx_power);
 	adapter->discovery_found = NULL;
@@ -1536,9 +1509,6 @@ static void discovery_cleanup(struct btd_adapter *adapter, int timeout)
 		if (device_is_temporary(dev) && !device_is_connectable(dev))
 			btd_adapter_remove_device(adapter, dev);
 	}
-
-	adapter->temp_devices_timeout = g_timeout_add_seconds(timeout,
-						remove_temp_devices, adapter);
 }
 
 static void discovery_free(void *user_data)
-- 
2.25.3


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH BlueZ 1/2] device: Add timer for removing temporary devices
  2020-07-15 20:46 [PATCH BlueZ 1/2] device: Add timer for removing temporary devices Luiz Augusto von Dentz
  2020-07-15 20:46 ` [PATCH BlueZ 2/2] adapter: Remove " Luiz Augusto von Dentz
@ 2020-07-15 21:03 ` Bastien Nocera
  2020-07-16 16:40   ` Luiz Augusto von Dentz
  1 sibling, 1 reply; 4+ messages in thread
From: Bastien Nocera @ 2020-07-15 21:03 UTC (permalink / raw)
  To: Luiz Augusto von Dentz, linux-bluetooth

On Wed, 2020-07-15 at 13:46 -0700, Luiz Augusto von Dentz wrote:
> From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
> 
> This adds a timer for how long to keep temporary devices, it is
> updated
> everytime the device is seen so devices will be removed even during
> an
> active discovery session which means the discovering for a long
> period
> will no longer just grow without removing devices that disappeared.

Both patches look good to me.

Cheers


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH BlueZ 1/2] device: Add timer for removing temporary devices
  2020-07-15 21:03 ` [PATCH BlueZ 1/2] device: Add " Bastien Nocera
@ 2020-07-16 16:40   ` Luiz Augusto von Dentz
  0 siblings, 0 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2020-07-16 16:40 UTC (permalink / raw)
  To: Bastien Nocera; +Cc: linux-bluetooth

Hi,

On Wed, Jul 15, 2020 at 2:03 PM Bastien Nocera <hadess@hadess.net> wrote:
>
> On Wed, 2020-07-15 at 13:46 -0700, Luiz Augusto von Dentz wrote:
> > From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
> >
> > This adds a timer for how long to keep temporary devices, it is
> > updated
> > everytime the device is seen so devices will be removed even during
> > an
> > active discovery session which means the discovering for a long
> > period
> > will no longer just grow without removing devices that disappeared.
>
> Both patches look good to me.

Pushed.

-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-15 20:46 [PATCH BlueZ 1/2] device: Add timer for removing temporary devices Luiz Augusto von Dentz
2020-07-15 20:46 ` [PATCH BlueZ 2/2] adapter: Remove " Luiz Augusto von Dentz
2020-07-15 21:03 ` [PATCH BlueZ 1/2] device: Add " Bastien Nocera
2020-07-16 16:40   ` Luiz Augusto von Dentz

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org
	public-inbox-index linux-bluetooth

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git