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

Commit c2d8341a authored by Michael Sun's avatar Michael Sun
Browse files

floss: avrcp: refactor the AVRCP topshim interface

Minor refactor the AVRCP topshim interface to utilize DeviceConnected,
and DeviceDisconnected naming conventions as more AVRCP functionalities
will start to depend on the device connection state. Pass in the
address info for AVRCP callbacks.

Bug: 240780817
Tag: #floss
Test: ./build.py

Change-Id: Idfd9075609b35620555bfa113cfd21cd77d1fa75
parent 73a92e76
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -214,17 +214,19 @@ impl BluetoothMedia {

    pub fn dispatch_avrcp_callbacks(&mut self, cb: AvrcpCallbacks) {
        match cb {
            AvrcpCallbacks::AvrcpAbsoluteVolumeEnabled(supported) => {
            AvrcpCallbacks::AvrcpDeviceConnected(_addr, supported) => {
                self.absolute_volume = supported;
                self.callbacks.lock().unwrap().for_all_callbacks(|callback| {
                    callback.on_absolute_volume_supported_changed(supported);
                });
            }
            AvrcpCallbacks::AvrcpDeviceDisconnected(_addr) => {}
            AvrcpCallbacks::AvrcpAbsoluteVolumeUpdate(volume) => {
                self.callbacks.lock().unwrap().for_all_callbacks(|callback| {
                    callback.on_absolute_volume_changed(volume);
                });
            }
            AvrcpCallbacks::AvrcpSendKeyEvent(_key, _value) => {}
        }
    }

+22 −16
Original line number Diff line number Diff line
@@ -33,7 +33,9 @@ namespace rusty = ::bluetooth::topshim::rust;
namespace bluetooth::avrcp {
class AvrcpMediaInterfaceImpl : public MediaInterface {
 public:
  void SendKeyEvent([[maybe_unused]] uint8_t key, [[maybe_unused]] KeyState state) {}
  void SendKeyEvent(uint8_t key, KeyState state) {
    rusty::avrcp_send_key_event(key, state == KeyState::PUSHED);
  }

  void GetSongInfo([[maybe_unused]] SongInfoCallback cb) override {}

@@ -65,18 +67,21 @@ class AvrcpMediaInterfaceImpl : public MediaInterface {

class VolumeInterfaceImpl : public VolumeInterface {
 public:
  void DeviceConnected([[maybe_unused]] const RawAddress& bdaddr) override {
    rusty::avrcp_absolute_volume_enabled(false);
  void DeviceConnected(const RawAddress& bdaddr) override {
    topshim::rust::RustRawAddress addr = rusty::CopyToRustAddress(bdaddr);
    rusty::avrcp_device_connected(addr, /*absolute_volume_enabled=*/false);
  }

  void DeviceConnected([[maybe_unused]] const RawAddress& bdaddr, VolumeChangedCb cb) override {
  void DeviceConnected(const RawAddress& bdaddr, VolumeChangedCb cb) override {
    topshim::rust::RustRawAddress addr = rusty::CopyToRustAddress(bdaddr);
    volumeCb = std::move(cb);
    rusty::avrcp_absolute_volume_enabled(true);
    rusty::avrcp_device_connected(addr, /*absolute_volume_enabled=*/true);
  }

  void DeviceDisconnected([[maybe_unused]] const RawAddress& bdaddr) override {
  void DeviceDisconnected(const RawAddress& bdaddr) override {
    topshim::rust::RustRawAddress addr = rusty::CopyToRustAddress(bdaddr);
    volumeCb.Reset();
    rusty::avrcp_absolute_volume_enabled(false);
    rusty::avrcp_device_disconnected(addr);
  }

  // Set TG's (Android, ChromeOS) volume.
@@ -107,7 +112,8 @@ static A2dpIntf* g_a2dpif;
static AvrcpIntf* g_avrcpif;

static A2dpCodecConfig to_rust_codec_config(const btav_a2dp_codec_config_t& config) {
  A2dpCodecConfig rconfig = {.codec_type = static_cast<uint8_t>(config.codec_type),
  A2dpCodecConfig rconfig = {
      .codec_type = static_cast<uint8_t>(config.codec_type),
      .codec_priority = config.codec_priority,
      .sample_rate = static_cast<uint8_t>(config.sample_rate),
      .bits_per_sample = static_cast<uint8_t>(config.bits_per_sample),
+43 −6
Original line number Diff line number Diff line
use crate::btif::BluetoothInterface;
use crate::btif::{BluetoothInterface, RawAddress};
use crate::topstack::get_dispatchers;

use std::sync::{Arc, Mutex};
@@ -6,6 +6,11 @@ use topshim_macros::cb_variant;

#[cxx::bridge(namespace = bluetooth::topshim::rust)]
pub mod ffi {
    #[derive(Debug, Copy, Clone)]
    pub struct RustRawAddress {
        address: [u8; 6],
    }

    unsafe extern "C++" {
        include!("btav/btav_shim.h");

@@ -19,15 +24,33 @@ pub mod ffi {

    }
    extern "Rust" {
        fn avrcp_absolute_volume_enabled(enabled: bool);
        fn avrcp_device_connected(addr: RustRawAddress, absolute_volume_enabled: bool);
        fn avrcp_device_disconnected(addr: RustRawAddress);
        fn avrcp_absolute_volume_update(volume: u8);
        fn avrcp_send_key_event(key: u8, state: u8);
    }
}

impl Into<RawAddress> for ffi::RustRawAddress {
    fn into(self) -> RawAddress {
        RawAddress { val: self.address }
    }
}

#[derive(Debug)]
pub enum AvrcpCallbacks {
    AvrcpAbsoluteVolumeEnabled(bool),
    /// Emitted when avrcp completes connection.
    /// Params: Device address, Absolute Volume Enabled
    AvrcpDeviceConnected(RawAddress, bool),
    /// Emitted when avrcp device disconnected.
    /// Params: Device address
    AvrcpDeviceDisconnected(RawAddress),
    /// Emitted when the absolute volume of a connected AVRCP device changed
    /// Params: Volume
    AvrcpAbsoluteVolumeUpdate(u8),
    /// Emitted when received a key event from a connected AVRCP device
    /// Params: Key, Value
    AvrcpSendKeyEvent(u8, u8),
}

pub struct AvrcpCallbacksDispatcher {
@@ -38,9 +61,17 @@ type AvrcpCb = Arc<Mutex<AvrcpCallbacksDispatcher>>;

cb_variant!(
    AvrcpCb,
    avrcp_absolute_volume_enabled -> AvrcpCallbacks::AvrcpAbsoluteVolumeEnabled,
    bool, {}
);
    avrcp_device_connected -> AvrcpCallbacks::AvrcpDeviceConnected,
    ffi::RustRawAddress -> RawAddress, bool, {
        let _0 = _0.into();
});

cb_variant!(
    AvrcpCb,
    avrcp_device_disconnected -> AvrcpCallbacks::AvrcpDeviceDisconnected,
    ffi::RustRawAddress -> RawAddress, {
        let _0 = _0.into();
});

cb_variant!(
    AvrcpCb,
@@ -48,6 +79,12 @@ cb_variant!(
    u8, {}
);

cb_variant!(
    AvrcpCb,
    avrcp_send_key_event -> AvrcpCallbacks::AvrcpSendKeyEvent,
    u8, u8, {}
);

pub struct Avrcp {
    internal: cxx::UniquePtr<ffi::AvrcpIntf>,
    _is_init: bool,