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

Commit 2efc84d5 authored by Jesse Melhuish's avatar Jesse Melhuish Committed by Gerrit Code Review
Browse files

Merge "Floss: Propagate HFP battery info into Floss"

parents bdabe356 532da0d4
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -120,8 +120,6 @@ fn main() -> Result<(), Box<dyn Error>> {
    let intf = Arc::new(Mutex::new(get_btinterface().unwrap()));
    let bluetooth_gatt =
        Arc::new(Mutex::new(Box::new(BluetoothGatt::new(intf.clone(), tx.clone()))));
    let bluetooth_media =
        Arc::new(Mutex::new(Box::new(BluetoothMedia::new(tx.clone(), intf.clone()))));
    let battery_provider_manager =
        Arc::new(Mutex::new(Box::new(BatteryProviderManager::new(tx.clone()))));
    let battery_service = Arc::new(Mutex::new(Box::new(BatteryService::new(
@@ -133,6 +131,11 @@ fn main() -> Result<(), Box<dyn Error>> {
        battery_provider_manager.clone(),
        tx.clone(),
    ))));
    let bluetooth_media = Arc::new(Mutex::new(Box::new(BluetoothMedia::new(
        tx.clone(),
        intf.clone(),
        battery_provider_manager.clone(),
    ))));
    let bluetooth_admin = Arc::new(Mutex::new(Box::new(BluetoothAdmin::new(
        String::from(ADMIN_SETTINGS_FILE_PATH),
        tx.clone(),
+7 −2
Original line number Diff line number Diff line
@@ -102,8 +102,13 @@ impl IBatteryManager for BatteryManager {
}

impl BatterySet {
    pub fn new(address: String, source_uuid: String, source_info: String) -> Self {
        Self { address, source_uuid, source_info, batteries: vec![] }
    pub fn new(
        address: String,
        source_uuid: String,
        source_info: String,
        batteries: Vec<Battery>,
    ) -> Self {
        Self { address, source_uuid, source_info, batteries }
    }

    pub fn add_or_update_battery(&mut self, new_battery: Battery) {
+12 −3
Original line number Diff line number Diff line
@@ -242,9 +242,13 @@ impl BatteryService {
        let level = u32::from_le_bytes(level.try_into().unwrap());
        debug!("Received battery level for {}: {}", remote_address.clone(), level);
        let battery_set = self.battery_sets.entry(remote_address.clone()).or_insert_with(|| {
            BatterySet::new(remote_address.clone(), uuid::BAS.to_string(), "BAS".to_string())
            BatterySet::new(
                remote_address.clone(),
                uuid::BAS.to_string(),
                "BAS".to_string(),
                vec![Battery { percentage: level, variant: "".to_string() }],
            )
        });
        battery_set.add_or_update_battery(Battery { percentage: level, variant: "".to_string() });
        self.battery_provider_manager
            .lock()
            .unwrap()
@@ -279,7 +283,12 @@ impl BatteryService {
        // Let BatteryProviderManager know that BAS no longer has a battery for this device.
        self.battery_provider_manager.lock().unwrap().set_battery_info(
            self.battery_provider_id,
            BatterySet::new(remote_address.clone(), uuid::BAS.to_string(), "BAS".to_string()),
            BatterySet::new(
                remote_address.clone(),
                uuid::BAS.to_string(),
                "BAS".to_string(),
                vec![],
            ),
        );
        self.battery_sets.remove(&remote_address);
    }
+48 −2
Original line number Diff line number Diff line
@@ -29,6 +29,10 @@ use tokio::sync::mpsc::Sender;
use tokio::task::JoinHandle;
use tokio::time::{sleep, Duration, Instant};

use crate::battery_manager::{Battery, BatterySet};
use crate::battery_provider_manager::{
    BatteryProviderManager, IBatteryProviderCallback, IBatteryProviderManager,
};
use crate::bluetooth::{Bluetooth, BluetoothDevice, IBluetooth};
use crate::callbacks::Callbacks;
use crate::uuid;
@@ -164,6 +168,8 @@ pub enum MediaActions {

pub struct BluetoothMedia {
    intf: Arc<Mutex<BluetoothInterface>>,
    battery_provider_manager: Arc<Mutex<Box<BatteryProviderManager>>>,
    battery_provider_id: u32,
    initialized: bool,
    callbacks: Arc<Mutex<Callbacks<dyn IBluetoothMediaCallback + Send>>>,
    tx: Sender<Message>,
@@ -187,9 +193,19 @@ pub struct BluetoothMedia {
}

impl BluetoothMedia {
    pub fn new(tx: Sender<Message>, intf: Arc<Mutex<BluetoothInterface>>) -> BluetoothMedia {
    pub fn new(
        tx: Sender<Message>,
        intf: Arc<Mutex<BluetoothInterface>>,
        battery_provider_manager: Arc<Mutex<Box<BatteryProviderManager>>>,
    ) -> BluetoothMedia {
        let battery_provider_id = battery_provider_manager
            .lock()
            .unwrap()
            .register_battery_provider(Box::new(BatteryProviderCallback::new()));
        BluetoothMedia {
            intf,
            battery_provider_manager,
            battery_provider_id,
            initialized: false,
            callbacks: Arc::new(Mutex::new(Callbacks::new(
                tx.clone(),
@@ -566,6 +582,18 @@ impl BluetoothMedia {
                    callback.on_hfp_volume_changed(volume, addr.to_string());
                });
            }
            HfpCallbacks::BatteryLevelUpdate(battery_level, addr) => {
                let battery_set = BatterySet::new(
                    addr.to_string(),
                    uuid::HFP.to_string(),
                    "HFP".to_string(),
                    vec![Battery { percentage: battery_level as u32, variant: "".to_string() }],
                );
                self.battery_provider_manager
                    .lock()
                    .unwrap()
                    .set_battery_info(self.battery_provider_id, battery_set);
            }
            HfpCallbacks::CapsUpdate(wbs_supported, addr) => {
                let hfp_cap = match wbs_supported {
                    true => HfpCodecCapability::CVSD | HfpCodecCapability::MSBC,
@@ -857,7 +885,6 @@ impl IBluetoothMedia for BluetoothMedia {
        // avrcp is disabled.
        // TODO: fix b/251692015
        self.enable_profile(&Profile::AvrcpTarget);

        true
    }

@@ -1355,3 +1382,22 @@ impl IBluetoothMedia for BluetoothMedia {
        };
    }
}

struct BatteryProviderCallback {}

impl BatteryProviderCallback {
    fn new() -> Self {
        Self {}
    }
}

impl IBatteryProviderCallback for BatteryProviderCallback {
    // We do not support refreshing HFP battery information.
    fn refresh_battery_info(&self) {}
}

impl RPCProxy for BatteryProviderCallback {
    fn get_object_id(&self) -> String {
        "HFP BatteryProvider Callback".to_string()
    }
}
+21 −5
Original line number Diff line number Diff line
@@ -41,6 +41,10 @@ static void audio_state_cb(bluetooth::headset::bthf_audio_state_t state, RawAddr
static void volume_update_cb(uint8_t volume, RawAddress* addr) {
  rusty::hfp_volume_update_callback(volume, *addr);
}

static void battery_level_update_cb(uint8_t battery_level, RawAddress* addr) {
  rusty::hfp_battery_level_update_callback(battery_level, *addr);
}
}  // namespace internal

class DBusHeadsetCallbacks : public headset::Callbacks {
@@ -156,11 +160,23 @@ class DBusHeadsetCallbacks : public headset::Callbacks {
  }

  void AtBievCallback(headset::bthf_hf_ind_type_t ind_id, int ind_value, RawAddress* bd_addr) override {
    switch (ind_id) {
      case headset::bthf_hf_ind_type_t::BTHF_HF_IND_ENHANCED_DRIVER_SAFETY:
        // We don't do anything with this but we do know what it is, send OK.
        headset_->AtResponse(headset::BTHF_AT_RESPONSE_OK, 0, bd_addr);
        break;
      case headset::bthf_hf_ind_type_t::BTHF_HF_IND_BATTERY_LEVEL_STATUS:
        topshim::rust::internal::battery_level_update_cb(ind_value, bd_addr);
        headset_->AtResponse(headset::BTHF_AT_RESPONSE_OK, 0, bd_addr);
        break;
      default:
        LOG_WARN(
        "AT+BIEV=%d,%d from addr %s: Bluetooth HF Indicators is not supported.",
            "AT+BIEV indicator %i with value %i from addr %s",
            ind_id,
            ind_value,
            bd_addr->ToString().c_str());
        return;
    }
  }

  void AtBiaCallback(bool service, bool roam, bool signal, bool battery, RawAddress* bd_addr) override {
Loading