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

Commit b8b6cb24 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

floss: ConnectAllEnabledProfiles now connects A2DP

Add a MediaActions enum that we can dispatch to the mainloop so that we
can send actions from one part of the stack to other parts. Use this to
allow the Bluetooth interface to call Connect/Disconnect on the Media
interface and support A2DP in ConnectAllEnabledProfiles and
DisconnectAllEnabledProfiles.

Bug: 196887265
Tag: #floss
Test: Connect to audio device from btclient
Change-Id: Ib6911767b32de0e2f4be944d415f4f99084d273b
parent b0deb0dd
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ use std::sync::Mutex;
use std::time::Instant;
use tokio::sync::mpsc::Sender;

use crate::bluetooth_media::{BluetoothMedia, IBluetoothMedia};
use crate::bluetooth_media::{BluetoothMedia, IBluetoothMedia, MediaActions};
use crate::uuid::{Profile, UuidHelper};
use crate::{BluetoothCallbackType, Message, RPCProxy};

@@ -954,6 +954,15 @@ impl IBluetooth for Bluetooth {
                                self.hh.as_ref().unwrap().connect(&mut addr.unwrap());
                            }

                            Profile::A2dpSink | Profile::A2dpSource => {
                                let txl = self.tx.clone();
                                let address = device.address.clone();
                                topstack::get_runtime().spawn(async move {
                                    let _ = txl
                                        .send(Message::Media(MediaActions::Connect(address)))
                                        .await;
                                });
                            }
                            // We don't connect most profiles
                            _ => (),
                        }
@@ -987,6 +996,16 @@ impl IBluetooth for Bluetooth {
                                self.hh.as_ref().unwrap().disconnect(&mut addr.unwrap());
                            }

                            Profile::A2dpSink | Profile::A2dpSource => {
                                let txl = self.tx.clone();
                                let address = device.address.clone();
                                topstack::get_runtime().spawn(async move {
                                    let _ = txl
                                        .send(Message::Media(MediaActions::Disconnect(address)))
                                        .await;
                                });
                            }

                            // We don't connect most profiles
                            _ => (),
                        }
+13 −0
Original line number Diff line number Diff line
@@ -63,6 +63,12 @@ pub trait IBluetoothMediaCallback {
    fn on_absolute_volume_changed(&self, volume: i32);
}

/// Actions that `BluetoothMedia` can take on behalf of the stack.
pub enum MediaActions {
    Connect(String),
    Disconnect(String),
}

pub struct BluetoothMedia {
    intf: Arc<Mutex<BluetoothInterface>>,
    initialized: bool,
@@ -152,6 +158,13 @@ impl BluetoothMedia {
        }
    }

    pub fn dispatch_media_actions(&mut self, action: MediaActions) {
        match action {
            MediaActions::Connect(address) => self.connect(address),
            MediaActions::Disconnect(address) => self.disconnect(address),
        }
    }

    fn for_all_callbacks<F: Fn(&Box<dyn IBluetoothMediaCallback + Send>)>(&self, f: F) {
        for callback in &self.callbacks {
            f(&callback.1);
+11 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ use tokio::sync::mpsc::{Receiver, Sender};

use crate::bluetooth::Bluetooth;
use crate::bluetooth_gatt::BluetoothGatt;
use crate::bluetooth_media::BluetoothMedia;
use crate::bluetooth_media::{BluetoothMedia, MediaActions};
use bt_topshim::{
    btif::BaseCallbacks,
    profiles::{
@@ -38,6 +38,7 @@ pub enum BluetoothCallbackType {

/// Message types that are sent to the stack main dispatch loop.
pub enum Message {
    // Callbacks from libbluetooth
    A2dp(A2dpCallbacks),
    Avrcp(AvrcpCallbacks),
    Base(BaseCallbacks),
@@ -45,6 +46,11 @@ pub enum Message {
    GattServer(GattServerCallbacks),
    HidHost(HHCallbacks),
    Sdp(SdpCallbacks),

    // Actions within the stack
    Media(MediaActions),

    // Client callback disconnections
    BluetoothCallbackDisconnected(u32, BluetoothCallbackType),
}

@@ -103,6 +109,10 @@ impl Stack {
                    bluetooth.lock().unwrap().dispatch_sdp_callbacks(s);
                }

                Message::Media(action) => {
                    bluetooth_media.lock().unwrap().dispatch_media_actions(action);
                }

                Message::BluetoothCallbackDisconnected(id, cb_type) => {
                    bluetooth.lock().unwrap().callback_disconnected(id, cb_type);
                }