From 8d44a5789e6856ffbd959a1418a0d86a8d95ffc0 Mon Sep 17 00:00:00 2001 From: Archie Pusaka Date: Mon, 22 Feb 2021 13:18:45 +0800 Subject: [PATCH] adapter: Check whether adapter is pending powered The powered setting of adapter is frequently being checked, but the check only accounts for whether it currently is powered. When powering off the adapter, there is a brief moment when the adapter is already off kernel-wise, but powered property is still true. If powered property is accessed at this time, some problems might occur. For example, if RemoveDevice DBus API is called, we would still carry out the request and remove the device from the user space, but we would fail to remove it from the kernel, therefore resulting an error when we want to re-pair the device. This patch addresses this issue by also checking whether adapter is being powered off when checking for powered. Reviewed-by: Sonny Sasaka Signed-off-by: Anuj Jain Signed-off-by: Ayush Garg --- src/adapter.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index d5a5765..e46a839 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -958,11 +958,10 @@ static void settings_changed(struct btd_adapter *adapter, uint32_t settings) } } - if (changed_mask & MGMT_SETTING_LE) { - if ((adapter->current_settings & MGMT_SETTING_POWERED) && + if ((changed_mask & MGMT_SETTING_LE) && + btd_adapter_get_powered(adapter) && (adapter->current_settings & MGMT_SETTING_LE)) - trigger_passive_scanning(adapter); - } + trigger_passive_scanning(adapter); if (changed_mask & MGMT_SETTING_DISCOVERABLE) { g_dbus_emit_property_changed(dbus_conn, adapter->path, @@ -2534,7 +2533,7 @@ static void trigger_start_discovery(struct btd_adapter *adapter, guint delay) * * This is safe-guard and should actually never trigger. */ - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return; adapter->discovery_idle_timeout = g_timeout_add_seconds(delay, @@ -6369,7 +6368,7 @@ static DBusMessage *start_discovery(DBusConnection *conn, DBG("sender %s", sender); - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return btd_error_not_ready(msg); is_discovering = get_discovery_client(adapter, sender, &client); @@ -6672,7 +6671,7 @@ static DBusMessage *set_discovery_filter(DBusConnection *conn, DBG("sender %s", sender); - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return btd_error_not_ready(msg); if (MGMT_VERSION(mgmt_version, mgmt_revision) < MGMT_VERSION(1, 8)) @@ -6729,7 +6728,7 @@ static DBusMessage *stop_discovery(DBusConnection *conn, DBG("sender %s", sender); - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return btd_error_not_ready(msg); list = g_slist_find_custom(adapter->discovery_list, sender, @@ -7151,7 +7150,7 @@ static void property_set_discoverable(const GDBusPropertyTable *property, struct btd_adapter *adapter = user_data; if (adapter->discoverable_timeout > 0 && - !(adapter->current_settings & MGMT_SETTING_POWERED)) { + !btd_adapter_get_powered(adapter)) { g_dbus_pending_property_error(id, ERROR_INTERFACE ".Failed", "Not Powered"); return; @@ -7548,7 +7547,7 @@ static DBusMessage *remove_device(DBusConnection *conn, if (!list) return btd_error_does_not_exist(msg); - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return btd_error_not_ready(msg); device = list->data; @@ -7699,7 +7698,7 @@ static DBusMessage *connect_device(DBusConnection *conn, DBG("sender %s", dbus_message_get_sender(msg)); - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return btd_error_not_ready(msg); dbus_message_iter_init(msg, &iter); @@ -9668,7 +9667,8 @@ bool btd_adapter_get_pairable(struct btd_adapter *adapter) bool btd_adapter_get_powered(struct btd_adapter *adapter) { - if (adapter->current_settings & MGMT_SETTING_POWERED) + if ((adapter->current_settings & MGMT_SETTING_POWERED) && + !(adapter->pending_settings & MGMT_SETTING_POWERED)) return true; return false; @@ -9759,7 +9759,7 @@ int adapter_connect_list_add(struct btd_adapter *adapter, adapter->system_name); done: - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return 0; trigger_passive_scanning(adapter); @@ -9796,7 +9796,7 @@ void adapter_connect_list_remove(struct btd_adapter *adapter, return; } - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return; trigger_passive_scanning(adapter); @@ -12537,7 +12537,7 @@ int btd_cancel_authorization(guint id) int btd_adapter_restore_powered(struct btd_adapter *adapter) { - if (adapter->current_settings & MGMT_SETTING_POWERED) + if (btd_adapter_get_powered(adapter)) return 0; set_mode(adapter, MGMT_OP_SET_POWERED, 0x01); @@ -12572,7 +12572,7 @@ void btd_adapter_register_msd_cb(struct btd_adapter *adapter, int btd_adapter_set_fast_connectable(struct btd_adapter *adapter, gboolean enable) { - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return -EINVAL; set_mode(adapter, MGMT_OP_SET_FAST_CONNECTABLE, enable ? 0x01 : 0x00); @@ -12584,7 +12584,7 @@ int btd_adapter_read_clock(struct btd_adapter *adapter, const bdaddr_t *bdaddr, int which, int timeout, uint32_t *clock, uint16_t *accuracy) { - if (!(adapter->current_settings & MGMT_SETTING_POWERED)) + if (!btd_adapter_get_powered(adapter)) return -EINVAL; return -ENOSYS; @@ -14704,7 +14704,7 @@ load: /* retrieve the active connections: address the scenario where * the are active connections before the daemon've started */ - if (adapter->current_settings & MGMT_SETTING_POWERED) + if (btd_adapter_get_powered(adapter)) load_connections(adapter); adapter->initialized = TRUE; @@ -15879,7 +15879,7 @@ static void read_info_complete(uint8_t status, uint16_t length, if (adapter->stored_discoverable && !adapter->discoverable_timeout) set_discoverable(adapter, 0x01, 0); - if (adapter->current_settings & MGMT_SETTING_POWERED) + if (btd_adapter_get_powered(adapter)) #ifdef TIZEN_FEATURE_BLUEZ_MODIFY g_idle_add(adapter_start_idle_cb, adapter); #else -- 2.7.4