adv_monitor: Clear any running DeviceLost timers on power down
authorManish Mandlik <mmandlik@google.com>
Fri, 10 Sep 2021 02:05:36 +0000 (19:05 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 11 Mar 2022 13:38:36 +0000 (19:08 +0530)
This patch clears any running Adv Monitor DeviceLost timers on bt power
down. It'll also invoke DeviceLost event if the device is already found
and is being tracked for the DeviceLost event.

Verified this by adding a monitor using bluetoothctl and confirming that
the DeviceLost event is getting triggered for already found device in
case of bt power down.

Reviewed-by: mcchou@google.com
Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/adapter.c
src/adv_monitor.c
src/adv_monitor.h

index 1ca4031..2a850fe 100644 (file)
@@ -7102,6 +7102,8 @@ static void property_set_mode(struct btd_adapter *adapter, uint32_t setting,
                len = sizeof(mode);
 
                if (!mode) {
+                       btd_adv_monitor_power_down(
+                                               adapter->adv_monitor_manager);
                        clear_discoverable(adapter);
                        remove_temporary_devices(adapter);
                }
index f8f1b86..4cd3627 100644 (file)
@@ -2010,3 +2010,54 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
                                            NULL);
        }
 }
+
+/* Clears running DeviceLost timer for a given device */
+static void clear_device_lost_timer(void *data, void *user_data)
+{
+       struct adv_monitor_device *dev = data;
+       struct adv_monitor *monitor = NULL;
+
+       if (dev->lost_timer) {
+               timeout_remove(dev->lost_timer);
+               dev->lost_timer = 0;
+
+               monitor = dev->monitor;
+
+               DBG("Calling DeviceLost() for device %p on Adv Monitor "
+                               "of owner %s at path %s", dev->device,
+                               monitor->app->owner, monitor->path);
+
+               g_dbus_proxy_method_call(monitor->proxy, "DeviceLost",
+                               report_device_state_setup,
+                               NULL, dev->device, NULL);
+       }
+}
+
+/* Clears running DeviceLost timers from each monitor */
+static void clear_lost_timers_from_monitor(void *data, void *user_data)
+{
+       struct adv_monitor *monitor = data;
+
+       queue_foreach(monitor->devices, clear_device_lost_timer, NULL);
+}
+
+/* Clears running DeviceLost timers from each app */
+static void clear_lost_timers_from_app(void *data, void *user_data)
+{
+       struct adv_monitor_app *app = data;
+
+       queue_foreach(app->monitors, clear_lost_timers_from_monitor, NULL);
+}
+
+/* Handles bt power down scenario */
+void btd_adv_monitor_power_down(struct btd_adv_monitor_manager *manager)
+{
+       if (!manager) {
+               error("Unexpected NULL btd_adv_monitor_manager object upon "
+                               "power down");
+               return;
+       }
+
+       /* Clear any running DeviceLost timers in case of power down */
+       queue_foreach(manager->apps, clear_lost_timers_from_app, NULL);
+}
index b5a143c..d9cb9cc 100644 (file)
@@ -38,5 +38,6 @@ void btd_adv_monitor_notify_monitors(struct btd_adv_monitor_manager *manager,
 void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager,
                                   struct btd_device *device);
 
+void btd_adv_monitor_power_down(struct btd_adv_monitor_manager *manager);
 
 #endif /* __ADV_MONITOR_H */