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

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

Merge "Exported adapter object using --hci flag"

parents ea75976e 4807234a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -116,13 +116,13 @@ pub(crate) struct BluetoothDBus {

impl BluetoothDBus {
    pub(crate) fn new(conn: Arc<SyncConnection>, cr: Arc<Mutex<Crossroads>>) -> BluetoothDBus {
        // TODO: Adapter path should have hci number, e.g. /org/chromium/bluetooth/adapter/hci0.
        // TODO: Adapter client should dynamically accept hci # and be initialized
        BluetoothDBus {
            client_proxy: ClientDBusProxy {
                conn: conn.clone(),
                cr: cr,
                bus_name: String::from("org.chromium.bluetooth"),
                objpath: dbus::Path::new("/org/chromium/bluetooth/adapter").unwrap(),
                objpath: dbus::Path::new("/org/chromium/bluetooth/adapter0").unwrap(),
                interface: String::from("org.chromium.bluetooth.Bluetooth"),
            },
        }
+39 −3
Original line number Diff line number Diff line
@@ -31,6 +31,21 @@ const OBJECT_BLUETOOTH: &str = "/org/chromium/bluetooth/adapter";
const OBJECT_BLUETOOTH_GATT: &str = "/org/chromium/bluetooth/gatt";
const OBJECT_BLUETOOTH_MEDIA: &str = "/org/chromium/bluetooth/media";

/// Check command line arguments for target hci adapter (--hci=N). If no adapter
/// is set, default to 0.
fn get_adapter_index(args: &Vec<String>) -> i32 {
    for arg in args {
        if arg.starts_with("--hci=") {
            let num = (&arg[6..]).parse::<i32>();
            if num.is_ok() {
                return num.unwrap();
            }
        }
    }

    0
}

/// Runs the Bluetooth daemon serving D-Bus IPC.
fn main() -> Result<(), Box<dyn Error>> {
    let (tx, rx) = Stack::create_channel();
@@ -44,6 +59,9 @@ fn main() -> Result<(), Box<dyn Error>> {
    // Args don't include arg[0] which is the binary name
    let all_args = std::env::args().collect::<Vec<String>>();
    let args = all_args[1..].to_vec();

    let adapter_index = get_adapter_index(&args);

    intf.lock().unwrap().initialize(get_bt_dispatcher(tx), args);

    bluetooth.lock().unwrap().init_profiles();
@@ -85,7 +103,7 @@ fn main() -> Result<(), Box<dyn Error>> {

        // Register D-Bus method handlers of IBluetooth.
        iface_bluetooth::export_bluetooth_dbus_obj(
            String::from(OBJECT_BLUETOOTH),
            String::from(format!("{}{}", OBJECT_BLUETOOTH, adapter_index)),
            conn.clone(),
            &mut cr,
            bluetooth,
@@ -93,7 +111,7 @@ fn main() -> Result<(), Box<dyn Error>> {
        );
        // Register D-Bus method handlers of IBluetoothGatt.
        iface_bluetooth_gatt::export_bluetooth_gatt_dbus_obj(
            String::from(OBJECT_BLUETOOTH_GATT),
            String::from(format!("{}{}", OBJECT_BLUETOOTH_GATT, adapter_index)),
            conn.clone(),
            &mut cr,
            bluetooth_gatt,
@@ -101,7 +119,7 @@ fn main() -> Result<(), Box<dyn Error>> {
        );

        iface_bluetooth_media::export_bluetooth_media_dbus_obj(
            String::from(OBJECT_BLUETOOTH_MEDIA),
            String::from(format!("{}{}", OBJECT_BLUETOOTH_MEDIA, adapter_index)),
            conn.clone(),
            &mut cr,
            bluetooth_media,
@@ -121,3 +139,21 @@ fn main() -> Result<(), Box<dyn Error>> {
        unreachable!()
    })
}

#[cfg(test)]
mod tests {
    use crate::get_adapter_index;

    #[test]
    fn device_index_parsed() {
        // A few failing cases
        assert_eq!(get_adapter_index(&vec! {}), 0);
        assert_eq!(get_adapter_index(&vec! {"--bar".to_string(), "--hci".to_string()}), 0);
        assert_eq!(get_adapter_index(&vec! {"--hci=foo".to_string()}), 0);
        assert_eq!(get_adapter_index(&vec! {"--hci=12t".to_string()}), 0);

        // Some passing cases
        assert_eq!(get_adapter_index(&vec! {"--hci=12".to_string()}), 12);
        assert_eq!(get_adapter_index(&vec! {"--hci=1".to_string(), "--hci=2".to_string()}), 1);
    }
}