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

Commit 0f30a701 authored by Katherine Lai's avatar Katherine Lai
Browse files

floss: Add Get/SetConnectable and Get/SetDiscoverable adapter APIs

Bug: 196885534
Tag: #floss
Test: SetDiscoverable on/off using btclient
Change-Id: Ib080cfab9d3421edc8eee57bda5ef4076fb46370
parent 285f4f34
Loading
Loading
Loading
Loading
+88 −40
Original line number Diff line number Diff line
@@ -85,7 +85,8 @@ fn build_commands() -> HashMap<String, CommandOption> {
        String::from("adapter"),
        CommandOption {
            description: String::from(
                "Enable/Disable/Show default bluetooth adapter. (e.g. adapter enable)",
                "Enable/Disable/Show default bluetooth adapter. (e.g. adapter enable)\n
                 Discoverable On/Off (e.g. adapter discoverable on)",
            ),
            function_pointer: CommandHandler::cmd_adapter,
        },
@@ -244,7 +245,8 @@ impl CommandHandler {
        }

        let default_adapter = self.context.lock().unwrap().default_adapter;
        enforce_arg_len(args, 1, "adapter <enable|disable|show>", || match &args[0][0..] {
        enforce_arg_len(args, 1, "adapter <enable|disable|show|discoverable>", || {
            match &args[0][0..] {
                "enable" => {
                    self.context.lock().unwrap().manager_dbus.start(default_adapter);
                }
@@ -262,8 +264,18 @@ impl CommandHandler {
                        Some(x) => x.clone(),
                        None => String::from(""),
                    };
                let name = self.context.lock().unwrap().adapter_dbus.as_ref().unwrap().get_name();
                let uuids = self.context.lock().unwrap().adapter_dbus.as_ref().unwrap().get_uuids();
                    let name =
                        self.context.lock().unwrap().adapter_dbus.as_ref().unwrap().get_name();
                    let uuids =
                        self.context.lock().unwrap().adapter_dbus.as_ref().unwrap().get_uuids();
                    let is_discoverable = self
                        .context
                        .lock()
                        .unwrap()
                        .adapter_dbus
                        .as_ref()
                        .unwrap()
                        .get_discoverable();
                    let cod = self
                        .context
                        .lock()
@@ -275,17 +287,53 @@ impl CommandHandler {
                    print_info!("Address: {}", address);
                    print_info!("Name: {}", name);
                    print_info!("State: {}", if enabled { "enabled" } else { "disabled" });
                    print_info!("Discoverable: {}", is_discoverable);
                    print_info!("Class: {:#06x}", cod);
                    print_info!(
                        "Uuids: {}",
                        DisplayList(
                        uuids.iter().map(|&x| UuidHelper::to_string(&x)).collect::<Vec<String>>()
                            uuids
                                .iter()
                                .map(|&x| UuidHelper::to_string(&x))
                                .collect::<Vec<String>>()
                        )
                    );
                }
                "discoverable" => match &args[1][0..] {
                    "on" => {
                        let discoverable = self
                            .context
                            .lock()
                            .unwrap()
                            .adapter_dbus
                            .as_ref()
                            .unwrap()
                            .set_discoverable(true, 60);
                        print_info!(
                            "Set discoverable for 60s: {}",
                            if discoverable { "succeeded" } else { "failed" }
                        );
                    }
                    "off" => {
                        let discoverable = self
                            .context
                            .lock()
                            .unwrap()
                            .adapter_dbus
                            .as_ref()
                            .unwrap()
                            .set_discoverable(false, 60);
                        print_info!(
                            "Turn discoverable off: {}",
                            if discoverable { "succeeded" } else { "failed" }
                        );
                    }
                    _ => println!("Invalid argument for adapter discoverable '{}'", args[1]),
                },
                _ => {
                    println!("Invalid argument '{}'", args[0]);
                }
            }
        });
    }

+8 −0
Original line number Diff line number Diff line
@@ -309,6 +309,14 @@ impl IBluetooth for BluetoothDBus {
        self.client_proxy.method("SetBluetoothClass", (cod,))
    }

    fn get_discoverable(&self) -> bool {
        self.client_proxy.method("GetDiscoverable", ())
    }

    fn set_discoverable(&self, mode: bool, duration: u32) -> bool {
        self.client_proxy.method("SetDiscoverable", (mode, duration))
    }

    fn start_discovery(&self) -> bool {
        self.client_proxy.method("StartDiscovery", ())
    }
+10 −0
Original line number Diff line number Diff line
@@ -130,6 +130,16 @@ impl IBluetooth for IBluetoothDBus {
        true
    }

    #[dbus_method("GetDiscoverable")]
    fn get_discoverable(&self) -> bool {
        true
    }

    #[dbus_method("SetDiscoverable")]
    fn set_discoverable(&self, mode: bool, duration: u32) -> bool {
        true
    }

    #[dbus_method("StartDiscovery")]
    fn start_discovery(&self) -> bool {
        true
+64 −2
Original line number Diff line number Diff line
@@ -2,8 +2,8 @@

use bt_topshim::btif::{
    BaseCallbacks, BaseCallbacksDispatcher, BluetoothInterface, BluetoothProperty, BtAclState,
    BtBondState, BtDiscoveryState, BtHciErrorCode, BtPinCode, BtPropertyType, BtSspVariant,
    BtState, BtStatus, BtTransport, RawAddress, Uuid, Uuid128Bit,
    BtBondState, BtDiscoveryState, BtHciErrorCode, BtPinCode, BtPropertyType, BtScanMode,
    BtSspVariant, BtState, BtStatus, BtTransport, RawAddress, Uuid, Uuid128Bit,
};
use bt_topshim::{
    profiles::hid_host::{HHCallbacksDispatcher, HidHost},
@@ -69,6 +69,12 @@ pub trait IBluetooth {
    /// Sets the bluetooth class.
    fn set_bluetooth_class(&self, cod: u32) -> bool;

    /// Returns whether the adapter is discoverable.
    fn get_discoverable(&self) -> bool;

    /// Sets discoverability. If discoverable, limits the duration with given value.
    fn set_discoverable(&self, mode: bool, duration: u32) -> bool;

    /// Starts BREDR Inquiry.
    fn start_discovery(&self) -> bool;

@@ -245,6 +251,7 @@ pub struct Bluetooth {
    state: BtState,
    tx: Sender<Message>,
    uuid_helper: UuidHelper,
    is_connectable: bool,
}

impl Bluetooth {
@@ -271,6 +278,7 @@ impl Bluetooth {
            state: BtState::Off,
            tx,
            uuid_helper: UuidHelper::new(),
            is_connectable: false,
        }
    }

@@ -324,6 +332,29 @@ impl Bluetooth {
        }
    }

    fn get_connectable(&self) -> bool {
        match self.properties.get(&BtPropertyType::AdapterScanMode) {
            Some(prop) => match prop {
                BluetoothProperty::AdapterScanMode(mode) => match *mode {
                    BtScanMode::Connectable | BtScanMode::ConnectableDiscoverable => true,
                    _ => false,
                },
                _ => false,
            },
            _ => false,
        }
    }

    fn set_connectable(&mut self, mode: bool) -> bool {
        self.is_connectable = mode;
        if mode && self.get_discoverable() {
            return true;
        }
        self.intf.lock().unwrap().set_adapter_property(BluetoothProperty::AdapterScanMode(
            if mode { BtScanMode::Connectable } else { BtScanMode::None_ },
        )) == 0
    }

    pub(crate) fn callback_disconnected(&mut self, id: u32, cb_type: BluetoothCallbackType) {
        match cb_type {
            BluetoothCallbackType::Adapter => {
@@ -788,6 +819,37 @@ impl IBluetooth for Bluetooth {
        self.intf.lock().unwrap().set_adapter_property(BluetoothProperty::ClassOfDevice(cod)) == 0
    }

    fn get_discoverable(&self) -> bool {
        match self.properties.get(&BtPropertyType::AdapterScanMode) {
            Some(prop) => match prop {
                BluetoothProperty::AdapterScanMode(mode) => match mode {
                    BtScanMode::ConnectableDiscoverable => true,
                    _ => false,
                },
                _ => false,
            },
            _ => false,
        }
    }

    fn set_discoverable(&self, mode: bool, duration: u32) -> bool {
        self.intf
            .lock()
            .unwrap()
            .set_adapter_property(BluetoothProperty::AdapterDiscoverableTimeout(duration));
        self.intf.lock().unwrap().set_adapter_property(BluetoothProperty::AdapterScanMode(
            if mode {
                BtScanMode::ConnectableDiscoverable
            } else {
                if self.is_connectable {
                    BtScanMode::Connectable
                } else {
                    BtScanMode::None_
                }
            },
        )) == 0
    }

    fn start_discovery(&self) -> bool {
        self.intf.lock().unwrap().start_discovery() == 0
    }