Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7e4e72ba authored by Hsin-chen Chuang's avatar Hsin-chen Chuang Committed by Gerrit Code Review
Browse files

Merge "Revert "Floss: Only enable page scan when necessary"" into main

parents c5e5eda8 de1b2729
Loading
Loading
Loading
Loading
+14 −66
Original line number Diff line number Diff line
@@ -308,9 +308,6 @@ pub enum DelayedActions {

    /// Scanner for BLE discovery is reporting a result.
    BleDiscoveryScannerResult(ScanResult),

    /// Update the connectable mode to allow or disallow classic reconnect
    UpdateConnectableMode(bool),
}

/// Serializable device used in various apis.
@@ -605,35 +602,6 @@ impl Bluetooth {
        }
    }

    fn update_connectable_mode(&mut self, is_sock_listening: bool) {
        // Set connectable if
        // - there is bredr socket listening, or
        // - there is a classic device bonded and not connected
        self.set_connectable_internal(
            is_sock_listening
                || self.bonded_devices.iter().any(|(_name, ctx)| {
                    ctx.acl_state == BtAclState::Disconnected
                        && ctx
                            .properties
                            .get(&BtPropertyType::TypeOfDevice)
                            .and_then(|prop| match prop {
                                BluetoothProperty::TypeOfDevice(transport) => {
                                    Some(*transport != BtDeviceType::Ble)
                                }
                                _ => None,
                            })
                            .unwrap_or(false)
                }),
        );
    }

    fn trigger_update_connectable_mode(&self) {
        let txl = self.tx.clone();
        tokio::spawn(async move {
            let _ = txl.send(Message::TriggerUpdateConnectableMode).await;
        });
    }

    fn disable_profile(&mut self, profile: &Profile) {
        if !UuidHelper::is_profile_supported(profile) {
            return;
@@ -1106,9 +1074,6 @@ impl Bluetooth {
                    self.found_devices.insert(address.clone(), device_with_props);
                }
            }
            DelayedActions::UpdateConnectableMode(is_sock_listening) => {
                self.update_connectable_mode(is_sock_listening);
            }
        }
    }

