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

Commit 1dd39042 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "floss: Disconnect GATT in DisconnectAllEnabledProfiles" into main

parents 1fe5f59c a6cbc981
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -42,7 +42,9 @@ use tokio::time;

use crate::battery_service::BatteryServiceActions;
use crate::bluetooth_admin::{BluetoothAdmin, IBluetoothAdmin};
use crate::bluetooth_gatt::{BluetoothGatt, IBluetoothGatt, IScannerCallback, ScanResult};
use crate::bluetooth_gatt::{
    BluetoothGatt, GattActions, IBluetoothGatt, IScannerCallback, ScanResult,
};
use crate::bluetooth_media::{BluetoothMedia, IBluetoothMedia, MediaActions};
use crate::callbacks::Callbacks;
use crate::socket_manager::SocketActions;
@@ -228,6 +230,8 @@ pub trait IBluetooth {
    fn connect_all_enabled_profiles(&mut self, device: BluetoothDevice) -> bool;

    /// Disconnect all profiles supported by device and enabled on adapter.
    /// Note that it includes all custom profiles enabled by the users e.g. through SocketManager or
    /// BluetoothGatt interfaces; The device shall be disconnected on baseband eventually.
    fn disconnect_all_enabled_profiles(&mut self, device: BluetoothDevice) -> bool;

    /// Returns whether WBS is supported.
@@ -2718,6 +2722,12 @@ impl IBluetooth for Bluetooth {
            });
        }

        // Disconnect all GATT connections
        let txl = self.tx.clone();
        topstack::get_runtime().spawn(async move {
            let _ = txl.send(Message::GattActions(GattActions::Disconnect(device.clone()))).await;
        });

        return true;
    }

+68 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ use bt_utils::adv_parser;
use bt_utils::array_utils;

use crate::async_helper::{AsyncHelper, CallbackSender};
use crate::bluetooth::{Bluetooth, IBluetooth};
use crate::bluetooth::{Bluetooth, BluetoothDevice, IBluetooth};
use crate::bluetooth_adv::{
    AdvertiseData, AdvertiserId, Advertisers, AdvertisingSetInfo, AdvertisingSetParameters,
    IAdvertisingSetCallback, PeriodicAdvertisingParameters, INVALID_REG_ID,
@@ -184,6 +184,14 @@ impl ContextMap {
        }
    }

    fn get_client_ids_from_address(&self, address: &String) -> Vec<i32> {
        self.connections
            .iter()
            .filter(|conn| conn.address == *address)
            .map(|conn| conn.client_id)
            .collect()
    }

    fn get_callback_from_callback_id(
        &mut self,
        callback_id: u32,
@@ -328,6 +336,14 @@ impl ServerContextMap {
            .map(|conn| conn.conn_id);
    }

    fn get_server_ids_from_address(&self, address: &String) -> Vec<i32> {
        self.connections
            .iter()
            .filter(|conn| conn.address == *address)
            .map(|conn| conn.server_id)
            .collect()
    }

    fn get_address_from_conn_id(&self, conn_id: i32) -> Option<String> {
        self.connections
            .iter()
@@ -1354,6 +1370,12 @@ impl GattAsyncIntf {
    }
}

pub enum GattActions {
    /// This disconnects all server and client connections to the device.
    /// Params: remote_device
    Disconnect(BluetoothDevice),
}

/// Implementation of the GATT API (IBluetoothGatt).
pub struct BluetoothGatt {
    intf: Arc<Mutex<BluetoothInterface>>,
@@ -1810,6 +1832,51 @@ impl BluetoothGatt {
    pub(crate) fn stop_active_scan(&mut self, scanner_id: u8) -> BtStatus {
        self.stop_scan(scanner_id)
    }

    pub fn handle_action(&mut self, action: GattActions) {
        match action {
            GattActions::Disconnect(device) => {
                let address = match RawAddress::from_string(&device.address) {
                    None => {
                        warn!(
                            "GattActions::Disconnect failed: Invalid device address={}",
                            device.address
                        );
                        return;
                    }
                    Some(addr) => addr,
                };
                for client_id in self.context_map.get_client_ids_from_address(&device.address) {
                    if let Some(conn_id) =
                        self.context_map.get_conn_id_from_address(client_id, &device.address)
                    {
                        self.gatt
                            .as_ref()
                            .unwrap()
                            .lock()
                            .unwrap()
                            .client
                            .disconnect(client_id, &address, conn_id);
                    }
                }
                for server_id in
                    self.server_context_map.get_server_ids_from_address(&device.address)
                {
                    if let Some(conn_id) =
                        self.server_context_map.get_conn_id_from_address(server_id, &device.address)
                    {
                        self.gatt
                            .as_ref()
                            .unwrap()
                            .lock()
                            .unwrap()
                            .server
                            .disconnect(server_id, &address, conn_id);
                    }
                }
            }
        }
    }
}

#[derive(Debug, FromPrimitive, ToPrimitive)]
+5 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ use crate::bluetooth_admin::{BluetoothAdmin, IBluetoothAdmin};
use crate::bluetooth_gatt::{
    dispatch_gatt_client_callbacks, dispatch_gatt_server_callbacks, dispatch_le_adv_callbacks,
    dispatch_le_scanner_callbacks, dispatch_le_scanner_inband_callbacks, BluetoothGatt,
    GattActions,
};
use crate::bluetooth_media::{BluetoothMedia, MediaActions};
use crate::dis::{DeviceInformation, ServiceCallbacks};
@@ -124,6 +125,7 @@ pub enum Message {
    BatteryServiceRefresh,
    BatteryManagerCallbackDisconnected(u32),

    GattActions(GattActions),
    GattClientCallbackDisconnected(u32),
    GattServerCallbackDisconnected(u32),

@@ -377,6 +379,9 @@ impl Stack {
                Message::BatteryManagerCallbackDisconnected(id) => {
                    battery_manager.lock().unwrap().remove_callback(id);
                }
                Message::GattActions(action) => {
                    bluetooth_gatt.lock().unwrap().handle_action(action);
                }
                Message::GattClientCallbackDisconnected(id) => {
                    bluetooth_gatt.lock().unwrap().remove_client_callback(id);
                }