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

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

Merge changes Ib080cfab,I0fe28244

* changes:
  floss: Add Get/SetConnectable and Get/SetDiscoverable adapter APIs
  floss: Rename AdapterDiscoveryTimeout to AdapterDiscoverableTimeout
parents d3b07a15 0f30a701
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
+71 −16
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},
@@ -25,6 +25,8 @@ use crate::bluetooth_media::{BluetoothMedia, IBluetoothMedia, MediaActions};
use crate::uuid::{Profile, UuidHelper};
use crate::{BluetoothCallbackType, Message, RPCProxy};

const DEFAULT_DISCOVERY_TIMEOUT_MS: u64 = 12800;

/// Defines the adapter API.
pub trait IBluetooth {
    /// Adds a callback from a client who wishes to observe adapter events.
@@ -67,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;

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

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

@@ -322,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 => {
@@ -786,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
    }
@@ -803,20 +867,11 @@ impl IBluetooth for Bluetooth {
            return 0;
        }

        match self.properties.get(&BtPropertyType::AdapterDiscoveryTimeout) {
            Some(variant) => match variant {
                BluetoothProperty::AdapterDiscoveryTimeout(timeout) => {
                    let seconds: u64 = (*timeout).into();
                    let elapsed = self.discovering_started.elapsed();
                    if elapsed.as_secs() >= seconds {
        let elapsed_ms = self.discovering_started.elapsed().as_millis() as u64;
        if elapsed_ms >= DEFAULT_DISCOVERY_TIMEOUT_MS {
            0
        } else {
                        seconds * 1000 - elapsed.as_millis() as u64
                    }
                }
                _ => 0,
            },
            _ => 0,
            DEFAULT_DISCOVERY_TIMEOUT_MS - elapsed_ms
        }
    }

+8 −8
Original line number Diff line number Diff line
@@ -134,7 +134,7 @@ pub enum BtPropertyType {
    ServiceRecord,
    AdapterScanMode,
    AdapterBondedDevices,
    AdapterDiscoveryTimeout,
    AdapterDiscoverableTimeout,
    RemoteFriendlyName,
    RemoteRssi,
    RemoteVersionInfo,
@@ -299,7 +299,7 @@ pub enum BluetoothProperty {
    ServiceRecord(BtServiceRecord),
    AdapterScanMode(BtScanMode),
    AdapterBondedDevices(Vec<RawAddress>),
    AdapterDiscoveryTimeout(u32),
    AdapterDiscoverableTimeout(u32),
    RemoteFriendlyName(String),
    RemoteRssi(i8),
    RemoteVersionInfo(BtRemoteVersion),
@@ -327,8 +327,8 @@ impl BluetoothProperty {
            BluetoothProperty::ServiceRecord(_) => BtPropertyType::ServiceRecord,
            BluetoothProperty::AdapterScanMode(_) => BtPropertyType::AdapterScanMode,
            BluetoothProperty::AdapterBondedDevices(_) => BtPropertyType::AdapterBondedDevices,
            BluetoothProperty::AdapterDiscoveryTimeout(_) => {
                BtPropertyType::AdapterDiscoveryTimeout
            BluetoothProperty::AdapterDiscoverableTimeout(_) => {
                BtPropertyType::AdapterDiscoverableTimeout
            }
            BluetoothProperty::RemoteFriendlyName(_) => BtPropertyType::RemoteFriendlyName,
            BluetoothProperty::RemoteRssi(_) => BtPropertyType::RemoteRssi,
@@ -356,7 +356,7 @@ impl BluetoothProperty {
            BluetoothProperty::AdapterBondedDevices(devlist) => {
                devlist.len() * mem::size_of::<RawAddress>()
            }
            BluetoothProperty::AdapterDiscoveryTimeout(_) => mem::size_of::<u32>(),
            BluetoothProperty::AdapterDiscoverableTimeout(_) => mem::size_of::<u32>(),
            BluetoothProperty::RemoteFriendlyName(name) => {
                cmp::min(PROPERTY_NAME_MAX, name.len() + 1)
            }
@@ -423,7 +423,7 @@ impl BluetoothProperty {
                    data[start..end].copy_from_slice(&dev.val);
                }
            }
            BluetoothProperty::AdapterDiscoveryTimeout(timeout) => {
            BluetoothProperty::AdapterDiscoverableTimeout(timeout) => {
                data.copy_from_slice(&timeout.to_ne_bytes());
            }
            BluetoothProperty::RemoteFriendlyName(name) => {
@@ -497,8 +497,8 @@ impl From<bindings::bt_property_t> for BluetoothProperty {
                    count,
                ))
            }
            BtPropertyType::AdapterDiscoveryTimeout => {
                BluetoothProperty::AdapterDiscoveryTimeout(u32_from_bytes(slice))
            BtPropertyType::AdapterDiscoverableTimeout => {
                BluetoothProperty::AdapterDiscoverableTimeout(u32_from_bytes(slice))
            }
            BtPropertyType::RemoteFriendlyName => {
                BluetoothProperty::RemoteFriendlyName(ascii_to_string(slice, len))