@@ -1435,8 +1400,8 @@ impl BtifBluetoothCallbacks for Bluetooth {
                self.ble_scanner_uuid =
                    Some(self.bluetooth_gatt.lock().unwrap().register_scanner(callback_id));

                // Update connectable mode so that disconnected bonded classic device can reconnect
                self.trigger_update_connectable_mode();
                // Ensure device is connectable so that disconnected device can reconnect
                self.set_connectable(true);

                // Spawn a freshness check job in the background.
                self.freshness_check.take().map(|h| h.abort());
@@ -1522,9 +1487,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
                        self.bonded_devices.entry(address.clone()).or_insert(device).bond_state =
                            BtBondState::Bonded;
                    }

                    // Update the connectable mode since bonded device list might be updated.
                    self.trigger_update_connectable_mode();
                }
                BluetoothProperty::BdName(bdname) => {
                    self.callbacks.for_all_callbacks(|callback| {
@@ -1755,9 +1717,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
            self.resume_discovery();
        }

        // Update the connectable mode since bonded list could be changed.
        self.trigger_update_connectable_mode();

        // Send bond state changed notifications
        self.callbacks.for_all_callbacks(|callback| {
            callback.on_bond_state_changed(
@@ -1785,7 +1744,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
    ) {
        let address = addr.to_string();
        let txl = self.tx.clone();

        let device = match self.get_remote_device_if_found_mut(&address) {
            None => {
                self.found_devices.insert(
@@ -1827,9 +1785,8 @@ impl BtifBluetoothCallbacks for Bluetooth {
                    d.wait_to_connect = false;

                    let sent_info = info.clone();
                    let tx = txl.clone();
                    tokio::spawn(async move {
                        let _ = tx
                        let _ = txl
                            .send(Message::DelayedAdapterActions(
                                DelayedActions::ConnectAllProfiles(sent_info),
                            ))
@@ -1852,14 +1809,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
            }
            None => (),
        }

        if properties.iter().any(|prop| match prop {
            BluetoothProperty::TypeOfDevice(_) => true,
            _ => false,
        }) {
            // Update the connectable mode since the device type is changed.
            self.trigger_update_connectable_mode();
        }
    }

    fn acl_state(
@@ -1894,7 +1843,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
            return;
        }

        let txl = self.tx.clone();
        let address = addr.to_string();
        let device = match self.get_remote_device_if_found_mut(&address) {
            None => {
@@ -1940,12 +1888,12 @@ impl BtifBluetoothCallbacks for Bluetooth {
                            self.connection_callbacks.for_all_callbacks(|callback| {
                                callback.on_device_connected(device.clone());
                            });
                            let tx = self.tx.clone();
                            let transport = match self.get_remote_type(bluetooth_device.clone()) {
                                BtDeviceType::Bredr => BtTransport::Bredr,
                                BtDeviceType::Ble => BtTransport::Le,
                                _ => acl_reported_transport,
                            };
                            let tx = txl.clone();
                            tokio::spawn(async move {
                                let _ = tx
                                    .send(Message::OnAclConnected(bluetooth_device, transport))
@@ -1956,7 +1904,7 @@ impl BtifBluetoothCallbacks for Bluetooth {
                            self.connection_callbacks.for_all_callbacks(|callback| {
                                callback.on_device_disconnected(device.clone());
                            });
                            let tx = txl.clone();
                            let tx = self.tx.clone();
                            tokio::spawn(async move {
                                let _ = tx.send(Message::OnAclDisconnected(device.clone())).await;
                            });
@@ -1966,9 +1914,6 @@ impl BtifBluetoothCallbacks for Bluetooth {
            }
            None => (),
        };

        // Update the connectable since the connected state could be changed.
        self.trigger_update_connectable_mode();
    }

    fn thread_event(&mut self, event: BtThreadEvent) {
@@ -2152,13 +2097,13 @@ impl IBluetooth for Bluetooth {
            return false;
        }

        let off_mode =
            if self.is_connectable { BtScanMode::Connectable } else { BtScanMode::None_ };

        let new_mode = match mode {
            BtDiscMode::LimitedDiscoverable => BtScanMode::ConnectableLimitedDiscoverable,
            BtDiscMode::GeneralDiscoverable => BtScanMode::ConnectableDiscoverable,
            BtDiscMode::NonDiscoverable => match self.is_connectable {
                true => BtScanMode::Connectable,
                false => BtScanMode::None_,
            },
            BtDiscMode::NonDiscoverable => off_mode.clone(),
        };

        // The old timer should be overwritten regardless of what the new mode is.
@@ -2174,10 +2119,13 @@ impl IBluetooth for Bluetooth {
        }

        if (mode != BtDiscMode::NonDiscoverable) && (duration != 0) {
            let txl = self.tx.clone();
            let intf_clone = self.intf.clone();
            self.discoverable_timeout = Some(tokio::spawn(async move {
                time::sleep(Duration::from_secs(duration.into())).await;
                let _ = txl.send(Message::TriggerUpdateConnectableMode).await;
                intf_clone
                    .lock()
                    .unwrap()
                    .set_adapter_property(BluetoothProperty::AdapterScanMode(off_mode));
            }));
        }

+0 −8
Original line number Diff line number Diff line
@@ -97,7 +97,6 @@ pub enum Message {
    ConnectionCallbackDisconnected(u32),

    // Some delayed actions for the adapter.
    TriggerUpdateConnectableMode,
    DelayedAdapterActions(DelayedActions),

    // Follows IBluetooth's on_device_(dis)connected callback but doesn't require depending on
@@ -318,13 +317,6 @@ impl Stack {
                    bluetooth.lock().unwrap().connection_callback_disconnected(id);
                }

                Message::TriggerUpdateConnectableMode => {
                    let is_listening = bluetooth_socketmgr.lock().unwrap().is_listening();
                    bluetooth.lock().unwrap().handle_delayed_actions(
                        DelayedActions::UpdateConnectableMode(is_listening),
                    );
                }

                Message::DelayedAdapterActions(action) => {
                    bluetooth.lock().unwrap().handle_delayed_actions(action);
                }
+1 −24
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::task::JoinHandle;
use tokio::time;

use crate::bluetooth::{BluetoothDevice, DelayedActions};
use crate::bluetooth::BluetoothDevice;
use crate::bluetooth_admin::{BluetoothAdmin, IBluetoothAdmin};
use crate::callbacks::Callbacks;
use crate::uuid::UuidHelper;
@@ -554,19 +554,6 @@ impl BluetoothSocketManager {
        self.sock = Some(socket::BtSocket::new(&intf.lock().unwrap()));
    }

    /// Check if there is any listening socket.
    pub fn is_listening(&self) -> bool {
        self.listening.values().any(|vs| !vs.is_empty())
    }

    /// Trigger adapter to update connectable mode.
    fn trigger_update_connectable_mode(&self) {
        let txl = self.tx.clone();
        tokio::spawn(async move {
            let _ = txl.send(Message::TriggerUpdateConnectableMode).await;
        });
    }

    // TODO(abps) - We need to save information about who the caller is so that
    //              we can pipe it down to the lower levels. This needs to be
    //              provided by the projection layer and is currently missing.
@@ -665,9 +652,6 @@ impl BluetoothSocketManager {
                    .or_default()
                    .push(InternalListeningSocket::new(cbid, id, runner_tx, uuid, joinhandle));

                // Update the connectable mode since the list of listening socket has changed.
                self.trigger_update_connectable_mode();

                SocketResult::new(status, id)
            }
            Err(_) => {
@@ -1188,9 +1172,6 @@ impl BluetoothSocketManager {
                        .entry(cbid)
                        .and_modify(|v| v.retain(|s| s.socket_id != socket_id));
                }

                // Update the connectable mode since the list of listening socket has changed.
                self.trigger_update_connectable_mode();
            }

            SocketActions::OnHandleIncomingConnection(cbid, socket_id, socket) => {
@@ -1264,10 +1245,6 @@ impl BluetoothSocketManager {
                });
            }
        });

        // Update the connectable mode since the list of listening socket has changed.
        self.trigger_update_connectable_mode();

        self.callbacks.remove_callback(callback);
    }