device: Add timer for removing temporary devices
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 15 Jul 2020 20:23:23 +0000 (13:23 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 12 Apr 2021 09:00:49 +0000 (14:30 +0530)
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.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/device.c
src/hcid.h
src/main.c
src/main.conf

index e04ee2419cd927fdc861bf63a0e99388cab2f023..7f2874333b575675738831b6838d344fd6a542c8 100644 (file)
@@ -276,6 +276,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 */
@@ -929,6 +930,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);
 
@@ -6165,12 +6169,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
@@ -8070,10 +8096,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;
        }
 
index e675ba2f9d5ca0a7ca793083f5595eb9d2d6c234..bd90ef8e1d7a53b3af153f22f4f15829e536a5f2 100755 (executable)
@@ -53,6 +53,7 @@ struct main_opts {
        gboolean        pairable;
        uint32_t        pairto;
        uint32_t        discovto;
+       uint32_t        tmpto;
        uint8_t         privacy;
 
        struct {
index 139732471cf055eeec305ac64fb87e3758c1aa10..357a50da009b72cf3aee557a1f3b43d5d170e144 100755 (executable)
@@ -66,6 +66,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
 
@@ -88,6 +89,7 @@ static const char *supported_options[] = {
        "FastConnectable",
        "Privacy",
        "JustWorksRepairing",
+       "TemporaryTimeout",
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        "EnableLEPrivacy",
        "PinCode",
@@ -535,6 +537,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);
@@ -702,6 +714,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;
index 2b7a6f02dea424bf645c6a075c5999b998d50be5..215b2d51d011d4bf75aa09ce64610919c49250b8 100755 (executable)
 # 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