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

Commit d6f23e79 authored by En-Shuo Hsu's avatar En-Shuo Hsu
Browse files

floss: Add the interface to set HFP device active

It was covered by the bta_ag_svc_conn_open so we didn't need to call it
proactively.
```
    if (bta_ag_get_active_device().IsEmpty()) {
      bta_ag_api_set_active_device(p_scb->peer_addr);
    }
```

However, it's better to follow the same flow as A2DP to set the device
active first before we use it. This facilitates future work to support
multiple Bluetooth device connections. This also prevent future
regression if we decide to remove the set device in svc_conn_open.

Bug: 241816822
Tag: #floss
Test: dbus-send --system  --type=method_call --print-reply \
--dest=org.chromium.bluetooth /org/chromium/bluetooth/hci0/media \
org.chromium.bluetooth.BluetoothMedia.SetHfpActiveDevice
string:<address>

Change-Id: Ib66a83ebdffd94402ce0a9660e4447421f667116
parent 1dfdf11b
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -105,13 +105,18 @@ impl IBluetoothMedia for IBluetoothMediaDBus {
        dbus_generated!()
    }

    #[dbus_method("Disconnect")]
    fn disconnect(&mut self, address: String) {
        dbus_generated!()
    }

    #[dbus_method("SetActiveDevice")]
    fn set_active_device(&mut self, address: String) {
        dbus_generated!()
    }

    #[dbus_method("Disconnect")]
    fn disconnect(&mut self, address: String) {
    #[dbus_method("SetHfpActiveDevice")]
    fn set_hfp_active_device(&mut self, address: String) {
        dbus_generated!()
    }

+38 −15
Original line number Diff line number Diff line
@@ -45,8 +45,14 @@ pub trait IBluetoothMedia {
    fn cleanup(&mut self) -> bool;

    fn connect(&mut self, address: String);
    fn set_active_device(&mut self, address: String);
    fn disconnect(&mut self, address: String);

    // Set the device as the active A2DP device
    fn set_active_device(&mut self, address: String);

    // Set the device as the active HFP device
    fn set_hfp_active_device(&mut self, address: String);

    fn set_audio_config(
        &mut self,
        sample_rate: i32,
@@ -608,6 +614,31 @@ impl IBluetoothMedia for BluetoothMedia {
        true
    }

    fn disconnect(&mut self, address: String) {
        let addr = match RawAddress::from_string(address.clone()) {
            None => {
                warn!("Invalid device address {}", address);
                return;
            }
            Some(addr) => addr,
        };

        match self.a2dp.as_mut() {
            Some(a2dp) => a2dp.disconnect(addr),
            None => warn!("Uninitialized A2DP to disconnect {}", address),
        };

        match self.hfp.as_mut() {
            Some(hfp) => hfp.disconnect(addr),
            None => warn!("Uninitialized HFP to disconnect {}", address),
        };

        match self.avrcp.as_mut() {
            Some(avrcp) => avrcp.disconnect(addr),
            None => warn!("Uninitialized AVRCP to disconnect {}", address),
        };
    }

    fn set_active_device(&mut self, address: String) {
        let addr = match RawAddress::from_string(address.clone()) {
            None => {
@@ -624,7 +655,7 @@ impl IBluetoothMedia for BluetoothMedia {
        self.uinput.set_active_device(addr.to_string());
    }

    fn disconnect(&mut self, address: String) {
    fn set_hfp_active_device(&mut self, address: String) {
        let addr = match RawAddress::from_string(address.clone()) {
            None => {
                warn!("Invalid device address {}", address);
@@ -633,20 +664,12 @@ impl IBluetoothMedia for BluetoothMedia {
            Some(addr) => addr,
        };

        match self.a2dp.as_mut() {
            Some(a2dp) => a2dp.disconnect(addr),
            None => warn!("Uninitialized A2DP to disconnect {}", address),
        };

        match self.hfp.as_mut() {
            Some(hfp) => hfp.disconnect(addr),
            None => warn!("Uninitialized HFP to disconnect {}", address),
        };

        match self.avrcp.as_mut() {
            Some(avrcp) => avrcp.disconnect(addr),
            None => warn!("Uninitialized AVRCP to disconnect {}", address),
        };
            Some(hfp) => {
                hfp.set_active_device(addr);
            }
            None => warn!("Uninitialized HFP to set active device"),
        }
    }

    fn set_audio_config(
+5 −0
Original line number Diff line number Diff line
@@ -221,6 +221,11 @@ int HfpIntf::connect_audio(RustRawAddress bt_addr, bool sco_offload) {
  return intf_->ConnectAudio(&addr);
}

int HfpIntf::set_active_device(RustRawAddress bt_addr) {
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->SetActiveDevice(&addr);
}

int HfpIntf::set_volume(int8_t volume, RustRawAddress bt_addr) {
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->VolumeControl(headset::bthf_volume_type_t::BTHF_VOLUME_TYPE_SPK, volume, &addr);
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ class HfpIntf {
  int init();
  int connect(RustRawAddress bt_addr);
  int connect_audio(RustRawAddress bt_addr, bool sco_offload);
  int set_active_device(RustRawAddress bt_addr);
  int set_volume(int8_t volume, RustRawAddress bt_addr);
  int disconnect(RustRawAddress bt_addr);
  int disconnect_audio(RustRawAddress bt_addr);
+5 −0
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ pub mod ffi {
            bt_addr: RustRawAddress,
            sco_offload: bool,
        ) -> i32;
        fn set_active_device(self: Pin<&mut HfpIntf>, bt_addr: RustRawAddress) -> i32;
        fn set_volume(self: Pin<&mut HfpIntf>, volume: i8, bt_addr: RustRawAddress) -> i32;
        fn disconnect(self: Pin<&mut HfpIntf>, bt_addr: RustRawAddress) -> i32;
        fn disconnect_audio(self: Pin<&mut HfpIntf>, bt_addr: RustRawAddress) -> i32;
@@ -187,6 +188,10 @@ impl Hfp {
        self.internal.pin_mut().connect_audio(addr.into(), sco_offload)
    }

    pub fn set_active_device(&mut self, addr: RawAddress) -> i32 {
        self.internal.pin_mut().set_active_device(addr.into())
    }

    pub fn set_volume(&mut self, volume: i8, addr: RawAddress) -> i32 {
        self.internal.pin_mut().set_volume(volume, addr.into())
    }